Skip to content

feat: wt follow brings a worktree to its upstream branch - #28

Merged
lorenzolfm merged 2 commits into
mainfrom
follow-upstream
Nov 5, 2026
Merged

lorenzolfm merged 2 commits into
mainfrom
follow-upstream

Conversation

@lorenzolfm

@lorenzolfm lorenzolfm commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner
wt follow

  fetch the upstream branch
         │
         ├─ at the upstream branch ────────────────────── nothing to do
         ├─ a change that is not committed ────────────── refuse
         ├─ a git operation that is not finished ──────── refuse
         ├─ an untracked or ignored file in the way ───── refuse
         ├─ behind ────────────────────────────────────── move
         ├─ rewritten, at the remote-tracking ref ─────── move
         └─ commits that the remote does not have ─────── refuse

wt add <link> goes to the worktree that has the branch, and does not change it. A second look at a pull request therefore reads the commits of the first look, and nothing says so. --fetch does not help: it runs after the return for a worktree that exists, and a local branch wins over the ref it fetched.

wt follow brings the current worktree to its upstream branch. luneta needs this for lorenzolfm/luneta#291, where pressing r on a pull request's link should review what GitHub has. The operation knows nothing of pull requests: wt add <link> already makes the branch track the head of the pull request, also from a fork.

What changes

  • cmd::follow::run(dir) takes a worktree directory and no Repo, because it makes no worktree. It works in a clone with a work tree too. It refuses a directory that is not in a worktree, before the fetch.
  • It moves with git switch --no-overwrite-ignore -C, for the fast-forward and for the rewrite. git reset --keep is not safe here: it overwrites an ignored file, and each link that wt makes is an ignored file. It also moves the branch under a paused git am, and it ends a paused cherry-pick. git switch refuses all of these.
  • The fetch does not move the remote-tracking ref (--refmap=). A refusal therefore keeps the record of a rewrite, and the next run can move the branch. The ref moves after a move, or when the branch is at the upstream branch already, so the next rewrite also finds the branch at the ref.
  • branch.<name>.merge is fetched as it is, so a branch from gh pr checkout that merges from refs/pull/N/head also follows. That ref has no remote-tracking ref, so a rewrite there is refused.
  • The command, the fish function and completions, the README and the crate docs.

Choices to check

  • A change at the upstream branch does not stop the command. Nothing has to move, so nothing is in danger. luneta's issue needs this.
  • A different fetch stops a rewrite. git fetch or wt prune between two looks moves the remote-tracking ref, and then nothing records the old commits. The command refuses, says that it cannot tell your work from the old commits of a rewrite, and prints the command that moves the branch. The README says this. A real fix needs wt to keep its own record, so that is a separate change.
  • A path that the upstream branch starts to track stops the move while the worktree has an ignored file or a link there. The user moves the file and runs the command again.
  • A refusal leaves the remote-tracking ref where it was, so git status can look out of date until a move or a different fetch.
  • There is no short form. wt f stays the jump to the only worktree whose name starts with f.
  • wt clone leaves branches with no upstream. git clone --bare makes each remote branch a local branch with no upstream, and wt follow refuses those and says how to set one. wt add <link> sets the upstream, so pull requests are not affected. wt add <branch> with a local branch does not; I left that alone.
  • The name. wt sync is taken: it makes the links.

Testing

  • wt, wt-clippy, wt-test and wt-fmt pass: 109 tests, 20 of them for this command, on real git.
  • The tests for the fixes in the second commit failed on the first commit, for the intended reason. Two others guard behaviour that those fixes must keep, and they passed before and after.
  • Spot mutations of 11 guards: 10 caught. The survivor is the filter on refs/remotes/, which matters only when a local upstream branch moves during the run.
  • The real binary on a wt clone of a local remote, including --repo from another directory: at the upstream branch, behind, rewritten, a change, a local commit and a rewrite, no upstream, no branch, an ignored file, an ignored directory, a link to a file and to a directory, a retry after each refusal, a different fetch, a paused git am, cherry-pick and merge, a fork remote and a local upstream. Also wt f and wt follow through the fish function.
  • Not run: luneta's tests against this version. luneta pins 4bbe91f, and it needs a bump to the merge.

`wt add` goes to the worktree that has the branch, and does not change it.
A second look at a pull request therefore reads the commits of the first
look, and nothing says so. `--fetch` does not help: it runs after the return
for a worktree that exists, and a local branch wins over the ref it fetched.

`wt follow` fetches the upstream branch, and moves the local branch when the
move cannot lose work: the branch is behind, or the upstream branch was
rewritten and the local branch is where the remote-tracking ref was before
the fetch. It refuses a change that is not committed, commits that the remote
does not have, and an untracked file in the way. A branch that is at the
upstream branch needs no move, so a change there does not stop it.

