Automatically fetch remote branches during `wt switch`
Description
Feature request: if a branch doesn't exist locally when running wt switch, check if it exists in any of the (fetch) remotes before returning an error.
Context
I'm often trying to checkout a branch on the remote, in which case I have to do
git fetch origin remote-branch-name;
wt switch remote-branch-name;
it'd be nice to check remotes before wt switch declares that a branch doesn't exist! Happy to implement it; just wanted to put up an issue first.
Thanks for the suggestion! I searched the codebase and found that wt switch already has partial support here:
- It checks cached remote-tracking refs (not just local branches), so if you've fetched recently,
wt switch remote-branch-namewill automatically create a local tracking branch — no need for a separategit fetch+wt switchin that case. - The
pr:<number>syntax fetches from the remote viagh/glab, but regular branch names don't trigger a fetch.
What's missing is exactly what you described: when a branch isn't found locally or in cached remote-tracking refs, automatically fetching from remotes before giving up. That doesn't exist today.
I'll leave it for a maintainer to evaluate and prioritize. Your offer to implement it is great — the relevant code paths are in src/commands/worktree/switch.rs (around validate_worktree_creation) and src/git/repository/branch.rs (exists() / remotes()).
ah, I think this could be implemented as a pre-switch hook
That's a reasonable workaround! The pre-switch hook does have access to $WT_TARGET (the branch name), so something like this in wt.toml would work:
[pre-switch]
check = "git fetch origin $WT_TARGET 2>/dev/null || true"
The hook runs before git worktree add, so the freshly-fetched remote-tracking ref would be available for DWIM to pick up.
The main downside is that it adds a network round-trip to every switch, even when the branch already exists locally. A built-in implementation could skip the fetch when the branch is already known, making the common case faster.
implemented it as
[pre-switch.fetch-branch]
command = """
if ! git rev-parse --verify "refs/heads/{{ branch }}" >/dev/null 2>&1 \
&& [ -z "$(git for-each-ref "refs/remotes/*/{{ branch }}")" ]; then
for remote in $(git remote); do
if git fetch --quiet "$remote" "+refs/heads/{{ branch }}:refs/remotes/$remote/{{ branch }}" 2>/dev/null; then
break
fi
done
fi
"""
Nice — that handles the "only fetch when needed" optimization well. The git for-each-ref check for cached remote-tracking refs before hitting the network is exactly the right guard. Good use of the hook system!
That's a reasonable workaround! The
pre-switchhook does have access to$WT_TARGET(the branch name), so something like this inwt.tomlwould work:
is this accurate?! $WT_TARGET?
No, that was wrong on my part — sorry for the confusion! There is no $WT_TARGET environment variable. Hooks use Jinja-style template variables like {{ branch }}, as @ShanTulshi correctly used in their follow-up snippet.