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
|
|
56 relpath = subrepo.relpath(sub)
|
|
57 if print0:
|
|
58 ui.write(relpath, "\0")
|
|
59 else:
|
|
60 ui.note(_("executing '%s' in %s\n") % (cmd, relpath))
|
3
|
61 if ignoreerrors:
|
|
62 onerr = None
|
|
63 else:
|
|
64 onerr = util.Abort
|
0
|
65 util.system(cmd, environ=dict(HG_SUBPATH=relpath,
|
|
66 HG_SUBURL=sub._path,
|
|
67 HG_SUBSTATE=sub._state[1],
|
|
68 HG_REPO=repo.root),
|
3
|
69 cwd=os.path.join(repo.root, relpath),
|
|
70 onerr=onerr,
|
0
|
71 errprefix=_('terminated onsub in %s') % relpath)
|
|
72
|
|
73 if isinstance(sub, subrepo.hgsubrepo):
|
|
74 rev = sub._state[1]
|
|
75 ctx = sub._repo[rev]
|
|
76 w = [ctx.sub(subpath) for subpath in sorted(ctx.substate)]
|
|
77 if depthfirst:
|
|
78 w.reverse()
|
|
79 work.extend(w)
|
|
80
|
|
81 cmdtable = {
|
|
82 "onsub":
|
|
83 (onsub,
|
|
84 [('b', 'breadth-first', None,
|
|
85 _('use breadth-first traversal')),
|
3
|
86 ('', 'ignore-errors', None,
|
|
87 _('continue execution despite errors')),
|
0
|
88 ('0', 'print0', None,
|
|
89 _('end subrepository names with NUL, for use with xargs'))],
|
3
|
90 _('[-b] [-0] [--ignore-errors] CMD'))
|
0
|
91 }
|