Skip to content

Commit

Permalink
feat: hackishly checkout remote branch locally if not checked out yet
Browse files Browse the repository at this point in the history
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
  • Loading branch information
kiprasmel committed Apr 11, 2022
1 parent 8e1ade1 commit 3151bc5
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion branchSequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const branchSequencer: BranchSequencer = async ({
});

const cmd = cmds[0];

assert(cmd.rebaseKind === "stacked");

const targetCommitSHA: string | null = cmd.commitSHAThatBranchPointsTo;
Expand All @@ -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/<name>
* hint:
* hint: If you'd like to always have checkouts of an ambiguous <name> 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 });

/**
Expand Down

0 comments on commit 3151bc5

Please sign in to comment.