1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var Minimist = require("minimist");
|
4 | var git_1 = require("./git");
|
5 | var local_storage_1 = require("./local-storage");
|
6 | var step_1 = require("./step");
|
7 | var submodule_1 = require("./submodule");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function init() {
|
13 | if (require.main !== module) {
|
14 | return;
|
15 | }
|
16 | var argv = Minimist(process.argv.slice(2), {
|
17 | string: ['_'],
|
18 | });
|
19 | var method = argv._[0];
|
20 | var arg1 = argv._[1];
|
21 | switch (method) {
|
22 | case 'reword':
|
23 | return rewordRecentStep(arg1);
|
24 | case 'super-pick':
|
25 | return superPickStep(arg1);
|
26 | case 'rebranch-super':
|
27 | return rebranchSuperSteps();
|
28 | }
|
29 | }
|
30 | init();
|
31 |
|
32 |
|
33 | function rewordRecentStep(message) {
|
34 |
|
35 | var commitMessage = git_1.Git.recentCommit(['--format=%s']);
|
36 | var stepDescriptor = step_1.Step.descriptor(commitMessage);
|
37 | var nextStep = getNextStep(stepDescriptor);
|
38 |
|
39 | var args = ['commit', '--amend', '--allow-empty'];
|
40 |
|
41 | if (message) {
|
42 | args.push('-m', message);
|
43 | }
|
44 |
|
45 | local_storage_1.localStorage.setItem('HOOK_STEP', nextStep);
|
46 |
|
47 | if (stepDescriptor && step_1.Step.ensureStepMap()) {
|
48 | step_1.Step.updateStepMap('reset', { oldStep: stepDescriptor.number, newStep: nextStep });
|
49 | }
|
50 |
|
51 | git_1.Git.print(args);
|
52 | }
|
53 | exports.rewordRecentStep = rewordRecentStep;
|
54 |
|
55 |
|
56 | function superPickStep(hash) {
|
57 | var message = git_1.Git.recentCommit(hash, ['--format=%s']);
|
58 | var oldStep = step_1.Step.descriptor(message).number;
|
59 | var newStep = step_1.Step.nextSuper();
|
60 |
|
61 | var diff = newStep - oldStep;
|
62 | var stepFilePattern = /step(\d+)\.(md|tmpl)/g;
|
63 | var patch = git_1.Git(['format-patch', '-1', hash, '--stdout']);
|
64 |
|
65 | var fixedPatch = patch.replace(stepFilePattern, function (file, step, extension) {
|
66 | step = Number(step) + diff;
|
67 | return "step" + step + "." + extension;
|
68 | });
|
69 | var submoduleCwd = local_storage_1.localStorage.getItem('SUBMODULE_CWD');
|
70 | var stepMap = step_1.Step.getStepMap(submoduleCwd, true);
|
71 | if (stepMap) {
|
72 |
|
73 | var submodule_2 = submodule_1.Submodule.getLocalName(submoduleCwd);
|
74 | var diffStepPattern = /\{\{\s*diffStep\s+"?(\d+\.\d+)"?.*\}\}/g;
|
75 |
|
76 | fixedPatch = fixedPatch.replace(diffStepPattern, function (helper, oldStepId) {
|
77 | var helperSubmodule = helper.match(/module\s?=\s?"?([^\s"]+)"?/);
|
78 | helperSubmodule = helperSubmodule ? helperSubmodule[1] : '';
|
79 |
|
80 | if (helperSubmodule !== submodule_2) {
|
81 | return helper;
|
82 | }
|
83 |
|
84 | var newStepId = stepMap[oldStepId] || 'XX.XX';
|
85 | return helper.replace(/(diffStep\s+"?)\d+\.\d+/, "$1" + newStepId);
|
86 | });
|
87 | }
|
88 |
|
89 | git_1.Git(['am'], {
|
90 | input: fixedPatch,
|
91 | });
|
92 | }
|
93 | exports.superPickStep = superPickStep;
|
94 |
|
95 | function rebranchSuperSteps() {
|
96 | var rootBranch = git_1.Git.activeBranchName();
|
97 |
|
98 | try {
|
99 | git_1.Git(['branch', '-D', rootBranch + "-root"]);
|
100 | }
|
101 | catch (e) {
|
102 |
|
103 | }
|
104 |
|
105 | git_1.Git(['branch']).split('\n').filter(function (branch) {
|
106 | return branch.match(new RegExp(rootBranch + "-step\\d+"));
|
107 | })
|
108 | .forEach(function (branch) {
|
109 | git_1.Git(['branch', '-D', branch.trim()]);
|
110 | });
|
111 |
|
112 | git_1.Git(['branch', rootBranch + "-root", git_1.Git.rootHash()]);
|
113 |
|
114 | git_1.Git(['log', '--format=%H %s', '--grep=^Step [0-9]\\+:'])
|
115 | .split('\n')
|
116 | .filter(Boolean)
|
117 | .map(function (log) {
|
118 | var message = log.split(' ');
|
119 | var hash = message.shift();
|
120 | message = message.join(' ');
|
121 | return {
|
122 | number: step_1.Step.descriptor(message).number,
|
123 | hash: hash,
|
124 | };
|
125 | })
|
126 | .forEach(function (step) {
|
127 | git_1.Git(['branch', rootBranch + "-step" + step.number, step.hash]);
|
128 | });
|
129 | }
|
130 | exports.rebranchSuperSteps = rebranchSuperSteps;
|
131 |
|
132 | function getNextStep(stepDescriptor) {
|
133 | if (!stepDescriptor) {
|
134 | return '';
|
135 | }
|
136 | var isSubStep = !!stepDescriptor.number.split('.')[1];
|
137 | return isSubStep ? step_1.Step.next(1) : step_1.Step.nextSuper(1);
|
138 | }
|
139 |
|
\ | No newline at end of file |