-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.js
201 lines (186 loc) · 5.65 KB
/
git.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
'use strict';
/* exports:
setGitPath("path/to/git")
setGitRepoBase("basedir")
branchList()
diffState("reviewBranch", "targetBranch", cb(reviewBranchFileName, targetBranchTempFilename, conflictFlag))
*/
const cp = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
exports.CONFILCT = 1;
exports.MODIFIED = 2;
exports.NEW = 3;
exports.DELETED = 4;
let gitPath = 'git';
let gitRepoBase = null;
const gitRun = cmdArgs => {
return new Promise((resolve, reject) => {
const subProc = cp.spawn(gitPath, cmdArgs, {
cwd: gitRepoBase || process.cwd(),
env: process.env
});
let data = [],
err = [];
subProc.stdout.on('data', d => data.push(d));
subProc.stderr.on('data', d => err.push(d));
subProc.on('exit', () => {
if (!data.length) {
if (err.length) reject(Buffer.concat(err)
.toString());
else resolve('');
} else resolve(Buffer.concat(data)
.toString());
});
});
};
exports.setGitPath = newPath => gitPath = newPath;
exports.setGitRepoBase = newBase => gitRepoBase = newBase;
exports.currentBranch = () => {
return gitRun(['rev-parse', '--abbrev-ref', 'HEAD'])
.then(result => result.trim());
};
const fileStatusList = () => gitRun(['status', '-s', '--porcelain'])
.then(modifiedString => {
const matcher = /\s*([^\s]+)\s+(.+)/y;
let match = matcher.exec(modifiedString);
let files = {};
while (match) {
files[match[2].trim()] = match[1].trim();
match = matcher.exec(modifiedString);
}
return files;
});
const tmpFile = (branch, file) => {
return gitRun(['show', `${branch}:${file}`])
.then(fileContent => {
return new Promise((resolve, reject) =>
fs.mkdtemp(path.join(os.tmpdir(), 'vscode'), (err, folder) => {
if (err) return reject(err.message);
let fName = path.join(folder, file.replace(/[\\\/]/g, '!'));
fs.writeFile(fName, fileContent, err => {
if (err) return reject(err.message);
resolve(fName);
});
}));
});
};
const blankFile = () => {
return new Promise((resolve, reject) =>
fs.mkdtemp(path.join(os.tmpdir(), 'vscode'), (err, folder) => {
if (err) return reject(err.message);
let fName = path.join(folder, 'null');
fs.writeFile(fName, "", err => {
if (err) return reject(err.message);
resolve(fName);
});
}));
};
function dump(fr) {
return function(n) {
console.log(fr, "::", n);
return n;
};
}
exports.diffState = (reviewBranch, targetBranch) => {
reviewBranch = reviewBranch.trim();
targetBranch = targetBranch.trim();
return gitRun(['branch'])
.then(resultString => {
const matcher = /[\s\*]*(.+)/y;
let match = matcher.exec(resultString);
let result = [];
let returnBranch = "";
while (match) {
result.push(match[1].trim());
match = matcher.exec(resultString);
}
let outputbranch = 'vscodeReview_';
while (result.includes(outputbranch)) {
outputbranch += 'x';
}
// move to the target
return gitRun(['checkout', targetBranch])
.then(dump('checkout'), dump('checkout F'))
// create a junk branch
.then(() => gitRun(['checkout', '-b', outputbranch])) // returns data on stderr
.then(dump('checkout -b'), dump('checkout -b F'))
// run the intended merge
.then(() => gitRun(['merge', '--no-ff', '--no-commit', reviewBranch])
// .then(dump('merge into new'), dump('merge into new F'))
// get the status, aborting if it failed
.then(fileStatusList,
failure => {
return fileStatusList()
.then(list => gitRun(['merge', '--abort'])
.then(dump('merge abort'), dump('merge abort F'))
.then(dump('merge --abort'))
.then(() => list));
})
// clear the changes
.then(files => gitRun(['reset', '--hard', 'HEAD~1'])
.then(dump('reset'), dump('reset F'))
// return to the reviewBranch
.then(() => gitRun(['checkout', reviewBranch])
.then(dump('reset'), dump('reset F'))
// drop the junk branch
.then(() => gitRun(['branch', '-D', outputbranch]))
.then(() => {
let rtnPromises = [];
let count = 0;
// we now have the required file list
for (let file in files) {
count++;
console.log("file:", file);
switch (files[file][0]) {
case 'U': // conflict
rtnPromises.push(Promise.resolve([file, null, exports.CONFILCT]));
break;
case 'M': // modified
rtnPromises.push(tmpFile(targetBranch, file)
.then(tmp => [file, tmp, exports.MODIFIED]));
break;
case 'A': // added
case 'C': // copied
rtnPromises.push(
blankFile()
.then(tmp => [file, tmp, exports.NEW]));
break;
case 'D': // deleted
rtnPromises.push(
tmpFile(targetBranch, file)
.then(tmp => blankFile()
.then(blnk => [blnk, tmp, exports.DELETED, file])));
break;
case 'R': // renamed (deleted+added)
// there must be more info here.
console.log("Rename: ", file);
// tmpFile(targetBranch, file)
// .then(tmp => cb(file, tmp));
break;
default:
break;
}
}
return Promise.all(rtnPromises);
}))));
});
};
exports.branchList = () => {
return gitRun(['branch', '-v', '--sort=-committerdate'])
.then(resultString => {
const matcher = /[\s\*]+(.+?)\s+([a-fA-F0-9]+)\s+(.*)/y;
let match = matcher.exec(resultString);
let result = [];
while (match) {
result.push({
name: match[1].trim(),
sha: match[2].trim(),
msg: match[3].trim()
});
match = matcher.exec(resultString);
}
return result;
});
};