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 11, 2024
1 parent 9092b05 commit ebe17d7
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pkg/git/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"regexp"
"strconv"
"strings"

Expand All @@ -28,6 +29,8 @@ type FilePatch struct {
Chunks Chunks
}

var pathSplitterRegexp = regexp.MustCompile(`^(([^\.]+)\.([^\s]+))\s(.+)$`)

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

Expand Down Expand Up @@ -113,20 +116,33 @@ func parseDiff(scanner *linescanner.Scanner) ([]FilePatch, error) {
}

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

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

return fromPath, toPath, nil
}

func splitPath(value string, offset int, expectedLen int) []string {
parts := strings.Split(value, " ")
if len(parts) == expectedLen {
return parts
}

// parts may contain spaces
// TODO: how to handle spaces plus multi-fullstop filenames e.g "who would.do.this"?
valueWithoutOffset := strings.Join(parts[offset:], " ")
matches := pathSplitterRegexp.FindStringSubmatch(valueWithoutOffset)
return []string{matches[1], matches[4]}
}

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

Expand Down

0 comments on commit ebe17d7

Please sign in to comment.