Mercurial > hg-onsub
annotate onsub.py @ 7:a2ad7553ba79
Improve help text (in particular, clarify meaning of --print0).
author | Greg Ward <greg@gerg.ca> |
---|---|
date | Sun, 24 Apr 2011 10:07:03 -0400 |
parents | 11fd0da50daa |
children | ecc4fd16555d |
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) | |
3 | 46 foreach(ui, repo, cmd, not opts.get('breadth_first'), opts.get('print0'), |
47 opts.get('ignore_errors')) | |
0 | 48 |
3 | 49 def foreach(ui, repo, cmd, depthfirst, print0, ignoreerrors): |
0 | 50 """execute cmd in repo.root and in each subrepository""" |
51 ctx = repo['.'] | |
52 work = [ctx.sub(subpath) for subpath in sorted(ctx.substate)] | |
53 if depthfirst: | |
54 work.reverse() | |
55 | |
56 while work: | |
57 if depthfirst: | |
58 sub = work.pop() | |
59 else: | |
60 sub = work.pop(0) | |
61 | |
4
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
62 # subrepo.relpath was renamed to subrepo.subrelpath in |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
63 # 18b5b6392fcf. |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
64 if hasattr(subrepo, 'relpath'): |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
65 relpath = subrepo.relpath(sub) |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
66 else: |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
67 relpath = subrepo.subrelpath(sub) |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
68 |
0 | 69 if print0: |
70 ui.write(relpath, "\0") | |
71 else: | |
72 ui.note(_("executing '%s' in %s\n") % (cmd, relpath)) | |
3 | 73 if ignoreerrors: |
74 onerr = None | |
75 else: | |
76 onerr = util.Abort | |
0 | 77 util.system(cmd, environ=dict(HG_SUBPATH=relpath, |
78 HG_SUBURL=sub._path, | |
79 HG_SUBSTATE=sub._state[1], | |
80 HG_REPO=repo.root), | |
3 | 81 cwd=os.path.join(repo.root, relpath), |
82 onerr=onerr, | |
0 | 83 errprefix=_('terminated onsub in %s') % relpath) |
84 | |
85 if isinstance(sub, subrepo.hgsubrepo): | |
86 rev = sub._state[1] | |
87 ctx = sub._repo[rev] | |
88 w = [ctx.sub(subpath) for subpath in sorted(ctx.substate)] | |
89 if depthfirst: | |
90 w.reverse() | |
91 work.extend(w) | |
92 | |
93 cmdtable = { | |
94 "onsub": | |
95 (onsub, | |
96 [('b', 'breadth-first', None, | |
97 _('use breadth-first traversal')), | |
3 | 98 ('', 'ignore-errors', None, |
99 _('continue execution despite errors')), | |
0 | 100 ('0', 'print0', None, |
101 _('end subrepository names with NUL, for use with xargs'))], | |
3 | 102 _('[-b] [-0] [--ignore-errors] CMD')) |
0 | 103 } |