Skip to content

Commit bee9244

Browse files
committed
feat: expose branchSequencer to the CLI (--branch-sequencer|-s, subject to change)
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent 90f6796 commit bee9244

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

branchSequencer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ export const branchSequencer: BranchSequencer = async ({
120120
const execSyncInRepo = createExecSyncInRepo(repo);
121121

122122
const checkout = async (cmds: GoodCommand[]): Promise<void> => {
123-
console.log("\ncheckout", cmds.length);
124123
if (!cmds.length) {
125124
return;
126125
}
127126

127+
console.log("\ncheckout", cmds.length);
128+
128129
const goNext = () =>
129130
new Promise<void>((r) => {
130131
setTimeout(() => {

git-stacked-rebase.ts

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { filenames } from "./filenames";
1313
import { configKeys } from "./configKeys";
1414
import { apply, applyIfNeedsToApply } from "./apply";
1515
import { forcePush } from "./forcePush";
16+
import { branchSequencer } from "./branchSequencer";
1617

1718
import { createExecSyncInRepo } from "./util/execSyncInRepo";
1819
import { noop } from "./util/noop";
@@ -40,6 +41,9 @@ export type OptionsForGitStackedRebase = {
4041
apply: boolean;
4142
push: boolean;
4243
forcePush: boolean;
44+
45+
branchSequencer: boolean;
46+
branchSequencerExec: string | false;
4347
};
4448

4549
export type SomeOptionsForGitStackedRebase = Partial<OptionsForGitStackedRebase>;
@@ -52,6 +56,8 @@ const getDefaultOptions = (): OptionsForGitStackedRebase => ({
5256
apply: false,
5357
push: false,
5458
forcePush: false,
59+
branchSequencer: false,
60+
branchSequencerExec: false,
5561
});
5662

5763
function areOptionsIncompetible(
@@ -62,6 +68,8 @@ function areOptionsIncompetible(
6268
if (options.apply) reasons.push("--apply cannot be used together with --view-todo");
6369
if (options.push) reasons.push("--apply cannot be used together with --push");
6470
if (options.forcePush) reasons.push("--apply cannot be used together with --push -f");
71+
if (options.branchSequencer) reasons.push("--apply cannot be used together with --branch-sequencer");
72+
if (options.branchSequencerExec) reasons.push("--apply cannot be used together with --branch-sequencer --exec");
6573
}
6674

6775
/**
@@ -197,6 +205,28 @@ export const gitStackedRebase = async (
197205
});
198206
}
199207

208+
if (options.branchSequencer) {
209+
if (options.branchSequencerExec) {
210+
const toExec: string = options.branchSequencerExec;
211+
212+
return branchSequencer({
213+
gitCmd: options.gitCmd,
214+
repo,
215+
rootLevelCommandName: "--branch-sequencer --exec",
216+
actionInsideEachCheckedOutBranch: ({ execSyncInRepo: execS }) => (execS(toExec), void 0),
217+
pathToStackedRebaseDirInsideDotGit,
218+
pathToStackedRebaseTodoFile,
219+
});
220+
} else {
221+
/**
222+
* we'll likely end up adding more sub-commands
223+
* to branchSequencer later.
224+
*/
225+
226+
return fail("\n--branch-sequencer (without --exec) - nothing to do?\n\n");
227+
}
228+
}
229+
200230
fs.mkdirSync(pathToStackedRebaseDirInsideDotGit, { recursive: true });
201231

202232
const initialBranch: Git.Reference | void = await Git.Branch.lookup(
@@ -1065,19 +1095,40 @@ git-stacked-rebase ${gitStackedRebaseVersionStr}
10651095
!!second && ["--view-todo", "-v", "--view-only", "--view-todo-only"].includes(second);
10661096
const isApply: boolean = !!second && ["--apply", "-a"].includes(second);
10671097
const isPush: boolean = !!second && ["--push", "-p"].includes(second);
1098+
const isBranchSequencer: boolean = !!second && ["--branch-sequencer", "--bs", "-s"].includes(second);
10681099

1069-
if (isViewTodoOnly || isApply || isPush) {
1100+
if (isViewTodoOnly || isApply || isPush || isBranchSequencer) {
10701101
eatNextArg();
10711102
}
10721103

10731104
let isForcePush: boolean = false;
1074-
if (isPush && peakNextArg()) {
1075-
const fourth = eatNextArg() || "";
1105+
let branchSequencerExec: string | false = false;
1106+
1107+
if (peakNextArg() && (isPush || isBranchSequencer)) {
1108+
const third = eatNextArg() || "";
10761109

1077-
isForcePush = ["--force", "-f"].includes(fourth);
1110+
if (isPush) {
1111+
isForcePush = ["--force", "-f"].includes(third);
1112+
} else if (isBranchSequencer) {
1113+
/**
1114+
* TODO separate --exec & --something
1115+
* 1. for execing only the next arg (must be quoted), and
1116+
* 2. for stopping arg parsing & execing everything afterwards (no quoting)
1117+
*
1118+
* TODO also allow selecting if want to exec before, after (or both) each branch
1119+
*
1120+
*/
1121+
const execNames = ["--exec", "-x"];
1122+
if (execNames.includes(third) && peakNextArg()) {
1123+
const fourth = eatNextArg();
1124+
branchSequencerExec = fourth ? fourth : false;
1125+
} else {
1126+
return fail(`\n--branch-sequencer can only (for now) be followed by ${execNames.join("|")}\n\n`);
1127+
}
1128+
}
10781129

1079-
if (!isForcePush) {
1080-
return fail(`\nunrecognized 4th option (after --push) (got "${fourth}")\n\n`);
1130+
if (!isForcePush && !branchSequencerExec) {
1131+
return fail(`\nunrecognized 3th option (got "${third}")\n\n`);
10811132
}
10821133
}
10831134

@@ -1096,6 +1147,8 @@ git-stacked-rebase ${gitStackedRebaseVersionStr}
10961147
apply: isApply,
10971148
push: isPush,
10981149
forcePush: isForcePush,
1150+
branchSequencer: isBranchSequencer,
1151+
branchSequencerExec,
10991152
};
11001153

11011154
// await

0 commit comments

Comments
 (0)