@@ -251,6 +251,17 @@ def update(self, recursive=False, init=True):
251251 local_branch = git .Head (mrepo , git .Head .to_full_path (self .branch ))
252252 if not local_branch .is_valid ():
253253 mrepo .git .checkout (remote_branch , b = self .branch )
254+ else :
255+ # have a valid branch, but no checkout - make sure we can figure
256+ # that out by marking the commit with a null_sha
257+ # have to write it directly as .commit = NULLSHA tries to resolve the sha
258+ ref = mrepo .head .ref
259+ refpath = join_path_native (mrepo .git_dir , ref .to_full_path (ref .path ))
260+ refdir = os .path .dirname (refpath )
261+ if not os .path .isdir (refdir ):
262+ os .makedirs (refdir )
263+ #END handle directory
264+ open (refpath , 'w' ).write (self .NULL_HEX_SHA )
254265 # END initial checkout + branch creation
255266 # make sure we are not detached
256267 mrepo .head .ref = local_branch
@@ -259,24 +270,24 @@ def update(self, recursive=False, init=True):
259270 #END handle tracking branch
260271 #END handle initalization
261272
262- # if the commit to checkout is on the current branch, merge the branch
263- if mrepo .head .is_detached :
264- if mrepo .head .commit . binsha != self . binsha :
273+ # update the working tree
274+ if mrepo .head .commit . binsha != self . binsha :
275+ if mrepo .head .is_detached :
265276 mrepo .git .checkout (self .hexsha )
266- # END checkout commit
267- else :
268- # TODO: allow to specify a rebase, merge, or reset
269- # TODO: Warn if the hexsha forces the tracking branch off the remote
270- # branch - this should be prevented when setting the branch option
271- mrepo . head . reset ( self . hexsha , index = True , working_tree = True )
272- # END handle checkout
273-
274- if recursive :
275- for submodule in self . iter_items ( self . module ()):
276- submodule . update ( recursive , init )
277- # END handle recursive update
278- # END for each submodule
279-
277+ else :
278+ # TODO: allow to specify a rebase, merge, or reset
279+ # TODO: Warn if the hexsha forces the tracking branch off the remote
280+ # branch - this should be prevented when setting the branch option
281+ mrepo . head . reset ( self . hexsha , index = True , working_tree = True )
282+ # END handle checkout
283+
284+ if recursive :
285+ for submodule in self . iter_items ( self . module ()) :
286+ submodule . update ( recursive , init )
287+ # END handle recursive update
288+ # END for each submodule
289+ # END update to new commit only if needed
290+
280291 return self
281292
282293 def set_parent_commit (self , commit , check = True ):
@@ -354,6 +365,15 @@ def module(self):
354365 def module_path (self ):
355366 """:return: full path to the root of our module. It is relative to the filesystem root"""
356367 return join_path_native (self .repo .working_tree_dir , self .path )
368+
369+ def module_exists (self ):
370+ """:return: True if our module exists and is a valid git repository. See module() method"""
371+ try :
372+ self .module ()
373+ return True
374+ except InvalidGitRepositoryError :
375+ return False
376+ # END handle exception
357377
358378 @property
359379 def branch (self ):
@@ -391,6 +411,12 @@ def config_reader(self):
391411 :raise IOError: If the .gitmodules file/blob could not be read"""
392412 return self ._config_parser_constrained (read_only = True )
393413
414+ def children (self ):
415+ """:return: IterableList(Submodule, ...) an iterable list of submodules instances
416+ which are children of this submodule
417+ :raise InvalidGitRepositoryError: if the submodule is not checked-out"""
418+ return self ._get_intermediate_items (self )
419+
394420 #} END query interface
395421
396422 #{ Iterable Interface
0 commit comments