From 90f82394487479c8d5469c21d1c9fef4c6b16407 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 14 Jun 2025 19:50:58 +0200 Subject: [PATCH] fix: make test suite run on older git version (#8188) Ref: forgejo/forgejo#8140, forgejo/forgejo#8144 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8188 Reviewed-by: Michael Kriese Co-authored-by: Gusted Co-committed-by: Gusted --- modules/git/git.go | 2 + modules/git/grep.go | 3 +- modules/git/grep_test.go | 87 ++++++++++++----------- modules/git/repo_attribute_test.go | 10 ++- tests/integration/patch_status_test.go | 7 +- tests/integration/repo_signed_tag_test.go | 4 ++ tests/integration/signing_git_test.go | 4 ++ 7 files changed, 71 insertions(+), 46 deletions(-) diff --git a/modules/git/git.go b/modules/git/git.go index 743ff59ddd..1dfd0b5134 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -38,6 +38,7 @@ var ( InvertedGitFlushEnv bool // 2.43.1 SupportCheckAttrOnBare bool // >= 2.40 SupportGitMergeTree bool // >= 2.38 + SupportGrepMaxCount bool // >= 2.38 HasSSHExecutable bool @@ -191,6 +192,7 @@ func InitFull(ctx context.Context) (err error) { InvertedGitFlushEnv = CheckGitVersionEqual("2.43.1") == nil SupportGitMergeTree = CheckGitVersionAtLeast("2.38") == nil + SupportGrepMaxCount = CheckGitVersionAtLeast("2.38") == nil if setting.LFS.StartServer { if CheckGitVersionAtLeast("2.1.2") != nil { diff --git a/modules/git/grep.go b/modules/git/grep.go index 117b09fc83..3703b13660 100644 --- a/modules/git/grep.go +++ b/modules/git/grep.go @@ -98,8 +98,7 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber)) - // --max-count requires at least git 2.38 - if CheckGitVersionAtLeast("2.38.0") == nil { + if SupportGrepMaxCount { cmd.AddOptionValues("--max-count", fmt.Sprint(opts.MatchesPerFile)) } else { log.Warn("git-grep: --max-count requires at least git 2.38") diff --git a/modules/git/grep_test.go b/modules/git/grep_test.go index 534468e268..83ddb766af 100644 --- a/modules/git/grep_test.go +++ b/modules/git/grep_test.go @@ -59,48 +59,55 @@ func TestGrepSearch(t *testing.T) { }, }, res) - res, err = GrepSearch(t.Context(), repo, "world", GrepOptions{MatchesPerFile: 1}) - require.NoError(t, err) - assert.Equal(t, []*GrepResult{ - { - Filename: "i-am-a-python.p", - LineNumbers: []int{1}, - LineCodes: []string{"## This is a simple file to do a hello world"}, - HighlightedRanges: [][3]int{{0, 39, 44}}, - }, - { - Filename: "java-hello/main.java", - LineNumbers: []int{1}, - LineCodes: []string{"public class HelloWorld"}, - HighlightedRanges: [][3]int{{0, 18, 23}}, - }, - { - Filename: "main.vendor.java", - LineNumbers: []int{1}, - LineCodes: []string{"public class HelloWorld"}, - HighlightedRanges: [][3]int{{0, 18, 23}}, - }, - { - Filename: "python-hello/hello.py", - LineNumbers: []int{1}, - LineCodes: []string{"## This is a simple file to do a hello world"}, - HighlightedRanges: [][3]int{{0, 39, 44}}, - }, - }, res) + t.Run("Max count", func(t *testing.T) { + if !SupportGrepMaxCount { + t.Skip("Skipping, git grep --max-count is not supported") + return + } - res, err = GrepSearch(t.Context(), repo, "world", GrepOptions{ - MatchesPerFile: 1, - Filename: "java-hello/", + res, err = GrepSearch(t.Context(), repo, "world", GrepOptions{MatchesPerFile: 1}) + require.NoError(t, err) + assert.Equal(t, []*GrepResult{ + { + Filename: "i-am-a-python.p", + LineNumbers: []int{1}, + LineCodes: []string{"## This is a simple file to do a hello world"}, + HighlightedRanges: [][3]int{{0, 39, 44}}, + }, + { + Filename: "java-hello/main.java", + LineNumbers: []int{1}, + LineCodes: []string{"public class HelloWorld"}, + HighlightedRanges: [][3]int{{0, 18, 23}}, + }, + { + Filename: "main.vendor.java", + LineNumbers: []int{1}, + LineCodes: []string{"public class HelloWorld"}, + HighlightedRanges: [][3]int{{0, 18, 23}}, + }, + { + Filename: "python-hello/hello.py", + LineNumbers: []int{1}, + LineCodes: []string{"## This is a simple file to do a hello world"}, + HighlightedRanges: [][3]int{{0, 39, 44}}, + }, + }, res) + + res, err = GrepSearch(t.Context(), repo, "world", GrepOptions{ + MatchesPerFile: 1, + Filename: "java-hello/", + }) + require.NoError(t, err) + assert.Equal(t, []*GrepResult{ + { + Filename: "java-hello/main.java", + LineNumbers: []int{1}, + LineCodes: []string{"public class HelloWorld"}, + HighlightedRanges: [][3]int{{0, 18, 23}}, + }, + }, res) }) - require.NoError(t, err) - assert.Equal(t, []*GrepResult{ - { - Filename: "java-hello/main.java", - LineNumbers: []int{1}, - LineCodes: []string{"public class HelloWorld"}, - HighlightedRanges: [][3]int{{0, 18, 23}}, - }, - }, res) res, err = GrepSearch(t.Context(), repo, "no-such-content", GrepOptions{}) require.NoError(t, err) diff --git a/modules/git/repo_attribute_test.go b/modules/git/repo_attribute_test.go index 0006559709..c69382e245 100644 --- a/modules/git/repo_attribute_test.go +++ b/modules/git/repo_attribute_test.go @@ -251,10 +251,14 @@ func TestGitAttributeCheckerError(t *testing.T) { cancel() ac, err := gitRepo.GitAttributeChecker("8fee858da5796dfb37704761701bb8e800ad9ef3", "linguist-language") - require.NoError(t, err) + if SupportCheckAttrOnBare { + require.NoError(t, err) - _, err = ac.CheckPath("i-am-a-python.p") - require.Error(t, err) + _, err = ac.CheckPath("i-am-a-python.p") + require.Error(t, err) + } else { + require.Error(t, err) + } }) t.Run("Cancelled/DuringRun", func(t *testing.T) { diff --git a/tests/integration/patch_status_test.go b/tests/integration/patch_status_test.go index 49eb9c501c..078051fe63 100644 --- a/tests/integration/patch_status_test.go +++ b/tests/integration/patch_status_test.go @@ -32,10 +32,15 @@ func TestPatchStatus(t *testing.T) { user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) session := loginUser(t, user2.Name) + var objectFormat optional.Option[string] + if git.SupportHashSha256 { + objectFormat = optional.Some("sha256") + } + repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user2, tests.DeclarativeRepoOptions{ AutoInit: optional.Some(true), EnabledUnits: optional.Some([]unit_model.Type{unit_model.TypeCode}), - ObjectFormat: optional.Some("sha256"), + ObjectFormat: objectFormat, Files: optional.Some([]*files_service.ChangeRepoFile{ { Operation: "create", diff --git a/tests/integration/repo_signed_tag_test.go b/tests/integration/repo_signed_tag_test.go index 16d8841304..686690bd19 100644 --- a/tests/integration/repo_signed_tag_test.go +++ b/tests/integration/repo_signed_tag_test.go @@ -25,6 +25,10 @@ import ( ) func TestRepoSSHSignedTags(t *testing.T) { + if git.CheckGitVersionAtLeast("2.34") != nil { + t.Skip("Skipping, does not support SSH signing") + return + } defer tests.PrepareTestEnv(t)() // Preparations diff --git a/tests/integration/signing_git_test.go b/tests/integration/signing_git_test.go index e82abea5e6..9d69306e0a 100644 --- a/tests/integration/signing_git_test.go +++ b/tests/integration/signing_git_test.go @@ -42,6 +42,10 @@ func TestInstanceSigning(t *testing.T) { defer test.MockProtect(&setting.Repository.Signing.CRUDActions)() t.Run("SSH", func(t *testing.T) { + if git.CheckGitVersionAtLeast("2.34") != nil { + t.Skip("Skipping, does not support git SSH signing") + return + } defer tests.PrintCurrentTest(t)() pubKeyContent, err := os.ReadFile("tests/integration/ssh-signing-key.pub")