##// END OF EJS Templates
copyfile: preserve stat info (mtime, etc.) when doing copies/renames...
Kyle Lippincott -
r37106:08890706 default
parent child Browse files
Show More
@@ -1,1493 +1,1493 b''
1 1 # Copyright 2009-2010 Gregory P. Ward
2 2 # Copyright 2009-2010 Intelerad Medical Systems Incorporated
3 3 # Copyright 2010-2011 Fog Creek Software
4 4 # Copyright 2010-2011 Unity Technologies
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 8
9 9 '''Overridden Mercurial commands and functions for the largefiles extension'''
10 10 from __future__ import absolute_import
11 11
12 12 import copy
13 13 import os
14 14
15 15 from mercurial.i18n import _
16 16
17 17 from mercurial import (
18 18 archival,
19 19 cmdutil,
20 20 error,
21 21 hg,
22 22 logcmdutil,
23 23 match as matchmod,
24 24 pathutil,
25 25 pycompat,
26 26 registrar,
27 27 scmutil,
28 28 smartset,
29 29 util,
30 30 )
31 31
32 32 from . import (
33 33 lfcommands,
34 34 lfutil,
35 35 storefactory,
36 36 )
37 37
38 38 # -- Utility functions: commonly/repeatedly needed functionality ---------------
39 39
40 40 def composelargefilematcher(match, manifest):
41 41 '''create a matcher that matches only the largefiles in the original
42 42 matcher'''
43 43 m = copy.copy(match)
44 44 lfile = lambda f: lfutil.standin(f) in manifest
45 45 m._files = [lf for lf in m._files if lfile(lf)]
46 46 m._fileset = set(m._files)
47 47 m.always = lambda: False
48 48 origmatchfn = m.matchfn
49 49 m.matchfn = lambda f: lfile(f) and origmatchfn(f)
50 50 return m
51 51
52 52 def composenormalfilematcher(match, manifest, exclude=None):
53 53 excluded = set()
54 54 if exclude is not None:
55 55 excluded.update(exclude)
56 56
57 57 m = copy.copy(match)
58 58 notlfile = lambda f: not (lfutil.isstandin(f) or lfutil.standin(f) in
59 59 manifest or f in excluded)
60 60 m._files = [lf for lf in m._files if notlfile(lf)]
61 61 m._fileset = set(m._files)
62 62 m.always = lambda: False
63 63 origmatchfn = m.matchfn
64 64 m.matchfn = lambda f: notlfile(f) and origmatchfn(f)
65 65 return m
66 66
67 67 def installnormalfilesmatchfn(manifest):
68 68 '''installmatchfn with a matchfn that ignores all largefiles'''
69 69 def overridematch(ctx, pats=(), opts=None, globbed=False,
70 70 default='relpath', badfn=None):
71 71 if opts is None:
72 72 opts = {}
73 73 match = oldmatch(ctx, pats, opts, globbed, default, badfn=badfn)
74 74 return composenormalfilematcher(match, manifest)
75 75 oldmatch = installmatchfn(overridematch)
76 76
77 77 def installmatchfn(f):
78 78 '''monkey patch the scmutil module with a custom match function.
79 79 Warning: it is monkey patching the _module_ on runtime! Not thread safe!'''
80 80 oldmatch = scmutil.match
81 81 setattr(f, 'oldmatch', oldmatch)
82 82 scmutil.match = f
83 83 return oldmatch
84 84
85 85 def restorematchfn():
86 86 '''restores scmutil.match to what it was before installmatchfn
87 87 was called. no-op if scmutil.match is its original function.
88 88
89 89 Note that n calls to installmatchfn will require n calls to
90 90 restore the original matchfn.'''
91 91 scmutil.match = getattr(scmutil.match, 'oldmatch')
92 92
93 93 def installmatchandpatsfn(f):
94 94 oldmatchandpats = scmutil.matchandpats
95 95 setattr(f, 'oldmatchandpats', oldmatchandpats)
96 96 scmutil.matchandpats = f
97 97 return oldmatchandpats
98 98
99 99 def restorematchandpatsfn():
100 100 '''restores scmutil.matchandpats to what it was before
101 101 installmatchandpatsfn was called. No-op if scmutil.matchandpats
102 102 is its original function.
103 103
104 104 Note that n calls to installmatchandpatsfn will require n calls
105 105 to restore the original matchfn.'''
106 106 scmutil.matchandpats = getattr(scmutil.matchandpats, 'oldmatchandpats',
107 107 scmutil.matchandpats)
108 108
109 109 def addlargefiles(ui, repo, isaddremove, matcher, **opts):
110 110 large = opts.get(r'large')
111 111 lfsize = lfutil.getminsize(
112 112 ui, lfutil.islfilesrepo(repo), opts.get(r'lfsize'))
113 113
114 114 lfmatcher = None
115 115 if lfutil.islfilesrepo(repo):
116 116 lfpats = ui.configlist(lfutil.longname, 'patterns')
117 117 if lfpats:
118 118 lfmatcher = matchmod.match(repo.root, '', list(lfpats))
119 119
120 120 lfnames = []
121 121 m = matcher
122 122
123 123 wctx = repo[None]
124 124 for f in wctx.walk(matchmod.badmatch(m, lambda x, y: None)):
125 125 exact = m.exact(f)
126 126 lfile = lfutil.standin(f) in wctx
127 127 nfile = f in wctx
128 128 exists = lfile or nfile
129 129
130 130 # addremove in core gets fancy with the name, add doesn't
131 131 if isaddremove:
132 132 name = m.uipath(f)
133 133 else:
134 134 name = m.rel(f)
135 135
136 136 # Don't warn the user when they attempt to add a normal tracked file.
137 137 # The normal add code will do that for us.
138 138 if exact and exists:
139 139 if lfile:
140 140 ui.warn(_('%s already a largefile\n') % name)
141 141 continue
142 142
143 143 if (exact or not exists) and not lfutil.isstandin(f):
144 144 # In case the file was removed previously, but not committed
145 145 # (issue3507)
146 146 if not repo.wvfs.exists(f):
147 147 continue
148 148
149 149 abovemin = (lfsize and
150 150 repo.wvfs.lstat(f).st_size >= lfsize * 1024 * 1024)
151 151 if large or abovemin or (lfmatcher and lfmatcher(f)):
152 152 lfnames.append(f)
153 153 if ui.verbose or not exact:
154 154 ui.status(_('adding %s as a largefile\n') % name)
155 155
156 156 bad = []
157 157
158 158 # Need to lock, otherwise there could be a race condition between
159 159 # when standins are created and added to the repo.
160 160 with repo.wlock():
161 161 if not opts.get(r'dry_run'):
162 162 standins = []
163 163 lfdirstate = lfutil.openlfdirstate(ui, repo)
164 164 for f in lfnames:
165 165 standinname = lfutil.standin(f)
166 166 lfutil.writestandin(repo, standinname, hash='',
167 167 executable=lfutil.getexecutable(repo.wjoin(f)))
168 168 standins.append(standinname)
169 169 if lfdirstate[f] == 'r':
170 170 lfdirstate.normallookup(f)
171 171 else:
172 172 lfdirstate.add(f)
173 173 lfdirstate.write()
174 174 bad += [lfutil.splitstandin(f)
175 175 for f in repo[None].add(standins)
176 176 if f in m.files()]
177 177
178 178 added = [f for f in lfnames if f not in bad]
179 179 return added, bad
180 180
181 181 def removelargefiles(ui, repo, isaddremove, matcher, **opts):
182 182 after = opts.get(r'after')
183 183 m = composelargefilematcher(matcher, repo[None].manifest())
184 184 try:
185 185 repo.lfstatus = True
186 186 s = repo.status(match=m, clean=not isaddremove)
187 187 finally:
188 188 repo.lfstatus = False
189 189 manifest = repo[None].manifest()
190 190 modified, added, deleted, clean = [[f for f in list
191 191 if lfutil.standin(f) in manifest]
192 192 for list in (s.modified, s.added,
193 193 s.deleted, s.clean)]
194 194
195 195 def warn(files, msg):
196 196 for f in files:
197 197 ui.warn(msg % m.rel(f))
198 198 return int(len(files) > 0)
199 199
200 200 result = 0
201 201
202 202 if after:
203 203 remove = deleted
204 204 result = warn(modified + added + clean,
205 205 _('not removing %s: file still exists\n'))
206 206 else:
207 207 remove = deleted + clean
208 208 result = warn(modified, _('not removing %s: file is modified (use -f'
209 209 ' to force removal)\n'))
210 210 result = warn(added, _('not removing %s: file has been marked for add'
211 211 ' (use forget to undo)\n')) or result
212 212
213 213 # Need to lock because standin files are deleted then removed from the
214 214 # repository and we could race in-between.
215 215 with repo.wlock():
216 216 lfdirstate = lfutil.openlfdirstate(ui, repo)
217 217 for f in sorted(remove):
218 218 if ui.verbose or not m.exact(f):
219 219 # addremove in core gets fancy with the name, remove doesn't
220 220 if isaddremove:
221 221 name = m.uipath(f)
222 222 else:
223 223 name = m.rel(f)
224 224 ui.status(_('removing %s\n') % name)
225 225
226 226 if not opts.get(r'dry_run'):
227 227 if not after:
228 228 repo.wvfs.unlinkpath(f, ignoremissing=True)
229 229
230 230 if opts.get(r'dry_run'):
231 231 return result
232 232
233 233 remove = [lfutil.standin(f) for f in remove]
234 234 # If this is being called by addremove, let the original addremove
235 235 # function handle this.
236 236 if not isaddremove:
237 237 for f in remove:
238 238 repo.wvfs.unlinkpath(f, ignoremissing=True)
239 239 repo[None].forget(remove)
240 240
241 241 for f in remove:
242 242 lfutil.synclfdirstate(repo, lfdirstate, lfutil.splitstandin(f),
243 243 False)
244 244
245 245 lfdirstate.write()
246 246
247 247 return result
248 248
249 249 # For overriding mercurial.hgweb.webcommands so that largefiles will
250 250 # appear at their right place in the manifests.
251 251 def decodepath(orig, path):
252 252 return lfutil.splitstandin(path) or path
253 253
254 254 # -- Wrappers: modify existing commands --------------------------------
255 255
256 256 def overrideadd(orig, ui, repo, *pats, **opts):
257 257 if opts.get(r'normal') and opts.get(r'large'):
258 258 raise error.Abort(_('--normal cannot be used with --large'))
259 259 return orig(ui, repo, *pats, **opts)
260 260
261 261 def cmdutiladd(orig, ui, repo, matcher, prefix, explicitonly, **opts):
262 262 # The --normal flag short circuits this override
263 263 if opts.get(r'normal'):
264 264 return orig(ui, repo, matcher, prefix, explicitonly, **opts)
265 265
266 266 ladded, lbad = addlargefiles(ui, repo, False, matcher, **opts)
267 267 normalmatcher = composenormalfilematcher(matcher, repo[None].manifest(),
268 268 ladded)
269 269 bad = orig(ui, repo, normalmatcher, prefix, explicitonly, **opts)
270 270
271 271 bad.extend(f for f in lbad)
272 272 return bad
273 273
274 274 def cmdutilremove(orig, ui, repo, matcher, prefix, after, force, subrepos):
275 275 normalmatcher = composenormalfilematcher(matcher, repo[None].manifest())
276 276 result = orig(ui, repo, normalmatcher, prefix, after, force, subrepos)
277 277 return removelargefiles(ui, repo, False, matcher, after=after,
278 278 force=force) or result
279 279
280 280 def overridestatusfn(orig, repo, rev2, **opts):
281 281 try:
282 282 repo._repo.lfstatus = True
283 283 return orig(repo, rev2, **opts)
284 284 finally:
285 285 repo._repo.lfstatus = False
286 286
287 287 def overridestatus(orig, ui, repo, *pats, **opts):
288 288 try:
289 289 repo.lfstatus = True
290 290 return orig(ui, repo, *pats, **opts)
291 291 finally:
292 292 repo.lfstatus = False
293 293
294 294 def overridedirty(orig, repo, ignoreupdate=False, missing=False):
295 295 try:
296 296 repo._repo.lfstatus = True
297 297 return orig(repo, ignoreupdate=ignoreupdate, missing=missing)
298 298 finally:
299 299 repo._repo.lfstatus = False
300 300
301 301 def overridelog(orig, ui, repo, *pats, **opts):
302 302 def overridematchandpats(ctx, pats=(), opts=None, globbed=False,
303 303 default='relpath', badfn=None):
304 304 """Matcher that merges root directory with .hglf, suitable for log.
305 305 It is still possible to match .hglf directly.
306 306 For any listed files run log on the standin too.
307 307 matchfn tries both the given filename and with .hglf stripped.
308 308 """
309 309 if opts is None:
310 310 opts = {}
311 311 matchandpats = oldmatchandpats(ctx, pats, opts, globbed, default,
312 312 badfn=badfn)
313 313 m, p = copy.copy(matchandpats)
314 314
315 315 if m.always():
316 316 # We want to match everything anyway, so there's no benefit trying
317 317 # to add standins.
318 318 return matchandpats
319 319
320 320 pats = set(p)
321 321
322 322 def fixpats(pat, tostandin=lfutil.standin):
323 323 if pat.startswith('set:'):
324 324 return pat
325 325
326 326 kindpat = matchmod._patsplit(pat, None)
327 327
328 328 if kindpat[0] is not None:
329 329 return kindpat[0] + ':' + tostandin(kindpat[1])
330 330 return tostandin(kindpat[1])
331 331
332 332 if m._cwd:
333 333 hglf = lfutil.shortname
334 334 back = util.pconvert(m.rel(hglf)[:-len(hglf)])
335 335
336 336 def tostandin(f):
337 337 # The file may already be a standin, so truncate the back
338 338 # prefix and test before mangling it. This avoids turning
339 339 # 'glob:../.hglf/foo*' into 'glob:../.hglf/../.hglf/foo*'.
340 340 if f.startswith(back) and lfutil.splitstandin(f[len(back):]):
341 341 return f
342 342
343 343 # An absolute path is from outside the repo, so truncate the
344 344 # path to the root before building the standin. Otherwise cwd
345 345 # is somewhere in the repo, relative to root, and needs to be
346 346 # prepended before building the standin.
347 347 if os.path.isabs(m._cwd):
348 348 f = f[len(back):]
349 349 else:
350 350 f = m._cwd + '/' + f
351 351 return back + lfutil.standin(f)
352 352 else:
353 353 def tostandin(f):
354 354 if lfutil.isstandin(f):
355 355 return f
356 356 return lfutil.standin(f)
357 357 pats.update(fixpats(f, tostandin) for f in p)
358 358
359 359 for i in range(0, len(m._files)):
360 360 # Don't add '.hglf' to m.files, since that is already covered by '.'
361 361 if m._files[i] == '.':
362 362 continue
363 363 standin = lfutil.standin(m._files[i])
364 364 # If the "standin" is a directory, append instead of replace to
365 365 # support naming a directory on the command line with only
366 366 # largefiles. The original directory is kept to support normal
367 367 # files.
368 368 if standin in ctx:
369 369 m._files[i] = standin
370 370 elif m._files[i] not in ctx and repo.wvfs.isdir(standin):
371 371 m._files.append(standin)
372 372
373 373 m._fileset = set(m._files)
374 374 m.always = lambda: False
375 375 origmatchfn = m.matchfn
376 376 def lfmatchfn(f):
377 377 lf = lfutil.splitstandin(f)
378 378 if lf is not None and origmatchfn(lf):
379 379 return True
380 380 r = origmatchfn(f)
381 381 return r
382 382 m.matchfn = lfmatchfn
383 383
384 384 ui.debug('updated patterns: %s\n' % ', '.join(sorted(pats)))
385 385 return m, pats
386 386
387 387 # For hg log --patch, the match object is used in two different senses:
388 388 # (1) to determine what revisions should be printed out, and
389 389 # (2) to determine what files to print out diffs for.
390 390 # The magic matchandpats override should be used for case (1) but not for
391 391 # case (2).
392 392 def overridemakefilematcher(repo, pats, opts, badfn=None):
393 393 wctx = repo[None]
394 394 match, pats = oldmatchandpats(wctx, pats, opts, badfn=badfn)
395 395 return lambda ctx: match
396 396
397 397 oldmatchandpats = installmatchandpatsfn(overridematchandpats)
398 398 oldmakefilematcher = logcmdutil._makenofollowfilematcher
399 399 setattr(logcmdutil, '_makenofollowfilematcher', overridemakefilematcher)
400 400
401 401 try:
402 402 return orig(ui, repo, *pats, **opts)
403 403 finally:
404 404 restorematchandpatsfn()
405 405 setattr(logcmdutil, '_makenofollowfilematcher', oldmakefilematcher)
406 406
407 407 def overrideverify(orig, ui, repo, *pats, **opts):
408 408 large = opts.pop(r'large', False)
409 409 all = opts.pop(r'lfa', False)
410 410 contents = opts.pop(r'lfc', False)
411 411
412 412 result = orig(ui, repo, *pats, **opts)
413 413 if large or all or contents:
414 414 result = result or lfcommands.verifylfiles(ui, repo, all, contents)
415 415 return result
416 416
417 417 def overridedebugstate(orig, ui, repo, *pats, **opts):
418 418 large = opts.pop(r'large', False)
419 419 if large:
420 420 class fakerepo(object):
421 421 dirstate = lfutil.openlfdirstate(ui, repo)
422 422 orig(ui, fakerepo, *pats, **opts)
423 423 else:
424 424 orig(ui, repo, *pats, **opts)
425 425
426 426 # Before starting the manifest merge, merge.updates will call
427 427 # _checkunknownfile to check if there are any files in the merged-in
428 428 # changeset that collide with unknown files in the working copy.
429 429 #
430 430 # The largefiles are seen as unknown, so this prevents us from merging
431 431 # in a file 'foo' if we already have a largefile with the same name.
432 432 #
433 433 # The overridden function filters the unknown files by removing any
434 434 # largefiles. This makes the merge proceed and we can then handle this
435 435 # case further in the overridden calculateupdates function below.
436 436 def overridecheckunknownfile(origfn, repo, wctx, mctx, f, f2=None):
437 437 if lfutil.standin(repo.dirstate.normalize(f)) in wctx:
438 438 return False
439 439 return origfn(repo, wctx, mctx, f, f2)
440 440
441 441 # The manifest merge handles conflicts on the manifest level. We want
442 442 # to handle changes in largefile-ness of files at this level too.
443 443 #
444 444 # The strategy is to run the original calculateupdates and then process
445 445 # the action list it outputs. There are two cases we need to deal with:
446 446 #
447 447 # 1. Normal file in p1, largefile in p2. Here the largefile is
448 448 # detected via its standin file, which will enter the working copy
449 449 # with a "get" action. It is not "merge" since the standin is all
450 450 # Mercurial is concerned with at this level -- the link to the
451 451 # existing normal file is not relevant here.
452 452 #
453 453 # 2. Largefile in p1, normal file in p2. Here we get a "merge" action
454 454 # since the largefile will be present in the working copy and
455 455 # different from the normal file in p2. Mercurial therefore
456 456 # triggers a merge action.
457 457 #
458 458 # In both cases, we prompt the user and emit new actions to either
459 459 # remove the standin (if the normal file was kept) or to remove the
460 460 # normal file and get the standin (if the largefile was kept). The
461 461 # default prompt answer is to use the largefile version since it was
462 462 # presumably changed on purpose.
463 463 #
464 464 # Finally, the merge.applyupdates function will then take care of
465 465 # writing the files into the working copy and lfcommands.updatelfiles
466 466 # will update the largefiles.
467 467 def overridecalculateupdates(origfn, repo, p1, p2, pas, branchmerge, force,
468 468 acceptremote, *args, **kwargs):
469 469 overwrite = force and not branchmerge
470 470 actions, diverge, renamedelete = origfn(
471 471 repo, p1, p2, pas, branchmerge, force, acceptremote, *args, **kwargs)
472 472
473 473 if overwrite:
474 474 return actions, diverge, renamedelete
475 475
476 476 # Convert to dictionary with filename as key and action as value.
477 477 lfiles = set()
478 478 for f in actions:
479 479 splitstandin = lfutil.splitstandin(f)
480 480 if splitstandin in p1:
481 481 lfiles.add(splitstandin)
482 482 elif lfutil.standin(f) in p1:
483 483 lfiles.add(f)
484 484
485 485 for lfile in sorted(lfiles):
486 486 standin = lfutil.standin(lfile)
487 487 (lm, largs, lmsg) = actions.get(lfile, (None, None, None))
488 488 (sm, sargs, smsg) = actions.get(standin, (None, None, None))
489 489 if sm in ('g', 'dc') and lm != 'r':
490 490 if sm == 'dc':
491 491 f1, f2, fa, move, anc = sargs
492 492 sargs = (p2[f2].flags(), False)
493 493 # Case 1: normal file in the working copy, largefile in
494 494 # the second parent
495 495 usermsg = _('remote turned local normal file %s into a largefile\n'
496 496 'use (l)argefile or keep (n)ormal file?'
497 497 '$$ &Largefile $$ &Normal file') % lfile
498 498 if repo.ui.promptchoice(usermsg, 0) == 0: # pick remote largefile
499 499 actions[lfile] = ('r', None, 'replaced by standin')
500 500 actions[standin] = ('g', sargs, 'replaces standin')
501 501 else: # keep local normal file
502 502 actions[lfile] = ('k', None, 'replaces standin')
503 503 if branchmerge:
504 504 actions[standin] = ('k', None, 'replaced by non-standin')
505 505 else:
506 506 actions[standin] = ('r', None, 'replaced by non-standin')
507 507 elif lm in ('g', 'dc') and sm != 'r':
508 508 if lm == 'dc':
509 509 f1, f2, fa, move, anc = largs
510 510 largs = (p2[f2].flags(), False)
511 511 # Case 2: largefile in the working copy, normal file in
512 512 # the second parent
513 513 usermsg = _('remote turned local largefile %s into a normal file\n'
514 514 'keep (l)argefile or use (n)ormal file?'
515 515 '$$ &Largefile $$ &Normal file') % lfile
516 516 if repo.ui.promptchoice(usermsg, 0) == 0: # keep local largefile
517 517 if branchmerge:
518 518 # largefile can be restored from standin safely
519 519 actions[lfile] = ('k', None, 'replaced by standin')
520 520 actions[standin] = ('k', None, 'replaces standin')
521 521 else:
522 522 # "lfile" should be marked as "removed" without
523 523 # removal of itself
524 524 actions[lfile] = ('lfmr', None,
525 525 'forget non-standin largefile')
526 526
527 527 # linear-merge should treat this largefile as 're-added'
528 528 actions[standin] = ('a', None, 'keep standin')
529 529 else: # pick remote normal file
530 530 actions[lfile] = ('g', largs, 'replaces standin')
531 531 actions[standin] = ('r', None, 'replaced by non-standin')
532 532
533 533 return actions, diverge, renamedelete
534 534
535 535 def mergerecordupdates(orig, repo, actions, branchmerge):
536 536 if 'lfmr' in actions:
537 537 lfdirstate = lfutil.openlfdirstate(repo.ui, repo)
538 538 for lfile, args, msg in actions['lfmr']:
539 539 # this should be executed before 'orig', to execute 'remove'
540 540 # before all other actions
541 541 repo.dirstate.remove(lfile)
542 542 # make sure lfile doesn't get synclfdirstate'd as normal
543 543 lfdirstate.add(lfile)
544 544 lfdirstate.write()
545 545
546 546 return orig(repo, actions, branchmerge)
547 547
548 548 # Override filemerge to prompt the user about how they wish to merge
549 549 # largefiles. This will handle identical edits without prompting the user.
550 550 def overridefilemerge(origfn, premerge, repo, wctx, mynode, orig, fcd, fco, fca,
551 551 labels=None):
552 552 if not lfutil.isstandin(orig) or fcd.isabsent() or fco.isabsent():
553 553 return origfn(premerge, repo, wctx, mynode, orig, fcd, fco, fca,
554 554 labels=labels)
555 555
556 556 ahash = lfutil.readasstandin(fca).lower()
557 557 dhash = lfutil.readasstandin(fcd).lower()
558 558 ohash = lfutil.readasstandin(fco).lower()
559 559 if (ohash != ahash and
560 560 ohash != dhash and
561 561 (dhash == ahash or
562 562 repo.ui.promptchoice(
563 563 _('largefile %s has a merge conflict\nancestor was %s\n'
564 564 'keep (l)ocal %s or\ntake (o)ther %s?'
565 565 '$$ &Local $$ &Other') %
566 566 (lfutil.splitstandin(orig), ahash, dhash, ohash),
567 567 0) == 1)):
568 568 repo.wwrite(fcd.path(), fco.data(), fco.flags())
569 569 return True, 0, False
570 570
571 571 def copiespathcopies(orig, ctx1, ctx2, match=None):
572 572 copies = orig(ctx1, ctx2, match=match)
573 573 updated = {}
574 574
575 575 for k, v in copies.iteritems():
576 576 updated[lfutil.splitstandin(k) or k] = lfutil.splitstandin(v) or v
577 577
578 578 return updated
579 579
580 580 # Copy first changes the matchers to match standins instead of
581 581 # largefiles. Then it overrides util.copyfile in that function it
582 582 # checks if the destination largefile already exists. It also keeps a
583 583 # list of copied files so that the largefiles can be copied and the
584 584 # dirstate updated.
585 585 def overridecopy(orig, ui, repo, pats, opts, rename=False):
586 586 # doesn't remove largefile on rename
587 587 if len(pats) < 2:
588 588 # this isn't legal, let the original function deal with it
589 589 return orig(ui, repo, pats, opts, rename)
590 590
591 591 # This could copy both lfiles and normal files in one command,
592 592 # but we don't want to do that. First replace their matcher to
593 593 # only match normal files and run it, then replace it to just
594 594 # match largefiles and run it again.
595 595 nonormalfiles = False
596 596 nolfiles = False
597 597 installnormalfilesmatchfn(repo[None].manifest())
598 598 try:
599 599 result = orig(ui, repo, pats, opts, rename)
600 600 except error.Abort as e:
601 601 if pycompat.bytestr(e) != _('no files to copy'):
602 602 raise e
603 603 else:
604 604 nonormalfiles = True
605 605 result = 0
606 606 finally:
607 607 restorematchfn()
608 608
609 609 # The first rename can cause our current working directory to be removed.
610 610 # In that case there is nothing left to copy/rename so just quit.
611 611 try:
612 612 repo.getcwd()
613 613 except OSError:
614 614 return result
615 615
616 616 def makestandin(relpath):
617 617 path = pathutil.canonpath(repo.root, repo.getcwd(), relpath)
618 618 return repo.wvfs.join(lfutil.standin(path))
619 619
620 620 fullpats = scmutil.expandpats(pats)
621 621 dest = fullpats[-1]
622 622
623 623 if os.path.isdir(dest):
624 624 if not os.path.isdir(makestandin(dest)):
625 625 os.makedirs(makestandin(dest))
626 626
627 627 try:
628 628 # When we call orig below it creates the standins but we don't add
629 629 # them to the dir state until later so lock during that time.
630 630 wlock = repo.wlock()
631 631
632 632 manifest = repo[None].manifest()
633 633 def overridematch(ctx, pats=(), opts=None, globbed=False,
634 634 default='relpath', badfn=None):
635 635 if opts is None:
636 636 opts = {}
637 637 newpats = []
638 638 # The patterns were previously mangled to add the standin
639 639 # directory; we need to remove that now
640 640 for pat in pats:
641 641 if matchmod.patkind(pat) is None and lfutil.shortname in pat:
642 642 newpats.append(pat.replace(lfutil.shortname, ''))
643 643 else:
644 644 newpats.append(pat)
645 645 match = oldmatch(ctx, newpats, opts, globbed, default, badfn=badfn)
646 646 m = copy.copy(match)
647 647 lfile = lambda f: lfutil.standin(f) in manifest
648 648 m._files = [lfutil.standin(f) for f in m._files if lfile(f)]
649 649 m._fileset = set(m._files)
650 650 origmatchfn = m.matchfn
651 651 def matchfn(f):
652 652 lfile = lfutil.splitstandin(f)
653 653 return (lfile is not None and
654 654 (f in manifest) and
655 655 origmatchfn(lfile) or
656 656 None)
657 657 m.matchfn = matchfn
658 658 return m
659 659 oldmatch = installmatchfn(overridematch)
660 660 listpats = []
661 661 for pat in pats:
662 662 if matchmod.patkind(pat) is not None:
663 663 listpats.append(pat)
664 664 else:
665 665 listpats.append(makestandin(pat))
666 666
667 667 try:
668 668 origcopyfile = util.copyfile
669 669 copiedfiles = []
670 def overridecopyfile(src, dest):
670 def overridecopyfile(src, dest, *args, **kwargs):
671 671 if (lfutil.shortname in src and
672 672 dest.startswith(repo.wjoin(lfutil.shortname))):
673 673 destlfile = dest.replace(lfutil.shortname, '')
674 674 if not opts['force'] and os.path.exists(destlfile):
675 675 raise IOError('',
676 676 _('destination largefile already exists'))
677 677 copiedfiles.append((src, dest))
678 origcopyfile(src, dest)
678 origcopyfile(src, dest, *args, **kwargs)
679 679
680 680 util.copyfile = overridecopyfile
681 681 result += orig(ui, repo, listpats, opts, rename)
682 682 finally:
683 683 util.copyfile = origcopyfile
684 684
685 685 lfdirstate = lfutil.openlfdirstate(ui, repo)
686 686 for (src, dest) in copiedfiles:
687 687 if (lfutil.shortname in src and
688 688 dest.startswith(repo.wjoin(lfutil.shortname))):
689 689 srclfile = src.replace(repo.wjoin(lfutil.standin('')), '')
690 690 destlfile = dest.replace(repo.wjoin(lfutil.standin('')), '')
691 691 destlfiledir = repo.wvfs.dirname(repo.wjoin(destlfile)) or '.'
692 692 if not os.path.isdir(destlfiledir):
693 693 os.makedirs(destlfiledir)
694 694 if rename:
695 695 os.rename(repo.wjoin(srclfile), repo.wjoin(destlfile))
696 696
697 697 # The file is gone, but this deletes any empty parent
698 698 # directories as a side-effect.
699 699 repo.wvfs.unlinkpath(srclfile, ignoremissing=True)
700 700 lfdirstate.remove(srclfile)
701 701 else:
702 702 util.copyfile(repo.wjoin(srclfile),
703 703 repo.wjoin(destlfile))
704 704
705 705 lfdirstate.add(destlfile)
706 706 lfdirstate.write()
707 707 except error.Abort as e:
708 708 if pycompat.bytestr(e) != _('no files to copy'):
709 709 raise e
710 710 else:
711 711 nolfiles = True
712 712 finally:
713 713 restorematchfn()
714 714 wlock.release()
715 715
716 716 if nolfiles and nonormalfiles:
717 717 raise error.Abort(_('no files to copy'))
718 718
719 719 return result
720 720
721 721 # When the user calls revert, we have to be careful to not revert any
722 722 # changes to other largefiles accidentally. This means we have to keep
723 723 # track of the largefiles that are being reverted so we only pull down
724 724 # the necessary largefiles.
725 725 #
726 726 # Standins are only updated (to match the hash of largefiles) before
727 727 # commits. Update the standins then run the original revert, changing
728 728 # the matcher to hit standins instead of largefiles. Based on the
729 729 # resulting standins update the largefiles.
730 730 def overriderevert(orig, ui, repo, ctx, parents, *pats, **opts):
731 731 # Because we put the standins in a bad state (by updating them)
732 732 # and then return them to a correct state we need to lock to
733 733 # prevent others from changing them in their incorrect state.
734 734 with repo.wlock():
735 735 lfdirstate = lfutil.openlfdirstate(ui, repo)
736 736 s = lfutil.lfdirstatestatus(lfdirstate, repo)
737 737 lfdirstate.write()
738 738 for lfile in s.modified:
739 739 lfutil.updatestandin(repo, lfile, lfutil.standin(lfile))
740 740 for lfile in s.deleted:
741 741 fstandin = lfutil.standin(lfile)
742 742 if (repo.wvfs.exists(fstandin)):
743 743 repo.wvfs.unlink(fstandin)
744 744
745 745 oldstandins = lfutil.getstandinsstate(repo)
746 746
747 747 def overridematch(mctx, pats=(), opts=None, globbed=False,
748 748 default='relpath', badfn=None):
749 749 if opts is None:
750 750 opts = {}
751 751 match = oldmatch(mctx, pats, opts, globbed, default, badfn=badfn)
752 752 m = copy.copy(match)
753 753
754 754 # revert supports recursing into subrepos, and though largefiles
755 755 # currently doesn't work correctly in that case, this match is
756 756 # called, so the lfdirstate above may not be the correct one for
757 757 # this invocation of match.
758 758 lfdirstate = lfutil.openlfdirstate(mctx.repo().ui, mctx.repo(),
759 759 False)
760 760
761 761 wctx = repo[None]
762 762 matchfiles = []
763 763 for f in m._files:
764 764 standin = lfutil.standin(f)
765 765 if standin in ctx or standin in mctx:
766 766 matchfiles.append(standin)
767 767 elif standin in wctx or lfdirstate[f] == 'r':
768 768 continue
769 769 else:
770 770 matchfiles.append(f)
771 771 m._files = matchfiles
772 772 m._fileset = set(m._files)
773 773 origmatchfn = m.matchfn
774 774 def matchfn(f):
775 775 lfile = lfutil.splitstandin(f)
776 776 if lfile is not None:
777 777 return (origmatchfn(lfile) and
778 778 (f in ctx or f in mctx))
779 779 return origmatchfn(f)
780 780 m.matchfn = matchfn
781 781 return m
782 782 oldmatch = installmatchfn(overridematch)
783 783 try:
784 784 orig(ui, repo, ctx, parents, *pats, **opts)
785 785 finally:
786 786 restorematchfn()
787 787
788 788 newstandins = lfutil.getstandinsstate(repo)
789 789 filelist = lfutil.getlfilestoupdate(oldstandins, newstandins)
790 790 # lfdirstate should be 'normallookup'-ed for updated files,
791 791 # because reverting doesn't touch dirstate for 'normal' files
792 792 # when target revision is explicitly specified: in such case,
793 793 # 'n' and valid timestamp in dirstate doesn't ensure 'clean'
794 794 # of target (standin) file.
795 795 lfcommands.updatelfiles(ui, repo, filelist, printmessage=False,
796 796 normallookup=True)
797 797
798 798 # after pulling changesets, we need to take some extra care to get
799 799 # largefiles updated remotely
800 800 def overridepull(orig, ui, repo, source=None, **opts):
801 801 revsprepull = len(repo)
802 802 if not source:
803 803 source = 'default'
804 804 repo.lfpullsource = source
805 805 result = orig(ui, repo, source, **opts)
806 806 revspostpull = len(repo)
807 807 lfrevs = opts.get(r'lfrev', [])
808 808 if opts.get(r'all_largefiles'):
809 809 lfrevs.append('pulled()')
810 810 if lfrevs and revspostpull > revsprepull:
811 811 numcached = 0
812 812 repo.firstpulled = revsprepull # for pulled() revset expression
813 813 try:
814 814 for rev in scmutil.revrange(repo, lfrevs):
815 815 ui.note(_('pulling largefiles for revision %d\n') % rev)
816 816 (cached, missing) = lfcommands.cachelfiles(ui, repo, rev)
817 817 numcached += len(cached)
818 818 finally:
819 819 del repo.firstpulled
820 820 ui.status(_("%d largefiles cached\n") % numcached)
821 821 return result
822 822
823 823 def overridepush(orig, ui, repo, *args, **kwargs):
824 824 """Override push command and store --lfrev parameters in opargs"""
825 825 lfrevs = kwargs.pop(r'lfrev', None)
826 826 if lfrevs:
827 827 opargs = kwargs.setdefault(r'opargs', {})
828 828 opargs['lfrevs'] = scmutil.revrange(repo, lfrevs)
829 829 return orig(ui, repo, *args, **kwargs)
830 830
831 831 def exchangepushoperation(orig, *args, **kwargs):
832 832 """Override pushoperation constructor and store lfrevs parameter"""
833 833 lfrevs = kwargs.pop(r'lfrevs', None)
834 834 pushop = orig(*args, **kwargs)
835 835 pushop.lfrevs = lfrevs
836 836 return pushop
837 837
838 838 revsetpredicate = registrar.revsetpredicate()
839 839
840 840 @revsetpredicate('pulled()')
841 841 def pulledrevsetsymbol(repo, subset, x):
842 842 """Changesets that just has been pulled.
843 843
844 844 Only available with largefiles from pull --lfrev expressions.
845 845
846 846 .. container:: verbose
847 847
848 848 Some examples:
849 849
850 850 - pull largefiles for all new changesets::
851 851
852 852 hg pull -lfrev "pulled()"
853 853
854 854 - pull largefiles for all new branch heads::
855 855
856 856 hg pull -lfrev "head(pulled()) and not closed()"
857 857
858 858 """
859 859
860 860 try:
861 861 firstpulled = repo.firstpulled
862 862 except AttributeError:
863 863 raise error.Abort(_("pulled() only available in --lfrev"))
864 864 return smartset.baseset([r for r in subset if r >= firstpulled])
865 865
866 866 def overrideclone(orig, ui, source, dest=None, **opts):
867 867 d = dest
868 868 if d is None:
869 869 d = hg.defaultdest(source)
870 870 if opts.get(r'all_largefiles') and not hg.islocal(d):
871 871 raise error.Abort(_(
872 872 '--all-largefiles is incompatible with non-local destination %s') %
873 873 d)
874 874
875 875 return orig(ui, source, dest, **opts)
876 876
877 877 def hgclone(orig, ui, opts, *args, **kwargs):
878 878 result = orig(ui, opts, *args, **kwargs)
879 879
880 880 if result is not None:
881 881 sourcerepo, destrepo = result
882 882 repo = destrepo.local()
883 883
884 884 # When cloning to a remote repo (like through SSH), no repo is available
885 885 # from the peer. Therefore the largefiles can't be downloaded and the
886 886 # hgrc can't be updated.
887 887 if not repo:
888 888 return result
889 889
890 890 # If largefiles is required for this repo, permanently enable it locally
891 891 if 'largefiles' in repo.requirements:
892 892 repo.vfs.append('hgrc',
893 893 util.tonativeeol('\n[extensions]\nlargefiles=\n'))
894 894
895 895 # Caching is implicitly limited to 'rev' option, since the dest repo was
896 896 # truncated at that point. The user may expect a download count with
897 897 # this option, so attempt whether or not this is a largefile repo.
898 898 if opts.get(r'all_largefiles'):
899 899 success, missing = lfcommands.downloadlfiles(ui, repo, None)
900 900
901 901 if missing != 0:
902 902 return None
903 903
904 904 return result
905 905
906 906 def hgpostshare(orig, sourcerepo, destrepo, bookmarks=True, defaultpath=None):
907 907 orig(sourcerepo, destrepo, bookmarks, defaultpath)
908 908
909 909 # If largefiles is required for this repo, permanently enable it locally
910 910 if 'largefiles' in destrepo.requirements:
911 911 destrepo.vfs.append('hgrc',
912 912 util.tonativeeol('\n[extensions]\nlargefiles=\n'))
913 913
914 914 def overriderebase(orig, ui, repo, **opts):
915 915 if not util.safehasattr(repo, '_largefilesenabled'):
916 916 return orig(ui, repo, **opts)
917 917
918 918 resuming = opts.get(r'continue')
919 919 repo._lfcommithooks.append(lfutil.automatedcommithook(resuming))
920 920 repo._lfstatuswriters.append(lambda *msg, **opts: None)
921 921 try:
922 922 return orig(ui, repo, **opts)
923 923 finally:
924 924 repo._lfstatuswriters.pop()
925 925 repo._lfcommithooks.pop()
926 926
927 927 def overridearchivecmd(orig, ui, repo, dest, **opts):
928 928 repo.unfiltered().lfstatus = True
929 929
930 930 try:
931 931 return orig(ui, repo.unfiltered(), dest, **opts)
932 932 finally:
933 933 repo.unfiltered().lfstatus = False
934 934
935 935 def hgwebarchive(orig, web):
936 936 web.repo.lfstatus = True
937 937
938 938 try:
939 939 return orig(web)
940 940 finally:
941 941 web.repo.lfstatus = False
942 942
943 943 def overridearchive(orig, repo, dest, node, kind, decode=True, matchfn=None,
944 944 prefix='', mtime=None, subrepos=None):
945 945 # For some reason setting repo.lfstatus in hgwebarchive only changes the
946 946 # unfiltered repo's attr, so check that as well.
947 947 if not repo.lfstatus and not repo.unfiltered().lfstatus:
948 948 return orig(repo, dest, node, kind, decode, matchfn, prefix, mtime,
949 949 subrepos)
950 950
951 951 # No need to lock because we are only reading history and
952 952 # largefile caches, neither of which are modified.
953 953 if node is not None:
954 954 lfcommands.cachelfiles(repo.ui, repo, node)
955 955
956 956 if kind not in archival.archivers:
957 957 raise error.Abort(_("unknown archive type '%s'") % kind)
958 958
959 959 ctx = repo[node]
960 960
961 961 if kind == 'files':
962 962 if prefix:
963 963 raise error.Abort(
964 964 _('cannot give prefix when archiving to files'))
965 965 else:
966 966 prefix = archival.tidyprefix(dest, kind, prefix)
967 967
968 968 def write(name, mode, islink, getdata):
969 969 if matchfn and not matchfn(name):
970 970 return
971 971 data = getdata()
972 972 if decode:
973 973 data = repo.wwritedata(name, data)
974 974 archiver.addfile(prefix + name, mode, islink, data)
975 975
976 976 archiver = archival.archivers[kind](dest, mtime or ctx.date()[0])
977 977
978 978 if repo.ui.configbool("ui", "archivemeta"):
979 979 write('.hg_archival.txt', 0o644, False,
980 980 lambda: archival.buildmetadata(ctx))
981 981
982 982 for f in ctx:
983 983 ff = ctx.flags(f)
984 984 getdata = ctx[f].data
985 985 lfile = lfutil.splitstandin(f)
986 986 if lfile is not None:
987 987 if node is not None:
988 988 path = lfutil.findfile(repo, getdata().strip())
989 989
990 990 if path is None:
991 991 raise error.Abort(
992 992 _('largefile %s not found in repo store or system cache')
993 993 % lfile)
994 994 else:
995 995 path = lfile
996 996
997 997 f = lfile
998 998
999 999 getdata = lambda: util.readfile(path)
1000 1000 write(f, 'x' in ff and 0o755 or 0o644, 'l' in ff, getdata)
1001 1001
1002 1002 if subrepos:
1003 1003 for subpath in sorted(ctx.substate):
1004 1004 sub = ctx.workingsub(subpath)
1005 1005 submatch = matchmod.subdirmatcher(subpath, matchfn)
1006 1006 sub._repo.lfstatus = True
1007 1007 sub.archive(archiver, prefix, submatch)
1008 1008
1009 1009 archiver.done()
1010 1010
1011 1011 def hgsubrepoarchive(orig, repo, archiver, prefix, match=None, decode=True):
1012 1012 lfenabled = util.safehasattr(repo._repo, '_largefilesenabled')
1013 1013 if not lfenabled or not repo._repo.lfstatus:
1014 1014 return orig(repo, archiver, prefix, match, decode)
1015 1015
1016 1016 repo._get(repo._state + ('hg',))
1017 1017 rev = repo._state[1]
1018 1018 ctx = repo._repo[rev]
1019 1019
1020 1020 if ctx.node() is not None:
1021 1021 lfcommands.cachelfiles(repo.ui, repo._repo, ctx.node())
1022 1022
1023 1023 def write(name, mode, islink, getdata):
1024 1024 # At this point, the standin has been replaced with the largefile name,
1025 1025 # so the normal matcher works here without the lfutil variants.
1026 1026 if match and not match(f):
1027 1027 return
1028 1028 data = getdata()
1029 1029 if decode:
1030 1030 data = repo._repo.wwritedata(name, data)
1031 1031
1032 1032 archiver.addfile(prefix + repo._path + '/' + name, mode, islink, data)
1033 1033
1034 1034 for f in ctx:
1035 1035 ff = ctx.flags(f)
1036 1036 getdata = ctx[f].data
1037 1037 lfile = lfutil.splitstandin(f)
1038 1038 if lfile is not None:
1039 1039 if ctx.node() is not None:
1040 1040 path = lfutil.findfile(repo._repo, getdata().strip())
1041 1041
1042 1042 if path is None:
1043 1043 raise error.Abort(
1044 1044 _('largefile %s not found in repo store or system cache')
1045 1045 % lfile)
1046 1046 else:
1047 1047 path = lfile
1048 1048
1049 1049 f = lfile
1050 1050
1051 1051 getdata = lambda: util.readfile(os.path.join(prefix, path))
1052 1052
1053 1053 write(f, 'x' in ff and 0o755 or 0o644, 'l' in ff, getdata)
1054 1054
1055 1055 for subpath in sorted(ctx.substate):
1056 1056 sub = ctx.workingsub(subpath)
1057 1057 submatch = matchmod.subdirmatcher(subpath, match)
1058 1058 sub._repo.lfstatus = True
1059 1059 sub.archive(archiver, prefix + repo._path + '/', submatch, decode)
1060 1060
1061 1061 # If a largefile is modified, the change is not reflected in its
1062 1062 # standin until a commit. cmdutil.bailifchanged() raises an exception
1063 1063 # if the repo has uncommitted changes. Wrap it to also check if
1064 1064 # largefiles were changed. This is used by bisect, backout and fetch.
1065 1065 def overridebailifchanged(orig, repo, *args, **kwargs):
1066 1066 orig(repo, *args, **kwargs)
1067 1067 repo.lfstatus = True
1068 1068 s = repo.status()
1069 1069 repo.lfstatus = False
1070 1070 if s.modified or s.added or s.removed or s.deleted:
1071 1071 raise error.Abort(_('uncommitted changes'))
1072 1072
1073 1073 def postcommitstatus(orig, repo, *args, **kwargs):
1074 1074 repo.lfstatus = True
1075 1075 try:
1076 1076 return orig(repo, *args, **kwargs)
1077 1077 finally:
1078 1078 repo.lfstatus = False
1079 1079
1080 1080 def cmdutilforget(orig, ui, repo, match, prefix, explicitonly, dryrun):
1081 1081 normalmatcher = composenormalfilematcher(match, repo[None].manifest())
1082 1082 bad, forgot = orig(ui, repo, normalmatcher, prefix, explicitonly, dryrun)
1083 1083 m = composelargefilematcher(match, repo[None].manifest())
1084 1084
1085 1085 try:
1086 1086 repo.lfstatus = True
1087 1087 s = repo.status(match=m, clean=True)
1088 1088 finally:
1089 1089 repo.lfstatus = False
1090 1090 manifest = repo[None].manifest()
1091 1091 forget = sorted(s.modified + s.added + s.deleted + s.clean)
1092 1092 forget = [f for f in forget if lfutil.standin(f) in manifest]
1093 1093
1094 1094 for f in forget:
1095 1095 fstandin = lfutil.standin(f)
1096 1096 if fstandin not in repo.dirstate and not repo.wvfs.isdir(fstandin):
1097 1097 ui.warn(_('not removing %s: file is already untracked\n')
1098 1098 % m.rel(f))
1099 1099 bad.append(f)
1100 1100
1101 1101 for f in forget:
1102 1102 if ui.verbose or not m.exact(f):
1103 1103 ui.status(_('removing %s\n') % m.rel(f))
1104 1104
1105 1105 # Need to lock because standin files are deleted then removed from the
1106 1106 # repository and we could race in-between.
1107 1107 with repo.wlock():
1108 1108 lfdirstate = lfutil.openlfdirstate(ui, repo)
1109 1109 for f in forget:
1110 1110 if lfdirstate[f] == 'a':
1111 1111 lfdirstate.drop(f)
1112 1112 else:
1113 1113 lfdirstate.remove(f)
1114 1114 lfdirstate.write()
1115 1115 standins = [lfutil.standin(f) for f in forget]
1116 1116 for f in standins:
1117 1117 repo.wvfs.unlinkpath(f, ignoremissing=True)
1118 1118 rejected = repo[None].forget(standins)
1119 1119
1120 1120 bad.extend(f for f in rejected if f in m.files())
1121 1121 forgot.extend(f for f in forget if f not in rejected)
1122 1122 return bad, forgot
1123 1123
1124 1124 def _getoutgoings(repo, other, missing, addfunc):
1125 1125 """get pairs of filename and largefile hash in outgoing revisions
1126 1126 in 'missing'.
1127 1127
1128 1128 largefiles already existing on 'other' repository are ignored.
1129 1129
1130 1130 'addfunc' is invoked with each unique pairs of filename and
1131 1131 largefile hash value.
1132 1132 """
1133 1133 knowns = set()
1134 1134 lfhashes = set()
1135 1135 def dedup(fn, lfhash):
1136 1136 k = (fn, lfhash)
1137 1137 if k not in knowns:
1138 1138 knowns.add(k)
1139 1139 lfhashes.add(lfhash)
1140 1140 lfutil.getlfilestoupload(repo, missing, dedup)
1141 1141 if lfhashes:
1142 1142 lfexists = storefactory.openstore(repo, other).exists(lfhashes)
1143 1143 for fn, lfhash in knowns:
1144 1144 if not lfexists[lfhash]: # lfhash doesn't exist on "other"
1145 1145 addfunc(fn, lfhash)
1146 1146
1147 1147 def outgoinghook(ui, repo, other, opts, missing):
1148 1148 if opts.pop('large', None):
1149 1149 lfhashes = set()
1150 1150 if ui.debugflag:
1151 1151 toupload = {}
1152 1152 def addfunc(fn, lfhash):
1153 1153 if fn not in toupload:
1154 1154 toupload[fn] = []
1155 1155 toupload[fn].append(lfhash)
1156 1156 lfhashes.add(lfhash)
1157 1157 def showhashes(fn):
1158 1158 for lfhash in sorted(toupload[fn]):
1159 1159 ui.debug(' %s\n' % (lfhash))
1160 1160 else:
1161 1161 toupload = set()
1162 1162 def addfunc(fn, lfhash):
1163 1163 toupload.add(fn)
1164 1164 lfhashes.add(lfhash)
1165 1165 def showhashes(fn):
1166 1166 pass
1167 1167 _getoutgoings(repo, other, missing, addfunc)
1168 1168
1169 1169 if not toupload:
1170 1170 ui.status(_('largefiles: no files to upload\n'))
1171 1171 else:
1172 1172 ui.status(_('largefiles to upload (%d entities):\n')
1173 1173 % (len(lfhashes)))
1174 1174 for file in sorted(toupload):
1175 1175 ui.status(lfutil.splitstandin(file) + '\n')
1176 1176 showhashes(file)
1177 1177 ui.status('\n')
1178 1178
1179 1179 def summaryremotehook(ui, repo, opts, changes):
1180 1180 largeopt = opts.get('large', False)
1181 1181 if changes is None:
1182 1182 if largeopt:
1183 1183 return (False, True) # only outgoing check is needed
1184 1184 else:
1185 1185 return (False, False)
1186 1186 elif largeopt:
1187 1187 url, branch, peer, outgoing = changes[1]
1188 1188 if peer is None:
1189 1189 # i18n: column positioning for "hg summary"
1190 1190 ui.status(_('largefiles: (no remote repo)\n'))
1191 1191 return
1192 1192
1193 1193 toupload = set()
1194 1194 lfhashes = set()
1195 1195 def addfunc(fn, lfhash):
1196 1196 toupload.add(fn)
1197 1197 lfhashes.add(lfhash)
1198 1198 _getoutgoings(repo, peer, outgoing.missing, addfunc)
1199 1199
1200 1200 if not toupload:
1201 1201 # i18n: column positioning for "hg summary"
1202 1202 ui.status(_('largefiles: (no files to upload)\n'))
1203 1203 else:
1204 1204 # i18n: column positioning for "hg summary"
1205 1205 ui.status(_('largefiles: %d entities for %d files to upload\n')
1206 1206 % (len(lfhashes), len(toupload)))
1207 1207
1208 1208 def overridesummary(orig, ui, repo, *pats, **opts):
1209 1209 try:
1210 1210 repo.lfstatus = True
1211 1211 orig(ui, repo, *pats, **opts)
1212 1212 finally:
1213 1213 repo.lfstatus = False
1214 1214
1215 1215 def scmutiladdremove(orig, repo, matcher, prefix, opts=None, dry_run=None,
1216 1216 similarity=None):
1217 1217 if opts is None:
1218 1218 opts = {}
1219 1219 if not lfutil.islfilesrepo(repo):
1220 1220 return orig(repo, matcher, prefix, opts, dry_run, similarity)
1221 1221 # Get the list of missing largefiles so we can remove them
1222 1222 lfdirstate = lfutil.openlfdirstate(repo.ui, repo)
1223 1223 unsure, s = lfdirstate.status(matchmod.always(repo.root, repo.getcwd()),
1224 1224 subrepos=[], ignored=False, clean=False,
1225 1225 unknown=False)
1226 1226
1227 1227 # Call into the normal remove code, but the removing of the standin, we want
1228 1228 # to have handled by original addremove. Monkey patching here makes sure
1229 1229 # we don't remove the standin in the largefiles code, preventing a very
1230 1230 # confused state later.
1231 1231 if s.deleted:
1232 1232 m = copy.copy(matcher)
1233 1233
1234 1234 # The m._files and m._map attributes are not changed to the deleted list
1235 1235 # because that affects the m.exact() test, which in turn governs whether
1236 1236 # or not the file name is printed, and how. Simply limit the original
1237 1237 # matches to those in the deleted status list.
1238 1238 matchfn = m.matchfn
1239 1239 m.matchfn = lambda f: f in s.deleted and matchfn(f)
1240 1240
1241 1241 removelargefiles(repo.ui, repo, True, m, **pycompat.strkwargs(opts))
1242 1242 # Call into the normal add code, and any files that *should* be added as
1243 1243 # largefiles will be
1244 1244 added, bad = addlargefiles(repo.ui, repo, True, matcher,
1245 1245 **pycompat.strkwargs(opts))
1246 1246 # Now that we've handled largefiles, hand off to the original addremove
1247 1247 # function to take care of the rest. Make sure it doesn't do anything with
1248 1248 # largefiles by passing a matcher that will ignore them.
1249 1249 matcher = composenormalfilematcher(matcher, repo[None].manifest(), added)
1250 1250 return orig(repo, matcher, prefix, opts, dry_run, similarity)
1251 1251
1252 1252 # Calling purge with --all will cause the largefiles to be deleted.
1253 1253 # Override repo.status to prevent this from happening.
1254 1254 def overridepurge(orig, ui, repo, *dirs, **opts):
1255 1255 # XXX Monkey patching a repoview will not work. The assigned attribute will
1256 1256 # be set on the unfiltered repo, but we will only lookup attributes in the
1257 1257 # unfiltered repo if the lookup in the repoview object itself fails. As the
1258 1258 # monkey patched method exists on the repoview class the lookup will not
1259 1259 # fail. As a result, the original version will shadow the monkey patched
1260 1260 # one, defeating the monkey patch.
1261 1261 #
1262 1262 # As a work around we use an unfiltered repo here. We should do something
1263 1263 # cleaner instead.
1264 1264 repo = repo.unfiltered()
1265 1265 oldstatus = repo.status
1266 1266 def overridestatus(node1='.', node2=None, match=None, ignored=False,
1267 1267 clean=False, unknown=False, listsubrepos=False):
1268 1268 r = oldstatus(node1, node2, match, ignored, clean, unknown,
1269 1269 listsubrepos)
1270 1270 lfdirstate = lfutil.openlfdirstate(ui, repo)
1271 1271 unknown = [f for f in r.unknown if lfdirstate[f] == '?']
1272 1272 ignored = [f for f in r.ignored if lfdirstate[f] == '?']
1273 1273 return scmutil.status(r.modified, r.added, r.removed, r.deleted,
1274 1274 unknown, ignored, r.clean)
1275 1275 repo.status = overridestatus
1276 1276 orig(ui, repo, *dirs, **opts)
1277 1277 repo.status = oldstatus
1278 1278
1279 1279 def overriderollback(orig, ui, repo, **opts):
1280 1280 with repo.wlock():
1281 1281 before = repo.dirstate.parents()
1282 1282 orphans = set(f for f in repo.dirstate
1283 1283 if lfutil.isstandin(f) and repo.dirstate[f] != 'r')
1284 1284 result = orig(ui, repo, **opts)
1285 1285 after = repo.dirstate.parents()
1286 1286 if before == after:
1287 1287 return result # no need to restore standins
1288 1288
1289 1289 pctx = repo['.']
1290 1290 for f in repo.dirstate:
1291 1291 if lfutil.isstandin(f):
1292 1292 orphans.discard(f)
1293 1293 if repo.dirstate[f] == 'r':
1294 1294 repo.wvfs.unlinkpath(f, ignoremissing=True)
1295 1295 elif f in pctx:
1296 1296 fctx = pctx[f]
1297 1297 repo.wwrite(f, fctx.data(), fctx.flags())
1298 1298 else:
1299 1299 # content of standin is not so important in 'a',
1300 1300 # 'm' or 'n' (coming from the 2nd parent) cases
1301 1301 lfutil.writestandin(repo, f, '', False)
1302 1302 for standin in orphans:
1303 1303 repo.wvfs.unlinkpath(standin, ignoremissing=True)
1304 1304
1305 1305 lfdirstate = lfutil.openlfdirstate(ui, repo)
1306 1306 orphans = set(lfdirstate)
1307 1307 lfiles = lfutil.listlfiles(repo)
1308 1308 for file in lfiles:
1309 1309 lfutil.synclfdirstate(repo, lfdirstate, file, True)
1310 1310 orphans.discard(file)
1311 1311 for lfile in orphans:
1312 1312 lfdirstate.drop(lfile)
1313 1313 lfdirstate.write()
1314 1314 return result
1315 1315
1316 1316 def overridetransplant(orig, ui, repo, *revs, **opts):
1317 1317 resuming = opts.get(r'continue')
1318 1318 repo._lfcommithooks.append(lfutil.automatedcommithook(resuming))
1319 1319 repo._lfstatuswriters.append(lambda *msg, **opts: None)
1320 1320 try:
1321 1321 result = orig(ui, repo, *revs, **opts)
1322 1322 finally:
1323 1323 repo._lfstatuswriters.pop()
1324 1324 repo._lfcommithooks.pop()
1325 1325 return result
1326 1326
1327 1327 def overridecat(orig, ui, repo, file1, *pats, **opts):
1328 1328 opts = pycompat.byteskwargs(opts)
1329 1329 ctx = scmutil.revsingle(repo, opts.get('rev'))
1330 1330 err = 1
1331 1331 notbad = set()
1332 1332 m = scmutil.match(ctx, (file1,) + pats, opts)
1333 1333 origmatchfn = m.matchfn
1334 1334 def lfmatchfn(f):
1335 1335 if origmatchfn(f):
1336 1336 return True
1337 1337 lf = lfutil.splitstandin(f)
1338 1338 if lf is None:
1339 1339 return False
1340 1340 notbad.add(lf)
1341 1341 return origmatchfn(lf)
1342 1342 m.matchfn = lfmatchfn
1343 1343 origbadfn = m.bad
1344 1344 def lfbadfn(f, msg):
1345 1345 if not f in notbad:
1346 1346 origbadfn(f, msg)
1347 1347 m.bad = lfbadfn
1348 1348
1349 1349 origvisitdirfn = m.visitdir
1350 1350 def lfvisitdirfn(dir):
1351 1351 if dir == lfutil.shortname:
1352 1352 return True
1353 1353 ret = origvisitdirfn(dir)
1354 1354 if ret:
1355 1355 return ret
1356 1356 lf = lfutil.splitstandin(dir)
1357 1357 if lf is None:
1358 1358 return False
1359 1359 return origvisitdirfn(lf)
1360 1360 m.visitdir = lfvisitdirfn
1361 1361
1362 1362 for f in ctx.walk(m):
1363 1363 with cmdutil.makefileobj(ctx, opts.get('output'), pathname=f) as fp:
1364 1364 lf = lfutil.splitstandin(f)
1365 1365 if lf is None or origmatchfn(f):
1366 1366 # duplicating unreachable code from commands.cat
1367 1367 data = ctx[f].data()
1368 1368 if opts.get('decode'):
1369 1369 data = repo.wwritedata(f, data)
1370 1370 fp.write(data)
1371 1371 else:
1372 1372 hash = lfutil.readasstandin(ctx[f])
1373 1373 if not lfutil.inusercache(repo.ui, hash):
1374 1374 store = storefactory.openstore(repo)
1375 1375 success, missing = store.get([(lf, hash)])
1376 1376 if len(success) != 1:
1377 1377 raise error.Abort(
1378 1378 _('largefile %s is not in cache and could not be '
1379 1379 'downloaded') % lf)
1380 1380 path = lfutil.usercachepath(repo.ui, hash)
1381 1381 with open(path, "rb") as fpin:
1382 1382 for chunk in util.filechunkiter(fpin):
1383 1383 fp.write(chunk)
1384 1384 err = 0
1385 1385 return err
1386 1386
1387 1387 def mergeupdate(orig, repo, node, branchmerge, force,
1388 1388 *args, **kwargs):
1389 1389 matcher = kwargs.get(r'matcher', None)
1390 1390 # note if this is a partial update
1391 1391 partial = matcher and not matcher.always()
1392 1392 with repo.wlock():
1393 1393 # branch | | |
1394 1394 # merge | force | partial | action
1395 1395 # -------+-------+---------+--------------
1396 1396 # x | x | x | linear-merge
1397 1397 # o | x | x | branch-merge
1398 1398 # x | o | x | overwrite (as clean update)
1399 1399 # o | o | x | force-branch-merge (*1)
1400 1400 # x | x | o | (*)
1401 1401 # o | x | o | (*)
1402 1402 # x | o | o | overwrite (as revert)
1403 1403 # o | o | o | (*)
1404 1404 #
1405 1405 # (*) don't care
1406 1406 # (*1) deprecated, but used internally (e.g: "rebase --collapse")
1407 1407
1408 1408 lfdirstate = lfutil.openlfdirstate(repo.ui, repo)
1409 1409 unsure, s = lfdirstate.status(matchmod.always(repo.root,
1410 1410 repo.getcwd()),
1411 1411 subrepos=[], ignored=False,
1412 1412 clean=True, unknown=False)
1413 1413 oldclean = set(s.clean)
1414 1414 pctx = repo['.']
1415 1415 dctx = repo[node]
1416 1416 for lfile in unsure + s.modified:
1417 1417 lfileabs = repo.wvfs.join(lfile)
1418 1418 if not repo.wvfs.exists(lfileabs):
1419 1419 continue
1420 1420 lfhash = lfutil.hashfile(lfileabs)
1421 1421 standin = lfutil.standin(lfile)
1422 1422 lfutil.writestandin(repo, standin, lfhash,
1423 1423 lfutil.getexecutable(lfileabs))
1424 1424 if (standin in pctx and
1425 1425 lfhash == lfutil.readasstandin(pctx[standin])):
1426 1426 oldclean.add(lfile)
1427 1427 for lfile in s.added:
1428 1428 fstandin = lfutil.standin(lfile)
1429 1429 if fstandin not in dctx:
1430 1430 # in this case, content of standin file is meaningless
1431 1431 # (in dctx, lfile is unknown, or normal file)
1432 1432 continue
1433 1433 lfutil.updatestandin(repo, lfile, fstandin)
1434 1434 # mark all clean largefiles as dirty, just in case the update gets
1435 1435 # interrupted before largefiles and lfdirstate are synchronized
1436 1436 for lfile in oldclean:
1437 1437 lfdirstate.normallookup(lfile)
1438 1438 lfdirstate.write()
1439 1439
1440 1440 oldstandins = lfutil.getstandinsstate(repo)
1441 1441 # Make sure the merge runs on disk, not in-memory. largefiles is not a
1442 1442 # good candidate for in-memory merge (large files, custom dirstate,
1443 1443 # matcher usage).
1444 1444 kwargs[r'wc'] = repo[None]
1445 1445 result = orig(repo, node, branchmerge, force, *args, **kwargs)
1446 1446
1447 1447 newstandins = lfutil.getstandinsstate(repo)
1448 1448 filelist = lfutil.getlfilestoupdate(oldstandins, newstandins)
1449 1449
1450 1450 # to avoid leaving all largefiles as dirty and thus rehash them, mark
1451 1451 # all the ones that didn't change as clean
1452 1452 for lfile in oldclean.difference(filelist):
1453 1453 lfdirstate.normal(lfile)
1454 1454 lfdirstate.write()
1455 1455
1456 1456 if branchmerge or force or partial:
1457 1457 filelist.extend(s.deleted + s.removed)
1458 1458
1459 1459 lfcommands.updatelfiles(repo.ui, repo, filelist=filelist,
1460 1460 normallookup=partial)
1461 1461
1462 1462 return result
1463 1463
1464 1464 def scmutilmarktouched(orig, repo, files, *args, **kwargs):
1465 1465 result = orig(repo, files, *args, **kwargs)
1466 1466
1467 1467 filelist = []
1468 1468 for f in files:
1469 1469 lf = lfutil.splitstandin(f)
1470 1470 if lf is not None:
1471 1471 filelist.append(lf)
1472 1472 if filelist:
1473 1473 lfcommands.updatelfiles(repo.ui, repo, filelist=filelist,
1474 1474 printmessage=False, normallookup=True)
1475 1475
1476 1476 return result
1477 1477
1478 1478 def upgraderequirements(orig, repo):
1479 1479 reqs = orig(repo)
1480 1480 if 'largefiles' in repo.requirements:
1481 1481 reqs.add('largefiles')
1482 1482 return reqs
1483 1483
1484 1484 _lfscheme = 'largefile://'
1485 1485 def openlargefile(orig, ui, url_, data=None):
1486 1486 if url_.startswith(_lfscheme):
1487 1487 if data:
1488 1488 msg = "cannot use data on a 'largefile://' url"
1489 1489 raise error.ProgrammingError(msg)
1490 1490 lfid = url_[len(_lfscheme):]
1491 1491 return storefactory.getlfile(ui, lfid)
1492 1492 else:
1493 1493 return orig(ui, url_, data=data)
@@ -1,3211 +1,3213 b''
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 __future__ import absolute_import
9 9
10 10 import errno
11 11 import os
12 12 import re
13 13 import tempfile
14 14
15 15 from .i18n import _
16 16 from .node import (
17 17 hex,
18 18 nullid,
19 19 nullrev,
20 20 short,
21 21 )
22 22
23 23 from . import (
24 24 bookmarks,
25 25 changelog,
26 26 copies,
27 27 crecord as crecordmod,
28 28 dirstateguard,
29 29 encoding,
30 30 error,
31 31 formatter,
32 32 logcmdutil,
33 33 match as matchmod,
34 34 merge as mergemod,
35 35 mergeutil,
36 36 obsolete,
37 37 patch,
38 38 pathutil,
39 39 pycompat,
40 40 registrar,
41 41 revlog,
42 42 rewriteutil,
43 43 scmutil,
44 44 smartset,
45 45 subrepoutil,
46 46 templatekw,
47 47 templater,
48 48 util,
49 49 vfs as vfsmod,
50 50 )
51 51
52 52 from .utils import (
53 53 dateutil,
54 54 stringutil,
55 55 )
56 56
57 57 stringio = util.stringio
58 58
59 59 # templates of common command options
60 60
61 61 dryrunopts = [
62 62 ('n', 'dry-run', None,
63 63 _('do not perform actions, just print output')),
64 64 ]
65 65
66 66 remoteopts = [
67 67 ('e', 'ssh', '',
68 68 _('specify ssh command to use'), _('CMD')),
69 69 ('', 'remotecmd', '',
70 70 _('specify hg command to run on the remote side'), _('CMD')),
71 71 ('', 'insecure', None,
72 72 _('do not verify server certificate (ignoring web.cacerts config)')),
73 73 ]
74 74
75 75 walkopts = [
76 76 ('I', 'include', [],
77 77 _('include names matching the given patterns'), _('PATTERN')),
78 78 ('X', 'exclude', [],
79 79 _('exclude names matching the given patterns'), _('PATTERN')),
80 80 ]
81 81
82 82 commitopts = [
83 83 ('m', 'message', '',
84 84 _('use text as commit message'), _('TEXT')),
85 85 ('l', 'logfile', '',
86 86 _('read commit message from file'), _('FILE')),
87 87 ]
88 88
89 89 commitopts2 = [
90 90 ('d', 'date', '',
91 91 _('record the specified date as commit date'), _('DATE')),
92 92 ('u', 'user', '',
93 93 _('record the specified user as committer'), _('USER')),
94 94 ]
95 95
96 96 # hidden for now
97 97 formatteropts = [
98 98 ('T', 'template', '',
99 99 _('display with template (EXPERIMENTAL)'), _('TEMPLATE')),
100 100 ]
101 101
102 102 templateopts = [
103 103 ('', 'style', '',
104 104 _('display using template map file (DEPRECATED)'), _('STYLE')),
105 105 ('T', 'template', '',
106 106 _('display with template'), _('TEMPLATE')),
107 107 ]
108 108
109 109 logopts = [
110 110 ('p', 'patch', None, _('show patch')),
111 111 ('g', 'git', None, _('use git extended diff format')),
112 112 ('l', 'limit', '',
113 113 _('limit number of changes displayed'), _('NUM')),
114 114 ('M', 'no-merges', None, _('do not show merges')),
115 115 ('', 'stat', None, _('output diffstat-style summary of changes')),
116 116 ('G', 'graph', None, _("show the revision DAG")),
117 117 ] + templateopts
118 118
119 119 diffopts = [
120 120 ('a', 'text', None, _('treat all files as text')),
121 121 ('g', 'git', None, _('use git extended diff format')),
122 122 ('', 'binary', None, _('generate binary diffs in git mode (default)')),
123 123 ('', 'nodates', None, _('omit dates from diff headers'))
124 124 ]
125 125
126 126 diffwsopts = [
127 127 ('w', 'ignore-all-space', None,
128 128 _('ignore white space when comparing lines')),
129 129 ('b', 'ignore-space-change', None,
130 130 _('ignore changes in the amount of white space')),
131 131 ('B', 'ignore-blank-lines', None,
132 132 _('ignore changes whose lines are all blank')),
133 133 ('Z', 'ignore-space-at-eol', None,
134 134 _('ignore changes in whitespace at EOL')),
135 135 ]
136 136
137 137 diffopts2 = [
138 138 ('', 'noprefix', None, _('omit a/ and b/ prefixes from filenames')),
139 139 ('p', 'show-function', None, _('show which function each change is in')),
140 140 ('', 'reverse', None, _('produce a diff that undoes the changes')),
141 141 ] + diffwsopts + [
142 142 ('U', 'unified', '',
143 143 _('number of lines of context to show'), _('NUM')),
144 144 ('', 'stat', None, _('output diffstat-style summary of changes')),
145 145 ('', 'root', '', _('produce diffs relative to subdirectory'), _('DIR')),
146 146 ]
147 147
148 148 mergetoolopts = [
149 149 ('t', 'tool', '', _('specify merge tool')),
150 150 ]
151 151
152 152 similarityopts = [
153 153 ('s', 'similarity', '',
154 154 _('guess renamed files by similarity (0<=s<=100)'), _('SIMILARITY'))
155 155 ]
156 156
157 157 subrepoopts = [
158 158 ('S', 'subrepos', None,
159 159 _('recurse into subrepositories'))
160 160 ]
161 161
162 162 debugrevlogopts = [
163 163 ('c', 'changelog', False, _('open changelog')),
164 164 ('m', 'manifest', False, _('open manifest')),
165 165 ('', 'dir', '', _('open directory manifest')),
166 166 ]
167 167
168 168 # special string such that everything below this line will be ingored in the
169 169 # editor text
170 170 _linebelow = "^HG: ------------------------ >8 ------------------------$"
171 171
172 172 def ishunk(x):
173 173 hunkclasses = (crecordmod.uihunk, patch.recordhunk)
174 174 return isinstance(x, hunkclasses)
175 175
176 176 def newandmodified(chunks, originalchunks):
177 177 newlyaddedandmodifiedfiles = set()
178 178 for chunk in chunks:
179 179 if ishunk(chunk) and chunk.header.isnewfile() and chunk not in \
180 180 originalchunks:
181 181 newlyaddedandmodifiedfiles.add(chunk.header.filename())
182 182 return newlyaddedandmodifiedfiles
183 183
184 184 def parsealiases(cmd):
185 185 return cmd.lstrip("^").split("|")
186 186
187 187 def setupwrapcolorwrite(ui):
188 188 # wrap ui.write so diff output can be labeled/colorized
189 189 def wrapwrite(orig, *args, **kw):
190 190 label = kw.pop(r'label', '')
191 191 for chunk, l in patch.difflabel(lambda: args):
192 192 orig(chunk, label=label + l)
193 193
194 194 oldwrite = ui.write
195 195 def wrap(*args, **kwargs):
196 196 return wrapwrite(oldwrite, *args, **kwargs)
197 197 setattr(ui, 'write', wrap)
198 198 return oldwrite
199 199
200 200 def filterchunks(ui, originalhunks, usecurses, testfile, operation=None):
201 201 if usecurses:
202 202 if testfile:
203 203 recordfn = crecordmod.testdecorator(testfile,
204 204 crecordmod.testchunkselector)
205 205 else:
206 206 recordfn = crecordmod.chunkselector
207 207
208 208 return crecordmod.filterpatch(ui, originalhunks, recordfn, operation)
209 209
210 210 else:
211 211 return patch.filterpatch(ui, originalhunks, operation)
212 212
213 213 def recordfilter(ui, originalhunks, operation=None):
214 214 """ Prompts the user to filter the originalhunks and return a list of
215 215 selected hunks.
216 216 *operation* is used for to build ui messages to indicate the user what
217 217 kind of filtering they are doing: reverting, committing, shelving, etc.
218 218 (see patch.filterpatch).
219 219 """
220 220 usecurses = crecordmod.checkcurses(ui)
221 221 testfile = ui.config('experimental', 'crecordtest')
222 222 oldwrite = setupwrapcolorwrite(ui)
223 223 try:
224 224 newchunks, newopts = filterchunks(ui, originalhunks, usecurses,
225 225 testfile, operation)
226 226 finally:
227 227 ui.write = oldwrite
228 228 return newchunks, newopts
229 229
230 230 def dorecord(ui, repo, commitfunc, cmdsuggest, backupall,
231 231 filterfn, *pats, **opts):
232 232 opts = pycompat.byteskwargs(opts)
233 233 if not ui.interactive():
234 234 if cmdsuggest:
235 235 msg = _('running non-interactively, use %s instead') % cmdsuggest
236 236 else:
237 237 msg = _('running non-interactively')
238 238 raise error.Abort(msg)
239 239
240 240 # make sure username is set before going interactive
241 241 if not opts.get('user'):
242 242 ui.username() # raise exception, username not provided
243 243
244 244 def recordfunc(ui, repo, message, match, opts):
245 245 """This is generic record driver.
246 246
247 247 Its job is to interactively filter local changes, and
248 248 accordingly prepare working directory into a state in which the
249 249 job can be delegated to a non-interactive commit command such as
250 250 'commit' or 'qrefresh'.
251 251
252 252 After the actual job is done by non-interactive command, the
253 253 working directory is restored to its original state.
254 254
255 255 In the end we'll record interesting changes, and everything else
256 256 will be left in place, so the user can continue working.
257 257 """
258 258
259 259 checkunfinished(repo, commit=True)
260 260 wctx = repo[None]
261 261 merge = len(wctx.parents()) > 1
262 262 if merge:
263 263 raise error.Abort(_('cannot partially commit a merge '
264 264 '(use "hg commit" instead)'))
265 265
266 266 def fail(f, msg):
267 267 raise error.Abort('%s: %s' % (f, msg))
268 268
269 269 force = opts.get('force')
270 270 if not force:
271 271 vdirs = []
272 272 match.explicitdir = vdirs.append
273 273 match.bad = fail
274 274
275 275 status = repo.status(match=match)
276 276 if not force:
277 277 repo.checkcommitpatterns(wctx, vdirs, match, status, fail)
278 278 diffopts = patch.difffeatureopts(ui, opts=opts, whitespace=True)
279 279 diffopts.nodates = True
280 280 diffopts.git = True
281 281 diffopts.showfunc = True
282 282 originaldiff = patch.diff(repo, changes=status, opts=diffopts)
283 283 originalchunks = patch.parsepatch(originaldiff)
284 284
285 285 # 1. filter patch, since we are intending to apply subset of it
286 286 try:
287 287 chunks, newopts = filterfn(ui, originalchunks)
288 288 except error.PatchError as err:
289 289 raise error.Abort(_('error parsing patch: %s') % err)
290 290 opts.update(newopts)
291 291
292 292 # We need to keep a backup of files that have been newly added and
293 293 # modified during the recording process because there is a previous
294 294 # version without the edit in the workdir
295 295 newlyaddedandmodifiedfiles = newandmodified(chunks, originalchunks)
296 296 contenders = set()
297 297 for h in chunks:
298 298 try:
299 299 contenders.update(set(h.files()))
300 300 except AttributeError:
301 301 pass
302 302
303 303 changed = status.modified + status.added + status.removed
304 304 newfiles = [f for f in changed if f in contenders]
305 305 if not newfiles:
306 306 ui.status(_('no changes to record\n'))
307 307 return 0
308 308
309 309 modified = set(status.modified)
310 310
311 311 # 2. backup changed files, so we can restore them in the end
312 312
313 313 if backupall:
314 314 tobackup = changed
315 315 else:
316 316 tobackup = [f for f in newfiles if f in modified or f in \
317 317 newlyaddedandmodifiedfiles]
318 318 backups = {}
319 319 if tobackup:
320 320 backupdir = repo.vfs.join('record-backups')
321 321 try:
322 322 os.mkdir(backupdir)
323 323 except OSError as err:
324 324 if err.errno != errno.EEXIST:
325 325 raise
326 326 try:
327 327 # backup continues
328 328 for f in tobackup:
329 329 fd, tmpname = tempfile.mkstemp(prefix=f.replace('/', '_')+'.',
330 330 dir=backupdir)
331 331 os.close(fd)
332 332 ui.debug('backup %r as %r\n' % (f, tmpname))
333 333 util.copyfile(repo.wjoin(f), tmpname, copystat=True)
334 334 backups[f] = tmpname
335 335
336 336 fp = stringio()
337 337 for c in chunks:
338 338 fname = c.filename()
339 339 if fname in backups:
340 340 c.write(fp)
341 341 dopatch = fp.tell()
342 342 fp.seek(0)
343 343
344 344 # 2.5 optionally review / modify patch in text editor
345 345 if opts.get('review', False):
346 346 patchtext = (crecordmod.diffhelptext
347 347 + crecordmod.patchhelptext
348 348 + fp.read())
349 349 reviewedpatch = ui.edit(patchtext, "",
350 350 action="diff",
351 351 repopath=repo.path)
352 352 fp.truncate(0)
353 353 fp.write(reviewedpatch)
354 354 fp.seek(0)
355 355
356 356 [os.unlink(repo.wjoin(c)) for c in newlyaddedandmodifiedfiles]
357 357 # 3a. apply filtered patch to clean repo (clean)
358 358 if backups:
359 359 # Equivalent to hg.revert
360 360 m = scmutil.matchfiles(repo, backups.keys())
361 361 mergemod.update(repo, repo.dirstate.p1(),
362 362 False, True, matcher=m)
363 363
364 364 # 3b. (apply)
365 365 if dopatch:
366 366 try:
367 367 ui.debug('applying patch\n')
368 368 ui.debug(fp.getvalue())
369 369 patch.internalpatch(ui, repo, fp, 1, eolmode=None)
370 370 except error.PatchError as err:
371 371 raise error.Abort(pycompat.bytestr(err))
372 372 del fp
373 373
374 374 # 4. We prepared working directory according to filtered
375 375 # patch. Now is the time to delegate the job to
376 376 # commit/qrefresh or the like!
377 377
378 378 # Make all of the pathnames absolute.
379 379 newfiles = [repo.wjoin(nf) for nf in newfiles]
380 380 return commitfunc(ui, repo, *newfiles, **pycompat.strkwargs(opts))
381 381 finally:
382 382 # 5. finally restore backed-up files
383 383 try:
384 384 dirstate = repo.dirstate
385 385 for realname, tmpname in backups.iteritems():
386 386 ui.debug('restoring %r to %r\n' % (tmpname, realname))
387 387
388 388 if dirstate[realname] == 'n':
389 389 # without normallookup, restoring timestamp
390 390 # may cause partially committed files
391 391 # to be treated as unmodified
392 392 dirstate.normallookup(realname)
393 393
394 394 # copystat=True here and above are a hack to trick any
395 395 # editors that have f open that we haven't modified them.
396 396 #
397 397 # Also note that this racy as an editor could notice the
398 398 # file's mtime before we've finished writing it.
399 399 util.copyfile(tmpname, repo.wjoin(realname), copystat=True)
400 400 os.unlink(tmpname)
401 401 if tobackup:
402 402 os.rmdir(backupdir)
403 403 except OSError:
404 404 pass
405 405
406 406 def recordinwlock(ui, repo, message, match, opts):
407 407 with repo.wlock():
408 408 return recordfunc(ui, repo, message, match, opts)
409 409
410 410 return commit(ui, repo, recordinwlock, pats, opts)
411 411
412 412 class dirnode(object):
413 413 """
414 414 Represent a directory in user working copy with information required for
415 415 the purpose of tersing its status.
416 416
417 417 path is the path to the directory
418 418
419 419 statuses is a set of statuses of all files in this directory (this includes
420 420 all the files in all the subdirectories too)
421 421
422 422 files is a list of files which are direct child of this directory
423 423
424 424 subdirs is a dictionary of sub-directory name as the key and it's own
425 425 dirnode object as the value
426 426 """
427 427
428 428 def __init__(self, dirpath):
429 429 self.path = dirpath
430 430 self.statuses = set([])
431 431 self.files = []
432 432 self.subdirs = {}
433 433
434 434 def _addfileindir(self, filename, status):
435 435 """Add a file in this directory as a direct child."""
436 436 self.files.append((filename, status))
437 437
438 438 def addfile(self, filename, status):
439 439 """
440 440 Add a file to this directory or to its direct parent directory.
441 441
442 442 If the file is not direct child of this directory, we traverse to the
443 443 directory of which this file is a direct child of and add the file
444 444 there.
445 445 """
446 446
447 447 # the filename contains a path separator, it means it's not the direct
448 448 # child of this directory
449 449 if '/' in filename:
450 450 subdir, filep = filename.split('/', 1)
451 451
452 452 # does the dirnode object for subdir exists
453 453 if subdir not in self.subdirs:
454 454 subdirpath = os.path.join(self.path, subdir)
455 455 self.subdirs[subdir] = dirnode(subdirpath)
456 456
457 457 # try adding the file in subdir
458 458 self.subdirs[subdir].addfile(filep, status)
459 459
460 460 else:
461 461 self._addfileindir(filename, status)
462 462
463 463 if status not in self.statuses:
464 464 self.statuses.add(status)
465 465
466 466 def iterfilepaths(self):
467 467 """Yield (status, path) for files directly under this directory."""
468 468 for f, st in self.files:
469 469 yield st, os.path.join(self.path, f)
470 470
471 471 def tersewalk(self, terseargs):
472 472 """
473 473 Yield (status, path) obtained by processing the status of this
474 474 dirnode.
475 475
476 476 terseargs is the string of arguments passed by the user with `--terse`
477 477 flag.
478 478
479 479 Following are the cases which can happen:
480 480
481 481 1) All the files in the directory (including all the files in its
482 482 subdirectories) share the same status and the user has asked us to terse
483 483 that status. -> yield (status, dirpath)
484 484
485 485 2) Otherwise, we do following:
486 486
487 487 a) Yield (status, filepath) for all the files which are in this
488 488 directory (only the ones in this directory, not the subdirs)
489 489
490 490 b) Recurse the function on all the subdirectories of this
491 491 directory
492 492 """
493 493
494 494 if len(self.statuses) == 1:
495 495 onlyst = self.statuses.pop()
496 496
497 497 # Making sure we terse only when the status abbreviation is
498 498 # passed as terse argument
499 499 if onlyst in terseargs:
500 500 yield onlyst, self.path + pycompat.ossep
501 501 return
502 502
503 503 # add the files to status list
504 504 for st, fpath in self.iterfilepaths():
505 505 yield st, fpath
506 506
507 507 #recurse on the subdirs
508 508 for dirobj in self.subdirs.values():
509 509 for st, fpath in dirobj.tersewalk(terseargs):
510 510 yield st, fpath
511 511
512 512 def tersedir(statuslist, terseargs):
513 513 """
514 514 Terse the status if all the files in a directory shares the same status.
515 515
516 516 statuslist is scmutil.status() object which contains a list of files for
517 517 each status.
518 518 terseargs is string which is passed by the user as the argument to `--terse`
519 519 flag.
520 520
521 521 The function makes a tree of objects of dirnode class, and at each node it
522 522 stores the information required to know whether we can terse a certain
523 523 directory or not.
524 524 """
525 525 # the order matters here as that is used to produce final list
526 526 allst = ('m', 'a', 'r', 'd', 'u', 'i', 'c')
527 527
528 528 # checking the argument validity
529 529 for s in pycompat.bytestr(terseargs):
530 530 if s not in allst:
531 531 raise error.Abort(_("'%s' not recognized") % s)
532 532
533 533 # creating a dirnode object for the root of the repo
534 534 rootobj = dirnode('')
535 535 pstatus = ('modified', 'added', 'deleted', 'clean', 'unknown',
536 536 'ignored', 'removed')
537 537
538 538 tersedict = {}
539 539 for attrname in pstatus:
540 540 statuschar = attrname[0:1]
541 541 for f in getattr(statuslist, attrname):
542 542 rootobj.addfile(f, statuschar)
543 543 tersedict[statuschar] = []
544 544
545 545 # we won't be tersing the root dir, so add files in it
546 546 for st, fpath in rootobj.iterfilepaths():
547 547 tersedict[st].append(fpath)
548 548
549 549 # process each sub-directory and build tersedict
550 550 for subdir in rootobj.subdirs.values():
551 551 for st, f in subdir.tersewalk(terseargs):
552 552 tersedict[st].append(f)
553 553
554 554 tersedlist = []
555 555 for st in allst:
556 556 tersedict[st].sort()
557 557 tersedlist.append(tersedict[st])
558 558
559 559 return tersedlist
560 560
561 561 def _commentlines(raw):
562 562 '''Surround lineswith a comment char and a new line'''
563 563 lines = raw.splitlines()
564 564 commentedlines = ['# %s' % line for line in lines]
565 565 return '\n'.join(commentedlines) + '\n'
566 566
567 567 def _conflictsmsg(repo):
568 568 mergestate = mergemod.mergestate.read(repo)
569 569 if not mergestate.active():
570 570 return
571 571
572 572 m = scmutil.match(repo[None])
573 573 unresolvedlist = [f for f in mergestate.unresolved() if m(f)]
574 574 if unresolvedlist:
575 575 mergeliststr = '\n'.join(
576 576 [' %s' % util.pathto(repo.root, pycompat.getcwd(), path)
577 577 for path in unresolvedlist])
578 578 msg = _('''Unresolved merge conflicts:
579 579
580 580 %s
581 581
582 582 To mark files as resolved: hg resolve --mark FILE''') % mergeliststr
583 583 else:
584 584 msg = _('No unresolved merge conflicts.')
585 585
586 586 return _commentlines(msg)
587 587
588 588 def _helpmessage(continuecmd, abortcmd):
589 589 msg = _('To continue: %s\n'
590 590 'To abort: %s') % (continuecmd, abortcmd)
591 591 return _commentlines(msg)
592 592
593 593 def _rebasemsg():
594 594 return _helpmessage('hg rebase --continue', 'hg rebase --abort')
595 595
596 596 def _histeditmsg():
597 597 return _helpmessage('hg histedit --continue', 'hg histedit --abort')
598 598
599 599 def _unshelvemsg():
600 600 return _helpmessage('hg unshelve --continue', 'hg unshelve --abort')
601 601
602 602 def _updatecleanmsg(dest=None):
603 603 warning = _('warning: this will discard uncommitted changes')
604 604 return 'hg update --clean %s (%s)' % (dest or '.', warning)
605 605
606 606 def _graftmsg():
607 607 # tweakdefaults requires `update` to have a rev hence the `.`
608 608 return _helpmessage('hg graft --continue', _updatecleanmsg())
609 609
610 610 def _mergemsg():
611 611 # tweakdefaults requires `update` to have a rev hence the `.`
612 612 return _helpmessage('hg commit', _updatecleanmsg())
613 613
614 614 def _bisectmsg():
615 615 msg = _('To mark the changeset good: hg bisect --good\n'
616 616 'To mark the changeset bad: hg bisect --bad\n'
617 617 'To abort: hg bisect --reset\n')
618 618 return _commentlines(msg)
619 619
620 620 def fileexistspredicate(filename):
621 621 return lambda repo: repo.vfs.exists(filename)
622 622
623 623 def _mergepredicate(repo):
624 624 return len(repo[None].parents()) > 1
625 625
626 626 STATES = (
627 627 # (state, predicate to detect states, helpful message function)
628 628 ('histedit', fileexistspredicate('histedit-state'), _histeditmsg),
629 629 ('bisect', fileexistspredicate('bisect.state'), _bisectmsg),
630 630 ('graft', fileexistspredicate('graftstate'), _graftmsg),
631 631 ('unshelve', fileexistspredicate('unshelverebasestate'), _unshelvemsg),
632 632 ('rebase', fileexistspredicate('rebasestate'), _rebasemsg),
633 633 # The merge state is part of a list that will be iterated over.
634 634 # They need to be last because some of the other unfinished states may also
635 635 # be in a merge or update state (eg. rebase, histedit, graft, etc).
636 636 # We want those to have priority.
637 637 ('merge', _mergepredicate, _mergemsg),
638 638 )
639 639
640 640 def _getrepostate(repo):
641 641 # experimental config: commands.status.skipstates
642 642 skip = set(repo.ui.configlist('commands', 'status.skipstates'))
643 643 for state, statedetectionpredicate, msgfn in STATES:
644 644 if state in skip:
645 645 continue
646 646 if statedetectionpredicate(repo):
647 647 return (state, statedetectionpredicate, msgfn)
648 648
649 649 def morestatus(repo, fm):
650 650 statetuple = _getrepostate(repo)
651 651 label = 'status.morestatus'
652 652 if statetuple:
653 653 fm.startitem()
654 654 state, statedetectionpredicate, helpfulmsg = statetuple
655 655 statemsg = _('The repository is in an unfinished *%s* state.') % state
656 656 fm.write('statemsg', '%s\n', _commentlines(statemsg), label=label)
657 657 conmsg = _conflictsmsg(repo)
658 658 if conmsg:
659 659 fm.write('conflictsmsg', '%s\n', conmsg, label=label)
660 660 if helpfulmsg:
661 661 helpmsg = helpfulmsg()
662 662 fm.write('helpmsg', '%s\n', helpmsg, label=label)
663 663
664 664 def findpossible(cmd, table, strict=False):
665 665 """
666 666 Return cmd -> (aliases, command table entry)
667 667 for each matching command.
668 668 Return debug commands (or their aliases) only if no normal command matches.
669 669 """
670 670 choice = {}
671 671 debugchoice = {}
672 672
673 673 if cmd in table:
674 674 # short-circuit exact matches, "log" alias beats "^log|history"
675 675 keys = [cmd]
676 676 else:
677 677 keys = table.keys()
678 678
679 679 allcmds = []
680 680 for e in keys:
681 681 aliases = parsealiases(e)
682 682 allcmds.extend(aliases)
683 683 found = None
684 684 if cmd in aliases:
685 685 found = cmd
686 686 elif not strict:
687 687 for a in aliases:
688 688 if a.startswith(cmd):
689 689 found = a
690 690 break
691 691 if found is not None:
692 692 if aliases[0].startswith("debug") or found.startswith("debug"):
693 693 debugchoice[found] = (aliases, table[e])
694 694 else:
695 695 choice[found] = (aliases, table[e])
696 696
697 697 if not choice and debugchoice:
698 698 choice = debugchoice
699 699
700 700 return choice, allcmds
701 701
702 702 def findcmd(cmd, table, strict=True):
703 703 """Return (aliases, command table entry) for command string."""
704 704 choice, allcmds = findpossible(cmd, table, strict)
705 705
706 706 if cmd in choice:
707 707 return choice[cmd]
708 708
709 709 if len(choice) > 1:
710 710 clist = sorted(choice)
711 711 raise error.AmbiguousCommand(cmd, clist)
712 712
713 713 if choice:
714 714 return list(choice.values())[0]
715 715
716 716 raise error.UnknownCommand(cmd, allcmds)
717 717
718 718 def changebranch(ui, repo, revs, label):
719 719 """ Change the branch name of given revs to label """
720 720
721 721 with repo.wlock(), repo.lock(), repo.transaction('branches'):
722 722 # abort in case of uncommitted merge or dirty wdir
723 723 bailifchanged(repo)
724 724 revs = scmutil.revrange(repo, revs)
725 725 if not revs:
726 726 raise error.Abort("empty revision set")
727 727 roots = repo.revs('roots(%ld)', revs)
728 728 if len(roots) > 1:
729 729 raise error.Abort(_("cannot change branch of non-linear revisions"))
730 730 rewriteutil.precheck(repo, revs, 'change branch of')
731 731
732 732 root = repo[roots.first()]
733 733 if not root.p1().branch() == label and label in repo.branchmap():
734 734 raise error.Abort(_("a branch of the same name already exists"))
735 735
736 736 if repo.revs('merge() and %ld', revs):
737 737 raise error.Abort(_("cannot change branch of a merge commit"))
738 738 if repo.revs('obsolete() and %ld', revs):
739 739 raise error.Abort(_("cannot change branch of a obsolete changeset"))
740 740
741 741 # make sure only topological heads
742 742 if repo.revs('heads(%ld) - head()', revs):
743 743 raise error.Abort(_("cannot change branch in middle of a stack"))
744 744
745 745 replacements = {}
746 746 # avoid import cycle mercurial.cmdutil -> mercurial.context ->
747 747 # mercurial.subrepo -> mercurial.cmdutil
748 748 from . import context
749 749 for rev in revs:
750 750 ctx = repo[rev]
751 751 oldbranch = ctx.branch()
752 752 # check if ctx has same branch
753 753 if oldbranch == label:
754 754 continue
755 755
756 756 def filectxfn(repo, newctx, path):
757 757 try:
758 758 return ctx[path]
759 759 except error.ManifestLookupError:
760 760 return None
761 761
762 762 ui.debug("changing branch of '%s' from '%s' to '%s'\n"
763 763 % (hex(ctx.node()), oldbranch, label))
764 764 extra = ctx.extra()
765 765 extra['branch_change'] = hex(ctx.node())
766 766 # While changing branch of set of linear commits, make sure that
767 767 # we base our commits on new parent rather than old parent which
768 768 # was obsoleted while changing the branch
769 769 p1 = ctx.p1().node()
770 770 p2 = ctx.p2().node()
771 771 if p1 in replacements:
772 772 p1 = replacements[p1][0]
773 773 if p2 in replacements:
774 774 p2 = replacements[p2][0]
775 775
776 776 mc = context.memctx(repo, (p1, p2),
777 777 ctx.description(),
778 778 ctx.files(),
779 779 filectxfn,
780 780 user=ctx.user(),
781 781 date=ctx.date(),
782 782 extra=extra,
783 783 branch=label)
784 784
785 785 commitphase = ctx.phase()
786 786 overrides = {('phases', 'new-commit'): commitphase}
787 787 with repo.ui.configoverride(overrides, 'branch-change'):
788 788 newnode = repo.commitctx(mc)
789 789
790 790 replacements[ctx.node()] = (newnode,)
791 791 ui.debug('new node id is %s\n' % hex(newnode))
792 792
793 793 # create obsmarkers and move bookmarks
794 794 scmutil.cleanupnodes(repo, replacements, 'branch-change')
795 795
796 796 # move the working copy too
797 797 wctx = repo[None]
798 798 # in-progress merge is a bit too complex for now.
799 799 if len(wctx.parents()) == 1:
800 800 newid = replacements.get(wctx.p1().node())
801 801 if newid is not None:
802 802 # avoid import cycle mercurial.cmdutil -> mercurial.hg ->
803 803 # mercurial.cmdutil
804 804 from . import hg
805 805 hg.update(repo, newid[0], quietempty=True)
806 806
807 807 ui.status(_("changed branch on %d changesets\n") % len(replacements))
808 808
809 809 def findrepo(p):
810 810 while not os.path.isdir(os.path.join(p, ".hg")):
811 811 oldp, p = p, os.path.dirname(p)
812 812 if p == oldp:
813 813 return None
814 814
815 815 return p
816 816
817 817 def bailifchanged(repo, merge=True, hint=None):
818 818 """ enforce the precondition that working directory must be clean.
819 819
820 820 'merge' can be set to false if a pending uncommitted merge should be
821 821 ignored (such as when 'update --check' runs).
822 822
823 823 'hint' is the usual hint given to Abort exception.
824 824 """
825 825
826 826 if merge and repo.dirstate.p2() != nullid:
827 827 raise error.Abort(_('outstanding uncommitted merge'), hint=hint)
828 828 modified, added, removed, deleted = repo.status()[:4]
829 829 if modified or added or removed or deleted:
830 830 raise error.Abort(_('uncommitted changes'), hint=hint)
831 831 ctx = repo[None]
832 832 for s in sorted(ctx.substate):
833 833 ctx.sub(s).bailifchanged(hint=hint)
834 834
835 835 def logmessage(ui, opts):
836 836 """ get the log message according to -m and -l option """
837 837 message = opts.get('message')
838 838 logfile = opts.get('logfile')
839 839
840 840 if message and logfile:
841 841 raise error.Abort(_('options --message and --logfile are mutually '
842 842 'exclusive'))
843 843 if not message and logfile:
844 844 try:
845 845 if isstdiofilename(logfile):
846 846 message = ui.fin.read()
847 847 else:
848 848 message = '\n'.join(util.readfile(logfile).splitlines())
849 849 except IOError as inst:
850 850 raise error.Abort(_("can't read commit message '%s': %s") %
851 851 (logfile, encoding.strtolocal(inst.strerror)))
852 852 return message
853 853
854 854 def mergeeditform(ctxorbool, baseformname):
855 855 """return appropriate editform name (referencing a committemplate)
856 856
857 857 'ctxorbool' is either a ctx to be committed, or a bool indicating whether
858 858 merging is committed.
859 859
860 860 This returns baseformname with '.merge' appended if it is a merge,
861 861 otherwise '.normal' is appended.
862 862 """
863 863 if isinstance(ctxorbool, bool):
864 864 if ctxorbool:
865 865 return baseformname + ".merge"
866 866 elif 1 < len(ctxorbool.parents()):
867 867 return baseformname + ".merge"
868 868
869 869 return baseformname + ".normal"
870 870
871 871 def getcommiteditor(edit=False, finishdesc=None, extramsg=None,
872 872 editform='', **opts):
873 873 """get appropriate commit message editor according to '--edit' option
874 874
875 875 'finishdesc' is a function to be called with edited commit message
876 876 (= 'description' of the new changeset) just after editing, but
877 877 before checking empty-ness. It should return actual text to be
878 878 stored into history. This allows to change description before
879 879 storing.
880 880
881 881 'extramsg' is a extra message to be shown in the editor instead of
882 882 'Leave message empty to abort commit' line. 'HG: ' prefix and EOL
883 883 is automatically added.
884 884
885 885 'editform' is a dot-separated list of names, to distinguish
886 886 the purpose of commit text editing.
887 887
888 888 'getcommiteditor' returns 'commitforceeditor' regardless of
889 889 'edit', if one of 'finishdesc' or 'extramsg' is specified, because
890 890 they are specific for usage in MQ.
891 891 """
892 892 if edit or finishdesc or extramsg:
893 893 return lambda r, c, s: commitforceeditor(r, c, s,
894 894 finishdesc=finishdesc,
895 895 extramsg=extramsg,
896 896 editform=editform)
897 897 elif editform:
898 898 return lambda r, c, s: commiteditor(r, c, s, editform=editform)
899 899 else:
900 900 return commiteditor
901 901
902 902 def rendertemplate(ctx, tmpl, props=None):
903 903 """Expand a literal template 'tmpl' byte-string against one changeset
904 904
905 905 Each props item must be a stringify-able value or a callable returning
906 906 such value, i.e. no bare list nor dict should be passed.
907 907 """
908 908 repo = ctx.repo()
909 909 tres = formatter.templateresources(repo.ui, repo)
910 910 t = formatter.maketemplater(repo.ui, tmpl, defaults=templatekw.keywords,
911 911 resources=tres)
912 912 mapping = {'ctx': ctx, 'revcache': {}}
913 913 if props:
914 914 mapping.update(props)
915 915 return t.renderdefault(mapping)
916 916
917 917 def _buildfntemplate(pat, total=None, seqno=None, revwidth=None, pathname=None):
918 918 r"""Convert old-style filename format string to template string
919 919
920 920 >>> _buildfntemplate(b'foo-%b-%n.patch', seqno=0)
921 921 'foo-{reporoot|basename}-{seqno}.patch'
922 922 >>> _buildfntemplate(b'%R{tags % "{tag}"}%H')
923 923 '{rev}{tags % "{tag}"}{node}'
924 924
925 925 '\' in outermost strings has to be escaped because it is a directory
926 926 separator on Windows:
927 927
928 928 >>> _buildfntemplate(b'c:\\tmp\\%R\\%n.patch', seqno=0)
929 929 'c:\\\\tmp\\\\{rev}\\\\{seqno}.patch'
930 930 >>> _buildfntemplate(b'\\\\foo\\bar.patch')
931 931 '\\\\\\\\foo\\\\bar.patch'
932 932 >>> _buildfntemplate(b'\\{tags % "{tag}"}')
933 933 '\\\\{tags % "{tag}"}'
934 934
935 935 but inner strings follow the template rules (i.e. '\' is taken as an
936 936 escape character):
937 937
938 938 >>> _buildfntemplate(br'{"c:\tmp"}', seqno=0)
939 939 '{"c:\\tmp"}'
940 940 """
941 941 expander = {
942 942 b'H': b'{node}',
943 943 b'R': b'{rev}',
944 944 b'h': b'{node|short}',
945 945 b'm': br'{sub(r"[^\w]", "_", desc|firstline)}',
946 946 b'r': b'{if(revwidth, pad(rev, revwidth, "0", left=True), rev)}',
947 947 b'%': b'%',
948 948 b'b': b'{reporoot|basename}',
949 949 }
950 950 if total is not None:
951 951 expander[b'N'] = b'{total}'
952 952 if seqno is not None:
953 953 expander[b'n'] = b'{seqno}'
954 954 if total is not None and seqno is not None:
955 955 expander[b'n'] = b'{pad(seqno, total|stringify|count, "0", left=True)}'
956 956 if pathname is not None:
957 957 expander[b's'] = b'{pathname|basename}'
958 958 expander[b'd'] = b'{if(pathname|dirname, pathname|dirname, ".")}'
959 959 expander[b'p'] = b'{pathname}'
960 960
961 961 newname = []
962 962 for typ, start, end in templater.scantemplate(pat, raw=True):
963 963 if typ != b'string':
964 964 newname.append(pat[start:end])
965 965 continue
966 966 i = start
967 967 while i < end:
968 968 n = pat.find(b'%', i, end)
969 969 if n < 0:
970 970 newname.append(stringutil.escapestr(pat[i:end]))
971 971 break
972 972 newname.append(stringutil.escapestr(pat[i:n]))
973 973 if n + 2 > end:
974 974 raise error.Abort(_("incomplete format spec in output "
975 975 "filename"))
976 976 c = pat[n + 1:n + 2]
977 977 i = n + 2
978 978 try:
979 979 newname.append(expander[c])
980 980 except KeyError:
981 981 raise error.Abort(_("invalid format spec '%%%s' in output "
982 982 "filename") % c)
983 983 return ''.join(newname)
984 984
985 985 def makefilename(ctx, pat, **props):
986 986 if not pat:
987 987 return pat
988 988 tmpl = _buildfntemplate(pat, **props)
989 989 # BUG: alias expansion shouldn't be made against template fragments
990 990 # rewritten from %-format strings, but we have no easy way to partially
991 991 # disable the expansion.
992 992 return rendertemplate(ctx, tmpl, pycompat.byteskwargs(props))
993 993
994 994 def isstdiofilename(pat):
995 995 """True if the given pat looks like a filename denoting stdin/stdout"""
996 996 return not pat or pat == '-'
997 997
998 998 class _unclosablefile(object):
999 999 def __init__(self, fp):
1000 1000 self._fp = fp
1001 1001
1002 1002 def close(self):
1003 1003 pass
1004 1004
1005 1005 def __iter__(self):
1006 1006 return iter(self._fp)
1007 1007
1008 1008 def __getattr__(self, attr):
1009 1009 return getattr(self._fp, attr)
1010 1010
1011 1011 def __enter__(self):
1012 1012 return self
1013 1013
1014 1014 def __exit__(self, exc_type, exc_value, exc_tb):
1015 1015 pass
1016 1016
1017 1017 def makefileobj(ctx, pat, mode='wb', modemap=None, **props):
1018 1018 writable = mode not in ('r', 'rb')
1019 1019
1020 1020 if isstdiofilename(pat):
1021 1021 repo = ctx.repo()
1022 1022 if writable:
1023 1023 fp = repo.ui.fout
1024 1024 else:
1025 1025 fp = repo.ui.fin
1026 1026 return _unclosablefile(fp)
1027 1027 fn = makefilename(ctx, pat, **props)
1028 1028 if modemap is not None:
1029 1029 mode = modemap.get(fn, mode)
1030 1030 if mode == 'wb':
1031 1031 modemap[fn] = 'ab'
1032 1032 return open(fn, mode)
1033 1033
1034 1034 def openrevlog(repo, cmd, file_, opts):
1035 1035 """opens the changelog, manifest, a filelog or a given revlog"""
1036 1036 cl = opts['changelog']
1037 1037 mf = opts['manifest']
1038 1038 dir = opts['dir']
1039 1039 msg = None
1040 1040 if cl and mf:
1041 1041 msg = _('cannot specify --changelog and --manifest at the same time')
1042 1042 elif cl and dir:
1043 1043 msg = _('cannot specify --changelog and --dir at the same time')
1044 1044 elif cl or mf or dir:
1045 1045 if file_:
1046 1046 msg = _('cannot specify filename with --changelog or --manifest')
1047 1047 elif not repo:
1048 1048 msg = _('cannot specify --changelog or --manifest or --dir '
1049 1049 'without a repository')
1050 1050 if msg:
1051 1051 raise error.Abort(msg)
1052 1052
1053 1053 r = None
1054 1054 if repo:
1055 1055 if cl:
1056 1056 r = repo.unfiltered().changelog
1057 1057 elif dir:
1058 1058 if 'treemanifest' not in repo.requirements:
1059 1059 raise error.Abort(_("--dir can only be used on repos with "
1060 1060 "treemanifest enabled"))
1061 1061 dirlog = repo.manifestlog._revlog.dirlog(dir)
1062 1062 if len(dirlog):
1063 1063 r = dirlog
1064 1064 elif mf:
1065 1065 r = repo.manifestlog._revlog
1066 1066 elif file_:
1067 1067 filelog = repo.file(file_)
1068 1068 if len(filelog):
1069 1069 r = filelog
1070 1070 if not r:
1071 1071 if not file_:
1072 1072 raise error.CommandError(cmd, _('invalid arguments'))
1073 1073 if not os.path.isfile(file_):
1074 1074 raise error.Abort(_("revlog '%s' not found") % file_)
1075 1075 r = revlog.revlog(vfsmod.vfs(pycompat.getcwd(), audit=False),
1076 1076 file_[:-2] + ".i")
1077 1077 return r
1078 1078
1079 1079 def copy(ui, repo, pats, opts, rename=False):
1080 1080 # called with the repo lock held
1081 1081 #
1082 1082 # hgsep => pathname that uses "/" to separate directories
1083 1083 # ossep => pathname that uses os.sep to separate directories
1084 1084 cwd = repo.getcwd()
1085 1085 targets = {}
1086 1086 after = opts.get("after")
1087 1087 dryrun = opts.get("dry_run")
1088 1088 wctx = repo[None]
1089 1089
1090 1090 def walkpat(pat):
1091 1091 srcs = []
1092 1092 if after:
1093 1093 badstates = '?'
1094 1094 else:
1095 1095 badstates = '?r'
1096 1096 m = scmutil.match(wctx, [pat], opts, globbed=True)
1097 1097 for abs in wctx.walk(m):
1098 1098 state = repo.dirstate[abs]
1099 1099 rel = m.rel(abs)
1100 1100 exact = m.exact(abs)
1101 1101 if state in badstates:
1102 1102 if exact and state == '?':
1103 1103 ui.warn(_('%s: not copying - file is not managed\n') % rel)
1104 1104 if exact and state == 'r':
1105 1105 ui.warn(_('%s: not copying - file has been marked for'
1106 1106 ' remove\n') % rel)
1107 1107 continue
1108 1108 # abs: hgsep
1109 1109 # rel: ossep
1110 1110 srcs.append((abs, rel, exact))
1111 1111 return srcs
1112 1112
1113 1113 # abssrc: hgsep
1114 1114 # relsrc: ossep
1115 1115 # otarget: ossep
1116 1116 def copyfile(abssrc, relsrc, otarget, exact):
1117 1117 abstarget = pathutil.canonpath(repo.root, cwd, otarget)
1118 1118 if '/' in abstarget:
1119 1119 # We cannot normalize abstarget itself, this would prevent
1120 1120 # case only renames, like a => A.
1121 1121 abspath, absname = abstarget.rsplit('/', 1)
1122 1122 abstarget = repo.dirstate.normalize(abspath) + '/' + absname
1123 1123 reltarget = repo.pathto(abstarget, cwd)
1124 1124 target = repo.wjoin(abstarget)
1125 1125 src = repo.wjoin(abssrc)
1126 1126 state = repo.dirstate[abstarget]
1127 1127
1128 1128 scmutil.checkportable(ui, abstarget)
1129 1129
1130 1130 # check for collisions
1131 1131 prevsrc = targets.get(abstarget)
1132 1132 if prevsrc is not None:
1133 1133 ui.warn(_('%s: not overwriting - %s collides with %s\n') %
1134 1134 (reltarget, repo.pathto(abssrc, cwd),
1135 1135 repo.pathto(prevsrc, cwd)))
1136 1136 return
1137 1137
1138 1138 # check for overwrites
1139 1139 exists = os.path.lexists(target)
1140 1140 samefile = False
1141 1141 if exists and abssrc != abstarget:
1142 1142 if (repo.dirstate.normalize(abssrc) ==
1143 1143 repo.dirstate.normalize(abstarget)):
1144 1144 if not rename:
1145 1145 ui.warn(_("%s: can't copy - same file\n") % reltarget)
1146 1146 return
1147 1147 exists = False
1148 1148 samefile = True
1149 1149
1150 1150 if not after and exists or after and state in 'mn':
1151 1151 if not opts['force']:
1152 1152 if state in 'mn':
1153 1153 msg = _('%s: not overwriting - file already committed\n')
1154 1154 if after:
1155 1155 flags = '--after --force'
1156 1156 else:
1157 1157 flags = '--force'
1158 1158 if rename:
1159 1159 hint = _('(hg rename %s to replace the file by '
1160 1160 'recording a rename)\n') % flags
1161 1161 else:
1162 1162 hint = _('(hg copy %s to replace the file by '
1163 1163 'recording a copy)\n') % flags
1164 1164 else:
1165 1165 msg = _('%s: not overwriting - file exists\n')
1166 1166 if rename:
1167 1167 hint = _('(hg rename --after to record the rename)\n')
1168 1168 else:
1169 1169 hint = _('(hg copy --after to record the copy)\n')
1170 1170 ui.warn(msg % reltarget)
1171 1171 ui.warn(hint)
1172 1172 return
1173 1173
1174 1174 if after:
1175 1175 if not exists:
1176 1176 if rename:
1177 1177 ui.warn(_('%s: not recording move - %s does not exist\n') %
1178 1178 (relsrc, reltarget))
1179 1179 else:
1180 1180 ui.warn(_('%s: not recording copy - %s does not exist\n') %
1181 1181 (relsrc, reltarget))
1182 1182 return
1183 1183 elif not dryrun:
1184 1184 try:
1185 1185 if exists:
1186 1186 os.unlink(target)
1187 1187 targetdir = os.path.dirname(target) or '.'
1188 1188 if not os.path.isdir(targetdir):
1189 1189 os.makedirs(targetdir)
1190 1190 if samefile:
1191 1191 tmp = target + "~hgrename"
1192 1192 os.rename(src, tmp)
1193 1193 os.rename(tmp, target)
1194 1194 else:
1195 util.copyfile(src, target)
1195 # Preserve stat info on renames, not on copies; this matches
1196 # Linux CLI behavior.
1197 util.copyfile(src, target, copystat=rename)
1196 1198 srcexists = True
1197 1199 except IOError as inst:
1198 1200 if inst.errno == errno.ENOENT:
1199 1201 ui.warn(_('%s: deleted in working directory\n') % relsrc)
1200 1202 srcexists = False
1201 1203 else:
1202 1204 ui.warn(_('%s: cannot copy - %s\n') %
1203 1205 (relsrc, encoding.strtolocal(inst.strerror)))
1204 1206 return True # report a failure
1205 1207
1206 1208 if ui.verbose or not exact:
1207 1209 if rename:
1208 1210 ui.status(_('moving %s to %s\n') % (relsrc, reltarget))
1209 1211 else:
1210 1212 ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
1211 1213
1212 1214 targets[abstarget] = abssrc
1213 1215
1214 1216 # fix up dirstate
1215 1217 scmutil.dirstatecopy(ui, repo, wctx, abssrc, abstarget,
1216 1218 dryrun=dryrun, cwd=cwd)
1217 1219 if rename and not dryrun:
1218 1220 if not after and srcexists and not samefile:
1219 1221 repo.wvfs.unlinkpath(abssrc)
1220 1222 wctx.forget([abssrc])
1221 1223
1222 1224 # pat: ossep
1223 1225 # dest ossep
1224 1226 # srcs: list of (hgsep, hgsep, ossep, bool)
1225 1227 # return: function that takes hgsep and returns ossep
1226 1228 def targetpathfn(pat, dest, srcs):
1227 1229 if os.path.isdir(pat):
1228 1230 abspfx = pathutil.canonpath(repo.root, cwd, pat)
1229 1231 abspfx = util.localpath(abspfx)
1230 1232 if destdirexists:
1231 1233 striplen = len(os.path.split(abspfx)[0])
1232 1234 else:
1233 1235 striplen = len(abspfx)
1234 1236 if striplen:
1235 1237 striplen += len(pycompat.ossep)
1236 1238 res = lambda p: os.path.join(dest, util.localpath(p)[striplen:])
1237 1239 elif destdirexists:
1238 1240 res = lambda p: os.path.join(dest,
1239 1241 os.path.basename(util.localpath(p)))
1240 1242 else:
1241 1243 res = lambda p: dest
1242 1244 return res
1243 1245
1244 1246 # pat: ossep
1245 1247 # dest ossep
1246 1248 # srcs: list of (hgsep, hgsep, ossep, bool)
1247 1249 # return: function that takes hgsep and returns ossep
1248 1250 def targetpathafterfn(pat, dest, srcs):
1249 1251 if matchmod.patkind(pat):
1250 1252 # a mercurial pattern
1251 1253 res = lambda p: os.path.join(dest,
1252 1254 os.path.basename(util.localpath(p)))
1253 1255 else:
1254 1256 abspfx = pathutil.canonpath(repo.root, cwd, pat)
1255 1257 if len(abspfx) < len(srcs[0][0]):
1256 1258 # A directory. Either the target path contains the last
1257 1259 # component of the source path or it does not.
1258 1260 def evalpath(striplen):
1259 1261 score = 0
1260 1262 for s in srcs:
1261 1263 t = os.path.join(dest, util.localpath(s[0])[striplen:])
1262 1264 if os.path.lexists(t):
1263 1265 score += 1
1264 1266 return score
1265 1267
1266 1268 abspfx = util.localpath(abspfx)
1267 1269 striplen = len(abspfx)
1268 1270 if striplen:
1269 1271 striplen += len(pycompat.ossep)
1270 1272 if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
1271 1273 score = evalpath(striplen)
1272 1274 striplen1 = len(os.path.split(abspfx)[0])
1273 1275 if striplen1:
1274 1276 striplen1 += len(pycompat.ossep)
1275 1277 if evalpath(striplen1) > score:
1276 1278 striplen = striplen1
1277 1279 res = lambda p: os.path.join(dest,
1278 1280 util.localpath(p)[striplen:])
1279 1281 else:
1280 1282 # a file
1281 1283 if destdirexists:
1282 1284 res = lambda p: os.path.join(dest,
1283 1285 os.path.basename(util.localpath(p)))
1284 1286 else:
1285 1287 res = lambda p: dest
1286 1288 return res
1287 1289
1288 1290 pats = scmutil.expandpats(pats)
1289 1291 if not pats:
1290 1292 raise error.Abort(_('no source or destination specified'))
1291 1293 if len(pats) == 1:
1292 1294 raise error.Abort(_('no destination specified'))
1293 1295 dest = pats.pop()
1294 1296 destdirexists = os.path.isdir(dest) and not os.path.islink(dest)
1295 1297 if not destdirexists:
1296 1298 if len(pats) > 1 or matchmod.patkind(pats[0]):
1297 1299 raise error.Abort(_('with multiple sources, destination must be an '
1298 1300 'existing directory'))
1299 1301 if util.endswithsep(dest):
1300 1302 raise error.Abort(_('destination %s is not a directory') % dest)
1301 1303
1302 1304 tfn = targetpathfn
1303 1305 if after:
1304 1306 tfn = targetpathafterfn
1305 1307 copylist = []
1306 1308 for pat in pats:
1307 1309 srcs = walkpat(pat)
1308 1310 if not srcs:
1309 1311 continue
1310 1312 copylist.append((tfn(pat, dest, srcs), srcs))
1311 1313 if not copylist:
1312 1314 raise error.Abort(_('no files to copy'))
1313 1315
1314 1316 errors = 0
1315 1317 for targetpath, srcs in copylist:
1316 1318 for abssrc, relsrc, exact in srcs:
1317 1319 if copyfile(abssrc, relsrc, targetpath(abssrc), exact):
1318 1320 errors += 1
1319 1321
1320 1322 if errors:
1321 1323 ui.warn(_('(consider using --after)\n'))
1322 1324
1323 1325 return errors != 0
1324 1326
1325 1327 ## facility to let extension process additional data into an import patch
1326 1328 # list of identifier to be executed in order
1327 1329 extrapreimport = [] # run before commit
1328 1330 extrapostimport = [] # run after commit
1329 1331 # mapping from identifier to actual import function
1330 1332 #
1331 1333 # 'preimport' are run before the commit is made and are provided the following
1332 1334 # arguments:
1333 1335 # - repo: the localrepository instance,
1334 1336 # - patchdata: data extracted from patch header (cf m.patch.patchheadermap),
1335 1337 # - extra: the future extra dictionary of the changeset, please mutate it,
1336 1338 # - opts: the import options.
1337 1339 # XXX ideally, we would just pass an ctx ready to be computed, that would allow
1338 1340 # mutation of in memory commit and more. Feel free to rework the code to get
1339 1341 # there.
1340 1342 extrapreimportmap = {}
1341 1343 # 'postimport' are run after the commit is made and are provided the following
1342 1344 # argument:
1343 1345 # - ctx: the changectx created by import.
1344 1346 extrapostimportmap = {}
1345 1347
1346 1348 def tryimportone(ui, repo, hunk, parents, opts, msgs, updatefunc):
1347 1349 """Utility function used by commands.import to import a single patch
1348 1350
1349 1351 This function is explicitly defined here to help the evolve extension to
1350 1352 wrap this part of the import logic.
1351 1353
1352 1354 The API is currently a bit ugly because it a simple code translation from
1353 1355 the import command. Feel free to make it better.
1354 1356
1355 1357 :hunk: a patch (as a binary string)
1356 1358 :parents: nodes that will be parent of the created commit
1357 1359 :opts: the full dict of option passed to the import command
1358 1360 :msgs: list to save commit message to.
1359 1361 (used in case we need to save it when failing)
1360 1362 :updatefunc: a function that update a repo to a given node
1361 1363 updatefunc(<repo>, <node>)
1362 1364 """
1363 1365 # avoid cycle context -> subrepo -> cmdutil
1364 1366 from . import context
1365 1367 extractdata = patch.extract(ui, hunk)
1366 1368 tmpname = extractdata.get('filename')
1367 1369 message = extractdata.get('message')
1368 1370 user = opts.get('user') or extractdata.get('user')
1369 1371 date = opts.get('date') or extractdata.get('date')
1370 1372 branch = extractdata.get('branch')
1371 1373 nodeid = extractdata.get('nodeid')
1372 1374 p1 = extractdata.get('p1')
1373 1375 p2 = extractdata.get('p2')
1374 1376
1375 1377 nocommit = opts.get('no_commit')
1376 1378 importbranch = opts.get('import_branch')
1377 1379 update = not opts.get('bypass')
1378 1380 strip = opts["strip"]
1379 1381 prefix = opts["prefix"]
1380 1382 sim = float(opts.get('similarity') or 0)
1381 1383 if not tmpname:
1382 1384 return (None, None, False)
1383 1385
1384 1386 rejects = False
1385 1387
1386 1388 try:
1387 1389 cmdline_message = logmessage(ui, opts)
1388 1390 if cmdline_message:
1389 1391 # pickup the cmdline msg
1390 1392 message = cmdline_message
1391 1393 elif message:
1392 1394 # pickup the patch msg
1393 1395 message = message.strip()
1394 1396 else:
1395 1397 # launch the editor
1396 1398 message = None
1397 1399 ui.debug('message:\n%s\n' % message)
1398 1400
1399 1401 if len(parents) == 1:
1400 1402 parents.append(repo[nullid])
1401 1403 if opts.get('exact'):
1402 1404 if not nodeid or not p1:
1403 1405 raise error.Abort(_('not a Mercurial patch'))
1404 1406 p1 = repo[p1]
1405 1407 p2 = repo[p2 or nullid]
1406 1408 elif p2:
1407 1409 try:
1408 1410 p1 = repo[p1]
1409 1411 p2 = repo[p2]
1410 1412 # Without any options, consider p2 only if the
1411 1413 # patch is being applied on top of the recorded
1412 1414 # first parent.
1413 1415 if p1 != parents[0]:
1414 1416 p1 = parents[0]
1415 1417 p2 = repo[nullid]
1416 1418 except error.RepoError:
1417 1419 p1, p2 = parents
1418 1420 if p2.node() == nullid:
1419 1421 ui.warn(_("warning: import the patch as a normal revision\n"
1420 1422 "(use --exact to import the patch as a merge)\n"))
1421 1423 else:
1422 1424 p1, p2 = parents
1423 1425
1424 1426 n = None
1425 1427 if update:
1426 1428 if p1 != parents[0]:
1427 1429 updatefunc(repo, p1.node())
1428 1430 if p2 != parents[1]:
1429 1431 repo.setparents(p1.node(), p2.node())
1430 1432
1431 1433 if opts.get('exact') or importbranch:
1432 1434 repo.dirstate.setbranch(branch or 'default')
1433 1435
1434 1436 partial = opts.get('partial', False)
1435 1437 files = set()
1436 1438 try:
1437 1439 patch.patch(ui, repo, tmpname, strip=strip, prefix=prefix,
1438 1440 files=files, eolmode=None, similarity=sim / 100.0)
1439 1441 except error.PatchError as e:
1440 1442 if not partial:
1441 1443 raise error.Abort(pycompat.bytestr(e))
1442 1444 if partial:
1443 1445 rejects = True
1444 1446
1445 1447 files = list(files)
1446 1448 if nocommit:
1447 1449 if message:
1448 1450 msgs.append(message)
1449 1451 else:
1450 1452 if opts.get('exact') or p2:
1451 1453 # If you got here, you either use --force and know what
1452 1454 # you are doing or used --exact or a merge patch while
1453 1455 # being updated to its first parent.
1454 1456 m = None
1455 1457 else:
1456 1458 m = scmutil.matchfiles(repo, files or [])
1457 1459 editform = mergeeditform(repo[None], 'import.normal')
1458 1460 if opts.get('exact'):
1459 1461 editor = None
1460 1462 else:
1461 1463 editor = getcommiteditor(editform=editform,
1462 1464 **pycompat.strkwargs(opts))
1463 1465 extra = {}
1464 1466 for idfunc in extrapreimport:
1465 1467 extrapreimportmap[idfunc](repo, extractdata, extra, opts)
1466 1468 overrides = {}
1467 1469 if partial:
1468 1470 overrides[('ui', 'allowemptycommit')] = True
1469 1471 with repo.ui.configoverride(overrides, 'import'):
1470 1472 n = repo.commit(message, user,
1471 1473 date, match=m,
1472 1474 editor=editor, extra=extra)
1473 1475 for idfunc in extrapostimport:
1474 1476 extrapostimportmap[idfunc](repo[n])
1475 1477 else:
1476 1478 if opts.get('exact') or importbranch:
1477 1479 branch = branch or 'default'
1478 1480 else:
1479 1481 branch = p1.branch()
1480 1482 store = patch.filestore()
1481 1483 try:
1482 1484 files = set()
1483 1485 try:
1484 1486 patch.patchrepo(ui, repo, p1, store, tmpname, strip, prefix,
1485 1487 files, eolmode=None)
1486 1488 except error.PatchError as e:
1487 1489 raise error.Abort(stringutil.forcebytestr(e))
1488 1490 if opts.get('exact'):
1489 1491 editor = None
1490 1492 else:
1491 1493 editor = getcommiteditor(editform='import.bypass')
1492 1494 memctx = context.memctx(repo, (p1.node(), p2.node()),
1493 1495 message,
1494 1496 files=files,
1495 1497 filectxfn=store,
1496 1498 user=user,
1497 1499 date=date,
1498 1500 branch=branch,
1499 1501 editor=editor)
1500 1502 n = memctx.commit()
1501 1503 finally:
1502 1504 store.close()
1503 1505 if opts.get('exact') and nocommit:
1504 1506 # --exact with --no-commit is still useful in that it does merge
1505 1507 # and branch bits
1506 1508 ui.warn(_("warning: can't check exact import with --no-commit\n"))
1507 1509 elif opts.get('exact') and hex(n) != nodeid:
1508 1510 raise error.Abort(_('patch is damaged or loses information'))
1509 1511 msg = _('applied to working directory')
1510 1512 if n:
1511 1513 # i18n: refers to a short changeset id
1512 1514 msg = _('created %s') % short(n)
1513 1515 return (msg, n, rejects)
1514 1516 finally:
1515 1517 os.unlink(tmpname)
1516 1518
1517 1519 # facility to let extensions include additional data in an exported patch
1518 1520 # list of identifiers to be executed in order
1519 1521 extraexport = []
1520 1522 # mapping from identifier to actual export function
1521 1523 # function as to return a string to be added to the header or None
1522 1524 # it is given two arguments (sequencenumber, changectx)
1523 1525 extraexportmap = {}
1524 1526
1525 1527 def _exportsingle(repo, ctx, match, switch_parent, rev, seqno, write, diffopts):
1526 1528 node = scmutil.binnode(ctx)
1527 1529 parents = [p.node() for p in ctx.parents() if p]
1528 1530 branch = ctx.branch()
1529 1531 if switch_parent:
1530 1532 parents.reverse()
1531 1533
1532 1534 if parents:
1533 1535 prev = parents[0]
1534 1536 else:
1535 1537 prev = nullid
1536 1538
1537 1539 write("# HG changeset patch\n")
1538 1540 write("# User %s\n" % ctx.user())
1539 1541 write("# Date %d %d\n" % ctx.date())
1540 1542 write("# %s\n" % dateutil.datestr(ctx.date()))
1541 1543 if branch and branch != 'default':
1542 1544 write("# Branch %s\n" % branch)
1543 1545 write("# Node ID %s\n" % hex(node))
1544 1546 write("# Parent %s\n" % hex(prev))
1545 1547 if len(parents) > 1:
1546 1548 write("# Parent %s\n" % hex(parents[1]))
1547 1549
1548 1550 for headerid in extraexport:
1549 1551 header = extraexportmap[headerid](seqno, ctx)
1550 1552 if header is not None:
1551 1553 write('# %s\n' % header)
1552 1554 write(ctx.description().rstrip())
1553 1555 write("\n\n")
1554 1556
1555 1557 for chunk, label in patch.diffui(repo, prev, node, match, opts=diffopts):
1556 1558 write(chunk, label=label)
1557 1559
1558 1560 def export(repo, revs, fntemplate='hg-%h.patch', fp=None, switch_parent=False,
1559 1561 opts=None, match=None):
1560 1562 '''export changesets as hg patches
1561 1563
1562 1564 Args:
1563 1565 repo: The repository from which we're exporting revisions.
1564 1566 revs: A list of revisions to export as revision numbers.
1565 1567 fntemplate: An optional string to use for generating patch file names.
1566 1568 fp: An optional file-like object to which patches should be written.
1567 1569 switch_parent: If True, show diffs against second parent when not nullid.
1568 1570 Default is false, which always shows diff against p1.
1569 1571 opts: diff options to use for generating the patch.
1570 1572 match: If specified, only export changes to files matching this matcher.
1571 1573
1572 1574 Returns:
1573 1575 Nothing.
1574 1576
1575 1577 Side Effect:
1576 1578 "HG Changeset Patch" data is emitted to one of the following
1577 1579 destinations:
1578 1580 fp is specified: All revs are written to the specified
1579 1581 file-like object.
1580 1582 fntemplate specified: Each rev is written to a unique file named using
1581 1583 the given template.
1582 1584 Neither fp nor template specified: All revs written to repo.ui.write()
1583 1585 '''
1584 1586
1585 1587 total = len(revs)
1586 1588 revwidth = max(len(str(rev)) for rev in revs)
1587 1589 filemode = {}
1588 1590
1589 1591 write = None
1590 1592 dest = '<unnamed>'
1591 1593 if fp:
1592 1594 dest = getattr(fp, 'name', dest)
1593 1595 def write(s, **kw):
1594 1596 fp.write(s)
1595 1597 elif not fntemplate:
1596 1598 write = repo.ui.write
1597 1599
1598 1600 for seqno, rev in enumerate(revs, 1):
1599 1601 ctx = repo[rev]
1600 1602 fo = None
1601 1603 if not fp and fntemplate:
1602 1604 fo = makefileobj(ctx, fntemplate, mode='wb', modemap=filemode,
1603 1605 total=total, seqno=seqno, revwidth=revwidth)
1604 1606 dest = fo.name
1605 1607 def write(s, **kw):
1606 1608 fo.write(s)
1607 1609 if not dest.startswith('<'):
1608 1610 repo.ui.note("%s\n" % dest)
1609 1611 _exportsingle(
1610 1612 repo, ctx, match, switch_parent, rev, seqno, write, opts)
1611 1613 if fo is not None:
1612 1614 fo.close()
1613 1615
1614 1616 def showmarker(fm, marker, index=None):
1615 1617 """utility function to display obsolescence marker in a readable way
1616 1618
1617 1619 To be used by debug function."""
1618 1620 if index is not None:
1619 1621 fm.write('index', '%i ', index)
1620 1622 fm.write('prednode', '%s ', hex(marker.prednode()))
1621 1623 succs = marker.succnodes()
1622 1624 fm.condwrite(succs, 'succnodes', '%s ',
1623 1625 fm.formatlist(map(hex, succs), name='node'))
1624 1626 fm.write('flag', '%X ', marker.flags())
1625 1627 parents = marker.parentnodes()
1626 1628 if parents is not None:
1627 1629 fm.write('parentnodes', '{%s} ',
1628 1630 fm.formatlist(map(hex, parents), name='node', sep=', '))
1629 1631 fm.write('date', '(%s) ', fm.formatdate(marker.date()))
1630 1632 meta = marker.metadata().copy()
1631 1633 meta.pop('date', None)
1632 1634 smeta = util.rapply(pycompat.maybebytestr, meta)
1633 1635 fm.write('metadata', '{%s}', fm.formatdict(smeta, fmt='%r: %r', sep=', '))
1634 1636 fm.plain('\n')
1635 1637
1636 1638 def finddate(ui, repo, date):
1637 1639 """Find the tipmost changeset that matches the given date spec"""
1638 1640
1639 1641 df = dateutil.matchdate(date)
1640 1642 m = scmutil.matchall(repo)
1641 1643 results = {}
1642 1644
1643 1645 def prep(ctx, fns):
1644 1646 d = ctx.date()
1645 1647 if df(d[0]):
1646 1648 results[ctx.rev()] = d
1647 1649
1648 1650 for ctx in walkchangerevs(repo, m, {'rev': None}, prep):
1649 1651 rev = ctx.rev()
1650 1652 if rev in results:
1651 1653 ui.status(_("found revision %s from %s\n") %
1652 1654 (rev, dateutil.datestr(results[rev])))
1653 1655 return '%d' % rev
1654 1656
1655 1657 raise error.Abort(_("revision matching date not found"))
1656 1658
1657 1659 def increasingwindows(windowsize=8, sizelimit=512):
1658 1660 while True:
1659 1661 yield windowsize
1660 1662 if windowsize < sizelimit:
1661 1663 windowsize *= 2
1662 1664
1663 1665 def _walkrevs(repo, opts):
1664 1666 # Default --rev value depends on --follow but --follow behavior
1665 1667 # depends on revisions resolved from --rev...
1666 1668 follow = opts.get('follow') or opts.get('follow_first')
1667 1669 if opts.get('rev'):
1668 1670 revs = scmutil.revrange(repo, opts['rev'])
1669 1671 elif follow and repo.dirstate.p1() == nullid:
1670 1672 revs = smartset.baseset()
1671 1673 elif follow:
1672 1674 revs = repo.revs('reverse(:.)')
1673 1675 else:
1674 1676 revs = smartset.spanset(repo)
1675 1677 revs.reverse()
1676 1678 return revs
1677 1679
1678 1680 class FileWalkError(Exception):
1679 1681 pass
1680 1682
1681 1683 def walkfilerevs(repo, match, follow, revs, fncache):
1682 1684 '''Walks the file history for the matched files.
1683 1685
1684 1686 Returns the changeset revs that are involved in the file history.
1685 1687
1686 1688 Throws FileWalkError if the file history can't be walked using
1687 1689 filelogs alone.
1688 1690 '''
1689 1691 wanted = set()
1690 1692 copies = []
1691 1693 minrev, maxrev = min(revs), max(revs)
1692 1694 def filerevgen(filelog, last):
1693 1695 """
1694 1696 Only files, no patterns. Check the history of each file.
1695 1697
1696 1698 Examines filelog entries within minrev, maxrev linkrev range
1697 1699 Returns an iterator yielding (linkrev, parentlinkrevs, copied)
1698 1700 tuples in backwards order
1699 1701 """
1700 1702 cl_count = len(repo)
1701 1703 revs = []
1702 1704 for j in xrange(0, last + 1):
1703 1705 linkrev = filelog.linkrev(j)
1704 1706 if linkrev < minrev:
1705 1707 continue
1706 1708 # only yield rev for which we have the changelog, it can
1707 1709 # happen while doing "hg log" during a pull or commit
1708 1710 if linkrev >= cl_count:
1709 1711 break
1710 1712
1711 1713 parentlinkrevs = []
1712 1714 for p in filelog.parentrevs(j):
1713 1715 if p != nullrev:
1714 1716 parentlinkrevs.append(filelog.linkrev(p))
1715 1717 n = filelog.node(j)
1716 1718 revs.append((linkrev, parentlinkrevs,
1717 1719 follow and filelog.renamed(n)))
1718 1720
1719 1721 return reversed(revs)
1720 1722 def iterfiles():
1721 1723 pctx = repo['.']
1722 1724 for filename in match.files():
1723 1725 if follow:
1724 1726 if filename not in pctx:
1725 1727 raise error.Abort(_('cannot follow file not in parent '
1726 1728 'revision: "%s"') % filename)
1727 1729 yield filename, pctx[filename].filenode()
1728 1730 else:
1729 1731 yield filename, None
1730 1732 for filename_node in copies:
1731 1733 yield filename_node
1732 1734
1733 1735 for file_, node in iterfiles():
1734 1736 filelog = repo.file(file_)
1735 1737 if not len(filelog):
1736 1738 if node is None:
1737 1739 # A zero count may be a directory or deleted file, so
1738 1740 # try to find matching entries on the slow path.
1739 1741 if follow:
1740 1742 raise error.Abort(
1741 1743 _('cannot follow nonexistent file: "%s"') % file_)
1742 1744 raise FileWalkError("Cannot walk via filelog")
1743 1745 else:
1744 1746 continue
1745 1747
1746 1748 if node is None:
1747 1749 last = len(filelog) - 1
1748 1750 else:
1749 1751 last = filelog.rev(node)
1750 1752
1751 1753 # keep track of all ancestors of the file
1752 1754 ancestors = {filelog.linkrev(last)}
1753 1755
1754 1756 # iterate from latest to oldest revision
1755 1757 for rev, flparentlinkrevs, copied in filerevgen(filelog, last):
1756 1758 if not follow:
1757 1759 if rev > maxrev:
1758 1760 continue
1759 1761 else:
1760 1762 # Note that last might not be the first interesting
1761 1763 # rev to us:
1762 1764 # if the file has been changed after maxrev, we'll
1763 1765 # have linkrev(last) > maxrev, and we still need
1764 1766 # to explore the file graph
1765 1767 if rev not in ancestors:
1766 1768 continue
1767 1769 # XXX insert 1327 fix here
1768 1770 if flparentlinkrevs:
1769 1771 ancestors.update(flparentlinkrevs)
1770 1772
1771 1773 fncache.setdefault(rev, []).append(file_)
1772 1774 wanted.add(rev)
1773 1775 if copied:
1774 1776 copies.append(copied)
1775 1777
1776 1778 return wanted
1777 1779
1778 1780 class _followfilter(object):
1779 1781 def __init__(self, repo, onlyfirst=False):
1780 1782 self.repo = repo
1781 1783 self.startrev = nullrev
1782 1784 self.roots = set()
1783 1785 self.onlyfirst = onlyfirst
1784 1786
1785 1787 def match(self, rev):
1786 1788 def realparents(rev):
1787 1789 if self.onlyfirst:
1788 1790 return self.repo.changelog.parentrevs(rev)[0:1]
1789 1791 else:
1790 1792 return filter(lambda x: x != nullrev,
1791 1793 self.repo.changelog.parentrevs(rev))
1792 1794
1793 1795 if self.startrev == nullrev:
1794 1796 self.startrev = rev
1795 1797 return True
1796 1798
1797 1799 if rev > self.startrev:
1798 1800 # forward: all descendants
1799 1801 if not self.roots:
1800 1802 self.roots.add(self.startrev)
1801 1803 for parent in realparents(rev):
1802 1804 if parent in self.roots:
1803 1805 self.roots.add(rev)
1804 1806 return True
1805 1807 else:
1806 1808 # backwards: all parents
1807 1809 if not self.roots:
1808 1810 self.roots.update(realparents(self.startrev))
1809 1811 if rev in self.roots:
1810 1812 self.roots.remove(rev)
1811 1813 self.roots.update(realparents(rev))
1812 1814 return True
1813 1815
1814 1816 return False
1815 1817
1816 1818 def walkchangerevs(repo, match, opts, prepare):
1817 1819 '''Iterate over files and the revs in which they changed.
1818 1820
1819 1821 Callers most commonly need to iterate backwards over the history
1820 1822 in which they are interested. Doing so has awful (quadratic-looking)
1821 1823 performance, so we use iterators in a "windowed" way.
1822 1824
1823 1825 We walk a window of revisions in the desired order. Within the
1824 1826 window, we first walk forwards to gather data, then in the desired
1825 1827 order (usually backwards) to display it.
1826 1828
1827 1829 This function returns an iterator yielding contexts. Before
1828 1830 yielding each context, the iterator will first call the prepare
1829 1831 function on each context in the window in forward order.'''
1830 1832
1831 1833 follow = opts.get('follow') or opts.get('follow_first')
1832 1834 revs = _walkrevs(repo, opts)
1833 1835 if not revs:
1834 1836 return []
1835 1837 wanted = set()
1836 1838 slowpath = match.anypats() or (not match.always() and opts.get('removed'))
1837 1839 fncache = {}
1838 1840 change = repo.changectx
1839 1841
1840 1842 # First step is to fill wanted, the set of revisions that we want to yield.
1841 1843 # When it does not induce extra cost, we also fill fncache for revisions in
1842 1844 # wanted: a cache of filenames that were changed (ctx.files()) and that
1843 1845 # match the file filtering conditions.
1844 1846
1845 1847 if match.always():
1846 1848 # No files, no patterns. Display all revs.
1847 1849 wanted = revs
1848 1850 elif not slowpath:
1849 1851 # We only have to read through the filelog to find wanted revisions
1850 1852
1851 1853 try:
1852 1854 wanted = walkfilerevs(repo, match, follow, revs, fncache)
1853 1855 except FileWalkError:
1854 1856 slowpath = True
1855 1857
1856 1858 # We decided to fall back to the slowpath because at least one
1857 1859 # of the paths was not a file. Check to see if at least one of them
1858 1860 # existed in history, otherwise simply return
1859 1861 for path in match.files():
1860 1862 if path == '.' or path in repo.store:
1861 1863 break
1862 1864 else:
1863 1865 return []
1864 1866
1865 1867 if slowpath:
1866 1868 # We have to read the changelog to match filenames against
1867 1869 # changed files
1868 1870
1869 1871 if follow:
1870 1872 raise error.Abort(_('can only follow copies/renames for explicit '
1871 1873 'filenames'))
1872 1874
1873 1875 # The slow path checks files modified in every changeset.
1874 1876 # This is really slow on large repos, so compute the set lazily.
1875 1877 class lazywantedset(object):
1876 1878 def __init__(self):
1877 1879 self.set = set()
1878 1880 self.revs = set(revs)
1879 1881
1880 1882 # No need to worry about locality here because it will be accessed
1881 1883 # in the same order as the increasing window below.
1882 1884 def __contains__(self, value):
1883 1885 if value in self.set:
1884 1886 return True
1885 1887 elif not value in self.revs:
1886 1888 return False
1887 1889 else:
1888 1890 self.revs.discard(value)
1889 1891 ctx = change(value)
1890 1892 matches = [f for f in ctx.files() if match(f)]
1891 1893 if matches:
1892 1894 fncache[value] = matches
1893 1895 self.set.add(value)
1894 1896 return True
1895 1897 return False
1896 1898
1897 1899 def discard(self, value):
1898 1900 self.revs.discard(value)
1899 1901 self.set.discard(value)
1900 1902
1901 1903 wanted = lazywantedset()
1902 1904
1903 1905 # it might be worthwhile to do this in the iterator if the rev range
1904 1906 # is descending and the prune args are all within that range
1905 1907 for rev in opts.get('prune', ()):
1906 1908 rev = repo[rev].rev()
1907 1909 ff = _followfilter(repo)
1908 1910 stop = min(revs[0], revs[-1])
1909 1911 for x in xrange(rev, stop - 1, -1):
1910 1912 if ff.match(x):
1911 1913 wanted = wanted - [x]
1912 1914
1913 1915 # Now that wanted is correctly initialized, we can iterate over the
1914 1916 # revision range, yielding only revisions in wanted.
1915 1917 def iterate():
1916 1918 if follow and match.always():
1917 1919 ff = _followfilter(repo, onlyfirst=opts.get('follow_first'))
1918 1920 def want(rev):
1919 1921 return ff.match(rev) and rev in wanted
1920 1922 else:
1921 1923 def want(rev):
1922 1924 return rev in wanted
1923 1925
1924 1926 it = iter(revs)
1925 1927 stopiteration = False
1926 1928 for windowsize in increasingwindows():
1927 1929 nrevs = []
1928 1930 for i in xrange(windowsize):
1929 1931 rev = next(it, None)
1930 1932 if rev is None:
1931 1933 stopiteration = True
1932 1934 break
1933 1935 elif want(rev):
1934 1936 nrevs.append(rev)
1935 1937 for rev in sorted(nrevs):
1936 1938 fns = fncache.get(rev)
1937 1939 ctx = change(rev)
1938 1940 if not fns:
1939 1941 def fns_generator():
1940 1942 for f in ctx.files():
1941 1943 if match(f):
1942 1944 yield f
1943 1945 fns = fns_generator()
1944 1946 prepare(ctx, fns)
1945 1947 for rev in nrevs:
1946 1948 yield change(rev)
1947 1949
1948 1950 if stopiteration:
1949 1951 break
1950 1952
1951 1953 return iterate()
1952 1954
1953 1955 def add(ui, repo, match, prefix, explicitonly, **opts):
1954 1956 join = lambda f: os.path.join(prefix, f)
1955 1957 bad = []
1956 1958
1957 1959 badfn = lambda x, y: bad.append(x) or match.bad(x, y)
1958 1960 names = []
1959 1961 wctx = repo[None]
1960 1962 cca = None
1961 1963 abort, warn = scmutil.checkportabilityalert(ui)
1962 1964 if abort or warn:
1963 1965 cca = scmutil.casecollisionauditor(ui, abort, repo.dirstate)
1964 1966
1965 1967 badmatch = matchmod.badmatch(match, badfn)
1966 1968 dirstate = repo.dirstate
1967 1969 # We don't want to just call wctx.walk here, since it would return a lot of
1968 1970 # clean files, which we aren't interested in and takes time.
1969 1971 for f in sorted(dirstate.walk(badmatch, subrepos=sorted(wctx.substate),
1970 1972 unknown=True, ignored=False, full=False)):
1971 1973 exact = match.exact(f)
1972 1974 if exact or not explicitonly and f not in wctx and repo.wvfs.lexists(f):
1973 1975 if cca:
1974 1976 cca(f)
1975 1977 names.append(f)
1976 1978 if ui.verbose or not exact:
1977 1979 ui.status(_('adding %s\n') % match.rel(f))
1978 1980
1979 1981 for subpath in sorted(wctx.substate):
1980 1982 sub = wctx.sub(subpath)
1981 1983 try:
1982 1984 submatch = matchmod.subdirmatcher(subpath, match)
1983 1985 if opts.get(r'subrepos'):
1984 1986 bad.extend(sub.add(ui, submatch, prefix, False, **opts))
1985 1987 else:
1986 1988 bad.extend(sub.add(ui, submatch, prefix, True, **opts))
1987 1989 except error.LookupError:
1988 1990 ui.status(_("skipping missing subrepository: %s\n")
1989 1991 % join(subpath))
1990 1992
1991 1993 if not opts.get(r'dry_run'):
1992 1994 rejected = wctx.add(names, prefix)
1993 1995 bad.extend(f for f in rejected if f in match.files())
1994 1996 return bad
1995 1997
1996 1998 def addwebdirpath(repo, serverpath, webconf):
1997 1999 webconf[serverpath] = repo.root
1998 2000 repo.ui.debug('adding %s = %s\n' % (serverpath, repo.root))
1999 2001
2000 2002 for r in repo.revs('filelog("path:.hgsub")'):
2001 2003 ctx = repo[r]
2002 2004 for subpath in ctx.substate:
2003 2005 ctx.sub(subpath).addwebdirpath(serverpath, webconf)
2004 2006
2005 2007 def forget(ui, repo, match, prefix, explicitonly, dryrun):
2006 2008 join = lambda f: os.path.join(prefix, f)
2007 2009 bad = []
2008 2010 badfn = lambda x, y: bad.append(x) or match.bad(x, y)
2009 2011 wctx = repo[None]
2010 2012 forgot = []
2011 2013
2012 2014 s = repo.status(match=matchmod.badmatch(match, badfn), clean=True)
2013 2015 forget = sorted(s.modified + s.added + s.deleted + s.clean)
2014 2016 if explicitonly:
2015 2017 forget = [f for f in forget if match.exact(f)]
2016 2018
2017 2019 for subpath in sorted(wctx.substate):
2018 2020 sub = wctx.sub(subpath)
2019 2021 try:
2020 2022 submatch = matchmod.subdirmatcher(subpath, match)
2021 2023 subbad, subforgot = sub.forget(submatch, prefix, dryrun=dryrun)
2022 2024 bad.extend([subpath + '/' + f for f in subbad])
2023 2025 forgot.extend([subpath + '/' + f for f in subforgot])
2024 2026 except error.LookupError:
2025 2027 ui.status(_("skipping missing subrepository: %s\n")
2026 2028 % join(subpath))
2027 2029
2028 2030 if not explicitonly:
2029 2031 for f in match.files():
2030 2032 if f not in repo.dirstate and not repo.wvfs.isdir(f):
2031 2033 if f not in forgot:
2032 2034 if repo.wvfs.exists(f):
2033 2035 # Don't complain if the exact case match wasn't given.
2034 2036 # But don't do this until after checking 'forgot', so
2035 2037 # that subrepo files aren't normalized, and this op is
2036 2038 # purely from data cached by the status walk above.
2037 2039 if repo.dirstate.normalize(f) in repo.dirstate:
2038 2040 continue
2039 2041 ui.warn(_('not removing %s: '
2040 2042 'file is already untracked\n')
2041 2043 % match.rel(f))
2042 2044 bad.append(f)
2043 2045
2044 2046 for f in forget:
2045 2047 if ui.verbose or not match.exact(f):
2046 2048 ui.status(_('removing %s\n') % match.rel(f))
2047 2049
2048 2050 if not dryrun:
2049 2051 rejected = wctx.forget(forget, prefix)
2050 2052 bad.extend(f for f in rejected if f in match.files())
2051 2053 forgot.extend(f for f in forget if f not in rejected)
2052 2054 return bad, forgot
2053 2055
2054 2056 def files(ui, ctx, m, fm, fmt, subrepos):
2055 2057 rev = ctx.rev()
2056 2058 ret = 1
2057 2059 ds = ctx.repo().dirstate
2058 2060
2059 2061 for f in ctx.matches(m):
2060 2062 if rev is None and ds[f] == 'r':
2061 2063 continue
2062 2064 fm.startitem()
2063 2065 if ui.verbose:
2064 2066 fc = ctx[f]
2065 2067 fm.write('size flags', '% 10d % 1s ', fc.size(), fc.flags())
2066 2068 fm.data(abspath=f)
2067 2069 fm.write('path', fmt, m.rel(f))
2068 2070 ret = 0
2069 2071
2070 2072 for subpath in sorted(ctx.substate):
2071 2073 submatch = matchmod.subdirmatcher(subpath, m)
2072 2074 if (subrepos or m.exact(subpath) or any(submatch.files())):
2073 2075 sub = ctx.sub(subpath)
2074 2076 try:
2075 2077 recurse = m.exact(subpath) or subrepos
2076 2078 if sub.printfiles(ui, submatch, fm, fmt, recurse) == 0:
2077 2079 ret = 0
2078 2080 except error.LookupError:
2079 2081 ui.status(_("skipping missing subrepository: %s\n")
2080 2082 % m.abs(subpath))
2081 2083
2082 2084 return ret
2083 2085
2084 2086 def remove(ui, repo, m, prefix, after, force, subrepos, warnings=None):
2085 2087 join = lambda f: os.path.join(prefix, f)
2086 2088 ret = 0
2087 2089 s = repo.status(match=m, clean=True)
2088 2090 modified, added, deleted, clean = s[0], s[1], s[3], s[6]
2089 2091
2090 2092 wctx = repo[None]
2091 2093
2092 2094 if warnings is None:
2093 2095 warnings = []
2094 2096 warn = True
2095 2097 else:
2096 2098 warn = False
2097 2099
2098 2100 subs = sorted(wctx.substate)
2099 2101 total = len(subs)
2100 2102 count = 0
2101 2103 for subpath in subs:
2102 2104 count += 1
2103 2105 submatch = matchmod.subdirmatcher(subpath, m)
2104 2106 if subrepos or m.exact(subpath) or any(submatch.files()):
2105 2107 ui.progress(_('searching'), count, total=total, unit=_('subrepos'))
2106 2108 sub = wctx.sub(subpath)
2107 2109 try:
2108 2110 if sub.removefiles(submatch, prefix, after, force, subrepos,
2109 2111 warnings):
2110 2112 ret = 1
2111 2113 except error.LookupError:
2112 2114 warnings.append(_("skipping missing subrepository: %s\n")
2113 2115 % join(subpath))
2114 2116 ui.progress(_('searching'), None)
2115 2117
2116 2118 # warn about failure to delete explicit files/dirs
2117 2119 deleteddirs = util.dirs(deleted)
2118 2120 files = m.files()
2119 2121 total = len(files)
2120 2122 count = 0
2121 2123 for f in files:
2122 2124 def insubrepo():
2123 2125 for subpath in wctx.substate:
2124 2126 if f.startswith(subpath + '/'):
2125 2127 return True
2126 2128 return False
2127 2129
2128 2130 count += 1
2129 2131 ui.progress(_('deleting'), count, total=total, unit=_('files'))
2130 2132 isdir = f in deleteddirs or wctx.hasdir(f)
2131 2133 if (f in repo.dirstate or isdir or f == '.'
2132 2134 or insubrepo() or f in subs):
2133 2135 continue
2134 2136
2135 2137 if repo.wvfs.exists(f):
2136 2138 if repo.wvfs.isdir(f):
2137 2139 warnings.append(_('not removing %s: no tracked files\n')
2138 2140 % m.rel(f))
2139 2141 else:
2140 2142 warnings.append(_('not removing %s: file is untracked\n')
2141 2143 % m.rel(f))
2142 2144 # missing files will generate a warning elsewhere
2143 2145 ret = 1
2144 2146 ui.progress(_('deleting'), None)
2145 2147
2146 2148 if force:
2147 2149 list = modified + deleted + clean + added
2148 2150 elif after:
2149 2151 list = deleted
2150 2152 remaining = modified + added + clean
2151 2153 total = len(remaining)
2152 2154 count = 0
2153 2155 for f in remaining:
2154 2156 count += 1
2155 2157 ui.progress(_('skipping'), count, total=total, unit=_('files'))
2156 2158 if ui.verbose or (f in files):
2157 2159 warnings.append(_('not removing %s: file still exists\n')
2158 2160 % m.rel(f))
2159 2161 ret = 1
2160 2162 ui.progress(_('skipping'), None)
2161 2163 else:
2162 2164 list = deleted + clean
2163 2165 total = len(modified) + len(added)
2164 2166 count = 0
2165 2167 for f in modified:
2166 2168 count += 1
2167 2169 ui.progress(_('skipping'), count, total=total, unit=_('files'))
2168 2170 warnings.append(_('not removing %s: file is modified (use -f'
2169 2171 ' to force removal)\n') % m.rel(f))
2170 2172 ret = 1
2171 2173 for f in added:
2172 2174 count += 1
2173 2175 ui.progress(_('skipping'), count, total=total, unit=_('files'))
2174 2176 warnings.append(_("not removing %s: file has been marked for add"
2175 2177 " (use 'hg forget' to undo add)\n") % m.rel(f))
2176 2178 ret = 1
2177 2179 ui.progress(_('skipping'), None)
2178 2180
2179 2181 list = sorted(list)
2180 2182 total = len(list)
2181 2183 count = 0
2182 2184 for f in list:
2183 2185 count += 1
2184 2186 if ui.verbose or not m.exact(f):
2185 2187 ui.progress(_('deleting'), count, total=total, unit=_('files'))
2186 2188 ui.status(_('removing %s\n') % m.rel(f))
2187 2189 ui.progress(_('deleting'), None)
2188 2190
2189 2191 with repo.wlock():
2190 2192 if not after:
2191 2193 for f in list:
2192 2194 if f in added:
2193 2195 continue # we never unlink added files on remove
2194 2196 repo.wvfs.unlinkpath(f, ignoremissing=True)
2195 2197 repo[None].forget(list)
2196 2198
2197 2199 if warn:
2198 2200 for warning in warnings:
2199 2201 ui.warn(warning)
2200 2202
2201 2203 return ret
2202 2204
2203 2205 def _updatecatformatter(fm, ctx, matcher, path, decode):
2204 2206 """Hook for adding data to the formatter used by ``hg cat``.
2205 2207
2206 2208 Extensions (e.g., lfs) can wrap this to inject keywords/data, but must call
2207 2209 this method first."""
2208 2210 data = ctx[path].data()
2209 2211 if decode:
2210 2212 data = ctx.repo().wwritedata(path, data)
2211 2213 fm.startitem()
2212 2214 fm.write('data', '%s', data)
2213 2215 fm.data(abspath=path, path=matcher.rel(path))
2214 2216
2215 2217 def cat(ui, repo, ctx, matcher, basefm, fntemplate, prefix, **opts):
2216 2218 err = 1
2217 2219 opts = pycompat.byteskwargs(opts)
2218 2220
2219 2221 def write(path):
2220 2222 filename = None
2221 2223 if fntemplate:
2222 2224 filename = makefilename(ctx, fntemplate,
2223 2225 pathname=os.path.join(prefix, path))
2224 2226 # attempt to create the directory if it does not already exist
2225 2227 try:
2226 2228 os.makedirs(os.path.dirname(filename))
2227 2229 except OSError:
2228 2230 pass
2229 2231 with formatter.maybereopen(basefm, filename, opts) as fm:
2230 2232 _updatecatformatter(fm, ctx, matcher, path, opts.get('decode'))
2231 2233
2232 2234 # Automation often uses hg cat on single files, so special case it
2233 2235 # for performance to avoid the cost of parsing the manifest.
2234 2236 if len(matcher.files()) == 1 and not matcher.anypats():
2235 2237 file = matcher.files()[0]
2236 2238 mfl = repo.manifestlog
2237 2239 mfnode = ctx.manifestnode()
2238 2240 try:
2239 2241 if mfnode and mfl[mfnode].find(file)[0]:
2240 2242 scmutil.fileprefetchhooks(repo, ctx, [file])
2241 2243 write(file)
2242 2244 return 0
2243 2245 except KeyError:
2244 2246 pass
2245 2247
2246 2248 files = [f for f in ctx.walk(matcher)]
2247 2249 scmutil.fileprefetchhooks(repo, ctx, files)
2248 2250
2249 2251 for abs in files:
2250 2252 write(abs)
2251 2253 err = 0
2252 2254
2253 2255 for subpath in sorted(ctx.substate):
2254 2256 sub = ctx.sub(subpath)
2255 2257 try:
2256 2258 submatch = matchmod.subdirmatcher(subpath, matcher)
2257 2259
2258 2260 if not sub.cat(submatch, basefm, fntemplate,
2259 2261 os.path.join(prefix, sub._path),
2260 2262 **pycompat.strkwargs(opts)):
2261 2263 err = 0
2262 2264 except error.RepoLookupError:
2263 2265 ui.status(_("skipping missing subrepository: %s\n")
2264 2266 % os.path.join(prefix, subpath))
2265 2267
2266 2268 return err
2267 2269
2268 2270 def commit(ui, repo, commitfunc, pats, opts):
2269 2271 '''commit the specified files or all outstanding changes'''
2270 2272 date = opts.get('date')
2271 2273 if date:
2272 2274 opts['date'] = dateutil.parsedate(date)
2273 2275 message = logmessage(ui, opts)
2274 2276 matcher = scmutil.match(repo[None], pats, opts)
2275 2277
2276 2278 dsguard = None
2277 2279 # extract addremove carefully -- this function can be called from a command
2278 2280 # that doesn't support addremove
2279 2281 if opts.get('addremove'):
2280 2282 dsguard = dirstateguard.dirstateguard(repo, 'commit')
2281 2283 with dsguard or util.nullcontextmanager():
2282 2284 if dsguard:
2283 2285 if scmutil.addremove(repo, matcher, "", opts) != 0:
2284 2286 raise error.Abort(
2285 2287 _("failed to mark all new/missing files as added/removed"))
2286 2288
2287 2289 return commitfunc(ui, repo, message, matcher, opts)
2288 2290
2289 2291 def samefile(f, ctx1, ctx2):
2290 2292 if f in ctx1.manifest():
2291 2293 a = ctx1.filectx(f)
2292 2294 if f in ctx2.manifest():
2293 2295 b = ctx2.filectx(f)
2294 2296 return (not a.cmp(b)
2295 2297 and a.flags() == b.flags())
2296 2298 else:
2297 2299 return False
2298 2300 else:
2299 2301 return f not in ctx2.manifest()
2300 2302
2301 2303 def amend(ui, repo, old, extra, pats, opts):
2302 2304 # avoid cycle context -> subrepo -> cmdutil
2303 2305 from . import context
2304 2306
2305 2307 # amend will reuse the existing user if not specified, but the obsolete
2306 2308 # marker creation requires that the current user's name is specified.
2307 2309 if obsolete.isenabled(repo, obsolete.createmarkersopt):
2308 2310 ui.username() # raise exception if username not set
2309 2311
2310 2312 ui.note(_('amending changeset %s\n') % old)
2311 2313 base = old.p1()
2312 2314
2313 2315 with repo.wlock(), repo.lock(), repo.transaction('amend'):
2314 2316 # Participating changesets:
2315 2317 #
2316 2318 # wctx o - workingctx that contains changes from working copy
2317 2319 # | to go into amending commit
2318 2320 # |
2319 2321 # old o - changeset to amend
2320 2322 # |
2321 2323 # base o - first parent of the changeset to amend
2322 2324 wctx = repo[None]
2323 2325
2324 2326 # Copy to avoid mutating input
2325 2327 extra = extra.copy()
2326 2328 # Update extra dict from amended commit (e.g. to preserve graft
2327 2329 # source)
2328 2330 extra.update(old.extra())
2329 2331
2330 2332 # Also update it from the from the wctx
2331 2333 extra.update(wctx.extra())
2332 2334
2333 2335 user = opts.get('user') or old.user()
2334 2336 date = opts.get('date') or old.date()
2335 2337
2336 2338 # Parse the date to allow comparison between date and old.date()
2337 2339 date = dateutil.parsedate(date)
2338 2340
2339 2341 if len(old.parents()) > 1:
2340 2342 # ctx.files() isn't reliable for merges, so fall back to the
2341 2343 # slower repo.status() method
2342 2344 files = set([fn for st in repo.status(base, old)[:3]
2343 2345 for fn in st])
2344 2346 else:
2345 2347 files = set(old.files())
2346 2348
2347 2349 # add/remove the files to the working copy if the "addremove" option
2348 2350 # was specified.
2349 2351 matcher = scmutil.match(wctx, pats, opts)
2350 2352 if (opts.get('addremove')
2351 2353 and scmutil.addremove(repo, matcher, "", opts)):
2352 2354 raise error.Abort(
2353 2355 _("failed to mark all new/missing files as added/removed"))
2354 2356
2355 2357 # Check subrepos. This depends on in-place wctx._status update in
2356 2358 # subrepo.precommit(). To minimize the risk of this hack, we do
2357 2359 # nothing if .hgsub does not exist.
2358 2360 if '.hgsub' in wctx or '.hgsub' in old:
2359 2361 subs, commitsubs, newsubstate = subrepoutil.precommit(
2360 2362 ui, wctx, wctx._status, matcher)
2361 2363 # amend should abort if commitsubrepos is enabled
2362 2364 assert not commitsubs
2363 2365 if subs:
2364 2366 subrepoutil.writestate(repo, newsubstate)
2365 2367
2366 2368 ms = mergemod.mergestate.read(repo)
2367 2369 mergeutil.checkunresolved(ms)
2368 2370
2369 2371 filestoamend = set(f for f in wctx.files() if matcher(f))
2370 2372
2371 2373 changes = (len(filestoamend) > 0)
2372 2374 if changes:
2373 2375 # Recompute copies (avoid recording a -> b -> a)
2374 2376 copied = copies.pathcopies(base, wctx, matcher)
2375 2377 if old.p2:
2376 2378 copied.update(copies.pathcopies(old.p2(), wctx, matcher))
2377 2379
2378 2380 # Prune files which were reverted by the updates: if old
2379 2381 # introduced file X and the file was renamed in the working
2380 2382 # copy, then those two files are the same and
2381 2383 # we can discard X from our list of files. Likewise if X
2382 2384 # was removed, it's no longer relevant. If X is missing (aka
2383 2385 # deleted), old X must be preserved.
2384 2386 files.update(filestoamend)
2385 2387 files = [f for f in files if (not samefile(f, wctx, base)
2386 2388 or f in wctx.deleted())]
2387 2389
2388 2390 def filectxfn(repo, ctx_, path):
2389 2391 try:
2390 2392 # If the file being considered is not amongst the files
2391 2393 # to be amended, we should return the file context from the
2392 2394 # old changeset. This avoids issues when only some files in
2393 2395 # the working copy are being amended but there are also
2394 2396 # changes to other files from the old changeset.
2395 2397 if path not in filestoamend:
2396 2398 return old.filectx(path)
2397 2399
2398 2400 # Return None for removed files.
2399 2401 if path in wctx.removed():
2400 2402 return None
2401 2403
2402 2404 fctx = wctx[path]
2403 2405 flags = fctx.flags()
2404 2406 mctx = context.memfilectx(repo, ctx_,
2405 2407 fctx.path(), fctx.data(),
2406 2408 islink='l' in flags,
2407 2409 isexec='x' in flags,
2408 2410 copied=copied.get(path))
2409 2411 return mctx
2410 2412 except KeyError:
2411 2413 return None
2412 2414 else:
2413 2415 ui.note(_('copying changeset %s to %s\n') % (old, base))
2414 2416
2415 2417 # Use version of files as in the old cset
2416 2418 def filectxfn(repo, ctx_, path):
2417 2419 try:
2418 2420 return old.filectx(path)
2419 2421 except KeyError:
2420 2422 return None
2421 2423
2422 2424 # See if we got a message from -m or -l, if not, open the editor with
2423 2425 # the message of the changeset to amend.
2424 2426 message = logmessage(ui, opts)
2425 2427
2426 2428 editform = mergeeditform(old, 'commit.amend')
2427 2429 editor = getcommiteditor(editform=editform,
2428 2430 **pycompat.strkwargs(opts))
2429 2431
2430 2432 if not message:
2431 2433 editor = getcommiteditor(edit=True, editform=editform)
2432 2434 message = old.description()
2433 2435
2434 2436 pureextra = extra.copy()
2435 2437 extra['amend_source'] = old.hex()
2436 2438
2437 2439 new = context.memctx(repo,
2438 2440 parents=[base.node(), old.p2().node()],
2439 2441 text=message,
2440 2442 files=files,
2441 2443 filectxfn=filectxfn,
2442 2444 user=user,
2443 2445 date=date,
2444 2446 extra=extra,
2445 2447 editor=editor)
2446 2448
2447 2449 newdesc = changelog.stripdesc(new.description())
2448 2450 if ((not changes)
2449 2451 and newdesc == old.description()
2450 2452 and user == old.user()
2451 2453 and date == old.date()
2452 2454 and pureextra == old.extra()):
2453 2455 # nothing changed. continuing here would create a new node
2454 2456 # anyway because of the amend_source noise.
2455 2457 #
2456 2458 # This not what we expect from amend.
2457 2459 return old.node()
2458 2460
2459 2461 if opts.get('secret'):
2460 2462 commitphase = 'secret'
2461 2463 else:
2462 2464 commitphase = old.phase()
2463 2465 overrides = {('phases', 'new-commit'): commitphase}
2464 2466 with ui.configoverride(overrides, 'amend'):
2465 2467 newid = repo.commitctx(new)
2466 2468
2467 2469 # Reroute the working copy parent to the new changeset
2468 2470 repo.setparents(newid, nullid)
2469 2471 mapping = {old.node(): (newid,)}
2470 2472 obsmetadata = None
2471 2473 if opts.get('note'):
2472 2474 obsmetadata = {'note': opts['note']}
2473 2475 scmutil.cleanupnodes(repo, mapping, 'amend', metadata=obsmetadata)
2474 2476
2475 2477 # Fixing the dirstate because localrepo.commitctx does not update
2476 2478 # it. This is rather convenient because we did not need to update
2477 2479 # the dirstate for all the files in the new commit which commitctx
2478 2480 # could have done if it updated the dirstate. Now, we can
2479 2481 # selectively update the dirstate only for the amended files.
2480 2482 dirstate = repo.dirstate
2481 2483
2482 2484 # Update the state of the files which were added and
2483 2485 # and modified in the amend to "normal" in the dirstate.
2484 2486 normalfiles = set(wctx.modified() + wctx.added()) & filestoamend
2485 2487 for f in normalfiles:
2486 2488 dirstate.normal(f)
2487 2489
2488 2490 # Update the state of files which were removed in the amend
2489 2491 # to "removed" in the dirstate.
2490 2492 removedfiles = set(wctx.removed()) & filestoamend
2491 2493 for f in removedfiles:
2492 2494 dirstate.drop(f)
2493 2495
2494 2496 return newid
2495 2497
2496 2498 def commiteditor(repo, ctx, subs, editform=''):
2497 2499 if ctx.description():
2498 2500 return ctx.description()
2499 2501 return commitforceeditor(repo, ctx, subs, editform=editform,
2500 2502 unchangedmessagedetection=True)
2501 2503
2502 2504 def commitforceeditor(repo, ctx, subs, finishdesc=None, extramsg=None,
2503 2505 editform='', unchangedmessagedetection=False):
2504 2506 if not extramsg:
2505 2507 extramsg = _("Leave message empty to abort commit.")
2506 2508
2507 2509 forms = [e for e in editform.split('.') if e]
2508 2510 forms.insert(0, 'changeset')
2509 2511 templatetext = None
2510 2512 while forms:
2511 2513 ref = '.'.join(forms)
2512 2514 if repo.ui.config('committemplate', ref):
2513 2515 templatetext = committext = buildcommittemplate(
2514 2516 repo, ctx, subs, extramsg, ref)
2515 2517 break
2516 2518 forms.pop()
2517 2519 else:
2518 2520 committext = buildcommittext(repo, ctx, subs, extramsg)
2519 2521
2520 2522 # run editor in the repository root
2521 2523 olddir = pycompat.getcwd()
2522 2524 os.chdir(repo.root)
2523 2525
2524 2526 # make in-memory changes visible to external process
2525 2527 tr = repo.currenttransaction()
2526 2528 repo.dirstate.write(tr)
2527 2529 pending = tr and tr.writepending() and repo.root
2528 2530
2529 2531 editortext = repo.ui.edit(committext, ctx.user(), ctx.extra(),
2530 2532 editform=editform, pending=pending,
2531 2533 repopath=repo.path, action='commit')
2532 2534 text = editortext
2533 2535
2534 2536 # strip away anything below this special string (used for editors that want
2535 2537 # to display the diff)
2536 2538 stripbelow = re.search(_linebelow, text, flags=re.MULTILINE)
2537 2539 if stripbelow:
2538 2540 text = text[:stripbelow.start()]
2539 2541
2540 2542 text = re.sub("(?m)^HG:.*(\n|$)", "", text)
2541 2543 os.chdir(olddir)
2542 2544
2543 2545 if finishdesc:
2544 2546 text = finishdesc(text)
2545 2547 if not text.strip():
2546 2548 raise error.Abort(_("empty commit message"))
2547 2549 if unchangedmessagedetection and editortext == templatetext:
2548 2550 raise error.Abort(_("commit message unchanged"))
2549 2551
2550 2552 return text
2551 2553
2552 2554 def buildcommittemplate(repo, ctx, subs, extramsg, ref):
2553 2555 ui = repo.ui
2554 2556 spec = formatter.templatespec(ref, None, None)
2555 2557 t = logcmdutil.changesettemplater(ui, repo, spec)
2556 2558 t.t.cache.update((k, templater.unquotestring(v))
2557 2559 for k, v in repo.ui.configitems('committemplate'))
2558 2560
2559 2561 if not extramsg:
2560 2562 extramsg = '' # ensure that extramsg is string
2561 2563
2562 2564 ui.pushbuffer()
2563 2565 t.show(ctx, extramsg=extramsg)
2564 2566 return ui.popbuffer()
2565 2567
2566 2568 def hgprefix(msg):
2567 2569 return "\n".join(["HG: %s" % a for a in msg.split("\n") if a])
2568 2570
2569 2571 def buildcommittext(repo, ctx, subs, extramsg):
2570 2572 edittext = []
2571 2573 modified, added, removed = ctx.modified(), ctx.added(), ctx.removed()
2572 2574 if ctx.description():
2573 2575 edittext.append(ctx.description())
2574 2576 edittext.append("")
2575 2577 edittext.append("") # Empty line between message and comments.
2576 2578 edittext.append(hgprefix(_("Enter commit message."
2577 2579 " Lines beginning with 'HG:' are removed.")))
2578 2580 edittext.append(hgprefix(extramsg))
2579 2581 edittext.append("HG: --")
2580 2582 edittext.append(hgprefix(_("user: %s") % ctx.user()))
2581 2583 if ctx.p2():
2582 2584 edittext.append(hgprefix(_("branch merge")))
2583 2585 if ctx.branch():
2584 2586 edittext.append(hgprefix(_("branch '%s'") % ctx.branch()))
2585 2587 if bookmarks.isactivewdirparent(repo):
2586 2588 edittext.append(hgprefix(_("bookmark '%s'") % repo._activebookmark))
2587 2589 edittext.extend([hgprefix(_("subrepo %s") % s) for s in subs])
2588 2590 edittext.extend([hgprefix(_("added %s") % f) for f in added])
2589 2591 edittext.extend([hgprefix(_("changed %s") % f) for f in modified])
2590 2592 edittext.extend([hgprefix(_("removed %s") % f) for f in removed])
2591 2593 if not added and not modified and not removed:
2592 2594 edittext.append(hgprefix(_("no files changed")))
2593 2595 edittext.append("")
2594 2596
2595 2597 return "\n".join(edittext)
2596 2598
2597 2599 def commitstatus(repo, node, branch, bheads=None, opts=None):
2598 2600 if opts is None:
2599 2601 opts = {}
2600 2602 ctx = repo[node]
2601 2603 parents = ctx.parents()
2602 2604
2603 2605 if (not opts.get('amend') and bheads and node not in bheads and not
2604 2606 [x for x in parents if x.node() in bheads and x.branch() == branch]):
2605 2607 repo.ui.status(_('created new head\n'))
2606 2608 # The message is not printed for initial roots. For the other
2607 2609 # changesets, it is printed in the following situations:
2608 2610 #
2609 2611 # Par column: for the 2 parents with ...
2610 2612 # N: null or no parent
2611 2613 # B: parent is on another named branch
2612 2614 # C: parent is a regular non head changeset
2613 2615 # H: parent was a branch head of the current branch
2614 2616 # Msg column: whether we print "created new head" message
2615 2617 # In the following, it is assumed that there already exists some
2616 2618 # initial branch heads of the current branch, otherwise nothing is
2617 2619 # printed anyway.
2618 2620 #
2619 2621 # Par Msg Comment
2620 2622 # N N y additional topo root
2621 2623 #
2622 2624 # B N y additional branch root
2623 2625 # C N y additional topo head
2624 2626 # H N n usual case
2625 2627 #
2626 2628 # B B y weird additional branch root
2627 2629 # C B y branch merge
2628 2630 # H B n merge with named branch
2629 2631 #
2630 2632 # C C y additional head from merge
2631 2633 # C H n merge with a head
2632 2634 #
2633 2635 # H H n head merge: head count decreases
2634 2636
2635 2637 if not opts.get('close_branch'):
2636 2638 for r in parents:
2637 2639 if r.closesbranch() and r.branch() == branch:
2638 2640 repo.ui.status(_('reopening closed branch head %d\n') % r.rev())
2639 2641
2640 2642 if repo.ui.debugflag:
2641 2643 repo.ui.write(_('committed changeset %d:%s\n') % (ctx.rev(), ctx.hex()))
2642 2644 elif repo.ui.verbose:
2643 2645 repo.ui.write(_('committed changeset %d:%s\n') % (ctx.rev(), ctx))
2644 2646
2645 2647 def postcommitstatus(repo, pats, opts):
2646 2648 return repo.status(match=scmutil.match(repo[None], pats, opts))
2647 2649
2648 2650 def revert(ui, repo, ctx, parents, *pats, **opts):
2649 2651 opts = pycompat.byteskwargs(opts)
2650 2652 parent, p2 = parents
2651 2653 node = ctx.node()
2652 2654
2653 2655 mf = ctx.manifest()
2654 2656 if node == p2:
2655 2657 parent = p2
2656 2658
2657 2659 # need all matching names in dirstate and manifest of target rev,
2658 2660 # so have to walk both. do not print errors if files exist in one
2659 2661 # but not other. in both cases, filesets should be evaluated against
2660 2662 # workingctx to get consistent result (issue4497). this means 'set:**'
2661 2663 # cannot be used to select missing files from target rev.
2662 2664
2663 2665 # `names` is a mapping for all elements in working copy and target revision
2664 2666 # The mapping is in the form:
2665 2667 # <asb path in repo> -> (<path from CWD>, <exactly specified by matcher?>)
2666 2668 names = {}
2667 2669
2668 2670 with repo.wlock():
2669 2671 ## filling of the `names` mapping
2670 2672 # walk dirstate to fill `names`
2671 2673
2672 2674 interactive = opts.get('interactive', False)
2673 2675 wctx = repo[None]
2674 2676 m = scmutil.match(wctx, pats, opts)
2675 2677
2676 2678 # we'll need this later
2677 2679 targetsubs = sorted(s for s in wctx.substate if m(s))
2678 2680
2679 2681 if not m.always():
2680 2682 matcher = matchmod.badmatch(m, lambda x, y: False)
2681 2683 for abs in wctx.walk(matcher):
2682 2684 names[abs] = m.rel(abs), m.exact(abs)
2683 2685
2684 2686 # walk target manifest to fill `names`
2685 2687
2686 2688 def badfn(path, msg):
2687 2689 if path in names:
2688 2690 return
2689 2691 if path in ctx.substate:
2690 2692 return
2691 2693 path_ = path + '/'
2692 2694 for f in names:
2693 2695 if f.startswith(path_):
2694 2696 return
2695 2697 ui.warn("%s: %s\n" % (m.rel(path), msg))
2696 2698
2697 2699 for abs in ctx.walk(matchmod.badmatch(m, badfn)):
2698 2700 if abs not in names:
2699 2701 names[abs] = m.rel(abs), m.exact(abs)
2700 2702
2701 2703 # Find status of all file in `names`.
2702 2704 m = scmutil.matchfiles(repo, names)
2703 2705
2704 2706 changes = repo.status(node1=node, match=m,
2705 2707 unknown=True, ignored=True, clean=True)
2706 2708 else:
2707 2709 changes = repo.status(node1=node, match=m)
2708 2710 for kind in changes:
2709 2711 for abs in kind:
2710 2712 names[abs] = m.rel(abs), m.exact(abs)
2711 2713
2712 2714 m = scmutil.matchfiles(repo, names)
2713 2715
2714 2716 modified = set(changes.modified)
2715 2717 added = set(changes.added)
2716 2718 removed = set(changes.removed)
2717 2719 _deleted = set(changes.deleted)
2718 2720 unknown = set(changes.unknown)
2719 2721 unknown.update(changes.ignored)
2720 2722 clean = set(changes.clean)
2721 2723 modadded = set()
2722 2724
2723 2725 # We need to account for the state of the file in the dirstate,
2724 2726 # even when we revert against something else than parent. This will
2725 2727 # slightly alter the behavior of revert (doing back up or not, delete
2726 2728 # or just forget etc).
2727 2729 if parent == node:
2728 2730 dsmodified = modified
2729 2731 dsadded = added
2730 2732 dsremoved = removed
2731 2733 # store all local modifications, useful later for rename detection
2732 2734 localchanges = dsmodified | dsadded
2733 2735 modified, added, removed = set(), set(), set()
2734 2736 else:
2735 2737 changes = repo.status(node1=parent, match=m)
2736 2738 dsmodified = set(changes.modified)
2737 2739 dsadded = set(changes.added)
2738 2740 dsremoved = set(changes.removed)
2739 2741 # store all local modifications, useful later for rename detection
2740 2742 localchanges = dsmodified | dsadded
2741 2743
2742 2744 # only take into account for removes between wc and target
2743 2745 clean |= dsremoved - removed
2744 2746 dsremoved &= removed
2745 2747 # distinct between dirstate remove and other
2746 2748 removed -= dsremoved
2747 2749
2748 2750 modadded = added & dsmodified
2749 2751 added -= modadded
2750 2752
2751 2753 # tell newly modified apart.
2752 2754 dsmodified &= modified
2753 2755 dsmodified |= modified & dsadded # dirstate added may need backup
2754 2756 modified -= dsmodified
2755 2757
2756 2758 # We need to wait for some post-processing to update this set
2757 2759 # before making the distinction. The dirstate will be used for
2758 2760 # that purpose.
2759 2761 dsadded = added
2760 2762
2761 2763 # in case of merge, files that are actually added can be reported as
2762 2764 # modified, we need to post process the result
2763 2765 if p2 != nullid:
2764 2766 mergeadd = set(dsmodified)
2765 2767 for path in dsmodified:
2766 2768 if path in mf:
2767 2769 mergeadd.remove(path)
2768 2770 dsadded |= mergeadd
2769 2771 dsmodified -= mergeadd
2770 2772
2771 2773 # if f is a rename, update `names` to also revert the source
2772 2774 cwd = repo.getcwd()
2773 2775 for f in localchanges:
2774 2776 src = repo.dirstate.copied(f)
2775 2777 # XXX should we check for rename down to target node?
2776 2778 if src and src not in names and repo.dirstate[src] == 'r':
2777 2779 dsremoved.add(src)
2778 2780 names[src] = (repo.pathto(src, cwd), True)
2779 2781
2780 2782 # determine the exact nature of the deleted changesets
2781 2783 deladded = set(_deleted)
2782 2784 for path in _deleted:
2783 2785 if path in mf:
2784 2786 deladded.remove(path)
2785 2787 deleted = _deleted - deladded
2786 2788
2787 2789 # distinguish between file to forget and the other
2788 2790 added = set()
2789 2791 for abs in dsadded:
2790 2792 if repo.dirstate[abs] != 'a':
2791 2793 added.add(abs)
2792 2794 dsadded -= added
2793 2795
2794 2796 for abs in deladded:
2795 2797 if repo.dirstate[abs] == 'a':
2796 2798 dsadded.add(abs)
2797 2799 deladded -= dsadded
2798 2800
2799 2801 # For files marked as removed, we check if an unknown file is present at
2800 2802 # the same path. If a such file exists it may need to be backed up.
2801 2803 # Making the distinction at this stage helps have simpler backup
2802 2804 # logic.
2803 2805 removunk = set()
2804 2806 for abs in removed:
2805 2807 target = repo.wjoin(abs)
2806 2808 if os.path.lexists(target):
2807 2809 removunk.add(abs)
2808 2810 removed -= removunk
2809 2811
2810 2812 dsremovunk = set()
2811 2813 for abs in dsremoved:
2812 2814 target = repo.wjoin(abs)
2813 2815 if os.path.lexists(target):
2814 2816 dsremovunk.add(abs)
2815 2817 dsremoved -= dsremovunk
2816 2818
2817 2819 # action to be actually performed by revert
2818 2820 # (<list of file>, message>) tuple
2819 2821 actions = {'revert': ([], _('reverting %s\n')),
2820 2822 'add': ([], _('adding %s\n')),
2821 2823 'remove': ([], _('removing %s\n')),
2822 2824 'drop': ([], _('removing %s\n')),
2823 2825 'forget': ([], _('forgetting %s\n')),
2824 2826 'undelete': ([], _('undeleting %s\n')),
2825 2827 'noop': (None, _('no changes needed to %s\n')),
2826 2828 'unknown': (None, _('file not managed: %s\n')),
2827 2829 }
2828 2830
2829 2831 # "constant" that convey the backup strategy.
2830 2832 # All set to `discard` if `no-backup` is set do avoid checking
2831 2833 # no_backup lower in the code.
2832 2834 # These values are ordered for comparison purposes
2833 2835 backupinteractive = 3 # do backup if interactively modified
2834 2836 backup = 2 # unconditionally do backup
2835 2837 check = 1 # check if the existing file differs from target
2836 2838 discard = 0 # never do backup
2837 2839 if opts.get('no_backup'):
2838 2840 backupinteractive = backup = check = discard
2839 2841 if interactive:
2840 2842 dsmodifiedbackup = backupinteractive
2841 2843 else:
2842 2844 dsmodifiedbackup = backup
2843 2845 tobackup = set()
2844 2846
2845 2847 backupanddel = actions['remove']
2846 2848 if not opts.get('no_backup'):
2847 2849 backupanddel = actions['drop']
2848 2850
2849 2851 disptable = (
2850 2852 # dispatch table:
2851 2853 # file state
2852 2854 # action
2853 2855 # make backup
2854 2856
2855 2857 ## Sets that results that will change file on disk
2856 2858 # Modified compared to target, no local change
2857 2859 (modified, actions['revert'], discard),
2858 2860 # Modified compared to target, but local file is deleted
2859 2861 (deleted, actions['revert'], discard),
2860 2862 # Modified compared to target, local change
2861 2863 (dsmodified, actions['revert'], dsmodifiedbackup),
2862 2864 # Added since target
2863 2865 (added, actions['remove'], discard),
2864 2866 # Added in working directory
2865 2867 (dsadded, actions['forget'], discard),
2866 2868 # Added since target, have local modification
2867 2869 (modadded, backupanddel, backup),
2868 2870 # Added since target but file is missing in working directory
2869 2871 (deladded, actions['drop'], discard),
2870 2872 # Removed since target, before working copy parent
2871 2873 (removed, actions['add'], discard),
2872 2874 # Same as `removed` but an unknown file exists at the same path
2873 2875 (removunk, actions['add'], check),
2874 2876 # Removed since targe, marked as such in working copy parent
2875 2877 (dsremoved, actions['undelete'], discard),
2876 2878 # Same as `dsremoved` but an unknown file exists at the same path
2877 2879 (dsremovunk, actions['undelete'], check),
2878 2880 ## the following sets does not result in any file changes
2879 2881 # File with no modification
2880 2882 (clean, actions['noop'], discard),
2881 2883 # Existing file, not tracked anywhere
2882 2884 (unknown, actions['unknown'], discard),
2883 2885 )
2884 2886
2885 2887 for abs, (rel, exact) in sorted(names.items()):
2886 2888 # target file to be touch on disk (relative to cwd)
2887 2889 target = repo.wjoin(abs)
2888 2890 # search the entry in the dispatch table.
2889 2891 # if the file is in any of these sets, it was touched in the working
2890 2892 # directory parent and we are sure it needs to be reverted.
2891 2893 for table, (xlist, msg), dobackup in disptable:
2892 2894 if abs not in table:
2893 2895 continue
2894 2896 if xlist is not None:
2895 2897 xlist.append(abs)
2896 2898 if dobackup:
2897 2899 # If in interactive mode, don't automatically create
2898 2900 # .orig files (issue4793)
2899 2901 if dobackup == backupinteractive:
2900 2902 tobackup.add(abs)
2901 2903 elif (backup <= dobackup or wctx[abs].cmp(ctx[abs])):
2902 2904 bakname = scmutil.origpath(ui, repo, rel)
2903 2905 ui.note(_('saving current version of %s as %s\n') %
2904 2906 (rel, bakname))
2905 2907 if not opts.get('dry_run'):
2906 2908 if interactive:
2907 2909 util.copyfile(target, bakname)
2908 2910 else:
2909 2911 util.rename(target, bakname)
2910 2912 if ui.verbose or not exact:
2911 2913 if not isinstance(msg, bytes):
2912 2914 msg = msg(abs)
2913 2915 ui.status(msg % rel)
2914 2916 elif exact:
2915 2917 ui.warn(msg % rel)
2916 2918 break
2917 2919
2918 2920 if not opts.get('dry_run'):
2919 2921 needdata = ('revert', 'add', 'undelete')
2920 2922 if _revertprefetch is not _revertprefetchstub:
2921 2923 ui.deprecwarn("'cmdutil._revertprefetch' is deprecated, "
2922 2924 "add a callback to 'scmutil.fileprefetchhooks'",
2923 2925 '4.6', stacklevel=1)
2924 2926 _revertprefetch(repo, ctx,
2925 2927 *[actions[name][0] for name in needdata])
2926 2928 oplist = [actions[name][0] for name in needdata]
2927 2929 prefetch = scmutil.fileprefetchhooks
2928 2930 prefetch(repo, ctx, [f for sublist in oplist for f in sublist])
2929 2931 _performrevert(repo, parents, ctx, actions, interactive, tobackup)
2930 2932
2931 2933 if targetsubs:
2932 2934 # Revert the subrepos on the revert list
2933 2935 for sub in targetsubs:
2934 2936 try:
2935 2937 wctx.sub(sub).revert(ctx.substate[sub], *pats,
2936 2938 **pycompat.strkwargs(opts))
2937 2939 except KeyError:
2938 2940 raise error.Abort("subrepository '%s' does not exist in %s!"
2939 2941 % (sub, short(ctx.node())))
2940 2942
2941 2943 def _revertprefetchstub(repo, ctx, *files):
2942 2944 """Stub method for detecting extension wrapping of _revertprefetch(), to
2943 2945 issue a deprecation warning."""
2944 2946
2945 2947 _revertprefetch = _revertprefetchstub
2946 2948
2947 2949 def _performrevert(repo, parents, ctx, actions, interactive=False,
2948 2950 tobackup=None):
2949 2951 """function that actually perform all the actions computed for revert
2950 2952
2951 2953 This is an independent function to let extension to plug in and react to
2952 2954 the imminent revert.
2953 2955
2954 2956 Make sure you have the working directory locked when calling this function.
2955 2957 """
2956 2958 parent, p2 = parents
2957 2959 node = ctx.node()
2958 2960 excluded_files = []
2959 2961
2960 2962 def checkout(f):
2961 2963 fc = ctx[f]
2962 2964 repo.wwrite(f, fc.data(), fc.flags())
2963 2965
2964 2966 def doremove(f):
2965 2967 try:
2966 2968 repo.wvfs.unlinkpath(f)
2967 2969 except OSError:
2968 2970 pass
2969 2971 repo.dirstate.remove(f)
2970 2972
2971 2973 audit_path = pathutil.pathauditor(repo.root, cached=True)
2972 2974 for f in actions['forget'][0]:
2973 2975 if interactive:
2974 2976 choice = repo.ui.promptchoice(
2975 2977 _("forget added file %s (Yn)?$$ &Yes $$ &No") % f)
2976 2978 if choice == 0:
2977 2979 repo.dirstate.drop(f)
2978 2980 else:
2979 2981 excluded_files.append(f)
2980 2982 else:
2981 2983 repo.dirstate.drop(f)
2982 2984 for f in actions['remove'][0]:
2983 2985 audit_path(f)
2984 2986 if interactive:
2985 2987 choice = repo.ui.promptchoice(
2986 2988 _("remove added file %s (Yn)?$$ &Yes $$ &No") % f)
2987 2989 if choice == 0:
2988 2990 doremove(f)
2989 2991 else:
2990 2992 excluded_files.append(f)
2991 2993 else:
2992 2994 doremove(f)
2993 2995 for f in actions['drop'][0]:
2994 2996 audit_path(f)
2995 2997 repo.dirstate.remove(f)
2996 2998
2997 2999 normal = None
2998 3000 if node == parent:
2999 3001 # We're reverting to our parent. If possible, we'd like status
3000 3002 # to report the file as clean. We have to use normallookup for
3001 3003 # merges to avoid losing information about merged/dirty files.
3002 3004 if p2 != nullid:
3003 3005 normal = repo.dirstate.normallookup
3004 3006 else:
3005 3007 normal = repo.dirstate.normal
3006 3008
3007 3009 newlyaddedandmodifiedfiles = set()
3008 3010 if interactive:
3009 3011 # Prompt the user for changes to revert
3010 3012 torevert = [f for f in actions['revert'][0] if f not in excluded_files]
3011 3013 m = scmutil.matchfiles(repo, torevert)
3012 3014 diffopts = patch.difffeatureopts(repo.ui, whitespace=True)
3013 3015 diffopts.nodates = True
3014 3016 diffopts.git = True
3015 3017 operation = 'discard'
3016 3018 reversehunks = True
3017 3019 if node != parent:
3018 3020 operation = 'apply'
3019 3021 reversehunks = False
3020 3022 if reversehunks:
3021 3023 diff = patch.diff(repo, ctx.node(), None, m, opts=diffopts)
3022 3024 else:
3023 3025 diff = patch.diff(repo, None, ctx.node(), m, opts=diffopts)
3024 3026 originalchunks = patch.parsepatch(diff)
3025 3027
3026 3028 try:
3027 3029
3028 3030 chunks, opts = recordfilter(repo.ui, originalchunks,
3029 3031 operation=operation)
3030 3032 if reversehunks:
3031 3033 chunks = patch.reversehunks(chunks)
3032 3034
3033 3035 except error.PatchError as err:
3034 3036 raise error.Abort(_('error parsing patch: %s') % err)
3035 3037
3036 3038 newlyaddedandmodifiedfiles = newandmodified(chunks, originalchunks)
3037 3039 if tobackup is None:
3038 3040 tobackup = set()
3039 3041 # Apply changes
3040 3042 fp = stringio()
3041 3043 for c in chunks:
3042 3044 # Create a backup file only if this hunk should be backed up
3043 3045 if ishunk(c) and c.header.filename() in tobackup:
3044 3046 abs = c.header.filename()
3045 3047 target = repo.wjoin(abs)
3046 3048 bakname = scmutil.origpath(repo.ui, repo, m.rel(abs))
3047 3049 util.copyfile(target, bakname)
3048 3050 tobackup.remove(abs)
3049 3051 c.write(fp)
3050 3052 dopatch = fp.tell()
3051 3053 fp.seek(0)
3052 3054 if dopatch:
3053 3055 try:
3054 3056 patch.internalpatch(repo.ui, repo, fp, 1, eolmode=None)
3055 3057 except error.PatchError as err:
3056 3058 raise error.Abort(pycompat.bytestr(err))
3057 3059 del fp
3058 3060 else:
3059 3061 for f in actions['revert'][0]:
3060 3062 checkout(f)
3061 3063 if normal:
3062 3064 normal(f)
3063 3065
3064 3066 for f in actions['add'][0]:
3065 3067 # Don't checkout modified files, they are already created by the diff
3066 3068 if f not in newlyaddedandmodifiedfiles:
3067 3069 checkout(f)
3068 3070 repo.dirstate.add(f)
3069 3071
3070 3072 normal = repo.dirstate.normallookup
3071 3073 if node == parent and p2 == nullid:
3072 3074 normal = repo.dirstate.normal
3073 3075 for f in actions['undelete'][0]:
3074 3076 checkout(f)
3075 3077 normal(f)
3076 3078
3077 3079 copied = copies.pathcopies(repo[parent], ctx)
3078 3080
3079 3081 for f in actions['add'][0] + actions['undelete'][0] + actions['revert'][0]:
3080 3082 if f in copied:
3081 3083 repo.dirstate.copy(copied[f], f)
3082 3084
3083 3085 class command(registrar.command):
3084 3086 """deprecated: used registrar.command instead"""
3085 3087 def _doregister(self, func, name, *args, **kwargs):
3086 3088 func._deprecatedregistrar = True # flag for deprecwarn in extensions.py
3087 3089 return super(command, self)._doregister(func, name, *args, **kwargs)
3088 3090
3089 3091 # a list of (ui, repo, otherpeer, opts, missing) functions called by
3090 3092 # commands.outgoing. "missing" is "missing" of the result of
3091 3093 # "findcommonoutgoing()"
3092 3094 outgoinghooks = util.hooks()
3093 3095
3094 3096 # a list of (ui, repo) functions called by commands.summary
3095 3097 summaryhooks = util.hooks()
3096 3098
3097 3099 # a list of (ui, repo, opts, changes) functions called by commands.summary.
3098 3100 #
3099 3101 # functions should return tuple of booleans below, if 'changes' is None:
3100 3102 # (whether-incomings-are-needed, whether-outgoings-are-needed)
3101 3103 #
3102 3104 # otherwise, 'changes' is a tuple of tuples below:
3103 3105 # - (sourceurl, sourcebranch, sourcepeer, incoming)
3104 3106 # - (desturl, destbranch, destpeer, outgoing)
3105 3107 summaryremotehooks = util.hooks()
3106 3108
3107 3109 # A list of state files kept by multistep operations like graft.
3108 3110 # Since graft cannot be aborted, it is considered 'clearable' by update.
3109 3111 # note: bisect is intentionally excluded
3110 3112 # (state file, clearable, allowcommit, error, hint)
3111 3113 unfinishedstates = [
3112 3114 ('graftstate', True, False, _('graft in progress'),
3113 3115 _("use 'hg graft --continue' or 'hg update' to abort")),
3114 3116 ('updatestate', True, False, _('last update was interrupted'),
3115 3117 _("use 'hg update' to get a consistent checkout"))
3116 3118 ]
3117 3119
3118 3120 def checkunfinished(repo, commit=False):
3119 3121 '''Look for an unfinished multistep operation, like graft, and abort
3120 3122 if found. It's probably good to check this right before
3121 3123 bailifchanged().
3122 3124 '''
3123 3125 for f, clearable, allowcommit, msg, hint in unfinishedstates:
3124 3126 if commit and allowcommit:
3125 3127 continue
3126 3128 if repo.vfs.exists(f):
3127 3129 raise error.Abort(msg, hint=hint)
3128 3130
3129 3131 def clearunfinished(repo):
3130 3132 '''Check for unfinished operations (as above), and clear the ones
3131 3133 that are clearable.
3132 3134 '''
3133 3135 for f, clearable, allowcommit, msg, hint in unfinishedstates:
3134 3136 if not clearable and repo.vfs.exists(f):
3135 3137 raise error.Abort(msg, hint=hint)
3136 3138 for f, clearable, allowcommit, msg, hint in unfinishedstates:
3137 3139 if clearable and repo.vfs.exists(f):
3138 3140 util.unlink(repo.vfs.join(f))
3139 3141
3140 3142 afterresolvedstates = [
3141 3143 ('graftstate',
3142 3144 _('hg graft --continue')),
3143 3145 ]
3144 3146
3145 3147 def howtocontinue(repo):
3146 3148 '''Check for an unfinished operation and return the command to finish
3147 3149 it.
3148 3150
3149 3151 afterresolvedstates tuples define a .hg/{file} and the corresponding
3150 3152 command needed to finish it.
3151 3153
3152 3154 Returns a (msg, warning) tuple. 'msg' is a string and 'warning' is
3153 3155 a boolean.
3154 3156 '''
3155 3157 contmsg = _("continue: %s")
3156 3158 for f, msg in afterresolvedstates:
3157 3159 if repo.vfs.exists(f):
3158 3160 return contmsg % msg, True
3159 3161 if repo[None].dirty(missing=True, merge=False, branch=False):
3160 3162 return contmsg % _("hg commit"), False
3161 3163 return None, None
3162 3164
3163 3165 def checkafterresolved(repo):
3164 3166 '''Inform the user about the next action after completing hg resolve
3165 3167
3166 3168 If there's a matching afterresolvedstates, howtocontinue will yield
3167 3169 repo.ui.warn as the reporter.
3168 3170
3169 3171 Otherwise, it will yield repo.ui.note.
3170 3172 '''
3171 3173 msg, warning = howtocontinue(repo)
3172 3174 if msg is not None:
3173 3175 if warning:
3174 3176 repo.ui.warn("%s\n" % msg)
3175 3177 else:
3176 3178 repo.ui.note("%s\n" % msg)
3177 3179
3178 3180 def wrongtooltocontinue(repo, task):
3179 3181 '''Raise an abort suggesting how to properly continue if there is an
3180 3182 active task.
3181 3183
3182 3184 Uses howtocontinue() to find the active task.
3183 3185
3184 3186 If there's no task (repo.ui.note for 'hg commit'), it does not offer
3185 3187 a hint.
3186 3188 '''
3187 3189 after = howtocontinue(repo)
3188 3190 hint = None
3189 3191 if after[1]:
3190 3192 hint = after[0]
3191 3193 raise error.Abort(_('no %s in progress') % task, hint=hint)
3192 3194
3193 3195 class changeset_printer(logcmdutil.changesetprinter):
3194 3196
3195 3197 def __init__(self, ui, *args, **kwargs):
3196 3198 msg = ("'cmdutil.changeset_printer' is deprecated, "
3197 3199 "use 'logcmdutil.logcmdutil'")
3198 3200 ui.deprecwarn(msg, "4.6")
3199 3201 super(changeset_printer, self).__init__(ui, *args, **kwargs)
3200 3202
3201 3203 def displaygraph(ui, *args, **kwargs):
3202 3204 msg = ("'cmdutil.displaygraph' is deprecated, "
3203 3205 "use 'logcmdutil.displaygraph'")
3204 3206 ui.deprecwarn(msg, "4.6")
3205 3207 return logcmdutil.displaygraph(ui, *args, **kwargs)
3206 3208
3207 3209 def show_changeset(ui, *args, **kwargs):
3208 3210 msg = ("'cmdutil.show_changeset' is deprecated, "
3209 3211 "use 'logcmdutil.changesetdisplayer'")
3210 3212 ui.deprecwarn(msg, "4.6")
3211 3213 return logcmdutil.changesetdisplayer(ui, *args, **kwargs)
@@ -1,659 +1,692 b''
1 1 $ hg init
2 2 $ mkdir d1 d1/d11 d2
3 3 $ echo d1/a > d1/a
4 4 $ echo d1/ba > d1/ba
5 5 $ echo d1/a1 > d1/d11/a1
6 6 $ echo d1/b > d1/b
7 7 $ echo d2/b > d2/b
8 8 $ hg add d1/a d1/b d1/ba d1/d11/a1 d2/b
9 9 $ hg commit -m "1"
10 10
11 11 rename a single file
12 12
13 13 $ hg rename d1/d11/a1 d2/c
14 14 $ hg --config ui.portablefilenames=abort rename d1/a d1/con.xml
15 15 abort: filename contains 'con', which is reserved on Windows: d1/con.xml
16 16 [255]
17 17 $ hg sum
18 18 parent: 0:9b4b6e7b2c26 tip
19 19 1
20 20 branch: default
21 21 commit: 1 renamed
22 22 update: (current)
23 23 phases: 1 draft
24 24 $ hg status -C
25 25 A d2/c
26 26 d1/d11/a1
27 27 R d1/d11/a1
28 28 $ hg update -C
29 29 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
30 30 $ rm d2/c
31 31
32 32 rename a single file using absolute paths
33 33
34 34 $ hg rename `pwd`/d1/d11/a1 `pwd`/d2/c
35 35 $ hg status -C
36 36 A d2/c
37 37 d1/d11/a1
38 38 R d1/d11/a1
39 39 $ hg update -C
40 40 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
41 41 $ rm d2/c
42 42
43 43 rename --after a single file
44 44
45 45 $ mv d1/d11/a1 d2/c
46 46 $ hg rename --after d1/d11/a1 d2/c
47 47 $ hg status -C
48 48 A d2/c
49 49 d1/d11/a1
50 50 R d1/d11/a1
51 51 $ hg update -C
52 52 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
53 53 $ rm d2/c
54 54
55 55 rename --after a single file when src and tgt already tracked
56 56
57 57 $ mv d1/d11/a1 d2/c
58 58 $ hg addrem -s 0
59 59 removing d1/d11/a1
60 60 adding d2/c
61 61 $ hg rename --after d1/d11/a1 d2/c
62 62 $ hg status -C
63 63 A d2/c
64 64 d1/d11/a1
65 65 R d1/d11/a1
66 66 $ hg update -C
67 67 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
68 68 $ rm d2/c
69 69
70 70 rename --after a single file to a nonexistent target filename
71 71
72 72 $ hg rename --after d1/a dummy
73 73 d1/a: not recording move - dummy does not exist
74 74
75 75 move a single file to an existing directory
76 76
77 77 $ hg rename d1/d11/a1 d2
78 78 $ hg status -C
79 79 A d2/a1
80 80 d1/d11/a1
81 81 R d1/d11/a1
82 82 $ hg update -C
83 83 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
84 84 $ rm d2/a1
85 85
86 86 move --after a single file to an existing directory
87 87
88 88 $ mv d1/d11/a1 d2
89 89 $ hg rename --after d1/d11/a1 d2
90 90 $ hg status -C
91 91 A d2/a1
92 92 d1/d11/a1
93 93 R d1/d11/a1
94 94 $ hg update -C
95 95 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
96 96 $ rm d2/a1
97 97
98 98 rename a file using a relative path
99 99
100 100 $ (cd d1/d11; hg rename ../../d2/b e)
101 101 $ hg status -C
102 102 A d1/d11/e
103 103 d2/b
104 104 R d2/b
105 105 $ hg update -C
106 106 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
107 107 $ rm d1/d11/e
108 108
109 109 rename --after a file using a relative path
110 110
111 111 $ (cd d1/d11; mv ../../d2/b e; hg rename --after ../../d2/b e)
112 112 $ hg status -C
113 113 A d1/d11/e
114 114 d2/b
115 115 R d2/b
116 116 $ hg update -C
117 117 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
118 118 $ rm d1/d11/e
119 119
120 120 rename directory d1 as d3
121 121
122 122 $ hg rename d1/ d3
123 123 moving d1/a to d3/a
124 124 moving d1/b to d3/b
125 125 moving d1/ba to d3/ba
126 126 moving d1/d11/a1 to d3/d11/a1
127 127 $ hg status -C
128 128 A d3/a
129 129 d1/a
130 130 A d3/b
131 131 d1/b
132 132 A d3/ba
133 133 d1/ba
134 134 A d3/d11/a1
135 135 d1/d11/a1
136 136 R d1/a
137 137 R d1/b
138 138 R d1/ba
139 139 R d1/d11/a1
140 140 $ hg update -C
141 141 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
142 142 $ rm -rf d3
143 143
144 144 rename --after directory d1 as d3
145 145
146 146 $ mv d1 d3
147 147 $ hg rename --after d1 d3
148 148 moving d1/a to d3/a
149 149 moving d1/b to d3/b
150 150 moving d1/ba to d3/ba
151 151 moving d1/d11/a1 to d3/d11/a1
152 152 $ hg status -C
153 153 A d3/a
154 154 d1/a
155 155 A d3/b
156 156 d1/b
157 157 A d3/ba
158 158 d1/ba
159 159 A d3/d11/a1
160 160 d1/d11/a1
161 161 R d1/a
162 162 R d1/b
163 163 R d1/ba
164 164 R d1/d11/a1
165 165 $ hg update -C
166 166 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
167 167 $ rm -rf d3
168 168
169 169 move a directory using a relative path
170 170
171 171 $ (cd d2; mkdir d3; hg rename ../d1/d11 d3)
172 172 moving ../d1/d11/a1 to d3/d11/a1
173 173 $ hg status -C
174 174 A d2/d3/d11/a1
175 175 d1/d11/a1
176 176 R d1/d11/a1
177 177 $ hg update -C
178 178 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
179 179 $ rm -rf d2/d3
180 180
181 181 move --after a directory using a relative path
182 182
183 183 $ (cd d2; mkdir d3; mv ../d1/d11 d3; hg rename --after ../d1/d11 d3)
184 184 moving ../d1/d11/a1 to d3/d11/a1
185 185 $ hg status -C
186 186 A d2/d3/d11/a1
187 187 d1/d11/a1
188 188 R d1/d11/a1
189 189 $ hg update -C
190 190 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
191 191 $ rm -rf d2/d3
192 192
193 193 move directory d1/d11 to an existing directory d2 (removes empty d1)
194 194
195 195 $ hg rename d1/d11/ d2
196 196 moving d1/d11/a1 to d2/d11/a1
197 197 $ hg status -C
198 198 A d2/d11/a1
199 199 d1/d11/a1
200 200 R d1/d11/a1
201 201 $ hg update -C
202 202 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
203 203 $ rm -rf d2/d11
204 204
205 205 move directories d1 and d2 to a new directory d3
206 206
207 207 $ mkdir d3
208 208 $ hg rename d1 d2 d3
209 209 moving d1/a to d3/d1/a
210 210 moving d1/b to d3/d1/b
211 211 moving d1/ba to d3/d1/ba
212 212 moving d1/d11/a1 to d3/d1/d11/a1
213 213 moving d2/b to d3/d2/b
214 214 $ hg status -C
215 215 A d3/d1/a
216 216 d1/a
217 217 A d3/d1/b
218 218 d1/b
219 219 A d3/d1/ba
220 220 d1/ba
221 221 A d3/d1/d11/a1
222 222 d1/d11/a1
223 223 A d3/d2/b
224 224 d2/b
225 225 R d1/a
226 226 R d1/b
227 227 R d1/ba
228 228 R d1/d11/a1
229 229 R d2/b
230 230 $ hg update -C
231 231 5 files updated, 0 files merged, 0 files removed, 0 files unresolved
232 232 $ rm -rf d3
233 233
234 234 move --after directories d1 and d2 to a new directory d3
235 235
236 236 $ mkdir d3
237 237 $ mv d1 d2 d3
238 238 $ hg rename --after d1 d2 d3
239 239 moving d1/a to d3/d1/a
240 240 moving d1/b to d3/d1/b
241 241 moving d1/ba to d3/d1/ba
242 242 moving d1/d11/a1 to d3/d1/d11/a1
243 243 moving d2/b to d3/d2/b
244 244 $ hg status -C
245 245 A d3/d1/a
246 246 d1/a
247 247 A d3/d1/b
248 248 d1/b
249 249 A d3/d1/ba
250 250 d1/ba
251 251 A d3/d1/d11/a1
252 252 d1/d11/a1
253 253 A d3/d2/b
254 254 d2/b
255 255 R d1/a
256 256 R d1/b
257 257 R d1/ba
258 258 R d1/d11/a1
259 259 R d2/b
260 260 $ hg update -C
261 261 5 files updated, 0 files merged, 0 files removed, 0 files unresolved
262 262 $ rm -rf d3
263 263
264 264 move everything under directory d1 to existing directory d2, do not
265 265 overwrite existing files (d2/b)
266 266
267 267 $ hg rename d1/* d2
268 268 d2/b: not overwriting - file already committed
269 269 (hg rename --force to replace the file by recording a rename)
270 270 moving d1/d11/a1 to d2/d11/a1
271 271 $ hg status -C
272 272 A d2/a
273 273 d1/a
274 274 A d2/ba
275 275 d1/ba
276 276 A d2/d11/a1
277 277 d1/d11/a1
278 278 R d1/a
279 279 R d1/ba
280 280 R d1/d11/a1
281 281 $ diff -u d1/b d2/b
282 282 --- d1/b * (glob)
283 283 +++ d2/b * (glob)
284 284 @@ * (glob)
285 285 -d1/b
286 286 +d2/b
287 287 [1]
288 288 $ hg update -C
289 289 3 files updated, 0 files merged, 0 files removed, 0 files unresolved
290 290 $ rm d2/a d2/ba d2/d11/a1
291 291
292 292 attempt to move one file into a non-existent directory
293 293
294 294 $ hg rename d1/a dx/
295 295 abort: destination dx/ is not a directory
296 296 [255]
297 297 $ hg status -C
298 298 $ hg update -C
299 299 0 files updated, 0 files merged, 0 files removed, 0 files unresolved
300 300
301 301 attempt to move potentially more than one file into a non-existent directory
302 302
303 303 $ hg rename 'glob:d1/**' dx
304 304 abort: with multiple sources, destination must be an existing directory
305 305 [255]
306 306
307 307 move every file under d1 to d2/d21
308 308
309 309 $ mkdir d2/d21
310 310 $ hg rename 'glob:d1/**' d2/d21
311 311 moving d1/a to d2/d21/a
312 312 moving d1/b to d2/d21/b
313 313 moving d1/ba to d2/d21/ba
314 314 moving d1/d11/a1 to d2/d21/a1
315 315 $ hg status -C
316 316 A d2/d21/a
317 317 d1/a
318 318 A d2/d21/a1
319 319 d1/d11/a1
320 320 A d2/d21/b
321 321 d1/b
322 322 A d2/d21/ba
323 323 d1/ba
324 324 R d1/a
325 325 R d1/b
326 326 R d1/ba
327 327 R d1/d11/a1
328 328 $ hg update -C
329 329 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
330 330 $ rm -rf d2/d21
331 331
332 332 move --after some files under d1 to d2/d21
333 333
334 334 $ mkdir d2/d21
335 335 $ mv d1/a d1/d11/a1 d2/d21
336 336 $ hg rename --after 'glob:d1/**' d2/d21
337 337 moving d1/a to d2/d21/a
338 338 d1/b: not recording move - d2/d21/b does not exist
339 339 d1/ba: not recording move - d2/d21/ba does not exist
340 340 moving d1/d11/a1 to d2/d21/a1
341 341 $ hg status -C
342 342 A d2/d21/a
343 343 d1/a
344 344 A d2/d21/a1
345 345 d1/d11/a1
346 346 R d1/a
347 347 R d1/d11/a1
348 348 $ hg update -C
349 349 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
350 350 $ rm -rf d2/d21
351 351
352 352 move every file under d1 starting with an 'a' to d2/d21 (regexp)
353 353
354 354 $ mkdir d2/d21
355 355 $ hg rename 're:d1/([^a][^/]*/)*a.*' d2/d21
356 356 moving d1/a to d2/d21/a
357 357 moving d1/d11/a1 to d2/d21/a1
358 358 $ hg status -C
359 359 A d2/d21/a
360 360 d1/a
361 361 A d2/d21/a1
362 362 d1/d11/a1
363 363 R d1/a
364 364 R d1/d11/a1
365 365 $ hg update -C
366 366 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
367 367 $ rm -rf d2/d21
368 368
369 369 attempt to overwrite an existing file
370 370
371 371 $ echo "ca" > d1/ca
372 372 $ hg rename d1/ba d1/ca
373 373 d1/ca: not overwriting - file exists
374 374 (hg rename --after to record the rename)
375 375 $ hg status -C
376 376 ? d1/ca
377 377 $ hg update -C
378 378 0 files updated, 0 files merged, 0 files removed, 0 files unresolved
379 379
380 380 forced overwrite of an existing file
381 381
382 382 $ echo "ca" > d1/ca
383 383 $ hg rename --force d1/ba d1/ca
384 384 $ hg status -C
385 385 A d1/ca
386 386 d1/ba
387 387 R d1/ba
388 388 $ hg update -C
389 389 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
390 390 $ rm d1/ca
391 391
392 392 attempt to overwrite an existing broken symlink
393 393
394 394 #if symlink
395 395 $ ln -s ba d1/ca
396 396 $ hg rename --traceback d1/ba d1/ca
397 397 d1/ca: not overwriting - file exists
398 398 (hg rename --after to record the rename)
399 399 $ hg status -C
400 400 ? d1/ca
401 401 $ hg update -C
402 402 0 files updated, 0 files merged, 0 files removed, 0 files unresolved
403 403 $ rm d1/ca
404 404
405 405 replace a symlink with a file
406 406
407 407 $ ln -s ba d1/ca
408 408 $ hg rename --force d1/ba d1/ca
409 409 $ hg status -C
410 410 A d1/ca
411 411 d1/ba
412 412 R d1/ba
413 413 $ hg update -C
414 414 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
415 415 $ rm d1/ca
416 416 #endif
417 417
418 418 do not copy more than one source file to the same destination file
419 419
420 420 $ mkdir d3
421 421 $ hg rename d1/* d2/* d3
422 422 moving d1/d11/a1 to d3/d11/a1
423 423 d3/b: not overwriting - d2/b collides with d1/b
424 424 $ hg status -C
425 425 A d3/a
426 426 d1/a
427 427 A d3/b
428 428 d1/b
429 429 A d3/ba
430 430 d1/ba
431 431 A d3/d11/a1
432 432 d1/d11/a1
433 433 R d1/a
434 434 R d1/b
435 435 R d1/ba
436 436 R d1/d11/a1
437 437 $ hg update -C
438 438 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
439 439 $ rm -rf d3
440 440
441 441 move a whole subtree with "hg rename ."
442 442
443 443 $ mkdir d3
444 444 $ (cd d1; hg rename . ../d3)
445 445 moving a to ../d3/d1/a
446 446 moving b to ../d3/d1/b
447 447 moving ba to ../d3/d1/ba
448 448 moving d11/a1 to ../d3/d1/d11/a1
449 449 $ hg status -C
450 450 A d3/d1/a
451 451 d1/a
452 452 A d3/d1/b
453 453 d1/b
454 454 A d3/d1/ba
455 455 d1/ba
456 456 A d3/d1/d11/a1
457 457 d1/d11/a1
458 458 R d1/a
459 459 R d1/b
460 460 R d1/ba
461 461 R d1/d11/a1
462 462 $ hg update -C
463 463 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
464 464 $ rm -rf d3
465 465
466 466 move a whole subtree with "hg rename --after ."
467 467
468 468 $ mkdir d3
469 469 $ mv d1/* d3
470 470 $ (cd d1; hg rename --after . ../d3)
471 471 moving a to ../d3/a
472 472 moving b to ../d3/b
473 473 moving ba to ../d3/ba
474 474 moving d11/a1 to ../d3/d11/a1
475 475 $ hg status -C
476 476 A d3/a
477 477 d1/a
478 478 A d3/b
479 479 d1/b
480 480 A d3/ba
481 481 d1/ba
482 482 A d3/d11/a1
483 483 d1/d11/a1
484 484 R d1/a
485 485 R d1/b
486 486 R d1/ba
487 487 R d1/d11/a1
488 488 $ hg update -C
489 489 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
490 490 $ rm -rf d3
491 491
492 492 move the parent tree with "hg rename .."
493 493
494 494 $ (cd d1/d11; hg rename .. ../../d3)
495 495 moving ../a to ../../d3/a
496 496 moving ../b to ../../d3/b
497 497 moving ../ba to ../../d3/ba
498 498 moving a1 to ../../d3/d11/a1
499 499 $ hg status -C
500 500 A d3/a
501 501 d1/a
502 502 A d3/b
503 503 d1/b
504 504 A d3/ba
505 505 d1/ba
506 506 A d3/d11/a1
507 507 d1/d11/a1
508 508 R d1/a
509 509 R d1/b
510 510 R d1/ba
511 511 R d1/d11/a1
512 512 $ hg update -C
513 513 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
514 514 $ rm -rf d3
515 515
516 516 skip removed files
517 517
518 518 $ hg remove d1/b
519 519 $ hg rename d1 d3
520 520 moving d1/a to d3/a
521 521 moving d1/ba to d3/ba
522 522 moving d1/d11/a1 to d3/d11/a1
523 523 $ hg status -C
524 524 A d3/a
525 525 d1/a
526 526 A d3/ba
527 527 d1/ba
528 528 A d3/d11/a1
529 529 d1/d11/a1
530 530 R d1/a
531 531 R d1/b
532 532 R d1/ba
533 533 R d1/d11/a1
534 534 $ hg update -C
535 535 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
536 536 $ rm -rf d3
537 537
538 538 transitive rename
539 539
540 540 $ hg rename d1/b d1/bb
541 541 $ hg rename d1/bb d1/bc
542 542 $ hg status -C
543 543 A d1/bc
544 544 d1/b
545 545 R d1/b
546 546 $ hg update -C
547 547 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
548 548 $ rm d1/bc
549 549
550 550 transitive rename --after
551 551
552 552 $ hg rename d1/b d1/bb
553 553 $ mv d1/bb d1/bc
554 554 $ hg rename --after d1/bb d1/bc
555 555 $ hg status -C
556 556 A d1/bc
557 557 d1/b
558 558 R d1/b
559 559 $ hg update -C
560 560 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
561 561 $ rm d1/bc
562 562
563 563 $ echo "# idempotent renames (d1/b -> d1/bb followed by d1/bb -> d1/b)"
564 564 # idempotent renames (d1/b -> d1/bb followed by d1/bb -> d1/b)
565 565 $ hg rename d1/b d1/bb
566 566 $ echo "some stuff added to d1/bb" >> d1/bb
567 567 $ hg rename d1/bb d1/b
568 568 $ hg status -C
569 569 M d1/b
570 570 $ hg update -C
571 571 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
572 572
573 573 overwriting with renames (issue1959)
574 574
575 575 $ hg rename d1/a d1/c
576 576 $ hg rename d1/b d1/a
577 577 $ hg status -C
578 578 M d1/a
579 579 d1/b
580 580 A d1/c
581 581 d1/a
582 582 R d1/b
583 583 $ hg diff --git
584 584 diff --git a/d1/a b/d1/a
585 585 --- a/d1/a
586 586 +++ b/d1/a
587 587 @@ -1,1 +1,1 @@
588 588 -d1/a
589 589 +d1/b
590 590 diff --git a/d1/b b/d1/b
591 591 deleted file mode 100644
592 592 --- a/d1/b
593 593 +++ /dev/null
594 594 @@ -1,1 +0,0 @@
595 595 -d1/b
596 596 diff --git a/d1/a b/d1/c
597 597 copy from d1/a
598 598 copy to d1/c
599 599 $ hg update -C
600 600 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
601 601 $ rm d1/c # The file was marked as added, so 'hg update' action was 'forget'
602 602
603 603 check illegal path components
604 604
605 605 $ hg rename d1/d11/a1 .hg/foo
606 606 abort: path contains illegal component: .hg/foo
607 607 [255]
608 608 $ hg status -C
609 609 $ hg rename d1/d11/a1 ../foo
610 610 abort: ../foo not under root '$TESTTMP'
611 611 [255]
612 612 $ hg status -C
613 613
614 614 $ mv d1/d11/a1 .hg/foo
615 615 $ hg rename --after d1/d11/a1 .hg/foo
616 616 abort: path contains illegal component: .hg/foo
617 617 [255]
618 618 $ hg status -C
619 619 ! d1/d11/a1
620 620 $ hg update -C
621 621 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
622 622 $ rm .hg/foo
623 623
624 624 $ hg rename d1/d11/a1 .hg
625 625 abort: path contains illegal component: .hg/a1
626 626 [255]
627 627 $ hg --config extensions.largefiles= rename d1/d11/a1 .hg
628 628 The fsmonitor extension is incompatible with the largefiles extension and has been disabled. (fsmonitor !)
629 629 abort: path contains illegal component: .hg/a1
630 630 [255]
631 631 $ hg status -C
632 632 $ hg rename d1/d11/a1 ..
633 633 abort: ../a1 not under root '$TESTTMP'
634 634 [255]
635 635 $ hg --config extensions.largefiles= rename d1/d11/a1 ..
636 636 The fsmonitor extension is incompatible with the largefiles extension and has been disabled. (fsmonitor !)
637 637 abort: ../a1 not under root '$TESTTMP'
638 638 [255]
639 639 $ hg status -C
640 640
641 641 $ mv d1/d11/a1 .hg
642 642 $ hg rename --after d1/d11/a1 .hg
643 643 abort: path contains illegal component: .hg/a1
644 644 [255]
645 645 $ hg status -C
646 646 ! d1/d11/a1
647 647 $ hg update -C
648 648 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
649 649 $ rm .hg/a1
650 650
651 651 $ (cd d1/d11; hg rename ../../d2/b ../../.hg/foo)
652 652 abort: path contains illegal component: .hg/foo
653 653 [255]
654 654 $ hg status -C
655 655 $ (cd d1/d11; hg rename ../../d2/b ../../../foo)
656 656 abort: ../../../foo not under root '$TESTTMP'
657 657 [255]
658 658 $ hg status -C
659 659
660 check that stat information such as mtime is preserved on rename - it's unclear
661 whether the `touch` and `stat` commands are portable, so we mimic them using
662 python. Not all platforms support precision of even one-second granularity, so
663 we allow a rather generous fudge factor here; 1234567890 is 2009, and the
664 primary thing we care about is that it's not the machine's current time;
665 hopefully it's really unlikely for a machine to have such a broken clock that
666 this test fails. :)
667
668 $ mkdir mtime
669 Create the file (as empty), then update its mtime and atime to be 1234567890.
670 >>> import os
671 >>> filename = "mtime/f"
672 >>> mtime = 1234567890
673 >>> open(filename, "w").close()
674 >>> os.utime(filename, (mtime, mtime))
675 $ hg ci -qAm 'add mtime dir'
676 "hg cp" does not preserve the mtime, so it should be newer than the 2009
677 timestamp.
678 $ hg cp -q mtime mtime_cp
679 >>> from __future__ import print_function
680 >>> import os
681 >>> filename = "mtime_cp/f"
682 >>> print(os.stat(filename).st_mtime < 1234567999)
683 False
684 "hg mv" preserves the mtime, so it should be ~equal to the 2009 timestamp
685 (modulo some fudge factor due to not every system supporting 1s-level
686 precision).
687 $ hg mv -q mtime mtime_mv
688 >>> from __future__ import print_function
689 >>> import os
690 >>> filename = "mtime_mv/f"
691 >>> print(os.stat(filename).st_mtime < 1234567999)
692 True
General Comments 0
You need to be logged in to leave comments. Login now