-
Notifications
You must be signed in to change notification settings - Fork 50
/
merge_bare_util.js
276 lines (243 loc) · 11 KB
/
merge_bare_util.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright (c) 2019, Two Sigma Open Source
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of git-meta nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
const assert = require("chai").assert;
const co = require("co");
const colors = require("colors");
const NodeGit = require("nodegit");
const CherryPickUtil = require("./cherry_pick_util");
const ConfigUtil = require("./config_util");
const DoWorkQueue = require("./do_work_queue");
const GitUtil = require("./git_util");
const Open = require("./open");
const UserError = require("./user_error");
/**
* @enum {MODE}
* Flags to describe what type of merge to do.
*/
const MODE = {
NORMAL : 0, // will do a fast-forward merge when possible
FORCE_COMMIT: 1, // will generate merge commit even could fast-forward
};
exports.MODE = MODE;
/**
* Merge in each submodule and update in memory index accordingly.
*
* @param {NodeGit.Repository} repo
* @param {Open.Opener} opener
* @param {NodeGit.Index} mergeIndex
* @param {Object} subs map from sub name to changes
* @param {String} message commit message
* */
exports.mergeSubmoduleBare = co.wrap(function *(repo,
opener,
mergeIndex,
subs,
message) {
assert.instanceOf(repo, NodeGit.Repository);
assert.instanceOf(mergeIndex, NodeGit.Index);
assert.isObject(subs);
assert.isString(message);
const result = {
conflicts: {},
commits: {},
};
const sig = yield ConfigUtil.defaultSignature(repo);
const fetcher = yield opener.fetcher();
const mergeSubmodule = co.wrap(function *(name) {
const subRepo = yield opener.getSubrepo(name, true);
const change = subs[name];
const theirSha = change.newSha;
const ourSha = change.ourSha;
yield fetcher.fetchSha(subRepo, name, theirSha);
yield fetcher.fetchSha(subRepo, name, ourSha);
const theirCommit = yield subRepo.getCommit(theirSha);
const ourCommit = yield subRepo.getCommit(ourSha);
// Commit forwards or backwards are handled at meta repo level.
if ((yield NodeGit.Graph.descendantOf(subRepo, ourSha, theirSha)) ||
(yield NodeGit.Graph.descendantOf(subRepo, theirSha, ourSha))) {
return result; // RETURN
}
console.log(`Submodule ${colors.blue(name)}: merging commit ` +
`${colors.green(theirSha)}.`);
// Start the merge.
let subIndex = yield NodeGit.Merge.commits(subRepo,
ourCommit,
theirCommit,
null);
// Abort if conflicted.
if (subIndex.hasConflicts()) {
result.conflicts[name] = theirSha;
return; // RETURN
}
// Otherwise, finish off the merge.
const treeId = yield subIndex.writeTreeTo(subRepo);
const mergeCommit
= yield subRepo.createCommit(null,
sig,
sig,
message,
treeId,
[ourCommit, theirCommit]);
const mergeSha = mergeCommit.tostrS();
result.commits[name] = mergeSha;
yield CherryPickUtil.addSubmoduleCommit(mergeIndex, name, mergeSha);
});
yield DoWorkQueue.doInParallel(Object.keys(subs), mergeSubmodule);
return result;
});
/**
* Return a formatted string indicating merge will abort for
* irresolvable conflicts.
*/
function formatConflictsMessage(conflicts) {
let errorMessage = "CONFLICT (content): \n";
const names = Object.keys(conflicts).sort();
for (let name of names) {
errorMessage += `Conflicting entries for submodule: ` +
`${colors.red(name)}\n`;
}
errorMessage += "Automatic merge failed\n";
return errorMessage;
}
/**
* Merge `theirCommit` into `ourCommit` in the specified `repo` with specific
* commitMessage. Return `null` if there are merge conflicts and the conflicts
* cannot be resolved automatically. Return an object describing merge commits
* otherwise. Use our commit as the merged commit if our commit is up to date,
* use theirs if this is a fast forward merge and return a new merge commit
* otherwise. Throw a `UserError` if there are no commits in common between
* `theirCommit` and `ourCommit`.
*
* @async
* @param {NodeGit.Repository} repo
* @param {NodeGit.Commit} ourCommit
* @param {NodeGit.Commit} theirCommit
* @param {MODE} mode
* @param {String} commitMessage
* @return {Object}
* @return {String|null} return.metaCommit
* @return {Object} return.submoduleCommits map from submodule to commit
* @return {String|null} return.errorMessage
*/
exports.merge = co.wrap(function *(repo,
ourCommit,
theirCommit,
mode,
commitMessage) {
assert.instanceOf(repo, NodeGit.Repository);
assert.instanceOf(ourCommit, NodeGit.Commit);
assert.instanceOf(theirCommit, NodeGit.Commit);
assert.isNumber(mode);
assert.isString(commitMessage);
const baseCommit = yield GitUtil.getMergeBase(repo, ourCommit, theirCommit);
if (null === baseCommit) {
throw new UserError(`No commits in common between `+
`${colors.red(GitUtil.shortSha(ourCommit.id().tostrS()))} and ` +
`${colors.red(GitUtil.shortSha(theirCommit.id().tostrS()))}`);
}
yield CherryPickUtil.ensureNoURLChanges(repo, ourCommit, theirCommit);
const result = {
metaCommit: null,
submoduleCommits: {},
errorMessage: null,
};
const ourCommitSha = ourCommit.id().tostrS();
const theirCommitSha = theirCommit.id().tostrS();
if (ourCommitSha === theirCommitSha) {
console.log(`Nothing to do for merging ${colors.green(ourCommitSha)}` +
`into itself.`);
result.metaCommit = ourCommitSha;
return result;
}
const upToDate = yield NodeGit.Graph.descendantOf(repo,
ourCommitSha,
theirCommitSha);
if (upToDate) {
console.log(`${colors.green(ourCommitSha)} is up-to-date.`);
result.metaCommit = ourCommitSha;
return result; // RETURN
}
const canFF = yield NodeGit.Graph.descendantOf(repo,
theirCommitSha,
ourCommitSha);
if (canFF && MODE.FORCE_COMMIT !== mode) {
console.log(`Fast-forward merge: `+
`${colors.green(theirCommitSha)} is a descendant of `+
`${colors.green(ourCommitSha)}`);
result.metaCommit = theirCommitSha;
return result; // RETURN
}
const sig = yield ConfigUtil.defaultSignature(repo);
const changeIndex
= yield NodeGit.Merge.commits(repo, ourCommit, theirCommit, []);
const changes
= yield CherryPickUtil.computeChangesBetweenTwoCommits(repo,
changeIndex,
ourCommit,
theirCommit);
if (Object.keys(changes.conflicts).length > 0) {
result.errorMessage = formatConflictsMessage(changes.conflicts);
return result; // RETURN
}
const opener = new Open.Opener(repo, null);
const makeMetaCommit = co.wrap(function *(indexToWrite) {
console.log(`Merging meta-repo commits ` +
`${colors.green(ourCommitSha)} and ` +
`${colors.green(theirCommitSha)}`);
const id = yield indexToWrite.writeTreeTo(repo);
// And finally, commit it.
const metaCommit = yield repo.createCommit(null,
sig,
sig,
commitMessage,
id,
[ourCommit, theirCommit]);
result.metaCommit = metaCommit.tostrS();
console.log(`Merge commit created at ` +
`${colors.green(result.metaCommit)}.`);
});
yield CherryPickUtil.changeSubmodulesBare(repo,
changeIndex,
changes.simpleChanges);
const merges = yield exports.mergeSubmoduleBare(repo,
opener,
changeIndex,
changes.changes,
commitMessage);
if (Object.keys(merges.conflicts).length > 0) {
result.errorMessage = formatConflictsMessage(merges.conflicts);
} else {
yield makeMetaCommit(changeIndex);
result.submoduleCommits = merges.commits;
}
return result;
});