From 67605d48fb46f0db0d869e2b535f0518aa5f4d64 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 28 Dec 2023 14:08:41 +0800 Subject: [PATCH 1/9] Upgrade go git --- go.mod | 3 +-- go.sum | 12 ++++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 54a56086701c3..868118e2757ee 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,7 @@ require ( github.com/go-enry/go-enry/v2 v2.8.6 github.com/go-fed/httpsig v1.1.1-0.20201223112313-55836744818e github.com/go-git/go-billy/v5 v5.5.0 - github.com/go-git/go-git/v5 v5.9.0 + github.com/go-git/go-git/v5 v5.11.0 github.com/go-ldap/ldap/v3 v3.4.6 github.com/go-sql-driver/mysql v1.7.1 github.com/go-swagger/go-swagger v0.30.5 @@ -138,7 +138,6 @@ require ( github.com/Microsoft/go-winio v0.6.1 // indirect github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c // indirect github.com/RoaringBitmap/roaring v1.7.0 // indirect - github.com/acomagu/bufpipe v1.0.4 // indirect github.com/andybalholm/brotli v1.0.6 // indirect github.com/andybalholm/cascadia v1.3.2 // indirect github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect diff --git a/go.sum b/go.sum index 348fc86da4142..bf6c010e2b443 100644 --- a/go.sum +++ b/go.sum @@ -106,8 +106,6 @@ github.com/RoaringBitmap/roaring v0.4.23/go.mod h1:D0gp8kJQgE1A4LQ5wFLggQEyvDi06 github.com/RoaringBitmap/roaring v0.7.1/go.mod h1:jdT9ykXwHFNdJbEtxePexlFYH9LXucApeS0/+/g+p1I= github.com/RoaringBitmap/roaring v1.7.0 h1:OZF303tJCER1Tj3x+aArx/S5X7hrT186ri6JjrGvG68= github.com/RoaringBitmap/roaring v1.7.0/go.mod h1:6AXUsoIEzDTFFQCe1RbGA6uFONMhvejWj5rqITANK90= -github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ= -github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink= github.com/alecthomas/assert/v2 v2.2.1/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/chroma/v2 v2.2.0/go.mod h1:vf4zrexSH54oEjJ7EdB65tGNHmH3pGZmVkgTP5RHvAs= @@ -352,10 +350,10 @@ github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66D github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= -github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20230305113008-0c11038e723f h1:Pz0DHeFij3XFhoBRGUDPzSJ+w2UcK5/0JvF8DRI58r8= -github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20230305113008-0c11038e723f/go.mod h1:8LHG1a3SRW71ettAD/jW13h8c6AqjVSeL11RAdgaqpo= -github.com/go-git/go-git/v5 v5.9.0 h1:cD9SFA7sHVRdJ7AYck1ZaAa/yeuBvGPxwXDL8cxrObY= -github.com/go-git/go-git/v5 v5.9.0/go.mod h1:RKIqga24sWdMGZF+1Ekv9kylsDz6LzdTSI2s/OsZWE0= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= +github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= +github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -697,8 +695,6 @@ github.com/markbates/going v1.0.3 h1:mY45T5TvW+Xz5A6jY7lf4+NLg9D8+iuStIHyR7M8qsE github.com/markbates/going v1.0.3/go.mod h1:fQiT6v6yQar9UD6bd/D4Z5Afbk9J6BBVBtLiyY4gp2o= github.com/markbates/goth v1.78.0 h1:7VEIFDycJp9deyVv3YraGBPdD0ZYQW93Y3Aw1eVP3BY= github.com/markbates/goth v1.78.0/go.mod h1:X6xdNgpapSENS0O35iTBBcMHoJDQDfI9bJl+APCkYMc= -github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= -github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= From 9655e24882a01a2c01ccd11a688d4ed8c07fdb0f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 28 Dec 2023 17:59:38 +0800 Subject: [PATCH 2/9] Fix gogit index --- modules/git/repo_commitgraph_gogit.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/git/repo_commitgraph_gogit.go b/modules/git/repo_commitgraph_gogit.go index c2d741daae259..3a7e4c8a746a2 100644 --- a/modules/git/repo_commitgraph_gogit.go +++ b/modules/git/repo_commitgraph_gogit.go @@ -12,7 +12,7 @@ import ( gitealog "code.gitea.io/gitea/modules/log" - "github.com/go-git/go-git/v5/plumbing/format/commitgraph" + commitgraph "github.com/go-git/go-git/v5/plumbing/format/commitgraph/v2" cgobject "github.com/go-git/go-git/v5/plumbing/object/commitgraph" ) @@ -22,8 +22,7 @@ func (r *Repository) CommitNodeIndex() (cgobject.CommitNodeIndex, *os.File) { file, err := os.Open(indexPath) if err == nil { - var index commitgraph.Index // TODO: in newer go-git, it might need to use "github.com/go-git/go-git/v5/plumbing/format/commitgraph/v2" package to compile - index, err = commitgraph.OpenFileIndex(file) + index, err := commitgraph.OpenFileIndex(file) if err == nil { return cgobject.NewGraphCommitNodeIndex(index, r.gogitRepo.Storer), file } From 72de825a3b85f7a2927880d4a28e3da898d8fc42 Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Mon, 15 Jan 2024 17:00:44 -0500 Subject: [PATCH 3/9] Use alternate fs required by gogit v5.11.0 --- modules/git/repo_base_gogit.go | 14 +++++++++++++- services/repository/files/temp_repo.go | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_base_gogit.go b/modules/git/repo_base_gogit.go index d0b8e79368420..22560a69c621c 100644 --- a/modules/git/repo_base_gogit.go +++ b/modules/git/repo_base_gogit.go @@ -43,6 +43,11 @@ func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) { // OpenRepository opens the repository at the given path within the context.Context func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { + return OpenRepositoryWithAlternates(ctx, repoPath, "") +} + +// OpenRepository opens the repository at the given path within the context.Context +func OpenRepositoryWithAlternates(ctx context.Context, repoPath string, altPath string) (*Repository, error) { repoPath, err := filepath.Abs(repoPath) if err != nil { return nil, err @@ -58,7 +63,14 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { return nil, err } } - storage := filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), filesystem.Options{KeepDescriptors: true, LargeObjectThreshold: setting.Git.LargeObjectThreshold}) + options := filesystem.Options{ + KeepDescriptors: true, + LargeObjectThreshold: setting.Git.LargeObjectThreshold, + } + if altPath != "" { + options.AlternatesFS = osfs.New(altPath) + } + storage := filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), options) gogitRepo, err := gogit.Open(storage, fs) if err != nil { return nil, err diff --git a/services/repository/files/temp_repo.go b/services/repository/files/temp_repo.go index 6a0b7b675c81e..ab009bb8b8374 100644 --- a/services/repository/files/temp_repo.go +++ b/services/repository/files/temp_repo.go @@ -68,7 +68,7 @@ func (t *TemporaryUploadRepository) Clone(branch string) error { } return fmt.Errorf("Clone: %w %s", err, stderr) } - gitRepo, err := git.OpenRepository(t.ctx, t.basePath) + gitRepo, err := git.OpenRepositoryWithAlternates(t.ctx, t.basePath, t.repo.RepoPath()) if err != nil { return err } From 5eb098aedb413bb8cab10af4486d7a6eaf29c12f Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Mon, 15 Jan 2024 17:09:18 -0500 Subject: [PATCH 4/9] Update comments --- modules/git/repo_base_gogit.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_base_gogit.go b/modules/git/repo_base_gogit.go index 22560a69c621c..6ea34bef0f657 100644 --- a/modules/git/repo_base_gogit.go +++ b/modules/git/repo_base_gogit.go @@ -41,12 +41,13 @@ func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) { return OpenRepository(DefaultContext, repoPath) } -// OpenRepository opens the repository at the given path within the context.Context +// OpenRepository opens the repository at the given path within the context.Context. func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { return OpenRepositoryWithAlternates(ctx, repoPath, "") } -// OpenRepository opens the repository at the given path within the context.Context +// OpenRepositoryWithAlternates opens the repository at the given path within the context.Context, with a provided alternate path. +// altPath is ignored if empty. func OpenRepositoryWithAlternates(ctx context.Context, repoPath string, altPath string) (*Repository, error) { repoPath, err := filepath.Abs(repoPath) if err != nil { From 013d92bd5d1e611ecaf1c8a1facf255190f37e5f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 16 Jan 2024 09:56:43 +0800 Subject: [PATCH 5/9] Fix lint --- services/repository/files/temp_repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/repository/files/temp_repo.go b/services/repository/files/temp_repo.go index ab009bb8b8374..6a0b7b675c81e 100644 --- a/services/repository/files/temp_repo.go +++ b/services/repository/files/temp_repo.go @@ -68,7 +68,7 @@ func (t *TemporaryUploadRepository) Clone(branch string) error { } return fmt.Errorf("Clone: %w %s", err, stderr) } - gitRepo, err := git.OpenRepositoryWithAlternates(t.ctx, t.basePath, t.repo.RepoPath()) + gitRepo, err := git.OpenRepository(t.ctx, t.basePath) if err != nil { return err } From 6fb09b0aff0b00564ceef681c757a4d026d8a02d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 16 Jan 2024 12:21:59 +0800 Subject: [PATCH 6/9] Fix test --- modules/git/repo_base_gogit.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_base_gogit.go b/modules/git/repo_base_gogit.go index 6ea34bef0f657..eb6bbfe95dc52 100644 --- a/modules/git/repo_base_gogit.go +++ b/modules/git/repo_base_gogit.go @@ -68,9 +68,11 @@ func OpenRepositoryWithAlternates(ctx context.Context, repoPath string, altPath KeepDescriptors: true, LargeObjectThreshold: setting.Git.LargeObjectThreshold, } - if altPath != "" { - options.AlternatesFS = osfs.New(altPath) + if altPath == "" { + altPath = repoPath } + options.AlternatesFS = osfs.New(altPath) + storage := filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), options) gogitRepo, err := gogit.Open(storage, fs) if err != nil { From 99716af0b416c4e730a95b64c9859bb2efde4a5a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 16 Jan 2024 13:53:10 +0800 Subject: [PATCH 7/9] Fix open repository --- modules/git/repo_base_gogit.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/modules/git/repo_base_gogit.go b/modules/git/repo_base_gogit.go index eb6bbfe95dc52..c71adedfe6061 100644 --- a/modules/git/repo_base_gogit.go +++ b/modules/git/repo_base_gogit.go @@ -7,8 +7,11 @@ package git import ( + "bufio" + "bytes" "context" "errors" + "os" "path/filepath" gitealog "code.gitea.io/gitea/modules/log" @@ -43,12 +46,6 @@ func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) { // OpenRepository opens the repository at the given path within the context.Context. func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { - return OpenRepositoryWithAlternates(ctx, repoPath, "") -} - -// OpenRepositoryWithAlternates opens the repository at the given path within the context.Context, with a provided alternate path. -// altPath is ignored if empty. -func OpenRepositoryWithAlternates(ctx context.Context, repoPath string, altPath string) (*Repository, error) { repoPath, err := filepath.Abs(repoPath) if err != nil { return nil, err @@ -68,10 +65,22 @@ func OpenRepositoryWithAlternates(ctx context.Context, repoPath string, altPath KeepDescriptors: true, LargeObjectThreshold: setting.Git.LargeObjectThreshold, } - if altPath == "" { - altPath = repoPath + + // attach possible alternates + bs, err := os.ReadFile(filepath.Join(repoPath, "objects", "info", "alternates")) + if err != nil { + if !errors.Is(err, os.ErrNotExist) { + return nil, err + } + } else { + scanner := bufio.NewScanner(bytes.NewReader(bs)) + for scanner.Scan() { + // FIXME: we just use the first alternate because it seems go-git don't support multiple alternates yet + altPath := scanner.Text() + options.AlternatesFS = osfs.New(altPath) + break + } } - options.AlternatesFS = osfs.New(altPath) storage := filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), options) gogitRepo, err := gogit.Open(storage, fs) From 67aa03c140cbd64909393f63533d45db4d5a500a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 16 Jan 2024 14:24:52 +0800 Subject: [PATCH 8/9] Fix possible bug --- modules/git/repo_base_gogit.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/git/repo_base_gogit.go b/modules/git/repo_base_gogit.go index c71adedfe6061..34ebfc9f81fb9 100644 --- a/modules/git/repo_base_gogit.go +++ b/modules/git/repo_base_gogit.go @@ -13,6 +13,7 @@ import ( "errors" "os" "path/filepath" + "strings" gitealog "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -77,6 +78,9 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { for scanner.Scan() { // FIXME: we just use the first alternate because it seems go-git don't support multiple alternates yet altPath := scanner.Text() + if strings.HasSuffix(altPath, "objects") { + altPath = filepath.Dir(altPath) + } options.AlternatesFS = osfs.New(altPath) break } From 4f26e656efb2d05c692539d0797471747e2ad748 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 17 Jan 2024 10:14:03 +0800 Subject: [PATCH 9/9] Use osfs.WithBoundOS parameter --- modules/git/repo_base_gogit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/repo_base_gogit.go b/modules/git/repo_base_gogit.go index 34ebfc9f81fb9..831ec3cd2c46a 100644 --- a/modules/git/repo_base_gogit.go +++ b/modules/git/repo_base_gogit.go @@ -81,7 +81,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { if strings.HasSuffix(altPath, "objects") { altPath = filepath.Dir(altPath) } - options.AlternatesFS = osfs.New(altPath) + options.AlternatesFS = osfs.New(altPath, osfs.WithBoundOS()) break } }