Commits

Anonymous committed a33f606

reworked to use lambda functions for all subrepo operations

Comments (0)

Files changed (1)

 
     if optList:
         ui.status("listing subrepos:\n-------\n")
-        listSubrepos(ui, repo, "", optNoRecurse)
+        func = lambda repoPath, remotePath: ListRepo(ui, repoPath, remotePath)
+        doCommand(ui, repo, func, False, optNoRecurse)
         ui.status("-------\n")
 
     if optReclone:
         ui.status("checking for missing subrepo clones...\n")
-        recloneSubrepoRecursive(ui, repo, "", optNoRecurse)
+        func = lambda repoPath, remotePath: doReclone(ui, repoPath, remotePath)
+        doCommand(ui, repo, func, True, optNoRecurse)
         ui.status("finishing recloning\n")
 
     if optPull:
         ui.status("pulling all subrepos...\n")
-        doCommand(ui, repo, "", "pull", True, optNoRecurse)
+        func = lambda repoPath, remotePath: doHgTextCommand(ui, repoPath, remotePath, "pull")
+        doCommand(ui, repo, func, True, optNoRecurse)
         ui.status("---------------------------\n")
 
     if optUpdate:
         ui.status("updating all subrepos to tip, watch output for necessity of user intervention...\n")
-        doCommand(ui, repo, "", "update", True, optNoRecurse)
+        func = lambda repoPath, remotePath: doHgTextCommand(ui, repoPath, remotePath, "update")
+        doCommand(ui, repo, func, True, optNoRecurse)
         ui.status("---------------------------\n")
 
     if optIncoming:
         ui.status("getting incoming changesets for all subrepos\n")
-        doCommand(ui, repo, "", "incoming", False, optNoRecurse)
+        func = lambda repoPath, remotePath: doHgTextCommand(ui, repoPath, remotePath, "incoming")
+        doCommand(ui, repo, func, False, optNoRecurse)
         ui.status("---------------------------\n")
 
     if optOutgoing:
         ui.status("getting outgoing changesets for all subrepos\n")
-        doCommand(ui, repo, "", "outgoing", False, optNoRecurse)
+        func = lambda repoPath, remotePath: doHgTextCommand(ui, repoPath, remotePath, "outgoing")
+        doCommand(ui, repo, func, False, optNoRecurse)
         ui.status("---------------------------\n")
 
     if optFetch:
         ui.status("fetching all subrepos, watch output for necessity of user intervention...\n")
-        doCommand(ui, repo, "", "fetch", True, optNoRecurse)
+        func = lambda repoPath, remotePath: doHgTextCommand(ui, repoPath, remotePath, "fetch")
+        doCommand(ui, repo, func, True, optNoRecurse)
         ui.status("---------------------------\n")
         ui.status("finished fetching, be sure to commit parent repo to update .hgsubstate\n")
 
     if optStatus:
         ui.status("getting status for all subrepos\n")
-        doCommand(ui, repo, "", "status", False, optNoRecurse)
+        func = lambda repoPath, remotePath: doHgTextCommand(ui, repoPath, remotePath, "status")
+        doCommand(ui, repo, func, False, optNoRecurse)
         ui.status("---------------------------\n")
 
 
-def doCommand(ui, repo, relativePath, command, cloneMissing, noRecurse):
+# execute a function for each subrepo with optional recloning and optional recursion
+# func is defined as func(localPath, remotePath)
+def doCommand(ui, repo, func, cloneMissing, noRecurse, relativePath=""):
     if os.path.exists(os.path.join(repo.root, ".hgsub")):
         for local, remote in getSubreposFromHgsub(repo):
             subrepoPath = os.path.join(relativePath, local)
+            if not os.path.exists(subrepoPath) and cloneMissing:
+                recloneSubrepo(ui, subrepoPath, remote)
             if os.path.exists(subrepoPath):
-                ui.status("---------------------------\n")
-                ui.status( "* %s\n" % subrepoPath)
-                pout = util.popen("cd " + subrepoPath + " && hg " + command + " && cd ..")
-                ui.status(pout.read())
+                func(subrepoPath, remote)
                 if not noRecurse:
-                    doCommand(ui, hg.repository(ui, subrepoPath, False), subrepoPath, command, cloneMissing, noRecurse)
-            elif cloneMissing:
-                recloneSubrepo(ui, subrepoPath, remote)
+                    doCommand(ui, hg.repository(ui, subrepoPath, False), func, cloneMissing, noRecurse, subrepoPath)
             else:
                 ui.status("* %s is missing (perhaps you should reclone)\n" % subrepoPath)
 
+
+
+def doHgTextCommand(ui, repoPath, remotePath, commandText):
+    ui.status("---------------------------\n")
+    ui.status("* %s\n" % repoPath)
+    currentwd = os.getcwd()
+    os.chdir(repoPath)
+    pout = util.popen("hg %s" % commandText)
+    ui.status(pout.read())
+    os.chdir(currentwd)
+
+
+def ListRepo(ui, repoPath, remotePath):
+    ui.status("* %s\t@ %s\n" %(repoPath, remotePath))
+
+
+def doReclone(ui, repoPath, remotePath):
+    return
+
+
 def getSubreposFromHgsub(repo):
     # XXX arguably this could, or should use:
     #  mercurial.subrepo.state(repo['.'])
     f = repo.wopener('.hgsub')
     return [map(string.strip, line.split('=')) for line in f]
 
-def listSubrepos(ui, repo, relativePath, noRecurse):
-    if os.path.exists(os.path.join(repo.root, ".hgsub")):
-        for local, remote in getSubreposFromHgsub(repo):
-            subrepoPath = os.path.join(relativePath, local)
-            if os.path.exists(subrepoPath):
-                ui.status( "* %s\t@ %s\n" % (subrepoPath, remote))
-                if not noRecurse:
-                    listSubrepos(ui, hg.repository(ui, subrepoPath, False), subrepoPath, noRecurse)
-            else:
-                ui.status( "* %s is missing (perhaps you should reclone)\n" % subrepoPath)
-
-
-def recloneSubrepoRecursive(ui, repo, relativePath, noRecurse):
-    if os.path.exists(os.path.join(repo.root, ".hgsub")):
-        for local, remote in getSubreposFromHgsub(repo):
-            subrepoPath = os.path.join(relativePath, local)
-            if os.path.exists(subrepoPath):
-                ui.status("* %s exists\n" % subrepoPath)
-            else:
-                recloneSubrepo(ui, subrepoPath, remote)
-            if not noRecurse:
-                recloneSubrepoRecursive(ui, hg.repository(ui, subrepoPath, False), subrepoPath, noRecurse)
 
 def recloneSubrepo(ui, local, remote):
     # todo: clone at the revision specified in .hgsubstate?
     ui.status("* %s is missing, recloning...\n" % local);
     hg.clone(ui, remote, dest=local)
 
+
 # Macro extension meta-data
 cmdtable = {
     "subrepo":