Skip to content

Support getting changed files when commit ID is EmptySHA #26290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,17 @@ 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) {
Expand Down
31 changes: 31 additions & 0 deletions modules/git/repo_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,37 @@ 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) {
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})
} else {
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)
Expand Down
35 changes: 35 additions & 0 deletions modules/git/repo_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,38 @@ 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 {
changedFiles, err := repo.GetCommitFilesChanged(tc.CommitID)
assert.NoError(t, err)
assert.ElementsMatch(t, changedFiles, tc.ExpectedFiles)
}
}