Skip to content

Commit

Permalink
split rewritten-list.applied into 2 separate, refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
  • Loading branch information
kiprasmel committed Jun 7, 2022
1 parent 5b63b1a commit 26274fe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 54 deletions.
109 changes: 57 additions & 52 deletions apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const apply: BranchSequencerBase = (args) =>
// callbackAfterDone: defaultApplyCallback,
delayMsBetweenCheckouts: 0,
}).then(
(ret) => (unmarkThatNeedsToApply(args.pathToStackedRebaseDirInsideDotGit), ret) //
(ret) => (markThatApplied(args.pathToStackedRebaseDirInsideDotGit), ret) //
);

const defaultApplyAction: ActionInsideEachCheckedOutBranch = async ({
Expand Down Expand Up @@ -82,44 +82,72 @@ const defaultApplyCallback__disabled: CallbackAfterDone = ({
};
noop(defaultApplyCallback__disabled);

export type ReturnOfApplyIfNeedsToApply =
export type ReturnOfApplyIfNeedsToApply = {
markThatNeedsToApply: () => void;
} & (
| {
neededToApply: false;
userAllowedToApplyAndWeApplied?: never;
markThatNeedsToApply: () => void;
}
| {
neededToApply: true;
userAllowedToApplyAndWeApplied: false;
// markThatNeedsToApply?: never; // TODO TS infer auto - force code owner to exit
markThatNeedsToApply: () => void;
}
| {
neededToApply: true;
userAllowedToApplyAndWeApplied: true;
markThatNeedsToApply: () => void;
};
}
);

const filenameOfNeedsToApply = "needs-to-apply" as const;
const getPaths = (
pathToStackedRebaseDirInsideDotGit: string //
) =>
({
rewrittenListPath: path.join(pathToStackedRebaseDirInsideDotGit, filenames.rewrittenList),
needsToApplyPath: path.join(pathToStackedRebaseDirInsideDotGit, filenames.needsToApply),
appliedPath: path.join(pathToStackedRebaseDirInsideDotGit, filenames.applied),
} as const);

const markThatNeedsToApply = (
pathToStackedRebaseDirInsideDotGit: string //
): void =>
[getPaths(pathToStackedRebaseDirInsideDotGit)].map(
({ rewrittenListPath, needsToApplyPath, appliedPath }) => (
fs.existsSync(rewrittenListPath)
? fs.copyFileSync(rewrittenListPath, needsToApplyPath)
: fs.writeFileSync(needsToApplyPath, ""),
fs.existsSync(appliedPath) && fs.unlinkSync(appliedPath),
void 0
)
)[0];

export const markThatApplied = (pathToStackedRebaseDirInsideDotGit: string): void =>
[getPaths(pathToStackedRebaseDirInsideDotGit)].map(
({ rewrittenListPath, needsToApplyPath, appliedPath }) => (
fs.existsSync(needsToApplyPath) && fs.unlinkSync(needsToApplyPath), //
fs.copyFileSync(rewrittenListPath, appliedPath),
void 0
)
)[0];

const doesNeedToApply = (pathToStackedRebaseDirInsideDotGit: string): boolean => {
const { rewrittenListPath, needsToApplyPath, appliedPath } = getPaths(pathToStackedRebaseDirInsideDotGit);

const needsToApplyPart1: boolean = fs.existsSync(needsToApplyPath);
if (needsToApplyPart1) {
return true;
}

const getPathOfFilenameOfNeedsToApply = (pathToStackedRebaseDirInsideDotGit: string): string =>
path.join(pathToStackedRebaseDirInsideDotGit, filenameOfNeedsToApply);
const needsToApplyPart2: boolean = fs.existsSync(appliedPath)
? /**
* check if has been applied, but that apply is outdated
*/
fs.readFileSync(appliedPath) !== fs.readFileSync(rewrittenListPath)
: false;

/**
* TODO rename "markThatApplied" because we are _not_ reversing the action
* (undo-ing the file rename),
* we are invoking a new action of removing the files.
*/
export const unmarkThatNeedsToApply = (
pathToStackedRebaseDirInsideDotGit: string,
mark = getPathOfFilenameOfNeedsToApply(pathToStackedRebaseDirInsideDotGit),
rewrittenListFile: string = path.join(pathToStackedRebaseDirInsideDotGit, filenames.rewrittenList),
rewrittenListAppliedFile: string = path.join(pathToStackedRebaseDirInsideDotGit, filenames.rewrittenListApplied)
): void => (
fs.existsSync(mark) && fs.unlinkSync(mark),
fs.existsSync(rewrittenListFile) && fs.renameSync(rewrittenListFile, rewrittenListAppliedFile),
void 0
);
return needsToApplyPart2;
};

export async function applyIfNeedsToApply({
repo,
Expand All @@ -132,34 +160,13 @@ export async function applyIfNeedsToApply({
autoApplyIfNeeded: boolean; //
config: Git.Config;
}): Promise<ReturnOfApplyIfNeedsToApply> {
/**
* currently we're not saving the branch names
* & where they point to etc.,
* so doing rebase after rebase without --apply
* will break the workflow after the 1st one.
*
* thus, until we have a more sophisticated solution,
* automatically --apply'ing (when needed) should do just fine.
*
*/
const pathToFileIndicatingThatNeedsToApply = getPathOfFilenameOfNeedsToApply(pathToStackedRebaseDirInsideDotGit);
const needsToApply: boolean = fs.existsSync(pathToFileIndicatingThatNeedsToApply);

const pathToAppliedRewrittenListFile = path.join(
pathToStackedRebaseDirInsideDotGit,
filenames.rewrittenListApplied
);

const markThatNeedsToApply = (): void => (
fs.writeFileSync(pathToFileIndicatingThatNeedsToApply, ""),
fs.existsSync(pathToAppliedRewrittenListFile) && fs.unlinkSync(pathToAppliedRewrittenListFile),
void 0
);
const needsToApply: boolean = doesNeedToApply(pathToStackedRebaseDirInsideDotGit);
const _markThatNeedsToApply = (): void => markThatNeedsToApply(pathToStackedRebaseDirInsideDotGit);

if (!needsToApply) {
return {
neededToApply: false,
markThatNeedsToApply,
markThatNeedsToApply: _markThatNeedsToApply,
};
}

Expand All @@ -181,7 +188,7 @@ export async function applyIfNeedsToApply({
return {
neededToApply: true,
userAllowedToApplyAndWeApplied: false,
markThatNeedsToApply,
markThatNeedsToApply: _markThatNeedsToApply,
};
}

Expand All @@ -196,13 +203,11 @@ export async function applyIfNeedsToApply({
pathToStackedRebaseDirInsideDotGit, //
...rest,
});

unmarkThatNeedsToApply(pathToStackedRebaseDirInsideDotGit);
}

return {
neededToApply: true,
userAllowedToApplyAndWeApplied: true, //
markThatNeedsToApply,
markThatNeedsToApply: _markThatNeedsToApply,
};
}
2 changes: 1 addition & 1 deletion branchSequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type BranchSequencerArgs = BranchSequencerArgsBase & {
actionInsideEachCheckedOutBranch: ActionInsideEachCheckedOutBranch;
delayMsBetweenCheckouts?: number;
callbackAfterDone?: CallbackAfterDone;
rewrittenListFile?: typeof filenames.rewrittenList | typeof filenames.rewrittenListApplied;
rewrittenListFile?: typeof filenames.rewrittenList;
};

export type BranchSequencerBase = (args: BranchSequencerArgsBase) => Promise<void>;
Expand Down
3 changes: 2 additions & 1 deletion filenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*/
export const filenames = {
rewrittenList: "rewritten-list",
rewrittenListApplied: "rewritten-list.applied",
needsToApply: "needs-to-apply",
applied: "applied",
//
gitRebaseTodo: "git-rebase-todo",
//
Expand Down

0 comments on commit 26274fe

Please sign in to comment.