Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/commands.py @ 312:09375250eb31
Optimize diff and status in subdirectories
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Optimize diff and status in subdirectories
This walks the tree from the subdirectory down.
Based on a patch by Arun Sharma
manifest hash: 3a8545369b4eb6911b9d0fcdd8e4dc77784fa3d8
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
iD8DBQFCrF6/ywK+sNU5EO8RAosUAKC0KWcMfsLErEInDk1X1vpM54HFEgCfUwcN
gXhaAviQZJ8h0ssl3BMmMfI=
=KnRD
-----END PGP SIGNATURE-----
author | mpm@selenic.com |
---|---|
date | Sun, 12 Jun 2005 08:11:43 -0800 |
parents | 273f6a01d18b |
children | b18ce742566a |
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 * | |
11 demandload(globals(), "mdiff time hgweb traceback") | |
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 | |
181 repo.commit(relpath(repo, files), text) | |
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 |
266
4af7677de4a9
Fix argument processing for patch and rawcommit
mpm@selenic.com
parents:
264
diff
changeset
|
415 def rawcommit(ui, repo, files, **rc): |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
416 "raw commit interface" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
417 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
418 text = rc['text'] |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
419 if not text and rc['logfile']: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
420 try: text = open(rc['logfile']).read() |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
421 except IOError: pass |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
422 if not text and not rc['logfile']: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
423 print "missing commit text" |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
424 return 1 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
425 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
426 files = relpath(repo, files) |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
427 if rc['files']: |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
428 files += open(rc['files']).read().splitlines() |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
429 |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
430 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
|
431 |
245 | 432 def recover(ui, repo): |
255 | 433 """roll back an interrupted transaction""" |
245 | 434 repo.recover() |
435 | |
436 def remove(ui, repo, file, *files): | |
437 """remove the specified files on the next commit""" | |
438 repo.remove(relpath(repo, (file,) + files)) | |
439 | |
440 def serve(ui, repo, **opts): | |
255 | 441 """export the repository via HTTP""" |
245 | 442 hgweb.server(repo.root, opts["name"], opts["templates"], |
443 opts["address"], opts["port"]) | |
444 | |
213 | 445 def status(ui, repo): |
446 '''show changed files in the working directory | |
447 | |
245 | 448 C = changed |
449 A = added | |
450 R = removed | |
451 ? = not tracked''' | |
312 | 452 |
453 (c, a, d, u) = repo.diffdir(os.getcwd()) | |
220 | 454 (c, a, d, u) = map(lambda x: relfilter(repo, x), (c, a, d, u)) |
213 | 455 |
456 for f in c: print "C", f | |
220 | 457 for f in a: print "A", f |
213 | 458 for f in d: print "R", f |
220 | 459 for f in u: print "?", f |
213 | 460 |
248 | 461 def tags(ui, repo): |
255 | 462 """list repository tags""" |
248 | 463 repo.lookup(0) # prime the cache |
464 i = repo.tags.items() | |
257 | 465 n = [] |
466 for e in i: | |
467 try: | |
468 l = repo.changelog.rev(e[1]) | |
469 except KeyError: | |
470 l = -2 | |
471 n.append((l, e)) | |
472 | |
473 n.sort() | |
474 n.reverse() | |
475 i = [ e[1] for e in n ] | |
248 | 476 for k, n in i: |
477 try: | |
478 r = repo.changelog.rev(n) | |
479 except KeyError: | |
480 r = "?" | |
481 print "%-30s %5d:%s" % (k, repo.changelog.rev(n), hg.hex(n)) | |
482 | |
245 | 483 def tip(ui, repo): |
255 | 484 """show the tip revision""" |
245 | 485 n = repo.changelog.tip() |
486 t = repo.changelog.rev(n) | |
487 ui.status("%d:%s\n" % (t, hg.hex(n))) | |
488 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
489 def undo(ui, repo): |
255 | 490 """undo the last transaction""" |
210 | 491 repo.undo() |
492 | |
275 | 493 def update(ui, repo, node=None, merge=False, clean=False): |
254 | 494 '''update or merge working directory |
495 | |
496 If there are no outstanding changes in the working directory and | |
497 there is a linear relationship between the current version and the | |
498 requested version, the result is the requested version. | |
499 | |
500 Otherwise the result is a merge between the contents of the | |
501 current working directory and the requested version. Files that | |
502 changed between either parent are marked as changed for the next | |
503 commit and a commit must be performed before any further updates | |
504 are allowed. | |
505 ''' | |
506 node = node and repo.lookup(node) or repo.changelog.tip() | |
275 | 507 return repo.update(node, allow=merge, force=clean) |
254 | 508 |
247 | 509 def verify(ui, repo): |
510 """verify the integrity of the repository""" | |
511 return repo.verify() | |
512 | |
255 | 513 # Command options and aliases are listed here, alphabetically |
514 | |
209 | 515 table = { |
245 | 516 "add": (add, [], "hg add [files]"), |
517 "addremove": (addremove, [], "hg addremove"), | |
209 | 518 "ann|annotate": (annotate, |
519 [('r', 'revision', '', 'revision'), | |
520 ('u', 'user', None, 'show user'), | |
521 ('n', 'number', None, 'show revision number'), | |
522 ('c', 'changeset', None, 'show changeset')], | |
523 'hg annotate [-u] [-c] [-n] [-r id] [files]'), | |
248 | 524 "cat|dump": (cat, [], 'hg cat <file> [rev]'), |
289 | 525 "commit|ci": (commit, |
526 [('t', 'text', "", 'commit text'), | |
527 ('l', 'logfile', "", 'commit text file')], | |
528 'hg commit [files]'), | |
248 | 529 "debugaddchangegroup": (debugaddchangegroup, [], 'debugaddchangegroup'), |
530 "debugchangegroup": (debugchangegroup, [], 'debugchangegroup [roots]'), | |
531 "debugindex": (debugindex, [], 'debugindex <file>'), | |
532 "debugindexdot": (debugindexdot, [], 'debugindexdot <file>'), | |
245 | 533 "diff": (diff, [('r', 'rev', [], 'revision')], |
534 'hg diff [-r A] [-r B] [files]'), | |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
535 "export": (export, [], "hg export <changeset>"), |
245 | 536 "forget": (forget, [], "hg forget [files]"), |
537 "heads": (heads, [], 'hg heads'), | |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
538 "history": (history, [], 'hg history'), |
245 | 539 "help": (help, [], 'hg help [command]'), |
290 | 540 "init": (init, [], 'hg init [url]'), |
245 | 541 "log": (log, [], 'hg log <file>'), |
248 | 542 "manifest|dumpmanifest": (manifest, [], 'hg manifest [rev]'), |
227 | 543 "parents": (parents, [], 'hg parents [node]'), |
246
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
544 "patch|import": (patch, |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
545 [('p', 'strip', 1, 'path strip'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
546 ('b', 'base', "", 'base path'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
547 ('q', 'quiet', "", 'silence diff')], |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
548 "hg import [options] patches"), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
549 "pull|merge": (pull, [], 'hg pull [source]'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
550 "rawcommit": (rawcommit, |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
551 [('p', 'parent', [], 'parent'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
552 ('d', 'date', "", 'data'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
553 ('u', 'user', "", 'user'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
554 ('F', 'files', "", 'file list'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
555 ('t', 'text', "", 'commit text'), |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
556 ('l', 'logfile', "", 'commit text file')], |
96cde50a746f
Migrate rawcommit, import, export, history, and merge
mpm@selenic.com
parents:
245
diff
changeset
|
557 'hg rawcommit [options] [files]'), |
245 | 558 "recover": (recover, [], "hg recover"), |
559 "remove": (remove, [], "hg remove [files]"), | |
560 "serve": (serve, [('p', 'port', 8000, 'listen port'), | |
561 ('a', 'address', '', 'interface address'), | |
562 ('n', 'name', os.getcwd(), 'repository name'), | |
563 ('t', 'templates', "", 'template map')], | |
564 "hg serve [options]"), | |
213 | 565 "status": (status, [], 'hg status'), |
248 | 566 "tags": (tags, [], 'hg tags'), |
245 | 567 "tip": (tip, [], 'hg tip'), |
210 | 568 "undo": (undo, [], 'hg undo'), |
275 | 569 "update|up|checkout|co|resolve": (update, |
570 [('m', 'merge', None, | |
571 'allow merging of conflicts'), | |
572 ('C', 'clean', None, | |
573 'overwrite locally modified files')], | |
574 'hg update [options] [node]'), | |
247 | 575 "verify": (verify, [], 'hg verify'), |
209 | 576 } |
577 | |
248 | 578 norepo = "init branch help debugindex debugindexdot" |
209 | 579 |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
580 def find(cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
581 i = None |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
582 for e in table.keys(): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
583 if re.match(e + "$", cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
584 return table[e] |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
585 |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
586 raise UnknownCommand(cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
587 |
214 | 588 class SignalInterrupt(Exception): pass |
589 | |
590 def catchterm(*args): | |
591 raise SignalInterrupt | |
592 | |
249 | 593 def run(): |
594 sys.exit(dispatch(sys.argv[1:])) | |
595 | |
209 | 596 def dispatch(args): |
597 options = {} | |
598 opts = [('v', 'verbose', None, 'verbose'), | |
599 ('d', 'debug', None, 'debug'), | |
600 ('q', 'quiet', None, 'quiet'), | |
309 | 601 ('p', 'profile', None, 'profile'), |
209 | 602 ('y', 'noninteractive', None, 'run non-interactively'), |
603 ] | |
604 | |
605 args = fancyopts.fancyopts(args, opts, options, | |
606 'hg [options] <command> [options] [files]') | |
607 | |
608 if not args: | |
609 cmd = "help" | |
610 else: | |
611 cmd, args = args[0], args[1:] | |
612 | |
613 u = ui.ui(options["verbose"], options["debug"], options["quiet"], | |
614 not options["noninteractive"]) | |
615 | |
252 | 616 try: |
617 i = find(cmd) | |
618 except UnknownCommand: | |
268 | 619 u.warn("hg: unknown command '%s'\n" % cmd) |
252 | 620 help(u) |
621 sys.exit(1) | |
209 | 622 |
214 | 623 signal.signal(signal.SIGTERM, catchterm) |
624 | |
209 | 625 cmdoptions = {} |
293 | 626 try: |
627 args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2]) | |
628 except fancyopts.getopt.GetoptError, inst: | |
629 u.warn("hg %s: %s\n" % (cmd, inst)) | |
630 help(u, cmd) | |
631 sys.exit(-1) | |
209 | 632 |
633 if cmd not in norepo.split(): | |
634 repo = hg.repository(ui = u) | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
635 d = lambda: i[0](u, repo, *args, **cmdoptions) |
209 | 636 else: |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
637 d = lambda: i[0](u, *args, **cmdoptions) |
209 | 638 |
639 try: | |
309 | 640 if options['profile']: |
641 import hotshot, hotshot.stats | |
642 prof = hotshot.Profile("hg.prof") | |
643 r = prof.runcall(d) | |
644 prof.close() | |
645 stats = hotshot.stats.load("hg.prof") | |
646 stats.strip_dirs() | |
647 stats.sort_stats('time', 'calls') | |
648 stats.print_stats(40) | |
649 return r | |
650 else: | |
651 return d() | |
214 | 652 except SignalInterrupt: |
653 u.warn("killed!\n") | |
209 | 654 except KeyboardInterrupt: |
655 u.warn("interrupted!\n") | |
250 | 656 except IOError, inst: |
657 if inst.errno == 32: | |
658 u.warn("broken pipe\n") | |
659 else: | |
660 raise | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
661 except TypeError, inst: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
662 # was this an argument error? |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
663 tb = traceback.extract_tb(sys.exc_info()[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
664 if len(tb) > 2: # no |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
665 raise |
293 | 666 u.debug(inst, "\n") |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
667 u.warn("%s: invalid arguments\n" % i[0].__name__) |
293 | 668 help(u, cmd) |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
669 sys.exit(-1) |
293 | 670 |