Skip to content

Ask git whether leaving a detached HEAD loses commits - #210

Merged
jorio merged 5 commits into
jorio:masterfrom
Grinv:detached-head-reachability
Nov 20, 2026
Merged

jorio merged 5 commits into
jorio:masterfrom
Grinv:detached-head-reachability

Conversation

@Grinv

@Grinv Grinv commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #205. SwitchBranch and CheckoutCommit decided from the graph whether leaving a detached HEAD would lose commits. Beyond the loaded commits the graph couldn't tell, so GF always warned, even when a branch still reached HEAD. It also warned when only refs/stash or a ref outside refs/heads, refs/remotes and refs/tags kept the commit.

RepoTask.flowCommitsLeftBehind now asks git the question that git checkout's own warning answers: git rev-list -n1 HEAD ^<target> --single-worktree --exclude=HEAD --not --all. I compared it with that warning for stashes, tags, remote and custom refs, refs/bisect, and other worktrees' HEADs and per-worktree refs; they agree.

It runs only when HEAD is detached, as a background git process. One switch on jellyfin-web (30,000 commits, 10,000 loaded):

HEAD depth Before (graph) After, with commit-graph After, without
5 0.00 ms 2.8 ms 3.1 ms
2,500 0.21 ms 3.7 ms 17.4 ms
10,000 0.38 ms 6.3 ms 68.2 ms
30,000 wrong warning 6.7 ms 90.6 ms

Question: the dialog says "You might lose track of this commit". Now that the answer is exact, should it say how many commits are left behind, like git does? That changes a translated string, so I left it.

Tested: full suite on PyQt6, PySide6 and PyQt5 (testToggleWordWrap fails on master too, see #208); branch and commit tests on Windows 11.

Prepared with Claude Code; I reviewed the changes.

Checklist

@jorio

jorio commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Nice. The cost doesn't bother me too much because the common case is cheap, and it occurs in a relatively narrow set of circumstances. I don't think making thousands of commits with a detached HEAD is a common enough use case to worry about performance here.

I appreciate the profiling as always, the results are interesting.

the dialog says "You might lose track of this commit". Now that the answer is exact, should it say how many commits are left behind, like git does?

Sure, since we get that info "for free" now, why not present it to the user.
Wording ideas:

  • (n==1) If you check out {commit}, you might lose track of the <b>detached HEAD {head}</b> because it isn’t reachable from any other branch.
  • (n>=2) If you check out {commit}, you might lose track of the <b>detached HEAD {head} and {n-1} more commits</b> that aren’t reachable from any other branch.

That changes a translated string, so I left it.

No problem changing translatable text if that's an improvement in the UI - don't worry.

One of the last things I do before each release is to update the .po files for Weblate and take care of French (that makes me proofread the English text with fresh eyes, and sometimes it reveals strings with poor wording).

Comment thread gitfourchette/tasks/repotask.py Resolved
@Grinv

Grinv commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Done: the warning now counts the lost commits, with your wording, and SwitchBranch says "If you switch to {branch}". The n-1 goes through _n ("1 more commit", "2 more commits").

Counting costs the same as the yes/no check, since git walks the same commits either way (median ms):

Case -n1 --count
60k-commit repo, nothing lost, target HEAD~10000 28.6 29.4
1,000 lost commits 4.3 4.3
10,000 lost commits 25.8 26.3

The "You are in Detached HEAD mode" paragraph is gone, since the sentence now names the detached HEAD.

Tags, remote branches and the stash also keep a commit reachable, but git's own message says "any of your branches" too, so I kept "any other branch".

@Grinv
Grinv requested a review from jorio November 20, 2026 00:26
Comment thread gitfourchette/tasks/branchtasks.py Resolved
Comment thread gitfourchette/tasks/committasks.py Resolved
Comment thread gitfourchette/tasks/committasks.py Resolved
Comment thread gitfourchette/tasks/committasks.py Resolved
Comment thread test/test_tasks_branch.py Resolved
Comment thread test/test_tasks_branch.py Resolved
Comment thread test/test_tasks_branch.py Resolved
Comment thread test/test_tasks_branch.py Resolved
@Grinv

Grinv commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Applied in fca8b93. Both hashes use tquo (no HTML to escape). The test keeps the exact "1 more commit" / "N more commits" rather than commits?, so a wrong plural still fails it.

@Grinv
Grinv requested a review from jorio November 20, 2026 16:08
SwitchBranch and CheckoutCommit warned about losing a detached HEAD based
on the graph: whether another ref sat at HEAD or a commit above it. When
HEAD was beyond the loaded commits, the graph couldn't tell, so GF always
warned, even if a branch still reached HEAD. It also warned when only
refs/stash or a ref outside refs/heads, refs/remotes and refs/tags kept
the commit.

RepoTask.flowCommitsLeftBehind asks git the question its own checkout
warning answers: `git rev-list -n1 HEAD ^target --single-worktree
--exclude=HEAD --not --all`, so this worktree's refs count, but not HEAD
or other worktrees' HEADs. It runs in the background like other git calls.

Graph.getCommitFrame lost its last caller and is gone.
The warning said "You might lose track of this commit" back when GF only
guessed. git now gives the exact count, so the warning names the
detached HEAD and how many more commits no other branch reaches.

commitsLeftBehind() moves to committasks.py, next to readCommentString(),
and counts with `rev-list --count` instead of stopping at the first
commit, at the same cost.
Quoted hashes read more easily in the sentence. The Check Out dialog
also gets "Check Out X" / "Don't Check Out" buttons like the Switch
dialog's.
Since jorio#211, tasks pass commits to git as <hash>^{commit}. rev-list read
the bare hash correctly already, but this keeps one rule for every task.
@jorio

jorio commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Can you please look at the conflicts? Then I'll merge. Thanks!

@Grinv
Grinv force-pushed the detached-head-reachability branch from fca8b93 to b176995 Compare November 20, 2026 17:14
@Grinv

Grinv commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Rebased; the conflict was with #207's flowSetGitConfig. Since #211 is in, CheckoutCommit passes GitDriver.commitArg(oid) like the other tasks (b176995).

Comment thread test/test_tasks_branch.py Resolved
@Grinv
Grinv requested a review from jorio November 20, 2026 17:25
@jorio
jorio merged commit 55f2f1e into jorio:master Nov 20, 2026
11 checks passed
@Grinv
Grinv deleted the detached-head-reachability branch November 20, 2026 18:59
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.

2 participants