From b732300f276f3e9d169c4349d4119145a4966124 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 2 Aug 2023 18:14:38 +0800 Subject: [PATCH 1/9] use 'diff-tree' to get changed file names --- modules/actions/workflows.go | 8 ++++---- modules/git/commit.go | 5 +++++ modules/git/repo_compare.go | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 2c7cec5591de3..04b0465b40f9a 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -255,9 +255,9 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa matchTimes++ } case "paths": - filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) + filesChanged, err := commit.GetFilesChanged() if err != nil { - log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) + log.Error("GetFilesChanged [commit_sha1: %s]: %v", commit.ID.String(), err) } else { patterns, err := workflowpattern.CompilePatterns(vals...) if err != nil { @@ -268,9 +268,9 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa } } case "paths-ignore": - filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) + filesChanged, err := commit.GetFilesChanged() if err != nil { - log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) + log.Error("GetFilesChanged [commit_sha1: %s]: %v", commit.ID.String(), err) } else { patterns, err := workflowpattern.CompilePatterns(vals...) if err != nil { diff --git a/modules/git/commit.go b/modules/git/commit.go index c44882d886171..1a2891ea2942a 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -305,6 +305,11 @@ func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) return c.repo.GetFilesChangedBetween(pastCommit, c.ID.String()) } +// GetFilesChanged get the changed file names of the commit +func (c *Commit) GetFilesChanged() ([]string, error) { + return c.repo.GetCommitFilesChanged(c.ID.String()) +} + // FileChangedSinceCommit Returns true if the file given has changed since the the past commit // YOU MUST ENSURE THAT pastCommit is a valid commit ID. func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, error) { diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index e706275856856..6a1fbe846c205 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -295,6 +295,22 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err return split, err } +// GetCommitFilesChanged get the changed file names of the specified commit +func (repo *Repository) GetCommitFilesChanged(commitID string) ([]string, error) { + stdout, _, err := NewCommand(repo.Ctx, "diff-tree", "--no-commit-id", "--name-only", "-r").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path}) + if err != nil { + return nil, err + } + split := strings.Split(stdout, "\n") + + // Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty. + if len(split) > 0 { + split = split[:len(split)-1] + } + + return split, err +} + // GetDiffFromMergeBase generates and return patch data from merge base to head func (repo *Repository) GetDiffFromMergeBase(base, head string, w io.Writer) error { stderr := new(bytes.Buffer) From cfa1815fa49be5461c77485462782ea4d4421c66 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 2 Aug 2023 20:23:20 +0800 Subject: [PATCH 2/9] add the check for pushPayload.Before --- modules/actions/workflows.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 04b0465b40f9a..ca7f5f3f2564d 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -255,7 +255,15 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa matchTimes++ } case "paths": - filesChanged, err := commit.GetFilesChanged() + var ( + filesChanged []string + err error + ) + if pushPayload.Before != git.EmptySHA { + filesChanged, err = commit.GetFilesChangedSinceCommit(pushPayload.Before) + } else { + filesChanged, err = commit.GetFilesChanged() + } if err != nil { log.Error("GetFilesChanged [commit_sha1: %s]: %v", commit.ID.String(), err) } else { @@ -268,7 +276,15 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa } } case "paths-ignore": - filesChanged, err := commit.GetFilesChanged() + var ( + filesChanged []string + err error + ) + if pushPayload.Before != git.EmptySHA { + filesChanged, err = commit.GetFilesChangedSinceCommit(pushPayload.Before) + } else { + filesChanged, err = commit.GetFilesChanged() + } if err != nil { log.Error("GetFilesChanged [commit_sha1: %s]: %v", commit.ID.String(), err) } else { From 4cb61c617c4b518ad5776455ee1424dc424f32c6 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 3 Aug 2023 12:28:15 +0800 Subject: [PATCH 3/9] support using 'ls-tree' to get changed files --- modules/git/commit.go | 2 +- modules/git/repo_compare.go | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 1a2891ea2942a..d5f1606e5f625 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -307,7 +307,7 @@ func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) // GetFilesChanged get the changed file names of the commit func (c *Commit) GetFilesChanged() ([]string, error) { - return c.repo.GetCommitFilesChanged(c.ID.String()) + return c.repo.GetCommitFilesChanged(c) } // FileChangedSinceCommit Returns true if the file given has changed since the the past commit diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 6a1fbe846c205..0def065527f69 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -296,8 +296,18 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err } // GetCommitFilesChanged get the changed file names of the specified commit -func (repo *Repository) GetCommitFilesChanged(commitID string) ([]string, error) { - stdout, _, err := NewCommand(repo.Ctx, "diff-tree", "--no-commit-id", "--name-only", "-r").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path}) +func (repo *Repository) GetCommitFilesChanged(commit *Commit) ([]string, error) { + var ( + stdout string + err error + ) + if len(commit.Parents) == 0 { + // if the commit is the root commit, diff-tree cannot show the changed files + // we need to use ls-tree in this case + stdout, _, err = NewCommand(repo.Ctx, "ls-tree", "--name-only", "-r").AddDynamicArguments(commit.ID.String()).RunStdString(&RunOpts{Dir: repo.Path}) + } else { + stdout, _, err = NewCommand(repo.Ctx, "diff-tree", "--no-commit-id", "--name-only", "-r").AddDynamicArguments(commit.ID.String()).RunStdString(&RunOpts{Dir: repo.Path}) + } if err != nil { return nil, err } From 7636f7a6dbdead77ff79289d2b240a0fa1e855de Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 3 Aug 2023 12:28:46 +0800 Subject: [PATCH 4/9] add TestGetCommitFilesChanged --- modules/git/repo_compare_test.go | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/modules/git/repo_compare_test.go b/modules/git/repo_compare_test.go index 5b50bc82adeae..47b3ed4a33996 100644 --- a/modules/git/repo_compare_test.go +++ b/modules/git/repo_compare_test.go @@ -119,3 +119,42 @@ func TestReadWritePullHead(t *testing.T) { err = repo.RemoveReference(PullPrefix + "1/head") assert.NoError(t, err) } + +func TestGetCommitFilesChanged(t *testing.T) { + bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") + clonedPath, err := cloneRepo(t, bareRepo1Path) + if err != nil { + assert.NoError(t, err) + return + } + repo, err := openRepositoryWithDefaultContext(clonedPath) + if err != nil { + assert.NoError(t, err) + return + } + defer repo.Close() + + testCases := []struct { + CommitID string + ExpectedFiles []string + }{ + { + "95bb4d39648ee7e325106df01a621c530863a653", + []string{"file1.txt"}, + }, + { + "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", + []string{"file2.txt"}, + }, + } + + for _, tc := range testCases { + id, err := repo.ConvertToSHA1(tc.CommitID) + assert.NoError(t, err) + commit, err := repo.getCommit(id) + assert.NoError(t, err) + changedFiles, err := repo.GetCommitFilesChanged(commit) + assert.NoError(t, err) + assert.ElementsMatch(t, changedFiles, tc.ExpectedFiles) + } +} From b5c32d64dd62751917de58e3111018fe66190e38 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 3 Aug 2023 13:37:45 +0800 Subject: [PATCH 5/9] check EmptySHA in GetFilesChangedSinceCommit --- modules/actions/workflows.go | 20 ++------------------ modules/git/commit.go | 5 ++++- modules/git/repo_compare.go | 19 ++++++++++++------- modules/git/repo_compare_test.go | 6 +----- 4 files changed, 19 insertions(+), 31 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index ca7f5f3f2564d..9f9fdbac2700d 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -255,15 +255,7 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa matchTimes++ } case "paths": - var ( - filesChanged []string - err error - ) - if pushPayload.Before != git.EmptySHA { - filesChanged, err = commit.GetFilesChangedSinceCommit(pushPayload.Before) - } else { - filesChanged, err = commit.GetFilesChanged() - } + filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) if err != nil { log.Error("GetFilesChanged [commit_sha1: %s]: %v", commit.ID.String(), err) } else { @@ -276,15 +268,7 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa } } case "paths-ignore": - var ( - filesChanged []string - err error - ) - if pushPayload.Before != git.EmptySHA { - filesChanged, err = commit.GetFilesChangedSinceCommit(pushPayload.Before) - } else { - filesChanged, err = commit.GetFilesChanged() - } + filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) if err != nil { log.Error("GetFilesChanged [commit_sha1: %s]: %v", commit.ID.String(), err) } else { diff --git a/modules/git/commit.go b/modules/git/commit.go index d5f1606e5f625..7f53e5684cc44 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -302,12 +302,15 @@ func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) { // GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) { + if pastCommit == EmptySHA { + return c.GetFilesChanged() + } return c.repo.GetFilesChangedBetween(pastCommit, c.ID.String()) } // GetFilesChanged get the changed file names of the commit func (c *Commit) GetFilesChanged() ([]string, error) { - return c.repo.GetCommitFilesChanged(c) + return c.repo.GetCommitFilesChanged(c.ID.String()) } // FileChangedSinceCommit Returns true if the file given has changed since the the past commit diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 0def065527f69..4ad59e642a286 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -296,17 +296,22 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err } // GetCommitFilesChanged get the changed file names of the specified commit -func (repo *Repository) GetCommitFilesChanged(commit *Commit) ([]string, error) { - var ( - stdout string - err error - ) +func (repo *Repository) GetCommitFilesChanged(commitID string) ([]string, error) { + id, err := repo.ConvertToSHA1(commitID) + if err != nil { + return nil, err + } + commit, err := repo.getCommit(id) + if err != nil { + return nil, err + } + var stdout string if len(commit.Parents) == 0 { // if the commit is the root commit, diff-tree cannot show the changed files // we need to use ls-tree in this case - stdout, _, err = NewCommand(repo.Ctx, "ls-tree", "--name-only", "-r").AddDynamicArguments(commit.ID.String()).RunStdString(&RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand(repo.Ctx, "ls-tree", "--name-only", "-r").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path}) } else { - stdout, _, err = NewCommand(repo.Ctx, "diff-tree", "--no-commit-id", "--name-only", "-r").AddDynamicArguments(commit.ID.String()).RunStdString(&RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand(repo.Ctx, "diff-tree", "--no-commit-id", "--name-only", "-r").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path}) } if err != nil { return nil, err diff --git a/modules/git/repo_compare_test.go b/modules/git/repo_compare_test.go index 47b3ed4a33996..b3ce7ac332152 100644 --- a/modules/git/repo_compare_test.go +++ b/modules/git/repo_compare_test.go @@ -149,11 +149,7 @@ func TestGetCommitFilesChanged(t *testing.T) { } for _, tc := range testCases { - id, err := repo.ConvertToSHA1(tc.CommitID) - assert.NoError(t, err) - commit, err := repo.getCommit(id) - assert.NoError(t, err) - changedFiles, err := repo.GetCommitFilesChanged(commit) + changedFiles, err := repo.GetCommitFilesChanged(tc.CommitID) assert.NoError(t, err) assert.ElementsMatch(t, changedFiles, tc.ExpectedFiles) } From e260c82936821fbce17cf61301dec4036f71d472 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 3 Aug 2023 13:40:01 +0800 Subject: [PATCH 6/9] fix error log --- modules/actions/workflows.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 9f9fdbac2700d..2c7cec5591de3 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -257,7 +257,7 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa case "paths": filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) if err != nil { - log.Error("GetFilesChanged [commit_sha1: %s]: %v", commit.ID.String(), err) + log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) } else { patterns, err := workflowpattern.CompilePatterns(vals...) if err != nil { @@ -270,7 +270,7 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa case "paths-ignore": filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) if err != nil { - log.Error("GetFilesChanged [commit_sha1: %s]: %v", commit.ID.String(), err) + log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) } else { patterns, err := workflowpattern.CompilePatterns(vals...) if err != nil { From 2e9656bbb973f12fefee333192f3c3fbb6e46ed2 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 3 Aug 2023 14:07:58 +0800 Subject: [PATCH 7/9] fix --- modules/git/commit.go | 8 ------- modules/git/repo_compare.go | 36 ++++++-------------------------- modules/git/repo_compare_test.go | 27 ++++++++++++------------ modules/git/sha1.go | 4 ++-- 4 files changed, 21 insertions(+), 54 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 7f53e5684cc44..c44882d886171 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -302,17 +302,9 @@ func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) { // GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) { - if pastCommit == EmptySHA { - return c.GetFilesChanged() - } return c.repo.GetFilesChangedBetween(pastCommit, c.ID.String()) } -// GetFilesChanged get the changed file names of the commit -func (c *Commit) GetFilesChanged() ([]string, error) { - return c.repo.GetCommitFilesChanged(c.ID.String()) -} - // FileChangedSinceCommit Returns true if the file given has changed since the the past commit // YOU MUST ENSURE THAT pastCommit is a valid commit ID. func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, error) { diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 4ad59e642a286..ff1678fb4c962 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -280,43 +280,19 @@ func (repo *Repository) GetPatch(base, head string, w io.Writer) error { } // GetFilesChangedBetween returns a list of all files that have been changed between the given commits +// If base is undefined empty SHA (zeros), it only returns the files changed in the head commit func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, error) { - stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only", "-z").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path}) - if err != nil { - return nil, err - } - split := strings.Split(stdout, "\000") - - // Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty. - if len(split) > 0 { - split = split[:len(split)-1] - } - - return split, err -} - -// GetCommitFilesChanged get the changed file names of the specified commit -func (repo *Repository) GetCommitFilesChanged(commitID string) ([]string, error) { - id, err := repo.ConvertToSHA1(commitID) - if err != nil { - return nil, err - } - commit, err := repo.getCommit(id) - if err != nil { - return nil, err - } var stdout string - if len(commit.Parents) == 0 { - // if the commit is the root commit, diff-tree cannot show the changed files - // we need to use ls-tree in this case - stdout, _, err = NewCommand(repo.Ctx, "ls-tree", "--name-only", "-r").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path}) + var err error + if base == EmptySHA { + stdout, _, err = NewCommand(repo.Ctx, "diff-tree", "--name-only", "--root", "--no-commit-id", "-r", "-z").AddDynamicArguments(head).RunStdString(&RunOpts{Dir: repo.Path}) } else { - stdout, _, err = NewCommand(repo.Ctx, "diff-tree", "--no-commit-id", "--name-only", "-r").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path}) + stdout, _, err = NewCommand(repo.Ctx, "diff", "--name-only", "-z").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path}) } if err != nil { return nil, err } - split := strings.Split(stdout, "\n") + split := strings.Split(stdout, "\000") // Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty. if len(split) > 0 { diff --git a/modules/git/repo_compare_test.go b/modules/git/repo_compare_test.go index b3ce7ac332152..fcb11d59b1611 100644 --- a/modules/git/repo_compare_test.go +++ b/modules/git/repo_compare_test.go @@ -122,35 +122,34 @@ func TestReadWritePullHead(t *testing.T) { func TestGetCommitFilesChanged(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") - clonedPath, err := cloneRepo(t, bareRepo1Path) - if err != nil { - assert.NoError(t, err) - return - } - repo, err := openRepositoryWithDefaultContext(clonedPath) - if err != nil { - assert.NoError(t, err) - return - } + repo, err := openRepositoryWithDefaultContext(bareRepo1Path) + assert.NoError(t, err) defer repo.Close() testCases := []struct { - CommitID string - ExpectedFiles []string + base, head string + files []string }{ { + EmptySHA, "95bb4d39648ee7e325106df01a621c530863a653", []string{"file1.txt"}, }, { + EmptySHA, "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"file2.txt"}, }, + { + EmptyTreeSHA, + "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", + []string{"file1.txt", "file2.txt"}, + }, } for _, tc := range testCases { - changedFiles, err := repo.GetCommitFilesChanged(tc.CommitID) + changedFiles, err := repo.GetFilesChangedBetween(tc.base, tc.head) assert.NoError(t, err) - assert.ElementsMatch(t, changedFiles, tc.ExpectedFiles) + assert.ElementsMatch(t, tc.files, changedFiles) } } diff --git a/modules/git/sha1.go b/modules/git/sha1.go index 7d9d9776da9d4..8d6403e8657fb 100644 --- a/modules/git/sha1.go +++ b/modules/git/sha1.go @@ -11,10 +11,10 @@ import ( "strings" ) -// EmptySHA defines empty git SHA +// EmptySHA defines empty git SHA (undefined, non-existent) const EmptySHA = "0000000000000000000000000000000000000000" -// EmptyTreeSHA is the SHA of an empty tree +// EmptyTreeSHA is the SHA of an empty tree, the root of all git repositories const EmptyTreeSHA = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" // SHAFullLength is the full length of a git SHA From 17afa990f28a454e509ef661271e14e9aaeb8115 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 3 Aug 2023 16:52:24 +0800 Subject: [PATCH 8/9] use 'diff-tree' instead of 'diff' --- modules/git/repo_compare.go | 9 +++++---- modules/git/repo_compare_test.go | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index ff1678fb4c962..f22bfb3959967 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -281,14 +281,15 @@ func (repo *Repository) GetPatch(base, head string, w io.Writer) error { // GetFilesChangedBetween returns a list of all files that have been changed between the given commits // If base is undefined empty SHA (zeros), it only returns the files changed in the head commit +// If base is the SHA of an empty tree (EmptyTreeSHA), it returns the files changes from the initial commit to the head commit func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, error) { - var stdout string - var err error + cmd := NewCommand(repo.Ctx, "diff-tree", "--name-only", "--root", "--no-commit-id", "-r", "-z") if base == EmptySHA { - stdout, _, err = NewCommand(repo.Ctx, "diff-tree", "--name-only", "--root", "--no-commit-id", "-r", "-z").AddDynamicArguments(head).RunStdString(&RunOpts{Dir: repo.Path}) + cmd.AddDynamicArguments(head) } else { - stdout, _, err = NewCommand(repo.Ctx, "diff", "--name-only", "-z").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path}) + cmd.AddDynamicArguments(base + ".." + head) } + stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path}) if err != nil { return nil, err } diff --git a/modules/git/repo_compare_test.go b/modules/git/repo_compare_test.go index fcb11d59b1611..603aabde42a09 100644 --- a/modules/git/repo_compare_test.go +++ b/modules/git/repo_compare_test.go @@ -140,6 +140,11 @@ func TestGetCommitFilesChanged(t *testing.T) { "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"file2.txt"}, }, + { + "95bb4d39648ee7e325106df01a621c530863a653", + "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", + []string{"file2.txt"}, + }, { EmptyTreeSHA, "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", From 28dafde70100a3c03c4959cebfde40316f89f729 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 3 Aug 2023 18:10:19 +0800 Subject: [PATCH 9/9] fix '..' --- modules/git/repo_compare.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index f22bfb3959967..aad725fa9db89 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -287,7 +287,7 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err if base == EmptySHA { cmd.AddDynamicArguments(head) } else { - cmd.AddDynamicArguments(base + ".." + head) + cmd.AddDynamicArguments(base, head) } stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path}) if err != nil {