Any way to auto-fetch/pull before a new worktree?
I may have missed this in the docs/open issues, but my usecase is as follows:
- I have a checkout of
~/my-repowhich is designed to stay up to date withmain - I want to be able to spin up lightweight worktrees based on this repo
- Since I'm working/pulling
mainmostly inside worktrees,~/my-repocan often become out of date - Ideally, I want to be able to have
wt switch --create my-new-branchautomatically run agit pullor otherwise base the new worktree on latestmain, not necessarily the version that's currently checked out locally
I know I could probably create a new alias like new-worktree and have that essentially run cd ~/my-repo && git pull origin main && wt switch --create $1, but:
- I'm wondering if this can be made to work via hooks (there doesn't seem to exist a
pre-start?) - I'm also wondering whether the absence of this functionality means that I've misunderstood or overlooked something important with worktrees in general (new to me) or with
worktrunk's philosophy (I saw another comment in an issue thread explaining thatgit fetchis considered a mutable operation, so I'm assuming that's probably why pull-by-default isn't a thing?)
Curious on your thoughts and if you have any tips for my desired workflow - thank you!
I'm wondering if this can be made to work via hooks (there doesn't seem to exist a
pre-start?)
yes we could add this!
what would this do — run before checking branches etc, on any wt switch?
I think that's probably the best place; I'm generally wary of adding any network requests inside hot paths. (are you sure you would want to wait for git pull every time?)
what would this do — run before checking branches etc, on any wt switch?
Yeah, I was imagining that it'd work similar to post-{start,create} to match the blocking/non-blocking behaviour of those (although not sure there'd be much benefit to a non-blocking pre-create since presumably you could just do it in post-create instead).
I think that's probably the best place; I'm generally wary of adding any network requests inside hot paths. (are you sure you would want to wait for git pull every time?)
Would also be fine if there was some way to tell wt to create a new worktree based on the most up to date SHA of origin/main, though I'm not sure if that's a limitation of git worktrees themselves? I basically just want to guarantee that new worktrees are always up to date with the remote HEAD sha, so that I don't have to think about manually fetching or pulling when spinning up new branches. A plan wt switch --create on ~/my-repo could leave me accidentally far behind main otherwise (so then would have to pull and rebuild).
@max-sixty finally got around to testing out this version, thanks again for adding the pre-switch hook!
I was able to get the behaviour I wanted via
[pre-switch]
pull = "git pull origin HEAD"
However, I noticed that the docs show this recipe:
[pre-switch]
# Fetch if last fetch was more than 6 hours ago
fetch = """
FETCH_HEAD="$(git rev-parse --git-common-dir)/FETCH_HEAD"
if [ "$(find "$FETCH_HEAD" -mmin +360 2>/dev/null)" ] || [ ! -f "$FETCH_HEAD" ]; then
git fetch
fi
"""
I tried this out but I'm not sure it works the way I'd expect - the worktree created after a fresh git fetch is still based on whichever SHA is locally present in the source git repo, rather than the latest. Is there something I'm missing or does this example instead need to use git pull? Or is there another config option to have the new worktree based off origin/main instead (which would respect the latest fetched head branch)?
erm, yes, sorry! and thank you for spotting that @jdb8 !
@worktrunk-bot could we make that change?
Good catch @jdb8! The pre-switch example was using git fetch, which only updates remote tracking refs (origin/main) without advancing the local branch. So a new worktree created afterward would still be based on the old local main.
Fixed in #1323 — changed the example to use git pull instead.