Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/checkout-remote-branch-locally #6

Draft
wants to merge 1 commit into
base: misc1
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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