Skip to content

Commit

Permalink
diff: support unified diff extension used by gofmt -d
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Jul 26, 2020
1 parent 0b15961 commit 7b3bcaa
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 31 deletions.
30 changes: 15 additions & 15 deletions diff/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
)

const (
tokenDiffGit = "diff --git" // diff --git a/sample.old.txt b/sample.new.txt
tokenOldFile = "---" // --- sample.old.txt 2016-10-13 05:09:35.820791185 +0900
tokenNewFile = "+++" // +++ sample.new.txt 2016-10-13 05:15:26.839245048 +0900
tokenStartHunk = "@@" // @@ -1,3 +1,4 @@
tokenUnchangedLine = " " // unchanged, contextual line
tokenAddedLine = "+" // +added line
tokenDeletedLine = "-" // -deleted line
tokenNoNewlineAtEOF = `\` // \ No newline at end of file
tokenDiff = "diff" // diff --git a/sample.old.txt b/sample.new.txt
tokenOldFile = "---" // --- sample.old.txt 2016-10-13 05:09:35.820791185 +0900
tokenNewFile = "+++" // +++ sample.new.txt 2016-10-13 05:15:26.839245048 +0900
tokenStartHunk = "@@" // @@ -1,3 +1,4 @@
tokenUnchangedLine = " " // unchanged, contextual line
tokenAddedLine = "+" // +added line
tokenDeletedLine = "-" // -deleted line
tokenNoNewlineAtEOF = `\` // \ No newline at end of file
)

var (
Expand Down Expand Up @@ -103,11 +103,11 @@ func (p *fileParser) parseHunks() ([]*Hunk, error) {
return nil, ErrNoHunks
}
if !bytes.HasPrefix(b, []byte(tokenStartHunk)) {
b, err := p.r.Peek(len(tokenDiffGit))
b, err := p.r.Peek(len(tokenDiff))
if err != nil {
return nil, ErrNoHunks
}
if bytes.HasPrefix(b, []byte(tokenDiffGit)) {
if bytes.HasPrefix(b, []byte(tokenDiff)) {
// git diff may contain a file diff with empty hunks.
// e.g. delete an empty file.
return []*Hunk{}, nil
Expand Down Expand Up @@ -211,17 +211,17 @@ LOOP:

func parseExtendedHeader(r *bufio.Reader) []string {
var es []string
b, err := r.Peek(len(tokenDiffGit))
b, err := r.Peek(len(tokenDiff))
if err != nil {
return nil
}
// if starts with 'diff --git', parse extended header
if bytes.HasPrefix(b, []byte(tokenDiffGit)) {
// if starts with 'diff', parse extended header
if bytes.HasPrefix(b, []byte(tokenDiff)) {
diffgitline, _ := readline(r) // ignore err because we know it can read something
es = append(es, diffgitline)
for {
b, err := r.Peek(len(tokenDiffGit))
if err != nil || bytes.HasPrefix(b, []byte(tokenOldFile)) || bytes.HasPrefix(b, []byte(tokenDiffGit)) {
b, err := r.Peek(len(tokenDiff))
if err != nil || bytes.HasPrefix(b, []byte(tokenOldFile)) || bytes.HasPrefix(b, []byte(tokenDiff)) {
break
}
line, _ := readline(r)
Expand Down
1 change: 1 addition & 0 deletions diff/testdata/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ git diff --no-index golint.{old,new}.go > golint.diff
git diff --no-index empty.txt /dev/null > newline_and_empty_deleted.diff
git diff --no-index golint.{old,new}.go >> newline_and_empty_deleted.diff
git -c core.quotepath=true diff --no-index 日本語.{old,new}.txt > 日本語.diff
gofmt -d gofmt.go > gofmt.diff
27 changes: 27 additions & 0 deletions diff/testdata/gofmt.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
diff -u gofmt.go.orig gofmt.go
--- gofmt.go.orig 2020-07-26 08:01:09.260800318 +0000
+++ gofmt.go 2020-07-26 08:01:09.260800318 +0000
@@ -1,6 +1,6 @@
package testdata

-func fmt () {
+func fmt() {
// test
// test line
// test line
@@ -10,11 +10,11 @@
// test line
// test line

-println(
- "hello, gofmt test" )
-//comment
+ println(
+ "hello, gofmt test")
+ //comment
}

+type s struct{ A int }

-type s struct { A int }
func (s s) String() { return "s" }
185 changes: 185 additions & 0 deletions diff/testdata/gofmt.diff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
[
{
"PathOld": "gofmt.go.orig",
"PathNew": "gofmt.go",
"TimeOld": "2020-07-26 08:01:09.260800318 +0000",
"TimeNew": "2020-07-26 08:01:09.260800318 +0000",
"Hunks": [
{
"StartLineOld": 1,
"LineLengthOld": 6,
"StartLineNew": 1,
"LineLengthNew": 6,
"Section": "",
"Lines": [
{
"Type": 0,
"Content": "package testdata",
"LnumDiff": 1,
"LnumOld": 1,
"LnumNew": 1
},
{
"Type": 0,
"Content": "",
"LnumDiff": 2,
"LnumOld": 2,
"LnumNew": 2
},
{
"Type": 2,
"Content": "func fmt () {",
"LnumDiff": 3,
"LnumOld": 3,
"LnumNew": 0
},
{
"Type": 1,
"Content": "func fmt() {",
"LnumDiff": 4,
"LnumOld": 0,
"LnumNew": 3
},
{
"Type": 0,
"Content": "\t// test",
"LnumDiff": 5,
"LnumOld": 4,
"LnumNew": 4
},
{
"Type": 0,
"Content": "\t// test line",
"LnumDiff": 6,
"LnumOld": 5,
"LnumNew": 5
},
{
"Type": 0,
"Content": "\t// test line",
"LnumDiff": 7,
"LnumOld": 6,
"LnumNew": 6
}
]
},
{
"StartLineOld": 10,
"LineLengthOld": 11,
"StartLineNew": 10,
"LineLengthNew": 11,
"Section": "",
"Lines": [
{
"Type": 0,
"Content": "\t// test line",
"LnumDiff": 9,
"LnumOld": 10,
"LnumNew": 10
},
{
"Type": 0,
"Content": "\t// test line",
"LnumDiff": 10,
"LnumOld": 11,
"LnumNew": 11
},
{
"Type": 0,
"Content": "",
"LnumDiff": 11,
"LnumOld": 12,
"LnumNew": 12
},
{
"Type": 2,
"Content": "println(",
"LnumDiff": 12,
"LnumOld": 13,
"LnumNew": 0
},
{
"Type": 2,
"Content": "\t\t\"hello, gofmt test\" )",
"LnumDiff": 13,
"LnumOld": 14,
"LnumNew": 0
},
{
"Type": 2,
"Content": "//comment",
"LnumDiff": 14,
"LnumOld": 15,
"LnumNew": 0
},
{
"Type": 1,
"Content": "\tprintln(",
"LnumDiff": 15,
"LnumOld": 0,
"LnumNew": 13
},
{
"Type": 1,
"Content": "\t\t\"hello, gofmt test\")",
"LnumDiff": 16,
"LnumOld": 0,
"LnumNew": 14
},
{
"Type": 1,
"Content": "\t//comment",
"LnumDiff": 17,
"LnumOld": 0,
"LnumNew": 15
},
{
"Type": 0,
"Content": "}",
"LnumDiff": 18,
"LnumOld": 16,
"LnumNew": 16
},
{
"Type": 0,
"Content": "",
"LnumDiff": 19,
"LnumOld": 17,
"LnumNew": 17
},
{
"Type": 1,
"Content": "type s struct{ A int }",
"LnumDiff": 20,
"LnumOld": 0,
"LnumNew": 18
},
{
"Type": 0,
"Content": "",
"LnumDiff": 21,
"LnumOld": 18,
"LnumNew": 19
},
{
"Type": 2,
"Content": "type s struct { A int }",
"LnumDiff": 22,
"LnumOld": 19,
"LnumNew": 0
},
{
"Type": 0,
"Content": "func (s s) String() { return \"s\" }",
"LnumDiff": 23,
"LnumOld": 20,
"LnumNew": 20
}
]
}
],
"Extended": [
"diff -u gofmt.go.orig gofmt.go"
]
}
]
20 changes: 20 additions & 0 deletions diff/testdata/gofmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package testdata

func fmt () {
// test
// test line
// test line
// test line
// test line
// test line
// test line
// test line

println(
"hello, gofmt test" )
//comment
}


type s struct { A int }
func (s s) String() { return "s" }
4 changes: 2 additions & 2 deletions diff/testdata/nonewline.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- nonewline.old.txt 2016-10-13 15:34:14.931778318 +0900
+++ nonewline.new.txt 2016-10-13 15:34:14.868444672 +0900
--- nonewline.old.txt 2019-04-19 13:19:50.331526534 +0000
+++ nonewline.new.txt 2019-04-19 13:19:50.331526534 +0000
@@ -1,4 +1,4 @@
" vim: nofixeol noendofline
No newline at end of both the old and new file
Expand Down
4 changes: 2 additions & 2 deletions diff/testdata/nonewline.diff.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
"PathOld": "nonewline.old.txt",
"PathNew": "nonewline.new.txt",
"TimeOld": "2016-10-13 15:34:14.931778318 +0900",
"TimeNew": "2016-10-13 15:34:14.868444672 +0900",
"TimeOld": "2019-04-19 13:19:50.331526534 +0000",
"TimeNew": "2019-04-19 13:19:50.331526534 +0000",
"Hunks": [
{
"StartLineOld": 1,
Expand Down
4 changes: 2 additions & 2 deletions diff/testdata/nonewline2.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- nonewline2.old.txt 2016-10-13 15:34:15.141779355 +0900
+++ nonewline2.new.txt 2016-10-13 15:34:15.075112360 +0900
--- nonewline2.old.txt 2019-04-19 13:19:50.331526534 +0000
+++ nonewline2.new.txt 2019-04-19 13:19:50.331526534 +0000
@@ -1,4 +1,4 @@
No newline at end of both the old and new file (unchanged)
-a
Expand Down
4 changes: 2 additions & 2 deletions diff/testdata/nonewline2.diff.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
"PathOld": "nonewline2.old.txt",
"PathNew": "nonewline2.new.txt",
"TimeOld": "2016-10-13 15:34:15.141779355 +0900",
"TimeNew": "2016-10-13 15:34:15.075112360 +0900",
"TimeOld": "2019-04-19 13:19:50.331526534 +0000",
"TimeNew": "2019-04-19 13:19:50.331526534 +0000",
"Hunks": [
{
"StartLineOld": 1,
Expand Down
4 changes: 2 additions & 2 deletions diff/testdata/nonewline3.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- nonewline3.old.txt 2016-10-13 15:34:15.361780443 +0900
+++ nonewline3.new.txt 2016-10-13 15:34:15.288446747 +0900
--- nonewline3.old.txt 2019-04-19 13:19:50.331526534 +0000
+++ nonewline3.new.txt 2019-04-19 13:19:50.331526534 +0000
@@ -1,6 +1,7 @@
No newline at end of the old file
Newline at end of the new file
Expand Down
4 changes: 2 additions & 2 deletions diff/testdata/nonewline3.diff.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
"PathOld": "nonewline3.old.txt",
"PathNew": "nonewline3.new.txt",
"TimeOld": "2016-10-13 15:34:15.361780443 +0900",
"TimeNew": "2016-10-13 15:34:15.288446747 +0900",
"TimeOld": "2019-04-19 13:19:50.331526534 +0000",
"TimeNew": "2019-04-19 13:19:50.331526534 +0000",
"Hunks": [
{
"StartLineOld": 1,
Expand Down
4 changes: 2 additions & 2 deletions diff/testdata/sample.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- sample.old.txt 2016-10-13 05:09:35.820791185 +0900
+++ sample.new.txt 2016-10-13 05:15:26.839245048 +0900
--- sample.old.txt 2019-04-19 13:19:50.331526534 +0000
+++ sample.new.txt 2019-04-19 13:19:50.331526534 +0000
@@ -1,3 +1,4 @@
unchanged, contextual line
-deleted line
Expand Down
4 changes: 2 additions & 2 deletions diff/testdata/sample.diff.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
"PathOld": "sample.old.txt",
"PathNew": "sample.new.txt",
"TimeOld": "2016-10-13 05:09:35.820791185 +0900",
"TimeNew": "2016-10-13 05:15:26.839245048 +0900",
"TimeOld": "2019-04-19 13:19:50.331526534 +0000",
"TimeNew": "2019-04-19 13:19:50.331526534 +0000",
"Hunks": [
{
"StartLineOld": 1,
Expand Down

0 comments on commit 7b3bcaa

Please sign in to comment.