From 3151bc5e744543f2e186fa5196ef67ff00ffe4e1 Mon Sep 17 00:00:00 2001 From: Kipras Melnikovas Date: Sun, 6 Mar 2022 23:33:37 +0200 Subject: [PATCH] feat: hackishly checkout remote branch locally if not checked out yet Signed-off-by: Kipras Melnikovas --- branchSequencer.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/branchSequencer.ts b/branchSequencer.ts index 4b31728d..4e9a5d27 100644 --- a/branchSequencer.ts +++ b/branchSequencer.ts @@ -138,6 +138,7 @@ export const branchSequencer: BranchSequencer = async ({ }); const cmd = cmds[0]; + assert(cmd.rebaseKind === "stacked"); const targetCommitSHA: string | null = cmd.commitSHAThatBranchPointsTo; @@ -148,9 +149,59 @@ export const branchSequencer: BranchSequencer = async ({ assert(cmd.targets?.length); - const targetBranch = cmd.targets[0].replace("refs/heads/", ""); + let targetBranch = cmd.targets[0].replace("refs/heads/", ""); assert(targetBranch && typeof targetBranch === "string"); + /** + * if we only have the remote branch, but it's not checked out locally, + * we'd end up in a detached state, and things would break. + * + * thus, we checkout the branch locally if it's not. + */ + // if (!Git.Branch.lookup(repo, targetBranch, Git.Branch.BRANCH.LOCAL)) { + // execSyncInRepo(); + // } + if (targetBranch.startsWith("refs/remotes/")) { + /** + * TODO - probably should handle this "checkout remote branch locally" logic + * in a better place than here, + * + * especially since this is quite fragile, + * e.g. if multiple remotes exist & have the same branch.. + * + * here's a hint that git gives in this situation (& exits w/ 1 so good that errors) + * + * ``` + * hint: If you meant to check out a remote tracking branch on, e.g. 'origin', + * hint: you can do so by fully qualifying the name with the --track option: + * hint: + * hint: git checkout --track origin/ + * hint: + * hint: If you'd like to always have checkouts of an ambiguous prefer + * hint: one remote, e.g. the 'origin' remote, consider setting + * hint: checkout.defaultRemote=origin in your config. + * fatal: 'fork' matched multiple (2) remote tracking branches + * ``` + * + * seems like we should be checking all the branches somewhere early, + * and either checking them out locally, + * or if not possible because multiple remotes, then asking them + * which remote to use (or individual, idk). + * + * tho, this might not always be necessary, so maybe not good + * to ask for something that might not be needed? + * or maybe it is always necessary, then yeah, should handle early. + * + * probably a good spot would be in the `branchSequencer`, + * just not in an individual "checkout", + * but rather - before any checkouts take place -- + * by doing the pre-checking logic if branches exists + * before doing the checkouts. + * + */ + targetBranch = targetBranch.replace(/refs\/remotes\/[^/]+\//, ""); + } + // console.log({ targetCommitSHA, target: targetBranch }); /**