Skip to content

Commit 7960d2d

Browse files
committed
fix: use newline-aware trimming in diff() for backward compatibility
Preserve newlines during whitespace trimming so diff() can detect whether output contains multiple lines. When newlines are present, split by newline (handles filenames with spaces). When absent, fall back to strings.Fields() for backward compat with custom diff commands that return a single filename without a trailing newline.
1 parent 0df7073 commit 7960d2d

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

pipeline.go

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

108-
trimmed := strings.TrimSpace(output)
108+
trimmed := strings.Trim(output, " \t\r\v\f")
109109
if trimmed == "" {
110-
return nil, nil
110+
return []string{}, nil
111111
}
112112

113113
var fields []string
114-
for _, line := range strings.Split(trimmed, "\n") {
115-
line = strings.TrimSpace(line)
116-
if line != "" {
117-
fields = append(fields, line)
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+
}
118120
}
121+
} else {
122+
fields = strings.Fields(trimmed)
119123
}
120124

121125
paths := make([]string, 0, len(fields))

pipeline_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestDiffWithQuotedPaths(t *testing.T) {
120120
"projects/test/pages/17_🪁_testfile.py",
121121
"normal/file.txt",
122122
}
123-
got, err := diff(`printf '"projects/test/pages/17_\360\237\252\201_testfile.py"\nnormal/file.txt'`)
123+
got, err := diff(`printf '"projects/test/pages/17_\360\237\252\201_testfile.py"\nnormal/file.txt\n'`)
124124
assert.NoError(t, err)
125125
assert.Equal(t, want, got)
126126
}
@@ -135,7 +135,7 @@ func TestDiffWithSpacesInFilenames(t *testing.T) {
135135
}
136136

137137
// printf produces newline-separated output, just like git diff --name-only
138-
got, err := diff(`printf 'directory/File Name With Spaces.md\nanother dir/some file.txt\nno-spaces.go'`)
138+
got, err := diff(`printf 'directory/File Name With Spaces.md\nanother dir/some file.txt\nno-spaces.go\n'`)
139139
assert.NoError(t, err)
140140
assert.Equal(t, want, got)
141141
}
@@ -145,7 +145,7 @@ func TestDiffSingleFile(t *testing.T) {
145145
"services/foo/serverless.yml",
146146
}
147147

148-
got, err := diff(`printf 'services/foo/serverless.yml'`)
148+
got, err := diff(`printf 'services/foo/serverless.yml\n'`)
149149
assert.NoError(t, err)
150150
assert.Equal(t, want, got)
151151
}
@@ -156,7 +156,7 @@ func TestDiffWithSpacesInFilenamesSingleFile(t *testing.T) {
156156
}
157157

158158
// printf produces newline-separated output, just like git diff --name-only
159-
got, err := diff(`printf 'directory/File Name With Spaces.md'`)
159+
got, err := diff(`printf 'directory/File Name With Spaces.md\n'`)
160160
assert.NoError(t, err)
161161
assert.Equal(t, want, got)
162162
}

0 commit comments

Comments
 (0)