1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var Fs = require("fs-extra");
|
4 | var Minimist = require("minimist");
|
5 | var git_1 = require("./git");
|
6 | var local_storage_1 = require("./local-storage");
|
7 | var paths_1 = require("./paths");
|
8 | var step_1 = require("./step");
|
9 | var submodule_1 = require("./submodule");
|
10 | var utils_1 = require("./utils");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function init() {
|
16 | if (require.main !== module) {
|
17 | return;
|
18 | }
|
19 | var argv = Minimist(process.argv.slice(2), {
|
20 | string: ['_'],
|
21 | });
|
22 | var method = argv._[0];
|
23 | var arg1 = argv._[1];
|
24 | switch (method) {
|
25 | case 'reword':
|
26 | return rewordRecentStep(arg1);
|
27 | case 'super-pick':
|
28 | return superPickStep(arg1);
|
29 | case 'rebranch-super':
|
30 | return rebranchSuperSteps();
|
31 | case 'stash-rebase-state':
|
32 | return stashRebaseState(arg1);
|
33 | }
|
34 | }
|
35 | init();
|
36 |
|
37 |
|
38 | function rewordRecentStep(message) {
|
39 |
|
40 | var commitMessage = git_1.Git.recentCommit(['--format=%s']);
|
41 | var stepDescriptor = step_1.Step.descriptor(commitMessage);
|
42 | var nextStep = getNextStep(stepDescriptor);
|
43 |
|
44 | var args = ['commit', '--amend', '--allow-empty'];
|
45 |
|
46 | if (message) {
|
47 | args.push('-m', message);
|
48 | }
|
49 |
|
50 | local_storage_1.localStorage.setItem('HOOK_STEP', nextStep);
|
51 |
|
52 | if (stepDescriptor && step_1.Step.ensureStepMap()) {
|
53 | step_1.Step.updateStepMap('reset', { oldStep: stepDescriptor.number, newStep: nextStep });
|
54 | }
|
55 |
|
56 | git_1.Git.print(args);
|
57 | }
|
58 | exports.rewordRecentStep = rewordRecentStep;
|
59 |
|
60 |
|
61 | function superPickStep(hash) {
|
62 | var message = git_1.Git.recentCommit(hash, ['--format=%s']);
|
63 | var oldStep = step_1.Step.descriptor(message).number;
|
64 | var newStep = step_1.Step.nextSuper();
|
65 |
|
66 | var diff = newStep - oldStep;
|
67 | var stepFilePattern = /step(\d+)\.(md|tmpl)/g;
|
68 | var patch = git_1.Git(['format-patch', '-1', hash, '--stdout']);
|
69 |
|
70 | var fixedPatch = patch.replace(stepFilePattern, function (file, step, extension) {
|
71 | step = Number(step) + diff;
|
72 | return "step" + step + "." + extension;
|
73 | });
|
74 | var submoduleCwd = local_storage_1.localStorage.getItem('SUBMODULE_CWD');
|
75 | var stepMap = step_1.Step.getStepMap(submoduleCwd, true);
|
76 | if (stepMap) {
|
77 |
|
78 | var submodule_2 = submodule_1.Submodule.getLocalName(submoduleCwd);
|
79 | var diffStepPattern = /\{\{\s*diffStep\s+"?(\d+\.\d+)"?.*\}\}/g;
|
80 |
|
81 | fixedPatch = fixedPatch.replace(diffStepPattern, function (helper, oldStepId) {
|
82 | var helperSubmodule = helper.match(/module\s?=\s?"?([^\s"]+)"?/);
|
83 | helperSubmodule = helperSubmodule ? helperSubmodule[1] : '';
|
84 |
|
85 | if (helperSubmodule !== submodule_2) {
|
86 | return helper;
|
87 | }
|
88 |
|
89 | var newStepId = stepMap[oldStepId] || 'XX.XX';
|
90 | return helper.replace(/(diffStep\s+"?)\d+\.\d+/, "$1" + newStepId);
|
91 | });
|
92 | }
|
93 |
|
94 | git_1.Git(['am'], {
|
95 | input: fixedPatch,
|
96 | });
|
97 | }
|
98 | exports.superPickStep = superPickStep;
|
99 |
|
100 | function rebranchSuperSteps() {
|
101 | var rootBranch = git_1.Git.activeBranchName();
|
102 |
|
103 | try {
|
104 | git_1.Git(['branch', '-D', rootBranch + "-root"]);
|
105 | }
|
106 | catch (e) {
|
107 |
|
108 | }
|
109 |
|
110 | git_1.Git(['branch']).split('\n').filter(function (branch) {
|
111 | return branch.match(new RegExp(rootBranch + "-step\\d+"));
|
112 | })
|
113 | .forEach(function (branch) {
|
114 | git_1.Git(['branch', '-D', branch.trim()]);
|
115 | });
|
116 |
|
117 | git_1.Git(['branch', rootBranch + "-root", git_1.Git.rootHash()]);
|
118 |
|
119 | git_1.Git(['log', '--format=%H %s', '--grep=^Step [0-9]\\+:'])
|
120 | .split('\n')
|
121 | .filter(Boolean)
|
122 | .map(function (log) {
|
123 | var message = log.split(' ');
|
124 | var hash = message.shift();
|
125 | message = message.join(' ');
|
126 | return {
|
127 | number: step_1.Step.descriptor(message).number,
|
128 | hash: hash,
|
129 | };
|
130 | })
|
131 | .forEach(function (step) {
|
132 | git_1.Git(['branch', rootBranch + "-step" + step.number, step.hash]);
|
133 | });
|
134 | }
|
135 | exports.rebranchSuperSteps = rebranchSuperSteps;
|
136 | function getPreviousEditedSteps() {
|
137 | return git_1.Git(['log', '--format=%s'], {
|
138 | cwd: paths_1.Paths.rebaseStates,
|
139 | }).split('\n').slice(1);
|
140 | }
|
141 | exports.getPreviousEditedSteps = getPreviousEditedSteps;
|
142 |
|
143 |
|
144 | function hardResetRebaseState(step) {
|
145 |
|
146 | var rebaseStateObject = git_1.Git(['log', "--grep=" + step, '--format=%H'], {
|
147 | cwd: paths_1.Paths.rebaseStates,
|
148 | });
|
149 | git_1.Git(['reset', '--hard', rebaseStateObject], {
|
150 | cwd: paths_1.Paths.rebaseStates,
|
151 | });
|
152 | var rebaseStatesStorage = new local_storage_1.localStorage.native(paths_1.Paths.rebaseStates);
|
153 | var head = rebaseStatesStorage.getItem('HEAD');
|
154 |
|
155 | git_1.Git(['reset', '--hard', head]);
|
156 |
|
157 | local_storage_1.localStorage.setItem('REBASE_OLD_STEP', rebaseStatesStorage.getItem('REBASE_OLD_STEP'));
|
158 | local_storage_1.localStorage.setItem('REBASE_NEW_STEP', rebaseStatesStorage.getItem('REBASE_NEW_STEP'));
|
159 | local_storage_1.localStorage.removeItem('REBASE_HOOKS_DISABLED');
|
160 |
|
161 | git_1.Git(['rebase', '--edit-todo'], {
|
162 | env: {
|
163 | GIT_SEQUENCE_EDITOR: "node " + paths_1.Paths.tortilla.editor + " reset-todo"
|
164 | }
|
165 | });
|
166 | var statusMessage = git_1.Git(['log', '-1', '--format=%h... %s']);
|
167 |
|
168 | console.log(utils_1.freeText("\n Stopped at " + statusMessage + "\n You can amend the commit now, with\n\n git commit --amend\n\n Once you are satisfied with your changes, run\n\n git rebase --continue\n "));
|
169 | }
|
170 | exports.hardResetRebaseState = hardResetRebaseState;
|
171 |
|
172 | function stashRebaseState(todoFile) {
|
173 | var todo = Fs.readFileSync(todoFile).toString();
|
174 | var tasks = todo.split('\n');
|
175 | var _a = tasks[0].trim().match(/edit +(\w+) +Step +(\d+(?:\.\d+)?)/) || [], _b = _a[1], head = _b === void 0 ? '' : _b, _c = _a[2], step = _c === void 0 ? '' : _c;
|
176 | if (!head || !step) {
|
177 | return;
|
178 | }
|
179 | var rebaseStatesStorage = new local_storage_1.localStorage.native(paths_1.Paths.rebaseStates);
|
180 |
|
181 |
|
182 | todo = tasks.slice(1).join('\n');
|
183 |
|
184 | rebaseStatesStorage.setItem('REBASE_OLD_STEP', local_storage_1.localStorage.getItem('REBASE_OLD_STEP') || step);
|
185 | rebaseStatesStorage.setItem('REBASE_NEW_STEP', local_storage_1.localStorage.getItem('REBASE_NEW_STEP') || step);
|
186 | rebaseStatesStorage.setItem('HEAD', git_1.Git(['rev-parse', head]));
|
187 | rebaseStatesStorage.setItem('TODO', todo);
|
188 |
|
189 | git_1.Git(['add', '.'], {
|
190 | cwd: paths_1.Paths.rebaseStates,
|
191 | });
|
192 | git_1.Git(['commit', '-m', step], {
|
193 | cwd: paths_1.Paths.rebaseStates,
|
194 | });
|
195 | }
|
196 | exports.stashRebaseState = stashRebaseState;
|
197 |
|
198 | function getNextStep(stepDescriptor) {
|
199 | if (!stepDescriptor) {
|
200 | return '';
|
201 | }
|
202 | var isSubStep = !!stepDescriptor.number.split('.')[1];
|
203 | return isSubStep ? step_1.Step.next(1) : step_1.Step.nextSuper(1);
|
204 | }
|
205 |
|
\ | No newline at end of file |