Skip to content

Commit eade3a7

Browse files
committed
feat: allow re-ordering latest branch w/o having to create a new one
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent 3542019 commit eade3a7

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
@@ -497,6 +497,7 @@ export const gitStackedRebase = async (
497497

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

523535
/**
@@ -576,10 +588,12 @@ export const gitStackedRebase = async (
576588
const oldLatestBranchCmd: GoodCommandStacked = goodCommands[oldLatestBranchCmdIndex] as GoodCommandStacked; // TODO TS
577589
const newLatestBranchCmd: GoodCommandStacked = goodCommands[newLatestBranchCmdIndex] as GoodCommandStacked; // TODO TS
578590

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

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

602616
replaceCommandInText(oldLatestBranchCmd, ["branch-end-last"], "branch-end");
603-
replaceCommandInText(newLatestBranchCmd, ["branch-end-new", "ben"], "branch-end-last");
617+
replaceCommandInText(
618+
newLatestBranchCmd, //
619+
userOnlyReorderedWithoutCreatingNew ? ["branch-end", "be"] : ["branch-end-new", "ben"],
620+
"branch-end-last"
621+
);
604622

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

0 commit comments

Comments
 (0)