Skip to content

feat: fetch_upstream for luneta's behind count - #30

Merged
lorenzolfm merged 5 commits into
mainfrom
fetch-upstream
Nov 10, 2026
Merged

lorenzolfm merged 5 commits into
mainfrom
fetch-upstream

Conversation

@lorenzolfm

Copy link
Copy Markdown
Owner

Problem

luneta shows how far each branch is behind its upstream, from git status. That count is only as current as the last fetch, so luneta now fetches the upstream of each branch a pane works on, every five minutes (lorenzolfm/luneta#351). It needs the fetch that wt cd --fetch does, which moves the remote-tracking ref only on a fast-forward and so keeps the record of a rewrite for wt follow, but without the report, and with an answer it can act on.

Change

fetch_upstream(dir, branch)

  branch.<b>.remote / merge, %(upstream)
         │
         ├─ none, or not under refs/remotes/ ──────────── Untracked
  git ls-remote --exit-code
         ├─ exit 2, the remote has no such branch ─────── Gone
         ├─ the tip is the ref already ────────────────── Current   (no fetch)
  git fetch --porcelain --refmap= <merge>:<ref>   (no +)
         ├─ fast-forward, or a new ref ────────────────── Advanced
         └─ ! rejected, not a fast-forward ────────────── Rewritten (the ref stays)
  • pub fn fetch_upstream(dir: &Path, branch: &str) -> Result<Upstream>. It takes the branch by name, so the fetch is of the branch the caller asked about and not of whatever HEAD is at by then. It changes no branch and no file, and prints nothing.
  • git moves the ref in the fetch itself. The refspec has no +, so git refuses a move that is not a fast-forward, and the empty refmap keeps the config's forced refspec out of it. --no-write-fetch-head leaves FETCH_HEAD alone: the tip no longer comes from a file that a different fetch in the same worktree can write between the fetch and the read.
  • A gone branch, as after a pull request merges and GitHub deletes it, is Gone, not an error.
  • git::output and git::failure let a caller read an exit code that tells a result.

wt follow after a background fetch

wt follow followed a rewrite only when the branch was at the remote-tracking ref. A fetch that fast-forwards the ref past the branch, as fetch_upstream does, broke that: after a push, a fetch, and then a force-push, wt follow refused the rewrite, though every commit on the branch was one the remote had. It now follows a rewrite when the ref has each commit of the branch (the branch is at the ref or behind it). A branch with commits of its own is still refused, and a fetch that moved the ref to the rewrite still removes the record.

Testing

cargo test: 131 pass; cargo clippy --all-targets -- --deny warnings and cargo fmt --check are clean.

  • a_fetch_of_the_upstream_branch_moves_nothing_but_the_ref: Advanced, then Current; the branch, a changed file and FETCH_HEAD stay.
  • a_fetch_of_a_rewrite_keeps_the_record_for_wt_follow: Rewritten, the ref stays, wt follow then moves the branch.
  • a_fetch_of_a_deleted_branch_finds_it_gone: Gone, the ref stays.
  • a_fetch_without_an_upstream_branch_uses_no_network: a deleted remote fails; a refs/pull/N/head upstream and no upstream are Untracked.
  • a_rewrite_after_a_fetch_of_a_fast_forward_moves_the_branch: fails without the Plan::of change, passes with it.

luneta fetches the upstream branch of each branch a pane sits on, so the
behind count on its row is of what the remote has. It needs the fetch
that check does, which moves the remote-tracking ref only on a
fast-forward and so keeps the record of a rewrite for wt follow, without
the report, the counts, or the git status that check runs.

The fetching line moves from fetch to run and inspect, so fetch prints
nothing.
fetch_upstream takes the branch by name, so the fetch is of the branch
luneta asked about and not of whatever HEAD is at by then, and it returns
what it found: Untracked, Current, Advanced, Rewritten or Gone.

It asks the remote for the tip with git ls-remote first. A ref already
at the tip needs no fetch, and a remote with no such branch, as after a
pull request merges and GitHub deletes it, is Gone and not a failure.
Otherwise one git fetch moves the ref, with a refspec that has no +, so
git itself refuses a move that is not a fast-forward and keeps the
record of a rewrite for wt follow; --porcelain marks that refusal with
!, which is Rewritten. An empty refmap keeps the config's forced refspec
out of it, and --no-write-fetch-head leaves FETCH_HEAD alone, so the tip
no longer comes from a file that a different fetch in the worktree can
write between the fetch and the read.

git::output and git::failure let a caller read an exit code that tells
a result.
wt follow followed a rewrite only when the branch was at the
remote-tracking ref. A fetch that fast-forwards that ref past the branch,
as fetch_upstream does in the background for luneta, broke that: after
a push, a fetch and then a force-push, wt follow refused the rewrite,
though each commit on the branch was one the remote had.

The rewrite is now followed when the ref has each commit of the branch:
the branch is at the ref or behind it. A branch with commits of its own
still has a commit that the ref does not have, and is still refused, and
a fetch that moved the ref to the rewrite still removes the record.
git marks a ref it did not update with ! in the porcelain output, for a
move that is not a fast-forward and also for a ref it cannot lock. A
fetch that collided with a different git process, or met a stale lock
file, was therefore Rewritten, and the failure was never told. The line
carries the old and new ids, so Rewritten is now only the case where git
says that the new commit does not contain the old one; any other ! is
the error of the fetch.

The remote and the branch on it came from git config --get, which reads
the last branch.<b>.merge, and the ref from %(upstream), which reads the
first. With two, the fetch wrote the tip of the second branch into the
ref of the first. One for-each-ref now gives the ref, the remote and the
branch from the same entry, and it must name the branch exactly, as the
pattern also matches the branches under refs/heads/<b>/. A git that
fails there is an error and no longer Untracked.
The README still said the branch must be at the remote-tracking ref, and
that any different fetch stops the move. A fast-forward of the ref now
keeps the record; only a fetch that moves the ref to the rewrite stops it.
@lorenzolfm
lorenzolfm merged commit a6fee7e into main Nov 10, 2026
4 checks passed
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