test: fix unit tests on git older than 2.42 (#40557)

Gitea supports git 2.34 and newer, but with git 2.34.1 `go test
./modules/git/` panics in `TestReadingBlameOutput/sha256` (nil pointer
dereference), which aborts the rest of the package, and `services/pull`
fails its `*-MergeTree` cases with `exit status 129`. These tests use
sha256 repositories, `cat-file --batch-command` and `merge-tree
--write-tree`, which production code only uses when `DefaultFeatures()`
says git is new enough (2.42, 2.36 and 2.40).

Those cases now skip on the same `DefaultFeatures()` flags, as the
check-attr tests in `modules/git/attribute` already do. With git 2.34.1
both packages failed before; now they pass with those cases skipped, and
`make test-backend` passes as a whole. With git 2.42.0, 2.43.0 and
2.56.1 nothing is skipped; with 2.41.3 only the sha256 cases are.

Related: https://github.com/go-gitea/gitea/pull/39565

Assisted-by: Hermes Agent:claude-opus-5-5
This commit is contained in:
Zhaowei Gu authored and GitHub committed 2026-11-27 12:53:33 +00:00
1 parent e77c1c26e5
commit de28726658
6 files changed
+25

No files matched your search

+3
View File
@@ -55,6 +55,9 @@ func TestReadingBlameOutput(t *testing.T) {
},
} {
t.Run(scenario.name, func(t *testing.T) {
if scenario.objectFormat == Sha256ObjectFormat && !DefaultFeatures().SupportHashSha256 {
t.Skip("git version 2.42 is required to support sha256")
}
setting.AppDataPath = t.TempDir()
ctx := t.Context()
+4
View File
@@ -17,9 +17,13 @@ import (
)
func TestCatFileBatch(t *testing.T) {
supportBatchCommand := DefaultFeatures().SupportCatFileBatchCommand
defer test.MockVariableValue(&DefaultFeatures().SupportCatFileBatchCommand)()
DefaultFeatures().SupportCatFileBatchCommand = false
t.Run("LegacyCheck", testCatFileBatch)
if !supportBatchCommand {
t.Skip("git version 2.36 is required to support cat-file --batch-command")
}
DefaultFeatures().SupportCatFileBatchCommand = true
t.Run("BatchCommand", testCatFileBatch)
}
+3
View File
@@ -25,6 +25,9 @@ func TestGetFullCommitIDErrorSha256(t *testing.T) {
}
func TestHasPreviousCommitSha256(t *testing.T) {
if !DefaultFeatures().SupportHashSha256 {
t.Skip("git version 2.42 is required to support sha256")
}
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare_sha256")
repo, err := OpenRepositoryLocal(t.Context(), bareRepo1Path)
+3
View File
@@ -182,6 +182,9 @@ signed commit`,
},
} {
t.Run(testCase.name, func(t *testing.T) {
if testCase.sha.Type() == Sha256ObjectFormat && !DefaultFeatures().SupportHashSha256 {
t.Skip("git version 2.42 is required to support sha256")
}
gitRepo, err := OpenRepositoryLocal(t.Context(), filepath.Join(testReposDir, testCase.repo))
assert.NoError(t, err)
assert.NotNil(t, gitRepo)
+3
View File
@@ -67,6 +67,9 @@ M 100644 :5 z/d
}
func TestMergeTreeDirectoryRenameConflictWithoutFiles(t *testing.T) {
if !DefaultFeatures().SupportGitMergeTree {
t.Skip("git version 2.40 is required to support merge-tree")
}
repoDir := prepareRepoDirRenameConflict(t)
require.DirExists(t, repoDir)
repo := mockRepository(repoDir)
+9
View File
@@ -46,6 +46,9 @@ func TestPullRequestMergeable(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
t.Run("NoConflict-MergeTree", func(t *testing.T) {
if !git.DefaultFeatures().SupportGitMergeTree {
t.Skip("git version 2.40 is required to support merge-tree")
}
testPullRequestMergeCheck(t, checkPullRequestMergeableByMergeTree, pr, issues_model.PullRequestStatusMergeable, nil, nil)
mergeRef := git.MergeRefNameFromPullIndex(pr.Index).String()
mergeCommitID, err := git.GetFullCommitID(t.Context(), pr.BaseRepo, mergeRef)
@@ -70,6 +73,9 @@ func TestPullRequestMergeable(t *testing.T) {
pr.BaseBranch, pr.HeadBranch = "test-merge-tree-conflict-base", "test-merge-tree-conflict-head"
conflictFiles := createConflictBranches(t, pr.BaseRepo, pr.BaseBranch, pr.HeadBranch)
t.Run("Conflict-MergeTree", func(t *testing.T) {
if !git.DefaultFeatures().SupportGitMergeTree {
t.Skip("git version 2.40 is required to support merge-tree")
}
testPullRequestMergeCheck(t, checkPullRequestMergeableByMergeTree, pr, issues_model.PullRequestStatusConflict, conflictFiles, nil)
})
t.Run("Conflict-TmpRepo", func(t *testing.T) {
@@ -79,6 +85,9 @@ func TestPullRequestMergeable(t *testing.T) {
pr.BaseBranch, pr.HeadBranch = "test-merge-tree-empty-base", "test-merge-tree-empty-head"
createEmptyBranches(t, pr.BaseRepo, pr.BaseBranch, pr.HeadBranch)
t.Run("Empty-MergeTree", func(t *testing.T) {
if !git.DefaultFeatures().SupportGitMergeTree {
t.Skip("git version 2.40 is required to support merge-tree")
}
testPullRequestMergeCheck(t, checkPullRequestMergeableByMergeTree, pr, issues_model.PullRequestStatusEmpty, nil, nil)
})
t.Run("Empty-TmpRepo", func(t *testing.T) {