Repository navigation
feat: fetch_upstream for luneta's behind count - #30
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thatwt cd --fetchdoes, which moves the remote-tracking ref only on a fast-forward and so keeps the record of a rewrite forwt follow, but without the report, and with an answer it can act on.Change
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 whateverHEADis at by then. It changes no branch and no file, and prints nothing.+, 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-headleavesFETCH_HEADalone: the tip no longer comes from a file that a different fetch in the same worktree can write between the fetch and the read.Gone, not an error.git::outputandgit::failurelet a caller read an exit code that tells a result.wt followafter a background fetchwt followfollowed a rewrite only when the branch was at the remote-tracking ref. A fetch that fast-forwards the ref past the branch, asfetch_upstreamdoes, broke that: after a push, a fetch, and then a force-push,wt followrefused 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 warningsandcargo fmt --checkare clean.a_fetch_of_the_upstream_branch_moves_nothing_but_the_ref: Advanced, then Current; the branch, a changed file andFETCH_HEADstay.a_fetch_of_a_rewrite_keeps_the_record_for_wt_follow: Rewritten, the ref stays,wt followthen 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; arefs/pull/N/headupstream and no upstream are Untracked.a_rewrite_after_a_fetch_of_a_fast_forward_moves_the_branch: fails without thePlan::ofchange, passes with it.