Skip to content

Commit dcc97ed

Browse files
committed
merge tests!
1 parent 8d215c6 commit dcc97ed

File tree

1 file changed

+287
-0
lines changed

1 file changed

+287
-0
lines changed

test/tests/merge.js

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
var assert = require("assert");
2+
var path = require("path");
3+
var promisify = require("promisify-node");
4+
var fse = promisify(require("fs-extra"));
5+
fse.ensureDir = promisify(fse.ensureDir);
6+
7+
describe("Merge", function() {
8+
var nodegit = require("../../");
9+
10+
var repoDir = path.resolve("test/repos/merge");
11+
var ourBranchName = "ours";
12+
var theirBranchName = "theirs";
13+
14+
beforeEach(function() {
15+
var test = this;
16+
return fse.remove(path.resolve(__dirname, repoDir))
17+
.then(function() {
18+
return fse.ensureDir(path.resolve(__dirname, repoDir));
19+
})
20+
.then(function() {
21+
return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0);
22+
})
23+
.then(function(repo) {
24+
test.repository = repo;
25+
});
26+
});
27+
28+
it("can cleanly merge 2 files", function() {
29+
var ourFileName = "ourNewFile.txt";
30+
var theirFileName = "theirNewFile.txt";
31+
32+
var ourFileContent = "I like Toll Roads. I have an EZ-Pass!";
33+
var theirFileContent = "I'm skeptical about Toll Roads";
34+
35+
var ourSignature = nodegit.Signature.create
36+
("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456789, 60);
37+
var theirSignature = nodegit.Signature.create
38+
("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60);
39+
40+
var repository = this.repository;
41+
var ourCommit;
42+
var theirCommit;
43+
var ourBranch;
44+
var theirBranch;
45+
46+
return fse.writeFile(path.join(repository.workdir(), ourFileName),
47+
ourFileContent)
48+
// Load up the repository index and make our initial commit to HEAD
49+
.then(function() {
50+
return repository.openIndex()
51+
.then(function(index) {
52+
index.read(1);
53+
index.addByPath(ourFileName);
54+
index.write();
55+
56+
return index.writeTree();
57+
});
58+
})
59+
.then(function(oid) {
60+
assert.equal(oid.toString(),
61+
"11ead82b1135b8e240fb5d61e703312fb9cc3d6a");
62+
63+
return repository.createCommit("HEAD", ourSignature,
64+
ourSignature, "we made a commit", oid, []);
65+
})
66+
.then(function(commitOid) {
67+
assert.equal(commitOid.toString(),
68+
"91a183f87842ebb7a9b08dad8bc2473985796844");
69+
70+
return repository.getCommit(commitOid).then(function(commit) {
71+
ourCommit = commit;
72+
}).then(function() {
73+
return repository.createBranch(ourBranchName, commitOid)
74+
.then(function(branch) {
75+
ourBranch = branch;
76+
return repository.createBranch(theirBranchName, commitOid);
77+
});
78+
});
79+
})
80+
.then(function(branch) {
81+
theirBranch = branch;
82+
return fse.writeFile(path.join(repository.workdir(), theirFileName),
83+
theirFileContent);
84+
})
85+
.then(function() {
86+
return repository.openIndex()
87+
.then(function(index) {
88+
index.read(1);
89+
index.addByPath(theirFileName);
90+
index.write();
91+
92+
return index.writeTree();
93+
});
94+
})
95+
.then(function(oid) {
96+
assert.equal(oid.toString(),
97+
"76631cb5a290dafe2959152626bb90f2a6d8ec94");
98+
99+
return repository.createCommit(theirBranch.name(), theirSignature,
100+
theirSignature, "they made a commit", oid, [ourCommit]);
101+
})
102+
.then(function(commitOid) {
103+
assert.equal(commitOid.toString(),
104+
"0e9231d489b3f4303635fc4b0397830da095e7e7");
105+
106+
return repository.getCommit(commitOid).then(function(commit) {
107+
theirCommit = commit;
108+
});
109+
})
110+
.then(function() {
111+
return nodegit.Merge.commits(repository, ourCommit, theirCommit);
112+
})
113+
.then(function(index) {
114+
assert(!index.hasConflicts());
115+
index.write();
116+
return index.writeTreeTo(repository);
117+
})
118+
.then(function(oid) {
119+
assert.equal(oid.toString(),
120+
"76631cb5a290dafe2959152626bb90f2a6d8ec94");
121+
122+
return repository.createCommit(ourBranch.name(), ourSignature,
123+
ourSignature, "we merged their commit", oid,
124+
[ourCommit, theirCommit]);
125+
})
126+
.then(function(commitId) {
127+
assert.equal(commitId.toString(),
128+
"eedee554af34dd4001d8abc799cb55bb7e56a58b");
129+
});
130+
});
131+
132+
it("can merge 2 branchs with conflicts on a single file", function () {
133+
var baseFileContent = "All Bobs are created equal. ish.\n";
134+
var ourFileContent = "Big Bobs are best, IMHO.\n";
135+
var theirFileContent = "Nobody expects the small Bobquisition!\n";
136+
var finalFileContent =
137+
"Big Bobs are beautiful, and the small are unexpected!\n";
138+
139+
var baseSignature = nodegit.Signature.create
140+
("Peaceful Bob", "justchill@bob.net", 123456789, 60);
141+
var ourSignature = nodegit.Signature.create
142+
("Big Bob", "impressive@bob.net", 123456789, 60);
143+
var theirSignature = nodegit.Signature.create
144+
("Small Bob", "underestimated@bob.net", 123456789, 60);
145+
146+
var repository = this.repository;
147+
var baseCommit;
148+
var baseCommitOid;
149+
var ourCommit;
150+
var theirCommit;
151+
var ourBranch;
152+
var theirBranch;
153+
var fileName = "newFile.txt";
154+
155+
return fse.writeFile(path.join(repository.workdir(), fileName),
156+
baseFileContent)
157+
.then(function() {
158+
return repository.openIndex()
159+
.then(function(index) {
160+
index.read(1);
161+
index.addByPath(fileName);
162+
index.write();
163+
164+
return index.writeTree();
165+
});
166+
})
167+
.then(function(oid) {
168+
assert.equal(oid.toString(),
169+
"ea2f6521fb8097a881f43796946ac1603e1f4d75");
170+
171+
return repository.createCommit("HEAD", baseSignature,
172+
baseSignature, "bobs are all ok", oid, []);
173+
})
174+
.then(function(commitOid) {
175+
assert.equal(commitOid.toString(),
176+
"a9b202f7612273fb3a68f718304298704eaeb735");
177+
baseCommitOid = commitOid;
178+
179+
return repository.getCommit(commitOid).then(function(commit) {
180+
baseCommit = commit;
181+
});
182+
})
183+
.then(function() {
184+
return repository.createBranch(ourBranchName, baseCommitOid)
185+
.then(function(branch) {
186+
ourBranch = branch;
187+
});
188+
})
189+
.then(function() {
190+
return repository.createBranch(theirBranchName, baseCommitOid)
191+
.then(function(branch) {
192+
theirBranch = branch;
193+
});
194+
})
195+
.then(function() {
196+
return fse.writeFile(path.join(repository.workdir(), fileName),
197+
ourFileContent);
198+
})
199+
.then(function() {
200+
return repository.openIndex().then(function(index) {
201+
index.read(1);
202+
index.addByPath(fileName);
203+
index.write();
204+
205+
return index.writeTree();
206+
});
207+
})
208+
.then(function(oid) {
209+
assert.equal(oid.toString(),
210+
"c39b1e38b09085856cec7e7ff33e90f5a537d8a5");
211+
212+
return repository.createCommit(ourBranch.name(), ourSignature,
213+
ourSignature, "lol big bobs :yesway:", oid, [baseCommit]);
214+
})
215+
.then(function(commitOid) {
216+
assert.equal(commitOid.toString(),
217+
"935a89c09ad757a9dde2c0257f6f1e379f71816f");
218+
219+
return repository.getCommit(commitOid).then(function(commit) {
220+
ourCommit = commit;
221+
});
222+
})
223+
.then(function() {
224+
return fse.writeFile(path.join(repository.workdir(), fileName),
225+
theirFileContent);
226+
})
227+
.then(function() {
228+
return repository.openIndex().then(function(index) {
229+
index.read(1);
230+
index.addByPath(fileName);
231+
index.write();
232+
233+
return index.writeTree();
234+
});
235+
})
236+
.then(function(oid) {
237+
assert.equal(oid.toString(),
238+
"d1a894a9a4a8c820eb66c82cdd7e6b76c8f713cb");
239+
240+
return repository.createCommit(theirBranch.name(), theirSignature,
241+
theirSignature, "lol big bobs :poop:", oid, [baseCommit]);
242+
})
243+
.then(function(commitOid) {
244+
assert.equal(commitOid.toString(),
245+
"bebb9ec2e0684c7cb7c1e1601c7d5a8f52b8b123");
246+
247+
return repository.getCommit(commitOid).then(function(commit) {
248+
theirCommit = commit;
249+
});
250+
})
251+
.then(function() {
252+
return nodegit.Reference.lookup(repository, "HEAD")
253+
.then(function(head) {
254+
return head.symbolicSetTarget(ourBranch.name(), ourSignature, "");
255+
});
256+
})
257+
.then(function() {
258+
return nodegit.Merge.commits(repository, ourCommit, theirCommit, null);
259+
})
260+
.then(function(index) {
261+
assert(index.hasConflicts());
262+
fse.writeFileSync(path.join(repository.workdir(), fileName),
263+
finalFileContent);
264+
})
265+
.then(function() {
266+
return repository.openIndex().then(function(index) {
267+
index.read(1);
268+
index.addByPath(fileName);
269+
index.write();
270+
271+
return index.writeTree();
272+
});
273+
})
274+
.then(function(oid) {
275+
assert.equal(oid.toString(),
276+
"b1cd49a27cd33b99ab6dad2fb82b3174812a8b47");
277+
278+
return repository.createCommit(ourBranch.name(), baseSignature,
279+
baseSignature, "Stop this bob sized fued", oid,
280+
[ourCommit, theirCommit]);
281+
})
282+
.then(function(commitId) {
283+
assert.equal(commitId.toString(),
284+
"49014ccabf5125f9b69316acde36f891dfdb8b5c");
285+
});
286+
});
287+
});

0 commit comments

Comments
 (0)