You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clarify detached HEAD access and isolate test branch state (#2230)
Reading head.reference intentionally returns another reference, so it raises
TypeError when HEAD contains a commit ID directly. That contract is useful,
but the public properties did not explain how to obtain the commit instead.
The package tests also assumed the source checkout was attached, sometimes
specifically to master. The seven tests reported in #2230 passed attached
and failed after detaching at the same commit.
Document head.commit.hexsha for both HEAD states, the reference setter/getter
asymmetry, and active_branch's detached-HEAD restriction. Keep TypeError and
the existing exception message prefix, appending a hint to use .commit or
.object. The full diagnostic changes; return types and exception types do
not. The extended API regression failed on the missing hint before this fix
and verifies that commit access still works after detaching.
Make writable test clones attach a branch when cloning leaves HEAD detached,
while retaining clone-created branch/tracking configuration otherwise. Give
the temporary bare remote its own master branch. Move branch-dependent
assertions and tutorial setup to writable fixtures, make remote tests supply
their refspecs, and establish tracking configuration before testing its
removal. Compare @{1} against git rev-parse instead of assuming successive
reflog entries contain different commits.
Add a helper regression using a detached commit that no branch points to,
covering both bare and working clones without reattaching the source. The
Ubuntu/Python 3.14 CI job now also runs the full suite after detaching the
source and after renaming master to another branch, leaving master absent.
Git baseline: /Users/byron/dev/github.com/git/git at
1630431f326e15fcde608827b5ff38422528eb59, Documentation/git-symbolic-ref.adoc,
documents exit status 1 when the requested name is not symbolic. Apple Git
2.50.1 confirmed that symbolic-ref -q HEAD exits 1 for detached HEAD while
rev-parse HEAD still returns its commit ID.
Validation on Python 3.14.7:
- Full pytest suite using GIT_PYTHON_TEST_GIT_REPO_BASE with non-master and
detached source clones, each without a local master: 760 passed, 81 skipped,
1 xfailed and 14 subtests passed per state. Six tests needing network or
process access were run separately outside the sandbox and all passed.
- Ruff check and format --check passed for the repository.
- Sphinx HTML build succeeded; its 12 Python 3.14 annotation warnings are
byte-for-byte identical to the unchanged baseline's warnings.
Copy file name to clipboardExpand all lines: doc/source/tutorial.rst
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ The first step is to create a :class:`git.Repo <git.repo.base.Repo>` object to r
23
23
:start-after: # [1-test_init_repo_object]
24
24
:end-before: # ![1-test_init_repo_object]
25
25
26
-
In the above example, the directory ``self.rorepo.working_tree_dir`` equals ``/Users/mtrier/Development/git-python`` and is my working repository which contains the ``.git`` directory. You can also initialize GitPython with a *bare* repository.
26
+
In the above example, ``rw_repo.working_tree_dir`` is the path to a working repository which contains the ``.git`` directory. You can also initialize GitPython with a *bare* repository.
:class:`Heads <git.refs.head.Head>` Heads are branches in git-speak. :class:`References <git.refs.reference.Reference>` are pointers to a specific commit or to other references. Heads and :class:`Tags <git.refs.tag.TagReference>` are a kind of references. GitPython allows you to query them rather intuitively.
80
80
81
+
To obtain the current commit ID, use ``repo.head.commit.hexsha``. This works both
82
+
on a branch and with a detached HEAD, provided HEAD resolves to an existing commit.
83
+
When ``repo.head.is_detached`` is true, HEAD points directly to a commit and there
84
+
is no active branch: reading ``repo.head.reference`` or ``repo.active_branch``
85
+
raises :exc:`TypeError`. The branch examples below assume an attached HEAD.
86
+
81
87
.. literalinclude:: ../../test/test_docs.py
82
88
:language: python
83
89
:dedent: 8
@@ -152,7 +158,7 @@ Examining References
152
158
:start-after: # [2-test_references_and_objects]
153
159
:end-before: # ![2-test_references_and_objects]
154
160
155
-
A :class:`symbolic reference <git.refs.symbolic.SymbolicReference>` is a special case of a reference as it points to another reference instead of a commit.
161
+
A :class:`symbolic reference <git.refs.symbolic.SymbolicReference>` can point to another reference. When detached, it points directly to a commit instead. Reading its ``commit`` property resolves the commit in either state. Assigning a commit to ``reference`` detaches it; reading ``reference`` then raises :exc:`TypeError`.
0 commit comments