Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/commands.py @ 320:292e10b5831a
hg push: propagate return code
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
hg push: propagate return code
manifest hash: 3c4c5a0eba4c2eb431e4830fafe3138c12f9bddf
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
iD8DBQFCrQ3sywK+sNU5EO8RAkGpAJ40HsYprOWEZzmqw47ZYvZukMzKvgCeIGfD
H6gB6idJV4K2SpEaFUByRYE=
=LV1Z
-----END PGP SIGNATURE-----
author | mpm@selenic.com |
---|---|
date | Sun, 12 Jun 2005 20:39:08 -0800 |
parents | 9ab17e83bce3 |
children | 67c19ad374a9 27d08c0c2a7e |
rev | line source |
---|---|
249 | 1 # commands.py - command processing for mercurial |
2 # | |
3 # Copyright 2005 Matt Mackall <mpm@selenic.com> | |
4 # | |
5 # This software may be used and distributed according to the terms | |
6 # of the GNU General Public License, incorporated herein by reference. | |
7 | |
262 | 8 import os, re, sys, signal |
9 import fancyopts, ui, hg | |
10 from demandload import * | |
319 | 11 demandload(globals(), "mdiff time hgweb traceback random signal") |
209 | 12 |
13 class UnknownCommand(Exception): pass | |
14 | |
245 | 15 def filterfiles(filters, files): |
16 l = [ x for x in files if x in filters ] | |
213 | 17 |
245 | 18 for t in filters: |
19 if t and t[-1] != os.sep: t += os.sep | |
20 l += [ x for x in files if x.startswith(t) ] | |
213 | 21 return l |
22 | |
245 | 23 def relfilter(repo, files): |
213 | 24 if os.getcwd() != repo.root: |
25 p = os.getcwd()[len(repo.root) + 1: ] | |
281 | 26 return filterfiles([p], files) |
245 | 27 return files |
213 | 28 |
209 | 29 def relpath(repo, args): |
30 if os.getcwd() != repo.root: | |
31 p = os.getcwd()[len(repo.root) + 1: ] | |
245 | 32 return [ os.path.normpath(os.path.join(p, x)) for x in args ] |
209 | 33 return args |
245 | 34 |
312 | 35 def dodiff(repo, path, files = None, node1 = None, node2 = None): |
245 | 36 def date(c): |
37 return time.asctime(time.gmtime(float(c[2].split(' ')[0]))) | |
38 | |
39 if node2: | |
40 change = repo.changelog.read(node2) | |
41 mmap2 = repo.manifest.read(change[0]) | |
42 (c, a, d) = repo.diffrevs(node1, node2) | |
43 def read(f): return repo.file(f).read(mmap2[f]) | |
44 date2 = date(change) | |
45 else: | |
46 date2 = time.asctime() | |
312 | 47 (c, a, d, u) = repo.diffdir(path, node1) |
245 | 48 if not node1: |
49 node1 = repo.dirstate.parents()[0] | |
50 def read(f): return file(os.path.join(repo.root, f)).read() | |
51 | |
52 change = repo.changelog.read(node1) | |
53 mmap = repo.manifest.read(change[0]) | |
54 date1 = date(change) | |
55 | |
56 if files: | |
57 c, a, d = map(lambda x: filterfiles(files, x), (c, a, d)) | |
58 | |
59 for f in c: | |
275 | 60 to = None |
61 if f in mmap: | |
62 to = repo.file(f).read(mmap[f]) | |
245 | 63 tn = read(f) |
64 sys.stdout.write(mdiff.unidiff(to, date1, tn, date2, f)) | |
65 for f in a: | |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
262
diff
changeset
|
66 to = None |
245 | 67 tn = read(f) |
68 sys.stdout.write(mdiff.unidiff(to, date1, tn, date2, f)) | |
69 for f in d: | |
70 to = repo.file(f).read(mmap[f]) | |
264
4c1d7072d5cd
Attempt to make diff deal with null sources properly
mpm@selenic.com
parents:
262
diff
changeset
|
71 tn = None |
245 | 72 sys.stdout.write(mdiff.unidiff(to, date1, tn, date2, f)) |
209 | 73 |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
74 def help(ui, cmd=None): |
255 | 75 '''show help for a given command or all commands''' |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
76 if cmd: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
77 try: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
78 i = find(cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
79 ui.write("%s\n\n" % i[2]) |
293 | 80 |
81 if i[1]: | |
82 for s, l, d, c in i[1]: | |
83 opt=' ' | |
84 if s: opt = opt + '-' + s + ' ' | |
85 if l: opt = opt + '--' + l + ' ' | |
86 if d: opt = opt + '(' + str(d) + ')' | |
87 ui.write(opt, "\n") | |
88 if c: ui.write(' %s\n' % c) | |
89 ui.write("\n") | |
90 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
91 ui.write(i[0].__doc__, "\n") |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
92 except UnknownCommand: |
268 | 93 ui.warn("hg: unknown command %s\n" % cmd) |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
94 sys.exit(0) |
255 | 95 else: |
96 ui.status('hg commands:\n\n') | |
209 | 97 |
255 | 98 h = {} |
99 for e in table.values(): | |
100 f = e[0] | |
101 if f.__name__.startswith("debug"): continue | |
102 d = "" | |
103 if f.__doc__: | |
104 d = f.__doc__.splitlines(0)[0].rstrip() | |
105 h[f.__name__] = d | |
106 | |
107 fns = h.keys() | |
108 fns.sort() | |
109 m = max(map(len, fns)) | |
110 for f in fns: | |
111 ui.status(' %-*s %s\n' % (m, f, h[f])) | |
112 | |
113 # Commands start here, listed alphabetically | |
209 | 114 |
245 | 115 def add(ui, repo, file, *files): |
116 '''add the specified files on the next commit''' | |
117 repo.add(relpath(repo, (file,) + files)) | |
213 | 118 |
245 | 119 def addremove(ui, repo): |
255 | 120 """add all new files, delete all missing files""" |
230 | 121 (c, a, d, u) = repo.diffdir(repo.root) |
259 | 122 repo.add(u) |
245 | 123 repo.remove(d) |
219
8ff4532376a4
hg checkout: refuse to checkout if there are outstanding changes
mpm@selenic.com
parents:
214
diff
changeset
|
124 |
245 | 125 def annotate(u, repo, file, *files, **ops): |
255 | 126 """show changeset information per file line""" |
209 | 127 def getnode(rev): |
128 return hg.short(repo.changelog.node(rev)) | |
129 | |
130 def getname(rev): | |
131 try: | |
132 return bcache[rev] | |
133 except KeyError: | |
134 cl = repo.changelog.read(repo.changelog.node(rev)) | |
135 name = cl[1] | |
136 f = name.find('@') | |
137 if f >= 0: | |
138 name = name[:f] | |
139 bcache[rev] = name | |
140 return name | |
141 | |
142 bcache = {} | |
143 opmap = [['user', getname], ['number', str], ['changeset', getnode]] | |
144 if not ops['user'] and not ops['changeset']: | |
145 ops['number'] = 1 | |
146 | |
227 | 147 node = repo.dirstate.parents()[0] |
209 | 148 if ops['revision']: |
149 node = repo.changelog.lookup(ops['revision']) | |
150 change = repo.changelog.read(node) | |
151 mmap = repo.manifest.read(change[0]) | |
152 maxuserlen = 0 | |
153 maxchangelen = 0 | |
245 | 154 for f in relpath(repo, (file,) + files): |
209 | 155 lines = repo.file(f).annotate(mmap[f]) |
156 pieces = [] | |
157 | |
158 for o, f in opmap: | |
159 if ops[o]: | |
160 l = [ f(n) for n,t in lines ] | |
161 m = max(map(len, l)) | |
162 pieces.append([ "%*s" % (m, x) for x in l]) | |
163 | |
164 for p,l in zip(zip(*pieces), lines): | |
165 u.write(" ".join(p) + ": " + l[1]) | |
166 | |
248 | 167 def cat(ui, repo, file, rev = []): |
255 | 168 """output the latest or given revision of a file""" |
281 | 169 r = repo.file(relpath(repo, [file])[0]) |
248 | 170 n = r.tip() |
171 if rev: n = r.lookup(rev) | |
172 sys.stdout.write(r.read(n)) | |
173 | |
289 | 174 def commit(ui, repo, *files, **opts): |
245 | 175 """commit the specified files or all outstanding changes""" |
289 | 176 text = opts['text'] |
177 if not text and opts['logfile']: | |
178 try: text = open(opts['logfile']).read() | |
179 except IOError: pass | |
180 | |
317 | 181 repo.commit(relpath(repo, files), text, opts['user'], opts['date']) |
245 | 182 |
248 | 183 def debugaddchangegroup(ui, repo): |
184 data = sys.stdin.read() | |
185 repo.addchangegroup(data) | |
186 | |
187 def debugchangegroup(ui, repo, roots): | |
188 newer = repo.newer(map(repo.lookup, roots)) | |
189 for chunk in repo.changegroup(newer): | |
190 sys.stdout.write(chunk) | |
191 | |
192 def debugindex(ui, file): | |
193 r = hg.revlog(open, file, "") | |
194 print " rev offset length base linkrev"+\ | |
195 " p1 p2 nodeid" | |
196 for i in range(r.count()): | |
197 e = r.index[i] | |
198 print "% 6d % 9d % 7d % 6d % 7d %s.. %s.. %s.." % ( | |
199 i, e[0], e[1], e[2], e[3], | |
200 hg.hex(e[4][:5]), hg.hex(e[5][:5]), hg.hex(e[6][:5])) | |
201 | |
202 def debugindexdot(ui, file): | |
203 r = hg.revlog(open, file, "") | |
204 print "digraph G {" | |
205 for i in range(r.count()): | |
206 e = r.index[i] | |
207 print "\t%d -> %d" % (r.rev(e[4]), i) | |
208 if e[5] != hg.nullid: | |
209 print "\t%d -> %d" % (r.rev(e[5]), i) | |
210 print "}" | |
211 | |
245 | 212 def diff(ui, repo, *files, **opts): |
255 | 213 """diff working directory (or selected files)""" |
245 | 214 revs = [] |
215 if opts['rev']: | |
216 revs = map(lambda x: repo.lookup(x), opts['rev']) | |
217 | |
218 if len(revs) > 2: | |
219 self.ui.warn("too many revisions to diff\n") | |
220 sys.exit(1) | |
221 | |
222 if files: | |
223 files = relpath(repo, files) | |
224 else: | |
225 files = relpath(repo, [""]) | |
226 | |
312 | 227 dodiff(repo, os.getcwd(), files, *revs) |
245 | 228 |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
229 def export(ui, repo, changeset): |
255 | 230 """dump the changeset header and diffs for a revision""" |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
231 node = repo.lookup(changeset) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
232 prev, other = repo.changelog.parents(node) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
233 change = repo.changelog.read(node) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
234 print "# HG changeset patch" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
235 print "# User %s" % change[1] |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
236 print "# Node ID %s" % hg.hex(node) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
237 print "# Parent %s" % hg.hex(prev) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
238 print |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
239 if other != hg.nullid: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
240 print "# Parent %s" % hg.hex(other) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
241 print change[4].rstrip() |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
242 print |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
243 |
312 | 244 dodiff(repo, "", None, prev, node) |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
245 |
245 | 246 def forget(ui, repo, file, *files): |
247 """don't add the specified files on the next commit""" | |
248 repo.forget(relpath(repo, (file,) + files)) | |
249 | |
221 | 250 def heads(ui, repo): |
251 '''show current repository heads''' | |
252 for n in repo.changelog.heads(): | |
253 i = repo.changelog.rev(n) | |
254 changes = repo.changelog.read(n) | |
255 (p1, p2) = repo.changelog.parents(n) | |
256 (h, h1, h2) = map(hg.hex, (n, p1, p2)) | |
257 (i1, i2) = map(repo.changelog.rev, (p1, p2)) | |
258 print "rev: %4d:%s" % (i, h) | |
259 print "parents: %4d:%s" % (i1, h1) | |
260 if i2: print " %4d:%s" % (i2, h2) | |
261 print "manifest: %4d:%s" % (repo.manifest.rev(changes[0]), | |
262 hg.hex(changes[0])) | |
263 print "user:", changes[1] | |
264 print "date:", time.asctime( | |
265 time.localtime(float(changes[2].split(' ')[0]))) | |
266 if ui.verbose: print "files:", " ".join(changes[3]) | |
267 print "description:" | |
268 print changes[4] | |
269 | |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
270 def history(ui, repo): |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
271 """show the changelog history""" |
270
5a80ed2158c8
Reverse order of hg log and hg history lists
mpm@selenic.com
parents:
268
diff
changeset
|
272 for i in range(repo.changelog.count() - 1, -1, -1): |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
273 n = repo.changelog.node(i) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
274 changes = repo.changelog.read(n) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
275 (p1, p2) = repo.changelog.parents(n) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
276 (h, h1, h2) = map(hg.hex, (n, p1, p2)) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
277 (i1, i2) = map(repo.changelog.rev, (p1, p2)) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
278 print "rev: %4d:%s" % (i, h) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
279 print "parents: %4d:%s" % (i1, h1) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
280 if i2: print " %4d:%s" % (i2, h2) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
281 print "manifest: %4d:%s" % (repo.manifest.rev(changes[0]), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
282 hg.hex(changes[0])) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
283 print "user:", changes[1] |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
284 print "date:", time.asctime( |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
285 time.localtime(float(changes[2].split(' ')[0]))) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
286 if ui.verbose: print "files:", " ".join(changes[3]) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
287 print "description:" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
288 print changes[4] |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
289 |
290 | 290 def init(ui, source=None): |
291 """create a new repository or copy an existing one""" | |
292 | |
293 if source: | |
294 paths = {} | |
295 for name, path in ui.configitems("paths"): | |
296 paths[name] = path | |
297 | |
298 if source in paths: source = paths[source] | |
255 | 299 |
290 | 300 link = 0 |
301 if not source.startswith("http://"): | |
302 d1 = os.stat(os.getcwd()).st_dev | |
303 d2 = os.stat(source).st_dev | |
304 if d1 == d2: link = 1 | |
305 | |
306 if link: | |
307 ui.debug("copying by hardlink\n") | |
308 os.system("cp -al %s/.hg .hg" % source) | |
300 | 309 try: |
310 os.remove(".hg/dirstate") | |
311 except: pass | |
290 | 312 else: |
313 repo = hg.repository(ui, ".", create=1) | |
314 other = hg.repository(ui, source) | |
315 cg = repo.getchangegroup(other) | |
316 repo.addchangegroup(cg) | |
317 else: | |
318 hg.repository(ui, ".", create=1) | |
319 | |
255 | 320 def log(ui, repo, f): |
321 """show the revision history of a single file""" | |
322 f = relpath(repo, [f])[0] | |
323 | |
324 r = repo.file(f) | |
270
5a80ed2158c8
Reverse order of hg log and hg history lists
mpm@selenic.com
parents:
268
diff
changeset
|
325 for i in range(r.count() - 1, -1, -1): |
255 | 326 n = r.node(i) |
327 (p1, p2) = r.parents(n) | |
328 (h, h1, h2) = map(hg.hex, (n, p1, p2)) | |
329 (i1, i2) = map(r.rev, (p1, p2)) | |
330 cr = r.linkrev(n) | |
331 cn = hg.hex(repo.changelog.node(cr)) | |
332 print "rev: %4d:%s" % (i, h) | |
333 print "changeset: %4d:%s" % (cr, cn) | |
334 print "parents: %4d:%s" % (i1, h1) | |
335 if i2: print " %4d:%s" % (i2, h2) | |
336 changes = repo.changelog.read(repo.changelog.node(cr)) | |
337 print "user: %s" % changes[1] | |
338 print "date: %s" % time.asctime( | |
339 time.localtime(float(changes[2].split(' ')[0]))) | |
340 print "description:" | |
341 print changes[4].rstrip() | |
342 print | |
343 | |
344 def manifest(ui, repo, rev = []): | |
345 """output the latest or given revision of the project manifest""" | |
346 n = repo.manifest.tip() | |
347 if rev: | |
348 n = repo.manifest.lookup(rev) | |
349 m = repo.manifest.read(n) | |
276 | 350 mf = repo.manifest.readflags(n) |
255 | 351 files = m.keys() |
352 files.sort() | |
353 | |
354 for f in files: | |
276 | 355 ui.write("%40s %3s %s\n" % (hg.hex(m[f]), mf[f] and "755" or "644", f)) |
255 | 356 |
357 def parents(ui, repo, node = None): | |
358 '''show the parents of the current working dir''' | |
359 if node: | |
360 p = repo.changelog.parents(repo.lookup(hg.bin(node))) | |
361 else: | |
362 p = repo.dirstate.parents() | |
363 | |
364 for n in p: | |
365 if n != hg.nullid: | |
366 ui.write("%d:%s\n" % (repo.changelog.rev(n), hg.hex(n))) | |
367 | |
294
f8d56da6ac8f
hg patch: fix to actually take a list of patches
mpm@selenic.com
parents:
293
diff
changeset
|
368 def patch(ui, repo, patch1, *patches, **opts): |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
369 """import an ordered set of patches""" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
370 try: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
371 import psyco |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
372 psyco.full() |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
373 except: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
374 pass |
294
f8d56da6ac8f
hg patch: fix to actually take a list of patches
mpm@selenic.com
parents:
293
diff
changeset
|
375 |
295 | 376 patches = (patch1,) + patches |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
377 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
378 d = opts["base"] |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
379 strip = opts["strip"] |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
380 quiet = opts["quiet"] and "> /dev/null" or "" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
381 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
382 for patch in patches: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
383 ui.status("applying %s\n" % patch) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
384 pf = os.path.join(d, patch) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
385 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
386 text = "" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
387 for l in file(pf): |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
388 if l[:4] == "--- ": break |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
389 text += l |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
390 |
310 | 391 # make sure text isn't empty |
392 if not text: text = "imported patch %s\n" % patch | |
393 | |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
394 f = os.popen("lsdiff --strip %d %s" % (strip, pf)) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
395 files = filter(None, map(lambda x: x.rstrip(), f.read().splitlines())) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
396 f.close() |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
397 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
398 if files: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
399 if os.system("patch -p%d < %s %s" % (strip, pf, quiet)): |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
400 raise "patch failed!" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
401 repo.commit(files, text) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
402 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
403 def pull(ui, repo, source): |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
404 """pull changes from the specified source""" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
405 paths = {} |
286 | 406 for name, path in ui.configitems("paths"): |
290 | 407 paths[name] = path |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
408 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
409 if source in paths: source = paths[source] |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
410 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
411 other = hg.repository(ui, source) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
412 cg = repo.getchangegroup(other) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
413 repo.addchangegroup(cg) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
414 |
319 | 415 def push(ui, repo, dest): |
416 """push changes to the specified destination""" | |
417 paths = {} | |
418 for name, path in ui.configitems("paths"): | |
419 paths[name] = path | |
420 | |
421 if dest in paths: dest = paths[dest] | |
422 | |
423 if not dest.startswith("ssh://"): | |
424 ui.warn("abort: can only push to ssh:// destinations currently\n") | |
425 return 1 | |
426 | |
427 m = re.match(r'ssh://(([^@]+)@)?([^:/]+)(:(\d+))?(/(.*))?', dest) | |
428 if not m: | |
429 ui.warn("abort: couldn't parse destination %s\n" % dest) | |
430 return 1 | |
431 | |
432 user, host, port, path = map(m.group, (2, 3, 5, 7)) | |
433 host = user and ("%s@%s" % (user, host)) or host | |
434 port = port and (" -p %s") % port or "" | |
435 path = path or "" | |
436 | |
437 sport = random.randrange(30000, 60000) | |
438 cmd = "ssh %s%s -R %d:localhost:%d 'cd %s; hg pull http://localhost:%d/'" | |
439 cmd = cmd % (host, port, sport+1, sport, path, sport+1) | |
440 | |
441 child = os.fork() | |
442 if not child: | |
443 sys.stdout = file("/dev/null", "w") | |
444 sys.stderr = sys.stdout | |
445 hgweb.server(repo.root, "pull", "", "localhost", sport) | |
446 else: | |
447 r = os.system(cmd) | |
448 os.kill(child, signal.SIGTERM) | |
320 | 449 return r |
319 | 450 |
266
4af7677de4a9
Fix argument processing for patch and rawcommit
mpm@selenic.com
parents:
264
diff
changeset
|
451 def rawcommit(ui, repo, files, **rc): |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
452 "raw commit interface" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
453 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
454 text = rc['text'] |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
455 if not text and rc['logfile']: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
456 try: text = open(rc['logfile']).read() |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
457 except IOError: pass |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
458 if not text and not rc['logfile']: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
459 print "missing commit text" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
460 return 1 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
461 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
462 files = relpath(repo, files) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
463 if rc['files']: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
464 files += open(rc['files']).read().splitlines() |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
465 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
466 repo.rawcommit(files, text, rc['user'], rc['date'], *rc['parent']) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
467 |
245 | 468 def recover(ui, repo): |
255 | 469 """roll back an interrupted transaction""" |
245 | 470 repo.recover() |
471 | |
472 def remove(ui, repo, file, *files): | |
473 """remove the specified files on the next commit""" | |
474 repo.remove(relpath(repo, (file,) + files)) | |
475 | |
476 def serve(ui, repo, **opts): | |
255 | 477 """export the repository via HTTP""" |
245 | 478 hgweb.server(repo.root, opts["name"], opts["templates"], |
479 opts["address"], opts["port"]) | |
480 | |
213 | 481 def status(ui, repo): |
482 '''show changed files in the working directory | |
483 | |
245 | 484 C = changed |
485 A = added | |
486 R = removed | |
487 ? = not tracked''' | |
312 | 488 |
489 (c, a, d, u) = repo.diffdir(os.getcwd()) | |
220 | 490 (c, a, d, u) = map(lambda x: relfilter(repo, x), (c, a, d, u)) |
213 | 491 |
492 for f in c: print "C", f | |
220 | 493 for f in a: print "A", f |
213 | 494 for f in d: print "R", f |
220 | 495 for f in u: print "?", f |
213 | 496 |
248 | 497 def tags(ui, repo): |
255 | 498 """list repository tags""" |
248 | 499 repo.lookup(0) # prime the cache |
500 i = repo.tags.items() | |
257 | 501 n = [] |
502 for e in i: | |
503 try: | |
504 l = repo.changelog.rev(e[1]) | |
505 except KeyError: | |
506 l = -2 | |
507 n.append((l, e)) | |
508 | |
509 n.sort() | |
510 n.reverse() | |
511 i = [ e[1] for e in n ] | |
248 | 512 for k, n in i: |
513 try: | |
514 r = repo.changelog.rev(n) | |
515 except KeyError: | |
516 r = "?" | |
517 print "%-30s %5d:%s" % (k, repo.changelog.rev(n), hg.hex(n)) | |
518 | |
245 | 519 def tip(ui, repo): |
255 | 520 """show the tip revision""" |
245 | 521 n = repo.changelog.tip() |
522 t = repo.changelog.rev(n) | |
523 ui.status("%d:%s\n" % (t, hg.hex(n))) | |
524 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
525 def undo(ui, repo): |
255 | 526 """undo the last transaction""" |
210 | 527 repo.undo() |
528 | |
275 | 529 def update(ui, repo, node=None, merge=False, clean=False): |
254 | 530 '''update or merge working directory |
531 | |
532 If there are no outstanding changes in the working directory and | |
533 there is a linear relationship between the current version and the | |
534 requested version, the result is the requested version. | |
535 | |
536 Otherwise the result is a merge between the contents of the | |
537 current working directory and the requested version. Files that | |
538 changed between either parent are marked as changed for the next | |
539 commit and a commit must be performed before any further updates | |
540 are allowed. | |
541 ''' | |
542 node = node and repo.lookup(node) or repo.changelog.tip() | |
275 | 543 return repo.update(node, allow=merge, force=clean) |
254 | 544 |
247 | 545 def verify(ui, repo): |
546 """verify the integrity of the repository""" | |
547 return repo.verify() | |
548 | |
255 | 549 # Command options and aliases are listed here, alphabetically |
550 | |
209 | 551 table = { |
245 | 552 "add": (add, [], "hg add [files]"), |
553 "addremove": (addremove, [], "hg addremove"), | |
209 | 554 "ann|annotate": (annotate, |
555 [('r', 'revision', '', 'revision'), | |
556 ('u', 'user', None, 'show user'), | |
557 ('n', 'number', None, 'show revision number'), | |
558 ('c', 'changeset', None, 'show changeset')], | |
559 'hg annotate [-u] [-c] [-n] [-r id] [files]'), | |
248 | 560 "cat|dump": (cat, [], 'hg cat <file> [rev]'), |
289 | 561 "commit|ci": (commit, |
562 [('t', 'text', "", 'commit text'), | |
317 | 563 ('l', 'logfile', "", 'commit text file'), |
564 ('d', 'date', "", 'data'), | |
565 ('u', 'user', "", 'user')], | |
289 | 566 'hg commit [files]'), |
248 | 567 "debugaddchangegroup": (debugaddchangegroup, [], 'debugaddchangegroup'), |
568 "debugchangegroup": (debugchangegroup, [], 'debugchangegroup [roots]'), | |
569 "debugindex": (debugindex, [], 'debugindex <file>'), | |
570 "debugindexdot": (debugindexdot, [], 'debugindexdot <file>'), | |
245 | 571 "diff": (diff, [('r', 'rev', [], 'revision')], |
572 'hg diff [-r A] [-r B] [files]'), | |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
573 "export": (export, [], "hg export <changeset>"), |
245 | 574 "forget": (forget, [], "hg forget [files]"), |
575 "heads": (heads, [], 'hg heads'), | |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
576 "history": (history, [], 'hg history'), |
245 | 577 "help": (help, [], 'hg help [command]'), |
290 | 578 "init": (init, [], 'hg init [url]'), |
245 | 579 "log": (log, [], 'hg log <file>'), |
248 | 580 "manifest|dumpmanifest": (manifest, [], 'hg manifest [rev]'), |
227 | 581 "parents": (parents, [], 'hg parents [node]'), |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
582 "patch|import": (patch, |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
583 [('p', 'strip', 1, 'path strip'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
584 ('b', 'base', "", 'base path'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
585 ('q', 'quiet', "", 'silence diff')], |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
586 "hg import [options] patches"), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
587 "pull|merge": (pull, [], 'hg pull [source]'), |
319 | 588 "push": (push, [], 'hg push <destination>'), |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
589 "rawcommit": (rawcommit, |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
590 [('p', 'parent', [], 'parent'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
591 ('d', 'date', "", 'data'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
592 ('u', 'user', "", 'user'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
593 ('F', 'files', "", 'file list'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
594 ('t', 'text', "", 'commit text'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
595 ('l', 'logfile', "", 'commit text file')], |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
596 'hg rawcommit [options] [files]'), |
245 | 597 "recover": (recover, [], "hg recover"), |
598 "remove": (remove, [], "hg remove [files]"), | |
599 "serve": (serve, [('p', 'port', 8000, 'listen port'), | |
600 ('a', 'address', '', 'interface address'), | |
601 ('n', 'name', os.getcwd(), 'repository name'), | |
602 ('t', 'templates', "", 'template map')], | |
603 "hg serve [options]"), | |
213 | 604 "status": (status, [], 'hg status'), |
248 | 605 "tags": (tags, [], 'hg tags'), |
245 | 606 "tip": (tip, [], 'hg tip'), |
210 | 607 "undo": (undo, [], 'hg undo'), |
275 | 608 "update|up|checkout|co|resolve": (update, |
609 [('m', 'merge', None, | |
610 'allow merging of conflicts'), | |
611 ('C', 'clean', None, | |
612 'overwrite locally modified files')], | |
613 'hg update [options] [node]'), | |
247 | 614 "verify": (verify, [], 'hg verify'), |
209 | 615 } |
616 | |
248 | 617 norepo = "init branch help debugindex debugindexdot" |
209 | 618 |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
619 def find(cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
620 i = None |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
621 for e in table.keys(): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
622 if re.match(e + "$", cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
623 return table[e] |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
624 |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
625 raise UnknownCommand(cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
626 |
214 | 627 class SignalInterrupt(Exception): pass |
628 | |
629 def catchterm(*args): | |
630 raise SignalInterrupt | |
631 | |
249 | 632 def run(): |
633 sys.exit(dispatch(sys.argv[1:])) | |
634 | |
209 | 635 def dispatch(args): |
636 options = {} | |
637 opts = [('v', 'verbose', None, 'verbose'), | |
638 ('d', 'debug', None, 'debug'), | |
639 ('q', 'quiet', None, 'quiet'), | |
309 | 640 ('p', 'profile', None, 'profile'), |
209 | 641 ('y', 'noninteractive', None, 'run non-interactively'), |
642 ] | |
643 | |
644 args = fancyopts.fancyopts(args, opts, options, | |
645 'hg [options] <command> [options] [files]') | |
646 | |
647 if not args: | |
648 cmd = "help" | |
649 else: | |
650 cmd, args = args[0], args[1:] | |
651 | |
652 u = ui.ui(options["verbose"], options["debug"], options["quiet"], | |
653 not options["noninteractive"]) | |
654 | |
252 | 655 try: |
656 i = find(cmd) | |
657 except UnknownCommand: | |
268 | 658 u.warn("hg: unknown command '%s'\n" % cmd) |
252 | 659 help(u) |
660 sys.exit(1) | |
209 | 661 |
214 | 662 signal.signal(signal.SIGTERM, catchterm) |
663 | |
209 | 664 cmdoptions = {} |
293 | 665 try: |
666 args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2]) | |
667 except fancyopts.getopt.GetoptError, inst: | |
668 u.warn("hg %s: %s\n" % (cmd, inst)) | |
669 help(u, cmd) | |
670 sys.exit(-1) | |
209 | 671 |
672 if cmd not in norepo.split(): | |
673 repo = hg.repository(ui = u) | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
674 d = lambda: i[0](u, repo, *args, **cmdoptions) |
209 | 675 else: |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
676 d = lambda: i[0](u, *args, **cmdoptions) |
209 | 677 |
678 try: | |
309 | 679 if options['profile']: |
680 import hotshot, hotshot.stats | |
681 prof = hotshot.Profile("hg.prof") | |
682 r = prof.runcall(d) | |
683 prof.close() | |
684 stats = hotshot.stats.load("hg.prof") | |
685 stats.strip_dirs() | |
686 stats.sort_stats('time', 'calls') | |
687 stats.print_stats(40) | |
688 return r | |
689 else: | |
690 return d() | |
214 | 691 except SignalInterrupt: |
692 u.warn("killed!\n") | |
209 | 693 except KeyboardInterrupt: |
694 u.warn("interrupted!\n") | |
250 | 695 except IOError, inst: |
696 if inst.errno == 32: | |
697 u.warn("broken pipe\n") | |
698 else: | |
699 raise | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
700 except TypeError, inst: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
701 # was this an argument error? |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
702 tb = traceback.extract_tb(sys.exc_info()[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
703 if len(tb) > 2: # no |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
704 raise |
293 | 705 u.debug(inst, "\n") |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
706 u.warn("%s: invalid arguments\n" % i[0].__name__) |
293 | 707 help(u, cmd) |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
708 sys.exit(-1) |
293 | 709 |