Skip to content

Commit c3b3ca5

Browse files
committed
replace the command names in the git-rebase-todo file as well (keep in sync / source of truth)
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent ec49206 commit c3b3ca5

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

git-stacked-rebase.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
GoodCommandStacked, //
2727
namesOfRebaseCommandsThatMakeRebaseExitToPause,
2828
StackedRebaseCommand,
29+
StackedRebaseEitherCommandOrAlias,
2930
} from "./parse-todo-of-stacked-rebase/validator";
3031

3132
// console.log = () => {};
@@ -595,8 +596,8 @@ export const gitStackedRebase = async (
595596
* update the commands
596597
* TODO update in the actual `git-rebase-todo` file
597598
*/
598-
// swapKeys(goodCommands[oldLatestBranchCmdIndex], goodCommands[newLatestBranchCmdIndex], "commandName");
599-
// swapKeys(goodCommands[oldLatestBranchCmdIndex], goodCommands[newLatestBranchCmdIndex], "commandOrAliasName"); // eslint-disable-line prettier/prettier
599+
// swapKeys(oldLatestBranchCmd, newLatestBranchCmd, "commandName");
600+
// swapKeys(oldLatestBranchCmd, newLatestBranchCmd, "commandOrAliasName"); // eslint-disable-line prettier/prettier
600601

601602
// // eslint-disable-next-line no-inner-declarations
602603
// function swapKeys<T>(A: T, B: T, key: keyof T): void {
@@ -607,11 +608,47 @@ export const gitStackedRebase = async (
607608
* need to change to "branch-end", instead of "branch-end-new",
608609
* since obviously the branch already exists
609610
*/
610-
goodCommands[oldLatestBranchCmdIndex].commandName = "branch-end";
611-
goodCommands[oldLatestBranchCmdIndex].commandOrAliasName = "branch-end";
611+
oldLatestBranchCmd.commandName = "branch-end";
612+
oldLatestBranchCmd.commandOrAliasName = "branch-end";
612613

613-
goodCommands[newLatestBranchCmdIndex].commandName = "branch-end-last";
614-
goodCommands[newLatestBranchCmdIndex].commandOrAliasName = "branch-end-last";
614+
newLatestBranchCmd.commandName = "branch-end-last";
615+
newLatestBranchCmd.commandOrAliasName = "branch-end-last";
616+
617+
/**
618+
* TODO FIXME don't do this so hackishly lmao
619+
*/
620+
const editedRebaseTodo: string = fs.readFileSync(pathToStackedRebaseTodoFile, { encoding: "utf-8" });
621+
const linesOfEditedRebaseTodo: string[] = editedRebaseTodo.split("\n");
622+
623+
/**
624+
* replace the "branch-end-last" with "branch-end"
625+
*/
626+
// const oldLineBefore = linesOfEditedRebaseTodo[oldLatestBranchCmd.lineNumber].split(" ");
627+
// assert.equal(oldLineBefore[0], "branch-end-last");
628+
// oldLineBefore.splice(0, 1, "branch-end"); // remove 1st word ("branch-end-last") and add "branch-end"
629+
// linesOfEditedRebaseTodo[oldLatestBranchCmd.lineNumber] = oldLineBefore.join(" ");
630+
631+
// const newLineBefore = linesOfEditedRebaseTodo[newLatestBranchCmd.lineNumber].split(" ");
632+
// assert.equal(newLineBefore[0], "branch-end-new");
633+
// newLineBefore.splice(0, 1, "branch-end-last"); // remove 1st word ("branch-end") and add "branch-end-last"
634+
// linesOfEditedRebaseTodo[newLatestBranchCmd.lineNumber] = newLineBefore.join(" ");
635+
636+
replaceCommandInText(oldLatestBranchCmd, "branch-end-last", "branch-end");
637+
replaceCommandInText(newLatestBranchCmd, "branch-end-new", "branch-end-last");
638+
639+
// eslint-disable-next-line no-inner-declarations
640+
function replaceCommandInText(
641+
cmd: GoodCommandStacked, //
642+
expectedOldName: StackedRebaseEitherCommandOrAlias,
643+
newName: StackedRebaseCommand
644+
): void {
645+
const words = linesOfEditedRebaseTodo[cmd.lineNumber].split(" ");
646+
assert.equal(words[0], expectedOldName);
647+
words[0] = newName;
648+
linesOfEditedRebaseTodo[oldLatestBranchCmd.lineNumber] = words.join(" ");
649+
}
650+
651+
fs.writeFileSync(pathToStackedRebaseTodoFile, linesOfEditedRebaseTodo.join("\n"), { encoding: "utf-8" });
615652

616653
/**
617654
* it's fine if the new "latest branch" does not have

parse-todo-of-stacked-rebase/validator.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export const regularRebaseCommands = {
144144
} as const;
145145

146146
export type RegularRebaseCommand = keyof typeof regularRebaseCommands;
147+
export type RegularRebaseEitherCommandOrAlias = RegularRebaseCommand | RegularRebaseCommandAlias;
147148

148149
/**
149150
* TODO: assert each value is `RegularRebaseCommand`,
@@ -227,8 +228,12 @@ const stackedRebaseCommandAliases = {
227228
ben: "branch-end-new",
228229
} as const;
229230

230-
type StackedRebaseCommandAlias = keyof typeof stackedRebaseCommandAliases;
231+
export type StackedRebaseCommandAlias = keyof typeof stackedRebaseCommandAliases;
232+
export type StackedRebaseEitherCommandOrAlias = StackedRebaseCommand | StackedRebaseCommandAlias;
231233

234+
/**
235+
* combined
236+
*/
232237
export type EitherRebaseCommand = RegularRebaseCommand | StackedRebaseCommand;
233238
export type EitherRebaseCommandAlias = RegularRebaseCommandAlias | StackedRebaseCommandAlias;
234239

0 commit comments

Comments
 (0)