Skip to content

Commit

Permalink
Fix diff path unquoting
Browse files Browse the repository at this point in the history
services/gitdiff/gitdiff.go whereby there it assumed that the path would
always be quoted on both sides

This PR simplifies the code here and uses fmt.Fscanf to parse the
strings as necessary.

Fix go-gitea#12546

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Aug 21, 2020
1 parent 7c2cf23 commit dd0b192
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"os/exec"
"regexp"
"sort"
"strconv"
"strings"

"code.gitea.io/gitea/models"
Expand Down Expand Up @@ -570,40 +569,28 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
break
}

var middle int

// Note: In case file name is surrounded by double quotes (it happens only in git-shell).
// e.g. diff --git "a/xxx" "b/xxx"
hasQuote := line[len(cmdDiffHead)] == '"'
if hasQuote {
middle = strings.Index(line, ` "b/`)
var a string
var b string

rd := strings.NewReader(line[len(cmdDiffHead):])
char, _ := rd.ReadByte()
_ = rd.UnreadByte()
if char == '"' {
fmt.Fscanf(rd, "%q ", &a)
} else {
middle = strings.Index(line, " b/")
fmt.Fscanf(rd, "%s ", &a)
}

beg := len(cmdDiffHead)
a := line[beg+2 : middle]
b := line[middle+3:]

if hasQuote {
// Keep the entire string in double quotes for now
a = line[beg:middle]
b = line[middle+1:]

var err error
a, err = strconv.Unquote(a)
if err != nil {
return nil, fmt.Errorf("Unquote: %v", err)
}
b, err = strconv.Unquote(b)
if err != nil {
return nil, fmt.Errorf("Unquote: %v", err)
}
// Now remove the /a /b
a = a[2:]
b = b[2:]

char, _ = rd.ReadByte()
_ = rd.UnreadByte()
if char == '"' {
fmt.Fscanf(rd, "%q", &b)
} else {
fmt.Fscanf(rd, "%s", &b)
}
a = a[2:]
b = b[2:]

curFile = &DiffFile{
Name: b,
Expand Down

0 comments on commit dd0b192

Please sign in to comment.