git2go/merge_test.go

186 lines
4.1 KiB
Go
Raw Normal View History

package git
import (
"testing"
"time"
)
2020-02-23 08:58:21 -06:00
func TestAnnotatedCommitFromRevspec(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
mergeHead, err := repo.AnnotatedCommitFromRevspec("refs/heads/master")
checkFatal(t, err)
expectedId := "473bf778b67b6d53e2ab289e0f1a2e8addef2fc2"
if mergeHead.Id().String() != expectedId {
t.Errorf("mergeHead.Id() = %v, want %v", mergeHead.Id(), expectedId)
}
}
2014-03-26 13:28:48 -05:00
func TestMergeWithSelf(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
master, err := repo.References.Lookup("refs/heads/master")
checkFatal(t, err)
2014-12-02 18:50:37 -06:00
mergeHead, err := repo.AnnotatedCommitFromRef(master)
checkFatal(t, err)
2020-02-23 08:58:21 -06:00
expectedId := "473bf778b67b6d53e2ab289e0f1a2e8addef2fc2"
if mergeHead.Id().String() != expectedId {
t.Errorf("mergeHead.Id() = %v, want %v", mergeHead.Id(), expectedId)
}
2014-12-02 18:50:37 -06:00
mergeHeads := make([]*AnnotatedCommit, 1)
mergeHeads[0] = mergeHead
2014-03-26 14:17:23 -05:00
err = repo.Merge(mergeHeads, nil, nil)
checkFatal(t, err)
}
2014-03-26 16:42:01 -05:00
func TestMergeAnalysisWithSelf(t *testing.T) {
t.Parallel()
2014-03-26 16:42:01 -05:00
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
2014-03-26 16:42:01 -05:00
seedTestRepo(t, repo)
master, err := repo.References.Lookup("refs/heads/master")
2014-03-26 16:42:01 -05:00
checkFatal(t, err)
2014-12-02 18:50:37 -06:00
mergeHead, err := repo.AnnotatedCommitFromRef(master)
2014-03-26 16:42:01 -05:00
checkFatal(t, err)
2014-12-02 18:50:37 -06:00
mergeHeads := make([]*AnnotatedCommit, 1)
2014-03-26 16:42:01 -05:00
mergeHeads[0] = mergeHead
a, _, err := repo.MergeAnalysis(mergeHeads)
2014-03-26 16:42:01 -05:00
checkFatal(t, err)
if a != MergeAnalysisUpToDate {
t.Fatalf("Expected up to date merge, not %v", a)
}
}
func TestMergeSameFile(t *testing.T) {
t.Parallel()
2014-03-26 16:42:01 -05:00
file := MergeFileInput{
Path: "test",
Mode: 33188,
Contents: []byte("hello world"),
}
result, err := MergeFile(file, file, file, nil)
checkFatal(t, err)
if !result.Automergeable {
t.Fatal("expected automergeable")
}
if result.Path != file.Path {
t.Fatal("path was incorrect")
}
if result.Mode != file.Mode {
t.Fatal("mode was incorrect")
}
compareBytes(t, file.Contents, result.Contents)
}
func TestMergeTreesWithoutAncestor(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, 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)
index, err := repo.MergeTrees(nil, originalTree, newTree, nil)
if !index.HasConflicts() {
t.Fatal("expected conflicts in the index")
}
_, err = index.Conflict("README")
checkFatal(t, err)
2014-03-26 16:42:01 -05:00
}
func appendCommit(t *testing.T, repo *Repository) (*Oid, *Oid) {
loc, err := time.LoadLocation("Europe/Berlin")
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc),
}
idx, err := repo.Index()
checkFatal(t, err)
err = idx.AddByPath("README")
checkFatal(t, err)
treeId, err := idx.WriteTree()
checkFatal(t, err)
message := "This is another commit\n"
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
ref, err := repo.References.Lookup("HEAD")
checkFatal(t, err)
parent, err := ref.Peel(ObjectCommit)
checkFatal(t, err)
parentCommit, err := parent.AsCommit()
checkFatal(t, err)
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree, parentCommit)
checkFatal(t, err)
return commitId, treeId
}
func TestMergeBase(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitAId, _ := seedTestRepo(t, repo)
commitBId, _ := appendCommit(t, repo)
mergeBase, err := repo.MergeBase(commitAId, commitBId)
checkFatal(t, err)
if mergeBase.Cmp(commitAId) != 0 {
t.Fatalf("unexpected merge base")
}
mergeBases, err := repo.MergeBases(commitAId, commitBId)
checkFatal(t, err)
if len(mergeBases) != 1 {
t.Fatalf("expected merge bases len to be 1, got %v", len(mergeBases))
}
if mergeBases[0].Cmp(commitAId) != 0 {
t.Fatalf("unexpected merge base")
}
}
2014-03-26 16:42:01 -05:00
func compareBytes(t *testing.T, expected, actual []byte) {
for i, v := range expected {
if actual[i] != v {
t.Fatalf("Bad bytes")
}
}
}