Mercurial > hg-onsub
annotate onsub.py @ 4:aa0c2e9f5f59
Adapt to Mercurial API change
author | Martin Geisler <mg@aragost.com> |
---|---|
date | Wed, 24 Nov 2010 14:58:48 +0100 |
parents | a2184bbf38e6 |
children | 11fd0da50daa |
rev | line source |
---|---|
0 | 1 |
2 import os | |
3 from mercurial.i18n import _ | |
4 from mercurial import extensions, subrepo, util | |
5 | |
6 """execute a command in each subrepository""" | |
7 | |
8 def onsub(ui, repo, *args, **opts): | |
9 """execute a command in each subrepository | |
10 | |
11 The command is executed with the current working directory set to | |
12 the root of each subrepository. By default, execution stops if the | |
13 command returns a non-zero exit code. Use --ignore-errors to | |
14 override this. | |
15 | |
16 Use --verbose/-v to print the name of each subrepo before the | |
17 command is executed, use --print0/-0 to terminate this line with a | |
18 NUL character instead of a newline. This can for instance be | |
19 useful in combination with :hg:`status --print0`. | |
20 | |
21 The command has access to the following environment variables: | |
22 | |
23 ``HG_REPO``: | |
24 Absolute path to the top-level repository in which the onsub | |
25 command was executed. | |
26 | |
27 ``HG_SUBPATH``: | |
28 Relative path to the current subrepository from the top-level | |
29 repository. | |
30 | |
31 ``HG_SUBURL``: | |
32 URL for the current subrepository as specified in the | |
33 containing repository's ``.hgsub`` file. | |
34 | |
35 ``HG_SUBSTATE``: | |
36 State of the current subrepository as specified in the | |
37 containing repository's ``.hgsubstate`` file. | |
38 """ | |
39 cmd = ' '.join(args) | |
3 | 40 foreach(ui, repo, cmd, not opts.get('breadth_first'), opts.get('print0'), |
41 opts.get('ignore_errors')) | |
0 | 42 |
3 | 43 def foreach(ui, repo, cmd, depthfirst, print0, ignoreerrors): |
0 | 44 """execute cmd in repo.root and in each subrepository""" |
45 ctx = repo['.'] | |
46 work = [ctx.sub(subpath) for subpath in sorted(ctx.substate)] | |
47 if depthfirst: | |
48 work.reverse() | |
49 | |
50 while work: | |
51 if depthfirst: | |
52 sub = work.pop() | |
53 else: | |
54 sub = work.pop(0) | |
55 | |
4
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
56 # subrepo.relpath was renamed to subrepo.subrelpath in |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
57 # 18b5b6392fcf. |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
58 if hasattr(subrepo, 'relpath'): |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
59 relpath = subrepo.relpath(sub) |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
60 else: |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
61 relpath = subrepo.subrelpath(sub) |
aa0c2e9f5f59
Adapt to Mercurial API change
Martin Geisler <mg@aragost.com>
parents:
3
diff
changeset
|
62 |
0 | 63 if print0: |
64 ui.write(relpath, "\0") | |
65 else: | |
66 ui.note(_("executing '%s' in %s\n") % (cmd, relpath)) | |
3 | 67 if ignoreerrors: |
68 onerr = None | |
69 else: | |
70 onerr = util.Abort | |
0 | 71 util.system(cmd, environ=dict(HG_SUBPATH=relpath, |
72 HG_SUBURL=sub._path, | |
73 HG_SUBSTATE=sub._state[1], | |
74 HG_REPO=repo.root), | |
3 | 75 cwd=os.path.join(repo.root, relpath), |
76 onerr=onerr, | |
0 | 77 errprefix=_('terminated onsub in %s') % relpath) |
78 | |
79 if isinstance(sub, subrepo.hgsubrepo): | |
80 rev = sub._state[1] | |
81 ctx = sub._repo[rev] | |
82 w = [ctx.sub(subpath) for subpath in sorted(ctx.substate)] | |
83 if depthfirst: | |
84 w.reverse() | |
85 work.extend(w) | |
86 | |
87 cmdtable = { | |
88 "onsub": | |
89 (onsub, | |
90 [('b', 'breadth-first', None, | |
91 _('use breadth-first traversal')), | |
3 | 92 ('', 'ignore-errors', None, |
93 _('continue execution despite errors')), | |
0 | 94 ('0', 'print0', None, |
95 _('end subrepository names with NUL, for use with xargs'))], | |
3 | 96 _('[-b] [-0] [--ignore-errors] CMD')) |
0 | 97 } |