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

Add bundle download for repository #14538

Merged
merged 6 commits into from
Aug 24, 2021
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
4 changes: 4 additions & 0 deletions modules/git/commit_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
ZIP ArchiveType = iota + 1
// TARGZ tar gz archive type
TARGZ
// BUNDLE bundle archive type
BUNDLE
)

// String converts an ArchiveType to string
Expand All @@ -29,6 +31,8 @@ func (a ArchiveType) String() string {
return "zip"
case TARGZ:
return "tar.gz"
case BUNDLE:
return "bundle"
}
return "unknown"
}
Expand Down
13 changes: 13 additions & 0 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,16 @@ func GetDivergingCommits(repoPath string, baseBranch string, targetBranch string

return DivergeObject{ahead, behind}, nil
}

// CreateBundle create bundle content to the target path
func (repo *Repository) CreateBundle(ctx context.Context, target string) error {
args := []string{
"bundle",
"create",
target,
"--all",
}

_, err := NewCommandContext(ctx, args...).RunInDir(repo.Path)
return err
}
7 changes: 6 additions & 1 deletion routers/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
auth "code.gitea.io/gitea/modules/forms"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web"
Expand Down Expand Up @@ -335,7 +336,11 @@ func Download(ctx *context.Context) {
return
}

downloadName := ctx.Repo.Repository.Name + "-" + aReq.GetArchiveName()
downloadName := aReq.GetArchiveName()
if aReq.ArchiveType() != git.BUNDLE {
downloadName = fmt.Sprintf("%s-%s", ctx.Repo.Repository.Name, downloadName)
}

complete := aReq.IsComplete()
if !complete {
aReq = archiver_service.ArchiveRepository(aReq)
Expand Down
39 changes: 33 additions & 6 deletions services/archiver/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ var archiveQueueMutex *sync.Mutex
var archiveQueueStartCond *sync.Cond
var archiveQueueReleaseCond *sync.Cond

// ArchiveType exposes the underlying archive type without allowing it to be modified
func (aReq *ArchiveRequest) ArchiveType() git.ArchiveType {
return aReq.archiveType
}

// GetArchivePath returns the path from which we can serve this archive.
func (aReq *ArchiveRequest) GetArchivePath() string {
return aReq.archivePath
Expand Down Expand Up @@ -131,6 +136,10 @@ func DeriveRequestFrom(ctx *context.Context, uri string) *ArchiveRequest {
r.ext = ".tar.gz"
r.archivePath = path.Join(r.repo.Path, "archives/targz")
r.archiveType = git.TARGZ
case strings.HasSuffix(uri, ".bundle"):
r.ext = ".bundle"
r.archivePath = path.Join(r.repo.Path, "archives/bundle")
r.archiveType = git.BUNDLE
default:
log.Trace("Unknown format: %s", uri)
return nil
Expand Down Expand Up @@ -225,12 +234,30 @@ func doArchive(r *ArchiveRequest) {
os.Remove(tmpArchive.Name())
}()

if err = r.commit.CreateArchive(graceful.GetManager().ShutdownContext(), tmpArchive.Name(), git.CreateArchiveOpts{
Format: r.archiveType,
Prefix: setting.Repository.PrefixArchiveFiles,
}); err != nil {
log.Error("Download -> CreateArchive "+tmpArchive.Name(), err)
return
if r.archiveType == git.BUNDLE {
err = r.repo.CreateBundle(graceful.GetManager().ShutdownContext(), tmpArchive.Name())
if err != nil {
log.Error("Download -> CreateBundle "+tmpArchive.Name(), err)
return
}
// Bundle needed to be closed to finish writing, Sync did not seem to work...
if err := tmpArchive.Close(); err != nil {
log.Error("Download -> CreateBundle -> Close "+tmpArchive.Name(), err)
return
}
if tmpArchive, err = os.Open(tmpArchive.Name()); err != nil {
log.Error("Download -> CreateBundle -> Open ", err)
return
}
} else {
err = r.commit.CreateArchive(graceful.GetManager().ShutdownContext(), tmpArchive.Name(), git.CreateArchiveOpts{
Format: r.archiveType,
Prefix: setting.Repository.PrefixArchiveFiles,
})
if err != nil {
log.Error("Download -> CreateArchive "+tmpArchive.Name(), err)
return
}
}

// Now we copy it into place
Expand Down
1 change: 1 addition & 0 deletions templates/repo/home.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<div class="menu">
<a class="item archive-link" data-url="{{$.RepoLink}}/archive/{{EscapePound $.BranchName}}.zip">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
<a class="item archive-link" data-url="{{$.RepoLink}}/archive/{{EscapePound $.BranchName}}.tar.gz">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
<a class="item archive-link" data-url="{{$.RepoLink}}/archive/{{EscapePound $.BranchName}}.bundle">{{svg "octicon-package"}}&nbsp;BUNDLE</a>
</div>
</div>
</div>
Expand Down