From 7b0fb3ab68de8e662be360202c8e44f48643b5f2 Mon Sep 17 00:00:00 2001 From: "j. mccann" Date: Wed, 13 Mar 2019 22:16:13 -0400 Subject: [PATCH 1/3] 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 --- models/git_diff.go | 7 +++++++ models/git_diff_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/models/git_diff.go b/models/git_diff.go index 2a8019995d73..0a56029ccf3e 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -550,7 +550,13 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D beg := len(cmdDiffHead) a := line[beg+2 : middle] b := line[middle+3:] + if hasQuote { + // When /a and /b are surrounded by double quotes, we want to first + // make sure we keep everything the quotes and then strip out the leading /a /b + a = strings.Replace(line[beg:middle], "\"a/", "\"", -1) + b = strings.Replace(line[middle+1:], "\"b/", "\"", -1) + var err error a, err = strconv.Unquote(a) if err != nil { @@ -637,6 +643,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D } } } + return diff, nil } diff --git a/models/git_diff_test.go b/models/git_diff_test.go index ac6477122681..481bfc643a74 100644 --- a/models/git_diff_test.go +++ b/models/git_diff_test.go @@ -5,6 +5,8 @@ import ( "strings" "testing" + "code.gitea.io/gitea/modules/setting" + dmp "github.com/sergi/go-diff/diffmatchpatch" "github.com/stretchr/testify/assert" ) @@ -99,6 +101,42 @@ func ExampleCutDiffAroundLine() { 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) +} + func setupDefaultDiff() *Diff { return &Diff{ Files: []*DiffFile{ From 5f432ff1c788da9660abff2d7f876bcc0e4cf311 Mon Sep 17 00:00:00 2001 From: "j. mccann" Date: Wed, 13 Mar 2019 22:26:16 -0400 Subject: [PATCH 2/3] Add test for regular case while here --- models/git_diff_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/models/git_diff_test.go b/models/git_diff_test.go index 481bfc643a74..2111e9044f99 100644 --- a/models/git_diff_test.go +++ b/models/git_diff_test.go @@ -135,6 +135,23 @@ func TestParsePatch(t *testing.T) { 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 { From 5b7961d051309d3aba04c37c33c0e16f3b250683 Mon Sep 17 00:00:00 2001 From: "j. mccann" Date: Thu, 14 Mar 2019 09:36:05 -0400 Subject: [PATCH 3/3] Simplify string modification --- models/git_diff.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/models/git_diff.go b/models/git_diff.go index 0a56029ccf3e..2e76e56bea17 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -552,10 +552,9 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D b := line[middle+3:] if hasQuote { - // When /a and /b are surrounded by double quotes, we want to first - // make sure we keep everything the quotes and then strip out the leading /a /b - a = strings.Replace(line[beg:middle], "\"a/", "\"", -1) - b = strings.Replace(line[middle+1:], "\"b/", "\"", -1) + // 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) @@ -566,6 +565,10 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D if err != nil { return nil, fmt.Errorf("Unquote: %v", err) } + // Now remove the /a /b + a = a[2:] + b = b[2:] + } curFile = &DiffFile{