fix(git): list affected files NUL-separated so special names are not quoted (#40362)

Backport of the `GetAffectedFiles` fix from main (#40326) to v28.

`git diff --name-only` C-quotes paths containing characters such as `"`
or non-ASCII bytes, so they never matched protected file patterns. Use
`diff-tree -r -z` and split on NUL. Renames now report both old and new
paths.

Assisted-by: Codet:claude
This commit is contained in:
Lunny Xiao authored and GitHub committed 2026-11-14 12:19:53 +00:00
1 parent 0c6645958b
commit 06a2286930
2 files changed
+50 -22

No files matched your search

+9 -22
View File
@@ -308,31 +308,18 @@ func GetAffectedFiles(ctx context.Context, repo *Repository, branchName, oldComm
oldCommitID = startCommitID
}
affectedFiles := make([]string, 0, 32)
// Run `git diff --name-only` to get the names of the changed files
cmd := gitcmd.NewCommand("diff", "--name-only").AddDynamicArguments(oldCommitID, newCommitID)
stdoutReader, stdoutReaderClose := cmd.MakeStdoutPipe()
defer stdoutReaderClose()
err := cmd.WithEnv(env).WithRepo(repo).
WithPipelineFunc(func(ctx gitcmd.Context) error {
// Now scan the output from the command
scanner := bufio.NewScanner(stdoutReader)
for scanner.Scan() {
path := strings.TrimSpace(scanner.Text())
if len(path) == 0 {
continue
}
affectedFiles = append(affectedFiles, path)
}
return scanner.Err()
}).
Run(ctx)
// use "diff-tree -r -z": paths are NUL-separated (never quoted) and renames report both the old and new path
cmd := gitcmd.NewCommand("diff-tree", "--name-only", "--no-commit-id", "-r", "-z").AddDynamicArguments(oldCommitID, newCommitID)
stdout, _, err := cmd.WithEnv(env).WithRepo(repo).RunStdString(ctx)
if err != nil {
log.Error("Unable to get affected files for commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.LogString(), err)
return nil, err
}
return affectedFiles, err
affectedFiles := strings.Split(strings.TrimSuffix(stdout, "\x00"), "\x00")
if len(affectedFiles) == 1 && affectedFiles[0] == "" {
affectedFiles = nil
}
return affectedFiles, nil
}
// GetReverseRawDiff dumps the reverse diff results of repository in given commit ID to io.Writer.