Clarify detached HEAD access and isolate test branch state - #2231
Merged
Conversation
<!-- agent --> Reading head.reference or active_branch raises TypeError when HEAD points directly to a commit, but the public documentation did not clearly explain how to access that commit. Document head.commit.hexsha for attached and detached HEADs, explain the reference setter/getter asymmetry, and clarify that active_branch requires an attached HEAD. Preserve the exception type and existing message prefix while adding a hint to use .commit or .object. Writable test fixtures assumed cloning produced an attached HEAD, so their branch access could fail when the source checkout was detached. Have with_rw_repo create and attach master at the requested revision when its clone is detached, retaining the clone's branch and tracking configuration otherwise. Explicitly attach the temporary bare remote to its own master branch before cloning it for remote tests. Assisted-by: GPT 6.0 Co-authored-by: GPT 6.0 <codex@openai.com>
Byron
force-pushed
the
detached-head
branch
from
September 10, 2026 03:17
fb9fe55 to
6bcb25b
Compare
Byron
marked this pull request as ready for review
September 10, 2026 03:19
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Normalize annotated-tag revisions to commits before creating the fixture branch.
Pull request overview
This pull request clarifies detached-HEAD access and isolates test fixtures from source branch state.
Changes:
- Documents detached-HEAD commit and reference behavior.
- Improves detached-reference diagnostics.
- Stabilizes temporary repository branch setup.
File summaries
| File | Summary |
|---|---|
test/lib/helper.py |
Adjusts writable fixture branch setup; annotated-tag revisions need normalization to a commit. |
git/repo/base.py |
Clarifies detached active_branch behavior. |
git/refs/symbolic.py |
Documents reference semantics and improves diagnostics. |
doc/source/tutorial.rst |
Adds detached-HEAD usage guidance. |
Review details
Suppressed comments (1)
test/lib/helper.py:165
create_headresolves a string withrev_parse, so an annotated tag resolves to aTagObjectandHeadrejects it as non-commit. The attached-HEAD branch above avoids this by callingrw_repo.commit(working_tree_ref), so detached-source fixtures regress for any annotated-tagworking_tree_ref; normalize the revision to a commit before creatingmaster.
rw_repo.head.reference = rw_repo.create_head(
"master", working_tree_ref, force=True, logmsg="Create test branch"
)
- Files reviewed: 4/4 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Tasks
This section is for Byron only. Models continuing this PR must not add, remove, check, uncheck, rename, or reorder checkboxes here.
Everything below this line was generated by
Codex GPT-6.Created by Codex on behalf of Byron. Byron will review before this is ready to merge.
Detached HEAD makes
repo.head.referenceraiseTypeError, but its public documentation and diagnostic did not explain how to obtain the commit. The diagnostic now points to.commitand.object, and the property documentation and tutorial showrepo.head.commit.hexshafor both HEAD states. They also explain why assigning a commit to.referencesucceeds while reading it afterward raises. Return types and exception types are preserved; the existing diagnostic gains a hint.The test suite now supplies branch state in its temporary repositories, gives writable remote fixtures their own
master, and uses explicit refspecs where an upstream is unnecessary. Branch-dependent assertions use writable fixtures, and the reflog assertion compares against Git instead of assuming consecutive entries name different commits. A regression covers bare and working clones of a detached commit that no branch points to. The Ubuntu/Python 3.14 CI job additionally runs the suite from detached and non-master source checkouts, withmasterabsent in the latter.Refs #2230.
Validation:
GIT_PYTHON_TEST_GIT_REPO_BASEwith non-master and detached source clones, each without a localmaster: 760 passed, 81 skipped, 1 xfailed, plus 14 passing subtests per state. Six tests needing network or process access were run separately and passed in both states.Git reference:
git/gitat1630431f326e15fcde608827b5ff38422528eb59,Documentation/git-symbolic-ref.adoc. Apple Git 2.50.1 confirms thatgit symbolic-ref -q HEADexits 1 for detached HEAD whilegit rev-parse HEADreturns the commit ID.