forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-fork.js
109 lines (96 loc) · 2.62 KB
/
merge-fork.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
'use strict';
/* eslint-disable no-for-of-loops/no-for-of-loops */
const {writeFileSync} = require('fs');
const path = require('path');
const {spawnSync} = require('child_process');
const minimist = require('minimist');
const tmp = require('tmp');
const argv = minimist(process.argv.slice(2), {
boolean: ['reverse'],
default: {
'base-ref': 'HEAD',
},
});
const baseRef = argv['base-ref'];
const baseDir = argv['base-dir'];
function resolvePath(file) {
return baseDir !== undefined ? path.join(baseDir, file) : file;
}
function getTransforms() {
const old = argv.old;
const base = argv.base;
const _new = argv.new;
if (old !== undefined) {
if (_new === undefined) {
throw Error('Cannot provide --old without also providing --new');
}
const oldPath = resolvePath(old);
const newPath = resolvePath(_new);
let basePath;
let fromPath;
let toPath;
if (argv.reverse) {
fromPath = oldPath;
toPath = newPath;
basePath = base !== undefined ? resolvePath(basePath) : oldPath;
} else {
fromPath = newPath;
toPath = oldPath;
basePath = base !== undefined ? resolvePath(basePath) : newPath;
}
return [
{
base: basePath,
from: fromPath,
to: toPath,
},
];
} else if (_new !== undefined) {
throw Error('Cannot provide --new without also providing --old');
}
return argv._.map(filename => {
const oldPath = resolvePath(filename + '.old.js');
const newPath = resolvePath(filename + '.new.js');
let basePath;
let fromPath;
let toPath;
if (argv.reverse) {
fromPath = oldPath;
toPath = newPath;
basePath = base !== undefined ? resolvePath(basePath) : oldPath;
} else {
fromPath = newPath;
toPath = oldPath;
basePath = base !== undefined ? resolvePath(basePath) : newPath;
}
return {
base: basePath,
from: fromPath,
to: toPath,
};
});
}
for (const {base: baseFilename, from, to} of getTransforms()) {
// Write the base file contents to a temporary file
const gitShowResult = spawnSync(
'git',
['show', `${baseRef}:${baseFilename}`],
{
stdio: 'pipe',
}
);
if (gitShowResult.status !== 0) {
console.error('' + gitShowResult.stderr);
continue;
}
const baseFileContents = gitShowResult.stdout;
const base = tmp.fileSync().name;
writeFileSync(base, baseFileContents);
// Run the merge with `git merge-file`
const mergeFileResult = spawnSync('git', ['merge-file', to, base, from], {
stdio: 'pipe',
});
if (mergeFileResult.status !== 0) {
console.error('' + mergeFileResult.stderr);
}
}