Skip to content

Commit

Permalink
move! 42946d1 allow providing custom behavior when asking questions…
Browse files Browse the repository at this point in the history
… from user

Signed-off-by: Kipras Melnikovas <[email protected]>
  • Loading branch information
kiprasmel committed Apr 3, 2023
1 parent a39a9ec commit c34f56b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 7 additions & 1 deletion git-stacked-rebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { apply, applyIfNeedsToApply, markThatNeedsToApply } from "./apply";
import { forcePush } from "./forcePush";
import { BehaviorOfGetBranchBoundaries, branchSequencer } from "./branchSequencer";
import { autosquash } from "./autosquash";
import { editor__internal, EitherEditor } from "./internal";
import { askQuestion__internal, editor__internal, EitherEditor } from "./internal";

import { createExecSyncInRepo } from "./util/execSyncInRepo";
import { noop } from "./util/noop";
Expand All @@ -37,6 +37,7 @@ import { Termination } from "./util/error";
import { assertNever } from "./util/assertNever";
import { Single, Tuple } from "./util/tuple";
import { isDirEmptySync } from "./util/fs";
import { AskQuestion, question } from "./util/createQuestion";
import {
getParseTargetsCtxFromLine,
GoodCommand,
Expand Down Expand Up @@ -86,6 +87,8 @@ export async function gitStackedRebase(

const currentBranch: Git.Reference = await repo.getCurrentBranch();

const askQuestion: AskQuestion = askQuestion__internal in options ? options[askQuestion__internal]! : question;

if (fs.existsSync(path.join(pathToStackedRebaseDirInsideDotGit, filenames.willNeedToApply))) {
markThatNeedsToApply(pathToStackedRebaseDirInsideDotGit);
}
Expand Down Expand Up @@ -129,6 +132,7 @@ export async function gitStackedRebase(
config,
initialBranch,
currentBranch,
askQuestion,
});

return;
Expand All @@ -154,6 +158,7 @@ export async function gitStackedRebase(
config,
initialBranch,
currentBranch,
askQuestion,
});

if (options.push) {
Expand Down Expand Up @@ -703,6 +708,7 @@ mv -f "${preparedRegularRebaseTodoFile}" "${pathToRegularRebaseTodoFile}"
config,
initialBranch,
currentBranch,
askQuestion,
});
}

Expand Down
5 changes: 5 additions & 0 deletions internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

import Git from "nodegit";

import { AskQuestion } from "./util/createQuestion";

export const editor__internal = Symbol("editor__internal");
export const getGitConfig__internal = Symbol("getGitConfig__internal");

export const noEditor = {
[editor__internal]: () => void 0,
};

export const askQuestion__internal = Symbol("askQuestion__internal");

/**
* meant to NOT be exported to the end user of the library
*/
export type InternalOnlyOptions = {
[editor__internal]?: EitherEditor;
[getGitConfig__internal]?: GetGitConfig;
[askQuestion__internal]?: AskQuestion;
};

export type EitherEditor = string | ((ctx: { filePath: string }) => void | Promise<void>);
Expand Down
13 changes: 11 additions & 2 deletions util/createQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,14 @@ export const createQuestion =
})
);

export const question = (q: string, cb: (ans: string) => string, { prefix = "\n" } = {}): Promise<string> =>
createQuestion()(prefix + q).then(cb);
export type AskQuestion = typeof question;

export const question = (
q: string, //
cb: (ans: string) => string = (ans) => ans,
{ prefix = "\n" } = {}
): string | Promise<string> => createQuestion()(prefix + q).then(cb);

export const Questions = {
need_to_apply_before_continuing: "need to --apply before continuing. proceed? [Y/n/(a)lways] ", //
} as const;

0 comments on commit c34f56b

Please sign in to comment.