# HG changeset patch # User Greg Ward # Date 1303657776 14400 # Node ID ecc4fd16555d5615f7eb0cbed293ecf948853277 # Parent a2ad7553ba7923ae78314d235aa051a6ac37026e Add --max-depth option to limit depth of recursion. diff -r a2ad7553ba79 -r ecc4fd16555d onsub.py --- a/onsub.py Sun Apr 24 10:07:03 2011 -0400 +++ b/onsub.py Sun Apr 24 11:09:36 2011 -0400 @@ -43,21 +43,26 @@ 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. @@ -85,7 +90,8 @@ 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) @@ -95,6 +101,8 @@ (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, diff -r a2ad7553ba79 -r ecc4fd16555d test-onsub.t --- a/test-onsub.t Sun Apr 24 10:07:03 2011 -0400 +++ b/test-onsub.t Sun Apr 24 11:09:36 2011 -0400 @@ -40,6 +40,8 @@ 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 @@ -105,6 +107,25 @@ 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"'