Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/commands.py @ 214:2d60aa9bde0a
catch TERM signal in command processor
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
catch TERM signal in command processor
This keeps kill from interrupting a transaction without cleanup.
manifest hash: c50091696a3396dfed5c3168bd9e0d94c457a04b
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
iD8DBQFCniLNywK+sNU5EO8RAvepAKCCnEX7vPheIyOu2IvV6dDahdFMWACeMih6
E2R3rA/MGACxG9HpSNH6lak=
=HR1s
-----END PGP SIGNATURE-----
author | mpm@selenic.com |
---|---|
date | Wed, 01 Jun 2005 13:04:13 -0800 |
parents | d2172916ef6c |
children | 8ff4532376a4 |
rev | line source |
---|---|
214 | 1 import os, re, traceback, sys, signal |
209 | 2 from mercurial import fancyopts, ui, hg |
3 | |
4 class UnknownCommand(Exception): pass | |
5 | |
213 | 6 def filterfiles(list, files): |
7 l = [ x for x in list if x in files ] | |
8 | |
9 for f in files: | |
10 if f[-1] != os.sep: f += os.sep | |
11 l += [ x for x in list if x.startswith(f) ] | |
12 return l | |
13 | |
14 def relfilter(repo, args): | |
15 if os.getcwd() != repo.root: | |
16 p = os.getcwd()[len(repo.root) + 1: ] | |
17 return filterfiles(p, args) | |
18 return args | |
19 | |
209 | 20 def relpath(repo, args): |
21 if os.getcwd() != repo.root: | |
22 p = os.getcwd()[len(repo.root) + 1: ] | |
23 return [ os.path.join(p, x) for x in args ] | |
24 return args | |
25 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
26 def help(ui, cmd=None): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
27 '''show help''' |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
28 if cmd: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
29 try: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
30 i = find(cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
31 ui.write("%s\n\n" % i[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
32 ui.write(i[0].__doc__, "\n") |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
33 except UnknownCommand: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
34 ui.warn("unknown command %s", cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
35 sys.exit(0) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
36 |
209 | 37 ui.status("""\ |
38 hg commands: | |
39 | |
40 add [files...] add the given files in the next commit | |
41 addremove add all new files, delete all missing files | |
42 annotate [files...] show changeset number per file line | |
43 branch <path> create a branch of <path> in this directory | |
44 checkout [changeset] checkout the latest or given changeset | |
45 commit commit all changes to the repository | |
46 diff [files...] diff working directory (or selected files) | |
47 dump <file> [rev] dump the latest or given revision of a file | |
48 dumpmanifest [rev] dump the latest or given revision of the manifest | |
49 export <rev> dump the changeset header and diffs for a revision | |
50 history show changeset history | |
51 init create a new repository in this directory | |
52 log <file> show revision history of a single file | |
53 merge <path> merge changes from <path> into local repository | |
54 recover rollback an interrupted transaction | |
55 remove [files...] remove the given files in the next commit | |
56 serve export the repository via HTTP | |
57 status show new, missing, and changed files in working dir | |
58 tags show current changeset tags | |
59 undo undo the last transaction | |
60 """) | |
61 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
62 def init(ui): |
209 | 63 """create a repository""" |
64 hg.repository(ui, ".", create=1) | |
65 | |
213 | 66 def branch(ui, path): |
67 '''branch from a local repository''' | |
68 # this should eventually support remote repos | |
69 os.system("cp -al %s/.hg .hg" % path) | |
70 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
71 def checkout(u, repo, changeset=None): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
72 '''checkout a given changeset or the current tip''' |
209 | 73 node = repo.changelog.tip() |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
74 if changeset: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
75 node = repo.lookup(changeset) |
209 | 76 repo.checkout(node) |
77 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
78 def annotate(u, repo, *args, **ops): |
209 | 79 def getnode(rev): |
80 return hg.short(repo.changelog.node(rev)) | |
81 | |
82 def getname(rev): | |
83 try: | |
84 return bcache[rev] | |
85 except KeyError: | |
86 cl = repo.changelog.read(repo.changelog.node(rev)) | |
87 name = cl[1] | |
88 f = name.find('@') | |
89 if f >= 0: | |
90 name = name[:f] | |
91 bcache[rev] = name | |
92 return name | |
93 | |
94 bcache = {} | |
95 opmap = [['user', getname], ['number', str], ['changeset', getnode]] | |
96 if not ops['user'] and not ops['changeset']: | |
97 ops['number'] = 1 | |
98 | |
99 args = relpath(repo, args) | |
100 node = repo.current | |
101 if ops['revision']: | |
102 node = repo.changelog.lookup(ops['revision']) | |
103 change = repo.changelog.read(node) | |
104 mmap = repo.manifest.read(change[0]) | |
105 maxuserlen = 0 | |
106 maxchangelen = 0 | |
107 for f in args: | |
108 lines = repo.file(f).annotate(mmap[f]) | |
109 pieces = [] | |
110 | |
111 for o, f in opmap: | |
112 if ops[o]: | |
113 l = [ f(n) for n,t in lines ] | |
114 m = max(map(len, l)) | |
115 pieces.append([ "%*s" % (m, x) for x in l]) | |
116 | |
117 for p,l in zip(zip(*pieces), lines): | |
118 u.write(" ".join(p) + ": " + l[1]) | |
119 | |
213 | 120 def status(ui, repo): |
121 '''show changed files in the working directory | |
122 | |
123 C = changed | |
124 A = added | |
125 R = removed | |
126 ? = not tracked''' | |
127 (c, a, d) = repo.diffdir(repo.root, repo.current) | |
128 (c, a, d) = map(lambda x: relfilter(repo, x), (c, a, d)) | |
129 | |
130 for f in c: print "C", f | |
131 for f in a: print "?", f | |
132 for f in d: print "R", f | |
133 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
134 def undo(ui, repo): |
210 | 135 repo.undo() |
136 | |
209 | 137 table = { |
138 "init": (init, [], 'hg init'), | |
213 | 139 "branch|clone": (branch, [], 'hg branch [path]'), |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
140 "help": (help, [], 'hg help [command]'), |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
141 "checkout|co": (checkout, [], 'hg checkout [changeset]'), |
209 | 142 "ann|annotate": (annotate, |
143 [('r', 'revision', '', 'revision'), | |
144 ('u', 'user', None, 'show user'), | |
145 ('n', 'number', None, 'show revision number'), | |
146 ('c', 'changeset', None, 'show changeset')], | |
147 'hg annotate [-u] [-c] [-n] [-r id] [files]'), | |
213 | 148 "status": (status, [], 'hg status'), |
210 | 149 "undo": (undo, [], 'hg undo'), |
209 | 150 } |
151 | |
152 norepo = "init branch help" | |
153 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
154 def find(cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
155 i = None |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
156 for e in table.keys(): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
157 if re.match(e + "$", cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
158 return table[e] |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
159 |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
160 raise UnknownCommand(cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
161 |
214 | 162 class SignalInterrupt(Exception): pass |
163 | |
164 def catchterm(*args): | |
165 raise SignalInterrupt | |
166 | |
209 | 167 def dispatch(args): |
168 options = {} | |
169 opts = [('v', 'verbose', None, 'verbose'), | |
170 ('d', 'debug', None, 'debug'), | |
171 ('q', 'quiet', None, 'quiet'), | |
172 ('y', 'noninteractive', None, 'run non-interactively'), | |
173 ] | |
174 | |
175 args = fancyopts.fancyopts(args, opts, options, | |
176 'hg [options] <command> [options] [files]') | |
177 | |
178 if not args: | |
179 cmd = "help" | |
180 else: | |
181 cmd, args = args[0], args[1:] | |
182 | |
183 u = ui.ui(options["verbose"], options["debug"], options["quiet"], | |
184 not options["noninteractive"]) | |
185 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
186 # deal with unfound commands later |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
187 i = find(cmd) |
209 | 188 |
214 | 189 signal.signal(signal.SIGTERM, catchterm) |
190 | |
209 | 191 cmdoptions = {} |
192 args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2]) | |
193 | |
194 if cmd not in norepo.split(): | |
195 repo = hg.repository(ui = u) | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
196 d = lambda: i[0](u, repo, *args, **cmdoptions) |
209 | 197 else: |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
198 d = lambda: i[0](u, *args, **cmdoptions) |
209 | 199 |
200 try: | |
201 d() | |
214 | 202 except SignalInterrupt: |
203 u.warn("killed!\n") | |
209 | 204 except KeyboardInterrupt: |
205 u.warn("interrupted!\n") | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
206 except TypeError, inst: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
207 # was this an argument error? |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
208 tb = traceback.extract_tb(sys.exc_info()[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
209 if len(tb) > 2: # no |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
210 raise |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
211 u.warn("%s: invalid arguments\n" % i[0].__name__) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
212 u.warn("syntax: %s\n" % i[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
213 sys.exit(-1) |