-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathmerge-with-conflicts.js
More file actions
201 lines (180 loc) · 5.15 KB
/
merge-with-conflicts.js
File metadata and controls
201 lines (180 loc) · 5.15 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var nodegit = require("../");
var path = require("path");
var promisify = require("promisify-node");
var fse = promisify(require("fs-extra"));
fse.ensureDir = promisify(fse.ensureDir);
var repoDir = "../../newRepo";
var fileName = "newFile.txt";
var baseFileContent = "All Bobs are created equal. ish.\n";
var ourFileContent = "Big Bobs are best, IMHO.\n";
var theirFileContent = "Nobody expects the small Bobquisition!\n";
var finalFileContent = "Big Bobs are beautiful and the small are unexpected!\n";
var baseSignature = nodegit.Signature.create("Peaceful Bob",
"justchill@bob.net", 123456789, 60);
var ourSignature = nodegit.Signature.create("Big Bob",
"impressive@bob.net", 123456789, 60);
var theirSignature = nodegit.Signature.create("Small Bob",
"underestimated@bob.net", 123456789, 60);
var ourBranchName = "ours";
var theirBranchName = "theirs";
var repository;
var baseCommit;
var baseCommitOid;
var ourCommit;
var theirCommit;
var ourBranch;
var theirBranch;
// Create a new repository in a clean directory, and add our first file
fse.remove(path.resolve(__dirname, repoDir))
.then(function() {
return fse.ensureDir(path.resolve(__dirname, repoDir));
})
.then(function() {
return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0);
})
.then(function(repo) {
repository = repo;
return fse.writeFile(
path.join(repository.workdir(), fileName),
baseFileContent
);
})
// Load up the repository index and make our initial commit to HEAD
.then(function() {
return repository.refreshIndex();
})
.then(function(index) {
return index.addByPath(fileName)
.then(function() {
return index.write();
})
.then(function() {
return index.writeTree();
});
})
.then(function(oid) {
return repository.createCommit("HEAD", baseSignature,
baseSignature, "bobs are all ok", oid, []);
})
.then(function(commitOid) {
baseCommitOid = commitOid;
return repository.getCommit(commitOid)
.then(function(commit) {
baseCommit = commit;
});
})
// create our branches
.then(function() {
return repository.createBranch(ourBranchName, baseCommitOid)
.then(function(branch) {
ourBranch = branch;
});
})
.then(function() {
return repository.createBranch(theirBranchName, baseCommitOid)
.then(function(branch) {
theirBranch = branch;
});
})
// Write and commit our version of the file
.then(function() {
return fse.writeFile(
path.join(repository.workdir(), fileName),
ourFileContent
);
})
.then(function() {
return repository.refreshIndex()
.then(function(index) {
return index.addByPath(fileName)
.then(function() {
return index.write();
})
.then(function() {
return index.writeTree();
});
});
})
.then(function(oid) {
return repository.createCommit(ourBranch.name(), ourSignature,
ourSignature, "lol big bobs :yesway:", oid, [baseCommit]);
})
.then(function(commitOid) {
return repository.getCommit(commitOid).then(function(commit) {
ourCommit = commit;
});
})
// Write and commit their version of the file
.then(function() {
return fse.writeFile(
path.join(repository.workdir(), fileName),
theirFileContent
);
})
.then(function() {
return repository.refreshIndex()
.then(function(index) {
return index.addByPath(fileName)
.then(function() {
return index.write();
})
.then(function() {
return index.writeTree();
});
});
})
.then(function(oid) {
return repository.createCommit(theirBranch.name(), theirSignature,
theirSignature, "lol big bobs :poop:", oid, [baseCommit]);
})
.then(function(commitOid) {
return repository.getCommit(commitOid).then(function(commit) {
theirCommit = commit;
});
})
// move the head to our branch, just to keep things tidy
.then(function() {
return nodegit.Reference.lookup(repository, "HEAD");
})
.then(function(head) {
return head.symbolicSetTarget(ourBranch.name(), "");
})
// Merge their branch into our branch
.then(function() {
return nodegit.Merge.commits(repository, ourCommit, theirCommit, null);
})
// Merging returns an index that isn't backed by the repository.
// You have to write it to the repository instead of just writing it.
.then(function(index) {
if (index.hasConflicts()) {
console.log("Conflict time!");
// if the merge had comflicts, solve them
// (in this case, we simply overwrite the file)
fse.writeFileSync(
path.join(repository.workdir(), fileName),
finalFileContent
);
}
})
// we need to get a new index as the other one isnt backed to
// the repository in the usual fashion, and just behaves weirdly
.then(function() {
return repository.refreshIndex()
.then(function(index) {
return index.addByPath(fileName)
.then(function() {
return index.write();
})
.then(function() {
return index.writeTree();
});
});
})
.then(function(oid) {
// create the new merge commit on our branch
return repository.createCommit(ourBranch.name(), baseSignature,
baseSignature, "Stop this bob sized fued", oid, [ourCommit, theirCommit]);
})
.done(function(commitId) {
console.log("New Commit: ", commitId);
});