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.
+41
View File
@@ -4,10 +4,14 @@
package git
import (
"path/filepath"
"strings"
"testing"
"gitea.dev/modules/git/gitcmd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const exampleDiff = `diff --git a/README.md b/README.md
@@ -190,3 +194,40 @@ func TestParseDiffHunkString(t *testing.T) {
assert.Equal(t, 19, rightLine)
assert.Equal(t, 5, rightHunk)
}
func TestGetAffectedFilesSpecialNames(t *testing.T) {
repoDir := filepath.Join(t.TempDir(), "repo.git")
require.NoError(t, gitcmd.NewCommand("init").AddDynamicArguments(repoDir).Run(t.Context()))
stdin := `commit refs/heads/main
committer User <user@example.com> 1714310400 +0000
data 1
a
M 100644 inline base.txt
data 1
x
commit refs/heads/main
committer User <user@example.com> 1714310401 +0000
data 1
b
M 100644 inline "protected/we\"ird.md"
data 1
x
M 100644 inline "protected/\344\270\255\346\226\207.md"
data 1
x
M 100644 inline protected/normal.md
data 1
x
R base.txt protected/renamed.md
`
require.NoError(t, gitcmd.NewCommand("fast-import").WithDir(repoDir).WithStdinBytes([]byte(stdin)).Run(t.Context()))
repo, err := OpenRepository(t.Context(), mockRepository(repoDir))
require.NoError(t, err)
defer repo.Close()
files, err := GetAffectedFiles(t.Context(), repo, "main", "main~1", "main", nil)
require.NoError(t, err)
assert.ElementsMatch(t, []string{`protected/we"ird.md`, "protected/中文.md", "protected/normal.md", "base.txt", "protected/renamed.md"}, files)
}