Skip to content

Commit 7ae35d3

Browse files
committed
merge with conflicts example partly done
1 parent 7fd6152 commit 7ae35d3

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

example/merge-with-conflicts.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
var nodegit = require('../');
2+
var path = require('path');
3+
var Promise = require('nodegit-promise');
4+
var promisify = require('promisify-node');
5+
var fse = promisify(require('fs-extra'));
6+
fse.ensureDir = promisify(fse.ensureDir);
7+
8+
var repoDir = '../../newRepo';
9+
var fileName = 'newFile.txt';
10+
11+
var baseFileContent = 'All Bobs are created equal. ish.';
12+
var ourFileContent = "Big Bobs are best, IMHO.";
13+
var theirFileContent = "Nobody expects the small Bobquisition!";
14+
var finalFileContent = "Big Bobs are beautiful, and the small are unexpected!";
15+
16+
var baseSignature = nodegit.Signature.create("Peaceful Bob", "justchill@bob.net", 123456789, 60);
17+
var ourSignature = nodegit.Signature.create("Big Bob", "impressive@bob.net", 123456789, 60);
18+
var theirSignature = nodegit.Signature.create("Small Bob", "underestimated@bob.net", 123456789, 60);
19+
20+
var ourBranchName = "ours";
21+
var theirBranchName = "theirs";
22+
23+
var repository;
24+
var baseCommit;
25+
var baseCommitOid;
26+
var ourCommit;
27+
var theirCommit;
28+
var ourBranch;
29+
var theirBranch;
30+
31+
// Create a new repository in a clean directory, and add our first file
32+
fse.remove(path.resolve(__dirname, repoDir))
33+
.then(function() {
34+
return fse.ensureDir(path.resolve(__dirname, repoDir));
35+
})
36+
.then(function() {
37+
return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0);
38+
})
39+
.then(function(repo) {
40+
repository = repo;
41+
return fse.writeFile(path.join(repository.workdir(), fileName), baseFileContent);
42+
})
43+
44+
45+
// Load up the repository index and make our initial commit to HEAD
46+
.then(function() {
47+
return repository.openIndex();
48+
})
49+
.then(function(index) {
50+
index.read(1);
51+
index.addByPath(fileName);
52+
index.write()
53+
54+
return index.writeTree();
55+
})
56+
.then(function(oid) {
57+
return repository.createCommit('HEAD', baseSignature, baseSignature, 'bobs are all ok', oid, []);
58+
})
59+
.then(function(commitOid) {
60+
baseCommitOid = commitOid;
61+
return repository.getCommit(commitOid).then(function(commit) {
62+
baseCommit = commit;
63+
});
64+
})
65+
66+
67+
// create our branches
68+
.then(function() {
69+
return repository.createBranch(ourBranchName, baseCommitOid).then(function(branch) {
70+
ourBranch = branch;
71+
});
72+
})
73+
.then(function() {
74+
return repository.createBranch(theirBranchName, baseCommitOid).then(function(branch) {
75+
theirBranch = branch;
76+
});
77+
})
78+
79+
80+
// Write and commit our version of the file
81+
.then(function() {
82+
return fse.writeFile(path.join(repository.workdir(), fileName), ourFileContent);
83+
})
84+
.then(function() {
85+
return repository.openIndex().then(function(index) {
86+
index.read(1);
87+
index.addByPath(fileName);
88+
index.write()
89+
90+
console.log('OURS');
91+
for (var i = 0, l = index.entryCount(); i < l; i++) {
92+
var entry = index.getByIndex(i);
93+
console.log(entry.id().toString() + " " + entry.path());
94+
}
95+
console.log('\n\n');
96+
97+
return index.writeTree();
98+
});
99+
})
100+
.then(function(oid) {
101+
return repository.createCommit(ourBranch.name(), ourSignature, ourSignature, 'lol big bobs :yesway:', oid, [baseCommit]);
102+
})
103+
.then(function(commitOid) {
104+
return repository.getCommit(commitOid).then(function(commit) {
105+
ourCommit = commit;
106+
});
107+
})
108+
109+
110+
// Write and commit their version of the file
111+
.then(function() {
112+
return fse.writeFile(path.join(repository.workdir(), fileName), theirFileContent);
113+
})
114+
.then(function() {
115+
return repository.openIndex().then(function(index) {
116+
index.read(1);
117+
index.addByPath(fileName);
118+
index.write()
119+
console.log('Theirs');
120+
for (var i = 0, l = index.entryCount(); i < l; i++) {
121+
var entry = index.getByIndex(i);
122+
console.log(entry.id().toString() + " " + entry.path());
123+
}
124+
console.log('\n\n');
125+
126+
return index.writeTree();
127+
});
128+
})
129+
.then(function(oid) {
130+
return repository.createCommit(theirBranch.name(), theirSignature, theirSignature, 'lol big bobs :poop:', oid, [baseCommit]);
131+
})
132+
.then(function(commitOid) {
133+
return repository.getCommit(commitOid).then(function(commit) {
134+
theirCommit = commit;
135+
});
136+
})
137+
138+
// Merge their branch into our branch
139+
.then(function() {
140+
return nodegit.Merge.commits(repository, ourCommit, theirCommit, null);
141+
})
142+
143+
// Merging returns an index that isn't backed by the repository.
144+
// You have to write it to the repository instead of just writing it.
145+
.then(function(index) {
146+
return nodegit.Reference.lookup(repository, 'HEAD').then(function(head) {
147+
return head.symbolicSetTarget(ourBranch.name(), ourSignature, "");
148+
}).then(function() {
149+
if (index.hasConflicts) {
150+
console.log('Conflict time!');
151+
for (var i = 0, l = index.entryCount(); i < l; i++) {
152+
var entry = index.getByIndex(i);
153+
console.log(entry.id().toString() + " " + entry.path());
154+
}
155+
console.log('\n\n');
156+
//if the merge had comflicts, solve them (in this case, we simply overwrite the file)
157+
fse.writeFileSync(path.join(repository.workdir(), fileName), finalFileContent);
158+
// read the fs updates into the index
159+
index.read(1);
160+
161+
index.addByPath(fileName);
162+
// mark all conflicts as resolved
163+
index.conflictCleanup();
164+
// write back to the index
165+
index.write();
166+
}
167+
168+
return index.writeTreeTo(repository);
169+
});
170+
})
171+
.then(function(oid) {
172+
// create the new merge commit on our branch
173+
return repository.createCommit(ourBranch.name(), baseSignature, baseSignature, 'Stop this bob sized fued', oid, [ourCommit, theirCommit]);
174+
})
175+
.done(function(commitId) {
176+
console.log('New Commit: ', commitId);
177+
});

0 commit comments

Comments
 (0)