-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add only diff run flag * fix: lint issues * docs: add diff flag * docs: fix github action path * fix: engine.New too many params Codeclimate issue --------- Co-authored-by: Nikita Rusin <[email protected]> Co-authored-by: Davide Petilli <[email protected]>
- Loading branch information
1 parent
bd792f3
commit 84488fa
Showing
19 changed files
with
508 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package diff | ||
|
||
import ( | ||
"go/token" | ||
|
||
"github.com/bluekeyes/go-gitdiff/gitdiff" | ||
) | ||
|
||
type FileName string | ||
|
||
type Change struct { | ||
StartLine int | ||
EndLine int | ||
} | ||
|
||
type Diff map[FileName][]Change | ||
|
||
func newDiff(files []*gitdiff.File) Diff { | ||
result := map[FileName][]Change{} | ||
|
||
for _, file := range files { | ||
name, changes := newChanges(file) | ||
|
||
result[name] = changes | ||
} | ||
|
||
return result | ||
} | ||
|
||
func newChanges(file *gitdiff.File) (FileName, []Change) { | ||
var changes []Change | ||
|
||
for _, fragment := range file.TextFragments { | ||
if fragment.LinesAdded == 0 { | ||
continue | ||
} | ||
|
||
startLine := int(fragment.NewPosition + fragment.LeadingContext) | ||
|
||
changes = append(changes, Change{ | ||
StartLine: startLine, | ||
EndLine: startLine + int(fragment.LinesAdded-1), | ||
}) | ||
} | ||
|
||
return FileName(file.NewName), changes | ||
} | ||
|
||
func (d Diff) IsChanged(pos token.Position) bool { | ||
if len(d) == 0 { | ||
return true | ||
} | ||
|
||
fileDiff := d[FileName(pos.Filename)] | ||
|
||
for _, change := range fileDiff { | ||
if pos.Line >= change.StartLine && pos.Line <= change.EndLine { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package diff | ||
|
||
import ( | ||
"go/token" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/bluekeyes/go-gitdiff/gitdiff" | ||
) | ||
|
||
func TestDiff_IsChanged(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
d Diff | ||
pos token.Position | ||
want bool | ||
}{ | ||
{ | ||
name: "must be changed on nil Diff", | ||
d: nil, | ||
pos: token.Position{}, | ||
want: true, | ||
}, | ||
{ | ||
name: "must be changed on empty Diff", | ||
d: map[FileName][]Change{}, | ||
pos: token.Position{}, | ||
want: true, | ||
}, | ||
{ | ||
name: "must be changed if in range", | ||
d: map[FileName][]Change{ | ||
"test": {{StartLine: 21, EndLine: 21}}, | ||
}, | ||
pos: token.Position{Filename: "test", Line: 21}, | ||
want: true, | ||
}, | ||
{ | ||
name: "must be unchanged if outside range", | ||
d: map[FileName][]Change{ | ||
"test": {{StartLine: 21, EndLine: 21}}, | ||
}, | ||
pos: token.Position{Filename: "test", Line: 22}, | ||
want: false, | ||
}, | ||
{ | ||
name: "must be unchanged if no such file", | ||
d: map[FileName][]Change{ | ||
"test": {{StartLine: 21, EndLine: 21}}, | ||
}, | ||
pos: token.Position{Filename: "test1", Line: 21}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := tt.d.IsChanged(tt.pos) | ||
if got != tt.want { | ||
t.Errorf("IsChanged() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_newDiff(t *testing.T) { | ||
fragments := []*gitdiff.TextFragment{fragment(21, 1)} | ||
|
||
files := []*gitdiff.File{ | ||
{ | ||
NewName: "test1", | ||
TextFragments: fragments, | ||
}, | ||
{ | ||
NewName: "test2", | ||
TextFragments: fragments, | ||
}, | ||
} | ||
|
||
expected := Diff{ | ||
"test1": {{StartLine: 25, EndLine: 25}}, | ||
"test2": {{StartLine: 25, EndLine: 25}}, | ||
} | ||
|
||
result := newDiff(files) | ||
if !reflect.DeepEqual(result, expected) { | ||
t.Log("want", expected) | ||
t.Log("got", result) | ||
t.Fatalf("unexpected newDiff result") | ||
} | ||
} | ||
|
||
func Test_newChanges(t *testing.T) { | ||
fragments := []*gitdiff.TextFragment{ | ||
fragment(0, 1), | ||
fragment(10, 0), | ||
fragment(21, 2), | ||
fragment(44, 4), | ||
fragment(231, 201), | ||
} | ||
file := &gitdiff.File{ | ||
NewName: "test", | ||
TextFragments: fragments, | ||
} | ||
|
||
expect := []Change{ | ||
{StartLine: 4, EndLine: 4}, | ||
{StartLine: 25, EndLine: 26}, | ||
{StartLine: 48, EndLine: 51}, | ||
{StartLine: 235, EndLine: 435}, | ||
} | ||
|
||
name, changes := newChanges(file) | ||
|
||
if name != "test" { | ||
t.Fatalf("name %s unexpected", name) | ||
} | ||
if !reflect.DeepEqual(changes, expect) { | ||
t.Log("want", expect) | ||
t.Log("got", changes) | ||
t.Fatalf("unexpected newChanges result") | ||
} | ||
} | ||
|
||
func fragment(startLine int, adds int, del ...int) *gitdiff.TextFragment { | ||
const contexts = 4 | ||
|
||
dels := adds | ||
if len(del) > 0 { | ||
dels = del[0] | ||
} | ||
|
||
var lines []gitdiff.Line | ||
|
||
lines = append(lines, opLines(gitdiff.OpContext, contexts)...) | ||
lines = append(lines, opLines(gitdiff.OpDelete, dels)...) | ||
lines = append(lines, opLines(gitdiff.OpAdd, adds)...) | ||
lines = append(lines, opLines(gitdiff.OpContext, contexts)...) | ||
|
||
line := int64(startLine) | ||
added := int64(adds) | ||
deleted := int64(dels) | ||
|
||
return &gitdiff.TextFragment{ | ||
OldLines: line - 1, | ||
NewPosition: line, | ||
LinesAdded: added, | ||
LinesDeleted: deleted, | ||
LeadingContext: contexts, | ||
TrailingContext: contexts, | ||
Lines: lines, | ||
} | ||
} | ||
|
||
func opLines(op gitdiff.LineOp, count int) []gitdiff.Line { | ||
result := make([]gitdiff.Line, count) | ||
|
||
for i := 0; i < count; i++ { | ||
result[i] = gitdiff.Line{Op: op, Line: "test"} | ||
} | ||
|
||
return result | ||
} |
Oops, something went wrong.