Mercurial > hg-onsub
annotate onsub.py @ 8:ecc4fd16555d
Add --max-depth option to limit depth of recursion.
author | Greg Ward <greg@gerg.ca> |
---|---|
date | Sun, 24 Apr 2011 11:09:36 -0400 |
parents | a2ad7553ba79 |
children | 90b54c4fe4fe |
rev | line source |
---|---|
6 | 1 # onsub.py - execute commands recursively on subrepositories |
2 # | |
3 # Copyright 2010, 2011 aragost Trifork | |
4 # | |
5 # This software may be used and distributed according to the terms of | |
6 # the GNU General Public License version 2 or any later version. | |
0 | 7 |
8 import os | |
9 from mercurial.i18n import _ | |
10 from mercurial import extensions, subrepo, util | |
11 | |
12 """execute a command in each subrepository""" | |
13 | |
14 def onsub(ui, repo, *args, **opts): | |
15 """execute a command in each subrepository | |
16 | |
7
a2ad7553ba79
Improve help text (in particular, clarify meaning of --print0).
Greg Ward <greg@gerg.ca>
parents:
6
diff
changeset
|
17 Executes CMD with the current working directory set to the root of |
a2ad7553ba79
Improve help text (in particular, clarify meaning of --print0).
Greg Ward <greg@gerg.ca>
parents:
6
diff
changeset
|
18 each subrepository. By default, execution stops if CMD returns a |
a2ad7553ba79
Improve help text (in particular, clarify meaning of --print0).
Greg Ward <greg@gerg.ca>
parents:
6
diff
changeset
|
19 non-zero exit code. Use --ignore-errors to override this. |
0 | 20 |
7
a2ad7553ba79
Improve help text (in particular, clarify meaning of --print0).
Greg Ward <greg@gerg.ca>
parents:
6
diff
changeset
|
21 Use --verbose/-v to print the command being run and the subrepo |
a2ad7553ba79
Improve help text (in particular, clarify meaning of --print0).
Greg Ward <greg@gerg.ca>
parents:
6
diff
changeset
|
22 name for each run of CMD in a subrepo. Alternately, use |
a2ad7553ba79
Improve help text (in particular, clarify meaning of --print0).
Greg Ward <greg@gerg.ca>
parents:
6
diff
changeset
|
23 --print0/-0 to print just the subrepo name followed by a NUL |
a2ad7553ba79
Improve help text (in particular, clarify meaning of --print0).
Greg Ward <greg@gerg.ca>
parents:
6
diff
changeset
|
24 character instead of a newline. This can be useful in combination |
a2ad7553ba79
Improve help text (in particular, clarify meaning of --print0).
Greg Ward <greg@gerg.ca>
parents:
6
diff
changeset
|
25 with :hg:`status --print0`. |
0 | 26 |
27 The command has access to the following environment variables: | |
28 | |
29 ``HG_REPO``: | |
30 Absolute path to the top-level repository in which the onsub | |
31 command was executed. | |
32 | |
33 ``HG_SUBPATH``: | |
34 Relative path to the current subrepository from the top-level | |
35 repository. | |
36 | |
37 ``HG_SUBURL``: | |
38 URL for the current subrepository as specified in the | |
39 containing repository's ``.hgsub`` file. | |
40 | |
41 ``HG_SUBSTATE``: | |
42 State of the current subrepository as specified in the | |
43 containing repository's ``.hgsubstate`` file. | |
44 """ | |
45 cmd = ' '.join(args) | |
8
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
46 foreach(ui, repo, cmd, |
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
47 not opts.get('breadth_first'), |
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
48 opts.get('max_depth'), |
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
49 opts.get('print0'), |
3 | 50 opts.get('ignore_errors')) |
0 | 51 |
8
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
52 def foreach(ui, repo, cmd, depthfirst, maxdepth, print0, ignoreerrors): |
0 | 53 """execute cmd in repo.root and in each subrepository""" |
54 ctx = repo['.'] | |
8
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
55 work = [(1, ctx.sub(subpath)) for subpath in sorted(ctx.substate)] |
0 | 56 if depthfirst: |
57 work.reverse() | |
58 | |
59 while work: | |
60 if depthfirst: | |
8
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
61 (depth, sub) = work.pop() |
0 | 62 else: |
8
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
63 (depth, sub) = work.pop(0) |
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
64 if depth > maxdepth >= 0: |
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
65 continue |
0 | 66 |
4
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
67 # subrepo.relpath was renamed to subrepo.subrelpath in |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
68 # 18b5b6392fcf. |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
69 if hasattr(subrepo, 'relpath'): |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
70 relpath = subrepo.relpath(sub) |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
71 else: |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
72 relpath = subrepo.subrelpath(sub) |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
73 |
0 | 74 if print0: |
75 ui.write(relpath, "\0") | |
76 else: | |
77 ui.note(_("executing '%s' in %s\n") % (cmd, relpath)) | |
3 | 78 if ignoreerrors: |
79 onerr = None | |
80 else: | |
81 onerr = util.Abort | |
0 | 82 util.system(cmd, environ=dict(HG_SUBPATH=relpath, |
83 HG_SUBURL=sub._path, | |
84 HG_SUBSTATE=sub._state[1], | |
85 HG_REPO=repo.root), | |
3 | 86 cwd=os.path.join(repo.root, relpath), |
87 onerr=onerr, | |
0 | 88 errprefix=_('terminated onsub in %s') % relpath) |
89 | |
90 if isinstance(sub, subrepo.hgsubrepo): | |
91 rev = sub._state[1] | |
92 ctx = sub._repo[rev] | |
8
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
93 w = [(depth + 1, ctx.sub(subpath)) |
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
94 for subpath in sorted(ctx.substate)] |
0 | 95 if depthfirst: |
96 w.reverse() | |
97 work.extend(w) | |
98 | |
99 cmdtable = { | |
100 "onsub": | |
101 (onsub, | |
102 [('b', 'breadth-first', None, | |
103 _('use breadth-first traversal')), | |
8
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
104 ('', 'max-depth', -1, |
ecc4fd16555d
Add --max-depth option to limit depth of recursion.
Greg Ward <greg@gerg.ca>
parents:
7
diff
changeset
|
105 _('limit recursion to N levels (negative for no limit)'), 'N'), |
3 | 106 ('', 'ignore-errors', None, |
107 _('continue execution despite errors')), | |
0 | 108 ('0', 'print0', None, |
109 _('end subrepository names with NUL, for use with xargs'))], | |
3 | 110 _('[-b] [-0] [--ignore-errors] CMD')) |
0 | 111 } |