Skip to content

Commit

Permalink
Fix Submodule.head_id if submodule isn't in superproject HEAD tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio authored and jdavid committed Jun 3, 2024
1 parent 7bea492 commit a9c7cce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pygit2/submodules.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,17 @@ def branch(self):
return ffi.string(branch).decode('utf-8')

@property
def head_id(self):
"""Head of the submodule."""
def head_id(self) -> Union[Oid, None]:
"""
The submodule's HEAD commit id (as recorded in the superproject's
current HEAD tree).
Returns None if the superproject's HEAD doesn't contain the submodule.
"""

head = C.git_submodule_head_id(self._subm)
return Oid(raw=bytes(ffi.buffer(head)[:]))
if head == ffi.NULL:
return None
return Oid(raw=bytes(ffi.buffer(head.id)[:]))


class SubmoduleCollection:
Expand Down
18 changes: 18 additions & 0 deletions test/test_submodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,24 @@ def test_head_id(repo):
assert repo.submodules[SUBM_PATH].head_id == SUBM_HEAD_SHA


@utils.requires_network
def test_head_id_null(repo):
gitmodules_newlines = (
'\n'
'[submodule "uncommitted_submodule"]\n'
' path = pygit2\n'
' url = https://github.com/libgit2/pygit2\n'
'\n'
)
with open(Path(repo.workdir, '.gitmodules'), 'a') as f:
f.write(gitmodules_newlines)

subm = repo.submodules['uncommitted_submodule']

# The submodule isn't in the HEAD yet, so head_id should be None
assert subm.head_id is None


@utils.requires_network
@pytest.mark.parametrize('depth', [0, 1])
def test_add_submodule(repo, depth):
Expand Down

0 comments on commit a9c7cce

Please sign in to comment.