From 06a22869309d4bfd92e3663d19da1edaa0d0ae7d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 14 Nov 2026 12:19:53 +0000 Subject: [PATCH] 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 --- modules/git/diff.go | 31 +++++++++--------------------- modules/git/diff_test.go | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/modules/git/diff.go b/modules/git/diff.go index 5326d4024a8..5addba762cb 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -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. diff --git a/modules/git/diff_test.go b/modules/git/diff_test.go index 454405fdcd6..1b2598827c0 100644 --- a/modules/git/diff_test.go +++ b/modules/git/diff_test.go @@ -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 1714310400 +0000 +data 1 +a +M 100644 inline base.txt +data 1 +x + +commit refs/heads/main +committer User 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) +}