Commits

Greg Ward committed ecc4fd1

Add --max-depth option to limit depth of recursion.

  • Participants
  • Parent commits a2ad755

Comments (0)

Files changed (2)

         containing repository's ``.hgsubstate`` file.
     """
     cmd = ' '.join(args)
-    foreach(ui, repo, cmd, not opts.get('breadth_first'), opts.get('print0'),
+    foreach(ui, repo, cmd, 
+            not opts.get('breadth_first'),
+            opts.get('max_depth'),
+            opts.get('print0'),
             opts.get('ignore_errors'))
 
-def foreach(ui, repo, cmd, depthfirst, print0, ignoreerrors):
+def foreach(ui, repo, cmd, depthfirst, maxdepth, print0, ignoreerrors):
     """execute cmd in repo.root and in each subrepository"""
     ctx = repo['.']
-    work = [ctx.sub(subpath) for subpath in sorted(ctx.substate)]
+    work = [(1, ctx.sub(subpath)) for subpath in sorted(ctx.substate)]
     if depthfirst:
         work.reverse()
 
     while work:
         if depthfirst:
-            sub = work.pop()
+            (depth, sub) = work.pop()
         else:
-            sub = work.pop(0)
+            (depth, sub) = work.pop(0)
+        if depth > maxdepth >= 0:
+            continue
 
         # subrepo.relpath was renamed to subrepo.subrelpath in
         # 18b5b6392fcf.
         if isinstance(sub, subrepo.hgsubrepo):
             rev = sub._state[1]
             ctx = sub._repo[rev]
-            w = [ctx.sub(subpath) for subpath in sorted(ctx.substate)]
+            w = [(depth + 1, ctx.sub(subpath)) 
+                 for subpath in sorted(ctx.substate)]
             if depthfirst:
                 w.reverse()
             work.extend(w)
         (onsub,
          [('b', 'breadth-first', None,
            _('use breadth-first traversal')),
+          ('', 'max-depth', -1,
+           _('limit recursion to N levels (negative for no limit)'), 'N'),
           ('', 'ignore-errors', None,
            _('continue execution despite errors')),
           ('0', 'print0', None,
   options:
   
    -b --breadth-first  use breadth-first traversal
+      --max-depth N    limit recursion to N levels (negative for no limit)
+                       (default: -1)
       --ignore-errors  continue execution despite errors
    -0 --print0         end subrepository names with NUL, for use with xargs
   
   a/y/s
   a/y/t
 
+Limit depth of traversal:
+  $ hg onsub --max-depth 1 'echo $HG_SUBPATH'
+  a
+  b
+  $ hg onsub --max-depth 2 'echo $HG_SUBPATH'
+  a
+  a/x
+  a/y
+  b
+  b/u
+  b/v
+  $ hg onsub --max-depth 2 -b 'echo $HG_SUBPATH'
+  a
+  b
+  a/x
+  a/y
+  b/u
+  b/v
+
 Test aborting:
 
   $ hg onsub -v 'test $HG_SUBPATH != "a/y/r"'