11import fs from "fs" ;
22import assert from "assert" ;
3+ import os from "os" ;
34
45import Git from "nodegit" ;
56
@@ -8,7 +9,6 @@ import { getWantedCommitsWithBranchBoundariesOurCustomImpl } from "./git-stacked
89import { createExecSyncInRepo } from "./util/execSyncInRepo" ;
910import { Termination } from "./util/error" ;
1011import { assertNever } from "./util/assertNever" ;
11- import { sequentialResolve } from "./util/sequentialResolve" ;
1212
1313import { parseNewGoodCommands } from "./parse-todo-of-stacked-rebase/parseNewGoodCommands" ;
1414import { GoodCommand , GoodCommandStacked } from "./parse-todo-of-stacked-rebase/validator" ;
@@ -307,19 +307,42 @@ 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+ }
331+
332+ /** add listeners */
333+ process . on ( "SIGINT" , handleSigint ) ;
334+ process . on ( "SIGTERM" , handleSigterm ) ;
319335
320- // await repo.checkoutBranch(latestBoundary.branchEndFullName);
321- execSyncInRepo ( `${ gitCmd } checkout ${ currentBranch . shorthand ( ) } ` ) ;
336+ return checkout ( branchesAndCommits ) . finally ( ( ) => {
337+ /** remove listeners */
338+ process . removeListener ( "SIGINT" , handleSigint ) ;
339+ process . removeListener ( "SIGTERM" , handleSigterm ) ;
340+ } ) ;
322341
342+ async function checkout ( boundaries : SimpleBranchAndCommit [ ] ) : Promise < void > {
343+ if ( ! boundaries . length ) {
344+ /** done */
345+ switchBackToLatestBranch ( ) ;
323346 return ;
324347 }
325348
@@ -344,24 +367,22 @@ export const branchSequencer: BranchSequencer = async ({
344367 ? boundaries . length === originalBoundariesLength
345368 : boundaries . length === 1 ;
346369
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- ) ;
370+ for ( const target of targetBranch ) {
371+ /**
372+ * https://libgit2.org/libgit2/#HEAD/group/checkout/git_checkout_head
373+ */
374+ // await Git.Checkout.tree(repo, targetBranch as any); // TODO TS FIXME
375+ execSyncInRepo ( `${ gitCmd } checkout ${ target } ` ) ; // f this
376+
377+ await actionInsideEachCheckedOutBranch ( {
378+ repo, //
379+ gitCmd,
380+ targetBranch : target ,
381+ targetCommitSHA,
382+ isLatestBranch,
383+ execSyncInRepo,
384+ } ) ;
385+ }
365386
366387 return goNext ( ) ;
367388 }
0 commit comments