The fetch moves the remote-tracking ref itself, so the next rewrite also
finds the branch where the ref was.
lorenzolfm added a commit to lorenzolfm/luneta that referenced this pull request Nov 5, 2026
The moves had nothing of luneta's in them, and two parts of them were there
only because luneta cannot see what wt knows: setting the upstream by hand
after fetching `refs/pull/N/head`, and rebuilding the pull request's branch
from git's config. wt fetches the upstream branch itself, which moves the
ref, and `wt add <link>` already makes that the pull request's.

luneta keeps what is about a pull request: `gh` for the base and the head, the
base's fetch, and a check that the worktree came to the head, which stands in
for the check of its branch. It runs wt as `luneta follow`, a program of its
own and alone, as `luneta worktree` is: wt talks on stderr, and git may ask
for a passphrase.

Pins wt to lorenzolfm/wt#28's head, to pin again once that merges.
`git reset --keep` refuses to overwrite an untracked file only when git
does not ignore it. Each link that wt makes is an ignored file. A path that
the upstream branch started to track therefore replaced the link, and it
deleted an ignored directory in its way, with no message. The command now
moves with `git switch --no-overwrite-ignore`. That also refuses to move the
branch under a paused `git am` or cherry-pick.

The fetch moved the remote-tracking ref before the checks, and that ref is
the only record of a rewrite. After one refusal, each later run refused the
branch. The fetch now uses an empty refmap, and the ref moves after the move.

A different fetch still removes the record. The README says so, and the
refusal gives the command that moves the branch.

The short form `wt f` hid the jump to the only worktree whose name starts
with f, so remove it. The command also refuses a directory that is not in a
worktree, before the fetch.
@lorenzolfm
lorenzolfm merged commit fc6bcd4 into main Nov 5, 2026
4 checks passed
lorenzolfm added a commit to lorenzolfm/luneta that referenced this pull request Nov 5, 2026
lorenzolfm/wt#28 merged as fc6bcd4, with a fix on top of the commit this
pinned. A move no longer overwrites an ignored file, which each of wt's
links is. A refusal keeps the record of a rewrite, so `r` goes on once the
cause is gone. A rewrite another fetch saw first is still refused, and the
refusal now gives the command that moves the branch.

Two of these are tested here as well as in wt. luneta fetches the base just
before wt reads that record, and the command has to reach the note whole,
as the last of the three lines wt says.

That fix moves the remote-tracking ref after the move, from the directory it
is given. A pane in a directory the head leaves out gave it one the move had
just deleted, so wt moved the branch, failed, and left the ref behind, which
loses the record for the next rewrite. luneta now gives wt the worktree's top,
which no move deletes, and says that the pane's directory has gone.
lorenzolfm added a commit to lorenzolfm/luneta that referenced this pull request Nov 6, 2026
The moves had nothing of luneta's in them, and two parts of them were there
only because luneta cannot see what wt knows: setting the upstream by hand
after fetching `refs/pull/N/head`, and rebuilding the pull request's branch
from git's config. wt fetches the upstream branch itself, which moves the
ref, and `wt add <link>` already makes that the pull request's.

luneta keeps what is about a pull request: `gh` for the base and the head, the
base's fetch, and a check that the worktree came to the head, which stands in
for the check of its branch. It runs wt as `luneta follow`, a program of its
own and alone, as `luneta worktree` is: wt talks on stderr, and git may ask
for a passphrase.

Pins wt to lorenzolfm/wt#28's head, to pin again once that merges.
lorenzolfm added a commit to lorenzolfm/luneta that referenced this pull request Nov 6, 2026
lorenzolfm/wt#28 merged as fc6bcd4, with a fix on top of the commit this
pinned. A move no longer overwrites an ignored file, which each of wt's
links is. A refusal keeps the record of a rewrite, so `r` goes on once the
cause is gone. A rewrite another fetch saw first is still refused, and the
refusal now gives the command that moves the branch.

Two of these are tested here as well as in wt. luneta fetches the base just
before wt reads that record, and the command has to reach the note whole,
as the last of the three lines wt says.

That fix moves the remote-tracking ref after the move, from the directory it
is given. A pane in a directory the head leaves out gave it one the move had
just deleted, so wt moved the branch, failed, and left the ref behind, which
loses the record for the next rewrite. luneta now gives wt the worktree's top,
which no move deletes, and says that the pane's directory has gone.
lorenzolfm added a commit to lorenzolfm/luneta that referenced this pull request Nov 6, 2026
The moves had nothing of luneta's in them, and two parts of them were there
only because luneta cannot see what wt knows: setting the upstream by hand
after fetching `refs/pull/N/head`, and rebuilding the pull request's branch
from git's config. wt fetches the upstream branch itself, which moves the
ref, and `wt add <link>` already makes that the pull request's.

luneta keeps what is about a pull request: `gh` for the base and the head, the
base's fetch, and a check that the worktree came to the head, which stands in
for the check of its branch. It runs wt as `luneta follow`, a program of its
own and alone, as `luneta worktree` is: wt talks on stderr, and git may ask
for a passphrase.

