Skip to content

Commit f202ff4

Browse files
committed
feat: implement custom getBoundariesInclInitial for forcePush, using only existing commits 🔥🔥🔥
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent c5a4781 commit f202ff4

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

branchSequencer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export type SimpleBranchAndCommit = {
2020
branchEndFullName: string;
2121
// branchExistsYet: boolean; // TODO
2222
};
23-
export type GetBoundariesInclInitial = (ctx: GetBranchesCtx) => SimpleBranchAndCommit[];
23+
export type GetBoundariesInclInitial = (
24+
ctx: GetBranchesCtx //
25+
) => SimpleBranchAndCommit[] | Promise<SimpleBranchAndCommit[]>;
2426

2527
const defautlGetBoundariesInclInitial: GetBoundariesInclInitial = ({
2628
pathToStackedRebaseDirInsideDotGit, //
@@ -116,6 +118,9 @@ export type BranchSequencerArgsBase = {
116118
repo: Git.Repository;
117119
rootLevelCommandName: string;
118120
gitCmd: string;
121+
//
122+
initialBranch: Git.Reference;
123+
currentBranch: Git.Reference;
119124
};
120125
export type BranchSequencerArgs = BranchSequencerArgsBase & {
121126
// callbackBeforeBegin?: CallbackAfterDone; // TODO
@@ -144,7 +149,7 @@ export const branchSequencer: BranchSequencer = async ({
144149
}) => {
145150
const execSyncInRepo = createExecSyncInRepo(repo);
146151

147-
const branchesAndCommits: SimpleBranchAndCommit[] = getBoundariesInclInitial({
152+
const branchesAndCommits: SimpleBranchAndCommit[] = await getBoundariesInclInitial({
148153
pathToStackedRebaseDirInsideDotGit,
149154
pathToStackedRebaseTodoFile,
150155
repo,

forcePush.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
// import fs from "fs";
44

5+
import { getWantedCommitsWithBranchBoundariesOurCustomImpl } from "./git-stacked-rebase";
56
import {
67
branchSequencer, //
78
BranchSequencerBase,
9+
SimpleBranchAndCommit,
810
// getBackupPathOfPreviousStackedRebase,
911
} from "./branchSequencer";
1012

@@ -38,4 +40,19 @@ export const forcePush: BranchSequencerBase = (argsBase) =>
3840
execSyncInRepo(`${argsBase.gitCmd} push --force`);
3941
},
4042
delayMsBetweenCheckouts: 0,
43+
getBoundariesInclInitial: () =>
44+
getWantedCommitsWithBranchBoundariesOurCustomImpl(
45+
argsBase.repo, //
46+
argsBase.initialBranch,
47+
argsBase.currentBranch
48+
).then((boundaries) =>
49+
boundaries
50+
.filter((b) => !!b.branchEnd)
51+
.map(
52+
(boundary): SimpleBranchAndCommit => ({
53+
branchEndFullName: boundary.branchEnd!.name(), // TS ok because of the filter
54+
commitSHA: boundary.commit.sha(),
55+
})
56+
)
57+
),
4158
});

git-stacked-rebase.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,19 @@ export const gitStackedRebase = async (
289289

290290
const checkIsRegularRebaseStillInProgress = (): boolean => fs.existsSync(pathToRegularRebaseDirInsideDotGit);
291291

292+
fs.mkdirSync(pathToStackedRebaseDirInsideDotGit, { recursive: true });
293+
294+
const initialBranch: Git.Reference | void = await Git.Branch.lookup(
295+
repo, //
296+
nameOfInitialBranch,
297+
Git.Branch.BRANCH.ALL
298+
);
299+
if (!initialBranch) {
300+
throw new Error("initialBranch lookup failed");
301+
}
302+
303+
const currentBranch: Git.Reference = await repo.getCurrentBranch();
304+
292305
if (fs.existsSync(path.join(pathToStackedRebaseDirInsideDotGit, filenames.willNeedToApply))) {
293306
_markThatNeedsToApply(pathToStackedRebaseDirInsideDotGit);
294307
}
@@ -300,6 +313,8 @@ export const gitStackedRebase = async (
300313
pathToStackedRebaseDirInsideDotGit, //
301314
rootLevelCommandName: "--apply",
302315
gitCmd: options.gitCmd,
316+
initialBranch,
317+
currentBranch,
303318
});
304319
}
305320

@@ -327,6 +342,8 @@ export const gitStackedRebase = async (
327342
gitCmd: options.gitCmd,
328343
autoApplyIfNeeded: configValues.autoApplyIfNeeded,
329344
config,
345+
initialBranch,
346+
currentBranch,
330347
});
331348

332349
return;
@@ -340,6 +357,8 @@ export const gitStackedRebase = async (
340357
gitCmd: options.gitCmd,
341358
autoApplyIfNeeded: configValues.autoApplyIfNeeded,
342359
config,
360+
initialBranch,
361+
currentBranch,
343362
});
344363

345364
if (neededToApply && !userAllowedToApplyAndWeApplied) {
@@ -357,6 +376,8 @@ export const gitStackedRebase = async (
357376
pathToStackedRebaseDirInsideDotGit,
358377
rootLevelCommandName: "--push --force",
359378
gitCmd: options.gitCmd,
379+
initialBranch,
380+
currentBranch,
360381
});
361382
}
362383

@@ -371,6 +392,8 @@ export const gitStackedRebase = async (
371392
actionInsideEachCheckedOutBranch: ({ execSyncInRepo: execS }) => (execS(toExec), void 0),
372393
pathToStackedRebaseDirInsideDotGit,
373394
pathToStackedRebaseTodoFile,
395+
initialBranch,
396+
currentBranch,
374397
});
375398
} else {
376399
/**
@@ -382,15 +405,6 @@ export const gitStackedRebase = async (
382405
}
383406
}
384407

385-
fs.mkdirSync(pathToStackedRebaseDirInsideDotGit, { recursive: true });
386-
387-
const initialBranch: Git.Reference | void = await Git.Branch.lookup(
388-
repo, //
389-
nameOfInitialBranch,
390-
Git.Branch.BRANCH.ALL
391-
);
392-
const currentBranch: Git.Reference = await repo.getCurrentBranch();
393-
394408
const wasRegularRebaseInProgress: boolean = checkIsRegularRebaseStillInProgress();
395409
// const
396410

@@ -905,6 +919,8 @@ mv -f "${preparedRegularRebaseTodoFile}" "${pathToRegularRebaseTodoFile}"
905919
gitCmd: options.gitCmd,
906920
autoApplyIfNeeded: configValues.autoApplyIfNeeded,
907921
config,
922+
initialBranch,
923+
currentBranch,
908924
});
909925
}
910926
}
@@ -1082,7 +1098,7 @@ type CommitAndBranchBoundary = {
10821098
branchEnd: Git.Reference | null;
10831099
};
10841100

1085-
async function getWantedCommitsWithBranchBoundariesOurCustomImpl(
1101+
export async function getWantedCommitsWithBranchBoundariesOurCustomImpl(
10861102
repo: Git.Repository, //
10871103
/** beginningBranch */
10881104
bb: Git.Reference,

0 commit comments

Comments
 (0)