Skip to content

Commit 44da91e

Browse files
authored
fix: filenames with spaces in diff scan (#1696)
* fix: do not sign test commits * fix: filenames with spaces in diff scan
1 parent 9092b05 commit 44da91e

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

pkg/git/diff.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type FilePatch struct {
2828
Chunks Chunks
2929
}
3030

31+
const SRC_PREFIX = "caf67b1d-c9b7-44ba-9b85-bd94727547af"
32+
const DST_PREFIX = "7dcfe536-96a9-49c5-8b86-7caa3d9e5e9c"
33+
3134
func Diff(rootDir, baseRef string) ([]FilePatch, error) {
3235
var result []FilePatch
3336

@@ -40,7 +43,8 @@ func Diff(rootDir, baseRef string) ([]FilePatch, error) {
4043
"--first-parent",
4144
"--find-renames",
4245
"--break-rewrites",
43-
"--no-prefix",
46+
"--src-prefix=" + SRC_PREFIX,
47+
"--dst-prefix=" + DST_PREFIX,
4448
"--no-color",
4549
baseRef,
4650
"--",
@@ -113,20 +117,40 @@ func parseDiff(scanner *linescanner.Scanner) ([]FilePatch, error) {
113117
}
114118

115119
func parseDiffHeader(value string) (string, string, error) {
116-
parts := strings.Split(value, " ")
117-
fromPath, err := unquoteFilename(parts[2])
120+
rawFromPath, rawToPath := splitPaths(value)
121+
fromPath, err := unquoteFilename(rawFromPath)
118122
if err != nil {
119123
return "", "", fmt.Errorf("error parsing header 'from' path: %w", err)
120124
}
121125

122-
toPath, err := unquoteFilename(parts[3])
126+
toPath, err := unquoteFilename(rawToPath)
123127
if err != nil {
124128
return "", "", fmt.Errorf("error parsing header 'to' path: %w", err)
125129
}
126130

127131
return fromPath, toPath, nil
128132
}
129133

134+
func splitPaths(value string) (fromPath string, toPath string) {
135+
split1 := strings.Split(value, SRC_PREFIX)[1]
136+
split2 := strings.Split(split1, DST_PREFIX)
137+
138+
fromPath = strings.TrimSpace(split2[0])
139+
toPath = strings.TrimSpace(split2[1])
140+
141+
// handle trailing whitespaces and missing quotation marks
142+
// e.g. `foo\\t.txt \"`
143+
if strings.Contains(fromPath, "\"") {
144+
fromPath = "\"" + strings.TrimSpace(strings.ReplaceAll(fromPath, "\"", "")) + "\""
145+
}
146+
147+
if strings.Contains(toPath, "\"") {
148+
toPath = "\"" + strings.ReplaceAll(toPath, "\"", "") + "\""
149+
}
150+
151+
return fromPath, toPath
152+
}
153+
130154
func parseChunkHeader(value string) (Chunk, error) {
131155
parts := strings.Split(value, " ")
132156

pkg/git/diff_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ var _ = Describe("Diff", func() {
105105
{FromPath: fromPath, ToPath: toPath},
106106
}))
107107
})
108+
109+
fromPath = "from bar.txt"
110+
toPath = "to foo.txt"
111+
112+
It("decodes the paths correctly with whitespace in both from and to path", func() {
113+
Expect(git.Diff(tempDir, baseSHA)).To(ConsistOf([]git.FilePatch{
114+
{FromPath: fromPath, ToPath: toPath},
115+
}))
116+
})
117+
118+
fromPath = "from bar.txt"
119+
toPath = "to.txt"
120+
121+
It("decodes the paths correctly with whitespace in from path", func() {
122+
Expect(git.Diff(tempDir, baseSHA)).To(ConsistOf([]git.FilePatch{
123+
{FromPath: fromPath, ToPath: toPath},
124+
}))
125+
})
108126
})
109127

110128
When("a file contains changes", func() {

pkg/git/git_suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func addAndCommit(dir string) {
3333
dir,
3434
"-c", "user.name=Bearer CI",
3535
36+
"-c", "commit.gpgSign=false",
3637
"commit",
3738
"--allow-empty-message",
3839
"--message=",

0 commit comments

Comments
 (0)