Skip to content

Commit 8995d2c

Browse files
committed
refactor: extract rewritten-list reading logic into apply.ts
...because completely related to the logic of "doesNeedToApply", "markThatNeedsToApply", "markThatApplied", etc. thought about extracting the above utils into a separate, "rewrittenList.ts" file, but it doesn't make sense to separate the utils out from "apply" itself, because if we have the "rewrittenList.ts" file, then it'd sound reasonable to move the "reduce-path"'s "combineRewrittenLists" there as well, but it doesnt, because the apply logic is not related to the parsing/combinning logic. Signed-off-by: Kipras Melnikovas <[email protected]>
1 parent b31d48d commit 8995d2c

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

apply.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import { noop } from "./util/noop";
88

99
import { filenames } from "./filenames";
1010
import { configKeys } from "./configKeys";
11+
// eslint-disable-next-line import/no-cycle
1112
import {
1213
BranchSequencerBase, //
1314
branchSequencer,
1415
ActionInsideEachCheckedOutBranch,
1516
CallbackAfterDone,
1617
BranchSequencerArgsBase,
1718
} from "./branchSequencer";
19+
import { combineRewrittenLists } from "./reducePath";
1820

1921
export const apply: BranchSequencerBase = (args) =>
2022
branchSequencer({
@@ -156,6 +158,46 @@ const doesNeedToApply = (pathToStackedRebaseDirInsideDotGit: string): boolean =>
156158
return needsToApplyPart2;
157159
};
158160

161+
export function readRewrittenListNotAppliedOrAppliedOrError(
162+
repoPath: string
163+
): {
164+
pathOfRewrittenList: string;
165+
pathOfRewrittenListApplied: string;
166+
rewrittenListRaw: string;
167+
/**
168+
* you probably want these:
169+
*/
170+
combinedRewrittenList: string;
171+
combinedRewrittenListLines: string[];
172+
} {
173+
const pathOfRewrittenList: string = path.join(repoPath, "stacked-rebase", filenames.rewrittenList);
174+
const pathOfRewrittenListApplied: string = path.join(repoPath, "stacked-rebase", filenames.applied);
175+
176+
/**
177+
* not combined yet
178+
*/
179+
let rewrittenListRaw: string;
180+
if (fs.existsSync(pathOfRewrittenList)) {
181+
rewrittenListRaw = fs.readFileSync(pathOfRewrittenList, { encoding: "utf-8" });
182+
} else if (fs.existsSync(pathOfRewrittenListApplied)) {
183+
rewrittenListRaw = fs.readFileSync(pathOfRewrittenListApplied, { encoding: "utf-8" });
184+
} else {
185+
throw new Error(
186+
`rewritten-list not found neither in ${pathOfRewrittenList}, nor in ${pathOfRewrittenListApplied}`
187+
);
188+
}
189+
190+
const { combinedRewrittenList } = combineRewrittenLists(rewrittenListRaw);
191+
192+
return {
193+
pathOfRewrittenList,
194+
pathOfRewrittenListApplied,
195+
rewrittenListRaw,
196+
combinedRewrittenList,
197+
combinedRewrittenListLines: combinedRewrittenList.split("\n").filter((line) => !!line),
198+
};
199+
}
200+
159201
export async function applyIfNeedsToApply({
160202
repo,
161203
pathToStackedRebaseTodoFile,

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/* eslint-disable indent */
22

3-
import fs from "fs";
4-
import path from "path";
53
import assert from "assert";
64

75
import Git from "nodegit";
86
import { array } from "nice-comment";
97

108
import { filenames } from "../filenames";
11-
import { combineRewrittenLists } from "../reduce-path";
9+
// eslint-disable-next-line import/no-cycle
10+
import { readRewrittenListNotAppliedOrAppliedOrError } from "../apply";
1211

1312
import { parseTodoOfStackedRebase } from "./parseTodoOfStackedRebase";
1413
import { GoodCommand, stackedRebaseCommands } from "./validator";
@@ -21,29 +20,14 @@ export function parseNewGoodCommands(
2120

2221
logGoodCmds(oldGoodCommands);
2322

24-
const pathOfRewrittenList: string = path.join(repo.path(), "stacked-rebase", filenames.rewrittenList);
25-
const pathOfRewrittenListApplied: string = path.join(repo.path(), "stacked-rebase", filenames.applied);
26-
let rewrittenList: string;
27-
if (fs.existsSync(pathOfRewrittenList)) {
28-
rewrittenList = fs.readFileSync(pathOfRewrittenList, { encoding: "utf-8" });
29-
} else if (fs.existsSync(pathOfRewrittenListApplied)) {
30-
rewrittenList = fs.readFileSync(pathOfRewrittenListApplied, { encoding: "utf-8" });
31-
} else {
32-
throw new Error(
33-
`rewritten-list not found neither in ${pathOfRewrittenList}, nor in ${pathOfRewrittenListApplied}`
34-
);
35-
}
36-
const { combinedRewrittenList } = combineRewrittenLists(rewrittenList);
37-
const rewrittenListLines: string[] = combinedRewrittenList.split("\n").filter((line) => !!line);
38-
39-
console.log({ rewrittenListLines });
23+
const { combinedRewrittenListLines } = readRewrittenListNotAppliedOrAppliedOrError(repo.path());
4024

4125
const newCommits: { newSHA: string; oldSHAs: string[] }[] = [];
4226

4327
type OldCommit = { oldSHA: string; newSHA: string; changed: boolean };
4428
const oldCommits: OldCommit[] = [];
4529

46-
rewrittenListLines.map((line) => {
30+
combinedRewrittenListLines.map((line) => {
4731
const fromToSHA = line.split(" ");
4832
assert(
4933
fromToSHA.length === 2,

0 commit comments

Comments
 (0)