Skip to content

Commit

Permalink
fix: filenames with spaces in diff scan
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet committed Oct 17, 2024
1 parent aed0f31 commit b7de36c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
32 changes: 28 additions & 4 deletions pkg/git/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type FilePatch struct {
Chunks Chunks
}

const SRC_PREFIX = "caf67b1d-c9b7-44ba-9b85-bd94727547af"
const DST_PREFIX = "7dcfe536-96a9-49c5-8b86-7caa3d9e5e9c"

func Diff(rootDir, baseRef string) ([]FilePatch, error) {
var result []FilePatch

Expand All @@ -40,7 +43,8 @@ func Diff(rootDir, baseRef string) ([]FilePatch, error) {
"--first-parent",
"--find-renames",
"--break-rewrites",
"--no-prefix",
"--src-prefix=" + SRC_PREFIX,
"--dst-prefix=" + DST_PREFIX,
"--no-color",
baseRef,
"--",
Expand Down Expand Up @@ -113,20 +117,40 @@ func parseDiff(scanner *linescanner.Scanner) ([]FilePatch, error) {
}

func parseDiffHeader(value string) (string, string, error) {
parts := strings.Split(value, " ")
fromPath, err := unquoteFilename(parts[2])
rawFromPath, rawToPath := splitPaths(value)
fromPath, err := unquoteFilename(rawFromPath)
if err != nil {
return "", "", fmt.Errorf("error parsing header 'from' path: %w", err)
}

toPath, err := unquoteFilename(parts[3])
toPath, err := unquoteFilename(rawToPath)
if err != nil {
return "", "", fmt.Errorf("error parsing header 'to' path: %w", err)
}

return fromPath, toPath, nil
}

func splitPaths(value string) (fromPath string, toPath string) {
split1 := strings.Split(value, SRC_PREFIX)[1]
split2 := strings.Split(split1, DST_PREFIX)

fromPath = strings.TrimSpace(split2[0])
toPath = strings.TrimSpace(split2[1])

// handle trailing whitespaces and missing quotation marks
// e.g. `foo\\t.txt \"`
if strings.Contains(fromPath, "\"") {
fromPath = "\"" + strings.TrimSpace(strings.ReplaceAll(fromPath, "\"", "")) + "\""
}

if strings.Contains(toPath, "\"") {
toPath = "\"" + strings.ReplaceAll(toPath, "\"", "") + "\""
}

return fromPath, toPath
}

func parseChunkHeader(value string) (Chunk, error) {
parts := strings.Split(value, " ")

Expand Down
11 changes: 10 additions & 1 deletion pkg/git/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var _ = Describe("Diff", func() {

When("paths contain characters requiring quoting", func() {
fromPath := "from\t.txt"
toPath := "to\t.txt"
toPath := "to text.txt"

BeforeEach(func() {
writeFile(tempDir, fromPath, "1\n2")
Expand All @@ -105,6 +105,15 @@ var _ = Describe("Diff", func() {
{FromPath: fromPath, ToPath: toPath},
}))
})

fromPath = "from bar.txt"
toPath = "to.txt"

It("decodes the paths correctly with whitespace in from path", func() {
Expect(git.Diff(tempDir, baseSHA)).To(ConsistOf([]git.FilePatch{
{FromPath: fromPath, ToPath: toPath},
}))
})
})

When("a file contains changes", func() {
Expand Down

0 comments on commit b7de36c

Please sign in to comment.