Skip to content

Commit

Permalink
Fix GetFilesChangedBetween if the file name may be escaped (#23272)
Browse files Browse the repository at this point in the history
The code for GetFilesChangedBetween uses `git diff --name-only
base..head` to get the names of files changed between base and head
however this forgets that git will escape certain values.

This PR simply switches to use `-z` which has the `NUL` character as the
separator.

Ref #22568 (comment)

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
  • Loading branch information
zeripath and techknowlogick committed Mar 3, 2023
1 parent 7f9d58f commit 5c4075e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions modules/git/repo_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,18 @@ 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
func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, error) {
stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only").AddDynamicArguments(base + ".." + head).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
}
return strings.Split(stdout, "\n"), 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
}

// GetDiffFromMergeBase generates and return patch data from merge base to head
Expand Down

0 comments on commit 5c4075e

Please sign in to comment.