Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filenames with spaces in diff scan #1696

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading