2014-03-20 23:56:41 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2014-03-21 00:54:18 -05:00
|
|
|
"errors"
|
2020-08-18 08:14:02 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
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) {
|
2016-08-27 12:21:05 -05:00
|
|
|
t.Parallel()
|
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) {
|
2016-08-27 12:21:05 -05:00
|
|
|
t.Parallel()
|
2014-11-26 10:22:15 -06:00
|
|
|
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
|
|
|
|
}
|
2015-06-12 12:10:00 -05:00
|
|
|
|
|
|
|
func TestDiffBlobs(t *testing.T) {
|
2016-08-27 12:21:05 -05:00
|
|
|
t.Parallel()
|
2015-06-12 12:10:00 -05:00
|
|
|
repo := createTestRepo(t)
|
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
|
|
|
|
odb, err := repo.Odb()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
id1, err := odb.Write([]byte("hello\nhello\n"), ObjectBlob)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
id2, err := odb.Write([]byte("hallo\nhallo\n"), ObjectBlob)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
blob1, err := repo.LookupBlob(id1)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
blob2, err := repo.LookupBlob(id2)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
var files, hunks, lines int
|
|
|
|
err = DiffBlobs(blob1, "hi", blob2, "hi", nil,
|
|
|
|
func(delta DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
|
|
|
|
files++
|
|
|
|
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
|
|
|
|
hunks++
|
|
|
|
return func(line DiffLine) error {
|
|
|
|
lines++
|
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
DiffDetailLines)
|
|
|
|
|
|
|
|
if files != 1 {
|
|
|
|
t.Fatal("Bad number of files iterated")
|
|
|
|
}
|
|
|
|
|
|
|
|
if hunks != 1 {
|
|
|
|
t.Fatal("Bad number of hunks iterated")
|
|
|
|
}
|
|
|
|
|
|
|
|
// two removals, two additions
|
|
|
|
if lines != 4 {
|
|
|
|
t.Fatalf("Bad number of lines iterated")
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 08:14:02 -05:00
|
|
|
|
|
|
|
func TestApplyDiffAddfile(t *testing.T) {
|
|
|
|
repo := createTestRepo(t)
|
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
|
|
|
|
seedTestRepo(t, repo)
|
|
|
|
|
|
|
|
addFirstFileCommit, addFileTree := addAndGetTree(t, repo, "file1", `hello`)
|
|
|
|
addSecondFileCommit, addSecondFileTree := addAndGetTree(t, repo, "file2", `hello2`)
|
|
|
|
|
|
|
|
diff, err := repo.DiffTreeToTree(addFileTree, addSecondFileTree, nil)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
t.Run("check does not apply to current tree because file exists", func(t *testing.T) {
|
|
|
|
err = repo.ResetToCommit(addSecondFileCommit, ResetHard, &CheckoutOpts{})
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
err = repo.ApplyDiff(diff, ApplyLocationBoth, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expecting applying patch to current repo to fail")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check apply to correct commit", func(t *testing.T) {
|
|
|
|
err = repo.ResetToCommit(addFirstFileCommit, ResetHard, &CheckoutOpts{})
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
err = repo.ApplyDiff(diff, ApplyLocationBoth, nil)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
t.Run("Check that diff only changed one file", func(t *testing.T) {
|
|
|
|
checkSecondFileStaged(t, repo)
|
|
|
|
|
|
|
|
index, err := repo.Index()
|
|
|
|
checkFatal(t, err)
|
|
|
|
defer index.Free()
|
|
|
|
|
|
|
|
newTreeOID, err := index.WriteTreeTo(repo)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
newTree, err := repo.LookupTree(newTreeOID)
|
|
|
|
checkFatal(t, err)
|
|
|
|
defer newTree.Free()
|
|
|
|
|
|
|
|
_, err = repo.CreateCommit("HEAD", signature(), signature(), fmt.Sprintf("patch apply"), newTree, addFirstFileCommit)
|
|
|
|
checkFatal(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test applying patch produced the same diff", func(t *testing.T) {
|
|
|
|
head, err := repo.Head()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
commit, err := repo.LookupCommit(head.Target())
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
tree, err := commit.Tree()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
newDiff, err := repo.DiffTreeToTree(addFileTree, tree, nil)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
raw1b, err := diff.ToBuf(DiffFormatPatch)
|
|
|
|
checkFatal(t, err)
|
|
|
|
raw2b, err := newDiff.ToBuf(DiffFormatPatch)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
raw1 := string(raw1b)
|
|
|
|
raw2 := string(raw2b)
|
|
|
|
|
|
|
|
if raw1 != raw2 {
|
|
|
|
t.Error("diffs should be the same")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check convert to raw buffer and apply", func(t *testing.T) {
|
|
|
|
err = repo.ResetToCommit(addFirstFileCommit, ResetHard, &CheckoutOpts{})
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
raw, err := diff.ToBuf(DiffFormatPatch)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if len(raw) == 0 {
|
|
|
|
t.Error("empty diff created")
|
|
|
|
}
|
|
|
|
|
|
|
|
diff2, err := DiffFromBuffer(raw, repo)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
err = repo.ApplyDiff(diff2, ApplyLocationBoth, nil)
|
|
|
|
checkFatal(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check apply callbacks work", func(t *testing.T) {
|
|
|
|
// reset the state and get new default options for test
|
|
|
|
resetAndGetOpts := func(t *testing.T) *ApplyOptions {
|
|
|
|
err = repo.ResetToCommit(addFirstFileCommit, ResetHard, &CheckoutOpts{})
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
opts, err := DefaultApplyOptions()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Check hunk callback working applies patch", func(t *testing.T) {
|
|
|
|
opts := resetAndGetOpts(t)
|
|
|
|
|
|
|
|
called := false
|
|
|
|
opts.ApplyHunkCallback = func(hunk *DiffHunk) (apply bool, err error) {
|
|
|
|
called = true
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if called == false {
|
|
|
|
t.Error("apply hunk callback was not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkSecondFileStaged(t, repo)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check delta callback working applies patch", func(t *testing.T) {
|
|
|
|
opts := resetAndGetOpts(t)
|
|
|
|
|
|
|
|
called := false
|
|
|
|
opts.ApplyDeltaCallback = func(hunk *DiffDelta) (apply bool, err error) {
|
|
|
|
if hunk.NewFile.Path != "file2" {
|
|
|
|
t.Error("Unexpected delta in diff application")
|
|
|
|
}
|
|
|
|
called = true
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if called == false {
|
|
|
|
t.Error("apply hunk callback was not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkSecondFileStaged(t, repo)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check delta callback returning false does not apply patch", func(t *testing.T) {
|
|
|
|
opts := resetAndGetOpts(t)
|
|
|
|
|
|
|
|
called := false
|
|
|
|
opts.ApplyDeltaCallback = func(hunk *DiffDelta) (apply bool, err error) {
|
|
|
|
if hunk.NewFile.Path != "file2" {
|
|
|
|
t.Error("Unexpected hunk in diff application")
|
|
|
|
}
|
|
|
|
called = true
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if called == false {
|
|
|
|
t.Error("apply hunk callback was not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkNoFilesStaged(t, repo)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check hunk callback returning causes application to fail", func(t *testing.T) {
|
|
|
|
opts := resetAndGetOpts(t)
|
|
|
|
|
|
|
|
called := false
|
|
|
|
opts.ApplyHunkCallback = func(hunk *DiffHunk) (apply bool, err error) {
|
|
|
|
called = true
|
|
|
|
return false, errors.New("something happened")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected an error after trying to apply")
|
|
|
|
}
|
|
|
|
|
|
|
|
if called == false {
|
|
|
|
t.Error("apply hunk callback was not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkNoFilesStaged(t, repo)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check delta callback returning causes application to fail", func(t *testing.T) {
|
|
|
|
opts := resetAndGetOpts(t)
|
|
|
|
|
|
|
|
called := false
|
|
|
|
opts.ApplyDeltaCallback = func(hunk *DiffDelta) (apply bool, err error) {
|
|
|
|
if hunk.NewFile.Path != "file2" {
|
|
|
|
t.Error("Unexpected delta in diff application")
|
|
|
|
}
|
|
|
|
called = true
|
|
|
|
return false, errors.New("something happened")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected an error after trying to apply")
|
|
|
|
}
|
|
|
|
|
|
|
|
if called == false {
|
|
|
|
t.Error("apply hunk callback was not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkNoFilesStaged(t, repo)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkSecondFileStaged checks that there is a single file called "file2" uncommitted in the repo
|
|
|
|
func checkSecondFileStaged(t *testing.T, repo *Repository) {
|
|
|
|
opts := StatusOptions{
|
|
|
|
Show: StatusShowIndexAndWorkdir,
|
|
|
|
Flags: StatusOptIncludeUntracked,
|
|
|
|
}
|
|
|
|
|
|
|
|
statuses, err := repo.StatusList(&opts)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
count, err := statuses.EntryCount()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if count != 1 {
|
|
|
|
t.Error("diff should affect exactly one file")
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
t.Fatal("no statuses, cannot continue test")
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := statuses.ByIndex(0)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if entry.Status != StatusIndexNew {
|
|
|
|
t.Error("status should be 'new' as file has been added between commits")
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.HeadToIndex.NewFile.Path != "file2" {
|
|
|
|
t.Error("new file should be 'file2")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkNoFilesStaged checks that there is a single file called "file2" uncommitted in the repo
|
|
|
|
func checkNoFilesStaged(t *testing.T, repo *Repository) {
|
|
|
|
opts := StatusOptions{
|
|
|
|
Show: StatusShowIndexAndWorkdir,
|
|
|
|
Flags: StatusOptIncludeUntracked,
|
|
|
|
}
|
|
|
|
|
|
|
|
statuses, err := repo.StatusList(&opts)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
count, err := statuses.EntryCount()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if count != 0 {
|
|
|
|
t.Error("files changed unexpectedly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addAndGetTree creates a file and commits it, returning the commit and tree
|
|
|
|
func addAndGetTree(t *testing.T, repo *Repository, filename string, content string) (*Commit, *Tree) {
|
|
|
|
headCommit, err := headCommit(repo)
|
|
|
|
checkFatal(t, err)
|
|
|
|
defer headCommit.Free()
|
|
|
|
|
|
|
|
p := repo.Path()
|
|
|
|
p = strings.TrimSuffix(p, ".git")
|
|
|
|
p = strings.TrimSuffix(p, ".git/")
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(path.Join(p, filename), []byte((content)), 0777)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
index, err := repo.Index()
|
|
|
|
checkFatal(t, err)
|
|
|
|
defer index.Free()
|
|
|
|
|
|
|
|
err = index.AddByPath(filename)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
newTreeOID, err := index.WriteTreeTo(repo)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
newTree, err := repo.LookupTree(newTreeOID)
|
|
|
|
checkFatal(t, err)
|
|
|
|
defer newTree.Free()
|
|
|
|
|
|
|
|
commitId, err := repo.CreateCommit("HEAD", signature(), signature(), fmt.Sprintf("add %s", filename), newTree, headCommit)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
commit, err := repo.LookupCommit(commitId)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
tree, err := commit.Tree()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
return commit, tree
|
|
|
|
}
|