Skip to content

Commit ca45891

Browse files
zeripathtechknowlogick
authored andcommitted
Fix GetFilesChangedBetween if the file name may be escaped (go-gitea#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 go-gitea#22568 (comment) Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent 464bbd7 commit ca45891

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

modules/git/repo_compare.go

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

278278
// GetFilesChangedBetween returns a list of all files that have been changed between the given commits
279279
func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, error) {
280-
stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path})
280+
stdout, _, err := NewCommand(repo.Ctx, "diff", "--name-only", "-z").AddDynamicArguments(base + ".." + head).RunStdString(&RunOpts{Dir: repo.Path})
281281
if err != nil {
282282
return nil, err
283283
}
284-
return strings.Split(stdout, "\n"), err
284+
split := strings.Split(stdout, "\000")
285+
286+
// Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty.
287+
if len(split) > 0 {
288+
split = split[:len(split)-1]
289+
}
290+
291+
return split, err
285292
}
286293

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

0 commit comments

Comments
 (0)