mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-29 00:59:39 +09:00
fix(pulls): respect diff.orderFile in diff file tree (#38566)
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
co-authored by
wxiaoguang
parent
bc3f63095b
commit
c8df67c0d0
+15
-1
@@ -91,7 +91,7 @@ func syncGitConfig(ctx context.Context) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// By default partial clones are disabled, enable them from git v2.22
|
// By default, partial clones are disabled, enable them from git v2.22
|
||||||
if !setting.Git.DisablePartialClone && DefaultFeatures().CheckVersionAtLeast("2.22") {
|
if !setting.Git.DisablePartialClone && DefaultFeatures().CheckVersionAtLeast("2.22") {
|
||||||
if err = configSet(ctx, "uploadpack.allowfilter", "true"); err != nil {
|
if err = configSet(ctx, "uploadpack.allowfilter", "true"); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -114,9 +114,23 @@ func syncGitConfig(ctx context.Context) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GlobalConfig = &GlobalConfigStruct{}
|
||||||
|
// HINT: GIT-DIFF-TREE-UI-CONFIG: Git's bug: git-diff-tree loads config with /* no "diff" UI options */ (since 20 years ago).
|
||||||
|
// https://github.com/git/git/blame/5d2e7709234afea1b6ddb25cd4f60d3d5fb3c200/builtin/diff-tree.c#L127
|
||||||
|
// Although document and manual say that "git-diff-tree" supports "diff.orderfile" option, but it is not actually supported.
|
||||||
|
// So we need to apply the diff.orderfile explicitly in our code.
|
||||||
|
GlobalConfig.DiffOrderFile, _ = configGet(ctx, "diff.orderfile")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func configGet(ctx context.Context, key string) (string, error) {
|
||||||
|
stdout, _, err := gitcmd.NewCommand("config", "--global", "--get").AddDynamicArguments(key).RunStdString(ctx)
|
||||||
|
if err != nil && !gitcmd.IsErrorExitCode(err, 1) {
|
||||||
|
return "", fmt.Errorf("failed to get git config %s, err: %w", key, err)
|
||||||
|
}
|
||||||
|
return strings.TrimRight(stdout, "\r\n"), nil
|
||||||
|
}
|
||||||
|
|
||||||
func configSet(ctx context.Context, key, value string) error {
|
func configSet(ctx context.Context, key, value string) error {
|
||||||
stdout, _, err := gitcmd.NewCommand("config", "--global", "--get").
|
stdout, _, err := gitcmd.NewCommand("config", "--global", "--get").
|
||||||
AddDynamicArguments(key).
|
AddDynamicArguments(key).
|
||||||
|
|||||||
+8
-1
@@ -37,7 +37,14 @@ type Features struct {
|
|||||||
SupportGitMergeTree bool // >= 2.40 // we also need "--merge-base"
|
SupportGitMergeTree bool // >= 2.40 // we also need "--merge-base"
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultFeatures *Features
|
type GlobalConfigStruct struct {
|
||||||
|
DiffOrderFile string
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
defaultFeatures *Features
|
||||||
|
GlobalConfig *GlobalConfigStruct
|
||||||
|
)
|
||||||
|
|
||||||
func (f *Features) CheckVersionAtLeast(atLeast string) bool {
|
func (f *Features) CheckVersionAtLeast(atLeast string) bool {
|
||||||
return f.gitVersion.Compare(version.Must(version.NewVersion(atLeast))) >= 0
|
return f.gitVersion.Compare(version.Must(version.NewVersion(atLeast))) >= 0
|
||||||
|
|||||||
@@ -60,6 +60,11 @@ func runGitDiffTree(ctx context.Context, gitRepo *git.Repository, useMergeBase b
|
|||||||
cmd := gitcmd.NewCommand("diff-tree", "--raw", "-r", "--root").
|
cmd := gitcmd.NewCommand("diff-tree", "--raw", "-r", "--root").
|
||||||
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold)
|
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold)
|
||||||
|
|
||||||
|
// HINT: GIT-DIFF-TREE-UI-CONFIG: apply the diff.orderfile explicitly
|
||||||
|
if git.GlobalConfig.DiffOrderFile != "" {
|
||||||
|
cmd.AddOptionFormat("-O%s", git.GlobalConfig.DiffOrderFile)
|
||||||
|
}
|
||||||
|
|
||||||
if useMergeBase {
|
if useMergeBase {
|
||||||
cmd.AddArguments("--merge-base")
|
cmd.AddArguments("--merge-base")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,13 @@
|
|||||||
package gitdiff
|
package gitdiff
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.dev/modules/git"
|
"gitea.dev/modules/git"
|
||||||
|
"gitea.dev/modules/git/gitcmd"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -217,6 +220,43 @@ func TestGitDiffTree(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGitDiffTreeRespectsDiffOrderFile(t *testing.T) {
|
||||||
|
gitRepo, err := git.OpenRepositoryLocal("../../modules/git/tests/repos/repo5_pulls")
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer gitRepo.Close()
|
||||||
|
|
||||||
|
testDiffTree := func(t *testing.T) (filePaths []string) {
|
||||||
|
t.Helper()
|
||||||
|
diffTree, err := GetDiffTree(t.Context(), gitRepo, false, "72866af952e98d02a73003501836074b286a78f6", "d8e0bbb45f200e67d9a784ce55bd90821af45ebd")
|
||||||
|
require.NoError(t, err)
|
||||||
|
for _, f := range diffTree.Files {
|
||||||
|
filePaths = append(filePaths, f.HeadPath)
|
||||||
|
}
|
||||||
|
return filePaths
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("NoDiffOrderFile", func(t *testing.T) {
|
||||||
|
assert.Equal(t, []string{"LICENSE", "README.md"}, testDiffTree(t))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("GlobalDiffOrderFile", func(t *testing.T) {
|
||||||
|
diffOrderFilePath := filepath.Join(t.TempDir(), "test-diff-order.txt")
|
||||||
|
err = os.WriteFile(diffOrderFilePath, []byte("README.md\nLICENSE\n"), 0o644)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, _, err = gitcmd.NewCommand("config", "set", "--global").AddDynamicArguments("diff.orderFile", diffOrderFilePath).RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, git.InitFull())
|
||||||
|
defer func() {
|
||||||
|
_, _, err = gitcmd.NewCommand("config", "unset", "--global").AddDynamicArguments("diff.orderFile").RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, git.InitFull())
|
||||||
|
}()
|
||||||
|
|
||||||
|
assert.Equal(t, []string{"README.md", "LICENSE"}, testDiffTree(t))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseGitDiffTree(t *testing.T) {
|
func TestParseGitDiffTree(t *testing.T) {
|
||||||
test := []struct {
|
test := []struct {
|
||||||
Name string
|
Name string
|
||||||
|
|||||||
Reference in New Issue
Block a user