2014-03-20 23:56:41 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2014-03-21 00:54:18 -05:00
|
|
|
"errors"
|
2014-12-30 04:03:20 -06:00
|
|
|
"strings"
|
2014-03-20 23:56:41 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-11-26 10:22:15 -06:00
|
|
|
func TestFindSimilar(t *testing.T) {
|
2014-03-20 23:56:41 -05:00
|
|
|
repo := createTestRepo(t)
|
2015-04-24 05:59:29 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
2014-03-20 23:56:41 -05:00
|
|
|
|
2014-11-26 10:22:15 -06:00
|
|
|
originalTree, newTree := createTestTrees(t, repo)
|
|
|
|
|
|
|
|
diffOpt, _ := DefaultDiffOptions()
|
2014-03-20 23:56:41 -05:00
|
|
|
|
2014-11-26 10:22:15 -06:00
|
|
|
diff, err := repo.DiffTreeToTree(originalTree, newTree, &diffOpt)
|
2014-03-20 23:56:41 -05:00
|
|
|
checkFatal(t, err)
|
2014-11-26 10:22:15 -06:00
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("no diff returned")
|
|
|
|
}
|
2014-03-20 23:56:41 -05:00
|
|
|
|
2014-11-26 10:22:15 -06:00
|
|
|
findOpts, err := DefaultDiffFindOptions()
|
|
|
|
checkFatal(t, err)
|
|
|
|
findOpts.Flags = DiffFindBreakRewrites
|
2014-03-21 00:54:18 -05:00
|
|
|
|
2014-11-26 10:22:15 -06:00
|
|
|
err = diff.FindSimilar(&findOpts)
|
2014-03-20 23:56:41 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
|
2014-11-26 10:22:15 -06:00
|
|
|
numDiffs := 0
|
|
|
|
numAdded := 0
|
|
|
|
numDeleted := 0
|
|
|
|
|
|
|
|
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
|
|
|
|
numDiffs++
|
|
|
|
|
|
|
|
switch file.Status {
|
|
|
|
case DeltaAdded:
|
|
|
|
numAdded++
|
|
|
|
case DeltaDeleted:
|
|
|
|
numDeleted++
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
|
|
|
|
return func(line DiffLine) error {
|
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}, DiffDetailLines)
|
|
|
|
|
|
|
|
if numDiffs != 2 {
|
|
|
|
t.Fatal("Incorrect number of files in diff")
|
|
|
|
}
|
|
|
|
if numAdded != 1 {
|
|
|
|
t.Fatal("Incorrect number of new files in diff")
|
|
|
|
}
|
|
|
|
if numDeleted != 1 {
|
|
|
|
t.Fatal("Incorrect number of deleted files in diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiffTreeToTree(t *testing.T) {
|
|
|
|
|
|
|
|
repo := createTestRepo(t)
|
2015-04-24 05:59:29 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
2014-11-26 10:22:15 -06:00
|
|
|
|
|
|
|
originalTree, newTree := createTestTrees(t, repo)
|
|
|
|
|
2014-03-22 00:16:26 -05:00
|
|
|
callbackInvoked := false
|
|
|
|
opts := DiffOptions{
|
|
|
|
NotifyCallback: func(diffSoFar *Diff, delta DiffDelta, matchedPathSpec string) error {
|
|
|
|
callbackInvoked = true
|
|
|
|
return nil
|
|
|
|
},
|
2014-12-30 04:03:20 -06:00
|
|
|
OldPrefix: "x1/",
|
|
|
|
NewPrefix: "y1/",
|
2014-03-22 00:16:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
diff, err := repo.DiffTreeToTree(originalTree, newTree, &opts)
|
2014-03-20 23:56:41 -05:00
|
|
|
checkFatal(t, err)
|
2014-03-22 00:16:26 -05:00
|
|
|
if !callbackInvoked {
|
|
|
|
t.Fatal("callback not invoked")
|
|
|
|
}
|
2014-03-20 23:56:41 -05:00
|
|
|
|
2014-03-21 00:54:18 -05:00
|
|
|
if diff == nil {
|
|
|
|
t.Fatal("no diff returned")
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
files := make([]string, 0)
|
2014-03-22 00:16:26 -05:00
|
|
|
hunks := make([]DiffHunk, 0)
|
|
|
|
lines := make([]DiffLine, 0)
|
2014-12-30 04:03:20 -06:00
|
|
|
patches := make([]string, 0)
|
2014-03-22 00:16:26 -05:00
|
|
|
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
|
2014-12-30 04:03:20 -06:00
|
|
|
patch, err := diff.Patch(len(patches))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer patch.Free()
|
|
|
|
patchStr, err := patch.String()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
patches = append(patches, patchStr)
|
|
|
|
|
2014-03-21 00:54:18 -05:00
|
|
|
files = append(files, file.OldFile.Path)
|
2014-03-22 00:16:26 -05:00
|
|
|
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
|
2014-03-21 19:20:48 -05:00
|
|
|
hunks = append(hunks, hunk)
|
2014-03-22 00:16:26 -05:00
|
|
|
return func(line DiffLine) error {
|
2014-03-21 19:20:48 -05:00
|
|
|
lines = append(lines, line)
|
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
}, nil
|
2014-03-22 00:16:26 -05:00
|
|
|
}, DiffDetailLines)
|
2014-03-20 23:56:41 -05:00
|
|
|
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
2014-03-21 00:54:18 -05:00
|
|
|
if len(files) != 1 {
|
2014-03-20 23:56:41 -05:00
|
|
|
t.Fatal("Incorrect number of files in diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if files[0] != "README" {
|
|
|
|
t.Fatal("File in diff was expected to be README")
|
|
|
|
}
|
2014-03-21 00:54:18 -05:00
|
|
|
|
2014-03-21 19:20:48 -05:00
|
|
|
if len(hunks) != 1 {
|
|
|
|
t.Fatal("Incorrect number of hunks in diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if hunks[0].OldStart != 1 || hunks[0].NewStart != 1 {
|
|
|
|
t.Fatal("Incorrect hunk")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lines) != 2 {
|
|
|
|
t.Fatal("Incorrect number of lines in diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if lines[0].Content != "foo\n" {
|
|
|
|
t.Fatal("Incorrect lines in diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if lines[1].Content != "file changed\n" {
|
|
|
|
t.Fatal("Incorrect lines in diff")
|
|
|
|
}
|
|
|
|
|
2014-12-30 04:03:20 -06:00
|
|
|
if want1, want2 := "x1/README", "y1/README"; !strings.Contains(patches[0], want1) || !strings.Contains(patches[0], want2) {
|
|
|
|
t.Errorf("Diff patch doesn't contain %q or %q\n\n%s", want1, want2, patches[0])
|
2014-12-28 16:07:33 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := diff.Stats()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if stats.Insertions() != 1 {
|
|
|
|
t.Fatal("Incorrect number of insertions in diff")
|
|
|
|
}
|
|
|
|
if stats.Deletions() != 1 {
|
|
|
|
t.Fatal("Incorrect number of deletions in diff")
|
|
|
|
}
|
|
|
|
if stats.FilesChanged() != 1 {
|
|
|
|
t.Fatal("Incorrect number of changed files in diff")
|
2014-12-30 04:03:20 -06:00
|
|
|
}
|
|
|
|
|
2014-03-21 00:54:18 -05:00
|
|
|
errTest := errors.New("test error")
|
|
|
|
|
2014-03-22 00:16:26 -05:00
|
|
|
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
|
2014-03-21 19:20:48 -05:00
|
|
|
return nil, errTest
|
2014-03-22 00:16:26 -05:00
|
|
|
}, DiffDetailLines)
|
2014-03-21 00:54:18 -05:00
|
|
|
|
|
|
|
if err != errTest {
|
|
|
|
t.Fatal("Expected custom error to be returned")
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:56:41 -05:00
|
|
|
}
|
2014-11-26 10:22:15 -06:00
|
|
|
|
|
|
|
func createTestTrees(t *testing.T, repo *Repository) (originalTree *Tree, newTree *Tree) {
|
|
|
|
var err error
|
|
|
|
_, originalTreeId := seedTestRepo(t, repo)
|
|
|
|
originalTree, err = repo.LookupTree(originalTreeId)
|
|
|
|
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
_, newTreeId := updateReadme(t, repo, "file changed\n")
|
|
|
|
|
|
|
|
newTree, err = repo.LookupTree(newTreeId)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
return originalTree, newTree
|
|
|
|
}
|