Skip to content

Commit e910528

Browse files
authored
Merge pull request #162 from siredmar/fix/git-diff-compat
Support whitespaces in filenames
2 parents 50a9e56 + 7960d2d commit e910528

2 files changed

Lines changed: 56 additions & 8 deletions

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.Trim(output, " \t\r\v\f")
109+
if trimmed == "" {
110+
return []string{}, nil
111+
}
112+
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 {

pipeline_test.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,7 @@ func TestDiff(t *testing.T) {
9999
"README.md",
100100
}
101101

102-
got, err := diff(`echo services/foo/serverless.yml
103-
services/bar/config.yml
104-
105-
ops/bar/config.yml
106-
README.md`)
102+
got, err := diff(`printf 'services/foo/serverless.yml\nservices/bar/config.yml\n\nops/bar/config.yml\nREADME.md\n'`)
107103

108104
assert.NoError(t, err)
109105
assert.Equal(t, want, got)
@@ -114,7 +110,7 @@ func TestDiffWithSubshell(t *testing.T) {
114110
"user-service/infrastructure/cloudfront.yaml",
115111
"user-service/serverless.yaml",
116112
}
117-
got, err := diff("echo $(cat e2e/multiple-paths)")
113+
got, err := diff("cat e2e/multiple-paths")
118114
assert.NoError(t, err)
119115
assert.Equal(t, want, got)
120116
}
@@ -124,7 +120,43 @@ func TestDiffWithQuotedPaths(t *testing.T) {
124120
"projects/test/pages/17_🪁_testfile.py",
125121
"normal/file.txt",
126122
}
127-
got, err := diff(`printf '"projects/test/pages/17_\360\237\252\201_testfile.py" normal/file.txt'`)
123+
got, err := diff(`printf '"projects/test/pages/17_\360\237\252\201_testfile.py"\nnormal/file.txt\n'`)
124+
assert.NoError(t, err)
125+
assert.Equal(t, want, got)
126+
}
127+
128+
func TestDiffWithSpacesInFilenames(t *testing.T) {
129+
// Simulates git diff --name-only output with one filename per line,
130+
// where some filenames contain spaces.
131+
want := []string{
132+
"directory/File Name With Spaces.md",
133+
"another dir/some file.txt",
134+
"no-spaces.go",
135+
}
136+
137+
// 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\n'`)
139+
assert.NoError(t, err)
140+
assert.Equal(t, want, got)
141+
}
142+
143+
func TestDiffSingleFile(t *testing.T) {
144+
want := []string{
145+
"services/foo/serverless.yml",
146+
}
147+
148+
got, err := diff(`printf 'services/foo/serverless.yml\n'`)
149+
assert.NoError(t, err)
150+
assert.Equal(t, want, got)
151+
}
152+
153+
func TestDiffWithSpacesInFilenamesSingleFile(t *testing.T) {
154+
want := []string{
155+
"directory/File Name With Spaces.md",
156+
}
157+
158+
// printf produces newline-separated output, just like git diff --name-only
159+
got, err := diff(`printf 'directory/File Name With Spaces.md\n'`)
128160
assert.NoError(t, err)
129161
assert.Equal(t, want, got)
130162
}

0 commit comments

Comments
 (0)