@@ -38,6 +38,7 @@ import { assertNever } from "./util/assertNever";
3838import { Single , Tuple } from "./util/tuple" ;
3939import { isDirEmptySync } from "./util/fs" ;
4040import {
41+ getParseTargetsCtxFromLine ,
4142 GoodCommand ,
4243 GoodCommandRegular ,
4344 GoodCommandStacked , //
@@ -46,6 +47,8 @@ import {
4647 RegularRebaseEitherCommandOrAlias ,
4748 StackedRebaseCommand ,
4849 StackedRebaseCommandAlias ,
50+ stackedRebaseCommands ,
51+ Targets ,
4952} from "./parse-todo-of-stacked-rebase/validator" ;
5053
5154export * from "./options" ;
@@ -514,16 +517,8 @@ export async function gitStackedRebase(
514517 if ( cmd . commandName === "branch-end-new" ) {
515518 await createBranchForCommand ( cmd as any ) ; // TODO TS
516519 } else if ( cmd . commandName === "branch-end-new-from-remote" ) {
517- const nameWithoutRefsRemotesOriginPrefix : string = cmd . targets ! [ 0 ] ;
518- const remote : string = cmd . targets ! [ 1 ] ;
519- const fullNameOfBranchWithRemote : string =
520- "refs/remotes/" + remote + "/" + nameWithoutRefsRemotesOriginPrefix ;
521-
522- branchesWhoNeedLocalCheckout . push ( {
523- nameWithoutRefsRemotesOriginPrefix,
524- remote,
525- fullNameOfBranchWithRemote,
526- } ) ;
520+ const b = parseBranchWhichNeedsLocalCheckout ( cmd . targets ! ) ; // TODO TS NARROWER TYPES
521+ branchesWhoNeedLocalCheckout . push ( b ) ;
527522 }
528523 } else {
529524 assertNever ( cmd ) ;
@@ -883,13 +878,17 @@ async function createInitialEditTodoOfGitStackedRebase(
883878 *
884879 */
885880 if ( branch . isRemote ( ) || branch . name ( ) . startsWith ( "refs/remotes/" ) ) {
886- const nameWithoutRefsRemotesOriginPrefix : string = branch
881+ const wantedLocalBranchName : string = branch
887882 . name ( )
888883 . replace ( removeRemoteRegex , "" ) ;
889884
890- const remote : string = branch . name ( ) . match ( removeRemoteRegex ) ! [ 1 ] ;
885+ const remoteName : string = branch . name ( ) . match ( removeRemoteRegex ) ! [ 1 ] ;
891886
892- return `branch-end-new-from-remote ${ nameWithoutRefsRemotesOriginPrefix } ${ remote } ` ;
887+ return encodeCmdToLine ( {
888+ wantedLocalBranchName,
889+ remoteName,
890+ fullNameOfBranchWithRemote : getFullNameOfBranchWithRemote ( { wantedLocalBranchName, remoteName } )
891+ } )
893892 } else {
894893 return `branch-end ${ branch . name ( ) } ` ;
895894 }
@@ -909,12 +908,49 @@ async function createInitialEditTodoOfGitStackedRebase(
909908 return ;
910909}
911910
912- type BranchWhoNeedsLocalCheckout = {
913- nameWithoutRefsRemotesOriginPrefix : string ; //
914- remote : string ;
911+ export type BranchWhoNeedsLocalCheckout = {
912+ wantedLocalBranchName : string ; //
913+ remoteName : string ;
915914 fullNameOfBranchWithRemote : string ;
916915} ;
917916
917+ /**
918+ * expects targets from the format of "branch-end-new-from-remote"
919+ * TODO TS NARROWER TYPES (specific targets for each type of cmd)
920+ */
921+ export function parseBranchWhichNeedsLocalCheckout ( targets : NonNullable < Targets > ) : BranchWhoNeedsLocalCheckout {
922+ const wantedLocalBranchName : string = targets [ 0 ] ;
923+ const remoteAndRemoteBranchName : string = targets [ 1 ] ;
924+ const remoteName : string = remoteAndRemoteBranchName . split ( "/" ) [ 0 ] ;
925+ const fullNameOfBranchWithRemote : string = getFullNameOfBranchWithRemote ( { fullNameOfBranchWithRemote : remoteAndRemoteBranchName } ) ;
926+
927+ return {
928+ wantedLocalBranchName,
929+ remoteName,
930+ fullNameOfBranchWithRemote,
931+ }
932+ }
933+
934+ function getFullNameOfBranchWithRemote ( b : Pick < BranchWhoNeedsLocalCheckout , "fullNameOfBranchWithRemote" > | Pick < BranchWhoNeedsLocalCheckout , "wantedLocalBranchName" | "remoteName" > ) : string {
935+ return "refs/remotes/" + (
936+ "fullNameOfBranchWithRemote" in b
937+ ? b . fullNameOfBranchWithRemote
938+ : b . remoteName + "/" + b . wantedLocalBranchName
939+ ) ;
940+ }
941+
942+ // TODO RENAME
943+ export function encodeCmdToLine ( b : BranchWhoNeedsLocalCheckout ) {
944+ return `branch-end-new-from-remote ${ b . wantedLocalBranchName } ${ b . fullNameOfBranchWithRemote } ` ;
945+ }
946+
947+ // TODO RENAME
948+ export function decodeLineToCmd ( line : string ) : BranchWhoNeedsLocalCheckout {
949+ // TODO TS NARROWER TYPES
950+ const targets : NonNullable < Targets > = stackedRebaseCommands [ "branch-end-new-from-remote" ] . parseTargets ( getParseTargetsCtxFromLine ( line ) ) !
951+ return parseBranchWhichNeedsLocalCheckout ( targets ) ;
952+ }
953+
918954function checkoutRemotePartialBranchesLocally (
919955 repo : Git . Repository , //
920956 currentBranch : Git . Reference ,
@@ -929,7 +965,7 @@ function checkoutRemotePartialBranchesLocally(
929965 const execSyncInRepo = createExecSyncInRepo ( repo ) ;
930966
931967 for ( const b of branchesWhoNeedLocalCheckout ) {
932- const cmd = `git checkout -b ${ b . nameWithoutRefsRemotesOriginPrefix } --track ${ b . fullNameOfBranchWithRemote } ` ;
968+ const cmd = `git checkout -b ${ b . wantedLocalBranchName } --track ${ b . fullNameOfBranchWithRemote } ` ;
933969 execSyncInRepo ( cmd ) ;
934970 }
935971
0 commit comments