##// END OF EJS Templates
write_bundle: close the file before removing it in case of failure
Benoit Boissinot -
r1969:6d17d420 default
parent child Browse files
Show More
@@ -1,3265 +1,3266
1 1 # commands.py - command processing for mercurial
2 2 #
3 3 # Copyright 2005 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 8 from demandload import demandload
9 9 from node import *
10 10 from i18n import gettext as _
11 11 demandload(globals(), "os re sys signal shutil imp urllib pdb")
12 12 demandload(globals(), "fancyopts ui hg util lock revlog templater bundlerepo")
13 13 demandload(globals(), "fnmatch hgweb mdiff random signal tempfile time")
14 14 demandload(globals(), "traceback errno socket version struct atexit sets bz2")
15 15
16 16 class UnknownCommand(Exception):
17 17 """Exception raised if command is not in the command table."""
18 18 class AmbiguousCommand(Exception):
19 19 """Exception raised if command shortcut matches more than one command."""
20 20
21 21 def filterfiles(filters, files):
22 22 l = [x for x in files if x in filters]
23 23
24 24 for t in filters:
25 25 if t and t[-1] != "/":
26 26 t += "/"
27 27 l += [x for x in files if x.startswith(t)]
28 28 return l
29 29
30 30 def relpath(repo, args):
31 31 cwd = repo.getcwd()
32 32 if cwd:
33 33 return [util.normpath(os.path.join(cwd, x)) for x in args]
34 34 return args
35 35
36 36 def matchpats(repo, pats=[], opts={}, head=''):
37 37 cwd = repo.getcwd()
38 38 if not pats and cwd:
39 39 opts['include'] = [os.path.join(cwd, i) for i in opts['include']]
40 40 opts['exclude'] = [os.path.join(cwd, x) for x in opts['exclude']]
41 41 cwd = ''
42 42 return util.cmdmatcher(repo.root, cwd, pats or ['.'], opts.get('include'),
43 43 opts.get('exclude'), head)
44 44
45 45 def makewalk(repo, pats, opts, node=None, head=''):
46 46 files, matchfn, anypats = matchpats(repo, pats, opts, head)
47 47 exact = dict(zip(files, files))
48 48 def walk():
49 49 for src, fn in repo.walk(node=node, files=files, match=matchfn):
50 50 yield src, fn, util.pathto(repo.getcwd(), fn), fn in exact
51 51 return files, matchfn, walk()
52 52
53 53 def walk(repo, pats, opts, node=None, head=''):
54 54 files, matchfn, results = makewalk(repo, pats, opts, node, head)
55 55 for r in results:
56 56 yield r
57 57
58 58 def walkchangerevs(ui, repo, pats, opts):
59 59 '''Iterate over files and the revs they changed in.
60 60
61 61 Callers most commonly need to iterate backwards over the history
62 62 it is interested in. Doing so has awful (quadratic-looking)
63 63 performance, so we use iterators in a "windowed" way.
64 64
65 65 We walk a window of revisions in the desired order. Within the
66 66 window, we first walk forwards to gather data, then in the desired
67 67 order (usually backwards) to display it.
68 68
69 69 This function returns an (iterator, getchange, matchfn) tuple. The
70 70 getchange function returns the changelog entry for a numeric
71 71 revision. The iterator yields 3-tuples. They will be of one of
72 72 the following forms:
73 73
74 74 "window", incrementing, lastrev: stepping through a window,
75 75 positive if walking forwards through revs, last rev in the
76 76 sequence iterated over - use to reset state for the current window
77 77
78 78 "add", rev, fns: out-of-order traversal of the given file names
79 79 fns, which changed during revision rev - use to gather data for
80 80 possible display
81 81
82 82 "iter", rev, None: in-order traversal of the revs earlier iterated
83 83 over with "add" - use to display data'''
84 84
85 85 def increasing_windows(start, end, windowsize=8, sizelimit=512):
86 86 if start < end:
87 87 while start < end:
88 88 yield start, min(windowsize, end-start)
89 89 start += windowsize
90 90 if windowsize < sizelimit:
91 91 windowsize *= 2
92 92 else:
93 93 while start > end:
94 94 yield start, min(windowsize, start-end-1)
95 95 start -= windowsize
96 96 if windowsize < sizelimit:
97 97 windowsize *= 2
98 98
99 99
100 100 files, matchfn, anypats = matchpats(repo, pats, opts)
101 101
102 102 if repo.changelog.count() == 0:
103 103 return [], False, matchfn
104 104
105 105 revs = map(int, revrange(ui, repo, opts['rev'] or ['tip:0']))
106 106 wanted = {}
107 107 slowpath = anypats
108 108 fncache = {}
109 109
110 110 chcache = {}
111 111 def getchange(rev):
112 112 ch = chcache.get(rev)
113 113 if ch is None:
114 114 chcache[rev] = ch = repo.changelog.read(repo.lookup(str(rev)))
115 115 return ch
116 116
117 117 if not slowpath and not files:
118 118 # No files, no patterns. Display all revs.
119 119 wanted = dict(zip(revs, revs))
120 120 if not slowpath:
121 121 # Only files, no patterns. Check the history of each file.
122 122 def filerevgen(filelog):
123 123 for i, window in increasing_windows(filelog.count()-1, -1):
124 124 revs = []
125 125 for j in xrange(i - window, i + 1):
126 126 revs.append(filelog.linkrev(filelog.node(j)))
127 127 revs.reverse()
128 128 for rev in revs:
129 129 yield rev
130 130
131 131 minrev, maxrev = min(revs), max(revs)
132 132 for file_ in files:
133 133 filelog = repo.file(file_)
134 134 # A zero count may be a directory or deleted file, so
135 135 # try to find matching entries on the slow path.
136 136 if filelog.count() == 0:
137 137 slowpath = True
138 138 break
139 139 for rev in filerevgen(filelog):
140 140 if rev <= maxrev:
141 141 if rev < minrev:
142 142 break
143 143 fncache.setdefault(rev, [])
144 144 fncache[rev].append(file_)
145 145 wanted[rev] = 1
146 146 if slowpath:
147 147 # The slow path checks files modified in every changeset.
148 148 def changerevgen():
149 149 for i, window in increasing_windows(repo.changelog.count()-1, -1):
150 150 for j in xrange(i - window, i + 1):
151 151 yield j, getchange(j)[3]
152 152
153 153 for rev, changefiles in changerevgen():
154 154 matches = filter(matchfn, changefiles)
155 155 if matches:
156 156 fncache[rev] = matches
157 157 wanted[rev] = 1
158 158
159 159 def iterate():
160 160 for i, window in increasing_windows(0, len(revs)):
161 161 yield 'window', revs[0] < revs[-1], revs[-1]
162 162 nrevs = [rev for rev in revs[i:i+window]
163 163 if rev in wanted]
164 164 srevs = list(nrevs)
165 165 srevs.sort()
166 166 for rev in srevs:
167 167 fns = fncache.get(rev) or filter(matchfn, getchange(rev)[3])
168 168 yield 'add', rev, fns
169 169 for rev in nrevs:
170 170 yield 'iter', rev, None
171 171 return iterate(), getchange, matchfn
172 172
173 173 revrangesep = ':'
174 174
175 175 def revrange(ui, repo, revs, revlog=None):
176 176 """Yield revision as strings from a list of revision specifications."""
177 177 if revlog is None:
178 178 revlog = repo.changelog
179 179 revcount = revlog.count()
180 180 def fix(val, defval):
181 181 if not val:
182 182 return defval
183 183 try:
184 184 num = int(val)
185 185 if str(num) != val:
186 186 raise ValueError
187 187 if num < 0:
188 188 num += revcount
189 189 if num < 0:
190 190 num = 0
191 191 elif num >= revcount:
192 192 raise ValueError
193 193 except ValueError:
194 194 try:
195 195 num = repo.changelog.rev(repo.lookup(val))
196 196 except KeyError:
197 197 try:
198 198 num = revlog.rev(revlog.lookup(val))
199 199 except KeyError:
200 200 raise util.Abort(_('invalid revision identifier %s'), val)
201 201 return num
202 202 seen = {}
203 203 for spec in revs:
204 204 if spec.find(revrangesep) >= 0:
205 205 start, end = spec.split(revrangesep, 1)
206 206 start = fix(start, 0)
207 207 end = fix(end, revcount - 1)
208 208 step = start > end and -1 or 1
209 209 for rev in xrange(start, end+step, step):
210 210 if rev in seen:
211 211 continue
212 212 seen[rev] = 1
213 213 yield str(rev)
214 214 else:
215 215 rev = fix(spec, None)
216 216 if rev in seen:
217 217 continue
218 218 seen[rev] = 1
219 219 yield str(rev)
220 220
221 221 def make_filename(repo, r, pat, node=None,
222 222 total=None, seqno=None, revwidth=None, pathname=None):
223 223 node_expander = {
224 224 'H': lambda: hex(node),
225 225 'R': lambda: str(r.rev(node)),
226 226 'h': lambda: short(node),
227 227 }
228 228 expander = {
229 229 '%': lambda: '%',
230 230 'b': lambda: os.path.basename(repo.root),
231 231 }
232 232
233 233 try:
234 234 if node:
235 235 expander.update(node_expander)
236 236 if node and revwidth is not None:
237 237 expander['r'] = lambda: str(r.rev(node)).zfill(revwidth)
238 238 if total is not None:
239 239 expander['N'] = lambda: str(total)
240 240 if seqno is not None:
241 241 expander['n'] = lambda: str(seqno)
242 242 if total is not None and seqno is not None:
243 243 expander['n'] = lambda:str(seqno).zfill(len(str(total)))
244 244 if pathname is not None:
245 245 expander['s'] = lambda: os.path.basename(pathname)
246 246 expander['d'] = lambda: os.path.dirname(pathname) or '.'
247 247 expander['p'] = lambda: pathname
248 248
249 249 newname = []
250 250 patlen = len(pat)
251 251 i = 0
252 252 while i < patlen:
253 253 c = pat[i]
254 254 if c == '%':
255 255 i += 1
256 256 c = pat[i]
257 257 c = expander[c]()
258 258 newname.append(c)
259 259 i += 1
260 260 return ''.join(newname)
261 261 except KeyError, inst:
262 262 raise util.Abort(_("invalid format spec '%%%s' in output file name"),
263 263 inst.args[0])
264 264
265 265 def make_file(repo, r, pat, node=None,
266 266 total=None, seqno=None, revwidth=None, mode='wb', pathname=None):
267 267 if not pat or pat == '-':
268 268 return 'w' in mode and sys.stdout or sys.stdin
269 269 if hasattr(pat, 'write') and 'w' in mode:
270 270 return pat
271 271 if hasattr(pat, 'read') and 'r' in mode:
272 272 return pat
273 273 return open(make_filename(repo, r, pat, node, total, seqno, revwidth,
274 274 pathname),
275 275 mode)
276 276
277 277 def write_bundle(cg, filename, compress=True, fh=None):
278 278 if fh is None:
279 279 fh = open(filename, "wb")
280 280
281 281 class nocompress(object):
282 282 def compress(self, x):
283 283 return x
284 284 def flush(self):
285 285 return ""
286 286 try:
287 287 if compress:
288 288 fh.write("HG10")
289 289 z = bz2.BZ2Compressor(9)
290 290 else:
291 291 fh.write("HG11")
292 292 z = nocompress()
293 293 while 1:
294 294 chunk = cg.read(4096)
295 295 if not chunk:
296 296 break
297 297 fh.write(z.compress(chunk))
298 298 fh.write(z.flush())
299 299 except:
300 fh.close()
300 301 os.unlink(filename)
301 302 raise
303 fh.close()
302 304
303 305 def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always,
304 306 changes=None, text=False, opts={}):
305 307 if not node1:
306 308 node1 = repo.dirstate.parents()[0]
307 309 # reading the data for node1 early allows it to play nicely
308 310 # with repo.changes and the revlog cache.
309 311 change = repo.changelog.read(node1)
310 312 mmap = repo.manifest.read(change[0])
311 313 date1 = util.datestr(change[2])
312 314
313 315 if not changes:
314 316 changes = repo.changes(node1, node2, files, match=match)
315 317 modified, added, removed, deleted, unknown = changes
316 318 if files:
317 319 modified, added, removed = map(lambda x: filterfiles(files, x),
318 320 (modified, added, removed))
319 321
320 322 if not modified and not added and not removed:
321 323 return
322 324
323 325 if node2:
324 326 change = repo.changelog.read(node2)
325 327 mmap2 = repo.manifest.read(change[0])
326 328 date2 = util.datestr(change[2])
327 329 def read(f):
328 330 return repo.file(f).read(mmap2[f])
329 331 else:
330 332 date2 = util.datestr()
331 333 def read(f):
332 334 return repo.wread(f)
333 335
334 336 if ui.quiet:
335 337 r = None
336 338 else:
337 339 hexfunc = ui.verbose and hex or short
338 340 r = [hexfunc(node) for node in [node1, node2] if node]
339 341
340 342 diffopts = ui.diffopts()
341 343 showfunc = opts.get('show_function') or diffopts['showfunc']
342 344 ignorews = opts.get('ignore_all_space') or diffopts['ignorews']
343 345 for f in modified:
344 346 to = None
345 347 if f in mmap:
346 348 to = repo.file(f).read(mmap[f])
347 349 tn = read(f)
348 350 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
349 351 showfunc=showfunc, ignorews=ignorews))
350 352 for f in added:
351 353 to = None
352 354 tn = read(f)
353 355 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
354 356 showfunc=showfunc, ignorews=ignorews))
355 357 for f in removed:
356 358 to = repo.file(f).read(mmap[f])
357 359 tn = None
358 360 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
359 361 showfunc=showfunc, ignorews=ignorews))
360 362
361 363 def trimuser(ui, name, rev, revcache):
362 364 """trim the name of the user who committed a change"""
363 365 user = revcache.get(rev)
364 366 if user is None:
365 367 user = revcache[rev] = ui.shortuser(name)
366 368 return user
367 369
368 370 class changeset_templater(object):
369 371 '''use templater module to format changeset information.'''
370 372
371 373 def __init__(self, ui, repo, mapfile):
372 374 self.t = templater.templater(mapfile, templater.common_filters,
373 375 cache={'parent': '{rev}:{node|short} ',
374 376 'manifest': '{rev}:{node|short}'})
375 377 self.ui = ui
376 378 self.repo = repo
377 379
378 380 def use_template(self, t):
379 381 '''set template string to use'''
380 382 self.t.cache['changeset'] = t
381 383
382 384 def write(self, thing):
383 385 '''write expanded template.
384 386 uses in-order recursive traverse of iterators.'''
385 387 for t in thing:
386 388 if hasattr(t, '__iter__'):
387 389 self.write(t)
388 390 else:
389 391 self.ui.write(t)
390 392
391 393 def show(self, rev=0, changenode=None, brinfo=None):
392 394 '''show a single changeset or file revision'''
393 395 log = self.repo.changelog
394 396 if changenode is None:
395 397 changenode = log.node(rev)
396 398 elif not rev:
397 399 rev = log.rev(changenode)
398 400
399 401 changes = log.read(changenode)
400 402
401 403 def showlist(name, values, plural=None, **args):
402 404 '''expand set of values.
403 405 name is name of key in template map.
404 406 values is list of strings or dicts.
405 407 plural is plural of name, if not simply name + 's'.
406 408
407 409 expansion works like this, given name 'foo'.
408 410
409 411 if values is empty, expand 'no_foos'.
410 412
411 413 if 'foo' not in template map, return values as a string,
412 414 joined by space.
413 415
414 416 expand 'start_foos'.
415 417
416 418 for each value, expand 'foo'. if 'last_foo' in template
417 419 map, expand it instead of 'foo' for last key.
418 420
419 421 expand 'end_foos'.
420 422 '''
421 423 if plural: names = plural
422 424 else: names = name + 's'
423 425 if not values:
424 426 noname = 'no_' + names
425 427 if noname in self.t:
426 428 yield self.t(noname, **args)
427 429 return
428 430 if name not in self.t:
429 431 if isinstance(values[0], str):
430 432 yield ' '.join(values)
431 433 else:
432 434 for v in values:
433 435 yield dict(v, **args)
434 436 return
435 437 startname = 'start_' + names
436 438 if startname in self.t:
437 439 yield self.t(startname, **args)
438 440 vargs = args.copy()
439 441 def one(v, tag=name):
440 442 try:
441 443 vargs.update(v)
442 444 except (AttributeError, ValueError):
443 445 try:
444 446 for a, b in v:
445 447 vargs[a] = b
446 448 except ValueError:
447 449 vargs[name] = v
448 450 return self.t(tag, **vargs)
449 451 lastname = 'last_' + name
450 452 if lastname in self.t:
451 453 last = values.pop()
452 454 else:
453 455 last = None
454 456 for v in values:
455 457 yield one(v)
456 458 if last is not None:
457 459 yield one(last, tag=lastname)
458 460 endname = 'end_' + names
459 461 if endname in self.t:
460 462 yield self.t(endname, **args)
461 463
462 464 if brinfo:
463 465 def showbranches(**args):
464 466 if changenode in brinfo:
465 467 for x in showlist('branch', brinfo[changenode],
466 468 plural='branches', **args):
467 469 yield x
468 470 else:
469 471 showbranches = ''
470 472
471 473 if self.ui.debugflag:
472 474 def showmanifest(**args):
473 475 args = args.copy()
474 476 args.update(dict(rev=self.repo.manifest.rev(changes[0]),
475 477 node=hex(changes[0])))
476 478 yield self.t('manifest', **args)
477 479 else:
478 480 showmanifest = ''
479 481
480 482 def showparents(**args):
481 483 parents = [[('rev', log.rev(p)), ('node', hex(p))]
482 484 for p in log.parents(changenode)
483 485 if self.ui.debugflag or p != nullid]
484 486 if (not self.ui.debugflag and len(parents) == 1 and
485 487 parents[0][0][1] == rev - 1):
486 488 return
487 489 for x in showlist('parent', parents, **args):
488 490 yield x
489 491
490 492 def showtags(**args):
491 493 for x in showlist('tag', self.repo.nodetags(changenode), **args):
492 494 yield x
493 495
494 496 if self.ui.debugflag:
495 497 files = self.repo.changes(log.parents(changenode)[0], changenode)
496 498 def showfiles(**args):
497 499 for x in showlist('file', files[0], **args): yield x
498 500 def showadds(**args):
499 501 for x in showlist('file_add', files[1], **args): yield x
500 502 def showdels(**args):
501 503 for x in showlist('file_del', files[2], **args): yield x
502 504 else:
503 505 def showfiles(**args):
504 506 for x in showlist('file', changes[3], **args): yield x
505 507 showadds = ''
506 508 showdels = ''
507 509
508 510 props = {
509 511 'author': changes[1],
510 512 'branches': showbranches,
511 513 'date': changes[2],
512 514 'desc': changes[4],
513 515 'file_adds': showadds,
514 516 'file_dels': showdels,
515 517 'files': showfiles,
516 518 'manifest': showmanifest,
517 519 'node': hex(changenode),
518 520 'parents': showparents,
519 521 'rev': rev,
520 522 'tags': showtags,
521 523 }
522 524
523 525 try:
524 526 if self.ui.debugflag and 'changeset_debug' in self.t:
525 527 key = 'changeset_debug'
526 528 elif self.ui.quiet and 'changeset_quiet' in self.t:
527 529 key = 'changeset_quiet'
528 530 elif self.ui.verbose and 'changeset_verbose' in self.t:
529 531 key = 'changeset_verbose'
530 532 else:
531 533 key = 'changeset'
532 534 self.write(self.t(key, **props))
533 535 except KeyError, inst:
534 536 raise util.Abort(_("%s: no key named '%s'") % (self.t.mapfile,
535 537 inst.args[0]))
536 538 except SyntaxError, inst:
537 539 raise util.Abort(_('%s: %s') % (self.t.mapfile, inst.args[0]))
538 540
539 541 class changeset_printer(object):
540 542 '''show changeset information when templating not requested.'''
541 543
542 544 def __init__(self, ui, repo):
543 545 self.ui = ui
544 546 self.repo = repo
545 547
546 548 def show(self, rev=0, changenode=None, brinfo=None):
547 549 '''show a single changeset or file revision'''
548 550 log = self.repo.changelog
549 551 if changenode is None:
550 552 changenode = log.node(rev)
551 553 elif not rev:
552 554 rev = log.rev(changenode)
553 555
554 556 if self.ui.quiet:
555 557 self.ui.write("%d:%s\n" % (rev, short(changenode)))
556 558 return
557 559
558 560 changes = log.read(changenode)
559 561 date = util.datestr(changes[2])
560 562
561 563 parents = [(log.rev(p), self.ui.verbose and hex(p) or short(p))
562 564 for p in log.parents(changenode)
563 565 if self.ui.debugflag or p != nullid]
564 566 if (not self.ui.debugflag and len(parents) == 1 and
565 567 parents[0][0] == rev-1):
566 568 parents = []
567 569
568 570 if self.ui.verbose:
569 571 self.ui.write(_("changeset: %d:%s\n") % (rev, hex(changenode)))
570 572 else:
571 573 self.ui.write(_("changeset: %d:%s\n") % (rev, short(changenode)))
572 574
573 575 for tag in self.repo.nodetags(changenode):
574 576 self.ui.status(_("tag: %s\n") % tag)
575 577 for parent in parents:
576 578 self.ui.write(_("parent: %d:%s\n") % parent)
577 579
578 580 if brinfo and changenode in brinfo:
579 581 br = brinfo[changenode]
580 582 self.ui.write(_("branch: %s\n") % " ".join(br))
581 583
582 584 self.ui.debug(_("manifest: %d:%s\n") %
583 585 (self.repo.manifest.rev(changes[0]), hex(changes[0])))
584 586 self.ui.status(_("user: %s\n") % changes[1])
585 587 self.ui.status(_("date: %s\n") % date)
586 588
587 589 if self.ui.debugflag:
588 590 files = self.repo.changes(log.parents(changenode)[0], changenode)
589 591 for key, value in zip([_("files:"), _("files+:"), _("files-:")],
590 592 files):
591 593 if value:
592 594 self.ui.note("%-12s %s\n" % (key, " ".join(value)))
593 595 else:
594 596 self.ui.note(_("files: %s\n") % " ".join(changes[3]))
595 597
596 598 description = changes[4].strip()
597 599 if description:
598 600 if self.ui.verbose:
599 601 self.ui.status(_("description:\n"))
600 602 self.ui.status(description)
601 603 self.ui.status("\n\n")
602 604 else:
603 605 self.ui.status(_("summary: %s\n") %
604 606 description.splitlines()[0])
605 607 self.ui.status("\n")
606 608
607 609 def show_changeset(ui, repo, opts):
608 610 '''show one changeset. uses template or regular display. caller
609 611 can pass in 'style' and 'template' options in opts.'''
610 612
611 613 tmpl = opts.get('template')
612 614 if tmpl:
613 615 tmpl = templater.parsestring(tmpl, quoted=False)
614 616 else:
615 617 tmpl = ui.config('ui', 'logtemplate')
616 618 if tmpl: tmpl = templater.parsestring(tmpl)
617 619 mapfile = opts.get('style') or ui.config('ui', 'style')
618 620 if tmpl or mapfile:
619 621 if mapfile:
620 622 if not os.path.isfile(mapfile):
621 623 mapname = templater.templatepath('map-cmdline.' + mapfile)
622 624 if not mapname: mapname = templater.templatepath(mapfile)
623 625 if mapname: mapfile = mapname
624 626 try:
625 627 t = changeset_templater(ui, repo, mapfile)
626 628 except SyntaxError, inst:
627 629 raise util.Abort(inst.args[0])
628 630 if tmpl: t.use_template(tmpl)
629 631 return t
630 632 return changeset_printer(ui, repo)
631 633
632 634 def show_version(ui):
633 635 """output version and copyright information"""
634 636 ui.write(_("Mercurial Distributed SCM (version %s)\n")
635 637 % version.get_version())
636 638 ui.status(_(
637 639 "\nCopyright (C) 2005 Matt Mackall <mpm@selenic.com>\n"
638 640 "This is free software; see the source for copying conditions. "
639 641 "There is NO\nwarranty; "
640 642 "not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
641 643 ))
642 644
643 645 def help_(ui, cmd=None, with_version=False):
644 646 """show help for a given command or all commands"""
645 647 option_lists = []
646 648 if cmd and cmd != 'shortlist':
647 649 if with_version:
648 650 show_version(ui)
649 651 ui.write('\n')
650 652 aliases, i = find(cmd)
651 653 # synopsis
652 654 ui.write("%s\n\n" % i[2])
653 655
654 656 # description
655 657 doc = i[0].__doc__
656 658 if not doc:
657 659 doc = _("(No help text available)")
658 660 if ui.quiet:
659 661 doc = doc.splitlines(0)[0]
660 662 ui.write("%s\n" % doc.rstrip())
661 663
662 664 if not ui.quiet:
663 665 # aliases
664 666 if len(aliases) > 1:
665 667 ui.write(_("\naliases: %s\n") % ', '.join(aliases[1:]))
666 668
667 669 # options
668 670 if i[1]:
669 671 option_lists.append(("options", i[1]))
670 672
671 673 else:
672 674 # program name
673 675 if ui.verbose or with_version:
674 676 show_version(ui)
675 677 else:
676 678 ui.status(_("Mercurial Distributed SCM\n"))
677 679 ui.status('\n')
678 680
679 681 # list of commands
680 682 if cmd == "shortlist":
681 683 ui.status(_('basic commands (use "hg help" '
682 684 'for the full list or option "-v" for details):\n\n'))
683 685 elif ui.verbose:
684 686 ui.status(_('list of commands:\n\n'))
685 687 else:
686 688 ui.status(_('list of commands (use "hg help -v" '
687 689 'to show aliases and global options):\n\n'))
688 690
689 691 h = {}
690 692 cmds = {}
691 693 for c, e in table.items():
692 694 f = c.split("|")[0]
693 695 if cmd == "shortlist" and not f.startswith("^"):
694 696 continue
695 697 f = f.lstrip("^")
696 698 if not ui.debugflag and f.startswith("debug"):
697 699 continue
698 700 doc = e[0].__doc__
699 701 if not doc:
700 702 doc = _("(No help text available)")
701 703 h[f] = doc.splitlines(0)[0].rstrip()
702 704 cmds[f] = c.lstrip("^")
703 705
704 706 fns = h.keys()
705 707 fns.sort()
706 708 m = max(map(len, fns))
707 709 for f in fns:
708 710 if ui.verbose:
709 711 commands = cmds[f].replace("|",", ")
710 712 ui.write(" %s:\n %s\n"%(commands, h[f]))
711 713 else:
712 714 ui.write(' %-*s %s\n' % (m, f, h[f]))
713 715
714 716 # global options
715 717 if ui.verbose:
716 718 option_lists.append(("global options", globalopts))
717 719
718 720 # list all option lists
719 721 opt_output = []
720 722 for title, options in option_lists:
721 723 opt_output.append(("\n%s:\n" % title, None))
722 724 for shortopt, longopt, default, desc in options:
723 725 opt_output.append(("%2s%s" % (shortopt and "-%s" % shortopt,
724 726 longopt and " --%s" % longopt),
725 727 "%s%s" % (desc,
726 728 default
727 729 and _(" (default: %s)") % default
728 730 or "")))
729 731
730 732 if opt_output:
731 733 opts_len = max([len(line[0]) for line in opt_output if line[1]])
732 734 for first, second in opt_output:
733 735 if second:
734 736 ui.write(" %-*s %s\n" % (opts_len, first, second))
735 737 else:
736 738 ui.write("%s\n" % first)
737 739
738 740 # Commands start here, listed alphabetically
739 741
740 742 def add(ui, repo, *pats, **opts):
741 743 """add the specified files on the next commit
742 744
743 745 Schedule files to be version controlled and added to the repository.
744 746
745 747 The files will be added to the repository at the next commit.
746 748
747 749 If no names are given, add all files in the repository.
748 750 """
749 751
750 752 names = []
751 753 for src, abs, rel, exact in walk(repo, pats, opts):
752 754 if exact:
753 755 if ui.verbose:
754 756 ui.status(_('adding %s\n') % rel)
755 757 names.append(abs)
756 758 elif repo.dirstate.state(abs) == '?':
757 759 ui.status(_('adding %s\n') % rel)
758 760 names.append(abs)
759 761 repo.add(names)
760 762
761 763 def addremove(ui, repo, *pats, **opts):
762 764 """add all new files, delete all missing files
763 765
764 766 Add all new files and remove all missing files from the repository.
765 767
766 768 New files are ignored if they match any of the patterns in .hgignore. As
767 769 with add, these changes take effect at the next commit.
768 770 """
769 771 return addremove_lock(ui, repo, pats, opts)
770 772
771 773 def addremove_lock(ui, repo, pats, opts, wlock=None):
772 774 add, remove = [], []
773 775 for src, abs, rel, exact in walk(repo, pats, opts):
774 776 if src == 'f' and repo.dirstate.state(abs) == '?':
775 777 add.append(abs)
776 778 if ui.verbose or not exact:
777 779 ui.status(_('adding %s\n') % ((pats and rel) or abs))
778 780 if repo.dirstate.state(abs) != 'r' and not os.path.exists(rel):
779 781 remove.append(abs)
780 782 if ui.verbose or not exact:
781 783 ui.status(_('removing %s\n') % ((pats and rel) or abs))
782 784 repo.add(add, wlock=wlock)
783 785 repo.remove(remove, wlock=wlock)
784 786
785 787 def annotate(ui, repo, *pats, **opts):
786 788 """show changeset information per file line
787 789
788 790 List changes in files, showing the revision id responsible for each line
789 791
790 792 This command is useful to discover who did a change or when a change took
791 793 place.
792 794
793 795 Without the -a option, annotate will avoid processing files it
794 796 detects as binary. With -a, annotate will generate an annotation
795 797 anyway, probably with undesirable results.
796 798 """
797 799 def getnode(rev):
798 800 return short(repo.changelog.node(rev))
799 801
800 802 ucache = {}
801 803 def getname(rev):
802 804 cl = repo.changelog.read(repo.changelog.node(rev))
803 805 return trimuser(ui, cl[1], rev, ucache)
804 806
805 807 dcache = {}
806 808 def getdate(rev):
807 809 datestr = dcache.get(rev)
808 810 if datestr is None:
809 811 cl = repo.changelog.read(repo.changelog.node(rev))
810 812 datestr = dcache[rev] = util.datestr(cl[2])
811 813 return datestr
812 814
813 815 if not pats:
814 816 raise util.Abort(_('at least one file name or pattern required'))
815 817
816 818 opmap = [['user', getname], ['number', str], ['changeset', getnode],
817 819 ['date', getdate]]
818 820 if not opts['user'] and not opts['changeset'] and not opts['date']:
819 821 opts['number'] = 1
820 822
821 823 if opts['rev']:
822 824 node = repo.changelog.lookup(opts['rev'])
823 825 else:
824 826 node = repo.dirstate.parents()[0]
825 827 change = repo.changelog.read(node)
826 828 mmap = repo.manifest.read(change[0])
827 829
828 830 for src, abs, rel, exact in walk(repo, pats, opts, node=node):
829 831 f = repo.file(abs)
830 832 if not opts['text'] and util.binary(f.read(mmap[abs])):
831 833 ui.write(_("%s: binary file\n") % ((pats and rel) or abs))
832 834 continue
833 835
834 836 lines = f.annotate(mmap[abs])
835 837 pieces = []
836 838
837 839 for o, f in opmap:
838 840 if opts[o]:
839 841 l = [f(n) for n, dummy in lines]
840 842 if l:
841 843 m = max(map(len, l))
842 844 pieces.append(["%*s" % (m, x) for x in l])
843 845
844 846 if pieces:
845 847 for p, l in zip(zip(*pieces), lines):
846 848 ui.write("%s: %s" % (" ".join(p), l[1]))
847 849
848 850 def bundle(ui, repo, fname, dest="default-push", **opts):
849 851 """create a changegroup file
850 852
851 853 Generate a compressed changegroup file collecting all changesets
852 854 not found in the other repository.
853 855
854 856 This file can then be transferred using conventional means and
855 857 applied to another repository with the unbundle command. This is
856 858 useful when native push and pull are not available or when
857 859 exporting an entire repository is undesirable. The standard file
858 860 extension is ".hg".
859 861
860 862 Unlike import/export, this exactly preserves all changeset
861 863 contents including permissions, rename data, and revision history.
862 864 """
863 865 dest = ui.expandpath(dest)
864 866 other = hg.repository(ui, dest)
865 867 o = repo.findoutgoing(other, force=opts['force'])
866 868 cg = repo.changegroup(o, 'bundle')
867 869 write_bundle(cg, fname)
868 870
869 871 def cat(ui, repo, file1, *pats, **opts):
870 872 """output the latest or given revisions of files
871 873
872 874 Print the specified files as they were at the given revision.
873 875 If no revision is given then the tip is used.
874 876
875 877 Output may be to a file, in which case the name of the file is
876 878 given using a format string. The formatting rules are the same as
877 879 for the export command, with the following additions:
878 880
879 881 %s basename of file being printed
880 882 %d dirname of file being printed, or '.' if in repo root
881 883 %p root-relative path name of file being printed
882 884 """
883 885 mf = {}
884 886 rev = opts['rev']
885 887 if rev:
886 888 node = repo.lookup(rev)
887 889 else:
888 890 node = repo.changelog.tip()
889 891 change = repo.changelog.read(node)
890 892 mf = repo.manifest.read(change[0])
891 893 for src, abs, rel, exact in walk(repo, (file1,) + pats, opts, node):
892 894 r = repo.file(abs)
893 895 n = mf[abs]
894 896 fp = make_file(repo, r, opts['output'], node=n, pathname=abs)
895 897 fp.write(r.read(n))
896 898
897 899 def clone(ui, source, dest=None, **opts):
898 900 """make a copy of an existing repository
899 901
900 902 Create a copy of an existing repository in a new directory.
901 903
902 904 If no destination directory name is specified, it defaults to the
903 905 basename of the source.
904 906
905 907 The location of the source is added to the new repository's
906 908 .hg/hgrc file, as the default to be used for future pulls.
907 909
908 910 For efficiency, hardlinks are used for cloning whenever the source
909 911 and destination are on the same filesystem. Some filesystems,
910 912 such as AFS, implement hardlinking incorrectly, but do not report
911 913 errors. In these cases, use the --pull option to avoid
912 914 hardlinking.
913 915
914 916 See pull for valid source format details.
915 917 """
916 918 if dest is None:
917 919 dest = os.path.basename(os.path.normpath(source))
918 920
919 921 if os.path.exists(dest):
920 922 raise util.Abort(_("destination '%s' already exists"), dest)
921 923
922 924 dest = os.path.realpath(dest)
923 925
924 926 class Dircleanup(object):
925 927 def __init__(self, dir_):
926 928 self.rmtree = shutil.rmtree
927 929 self.dir_ = dir_
928 930 os.mkdir(dir_)
929 931 def close(self):
930 932 self.dir_ = None
931 933 def __del__(self):
932 934 if self.dir_:
933 935 self.rmtree(self.dir_, True)
934 936
935 937 if opts['ssh']:
936 938 ui.setconfig("ui", "ssh", opts['ssh'])
937 939 if opts['remotecmd']:
938 940 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
939 941
940 942 source = ui.expandpath(source)
941 943
942 944 d = Dircleanup(dest)
943 945 abspath = source
944 946 other = hg.repository(ui, source)
945 947
946 948 copy = False
947 949 if other.dev() != -1:
948 950 abspath = os.path.abspath(source)
949 951 if not opts['pull'] and not opts['rev']:
950 952 copy = True
951 953
952 954 if copy:
953 955 try:
954 956 # we use a lock here because if we race with commit, we
955 957 # can end up with extra data in the cloned revlogs that's
956 958 # not pointed to by changesets, thus causing verify to
957 959 # fail
958 960 l1 = other.lock()
959 961 except lock.LockException:
960 962 copy = False
961 963
962 964 if copy:
963 965 # we lock here to avoid premature writing to the target
964 966 os.mkdir(os.path.join(dest, ".hg"))
965 967 l2 = lock.lock(os.path.join(dest, ".hg", "lock"))
966 968
967 969 files = "data 00manifest.d 00manifest.i 00changelog.d 00changelog.i"
968 970 for f in files.split():
969 971 src = os.path.join(source, ".hg", f)
970 972 dst = os.path.join(dest, ".hg", f)
971 973 try:
972 974 util.copyfiles(src, dst)
973 975 except OSError, inst:
974 976 if inst.errno != errno.ENOENT:
975 977 raise
976 978
977 979 repo = hg.repository(ui, dest)
978 980
979 981 else:
980 982 revs = None
981 983 if opts['rev']:
982 984 if not other.local():
983 985 error = _("clone -r not supported yet for remote repositories.")
984 986 raise util.Abort(error)
985 987 else:
986 988 revs = [other.lookup(rev) for rev in opts['rev']]
987 989 repo = hg.repository(ui, dest, create=1)
988 990 repo.pull(other, heads = revs)
989 991
990 992 f = repo.opener("hgrc", "w", text=True)
991 993 f.write("[paths]\n")
992 994 f.write("default = %s\n" % abspath)
993 995 f.close()
994 996
995 997 if not opts['noupdate']:
996 998 update(repo.ui, repo)
997 999
998 1000 d.close()
999 1001
1000 1002 def commit(ui, repo, *pats, **opts):
1001 1003 """commit the specified files or all outstanding changes
1002 1004
1003 1005 Commit changes to the given files into the repository.
1004 1006
1005 1007 If a list of files is omitted, all changes reported by "hg status"
1006 1008 will be commited.
1007 1009
1008 1010 The HGEDITOR or EDITOR environment variables are used to start an
1009 1011 editor to add a commit comment.
1010 1012 """
1011 1013 message = opts['message']
1012 1014 logfile = opts['logfile']
1013 1015
1014 1016 if message and logfile:
1015 1017 raise util.Abort(_('options --message and --logfile are mutually '
1016 1018 'exclusive'))
1017 1019 if not message and logfile:
1018 1020 try:
1019 1021 if logfile == '-':
1020 1022 message = sys.stdin.read()
1021 1023 else:
1022 1024 message = open(logfile).read()
1023 1025 except IOError, inst:
1024 1026 raise util.Abort(_("can't read commit message '%s': %s") %
1025 1027 (logfile, inst.strerror))
1026 1028
1027 1029 if opts['addremove']:
1028 1030 addremove(ui, repo, *pats, **opts)
1029 1031 fns, match, anypats = matchpats(repo, pats, opts)
1030 1032 if pats:
1031 1033 modified, added, removed, deleted, unknown = (
1032 1034 repo.changes(files=fns, match=match))
1033 1035 files = modified + added + removed
1034 1036 else:
1035 1037 files = []
1036 1038 try:
1037 1039 repo.commit(files, message, opts['user'], opts['date'], match)
1038 1040 except ValueError, inst:
1039 1041 raise util.Abort(str(inst))
1040 1042
1041 1043 def docopy(ui, repo, pats, opts, wlock):
1042 1044 # called with the repo lock held
1043 1045 cwd = repo.getcwd()
1044 1046 errors = 0
1045 1047 copied = []
1046 1048 targets = {}
1047 1049
1048 1050 def okaytocopy(abs, rel, exact):
1049 1051 reasons = {'?': _('is not managed'),
1050 1052 'a': _('has been marked for add'),
1051 1053 'r': _('has been marked for remove')}
1052 1054 state = repo.dirstate.state(abs)
1053 1055 reason = reasons.get(state)
1054 1056 if reason:
1055 1057 if state == 'a':
1056 1058 origsrc = repo.dirstate.copied(abs)
1057 1059 if origsrc is not None:
1058 1060 return origsrc
1059 1061 if exact:
1060 1062 ui.warn(_('%s: not copying - file %s\n') % (rel, reason))
1061 1063 else:
1062 1064 return abs
1063 1065
1064 1066 def copy(origsrc, abssrc, relsrc, target, exact):
1065 1067 abstarget = util.canonpath(repo.root, cwd, target)
1066 1068 reltarget = util.pathto(cwd, abstarget)
1067 1069 prevsrc = targets.get(abstarget)
1068 1070 if prevsrc is not None:
1069 1071 ui.warn(_('%s: not overwriting - %s collides with %s\n') %
1070 1072 (reltarget, abssrc, prevsrc))
1071 1073 return
1072 1074 if (not opts['after'] and os.path.exists(reltarget) or
1073 1075 opts['after'] and repo.dirstate.state(abstarget) not in '?r'):
1074 1076 if not opts['force']:
1075 1077 ui.warn(_('%s: not overwriting - file exists\n') %
1076 1078 reltarget)
1077 1079 return
1078 1080 if not opts['after']:
1079 1081 os.unlink(reltarget)
1080 1082 if opts['after']:
1081 1083 if not os.path.exists(reltarget):
1082 1084 return
1083 1085 else:
1084 1086 targetdir = os.path.dirname(reltarget) or '.'
1085 1087 if not os.path.isdir(targetdir):
1086 1088 os.makedirs(targetdir)
1087 1089 try:
1088 1090 restore = repo.dirstate.state(abstarget) == 'r'
1089 1091 if restore:
1090 1092 repo.undelete([abstarget], wlock)
1091 1093 try:
1092 1094 shutil.copyfile(relsrc, reltarget)
1093 1095 shutil.copymode(relsrc, reltarget)
1094 1096 restore = False
1095 1097 finally:
1096 1098 if restore:
1097 1099 repo.remove([abstarget], wlock)
1098 1100 except shutil.Error, inst:
1099 1101 raise util.Abort(str(inst))
1100 1102 except IOError, inst:
1101 1103 if inst.errno == errno.ENOENT:
1102 1104 ui.warn(_('%s: deleted in working copy\n') % relsrc)
1103 1105 else:
1104 1106 ui.warn(_('%s: cannot copy - %s\n') %
1105 1107 (relsrc, inst.strerror))
1106 1108 errors += 1
1107 1109 return
1108 1110 if ui.verbose or not exact:
1109 1111 ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
1110 1112 targets[abstarget] = abssrc
1111 1113 if abstarget != origsrc:
1112 1114 repo.copy(origsrc, abstarget, wlock)
1113 1115 copied.append((abssrc, relsrc, exact))
1114 1116
1115 1117 def targetpathfn(pat, dest, srcs):
1116 1118 if os.path.isdir(pat):
1117 1119 abspfx = util.canonpath(repo.root, cwd, pat)
1118 1120 if destdirexists:
1119 1121 striplen = len(os.path.split(abspfx)[0])
1120 1122 else:
1121 1123 striplen = len(abspfx)
1122 1124 if striplen:
1123 1125 striplen += len(os.sep)
1124 1126 res = lambda p: os.path.join(dest, p[striplen:])
1125 1127 elif destdirexists:
1126 1128 res = lambda p: os.path.join(dest, os.path.basename(p))
1127 1129 else:
1128 1130 res = lambda p: dest
1129 1131 return res
1130 1132
1131 1133 def targetpathafterfn(pat, dest, srcs):
1132 1134 if util.patkind(pat, None)[0]:
1133 1135 # a mercurial pattern
1134 1136 res = lambda p: os.path.join(dest, os.path.basename(p))
1135 1137 else:
1136 1138 abspfx = util.canonpath(repo.root, cwd, pat)
1137 1139 if len(abspfx) < len(srcs[0][0]):
1138 1140 # A directory. Either the target path contains the last
1139 1141 # component of the source path or it does not.
1140 1142 def evalpath(striplen):
1141 1143 score = 0
1142 1144 for s in srcs:
1143 1145 t = os.path.join(dest, s[0][striplen:])
1144 1146 if os.path.exists(t):
1145 1147 score += 1
1146 1148 return score
1147 1149
1148 1150 striplen = len(abspfx)
1149 1151 if striplen:
1150 1152 striplen += len(os.sep)
1151 1153 if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
1152 1154 score = evalpath(striplen)
1153 1155 striplen1 = len(os.path.split(abspfx)[0])
1154 1156 if striplen1:
1155 1157 striplen1 += len(os.sep)
1156 1158 if evalpath(striplen1) > score:
1157 1159 striplen = striplen1
1158 1160 res = lambda p: os.path.join(dest, p[striplen:])
1159 1161 else:
1160 1162 # a file
1161 1163 if destdirexists:
1162 1164 res = lambda p: os.path.join(dest, os.path.basename(p))
1163 1165 else:
1164 1166 res = lambda p: dest
1165 1167 return res
1166 1168
1167 1169
1168 1170 pats = list(pats)
1169 1171 if not pats:
1170 1172 raise util.Abort(_('no source or destination specified'))
1171 1173 if len(pats) == 1:
1172 1174 raise util.Abort(_('no destination specified'))
1173 1175 dest = pats.pop()
1174 1176 destdirexists = os.path.isdir(dest)
1175 1177 if (len(pats) > 1 or util.patkind(pats[0], None)[0]) and not destdirexists:
1176 1178 raise util.Abort(_('with multiple sources, destination must be an '
1177 1179 'existing directory'))
1178 1180 if opts['after']:
1179 1181 tfn = targetpathafterfn
1180 1182 else:
1181 1183 tfn = targetpathfn
1182 1184 copylist = []
1183 1185 for pat in pats:
1184 1186 srcs = []
1185 1187 for tag, abssrc, relsrc, exact in walk(repo, [pat], opts):
1186 1188 origsrc = okaytocopy(abssrc, relsrc, exact)
1187 1189 if origsrc:
1188 1190 srcs.append((origsrc, abssrc, relsrc, exact))
1189 1191 if not srcs:
1190 1192 continue
1191 1193 copylist.append((tfn(pat, dest, srcs), srcs))
1192 1194 if not copylist:
1193 1195 raise util.Abort(_('no files to copy'))
1194 1196
1195 1197 for targetpath, srcs in copylist:
1196 1198 for origsrc, abssrc, relsrc, exact in srcs:
1197 1199 copy(origsrc, abssrc, relsrc, targetpath(abssrc), exact)
1198 1200
1199 1201 if errors:
1200 1202 ui.warn(_('(consider using --after)\n'))
1201 1203 return errors, copied
1202 1204
1203 1205 def copy(ui, repo, *pats, **opts):
1204 1206 """mark files as copied for the next commit
1205 1207
1206 1208 Mark dest as having copies of source files. If dest is a
1207 1209 directory, copies are put in that directory. If dest is a file,
1208 1210 there can only be one source.
1209 1211
1210 1212 By default, this command copies the contents of files as they
1211 1213 stand in the working directory. If invoked with --after, the
1212 1214 operation is recorded, but no copying is performed.
1213 1215
1214 1216 This command takes effect in the next commit.
1215 1217
1216 1218 NOTE: This command should be treated as experimental. While it
1217 1219 should properly record copied files, this information is not yet
1218 1220 fully used by merge, nor fully reported by log.
1219 1221 """
1220 1222 try:
1221 1223 wlock = repo.wlock(0)
1222 1224 errs, copied = docopy(ui, repo, pats, opts, wlock)
1223 1225 except lock.LockHeld, inst:
1224 1226 ui.warn(_("repository lock held by %s\n") % inst.args[0])
1225 1227 errs = 1
1226 1228 return errs
1227 1229
1228 1230 def debugancestor(ui, index, rev1, rev2):
1229 1231 """find the ancestor revision of two revisions in a given index"""
1230 1232 r = revlog.revlog(util.opener(os.getcwd(), audit=False), index, "")
1231 1233 a = r.ancestor(r.lookup(rev1), r.lookup(rev2))
1232 1234 ui.write("%d:%s\n" % (r.rev(a), hex(a)))
1233 1235
1234 1236 def debugcomplete(ui, cmd):
1235 1237 """returns the completion list associated with the given command"""
1236 1238 clist = findpossible(cmd).keys()
1237 1239 clist.sort()
1238 1240 ui.write("%s\n" % " ".join(clist))
1239 1241
1240 1242 def debugrebuildstate(ui, repo, rev=None):
1241 1243 """rebuild the dirstate as it would look like for the given revision"""
1242 1244 if not rev:
1243 1245 rev = repo.changelog.tip()
1244 1246 else:
1245 1247 rev = repo.lookup(rev)
1246 1248 change = repo.changelog.read(rev)
1247 1249 n = change[0]
1248 1250 files = repo.manifest.readflags(n)
1249 1251 wlock = repo.wlock()
1250 1252 repo.dirstate.rebuild(rev, files.iteritems())
1251 1253
1252 1254 def debugcheckstate(ui, repo):
1253 1255 """validate the correctness of the current dirstate"""
1254 1256 parent1, parent2 = repo.dirstate.parents()
1255 1257 repo.dirstate.read()
1256 1258 dc = repo.dirstate.map
1257 1259 keys = dc.keys()
1258 1260 keys.sort()
1259 1261 m1n = repo.changelog.read(parent1)[0]
1260 1262 m2n = repo.changelog.read(parent2)[0]
1261 1263 m1 = repo.manifest.read(m1n)
1262 1264 m2 = repo.manifest.read(m2n)
1263 1265 errors = 0
1264 1266 for f in dc:
1265 1267 state = repo.dirstate.state(f)
1266 1268 if state in "nr" and f not in m1:
1267 1269 ui.warn(_("%s in state %s, but not in manifest1\n") % (f, state))
1268 1270 errors += 1
1269 1271 if state in "a" and f in m1:
1270 1272 ui.warn(_("%s in state %s, but also in manifest1\n") % (f, state))
1271 1273 errors += 1
1272 1274 if state in "m" and f not in m1 and f not in m2:
1273 1275 ui.warn(_("%s in state %s, but not in either manifest\n") %
1274 1276 (f, state))
1275 1277 errors += 1
1276 1278 for f in m1:
1277 1279 state = repo.dirstate.state(f)
1278 1280 if state not in "nrm":
1279 1281 ui.warn(_("%s in manifest1, but listed as state %s") % (f, state))
1280 1282 errors += 1
1281 1283 if errors:
1282 1284 error = _(".hg/dirstate inconsistent with current parent's manifest")
1283 1285 raise util.Abort(error)
1284 1286
1285 1287 def debugconfig(ui, repo):
1286 1288 """show combined config settings from all hgrc files"""
1287 1289 for section, name, value in ui.walkconfig():
1288 1290 ui.write('%s.%s=%s\n' % (section, name, value))
1289 1291
1290 1292 def debugsetparents(ui, repo, rev1, rev2=None):
1291 1293 """manually set the parents of the current working directory
1292 1294
1293 1295 This is useful for writing repository conversion tools, but should
1294 1296 be used with care.
1295 1297 """
1296 1298
1297 1299 if not rev2:
1298 1300 rev2 = hex(nullid)
1299 1301
1300 1302 repo.dirstate.setparents(repo.lookup(rev1), repo.lookup(rev2))
1301 1303
1302 1304 def debugstate(ui, repo):
1303 1305 """show the contents of the current dirstate"""
1304 1306 repo.dirstate.read()
1305 1307 dc = repo.dirstate.map
1306 1308 keys = dc.keys()
1307 1309 keys.sort()
1308 1310 for file_ in keys:
1309 1311 ui.write("%c %3o %10d %s %s\n"
1310 1312 % (dc[file_][0], dc[file_][1] & 0777, dc[file_][2],
1311 1313 time.strftime("%x %X",
1312 1314 time.localtime(dc[file_][3])), file_))
1313 1315 for f in repo.dirstate.copies:
1314 1316 ui.write(_("copy: %s -> %s\n") % (repo.dirstate.copies[f], f))
1315 1317
1316 1318 def debugdata(ui, file_, rev):
1317 1319 """dump the contents of an data file revision"""
1318 1320 r = revlog.revlog(util.opener(os.getcwd(), audit=False),
1319 1321 file_[:-2] + ".i", file_)
1320 1322 try:
1321 1323 ui.write(r.revision(r.lookup(rev)))
1322 1324 except KeyError:
1323 1325 raise util.Abort(_('invalid revision identifier %s'), rev)
1324 1326
1325 1327 def debugindex(ui, file_):
1326 1328 """dump the contents of an index file"""
1327 1329 r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_, "")
1328 1330 ui.write(" rev offset length base linkrev" +
1329 1331 " nodeid p1 p2\n")
1330 1332 for i in range(r.count()):
1331 1333 e = r.index[i]
1332 1334 ui.write("% 6d % 9d % 7d % 6d % 7d %s %s %s\n" % (
1333 1335 i, e[0], e[1], e[2], e[3],
1334 1336 short(e[6]), short(e[4]), short(e[5])))
1335 1337
1336 1338 def debugindexdot(ui, file_):
1337 1339 """dump an index DAG as a .dot file"""
1338 1340 r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_, "")
1339 1341 ui.write("digraph G {\n")
1340 1342 for i in range(r.count()):
1341 1343 e = r.index[i]
1342 1344 ui.write("\t%d -> %d\n" % (r.rev(e[4]), i))
1343 1345 if e[5] != nullid:
1344 1346 ui.write("\t%d -> %d\n" % (r.rev(e[5]), i))
1345 1347 ui.write("}\n")
1346 1348
1347 1349 def debugrename(ui, repo, file, rev=None):
1348 1350 """dump rename information"""
1349 1351 r = repo.file(relpath(repo, [file])[0])
1350 1352 if rev:
1351 1353 try:
1352 1354 # assume all revision numbers are for changesets
1353 1355 n = repo.lookup(rev)
1354 1356 change = repo.changelog.read(n)
1355 1357 m = repo.manifest.read(change[0])
1356 1358 n = m[relpath(repo, [file])[0]]
1357 1359 except (hg.RepoError, KeyError):
1358 1360 n = r.lookup(rev)
1359 1361 else:
1360 1362 n = r.tip()
1361 1363 m = r.renamed(n)
1362 1364 if m:
1363 1365 ui.write(_("renamed from %s:%s\n") % (m[0], hex(m[1])))
1364 1366 else:
1365 1367 ui.write(_("not renamed\n"))
1366 1368
1367 1369 def debugwalk(ui, repo, *pats, **opts):
1368 1370 """show how files match on given patterns"""
1369 1371 items = list(walk(repo, pats, opts))
1370 1372 if not items:
1371 1373 return
1372 1374 fmt = '%%s %%-%ds %%-%ds %%s' % (
1373 1375 max([len(abs) for (src, abs, rel, exact) in items]),
1374 1376 max([len(rel) for (src, abs, rel, exact) in items]))
1375 1377 for src, abs, rel, exact in items:
1376 1378 line = fmt % (src, abs, rel, exact and 'exact' or '')
1377 1379 ui.write("%s\n" % line.rstrip())
1378 1380
1379 1381 def diff(ui, repo, *pats, **opts):
1380 1382 """diff repository (or selected files)
1381 1383
1382 1384 Show differences between revisions for the specified files.
1383 1385
1384 1386 Differences between files are shown using the unified diff format.
1385 1387
1386 1388 When two revision arguments are given, then changes are shown
1387 1389 between those revisions. If only one revision is specified then
1388 1390 that revision is compared to the working directory, and, when no
1389 1391 revisions are specified, the working directory files are compared
1390 1392 to its parent.
1391 1393
1392 1394 Without the -a option, diff will avoid generating diffs of files
1393 1395 it detects as binary. With -a, diff will generate a diff anyway,
1394 1396 probably with undesirable results.
1395 1397 """
1396 1398 node1, node2 = None, None
1397 1399 revs = [repo.lookup(x) for x in opts['rev']]
1398 1400
1399 1401 if len(revs) > 0:
1400 1402 node1 = revs[0]
1401 1403 if len(revs) > 1:
1402 1404 node2 = revs[1]
1403 1405 if len(revs) > 2:
1404 1406 raise util.Abort(_("too many revisions to diff"))
1405 1407
1406 1408 fns, matchfn, anypats = matchpats(repo, pats, opts)
1407 1409
1408 1410 dodiff(sys.stdout, ui, repo, node1, node2, fns, match=matchfn,
1409 1411 text=opts['text'], opts=opts)
1410 1412
1411 1413 def doexport(ui, repo, changeset, seqno, total, revwidth, opts):
1412 1414 node = repo.lookup(changeset)
1413 1415 parents = [p for p in repo.changelog.parents(node) if p != nullid]
1414 1416 if opts['switch_parent']:
1415 1417 parents.reverse()
1416 1418 prev = (parents and parents[0]) or nullid
1417 1419 change = repo.changelog.read(node)
1418 1420
1419 1421 fp = make_file(repo, repo.changelog, opts['output'],
1420 1422 node=node, total=total, seqno=seqno,
1421 1423 revwidth=revwidth)
1422 1424 if fp != sys.stdout:
1423 1425 ui.note("%s\n" % fp.name)
1424 1426
1425 1427 fp.write("# HG changeset patch\n")
1426 1428 fp.write("# User %s\n" % change[1])
1427 1429 fp.write("# Node ID %s\n" % hex(node))
1428 1430 fp.write("# Parent %s\n" % hex(prev))
1429 1431 if len(parents) > 1:
1430 1432 fp.write("# Parent %s\n" % hex(parents[1]))
1431 1433 fp.write(change[4].rstrip())
1432 1434 fp.write("\n\n")
1433 1435
1434 1436 dodiff(fp, ui, repo, prev, node, text=opts['text'])
1435 1437 if fp != sys.stdout:
1436 1438 fp.close()
1437 1439
1438 1440 def export(ui, repo, *changesets, **opts):
1439 1441 """dump the header and diffs for one or more changesets
1440 1442
1441 1443 Print the changeset header and diffs for one or more revisions.
1442 1444
1443 1445 The information shown in the changeset header is: author,
1444 1446 changeset hash, parent and commit comment.
1445 1447
1446 1448 Output may be to a file, in which case the name of the file is
1447 1449 given using a format string. The formatting rules are as follows:
1448 1450
1449 1451 %% literal "%" character
1450 1452 %H changeset hash (40 bytes of hexadecimal)
1451 1453 %N number of patches being generated
1452 1454 %R changeset revision number
1453 1455 %b basename of the exporting repository
1454 1456 %h short-form changeset hash (12 bytes of hexadecimal)
1455 1457 %n zero-padded sequence number, starting at 1
1456 1458 %r zero-padded changeset revision number
1457 1459
1458 1460 Without the -a option, export will avoid generating diffs of files
1459 1461 it detects as binary. With -a, export will generate a diff anyway,
1460 1462 probably with undesirable results.
1461 1463
1462 1464 With the --switch-parent option, the diff will be against the second
1463 1465 parent. It can be useful to review a merge.
1464 1466 """
1465 1467 if not changesets:
1466 1468 raise util.Abort(_("export requires at least one changeset"))
1467 1469 seqno = 0
1468 1470 revs = list(revrange(ui, repo, changesets))
1469 1471 total = len(revs)
1470 1472 revwidth = max(map(len, revs))
1471 1473 msg = len(revs) > 1 and _("Exporting patches:\n") or _("Exporting patch:\n")
1472 1474 ui.note(msg)
1473 1475 for cset in revs:
1474 1476 seqno += 1
1475 1477 doexport(ui, repo, cset, seqno, total, revwidth, opts)
1476 1478
1477 1479 def forget(ui, repo, *pats, **opts):
1478 1480 """don't add the specified files on the next commit
1479 1481
1480 1482 Undo an 'hg add' scheduled for the next commit.
1481 1483 """
1482 1484 forget = []
1483 1485 for src, abs, rel, exact in walk(repo, pats, opts):
1484 1486 if repo.dirstate.state(abs) == 'a':
1485 1487 forget.append(abs)
1486 1488 if ui.verbose or not exact:
1487 1489 ui.status(_('forgetting %s\n') % ((pats and rel) or abs))
1488 1490 repo.forget(forget)
1489 1491
1490 1492 def grep(ui, repo, pattern, *pats, **opts):
1491 1493 """search for a pattern in specified files and revisions
1492 1494
1493 1495 Search revisions of files for a regular expression.
1494 1496
1495 1497 This command behaves differently than Unix grep. It only accepts
1496 1498 Python/Perl regexps. It searches repository history, not the
1497 1499 working directory. It always prints the revision number in which
1498 1500 a match appears.
1499 1501
1500 1502 By default, grep only prints output for the first revision of a
1501 1503 file in which it finds a match. To get it to print every revision
1502 1504 that contains a change in match status ("-" for a match that
1503 1505 becomes a non-match, or "+" for a non-match that becomes a match),
1504 1506 use the --all flag.
1505 1507 """
1506 1508 reflags = 0
1507 1509 if opts['ignore_case']:
1508 1510 reflags |= re.I
1509 1511 regexp = re.compile(pattern, reflags)
1510 1512 sep, eol = ':', '\n'
1511 1513 if opts['print0']:
1512 1514 sep = eol = '\0'
1513 1515
1514 1516 fcache = {}
1515 1517 def getfile(fn):
1516 1518 if fn not in fcache:
1517 1519 fcache[fn] = repo.file(fn)
1518 1520 return fcache[fn]
1519 1521
1520 1522 def matchlines(body):
1521 1523 begin = 0
1522 1524 linenum = 0
1523 1525 while True:
1524 1526 match = regexp.search(body, begin)
1525 1527 if not match:
1526 1528 break
1527 1529 mstart, mend = match.span()
1528 1530 linenum += body.count('\n', begin, mstart) + 1
1529 1531 lstart = body.rfind('\n', begin, mstart) + 1 or begin
1530 1532 lend = body.find('\n', mend)
1531 1533 yield linenum, mstart - lstart, mend - lstart, body[lstart:lend]
1532 1534 begin = lend + 1
1533 1535
1534 1536 class linestate(object):
1535 1537 def __init__(self, line, linenum, colstart, colend):
1536 1538 self.line = line
1537 1539 self.linenum = linenum
1538 1540 self.colstart = colstart
1539 1541 self.colend = colend
1540 1542 def __eq__(self, other):
1541 1543 return self.line == other.line
1542 1544 def __hash__(self):
1543 1545 return hash(self.line)
1544 1546
1545 1547 matches = {}
1546 1548 def grepbody(fn, rev, body):
1547 1549 matches[rev].setdefault(fn, {})
1548 1550 m = matches[rev][fn]
1549 1551 for lnum, cstart, cend, line in matchlines(body):
1550 1552 s = linestate(line, lnum, cstart, cend)
1551 1553 m[s] = s
1552 1554
1553 1555 # FIXME: prev isn't used, why ?
1554 1556 prev = {}
1555 1557 ucache = {}
1556 1558 def display(fn, rev, states, prevstates):
1557 1559 diff = list(sets.Set(states).symmetric_difference(sets.Set(prevstates)))
1558 1560 diff.sort(lambda x, y: cmp(x.linenum, y.linenum))
1559 1561 counts = {'-': 0, '+': 0}
1560 1562 filerevmatches = {}
1561 1563 for l in diff:
1562 1564 if incrementing or not opts['all']:
1563 1565 change = ((l in prevstates) and '-') or '+'
1564 1566 r = rev
1565 1567 else:
1566 1568 change = ((l in states) and '-') or '+'
1567 1569 r = prev[fn]
1568 1570 cols = [fn, str(rev)]
1569 1571 if opts['line_number']:
1570 1572 cols.append(str(l.linenum))
1571 1573 if opts['all']:
1572 1574 cols.append(change)
1573 1575 if opts['user']:
1574 1576 cols.append(trimuser(ui, getchange(rev)[1], rev,
1575 1577 ucache))
1576 1578 if opts['files_with_matches']:
1577 1579 c = (fn, rev)
1578 1580 if c in filerevmatches:
1579 1581 continue
1580 1582 filerevmatches[c] = 1
1581 1583 else:
1582 1584 cols.append(l.line)
1583 1585 ui.write(sep.join(cols), eol)
1584 1586 counts[change] += 1
1585 1587 return counts['+'], counts['-']
1586 1588
1587 1589 fstate = {}
1588 1590 skip = {}
1589 1591 changeiter, getchange, matchfn = walkchangerevs(ui, repo, pats, opts)
1590 1592 count = 0
1591 1593 incrementing = False
1592 1594 for st, rev, fns in changeiter:
1593 1595 if st == 'window':
1594 1596 incrementing = rev
1595 1597 matches.clear()
1596 1598 elif st == 'add':
1597 1599 change = repo.changelog.read(repo.lookup(str(rev)))
1598 1600 mf = repo.manifest.read(change[0])
1599 1601 matches[rev] = {}
1600 1602 for fn in fns:
1601 1603 if fn in skip:
1602 1604 continue
1603 1605 fstate.setdefault(fn, {})
1604 1606 try:
1605 1607 grepbody(fn, rev, getfile(fn).read(mf[fn]))
1606 1608 except KeyError:
1607 1609 pass
1608 1610 elif st == 'iter':
1609 1611 states = matches[rev].items()
1610 1612 states.sort()
1611 1613 for fn, m in states:
1612 1614 if fn in skip:
1613 1615 continue
1614 1616 if incrementing or not opts['all'] or fstate[fn]:
1615 1617 pos, neg = display(fn, rev, m, fstate[fn])
1616 1618 count += pos + neg
1617 1619 if pos and not opts['all']:
1618 1620 skip[fn] = True
1619 1621 fstate[fn] = m
1620 1622 prev[fn] = rev
1621 1623
1622 1624 if not incrementing:
1623 1625 fstate = fstate.items()
1624 1626 fstate.sort()
1625 1627 for fn, state in fstate:
1626 1628 if fn in skip:
1627 1629 continue
1628 1630 display(fn, rev, {}, state)
1629 1631 return (count == 0 and 1) or 0
1630 1632
1631 1633 def heads(ui, repo, **opts):
1632 1634 """show current repository heads
1633 1635
1634 1636 Show all repository head changesets.
1635 1637
1636 1638 Repository "heads" are changesets that don't have children
1637 1639 changesets. They are where development generally takes place and
1638 1640 are the usual targets for update and merge operations.
1639 1641 """
1640 1642 if opts['rev']:
1641 1643 heads = repo.heads(repo.lookup(opts['rev']))
1642 1644 else:
1643 1645 heads = repo.heads()
1644 1646 br = None
1645 1647 if opts['branches']:
1646 1648 br = repo.branchlookup(heads)
1647 1649 displayer = show_changeset(ui, repo, opts)
1648 1650 for n in heads:
1649 1651 displayer.show(changenode=n, brinfo=br)
1650 1652
1651 1653 def identify(ui, repo):
1652 1654 """print information about the working copy
1653 1655
1654 1656 Print a short summary of the current state of the repo.
1655 1657
1656 1658 This summary identifies the repository state using one or two parent
1657 1659 hash identifiers, followed by a "+" if there are uncommitted changes
1658 1660 in the working directory, followed by a list of tags for this revision.
1659 1661 """
1660 1662 parents = [p for p in repo.dirstate.parents() if p != nullid]
1661 1663 if not parents:
1662 1664 ui.write(_("unknown\n"))
1663 1665 return
1664 1666
1665 1667 hexfunc = ui.verbose and hex or short
1666 1668 modified, added, removed, deleted, unknown = repo.changes()
1667 1669 output = ["%s%s" %
1668 1670 ('+'.join([hexfunc(parent) for parent in parents]),
1669 1671 (modified or added or removed or deleted) and "+" or "")]
1670 1672
1671 1673 if not ui.quiet:
1672 1674 # multiple tags for a single parent separated by '/'
1673 1675 parenttags = ['/'.join(tags)
1674 1676 for tags in map(repo.nodetags, parents) if tags]
1675 1677 # tags for multiple parents separated by ' + '
1676 1678 if parenttags:
1677 1679 output.append(' + '.join(parenttags))
1678 1680
1679 1681 ui.write("%s\n" % ' '.join(output))
1680 1682
1681 1683 def import_(ui, repo, patch1, *patches, **opts):
1682 1684 """import an ordered set of patches
1683 1685
1684 1686 Import a list of patches and commit them individually.
1685 1687
1686 1688 If there are outstanding changes in the working directory, import
1687 1689 will abort unless given the -f flag.
1688 1690
1689 1691 If a patch looks like a mail message (its first line starts with
1690 1692 "From " or looks like an RFC822 header), it will not be applied
1691 1693 unless the -f option is used. The importer neither parses nor
1692 1694 discards mail headers, so use -f only to override the "mailness"
1693 1695 safety check, not to import a real mail message.
1694 1696 """
1695 1697 patches = (patch1,) + patches
1696 1698
1697 1699 if not opts['force']:
1698 1700 modified, added, removed, deleted, unknown = repo.changes()
1699 1701 if modified or added or removed or deleted:
1700 1702 raise util.Abort(_("outstanding uncommitted changes"))
1701 1703
1702 1704 d = opts["base"]
1703 1705 strip = opts["strip"]
1704 1706
1705 1707 mailre = re.compile(r'(?:From |[\w-]+:)')
1706 1708
1707 1709 # attempt to detect the start of a patch
1708 1710 # (this heuristic is borrowed from quilt)
1709 1711 diffre = re.compile(r'(?:Index:[ \t]|diff[ \t]|RCS file: |' +
1710 1712 'retrieving revision [0-9]+(\.[0-9]+)*$|' +
1711 1713 '(---|\*\*\*)[ \t])')
1712 1714
1713 1715 for patch in patches:
1714 1716 ui.status(_("applying %s\n") % patch)
1715 1717 pf = os.path.join(d, patch)
1716 1718
1717 1719 message = []
1718 1720 user = None
1719 1721 hgpatch = False
1720 1722 for line in file(pf):
1721 1723 line = line.rstrip()
1722 1724 if (not message and not hgpatch and
1723 1725 mailre.match(line) and not opts['force']):
1724 1726 if len(line) > 35:
1725 1727 line = line[:32] + '...'
1726 1728 raise util.Abort(_('first line looks like a '
1727 1729 'mail header: ') + line)
1728 1730 if diffre.match(line):
1729 1731 break
1730 1732 elif hgpatch:
1731 1733 # parse values when importing the result of an hg export
1732 1734 if line.startswith("# User "):
1733 1735 user = line[7:]
1734 1736 ui.debug(_('User: %s\n') % user)
1735 1737 elif not line.startswith("# ") and line:
1736 1738 message.append(line)
1737 1739 hgpatch = False
1738 1740 elif line == '# HG changeset patch':
1739 1741 hgpatch = True
1740 1742 message = [] # We may have collected garbage
1741 1743 else:
1742 1744 message.append(line)
1743 1745
1744 1746 # make sure message isn't empty
1745 1747 if not message:
1746 1748 message = _("imported patch %s\n") % patch
1747 1749 else:
1748 1750 message = "%s\n" % '\n'.join(message)
1749 1751 ui.debug(_('message:\n%s\n') % message)
1750 1752
1751 1753 files = util.patch(strip, pf, ui)
1752 1754
1753 1755 if len(files) > 0:
1754 1756 addremove(ui, repo, *files)
1755 1757 repo.commit(files, message, user)
1756 1758
1757 1759 def incoming(ui, repo, source="default", **opts):
1758 1760 """show new changesets found in source
1759 1761
1760 1762 Show new changesets found in the specified repo or the default
1761 1763 pull repo. These are the changesets that would be pulled if a pull
1762 1764 was requested.
1763 1765
1764 1766 For remote repository, using --bundle avoids downloading the changesets
1765 1767 twice if the incoming is followed by a pull.
1766 1768 """
1767 1769 source = ui.expandpath(source)
1768 1770 if opts['ssh']:
1769 1771 ui.setconfig("ui", "ssh", opts['ssh'])
1770 1772 if opts['remotecmd']:
1771 1773 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
1772 1774
1773 1775 other = hg.repository(ui, source)
1774 1776 incoming = repo.findincoming(other, force=opts["force"])
1775 1777 if not incoming:
1776 1778 return
1777 1779
1778 1780 cleanup = None
1779 1781 if not other.local() or opts["bundle"]:
1780 1782 # create an uncompressed bundle
1781 1783 if not opts["bundle"]:
1782 1784 # create a temporary bundle
1783 1785 fd, fname = tempfile.mkstemp(suffix=".hg",
1784 1786 prefix="tmp-hg-incoming")
1785 1787 f = os.fdopen(fd, "wb")
1786 1788 cleanup = fname
1787 1789 else:
1788 1790 fname = opts["bundle"]
1789 1791 f = open(fname, "wb")
1790 1792
1791 1793 cg = other.changegroup(incoming, "incoming")
1792 1794 write_bundle(cg, fname, compress=other.local(), fh=f)
1793 f.close()
1794 1795 if not other.local():
1795 1796 # use a bundlerepo
1796 1797 other = bundlerepo.bundlerepository(ui, repo.root, fname)
1797 1798
1798 1799 o = other.changelog.nodesbetween(incoming)[0]
1799 1800 if opts['newest_first']:
1800 1801 o.reverse()
1801 1802 displayer = show_changeset(ui, other, opts)
1802 1803 for n in o:
1803 1804 parents = [p for p in other.changelog.parents(n) if p != nullid]
1804 1805 if opts['no_merges'] and len(parents) == 2:
1805 1806 continue
1806 1807 displayer.show(changenode=n)
1807 1808 if opts['patch']:
1808 1809 prev = (parents and parents[0]) or nullid
1809 1810 dodiff(ui, ui, other, prev, n)
1810 1811 ui.write("\n")
1811 1812
1812 1813 if cleanup:
1813 1814 os.unlink(cleanup)
1814 1815
1815 1816 def init(ui, dest="."):
1816 1817 """create a new repository in the given directory
1817 1818
1818 1819 Initialize a new repository in the given directory. If the given
1819 1820 directory does not exist, it is created.
1820 1821
1821 1822 If no directory is given, the current directory is used.
1822 1823 """
1823 1824 if not os.path.exists(dest):
1824 1825 os.mkdir(dest)
1825 1826 hg.repository(ui, dest, create=1)
1826 1827
1827 1828 def locate(ui, repo, *pats, **opts):
1828 1829 """locate files matching specific patterns
1829 1830
1830 1831 Print all files under Mercurial control whose names match the
1831 1832 given patterns.
1832 1833
1833 1834 This command searches the current directory and its
1834 1835 subdirectories. To search an entire repository, move to the root
1835 1836 of the repository.
1836 1837
1837 1838 If no patterns are given to match, this command prints all file
1838 1839 names.
1839 1840
1840 1841 If you want to feed the output of this command into the "xargs"
1841 1842 command, use the "-0" option to both this command and "xargs".
1842 1843 This will avoid the problem of "xargs" treating single filenames
1843 1844 that contain white space as multiple filenames.
1844 1845 """
1845 1846 end = opts['print0'] and '\0' or '\n'
1846 1847 rev = opts['rev']
1847 1848 if rev:
1848 1849 node = repo.lookup(rev)
1849 1850 else:
1850 1851 node = None
1851 1852
1852 1853 for src, abs, rel, exact in walk(repo, pats, opts, node=node,
1853 1854 head='(?:.*/|)'):
1854 1855 if not node and repo.dirstate.state(abs) == '?':
1855 1856 continue
1856 1857 if opts['fullpath']:
1857 1858 ui.write(os.path.join(repo.root, abs), end)
1858 1859 else:
1859 1860 ui.write(((pats and rel) or abs), end)
1860 1861
1861 1862 def log(ui, repo, *pats, **opts):
1862 1863 """show revision history of entire repository or files
1863 1864
1864 1865 Print the revision history of the specified files or the entire project.
1865 1866
1866 1867 By default this command outputs: changeset id and hash, tags,
1867 1868 non-trivial parents, user, date and time, and a summary for each
1868 1869 commit. When the -v/--verbose switch is used, the list of changed
1869 1870 files and full commit message is shown.
1870 1871 """
1871 1872 class dui(object):
1872 1873 # Implement and delegate some ui protocol. Save hunks of
1873 1874 # output for later display in the desired order.
1874 1875 def __init__(self, ui):
1875 1876 self.ui = ui
1876 1877 self.hunk = {}
1877 1878 def bump(self, rev):
1878 1879 self.rev = rev
1879 1880 self.hunk[rev] = []
1880 1881 def note(self, *args):
1881 1882 if self.verbose:
1882 1883 self.write(*args)
1883 1884 def status(self, *args):
1884 1885 if not self.quiet:
1885 1886 self.write(*args)
1886 1887 def write(self, *args):
1887 1888 self.hunk[self.rev].append(args)
1888 1889 def debug(self, *args):
1889 1890 if self.debugflag:
1890 1891 self.write(*args)
1891 1892 def __getattr__(self, key):
1892 1893 return getattr(self.ui, key)
1893 1894
1894 1895 changeiter, getchange, matchfn = walkchangerevs(ui, repo, pats, opts)
1895 1896
1896 1897 if opts['limit']:
1897 1898 try:
1898 1899 limit = int(opts['limit'])
1899 1900 except ValueError:
1900 1901 raise util.Abort(_('limit must be a positive integer'))
1901 1902 if limit <= 0: raise util.Abort(_('limit must be positive'))
1902 1903 else:
1903 1904 limit = sys.maxint
1904 1905 count = 0
1905 1906
1906 1907 displayer = show_changeset(ui, repo, opts)
1907 1908 for st, rev, fns in changeiter:
1908 1909 if st == 'window':
1909 1910 du = dui(ui)
1910 1911 displayer.ui = du
1911 1912 elif st == 'add':
1912 1913 du.bump(rev)
1913 1914 changenode = repo.changelog.node(rev)
1914 1915 parents = [p for p in repo.changelog.parents(changenode)
1915 1916 if p != nullid]
1916 1917 if opts['no_merges'] and len(parents) == 2:
1917 1918 continue
1918 1919 if opts['only_merges'] and len(parents) != 2:
1919 1920 continue
1920 1921
1921 1922 if opts['keyword']:
1922 1923 changes = getchange(rev)
1923 1924 miss = 0
1924 1925 for k in [kw.lower() for kw in opts['keyword']]:
1925 1926 if not (k in changes[1].lower() or
1926 1927 k in changes[4].lower() or
1927 1928 k in " ".join(changes[3][:20]).lower()):
1928 1929 miss = 1
1929 1930 break
1930 1931 if miss:
1931 1932 continue
1932 1933
1933 1934 br = None
1934 1935 if opts['branches']:
1935 1936 br = repo.branchlookup([repo.changelog.node(rev)])
1936 1937
1937 1938 displayer.show(rev, brinfo=br)
1938 1939 if opts['patch']:
1939 1940 prev = (parents and parents[0]) or nullid
1940 1941 dodiff(du, du, repo, prev, changenode, match=matchfn)
1941 1942 du.write("\n\n")
1942 1943 elif st == 'iter':
1943 1944 if count == limit: break
1944 1945 if du.hunk[rev]:
1945 1946 count += 1
1946 1947 for args in du.hunk[rev]:
1947 1948 ui.write(*args)
1948 1949
1949 1950 def manifest(ui, repo, rev=None):
1950 1951 """output the latest or given revision of the project manifest
1951 1952
1952 1953 Print a list of version controlled files for the given revision.
1953 1954
1954 1955 The manifest is the list of files being version controlled. If no revision
1955 1956 is given then the tip is used.
1956 1957 """
1957 1958 if rev:
1958 1959 try:
1959 1960 # assume all revision numbers are for changesets
1960 1961 n = repo.lookup(rev)
1961 1962 change = repo.changelog.read(n)
1962 1963 n = change[0]
1963 1964 except hg.RepoError:
1964 1965 n = repo.manifest.lookup(rev)
1965 1966 else:
1966 1967 n = repo.manifest.tip()
1967 1968 m = repo.manifest.read(n)
1968 1969 mf = repo.manifest.readflags(n)
1969 1970 files = m.keys()
1970 1971 files.sort()
1971 1972
1972 1973 for f in files:
1973 1974 ui.write("%40s %3s %s\n" % (hex(m[f]), mf[f] and "755" or "644", f))
1974 1975
1975 1976 def outgoing(ui, repo, dest="default-push", **opts):
1976 1977 """show changesets not found in destination
1977 1978
1978 1979 Show changesets not found in the specified destination repo or the
1979 1980 default push repo. These are the changesets that would be pushed
1980 1981 if a push was requested.
1981 1982
1982 1983 See pull for valid source format details.
1983 1984 """
1984 1985 dest = ui.expandpath(dest)
1985 1986 if opts['ssh']:
1986 1987 ui.setconfig("ui", "ssh", opts['ssh'])
1987 1988 if opts['remotecmd']:
1988 1989 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
1989 1990
1990 1991 other = hg.repository(ui, dest)
1991 1992 o = repo.findoutgoing(other, force=opts['force'])
1992 1993 o = repo.changelog.nodesbetween(o)[0]
1993 1994 if opts['newest_first']:
1994 1995 o.reverse()
1995 1996 displayer = show_changeset(ui, repo, opts)
1996 1997 for n in o:
1997 1998 parents = [p for p in repo.changelog.parents(n) if p != nullid]
1998 1999 if opts['no_merges'] and len(parents) == 2:
1999 2000 continue
2000 2001 displayer.show(changenode=n)
2001 2002 if opts['patch']:
2002 2003 prev = (parents and parents[0]) or nullid
2003 2004 dodiff(ui, ui, repo, prev, n)
2004 2005 ui.write("\n")
2005 2006
2006 2007 def parents(ui, repo, rev=None, branches=None, **opts):
2007 2008 """show the parents of the working dir or revision
2008 2009
2009 2010 Print the working directory's parent revisions.
2010 2011 """
2011 2012 if rev:
2012 2013 p = repo.changelog.parents(repo.lookup(rev))
2013 2014 else:
2014 2015 p = repo.dirstate.parents()
2015 2016
2016 2017 br = None
2017 2018 if branches is not None:
2018 2019 br = repo.branchlookup(p)
2019 2020 displayer = show_changeset(ui, repo, opts)
2020 2021 for n in p:
2021 2022 if n != nullid:
2022 2023 displayer.show(changenode=n, brinfo=br)
2023 2024
2024 2025 def paths(ui, repo, search=None):
2025 2026 """show definition of symbolic path names
2026 2027
2027 2028 Show definition of symbolic path name NAME. If no name is given, show
2028 2029 definition of available names.
2029 2030
2030 2031 Path names are defined in the [paths] section of /etc/mercurial/hgrc
2031 2032 and $HOME/.hgrc. If run inside a repository, .hg/hgrc is used, too.
2032 2033 """
2033 2034 if search:
2034 2035 for name, path in ui.configitems("paths"):
2035 2036 if name == search:
2036 2037 ui.write("%s\n" % path)
2037 2038 return
2038 2039 ui.warn(_("not found!\n"))
2039 2040 return 1
2040 2041 else:
2041 2042 for name, path in ui.configitems("paths"):
2042 2043 ui.write("%s = %s\n" % (name, path))
2043 2044
2044 2045 def pull(ui, repo, source="default", **opts):
2045 2046 """pull changes from the specified source
2046 2047
2047 2048 Pull changes from a remote repository to a local one.
2048 2049
2049 2050 This finds all changes from the repository at the specified path
2050 2051 or URL and adds them to the local repository. By default, this
2051 2052 does not update the copy of the project in the working directory.
2052 2053
2053 2054 Valid URLs are of the form:
2054 2055
2055 2056 local/filesystem/path
2056 2057 http://[user@]host[:port][/path]
2057 2058 https://[user@]host[:port][/path]
2058 2059 ssh://[user@]host[:port][/path]
2059 2060
2060 2061 SSH requires an accessible shell account on the destination machine
2061 2062 and a copy of hg in the remote path. With SSH, paths are relative
2062 2063 to the remote user's home directory by default; use two slashes at
2063 2064 the start of a path to specify it as relative to the filesystem root.
2064 2065 """
2065 2066 source = ui.expandpath(source)
2066 2067 ui.status(_('pulling from %s\n') % (source))
2067 2068
2068 2069 if opts['ssh']:
2069 2070 ui.setconfig("ui", "ssh", opts['ssh'])
2070 2071 if opts['remotecmd']:
2071 2072 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
2072 2073
2073 2074 other = hg.repository(ui, source)
2074 2075 revs = None
2075 2076 if opts['rev'] and not other.local():
2076 2077 raise util.Abort(_("pull -r doesn't work for remote repositories yet"))
2077 2078 elif opts['rev']:
2078 2079 revs = [other.lookup(rev) for rev in opts['rev']]
2079 2080 r = repo.pull(other, heads=revs, force=opts['force'])
2080 2081 if not r:
2081 2082 if opts['update']:
2082 2083 return update(ui, repo)
2083 2084 else:
2084 2085 ui.status(_("(run 'hg update' to get a working copy)\n"))
2085 2086
2086 2087 return r
2087 2088
2088 2089 def push(ui, repo, dest="default-push", **opts):
2089 2090 """push changes to the specified destination
2090 2091
2091 2092 Push changes from the local repository to the given destination.
2092 2093
2093 2094 This is the symmetrical operation for pull. It helps to move
2094 2095 changes from the current repository to a different one. If the
2095 2096 destination is local this is identical to a pull in that directory
2096 2097 from the current one.
2097 2098
2098 2099 By default, push will refuse to run if it detects the result would
2099 2100 increase the number of remote heads. This generally indicates the
2100 2101 the client has forgotten to sync and merge before pushing.
2101 2102
2102 2103 Valid URLs are of the form:
2103 2104
2104 2105 local/filesystem/path
2105 2106 ssh://[user@]host[:port][/path]
2106 2107
2107 2108 SSH requires an accessible shell account on the destination
2108 2109 machine and a copy of hg in the remote path.
2109 2110 """
2110 2111 dest = ui.expandpath(dest)
2111 2112 ui.status('pushing to %s\n' % (dest))
2112 2113
2113 2114 if opts['ssh']:
2114 2115 ui.setconfig("ui", "ssh", opts['ssh'])
2115 2116 if opts['remotecmd']:
2116 2117 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
2117 2118
2118 2119 other = hg.repository(ui, dest)
2119 2120 revs = None
2120 2121 if opts['rev']:
2121 2122 revs = [repo.lookup(rev) for rev in opts['rev']]
2122 2123 r = repo.push(other, opts['force'], revs=revs)
2123 2124 return r
2124 2125
2125 2126 def rawcommit(ui, repo, *flist, **rc):
2126 2127 """raw commit interface (DEPRECATED)
2127 2128
2128 2129 (DEPRECATED)
2129 2130 Lowlevel commit, for use in helper scripts.
2130 2131
2131 2132 This command is not intended to be used by normal users, as it is
2132 2133 primarily useful for importing from other SCMs.
2133 2134
2134 2135 This command is now deprecated and will be removed in a future
2135 2136 release, please use debugsetparents and commit instead.
2136 2137 """
2137 2138
2138 2139 ui.warn(_("(the rawcommit command is deprecated)\n"))
2139 2140
2140 2141 message = rc['message']
2141 2142 if not message and rc['logfile']:
2142 2143 try:
2143 2144 message = open(rc['logfile']).read()
2144 2145 except IOError:
2145 2146 pass
2146 2147 if not message and not rc['logfile']:
2147 2148 raise util.Abort(_("missing commit message"))
2148 2149
2149 2150 files = relpath(repo, list(flist))
2150 2151 if rc['files']:
2151 2152 files += open(rc['files']).read().splitlines()
2152 2153
2153 2154 rc['parent'] = map(repo.lookup, rc['parent'])
2154 2155
2155 2156 try:
2156 2157 repo.rawcommit(files, message, rc['user'], rc['date'], *rc['parent'])
2157 2158 except ValueError, inst:
2158 2159 raise util.Abort(str(inst))
2159 2160
2160 2161 def recover(ui, repo):
2161 2162 """roll back an interrupted transaction
2162 2163
2163 2164 Recover from an interrupted commit or pull.
2164 2165
2165 2166 This command tries to fix the repository status after an interrupted
2166 2167 operation. It should only be necessary when Mercurial suggests it.
2167 2168 """
2168 2169 if repo.recover():
2169 2170 return repo.verify()
2170 2171 return False
2171 2172
2172 2173 def remove(ui, repo, pat, *pats, **opts):
2173 2174 """remove the specified files on the next commit
2174 2175
2175 2176 Schedule the indicated files for removal from the repository.
2176 2177
2177 2178 This command schedules the files to be removed at the next commit.
2178 2179 This only removes files from the current branch, not from the
2179 2180 entire project history. If the files still exist in the working
2180 2181 directory, they will be deleted from it.
2181 2182 """
2182 2183 names = []
2183 2184 def okaytoremove(abs, rel, exact):
2184 2185 modified, added, removed, deleted, unknown = repo.changes(files=[abs])
2185 2186 reason = None
2186 2187 if modified and not opts['force']:
2187 2188 reason = _('is modified')
2188 2189 elif added:
2189 2190 reason = _('has been marked for add')
2190 2191 elif unknown:
2191 2192 reason = _('is not managed')
2192 2193 if reason:
2193 2194 if exact:
2194 2195 ui.warn(_('not removing %s: file %s\n') % (rel, reason))
2195 2196 else:
2196 2197 return True
2197 2198 for src, abs, rel, exact in walk(repo, (pat,) + pats, opts):
2198 2199 if okaytoremove(abs, rel, exact):
2199 2200 if ui.verbose or not exact:
2200 2201 ui.status(_('removing %s\n') % rel)
2201 2202 names.append(abs)
2202 2203 repo.remove(names, unlink=True)
2203 2204
2204 2205 def rename(ui, repo, *pats, **opts):
2205 2206 """rename files; equivalent of copy + remove
2206 2207
2207 2208 Mark dest as copies of sources; mark sources for deletion. If
2208 2209 dest is a directory, copies are put in that directory. If dest is
2209 2210 a file, there can only be one source.
2210 2211
2211 2212 By default, this command copies the contents of files as they
2212 2213 stand in the working directory. If invoked with --after, the
2213 2214 operation is recorded, but no copying is performed.
2214 2215
2215 2216 This command takes effect in the next commit.
2216 2217
2217 2218 NOTE: This command should be treated as experimental. While it
2218 2219 should properly record rename files, this information is not yet
2219 2220 fully used by merge, nor fully reported by log.
2220 2221 """
2221 2222 try:
2222 2223 wlock = repo.wlock(0)
2223 2224 errs, copied = docopy(ui, repo, pats, opts, wlock)
2224 2225 names = []
2225 2226 for abs, rel, exact in copied:
2226 2227 if ui.verbose or not exact:
2227 2228 ui.status(_('removing %s\n') % rel)
2228 2229 names.append(abs)
2229 2230 repo.remove(names, True, wlock)
2230 2231 except lock.LockHeld, inst:
2231 2232 ui.warn(_("repository lock held by %s\n") % inst.args[0])
2232 2233 errs = 1
2233 2234 return errs
2234 2235
2235 2236 def revert(ui, repo, *pats, **opts):
2236 2237 """revert modified files or dirs back to their unmodified states
2237 2238
2238 2239 In its default mode, it reverts any uncommitted modifications made
2239 2240 to the named files or directories. This restores the contents of
2240 2241 the affected files to an unmodified state.
2241 2242
2242 2243 Using the -r option, it reverts the given files or directories to
2243 2244 their state as of an earlier revision. This can be helpful to "roll
2244 2245 back" some or all of a change that should not have been committed.
2245 2246
2246 2247 Revert modifies the working directory. It does not commit any
2247 2248 changes, or change the parent of the current working directory.
2248 2249
2249 2250 If a file has been deleted, it is recreated. If the executable
2250 2251 mode of a file was changed, it is reset.
2251 2252
2252 2253 If names are given, all files matching the names are reverted.
2253 2254
2254 2255 If no arguments are given, all files in the repository are reverted.
2255 2256 """
2256 2257 node = opts['rev'] and repo.lookup(opts['rev']) or \
2257 2258 repo.dirstate.parents()[0]
2258 2259
2259 2260 files, choose, anypats = matchpats(repo, pats, opts)
2260 2261 modified, added, removed, deleted, unknown = repo.changes(match=choose)
2261 2262 repo.forget(added)
2262 2263 repo.undelete(removed)
2263 2264
2264 2265 return repo.update(node, False, True, choose, False)
2265 2266
2266 2267 def root(ui, repo):
2267 2268 """print the root (top) of the current working dir
2268 2269
2269 2270 Print the root directory of the current repository.
2270 2271 """
2271 2272 ui.write(repo.root + "\n")
2272 2273
2273 2274 def serve(ui, repo, **opts):
2274 2275 """export the repository via HTTP
2275 2276
2276 2277 Start a local HTTP repository browser and pull server.
2277 2278
2278 2279 By default, the server logs accesses to stdout and errors to
2279 2280 stderr. Use the "-A" and "-E" options to log to files.
2280 2281 """
2281 2282
2282 2283 if opts["stdio"]:
2283 2284 fin, fout = sys.stdin, sys.stdout
2284 2285 sys.stdout = sys.stderr
2285 2286
2286 2287 # Prevent insertion/deletion of CRs
2287 2288 util.set_binary(fin)
2288 2289 util.set_binary(fout)
2289 2290
2290 2291 def getarg():
2291 2292 argline = fin.readline()[:-1]
2292 2293 arg, l = argline.split()
2293 2294 val = fin.read(int(l))
2294 2295 return arg, val
2295 2296 def respond(v):
2296 2297 fout.write("%d\n" % len(v))
2297 2298 fout.write(v)
2298 2299 fout.flush()
2299 2300
2300 2301 lock = None
2301 2302
2302 2303 while 1:
2303 2304 cmd = fin.readline()[:-1]
2304 2305 if cmd == '':
2305 2306 return
2306 2307 if cmd == "heads":
2307 2308 h = repo.heads()
2308 2309 respond(" ".join(map(hex, h)) + "\n")
2309 2310 if cmd == "lock":
2310 2311 lock = repo.lock()
2311 2312 respond("")
2312 2313 if cmd == "unlock":
2313 2314 if lock:
2314 2315 lock.release()
2315 2316 lock = None
2316 2317 respond("")
2317 2318 elif cmd == "branches":
2318 2319 arg, nodes = getarg()
2319 2320 nodes = map(bin, nodes.split(" "))
2320 2321 r = []
2321 2322 for b in repo.branches(nodes):
2322 2323 r.append(" ".join(map(hex, b)) + "\n")
2323 2324 respond("".join(r))
2324 2325 elif cmd == "between":
2325 2326 arg, pairs = getarg()
2326 2327 pairs = [map(bin, p.split("-")) for p in pairs.split(" ")]
2327 2328 r = []
2328 2329 for b in repo.between(pairs):
2329 2330 r.append(" ".join(map(hex, b)) + "\n")
2330 2331 respond("".join(r))
2331 2332 elif cmd == "changegroup":
2332 2333 nodes = []
2333 2334 arg, roots = getarg()
2334 2335 nodes = map(bin, roots.split(" "))
2335 2336
2336 2337 cg = repo.changegroup(nodes, 'serve')
2337 2338 while 1:
2338 2339 d = cg.read(4096)
2339 2340 if not d:
2340 2341 break
2341 2342 fout.write(d)
2342 2343
2343 2344 fout.flush()
2344 2345
2345 2346 elif cmd == "addchangegroup":
2346 2347 if not lock:
2347 2348 respond("not locked")
2348 2349 continue
2349 2350 respond("")
2350 2351
2351 2352 r = repo.addchangegroup(fin)
2352 2353 respond("")
2353 2354
2354 2355 optlist = "name templates style address port ipv6 accesslog errorlog"
2355 2356 for o in optlist.split():
2356 2357 if opts[o]:
2357 2358 ui.setconfig("web", o, opts[o])
2358 2359
2359 2360 if opts['daemon'] and not opts['daemon_pipefds']:
2360 2361 rfd, wfd = os.pipe()
2361 2362 args = sys.argv[:]
2362 2363 args.append('--daemon-pipefds=%d,%d' % (rfd, wfd))
2363 2364 pid = os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0),
2364 2365 args[0], args)
2365 2366 os.close(wfd)
2366 2367 os.read(rfd, 1)
2367 2368 os._exit(0)
2368 2369
2369 2370 try:
2370 2371 httpd = hgweb.create_server(repo)
2371 2372 except socket.error, inst:
2372 2373 raise util.Abort(_('cannot start server: ') + inst.args[1])
2373 2374
2374 2375 if ui.verbose:
2375 2376 addr, port = httpd.socket.getsockname()
2376 2377 if addr == '0.0.0.0':
2377 2378 addr = socket.gethostname()
2378 2379 else:
2379 2380 try:
2380 2381 addr = socket.gethostbyaddr(addr)[0]
2381 2382 except socket.error:
2382 2383 pass
2383 2384 if port != 80:
2384 2385 ui.status(_('listening at http://%s:%d/\n') % (addr, port))
2385 2386 else:
2386 2387 ui.status(_('listening at http://%s/\n') % addr)
2387 2388
2388 2389 if opts['pid_file']:
2389 2390 fp = open(opts['pid_file'], 'w')
2390 2391 fp.write(str(os.getpid()))
2391 2392 fp.close()
2392 2393
2393 2394 if opts['daemon_pipefds']:
2394 2395 rfd, wfd = [int(x) for x in opts['daemon_pipefds'].split(',')]
2395 2396 os.close(rfd)
2396 2397 os.write(wfd, 'y')
2397 2398 os.close(wfd)
2398 2399 sys.stdout.flush()
2399 2400 sys.stderr.flush()
2400 2401 fd = os.open(util.nulldev, os.O_RDWR)
2401 2402 if fd != 0: os.dup2(fd, 0)
2402 2403 if fd != 1: os.dup2(fd, 1)
2403 2404 if fd != 2: os.dup2(fd, 2)
2404 2405 if fd not in (0, 1, 2): os.close(fd)
2405 2406
2406 2407 httpd.serve_forever()
2407 2408
2408 2409 def status(ui, repo, *pats, **opts):
2409 2410 """show changed files in the working directory
2410 2411
2411 2412 Show changed files in the repository. If names are
2412 2413 given, only files that match are shown.
2413 2414
2414 2415 The codes used to show the status of files are:
2415 2416 M = modified
2416 2417 A = added
2417 2418 R = removed
2418 2419 ! = deleted, but still tracked
2419 2420 ? = not tracked
2420 2421 """
2421 2422
2422 2423 files, matchfn, anypats = matchpats(repo, pats, opts)
2423 2424 cwd = (pats and repo.getcwd()) or ''
2424 2425 modified, added, removed, deleted, unknown = [
2425 2426 [util.pathto(cwd, x) for x in n]
2426 2427 for n in repo.changes(files=files, match=matchfn)]
2427 2428
2428 2429 changetypes = [('modified', 'M', modified),
2429 2430 ('added', 'A', added),
2430 2431 ('removed', 'R', removed),
2431 2432 ('deleted', '!', deleted),
2432 2433 ('unknown', '?', unknown)]
2433 2434
2434 2435 end = opts['print0'] and '\0' or '\n'
2435 2436
2436 2437 for opt, char, changes in ([ct for ct in changetypes if opts[ct[0]]]
2437 2438 or changetypes):
2438 2439 if opts['no_status']:
2439 2440 format = "%%s%s" % end
2440 2441 else:
2441 2442 format = "%s %%s%s" % (char, end)
2442 2443
2443 2444 for f in changes:
2444 2445 ui.write(format % f)
2445 2446
2446 2447 def tag(ui, repo, name, rev_=None, **opts):
2447 2448 """add a tag for the current tip or a given revision
2448 2449
2449 2450 Name a particular revision using <name>.
2450 2451
2451 2452 Tags are used to name particular revisions of the repository and are
2452 2453 very useful to compare different revision, to go back to significant
2453 2454 earlier versions or to mark branch points as releases, etc.
2454 2455
2455 2456 If no revision is given, the tip is used.
2456 2457
2457 2458 To facilitate version control, distribution, and merging of tags,
2458 2459 they are stored as a file named ".hgtags" which is managed
2459 2460 similarly to other project files and can be hand-edited if
2460 2461 necessary. The file '.hg/localtags' is used for local tags (not
2461 2462 shared among repositories).
2462 2463 """
2463 2464 if name == "tip":
2464 2465 raise util.Abort(_("the name 'tip' is reserved"))
2465 2466 if rev_ is not None:
2466 2467 ui.warn(_("use of 'hg tag NAME [REV]' is deprecated, "
2467 2468 "please use 'hg tag [-r REV] NAME' instead\n"))
2468 2469 if opts['rev']:
2469 2470 raise util.Abort(_("use only one form to specify the revision"))
2470 2471 if opts['rev']:
2471 2472 rev_ = opts['rev']
2472 2473 if rev_:
2473 2474 r = hex(repo.lookup(rev_))
2474 2475 else:
2475 2476 r = hex(repo.changelog.tip())
2476 2477
2477 2478 disallowed = (revrangesep, '\r', '\n')
2478 2479 for c in disallowed:
2479 2480 if name.find(c) >= 0:
2480 2481 raise util.Abort(_("%s cannot be used in a tag name") % repr(c))
2481 2482
2482 2483 repo.hook('pretag', throw=True, node=r, tag=name,
2483 2484 local=int(not not opts['local']))
2484 2485
2485 2486 if opts['local']:
2486 2487 repo.opener("localtags", "a").write("%s %s\n" % (r, name))
2487 2488 repo.hook('tag', node=r, tag=name, local=1)
2488 2489 return
2489 2490
2490 2491 for x in repo.changes():
2491 2492 if ".hgtags" in x:
2492 2493 raise util.Abort(_("working copy of .hgtags is changed "
2493 2494 "(please commit .hgtags manually)"))
2494 2495
2495 2496 repo.wfile(".hgtags", "ab").write("%s %s\n" % (r, name))
2496 2497 if repo.dirstate.state(".hgtags") == '?':
2497 2498 repo.add([".hgtags"])
2498 2499
2499 2500 message = (opts['message'] or
2500 2501 _("Added tag %s for changeset %s") % (name, r))
2501 2502 try:
2502 2503 repo.commit([".hgtags"], message, opts['user'], opts['date'])
2503 2504 repo.hook('tag', node=r, tag=name, local=0)
2504 2505 except ValueError, inst:
2505 2506 raise util.Abort(str(inst))
2506 2507
2507 2508 def tags(ui, repo):
2508 2509 """list repository tags
2509 2510
2510 2511 List the repository tags.
2511 2512
2512 2513 This lists both regular and local tags.
2513 2514 """
2514 2515
2515 2516 l = repo.tagslist()
2516 2517 l.reverse()
2517 2518 for t, n in l:
2518 2519 try:
2519 2520 r = "%5d:%s" % (repo.changelog.rev(n), hex(n))
2520 2521 except KeyError:
2521 2522 r = " ?:?"
2522 2523 ui.write("%-30s %s\n" % (t, r))
2523 2524
2524 2525 def tip(ui, repo, **opts):
2525 2526 """show the tip revision
2526 2527
2527 2528 Show the tip revision.
2528 2529 """
2529 2530 n = repo.changelog.tip()
2530 2531 br = None
2531 2532 if opts['branches']:
2532 2533 br = repo.branchlookup([n])
2533 2534 show_changeset(ui, repo, opts).show(changenode=n, brinfo=br)
2534 2535 if opts['patch']:
2535 2536 dodiff(ui, ui, repo, repo.changelog.parents(n)[0], n)
2536 2537
2537 2538 def unbundle(ui, repo, fname, **opts):
2538 2539 """apply a changegroup file
2539 2540
2540 2541 Apply a compressed changegroup file generated by the bundle
2541 2542 command.
2542 2543 """
2543 2544 f = urllib.urlopen(fname)
2544 2545
2545 2546 header = f.read(4)
2546 2547 if header == "HG10":
2547 2548 def generator(f):
2548 2549 zd = bz2.BZ2Decompressor()
2549 2550 for chunk in f:
2550 2551 yield zd.decompress(chunk)
2551 2552 elif header == "HG11":
2552 2553 def generator(f):
2553 2554 for chunk in f:
2554 2555 yield chunk
2555 2556 else:
2556 2557 raise util.Abort(_("%s: not a Mercurial bundle file") % fname)
2557 2558 gen = generator(util.filechunkiter(f, 4096))
2558 2559 if repo.addchangegroup(util.chunkbuffer(gen)):
2559 2560 return 1
2560 2561
2561 2562 if opts['update']:
2562 2563 return update(ui, repo)
2563 2564 else:
2564 2565 ui.status(_("(run 'hg update' to get a working copy)\n"))
2565 2566
2566 2567 def undo(ui, repo):
2567 2568 """undo the last commit or pull
2568 2569
2569 2570 Roll back the last pull or commit transaction on the
2570 2571 repository, restoring the project to its earlier state.
2571 2572
2572 2573 This command should be used with care. There is only one level of
2573 2574 undo and there is no redo.
2574 2575
2575 2576 This command is not intended for use on public repositories. Once
2576 2577 a change is visible for pull by other users, undoing it locally is
2577 2578 ineffective.
2578 2579 """
2579 2580 repo.undo()
2580 2581
2581 2582 def update(ui, repo, node=None, merge=False, clean=False, force=None,
2582 2583 branch=None, **opts):
2583 2584 """update or merge working directory
2584 2585
2585 2586 Update the working directory to the specified revision.
2586 2587
2587 2588 If there are no outstanding changes in the working directory and
2588 2589 there is a linear relationship between the current version and the
2589 2590 requested version, the result is the requested version.
2590 2591
2591 2592 Otherwise the result is a merge between the contents of the
2592 2593 current working directory and the requested version. Files that
2593 2594 changed between either parent are marked as changed for the next
2594 2595 commit and a commit must be performed before any further updates
2595 2596 are allowed.
2596 2597
2597 2598 By default, update will refuse to run if doing so would require
2598 2599 merging or discarding local changes.
2599 2600 """
2600 2601 if branch:
2601 2602 br = repo.branchlookup(branch=branch)
2602 2603 found = []
2603 2604 for x in br:
2604 2605 if branch in br[x]:
2605 2606 found.append(x)
2606 2607 if len(found) > 1:
2607 2608 ui.warn(_("Found multiple heads for %s\n") % branch)
2608 2609 for x in found:
2609 2610 show_changeset(ui, repo, opts).show(changenode=x, brinfo=br)
2610 2611 return 1
2611 2612 if len(found) == 1:
2612 2613 node = found[0]
2613 2614 ui.warn(_("Using head %s for branch %s\n") % (short(node), branch))
2614 2615 else:
2615 2616 ui.warn(_("branch %s not found\n") % (branch))
2616 2617 return 1
2617 2618 else:
2618 2619 node = node and repo.lookup(node) or repo.changelog.tip()
2619 2620 return repo.update(node, allow=merge, force=clean, forcemerge=force)
2620 2621
2621 2622 def verify(ui, repo):
2622 2623 """verify the integrity of the repository
2623 2624
2624 2625 Verify the integrity of the current repository.
2625 2626
2626 2627 This will perform an extensive check of the repository's
2627 2628 integrity, validating the hashes and checksums of each entry in
2628 2629 the changelog, manifest, and tracked files, as well as the
2629 2630 integrity of their crosslinks and indices.
2630 2631 """
2631 2632 return repo.verify()
2632 2633
2633 2634 # Command options and aliases are listed here, alphabetically
2634 2635
2635 2636 table = {
2636 2637 "^add":
2637 2638 (add,
2638 2639 [('I', 'include', [], _('include names matching the given patterns')),
2639 2640 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2640 2641 _('hg add [OPTION]... [FILE]...')),
2641 2642 "addremove":
2642 2643 (addremove,
2643 2644 [('I', 'include', [], _('include names matching the given patterns')),
2644 2645 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2645 2646 _('hg addremove [OPTION]... [FILE]...')),
2646 2647 "^annotate":
2647 2648 (annotate,
2648 2649 [('r', 'rev', '', _('annotate the specified revision')),
2649 2650 ('a', 'text', None, _('treat all files as text')),
2650 2651 ('u', 'user', None, _('list the author')),
2651 2652 ('d', 'date', None, _('list the date')),
2652 2653 ('n', 'number', None, _('list the revision number (default)')),
2653 2654 ('c', 'changeset', None, _('list the changeset')),
2654 2655 ('I', 'include', [], _('include names matching the given patterns')),
2655 2656 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2656 2657 _('hg annotate [-r REV] [-a] [-u] [-d] [-n] [-c] FILE...')),
2657 2658 "bundle":
2658 2659 (bundle,
2659 2660 [('f', 'force', None,
2660 2661 _('run even when remote repository is unrelated'))],
2661 2662 _('hg bundle FILE DEST')),
2662 2663 "cat":
2663 2664 (cat,
2664 2665 [('o', 'output', '', _('print output to file with formatted name')),
2665 2666 ('r', 'rev', '', _('print the given revision')),
2666 2667 ('I', 'include', [], _('include names matching the given patterns')),
2667 2668 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2668 2669 _('hg cat [OPTION]... FILE...')),
2669 2670 "^clone":
2670 2671 (clone,
2671 2672 [('U', 'noupdate', None, _('do not update the new working directory')),
2672 2673 ('r', 'rev', [],
2673 2674 _('a changeset you would like to have after cloning')),
2674 2675 ('', 'pull', None, _('use pull protocol to copy metadata')),
2675 2676 ('e', 'ssh', '', _('specify ssh command to use')),
2676 2677 ('', 'remotecmd', '',
2677 2678 _('specify hg command to run on the remote side'))],
2678 2679 _('hg clone [OPTION]... SOURCE [DEST]')),
2679 2680 "^commit|ci":
2680 2681 (commit,
2681 2682 [('A', 'addremove', None, _('run addremove during commit')),
2682 2683 ('m', 'message', '', _('use <text> as commit message')),
2683 2684 ('l', 'logfile', '', _('read the commit message from <file>')),
2684 2685 ('d', 'date', '', _('record datecode as commit date')),
2685 2686 ('u', 'user', '', _('record user as commiter')),
2686 2687 ('I', 'include', [], _('include names matching the given patterns')),
2687 2688 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2688 2689 _('hg commit [OPTION]... [FILE]...')),
2689 2690 "copy|cp":
2690 2691 (copy,
2691 2692 [('A', 'after', None, _('record a copy that has already occurred')),
2692 2693 ('f', 'force', None,
2693 2694 _('forcibly copy over an existing managed file')),
2694 2695 ('I', 'include', [], _('include names matching the given patterns')),
2695 2696 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2696 2697 _('hg copy [OPTION]... [SOURCE]... DEST')),
2697 2698 "debugancestor": (debugancestor, [], _('debugancestor INDEX REV1 REV2')),
2698 2699 "debugcomplete": (debugcomplete, [], _('debugcomplete CMD')),
2699 2700 "debugrebuildstate":
2700 2701 (debugrebuildstate,
2701 2702 [('r', 'rev', '', _('revision to rebuild to'))],
2702 2703 _('debugrebuildstate [-r REV] [REV]')),
2703 2704 "debugcheckstate": (debugcheckstate, [], _('debugcheckstate')),
2704 2705 "debugconfig": (debugconfig, [], _('debugconfig')),
2705 2706 "debugsetparents": (debugsetparents, [], _('debugsetparents REV1 [REV2]')),
2706 2707 "debugstate": (debugstate, [], _('debugstate')),
2707 2708 "debugdata": (debugdata, [], _('debugdata FILE REV')),
2708 2709 "debugindex": (debugindex, [], _('debugindex FILE')),
2709 2710 "debugindexdot": (debugindexdot, [], _('debugindexdot FILE')),
2710 2711 "debugrename": (debugrename, [], _('debugrename FILE [REV]')),
2711 2712 "debugwalk":
2712 2713 (debugwalk,
2713 2714 [('I', 'include', [], _('include names matching the given patterns')),
2714 2715 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2715 2716 _('debugwalk [OPTION]... [FILE]...')),
2716 2717 "^diff":
2717 2718 (diff,
2718 2719 [('r', 'rev', [], _('revision')),
2719 2720 ('a', 'text', None, _('treat all files as text')),
2720 2721 ('p', 'show-function', None,
2721 2722 _('show which function each change is in')),
2722 2723 ('w', 'ignore-all-space', None,
2723 2724 _('ignore white space when comparing lines')),
2724 2725 ('I', 'include', [], _('include names matching the given patterns')),
2725 2726 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2726 2727 _('hg diff [-a] [-I] [-X] [-r REV1 [-r REV2]] [FILE]...')),
2727 2728 "^export":
2728 2729 (export,
2729 2730 [('o', 'output', '', _('print output to file with formatted name')),
2730 2731 ('a', 'text', None, _('treat all files as text')),
2731 2732 ('', 'switch-parent', None, _('diff against the second parent'))],
2732 2733 _('hg export [-a] [-o OUTFILESPEC] REV...')),
2733 2734 "forget":
2734 2735 (forget,
2735 2736 [('I', 'include', [], _('include names matching the given patterns')),
2736 2737 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2737 2738 _('hg forget [OPTION]... FILE...')),
2738 2739 "grep":
2739 2740 (grep,
2740 2741 [('0', 'print0', None, _('end fields with NUL')),
2741 2742 ('', 'all', None, _('print all revisions that match')),
2742 2743 ('i', 'ignore-case', None, _('ignore case when matching')),
2743 2744 ('l', 'files-with-matches', None,
2744 2745 _('print only filenames and revs that match')),
2745 2746 ('n', 'line-number', None, _('print matching line numbers')),
2746 2747 ('r', 'rev', [], _('search in given revision range')),
2747 2748 ('u', 'user', None, _('print user who committed change')),
2748 2749 ('I', 'include', [], _('include names matching the given patterns')),
2749 2750 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2750 2751 _('hg grep [OPTION]... PATTERN [FILE]...')),
2751 2752 "heads":
2752 2753 (heads,
2753 2754 [('b', 'branches', None, _('show branches')),
2754 2755 ('', 'style', '', _('display using template map file')),
2755 2756 ('r', 'rev', '', _('show only heads which are descendants of rev')),
2756 2757 ('', 'template', '', _('display with template'))],
2757 2758 _('hg heads [-b] [-r <rev>]')),
2758 2759 "help": (help_, [], _('hg help [COMMAND]')),
2759 2760 "identify|id": (identify, [], _('hg identify')),
2760 2761 "import|patch":
2761 2762 (import_,
2762 2763 [('p', 'strip', 1,
2763 2764 _('directory strip option for patch. This has the same\n') +
2764 2765 _('meaning as the corresponding patch option')),
2765 2766 ('b', 'base', '', _('base path')),
2766 2767 ('f', 'force', None,
2767 2768 _('skip check for outstanding uncommitted changes'))],
2768 2769 _('hg import [-p NUM] [-b BASE] [-f] PATCH...')),
2769 2770 "incoming|in": (incoming,
2770 2771 [('M', 'no-merges', None, _('do not show merges')),
2771 2772 ('f', 'force', None,
2772 2773 _('run even when remote repository is unrelated')),
2773 2774 ('', 'style', '', _('display using template map file')),
2774 2775 ('n', 'newest-first', None, _('show newest record first')),
2775 2776 ('', 'bundle', '', _('file to store the bundles into')),
2776 2777 ('p', 'patch', None, _('show patch')),
2777 2778 ('', 'template', '', _('display with template')),
2778 2779 ('e', 'ssh', '', _('specify ssh command to use')),
2779 2780 ('', 'remotecmd', '',
2780 2781 _('specify hg command to run on the remote side'))],
2781 2782 _('hg incoming [-p] [-n] [-M] [--bundle FILENAME] [SOURCE]')),
2782 2783 "^init": (init, [], _('hg init [DEST]')),
2783 2784 "locate":
2784 2785 (locate,
2785 2786 [('r', 'rev', '', _('search the repository as it stood at rev')),
2786 2787 ('0', 'print0', None,
2787 2788 _('end filenames with NUL, for use with xargs')),
2788 2789 ('f', 'fullpath', None,
2789 2790 _('print complete paths from the filesystem root')),
2790 2791 ('I', 'include', [], _('include names matching the given patterns')),
2791 2792 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2792 2793 _('hg locate [OPTION]... [PATTERN]...')),
2793 2794 "^log|history":
2794 2795 (log,
2795 2796 [('b', 'branches', None, _('show branches')),
2796 2797 ('k', 'keyword', [], _('search for a keyword')),
2797 2798 ('l', 'limit', '', _('limit number of changes displayed')),
2798 2799 ('r', 'rev', [], _('show the specified revision or range')),
2799 2800 ('M', 'no-merges', None, _('do not show merges')),
2800 2801 ('', 'style', '', _('display using template map file')),
2801 2802 ('m', 'only-merges', None, _('show only merges')),
2802 2803 ('p', 'patch', None, _('show patch')),
2803 2804 ('', 'template', '', _('display with template')),
2804 2805 ('I', 'include', [], _('include names matching the given patterns')),
2805 2806 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2806 2807 _('hg log [OPTION]... [FILE]')),
2807 2808 "manifest": (manifest, [], _('hg manifest [REV]')),
2808 2809 "outgoing|out": (outgoing,
2809 2810 [('M', 'no-merges', None, _('do not show merges')),
2810 2811 ('f', 'force', None,
2811 2812 _('run even when remote repository is unrelated')),
2812 2813 ('p', 'patch', None, _('show patch')),
2813 2814 ('', 'style', '', _('display using template map file')),
2814 2815 ('n', 'newest-first', None, _('show newest record first')),
2815 2816 ('', 'template', '', _('display with template')),
2816 2817 ('e', 'ssh', '', _('specify ssh command to use')),
2817 2818 ('', 'remotecmd', '',
2818 2819 _('specify hg command to run on the remote side'))],
2819 2820 _('hg outgoing [-M] [-p] [-n] [DEST]')),
2820 2821 "^parents":
2821 2822 (parents,
2822 2823 [('b', 'branches', None, _('show branches')),
2823 2824 ('', 'style', '', _('display using template map file')),
2824 2825 ('', 'template', '', _('display with template'))],
2825 2826 _('hg parents [-b] [REV]')),
2826 2827 "paths": (paths, [], _('hg paths [NAME]')),
2827 2828 "^pull":
2828 2829 (pull,
2829 2830 [('u', 'update', None,
2830 2831 _('update the working directory to tip after pull')),
2831 2832 ('e', 'ssh', '', _('specify ssh command to use')),
2832 2833 ('f', 'force', None,
2833 2834 _('run even when remote repository is unrelated')),
2834 2835 ('r', 'rev', [], _('a specific revision you would like to pull')),
2835 2836 ('', 'remotecmd', '',
2836 2837 _('specify hg command to run on the remote side'))],
2837 2838 _('hg pull [-u] [-e FILE] [-r REV]... [--remotecmd FILE] [SOURCE]')),
2838 2839 "^push":
2839 2840 (push,
2840 2841 [('f', 'force', None, _('force push')),
2841 2842 ('e', 'ssh', '', _('specify ssh command to use')),
2842 2843 ('r', 'rev', [], _('a specific revision you would like to push')),
2843 2844 ('', 'remotecmd', '',
2844 2845 _('specify hg command to run on the remote side'))],
2845 2846 _('hg push [-f] [-e FILE] [-r REV]... [--remotecmd FILE] [DEST]')),
2846 2847 "debugrawcommit|rawcommit":
2847 2848 (rawcommit,
2848 2849 [('p', 'parent', [], _('parent')),
2849 2850 ('d', 'date', '', _('date code')),
2850 2851 ('u', 'user', '', _('user')),
2851 2852 ('F', 'files', '', _('file list')),
2852 2853 ('m', 'message', '', _('commit message')),
2853 2854 ('l', 'logfile', '', _('commit message file'))],
2854 2855 _('hg debugrawcommit [OPTION]... [FILE]...')),
2855 2856 "recover": (recover, [], _('hg recover')),
2856 2857 "^remove|rm":
2857 2858 (remove,
2858 2859 [('f', 'force', None, _('remove file even if modified')),
2859 2860 ('I', 'include', [], _('include names matching the given patterns')),
2860 2861 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2861 2862 _('hg remove [OPTION]... FILE...')),
2862 2863 "rename|mv":
2863 2864 (rename,
2864 2865 [('A', 'after', None, _('record a rename that has already occurred')),
2865 2866 ('f', 'force', None,
2866 2867 _('forcibly copy over an existing managed file')),
2867 2868 ('I', 'include', [], _('include names matching the given patterns')),
2868 2869 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2869 2870 _('hg rename [OPTION]... SOURCE... DEST')),
2870 2871 "^revert":
2871 2872 (revert,
2872 2873 [('r', 'rev', '', _('revision to revert to')),
2873 2874 ('I', 'include', [], _('include names matching the given patterns')),
2874 2875 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2875 2876 _('hg revert [-r REV] [NAME]...')),
2876 2877 "root": (root, [], _('hg root')),
2877 2878 "^serve":
2878 2879 (serve,
2879 2880 [('A', 'accesslog', '', _('name of access log file to write to')),
2880 2881 ('d', 'daemon', None, _('run server in background')),
2881 2882 ('', 'daemon-pipefds', '', _('used internally by daemon mode')),
2882 2883 ('E', 'errorlog', '', _('name of error log file to write to')),
2883 2884 ('p', 'port', 0, _('port to use (default: 8000)')),
2884 2885 ('a', 'address', '', _('address to use')),
2885 2886 ('n', 'name', '',
2886 2887 _('name to show in web pages (default: working dir)')),
2887 2888 ('', 'pid-file', '', _('name of file to write process ID to')),
2888 2889 ('', 'stdio', None, _('for remote clients')),
2889 2890 ('t', 'templates', '', _('web templates to use')),
2890 2891 ('', 'style', '', _('template style to use')),
2891 2892 ('6', 'ipv6', None, _('use IPv6 in addition to IPv4'))],
2892 2893 _('hg serve [OPTION]...')),
2893 2894 "^status|st":
2894 2895 (status,
2895 2896 [('m', 'modified', None, _('show only modified files')),
2896 2897 ('a', 'added', None, _('show only added files')),
2897 2898 ('r', 'removed', None, _('show only removed files')),
2898 2899 ('d', 'deleted', None, _('show only deleted (but tracked) files')),
2899 2900 ('u', 'unknown', None, _('show only unknown (not tracked) files')),
2900 2901 ('n', 'no-status', None, _('hide status prefix')),
2901 2902 ('0', 'print0', None,
2902 2903 _('end filenames with NUL, for use with xargs')),
2903 2904 ('I', 'include', [], _('include names matching the given patterns')),
2904 2905 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2905 2906 _('hg status [OPTION]... [FILE]...')),
2906 2907 "tag":
2907 2908 (tag,
2908 2909 [('l', 'local', None, _('make the tag local')),
2909 2910 ('m', 'message', '', _('message for tag commit log entry')),
2910 2911 ('d', 'date', '', _('record datecode as commit date')),
2911 2912 ('u', 'user', '', _('record user as commiter')),
2912 2913 ('r', 'rev', '', _('revision to tag'))],
2913 2914 _('hg tag [-l] [-m TEXT] [-d DATE] [-u USER] [-r REV] NAME')),
2914 2915 "tags": (tags, [], _('hg tags')),
2915 2916 "tip":
2916 2917 (tip,
2917 2918 [('b', 'branches', None, _('show branches')),
2918 2919 ('', 'style', '', _('display using template map file')),
2919 2920 ('p', 'patch', None, _('show patch')),
2920 2921 ('', 'template', '', _('display with template'))],
2921 2922 _('hg tip [-b] [-p]')),
2922 2923 "unbundle":
2923 2924 (unbundle,
2924 2925 [('u', 'update', None,
2925 2926 _('update the working directory to tip after unbundle'))],
2926 2927 _('hg unbundle [-u] FILE')),
2927 2928 "undo": (undo, [], _('hg undo')),
2928 2929 "^update|up|checkout|co":
2929 2930 (update,
2930 2931 [('b', 'branch', '', _('checkout the head of a specific branch')),
2931 2932 ('', 'style', '', _('display using template map file')),
2932 2933 ('m', 'merge', None, _('allow merging of branches')),
2933 2934 ('C', 'clean', None, _('overwrite locally modified files')),
2934 2935 ('f', 'force', None, _('force a merge with outstanding changes')),
2935 2936 ('', 'template', '', _('display with template'))],
2936 2937 _('hg update [-b TAG] [-m] [-C] [-f] [REV]')),
2937 2938 "verify": (verify, [], _('hg verify')),
2938 2939 "version": (show_version, [], _('hg version')),
2939 2940 }
2940 2941
2941 2942 globalopts = [
2942 2943 ('R', 'repository', '',
2943 2944 _('repository root directory or symbolic path name')),
2944 2945 ('', 'cwd', '', _('change working directory')),
2945 2946 ('y', 'noninteractive', None,
2946 2947 _('do not prompt, assume \'yes\' for any required answers')),
2947 2948 ('q', 'quiet', None, _('suppress output')),
2948 2949 ('v', 'verbose', None, _('enable additional output')),
2949 2950 ('', 'debug', None, _('enable debugging output')),
2950 2951 ('', 'debugger', None, _('start debugger')),
2951 2952 ('', 'traceback', None, _('print traceback on exception')),
2952 2953 ('', 'time', None, _('time how long the command takes')),
2953 2954 ('', 'profile', None, _('print command execution profile')),
2954 2955 ('', 'version', None, _('output version information and exit')),
2955 2956 ('h', 'help', None, _('display help and exit')),
2956 2957 ]
2957 2958
2958 2959 norepo = ("clone init version help debugancestor debugcomplete debugdata"
2959 2960 " debugindex debugindexdot")
2960 2961 optionalrepo = ("paths debugconfig")
2961 2962
2962 2963 def findpossible(cmd):
2963 2964 """
2964 2965 Return cmd -> (aliases, command table entry)
2965 2966 for each matching command
2966 2967 """
2967 2968 choice = {}
2968 2969 debugchoice = {}
2969 2970 for e in table.keys():
2970 2971 aliases = e.lstrip("^").split("|")
2971 2972 if cmd in aliases:
2972 2973 choice[cmd] = (aliases, table[e])
2973 2974 continue
2974 2975 for a in aliases:
2975 2976 if a.startswith(cmd):
2976 2977 if aliases[0].startswith("debug"):
2977 2978 debugchoice[a] = (aliases, table[e])
2978 2979 else:
2979 2980 choice[a] = (aliases, table[e])
2980 2981 break
2981 2982
2982 2983 if not choice and debugchoice:
2983 2984 choice = debugchoice
2984 2985
2985 2986 return choice
2986 2987
2987 2988 def find(cmd):
2988 2989 """Return (aliases, command table entry) for command string."""
2989 2990 choice = findpossible(cmd)
2990 2991
2991 2992 if choice.has_key(cmd):
2992 2993 return choice[cmd]
2993 2994
2994 2995 if len(choice) > 1:
2995 2996 clist = choice.keys()
2996 2997 clist.sort()
2997 2998 raise AmbiguousCommand(cmd, clist)
2998 2999
2999 3000 if choice:
3000 3001 return choice.values()[0]
3001 3002
3002 3003 raise UnknownCommand(cmd)
3003 3004
3004 3005 class SignalInterrupt(Exception):
3005 3006 """Exception raised on SIGTERM and SIGHUP."""
3006 3007
3007 3008 def catchterm(*args):
3008 3009 raise SignalInterrupt
3009 3010
3010 3011 def run():
3011 3012 sys.exit(dispatch(sys.argv[1:]))
3012 3013
3013 3014 class ParseError(Exception):
3014 3015 """Exception raised on errors in parsing the command line."""
3015 3016
3016 3017 def parse(ui, args):
3017 3018 options = {}
3018 3019 cmdoptions = {}
3019 3020
3020 3021 try:
3021 3022 args = fancyopts.fancyopts(args, globalopts, options)
3022 3023 except fancyopts.getopt.GetoptError, inst:
3023 3024 raise ParseError(None, inst)
3024 3025
3025 3026 if args:
3026 3027 cmd, args = args[0], args[1:]
3027 3028 aliases, i = find(cmd)
3028 3029 cmd = aliases[0]
3029 3030 defaults = ui.config("defaults", cmd)
3030 3031 if defaults:
3031 3032 args = defaults.split() + args
3032 3033 c = list(i[1])
3033 3034 else:
3034 3035 cmd = None
3035 3036 c = []
3036 3037
3037 3038 # combine global options into local
3038 3039 for o in globalopts:
3039 3040 c.append((o[0], o[1], options[o[1]], o[3]))
3040 3041
3041 3042 try:
3042 3043 args = fancyopts.fancyopts(args, c, cmdoptions)
3043 3044 except fancyopts.getopt.GetoptError, inst:
3044 3045 raise ParseError(cmd, inst)
3045 3046
3046 3047 # separate global options back out
3047 3048 for o in globalopts:
3048 3049 n = o[1]
3049 3050 options[n] = cmdoptions[n]
3050 3051 del cmdoptions[n]
3051 3052
3052 3053 return (cmd, cmd and i[0] or None, args, options, cmdoptions)
3053 3054
3054 3055 def dispatch(args):
3055 3056 signal.signal(signal.SIGTERM, catchterm)
3056 3057 try:
3057 3058 signal.signal(signal.SIGHUP, catchterm)
3058 3059 except AttributeError:
3059 3060 pass
3060 3061
3061 3062 try:
3062 3063 u = ui.ui()
3063 3064 except util.Abort, inst:
3064 3065 sys.stderr.write(_("abort: %s\n") % inst)
3065 3066 sys.exit(1)
3066 3067
3067 3068 external = []
3068 3069 for x in u.extensions():
3069 3070 def on_exception(exc, inst):
3070 3071 u.warn(_("*** failed to import extension %s\n") % x[1])
3071 3072 u.warn("%s\n" % inst)
3072 3073 if "--traceback" in sys.argv[1:]:
3073 3074 traceback.print_exc()
3074 3075 if x[1]:
3075 3076 try:
3076 3077 mod = imp.load_source(x[0], x[1])
3077 3078 except Exception, inst:
3078 3079 on_exception(Exception, inst)
3079 3080 continue
3080 3081 else:
3081 3082 def importh(name):
3082 3083 mod = __import__(name)
3083 3084 components = name.split('.')
3084 3085 for comp in components[1:]:
3085 3086 mod = getattr(mod, comp)
3086 3087 return mod
3087 3088 try:
3088 3089 try:
3089 3090 mod = importh("hgext." + x[0])
3090 3091 except ImportError:
3091 3092 mod = importh(x[0])
3092 3093 except Exception, inst:
3093 3094 on_exception(Exception, inst)
3094 3095 continue
3095 3096
3096 3097 external.append(mod)
3097 3098 for x in external:
3098 3099 cmdtable = getattr(x, 'cmdtable', {})
3099 3100 for t in cmdtable:
3100 3101 if t in table:
3101 3102 u.warn(_("module %s overrides %s\n") % (x.__name__, t))
3102 3103 table.update(cmdtable)
3103 3104
3104 3105 try:
3105 3106 cmd, func, args, options, cmdoptions = parse(u, args)
3106 3107 if options["time"]:
3107 3108 def get_times():
3108 3109 t = os.times()
3109 3110 if t[4] == 0.0: # Windows leaves this as zero, so use time.clock()
3110 3111 t = (t[0], t[1], t[2], t[3], time.clock())
3111 3112 return t
3112 3113 s = get_times()
3113 3114 def print_time():
3114 3115 t = get_times()
3115 3116 u.warn(_("Time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n") %
3116 3117 (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3]))
3117 3118 atexit.register(print_time)
3118 3119
3119 3120 u.updateopts(options["verbose"], options["debug"], options["quiet"],
3120 3121 not options["noninteractive"])
3121 3122
3122 3123 # enter the debugger before command execution
3123 3124 if options['debugger']:
3124 3125 pdb.set_trace()
3125 3126
3126 3127 try:
3127 3128 if options['cwd']:
3128 3129 try:
3129 3130 os.chdir(options['cwd'])
3130 3131 except OSError, inst:
3131 3132 raise util.Abort('%s: %s' %
3132 3133 (options['cwd'], inst.strerror))
3133 3134
3134 3135 path = u.expandpath(options["repository"]) or ""
3135 3136 repo = path and hg.repository(u, path=path) or None
3136 3137
3137 3138 if options['help']:
3138 3139 help_(u, cmd, options['version'])
3139 3140 sys.exit(0)
3140 3141 elif options['version']:
3141 3142 show_version(u)
3142 3143 sys.exit(0)
3143 3144 elif not cmd:
3144 3145 help_(u, 'shortlist')
3145 3146 sys.exit(0)
3146 3147
3147 3148 if cmd not in norepo.split():
3148 3149 try:
3149 3150 if not repo:
3150 3151 repo = hg.repository(u, path=path)
3151 3152 u = repo.ui
3152 3153 for x in external:
3153 3154 if hasattr(x, 'reposetup'):
3154 3155 x.reposetup(u, repo)
3155 3156 except hg.RepoError:
3156 3157 if cmd not in optionalrepo.split():
3157 3158 raise
3158 3159 d = lambda: func(u, repo, *args, **cmdoptions)
3159 3160 else:
3160 3161 d = lambda: func(u, *args, **cmdoptions)
3161 3162
3162 3163 try:
3163 3164 if options['profile']:
3164 3165 import hotshot, hotshot.stats
3165 3166 prof = hotshot.Profile("hg.prof")
3166 3167 try:
3167 3168 try:
3168 3169 return prof.runcall(d)
3169 3170 except:
3170 3171 try:
3171 3172 u.warn(_('exception raised - generating '
3172 3173 'profile anyway\n'))
3173 3174 except:
3174 3175 pass
3175 3176 raise
3176 3177 finally:
3177 3178 prof.close()
3178 3179 stats = hotshot.stats.load("hg.prof")
3179 3180 stats.strip_dirs()
3180 3181 stats.sort_stats('time', 'calls')
3181 3182 stats.print_stats(40)
3182 3183 else:
3183 3184 return d()
3184 3185 finally:
3185 3186 u.flush()
3186 3187 except:
3187 3188 # enter the debugger when we hit an exception
3188 3189 if options['debugger']:
3189 3190 pdb.post_mortem(sys.exc_info()[2])
3190 3191 if options['traceback']:
3191 3192 traceback.print_exc()
3192 3193 raise
3193 3194 except ParseError, inst:
3194 3195 if inst.args[0]:
3195 3196 u.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1]))
3196 3197 help_(u, inst.args[0])
3197 3198 else:
3198 3199 u.warn(_("hg: %s\n") % inst.args[1])
3199 3200 help_(u, 'shortlist')
3200 3201 sys.exit(-1)
3201 3202 except AmbiguousCommand, inst:
3202 3203 u.warn(_("hg: command '%s' is ambiguous:\n %s\n") %
3203 3204 (inst.args[0], " ".join(inst.args[1])))
3204 3205 sys.exit(1)
3205 3206 except UnknownCommand, inst:
3206 3207 u.warn(_("hg: unknown command '%s'\n") % inst.args[0])
3207 3208 help_(u, 'shortlist')
3208 3209 sys.exit(1)
3209 3210 except hg.RepoError, inst:
3210 3211 u.warn(_("abort: "), inst, "!\n")
3211 3212 except revlog.RevlogError, inst:
3212 3213 u.warn(_("abort: "), inst, "!\n")
3213 3214 except SignalInterrupt:
3214 3215 u.warn(_("killed!\n"))
3215 3216 except KeyboardInterrupt:
3216 3217 try:
3217 3218 u.warn(_("interrupted!\n"))
3218 3219 except IOError, inst:
3219 3220 if inst.errno == errno.EPIPE:
3220 3221 if u.debugflag:
3221 3222 u.warn(_("\nbroken pipe\n"))
3222 3223 else:
3223 3224 raise
3224 3225 except IOError, inst:
3225 3226 if hasattr(inst, "code"):
3226 3227 u.warn(_("abort: %s\n") % inst)
3227 3228 elif hasattr(inst, "reason"):
3228 3229 u.warn(_("abort: error: %s\n") % inst.reason[1])
3229 3230 elif hasattr(inst, "args") and inst[0] == errno.EPIPE:
3230 3231 if u.debugflag:
3231 3232 u.warn(_("broken pipe\n"))
3232 3233 elif getattr(inst, "strerror", None):
3233 3234 if getattr(inst, "filename", None):
3234 3235 u.warn(_("abort: %s - %s\n") % (inst.strerror, inst.filename))
3235 3236 else:
3236 3237 u.warn(_("abort: %s\n") % inst.strerror)
3237 3238 else:
3238 3239 raise
3239 3240 except OSError, inst:
3240 3241 if hasattr(inst, "filename"):
3241 3242 u.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
3242 3243 else:
3243 3244 u.warn(_("abort: %s\n") % inst.strerror)
3244 3245 except util.Abort, inst:
3245 3246 u.warn(_('abort: '), inst.args[0] % inst.args[1:], '\n')
3246 3247 sys.exit(1)
3247 3248 except TypeError, inst:
3248 3249 # was this an argument error?
3249 3250 tb = traceback.extract_tb(sys.exc_info()[2])
3250 3251 if len(tb) > 2: # no
3251 3252 raise
3252 3253 u.debug(inst, "\n")
3253 3254 u.warn(_("%s: invalid arguments\n") % cmd)
3254 3255 help_(u, cmd)
3255 3256 except SystemExit:
3256 3257 # don't catch this in the catch-all below
3257 3258 raise
3258 3259 except:
3259 3260 u.warn(_("** unknown exception encountered, details follow\n"))
3260 3261 u.warn(_("** report bug details to mercurial@selenic.com\n"))
3261 3262 u.warn(_("** Mercurial Distributed SCM (version %s)\n")
3262 3263 % version.get_version())
3263 3264 raise
3264 3265
3265 3266 sys.exit(-1)
General Comments 0
You need to be logged in to leave comments. Login now