Skip to content

Commit 0dc5fc5

Browse files
committed
Fix whitespace splitting bug in diff() for filenames with spaces
Split on newlines when the diff output contains them (as git diff --name-only produces), preserving spaces within filenames. Fall back to strings.Fields() for single-line output to maintain backward compatibility with existing diff commands.
1 parent b32a597 commit 0dc5fc5

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

pipeline.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,23 @@ func diff(command string) ([]string, error) {
105105
return nil, fmt.Errorf("diff command failed: %v", err)
106106
}
107107

108-
fields := strings.Fields(strings.TrimSpace(output))
108+
trimmed := strings.TrimSpace(output)
109+
110+
// Git diff --name-only outputs one file per line. Split on newlines to
111+
// preserve filenames that contain spaces. Fall back to strings.Fields
112+
// for single-line output (e.g. legacy diff commands) for backward compat.
113+
var fields []string
114+
if strings.Contains(trimmed, "\n") {
115+
for _, line := range strings.Split(trimmed, "\n") {
116+
line = strings.TrimSpace(line)
117+
if line != "" {
118+
fields = append(fields, line)
119+
}
120+
}
121+
} else {
122+
fields = strings.Fields(trimmed)
123+
}
124+
109125
paths := make([]string, 0, len(fields))
110126

111127
for _, field := range fields {

0 commit comments

Comments
 (0)