From 70b32d292e659f485aaa764b72334d63db2771a9 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Mon, 1 Feb 2021 00:45:52 -0600 Subject: [PATCH 1/4] Add bundle download Signed-off-by: jolheiser --- modules/git/commit_archive.go | 4 ++++ modules/git/repo.go | 13 ++++++++++++ routers/repo/repo.go | 7 ++++++- services/archiver/archiver.go | 39 +++++++++++++++++++++++++++++------ templates/repo/home.tmpl | 1 + 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/modules/git/commit_archive.go b/modules/git/commit_archive.go index d075ba09115f..d73deb73ebb9 100644 --- a/modules/git/commit_archive.go +++ b/modules/git/commit_archive.go @@ -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 @@ -29,6 +31,8 @@ func (a ArchiveType) String() string { return "zip" case TARGZ: return "tar.gz" + case BUNDLE: + return "bundle" } return "unknown" } diff --git a/modules/git/repo.go b/modules/git/repo.go index 515899ab0498..02d1d1bdfc67 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -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 +} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index a8cfb9ad7c9b..118a83f6db89 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -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" @@ -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) diff --git a/services/archiver/archiver.go b/services/archiver/archiver.go index 359fc8b627d9..570a1a528702 100644 --- a/services/archiver/archiver.go +++ b/services/archiver/archiver.go @@ -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 @@ -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 @@ -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 diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index 4d8d28921172..74614d0c59ba 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -115,6 +115,7 @@ From c267195af9d1c9038eb96032acea2924ff7aff54 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Sun, 22 Aug 2021 22:37:57 -0500 Subject: [PATCH 2/4] Fix fmt Signed-off-by: jolheiser --- services/archiver/archiver.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/archiver/archiver.go b/services/archiver/archiver.go index 0d1897f3b120..2c521f11f31b 100644 --- a/services/archiver/archiver.go +++ b/services/archiver/archiver.go @@ -6,7 +6,6 @@ package archiver import ( - "code.gitea.io/gitea/modules/setting" "errors" "fmt" "io" @@ -19,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/storage" ) From 07130e8f4b73674699fc3eeceeee72f959c89d1a Mon Sep 17 00:00:00 2001 From: jolheiser Date: Sun, 22 Aug 2021 22:45:57 -0500 Subject: [PATCH 3/4] Fix build tags Signed-off-by: jolheiser --- modules/auth/pam/pam.go | 6 +++--- modules/auth/pam/pam_stub.go | 6 +++--- modules/graceful/manager_unix.go | 6 +++--- modules/graceful/manager_windows.go | 6 +++--- modules/graceful/net_unix.go | 6 +++--- modules/graceful/net_windows.go | 6 +++--- modules/graceful/restart_unix.go | 6 +++--- modules/options/dynamic.go | 6 +++--- modules/options/static.go | 6 +++--- modules/public/dynamic.go | 6 +++--- modules/public/static.go | 6 +++--- modules/templates/dynamic.go | 6 +++--- modules/templates/static.go | 6 +++--- routers/private/manager_unix.go | 6 +++--- routers/private/manager_windows.go | 6 +++--- 15 files changed, 45 insertions(+), 45 deletions(-) diff --git a/modules/auth/pam/pam.go b/modules/auth/pam/pam.go index 6906a9da8970..73ecae0c2c29 100644 --- a/modules/auth/pam/pam.go +++ b/modules/auth/pam/pam.go @@ -1,10 +1,10 @@ -//go:build pam -// +build pam - // Copyright 2014 The Gogs Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. +//go:build pam +// +build pam + package pam import ( diff --git a/modules/auth/pam/pam_stub.go b/modules/auth/pam/pam_stub.go index a84445639aab..815ccf2b0e2d 100644 --- a/modules/auth/pam/pam_stub.go +++ b/modules/auth/pam/pam_stub.go @@ -1,10 +1,10 @@ -//go:build !pam -// +build !pam - // Copyright 2014 The Gogs Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. +//go:build !pam +// +build !pam + package pam import ( diff --git a/modules/graceful/manager_unix.go b/modules/graceful/manager_unix.go index 41ab521b7231..fcbb16a3bb96 100644 --- a/modules/graceful/manager_unix.go +++ b/modules/graceful/manager_unix.go @@ -1,10 +1,10 @@ -//go:build !windows -// +build !windows - // Copyright 2019 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. +//go:build !windows +// +build !windows + package graceful import ( diff --git a/modules/graceful/manager_windows.go b/modules/graceful/manager_windows.go index 3388ccf881cc..e5f5541ed3c3 100644 --- a/modules/graceful/manager_windows.go +++ b/modules/graceful/manager_windows.go @@ -1,11 +1,11 @@ -//go:build windows -// +build windows - // Copyright 2019 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. // This code is heavily inspired by the archived gofacebook/gracenet/net.go handler +//go:build windows +// +build windows + package graceful import ( diff --git a/modules/graceful/net_unix.go b/modules/graceful/net_unix.go index e8f96aa80d1d..6ffa8150cc1a 100644 --- a/modules/graceful/net_unix.go +++ b/modules/graceful/net_unix.go @@ -1,11 +1,11 @@ -//go:build !windows -// +build !windows - // Copyright 2019 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. // This code is heavily inspired by the archived gofacebook/gracenet/net.go handler +//go:build !windows +// +build !windows + package graceful import ( diff --git a/modules/graceful/net_windows.go b/modules/graceful/net_windows.go index 75cd81dac447..35b7a9d1feae 100644 --- a/modules/graceful/net_windows.go +++ b/modules/graceful/net_windows.go @@ -1,11 +1,11 @@ -//go:build windows -// +build windows - // Copyright 2019 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. // This code is heavily inspired by the archived gofacebook/gracenet/net.go handler +//go:build windows +// +build windows + package graceful import "net" diff --git a/modules/graceful/restart_unix.go b/modules/graceful/restart_unix.go index 1bd120dcf6a2..392ed60cb381 100644 --- a/modules/graceful/restart_unix.go +++ b/modules/graceful/restart_unix.go @@ -1,11 +1,11 @@ -//go:build !windows -// +build !windows - // Copyright 2019 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. // This code is heavily inspired by the archived gofacebook/gracenet/net.go handler +//go:build !windows +// +build !windows + package graceful import ( diff --git a/modules/options/dynamic.go b/modules/options/dynamic.go index 32d3f52eaa35..13fa5d6aa7eb 100644 --- a/modules/options/dynamic.go +++ b/modules/options/dynamic.go @@ -1,10 +1,10 @@ -//go:build !bindata -// +build !bindata - // Copyright 2016 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. +//go:build !bindata +// +build !bindata + package options import ( diff --git a/modules/options/static.go b/modules/options/static.go index e6a4454b7deb..49e8445cd595 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -1,10 +1,10 @@ -//go:build bindata -// +build bindata - // Copyright 2016 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. +//go:build bindata +// +build bindata + package options import ( diff --git a/modules/public/dynamic.go b/modules/public/dynamic.go index c25bab979ff9..955c01e51031 100644 --- a/modules/public/dynamic.go +++ b/modules/public/dynamic.go @@ -1,10 +1,10 @@ -//go:build !bindata -// +build !bindata - // Copyright 2016 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. +//go:build !bindata +// +build !bindata + package public import ( diff --git a/modules/public/static.go b/modules/public/static.go index 1e5853961f0b..6994ed650857 100644 --- a/modules/public/static.go +++ b/modules/public/static.go @@ -1,10 +1,10 @@ -//go:build bindata -// +build bindata - // Copyright 2016 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. +//go:build bindata +// +build bindata + package public import ( diff --git a/modules/templates/dynamic.go b/modules/templates/dynamic.go index 8e083408d01c..4732fce421fa 100644 --- a/modules/templates/dynamic.go +++ b/modules/templates/dynamic.go @@ -1,10 +1,10 @@ -//go:build !bindata -// +build !bindata - // Copyright 2016 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. +//go:build !bindata +// +build !bindata + package templates import ( diff --git a/modules/templates/static.go b/modules/templates/static.go index 27449f926db6..ee20b2af3b3a 100644 --- a/modules/templates/static.go +++ b/modules/templates/static.go @@ -1,10 +1,10 @@ -//go:build bindata -// +build bindata - // Copyright 2016 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. +//go:build bindata +// +build bindata + package templates import ( diff --git a/routers/private/manager_unix.go b/routers/private/manager_unix.go index 84e6e60126cd..1738c06a05fb 100644 --- a/routers/private/manager_unix.go +++ b/routers/private/manager_unix.go @@ -1,10 +1,10 @@ -//go:build !windows -// +build !windows - // 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. +//go:build !windows +// +build !windows + package private import ( diff --git a/routers/private/manager_windows.go b/routers/private/manager_windows.go index 312a0ef22016..a8a477313fb0 100644 --- a/routers/private/manager_windows.go +++ b/routers/private/manager_windows.go @@ -1,10 +1,10 @@ -//go:build windows -// +build windows - // 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. +//go:build windows +// +build windows + package private import ( From fa2fe1f9c1a79a751e504daf01f3dd08575aaf15 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Mon, 23 Aug 2021 22:00:02 -0500 Subject: [PATCH 4/4] Download specific commit Signed-off-by: jolheiser --- modules/git/repo.go | 4 ++-- services/archiver/archiver.go | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/git/repo.go b/modules/git/repo.go index 26178640ffb0..e7d42dacb165 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -418,7 +418,7 @@ func GetDivergingCommits(repoPath string, baseBranch string, targetBranch string } // CreateBundle create bundle content to the target path -func (repo *Repository) CreateBundle(ctx context.Context, out io.Writer) error { +func (repo *Repository) CreateBundle(ctx context.Context, commit string, out io.Writer) error { tmp, err := os.MkdirTemp(os.TempDir(), "gitea-bundle") if err != nil { return err @@ -430,7 +430,7 @@ func (repo *Repository) CreateBundle(ctx context.Context, out io.Writer) error { "bundle", "create", tmpFile, - "--all", + commit, } _, err = NewCommandContext(ctx, args...).RunInDir(repo.Path) if err != nil { diff --git a/services/archiver/archiver.go b/services/archiver/archiver.go index 2c521f11f31b..7ae0a94d7eca 100644 --- a/services/archiver/archiver.go +++ b/services/archiver/archiver.go @@ -171,6 +171,7 @@ func doArchive(r *ArchiveRequest) (*models.RepoArchiver, error) { if archiver.Type == git.BUNDLE { err = gitRepo.CreateBundle( graceful.GetManager().ShutdownContext(), + archiver.CommitID, w, ) } else {