Skip to content

Commit a9c7cce

Browse files
joriojdavid
authored andcommitted
Fix Submodule.head_id if submodule isn't in superproject HEAD tree
1 parent 7bea492 commit a9c7cce

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

pygit2/submodules.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,17 @@ def branch(self):
148148
return ffi.string(branch).decode('utf-8')
149149

150150
@property
151-
def head_id(self):
152-
"""Head of the submodule."""
151+
def head_id(self) -> Union[Oid, None]:
152+
"""
153+
The submodule's HEAD commit id (as recorded in the superproject's
154+
current HEAD tree).
155+
Returns None if the superproject's HEAD doesn't contain the submodule.
156+
"""
157+
153158
head = C.git_submodule_head_id(self._subm)
154-
return Oid(raw=bytes(ffi.buffer(head)[:]))
159+
if head == ffi.NULL:
160+
return None
161+
return Oid(raw=bytes(ffi.buffer(head.id)[:]))
155162

156163

157164
class SubmoduleCollection:

test/test_submodule.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,24 @@ def test_head_id(repo):
210210
assert repo.submodules[SUBM_PATH].head_id == SUBM_HEAD_SHA
211211

212212

213+
@utils.requires_network
214+
def test_head_id_null(repo):
215+
gitmodules_newlines = (
216+
'\n'
217+
'[submodule "uncommitted_submodule"]\n'
218+
' path = pygit2\n'
219+
' url = https://github.com/libgit2/pygit2\n'
220+
'\n'
221+
)
222+
with open(Path(repo.workdir, '.gitmodules'), 'a') as f:
223+
f.write(gitmodules_newlines)
224+
225+
subm = repo.submodules['uncommitted_submodule']
226+
227+
# The submodule isn't in the HEAD yet, so head_id should be None
228+
assert subm.head_id is None
229+
230+
213231
@utils.requires_network
214232
@pytest.mark.parametrize('depth', [0, 1])
215233
def test_add_submodule(repo, depth):

0 commit comments

Comments
 (0)