worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Automatically fetch remote branches during `wt switch`

Open ShanTulshi opened this issue 8 months ago • 7 comments

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.

ShanTulshi avatar Mar 24 '26 17:03 ShanTulshi

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-name will automatically create a local tracking branch — no need for a separate git fetch + wt switch in that case.
  • The pr:<number> syntax fetches from the remote via gh/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()).

worktrunk-bot avatar Mar 24 '26 18:03 worktrunk-bot

ah, I think this could be implemented as a pre-switch hook

ShanTulshi avatar Mar 24 '26 18:03 ShanTulshi

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.

worktrunk-bot avatar Mar 24 '26 18:03 worktrunk-bot

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
"""

ShanTulshi avatar Mar 24 '26 18:03 ShanTulshi

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!

worktrunk-bot avatar Mar 24 '26 18:03 worktrunk-bot

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:

is this accurate?! $WT_TARGET?

max-sixty avatar Mar 24 '26 20:03 max-sixty

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.

worktrunk-bot avatar Mar 24 '26 20:03 worktrunk-bot