comparison onsub.py @ 0:e49f3bbfec4d

Initial version.
author Martin Geisler <mg@aragost.com>
date Mon, 06 Sep 2010 12:09:47 +0200
parents
children a2184bbf38e6
comparison
equal deleted inserted replaced
-1:000000000000 0:e49f3bbfec4d
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)
40 foreach(ui, repo, cmd, not opts.get('breadth_first'), opts.get('print0'))
41
42 def foreach(ui, repo, cmd, depthfirst, print0):
43 """execute cmd in repo.root and in each subrepository"""
44 ctx = repo['.']
45 work = [ctx.sub(subpath) for subpath in sorted(ctx.substate)]
46 if depthfirst:
47 work.reverse()
48
49 while work:
50 if depthfirst:
51 sub = work.pop()
52 else:
53 sub = work.pop(0)
54
55 relpath = subrepo.relpath(sub)
56 if print0:
57 ui.write(relpath, "\0")
58 else:
59 ui.note(_("executing '%s' in %s\n") % (cmd, relpath))
60 util.system(cmd, environ=dict(HG_SUBPATH=relpath,
61 HG_SUBURL=sub._path,
62 HG_SUBSTATE=sub._state[1],
63 HG_REPO=repo.root),
64 cwd=os.path.join(repo.root, relpath), onerr=util.Abort,
65 errprefix=_('terminated onsub in %s') % relpath)
66
67 if isinstance(sub, subrepo.hgsubrepo):
68 rev = sub._state[1]
69 ctx = sub._repo[rev]
70 w = [ctx.sub(subpath) for subpath in sorted(ctx.substate)]
71 if depthfirst:
72 w.reverse()
73 work.extend(w)
74
75 cmdtable = {
76 "onsub":
77 (onsub,
78 [('b', 'breadth-first', None,
79 _('use breadth-first traversal')),
80 ('0', 'print0', None,
81 _('end subrepository names with NUL, for use with xargs'))],
82 _('[-b] [-0] CMD'))
83 }