forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_test.go
156 lines (120 loc) · 3.42 KB
/
merge_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package git
import (
"testing"
"time"
)
func TestMergeWithSelf(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
master, err := repo.References.Lookup("refs/heads/master")
checkFatal(t, err)
mergeHead, err := repo.AnnotatedCommitFromRef(master)
checkFatal(t, err)
mergeHeads := make([]*AnnotatedCommit, 1)
mergeHeads[0] = mergeHead
err = repo.Merge(mergeHeads, nil, nil)
checkFatal(t, err)
}
func TestMergeAnalysisWithSelf(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
master, err := repo.References.Lookup("refs/heads/master")
checkFatal(t, err)
mergeHead, err := repo.AnnotatedCommitFromRef(master)
checkFatal(t, err)
mergeHeads := make([]*AnnotatedCommit, 1)
mergeHeads[0] = mergeHead
a, _, err := repo.MergeAnalysis(mergeHeads)
checkFatal(t, err)
if a != MergeAnalysisUpToDate {
t.Fatalf("Expected up to date merge, not %v", a)
}
}
func TestMergeSameFile(t *testing.T) {
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) {
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.GetConflict("README")
checkFatal(t, err)
}
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: "[email protected]",
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)
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree, parent.(*Commit))
checkFatal(t, err)
return commitId, treeId
}
func TestMergeBase(t *testing.T) {
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")
}
}
func compareBytes(t *testing.T, expected, actual []byte) {
for i, v := range expected {
if actual[i] != v {
t.Fatalf("Bad bytes")
}
}
}