Skip to content

Commit b76d899

Browse files
mrsdizziezeripath
authored andcommitted
Fix ParsePatch function to work with quoted diff --git strings (#6323) (#6332)
Backport of #6323
1 parent 9f33aa6 commit b76d899

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

models/git_diff.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,12 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
550550
beg := len(cmdDiffHead)
551551
a := line[beg+2 : middle]
552552
b := line[middle+3:]
553+
553554
if hasQuote {
555+
// Keep the entire string in double quotes for now
556+
a = line[beg:middle]
557+
b = line[middle+1:]
558+
554559
var err error
555560
a, err = strconv.Unquote(a)
556561
if err != nil {
@@ -560,6 +565,10 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
560565
if err != nil {
561566
return nil, fmt.Errorf("Unquote: %v", err)
562567
}
568+
// Now remove the /a /b
569+
a = a[2:]
570+
b = b[2:]
571+
563572
}
564573

565574
curFile = &DiffFile{
@@ -637,6 +646,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
637646
}
638647
}
639648
}
649+
640650
return diff, nil
641651
}
642652

models/git_diff_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"strings"
66
"testing"
77

8+
"code.gitea.io/gitea/modules/setting"
9+
810
dmp "github.com/sergi/go-diff/diffmatchpatch"
911
"github.com/stretchr/testify/assert"
1012
)
@@ -99,6 +101,59 @@ func ExampleCutDiffAroundLine() {
99101
println(result)
100102
}
101103

104+
func TestParsePatch(t *testing.T) {
105+
var diff = `diff --git "a/README.md" "b/README.md"
106+
--- a/README.md
107+
+++ b/README.md
108+
@@ -1,3 +1,6 @@
109+
# gitea-github-migrator
110+
+
111+
+ Build Status
112+
- Latest Release
113+
Docker Pulls
114+
+ cut off
115+
+ cut off`
116+
result, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff))
117+
if err != nil {
118+
t.Errorf("ParsePatch failed: %s", err)
119+
}
120+
println(result)
121+
122+
var diff2 = `diff --git "a/A \\ B" "b/A \\ B"
123+
--- "a/A \\ B"
124+
+++ "b/A \\ B"
125+
@@ -1,3 +1,6 @@
126+
# gitea-github-migrator
127+
+
128+
+ Build Status
129+
- Latest Release
130+
Docker Pulls
131+
+ cut off
132+
+ cut off`
133+
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2))
134+
if err != nil {
135+
t.Errorf("ParsePatch failed: %s", err)
136+
}
137+
println(result)
138+
139+
var diff3 = `diff --git a/README.md b/README.md
140+
--- a/README.md
141+
+++ b/README.md
142+
@@ -1,3 +1,6 @@
143+
# gitea-github-migrator
144+
+
145+
+ Build Status
146+
- Latest Release
147+
Docker Pulls
148+
+ cut off
149+
+ cut off`
150+
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff3))
151+
if err != nil {
152+
t.Errorf("ParsePatch failed: %s", err)
153+
}
154+
println(result)
155+
}
156+
102157
func setupDefaultDiff() *Diff {
103158
return &Diff{
104159
Files: []*DiffFile{

0 commit comments

Comments
 (0)