Pins wt to lorenzolfm/wt#28's head, to pin again once that merges.
lorenzolfm added a commit to lorenzolfm/luneta that referenced this pull request Nov 6, 2026
lorenzolfm/wt#28 merged as fc6bcd4, with a fix on top of the commit this
pinned. A move no longer overwrites an ignored file, which each of wt's
links is. A refusal keeps the record of a rewrite, so `r` goes on once the
cause is gone. A rewrite another fetch saw first is still refused, and the
refusal now gives the command that moves the branch.

Two of these are tested here as well as in wt. luneta fetches the base just
before wt reads that record, and the command has to reach the note whole,
as the last of the three lines wt says.

That fix moves the remote-tracking ref after the move, from the directory it
is given. A pane in a directory the head leaves out gave it one the move had
just deleted, so wt moved the branch, failed, and left the ref behind, which
loses the record for the next rewrite. luneta now gives wt the worktree's top,
which no move deletes, and says that the pane's directory has gone.
lorenzolfm added a commit to lorenzolfm/luneta that referenced this pull request Nov 6, 2026
* Sync a pull request's worktree before `r` reviews it

`r` started its review on whatever the clone last fetched, so a second
review after a push or a force-push read the commits the first one did,
and nothing said so.

`r` now fetches the pull request's head and its base in `ready`, on the
machine that has the repository, and starts no review where it cannot
bring the worktree to the head. `w` and `t` go on as they did.

The upstream is set to the head after a sync. Left behind, it would stop
matching the tip, and the next force-push would look like local commits.

A worktree moves only while the pull request's branch is checked out, as
a pane the daemon last saw there may be on `main` by now.

A daemon older than `at_head` ignores it, so the answer says it too, and
luneta refuses an answer that does not.

Closes #291

* Have `wt follow` move the worktree for `r`

The moves had nothing of luneta's in them, and two parts of them were there
only because luneta cannot see what wt knows: setting the upstream by hand
after fetching `refs/pull/N/head`, and rebuilding the pull request's branch
from git's config. wt fetches the upstream branch itself, which moves the
ref, and `wt add <link>` already makes that the pull request's.

luneta keeps what is about a pull request: `gh` for the base and the head, the
base's fetch, and a check that the worktree came to the head, which stands in
for the check of its branch. It runs wt as `luneta follow`, a program of its
own and alone, as `luneta worktree` is: wt talks on stderr, and git may ask
for a passphrase.

Pins wt to lorenzolfm/wt#28's head, to pin again once that merges.

* Pin wt to the merge of `wt follow`, and follow from the top

lorenzolfm/wt#28 merged as fc6bcd4, with a fix on top of the commit this
pinned. A move no longer overwrites an ignored file, which each of wt's
links is. A refusal keeps the record of a rewrite, so `r` goes on once the
cause is gone. A rewrite another fetch saw first is still refused, and the
refusal now gives the command that moves the branch.

Two of these are tested here as well as in wt. luneta fetches the base just
before wt reads that record, and the command has to reach the note whole,
as the last of the three lines wt says.

That fix moves the remote-tracking ref after the move, from the directory it
is given. A pane in a directory the head leaves out gave it one the move had
just deleted, so wt moved the branch, failed, and left the ref behind, which
loses the record for the next rewrite. luneta now gives wt the worktree's top,
which no move deletes, and says that the pane's directory has gone.

* Run no git hook while `r` syncs a worktree

wt's fix took `wt follow` from `reset --keep` to `switch -C`, and `switch`
runs `post-checkout`. Where a clone's `core.hooksPath` is a path in the tree,
as `.githooks` often is, that hook is the one in the head just checked out, so
pressing `r` ran the author's script, with no agent and nothing to approve.
Ref updates run `reference-transaction` from the same place, in wt's move and
in the base's fetch of the next `r`. wt's add runs git from `.bare`, which finds
no such hook, so a worktree's first `r` never had this.

`to_head` now gives its fetch and `luneta follow` `core.hooksPath=/dev/null`,
through `GIT_CONFIG_*`, which reaches each git wt starts with no change to wt,
and adds to what the daemon's environment has. wt's own hook acts only on a new
worktree, so a move loses nothing of it. Other hooks do not run for the move
either, as they did not for `reset --keep`. Git run in that tree afterwards, by
hand or by an agent, can still run one: that comes with such a `core.hooksPath`.

* Let `r` go on where no remote names the repository

A pane on the branch needed no remote before this branch, as gh names its
pull request by itself. `to_head` started by looking the remote up, so `r`
there was refused, even at the head, in a repository GitHub renamed, a
fork's clone with the parent as gh's default, an `insteadOf` spelling, or
`ssh.github.com:443`. With no remote named there is no base to fetch, and the
check at the head is what guards the review, so it goes on to that. git not
saying its remotes still starts no review. `t` on an issue goes back to
main's code.

Keep `core.fsmonitor` off for the fetch and `luneta follow` too. By a path in
the tree it is the author's script, as a hook in `core.hooksPath` is, and
lunetad's own `git status` has it off already. `core.hooksPath` does not reach
a hook named by `hook.<name>.command`, so the README now says that one runs.
@lorenzolfm
lorenzolfm deleted the follow-upstream branch November 9, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant