From 2f1d968b272c247dd8b46c5be7d308cb1888d24a Mon Sep 17 00:00:00 2001 From: Giteabot Date: Sat, 4 Mar 2023 02:11:50 -0500 Subject: [PATCH] Fix GetFilesChangedBetween if the file name may be escaped (#23272) (#23278) Backport #23272 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 https://github.com/go-gitea/gitea/pull/22568#discussion_r1123138096 Signed-off-by: Andrew Thornton Co-authored-by: zeripath Co-authored-by: techknowlogick --- modules/git/repo_compare.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 7575b11658..12c65ac731 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -282,11 +282,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