mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
Implement file name parsing for git headers
Also change how error handling works for similarity score parsing.
This commit is contained in:
+97
-33
@@ -49,6 +49,8 @@ const (
|
||||
fileHeaderPrefix = "diff --git "
|
||||
oldFilePrefix = "--- "
|
||||
newFilePrefix = "+++ "
|
||||
|
||||
devNull = "/dev/null"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -224,91 +226,98 @@ func parseFragmentHeader(f *Fragment, header string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseGitHeaderLine(f *File, line string) (more bool, err error) {
|
||||
func parseGitHeaderLine(f *File, line string) (next bool, err error) {
|
||||
match := func(s string) bool {
|
||||
if strings.HasPrefix(line, s) {
|
||||
// TODO(bkeyes): strip final line separator too
|
||||
line = line[len(s):]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if line[len(line)-1] == '\n' {
|
||||
line = line[:len(line)-1]
|
||||
}
|
||||
|
||||
switch {
|
||||
case match(fragmentHeaderPrefix):
|
||||
// start of a fragment indicates the end of the header
|
||||
return false, nil
|
||||
|
||||
case match(oldFilePrefix):
|
||||
existing := f.OldName
|
||||
|
||||
f.OldName, _, err = parseName(line, '\t')
|
||||
if err == nil {
|
||||
err = verifyName(f.OldName, existing, f.IsNew, "old")
|
||||
}
|
||||
|
||||
case match(newFilePrefix):
|
||||
existing := f.NewName
|
||||
|
||||
f.NewName, _, err = parseName(line, '\t')
|
||||
if err == nil {
|
||||
err = verifyName(f.NewName, existing, f.IsDelete, "new")
|
||||
}
|
||||
|
||||
case match("old mode "):
|
||||
if f.OldMode, err = parseModeLine(line); err != nil {
|
||||
return false, err
|
||||
}
|
||||
f.OldMode, err = parseModeLine(line)
|
||||
|
||||
case match("new mode "):
|
||||
if f.NewMode, err = parseModeLine(line); err != nil {
|
||||
return false, err
|
||||
}
|
||||
f.NewMode, err = parseModeLine(line)
|
||||
|
||||
case match("deleted file mode "):
|
||||
// TODO(bkeyes): maybe set old name from default?
|
||||
f.IsDelete = true
|
||||
if f.OldMode, err = parseModeLine(line); err != nil {
|
||||
return false, err
|
||||
}
|
||||
f.OldMode, err = parseModeLine(line)
|
||||
|
||||
case match("new file mode "):
|
||||
// TODO(bkeyes): maybe set new name from default?
|
||||
f.IsNew = true
|
||||
if f.NewMode, err = parseModeLine(line); err != nil {
|
||||
return false, err
|
||||
}
|
||||
f.NewMode, err = parseModeLine(line)
|
||||
|
||||
case match("copy from "):
|
||||
f.IsCopy = true
|
||||
// TODO(bkeyes): set old name
|
||||
f.OldName, _, err = parseName(line, 0)
|
||||
|
||||
case match("copy to "):
|
||||
f.IsCopy = true
|
||||
// TODO(bkeyes): set new name
|
||||
f.NewName, _, err = parseName(line, 0)
|
||||
|
||||
case match("rename old "):
|
||||
f.IsRename = true
|
||||
// TODO(bkeyes): set old name
|
||||
f.OldName, _, err = parseName(line, 0)
|
||||
|
||||
case match("rename new "):
|
||||
f.IsRename = true
|
||||
// TODO(bkeyes): set new name
|
||||
f.NewName, _, err = parseName(line, 0)
|
||||
|
||||
case match("rename from "):
|
||||
f.IsRename = true
|
||||
// TODO(bkeyes): set old name
|
||||
f.OldName, _, err = parseName(line, 0)
|
||||
|
||||
case match("rename to "):
|
||||
f.IsRename = true
|
||||
// TODO(bkeyes): set new name
|
||||
f.NewName, _, err = parseName(line, 0)
|
||||
|
||||
case match("similarity index "):
|
||||
f.Score = parseScoreLine(line)
|
||||
f.Score, err = parseScoreLine(line)
|
||||
|
||||
case match("dissimilarity index "):
|
||||
f.Score = parseScoreLine(line)
|
||||
f.Score, err = parseScoreLine(line)
|
||||
|
||||
case match("index "):
|
||||
|
||||
default:
|
||||
// unknown line also indicates the end of the header
|
||||
// this usually happens if the diff is empty
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
return err == nil, err
|
||||
}
|
||||
|
||||
func parseModeLine(s string) (os.FileMode, error) {
|
||||
s = strings.TrimSuffix(s, "\n")
|
||||
|
||||
mode, err := strconv.ParseInt(s, 8, 32)
|
||||
if err != nil {
|
||||
nerr := err.(*strconv.NumError)
|
||||
@@ -318,13 +327,68 @@ func parseModeLine(s string) (os.FileMode, error) {
|
||||
return os.FileMode(mode), nil
|
||||
}
|
||||
|
||||
func parseScoreLine(s string) int {
|
||||
s = strings.TrimSuffix(s, "\n")
|
||||
|
||||
// gitdiff_similarity / gitdiff_dissimilarity ignore invalid scores
|
||||
score, _ := strconv.ParseInt(s, 10, 32)
|
||||
if score <= 100 {
|
||||
return int(score)
|
||||
func parseScoreLine(s string) (int, error) {
|
||||
score, err := strconv.ParseInt(s, 10, 32)
|
||||
if err != nil {
|
||||
nerr := err.(*strconv.NumError)
|
||||
return 0, fmt.Errorf("invalid score line: %v", nerr.Err)
|
||||
}
|
||||
return 0
|
||||
|
||||
if score >= 100 {
|
||||
score = 0
|
||||
}
|
||||
return int(score), nil
|
||||
}
|
||||
|
||||
// parseName extracts a file name from the start of a string and returns the
|
||||
// name and the index of the first character after the name. If the name is
|
||||
// unquoted and term is non-0, parsing stops at the first occurance of term.
|
||||
// Otherwise parsing of unquoted names stops at the first space or tab.
|
||||
func parseName(s string, term byte) (name string, n int, err error) {
|
||||
// TODO(bkeyes): remove double forward slashes in parsed named
|
||||
|
||||
if len(s) > 0 && s[0] == '"' {
|
||||
// find matching end quote and then unquote the section
|
||||
for n = 1; n < len(s); n++ {
|
||||
if s[n] == '"' && s[n-1] != '\\' {
|
||||
break
|
||||
}
|
||||
}
|
||||
if n == 1 {
|
||||
err = fmt.Errorf("missing name")
|
||||
return
|
||||
}
|
||||
n++
|
||||
name, err = strconv.Unquote(s[:n])
|
||||
return
|
||||
}
|
||||
|
||||
for n = 0; n < len(s); n++ {
|
||||
if term > 0 && s[n] == term {
|
||||
break
|
||||
}
|
||||
if term == 0 && (s[n] == ' ' || s[n] == '\t') {
|
||||
break
|
||||
}
|
||||
}
|
||||
if n == 0 {
|
||||
err = fmt.Errorf("missing name")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// verifyName checks parsed names against state set by previous header lines
|
||||
func verifyName(parsed, existing string, isNull bool, side string) error {
|
||||
if existing != "" {
|
||||
if isNull {
|
||||
return fmt.Errorf("expected %s, got %s", devNull, existing)
|
||||
}
|
||||
if existing != parsed {
|
||||
return fmt.Errorf("inconsistent %s filename", side)
|
||||
}
|
||||
}
|
||||
if isNull && parsed != devNull {
|
||||
return fmt.Errorf("expected %s", devNull)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user