Skip to content

Commit c8d7ae1

Browse files
authored
Make archive prefixing configurable with a global setting (#9943)
* Allow archive prefix setting * Update copyright * Update copyright
1 parent 608cd58 commit c8d7ae1

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

custom/conf/app.ini.sample

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ DISABLED_REPO_UNITS =
4949
; External wiki and issue tracker can't be enabled by default as it requires additional settings.
5050
; Disabled repo units will not be added to new repositories regardless if it is in the default list.
5151
DEFAULT_REPO_UNITS = repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki
52+
; Prefix archive files by placing them in a directory named after the repository
53+
PREFIX_ARCHIVE_FILES = true
5254

5355
[repository.editor]
5456
; List of file extensions for which lines should be wrapped in the CodeMirror editor

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
6868
- `DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH`: **false**: Close an issue if a commit on a non default branch marks it as closed.
6969
- `ENABLE_PUSH_CREATE_USER`: **false**: Allow users to push local repositories to Gitea and have them automatically created for a user.
7070
- `ENABLE_PUSH_CREATE_ORG`: **false**: Allow users to push local repositories to Gitea and have them automatically created for an org.
71+
- `PREFIX_ARCHIVE_FILES`: **true**: Prefix archive files by placing them in a directory named after the repository.
7172

7273
### Repository - Pull Request (`repository.pull-request`)
7374

modules/git/commit_archive.go

+35-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Copyright 2020 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -20,18 +21,43 @@ const (
2021
TARGZ
2122
)
2223

23-
// CreateArchive create archive content to the target path
24-
func (c *Commit) CreateArchive(target string, archiveType ArchiveType) error {
25-
var format string
26-
switch archiveType {
24+
// String converts an ArchiveType to string
25+
func (a ArchiveType) String() string {
26+
switch a {
2727
case ZIP:
28-
format = "zip"
28+
return "zip"
2929
case TARGZ:
30-
format = "tar.gz"
31-
default:
32-
return fmt.Errorf("unknown format: %v", archiveType)
30+
return "tar.gz"
3331
}
32+
return "unknown"
33+
}
34+
35+
// CreateArchiveOpts represents options for creating an archive
36+
type CreateArchiveOpts struct {
37+
Format ArchiveType
38+
Prefix bool
39+
}
40+
41+
// CreateArchive create archive content to the target path
42+
func (c *Commit) CreateArchive(target string, opts CreateArchiveOpts) error {
43+
if opts.Format.String() == "unknown" {
44+
return fmt.Errorf("unknown format: %v", opts.Format)
45+
}
46+
47+
args := []string{
48+
"archive",
49+
}
50+
if opts.Prefix {
51+
args = append(args, "--prefix="+filepath.Base(strings.TrimSuffix(c.repo.Path, ".git"))+"/")
52+
}
53+
54+
args = append(args,
55+
"--format="+opts.Format.String(),
56+
"-o",
57+
target,
58+
c.ID.String(),
59+
)
3460

35-
_, err := NewCommand("archive", "--prefix="+filepath.Base(strings.TrimSuffix(c.repo.Path, ".git"))+"/", "--format="+format, "-o", target, c.ID.String()).RunInDir(c.repo.Path)
61+
_, err := NewCommand(args...).RunInDir(c.repo.Path)
3662
return err
3763
}

modules/setting/repository.go

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var (
3939
EnablePushCreateOrg bool
4040
DisabledRepoUnits []string
4141
DefaultRepoUnits []string
42+
PrefixArchiveFiles bool
4243

4344
// Repository editor settings
4445
Editor struct {
@@ -102,6 +103,7 @@ var (
102103
EnablePushCreateOrg: false,
103104
DisabledRepoUnits: []string{},
104105
DefaultRepoUnits: []string{},
106+
PrefixArchiveFiles: true,
105107

106108
// Repository editor settings
107109
Editor: struct {

routers/repo/repo.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2020 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -507,7 +508,10 @@ func Download(ctx *context.Context) {
507508

508509
archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext)
509510
if !com.IsFile(archivePath) {
510-
if err := commit.CreateArchive(archivePath, archiveType); err != nil {
511+
if err := commit.CreateArchive(archivePath, git.CreateArchiveOpts{
512+
Format: archiveType,
513+
Prefix: setting.Repository.PrefixArchiveFiles,
514+
}); err != nil {
511515
ctx.ServerError("Download -> CreateArchive "+archivePath, err)
512516
return
513517
}

0 commit comments

Comments
 (0)