forked from nmrugg/stockfish.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolling_merge.js
359 lines (320 loc) · 10.2 KB
/
rolling_merge.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// jshint bitwise:true, curly:true, eqeqeq:true, forin:true, immed:true, latedef:true, newcap:true, noarg:true, noempty:true, nonew:true, plusplus:true, quotmark:double, strict:true, undef:true, unused:strict, node:true
/// Usage:
/// node rolling-merge.js DATA_FILE.JSON
/// OR
/// node rolling-merge.js BRANCH_TO_MERGE [FORK_SHA]
"use strict";
var execFile = require("child_process").execFile;
var fs = require("fs");
var merge_candidates;
var build_path = require("path").join(__dirname, "build.sh");
var data_file = process.argv[2];
var branch;
var merge_data;
var total_commits;
try {
merge_data = JSON.parse(fs.readFileSync(data_file, "utf8"));
branch = merge_data.branch;
merge_data.merged = merge_data.merged || [];
} catch (e) {
branch = data_file;
data_file = undefined;
}
if (!branch) {
error("Error: No branch.");
error("Usage: node rolling-merge.js DATA_FILE.JSON");
console.log(" OR");
error("Usage: node rolling-merge.js BRANCH_TO_MERGE [FORK_SHA]");
return;
}
function store_commit(sha)
{
if (data_file && merge_data && merge_data.merged.indexOf(sha) === -1) {
merge_data.merged.push(sha);
fs.writeFileSync(data_file, JSON.stringify(merge_data, null, " "));
}
}
function beep()
{
process.stdout.write("\u0007");
}
function good(mixed)
{
console.log("\u001B[32m" + mixed + "\u001B[0m");
}
function warn(mixed)
{
console.warn("\u001B[33m" + mixed + "\u001B[0m");
}
function error(mixed)
{
console.error("\u001B[31m" + mixed + "\u001B[0m");
}
function git_cmd(args, dont_throw, cb)
{
if (typeof dont_throw === "function") {
cb = dont_throw;
dont_throw = false;
}
execFile("git", args, function onexec(err, stdout, stderr)
{
if (err && !dont_throw) {
error("Git error: git " + args.join(" ") + "\n");
error(stdout);
error(stderr);
throw new Error(err);
}
if (cb) {
if (dont_throw) {
cb(err, stdout, stderr);
} else {
cb(stdout, stderr);
}
}
});
}
function staged_files_found(cb)
{
git_cmd(["diff-index", "--quiet", "--cached", "HEAD"], true, function onexec(err)
{
if (err) {
if (err.code === 1) {
return cb(true);
}
throw err;
}
cb(false);
});
}
function check_for_changes(cb)
{
git_cmd(["ls-files", "-m"], function onexec(data)
{
cb(Boolean(data.trim()));
});
}
function get_sha1_from_branch(branch, cb)
{
git_cmd(["rev-parse", branch], function onexec(data)
{
cb(data.trim());
});
}
function get_fork_point(sha1, sha2, cb)
{
git_cmd(["merge-base", sha1, sha2], function onexec(data)
{
cb(data.trim());
});
}
function get_commit_history(from_sha, to_sha, cb)
{
git_cmd(["log", "--reverse", "--oneline", from_sha + ".." + to_sha], function onexec(data)
{
//cb(data.trim());
var commits = []
data.trim().split("\n").forEach(function oneach(line, i)
{
var sha = line.substr(0, line.indexOf(" "));
if (sha) {
commits[i] = sha;
} else {
warn("Warning: Cannot parse \"" + line + "\" for sha.");
}
});
cb(commits);
});
}
function async_loop(arr, done, oneach)
{
var len;
if (!Array.isArray(arr)) {
return done({error: "Not an array."});
}
len = arr.length;
(function loop(i)
{
if (i >= len) {
if (done) {
return done();
}
return;
}
oneach(arr[i], function next()
{
loop(i + 1);
}, i);
}(0));
}
function get_merge_candidates(branch, cb)
{
git_cmd(["cherry", "HEAD", branch], function oncheck(data)
{
var commits = []
data.trim().split("\n").forEach(function oneach(line, i)
{
///NOTE: If it's minus, we already have it.
if (line[0] === "+") {
/// Chop off the + and space.
commits[i] = line.substr(2);
}
});
cb(commits);
});
}
function is_candidate(candidates, sha)
{
var is_commited;
if (merge_data && merge_data.merged && merge_data.merged.length) {
is_commited = merge_data.merged.some(function oneach(this_sha)
{
if (this_sha.substr(0, sha.length) === sha) {
return true; /// Break and return.
}
});
}
if (is_commited) {
return false;
}
return candidates.some(function oneach(this_sha)
{
if (this_sha.substr(0, sha.length) === sha) {
return true; /// Break and return.
}
});
}
function cherry_pick(sha, cb)
{
///NOTE: Ideally, merge_candidates should'nt be a global.
if (is_candidate(merge_candidates, sha)) {
console.log("Cherrypicking " + sha);
///NOTE: -x adds the sha to the commit message to make it easier to see where it came from.
git_cmd(["cherry-pick", "-Xignore-all-space", "-x", sha], true, cb);
} else {
/// Skip
cb("skipped");
}
}
function test_it(sha, next)
{
console.log("Building " + sha + ". Please wait...");
//execFile("make", ["build", "ARCH=js"], {cwd: build_dir, env: process.env}, function onexec(err, stdout, stderr)
execFile(build_path, {env: process.env}, function onexec(err, stdout, stderr)
{
if (err || stdout.indexOf("warning: unresolved symbol") > -1 || stderr.indexOf("warning: unresolved symbol") > -1) {
error("Error: Cannot properly build " + sha);
if (stdout) {
console.log("STDOUT:");
error(stdout);
}
if (stderr) {
console.log("STDERR:");
error(stderr);
}
error("Error: Cannot properly build " + sha);
console.log("");
warn("*NOTE* To undo commit the last commit: git reset --hard HEAD~1");
console.log("");
throw new Error(err);
}
good("Built " + sha + " successfully!");
console.log("Testing " + sha + ". Please wait...");
execFile(process.execPath, ["tester.js"], {env: process.env}, function onexec(err, stdout, stderr)
{
if (err) {
error("Error: Failed testing: " + sha);
if (stdout) {
console.log("STDOUT:");
error(stdout);
}
if (stderr) {
console.log("STDERR:");
error(stderr);
}
error("Error: Failed testing: " + sha);
console.log("");
warn("*NOTE* To undo commit the last commit: git reset --hard HEAD~1");
console.log("");
throw new Error(err);
}
good("Tested " + sha + " successfully!");
store_commit(sha);
setImmediate(next);
});
});
}
function attempt_to_merge(sha, next, i)
{
if (total_commits) {
console.log((i + 1) + " / " + total_commits + " (" + (((i + 1) / total_commits) * 100).toFixed(2) + "%)");
}
cherry_pick(sha, function onpick(err, stdout, stderr)
{
if (err) {
/// Nothing changed, keep going.
if (err === "skipped" || stdout.indexOf("no changes added to commit") > -1 || stdout.indexOf("nothing to commit (working directory clean)") > -1 || stdout.indexOf("nothing to commit") > -1) {
store_commit(sha);
good("Skipping " + sha + ".");
return setImmediate(next);
} else if (stderr.indexOf("Automatic cherry-pick failed.") > -1 || stderr.indexOf("error: could not apply") > -1) {
warn("Merge conflict. Please fix manually.");
warn("*NOTE* To abort the merge: git reset --hard HEAD");
console.log("After merging, check build: ./build.sh && node tester.js");
if (data_file) {
console.log("Note, you may need to add " + sha + " manually.");
}
return;
} else {
error("Error: Cannot cherypick " + sha);
if (stdout) {
console.log("STDOUT:");
error(stdout);
}
if (stderr) {
console.log("STDERR:");
error(stderr);
}
throw new Error(err);
}
}
test_it(sha, next);
});
}
function init(cb)
{
check_for_changes(function oncheck(changes)
{
if (changes) {
return error("Found changes. Commit your changes first.");
}
get_sha1_from_branch("HEAD", function onget(head_sha)
{
get_sha1_from_branch(branch, function onget(to_sha)
{
get_fork_point(head_sha, to_sha, function onget(starting_sha)
{
/// Be able to override starting commit.
if (merge_data && merge_data.starting_sha) {
starting_sha = merge_data.starting_sha;
} else {
starting_sha = process.argv[3] || starting_sha;
}
if (!starting_sha) {
throw new Error("Caanot find starting fork point. Override by supplying fork sha.");
}
/// Find out which commits have not been cherry picked yet.
get_merge_candidates(branch, function onget(candidates)
{
merge_candidates = candidates;
get_commit_history(starting_sha, to_sha, function onget(commits)
{
total_commits = commits.length;
async_loop(commits, cb, attempt_to_merge);
});
});
});
});
});
});
}
init(beep);