Skip to content

Commit 2e9656b

Browse files
committed
fix
1 parent 20b8f5e commit 2e9656b

File tree

4 files changed

+21
-54
lines changed

4 files changed

+21
-54
lines changed

modules/git/commit.go

-8
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,9 @@ func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) {
302302

303303
// GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision
304304
func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) {
305-
if pastCommit == EmptySHA {
306-
return c.GetFilesChanged()
307-
}
308305
return c.repo.GetFilesChangedBetween(pastCommit, c.ID.String())
309306
}
310307

311-
// GetFilesChanged get the changed file names of the commit
312-
func (c *Commit) GetFilesChanged() ([]string, error) {
313-
return c.repo.GetCommitFilesChanged(c.ID.String())
314-
}
315-
316308
// FileChangedSinceCommit Returns true if the file given has changed since the the past commit
317309
// YOU MUST ENSURE THAT pastCommit is a valid commit ID.
318310
func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, error) {

modules/git/repo_compare.go

+6-30
Original file line numberDiff line numberDiff line change
@@ -280,43 +280,19 @@ 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
283284
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})
285-
if err != nil {
286-
return nil, err
287-
}
288-
split := strings.Split(stdout, "\000")
289-
290-
// Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty.
291-
if len(split) > 0 {
292-
split = split[:len(split)-1]
293-
}
294-
295-
return split, err
296-
}
297-
298-
// GetCommitFilesChanged get the changed file names of the specified commit
299-
func (repo *Repository) GetCommitFilesChanged(commitID string) ([]string, error) {
300-
id, err := repo.ConvertToSHA1(commitID)
301-
if err != nil {
302-
return nil, err
303-
}
304-
commit, err := repo.getCommit(id)
305-
if err != nil {
306-
return nil, err
307-
}
308285
var stdout string
309-
if len(commit.Parents) == 0 {
310-
// if the commit is the root commit, diff-tree cannot show the changed files
311-
// we need to use ls-tree in this case
312-
stdout, _, err = NewCommand(repo.Ctx, "ls-tree", "--name-only", "-r").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path})
286+
var err error
287+
if base == EmptySHA {
288+
stdout, _, err = NewCommand(repo.Ctx, "diff-tree", "--name-only", "--root", "--no-commit-id", "-r", "-z").AddDynamicArguments(head).RunStdString(&RunOpts{Dir: repo.Path})
313289
} else {
314-
stdout, _, err = NewCommand(repo.Ctx, "diff-tree", "--no-commit-id", "--name-only", "-r").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path})
290+
stdout, _, err = NewCommand(repo.Ctx, "diff", "--name-only", "-z").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path})
315291
}
316292
if err != nil {
317293
return nil, err
318294
}
319-
split := strings.Split(stdout, "\n")
295+
split := strings.Split(stdout, "\000")
320296

321297
// Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty.
322298
if len(split) > 0 {

modules/git/repo_compare_test.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -122,35 +122,34 @@ func TestReadWritePullHead(t *testing.T) {
122122

123123
func TestGetCommitFilesChanged(t *testing.T) {
124124
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
125-
clonedPath, err := cloneRepo(t, bareRepo1Path)
126-
if err != nil {
127-
assert.NoError(t, err)
128-
return
129-
}
130-
repo, err := openRepositoryWithDefaultContext(clonedPath)
131-
if err != nil {
132-
assert.NoError(t, err)
133-
return
134-
}
125+
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
126+
assert.NoError(t, err)
135127
defer repo.Close()
136128

137129
testCases := []struct {
138-
CommitID string
139-
ExpectedFiles []string
130+
base, head string
131+
files []string
140132
}{
141133
{
134+
EmptySHA,
142135
"95bb4d39648ee7e325106df01a621c530863a653",
143136
[]string{"file1.txt"},
144137
},
145138
{
139+
EmptySHA,
146140
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
147141
[]string{"file2.txt"},
148142
},
143+
{
144+
EmptyTreeSHA,
145+
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
146+
[]string{"file1.txt", "file2.txt"},
147+
},
149148
}
150149

151150
for _, tc := range testCases {
152-
changedFiles, err := repo.GetCommitFilesChanged(tc.CommitID)
151+
changedFiles, err := repo.GetFilesChangedBetween(tc.base, tc.head)
153152
assert.NoError(t, err)
154-
assert.ElementsMatch(t, changedFiles, tc.ExpectedFiles)
153+
assert.ElementsMatch(t, tc.files, changedFiles)
155154
}
156155
}

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)