Browse Source

Fix ParsePatch function to work with quoted diff --git strings (#6323)

* Fix ParsePatch to work properly with quoted diff --git string

Currently ParsePatch fails when a diff contains a quoted diff line like:

diff --git "a/file" "b/file"

This patch makes it properly parse the line when that happens.

Fixes #6309

* Add test for regular case while here

* Simplify string modification
tags/v1.9.0-dev
mrsdizzie 5 years ago
parent
commit
7780ea8890
2 changed files with 65 additions and 0 deletions
  1. 10
    0
      models/git_diff.go
  2. 55
    0
      models/git_diff_test.go

+ 10
- 0
models/git_diff.go View File

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

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

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

} }


curFile = &DiffFile{ curFile = &DiffFile{
} }
} }
} }

return diff, nil return diff, nil
} }



+ 55
- 0
models/git_diff_test.go View File

"strings" "strings"
"testing" "testing"


"code.gitea.io/gitea/modules/setting"

dmp "github.com/sergi/go-diff/diffmatchpatch" dmp "github.com/sergi/go-diff/diffmatchpatch"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
println(result) println(result)
} }


func TestParsePatch(t *testing.T) {
var diff = `diff --git "a/README.md" "b/README.md"
--- a/README.md
+++ b/README.md
@@ -1,3 +1,6 @@
# gitea-github-migrator
+
+ Build Status
- Latest Release
Docker Pulls
+ cut off
+ cut off`
result, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff))
if err != nil {
t.Errorf("ParsePatch failed: %s", err)
}
println(result)

var diff2 = `diff --git "a/A \\ B" "b/A \\ B"
--- "a/A \\ B"
+++ "b/A \\ B"
@@ -1,3 +1,6 @@
# gitea-github-migrator
+
+ Build Status
- Latest Release
Docker Pulls
+ cut off
+ cut off`
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2))
if err != nil {
t.Errorf("ParsePatch failed: %s", err)
}
println(result)

var diff3 = `diff --git a/README.md b/README.md
--- a/README.md
+++ b/README.md
@@ -1,3 +1,6 @@
# gitea-github-migrator
+
+ Build Status
- Latest Release
Docker Pulls
+ cut off
+ cut off`
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff3))
if err != nil {
t.Errorf("ParsePatch failed: %s", err)
}
println(result)
}

func setupDefaultDiff() *Diff { func setupDefaultDiff() *Diff {
return &Diff{ return &Diff{
Files: []*DiffFile{ Files: []*DiffFile{

Loading…
Cancel
Save