Skip to content

Commit 8a2f019

Browse files
Support getting changed files when commit ID is EmptySHA (#26290)
Fixes #26270. Co-Author: @wxiaoguang Thanks @lunny for providing this solution As #26270 (comment) said, at present we cannot get the names of changed files correctly when the `OldCommitID` is `EmptySHA`. In this PR, the `GetCommitFilesChanged` method is added and will be used to get the changed files by commit ID. References: - https://stackoverflow.com/a/424142 --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 865d222 commit 8a2f019

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

modules/git/repo_compare.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,16 @@ func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
280280
}
281281

282282
// GetFilesChangedBetween returns a list of all files that have been changed between the given commits
283+
// If base is undefined empty SHA (zeros), it only returns the files changed in the head commit
284+
// If base is the SHA of an empty tree (EmptyTreeSHA), it returns the files changes from the initial commit to the head commit
283285
func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, error) {
284-
stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only", "-z").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path})
286+
cmd := NewCommand(repo.Ctx, "diff-tree", "--name-only", "--root", "--no-commit-id", "-r", "-z")
287+
if base == EmptySHA {
288+
cmd.AddDynamicArguments(head)
289+
} else {
290+
cmd.AddDynamicArguments(base, head)
291+
}
292+
stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
285293
if err != nil {
286294
return nil, err
287295
}

modules/git/repo_compare_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,42 @@ func TestReadWritePullHead(t *testing.T) {
119119
err = repo.RemoveReference(PullPrefix + "1/head")
120120
assert.NoError(t, err)
121121
}
122+
123+
func TestGetCommitFilesChanged(t *testing.T) {
124+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
125+
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
126+
assert.NoError(t, err)
127+
defer repo.Close()
128+
129+
testCases := []struct {
130+
base, head string
131+
files []string
132+
}{
133+
{
134+
EmptySHA,
135+
"95bb4d39648ee7e325106df01a621c530863a653",
136+
[]string{"file1.txt"},
137+
},
138+
{
139+
EmptySHA,
140+
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
141+
[]string{"file2.txt"},
142+
},
143+
{
144+
"95bb4d39648ee7e325106df01a621c530863a653",
145+
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
146+
[]string{"file2.txt"},
147+
},
148+
{
149+
EmptyTreeSHA,
150+
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
151+
[]string{"file1.txt", "file2.txt"},
152+
},
153+
}
154+
155+
for _, tc := range testCases {
156+
changedFiles, err := repo.GetFilesChangedBetween(tc.base, tc.head)
157+
assert.NoError(t, err)
158+
assert.ElementsMatch(t, tc.files, changedFiles)
159+
}
160+
}

modules/git/sha1.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"strings"
1212
)
1313

14-
// EmptySHA defines empty git SHA
14+
// EmptySHA defines empty git SHA (undefined, non-existent)
1515
const EmptySHA = "0000000000000000000000000000000000000000"
1616

17-
// EmptyTreeSHA is the SHA of an empty tree
17+
// EmptyTreeSHA is the SHA of an empty tree, the root of all git repositories
1818
const EmptyTreeSHA = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
1919

2020
// SHAFullLength is the full length of a git SHA

0 commit comments

Comments
 (0)