Skip to content

Commit 59aa2b9

Browse files
committed
feat: allow re-ordering latest branch w/o having to create a new one
Signed-off-by: Kipras Melnikovas <[email protected]>
1 parent 5fac74b commit 59aa2b9

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

git-stacked-rebase.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ export const gitStackedRebase = async (
496496

497497
if (isThereANewLatestBranch) {
498498
let newLatestBranchCmdIndex: number | null = null;
499+
let userOnlyReorderedWithoutCreatingNew: boolean = false;
499500
for (let i = goodCommands.length - 1; i >= 0; i--) {
500501
const cmd = goodCommands[i];
501502
if (cmd.commandName === "branch-end-new") {
@@ -504,19 +505,30 @@ export const gitStackedRebase = async (
504505
}
505506
}
506507
if (newLatestBranchCmdIndex === null || newLatestBranchCmdIndex <= oldLatestBranchCmdIndex) {
507-
// TODO validator
508-
const when =
509-
newLatestBranchCmdIndex === null
510-
? "at all"
511-
: newLatestBranchCmdIndex <= oldLatestBranchCmdIndex
512-
? "after the branch-end-latest command"
513-
: ""; // assertNever(newLatestBranchCmdIndex);
514-
515-
throw new Termination(
516-
"\n" +
517-
`apparently a new latest branch was attempted (by adding commands _after_ the "branch-end-last")` +
518-
`\nbut there was no "branch-end-new" command (${when})`
519-
);
508+
/**
509+
* check if wanted to re-order w/o creating a new branch
510+
*/
511+
const hasExistingBranchAsLatest: boolean =
512+
goodCommands[goodCommands.length - 1].commandName === "branch-end";
513+
514+
if (newLatestBranchCmdIndex === null && hasExistingBranchAsLatest) {
515+
newLatestBranchCmdIndex = goodCommands.length - 1;
516+
userOnlyReorderedWithoutCreatingNew = true;
517+
} else {
518+
// TODO validator
519+
const when =
520+
newLatestBranchCmdIndex === null
521+
? "at all"
522+
: newLatestBranchCmdIndex <= oldLatestBranchCmdIndex
523+
? "after the branch-end-latest command"
524+
: ""; // assertNever(newLatestBranchCmdIndex);
525+
526+
throw new Termination(
527+
"\n" +
528+
`apparently a new latest branch was attempted (by adding commands _after_ the "branch-end-last")` +
529+
`\nbut there was no "branch-end-new" command (${when})`
530+
);
531+
}
520532
}
521533

522534
/**
@@ -575,10 +587,12 @@ export const gitStackedRebase = async (
575587
const oldLatestBranchCmd: GoodCommandStacked = goodCommands[oldLatestBranchCmdIndex] as GoodCommandStacked; // TODO TS
576588
const newLatestBranchCmd: GoodCommandStacked = goodCommands[newLatestBranchCmdIndex] as GoodCommandStacked; // TODO TS
577589

578-
/**
579-
* create the new "latest branch"
580-
*/
581-
await createBranchForCommand(newLatestBranchCmd as any); // TODO TS
590+
if (!userOnlyReorderedWithoutCreatingNew) {
591+
/**
592+
* create the new "latest branch"
593+
*/
594+
await createBranchForCommand(newLatestBranchCmd as any); // TODO TS
595+
}
582596

583597
/**
584598
* move the old "latest branch" earlier to it's target
@@ -599,7 +613,11 @@ export const gitStackedRebase = async (
599613
const linesOfEditedRebaseTodo: string[] = editedRebaseTodo.split("\n");
600614

601615
replaceCommandInText(oldLatestBranchCmd, ["branch-end-last"], "branch-end");
602-
replaceCommandInText(newLatestBranchCmd, ["branch-end-new", "ben"], "branch-end-last");
616+
replaceCommandInText(
617+
newLatestBranchCmd, //
618+
userOnlyReorderedWithoutCreatingNew ? ["branch-end", "be"] : ["branch-end-new", "ben"],
619+
"branch-end-last"
620+
);
603621

604622
// eslint-disable-next-line no-inner-declarations
605623
function replaceCommandInText(

0 commit comments

Comments
 (0)