fix: detect renames when using diff-tree (#11038)

Regression of forgejo/forgejo!9587

`git-diff` will always find renames because it is implied by the `diff.renames` configuration option. Specify `--find-renames` to detect renames again.

Resolves forgejo/forgejo#11032

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11038
Reviewed-by: Beowulf <beowulf@beocode.eu>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2026-01-25 15:51:18 +01:00 committed by Gusted
parent 217da2ae7f
commit 54017ee5c4
16 changed files with 48 additions and 2 deletions

View file

@ -189,7 +189,7 @@ var (
// `useMergebase` is specified then the merge base between `base` and `head` is
// used to compare against `head`.
func (repo *Repository) GetShortStat(base, head string, useMergebase bool) (numFiles, totalAdditions, totalDeletions int, err error) {
cmd := NewCommand(repo.Ctx, "diff-tree", "--shortstat")
cmd := NewCommand(repo.Ctx, "diff-tree", "--shortstat", "--find-renames")
if useMergebase {
cmd = cmd.AddArguments("--merge-base")
}
@ -211,7 +211,7 @@ func (repo *Repository) GetShortStat(base, head string, useMergebase bool) (numF
// GetCommitShortStat returns the number of files, total additions and total deletions the commit has.
func (repo *Repository) GetCommitShortStat(commitID string) (numFiles, totalAdditions, totalDeletions int, err error) {
cmd := NewCommand(repo.Ctx, "diff-tree", "--shortstat", "--no-commit-id", "--root").AddDynamicArguments(commitID)
cmd := NewCommand(repo.Ctx, "diff-tree", "--shortstat", "--no-commit-id", "--root", "--find-renames").AddDynamicArguments(commitID)
stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
if err != nil {
return 0, 0, 0, err

View file

@ -242,6 +242,18 @@ func TestGetCommitShortStat(t *testing.T) {
assert.Equal(t, 6, totalAddition)
assert.Equal(t, 0, totalDeletions)
})
t.Run("Renames", func(t *testing.T) {
repo, err := OpenRepository(t.Context(), filepath.Join(testReposDir, "renames"))
require.NoError(t, err)
defer repo.Close()
numFiles, totalAddition, totalDeletions, err := repo.GetCommitShortStat("f667f3a24223414e3bfbe01ab6e445c703ab8e25")
require.NoError(t, err)
assert.Equal(t, 1, numFiles)
assert.Zero(t, totalAddition)
assert.Zero(t, totalDeletions)
})
}
func TestGetShortStat(t *testing.T) {
@ -301,6 +313,28 @@ func TestGetShortStat(t *testing.T) {
assert.Zero(t, totalAdditions)
assert.Zero(t, totalDeletions)
})
t.Run("Renames", func(t *testing.T) {
repo, err := OpenRepository(t.Context(), filepath.Join(testReposDir, "renames"))
require.NoError(t, err)
defer repo.Close()
t.Run("Only rename", func(t *testing.T) {
numFiles, totalAdditions, totalDeletions, err := repo.GetShortStat("bc40f00489096a7d4090a609a6572f528e1acb76", "f667f3a24223414e3bfbe01ab6e445c703ab8e25", true)
require.NoError(t, err)
assert.Equal(t, 1, numFiles)
assert.Zero(t, totalAdditions)
assert.Zero(t, totalDeletions)
})
t.Run("Too much diverged", func(t *testing.T) {
numFiles, totalAdditions, totalDeletions, err := repo.GetShortStat("bc40f00489096a7d4090a609a6572f528e1acb76", "acdee217ada3fea6e503acfb969724cc799fc516", true)
require.NoError(t, err)
assert.Equal(t, 2, numFiles)
assert.Equal(t, 3, totalAdditions)
assert.Equal(t, 1, totalDeletions)
})
})
}
func TestGetMergeBaseSimple(t *testing.T) {

View file

@ -0,0 +1 @@
ref: refs/heads/main

View file

@ -0,0 +1,6 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
[remote "origin"]
url = /home/gusted/Desktop/renames

View file

@ -0,0 +1,3 @@
P pack-3c04d9ebf3e2c4620d7142f736b92d739834e2d4.pack
P pack-d1cbea94fcad36c556cd5921e8df7feff7cbbbb2.pack

View file

@ -0,0 +1,2 @@
# pack-refs with: peeled fully-peeled sorted
acdee217ada3fea6e503acfb969724cc799fc516 refs/heads/main