##// END OF EJS Templates
revert: allow configuring the .orig file location
Christian Delahousse -
r26938:080276d3 default
parent child Browse files
Show More
@@ -1,3402 +1,3402
1 1 # cmdutil.py - help for command processing in mercurial
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from node import hex, bin, nullid, nullrev, short
9 9 from i18n import _
10 10 import os, sys, errno, re, tempfile, cStringIO, shutil
11 11 import util, scmutil, templater, patch, error, templatekw, revlog, copies
12 12 import match as matchmod
13 13 import repair, graphmod, revset, phases, obsolete, pathutil
14 14 import changelog
15 15 import bookmarks
16 16 import encoding
17 17 import formatter
18 18 import crecord as crecordmod
19 19 import lock as lockmod
20 20
21 21 def ishunk(x):
22 22 hunkclasses = (crecordmod.uihunk, patch.recordhunk)
23 23 return isinstance(x, hunkclasses)
24 24
25 25 def newandmodified(chunks, originalchunks):
26 26 newlyaddedandmodifiedfiles = set()
27 27 for chunk in chunks:
28 28 if ishunk(chunk) and chunk.header.isnewfile() and chunk not in \
29 29 originalchunks:
30 30 newlyaddedandmodifiedfiles.add(chunk.header.filename())
31 31 return newlyaddedandmodifiedfiles
32 32
33 33 def parsealiases(cmd):
34 34 return cmd.lstrip("^").split("|")
35 35
36 36 def setupwrapcolorwrite(ui):
37 37 # wrap ui.write so diff output can be labeled/colorized
38 38 def wrapwrite(orig, *args, **kw):
39 39 label = kw.pop('label', '')
40 40 for chunk, l in patch.difflabel(lambda: args):
41 41 orig(chunk, label=label + l)
42 42
43 43 oldwrite = ui.write
44 44 def wrap(*args, **kwargs):
45 45 return wrapwrite(oldwrite, *args, **kwargs)
46 46 setattr(ui, 'write', wrap)
47 47 return oldwrite
48 48
49 49 def filterchunks(ui, originalhunks, usecurses, testfile, operation=None):
50 50 if usecurses:
51 51 if testfile:
52 52 recordfn = crecordmod.testdecorator(testfile,
53 53 crecordmod.testchunkselector)
54 54 else:
55 55 recordfn = crecordmod.chunkselector
56 56
57 57 return crecordmod.filterpatch(ui, originalhunks, recordfn, operation)
58 58
59 59 else:
60 60 return patch.filterpatch(ui, originalhunks, operation)
61 61
62 62 def recordfilter(ui, originalhunks, operation=None):
63 63 """ Prompts the user to filter the originalhunks and return a list of
64 64 selected hunks.
65 65 *operation* is used for ui purposes to indicate the user
66 66 what kind of filtering they are doing: reverting, committing, shelving, etc.
67 67 *operation* has to be a translated string.
68 68 """
69 69 usecurses = ui.configbool('experimental', 'crecord', False)
70 70 testfile = ui.config('experimental', 'crecordtest', None)
71 71 oldwrite = setupwrapcolorwrite(ui)
72 72 try:
73 73 newchunks = filterchunks(ui, originalhunks, usecurses, testfile,
74 74 operation)
75 75 finally:
76 76 ui.write = oldwrite
77 77 return newchunks
78 78
79 79 def dorecord(ui, repo, commitfunc, cmdsuggest, backupall,
80 80 filterfn, *pats, **opts):
81 81 import merge as mergemod
82 82
83 83 if not ui.interactive():
84 84 if cmdsuggest:
85 85 msg = _('running non-interactively, use %s instead') % cmdsuggest
86 86 else:
87 87 msg = _('running non-interactively')
88 88 raise error.Abort(msg)
89 89
90 90 # make sure username is set before going interactive
91 91 if not opts.get('user'):
92 92 ui.username() # raise exception, username not provided
93 93
94 94 def recordfunc(ui, repo, message, match, opts):
95 95 """This is generic record driver.
96 96
97 97 Its job is to interactively filter local changes, and
98 98 accordingly prepare working directory into a state in which the
99 99 job can be delegated to a non-interactive commit command such as
100 100 'commit' or 'qrefresh'.
101 101
102 102 After the actual job is done by non-interactive command, the
103 103 working directory is restored to its original state.
104 104
105 105 In the end we'll record interesting changes, and everything else
106 106 will be left in place, so the user can continue working.
107 107 """
108 108
109 109 checkunfinished(repo, commit=True)
110 110 merge = len(repo[None].parents()) > 1
111 111 if merge:
112 112 raise error.Abort(_('cannot partially commit a merge '
113 113 '(use "hg commit" instead)'))
114 114
115 115 status = repo.status(match=match)
116 116 diffopts = patch.difffeatureopts(ui, opts=opts, whitespace=True)
117 117 diffopts.nodates = True
118 118 diffopts.git = True
119 119 originaldiff = patch.diff(repo, changes=status, opts=diffopts)
120 120 originalchunks = patch.parsepatch(originaldiff)
121 121
122 122 # 1. filter patch, so we have intending-to apply subset of it
123 123 try:
124 124 chunks = filterfn(ui, originalchunks)
125 125 except patch.PatchError as err:
126 126 raise error.Abort(_('error parsing patch: %s') % err)
127 127
128 128 # We need to keep a backup of files that have been newly added and
129 129 # modified during the recording process because there is a previous
130 130 # version without the edit in the workdir
131 131 newlyaddedandmodifiedfiles = newandmodified(chunks, originalchunks)
132 132 contenders = set()
133 133 for h in chunks:
134 134 try:
135 135 contenders.update(set(h.files()))
136 136 except AttributeError:
137 137 pass
138 138
139 139 changed = status.modified + status.added + status.removed
140 140 newfiles = [f for f in changed if f in contenders]
141 141 if not newfiles:
142 142 ui.status(_('no changes to record\n'))
143 143 return 0
144 144
145 145 modified = set(status.modified)
146 146
147 147 # 2. backup changed files, so we can restore them in the end
148 148
149 149 if backupall:
150 150 tobackup = changed
151 151 else:
152 152 tobackup = [f for f in newfiles if f in modified or f in \
153 153 newlyaddedandmodifiedfiles]
154 154 backups = {}
155 155 if tobackup:
156 156 backupdir = repo.join('record-backups')
157 157 try:
158 158 os.mkdir(backupdir)
159 159 except OSError as err:
160 160 if err.errno != errno.EEXIST:
161 161 raise
162 162 try:
163 163 # backup continues
164 164 for f in tobackup:
165 165 fd, tmpname = tempfile.mkstemp(prefix=f.replace('/', '_')+'.',
166 166 dir=backupdir)
167 167 os.close(fd)
168 168 ui.debug('backup %r as %r\n' % (f, tmpname))
169 169 util.copyfile(repo.wjoin(f), tmpname)
170 170 shutil.copystat(repo.wjoin(f), tmpname)
171 171 backups[f] = tmpname
172 172
173 173 fp = cStringIO.StringIO()
174 174 for c in chunks:
175 175 fname = c.filename()
176 176 if fname in backups:
177 177 c.write(fp)
178 178 dopatch = fp.tell()
179 179 fp.seek(0)
180 180
181 181 [os.unlink(repo.wjoin(c)) for c in newlyaddedandmodifiedfiles]
182 182 # 3a. apply filtered patch to clean repo (clean)
183 183 if backups:
184 184 # Equivalent to hg.revert
185 185 choices = lambda key: key in backups
186 186 mergemod.update(repo, repo.dirstate.p1(),
187 187 False, True, choices)
188 188
189 189 # 3b. (apply)
190 190 if dopatch:
191 191 try:
192 192 ui.debug('applying patch\n')
193 193 ui.debug(fp.getvalue())
194 194 patch.internalpatch(ui, repo, fp, 1, eolmode=None)
195 195 except patch.PatchError as err:
196 196 raise error.Abort(str(err))
197 197 del fp
198 198
199 199 # 4. We prepared working directory according to filtered
200 200 # patch. Now is the time to delegate the job to
201 201 # commit/qrefresh or the like!
202 202
203 203 # Make all of the pathnames absolute.
204 204 newfiles = [repo.wjoin(nf) for nf in newfiles]
205 205 return commitfunc(ui, repo, *newfiles, **opts)
206 206 finally:
207 207 # 5. finally restore backed-up files
208 208 try:
209 209 dirstate = repo.dirstate
210 210 for realname, tmpname in backups.iteritems():
211 211 ui.debug('restoring %r to %r\n' % (tmpname, realname))
212 212
213 213 if dirstate[realname] == 'n':
214 214 # without normallookup, restoring timestamp
215 215 # may cause partially committed files
216 216 # to be treated as unmodified
217 217 dirstate.normallookup(realname)
218 218
219 219 util.copyfile(tmpname, repo.wjoin(realname))
220 220 # Our calls to copystat() here and above are a
221 221 # hack to trick any editors that have f open that
222 222 # we haven't modified them.
223 223 #
224 224 # Also note that this racy as an editor could
225 225 # notice the file's mtime before we've finished
226 226 # writing it.
227 227 shutil.copystat(tmpname, repo.wjoin(realname))
228 228 os.unlink(tmpname)
229 229 if tobackup:
230 230 os.rmdir(backupdir)
231 231 except OSError:
232 232 pass
233 233
234 234 def recordinwlock(ui, repo, message, match, opts):
235 235 wlock = repo.wlock()
236 236 try:
237 237 return recordfunc(ui, repo, message, match, opts)
238 238 finally:
239 239 wlock.release()
240 240
241 241 return commit(ui, repo, recordinwlock, pats, opts)
242 242
243 243 def findpossible(cmd, table, strict=False):
244 244 """
245 245 Return cmd -> (aliases, command table entry)
246 246 for each matching command.
247 247 Return debug commands (or their aliases) only if no normal command matches.
248 248 """
249 249 choice = {}
250 250 debugchoice = {}
251 251
252 252 if cmd in table:
253 253 # short-circuit exact matches, "log" alias beats "^log|history"
254 254 keys = [cmd]
255 255 else:
256 256 keys = table.keys()
257 257
258 258 allcmds = []
259 259 for e in keys:
260 260 aliases = parsealiases(e)
261 261 allcmds.extend(aliases)
262 262 found = None
263 263 if cmd in aliases:
264 264 found = cmd
265 265 elif not strict:
266 266 for a in aliases:
267 267 if a.startswith(cmd):
268 268 found = a
269 269 break
270 270 if found is not None:
271 271 if aliases[0].startswith("debug") or found.startswith("debug"):
272 272 debugchoice[found] = (aliases, table[e])
273 273 else:
274 274 choice[found] = (aliases, table[e])
275 275
276 276 if not choice and debugchoice:
277 277 choice = debugchoice
278 278
279 279 return choice, allcmds
280 280
281 281 def findcmd(cmd, table, strict=True):
282 282 """Return (aliases, command table entry) for command string."""
283 283 choice, allcmds = findpossible(cmd, table, strict)
284 284
285 285 if cmd in choice:
286 286 return choice[cmd]
287 287
288 288 if len(choice) > 1:
289 289 clist = choice.keys()
290 290 clist.sort()
291 291 raise error.AmbiguousCommand(cmd, clist)
292 292
293 293 if choice:
294 294 return choice.values()[0]
295 295
296 296 raise error.UnknownCommand(cmd, allcmds)
297 297
298 298 def findrepo(p):
299 299 while not os.path.isdir(os.path.join(p, ".hg")):
300 300 oldp, p = p, os.path.dirname(p)
301 301 if p == oldp:
302 302 return None
303 303
304 304 return p
305 305
306 306 def bailifchanged(repo, merge=True):
307 307 if merge and repo.dirstate.p2() != nullid:
308 308 raise error.Abort(_('outstanding uncommitted merge'))
309 309 modified, added, removed, deleted = repo.status()[:4]
310 310 if modified or added or removed or deleted:
311 311 raise error.Abort(_('uncommitted changes'))
312 312 ctx = repo[None]
313 313 for s in sorted(ctx.substate):
314 314 ctx.sub(s).bailifchanged()
315 315
316 316 def logmessage(ui, opts):
317 317 """ get the log message according to -m and -l option """
318 318 message = opts.get('message')
319 319 logfile = opts.get('logfile')
320 320
321 321 if message and logfile:
322 322 raise error.Abort(_('options --message and --logfile are mutually '
323 323 'exclusive'))
324 324 if not message and logfile:
325 325 try:
326 326 if logfile == '-':
327 327 message = ui.fin.read()
328 328 else:
329 329 message = '\n'.join(util.readfile(logfile).splitlines())
330 330 except IOError as inst:
331 331 raise error.Abort(_("can't read commit message '%s': %s") %
332 332 (logfile, inst.strerror))
333 333 return message
334 334
335 335 def mergeeditform(ctxorbool, baseformname):
336 336 """return appropriate editform name (referencing a committemplate)
337 337
338 338 'ctxorbool' is either a ctx to be committed, or a bool indicating whether
339 339 merging is committed.
340 340
341 341 This returns baseformname with '.merge' appended if it is a merge,
342 342 otherwise '.normal' is appended.
343 343 """
344 344 if isinstance(ctxorbool, bool):
345 345 if ctxorbool:
346 346 return baseformname + ".merge"
347 347 elif 1 < len(ctxorbool.parents()):
348 348 return baseformname + ".merge"
349 349
350 350 return baseformname + ".normal"
351 351
352 352 def getcommiteditor(edit=False, finishdesc=None, extramsg=None,
353 353 editform='', **opts):
354 354 """get appropriate commit message editor according to '--edit' option
355 355
356 356 'finishdesc' is a function to be called with edited commit message
357 357 (= 'description' of the new changeset) just after editing, but
358 358 before checking empty-ness. It should return actual text to be
359 359 stored into history. This allows to change description before
360 360 storing.
361 361
362 362 'extramsg' is a extra message to be shown in the editor instead of
363 363 'Leave message empty to abort commit' line. 'HG: ' prefix and EOL
364 364 is automatically added.
365 365
366 366 'editform' is a dot-separated list of names, to distinguish
367 367 the purpose of commit text editing.
368 368
369 369 'getcommiteditor' returns 'commitforceeditor' regardless of
370 370 'edit', if one of 'finishdesc' or 'extramsg' is specified, because
371 371 they are specific for usage in MQ.
372 372 """
373 373 if edit or finishdesc or extramsg:
374 374 return lambda r, c, s: commitforceeditor(r, c, s,
375 375 finishdesc=finishdesc,
376 376 extramsg=extramsg,
377 377 editform=editform)
378 378 elif editform:
379 379 return lambda r, c, s: commiteditor(r, c, s, editform=editform)
380 380 else:
381 381 return commiteditor
382 382
383 383 def loglimit(opts):
384 384 """get the log limit according to option -l/--limit"""
385 385 limit = opts.get('limit')
386 386 if limit:
387 387 try:
388 388 limit = int(limit)
389 389 except ValueError:
390 390 raise error.Abort(_('limit must be a positive integer'))
391 391 if limit <= 0:
392 392 raise error.Abort(_('limit must be positive'))
393 393 else:
394 394 limit = None
395 395 return limit
396 396
397 397 def makefilename(repo, pat, node, desc=None,
398 398 total=None, seqno=None, revwidth=None, pathname=None):
399 399 node_expander = {
400 400 'H': lambda: hex(node),
401 401 'R': lambda: str(repo.changelog.rev(node)),
402 402 'h': lambda: short(node),
403 403 'm': lambda: re.sub('[^\w]', '_', str(desc))
404 404 }
405 405 expander = {
406 406 '%': lambda: '%',
407 407 'b': lambda: os.path.basename(repo.root),
408 408 }
409 409
410 410 try:
411 411 if node:
412 412 expander.update(node_expander)
413 413 if node:
414 414 expander['r'] = (lambda:
415 415 str(repo.changelog.rev(node)).zfill(revwidth or 0))
416 416 if total is not None:
417 417 expander['N'] = lambda: str(total)
418 418 if seqno is not None:
419 419 expander['n'] = lambda: str(seqno)
420 420 if total is not None and seqno is not None:
421 421 expander['n'] = lambda: str(seqno).zfill(len(str(total)))
422 422 if pathname is not None:
423 423 expander['s'] = lambda: os.path.basename(pathname)
424 424 expander['d'] = lambda: os.path.dirname(pathname) or '.'
425 425 expander['p'] = lambda: pathname
426 426
427 427 newname = []
428 428 patlen = len(pat)
429 429 i = 0
430 430 while i < patlen:
431 431 c = pat[i]
432 432 if c == '%':
433 433 i += 1
434 434 c = pat[i]
435 435 c = expander[c]()
436 436 newname.append(c)
437 437 i += 1
438 438 return ''.join(newname)
439 439 except KeyError as inst:
440 440 raise error.Abort(_("invalid format spec '%%%s' in output filename") %
441 441 inst.args[0])
442 442
443 443 def makefileobj(repo, pat, node=None, desc=None, total=None,
444 444 seqno=None, revwidth=None, mode='wb', modemap=None,
445 445 pathname=None):
446 446
447 447 writable = mode not in ('r', 'rb')
448 448
449 449 if not pat or pat == '-':
450 450 if writable:
451 451 fp = repo.ui.fout
452 452 else:
453 453 fp = repo.ui.fin
454 454 if util.safehasattr(fp, 'fileno'):
455 455 return os.fdopen(os.dup(fp.fileno()), mode)
456 456 else:
457 457 # if this fp can't be duped properly, return
458 458 # a dummy object that can be closed
459 459 class wrappedfileobj(object):
460 460 noop = lambda x: None
461 461 def __init__(self, f):
462 462 self.f = f
463 463 def __getattr__(self, attr):
464 464 if attr == 'close':
465 465 return self.noop
466 466 else:
467 467 return getattr(self.f, attr)
468 468
469 469 return wrappedfileobj(fp)
470 470 if util.safehasattr(pat, 'write') and writable:
471 471 return pat
472 472 if util.safehasattr(pat, 'read') and 'r' in mode:
473 473 return pat
474 474 fn = makefilename(repo, pat, node, desc, total, seqno, revwidth, pathname)
475 475 if modemap is not None:
476 476 mode = modemap.get(fn, mode)
477 477 if mode == 'wb':
478 478 modemap[fn] = 'ab'
479 479 return open(fn, mode)
480 480
481 481 def openrevlog(repo, cmd, file_, opts):
482 482 """opens the changelog, manifest, a filelog or a given revlog"""
483 483 cl = opts['changelog']
484 484 mf = opts['manifest']
485 485 dir = opts['dir']
486 486 msg = None
487 487 if cl and mf:
488 488 msg = _('cannot specify --changelog and --manifest at the same time')
489 489 elif cl and dir:
490 490 msg = _('cannot specify --changelog and --dir at the same time')
491 491 elif cl or mf:
492 492 if file_:
493 493 msg = _('cannot specify filename with --changelog or --manifest')
494 494 elif not repo:
495 495 msg = _('cannot specify --changelog or --manifest or --dir '
496 496 'without a repository')
497 497 if msg:
498 498 raise error.Abort(msg)
499 499
500 500 r = None
501 501 if repo:
502 502 if cl:
503 503 r = repo.unfiltered().changelog
504 504 elif dir:
505 505 if 'treemanifest' not in repo.requirements:
506 506 raise error.Abort(_("--dir can only be used on repos with "
507 507 "treemanifest enabled"))
508 508 dirlog = repo.dirlog(file_)
509 509 if len(dirlog):
510 510 r = dirlog
511 511 elif mf:
512 512 r = repo.manifest
513 513 elif file_:
514 514 filelog = repo.file(file_)
515 515 if len(filelog):
516 516 r = filelog
517 517 if not r:
518 518 if not file_:
519 519 raise error.CommandError(cmd, _('invalid arguments'))
520 520 if not os.path.isfile(file_):
521 521 raise error.Abort(_("revlog '%s' not found") % file_)
522 522 r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False),
523 523 file_[:-2] + ".i")
524 524 return r
525 525
526 526 def copy(ui, repo, pats, opts, rename=False):
527 527 # called with the repo lock held
528 528 #
529 529 # hgsep => pathname that uses "/" to separate directories
530 530 # ossep => pathname that uses os.sep to separate directories
531 531 cwd = repo.getcwd()
532 532 targets = {}
533 533 after = opts.get("after")
534 534 dryrun = opts.get("dry_run")
535 535 wctx = repo[None]
536 536
537 537 def walkpat(pat):
538 538 srcs = []
539 539 if after:
540 540 badstates = '?'
541 541 else:
542 542 badstates = '?r'
543 543 m = scmutil.match(repo[None], [pat], opts, globbed=True)
544 544 for abs in repo.walk(m):
545 545 state = repo.dirstate[abs]
546 546 rel = m.rel(abs)
547 547 exact = m.exact(abs)
548 548 if state in badstates:
549 549 if exact and state == '?':
550 550 ui.warn(_('%s: not copying - file is not managed\n') % rel)
551 551 if exact and state == 'r':
552 552 ui.warn(_('%s: not copying - file has been marked for'
553 553 ' remove\n') % rel)
554 554 continue
555 555 # abs: hgsep
556 556 # rel: ossep
557 557 srcs.append((abs, rel, exact))
558 558 return srcs
559 559
560 560 # abssrc: hgsep
561 561 # relsrc: ossep
562 562 # otarget: ossep
563 563 def copyfile(abssrc, relsrc, otarget, exact):
564 564 abstarget = pathutil.canonpath(repo.root, cwd, otarget)
565 565 if '/' in abstarget:
566 566 # We cannot normalize abstarget itself, this would prevent
567 567 # case only renames, like a => A.
568 568 abspath, absname = abstarget.rsplit('/', 1)
569 569 abstarget = repo.dirstate.normalize(abspath) + '/' + absname
570 570 reltarget = repo.pathto(abstarget, cwd)
571 571 target = repo.wjoin(abstarget)
572 572 src = repo.wjoin(abssrc)
573 573 state = repo.dirstate[abstarget]
574 574
575 575 scmutil.checkportable(ui, abstarget)
576 576
577 577 # check for collisions
578 578 prevsrc = targets.get(abstarget)
579 579 if prevsrc is not None:
580 580 ui.warn(_('%s: not overwriting - %s collides with %s\n') %
581 581 (reltarget, repo.pathto(abssrc, cwd),
582 582 repo.pathto(prevsrc, cwd)))
583 583 return
584 584
585 585 # check for overwrites
586 586 exists = os.path.lexists(target)
587 587 samefile = False
588 588 if exists and abssrc != abstarget:
589 589 if (repo.dirstate.normalize(abssrc) ==
590 590 repo.dirstate.normalize(abstarget)):
591 591 if not rename:
592 592 ui.warn(_("%s: can't copy - same file\n") % reltarget)
593 593 return
594 594 exists = False
595 595 samefile = True
596 596
597 597 if not after and exists or after and state in 'mn':
598 598 if not opts['force']:
599 599 ui.warn(_('%s: not overwriting - file exists\n') %
600 600 reltarget)
601 601 return
602 602
603 603 if after:
604 604 if not exists:
605 605 if rename:
606 606 ui.warn(_('%s: not recording move - %s does not exist\n') %
607 607 (relsrc, reltarget))
608 608 else:
609 609 ui.warn(_('%s: not recording copy - %s does not exist\n') %
610 610 (relsrc, reltarget))
611 611 return
612 612 elif not dryrun:
613 613 try:
614 614 if exists:
615 615 os.unlink(target)
616 616 targetdir = os.path.dirname(target) or '.'
617 617 if not os.path.isdir(targetdir):
618 618 os.makedirs(targetdir)
619 619 if samefile:
620 620 tmp = target + "~hgrename"
621 621 os.rename(src, tmp)
622 622 os.rename(tmp, target)
623 623 else:
624 624 util.copyfile(src, target)
625 625 srcexists = True
626 626 except IOError as inst:
627 627 if inst.errno == errno.ENOENT:
628 628 ui.warn(_('%s: deleted in working directory\n') % relsrc)
629 629 srcexists = False
630 630 else:
631 631 ui.warn(_('%s: cannot copy - %s\n') %
632 632 (relsrc, inst.strerror))
633 633 return True # report a failure
634 634
635 635 if ui.verbose or not exact:
636 636 if rename:
637 637 ui.status(_('moving %s to %s\n') % (relsrc, reltarget))
638 638 else:
639 639 ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
640 640
641 641 targets[abstarget] = abssrc
642 642
643 643 # fix up dirstate
644 644 scmutil.dirstatecopy(ui, repo, wctx, abssrc, abstarget,
645 645 dryrun=dryrun, cwd=cwd)
646 646 if rename and not dryrun:
647 647 if not after and srcexists and not samefile:
648 648 util.unlinkpath(repo.wjoin(abssrc))
649 649 wctx.forget([abssrc])
650 650
651 651 # pat: ossep
652 652 # dest ossep
653 653 # srcs: list of (hgsep, hgsep, ossep, bool)
654 654 # return: function that takes hgsep and returns ossep
655 655 def targetpathfn(pat, dest, srcs):
656 656 if os.path.isdir(pat):
657 657 abspfx = pathutil.canonpath(repo.root, cwd, pat)
658 658 abspfx = util.localpath(abspfx)
659 659 if destdirexists:
660 660 striplen = len(os.path.split(abspfx)[0])
661 661 else:
662 662 striplen = len(abspfx)
663 663 if striplen:
664 664 striplen += len(os.sep)
665 665 res = lambda p: os.path.join(dest, util.localpath(p)[striplen:])
666 666 elif destdirexists:
667 667 res = lambda p: os.path.join(dest,
668 668 os.path.basename(util.localpath(p)))
669 669 else:
670 670 res = lambda p: dest
671 671 return res
672 672
673 673 # pat: ossep
674 674 # dest ossep
675 675 # srcs: list of (hgsep, hgsep, ossep, bool)
676 676 # return: function that takes hgsep and returns ossep
677 677 def targetpathafterfn(pat, dest, srcs):
678 678 if matchmod.patkind(pat):
679 679 # a mercurial pattern
680 680 res = lambda p: os.path.join(dest,
681 681 os.path.basename(util.localpath(p)))
682 682 else:
683 683 abspfx = pathutil.canonpath(repo.root, cwd, pat)
684 684 if len(abspfx) < len(srcs[0][0]):
685 685 # A directory. Either the target path contains the last
686 686 # component of the source path or it does not.
687 687 def evalpath(striplen):
688 688 score = 0
689 689 for s in srcs:
690 690 t = os.path.join(dest, util.localpath(s[0])[striplen:])
691 691 if os.path.lexists(t):
692 692 score += 1
693 693 return score
694 694
695 695 abspfx = util.localpath(abspfx)
696 696 striplen = len(abspfx)
697 697 if striplen:
698 698 striplen += len(os.sep)
699 699 if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
700 700 score = evalpath(striplen)
701 701 striplen1 = len(os.path.split(abspfx)[0])
702 702 if striplen1:
703 703 striplen1 += len(os.sep)
704 704 if evalpath(striplen1) > score:
705 705 striplen = striplen1
706 706 res = lambda p: os.path.join(dest,
707 707 util.localpath(p)[striplen:])
708 708 else:
709 709 # a file
710 710 if destdirexists:
711 711 res = lambda p: os.path.join(dest,
712 712 os.path.basename(util.localpath(p)))
713 713 else:
714 714 res = lambda p: dest
715 715 return res
716 716
717 717 pats = scmutil.expandpats(pats)
718 718 if not pats:
719 719 raise error.Abort(_('no source or destination specified'))
720 720 if len(pats) == 1:
721 721 raise error.Abort(_('no destination specified'))
722 722 dest = pats.pop()
723 723 destdirexists = os.path.isdir(dest) and not os.path.islink(dest)
724 724 if not destdirexists:
725 725 if len(pats) > 1 or matchmod.patkind(pats[0]):
726 726 raise error.Abort(_('with multiple sources, destination must be an '
727 727 'existing directory'))
728 728 if util.endswithsep(dest):
729 729 raise error.Abort(_('destination %s is not a directory') % dest)
730 730
731 731 tfn = targetpathfn
732 732 if after:
733 733 tfn = targetpathafterfn
734 734 copylist = []
735 735 for pat in pats:
736 736 srcs = walkpat(pat)
737 737 if not srcs:
738 738 continue
739 739 copylist.append((tfn(pat, dest, srcs), srcs))
740 740 if not copylist:
741 741 raise error.Abort(_('no files to copy'))
742 742
743 743 errors = 0
744 744 for targetpath, srcs in copylist:
745 745 for abssrc, relsrc, exact in srcs:
746 746 if copyfile(abssrc, relsrc, targetpath(abssrc), exact):
747 747 errors += 1
748 748
749 749 if errors:
750 750 ui.warn(_('(consider using --after)\n'))
751 751
752 752 return errors != 0
753 753
754 754 def service(opts, parentfn=None, initfn=None, runfn=None, logfile=None,
755 755 runargs=None, appendpid=False):
756 756 '''Run a command as a service.'''
757 757
758 758 def writepid(pid):
759 759 if opts['pid_file']:
760 760 if appendpid:
761 761 mode = 'a'
762 762 else:
763 763 mode = 'w'
764 764 fp = open(opts['pid_file'], mode)
765 765 fp.write(str(pid) + '\n')
766 766 fp.close()
767 767
768 768 if opts['daemon'] and not opts['daemon_pipefds']:
769 769 # Signal child process startup with file removal
770 770 lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-')
771 771 os.close(lockfd)
772 772 try:
773 773 if not runargs:
774 774 runargs = util.hgcmd() + sys.argv[1:]
775 775 runargs.append('--daemon-pipefds=%s' % lockpath)
776 776 # Don't pass --cwd to the child process, because we've already
777 777 # changed directory.
778 778 for i in xrange(1, len(runargs)):
779 779 if runargs[i].startswith('--cwd='):
780 780 del runargs[i]
781 781 break
782 782 elif runargs[i].startswith('--cwd'):
783 783 del runargs[i:i + 2]
784 784 break
785 785 def condfn():
786 786 return not os.path.exists(lockpath)
787 787 pid = util.rundetached(runargs, condfn)
788 788 if pid < 0:
789 789 raise error.Abort(_('child process failed to start'))
790 790 writepid(pid)
791 791 finally:
792 792 try:
793 793 os.unlink(lockpath)
794 794 except OSError as e:
795 795 if e.errno != errno.ENOENT:
796 796 raise
797 797 if parentfn:
798 798 return parentfn(pid)
799 799 else:
800 800 return
801 801
802 802 if initfn:
803 803 initfn()
804 804
805 805 if not opts['daemon']:
806 806 writepid(os.getpid())
807 807
808 808 if opts['daemon_pipefds']:
809 809 lockpath = opts['daemon_pipefds']
810 810 try:
811 811 os.setsid()
812 812 except AttributeError:
813 813 pass
814 814 os.unlink(lockpath)
815 815 util.hidewindow()
816 816 sys.stdout.flush()
817 817 sys.stderr.flush()
818 818
819 819 nullfd = os.open(os.devnull, os.O_RDWR)
820 820 logfilefd = nullfd
821 821 if logfile:
822 822 logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND)
823 823 os.dup2(nullfd, 0)
824 824 os.dup2(logfilefd, 1)
825 825 os.dup2(logfilefd, 2)
826 826 if nullfd not in (0, 1, 2):
827 827 os.close(nullfd)
828 828 if logfile and logfilefd not in (0, 1, 2):
829 829 os.close(logfilefd)
830 830
831 831 if runfn:
832 832 return runfn()
833 833
834 834 ## facility to let extension process additional data into an import patch
835 835 # list of identifier to be executed in order
836 836 extrapreimport = [] # run before commit
837 837 extrapostimport = [] # run after commit
838 838 # mapping from identifier to actual import function
839 839 #
840 840 # 'preimport' are run before the commit is made and are provided the following
841 841 # arguments:
842 842 # - repo: the localrepository instance,
843 843 # - patchdata: data extracted from patch header (cf m.patch.patchheadermap),
844 844 # - extra: the future extra dictionary of the changeset, please mutate it,
845 845 # - opts: the import options.
846 846 # XXX ideally, we would just pass an ctx ready to be computed, that would allow
847 847 # mutation of in memory commit and more. Feel free to rework the code to get
848 848 # there.
849 849 extrapreimportmap = {}
850 850 # 'postimport' are run after the commit is made and are provided the following
851 851 # argument:
852 852 # - ctx: the changectx created by import.
853 853 extrapostimportmap = {}
854 854
855 855 def tryimportone(ui, repo, hunk, parents, opts, msgs, updatefunc):
856 856 """Utility function used by commands.import to import a single patch
857 857
858 858 This function is explicitly defined here to help the evolve extension to
859 859 wrap this part of the import logic.
860 860
861 861 The API is currently a bit ugly because it a simple code translation from
862 862 the import command. Feel free to make it better.
863 863
864 864 :hunk: a patch (as a binary string)
865 865 :parents: nodes that will be parent of the created commit
866 866 :opts: the full dict of option passed to the import command
867 867 :msgs: list to save commit message to.
868 868 (used in case we need to save it when failing)
869 869 :updatefunc: a function that update a repo to a given node
870 870 updatefunc(<repo>, <node>)
871 871 """
872 872 # avoid cycle context -> subrepo -> cmdutil
873 873 import context
874 874 extractdata = patch.extract(ui, hunk)
875 875 tmpname = extractdata.get('filename')
876 876 message = extractdata.get('message')
877 877 user = extractdata.get('user')
878 878 date = extractdata.get('date')
879 879 branch = extractdata.get('branch')
880 880 nodeid = extractdata.get('nodeid')
881 881 p1 = extractdata.get('p1')
882 882 p2 = extractdata.get('p2')
883 883
884 884 update = not opts.get('bypass')
885 885 strip = opts["strip"]
886 886 prefix = opts["prefix"]
887 887 sim = float(opts.get('similarity') or 0)
888 888 if not tmpname:
889 889 return (None, None, False)
890 890 msg = _('applied to working directory')
891 891
892 892 rejects = False
893 893
894 894 try:
895 895 cmdline_message = logmessage(ui, opts)
896 896 if cmdline_message:
897 897 # pickup the cmdline msg
898 898 message = cmdline_message
899 899 elif message:
900 900 # pickup the patch msg
901 901 message = message.strip()
902 902 else:
903 903 # launch the editor
904 904 message = None
905 905 ui.debug('message:\n%s\n' % message)
906 906
907 907 if len(parents) == 1:
908 908 parents.append(repo[nullid])
909 909 if opts.get('exact'):
910 910 if not nodeid or not p1:
911 911 raise error.Abort(_('not a Mercurial patch'))
912 912 p1 = repo[p1]
913 913 p2 = repo[p2 or nullid]
914 914 elif p2:
915 915 try:
916 916 p1 = repo[p1]
917 917 p2 = repo[p2]
918 918 # Without any options, consider p2 only if the
919 919 # patch is being applied on top of the recorded
920 920 # first parent.
921 921 if p1 != parents[0]:
922 922 p1 = parents[0]
923 923 p2 = repo[nullid]
924 924 except error.RepoError:
925 925 p1, p2 = parents
926 926 if p2.node() == nullid:
927 927 ui.warn(_("warning: import the patch as a normal revision\n"
928 928 "(use --exact to import the patch as a merge)\n"))
929 929 else:
930 930 p1, p2 = parents
931 931
932 932 n = None
933 933 if update:
934 934 if p1 != parents[0]:
935 935 updatefunc(repo, p1.node())
936 936 if p2 != parents[1]:
937 937 repo.setparents(p1.node(), p2.node())
938 938
939 939 if opts.get('exact') or opts.get('import_branch'):
940 940 repo.dirstate.setbranch(branch or 'default')
941 941
942 942 partial = opts.get('partial', False)
943 943 files = set()
944 944 try:
945 945 patch.patch(ui, repo, tmpname, strip=strip, prefix=prefix,
946 946 files=files, eolmode=None, similarity=sim / 100.0)
947 947 except patch.PatchError as e:
948 948 if not partial:
949 949 raise error.Abort(str(e))
950 950 if partial:
951 951 rejects = True
952 952
953 953 files = list(files)
954 954 if opts.get('no_commit'):
955 955 if message:
956 956 msgs.append(message)
957 957 else:
958 958 if opts.get('exact') or p2:
959 959 # If you got here, you either use --force and know what
960 960 # you are doing or used --exact or a merge patch while
961 961 # being updated to its first parent.
962 962 m = None
963 963 else:
964 964 m = scmutil.matchfiles(repo, files or [])
965 965 editform = mergeeditform(repo[None], 'import.normal')
966 966 if opts.get('exact'):
967 967 editor = None
968 968 else:
969 969 editor = getcommiteditor(editform=editform, **opts)
970 970 allowemptyback = repo.ui.backupconfig('ui', 'allowemptycommit')
971 971 extra = {}
972 972 for idfunc in extrapreimport:
973 973 extrapreimportmap[idfunc](repo, extractdata, extra, opts)
974 974 try:
975 975 if partial:
976 976 repo.ui.setconfig('ui', 'allowemptycommit', True)
977 977 n = repo.commit(message, opts.get('user') or user,
978 978 opts.get('date') or date, match=m,
979 979 editor=editor, extra=extra)
980 980 for idfunc in extrapostimport:
981 981 extrapostimportmap[idfunc](repo[n])
982 982 finally:
983 983 repo.ui.restoreconfig(allowemptyback)
984 984 else:
985 985 if opts.get('exact') or opts.get('import_branch'):
986 986 branch = branch or 'default'
987 987 else:
988 988 branch = p1.branch()
989 989 store = patch.filestore()
990 990 try:
991 991 files = set()
992 992 try:
993 993 patch.patchrepo(ui, repo, p1, store, tmpname, strip, prefix,
994 994 files, eolmode=None)
995 995 except patch.PatchError as e:
996 996 raise error.Abort(str(e))
997 997 if opts.get('exact'):
998 998 editor = None
999 999 else:
1000 1000 editor = getcommiteditor(editform='import.bypass')
1001 1001 memctx = context.makememctx(repo, (p1.node(), p2.node()),
1002 1002 message,
1003 1003 opts.get('user') or user,
1004 1004 opts.get('date') or date,
1005 1005 branch, files, store,
1006 1006 editor=editor)
1007 1007 n = memctx.commit()
1008 1008 finally:
1009 1009 store.close()
1010 1010 if opts.get('exact') and opts.get('no_commit'):
1011 1011 # --exact with --no-commit is still useful in that it does merge
1012 1012 # and branch bits
1013 1013 ui.warn(_("warning: can't check exact import with --no-commit\n"))
1014 1014 elif opts.get('exact') and hex(n) != nodeid:
1015 1015 raise error.Abort(_('patch is damaged or loses information'))
1016 1016 if n:
1017 1017 # i18n: refers to a short changeset id
1018 1018 msg = _('created %s') % short(n)
1019 1019 return (msg, n, rejects)
1020 1020 finally:
1021 1021 os.unlink(tmpname)
1022 1022
1023 1023 # facility to let extensions include additional data in an exported patch
1024 1024 # list of identifiers to be executed in order
1025 1025 extraexport = []
1026 1026 # mapping from identifier to actual export function
1027 1027 # function as to return a string to be added to the header or None
1028 1028 # it is given two arguments (sequencenumber, changectx)
1029 1029 extraexportmap = {}
1030 1030
1031 1031 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,
1032 1032 opts=None, match=None):
1033 1033 '''export changesets as hg patches.'''
1034 1034
1035 1035 total = len(revs)
1036 1036 revwidth = max([len(str(rev)) for rev in revs])
1037 1037 filemode = {}
1038 1038
1039 1039 def single(rev, seqno, fp):
1040 1040 ctx = repo[rev]
1041 1041 node = ctx.node()
1042 1042 parents = [p.node() for p in ctx.parents() if p]
1043 1043 branch = ctx.branch()
1044 1044 if switch_parent:
1045 1045 parents.reverse()
1046 1046
1047 1047 if parents:
1048 1048 prev = parents[0]
1049 1049 else:
1050 1050 prev = nullid
1051 1051
1052 1052 shouldclose = False
1053 1053 if not fp and len(template) > 0:
1054 1054 desc_lines = ctx.description().rstrip().split('\n')
1055 1055 desc = desc_lines[0] #Commit always has a first line.
1056 1056 fp = makefileobj(repo, template, node, desc=desc, total=total,
1057 1057 seqno=seqno, revwidth=revwidth, mode='wb',
1058 1058 modemap=filemode)
1059 1059 if fp != template:
1060 1060 shouldclose = True
1061 1061 if fp and fp != sys.stdout and util.safehasattr(fp, 'name'):
1062 1062 repo.ui.note("%s\n" % fp.name)
1063 1063
1064 1064 if not fp:
1065 1065 write = repo.ui.write
1066 1066 else:
1067 1067 def write(s, **kw):
1068 1068 fp.write(s)
1069 1069
1070 1070 write("# HG changeset patch\n")
1071 1071 write("# User %s\n" % ctx.user())
1072 1072 write("# Date %d %d\n" % ctx.date())
1073 1073 write("# %s\n" % util.datestr(ctx.date()))
1074 1074 if branch and branch != 'default':
1075 1075 write("# Branch %s\n" % branch)
1076 1076 write("# Node ID %s\n" % hex(node))
1077 1077 write("# Parent %s\n" % hex(prev))
1078 1078 if len(parents) > 1:
1079 1079 write("# Parent %s\n" % hex(parents[1]))
1080 1080
1081 1081 for headerid in extraexport:
1082 1082 header = extraexportmap[headerid](seqno, ctx)
1083 1083 if header is not None:
1084 1084 write('# %s\n' % header)
1085 1085 write(ctx.description().rstrip())
1086 1086 write("\n\n")
1087 1087
1088 1088 for chunk, label in patch.diffui(repo, prev, node, match, opts=opts):
1089 1089 write(chunk, label=label)
1090 1090
1091 1091 if shouldclose:
1092 1092 fp.close()
1093 1093
1094 1094 for seqno, rev in enumerate(revs):
1095 1095 single(rev, seqno + 1, fp)
1096 1096
1097 1097 def diffordiffstat(ui, repo, diffopts, node1, node2, match,
1098 1098 changes=None, stat=False, fp=None, prefix='',
1099 1099 root='', listsubrepos=False):
1100 1100 '''show diff or diffstat.'''
1101 1101 if fp is None:
1102 1102 write = ui.write
1103 1103 else:
1104 1104 def write(s, **kw):
1105 1105 fp.write(s)
1106 1106
1107 1107 if root:
1108 1108 relroot = pathutil.canonpath(repo.root, repo.getcwd(), root)
1109 1109 else:
1110 1110 relroot = ''
1111 1111 if relroot != '':
1112 1112 # XXX relative roots currently don't work if the root is within a
1113 1113 # subrepo
1114 1114 uirelroot = match.uipath(relroot)
1115 1115 relroot += '/'
1116 1116 for matchroot in match.files():
1117 1117 if not matchroot.startswith(relroot):
1118 1118 ui.warn(_('warning: %s not inside relative root %s\n') % (
1119 1119 match.uipath(matchroot), uirelroot))
1120 1120
1121 1121 if stat:
1122 1122 diffopts = diffopts.copy(context=0)
1123 1123 width = 80
1124 1124 if not ui.plain():
1125 1125 width = ui.termwidth()
1126 1126 chunks = patch.diff(repo, node1, node2, match, changes, diffopts,
1127 1127 prefix=prefix, relroot=relroot)
1128 1128 for chunk, label in patch.diffstatui(util.iterlines(chunks),
1129 1129 width=width,
1130 1130 git=diffopts.git):
1131 1131 write(chunk, label=label)
1132 1132 else:
1133 1133 for chunk, label in patch.diffui(repo, node1, node2, match,
1134 1134 changes, diffopts, prefix=prefix,
1135 1135 relroot=relroot):
1136 1136 write(chunk, label=label)
1137 1137
1138 1138 if listsubrepos:
1139 1139 ctx1 = repo[node1]
1140 1140 ctx2 = repo[node2]
1141 1141 for subpath, sub in scmutil.itersubrepos(ctx1, ctx2):
1142 1142 tempnode2 = node2
1143 1143 try:
1144 1144 if node2 is not None:
1145 1145 tempnode2 = ctx2.substate[subpath][1]
1146 1146 except KeyError:
1147 1147 # A subrepo that existed in node1 was deleted between node1 and
1148 1148 # node2 (inclusive). Thus, ctx2's substate won't contain that
1149 1149 # subpath. The best we can do is to ignore it.
1150 1150 tempnode2 = None
1151 1151 submatch = matchmod.narrowmatcher(subpath, match)
1152 1152 sub.diff(ui, diffopts, tempnode2, submatch, changes=changes,
1153 1153 stat=stat, fp=fp, prefix=prefix)
1154 1154
1155 1155 class changeset_printer(object):
1156 1156 '''show changeset information when templating not requested.'''
1157 1157
1158 1158 def __init__(self, ui, repo, matchfn, diffopts, buffered):
1159 1159 self.ui = ui
1160 1160 self.repo = repo
1161 1161 self.buffered = buffered
1162 1162 self.matchfn = matchfn
1163 1163 self.diffopts = diffopts
1164 1164 self.header = {}
1165 1165 self.hunk = {}
1166 1166 self.lastheader = None
1167 1167 self.footer = None
1168 1168
1169 1169 def flush(self, ctx):
1170 1170 rev = ctx.rev()
1171 1171 if rev in self.header:
1172 1172 h = self.header[rev]
1173 1173 if h != self.lastheader:
1174 1174 self.lastheader = h
1175 1175 self.ui.write(h)
1176 1176 del self.header[rev]
1177 1177 if rev in self.hunk:
1178 1178 self.ui.write(self.hunk[rev])
1179 1179 del self.hunk[rev]
1180 1180 return 1
1181 1181 return 0
1182 1182
1183 1183 def close(self):
1184 1184 if self.footer:
1185 1185 self.ui.write(self.footer)
1186 1186
1187 1187 def show(self, ctx, copies=None, matchfn=None, **props):
1188 1188 if self.buffered:
1189 1189 self.ui.pushbuffer()
1190 1190 self._show(ctx, copies, matchfn, props)
1191 1191 self.hunk[ctx.rev()] = self.ui.popbuffer(labeled=True)
1192 1192 else:
1193 1193 self._show(ctx, copies, matchfn, props)
1194 1194
1195 1195 def _show(self, ctx, copies, matchfn, props):
1196 1196 '''show a single changeset or file revision'''
1197 1197 changenode = ctx.node()
1198 1198 rev = ctx.rev()
1199 1199 if self.ui.debugflag:
1200 1200 hexfunc = hex
1201 1201 else:
1202 1202 hexfunc = short
1203 1203 # as of now, wctx.node() and wctx.rev() return None, but we want to
1204 1204 # show the same values as {node} and {rev} templatekw
1205 1205 revnode = (scmutil.intrev(rev), hexfunc(bin(ctx.hex())))
1206 1206
1207 1207 if self.ui.quiet:
1208 1208 self.ui.write("%d:%s\n" % revnode, label='log.node')
1209 1209 return
1210 1210
1211 1211 date = util.datestr(ctx.date())
1212 1212
1213 1213 # i18n: column positioning for "hg log"
1214 1214 self.ui.write(_("changeset: %d:%s\n") % revnode,
1215 1215 label='log.changeset changeset.%s' % ctx.phasestr())
1216 1216
1217 1217 # branches are shown first before any other names due to backwards
1218 1218 # compatibility
1219 1219 branch = ctx.branch()
1220 1220 # don't show the default branch name
1221 1221 if branch != 'default':
1222 1222 # i18n: column positioning for "hg log"
1223 1223 self.ui.write(_("branch: %s\n") % branch,
1224 1224 label='log.branch')
1225 1225
1226 1226 for name, ns in self.repo.names.iteritems():
1227 1227 # branches has special logic already handled above, so here we just
1228 1228 # skip it
1229 1229 if name == 'branches':
1230 1230 continue
1231 1231 # we will use the templatename as the color name since those two
1232 1232 # should be the same
1233 1233 for name in ns.names(self.repo, changenode):
1234 1234 self.ui.write(ns.logfmt % name,
1235 1235 label='log.%s' % ns.colorname)
1236 1236 if self.ui.debugflag:
1237 1237 # i18n: column positioning for "hg log"
1238 1238 self.ui.write(_("phase: %s\n") % ctx.phasestr(),
1239 1239 label='log.phase')
1240 1240 for pctx in scmutil.meaningfulparents(self.repo, ctx):
1241 1241 label = 'log.parent changeset.%s' % pctx.phasestr()
1242 1242 # i18n: column positioning for "hg log"
1243 1243 self.ui.write(_("parent: %d:%s\n")
1244 1244 % (pctx.rev(), hexfunc(pctx.node())),
1245 1245 label=label)
1246 1246
1247 1247 if self.ui.debugflag and rev is not None:
1248 1248 mnode = ctx.manifestnode()
1249 1249 # i18n: column positioning for "hg log"
1250 1250 self.ui.write(_("manifest: %d:%s\n") %
1251 1251 (self.repo.manifest.rev(mnode), hex(mnode)),
1252 1252 label='ui.debug log.manifest')
1253 1253 # i18n: column positioning for "hg log"
1254 1254 self.ui.write(_("user: %s\n") % ctx.user(),
1255 1255 label='log.user')
1256 1256 # i18n: column positioning for "hg log"
1257 1257 self.ui.write(_("date: %s\n") % date,
1258 1258 label='log.date')
1259 1259
1260 1260 if self.ui.debugflag:
1261 1261 files = ctx.p1().status(ctx)[:3]
1262 1262 for key, value in zip([# i18n: column positioning for "hg log"
1263 1263 _("files:"),
1264 1264 # i18n: column positioning for "hg log"
1265 1265 _("files+:"),
1266 1266 # i18n: column positioning for "hg log"
1267 1267 _("files-:")], files):
1268 1268 if value:
1269 1269 self.ui.write("%-12s %s\n" % (key, " ".join(value)),
1270 1270 label='ui.debug log.files')
1271 1271 elif ctx.files() and self.ui.verbose:
1272 1272 # i18n: column positioning for "hg log"
1273 1273 self.ui.write(_("files: %s\n") % " ".join(ctx.files()),
1274 1274 label='ui.note log.files')
1275 1275 if copies and self.ui.verbose:
1276 1276 copies = ['%s (%s)' % c for c in copies]
1277 1277 # i18n: column positioning for "hg log"
1278 1278 self.ui.write(_("copies: %s\n") % ' '.join(copies),
1279 1279 label='ui.note log.copies')
1280 1280
1281 1281 extra = ctx.extra()
1282 1282 if extra and self.ui.debugflag:
1283 1283 for key, value in sorted(extra.items()):
1284 1284 # i18n: column positioning for "hg log"
1285 1285 self.ui.write(_("extra: %s=%s\n")
1286 1286 % (key, value.encode('string_escape')),
1287 1287 label='ui.debug log.extra')
1288 1288
1289 1289 description = ctx.description().strip()
1290 1290 if description:
1291 1291 if self.ui.verbose:
1292 1292 self.ui.write(_("description:\n"),
1293 1293 label='ui.note log.description')
1294 1294 self.ui.write(description,
1295 1295 label='ui.note log.description')
1296 1296 self.ui.write("\n\n")
1297 1297 else:
1298 1298 # i18n: column positioning for "hg log"
1299 1299 self.ui.write(_("summary: %s\n") %
1300 1300 description.splitlines()[0],
1301 1301 label='log.summary')
1302 1302 self.ui.write("\n")
1303 1303
1304 1304 self.showpatch(changenode, matchfn)
1305 1305
1306 1306 def showpatch(self, node, matchfn):
1307 1307 if not matchfn:
1308 1308 matchfn = self.matchfn
1309 1309 if matchfn:
1310 1310 stat = self.diffopts.get('stat')
1311 1311 diff = self.diffopts.get('patch')
1312 1312 diffopts = patch.diffallopts(self.ui, self.diffopts)
1313 1313 prev = self.repo.changelog.parents(node)[0]
1314 1314 if stat:
1315 1315 diffordiffstat(self.ui, self.repo, diffopts, prev, node,
1316 1316 match=matchfn, stat=True)
1317 1317 if diff:
1318 1318 if stat:
1319 1319 self.ui.write("\n")
1320 1320 diffordiffstat(self.ui, self.repo, diffopts, prev, node,
1321 1321 match=matchfn, stat=False)
1322 1322 self.ui.write("\n")
1323 1323
1324 1324 class jsonchangeset(changeset_printer):
1325 1325 '''format changeset information.'''
1326 1326
1327 1327 def __init__(self, ui, repo, matchfn, diffopts, buffered):
1328 1328 changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered)
1329 1329 self.cache = {}
1330 1330 self._first = True
1331 1331
1332 1332 def close(self):
1333 1333 if not self._first:
1334 1334 self.ui.write("\n]\n")
1335 1335 else:
1336 1336 self.ui.write("[]\n")
1337 1337
1338 1338 def _show(self, ctx, copies, matchfn, props):
1339 1339 '''show a single changeset or file revision'''
1340 1340 rev = ctx.rev()
1341 1341 if rev is None:
1342 1342 jrev = jnode = 'null'
1343 1343 else:
1344 1344 jrev = str(rev)
1345 1345 jnode = '"%s"' % hex(ctx.node())
1346 1346 j = encoding.jsonescape
1347 1347
1348 1348 if self._first:
1349 1349 self.ui.write("[\n {")
1350 1350 self._first = False
1351 1351 else:
1352 1352 self.ui.write(",\n {")
1353 1353
1354 1354 if self.ui.quiet:
1355 1355 self.ui.write('\n "rev": %s' % jrev)
1356 1356 self.ui.write(',\n "node": %s' % jnode)
1357 1357 self.ui.write('\n }')
1358 1358 return
1359 1359
1360 1360 self.ui.write('\n "rev": %s' % jrev)
1361 1361 self.ui.write(',\n "node": %s' % jnode)
1362 1362 self.ui.write(',\n "branch": "%s"' % j(ctx.branch()))
1363 1363 self.ui.write(',\n "phase": "%s"' % ctx.phasestr())
1364 1364 self.ui.write(',\n "user": "%s"' % j(ctx.user()))
1365 1365 self.ui.write(',\n "date": [%d, %d]' % ctx.date())
1366 1366 self.ui.write(',\n "desc": "%s"' % j(ctx.description()))
1367 1367
1368 1368 self.ui.write(',\n "bookmarks": [%s]' %
1369 1369 ", ".join('"%s"' % j(b) for b in ctx.bookmarks()))
1370 1370 self.ui.write(',\n "tags": [%s]' %
1371 1371 ", ".join('"%s"' % j(t) for t in ctx.tags()))
1372 1372 self.ui.write(',\n "parents": [%s]' %
1373 1373 ", ".join('"%s"' % c.hex() for c in ctx.parents()))
1374 1374
1375 1375 if self.ui.debugflag:
1376 1376 if rev is None:
1377 1377 jmanifestnode = 'null'
1378 1378 else:
1379 1379 jmanifestnode = '"%s"' % hex(ctx.manifestnode())
1380 1380 self.ui.write(',\n "manifest": %s' % jmanifestnode)
1381 1381
1382 1382 self.ui.write(',\n "extra": {%s}' %
1383 1383 ", ".join('"%s": "%s"' % (j(k), j(v))
1384 1384 for k, v in ctx.extra().items()))
1385 1385
1386 1386 files = ctx.p1().status(ctx)
1387 1387 self.ui.write(',\n "modified": [%s]' %
1388 1388 ", ".join('"%s"' % j(f) for f in files[0]))
1389 1389 self.ui.write(',\n "added": [%s]' %
1390 1390 ", ".join('"%s"' % j(f) for f in files[1]))
1391 1391 self.ui.write(',\n "removed": [%s]' %
1392 1392 ", ".join('"%s"' % j(f) for f in files[2]))
1393 1393
1394 1394 elif self.ui.verbose:
1395 1395 self.ui.write(',\n "files": [%s]' %
1396 1396 ", ".join('"%s"' % j(f) for f in ctx.files()))
1397 1397
1398 1398 if copies:
1399 1399 self.ui.write(',\n "copies": {%s}' %
1400 1400 ", ".join('"%s": "%s"' % (j(k), j(v))
1401 1401 for k, v in copies))
1402 1402
1403 1403 matchfn = self.matchfn
1404 1404 if matchfn:
1405 1405 stat = self.diffopts.get('stat')
1406 1406 diff = self.diffopts.get('patch')
1407 1407 diffopts = patch.difffeatureopts(self.ui, self.diffopts, git=True)
1408 1408 node, prev = ctx.node(), ctx.p1().node()
1409 1409 if stat:
1410 1410 self.ui.pushbuffer()
1411 1411 diffordiffstat(self.ui, self.repo, diffopts, prev, node,
1412 1412 match=matchfn, stat=True)
1413 1413 self.ui.write(',\n "diffstat": "%s"' % j(self.ui.popbuffer()))
1414 1414 if diff:
1415 1415 self.ui.pushbuffer()
1416 1416 diffordiffstat(self.ui, self.repo, diffopts, prev, node,
1417 1417 match=matchfn, stat=False)
1418 1418 self.ui.write(',\n "diff": "%s"' % j(self.ui.popbuffer()))
1419 1419
1420 1420 self.ui.write("\n }")
1421 1421
1422 1422 class changeset_templater(changeset_printer):
1423 1423 '''format changeset information.'''
1424 1424
1425 1425 def __init__(self, ui, repo, matchfn, diffopts, tmpl, mapfile, buffered):
1426 1426 changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered)
1427 1427 formatnode = ui.debugflag and (lambda x: x) or (lambda x: x[:12])
1428 1428 defaulttempl = {
1429 1429 'parent': '{rev}:{node|formatnode} ',
1430 1430 'manifest': '{rev}:{node|formatnode}',
1431 1431 'file_copy': '{name} ({source})',
1432 1432 'extra': '{key}={value|stringescape}'
1433 1433 }
1434 1434 # filecopy is preserved for compatibility reasons
1435 1435 defaulttempl['filecopy'] = defaulttempl['file_copy']
1436 1436 self.t = templater.templater(mapfile, {'formatnode': formatnode},
1437 1437 cache=defaulttempl)
1438 1438 if tmpl:
1439 1439 self.t.cache['changeset'] = tmpl
1440 1440
1441 1441 self.cache = {}
1442 1442
1443 1443 # find correct templates for current mode
1444 1444 tmplmodes = [
1445 1445 (True, None),
1446 1446 (self.ui.verbose, 'verbose'),
1447 1447 (self.ui.quiet, 'quiet'),
1448 1448 (self.ui.debugflag, 'debug'),
1449 1449 ]
1450 1450
1451 1451 self._parts = {'header': '', 'footer': '', 'changeset': 'changeset',
1452 1452 'docheader': '', 'docfooter': ''}
1453 1453 for mode, postfix in tmplmodes:
1454 1454 for t in self._parts:
1455 1455 cur = t
1456 1456 if postfix:
1457 1457 cur += "_" + postfix
1458 1458 if mode and cur in self.t:
1459 1459 self._parts[t] = cur
1460 1460
1461 1461 if self._parts['docheader']:
1462 1462 self.ui.write(templater.stringify(self.t(self._parts['docheader'])))
1463 1463
1464 1464 def close(self):
1465 1465 if self._parts['docfooter']:
1466 1466 if not self.footer:
1467 1467 self.footer = ""
1468 1468 self.footer += templater.stringify(self.t(self._parts['docfooter']))
1469 1469 return super(changeset_templater, self).close()
1470 1470
1471 1471 def _show(self, ctx, copies, matchfn, props):
1472 1472 '''show a single changeset or file revision'''
1473 1473 props = props.copy()
1474 1474 props.update(templatekw.keywords)
1475 1475 props['templ'] = self.t
1476 1476 props['ctx'] = ctx
1477 1477 props['repo'] = self.repo
1478 1478 props['revcache'] = {'copies': copies}
1479 1479 props['cache'] = self.cache
1480 1480
1481 1481 try:
1482 1482 # write header
1483 1483 if self._parts['header']:
1484 1484 h = templater.stringify(self.t(self._parts['header'], **props))
1485 1485 if self.buffered:
1486 1486 self.header[ctx.rev()] = h
1487 1487 else:
1488 1488 if self.lastheader != h:
1489 1489 self.lastheader = h
1490 1490 self.ui.write(h)
1491 1491
1492 1492 # write changeset metadata, then patch if requested
1493 1493 key = self._parts['changeset']
1494 1494 self.ui.write(templater.stringify(self.t(key, **props)))
1495 1495 self.showpatch(ctx.node(), matchfn)
1496 1496
1497 1497 if self._parts['footer']:
1498 1498 if not self.footer:
1499 1499 self.footer = templater.stringify(
1500 1500 self.t(self._parts['footer'], **props))
1501 1501 except KeyError as inst:
1502 1502 msg = _("%s: no key named '%s'")
1503 1503 raise error.Abort(msg % (self.t.mapfile, inst.args[0]))
1504 1504 except SyntaxError as inst:
1505 1505 raise error.Abort('%s: %s' % (self.t.mapfile, inst.args[0]))
1506 1506
1507 1507 def gettemplate(ui, tmpl, style):
1508 1508 """
1509 1509 Find the template matching the given template spec or style.
1510 1510 """
1511 1511
1512 1512 # ui settings
1513 1513 if not tmpl and not style: # template are stronger than style
1514 1514 tmpl = ui.config('ui', 'logtemplate')
1515 1515 if tmpl:
1516 1516 try:
1517 1517 tmpl = templater.unquotestring(tmpl)
1518 1518 except SyntaxError:
1519 1519 pass
1520 1520 return tmpl, None
1521 1521 else:
1522 1522 style = util.expandpath(ui.config('ui', 'style', ''))
1523 1523
1524 1524 if not tmpl and style:
1525 1525 mapfile = style
1526 1526 if not os.path.split(mapfile)[0]:
1527 1527 mapname = (templater.templatepath('map-cmdline.' + mapfile)
1528 1528 or templater.templatepath(mapfile))
1529 1529 if mapname:
1530 1530 mapfile = mapname
1531 1531 return None, mapfile
1532 1532
1533 1533 if not tmpl:
1534 1534 return None, None
1535 1535
1536 1536 return formatter.lookuptemplate(ui, 'changeset', tmpl)
1537 1537
1538 1538 def show_changeset(ui, repo, opts, buffered=False):
1539 1539 """show one changeset using template or regular display.
1540 1540
1541 1541 Display format will be the first non-empty hit of:
1542 1542 1. option 'template'
1543 1543 2. option 'style'
1544 1544 3. [ui] setting 'logtemplate'
1545 1545 4. [ui] setting 'style'
1546 1546 If all of these values are either the unset or the empty string,
1547 1547 regular display via changeset_printer() is done.
1548 1548 """
1549 1549 # options
1550 1550 matchfn = None
1551 1551 if opts.get('patch') or opts.get('stat'):
1552 1552 matchfn = scmutil.matchall(repo)
1553 1553
1554 1554 if opts.get('template') == 'json':
1555 1555 return jsonchangeset(ui, repo, matchfn, opts, buffered)
1556 1556
1557 1557 tmpl, mapfile = gettemplate(ui, opts.get('template'), opts.get('style'))
1558 1558
1559 1559 if not tmpl and not mapfile:
1560 1560 return changeset_printer(ui, repo, matchfn, opts, buffered)
1561 1561
1562 1562 try:
1563 1563 t = changeset_templater(ui, repo, matchfn, opts, tmpl, mapfile,
1564 1564 buffered)
1565 1565 except SyntaxError as inst:
1566 1566 raise error.Abort(inst.args[0])
1567 1567 return t
1568 1568
1569 1569 def showmarker(ui, marker):
1570 1570 """utility function to display obsolescence marker in a readable way
1571 1571
1572 1572 To be used by debug function."""
1573 1573 ui.write(hex(marker.precnode()))
1574 1574 for repl in marker.succnodes():
1575 1575 ui.write(' ')
1576 1576 ui.write(hex(repl))
1577 1577 ui.write(' %X ' % marker.flags())
1578 1578 parents = marker.parentnodes()
1579 1579 if parents is not None:
1580 1580 ui.write('{%s} ' % ', '.join(hex(p) for p in parents))
1581 1581 ui.write('(%s) ' % util.datestr(marker.date()))
1582 1582 ui.write('{%s}' % (', '.join('%r: %r' % t for t in
1583 1583 sorted(marker.metadata().items())
1584 1584 if t[0] != 'date')))
1585 1585 ui.write('\n')
1586 1586
1587 1587 def finddate(ui, repo, date):
1588 1588 """Find the tipmost changeset that matches the given date spec"""
1589 1589
1590 1590 df = util.matchdate(date)
1591 1591 m = scmutil.matchall(repo)
1592 1592 results = {}
1593 1593
1594 1594 def prep(ctx, fns):
1595 1595 d = ctx.date()
1596 1596 if df(d[0]):
1597 1597 results[ctx.rev()] = d
1598 1598
1599 1599 for ctx in walkchangerevs(repo, m, {'rev': None}, prep):
1600 1600 rev = ctx.rev()
1601 1601 if rev in results:
1602 1602 ui.status(_("found revision %s from %s\n") %
1603 1603 (rev, util.datestr(results[rev])))
1604 1604 return str(rev)
1605 1605
1606 1606 raise error.Abort(_("revision matching date not found"))
1607 1607
1608 1608 def increasingwindows(windowsize=8, sizelimit=512):
1609 1609 while True:
1610 1610 yield windowsize
1611 1611 if windowsize < sizelimit:
1612 1612 windowsize *= 2
1613 1613
1614 1614 class FileWalkError(Exception):
1615 1615 pass
1616 1616
1617 1617 def walkfilerevs(repo, match, follow, revs, fncache):
1618 1618 '''Walks the file history for the matched files.
1619 1619
1620 1620 Returns the changeset revs that are involved in the file history.
1621 1621
1622 1622 Throws FileWalkError if the file history can't be walked using
1623 1623 filelogs alone.
1624 1624 '''
1625 1625 wanted = set()
1626 1626 copies = []
1627 1627 minrev, maxrev = min(revs), max(revs)
1628 1628 def filerevgen(filelog, last):
1629 1629 """
1630 1630 Only files, no patterns. Check the history of each file.
1631 1631
1632 1632 Examines filelog entries within minrev, maxrev linkrev range
1633 1633 Returns an iterator yielding (linkrev, parentlinkrevs, copied)
1634 1634 tuples in backwards order
1635 1635 """
1636 1636 cl_count = len(repo)
1637 1637 revs = []
1638 1638 for j in xrange(0, last + 1):
1639 1639 linkrev = filelog.linkrev(j)
1640 1640 if linkrev < minrev:
1641 1641 continue
1642 1642 # only yield rev for which we have the changelog, it can
1643 1643 # happen while doing "hg log" during a pull or commit
1644 1644 if linkrev >= cl_count:
1645 1645 break
1646 1646
1647 1647 parentlinkrevs = []
1648 1648 for p in filelog.parentrevs(j):
1649 1649 if p != nullrev:
1650 1650 parentlinkrevs.append(filelog.linkrev(p))
1651 1651 n = filelog.node(j)
1652 1652 revs.append((linkrev, parentlinkrevs,
1653 1653 follow and filelog.renamed(n)))
1654 1654
1655 1655 return reversed(revs)
1656 1656 def iterfiles():
1657 1657 pctx = repo['.']
1658 1658 for filename in match.files():
1659 1659 if follow:
1660 1660 if filename not in pctx:
1661 1661 raise error.Abort(_('cannot follow file not in parent '
1662 1662 'revision: "%s"') % filename)
1663 1663 yield filename, pctx[filename].filenode()
1664 1664 else:
1665 1665 yield filename, None
1666 1666 for filename_node in copies:
1667 1667 yield filename_node
1668 1668
1669 1669 for file_, node in iterfiles():
1670 1670 filelog = repo.file(file_)
1671 1671 if not len(filelog):
1672 1672 if node is None:
1673 1673 # A zero count may be a directory or deleted file, so
1674 1674 # try to find matching entries on the slow path.
1675 1675 if follow:
1676 1676 raise error.Abort(
1677 1677 _('cannot follow nonexistent file: "%s"') % file_)
1678 1678 raise FileWalkError("Cannot walk via filelog")
1679 1679 else:
1680 1680 continue
1681 1681
1682 1682 if node is None:
1683 1683 last = len(filelog) - 1
1684 1684 else:
1685 1685 last = filelog.rev(node)
1686 1686
1687 1687 # keep track of all ancestors of the file
1688 1688 ancestors = set([filelog.linkrev(last)])
1689 1689
1690 1690 # iterate from latest to oldest revision
1691 1691 for rev, flparentlinkrevs, copied in filerevgen(filelog, last):
1692 1692 if not follow:
1693 1693 if rev > maxrev:
1694 1694 continue
1695 1695 else:
1696 1696 # Note that last might not be the first interesting
1697 1697 # rev to us:
1698 1698 # if the file has been changed after maxrev, we'll
1699 1699 # have linkrev(last) > maxrev, and we still need
1700 1700 # to explore the file graph
1701 1701 if rev not in ancestors:
1702 1702 continue
1703 1703 # XXX insert 1327 fix here
1704 1704 if flparentlinkrevs:
1705 1705 ancestors.update(flparentlinkrevs)
1706 1706
1707 1707 fncache.setdefault(rev, []).append(file_)
1708 1708 wanted.add(rev)
1709 1709 if copied:
1710 1710 copies.append(copied)
1711 1711
1712 1712 return wanted
1713 1713
1714 1714 class _followfilter(object):
1715 1715 def __init__(self, repo, onlyfirst=False):
1716 1716 self.repo = repo
1717 1717 self.startrev = nullrev
1718 1718 self.roots = set()
1719 1719 self.onlyfirst = onlyfirst
1720 1720
1721 1721 def match(self, rev):
1722 1722 def realparents(rev):
1723 1723 if self.onlyfirst:
1724 1724 return self.repo.changelog.parentrevs(rev)[0:1]
1725 1725 else:
1726 1726 return filter(lambda x: x != nullrev,
1727 1727 self.repo.changelog.parentrevs(rev))
1728 1728
1729 1729 if self.startrev == nullrev:
1730 1730 self.startrev = rev
1731 1731 return True
1732 1732
1733 1733 if rev > self.startrev:
1734 1734 # forward: all descendants
1735 1735 if not self.roots:
1736 1736 self.roots.add(self.startrev)
1737 1737 for parent in realparents(rev):
1738 1738 if parent in self.roots:
1739 1739 self.roots.add(rev)
1740 1740 return True
1741 1741 else:
1742 1742 # backwards: all parents
1743 1743 if not self.roots:
1744 1744 self.roots.update(realparents(self.startrev))
1745 1745 if rev in self.roots:
1746 1746 self.roots.remove(rev)
1747 1747 self.roots.update(realparents(rev))
1748 1748 return True
1749 1749
1750 1750 return False
1751 1751
1752 1752 def walkchangerevs(repo, match, opts, prepare):
1753 1753 '''Iterate over files and the revs in which they changed.
1754 1754
1755 1755 Callers most commonly need to iterate backwards over the history
1756 1756 in which they are interested. Doing so has awful (quadratic-looking)
1757 1757 performance, so we use iterators in a "windowed" way.
1758 1758
1759 1759 We walk a window of revisions in the desired order. Within the
1760 1760 window, we first walk forwards to gather data, then in the desired
1761 1761 order (usually backwards) to display it.
1762 1762
1763 1763 This function returns an iterator yielding contexts. Before
1764 1764 yielding each context, the iterator will first call the prepare
1765 1765 function on each context in the window in forward order.'''
1766 1766
1767 1767 follow = opts.get('follow') or opts.get('follow_first')
1768 1768 revs = _logrevs(repo, opts)
1769 1769 if not revs:
1770 1770 return []
1771 1771 wanted = set()
1772 1772 slowpath = match.anypats() or ((match.isexact() or match.prefix()) and
1773 1773 opts.get('removed'))
1774 1774 fncache = {}
1775 1775 change = repo.changectx
1776 1776
1777 1777 # First step is to fill wanted, the set of revisions that we want to yield.
1778 1778 # When it does not induce extra cost, we also fill fncache for revisions in
1779 1779 # wanted: a cache of filenames that were changed (ctx.files()) and that
1780 1780 # match the file filtering conditions.
1781 1781
1782 1782 if match.always():
1783 1783 # No files, no patterns. Display all revs.
1784 1784 wanted = revs
1785 1785 elif not slowpath:
1786 1786 # We only have to read through the filelog to find wanted revisions
1787 1787
1788 1788 try:
1789 1789 wanted = walkfilerevs(repo, match, follow, revs, fncache)
1790 1790 except FileWalkError:
1791 1791 slowpath = True
1792 1792
1793 1793 # We decided to fall back to the slowpath because at least one
1794 1794 # of the paths was not a file. Check to see if at least one of them
1795 1795 # existed in history, otherwise simply return
1796 1796 for path in match.files():
1797 1797 if path == '.' or path in repo.store:
1798 1798 break
1799 1799 else:
1800 1800 return []
1801 1801
1802 1802 if slowpath:
1803 1803 # We have to read the changelog to match filenames against
1804 1804 # changed files
1805 1805
1806 1806 if follow:
1807 1807 raise error.Abort(_('can only follow copies/renames for explicit '
1808 1808 'filenames'))
1809 1809
1810 1810 # The slow path checks files modified in every changeset.
1811 1811 # This is really slow on large repos, so compute the set lazily.
1812 1812 class lazywantedset(object):
1813 1813 def __init__(self):
1814 1814 self.set = set()
1815 1815 self.revs = set(revs)
1816 1816
1817 1817 # No need to worry about locality here because it will be accessed
1818 1818 # in the same order as the increasing window below.
1819 1819 def __contains__(self, value):
1820 1820 if value in self.set:
1821 1821 return True
1822 1822 elif not value in self.revs:
1823 1823 return False
1824 1824 else:
1825 1825 self.revs.discard(value)
1826 1826 ctx = change(value)
1827 1827 matches = filter(match, ctx.files())
1828 1828 if matches:
1829 1829 fncache[value] = matches
1830 1830 self.set.add(value)
1831 1831 return True
1832 1832 return False
1833 1833
1834 1834 def discard(self, value):
1835 1835 self.revs.discard(value)
1836 1836 self.set.discard(value)
1837 1837
1838 1838 wanted = lazywantedset()
1839 1839
1840 1840 # it might be worthwhile to do this in the iterator if the rev range
1841 1841 # is descending and the prune args are all within that range
1842 1842 for rev in opts.get('prune', ()):
1843 1843 rev = repo[rev].rev()
1844 1844 ff = _followfilter(repo)
1845 1845 stop = min(revs[0], revs[-1])
1846 1846 for x in xrange(rev, stop - 1, -1):
1847 1847 if ff.match(x):
1848 1848 wanted = wanted - [x]
1849 1849
1850 1850 # Now that wanted is correctly initialized, we can iterate over the
1851 1851 # revision range, yielding only revisions in wanted.
1852 1852 def iterate():
1853 1853 if follow and match.always():
1854 1854 ff = _followfilter(repo, onlyfirst=opts.get('follow_first'))
1855 1855 def want(rev):
1856 1856 return ff.match(rev) and rev in wanted
1857 1857 else:
1858 1858 def want(rev):
1859 1859 return rev in wanted
1860 1860
1861 1861 it = iter(revs)
1862 1862 stopiteration = False
1863 1863 for windowsize in increasingwindows():
1864 1864 nrevs = []
1865 1865 for i in xrange(windowsize):
1866 1866 rev = next(it, None)
1867 1867 if rev is None:
1868 1868 stopiteration = True
1869 1869 break
1870 1870 elif want(rev):
1871 1871 nrevs.append(rev)
1872 1872 for rev in sorted(nrevs):
1873 1873 fns = fncache.get(rev)
1874 1874 ctx = change(rev)
1875 1875 if not fns:
1876 1876 def fns_generator():
1877 1877 for f in ctx.files():
1878 1878 if match(f):
1879 1879 yield f
1880 1880 fns = fns_generator()
1881 1881 prepare(ctx, fns)
1882 1882 for rev in nrevs:
1883 1883 yield change(rev)
1884 1884
1885 1885 if stopiteration:
1886 1886 break
1887 1887
1888 1888 return iterate()
1889 1889
1890 1890 def _makefollowlogfilematcher(repo, files, followfirst):
1891 1891 # When displaying a revision with --patch --follow FILE, we have
1892 1892 # to know which file of the revision must be diffed. With
1893 1893 # --follow, we want the names of the ancestors of FILE in the
1894 1894 # revision, stored in "fcache". "fcache" is populated by
1895 1895 # reproducing the graph traversal already done by --follow revset
1896 1896 # and relating linkrevs to file names (which is not "correct" but
1897 1897 # good enough).
1898 1898 fcache = {}
1899 1899 fcacheready = [False]
1900 1900 pctx = repo['.']
1901 1901
1902 1902 def populate():
1903 1903 for fn in files:
1904 1904 for i in ((pctx[fn],), pctx[fn].ancestors(followfirst=followfirst)):
1905 1905 for c in i:
1906 1906 fcache.setdefault(c.linkrev(), set()).add(c.path())
1907 1907
1908 1908 def filematcher(rev):
1909 1909 if not fcacheready[0]:
1910 1910 # Lazy initialization
1911 1911 fcacheready[0] = True
1912 1912 populate()
1913 1913 return scmutil.matchfiles(repo, fcache.get(rev, []))
1914 1914
1915 1915 return filematcher
1916 1916
1917 1917 def _makenofollowlogfilematcher(repo, pats, opts):
1918 1918 '''hook for extensions to override the filematcher for non-follow cases'''
1919 1919 return None
1920 1920
1921 1921 def _makelogrevset(repo, pats, opts, revs):
1922 1922 """Return (expr, filematcher) where expr is a revset string built
1923 1923 from log options and file patterns or None. If --stat or --patch
1924 1924 are not passed filematcher is None. Otherwise it is a callable
1925 1925 taking a revision number and returning a match objects filtering
1926 1926 the files to be detailed when displaying the revision.
1927 1927 """
1928 1928 opt2revset = {
1929 1929 'no_merges': ('not merge()', None),
1930 1930 'only_merges': ('merge()', None),
1931 1931 '_ancestors': ('ancestors(%(val)s)', None),
1932 1932 '_fancestors': ('_firstancestors(%(val)s)', None),
1933 1933 '_descendants': ('descendants(%(val)s)', None),
1934 1934 '_fdescendants': ('_firstdescendants(%(val)s)', None),
1935 1935 '_matchfiles': ('_matchfiles(%(val)s)', None),
1936 1936 'date': ('date(%(val)r)', None),
1937 1937 'branch': ('branch(%(val)r)', ' or '),
1938 1938 '_patslog': ('filelog(%(val)r)', ' or '),
1939 1939 '_patsfollow': ('follow(%(val)r)', ' or '),
1940 1940 '_patsfollowfirst': ('_followfirst(%(val)r)', ' or '),
1941 1941 'keyword': ('keyword(%(val)r)', ' or '),
1942 1942 'prune': ('not (%(val)r or ancestors(%(val)r))', ' and '),
1943 1943 'user': ('user(%(val)r)', ' or '),
1944 1944 }
1945 1945
1946 1946 opts = dict(opts)
1947 1947 # follow or not follow?
1948 1948 follow = opts.get('follow') or opts.get('follow_first')
1949 1949 if opts.get('follow_first'):
1950 1950 followfirst = 1
1951 1951 else:
1952 1952 followfirst = 0
1953 1953 # --follow with FILE behavior depends on revs...
1954 1954 it = iter(revs)
1955 1955 startrev = it.next()
1956 1956 followdescendants = startrev < next(it, startrev)
1957 1957
1958 1958 # branch and only_branch are really aliases and must be handled at
1959 1959 # the same time
1960 1960 opts['branch'] = opts.get('branch', []) + opts.get('only_branch', [])
1961 1961 opts['branch'] = [repo.lookupbranch(b) for b in opts['branch']]
1962 1962 # pats/include/exclude are passed to match.match() directly in
1963 1963 # _matchfiles() revset but walkchangerevs() builds its matcher with
1964 1964 # scmutil.match(). The difference is input pats are globbed on
1965 1965 # platforms without shell expansion (windows).
1966 1966 wctx = repo[None]
1967 1967 match, pats = scmutil.matchandpats(wctx, pats, opts)
1968 1968 slowpath = match.anypats() or ((match.isexact() or match.prefix()) and
1969 1969 opts.get('removed'))
1970 1970 if not slowpath:
1971 1971 for f in match.files():
1972 1972 if follow and f not in wctx:
1973 1973 # If the file exists, it may be a directory, so let it
1974 1974 # take the slow path.
1975 1975 if os.path.exists(repo.wjoin(f)):
1976 1976 slowpath = True
1977 1977 continue
1978 1978 else:
1979 1979 raise error.Abort(_('cannot follow file not in parent '
1980 1980 'revision: "%s"') % f)
1981 1981 filelog = repo.file(f)
1982 1982 if not filelog:
1983 1983 # A zero count may be a directory or deleted file, so
1984 1984 # try to find matching entries on the slow path.
1985 1985 if follow:
1986 1986 raise error.Abort(
1987 1987 _('cannot follow nonexistent file: "%s"') % f)
1988 1988 slowpath = True
1989 1989
1990 1990 # We decided to fall back to the slowpath because at least one
1991 1991 # of the paths was not a file. Check to see if at least one of them
1992 1992 # existed in history - in that case, we'll continue down the
1993 1993 # slowpath; otherwise, we can turn off the slowpath
1994 1994 if slowpath:
1995 1995 for path in match.files():
1996 1996 if path == '.' or path in repo.store:
1997 1997 break
1998 1998 else:
1999 1999 slowpath = False
2000 2000
2001 2001 fpats = ('_patsfollow', '_patsfollowfirst')
2002 2002 fnopats = (('_ancestors', '_fancestors'),
2003 2003 ('_descendants', '_fdescendants'))
2004 2004 if slowpath:
2005 2005 # See walkchangerevs() slow path.
2006 2006 #
2007 2007 # pats/include/exclude cannot be represented as separate
2008 2008 # revset expressions as their filtering logic applies at file
2009 2009 # level. For instance "-I a -X a" matches a revision touching
2010 2010 # "a" and "b" while "file(a) and not file(b)" does
2011 2011 # not. Besides, filesets are evaluated against the working
2012 2012 # directory.
2013 2013 matchargs = ['r:', 'd:relpath']
2014 2014 for p in pats:
2015 2015 matchargs.append('p:' + p)
2016 2016 for p in opts.get('include', []):
2017 2017 matchargs.append('i:' + p)
2018 2018 for p in opts.get('exclude', []):
2019 2019 matchargs.append('x:' + p)
2020 2020 matchargs = ','.join(('%r' % p) for p in matchargs)
2021 2021 opts['_matchfiles'] = matchargs
2022 2022 if follow:
2023 2023 opts[fnopats[0][followfirst]] = '.'
2024 2024 else:
2025 2025 if follow:
2026 2026 if pats:
2027 2027 # follow() revset interprets its file argument as a
2028 2028 # manifest entry, so use match.files(), not pats.
2029 2029 opts[fpats[followfirst]] = list(match.files())
2030 2030 else:
2031 2031 op = fnopats[followdescendants][followfirst]
2032 2032 opts[op] = 'rev(%d)' % startrev
2033 2033 else:
2034 2034 opts['_patslog'] = list(pats)
2035 2035
2036 2036 filematcher = None
2037 2037 if opts.get('patch') or opts.get('stat'):
2038 2038 # When following files, track renames via a special matcher.
2039 2039 # If we're forced to take the slowpath it means we're following
2040 2040 # at least one pattern/directory, so don't bother with rename tracking.
2041 2041 if follow and not match.always() and not slowpath:
2042 2042 # _makefollowlogfilematcher expects its files argument to be
2043 2043 # relative to the repo root, so use match.files(), not pats.
2044 2044 filematcher = _makefollowlogfilematcher(repo, match.files(),
2045 2045 followfirst)
2046 2046 else:
2047 2047 filematcher = _makenofollowlogfilematcher(repo, pats, opts)
2048 2048 if filematcher is None:
2049 2049 filematcher = lambda rev: match
2050 2050
2051 2051 expr = []
2052 2052 for op, val in sorted(opts.iteritems()):
2053 2053 if not val:
2054 2054 continue
2055 2055 if op not in opt2revset:
2056 2056 continue
2057 2057 revop, andor = opt2revset[op]
2058 2058 if '%(val)' not in revop:
2059 2059 expr.append(revop)
2060 2060 else:
2061 2061 if not isinstance(val, list):
2062 2062 e = revop % {'val': val}
2063 2063 else:
2064 2064 e = '(' + andor.join((revop % {'val': v}) for v in val) + ')'
2065 2065 expr.append(e)
2066 2066
2067 2067 if expr:
2068 2068 expr = '(' + ' and '.join(expr) + ')'
2069 2069 else:
2070 2070 expr = None
2071 2071 return expr, filematcher
2072 2072
2073 2073 def _logrevs(repo, opts):
2074 2074 # Default --rev value depends on --follow but --follow behavior
2075 2075 # depends on revisions resolved from --rev...
2076 2076 follow = opts.get('follow') or opts.get('follow_first')
2077 2077 if opts.get('rev'):
2078 2078 revs = scmutil.revrange(repo, opts['rev'])
2079 2079 elif follow and repo.dirstate.p1() == nullid:
2080 2080 revs = revset.baseset()
2081 2081 elif follow:
2082 2082 revs = repo.revs('reverse(:.)')
2083 2083 else:
2084 2084 revs = revset.spanset(repo)
2085 2085 revs.reverse()
2086 2086 return revs
2087 2087
2088 2088 def getgraphlogrevs(repo, pats, opts):
2089 2089 """Return (revs, expr, filematcher) where revs is an iterable of
2090 2090 revision numbers, expr is a revset string built from log options
2091 2091 and file patterns or None, and used to filter 'revs'. If --stat or
2092 2092 --patch are not passed filematcher is None. Otherwise it is a
2093 2093 callable taking a revision number and returning a match objects
2094 2094 filtering the files to be detailed when displaying the revision.
2095 2095 """
2096 2096 limit = loglimit(opts)
2097 2097 revs = _logrevs(repo, opts)
2098 2098 if not revs:
2099 2099 return revset.baseset(), None, None
2100 2100 expr, filematcher = _makelogrevset(repo, pats, opts, revs)
2101 2101 if opts.get('rev'):
2102 2102 # User-specified revs might be unsorted, but don't sort before
2103 2103 # _makelogrevset because it might depend on the order of revs
2104 2104 revs.sort(reverse=True)
2105 2105 if expr:
2106 2106 # Revset matchers often operate faster on revisions in changelog
2107 2107 # order, because most filters deal with the changelog.
2108 2108 revs.reverse()
2109 2109 matcher = revset.match(repo.ui, expr)
2110 2110 # Revset matches can reorder revisions. "A or B" typically returns
2111 2111 # returns the revision matching A then the revision matching B. Sort
2112 2112 # again to fix that.
2113 2113 revs = matcher(repo, revs)
2114 2114 revs.sort(reverse=True)
2115 2115 if limit is not None:
2116 2116 limitedrevs = []
2117 2117 for idx, rev in enumerate(revs):
2118 2118 if idx >= limit:
2119 2119 break
2120 2120 limitedrevs.append(rev)
2121 2121 revs = revset.baseset(limitedrevs)
2122 2122
2123 2123 return revs, expr, filematcher
2124 2124
2125 2125 def getlogrevs(repo, pats, opts):
2126 2126 """Return (revs, expr, filematcher) where revs is an iterable of
2127 2127 revision numbers, expr is a revset string built from log options
2128 2128 and file patterns or None, and used to filter 'revs'. If --stat or
2129 2129 --patch are not passed filematcher is None. Otherwise it is a
2130 2130 callable taking a revision number and returning a match objects
2131 2131 filtering the files to be detailed when displaying the revision.
2132 2132 """
2133 2133 limit = loglimit(opts)
2134 2134 revs = _logrevs(repo, opts)
2135 2135 if not revs:
2136 2136 return revset.baseset([]), None, None
2137 2137 expr, filematcher = _makelogrevset(repo, pats, opts, revs)
2138 2138 if expr:
2139 2139 # Revset matchers often operate faster on revisions in changelog
2140 2140 # order, because most filters deal with the changelog.
2141 2141 if not opts.get('rev'):
2142 2142 revs.reverse()
2143 2143 matcher = revset.match(repo.ui, expr)
2144 2144 # Revset matches can reorder revisions. "A or B" typically returns
2145 2145 # returns the revision matching A then the revision matching B. Sort
2146 2146 # again to fix that.
2147 2147 revs = matcher(repo, revs)
2148 2148 if not opts.get('rev'):
2149 2149 revs.sort(reverse=True)
2150 2150 if limit is not None:
2151 2151 limitedrevs = []
2152 2152 for idx, r in enumerate(revs):
2153 2153 if limit <= idx:
2154 2154 break
2155 2155 limitedrevs.append(r)
2156 2156 revs = revset.baseset(limitedrevs)
2157 2157
2158 2158 return revs, expr, filematcher
2159 2159
2160 2160 def displaygraph(ui, dag, displayer, showparents, edgefn, getrenamed=None,
2161 2161 filematcher=None):
2162 2162 seen, state = [], graphmod.asciistate()
2163 2163 for rev, type, ctx, parents in dag:
2164 2164 char = 'o'
2165 2165 if ctx.node() in showparents:
2166 2166 char = '@'
2167 2167 elif ctx.obsolete():
2168 2168 char = 'x'
2169 2169 elif ctx.closesbranch():
2170 2170 char = '_'
2171 2171 copies = None
2172 2172 if getrenamed and ctx.rev():
2173 2173 copies = []
2174 2174 for fn in ctx.files():
2175 2175 rename = getrenamed(fn, ctx.rev())
2176 2176 if rename:
2177 2177 copies.append((fn, rename[0]))
2178 2178 revmatchfn = None
2179 2179 if filematcher is not None:
2180 2180 revmatchfn = filematcher(ctx.rev())
2181 2181 displayer.show(ctx, copies=copies, matchfn=revmatchfn)
2182 2182 lines = displayer.hunk.pop(rev).split('\n')
2183 2183 if not lines[-1]:
2184 2184 del lines[-1]
2185 2185 displayer.flush(ctx)
2186 2186 edges = edgefn(type, char, lines, seen, rev, parents)
2187 2187 for type, char, lines, coldata in edges:
2188 2188 graphmod.ascii(ui, state, type, char, lines, coldata)
2189 2189 displayer.close()
2190 2190
2191 2191 def graphlog(ui, repo, *pats, **opts):
2192 2192 # Parameters are identical to log command ones
2193 2193 revs, expr, filematcher = getgraphlogrevs(repo, pats, opts)
2194 2194 revdag = graphmod.dagwalker(repo, revs)
2195 2195
2196 2196 getrenamed = None
2197 2197 if opts.get('copies'):
2198 2198 endrev = None
2199 2199 if opts.get('rev'):
2200 2200 endrev = scmutil.revrange(repo, opts.get('rev')).max() + 1
2201 2201 getrenamed = templatekw.getrenamedfn(repo, endrev=endrev)
2202 2202 displayer = show_changeset(ui, repo, opts, buffered=True)
2203 2203 showparents = [ctx.node() for ctx in repo[None].parents()]
2204 2204 displaygraph(ui, revdag, displayer, showparents,
2205 2205 graphmod.asciiedges, getrenamed, filematcher)
2206 2206
2207 2207 def checkunsupportedgraphflags(pats, opts):
2208 2208 for op in ["newest_first"]:
2209 2209 if op in opts and opts[op]:
2210 2210 raise error.Abort(_("-G/--graph option is incompatible with --%s")
2211 2211 % op.replace("_", "-"))
2212 2212
2213 2213 def graphrevs(repo, nodes, opts):
2214 2214 limit = loglimit(opts)
2215 2215 nodes.reverse()
2216 2216 if limit is not None:
2217 2217 nodes = nodes[:limit]
2218 2218 return graphmod.nodes(repo, nodes)
2219 2219
2220 2220 def add(ui, repo, match, prefix, explicitonly, **opts):
2221 2221 join = lambda f: os.path.join(prefix, f)
2222 2222 bad = []
2223 2223
2224 2224 badfn = lambda x, y: bad.append(x) or match.bad(x, y)
2225 2225 names = []
2226 2226 wctx = repo[None]
2227 2227 cca = None
2228 2228 abort, warn = scmutil.checkportabilityalert(ui)
2229 2229 if abort or warn:
2230 2230 cca = scmutil.casecollisionauditor(ui, abort, repo.dirstate)
2231 2231
2232 2232 badmatch = matchmod.badmatch(match, badfn)
2233 2233 dirstate = repo.dirstate
2234 2234 # We don't want to just call wctx.walk here, since it would return a lot of
2235 2235 # clean files, which we aren't interested in and takes time.
2236 2236 for f in sorted(dirstate.walk(badmatch, sorted(wctx.substate),
2237 2237 True, False, full=False)):
2238 2238 exact = match.exact(f)
2239 2239 if exact or not explicitonly and f not in wctx and repo.wvfs.lexists(f):
2240 2240 if cca:
2241 2241 cca(f)
2242 2242 names.append(f)
2243 2243 if ui.verbose or not exact:
2244 2244 ui.status(_('adding %s\n') % match.rel(f))
2245 2245
2246 2246 for subpath in sorted(wctx.substate):
2247 2247 sub = wctx.sub(subpath)
2248 2248 try:
2249 2249 submatch = matchmod.narrowmatcher(subpath, match)
2250 2250 if opts.get('subrepos'):
2251 2251 bad.extend(sub.add(ui, submatch, prefix, False, **opts))
2252 2252 else:
2253 2253 bad.extend(sub.add(ui, submatch, prefix, True, **opts))
2254 2254 except error.LookupError:
2255 2255 ui.status(_("skipping missing subrepository: %s\n")
2256 2256 % join(subpath))
2257 2257
2258 2258 if not opts.get('dry_run'):
2259 2259 rejected = wctx.add(names, prefix)
2260 2260 bad.extend(f for f in rejected if f in match.files())
2261 2261 return bad
2262 2262
2263 2263 def forget(ui, repo, match, prefix, explicitonly):
2264 2264 join = lambda f: os.path.join(prefix, f)
2265 2265 bad = []
2266 2266 badfn = lambda x, y: bad.append(x) or match.bad(x, y)
2267 2267 wctx = repo[None]
2268 2268 forgot = []
2269 2269
2270 2270 s = repo.status(match=matchmod.badmatch(match, badfn), clean=True)
2271 2271 forget = sorted(s[0] + s[1] + s[3] + s[6])
2272 2272 if explicitonly:
2273 2273 forget = [f for f in forget if match.exact(f)]
2274 2274
2275 2275 for subpath in sorted(wctx.substate):
2276 2276 sub = wctx.sub(subpath)
2277 2277 try:
2278 2278 submatch = matchmod.narrowmatcher(subpath, match)
2279 2279 subbad, subforgot = sub.forget(submatch, prefix)
2280 2280 bad.extend([subpath + '/' + f for f in subbad])
2281 2281 forgot.extend([subpath + '/' + f for f in subforgot])
2282 2282 except error.LookupError:
2283 2283 ui.status(_("skipping missing subrepository: %s\n")
2284 2284 % join(subpath))
2285 2285
2286 2286 if not explicitonly:
2287 2287 for f in match.files():
2288 2288 if f not in repo.dirstate and not repo.wvfs.isdir(f):
2289 2289 if f not in forgot:
2290 2290 if repo.wvfs.exists(f):
2291 2291 # Don't complain if the exact case match wasn't given.
2292 2292 # But don't do this until after checking 'forgot', so
2293 2293 # that subrepo files aren't normalized, and this op is
2294 2294 # purely from data cached by the status walk above.
2295 2295 if repo.dirstate.normalize(f) in repo.dirstate:
2296 2296 continue
2297 2297 ui.warn(_('not removing %s: '
2298 2298 'file is already untracked\n')
2299 2299 % match.rel(f))
2300 2300 bad.append(f)
2301 2301
2302 2302 for f in forget:
2303 2303 if ui.verbose or not match.exact(f):
2304 2304 ui.status(_('removing %s\n') % match.rel(f))
2305 2305
2306 2306 rejected = wctx.forget(forget, prefix)
2307 2307 bad.extend(f for f in rejected if f in match.files())
2308 2308 forgot.extend(f for f in forget if f not in rejected)
2309 2309 return bad, forgot
2310 2310
2311 2311 def files(ui, ctx, m, fm, fmt, subrepos):
2312 2312 rev = ctx.rev()
2313 2313 ret = 1
2314 2314 ds = ctx.repo().dirstate
2315 2315
2316 2316 for f in ctx.matches(m):
2317 2317 if rev is None and ds[f] == 'r':
2318 2318 continue
2319 2319 fm.startitem()
2320 2320 if ui.verbose:
2321 2321 fc = ctx[f]
2322 2322 fm.write('size flags', '% 10d % 1s ', fc.size(), fc.flags())
2323 2323 fm.data(abspath=f)
2324 2324 fm.write('path', fmt, m.rel(f))
2325 2325 ret = 0
2326 2326
2327 2327 for subpath in sorted(ctx.substate):
2328 2328 def matchessubrepo(subpath):
2329 2329 return (m.always() or m.exact(subpath)
2330 2330 or any(f.startswith(subpath + '/') for f in m.files()))
2331 2331
2332 2332 if subrepos or matchessubrepo(subpath):
2333 2333 sub = ctx.sub(subpath)
2334 2334 try:
2335 2335 submatch = matchmod.narrowmatcher(subpath, m)
2336 2336 if sub.printfiles(ui, submatch, fm, fmt, subrepos) == 0:
2337 2337 ret = 0
2338 2338 except error.LookupError:
2339 2339 ui.status(_("skipping missing subrepository: %s\n")
2340 2340 % m.abs(subpath))
2341 2341
2342 2342 return ret
2343 2343
2344 2344 def remove(ui, repo, m, prefix, after, force, subrepos):
2345 2345 join = lambda f: os.path.join(prefix, f)
2346 2346 ret = 0
2347 2347 s = repo.status(match=m, clean=True)
2348 2348 modified, added, deleted, clean = s[0], s[1], s[3], s[6]
2349 2349
2350 2350 wctx = repo[None]
2351 2351
2352 2352 for subpath in sorted(wctx.substate):
2353 2353 def matchessubrepo(matcher, subpath):
2354 2354 if matcher.exact(subpath):
2355 2355 return True
2356 2356 for f in matcher.files():
2357 2357 if f.startswith(subpath):
2358 2358 return True
2359 2359 return False
2360 2360
2361 2361 if subrepos or matchessubrepo(m, subpath):
2362 2362 sub = wctx.sub(subpath)
2363 2363 try:
2364 2364 submatch = matchmod.narrowmatcher(subpath, m)
2365 2365 if sub.removefiles(submatch, prefix, after, force, subrepos):
2366 2366 ret = 1
2367 2367 except error.LookupError:
2368 2368 ui.status(_("skipping missing subrepository: %s\n")
2369 2369 % join(subpath))
2370 2370
2371 2371 # warn about failure to delete explicit files/dirs
2372 2372 deleteddirs = util.dirs(deleted)
2373 2373 for f in m.files():
2374 2374 def insubrepo():
2375 2375 for subpath in wctx.substate:
2376 2376 if f.startswith(subpath):
2377 2377 return True
2378 2378 return False
2379 2379
2380 2380 isdir = f in deleteddirs or wctx.hasdir(f)
2381 2381 if f in repo.dirstate or isdir or f == '.' or insubrepo():
2382 2382 continue
2383 2383
2384 2384 if repo.wvfs.exists(f):
2385 2385 if repo.wvfs.isdir(f):
2386 2386 ui.warn(_('not removing %s: no tracked files\n')
2387 2387 % m.rel(f))
2388 2388 else:
2389 2389 ui.warn(_('not removing %s: file is untracked\n')
2390 2390 % m.rel(f))
2391 2391 # missing files will generate a warning elsewhere
2392 2392 ret = 1
2393 2393
2394 2394 if force:
2395 2395 list = modified + deleted + clean + added
2396 2396 elif after:
2397 2397 list = deleted
2398 2398 for f in modified + added + clean:
2399 2399 ui.warn(_('not removing %s: file still exists\n') % m.rel(f))
2400 2400 ret = 1
2401 2401 else:
2402 2402 list = deleted + clean
2403 2403 for f in modified:
2404 2404 ui.warn(_('not removing %s: file is modified (use -f'
2405 2405 ' to force removal)\n') % m.rel(f))
2406 2406 ret = 1
2407 2407 for f in added:
2408 2408 ui.warn(_('not removing %s: file has been marked for add'
2409 2409 ' (use forget to undo)\n') % m.rel(f))
2410 2410 ret = 1
2411 2411
2412 2412 for f in sorted(list):
2413 2413 if ui.verbose or not m.exact(f):
2414 2414 ui.status(_('removing %s\n') % m.rel(f))
2415 2415
2416 2416 wlock = repo.wlock()
2417 2417 try:
2418 2418 if not after:
2419 2419 for f in list:
2420 2420 if f in added:
2421 2421 continue # we never unlink added files on remove
2422 2422 util.unlinkpath(repo.wjoin(f), ignoremissing=True)
2423 2423 repo[None].forget(list)
2424 2424 finally:
2425 2425 wlock.release()
2426 2426
2427 2427 return ret
2428 2428
2429 2429 def cat(ui, repo, ctx, matcher, prefix, **opts):
2430 2430 err = 1
2431 2431
2432 2432 def write(path):
2433 2433 fp = makefileobj(repo, opts.get('output'), ctx.node(),
2434 2434 pathname=os.path.join(prefix, path))
2435 2435 data = ctx[path].data()
2436 2436 if opts.get('decode'):
2437 2437 data = repo.wwritedata(path, data)
2438 2438 fp.write(data)
2439 2439 fp.close()
2440 2440
2441 2441 # Automation often uses hg cat on single files, so special case it
2442 2442 # for performance to avoid the cost of parsing the manifest.
2443 2443 if len(matcher.files()) == 1 and not matcher.anypats():
2444 2444 file = matcher.files()[0]
2445 2445 mf = repo.manifest
2446 2446 mfnode = ctx.manifestnode()
2447 2447 if mfnode and mf.find(mfnode, file)[0]:
2448 2448 write(file)
2449 2449 return 0
2450 2450
2451 2451 # Don't warn about "missing" files that are really in subrepos
2452 2452 def badfn(path, msg):
2453 2453 for subpath in ctx.substate:
2454 2454 if path.startswith(subpath):
2455 2455 return
2456 2456 matcher.bad(path, msg)
2457 2457
2458 2458 for abs in ctx.walk(matchmod.badmatch(matcher, badfn)):
2459 2459 write(abs)
2460 2460 err = 0
2461 2461
2462 2462 for subpath in sorted(ctx.substate):
2463 2463 sub = ctx.sub(subpath)
2464 2464 try:
2465 2465 submatch = matchmod.narrowmatcher(subpath, matcher)
2466 2466
2467 2467 if not sub.cat(submatch, os.path.join(prefix, sub._path),
2468 2468 **opts):
2469 2469 err = 0
2470 2470 except error.RepoLookupError:
2471 2471 ui.status(_("skipping missing subrepository: %s\n")
2472 2472 % os.path.join(prefix, subpath))
2473 2473
2474 2474 return err
2475 2475
2476 2476 def commit(ui, repo, commitfunc, pats, opts):
2477 2477 '''commit the specified files or all outstanding changes'''
2478 2478 date = opts.get('date')
2479 2479 if date:
2480 2480 opts['date'] = util.parsedate(date)
2481 2481 message = logmessage(ui, opts)
2482 2482 matcher = scmutil.match(repo[None], pats, opts)
2483 2483
2484 2484 # extract addremove carefully -- this function can be called from a command
2485 2485 # that doesn't support addremove
2486 2486 if opts.get('addremove'):
2487 2487 if scmutil.addremove(repo, matcher, "", opts) != 0:
2488 2488 raise error.Abort(
2489 2489 _("failed to mark all new/missing files as added/removed"))
2490 2490
2491 2491 return commitfunc(ui, repo, message, matcher, opts)
2492 2492
2493 2493 def amend(ui, repo, commitfunc, old, extra, pats, opts):
2494 2494 # avoid cycle context -> subrepo -> cmdutil
2495 2495 import context
2496 2496
2497 2497 # amend will reuse the existing user if not specified, but the obsolete
2498 2498 # marker creation requires that the current user's name is specified.
2499 2499 if obsolete.isenabled(repo, obsolete.createmarkersopt):
2500 2500 ui.username() # raise exception if username not set
2501 2501
2502 2502 ui.note(_('amending changeset %s\n') % old)
2503 2503 base = old.p1()
2504 2504 createmarkers = obsolete.isenabled(repo, obsolete.createmarkersopt)
2505 2505
2506 2506 wlock = lock = newid = None
2507 2507 try:
2508 2508 wlock = repo.wlock()
2509 2509 lock = repo.lock()
2510 2510 tr = repo.transaction('amend')
2511 2511 try:
2512 2512 # See if we got a message from -m or -l, if not, open the editor
2513 2513 # with the message of the changeset to amend
2514 2514 message = logmessage(ui, opts)
2515 2515 # ensure logfile does not conflict with later enforcement of the
2516 2516 # message. potential logfile content has been processed by
2517 2517 # `logmessage` anyway.
2518 2518 opts.pop('logfile')
2519 2519 # First, do a regular commit to record all changes in the working
2520 2520 # directory (if there are any)
2521 2521 ui.callhooks = False
2522 2522 activebookmark = repo._activebookmark
2523 2523 try:
2524 2524 repo._activebookmark = None
2525 2525 opts['message'] = 'temporary amend commit for %s' % old
2526 2526 node = commit(ui, repo, commitfunc, pats, opts)
2527 2527 finally:
2528 2528 repo._activebookmark = activebookmark
2529 2529 ui.callhooks = True
2530 2530 ctx = repo[node]
2531 2531
2532 2532 # Participating changesets:
2533 2533 #
2534 2534 # node/ctx o - new (intermediate) commit that contains changes
2535 2535 # | from working dir to go into amending commit
2536 2536 # | (or a workingctx if there were no changes)
2537 2537 # |
2538 2538 # old o - changeset to amend
2539 2539 # |
2540 2540 # base o - parent of amending changeset
2541 2541
2542 2542 # Update extra dict from amended commit (e.g. to preserve graft
2543 2543 # source)
2544 2544 extra.update(old.extra())
2545 2545
2546 2546 # Also update it from the intermediate commit or from the wctx
2547 2547 extra.update(ctx.extra())
2548 2548
2549 2549 if len(old.parents()) > 1:
2550 2550 # ctx.files() isn't reliable for merges, so fall back to the
2551 2551 # slower repo.status() method
2552 2552 files = set([fn for st in repo.status(base, old)[:3]
2553 2553 for fn in st])
2554 2554 else:
2555 2555 files = set(old.files())
2556 2556
2557 2557 # Second, we use either the commit we just did, or if there were no
2558 2558 # changes the parent of the working directory as the version of the
2559 2559 # files in the final amend commit
2560 2560 if node:
2561 2561 ui.note(_('copying changeset %s to %s\n') % (ctx, base))
2562 2562
2563 2563 user = ctx.user()
2564 2564 date = ctx.date()
2565 2565 # Recompute copies (avoid recording a -> b -> a)
2566 2566 copied = copies.pathcopies(base, ctx)
2567 2567 if old.p2:
2568 2568 copied.update(copies.pathcopies(old.p2(), ctx))
2569 2569
2570 2570 # Prune files which were reverted by the updates: if old
2571 2571 # introduced file X and our intermediate commit, node,
2572 2572 # renamed that file, then those two files are the same and
2573 2573 # we can discard X from our list of files. Likewise if X
2574 2574 # was deleted, it's no longer relevant
2575 2575 files.update(ctx.files())
2576 2576
2577 2577 def samefile(f):
2578 2578 if f in ctx.manifest():
2579 2579 a = ctx.filectx(f)
2580 2580 if f in base.manifest():
2581 2581 b = base.filectx(f)
2582 2582 return (not a.cmp(b)
2583 2583 and a.flags() == b.flags())
2584 2584 else:
2585 2585 return False
2586 2586 else:
2587 2587 return f not in base.manifest()
2588 2588 files = [f for f in files if not samefile(f)]
2589 2589
2590 2590 def filectxfn(repo, ctx_, path):
2591 2591 try:
2592 2592 fctx = ctx[path]
2593 2593 flags = fctx.flags()
2594 2594 mctx = context.memfilectx(repo,
2595 2595 fctx.path(), fctx.data(),
2596 2596 islink='l' in flags,
2597 2597 isexec='x' in flags,
2598 2598 copied=copied.get(path))
2599 2599 return mctx
2600 2600 except KeyError:
2601 2601 return None
2602 2602 else:
2603 2603 ui.note(_('copying changeset %s to %s\n') % (old, base))
2604 2604
2605 2605 # Use version of files as in the old cset
2606 2606 def filectxfn(repo, ctx_, path):
2607 2607 try:
2608 2608 return old.filectx(path)
2609 2609 except KeyError:
2610 2610 return None
2611 2611
2612 2612 user = opts.get('user') or old.user()
2613 2613 date = opts.get('date') or old.date()
2614 2614 editform = mergeeditform(old, 'commit.amend')
2615 2615 editor = getcommiteditor(editform=editform, **opts)
2616 2616 if not message:
2617 2617 editor = getcommiteditor(edit=True, editform=editform)
2618 2618 message = old.description()
2619 2619
2620 2620 pureextra = extra.copy()
2621 2621 extra['amend_source'] = old.hex()
2622 2622
2623 2623 new = context.memctx(repo,
2624 2624 parents=[base.node(), old.p2().node()],
2625 2625 text=message,
2626 2626 files=files,
2627 2627 filectxfn=filectxfn,
2628 2628 user=user,
2629 2629 date=date,
2630 2630 extra=extra,
2631 2631 editor=editor)
2632 2632
2633 2633 newdesc = changelog.stripdesc(new.description())
2634 2634 if ((not node)
2635 2635 and newdesc == old.description()
2636 2636 and user == old.user()
2637 2637 and date == old.date()
2638 2638 and pureextra == old.extra()):
2639 2639 # nothing changed. continuing here would create a new node
2640 2640 # anyway because of the amend_source noise.
2641 2641 #
2642 2642 # This not what we expect from amend.
2643 2643 return old.node()
2644 2644
2645 2645 ph = repo.ui.config('phases', 'new-commit', phases.draft)
2646 2646 try:
2647 2647 if opts.get('secret'):
2648 2648 commitphase = 'secret'
2649 2649 else:
2650 2650 commitphase = old.phase()
2651 2651 repo.ui.setconfig('phases', 'new-commit', commitphase, 'amend')
2652 2652 newid = repo.commitctx(new)
2653 2653 finally:
2654 2654 repo.ui.setconfig('phases', 'new-commit', ph, 'amend')
2655 2655 if newid != old.node():
2656 2656 # Reroute the working copy parent to the new changeset
2657 2657 repo.setparents(newid, nullid)
2658 2658
2659 2659 # Move bookmarks from old parent to amend commit
2660 2660 bms = repo.nodebookmarks(old.node())
2661 2661 if bms:
2662 2662 marks = repo._bookmarks
2663 2663 for bm in bms:
2664 2664 ui.debug('moving bookmarks %r from %s to %s\n' %
2665 2665 (marks, old.hex(), hex(newid)))
2666 2666 marks[bm] = newid
2667 2667 marks.recordchange(tr)
2668 2668 #commit the whole amend process
2669 2669 if createmarkers:
2670 2670 # mark the new changeset as successor of the rewritten one
2671 2671 new = repo[newid]
2672 2672 obs = [(old, (new,))]
2673 2673 if node:
2674 2674 obs.append((ctx, ()))
2675 2675
2676 2676 obsolete.createmarkers(repo, obs)
2677 2677 tr.close()
2678 2678 finally:
2679 2679 tr.release()
2680 2680 if not createmarkers and newid != old.node():
2681 2681 # Strip the intermediate commit (if there was one) and the amended
2682 2682 # commit
2683 2683 if node:
2684 2684 ui.note(_('stripping intermediate changeset %s\n') % ctx)
2685 2685 ui.note(_('stripping amended changeset %s\n') % old)
2686 2686 repair.strip(ui, repo, old.node(), topic='amend-backup')
2687 2687 finally:
2688 2688 lockmod.release(lock, wlock)
2689 2689 return newid
2690 2690
2691 2691 def commiteditor(repo, ctx, subs, editform=''):
2692 2692 if ctx.description():
2693 2693 return ctx.description()
2694 2694 return commitforceeditor(repo, ctx, subs, editform=editform,
2695 2695 unchangedmessagedetection=True)
2696 2696
2697 2697 def commitforceeditor(repo, ctx, subs, finishdesc=None, extramsg=None,
2698 2698 editform='', unchangedmessagedetection=False):
2699 2699 if not extramsg:
2700 2700 extramsg = _("Leave message empty to abort commit.")
2701 2701
2702 2702 forms = [e for e in editform.split('.') if e]
2703 2703 forms.insert(0, 'changeset')
2704 2704 templatetext = None
2705 2705 while forms:
2706 2706 tmpl = repo.ui.config('committemplate', '.'.join(forms))
2707 2707 if tmpl:
2708 2708 templatetext = committext = buildcommittemplate(
2709 2709 repo, ctx, subs, extramsg, tmpl)
2710 2710 break
2711 2711 forms.pop()
2712 2712 else:
2713 2713 committext = buildcommittext(repo, ctx, subs, extramsg)
2714 2714
2715 2715 # run editor in the repository root
2716 2716 olddir = os.getcwd()
2717 2717 os.chdir(repo.root)
2718 2718
2719 2719 # make in-memory changes visible to external process
2720 2720 tr = repo.currenttransaction()
2721 2721 repo.dirstate.write(tr)
2722 2722 pending = tr and tr.writepending() and repo.root
2723 2723
2724 2724 editortext = repo.ui.edit(committext, ctx.user(), ctx.extra(),
2725 2725 editform=editform, pending=pending)
2726 2726 text = re.sub("(?m)^HG:.*(\n|$)", "", editortext)
2727 2727 os.chdir(olddir)
2728 2728
2729 2729 if finishdesc:
2730 2730 text = finishdesc(text)
2731 2731 if not text.strip():
2732 2732 raise error.Abort(_("empty commit message"))
2733 2733 if unchangedmessagedetection and editortext == templatetext:
2734 2734 raise error.Abort(_("commit message unchanged"))
2735 2735
2736 2736 return text
2737 2737
2738 2738 def buildcommittemplate(repo, ctx, subs, extramsg, tmpl):
2739 2739 ui = repo.ui
2740 2740 tmpl, mapfile = gettemplate(ui, tmpl, None)
2741 2741
2742 2742 try:
2743 2743 t = changeset_templater(ui, repo, None, {}, tmpl, mapfile, False)
2744 2744 except SyntaxError as inst:
2745 2745 raise error.Abort(inst.args[0])
2746 2746
2747 2747 for k, v in repo.ui.configitems('committemplate'):
2748 2748 if k != 'changeset':
2749 2749 t.t.cache[k] = v
2750 2750
2751 2751 if not extramsg:
2752 2752 extramsg = '' # ensure that extramsg is string
2753 2753
2754 2754 ui.pushbuffer()
2755 2755 t.show(ctx, extramsg=extramsg)
2756 2756 return ui.popbuffer()
2757 2757
2758 2758 def hgprefix(msg):
2759 2759 return "\n".join(["HG: %s" % a for a in msg.split("\n") if a])
2760 2760
2761 2761 def buildcommittext(repo, ctx, subs, extramsg):
2762 2762 edittext = []
2763 2763 modified, added, removed = ctx.modified(), ctx.added(), ctx.removed()
2764 2764 if ctx.description():
2765 2765 edittext.append(ctx.description())
2766 2766 edittext.append("")
2767 2767 edittext.append("") # Empty line between message and comments.
2768 2768 edittext.append(hgprefix(_("Enter commit message."
2769 2769 " Lines beginning with 'HG:' are removed.")))
2770 2770 edittext.append(hgprefix(extramsg))
2771 2771 edittext.append("HG: --")
2772 2772 edittext.append(hgprefix(_("user: %s") % ctx.user()))
2773 2773 if ctx.p2():
2774 2774 edittext.append(hgprefix(_("branch merge")))
2775 2775 if ctx.branch():
2776 2776 edittext.append(hgprefix(_("branch '%s'") % ctx.branch()))
2777 2777 if bookmarks.isactivewdirparent(repo):
2778 2778 edittext.append(hgprefix(_("bookmark '%s'") % repo._activebookmark))
2779 2779 edittext.extend([hgprefix(_("subrepo %s") % s) for s in subs])
2780 2780 edittext.extend([hgprefix(_("added %s") % f) for f in added])
2781 2781 edittext.extend([hgprefix(_("changed %s") % f) for f in modified])
2782 2782 edittext.extend([hgprefix(_("removed %s") % f) for f in removed])
2783 2783 if not added and not modified and not removed:
2784 2784 edittext.append(hgprefix(_("no files changed")))
2785 2785 edittext.append("")
2786 2786
2787 2787 return "\n".join(edittext)
2788 2788
2789 2789 def commitstatus(repo, node, branch, bheads=None, opts=None):
2790 2790 if opts is None:
2791 2791 opts = {}
2792 2792 ctx = repo[node]
2793 2793 parents = ctx.parents()
2794 2794
2795 2795 if (not opts.get('amend') and bheads and node not in bheads and not
2796 2796 [x for x in parents if x.node() in bheads and x.branch() == branch]):
2797 2797 repo.ui.status(_('created new head\n'))
2798 2798 # The message is not printed for initial roots. For the other
2799 2799 # changesets, it is printed in the following situations:
2800 2800 #
2801 2801 # Par column: for the 2 parents with ...
2802 2802 # N: null or no parent
2803 2803 # B: parent is on another named branch
2804 2804 # C: parent is a regular non head changeset
2805 2805 # H: parent was a branch head of the current branch
2806 2806 # Msg column: whether we print "created new head" message
2807 2807 # In the following, it is assumed that there already exists some
2808 2808 # initial branch heads of the current branch, otherwise nothing is
2809 2809 # printed anyway.
2810 2810 #
2811 2811 # Par Msg Comment
2812 2812 # N N y additional topo root
2813 2813 #
2814 2814 # B N y additional branch root
2815 2815 # C N y additional topo head
2816 2816 # H N n usual case
2817 2817 #
2818 2818 # B B y weird additional branch root
2819 2819 # C B y branch merge
2820 2820 # H B n merge with named branch
2821 2821 #
2822 2822 # C C y additional head from merge
2823 2823 # C H n merge with a head
2824 2824 #
2825 2825 # H H n head merge: head count decreases
2826 2826
2827 2827 if not opts.get('close_branch'):
2828 2828 for r in parents:
2829 2829 if r.closesbranch() and r.branch() == branch:
2830 2830 repo.ui.status(_('reopening closed branch head %d\n') % r)
2831 2831
2832 2832 if repo.ui.debugflag:
2833 2833 repo.ui.write(_('committed changeset %d:%s\n') % (int(ctx), ctx.hex()))
2834 2834 elif repo.ui.verbose:
2835 2835 repo.ui.write(_('committed changeset %d:%s\n') % (int(ctx), ctx))
2836 2836
2837 2837 def revert(ui, repo, ctx, parents, *pats, **opts):
2838 2838 parent, p2 = parents
2839 2839 node = ctx.node()
2840 2840
2841 2841 mf = ctx.manifest()
2842 2842 if node == p2:
2843 2843 parent = p2
2844 2844 if node == parent:
2845 2845 pmf = mf
2846 2846 else:
2847 2847 pmf = None
2848 2848
2849 2849 # need all matching names in dirstate and manifest of target rev,
2850 2850 # so have to walk both. do not print errors if files exist in one
2851 2851 # but not other. in both cases, filesets should be evaluated against
2852 2852 # workingctx to get consistent result (issue4497). this means 'set:**'
2853 2853 # cannot be used to select missing files from target rev.
2854 2854
2855 2855 # `names` is a mapping for all elements in working copy and target revision
2856 2856 # The mapping is in the form:
2857 2857 # <asb path in repo> -> (<path from CWD>, <exactly specified by matcher?>)
2858 2858 names = {}
2859 2859
2860 2860 wlock = repo.wlock()
2861 2861 try:
2862 2862 ## filling of the `names` mapping
2863 2863 # walk dirstate to fill `names`
2864 2864
2865 2865 interactive = opts.get('interactive', False)
2866 2866 wctx = repo[None]
2867 2867 m = scmutil.match(wctx, pats, opts)
2868 2868
2869 2869 # we'll need this later
2870 2870 targetsubs = sorted(s for s in wctx.substate if m(s))
2871 2871
2872 2872 if not m.always():
2873 2873 for abs in repo.walk(matchmod.badmatch(m, lambda x, y: False)):
2874 2874 names[abs] = m.rel(abs), m.exact(abs)
2875 2875
2876 2876 # walk target manifest to fill `names`
2877 2877
2878 2878 def badfn(path, msg):
2879 2879 if path in names:
2880 2880 return
2881 2881 if path in ctx.substate:
2882 2882 return
2883 2883 path_ = path + '/'
2884 2884 for f in names:
2885 2885 if f.startswith(path_):
2886 2886 return
2887 2887 ui.warn("%s: %s\n" % (m.rel(path), msg))
2888 2888
2889 2889 for abs in ctx.walk(matchmod.badmatch(m, badfn)):
2890 2890 if abs not in names:
2891 2891 names[abs] = m.rel(abs), m.exact(abs)
2892 2892
2893 2893 # Find status of all file in `names`.
2894 2894 m = scmutil.matchfiles(repo, names)
2895 2895
2896 2896 changes = repo.status(node1=node, match=m,
2897 2897 unknown=True, ignored=True, clean=True)
2898 2898 else:
2899 2899 changes = repo.status(node1=node, match=m)
2900 2900 for kind in changes:
2901 2901 for abs in kind:
2902 2902 names[abs] = m.rel(abs), m.exact(abs)
2903 2903
2904 2904 m = scmutil.matchfiles(repo, names)
2905 2905
2906 2906 modified = set(changes.modified)
2907 2907 added = set(changes.added)
2908 2908 removed = set(changes.removed)
2909 2909 _deleted = set(changes.deleted)
2910 2910 unknown = set(changes.unknown)
2911 2911 unknown.update(changes.ignored)
2912 2912 clean = set(changes.clean)
2913 2913 modadded = set()
2914 2914
2915 2915 # split between files known in target manifest and the others
2916 2916 smf = set(mf)
2917 2917
2918 2918 # determine the exact nature of the deleted changesets
2919 2919 deladded = _deleted - smf
2920 2920 deleted = _deleted - deladded
2921 2921
2922 2922 # We need to account for the state of the file in the dirstate,
2923 2923 # even when we revert against something else than parent. This will
2924 2924 # slightly alter the behavior of revert (doing back up or not, delete
2925 2925 # or just forget etc).
2926 2926 if parent == node:
2927 2927 dsmodified = modified
2928 2928 dsadded = added
2929 2929 dsremoved = removed
2930 2930 # store all local modifications, useful later for rename detection
2931 2931 localchanges = dsmodified | dsadded
2932 2932 modified, added, removed = set(), set(), set()
2933 2933 else:
2934 2934 changes = repo.status(node1=parent, match=m)
2935 2935 dsmodified = set(changes.modified)
2936 2936 dsadded = set(changes.added)
2937 2937 dsremoved = set(changes.removed)
2938 2938 # store all local modifications, useful later for rename detection
2939 2939 localchanges = dsmodified | dsadded
2940 2940
2941 2941 # only take into account for removes between wc and target
2942 2942 clean |= dsremoved - removed
2943 2943 dsremoved &= removed
2944 2944 # distinct between dirstate remove and other
2945 2945 removed -= dsremoved
2946 2946
2947 2947 modadded = added & dsmodified
2948 2948 added -= modadded
2949 2949
2950 2950 # tell newly modified apart.
2951 2951 dsmodified &= modified
2952 2952 dsmodified |= modified & dsadded # dirstate added may needs backup
2953 2953 modified -= dsmodified
2954 2954
2955 2955 # We need to wait for some post-processing to update this set
2956 2956 # before making the distinction. The dirstate will be used for
2957 2957 # that purpose.
2958 2958 dsadded = added
2959 2959
2960 2960 # in case of merge, files that are actually added can be reported as
2961 2961 # modified, we need to post process the result
2962 2962 if p2 != nullid:
2963 2963 if pmf is None:
2964 2964 # only need parent manifest in the merge case,
2965 2965 # so do not read by default
2966 2966 pmf = repo[parent].manifest()
2967 2967 mergeadd = dsmodified - set(pmf)
2968 2968 dsadded |= mergeadd
2969 2969 dsmodified -= mergeadd
2970 2970
2971 2971 # if f is a rename, update `names` to also revert the source
2972 2972 cwd = repo.getcwd()
2973 2973 for f in localchanges:
2974 2974 src = repo.dirstate.copied(f)
2975 2975 # XXX should we check for rename down to target node?
2976 2976 if src and src not in names and repo.dirstate[src] == 'r':
2977 2977 dsremoved.add(src)
2978 2978 names[src] = (repo.pathto(src, cwd), True)
2979 2979
2980 2980 # distinguish between file to forget and the other
2981 2981 added = set()
2982 2982 for abs in dsadded:
2983 2983 if repo.dirstate[abs] != 'a':
2984 2984 added.add(abs)
2985 2985 dsadded -= added
2986 2986
2987 2987 for abs in deladded:
2988 2988 if repo.dirstate[abs] == 'a':
2989 2989 dsadded.add(abs)
2990 2990 deladded -= dsadded
2991 2991
2992 2992 # For files marked as removed, we check if an unknown file is present at
2993 2993 # the same path. If a such file exists it may need to be backed up.
2994 2994 # Making the distinction at this stage helps have simpler backup
2995 2995 # logic.
2996 2996 removunk = set()
2997 2997 for abs in removed:
2998 2998 target = repo.wjoin(abs)
2999 2999 if os.path.lexists(target):
3000 3000 removunk.add(abs)
3001 3001 removed -= removunk
3002 3002
3003 3003 dsremovunk = set()
3004 3004 for abs in dsremoved:
3005 3005 target = repo.wjoin(abs)
3006 3006 if os.path.lexists(target):
3007 3007 dsremovunk.add(abs)
3008 3008 dsremoved -= dsremovunk
3009 3009
3010 3010 # action to be actually performed by revert
3011 3011 # (<list of file>, message>) tuple
3012 3012 actions = {'revert': ([], _('reverting %s\n')),
3013 3013 'add': ([], _('adding %s\n')),
3014 3014 'remove': ([], _('removing %s\n')),
3015 3015 'drop': ([], _('removing %s\n')),
3016 3016 'forget': ([], _('forgetting %s\n')),
3017 3017 'undelete': ([], _('undeleting %s\n')),
3018 3018 'noop': (None, _('no changes needed to %s\n')),
3019 3019 'unknown': (None, _('file not managed: %s\n')),
3020 3020 }
3021 3021
3022 3022 # "constant" that convey the backup strategy.
3023 3023 # All set to `discard` if `no-backup` is set do avoid checking
3024 3024 # no_backup lower in the code.
3025 3025 # These values are ordered for comparison purposes
3026 3026 backup = 2 # unconditionally do backup
3027 3027 check = 1 # check if the existing file differs from target
3028 3028 discard = 0 # never do backup
3029 3029 if opts.get('no_backup'):
3030 3030 backup = check = discard
3031 3031
3032 3032 backupanddel = actions['remove']
3033 3033 if not opts.get('no_backup'):
3034 3034 backupanddel = actions['drop']
3035 3035
3036 3036 disptable = (
3037 3037 # dispatch table:
3038 3038 # file state
3039 3039 # action
3040 3040 # make backup
3041 3041
3042 3042 ## Sets that results that will change file on disk
3043 3043 # Modified compared to target, no local change
3044 3044 (modified, actions['revert'], discard),
3045 3045 # Modified compared to target, but local file is deleted
3046 3046 (deleted, actions['revert'], discard),
3047 3047 # Modified compared to target, local change
3048 3048 (dsmodified, actions['revert'], backup),
3049 3049 # Added since target
3050 3050 (added, actions['remove'], discard),
3051 3051 # Added in working directory
3052 3052 (dsadded, actions['forget'], discard),
3053 3053 # Added since target, have local modification
3054 3054 (modadded, backupanddel, backup),
3055 3055 # Added since target but file is missing in working directory
3056 3056 (deladded, actions['drop'], discard),
3057 3057 # Removed since target, before working copy parent
3058 3058 (removed, actions['add'], discard),
3059 3059 # Same as `removed` but an unknown file exists at the same path
3060 3060 (removunk, actions['add'], check),
3061 3061 # Removed since targe, marked as such in working copy parent
3062 3062 (dsremoved, actions['undelete'], discard),
3063 3063 # Same as `dsremoved` but an unknown file exists at the same path
3064 3064 (dsremovunk, actions['undelete'], check),
3065 3065 ## the following sets does not result in any file changes
3066 3066 # File with no modification
3067 3067 (clean, actions['noop'], discard),
3068 3068 # Existing file, not tracked anywhere
3069 3069 (unknown, actions['unknown'], discard),
3070 3070 )
3071 3071
3072 3072 for abs, (rel, exact) in sorted(names.items()):
3073 3073 # target file to be touch on disk (relative to cwd)
3074 3074 target = repo.wjoin(abs)
3075 3075 # search the entry in the dispatch table.
3076 3076 # if the file is in any of these sets, it was touched in the working
3077 3077 # directory parent and we are sure it needs to be reverted.
3078 3078 for table, (xlist, msg), dobackup in disptable:
3079 3079 if abs not in table:
3080 3080 continue
3081 3081 if xlist is not None:
3082 3082 xlist.append(abs)
3083 3083 if dobackup and (backup <= dobackup
3084 3084 or wctx[abs].cmp(ctx[abs])):
3085 bakname = "%s.orig" % rel
3085 bakname = origpath(ui, repo, rel)
3086 3086 ui.note(_('saving current version of %s as %s\n') %
3087 3087 (rel, bakname))
3088 3088 if not opts.get('dry_run'):
3089 3089 if interactive:
3090 3090 util.copyfile(target, bakname)
3091 3091 else:
3092 3092 util.rename(target, bakname)
3093 3093 if ui.verbose or not exact:
3094 3094 if not isinstance(msg, basestring):
3095 3095 msg = msg(abs)
3096 3096 ui.status(msg % rel)
3097 3097 elif exact:
3098 3098 ui.warn(msg % rel)
3099 3099 break
3100 3100
3101 3101 if not opts.get('dry_run'):
3102 3102 needdata = ('revert', 'add', 'undelete')
3103 3103 _revertprefetch(repo, ctx, *[actions[name][0] for name in needdata])
3104 3104 _performrevert(repo, parents, ctx, actions, interactive)
3105 3105
3106 3106 if targetsubs:
3107 3107 # Revert the subrepos on the revert list
3108 3108 for sub in targetsubs:
3109 3109 try:
3110 3110 wctx.sub(sub).revert(ctx.substate[sub], *pats, **opts)
3111 3111 except KeyError:
3112 3112 raise error.Abort("subrepository '%s' does not exist in %s!"
3113 3113 % (sub, short(ctx.node())))
3114 3114 finally:
3115 3115 wlock.release()
3116 3116
3117 3117 def origpath(ui, repo, filepath):
3118 3118 '''customize where .orig files are created
3119 3119
3120 3120 Fetch user defined path from config file: [ui] origbackuppath = <path>
3121 3121 Fall back to default (filepath) if not specified
3122 3122 '''
3123 3123 origbackuppath = ui.config('ui', 'origbackuppath', None)
3124 3124 if origbackuppath is None:
3125 3125 return filepath + ".orig"
3126 3126
3127 3127 filepathfromroot = os.path.relpath(filepath, start=repo.root)
3128 3128 fullorigpath = repo.wjoin(origbackuppath, filepathfromroot)
3129 3129
3130 3130 origbackupdir = repo.vfs.dirname(fullorigpath)
3131 3131 if not repo.vfs.exists(origbackupdir):
3132 3132 ui.note(_('creating directory: %s\n') % origbackupdir)
3133 3133 util.makedirs(origbackupdir)
3134 3134
3135 3135 return fullorigpath + ".orig"
3136 3136
3137 3137 def _revertprefetch(repo, ctx, *files):
3138 3138 """Let extension changing the storage layer prefetch content"""
3139 3139 pass
3140 3140
3141 3141 def _performrevert(repo, parents, ctx, actions, interactive=False):
3142 3142 """function that actually perform all the actions computed for revert
3143 3143
3144 3144 This is an independent function to let extension to plug in and react to
3145 3145 the imminent revert.
3146 3146
3147 3147 Make sure you have the working directory locked when calling this function.
3148 3148 """
3149 3149 parent, p2 = parents
3150 3150 node = ctx.node()
3151 3151 def checkout(f):
3152 3152 fc = ctx[f]
3153 3153 repo.wwrite(f, fc.data(), fc.flags())
3154 3154
3155 3155 audit_path = pathutil.pathauditor(repo.root)
3156 3156 for f in actions['forget'][0]:
3157 3157 repo.dirstate.drop(f)
3158 3158 for f in actions['remove'][0]:
3159 3159 audit_path(f)
3160 3160 try:
3161 3161 util.unlinkpath(repo.wjoin(f))
3162 3162 except OSError:
3163 3163 pass
3164 3164 repo.dirstate.remove(f)
3165 3165 for f in actions['drop'][0]:
3166 3166 audit_path(f)
3167 3167 repo.dirstate.remove(f)
3168 3168
3169 3169 normal = None
3170 3170 if node == parent:
3171 3171 # We're reverting to our parent. If possible, we'd like status
3172 3172 # to report the file as clean. We have to use normallookup for
3173 3173 # merges to avoid losing information about merged/dirty files.
3174 3174 if p2 != nullid:
3175 3175 normal = repo.dirstate.normallookup
3176 3176 else:
3177 3177 normal = repo.dirstate.normal
3178 3178
3179 3179 newlyaddedandmodifiedfiles = set()
3180 3180 if interactive:
3181 3181 # Prompt the user for changes to revert
3182 3182 torevert = [repo.wjoin(f) for f in actions['revert'][0]]
3183 3183 m = scmutil.match(ctx, torevert, {})
3184 3184 diffopts = patch.difffeatureopts(repo.ui, whitespace=True)
3185 3185 diffopts.nodates = True
3186 3186 diffopts.git = True
3187 3187 reversehunks = repo.ui.configbool('experimental',
3188 3188 'revertalternateinteractivemode',
3189 3189 True)
3190 3190 if reversehunks:
3191 3191 diff = patch.diff(repo, ctx.node(), None, m, opts=diffopts)
3192 3192 else:
3193 3193 diff = patch.diff(repo, None, ctx.node(), m, opts=diffopts)
3194 3194 originalchunks = patch.parsepatch(diff)
3195 3195
3196 3196 try:
3197 3197
3198 3198 chunks = recordfilter(repo.ui, originalchunks)
3199 3199 if reversehunks:
3200 3200 chunks = patch.reversehunks(chunks)
3201 3201
3202 3202 except patch.PatchError as err:
3203 3203 raise error.Abort(_('error parsing patch: %s') % err)
3204 3204
3205 3205 newlyaddedandmodifiedfiles = newandmodified(chunks, originalchunks)
3206 3206 # Apply changes
3207 3207 fp = cStringIO.StringIO()
3208 3208 for c in chunks:
3209 3209 c.write(fp)
3210 3210 dopatch = fp.tell()
3211 3211 fp.seek(0)
3212 3212 if dopatch:
3213 3213 try:
3214 3214 patch.internalpatch(repo.ui, repo, fp, 1, eolmode=None)
3215 3215 except patch.PatchError as err:
3216 3216 raise error.Abort(str(err))
3217 3217 del fp
3218 3218 else:
3219 3219 for f in actions['revert'][0]:
3220 3220 checkout(f)
3221 3221 if normal:
3222 3222 normal(f)
3223 3223
3224 3224 for f in actions['add'][0]:
3225 3225 # Don't checkout modified files, they are already created by the diff
3226 3226 if f not in newlyaddedandmodifiedfiles:
3227 3227 checkout(f)
3228 3228 repo.dirstate.add(f)
3229 3229
3230 3230 normal = repo.dirstate.normallookup
3231 3231 if node == parent and p2 == nullid:
3232 3232 normal = repo.dirstate.normal
3233 3233 for f in actions['undelete'][0]:
3234 3234 checkout(f)
3235 3235 normal(f)
3236 3236
3237 3237 copied = copies.pathcopies(repo[parent], ctx)
3238 3238
3239 3239 for f in actions['add'][0] + actions['undelete'][0] + actions['revert'][0]:
3240 3240 if f in copied:
3241 3241 repo.dirstate.copy(copied[f], f)
3242 3242
3243 3243 def command(table):
3244 3244 """Returns a function object to be used as a decorator for making commands.
3245 3245
3246 3246 This function receives a command table as its argument. The table should
3247 3247 be a dict.
3248 3248
3249 3249 The returned function can be used as a decorator for adding commands
3250 3250 to that command table. This function accepts multiple arguments to define
3251 3251 a command.
3252 3252
3253 3253 The first argument is the command name.
3254 3254
3255 3255 The options argument is an iterable of tuples defining command arguments.
3256 3256 See ``mercurial.fancyopts.fancyopts()`` for the format of each tuple.
3257 3257
3258 3258 The synopsis argument defines a short, one line summary of how to use the
3259 3259 command. This shows up in the help output.
3260 3260
3261 3261 The norepo argument defines whether the command does not require a
3262 3262 local repository. Most commands operate against a repository, thus the
3263 3263 default is False.
3264 3264
3265 3265 The optionalrepo argument defines whether the command optionally requires
3266 3266 a local repository.
3267 3267
3268 3268 The inferrepo argument defines whether to try to find a repository from the
3269 3269 command line arguments. If True, arguments will be examined for potential
3270 3270 repository locations. See ``findrepo()``. If a repository is found, it
3271 3271 will be used.
3272 3272 """
3273 3273 def cmd(name, options=(), synopsis=None, norepo=False, optionalrepo=False,
3274 3274 inferrepo=False):
3275 3275 def decorator(func):
3276 3276 if synopsis:
3277 3277 table[name] = func, list(options), synopsis
3278 3278 else:
3279 3279 table[name] = func, list(options)
3280 3280
3281 3281 if norepo:
3282 3282 # Avoid import cycle.
3283 3283 import commands
3284 3284 commands.norepo += ' %s' % ' '.join(parsealiases(name))
3285 3285
3286 3286 if optionalrepo:
3287 3287 import commands
3288 3288 commands.optionalrepo += ' %s' % ' '.join(parsealiases(name))
3289 3289
3290 3290 if inferrepo:
3291 3291 import commands
3292 3292 commands.inferrepo += ' %s' % ' '.join(parsealiases(name))
3293 3293
3294 3294 return func
3295 3295 return decorator
3296 3296
3297 3297 return cmd
3298 3298
3299 3299 # a list of (ui, repo, otherpeer, opts, missing) functions called by
3300 3300 # commands.outgoing. "missing" is "missing" of the result of
3301 3301 # "findcommonoutgoing()"
3302 3302 outgoinghooks = util.hooks()
3303 3303
3304 3304 # a list of (ui, repo) functions called by commands.summary
3305 3305 summaryhooks = util.hooks()
3306 3306
3307 3307 # a list of (ui, repo, opts, changes) functions called by commands.summary.
3308 3308 #
3309 3309 # functions should return tuple of booleans below, if 'changes' is None:
3310 3310 # (whether-incomings-are-needed, whether-outgoings-are-needed)
3311 3311 #
3312 3312 # otherwise, 'changes' is a tuple of tuples below:
3313 3313 # - (sourceurl, sourcebranch, sourcepeer, incoming)
3314 3314 # - (desturl, destbranch, destpeer, outgoing)
3315 3315 summaryremotehooks = util.hooks()
3316 3316
3317 3317 # A list of state files kept by multistep operations like graft.
3318 3318 # Since graft cannot be aborted, it is considered 'clearable' by update.
3319 3319 # note: bisect is intentionally excluded
3320 3320 # (state file, clearable, allowcommit, error, hint)
3321 3321 unfinishedstates = [
3322 3322 ('graftstate', True, False, _('graft in progress'),
3323 3323 _("use 'hg graft --continue' or 'hg update' to abort")),
3324 3324 ('updatestate', True, False, _('last update was interrupted'),
3325 3325 _("use 'hg update' to get a consistent checkout"))
3326 3326 ]
3327 3327
3328 3328 def checkunfinished(repo, commit=False):
3329 3329 '''Look for an unfinished multistep operation, like graft, and abort
3330 3330 if found. It's probably good to check this right before
3331 3331 bailifchanged().
3332 3332 '''
3333 3333 for f, clearable, allowcommit, msg, hint in unfinishedstates:
3334 3334 if commit and allowcommit:
3335 3335 continue
3336 3336 if repo.vfs.exists(f):
3337 3337 raise error.Abort(msg, hint=hint)
3338 3338
3339 3339 def clearunfinished(repo):
3340 3340 '''Check for unfinished operations (as above), and clear the ones
3341 3341 that are clearable.
3342 3342 '''
3343 3343 for f, clearable, allowcommit, msg, hint in unfinishedstates:
3344 3344 if not clearable and repo.vfs.exists(f):
3345 3345 raise error.Abort(msg, hint=hint)
3346 3346 for f, clearable, allowcommit, msg, hint in unfinishedstates:
3347 3347 if clearable and repo.vfs.exists(f):
3348 3348 util.unlink(repo.join(f))
3349 3349
3350 3350 class dirstateguard(object):
3351 3351 '''Restore dirstate at unexpected failure.
3352 3352
3353 3353 At the construction, this class does:
3354 3354
3355 3355 - write current ``repo.dirstate`` out, and
3356 3356 - save ``.hg/dirstate`` into the backup file
3357 3357
3358 3358 This restores ``.hg/dirstate`` from backup file, if ``release()``
3359 3359 is invoked before ``close()``.
3360 3360
3361 3361 This just removes the backup file at ``close()`` before ``release()``.
3362 3362 '''
3363 3363
3364 3364 def __init__(self, repo, name):
3365 3365 self._repo = repo
3366 3366 self._suffix = '.backup.%s.%d' % (name, id(self))
3367 3367 repo.dirstate._savebackup(repo.currenttransaction(), self._suffix)
3368 3368 self._active = True
3369 3369 self._closed = False
3370 3370
3371 3371 def __del__(self):
3372 3372 if self._active: # still active
3373 3373 # this may occur, even if this class is used correctly:
3374 3374 # for example, releasing other resources like transaction
3375 3375 # may raise exception before ``dirstateguard.release`` in
3376 3376 # ``release(tr, ....)``.
3377 3377 self._abort()
3378 3378
3379 3379 def close(self):
3380 3380 if not self._active: # already inactivated
3381 3381 msg = (_("can't close already inactivated backup: dirstate%s")
3382 3382 % self._suffix)
3383 3383 raise error.Abort(msg)
3384 3384
3385 3385 self._repo.dirstate._clearbackup(self._repo.currenttransaction(),
3386 3386 self._suffix)
3387 3387 self._active = False
3388 3388 self._closed = True
3389 3389
3390 3390 def _abort(self):
3391 3391 self._repo.dirstate._restorebackup(self._repo.currenttransaction(),
3392 3392 self._suffix)
3393 3393 self._active = False
3394 3394
3395 3395 def release(self):
3396 3396 if not self._closed:
3397 3397 if not self._active: # already inactivated
3398 3398 msg = (_("can't release already inactivated backup:"
3399 3399 " dirstate%s")
3400 3400 % self._suffix)
3401 3401 raise error.Abort(msg)
3402 3402 self._abort()
@@ -1,1068 +1,1078
1 1 $ hg init repo
2 2 $ cd repo
3 3 $ echo 123 > a
4 4 $ echo 123 > c
5 5 $ echo 123 > e
6 6 $ hg add a c e
7 7 $ hg commit -m "first" a c e
8 8
9 9 nothing changed
10 10
11 11 $ hg revert
12 12 abort: no files or directories specified
13 13 (use --all to revert all files)
14 14 [255]
15 15 $ hg revert --all
16 16
17 17 Introduce some changes and revert them
18 18 --------------------------------------
19 19
20 20 $ echo 123 > b
21 21
22 22 $ hg status
23 23 ? b
24 24 $ echo 12 > c
25 25
26 26 $ hg status
27 27 M c
28 28 ? b
29 29 $ hg add b
30 30
31 31 $ hg status
32 32 M c
33 33 A b
34 34 $ hg rm a
35 35
36 36 $ hg status
37 37 M c
38 38 A b
39 39 R a
40 40
41 41 revert removal of a file
42 42
43 43 $ hg revert a
44 44 $ hg status
45 45 M c
46 46 A b
47 47
48 48 revert addition of a file
49 49
50 50 $ hg revert b
51 51 $ hg status
52 52 M c
53 53 ? b
54 54
55 55 revert modification of a file (--no-backup)
56 56
57 57 $ hg revert --no-backup c
58 58 $ hg status
59 59 ? b
60 60
61 61 revert deletion (! status) of a added file
62 62 ------------------------------------------
63 63
64 64 $ hg add b
65 65
66 66 $ hg status b
67 67 A b
68 68 $ rm b
69 69 $ hg status b
70 70 ! b
71 71 $ hg revert -v b
72 72 forgetting b
73 73 $ hg status b
74 74 b: * (glob)
75 75
76 76 $ ls
77 77 a
78 78 c
79 79 e
80 80
81 81 Test creation of backup (.orig) files
82 82 -------------------------------------
83 83
84 84 $ echo z > e
85 85 $ hg revert --all -v
86 86 saving current version of e as e.orig
87 87 reverting e
88 88
89 Test creation of backup (.orig) file in configured file location
90 ----------------------------------------------------------------
91
92 $ echo z > e
93 $ hg revert --all -v --config 'ui.origbackuppath=.hg/origbackups'
94 creating directory: $TESTTMP/repo/.hg/origbackups
95 saving current version of e as $TESTTMP/repo/.hg/origbackups/e.orig
96 reverting e
97 $ rm -rf .hg/origbackups
98
89 99 revert on clean file (no change)
90 100 --------------------------------
91 101
92 102 $ hg revert a
93 103 no changes needed to a
94 104
95 105 revert on an untracked file
96 106 ---------------------------
97 107
98 108 $ echo q > q
99 109 $ hg revert q
100 110 file not managed: q
101 111 $ rm q
102 112
103 113 revert on file that does not exists
104 114 -----------------------------------
105 115
106 116 $ hg revert notfound
107 117 notfound: no such file in rev 334a9e57682c
108 118 $ touch d
109 119 $ hg add d
110 120 $ hg rm a
111 121 $ hg commit -m "second"
112 122 $ echo z > z
113 123 $ hg add z
114 124 $ hg st
115 125 A z
116 126 ? e.orig
117 127
118 128 revert to another revision (--rev)
119 129 ----------------------------------
120 130
121 131 $ hg revert --all -r0
122 132 adding a
123 133 removing d
124 134 forgetting z
125 135
126 136 revert explicitly to parent (--rev)
127 137 -----------------------------------
128 138
129 139 $ hg revert --all -rtip
130 140 forgetting a
131 141 undeleting d
132 142 $ rm a *.orig
133 143
134 144 revert to another revision (--rev) and exact match
135 145 --------------------------------------------------
136 146
137 147 exact match are more silent
138 148
139 149 $ hg revert -r0 a
140 150 $ hg st a
141 151 A a
142 152 $ hg rm d
143 153 $ hg st d
144 154 R d
145 155
146 156 should keep d removed
147 157
148 158 $ hg revert -r0 d
149 159 no changes needed to d
150 160 $ hg st d
151 161 R d
152 162
153 163 $ hg update -C
154 164 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
155 165
156 166 revert of exec bit
157 167 ------------------
158 168
159 169 #if execbit
160 170 $ chmod +x c
161 171 $ hg revert --all
162 172 reverting c
163 173
164 174 $ test -x c || echo non-executable
165 175 non-executable
166 176
167 177 $ chmod +x c
168 178 $ hg commit -m exe
169 179
170 180 $ chmod -x c
171 181 $ hg revert --all
172 182 reverting c
173 183
174 184 $ test -x c && echo executable
175 185 executable
176 186 #endif
177 187
178 188 Test that files reverted to other than the parent are treated as
179 189 "modified", even if none of mode, size and timestamp of it isn't
180 190 changed on the filesystem (see also issue4583).
181 191
182 192 $ echo 321 > e
183 193 $ hg diff --git
184 194 diff --git a/e b/e
185 195 --- a/e
186 196 +++ b/e
187 197 @@ -1,1 +1,1 @@
188 198 -123
189 199 +321
190 200 $ hg commit -m 'ambiguity from size'
191 201
192 202 $ cat e
193 203 321
194 204 $ touch -t 200001010000 e
195 205 $ hg debugrebuildstate
196 206
197 207 $ cat >> .hg/hgrc <<EOF
198 208 > [fakedirstatewritetime]
199 209 > # emulate invoking dirstate.write() via repo.status()
200 210 > # at 2000-01-01 00:00
201 211 > fakenow = 200001010000
202 212 >
203 213 > [extensions]
204 214 > fakedirstatewritetime = $TESTDIR/fakedirstatewritetime.py
205 215 > EOF
206 216 $ hg revert -r 0 e
207 217 $ cat >> .hg/hgrc <<EOF
208 218 > [extensions]
209 219 > fakedirstatewritetime = !
210 220 > EOF
211 221
212 222 $ cat e
213 223 123
214 224 $ touch -t 200001010000 e
215 225 $ hg status -A e
216 226 M e
217 227
218 228 $ cd ..
219 229
220 230
221 231 Issue241: update and revert produces inconsistent repositories
222 232 --------------------------------------------------------------
223 233
224 234 $ hg init a
225 235 $ cd a
226 236 $ echo a >> a
227 237 $ hg commit -A -d '1 0' -m a
228 238 adding a
229 239 $ echo a >> a
230 240 $ hg commit -d '2 0' -m a
231 241 $ hg update 0
232 242 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
233 243 $ mkdir b
234 244 $ echo b > b/b
235 245
236 246 call `hg revert` with no file specified
237 247 ---------------------------------------
238 248
239 249 $ hg revert -rtip
240 250 abort: no files or directories specified
241 251 (use --all to revert all files, or 'hg update 1' to update)
242 252 [255]
243 253
244 254 call `hg revert` with -I
245 255 ---------------------------
246 256
247 257 $ echo a >> a
248 258 $ hg revert -I a
249 259 reverting a
250 260
251 261 call `hg revert` with -X
252 262 ---------------------------
253 263
254 264 $ echo a >> a
255 265 $ hg revert -X d
256 266 reverting a
257 267
258 268 call `hg revert` with --all
259 269 ---------------------------
260 270
261 271 $ hg revert --all -rtip
262 272 reverting a
263 273 $ rm *.orig
264 274
265 275 Issue332: confusing message when reverting directory
266 276 ----------------------------------------------------
267 277
268 278 $ hg ci -A -m b
269 279 adding b/b
270 280 created new head
271 281 $ echo foobar > b/b
272 282 $ mkdir newdir
273 283 $ echo foo > newdir/newfile
274 284 $ hg add newdir/newfile
275 285 $ hg revert b newdir
276 286 reverting b/b (glob)
277 287 forgetting newdir/newfile (glob)
278 288 $ echo foobar > b/b
279 289 $ hg revert .
280 290 reverting b/b (glob)
281 291
282 292
283 293 reverting a rename target should revert the source
284 294 --------------------------------------------------
285 295
286 296 $ hg mv a newa
287 297 $ hg revert newa
288 298 $ hg st a newa
289 299 ? newa
290 300
291 301 Also true for move overwriting an existing file
292 302
293 303 $ hg mv --force a b/b
294 304 $ hg revert b/b
295 305 $ hg status a b/b
296 306
297 307 $ cd ..
298 308
299 309 $ hg init ignored
300 310 $ cd ignored
301 311 $ echo '^ignored$' > .hgignore
302 312 $ echo '^ignoreddir$' >> .hgignore
303 313 $ echo '^removed$' >> .hgignore
304 314
305 315 $ mkdir ignoreddir
306 316 $ touch ignoreddir/file
307 317 $ touch ignoreddir/removed
308 318 $ touch ignored
309 319 $ touch removed
310 320
311 321 4 ignored files (we will add/commit everything)
312 322
313 323 $ hg st -A -X .hgignore
314 324 I ignored
315 325 I ignoreddir/file
316 326 I ignoreddir/removed
317 327 I removed
318 328 $ hg ci -qAm 'add files' ignored ignoreddir/file ignoreddir/removed removed
319 329
320 330 $ echo >> ignored
321 331 $ echo >> ignoreddir/file
322 332 $ hg rm removed ignoreddir/removed
323 333
324 334 should revert ignored* and undelete *removed
325 335 --------------------------------------------
326 336
327 337 $ hg revert -a --no-backup
328 338 reverting ignored
329 339 reverting ignoreddir/file (glob)
330 340 undeleting ignoreddir/removed (glob)
331 341 undeleting removed
332 342 $ hg st -mardi
333 343
334 344 $ hg up -qC
335 345 $ echo >> ignored
336 346 $ hg rm removed
337 347
338 348 should silently revert the named files
339 349 --------------------------------------
340 350
341 351 $ hg revert --no-backup ignored removed
342 352 $ hg st -mardi
343 353
344 354 Reverting copy (issue3920)
345 355 --------------------------
346 356
347 357 someone set up us the copies
348 358
349 359 $ rm .hgignore
350 360 $ hg update -C
351 361 0 files updated, 0 files merged, 0 files removed, 0 files unresolved
352 362 $ hg mv ignored allyour
353 363 $ hg copy removed base
354 364 $ hg commit -m rename
355 365
356 366 copies and renames, you have no chance to survive make your time (issue3920)
357 367
358 368 $ hg update '.^'
359 369 1 files updated, 0 files merged, 2 files removed, 0 files unresolved
360 370 $ hg revert -rtip -a
361 371 adding allyour
362 372 adding base
363 373 removing ignored
364 374 $ hg status -C
365 375 A allyour
366 376 ignored
367 377 A base
368 378 removed
369 379 R ignored
370 380
371 381 Test revert of a file added by one side of the merge
372 382 ====================================================
373 383
374 384 remove any pending change
375 385
376 386 $ hg revert --all
377 387 forgetting allyour
378 388 forgetting base
379 389 undeleting ignored
380 390 $ hg purge --all --config extensions.purge=
381 391
382 392 Adds a new commit
383 393
384 394 $ echo foo > newadd
385 395 $ hg add newadd
386 396 $ hg commit -m 'other adds'
387 397 created new head
388 398
389 399
390 400 merge it with the other head
391 401
392 402 $ hg merge # merge 1 into 2
393 403 2 files updated, 0 files merged, 1 files removed, 0 files unresolved
394 404 (branch merge, don't forget to commit)
395 405 $ hg summary
396 406 parent: 2:b8ec310b2d4e tip
397 407 other adds
398 408 parent: 1:f6180deb8fbe
399 409 rename
400 410 branch: default
401 411 commit: 2 modified, 1 removed (merge)
402 412 update: (current)
403 413 phases: 3 draft
404 414
405 415 clarifies who added what
406 416
407 417 $ hg status
408 418 M allyour
409 419 M base
410 420 R ignored
411 421 $ hg status --change 'p1()'
412 422 A newadd
413 423 $ hg status --change 'p2()'
414 424 A allyour
415 425 A base
416 426 R ignored
417 427
418 428 revert file added by p1() to p1() state
419 429 -----------------------------------------
420 430
421 431 $ hg revert -r 'p1()' 'glob:newad?'
422 432 $ hg status
423 433 M allyour
424 434 M base
425 435 R ignored
426 436
427 437 revert file added by p1() to p2() state
428 438 ------------------------------------------
429 439
430 440 $ hg revert -r 'p2()' 'glob:newad?'
431 441 removing newadd
432 442 $ hg status
433 443 M allyour
434 444 M base
435 445 R ignored
436 446 R newadd
437 447
438 448 revert file added by p2() to p2() state
439 449 ------------------------------------------
440 450
441 451 $ hg revert -r 'p2()' 'glob:allyou?'
442 452 $ hg status
443 453 M allyour
444 454 M base
445 455 R ignored
446 456 R newadd
447 457
448 458 revert file added by p2() to p1() state
449 459 ------------------------------------------
450 460
451 461 $ hg revert -r 'p1()' 'glob:allyou?'
452 462 removing allyour
453 463 $ hg status
454 464 M base
455 465 R allyour
456 466 R ignored
457 467 R newadd
458 468
459 469 Systematic behavior validation of most possible cases
460 470 =====================================================
461 471
462 472 This section tests most of the possible combinations of revision states and
463 473 working directory states. The number of possible cases is significant but they
464 474 but they all have a slightly different handling. So this section commits to
465 475 and testing all of them to allow safe refactoring of the revert code.
466 476
467 477 A python script is used to generate a file history for each combination of
468 478 states, on one side the content (or lack thereof) in two revisions, and
469 479 on the other side, the content and "tracked-ness" of the working directory. The
470 480 three states generated are:
471 481
472 482 - a "base" revision
473 483 - a "parent" revision
474 484 - the working directory (based on "parent")
475 485
476 486 The files generated have names of the form:
477 487
478 488 <rev1-content>_<rev2-content>_<working-copy-content>-<tracked-ness>
479 489
480 490 All known states are not tested yet. See inline documentation for details.
481 491 Special cases from merge and rename are not tested by this section.
482 492
483 493 Write the python script to disk
484 494 -------------------------------
485 495
486 496 check list of planned files
487 497
488 498 $ python $TESTDIR/generate-working-copy-states.py filelist 2
489 499 content1_content1_content1-tracked
490 500 content1_content1_content1-untracked
491 501 content1_content1_content3-tracked
492 502 content1_content1_content3-untracked
493 503 content1_content1_missing-tracked
494 504 content1_content1_missing-untracked
495 505 content1_content2_content1-tracked
496 506 content1_content2_content1-untracked
497 507 content1_content2_content2-tracked
498 508 content1_content2_content2-untracked
499 509 content1_content2_content3-tracked
500 510 content1_content2_content3-untracked
501 511 content1_content2_missing-tracked
502 512 content1_content2_missing-untracked
503 513 content1_missing_content1-tracked
504 514 content1_missing_content1-untracked
505 515 content1_missing_content3-tracked
506 516 content1_missing_content3-untracked
507 517 content1_missing_missing-tracked
508 518 content1_missing_missing-untracked
509 519 missing_content2_content2-tracked
510 520 missing_content2_content2-untracked
511 521 missing_content2_content3-tracked
512 522 missing_content2_content3-untracked
513 523 missing_content2_missing-tracked
514 524 missing_content2_missing-untracked
515 525 missing_missing_content3-tracked
516 526 missing_missing_content3-untracked
517 527 missing_missing_missing-tracked
518 528 missing_missing_missing-untracked
519 529
520 530 Script to make a simple text version of the content
521 531 ---------------------------------------------------
522 532
523 533 $ cat << EOF >> dircontent.py
524 534 > # generate a simple text view of the directory for easy comparison
525 535 > import os
526 536 > files = os.listdir('.')
527 537 > files.sort()
528 538 > for filename in files:
529 539 > if os.path.isdir(filename):
530 540 > continue
531 541 > content = open(filename).read()
532 542 > print '%-6s %s' % (content.strip(), filename)
533 543 > EOF
534 544
535 545 Generate appropriate repo state
536 546 -------------------------------
537 547
538 548 $ hg init revert-ref
539 549 $ cd revert-ref
540 550
541 551 Generate base changeset
542 552
543 553 $ python $TESTDIR/generate-working-copy-states.py state 2 1
544 554 $ hg addremove --similarity 0
545 555 adding content1_content1_content1-tracked
546 556 adding content1_content1_content1-untracked
547 557 adding content1_content1_content3-tracked
548 558 adding content1_content1_content3-untracked
549 559 adding content1_content1_missing-tracked
550 560 adding content1_content1_missing-untracked
551 561 adding content1_content2_content1-tracked
552 562 adding content1_content2_content1-untracked
553 563 adding content1_content2_content2-tracked
554 564 adding content1_content2_content2-untracked
555 565 adding content1_content2_content3-tracked
556 566 adding content1_content2_content3-untracked
557 567 adding content1_content2_missing-tracked
558 568 adding content1_content2_missing-untracked
559 569 adding content1_missing_content1-tracked
560 570 adding content1_missing_content1-untracked
561 571 adding content1_missing_content3-tracked
562 572 adding content1_missing_content3-untracked
563 573 adding content1_missing_missing-tracked
564 574 adding content1_missing_missing-untracked
565 575 $ hg status
566 576 A content1_content1_content1-tracked
567 577 A content1_content1_content1-untracked
568 578 A content1_content1_content3-tracked
569 579 A content1_content1_content3-untracked
570 580 A content1_content1_missing-tracked
571 581 A content1_content1_missing-untracked
572 582 A content1_content2_content1-tracked
573 583 A content1_content2_content1-untracked
574 584 A content1_content2_content2-tracked
575 585 A content1_content2_content2-untracked
576 586 A content1_content2_content3-tracked
577 587 A content1_content2_content3-untracked
578 588 A content1_content2_missing-tracked
579 589 A content1_content2_missing-untracked
580 590 A content1_missing_content1-tracked
581 591 A content1_missing_content1-untracked
582 592 A content1_missing_content3-tracked
583 593 A content1_missing_content3-untracked
584 594 A content1_missing_missing-tracked
585 595 A content1_missing_missing-untracked
586 596 $ hg commit -m 'base'
587 597
588 598 (create a simple text version of the content)
589 599
590 600 $ python ../dircontent.py > ../content-base.txt
591 601 $ cat ../content-base.txt
592 602 content1 content1_content1_content1-tracked
593 603 content1 content1_content1_content1-untracked
594 604 content1 content1_content1_content3-tracked
595 605 content1 content1_content1_content3-untracked
596 606 content1 content1_content1_missing-tracked
597 607 content1 content1_content1_missing-untracked
598 608 content1 content1_content2_content1-tracked
599 609 content1 content1_content2_content1-untracked
600 610 content1 content1_content2_content2-tracked
601 611 content1 content1_content2_content2-untracked
602 612 content1 content1_content2_content3-tracked
603 613 content1 content1_content2_content3-untracked
604 614 content1 content1_content2_missing-tracked
605 615 content1 content1_content2_missing-untracked
606 616 content1 content1_missing_content1-tracked
607 617 content1 content1_missing_content1-untracked
608 618 content1 content1_missing_content3-tracked
609 619 content1 content1_missing_content3-untracked
610 620 content1 content1_missing_missing-tracked
611 621 content1 content1_missing_missing-untracked
612 622
613 623 Create parent changeset
614 624
615 625 $ python $TESTDIR/generate-working-copy-states.py state 2 2
616 626 $ hg addremove --similarity 0
617 627 removing content1_missing_content1-tracked
618 628 removing content1_missing_content1-untracked
619 629 removing content1_missing_content3-tracked
620 630 removing content1_missing_content3-untracked
621 631 removing content1_missing_missing-tracked
622 632 removing content1_missing_missing-untracked
623 633 adding missing_content2_content2-tracked
624 634 adding missing_content2_content2-untracked
625 635 adding missing_content2_content3-tracked
626 636 adding missing_content2_content3-untracked
627 637 adding missing_content2_missing-tracked
628 638 adding missing_content2_missing-untracked
629 639 $ hg status
630 640 M content1_content2_content1-tracked
631 641 M content1_content2_content1-untracked
632 642 M content1_content2_content2-tracked
633 643 M content1_content2_content2-untracked
634 644 M content1_content2_content3-tracked
635 645 M content1_content2_content3-untracked
636 646 M content1_content2_missing-tracked
637 647 M content1_content2_missing-untracked
638 648 A missing_content2_content2-tracked
639 649 A missing_content2_content2-untracked
640 650 A missing_content2_content3-tracked
641 651 A missing_content2_content3-untracked
642 652 A missing_content2_missing-tracked
643 653 A missing_content2_missing-untracked
644 654 R content1_missing_content1-tracked
645 655 R content1_missing_content1-untracked
646 656 R content1_missing_content3-tracked
647 657 R content1_missing_content3-untracked
648 658 R content1_missing_missing-tracked
649 659 R content1_missing_missing-untracked
650 660 $ hg commit -m 'parent'
651 661
652 662 (create a simple text version of the content)
653 663
654 664 $ python ../dircontent.py > ../content-parent.txt
655 665 $ cat ../content-parent.txt
656 666 content1 content1_content1_content1-tracked
657 667 content1 content1_content1_content1-untracked
658 668 content1 content1_content1_content3-tracked
659 669 content1 content1_content1_content3-untracked
660 670 content1 content1_content1_missing-tracked
661 671 content1 content1_content1_missing-untracked
662 672 content2 content1_content2_content1-tracked
663 673 content2 content1_content2_content1-untracked
664 674 content2 content1_content2_content2-tracked
665 675 content2 content1_content2_content2-untracked
666 676 content2 content1_content2_content3-tracked
667 677 content2 content1_content2_content3-untracked
668 678 content2 content1_content2_missing-tracked
669 679 content2 content1_content2_missing-untracked
670 680 content2 missing_content2_content2-tracked
671 681 content2 missing_content2_content2-untracked
672 682 content2 missing_content2_content3-tracked
673 683 content2 missing_content2_content3-untracked
674 684 content2 missing_content2_missing-tracked
675 685 content2 missing_content2_missing-untracked
676 686
677 687 Setup working directory
678 688
679 689 $ python $TESTDIR/generate-working-copy-states.py state 2 wc
680 690 $ hg addremove --similarity 0
681 691 adding content1_missing_content1-tracked
682 692 adding content1_missing_content1-untracked
683 693 adding content1_missing_content3-tracked
684 694 adding content1_missing_content3-untracked
685 695 adding content1_missing_missing-tracked
686 696 adding content1_missing_missing-untracked
687 697 adding missing_missing_content3-tracked
688 698 adding missing_missing_content3-untracked
689 699 adding missing_missing_missing-tracked
690 700 adding missing_missing_missing-untracked
691 701 $ hg forget *_*_*-untracked
692 702 $ rm *_*_missing-*
693 703 $ hg status
694 704 M content1_content1_content3-tracked
695 705 M content1_content2_content1-tracked
696 706 M content1_content2_content3-tracked
697 707 M missing_content2_content3-tracked
698 708 A content1_missing_content1-tracked
699 709 A content1_missing_content3-tracked
700 710 A missing_missing_content3-tracked
701 711 R content1_content1_content1-untracked
702 712 R content1_content1_content3-untracked
703 713 R content1_content1_missing-untracked
704 714 R content1_content2_content1-untracked
705 715 R content1_content2_content2-untracked
706 716 R content1_content2_content3-untracked
707 717 R content1_content2_missing-untracked
708 718 R missing_content2_content2-untracked
709 719 R missing_content2_content3-untracked
710 720 R missing_content2_missing-untracked
711 721 ! content1_content1_missing-tracked
712 722 ! content1_content2_missing-tracked
713 723 ! content1_missing_missing-tracked
714 724 ! missing_content2_missing-tracked
715 725 ! missing_missing_missing-tracked
716 726 ? content1_missing_content1-untracked
717 727 ? content1_missing_content3-untracked
718 728 ? missing_missing_content3-untracked
719 729
720 730 $ hg status --rev 'desc("base")'
721 731 M content1_content1_content3-tracked
722 732 M content1_content2_content2-tracked
723 733 M content1_content2_content3-tracked
724 734 M content1_missing_content3-tracked
725 735 A missing_content2_content2-tracked
726 736 A missing_content2_content3-tracked
727 737 A missing_missing_content3-tracked
728 738 R content1_content1_content1-untracked
729 739 R content1_content1_content3-untracked
730 740 R content1_content1_missing-untracked
731 741 R content1_content2_content1-untracked
732 742 R content1_content2_content2-untracked
733 743 R content1_content2_content3-untracked
734 744 R content1_content2_missing-untracked
735 745 R content1_missing_content1-untracked
736 746 R content1_missing_content3-untracked
737 747 R content1_missing_missing-untracked
738 748 ! content1_content1_missing-tracked
739 749 ! content1_content2_missing-tracked
740 750 ! content1_missing_missing-tracked
741 751 ! missing_content2_missing-tracked
742 752 ! missing_missing_missing-tracked
743 753 ? missing_missing_content3-untracked
744 754
745 755 (create a simple text version of the content)
746 756
747 757 $ python ../dircontent.py > ../content-wc.txt
748 758 $ cat ../content-wc.txt
749 759 content1 content1_content1_content1-tracked
750 760 content1 content1_content1_content1-untracked
751 761 content3 content1_content1_content3-tracked
752 762 content3 content1_content1_content3-untracked
753 763 content1 content1_content2_content1-tracked
754 764 content1 content1_content2_content1-untracked
755 765 content2 content1_content2_content2-tracked
756 766 content2 content1_content2_content2-untracked
757 767 content3 content1_content2_content3-tracked
758 768 content3 content1_content2_content3-untracked
759 769 content1 content1_missing_content1-tracked
760 770 content1 content1_missing_content1-untracked
761 771 content3 content1_missing_content3-tracked
762 772 content3 content1_missing_content3-untracked
763 773 content2 missing_content2_content2-tracked
764 774 content2 missing_content2_content2-untracked
765 775 content3 missing_content2_content3-tracked
766 776 content3 missing_content2_content3-untracked
767 777 content3 missing_missing_content3-tracked
768 778 content3 missing_missing_content3-untracked
769 779
770 780 $ cd ..
771 781
772 782 Test revert --all to parent content
773 783 -----------------------------------
774 784
775 785 (setup from reference repo)
776 786
777 787 $ cp -r revert-ref revert-parent-all
778 788 $ cd revert-parent-all
779 789
780 790 check revert output
781 791
782 792 $ hg revert --all
783 793 undeleting content1_content1_content1-untracked
784 794 reverting content1_content1_content3-tracked
785 795 undeleting content1_content1_content3-untracked
786 796 reverting content1_content1_missing-tracked
787 797 undeleting content1_content1_missing-untracked
788 798 reverting content1_content2_content1-tracked
789 799 undeleting content1_content2_content1-untracked
790 800 undeleting content1_content2_content2-untracked
791 801 reverting content1_content2_content3-tracked
792 802 undeleting content1_content2_content3-untracked
793 803 reverting content1_content2_missing-tracked
794 804 undeleting content1_content2_missing-untracked
795 805 forgetting content1_missing_content1-tracked
796 806 forgetting content1_missing_content3-tracked
797 807 forgetting content1_missing_missing-tracked
798 808 undeleting missing_content2_content2-untracked
799 809 reverting missing_content2_content3-tracked
800 810 undeleting missing_content2_content3-untracked
801 811 reverting missing_content2_missing-tracked
802 812 undeleting missing_content2_missing-untracked
803 813 forgetting missing_missing_content3-tracked
804 814 forgetting missing_missing_missing-tracked
805 815
806 816 Compare resulting directory with revert target.
807 817
808 818 The diff is filtered to include change only. The only difference should be
809 819 additional `.orig` backup file when applicable.
810 820
811 821 $ python ../dircontent.py > ../content-parent-all.txt
812 822 $ cd ..
813 823 $ diff -U 0 -- content-parent.txt content-parent-all.txt | grep _
814 824 +content3 content1_content1_content3-tracked.orig
815 825 +content3 content1_content1_content3-untracked.orig
816 826 +content1 content1_content2_content1-tracked.orig
817 827 +content1 content1_content2_content1-untracked.orig
818 828 +content3 content1_content2_content3-tracked.orig
819 829 +content3 content1_content2_content3-untracked.orig
820 830 +content1 content1_missing_content1-tracked
821 831 +content1 content1_missing_content1-untracked
822 832 +content3 content1_missing_content3-tracked
823 833 +content3 content1_missing_content3-untracked
824 834 +content3 missing_content2_content3-tracked.orig
825 835 +content3 missing_content2_content3-untracked.orig
826 836 +content3 missing_missing_content3-tracked
827 837 +content3 missing_missing_content3-untracked
828 838
829 839 Test revert --all to "base" content
830 840 -----------------------------------
831 841
832 842 (setup from reference repo)
833 843
834 844 $ cp -r revert-ref revert-base-all
835 845 $ cd revert-base-all
836 846
837 847 check revert output
838 848
839 849 $ hg revert --all --rev 'desc(base)'
840 850 undeleting content1_content1_content1-untracked
841 851 reverting content1_content1_content3-tracked
842 852 undeleting content1_content1_content3-untracked
843 853 reverting content1_content1_missing-tracked
844 854 undeleting content1_content1_missing-untracked
845 855 undeleting content1_content2_content1-untracked
846 856 reverting content1_content2_content2-tracked
847 857 undeleting content1_content2_content2-untracked
848 858 reverting content1_content2_content3-tracked
849 859 undeleting content1_content2_content3-untracked
850 860 reverting content1_content2_missing-tracked
851 861 undeleting content1_content2_missing-untracked
852 862 adding content1_missing_content1-untracked
853 863 reverting content1_missing_content3-tracked
854 864 adding content1_missing_content3-untracked
855 865 reverting content1_missing_missing-tracked
856 866 adding content1_missing_missing-untracked
857 867 removing missing_content2_content2-tracked
858 868 removing missing_content2_content3-tracked
859 869 removing missing_content2_missing-tracked
860 870 forgetting missing_missing_content3-tracked
861 871 forgetting missing_missing_missing-tracked
862 872
863 873 Compare resulting directory with revert target.
864 874
865 875 The diff is filtered to include change only. The only difference should be
866 876 additional `.orig` backup file when applicable.
867 877
868 878 $ python ../dircontent.py > ../content-base-all.txt
869 879 $ cd ..
870 880 $ diff -U 0 -- content-base.txt content-base-all.txt | grep _
871 881 +content3 content1_content1_content3-tracked.orig
872 882 +content3 content1_content1_content3-untracked.orig
873 883 +content2 content1_content2_content2-untracked.orig
874 884 +content3 content1_content2_content3-tracked.orig
875 885 +content3 content1_content2_content3-untracked.orig
876 886 +content3 content1_missing_content3-tracked.orig
877 887 +content3 content1_missing_content3-untracked.orig
878 888 +content2 missing_content2_content2-untracked
879 889 +content3 missing_content2_content3-tracked.orig
880 890 +content3 missing_content2_content3-untracked
881 891 +content3 missing_missing_content3-tracked
882 892 +content3 missing_missing_content3-untracked
883 893
884 894 Test revert to parent content with explicit file name
885 895 -----------------------------------------------------
886 896
887 897 (setup from reference repo)
888 898
889 899 $ cp -r revert-ref revert-parent-explicit
890 900 $ cd revert-parent-explicit
891 901
892 902 revert all files individually and check the output
893 903 (output is expected to be different than in the --all case)
894 904
895 905 $ for file in `python $TESTDIR/generate-working-copy-states.py filelist 2`; do
896 906 > echo '### revert for:' $file;
897 907 > hg revert $file;
898 908 > echo
899 909 > done
900 910 ### revert for: content1_content1_content1-tracked
901 911 no changes needed to content1_content1_content1-tracked
902 912
903 913 ### revert for: content1_content1_content1-untracked
904 914
905 915 ### revert for: content1_content1_content3-tracked
906 916
907 917 ### revert for: content1_content1_content3-untracked
908 918
909 919 ### revert for: content1_content1_missing-tracked
910 920
911 921 ### revert for: content1_content1_missing-untracked
912 922
913 923 ### revert for: content1_content2_content1-tracked
914 924
915 925 ### revert for: content1_content2_content1-untracked
916 926
917 927 ### revert for: content1_content2_content2-tracked
918 928 no changes needed to content1_content2_content2-tracked
919 929
920 930 ### revert for: content1_content2_content2-untracked
921 931
922 932 ### revert for: content1_content2_content3-tracked
923 933
924 934 ### revert for: content1_content2_content3-untracked
925 935
926 936 ### revert for: content1_content2_missing-tracked
927 937
928 938 ### revert for: content1_content2_missing-untracked
929 939
930 940 ### revert for: content1_missing_content1-tracked
931 941
932 942 ### revert for: content1_missing_content1-untracked
933 943 file not managed: content1_missing_content1-untracked
934 944
935 945 ### revert for: content1_missing_content3-tracked
936 946
937 947 ### revert for: content1_missing_content3-untracked
938 948 file not managed: content1_missing_content3-untracked
939 949
940 950 ### revert for: content1_missing_missing-tracked
941 951
942 952 ### revert for: content1_missing_missing-untracked
943 953 content1_missing_missing-untracked: no such file in rev * (glob)
944 954
945 955 ### revert for: missing_content2_content2-tracked
946 956 no changes needed to missing_content2_content2-tracked
947 957
948 958 ### revert for: missing_content2_content2-untracked
949 959
950 960 ### revert for: missing_content2_content3-tracked
951 961
952 962 ### revert for: missing_content2_content3-untracked
953 963
954 964 ### revert for: missing_content2_missing-tracked
955 965
956 966 ### revert for: missing_content2_missing-untracked
957 967
958 968 ### revert for: missing_missing_content3-tracked
959 969
960 970 ### revert for: missing_missing_content3-untracked
961 971 file not managed: missing_missing_content3-untracked
962 972
963 973 ### revert for: missing_missing_missing-tracked
964 974
965 975 ### revert for: missing_missing_missing-untracked
966 976 missing_missing_missing-untracked: no such file in rev * (glob)
967 977
968 978
969 979 check resulting directory against the --all run
970 980 (There should be no difference)
971 981
972 982 $ python ../dircontent.py > ../content-parent-explicit.txt
973 983 $ cd ..
974 984 $ diff -U 0 -- content-parent-all.txt content-parent-explicit.txt | grep _
975 985 [1]
976 986
977 987 Test revert to "base" content with explicit file name
978 988 -----------------------------------------------------
979 989
980 990 (setup from reference repo)
981 991
982 992 $ cp -r revert-ref revert-base-explicit
983 993 $ cd revert-base-explicit
984 994
985 995 revert all files individually and check the output
986 996 (output is expected to be different than in the --all case)
987 997
988 998 $ for file in `python $TESTDIR/generate-working-copy-states.py filelist 2`; do
989 999 > echo '### revert for:' $file;
990 1000 > hg revert $file --rev 'desc(base)';
991 1001 > echo
992 1002 > done
993 1003 ### revert for: content1_content1_content1-tracked
994 1004 no changes needed to content1_content1_content1-tracked
995 1005
996 1006 ### revert for: content1_content1_content1-untracked
997 1007
998 1008 ### revert for: content1_content1_content3-tracked
999 1009
1000 1010 ### revert for: content1_content1_content3-untracked
1001 1011
1002 1012 ### revert for: content1_content1_missing-tracked
1003 1013
1004 1014 ### revert for: content1_content1_missing-untracked
1005 1015
1006 1016 ### revert for: content1_content2_content1-tracked
1007 1017 no changes needed to content1_content2_content1-tracked
1008 1018
1009 1019 ### revert for: content1_content2_content1-untracked
1010 1020
1011 1021 ### revert for: content1_content2_content2-tracked
1012 1022
1013 1023 ### revert for: content1_content2_content2-untracked
1014 1024
1015 1025 ### revert for: content1_content2_content3-tracked
1016 1026
1017 1027 ### revert for: content1_content2_content3-untracked
1018 1028
1019 1029 ### revert for: content1_content2_missing-tracked
1020 1030
1021 1031 ### revert for: content1_content2_missing-untracked
1022 1032
1023 1033 ### revert for: content1_missing_content1-tracked
1024 1034 no changes needed to content1_missing_content1-tracked
1025 1035
1026 1036 ### revert for: content1_missing_content1-untracked
1027 1037
1028 1038 ### revert for: content1_missing_content3-tracked
1029 1039
1030 1040 ### revert for: content1_missing_content3-untracked
1031 1041
1032 1042 ### revert for: content1_missing_missing-tracked
1033 1043
1034 1044 ### revert for: content1_missing_missing-untracked
1035 1045
1036 1046 ### revert for: missing_content2_content2-tracked
1037 1047
1038 1048 ### revert for: missing_content2_content2-untracked
1039 1049 no changes needed to missing_content2_content2-untracked
1040 1050
1041 1051 ### revert for: missing_content2_content3-tracked
1042 1052
1043 1053 ### revert for: missing_content2_content3-untracked
1044 1054 no changes needed to missing_content2_content3-untracked
1045 1055
1046 1056 ### revert for: missing_content2_missing-tracked
1047 1057
1048 1058 ### revert for: missing_content2_missing-untracked
1049 1059 no changes needed to missing_content2_missing-untracked
1050 1060
1051 1061 ### revert for: missing_missing_content3-tracked
1052 1062
1053 1063 ### revert for: missing_missing_content3-untracked
1054 1064 file not managed: missing_missing_content3-untracked
1055 1065
1056 1066 ### revert for: missing_missing_missing-tracked
1057 1067
1058 1068 ### revert for: missing_missing_missing-untracked
1059 1069 missing_missing_missing-untracked: no such file in rev * (glob)
1060 1070
1061 1071
1062 1072 check resulting directory against the --all run
1063 1073 (There should be no difference)
1064 1074
1065 1075 $ python ../dircontent.py > ../content-base-explicit.txt
1066 1076 $ cd ..
1067 1077 $ diff -U 0 -- content-base-all.txt content-base-explicit.txt | grep _
1068 1078 [1]
General Comments 0
You need to be logged in to leave comments. Login now