Commits

Ludovic Chabant committed 5ea3f75

Exposed new environment variable with the subrepo type.
Updated test.
Added a test for mixed subrepo cases.

Comments (0)

Files changed (3)

     ``HG_SUBSTATE``:
         State of the current subrepository as specified in the
         containing repository's ``.hgsubstate`` file.
+
+    ``HG_SUBTYPE``:
+        The type of the current subrepository (hg, git or svn).
     """
 
     # function level "constants" - these won't be modified by the nested functions
     postcmd = None
     includeroot = opts.get('root_repo')
 
-    def execCmd(sub, cmd):
+    def execCmd(sub, cmd, kind):
         """if sub == None, cmd is executed inside repo; else, inside sub.
         If cmd == None, do nothing. If cmd == '', do only the print0 (if needed). 
         Else, do either print0 or the debugging message, then execute the command.
+        kind is the type of the (sub)repo.
         """
         if sub == None:
             envargdict = dict(HG_SUBPATH='.',
                               HG_SUBURL='.',
                               HG_SUBSTATE=repo['.'].hex(),
-                              HG_REPO=repo.root)
+                              HG_REPO=repo.root,
+                              HG_SUBTYPE=kind)
             relpath = '.'
             cmdwd = repo.root
         else:
             envargdict = dict(HG_SUBPATH=relpath,
                               HG_SUBURL=sub._path,
                               HG_SUBSTATE=sub._state[1],
-                              HG_REPO=repo.root)
+                              HG_REPO=repo.root,
+                              HG_SUBTYPE=kind)
             cmdwd = os.path.join(repo.root, relpath)
         if cmd != None:
             if print0:
     def bfs():
         """execute precmd in repo.root and in each subrepository, breadth-first"""
         if includeroot:
-            execCmd(None, precmd) 
+            execCmd(None, precmd, 'hg') 
         ctx = repo['.']
-        work = [(1, ctx.sub(subpath)) for subpath in sorted(ctx.substate)]
+        work = [(1, ctx.sub(subpath), ctx.substate[subpath][2]) for subpath in sorted(ctx.substate)]
         while work:
-            (depth, sub) = work.pop(0)
+            (depth, sub, kind) = work.pop(0)
             if depth > maxdepth >= 0:
                 continue
-            execCmd(sub, precmd) 
-            if isinstance(sub, subrepo.hgsubrepo):
+            execCmd(sub, precmd, kind) 
+            if kind == 'hg':
                 rev = sub._state[1]
                 ctx = sub._repo[rev]
-                w = [(depth + 1, ctx.sub(subpath)) 
+                w = [(depth + 1, ctx.sub(subpath), ctx.substate[subpath][2]) 
                      for subpath in sorted(ctx.substate)]
                 work.extend(w)
     
     def dfs():
         """execute pre-/postcmd in repo.root and in each subrepository, depth-first"""
 
-        def dfs_rek(depth, sub):
+        def dfs_rek(depth, sub, kind):
             if depth > maxdepth >= 0:
                 return
-            execCmd(sub, precmd) 
-            if isinstance(sub, subrepo.hgsubrepo):
+            execCmd(sub, precmd, kind) 
+            if kind == 'hg':
                 rev = sub._state[1]
                 ctx = sub._repo[rev]
                 for subpath in sorted(ctx.substate):
-                    dfs_rek(depth+1, ctx.sub(subpath))
-            execCmd(sub, postcmd)
+                    dfs_rek(depth+1, ctx.sub(subpath), ctx.substate[subpath][2])
+            execCmd(sub, postcmd, kind)
     
         ctx = repo['.']
-        work = [ctx.sub(subpath) for subpath in sorted(ctx.substate)]
+        work = [(ctx.sub(subpath), ctx.substate[subpath][2]) for subpath in sorted(ctx.substate)]
         if includeroot:
-            execCmd(None, precmd) 
-        for sub in work:
-            dfs_rek(1, sub)
+            execCmd(None, precmd, 'hg') 
+        for (sub, kind) in work:
+            dfs_rek(1, sub, kind)
         if includeroot:
-            execCmd(None, postcmd) 
+            execCmd(None, postcmd, 'hg') 
         
     ### start of main function part ###
     if len(args) == 2:
+Load extension:
+
+  $ echo "[extensions]" >> $HGRCPATH
+  $ echo "onsub = $TESTDIR/onsub.py" >> $HGRCPATH
+
+Create some nicely nested subrepositories with mixed types:
+
+  $ hg init
+  $ for d in a b; do hg init $d; echo "$d = $d" >> .hgsub; done
+  $ git init -q git-i
+  $ cd git-i
+  $ git config core.autocrlf false
+  $ echo something > something
+  $ git add something
+  $ git commit -q -m init
+  $ cd ..
+  $ echo "git-i = [git]$git-i" >> .hgsub
+  $ hg add .hgsub
+
+  $ cd a
+
+  $ git init -q git-j
+  $ cd git-j
+  $ git config core.autocrlf false
+  $ echo something > something
+  $ git add something
+  $ git commit -q -m init
+  $ cd ..
+  $ echo "git-j = [git]git-j" >> .hgsub
+  $ hg add .hgsub
+
+  $ cd ..
+
+  $ hg commit -m init -S
+  committing subrepository a
+  committing subrepository a/git-j
+  committing subrepository b
+  committing subrepository git-i
+
+Test the subrepo type
+
+  $ hg onsub 'echo $HG_SUBPATH = $HG_SUBTYPE'
+  a = hg
+  a/git-j = git
+  b = hg
+  git-i = git
+
+Test the subrepo type including the root repository
+
+  $ hg onsub 'echo $HG_SUBPATH = $HG_SUBTYPE' --root-repo
+  . = hg
+  a = hg
+  a/git-j = git
+  b = hg
+  git-i = git
+
           State of the current subrepository as specified in the containing
           repository's ".hgsubstate" file.
   
+      "HG_SUBTYPE":
+          The type of the current subrepository (hg, git or svn).
+  
   options:
   
    -b --breadth-first use breadth-first traversal