0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-12-17 19:58:48 +01:00

Add git.DIFF_RENAME_SIMILARITY_THRESHOLD option (#36164)

Make the threshold value passed to `git diff --find-renames` configurable
This commit is contained in:
silverwind 2025-12-17 11:02:32 +01:00 committed by GitHub
parent eaa47c3e09
commit 852bf5e2a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 52 additions and 32 deletions

View File

@ -733,6 +733,9 @@ LEVEL = Info
;DISABLE_CORE_PROTECT_NTFS=false ;DISABLE_CORE_PROTECT_NTFS=false
;; Disable the usage of using partial clones for git. ;; Disable the usage of using partial clones for git.
;DISABLE_PARTIAL_CLONE = false ;DISABLE_PARTIAL_CLONE = false
;; Set the similarity threshold passed to git commands via `--find-renames=<threshold>`.
;; Default is 50%, the same as git. Must be a integer percentage between 0% and 100%.
;DIFF_RENAME_SIMILARITY_THRESHOLD = 50%
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Git Operation timeout in seconds ;; Git Operation timeout in seconds

View File

@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/git/gitcmd" "code.gitea.io/gitea/modules/git/gitcmd"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
) )
// RawDiffType type of a raw diff. // RawDiffType type of a raw diff.
@ -47,7 +48,9 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
switch diffType { switch diffType {
case RawDiffNormal: case RawDiffNormal:
if len(startCommit) != 0 { if len(startCommit) != 0 {
cmd.AddArguments("diff", "-M").AddDynamicArguments(startCommit, endCommit).AddDashesAndList(files...) cmd.AddArguments("diff").
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold).
AddDynamicArguments(startCommit, endCommit).AddDashesAndList(files...)
} else if commit.ParentCount() == 0 { } else if commit.ParentCount() == 0 {
cmd.AddArguments("show").AddDynamicArguments(endCommit).AddDashesAndList(files...) cmd.AddArguments("show").AddDynamicArguments(endCommit).AddDashesAndList(files...)
} else { } else {
@ -55,7 +58,9 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
if err != nil { if err != nil {
return err return err
} }
cmd.AddArguments("diff", "-M").AddDynamicArguments(c.ID.String(), endCommit).AddDashesAndList(files...) cmd.AddArguments("diff").
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold).
AddDynamicArguments(c.ID.String(), endCommit).AddDashesAndList(files...)
} }
case RawDiffPatch: case RawDiffPatch:
if len(startCommit) != 0 { if len(startCommit) != 0 {

View File

@ -5,6 +5,7 @@ package setting
import ( import (
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"time" "time"
@ -17,20 +18,21 @@ var Git = struct {
HomePath string HomePath string
DisableDiffHighlight bool DisableDiffHighlight bool
MaxGitDiffLines int MaxGitDiffLines int
MaxGitDiffLineCharacters int MaxGitDiffLineCharacters int
MaxGitDiffFiles int MaxGitDiffFiles int
CommitsRangeSize int // CommitsRangeSize the default commits range size CommitsRangeSize int // CommitsRangeSize the default commits range size
BranchesRangeSize int // BranchesRangeSize the default branches range size BranchesRangeSize int // BranchesRangeSize the default branches range size
VerbosePush bool VerbosePush bool
VerbosePushDelay time.Duration VerbosePushDelay time.Duration
GCArgs []string `ini:"GC_ARGS" delim:" "` GCArgs []string `ini:"GC_ARGS" delim:" "`
EnableAutoGitWireProtocol bool EnableAutoGitWireProtocol bool
PullRequestPushMessage bool PullRequestPushMessage bool
LargeObjectThreshold int64 LargeObjectThreshold int64
DisableCoreProtectNTFS bool DisableCoreProtectNTFS bool
DisablePartialClone bool DisablePartialClone bool
Timeout struct { DiffRenameSimilarityThreshold string
Timeout struct {
Default int Default int
Migrate int Migrate int
Mirror int Mirror int
@ -39,19 +41,20 @@ var Git = struct {
GC int `ini:"GC"` GC int `ini:"GC"`
} `ini:"git.timeout"` } `ini:"git.timeout"`
}{ }{
DisableDiffHighlight: false, DisableDiffHighlight: false,
MaxGitDiffLines: 1000, MaxGitDiffLines: 1000,
MaxGitDiffLineCharacters: 5000, MaxGitDiffLineCharacters: 5000,
MaxGitDiffFiles: 100, MaxGitDiffFiles: 100,
CommitsRangeSize: 50, CommitsRangeSize: 50,
BranchesRangeSize: 20, BranchesRangeSize: 20,
VerbosePush: true, VerbosePush: true,
VerbosePushDelay: 5 * time.Second, VerbosePushDelay: 5 * time.Second,
GCArgs: []string{}, GCArgs: []string{},
EnableAutoGitWireProtocol: true, EnableAutoGitWireProtocol: true,
PullRequestPushMessage: true, PullRequestPushMessage: true,
LargeObjectThreshold: 1024 * 1024, LargeObjectThreshold: 1024 * 1024,
DisablePartialClone: false, DisablePartialClone: false,
DiffRenameSimilarityThreshold: "50%",
Timeout: struct { Timeout: struct {
Default int Default int
Migrate int Migrate int
@ -117,4 +120,9 @@ func loadGitFrom(rootCfg ConfigProvider) {
} else { } else {
Git.HomePath = filepath.Clean(Git.HomePath) Git.HomePath = filepath.Clean(Git.HomePath)
} }
// validate for a integer percentage between 0% and 100%
if !regexp.MustCompile(`^([0-9]|[1-9][0-9]|100)%$`).MatchString(Git.DiffRenameSimilarityThreshold) {
log.Fatal("Invalid git.DIFF_RENAME_SIMILARITY_THRESHOLD: %s", Git.DiffRenameSimilarityThreshold)
}
} }

View File

@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/git/gitcmd" "code.gitea.io/gitea/modules/git/gitcmd"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
) )
type DiffTree struct { type DiffTree struct {
@ -56,7 +57,9 @@ func runGitDiffTree(ctx context.Context, gitRepo *git.Repository, useMergeBase b
return nil, err return nil, err
} }
cmd := gitcmd.NewCommand("diff-tree", "--raw", "-r", "--find-renames", "--root") cmd := gitcmd.NewCommand("diff-tree", "--raw", "-r", "--root").
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold)
if useMergeBase { if useMergeBase {
cmd.AddArguments("--merge-base") cmd.AddArguments("--merge-base")
} }

View File

@ -1225,8 +1225,9 @@ func getDiffBasic(ctx context.Context, gitRepo *git.Repository, opts *DiffOption
} }
cmdDiff := gitcmd.NewCommand(). cmdDiff := gitcmd.NewCommand().
AddArguments("diff", "--src-prefix=\\a/", "--dst-prefix=\\b/", "-M"). AddArguments("diff", "--src-prefix=\\a/", "--dst-prefix=\\b/").
AddArguments(opts.WhitespaceBehavior...) AddArguments(opts.WhitespaceBehavior...).
AddOptionFormat("--find-renames=%s", setting.Git.DiffRenameSimilarityThreshold)
// In git 2.31, git diff learned --skip-to which we can use to shortcut skip to file // In git 2.31, git diff learned --skip-to which we can use to shortcut skip to file
// so if we are using at least this version of git we don't have to tell ParsePatch to do // so if we are using at least this version of git we don't have to tell ParsePatch to do