@@ -109,7 +109,12 @@ def __init__(self, repo, binsha, mode=None, path=None, name=None, parent_commit=
109109 def _set_cache_ (self , attr ):
110110 if attr == '_parent_commit' :
111111 # set a default value, which is the root tree of the current head
112- self ._parent_commit = self .repo .commit ()
112+ try :
113+ self ._parent_commit = self .repo .commit ()
114+ except ValueError :
115+ # This fails in an empty repository.
116+ self ._parent_commit = None
117+ # end exception handling
113118 elif attr in ('path' , '_url' , '_branch_path' ):
114119 reader = self .config_reader ()
115120 # default submodule values
@@ -163,7 +168,13 @@ def _config_parser(cls, repo, parent_commit, read_only):
163168 :raise IOError: If the .gitmodules file cannot be found, either locally or in the repository
164169 at the given parent commit. Otherwise the exception would be delayed until the first
165170 access of the config parser"""
166- parent_matches_head = repo .head .commit == parent_commit
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+ parent_matches_head = True
176+ # end
177+
167178 if not repo .bare and parent_matches_head :
168179 fp_module = os .path .join (repo .working_tree_dir , cls .k_modules_file )
169180 else :
@@ -370,6 +381,14 @@ def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
370381 mrepo = cls ._clone_repo (repo , url , path , name , ** kwargs )
371382 # END verify url
372383
384+ # It's important to add the URL to the parent config, to let `git submodule` know.
385+ # otherwise there is a '-' character in front of the submodule listing
386+ # a38efa84daef914e4de58d1905a500d8d14aaf45 mymodule (v0.9.0-1-ga38efa8)
387+ # -a38efa84daef914e4de58d1905a500d8d14aaf45 submodules/intermediate/one
388+ writer = sm .repo .config_writer ()
389+ writer .set_value (sm_section (name ), 'url' , url )
390+ writer .release ()
391+
373392 # update configuration and index
374393 index = sm .repo .index
375394 writer = sm .config_writer (index = index , write = False )
@@ -386,11 +405,23 @@ def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
386405 del (writer )
387406
388407 # we deliberatly assume that our head matches our index !
389- pcommit = repo .head .commit
390- sm ._parent_commit = pcommit
408+ parent_repo_is_empty = False
409+ try :
410+ sm ._parent_commit = repo .head .commit
411+ except ValueError :
412+ parent_repo_is_empty = True
413+ # Can't set this yet, if the parent repo is empty.
414+ # end
391415 sm .binsha = mrepo .head .commit .binsha
392416 index .add ([sm ], write = True )
393417
418+ if parent_repo_is_empty :
419+ # The user is expected to make a commit, and this submodule will initialize itself when
420+ # _parent_commit is required
421+ del sm ._parent_commit
422+ log .debug ("Will not set _parent_commit now as the parent repository has no commit yet." )
423+ # end
424+
394425 return sm
395426
396427 def update (self , recursive = False , init = True , to_latest_revision = False , progress = None ,
@@ -875,6 +906,49 @@ def config_writer(self, index=None, write=True):
875906 writer .config ._auto_write = write
876907 return writer
877908
909+ @unbare_repo
910+ def rename (self , new_name ):
911+ """Rename this submodule
912+ :note: This method takes care of renaming the submodule in various places, such as
913+
914+ * $parent_git_dir/config
915+ * $working_tree_dir/.gitmodules
916+ * (git >=v1.8.0: move submodule repository to new name)
917+
918+ As .gitmodules will be changed, you would need to make a commit afterwards. The changed .gitmodules file
919+ will already be added to the index
920+
921+ :return: this submodule instance
922+ """
923+ if self .name == new_name :
924+ return self
925+
926+ # .git/config
927+ pw = self .repo .config_writer ()
928+ # As we ourselves didn't write anything about submodules into the parent .git/config, we will not require
929+ # it to exist, and just ignore missing entries
930+ if pw .has_section (sm_section (self .name )):
931+ pw .rename_section (sm_section (self .name ), sm_section (new_name ))
932+ # end
933+ pw .release ()
934+
935+ # .gitmodules
936+ cw = self .config_writer ().config
937+ cw .rename_section (sm_section (self .name ), sm_section (new_name ))
938+ cw .release ()
939+
940+ self ._name = new_name
941+
942+ # .git/modules
943+ mod = self .module ()
944+ if mod .has_separate_working_tree ():
945+ module_abspath = self ._module_abspath (self .repo , self .path , new_name )
946+ os .renames (mod .git_dir , module_abspath )
947+ self ._write_git_file_and_module_config (mod .working_tree_dir , module_abspath )
948+ # end move separate git repository
949+
950+ return self
951+
878952 #} END edit interface
879953
880954 #{ Query Interface
0 commit comments