Skip to content

Commit 6bc7253

Browse files
committed
forcePush: on SIGINT/SIGTERM, switch back to latest branch
the unhandled promise rejection warning is annoying, but don't know easy way to get rid of, so won't bother for now.
1 parent e1f1b19 commit 6bc7253

File tree

1 file changed

+52
-29
lines changed

1 file changed

+52
-29
lines changed

branchSequencer.ts

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "fs";
22
import assert from "assert";
3+
import os from "os";
34

45
import Git from "nodegit";
56

@@ -8,7 +9,6 @@ import { getWantedCommitsWithBranchBoundariesOurCustomImpl } from "./git-stacked
89
import { createExecSyncInRepo } from "./util/execSyncInRepo";
910
import { Termination } from "./util/error";
1011
import { assertNever } from "./util/assertNever";
11-
import { sequentialResolve } from "./util/sequentialResolve";
1212

1313
import { parseNewGoodCommands } from "./parse-todo-of-stacked-rebase/parseNewGoodCommands";
1414
import { GoodCommand, GoodCommandStacked } from "./parse-todo-of-stacked-rebase/validator";
@@ -307,19 +307,44 @@ export const branchSequencer: BranchSequencer = async ({
307307
branchesAndCommits.reverse();
308308
}
309309

310-
return checkout(branchesAndCommits);
310+
const switchBackToLatestBranch = (msgCb?: (latestBranch: string) => string) => {
311+
const latestBranch: string = currentBranch.shorthand();
311312

312-
async function checkout(boundaries: SimpleBranchAndCommit[]): Promise<void> {
313-
if (!boundaries.length) {
314-
/**
315-
* done.
316-
*
317-
* now just checkout to the latest branch
318-
*/
313+
if (msgCb && msgCb instanceof Function) {
314+
process.stdout.write(msgCb(latestBranch));
315+
}
316+
317+
execSyncInRepo(`${gitCmd} checkout ${latestBranch}`);
318+
};
319+
320+
/**
321+
* if aborted, switch back to latest branch.
322+
*/
323+
function handleSigint() {
324+
switchBackToLatestBranch((latestBranch) => `\ncaught SIGINT, switching back to latest branch: ${latestBranch}\n`);
325+
process.exit(os.constants.signals.SIGINT);
326+
}
327+
function handleSigterm() {
328+
switchBackToLatestBranch((latestBranch) => `\ncaught SIGTERM, switching back to latest branch: ${latestBranch}\n`);
329+
process.exit(os.constants.signals.SIGTERM);
330+
}
319331

320-
// await repo.checkoutBranch(latestBoundary.branchEndFullName);
321-
execSyncInRepo(`${gitCmd} checkout ${currentBranch.shorthand()}`);
332+
/** add listeners */
333+
process.on("SIGINT", handleSigint);
334+
process.on("SIGTERM", handleSigterm);
322335

336+
return checkout(branchesAndCommits).catch(e => {
337+
console.log("caught err, prolly sigint/sigterm?", e)
338+
}).finally(() => {
339+
/** remove listeners */
340+
process.removeListener("SIGINT", handleSigint);
341+
process.removeListener("SIGTERM", handleSigterm);
342+
});
343+
344+
async function checkout(boundaries: SimpleBranchAndCommit[]): Promise<void> {
345+
if (!boundaries.length) {
346+
/** done */
347+
switchBackToLatestBranch();
323348
return;
324349
}
325350

@@ -344,24 +369,22 @@ export const branchSequencer: BranchSequencer = async ({
344369
? boundaries.length === originalBoundariesLength
345370
: boundaries.length === 1;
346371

347-
await sequentialResolve(
348-
targetBranch.map((x) => async () => {
349-
/**
350-
* https://libgit2.org/libgit2/#HEAD/group/checkout/git_checkout_head
351-
*/
352-
// await Git.Checkout.tree(repo, targetBranch as any); // TODO TS FIXME
353-
execSyncInRepo(`${gitCmd} checkout ${x}`); // f this
354-
355-
await actionInsideEachCheckedOutBranch({
356-
repo, //
357-
gitCmd,
358-
targetBranch: x,
359-
targetCommitSHA,
360-
isLatestBranch,
361-
execSyncInRepo,
362-
});
363-
})
364-
);
372+
for (const target of targetBranch) {
373+
/**
374+
* https://libgit2.org/libgit2/#HEAD/group/checkout/git_checkout_head
375+
*/
376+
// await Git.Checkout.tree(repo, targetBranch as any); // TODO TS FIXME
377+
execSyncInRepo(`${gitCmd} checkout ${target}`); // f this
378+
379+
await actionInsideEachCheckedOutBranch({
380+
repo, //
381+
gitCmd,
382+
targetBranch: target,
383+
targetCommitSHA,
384+
isLatestBranch,
385+
execSyncInRepo,
386+
});
387+
}
365388

366389
return goNext();
367390
}

0 commit comments

Comments
 (0)