From c8d7ae1ee33e31ed15632a26c74ba9a3ea115043 Mon Sep 17 00:00:00 2001 From: zeripath Date: Wed, 22 Jan 2020 23:46:46 +0000 Subject: [PATCH] Make archive prefixing configurable with a global setting (#9943) * Allow archive prefix setting * Update copyright * Update copyright --- custom/conf/app.ini.sample | 2 + .../doc/advanced/config-cheat-sheet.en-us.md | 1 + modules/git/commit_archive.go | 44 +++++++++++++++---- modules/setting/repository.go | 2 + routers/repo/repo.go | 6 ++- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index f0829f37b3..b0aafb8b31 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -49,6 +49,8 @@ DISABLED_REPO_UNITS = ; External wiki and issue tracker can't be enabled by default as it requires additional settings. ; Disabled repo units will not be added to new repositories regardless if it is in the default list. DEFAULT_REPO_UNITS = repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki +; Prefix archive files by placing them in a directory named after the repository +PREFIX_ARCHIVE_FILES = true [repository.editor] ; List of file extensions for which lines should be wrapped in the CodeMirror editor diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 1c03fce3d0..e7b87c5272 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -68,6 +68,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. - `DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH`: **false**: Close an issue if a commit on a non default branch marks it as closed. - `ENABLE_PUSH_CREATE_USER`: **false**: Allow users to push local repositories to Gitea and have them automatically created for a user. - `ENABLE_PUSH_CREATE_ORG`: **false**: Allow users to push local repositories to Gitea and have them automatically created for an org. +- `PREFIX_ARCHIVE_FILES`: **true**: Prefix archive files by placing them in a directory named after the repository. ### Repository - Pull Request (`repository.pull-request`) diff --git a/modules/git/commit_archive.go b/modules/git/commit_archive.go index e13825a962..c7d1d06c46 100644 --- a/modules/git/commit_archive.go +++ b/modules/git/commit_archive.go @@ -1,4 +1,5 @@ // Copyright 2015 The Gogs Authors. All rights reserved. +// Copyright 2020 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -20,18 +21,43 @@ const ( TARGZ ) -// CreateArchive create archive content to the target path -func (c *Commit) CreateArchive(target string, archiveType ArchiveType) error { - var format string - switch archiveType { +// String converts an ArchiveType to string +func (a ArchiveType) String() string { + switch a { case ZIP: - format = "zip" + return "zip" case TARGZ: - format = "tar.gz" - default: - return fmt.Errorf("unknown format: %v", archiveType) + return "tar.gz" + } + return "unknown" +} + +// CreateArchiveOpts represents options for creating an archive +type CreateArchiveOpts struct { + Format ArchiveType + Prefix bool +} + +// CreateArchive create archive content to the target path +func (c *Commit) CreateArchive(target string, opts CreateArchiveOpts) error { + if opts.Format.String() == "unknown" { + return fmt.Errorf("unknown format: %v", opts.Format) } - _, err := NewCommand("archive", "--prefix="+filepath.Base(strings.TrimSuffix(c.repo.Path, ".git"))+"/", "--format="+format, "-o", target, c.ID.String()).RunInDir(c.repo.Path) + args := []string{ + "archive", + } + if opts.Prefix { + args = append(args, "--prefix="+filepath.Base(strings.TrimSuffix(c.repo.Path, ".git"))+"/") + } + + args = append(args, + "--format="+opts.Format.String(), + "-o", + target, + c.ID.String(), + ) + + _, err := NewCommand(args...).RunInDir(c.repo.Path) return err } diff --git a/modules/setting/repository.go b/modules/setting/repository.go index 807b29b2d8..8af3eaaf46 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -39,6 +39,7 @@ var ( EnablePushCreateOrg bool DisabledRepoUnits []string DefaultRepoUnits []string + PrefixArchiveFiles bool // Repository editor settings Editor struct { @@ -102,6 +103,7 @@ var ( EnablePushCreateOrg: false, DisabledRepoUnits: []string{}, DefaultRepoUnits: []string{}, + PrefixArchiveFiles: true, // Repository editor settings Editor: struct { diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 0bd9f1d56b..6b6b22f3a1 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -1,4 +1,5 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2020 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -507,7 +508,10 @@ func Download(ctx *context.Context) { archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext) if !com.IsFile(archivePath) { - if err := commit.CreateArchive(archivePath, archiveType); err != nil { + if err := commit.CreateArchive(archivePath, git.CreateArchiveOpts{ + Format: archiveType, + Prefix: setting.Repository.PrefixArchiveFiles, + }); err != nil { ctx.ServerError("Download -> CreateArchive "+archivePath, err) return }