UNPKG

5.32 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var Minimist = require("minimist");
4var git_1 = require("./git");
5var local_storage_1 = require("./local-storage");
6var step_1 = require("./step");
7var submodule_1 = require("./submodule");
8/**
9 The rebase module is responsible for performing tasks done by the editor using an
10 interactive rebase.
11 */
12function 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}
30init();
31// Responsible for editing the recent commit's message. It will also sort the step's
32// number if needed
33function rewordRecentStep(message) {
34 // Calculate next step based on the current commit's message
35 var commitMessage = git_1.Git.recentCommit(['--format=%s']);
36 var stepDescriptor = step_1.Step.descriptor(commitMessage);
37 var nextStep = getNextStep(stepDescriptor);
38 // Open the editor by default
39 var args = ['commit', '--amend', '--allow-empty'];
40 // If message provided skip editor
41 if (message) {
42 args.push('-m', message);
43 }
44 // Specified step is gonna be used for when forming the commit message
45 local_storage_1.localStorage.setItem('HOOK_STEP', nextStep);
46 // This will be used later on to update the manuals
47 if (stepDescriptor && step_1.Step.ensureStepMap()) {
48 step_1.Step.updateStepMap('reset', { oldStep: stepDescriptor.number, newStep: nextStep });
49 }
50 // commit, let git hooks do the rest
51 git_1.Git.print(args);
52}
53exports.rewordRecentStep = rewordRecentStep;
54// The super-picker is responsible for cherry-picking super steps and
55// rename their manual files
56function 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 // Fetch patch data
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 // Replace references for old manual files with new manual files
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 // The submodule cwd was given from another process of tortilla
73 var submodule_2 = submodule_1.Submodule.getLocalName(submoduleCwd);
74 var diffStepPattern = /\{\{\s*diffStep\s+"?(\d+\.\d+)"?.*\}\}/g;
75 // Replace indexes presented in diffStep() template helpers
76 fixedPatch = fixedPatch.replace(diffStepPattern, function (helper, oldStepId) {
77 var helperSubmodule = helper.match(/module\s?=\s?"?([^\s"]+)"?/);
78 helperSubmodule = helperSubmodule ? helperSubmodule[1] : '';
79 // Don't replace anything if submodules don't match
80 if (helperSubmodule !== submodule_2) {
81 return helper;
82 }
83 // In case step has been removed in the process, replace it with a meaningless placeholder
84 var newStepId = stepMap[oldStepId] || 'XX.XX';
85 return helper.replace(/(diffStep\s+"?)\d+\.\d+/, "$1" + newStepId);
86 });
87 }
88 // Apply patch
89 git_1.Git(['am'], {
90 input: fixedPatch,
91 });
92}
93exports.superPickStep = superPickStep;
94// Updates the branches referencing all super steps
95function rebranchSuperSteps() {
96 var rootBranch = git_1.Git.activeBranchName();
97 // Delete root
98 try {
99 git_1.Git(['branch', '-D', rootBranch + "-root"]);
100 }
101 catch (e) {
102 // Ignore
103 }
104 // Delete steps
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 // Branch root
112 git_1.Git(['branch', rootBranch + "-root", git_1.Git.rootHash()]);
113 // Branch steps
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}
130exports.rebranchSuperSteps = rebranchSuperSteps;
131// Calculate the next step dynamically based on its super flag
132function 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//# sourceMappingURL=rebase.js.map
\No newline at end of file