@@ -98,8 +98,7 @@ def __init__(self, repo, binsha, mode=None, path=None, name=None, parent_commit=
9898 :param branch_path: full (relative) path to ref to checkout when cloning the remote repository"""
9999super (Submodule ,self ).__init__ (repo ,binsha ,mode ,path )
100100self .size = 0
101- if parent_commit is not None :
102- self ._parent_commit = parent_commit
101+ self ._parent_commit = parent_commit
103102if url is not None :
104103self ._url = url
105104if branch_path is not None :
@@ -109,18 +108,15 @@ def __init__(self, repo, binsha, mode=None, path=None, name=None, parent_commit=
109108self ._name = name
110109
111110def _set_cache_ (self ,attr ):
112- if attr == '_parent_commit' :
113- # set a default value, which is the root tree of the current head
114- try :
115- self ._parent_commit = self .repo .commit ()
116- except ValueError :
117- # This fails in an empty repository.
118- self ._parent_commit = None
119- # end exception handling
120- elif attr in ('path' ,'_url' ,'_branch_path' ):
111+ if attr in ('path' ,'_url' ,'_branch_path' ):
121112reader = self .config_reader ()
122113# default submodule values
123- self .path = reader .get_value ('path' )
114+ try :
115+ self .path = reader .get_value ('path' )
116+ except cp .NoSectionError :
117+ raise ValueError ("This submodule instance does not exist anymore in '%s' file"
118+ % os .path .join (self .repo .working_tree_dir ,'.gitmodules' ))
119+ # end
124120self ._url = reader .get_value ('url' )
125121# git-python extension values - optional
126122self ._branch_path = reader .get_value (self .k_head_option ,git .Head .to_full_path (self .k_head_default ))
@@ -170,16 +166,19 @@ def _config_parser(cls, repo, parent_commit, read_only):
170166 :raise IOError: If the .gitmodules file cannot be found, either locally or in the repository
171167 at the given parent commit. Otherwise the exception would be delayed until the first
172168 access of the config parser"""
173- try :
174- parent_matches_head = repo .head .commit == parent_commit
175- except ValueError :
176- # We are most likely in an empty repository, so the HEAD doesn't point to a valid ref
177- parent_matches_head = True
178- # end
169+ parent_matches_head = True
170+ if parent_commit is not None :
171+ try :
172+ parent_matches_head = repo .head .commit == parent_commit
173+ except ValueError :
174+ # We are most likely in an empty repository, so the HEAD doesn't point to a valid ref
175+ pass
176+ # end hanlde parent_commit
179177
180178if not repo .bare and parent_matches_head :
181179fp_module = os .path .join (repo .working_tree_dir ,cls .k_modules_file )
182180else :
181+ assert parent_commit is not None ,"need valid parent_commit in bare repositories"
183182try :
184183fp_module = cls ._sio_modules (parent_commit )
185184except KeyError :
@@ -213,7 +212,12 @@ def _sio_modules(cls, parent_commit):
213212
214213def _config_parser_constrained (self ,read_only ):
215214""":return: Config Parser constrained to our submodule in read or write mode"""
216- parser = self ._config_parser (self .repo ,self ._parent_commit ,read_only )
215+ try :
216+ pc = self .parent_commit
217+ except ValueError :
218+ pc = None
219+ # end hande empty parent repository
220+ parser = self ._config_parser (self .repo ,pc ,read_only )
217221parser .set_submodule (self )
218222return SectionConstraint (parser ,sm_section (self .name ))
219223
@@ -407,9 +411,10 @@ def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
407411del (writer )
408412
409413# we deliberatly assume that our head matches our index !
410- parent_repo_is_empty = False
414+
411415try :
412- sm ._parent_commit = repo .head .commit
416+ repo .head .commit
417+ parent_repo_is_empty = False
413418except ValueError :
414419parent_repo_is_empty = True
415420# Can't set this yet, if the parent repo is empty.
@@ -418,9 +423,6 @@ def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
418423index .add ([sm ],write = True )
419424
420425if parent_repo_is_empty :
421- # The user is expected to make a commit, and this submodule will initialize itself when
422- # _parent_commit is required
423- del sm ._parent_commit
424426log .debug ("Will not set _parent_commit now as the parent repository has no commit yet." )
425427# end
426428
@@ -885,13 +887,18 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
885887def set_parent_commit (self ,commit ,check = True ):
886888"""Set this instance to use the given commit whose tree is supposed to
887889 contain the .gitmodules blob.
888- :param commit: Commit'ish reference pointing at the root_tree, or None always point to the most recent commit
890+ :param commit: Commit'ish reference pointing at the root_tree, or None to always point to the
891+ most recent commit
889892 :param check: if True, relatively expensive checks will be performed to verify
890893 validity of the submodule.
891894 :raise ValueError: if the commit's tree didn't contain the .gitmodules blob.
892895 :raise ValueError: if the parent commit didn't store this submodule under the
893896 current path
894897 :return: self"""
898+ if commit is None :
899+ self ._parent_commit = None
900+ return self
901+ # end handle None
895902pcommit = self .repo .commit (commit )
896903pctree = pcommit .tree
897904if self .k_modules_file not in pctree :
@@ -1036,7 +1043,7 @@ def exists(self):
10361043if hasattr (self ,attr ):
10371044loc [attr ]= getattr (self ,attr )
10381045# END if we have the attribute cache
1039- except cp .NoSectionError :
1046+ except ( cp .NoSectionError , ValueError ) :
10401047# on PY3, this can happen apparently ... don't know why this doesn't happen on PY2
10411048pass
10421049# END for each attr
@@ -1086,6 +1093,8 @@ def url(self):
10861093def parent_commit (self ):
10871094""":return: Commit instance with the tree containing the .gitmodules file
10881095 :note: will always point to the current head's commit if it was not set explicitly"""
1096+ if self ._parent_commit is None :
1097+ return self .repo .commit ()
10891098return self ._parent_commit
10901099
10911100@property
@@ -1157,7 +1166,9 @@ def iter_items(cls, repo, parent_commit='HEAD'):
11571166
11581167# fill in remaining info - saves time as it doesn't have to be parsed again
11591168sm ._name = n
1160- sm ._parent_commit = pc
1169+ if pc != repo .commit ():
1170+ sm ._parent_commit = pc
1171+ # end set only if not most recent !
11611172sm ._branch_path = git .Head .to_full_path (b )
11621173sm ._url = u
11631174