@@ -60,8 +60,9 @@ def status(self):
6060 if not os .path .exists (os .path .join (smpath , ".git" )):
6161 rootgit = GitInterface (self .root_dir , self .logger )
6262 # submodule commands use path, not name
63- tags = rootgit .git_operation ("ls-remote" , "--tags" , self .url )
64- result = rootgit .git_operation ("submodule" ,"status" ,smpath ).split ()
63+ status , tags = rootgit .git_operation ("ls-remote" , "--tags" , self .url )
64+ status , result = rootgit .git_operation ("submodule" ,"status" ,smpath )
65+ result = result .split ()
6566
6667 if result :
6768 ahash = result [0 ][1 :]
@@ -80,9 +81,9 @@ def status(self):
8081 result = f"e { self .name :>20} not checked out, aligned at tag { self .fxtag } { optional } "
8182 needsupdate = True
8283 elif self .fxtag :
83- ahash = rootgit .git_operation (
84+ status , ahash = rootgit .git_operation (
8485 "submodule" , "status" , "{}" .format (self .path )
85- ). rstrip ()
86+ )
8687 ahash = ahash [1 : len (self .fxtag ) + 1 ]
8788 if self .fxtag == ahash :
8889 result = f"e { self .name :>20} not checked out, aligned at hash { ahash } { optional } "
@@ -96,14 +97,15 @@ def status(self):
9697 else :
9798 with utils .pushd (smpath ):
9899 git = GitInterface (smpath , self .logger )
99- remote = git .git_operation ("remote" ). rstrip ( )
100+ status , remote = git .git_operation ("remote" )
100101 if remote == '' :
101102 result = f"e { self .name :>20} has no associated remote"
102103 testfails = True
103104 needsupdate = True
104105 return result , needsupdate , localmods , testfails
105- rurl = git .git_operation ("ls-remote" ,"--get-url" ).rstrip ()
106- line = git .git_operation ("log" , "--pretty=format:\" %h %d\" " ).partition ('\n ' )[0 ]
106+ status , rurl = git .git_operation ("ls-remote" ,"--get-url" )
107+ status , lines = git .git_operation ("log" , "--pretty=format:\" %h %d\" " )
108+ line = lines .partition ('\n ' )[0 ]
107109 parts = line .split ()
108110 ahash = parts [0 ][1 :]
109111 atag = None
@@ -120,7 +122,7 @@ def status(self):
120122
121123
122124 #print(f"line is {line} ahash is {ahash} atag is {atag} {parts}")
123- # atag = git.git_operation("describe", "--tags", "--always").rstrip()
125+ # atag = git.git_operation("describe", "--tags", "--always")
124126 # ahash = git.git_operation("rev-list", "HEAD").partition("\n")[0]
125127
126128 recurse = False
@@ -149,10 +151,10 @@ def status(self):
149151 result = f"e { self .name :>20} has no fxtag defined in .gitmodules, module at { ahash } "
150152 testfails = False
151153
152- status = git .git_operation ("status" , "--ignore-submodules" , "-uno" )
153- if "nothing to commit" not in status :
154+ status , output = git .git_operation ("status" , "--ignore-submodules" , "-uno" )
155+ if "nothing to commit" not in output :
154156 localmods = True
155- result = "M" + textwrap .indent (status , " " )
157+ result = "M" + textwrap .indent (output , " " )
156158# print(f"result {result} needsupdate {needsupdate} localmods {localmods} testfails {testfails}")
157159 return result , needsupdate , localmods , testfails
158160
@@ -171,10 +173,11 @@ def _add_remote(self, git):
171173 Returns:
172174 str: The name of the new remote if added, or the name of the existing remote that matches the submodule's URL.
173175 """
174- remotes = git .git_operation ("remote" , "-v" ).splitlines ()
176+ status , remotes = git .git_operation ("remote" , "-v" )
177+ remotes = remotes .splitlines ()
175178 upstream = None
176179 if remotes :
177- upstream = git .git_operation ("ls-remote" , "--get-url" ). rstrip ( )
180+ status , upstream = git .git_operation ("ls-remote" , "--get-url" )
178181 newremote = "newremote.00"
179182 tmpurl = self .
url .
replace (
"[email protected] :" ,
"https://github.com/" )
180183 line = next ((s for s in remotes if self .url in s or tmpurl in s ), None )
@@ -183,7 +186,7 @@ def _add_remote(self, git):
183186 return newremote
184187 else :
185188 i = 0
186- while " newremote" in remotes :
189+ while newremote in remotes :
187190 i = i + 1
188191 newremote = f"newremote.{ i :02d} "
189192 else :
@@ -214,12 +217,19 @@ def sparse_checkout(self):
214217 """
215218 self .logger .info ("Called sparse_checkout for {}" .format (self .name ))
216219 rgit = GitInterface (self .root_dir , self .logger )
217- superroot = rgit .git_operation ("rev-parse" , "--show-superproject-working-tree" )
220+ status , superroot = rgit .git_operation ("rev-parse" , "--show-superproject-working-tree" )
218221 if superroot :
219222 gitroot = superroot .strip ()
220223 else :
221- gitroot = self .root_dir .strip ()
222- assert os .path .isdir (os .path .join (gitroot , ".git" ))
224+ gitroot = self .root_dir
225+ # Now need to move the .git dir to the submodule location
226+ rootdotgit = os .path .join (self .root_dir , ".git" )
227+ while os .path .isfile (rootdotgit ):
228+ with open (rootdotgit ) as f :
229+ line = f .readline ().rstrip ()
230+ if line .startswith ("gitdir: " ):
231+ rootdotgit = os .path .abspath (os .path .join (self .root_dir ,line [8 :]))
232+ assert os .path .isdir (rootdotgit )
223233 # first create the module directory
224234 if not os .path .isdir (os .path .join (self .root_dir , self .path )):
225235 os .makedirs (os .path .join (self .root_dir , self .path ))
@@ -244,8 +254,8 @@ def sparse_checkout(self):
244254 # set the repository remote
245255
246256 self .logger .info ("Setting remote origin in {}/{}" .format (self .root_dir , self .path ))
247- status = sprepo_git .git_operation ("remote" , "-v" )
248- if self .url not in status :
257+ status , remotes = sprepo_git .git_operation ("remote" , "-v" )
258+ if self .url not in remotes :
249259 sprepo_git .git_operation ("remote" , "add" , "origin" , self .url )
250260
251261 topgit = os .path .join (gitroot , ".git" )
@@ -256,46 +266,46 @@ def sparse_checkout(self):
256266 os .path .join (self .root_dir , f .read ().split ()[1 ]),
257267 start = os .path .join (self .root_dir , self .path ),
258268 )
259- topgit = os .path .join (gitpath , "modules" )
269+ rootdotgit = os .path .join (gitpath , "modules" , self . name )
260270 else :
261- topgit = os .path .relpath (
262- os .path .join (self .root_dir , ".git" , "modules" ),
271+ rootdotgit = os .path .relpath (
272+ os .path .join (self .root_dir , ".git" , "modules" , self . name ),
263273 start = os .path .join (self .root_dir , self .path ),
264274 )
265275
266- with utils .pushd (sprep_repo ):
267- if not os .path .isdir (topgit ):
268- os .makedirs (topgit )
269- topgit += os .sep + self .name
270-
271276 if os .path .isdir (os .path .join (self .root_dir , self .path , ".git" )):
272277 with utils .pushd (sprep_repo ):
273- if os .path .isdir (os .path .join (topgit ,".git" )):
274- shutil .rmtree (os .path .join (topgit ,".git" ))
275- shutil .move (".git" , topgit )
278+ if os .path .isdir (os .path .join (rootdotgit ,".git" )):
279+ shutil .rmtree (os .path .join (rootdotgit ,".git" ))
280+ shutil .move (".git" , rootdotgit )
276281 with open (".git" , "w" ) as f :
277- f .write ("gitdir: " + os .path .relpath (topgit ))
278- # assert(os.path.isdir(os.path.relpath(topgit, start=sprep_repo)))
279- gitsparse = os .path .abspath (os .path .join (topgit , "info" , "sparse-checkout" ))
282+ f .write ("gitdir: " + os .path .relpath (rootdotgit ))
283+ infodir = os .path .join (rootdotgit , "info" )
284+ if not os .path .isdir (infodir ):
285+ os .makedirs (infodir )
286+ gitsparse = os .path .abspath (os .path .join (infodir , "sparse-checkout" ))
280287 if os .path .isfile (gitsparse ):
281288 self .logger .warning (
282- "submodule {} is already initialized {}" .format (self .name , topgit )
289+ "submodule {} is already initialized {}" .format (self .name , rootdotgit )
283290 )
284291 return
285292
286293 with utils .pushd (sprep_repo ):
287294 if os .path .isfile (self .fxsparse ):
295+
288296 shutil .copy (self .fxsparse , gitsparse )
289297
290298
291299 # Finally checkout the repo
292300 sprepo_git .git_operation ("fetch" , "origin" , "--tags" )
293- sprepo_git .git_operation ("checkout" , self .fxtag )
294-
295- print (f"Successfully checked out { self .name :>20} at { self .fxtag } " )
296- rgit .config_set_value (f'submodule "{ self .name } "' , "active" , "true" )
297- rgit .config_set_value (f'submodule "{ self .name } "' , "url" , self .url )
298- rgit .config_set_value (f'submodule "{ self .name } "' , "path" , self .path )
301+ status ,_ = sprepo_git .git_operation ("checkout" , self .fxtag )
302+ if status :
303+ print (f"Error checking out { self .name :>20} at { self .fxtag } " )
304+ else :
305+ print (f"Successfully checked out { self .name :>20} at { self .fxtag } " )
306+ rgit .config_set_value ('submodule.' + self .name , "active" , "true" )
307+ rgit .config_set_value ('submodule.' + self .name , "url" , self .url )
308+ rgit .config_set_value ('submodule.' + self .name , "path" , self .path )
299309
300310 def update (self ):
301311 """
@@ -342,15 +352,15 @@ def update(self):
342352 git .git_operation ("clone" , self .url , self .path )
343353 smgit = GitInterface (repodir , self .logger )
344354 if not tag :
345- tag = smgit .git_operation ("describe" , "--tags" , "--always" ). rstrip ( )
355+ status , tag = smgit .git_operation ("describe" , "--tags" , "--always" )
346356 smgit .git_operation ("checkout" , tag )
347357 # Now need to move the .git dir to the submodule location
348358 rootdotgit = os .path .join (self .root_dir , ".git" )
349359 if os .path .isfile (rootdotgit ):
350360 with open (rootdotgit ) as f :
351361 line = f .readline ()
352362 if line .startswith ("gitdir: " ):
353- rootdotgit = line [8 :]. rstrip ()
363+ rootdotgit = line [8 :]
354364
355365 newpath = os .path .abspath (os .path .join (self .root_dir , rootdotgit , "modules" , self .name ))
356366 if os .path .exists (newpath ):
@@ -393,15 +403,16 @@ def update(self):
393403 git = GitInterface (submoddir , self .logger )
394404 # first make sure the url is correct
395405 newremote = self ._add_remote (git )
396- tags = git .git_operation ("tag" , "-l" )
406+ status , tags = git .git_operation ("tag" , "-l" )
397407 fxtag = self .fxtag
398408 if fxtag and fxtag not in tags :
399409 git .git_operation ("fetch" , newremote , "--tags" )
400- atag = git .git_operation ("describe" , "--tags" , "--always" ). rstrip ( )
410+ status , atag = git .git_operation ("describe" , "--tags" , "--always" )
401411 if fxtag and fxtag != atag :
402412 try :
403- git .git_operation ("checkout" , fxtag )
404- print (f"{ self .name :>20} updated to { fxtag } " )
413+ status , _ = git .git_operation ("checkout" , fxtag )
414+ if not status :
415+ print (f"{ self .name :>20} updated to { fxtag } " )
405416 except Exception as error :
406417 print (error )
407418
0 commit comments