Skip to content

Commit 0331bb5

Browse files
committed
split rewritten-list.applied into 2 separate, refactor
Signed-off-by: Kipras Melnikovas <[email protected]>
1 parent 87e3aab commit 0331bb5

File tree

3 files changed

+60
-54
lines changed

3 files changed

+60
-54
lines changed

apply.ts

+57-52
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const apply: BranchSequencerBase = (args) =>
2323
// callbackAfterDone: defaultApplyCallback,
2424
delayMsBetweenCheckouts: 0,
2525
}).then(
26-
(ret) => (unmarkThatNeedsToApply(args.pathToStackedRebaseDirInsideDotGit), ret) //
26+
(ret) => (markThatApplied(args.pathToStackedRebaseDirInsideDotGit), ret) //
2727
);
2828

2929
const defaultApplyAction: ActionInsideEachCheckedOutBranch = async ({
@@ -82,44 +82,72 @@ const defaultApplyCallback__disabled: CallbackAfterDone = ({
8282
};
8383
noop(defaultApplyCallback__disabled);
8484

85-
export type ReturnOfApplyIfNeedsToApply =
85+
export type ReturnOfApplyIfNeedsToApply = {
86+
markThatNeedsToApply: () => void;
87+
} & (
8688
| {
8789
neededToApply: false;
8890
userAllowedToApplyAndWeApplied?: never;
89-
markThatNeedsToApply: () => void;
9091
}
9192
| {
9293
neededToApply: true;
9394
userAllowedToApplyAndWeApplied: false;
9495
// markThatNeedsToApply?: never; // TODO TS infer auto - force code owner to exit
95-
markThatNeedsToApply: () => void;
9696
}
9797
| {
9898
neededToApply: true;
9999
userAllowedToApplyAndWeApplied: true;
100-
markThatNeedsToApply: () => void;
101-
};
100+
}
101+
);
102102

103-
const filenameOfNeedsToApply = "needs-to-apply" as const;
103+
const getPaths = (
104+
pathToStackedRebaseDirInsideDotGit: string //
105+
) =>
106+
({
107+
rewrittenListPath: path.join(pathToStackedRebaseDirInsideDotGit, filenames.rewrittenList),
108+
needsToApplyPath: path.join(pathToStackedRebaseDirInsideDotGit, filenames.needsToApply),
109+
appliedPath: path.join(pathToStackedRebaseDirInsideDotGit, filenames.applied),
110+
} as const);
111+
112+
const markThatNeedsToApply = (
113+
pathToStackedRebaseDirInsideDotGit: string //
114+
): void =>
115+
[getPaths(pathToStackedRebaseDirInsideDotGit)].map(
116+
({ rewrittenListPath, needsToApplyPath, appliedPath }) => (
117+
fs.existsSync(rewrittenListPath)
118+
? fs.copyFileSync(rewrittenListPath, needsToApplyPath)
119+
: fs.writeFileSync(needsToApplyPath, ""),
120+
fs.existsSync(appliedPath) && fs.unlinkSync(appliedPath),
121+
void 0
122+
)
123+
)[0];
124+
125+
export const markThatApplied = (pathToStackedRebaseDirInsideDotGit: string): void =>
126+
[getPaths(pathToStackedRebaseDirInsideDotGit)].map(
127+
({ rewrittenListPath, needsToApplyPath, appliedPath }) => (
128+
fs.existsSync(needsToApplyPath) && fs.unlinkSync(needsToApplyPath), //
129+
fs.copyFileSync(rewrittenListPath, appliedPath),
130+
void 0
131+
)
132+
)[0];
133+
134+
const doesNeedToApply = (pathToStackedRebaseDirInsideDotGit: string): boolean => {
135+
const { rewrittenListPath, needsToApplyPath, appliedPath } = getPaths(pathToStackedRebaseDirInsideDotGit);
136+
137+
const needsToApplyPart1: boolean = fs.existsSync(needsToApplyPath);
138+
if (needsToApplyPart1) {
139+
return true;
140+
}
104141

105-
const getPathOfFilenameOfNeedsToApply = (pathToStackedRebaseDirInsideDotGit: string): string =>
106-
path.join(pathToStackedRebaseDirInsideDotGit, filenameOfNeedsToApply);
142+
const needsToApplyPart2: boolean = fs.existsSync(appliedPath)
143+
? /**
144+
* check if has been applied, but that apply is outdated
145+
*/
146+
fs.readFileSync(appliedPath) !== fs.readFileSync(rewrittenListPath)
147+
: false;
107148

108-
/**
109-
* TODO rename "markThatApplied" because we are _not_ reversing the action
110-
* (undo-ing the file rename),
111-
* we are invoking a new action of removing the files.
112-
*/
113-
export const unmarkThatNeedsToApply = (
114-
pathToStackedRebaseDirInsideDotGit: string,
115-
mark = getPathOfFilenameOfNeedsToApply(pathToStackedRebaseDirInsideDotGit),
116-
rewrittenListFile: string = path.join(pathToStackedRebaseDirInsideDotGit, filenames.rewrittenList),
117-
rewrittenListAppliedFile: string = path.join(pathToStackedRebaseDirInsideDotGit, filenames.rewrittenListApplied)
118-
): void => (
119-
fs.existsSync(mark) && fs.unlinkSync(mark),
120-
fs.existsSync(rewrittenListFile) && fs.renameSync(rewrittenListFile, rewrittenListAppliedFile),
121-
void 0
122-
);
149+
return needsToApplyPart2;
150+
};
123151

124152
export async function applyIfNeedsToApply({
125153
repo,
@@ -132,34 +160,13 @@ export async function applyIfNeedsToApply({
132160
autoApplyIfNeeded: boolean; //
133161
config: Git.Config;
134162
}): Promise<ReturnOfApplyIfNeedsToApply> {
135-
/**
136-
* currently we're not saving the branch names
137-
* & where they point to etc.,
138-
* so doing rebase after rebase without --apply
139-
* will break the workflow after the 1st one.
140-
*
141-
* thus, until we have a more sophisticated solution,
142-
* automatically --apply'ing (when needed) should do just fine.
143-
*
144-
*/
145-
const pathToFileIndicatingThatNeedsToApply = getPathOfFilenameOfNeedsToApply(pathToStackedRebaseDirInsideDotGit);
146-
const needsToApply: boolean = fs.existsSync(pathToFileIndicatingThatNeedsToApply);
147-
148-
const pathToAppliedRewrittenListFile = path.join(
149-
pathToStackedRebaseDirInsideDotGit,
150-
filenames.rewrittenListApplied
151-
);
152-
153-
const markThatNeedsToApply = (): void => (
154-
fs.writeFileSync(pathToFileIndicatingThatNeedsToApply, ""),
155-
fs.existsSync(pathToAppliedRewrittenListFile) && fs.unlinkSync(pathToAppliedRewrittenListFile),
156-
void 0
157-
);
163+
const needsToApply: boolean = doesNeedToApply(pathToStackedRebaseDirInsideDotGit);
164+
const _markThatNeedsToApply = (): void => markThatNeedsToApply(pathToStackedRebaseDirInsideDotGit);
158165

159166
if (!needsToApply) {
160167
return {
161168
neededToApply: false,
162-
markThatNeedsToApply,
169+
markThatNeedsToApply: _markThatNeedsToApply,
163170
};
164171
}
165172

@@ -181,7 +188,7 @@ export async function applyIfNeedsToApply({
181188
return {
182189
neededToApply: true,
183190
userAllowedToApplyAndWeApplied: false,
184-
markThatNeedsToApply,
191+
markThatNeedsToApply: _markThatNeedsToApply,
185192
};
186193
}
187194

@@ -196,13 +203,11 @@ export async function applyIfNeedsToApply({
196203
pathToStackedRebaseDirInsideDotGit, //
197204
...rest,
198205
});
199-
200-
unmarkThatNeedsToApply(pathToStackedRebaseDirInsideDotGit);
201206
}
202207

203208
return {
204209
neededToApply: true,
205210
userAllowedToApplyAndWeApplied: true, //
206-
markThatNeedsToApply,
211+
markThatNeedsToApply: _markThatNeedsToApply,
207212
};
208213
}

branchSequencer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export type BranchSequencerArgs = BranchSequencerArgsBase & {
5454
actionInsideEachCheckedOutBranch: ActionInsideEachCheckedOutBranch;
5555
delayMsBetweenCheckouts?: number;
5656
callbackAfterDone?: CallbackAfterDone;
57-
rewrittenListFile?: typeof filenames.rewrittenList | typeof filenames.rewrittenListApplied;
57+
rewrittenListFile?: typeof filenames.rewrittenList;
5858
};
5959

6060
export type BranchSequencerBase = (args: BranchSequencerArgsBase) => Promise<void>;

filenames.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
*/
44
export const filenames = {
55
rewrittenList: "rewritten-list",
6-
rewrittenListApplied: "rewritten-list.applied",
6+
needsToApply: "needs-to-apply",
7+
applied: "applied",
78
//
89
gitRebaseTodo: "git-rebase-todo",
910
//

0 commit comments

Comments
 (0)