Skip to content

Commit 2f1d968

Browse files
GiteaBotzeripathtechknowlogick
authored
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 #22568 (comment) Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent 0c212b3 commit 2f1d968

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Diff for: modules/git/repo_compare.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,18 @@ func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
282282

283283
// GetFilesChangedBetween returns a list of all files that have been changed between the given commits
284284
func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, error) {
285-
stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path})
285+
stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only", "-z").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path})
286286
if err != nil {
287287
return nil, err
288288
}
289-
return strings.Split(stdout, "\n"), err
289+
split := strings.Split(stdout, "\000")
290+
291+
// Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty.
292+
if len(split) > 0 {
293+
split = split[:len(split)-1]
294+
}
295+
296+
return split, err
290297
}
291298

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

0 commit comments

Comments
 (0)