From 78a823e76706ed0bb91ecd978a7873da60d38492 Mon Sep 17 00:00:00 2001 From: Laurent Cahour Date: Thu, 28 Jul 2022 10:38:12 +0200 Subject: [PATCH] Make GetCommitFileStatus list all files impacted by a merge commit Enable APIs (such as 'api/v1/repos/{owner}/{repo}/git/commit/{sha1}') to correctly list all files impacted by a commit (making it mirror GitHub API behaviour). Previously would only return merge commit files if they where associated with a conflict resolution. module/git/commit.go GetCommitFileStatus now use the git log '-m' flag instead of '-c' which does not seem to work module/git/commit.go parseCommitFileStatus was modified to handle the change in format for merge commits --- modules/git/commit.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 32589f5349..810c9a9e31 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -455,17 +455,18 @@ func NewCommitFileStatus() *CommitFileStatus { func parseCommitFileStatus(fileStatus *CommitFileStatus, stdout io.Reader) { rd := bufio.NewReader(stdout) - peek, err := rd.Peek(1) - if err != nil { - if err != io.EOF { - log.Error("Unexpected error whilst reading from git log --name-status. Error: %v", err) - } - return - } - if peek[0] == '\n' || peek[0] == '\x00' { - _, _ = rd.Discard(1) - } for { + // In case of merge commits, Git will add a separator between each + peek, err := rd.Peek(1) + if err != nil { + if err != io.EOF { + log.Error("Unexpected error whilst reading from git log --name-status. Error: %v", err) + } + return + } + if peek[0] == '\n' || peek[0] == '\x00' { + _, _ = rd.Discard(1) + } modifier, err := rd.ReadSlice('\x00') if err != nil { if err != io.EOF { @@ -503,7 +504,7 @@ func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*Commi }() stderr := new(bytes.Buffer) - args := []string{"log", "--name-status", "-c", "--pretty=format:", "--parents", "--no-renames", "-z", "-1", commitID} + args := []string{"log", "--name-status", "-m", "--pretty=format:", "--parents", "--no-renames", "-z", "-1", commitID} err := NewCommand(ctx, args...).Run(&RunOpts{ Dir: repoPath,