Skip to content

Commit

Permalink
fix: filenames with spaces in diff scan (#1696)
Browse files Browse the repository at this point in the history
* fix: do not sign test commits

* fix: filenames with spaces in diff scan
  • Loading branch information
elsapet authored Oct 17, 2024
1 parent 9092b05 commit 44da91e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 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
18 changes: 18 additions & 0 deletions pkg/git/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ var _ = Describe("Diff", func() {
{FromPath: fromPath, ToPath: toPath},
}))
})

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

It("decodes the paths correctly with whitespace in both from and to path", func() {
Expect(git.Diff(tempDir, baseSHA)).To(ConsistOf([]git.FilePatch{
{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
1 change: 1 addition & 0 deletions pkg/git/git_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func addAndCommit(dir string) {
dir,
"-c", "user.name=Bearer CI",
"-c", "[email protected]",
"-c", "commit.gpgSign=false",
"commit",
"--allow-empty-message",
"--message=",
Expand Down

0 comments on commit 44da91e

Please sign in to comment.