File tree Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -148,10 +148,17 @@ def branch(self):
148
148
return ffi .string (branch ).decode ('utf-8' )
149
149
150
150
@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
+
153
158
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 )[:]))
155
162
156
163
157
164
class SubmoduleCollection :
Original file line number Diff line number Diff line change @@ -210,6 +210,24 @@ def test_head_id(repo):
210
210
assert repo .submodules [SUBM_PATH ].head_id == SUBM_HEAD_SHA
211
211
212
212
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
+
213
231
@utils .requires_network
214
232
@pytest .mark .parametrize ('depth' , [0 , 1 ])
215
233
def test_add_submodule (repo , depth ):
You can’t perform that action at this time.
0 commit comments