Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make archive prefixing configurable with a global setting #9943

Merged
merged 4 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow archive prefix setting
  • Loading branch information
zeripath committed Jan 22, 2020
commit f83b19bebcd8e04d9d5d2c5d8f2e908dc9495554
2 changes: 2 additions & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down
43 changes: 34 additions & 9 deletions modules/git/commit_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,43 @@ const (
TARGZ
)

// CreateArchive create archive content to the target path
zeripath marked this conversation as resolved.
Show resolved Hide resolved
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" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, if anything, we could add another constant for UNKNOWN and compare to that rather than a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really seem worth it - the unknown is only used in that file and in those two places.

return fmt.Errorf("unknown format: %v", opts.Format)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guillep2k this is the line I was referencing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'd missed it.

}

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(),
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
"-o",
target,
c.ID.String(),
)

_, err := NewCommand("archive", "--prefix="+filepath.Base(strings.TrimSuffix(c.repo.Path, ".git"))+"/", "--format="+format, "-o", target, c.ID.String()).RunInDir(c.repo.Path)
_, err := NewCommand(args...).RunInDir(c.repo.Path)
return err
}
2 changes: 2 additions & 0 deletions modules/setting/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
EnablePushCreateOrg bool
DisabledRepoUnits []string
DefaultRepoUnits []string
PrefixArchiveFiles bool

// Repository editor settings
Editor struct {
Expand Down Expand Up @@ -102,6 +103,7 @@ var (
EnablePushCreateOrg: false,
DisabledRepoUnits: []string{},
DefaultRepoUnits: []string{},
PrefixArchiveFiles: true,

// Repository editor settings
Editor: struct {
Expand Down
5 changes: 4 additions & 1 deletion routers/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,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 {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
if err := commit.CreateArchive(archivePath, git.CreateArchiveOpts{
Format: archiveType,
Prefix: setting.Repository.PrefixArchiveFiles,
}); err != nil {
ctx.ServerError("Download -> CreateArchive "+archivePath, err)
return
}
Expand Down