1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | var Fs = require("fs-extra");
|
15 | var Minimist = require("minimist");
|
16 | var Path = require("path");
|
17 | var git_1 = require("./git");
|
18 | var local_storage_1 = require("./local-storage");
|
19 | var paths_1 = require("./paths");
|
20 | var step_1 = require("./step");
|
21 | var utils_1 = require("./utils");
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function init() {
|
28 | if (require.main !== module) {
|
29 | return;
|
30 | }
|
31 | var argv = Minimist(process.argv.slice(2), {
|
32 | string: ['_', 'message', 'm', 'udiff'],
|
33 | });
|
34 |
|
35 | var method = argv._[0];
|
36 | var steps = argv._.slice(1, -1);
|
37 | var rebaseFilePath = argv._[argv._.length - 1];
|
38 |
|
39 | if (method === 'reset-todo') {
|
40 | var todo = new local_storage_1.localStorage.native(paths_1.Paths.rebaseStates).getItem('TODO');
|
41 | Fs.writeFileSync(rebaseFilePath, todo);
|
42 | return;
|
43 | }
|
44 | var message = argv.message || argv.m;
|
45 | var udiff = argv.udiff;
|
46 |
|
47 |
|
48 |
|
49 | var options = {
|
50 | udiff: udiff === 'true' ? '' : udiff,
|
51 | };
|
52 | var rebaseFileContent = Fs.readFileSync(rebaseFilePath, 'utf8');
|
53 |
|
54 | var operations = disassemblyOperations(rebaseFileContent);
|
55 |
|
56 | local_storage_1.localStorage.removeItem('REBASE_HOOKS_DISABLED');
|
57 |
|
58 |
|
59 | switch (method) {
|
60 | case 'edit':
|
61 | editStep(operations, steps, options);
|
62 | break;
|
63 | case 'edit-head':
|
64 | editHead(operations);
|
65 | break;
|
66 | case 'sort':
|
67 | sortSteps(operations, options);
|
68 | break;
|
69 | case 'reword':
|
70 | rewordStep(operations, message);
|
71 | break;
|
72 | case 'render':
|
73 | renderManuals(operations);
|
74 | break;
|
75 | }
|
76 |
|
77 | var newRebaseFileContent = assemblyOperations(operations);
|
78 | Fs.writeFileSync(rebaseFilePath, newRebaseFileContent);
|
79 | }
|
80 | init();
|
81 |
|
82 | function editStep(operations, steps, options) {
|
83 |
|
84 |
|
85 | if (options.udiff != null) {
|
86 |
|
87 | step_1.Step.initializeStepMap(!!options.udiff);
|
88 | }
|
89 | if (!steps || steps.length === 0) {
|
90 | var descriptor = step_1.Step.descriptor(operations[0].message);
|
91 | var step = (descriptor && descriptor.number) || 'root';
|
92 | steps = [step];
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 | steps = steps.map(function (step) { return new String(step); });
|
98 |
|
99 | steps.forEach(function (step) {
|
100 | if (String(step) === 'root') {
|
101 | var operation = operations[0];
|
102 | operation.method = 'edit';
|
103 | step.operation = operation;
|
104 | }
|
105 | else {
|
106 | var operation = operations.find(function (_a) {
|
107 | var message = _a.message;
|
108 | if (!message) {
|
109 | return;
|
110 | }
|
111 | var descriptor = step_1.Step.descriptor(message);
|
112 | return descriptor && descriptor.number === String(step);
|
113 | });
|
114 | if (!operation) {
|
115 | return;
|
116 | }
|
117 | operation.method = 'edit';
|
118 | step.operation = operation;
|
119 | }
|
120 | });
|
121 |
|
122 | if (operations.length > 1) {
|
123 |
|
124 | var descriptor = step_1.Step.descriptor(operations[0].message);
|
125 |
|
126 | if (descriptor) {
|
127 | local_storage_1.localStorage.setItem('REBASE_OLD_STEP', descriptor.number);
|
128 | local_storage_1.localStorage.setItem('REBASE_NEW_STEP', descriptor.number);
|
129 | }
|
130 | else {
|
131 | local_storage_1.localStorage.setItem('REBASE_OLD_STEP', 'root');
|
132 | local_storage_1.localStorage.setItem('REBASE_NEW_STEP', 'root');
|
133 | }
|
134 |
|
135 | var sort_1 = "node " + paths_1.Paths.tortilla.editor + " sort";
|
136 | if (options.udiff) {
|
137 | sort_1 = sort_1 + " --udiff=" + options.udiff;
|
138 | }
|
139 | sort_1 = "GIT_SEQUENCE_EDITOR=\"" + sort_1 + "\"";
|
140 | var saveRebaseState_1 = "GIT_SEQUENCE_EDITOR=\"node " + paths_1.Paths.tortilla.rebase + " stash-rebase-state\"";
|
141 |
|
142 | steps.forEach(function (step) {
|
143 | var operation = step.operation;
|
144 | if (!operation) {
|
145 | return;
|
146 | }
|
147 | var index = operations.indexOf(operation);
|
148 |
|
149 | operations.splice(index + 1, 0, {
|
150 | method: 'exec',
|
151 | command: sort_1 + " git rebase --edit-todo",
|
152 | });
|
153 |
|
154 | operations.splice(index, 0, {
|
155 | method: 'exec',
|
156 | command: saveRebaseState_1 + " git rebase --edit-todo",
|
157 | });
|
158 | });
|
159 | }
|
160 |
|
161 | var rebranchSuper = "GIT_SEQUENCE_EDITOR=\"node " + paths_1.Paths.tortilla.rebase + " rebranch-super\"";
|
162 |
|
163 | operations.push({
|
164 | method: 'exec',
|
165 | command: rebranchSuper + " git rebase --edit-todo",
|
166 | });
|
167 | }
|
168 |
|
169 | function sortSteps(operations, options) {
|
170 |
|
171 | var oldStep = local_storage_1.localStorage.getItem('REBASE_OLD_STEP');
|
172 | var newStep = local_storage_1.localStorage.getItem('REBASE_NEW_STEP');
|
173 | var submoduleCwd = local_storage_1.localStorage.getItem('SUBMODULE_CWD');
|
174 |
|
175 | if (oldStep === newStep) {
|
176 | local_storage_1.localStorage.setItem('REBASE_HOOKS_DISABLED', 1);
|
177 |
|
178 | if (!submoduleCwd) {
|
179 | return;
|
180 | }
|
181 | }
|
182 | var stepLimit = getStepLimit(oldStep, newStep);
|
183 | var editFlag = false;
|
184 | var offset = 0;
|
185 | operations.slice().some(function (operation, index) {
|
186 | var currStepDescriptor = step_1.Step.descriptor(operation.message || '');
|
187 |
|
188 | if (!currStepDescriptor) {
|
189 | return;
|
190 | }
|
191 | var currStepSplit = currStepDescriptor.number.split('.');
|
192 | var currSuperStep = currStepSplit[0];
|
193 | var currSubStep = currStepSplit[1];
|
194 | if (submoduleCwd) {
|
195 |
|
196 | if (!currSubStep) {
|
197 | operations.splice(index + offset, 1, {
|
198 | method: 'exec',
|
199 | command: "node " + paths_1.Paths.tortilla.rebase + " super-pick " + operation.hash,
|
200 | });
|
201 | }
|
202 | }
|
203 | else if (currSuperStep > stepLimit) {
|
204 |
|
205 |
|
206 | operations.splice(index + offset++, 0, {
|
207 | method: 'exec',
|
208 | command: "node " + paths_1.Paths.tortilla.localStorage + " set REBASE_HOOKS_DISABLED 1",
|
209 | });
|
210 |
|
211 | return true;
|
212 | }
|
213 |
|
214 | if (!currSubStep) {
|
215 | operations.splice(index + offset, 1, {
|
216 | method: 'exec',
|
217 | command: "node " + paths_1.Paths.tortilla.rebase + " super-pick " + operation.hash,
|
218 | });
|
219 | }
|
220 |
|
221 |
|
222 |
|
223 | if (operation.method === 'edit') {
|
224 |
|
225 | operations.splice(index + offset++, 0, __assign({}, operation, { method: 'pick' }));
|
226 |
|
227 | operations.splice(index + offset++, 0, {
|
228 | method: 'exec',
|
229 | command: "GIT_EDITOR=true node " + paths_1.Paths.tortilla.rebase + " reword",
|
230 | });
|
231 | var editor = "GIT_SEQUENCE_EDITOR=\"node " + paths_1.Paths.tortilla.editor + " edit-head\"";
|
232 |
|
233 | operations.splice(index + offset++, 0, {
|
234 | method: 'exec',
|
235 | command: editor + " git rebase --edit-todo",
|
236 | });
|
237 | operations.splice(index + offset, 1);
|
238 |
|
239 |
|
240 | return editFlag = true;
|
241 | }
|
242 |
|
243 | operations.splice(index + ++offset, 0, {
|
244 | method: 'exec',
|
245 | command: "GIT_EDITOR=true node " + paths_1.Paths.tortilla.rebase + " reword",
|
246 | });
|
247 | });
|
248 |
|
249 |
|
250 | if (!editFlag) {
|
251 | operations.push({
|
252 | method: 'exec',
|
253 | command: "node " + paths_1.Paths.tortilla.localStorage + " remove HOOK_STEP",
|
254 | });
|
255 |
|
256 | if (options.udiff) {
|
257 | var subCwd = utils_1.Utils.cwd();
|
258 | var cwd = Path.resolve(utils_1.Utils.cwd(), options.udiff);
|
259 |
|
260 |
|
261 |
|
262 | operations.push({
|
263 | method: 'exec',
|
264 | command: utils_1.Utils.shCmd("\n export GIT_DIR=" + cwd + "/.git\n export GIT_WORK_TREE=" + cwd + "\n export TORTILLA_CHILD_PROCESS=true\n export TORTILLA_CWD=" + cwd + "\n export TORTILLA_SUBMODULE_CWD=" + subCwd + "\n\n if node " + paths_1.Paths.cli.tortilla + " step edit --root ; then\n git rebase --continue\n else\n git rebase --abort\n fi\n "),
|
265 | });
|
266 | }
|
267 |
|
268 | operations.push({
|
269 | method: 'exec',
|
270 | command: "node " + paths_1.Paths.tortilla.localStorage + " remove STEP_MAP STEP_MAP_PENDING",
|
271 | });
|
272 | }
|
273 | }
|
274 |
|
275 | function editHead(operations) {
|
276 |
|
277 | var descriptor = step_1.Step.descriptor(operations[0].message);
|
278 |
|
279 | if (descriptor) {
|
280 | local_storage_1.localStorage.setItem('REBASE_OLD_STEP', descriptor.number);
|
281 | }
|
282 | var head = git_1.Git.recentCommit(['--format=%h m']).split(' ');
|
283 | var hash = head.shift();
|
284 | var message = head.join(' ');
|
285 |
|
286 | operations.push({
|
287 | method: 'exec',
|
288 | command: 'git reset --hard HEAD~1',
|
289 | });
|
290 |
|
291 | operations.push({
|
292 | method: 'edit',
|
293 | hash: hash,
|
294 | message: message,
|
295 | });
|
296 | }
|
297 |
|
298 | function rewordStep(operations, message) {
|
299 | var argv = [paths_1.Paths.tortilla.rebase, 'reword'];
|
300 | if (message) {
|
301 | argv.push("\"" + message + "\"");
|
302 | }
|
303 |
|
304 | operations.splice(1, 0, {
|
305 | method: 'exec',
|
306 | command: "node " + argv.join(' '),
|
307 | });
|
308 | }
|
309 |
|
310 | function renderManuals(operations) {
|
311 | var offset = 2;
|
312 |
|
313 | operations.splice(1, 0, {
|
314 | method: 'exec',
|
315 | command: "node " + paths_1.Paths.tortilla.manual + " render --root",
|
316 | });
|
317 | operations.slice(offset).forEach(function (operation, index) {
|
318 | var stepDescriptor = step_1.Step.superDescriptor(operation.message);
|
319 | if (!stepDescriptor) {
|
320 | return;
|
321 | }
|
322 |
|
323 | operations.splice(index + ++offset, 0, {
|
324 | method: 'exec',
|
325 | command: "node " + paths_1.Paths.tortilla.manual + " render " + stepDescriptor.number,
|
326 | });
|
327 | return offset;
|
328 | });
|
329 |
|
330 | operations.push({
|
331 | method: 'exec',
|
332 | command: "node " + paths_1.Paths.tortilla.rebase + " rebranch-super",
|
333 | });
|
334 | }
|
335 |
|
336 |
|
337 | function getStepLimit(oldStep, newStep) {
|
338 | oldStep = oldStep === 'root' ? '0' : oldStep;
|
339 | var newSuperStep = newStep === 'root' ? '0' : newStep;
|
340 |
|
341 | var oldStepSplits = oldStep.split('.');
|
342 | var newStepSplits = newStep.split('.');
|
343 | var oldSuperStep = oldStepSplits[0];
|
344 | newSuperStep = newStepSplits[0];
|
345 | var oldSubStep = oldStepSplits[1];
|
346 | var newSubStep = newStepSplits[1];
|
347 | if (oldSuperStep === newSuperStep) {
|
348 |
|
349 | if (oldSubStep) {
|
350 | return oldSuperStep;
|
351 | }
|
352 |
|
353 | return Infinity;
|
354 | }
|
355 |
|
356 | if (!oldSubStep && newSubStep && Number(newSuperStep) === Number(oldSuperStep) + 1) {
|
357 | return newSuperStep;
|
358 | }
|
359 |
|
360 | if (!newSubStep && oldSubStep && Number(oldSuperStep) === Number(newSuperStep) + 1) {
|
361 | return oldSuperStep;
|
362 | }
|
363 |
|
364 | return Infinity;
|
365 | }
|
366 |
|
367 | function disassemblyOperations(rebaseFileContent) {
|
368 | var operations = rebaseFileContent.match(/^[a-z]+\s.{7}.*$/mg);
|
369 | if (!operations) {
|
370 | return [];
|
371 | }
|
372 | return operations.map(function (line) {
|
373 | var split = line.split(' ');
|
374 | return {
|
375 | method: split[0],
|
376 | hash: split[1],
|
377 | message: split.slice(2).join(' '),
|
378 | };
|
379 | });
|
380 | }
|
381 |
|
382 | function assemblyOperations(operations) {
|
383 | return operations
|
384 |
|
385 | .map(function (operation) { return Object
|
386 | .keys(operation)
|
387 | .map(function (k) { return operation[k]; })
|
388 | .join(' '); })
|
389 |
|
390 | .join('\n') + '\n';
|
391 | }
|
392 |
|
\ | No newline at end of file |