From a57568bad7dac3c818d01a5b75fe66ce83bf140f Mon Sep 17 00:00:00 2001 From: Giteabot Date: Fri, 4 Aug 2023 11:27:45 +0800 Subject: [PATCH] Support getting changed files when commit ID is `EmptySHA` (#26290) (#26316) Backport #26290 by @Zettat123 Fixes #26270. Co-Author: @wxiaoguang Thanks @lunny for providing this solution As https://github.com/go-gitea/gitea/issues/26270#issuecomment-1661695151 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: Zettat123 Co-authored-by: wxiaoguang --- modules/git/repo_compare.go | 10 +++++++- modules/git/repo_compare_test.go | 39 ++++++++++++++++++++++++++++++++ modules/git/sha1.go | 4 ++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index e706275856..aad725fa9d 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -280,8 +280,16 @@ 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) { - stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only", "-z").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path}) + cmd := NewCommand(repo.Ctx, "diff-tree", "--name-only", "--root", "--no-commit-id", "-r", "-z") + if base == EmptySHA { + cmd.AddDynamicArguments(head) + } else { + 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 5b50bc82ad..603aabde42 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") + repo, err := openRepositoryWithDefaultContext(bareRepo1Path) + assert.NoError(t, err) + defer repo.Close() + + testCases := []struct { + base, head string + files []string + }{ + { + EmptySHA, + "95bb4d39648ee7e325106df01a621c530863a653", + []string{"file1.txt"}, + }, + { + EmptySHA, + "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", + []string{"file2.txt"}, + }, + { + "95bb4d39648ee7e325106df01a621c530863a653", + "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", + []string{"file2.txt"}, + }, + { + EmptyTreeSHA, + "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", + []string{"file1.txt", "file2.txt"}, + }, + } + + for _, tc := range testCases { + changedFiles, err := repo.GetFilesChangedBetween(tc.base, tc.head) + assert.NoError(t, err) + assert.ElementsMatch(t, tc.files, changedFiles) + } +} diff --git a/modules/git/sha1.go b/modules/git/sha1.go index 7d9d9776da..8d6403e865 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