Skip to content

Commit 852bf5e

Browse files
authored
Add git.DIFF_RENAME_SIMILARITY_THRESHOLD option (#36164)
Make the threshold value passed to `git diff --find-renames` configurable
1 parent eaa47c3 commit 852bf5e

File tree

5 files changed

+52
-32
lines changed

5 files changed

+52
-32
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,9 @@ LEVEL = Info
733733
;DISABLE_CORE_PROTECT_NTFS=false
734734
;; Disable the usage of using partial clones for git.
735735
;DISABLE_PARTIAL_CLONE = false
736+
;; Set the similarity threshold passed to git commands via `--find-renames=<threshold>`.
737+
;; Default is 50%, the same as git. Must be a integer percentage between 0% and 100%.
738+
;DIFF_RENAME_SIMILARITY_THRESHOLD = 50%
736739

737740
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
738741
;; Git Operation timeout in seconds

modules/git/diff.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"code.gitea.io/gitea/modules/git/gitcmd"
1818
"code.gitea.io/gitea/modules/log"
19+
"code.gitea.io/gitea/modules/setting"
1920
)
2021

2122
// RawDiffType type of a raw diff.
@@ -47,15 +48,19 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
4748
switch diffType {
4849
case RawDiffNormal:
4950
if len(startCommit) != 0 {
50-
cmd.AddArguments("diff", "-M").AddDynamicArguments(startCommit, endCommit).AddDashesAndList(files...)
51+
cmd.AddArguments("diff").
52+
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold).
53+
AddDynamicArguments(startCommit, endCommit).AddDashesAndList(files...)
5154
} else if commit.ParentCount() == 0 {
5255
cmd.AddArguments("show").AddDynamicArguments(endCommit).AddDashesAndList(files...)
5356
} else {
5457
c, err := commit.Parent(0)
5558
if err != nil {
5659
return err
5760
}
58-
cmd.AddArguments("diff", "-M").AddDynamicArguments(c.ID.String(), endCommit).AddDashesAndList(files...)
61+
cmd.AddArguments("diff").
62+
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold).
63+
AddDynamicArguments(c.ID.String(), endCommit).AddDashesAndList(files...)
5964
}
6065
case RawDiffPatch:
6166
if len(startCommit) != 0 {

modules/setting/git.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package setting
55

66
import (
77
"path/filepath"
8+
"regexp"
89
"strings"
910
"time"
1011

@@ -17,20 +18,21 @@ var Git = struct {
1718
HomePath string
1819
DisableDiffHighlight bool
1920

20-
MaxGitDiffLines int
21-
MaxGitDiffLineCharacters int
22-
MaxGitDiffFiles int
23-
CommitsRangeSize int // CommitsRangeSize the default commits range size
24-
BranchesRangeSize int // BranchesRangeSize the default branches range size
25-
VerbosePush bool
26-
VerbosePushDelay time.Duration
27-
GCArgs []string `ini:"GC_ARGS" delim:" "`
28-
EnableAutoGitWireProtocol bool
29-
PullRequestPushMessage bool
30-
LargeObjectThreshold int64
31-
DisableCoreProtectNTFS bool
32-
DisablePartialClone bool
33-
Timeout struct {
21+
MaxGitDiffLines int
22+
MaxGitDiffLineCharacters int
23+
MaxGitDiffFiles int
24+
CommitsRangeSize int // CommitsRangeSize the default commits range size
25+
BranchesRangeSize int // BranchesRangeSize the default branches range size
26+
VerbosePush bool
27+
VerbosePushDelay time.Duration
28+
GCArgs []string `ini:"GC_ARGS" delim:" "`
29+
EnableAutoGitWireProtocol bool
30+
PullRequestPushMessage bool
31+
LargeObjectThreshold int64
32+
DisableCoreProtectNTFS bool
33+
DisablePartialClone bool
34+
DiffRenameSimilarityThreshold string
35+
Timeout struct {
3436
Default int
3537
Migrate int
3638
Mirror int
@@ -39,19 +41,20 @@ var Git = struct {
3941
GC int `ini:"GC"`
4042
} `ini:"git.timeout"`
4143
}{
42-
DisableDiffHighlight: false,
43-
MaxGitDiffLines: 1000,
44-
MaxGitDiffLineCharacters: 5000,
45-
MaxGitDiffFiles: 100,
46-
CommitsRangeSize: 50,
47-
BranchesRangeSize: 20,
48-
VerbosePush: true,
49-
VerbosePushDelay: 5 * time.Second,
50-
GCArgs: []string{},
51-
EnableAutoGitWireProtocol: true,
52-
PullRequestPushMessage: true,
53-
LargeObjectThreshold: 1024 * 1024,
54-
DisablePartialClone: false,
44+
DisableDiffHighlight: false,
45+
MaxGitDiffLines: 1000,
46+
MaxGitDiffLineCharacters: 5000,
47+
MaxGitDiffFiles: 100,
48+
CommitsRangeSize: 50,
49+
BranchesRangeSize: 20,
50+
VerbosePush: true,
51+
VerbosePushDelay: 5 * time.Second,
52+
GCArgs: []string{},
53+
EnableAutoGitWireProtocol: true,
54+
PullRequestPushMessage: true,
55+
LargeObjectThreshold: 1024 * 1024,
56+
DisablePartialClone: false,
57+
DiffRenameSimilarityThreshold: "50%",
5558
Timeout: struct {
5659
Default int
5760
Migrate int
@@ -117,4 +120,9 @@ func loadGitFrom(rootCfg ConfigProvider) {
117120
} else {
118121
Git.HomePath = filepath.Clean(Git.HomePath)
119122
}
123+
124+
// validate for a integer percentage between 0% and 100%
125+
if !regexp.MustCompile(`^([0-9]|[1-9][0-9]|100)%$`).MatchString(Git.DiffRenameSimilarityThreshold) {
126+
log.Fatal("Invalid git.DIFF_RENAME_SIMILARITY_THRESHOLD: %s", Git.DiffRenameSimilarityThreshold)
127+
}
120128
}

services/gitdiff/git_diff_tree.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/git"
1616
"code.gitea.io/gitea/modules/git/gitcmd"
1717
"code.gitea.io/gitea/modules/log"
18+
"code.gitea.io/gitea/modules/setting"
1819
)
1920

2021
type DiffTree struct {
@@ -56,7 +57,9 @@ func runGitDiffTree(ctx context.Context, gitRepo *git.Repository, useMergeBase b
5657
return nil, err
5758
}
5859

59-
cmd := gitcmd.NewCommand("diff-tree", "--raw", "-r", "--find-renames", "--root")
60+
cmd := gitcmd.NewCommand("diff-tree", "--raw", "-r", "--root").
61+
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold)
62+
6063
if useMergeBase {
6164
cmd.AddArguments("--merge-base")
6265
}

services/gitdiff/gitdiff.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,9 @@ func getDiffBasic(ctx context.Context, gitRepo *git.Repository, opts *DiffOption
12251225
}
12261226

12271227
cmdDiff := gitcmd.NewCommand().
1228-
AddArguments("diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M").
1229-
AddArguments(opts.WhitespaceBehavior...)
1228+
AddArguments("diff", "--src-prefix=\\a/", "--dst-prefix=\\b/").
1229+
AddArguments(opts.WhitespaceBehavior...).
1230+
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold)
12301231

12311232
// In git 2.31, git diff learned --skip-to which we can use to shortcut skip to file
12321233
// so if we are using at least this version of git we don't have to tell ParsePatch to do

0 commit comments

Comments
 (0)