##// END OF EJS Templates
largefiles: replace invocation of os.path module by vfs in overrides.py...
liscju -
r28715:a7f7b7ac default
parent child Browse files
Show More
@@ -1,1404 +1,1404 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
11 11 import os
12 12 import copy
13 13
14 14 from mercurial import hg, util, cmdutil, scmutil, match as match_, \
15 15 archival, pathutil, registrar, revset, error
16 16 from mercurial.i18n import _
17 17
18 18 import lfutil
19 19 import lfcommands
20 20 import basestore
21 21
22 22 # -- Utility functions: commonly/repeatedly needed functionality ---------------
23 23
24 24 def composelargefilematcher(match, manifest):
25 25 '''create a matcher that matches only the largefiles in the original
26 26 matcher'''
27 27 m = copy.copy(match)
28 28 lfile = lambda f: lfutil.standin(f) in manifest
29 29 m._files = filter(lfile, m._files)
30 30 m._fileroots = set(m._files)
31 31 m._always = False
32 32 origmatchfn = m.matchfn
33 33 m.matchfn = lambda f: lfile(f) and origmatchfn(f)
34 34 return m
35 35
36 36 def composenormalfilematcher(match, manifest, exclude=None):
37 37 excluded = set()
38 38 if exclude is not None:
39 39 excluded.update(exclude)
40 40
41 41 m = copy.copy(match)
42 42 notlfile = lambda f: not (lfutil.isstandin(f) or lfutil.standin(f) in
43 43 manifest or f in excluded)
44 44 m._files = filter(notlfile, m._files)
45 45 m._fileroots = set(m._files)
46 46 m._always = False
47 47 origmatchfn = m.matchfn
48 48 m.matchfn = lambda f: notlfile(f) and origmatchfn(f)
49 49 return m
50 50
51 51 def installnormalfilesmatchfn(manifest):
52 52 '''installmatchfn with a matchfn that ignores all largefiles'''
53 53 def overridematch(ctx, pats=(), opts=None, globbed=False,
54 54 default='relpath', badfn=None):
55 55 if opts is None:
56 56 opts = {}
57 57 match = oldmatch(ctx, pats, opts, globbed, default, badfn=badfn)
58 58 return composenormalfilematcher(match, manifest)
59 59 oldmatch = installmatchfn(overridematch)
60 60
61 61 def installmatchfn(f):
62 62 '''monkey patch the scmutil module with a custom match function.
63 63 Warning: it is monkey patching the _module_ on runtime! Not thread safe!'''
64 64 oldmatch = scmutil.match
65 65 setattr(f, 'oldmatch', oldmatch)
66 66 scmutil.match = f
67 67 return oldmatch
68 68
69 69 def restorematchfn():
70 70 '''restores scmutil.match to what it was before installmatchfn
71 71 was called. no-op if scmutil.match is its original function.
72 72
73 73 Note that n calls to installmatchfn will require n calls to
74 74 restore the original matchfn.'''
75 75 scmutil.match = getattr(scmutil.match, 'oldmatch')
76 76
77 77 def installmatchandpatsfn(f):
78 78 oldmatchandpats = scmutil.matchandpats
79 79 setattr(f, 'oldmatchandpats', oldmatchandpats)
80 80 scmutil.matchandpats = f
81 81 return oldmatchandpats
82 82
83 83 def restorematchandpatsfn():
84 84 '''restores scmutil.matchandpats to what it was before
85 85 installmatchandpatsfn was called. No-op if scmutil.matchandpats
86 86 is its original function.
87 87
88 88 Note that n calls to installmatchandpatsfn will require n calls
89 89 to restore the original matchfn.'''
90 90 scmutil.matchandpats = getattr(scmutil.matchandpats, 'oldmatchandpats',
91 91 scmutil.matchandpats)
92 92
93 93 def addlargefiles(ui, repo, isaddremove, matcher, **opts):
94 94 large = opts.get('large')
95 95 lfsize = lfutil.getminsize(
96 96 ui, lfutil.islfilesrepo(repo), opts.get('lfsize'))
97 97
98 98 lfmatcher = None
99 99 if lfutil.islfilesrepo(repo):
100 100 lfpats = ui.configlist(lfutil.longname, 'patterns', default=[])
101 101 if lfpats:
102 102 lfmatcher = match_.match(repo.root, '', list(lfpats))
103 103
104 104 lfnames = []
105 105 m = matcher
106 106
107 107 wctx = repo[None]
108 108 for f in repo.walk(match_.badmatch(m, lambda x, y: None)):
109 109 exact = m.exact(f)
110 110 lfile = lfutil.standin(f) in wctx
111 111 nfile = f in wctx
112 112 exists = lfile or nfile
113 113
114 114 # addremove in core gets fancy with the name, add doesn't
115 115 if isaddremove:
116 116 name = m.uipath(f)
117 117 else:
118 118 name = m.rel(f)
119 119
120 120 # Don't warn the user when they attempt to add a normal tracked file.
121 121 # The normal add code will do that for us.
122 122 if exact and exists:
123 123 if lfile:
124 124 ui.warn(_('%s already a largefile\n') % name)
125 125 continue
126 126
127 127 if (exact or not exists) and not lfutil.isstandin(f):
128 128 # In case the file was removed previously, but not committed
129 129 # (issue3507)
130 130 if not repo.wvfs.exists(f):
131 131 continue
132 132
133 133 abovemin = (lfsize and
134 134 repo.wvfs.lstat(f).st_size >= lfsize * 1024 * 1024)
135 135 if large or abovemin or (lfmatcher and lfmatcher(f)):
136 136 lfnames.append(f)
137 137 if ui.verbose or not exact:
138 138 ui.status(_('adding %s as a largefile\n') % name)
139 139
140 140 bad = []
141 141
142 142 # Need to lock, otherwise there could be a race condition between
143 143 # when standins are created and added to the repo.
144 144 with repo.wlock():
145 145 if not opts.get('dry_run'):
146 146 standins = []
147 147 lfdirstate = lfutil.openlfdirstate(ui, repo)
148 148 for f in lfnames:
149 149 standinname = lfutil.standin(f)
150 150 lfutil.writestandin(repo, standinname, hash='',
151 151 executable=lfutil.getexecutable(repo.wjoin(f)))
152 152 standins.append(standinname)
153 153 if lfdirstate[f] == 'r':
154 154 lfdirstate.normallookup(f)
155 155 else:
156 156 lfdirstate.add(f)
157 157 lfdirstate.write()
158 158 bad += [lfutil.splitstandin(f)
159 159 for f in repo[None].add(standins)
160 160 if f in m.files()]
161 161
162 162 added = [f for f in lfnames if f not in bad]
163 163 return added, bad
164 164
165 165 def removelargefiles(ui, repo, isaddremove, matcher, **opts):
166 166 after = opts.get('after')
167 167 m = composelargefilematcher(matcher, repo[None].manifest())
168 168 try:
169 169 repo.lfstatus = True
170 170 s = repo.status(match=m, clean=not isaddremove)
171 171 finally:
172 172 repo.lfstatus = False
173 173 manifest = repo[None].manifest()
174 174 modified, added, deleted, clean = [[f for f in list
175 175 if lfutil.standin(f) in manifest]
176 176 for list in (s.modified, s.added,
177 177 s.deleted, s.clean)]
178 178
179 179 def warn(files, msg):
180 180 for f in files:
181 181 ui.warn(msg % m.rel(f))
182 182 return int(len(files) > 0)
183 183
184 184 result = 0
185 185
186 186 if after:
187 187 remove = deleted
188 188 result = warn(modified + added + clean,
189 189 _('not removing %s: file still exists\n'))
190 190 else:
191 191 remove = deleted + clean
192 192 result = warn(modified, _('not removing %s: file is modified (use -f'
193 193 ' to force removal)\n'))
194 194 result = warn(added, _('not removing %s: file has been marked for add'
195 195 ' (use forget to undo)\n')) or result
196 196
197 197 # Need to lock because standin files are deleted then removed from the
198 198 # repository and we could race in-between.
199 199 with repo.wlock():
200 200 lfdirstate = lfutil.openlfdirstate(ui, repo)
201 201 for f in sorted(remove):
202 202 if ui.verbose or not m.exact(f):
203 203 # addremove in core gets fancy with the name, remove doesn't
204 204 if isaddremove:
205 205 name = m.uipath(f)
206 206 else:
207 207 name = m.rel(f)
208 208 ui.status(_('removing %s\n') % name)
209 209
210 210 if not opts.get('dry_run'):
211 211 if not after:
212 212 util.unlinkpath(repo.wjoin(f), ignoremissing=True)
213 213
214 214 if opts.get('dry_run'):
215 215 return result
216 216
217 217 remove = [lfutil.standin(f) for f in remove]
218 218 # If this is being called by addremove, let the original addremove
219 219 # function handle this.
220 220 if not isaddremove:
221 221 for f in remove:
222 222 util.unlinkpath(repo.wjoin(f), ignoremissing=True)
223 223 repo[None].forget(remove)
224 224
225 225 for f in remove:
226 226 lfutil.synclfdirstate(repo, lfdirstate, lfutil.splitstandin(f),
227 227 False)
228 228
229 229 lfdirstate.write()
230 230
231 231 return result
232 232
233 233 # For overriding mercurial.hgweb.webcommands so that largefiles will
234 234 # appear at their right place in the manifests.
235 235 def decodepath(orig, path):
236 236 return lfutil.splitstandin(path) or path
237 237
238 238 # -- Wrappers: modify existing commands --------------------------------
239 239
240 240 def overrideadd(orig, ui, repo, *pats, **opts):
241 241 if opts.get('normal') and opts.get('large'):
242 242 raise error.Abort(_('--normal cannot be used with --large'))
243 243 return orig(ui, repo, *pats, **opts)
244 244
245 245 def cmdutiladd(orig, ui, repo, matcher, prefix, explicitonly, **opts):
246 246 # The --normal flag short circuits this override
247 247 if opts.get('normal'):
248 248 return orig(ui, repo, matcher, prefix, explicitonly, **opts)
249 249
250 250 ladded, lbad = addlargefiles(ui, repo, False, matcher, **opts)
251 251 normalmatcher = composenormalfilematcher(matcher, repo[None].manifest(),
252 252 ladded)
253 253 bad = orig(ui, repo, normalmatcher, prefix, explicitonly, **opts)
254 254
255 255 bad.extend(f for f in lbad)
256 256 return bad
257 257
258 258 def cmdutilremove(orig, ui, repo, matcher, prefix, after, force, subrepos):
259 259 normalmatcher = composenormalfilematcher(matcher, repo[None].manifest())
260 260 result = orig(ui, repo, normalmatcher, prefix, after, force, subrepos)
261 261 return removelargefiles(ui, repo, False, matcher, after=after,
262 262 force=force) or result
263 263
264 264 def overridestatusfn(orig, repo, rev2, **opts):
265 265 try:
266 266 repo._repo.lfstatus = True
267 267 return orig(repo, rev2, **opts)
268 268 finally:
269 269 repo._repo.lfstatus = False
270 270
271 271 def overridestatus(orig, ui, repo, *pats, **opts):
272 272 try:
273 273 repo.lfstatus = True
274 274 return orig(ui, repo, *pats, **opts)
275 275 finally:
276 276 repo.lfstatus = False
277 277
278 278 def overridedirty(orig, repo, ignoreupdate=False):
279 279 try:
280 280 repo._repo.lfstatus = True
281 281 return orig(repo, ignoreupdate)
282 282 finally:
283 283 repo._repo.lfstatus = False
284 284
285 285 def overridelog(orig, ui, repo, *pats, **opts):
286 286 def overridematchandpats(ctx, pats=(), opts=None, globbed=False,
287 287 default='relpath', badfn=None):
288 288 """Matcher that merges root directory with .hglf, suitable for log.
289 289 It is still possible to match .hglf directly.
290 290 For any listed files run log on the standin too.
291 291 matchfn tries both the given filename and with .hglf stripped.
292 292 """
293 293 if opts is None:
294 294 opts = {}
295 295 matchandpats = oldmatchandpats(ctx, pats, opts, globbed, default,
296 296 badfn=badfn)
297 297 m, p = copy.copy(matchandpats)
298 298
299 299 if m.always():
300 300 # We want to match everything anyway, so there's no benefit trying
301 301 # to add standins.
302 302 return matchandpats
303 303
304 304 pats = set(p)
305 305
306 306 def fixpats(pat, tostandin=lfutil.standin):
307 307 if pat.startswith('set:'):
308 308 return pat
309 309
310 310 kindpat = match_._patsplit(pat, None)
311 311
312 312 if kindpat[0] is not None:
313 313 return kindpat[0] + ':' + tostandin(kindpat[1])
314 314 return tostandin(kindpat[1])
315 315
316 316 if m._cwd:
317 317 hglf = lfutil.shortname
318 318 back = util.pconvert(m.rel(hglf)[:-len(hglf)])
319 319
320 320 def tostandin(f):
321 321 # The file may already be a standin, so truncate the back
322 322 # prefix and test before mangling it. This avoids turning
323 323 # 'glob:../.hglf/foo*' into 'glob:../.hglf/../.hglf/foo*'.
324 324 if f.startswith(back) and lfutil.splitstandin(f[len(back):]):
325 325 return f
326 326
327 327 # An absolute path is from outside the repo, so truncate the
328 328 # path to the root before building the standin. Otherwise cwd
329 329 # is somewhere in the repo, relative to root, and needs to be
330 330 # prepended before building the standin.
331 331 if os.path.isabs(m._cwd):
332 332 f = f[len(back):]
333 333 else:
334 334 f = m._cwd + '/' + f
335 335 return back + lfutil.standin(f)
336 336
337 337 pats.update(fixpats(f, tostandin) for f in p)
338 338 else:
339 339 def tostandin(f):
340 340 if lfutil.splitstandin(f):
341 341 return f
342 342 return lfutil.standin(f)
343 343 pats.update(fixpats(f, tostandin) for f in p)
344 344
345 345 for i in range(0, len(m._files)):
346 346 # Don't add '.hglf' to m.files, since that is already covered by '.'
347 347 if m._files[i] == '.':
348 348 continue
349 349 standin = lfutil.standin(m._files[i])
350 350 # If the "standin" is a directory, append instead of replace to
351 351 # support naming a directory on the command line with only
352 352 # largefiles. The original directory is kept to support normal
353 353 # files.
354 354 if standin in repo[ctx.node()]:
355 355 m._files[i] = standin
356 356 elif m._files[i] not in repo[ctx.node()] \
357 357 and repo.wvfs.isdir(standin):
358 358 m._files.append(standin)
359 359
360 360 m._fileroots = set(m._files)
361 361 m._always = False
362 362 origmatchfn = m.matchfn
363 363 def lfmatchfn(f):
364 364 lf = lfutil.splitstandin(f)
365 365 if lf is not None and origmatchfn(lf):
366 366 return True
367 367 r = origmatchfn(f)
368 368 return r
369 369 m.matchfn = lfmatchfn
370 370
371 371 ui.debug('updated patterns: %s\n' % sorted(pats))
372 372 return m, pats
373 373
374 374 # For hg log --patch, the match object is used in two different senses:
375 375 # (1) to determine what revisions should be printed out, and
376 376 # (2) to determine what files to print out diffs for.
377 377 # The magic matchandpats override should be used for case (1) but not for
378 378 # case (2).
379 379 def overridemakelogfilematcher(repo, pats, opts, badfn=None):
380 380 wctx = repo[None]
381 381 match, pats = oldmatchandpats(wctx, pats, opts, badfn=badfn)
382 382 return lambda rev: match
383 383
384 384 oldmatchandpats = installmatchandpatsfn(overridematchandpats)
385 385 oldmakelogfilematcher = cmdutil._makenofollowlogfilematcher
386 386 setattr(cmdutil, '_makenofollowlogfilematcher', overridemakelogfilematcher)
387 387
388 388 try:
389 389 return orig(ui, repo, *pats, **opts)
390 390 finally:
391 391 restorematchandpatsfn()
392 392 setattr(cmdutil, '_makenofollowlogfilematcher', oldmakelogfilematcher)
393 393
394 394 def overrideverify(orig, ui, repo, *pats, **opts):
395 395 large = opts.pop('large', False)
396 396 all = opts.pop('lfa', False)
397 397 contents = opts.pop('lfc', False)
398 398
399 399 result = orig(ui, repo, *pats, **opts)
400 400 if large or all or contents:
401 401 result = result or lfcommands.verifylfiles(ui, repo, all, contents)
402 402 return result
403 403
404 404 def overridedebugstate(orig, ui, repo, *pats, **opts):
405 405 large = opts.pop('large', False)
406 406 if large:
407 407 class fakerepo(object):
408 408 dirstate = lfutil.openlfdirstate(ui, repo)
409 409 orig(ui, fakerepo, *pats, **opts)
410 410 else:
411 411 orig(ui, repo, *pats, **opts)
412 412
413 413 # Before starting the manifest merge, merge.updates will call
414 414 # _checkunknownfile to check if there are any files in the merged-in
415 415 # changeset that collide with unknown files in the working copy.
416 416 #
417 417 # The largefiles are seen as unknown, so this prevents us from merging
418 418 # in a file 'foo' if we already have a largefile with the same name.
419 419 #
420 420 # The overridden function filters the unknown files by removing any
421 421 # largefiles. This makes the merge proceed and we can then handle this
422 422 # case further in the overridden calculateupdates function below.
423 423 def overridecheckunknownfile(origfn, repo, wctx, mctx, f, f2=None):
424 424 if lfutil.standin(repo.dirstate.normalize(f)) in wctx:
425 425 return False
426 426 return origfn(repo, wctx, mctx, f, f2)
427 427
428 428 # The manifest merge handles conflicts on the manifest level. We want
429 429 # to handle changes in largefile-ness of files at this level too.
430 430 #
431 431 # The strategy is to run the original calculateupdates and then process
432 432 # the action list it outputs. There are two cases we need to deal with:
433 433 #
434 434 # 1. Normal file in p1, largefile in p2. Here the largefile is
435 435 # detected via its standin file, which will enter the working copy
436 436 # with a "get" action. It is not "merge" since the standin is all
437 437 # Mercurial is concerned with at this level -- the link to the
438 438 # existing normal file is not relevant here.
439 439 #
440 440 # 2. Largefile in p1, normal file in p2. Here we get a "merge" action
441 441 # since the largefile will be present in the working copy and
442 442 # different from the normal file in p2. Mercurial therefore
443 443 # triggers a merge action.
444 444 #
445 445 # In both cases, we prompt the user and emit new actions to either
446 446 # remove the standin (if the normal file was kept) or to remove the
447 447 # normal file and get the standin (if the largefile was kept). The
448 448 # default prompt answer is to use the largefile version since it was
449 449 # presumably changed on purpose.
450 450 #
451 451 # Finally, the merge.applyupdates function will then take care of
452 452 # writing the files into the working copy and lfcommands.updatelfiles
453 453 # will update the largefiles.
454 454 def overridecalculateupdates(origfn, repo, p1, p2, pas, branchmerge, force,
455 455 acceptremote, *args, **kwargs):
456 456 overwrite = force and not branchmerge
457 457 actions, diverge, renamedelete = origfn(
458 458 repo, p1, p2, pas, branchmerge, force, acceptremote, *args, **kwargs)
459 459
460 460 if overwrite:
461 461 return actions, diverge, renamedelete
462 462
463 463 # Convert to dictionary with filename as key and action as value.
464 464 lfiles = set()
465 465 for f in actions:
466 466 splitstandin = lfutil.splitstandin(f)
467 467 if splitstandin in p1:
468 468 lfiles.add(splitstandin)
469 469 elif lfutil.standin(f) in p1:
470 470 lfiles.add(f)
471 471
472 472 for lfile in sorted(lfiles):
473 473 standin = lfutil.standin(lfile)
474 474 (lm, largs, lmsg) = actions.get(lfile, (None, None, None))
475 475 (sm, sargs, smsg) = actions.get(standin, (None, None, None))
476 476 if sm in ('g', 'dc') and lm != 'r':
477 477 if sm == 'dc':
478 478 f1, f2, fa, move, anc = sargs
479 479 sargs = (p2[f2].flags(), False)
480 480 # Case 1: normal file in the working copy, largefile in
481 481 # the second parent
482 482 usermsg = _('remote turned local normal file %s into a largefile\n'
483 483 'use (l)argefile or keep (n)ormal file?'
484 484 '$$ &Largefile $$ &Normal file') % lfile
485 485 if repo.ui.promptchoice(usermsg, 0) == 0: # pick remote largefile
486 486 actions[lfile] = ('r', None, 'replaced by standin')
487 487 actions[standin] = ('g', sargs, 'replaces standin')
488 488 else: # keep local normal file
489 489 actions[lfile] = ('k', None, 'replaces standin')
490 490 if branchmerge:
491 491 actions[standin] = ('k', None, 'replaced by non-standin')
492 492 else:
493 493 actions[standin] = ('r', None, 'replaced by non-standin')
494 494 elif lm in ('g', 'dc') and sm != 'r':
495 495 if lm == 'dc':
496 496 f1, f2, fa, move, anc = largs
497 497 largs = (p2[f2].flags(), False)
498 498 # Case 2: largefile in the working copy, normal file in
499 499 # the second parent
500 500 usermsg = _('remote turned local largefile %s into a normal file\n'
501 501 'keep (l)argefile or use (n)ormal file?'
502 502 '$$ &Largefile $$ &Normal file') % lfile
503 503 if repo.ui.promptchoice(usermsg, 0) == 0: # keep local largefile
504 504 if branchmerge:
505 505 # largefile can be restored from standin safely
506 506 actions[lfile] = ('k', None, 'replaced by standin')
507 507 actions[standin] = ('k', None, 'replaces standin')
508 508 else:
509 509 # "lfile" should be marked as "removed" without
510 510 # removal of itself
511 511 actions[lfile] = ('lfmr', None,
512 512 'forget non-standin largefile')
513 513
514 514 # linear-merge should treat this largefile as 're-added'
515 515 actions[standin] = ('a', None, 'keep standin')
516 516 else: # pick remote normal file
517 517 actions[lfile] = ('g', largs, 'replaces standin')
518 518 actions[standin] = ('r', None, 'replaced by non-standin')
519 519
520 520 return actions, diverge, renamedelete
521 521
522 522 def mergerecordupdates(orig, repo, actions, branchmerge):
523 523 if 'lfmr' in actions:
524 524 lfdirstate = lfutil.openlfdirstate(repo.ui, repo)
525 525 for lfile, args, msg in actions['lfmr']:
526 526 # this should be executed before 'orig', to execute 'remove'
527 527 # before all other actions
528 528 repo.dirstate.remove(lfile)
529 529 # make sure lfile doesn't get synclfdirstate'd as normal
530 530 lfdirstate.add(lfile)
531 531 lfdirstate.write()
532 532
533 533 return orig(repo, actions, branchmerge)
534 534
535 535
536 536 # Override filemerge to prompt the user about how they wish to merge
537 537 # largefiles. This will handle identical edits without prompting the user.
538 538 def overridefilemerge(origfn, premerge, repo, mynode, orig, fcd, fco, fca,
539 539 labels=None):
540 540 if not lfutil.isstandin(orig) or fcd.isabsent() or fco.isabsent():
541 541 return origfn(premerge, repo, mynode, orig, fcd, fco, fca,
542 542 labels=labels)
543 543
544 544 ahash = fca.data().strip().lower()
545 545 dhash = fcd.data().strip().lower()
546 546 ohash = fco.data().strip().lower()
547 547 if (ohash != ahash and
548 548 ohash != dhash and
549 549 (dhash == ahash or
550 550 repo.ui.promptchoice(
551 551 _('largefile %s has a merge conflict\nancestor was %s\n'
552 552 'keep (l)ocal %s or\ntake (o)ther %s?'
553 553 '$$ &Local $$ &Other') %
554 554 (lfutil.splitstandin(orig), ahash, dhash, ohash),
555 555 0) == 1)):
556 556 repo.wwrite(fcd.path(), fco.data(), fco.flags())
557 557 return True, 0, False
558 558
559 559 def copiespathcopies(orig, ctx1, ctx2, match=None):
560 560 copies = orig(ctx1, ctx2, match=match)
561 561 updated = {}
562 562
563 563 for k, v in copies.iteritems():
564 564 updated[lfutil.splitstandin(k) or k] = lfutil.splitstandin(v) or v
565 565
566 566 return updated
567 567
568 568 # Copy first changes the matchers to match standins instead of
569 569 # largefiles. Then it overrides util.copyfile in that function it
570 570 # checks if the destination largefile already exists. It also keeps a
571 571 # list of copied files so that the largefiles can be copied and the
572 572 # dirstate updated.
573 573 def overridecopy(orig, ui, repo, pats, opts, rename=False):
574 574 # doesn't remove largefile on rename
575 575 if len(pats) < 2:
576 576 # this isn't legal, let the original function deal with it
577 577 return orig(ui, repo, pats, opts, rename)
578 578
579 579 # This could copy both lfiles and normal files in one command,
580 580 # but we don't want to do that. First replace their matcher to
581 581 # only match normal files and run it, then replace it to just
582 582 # match largefiles and run it again.
583 583 nonormalfiles = False
584 584 nolfiles = False
585 585 installnormalfilesmatchfn(repo[None].manifest())
586 586 try:
587 587 result = orig(ui, repo, pats, opts, rename)
588 588 except error.Abort as e:
589 589 if str(e) != _('no files to copy'):
590 590 raise e
591 591 else:
592 592 nonormalfiles = True
593 593 result = 0
594 594 finally:
595 595 restorematchfn()
596 596
597 597 # The first rename can cause our current working directory to be removed.
598 598 # In that case there is nothing left to copy/rename so just quit.
599 599 try:
600 600 repo.getcwd()
601 601 except OSError:
602 602 return result
603 603
604 604 def makestandin(relpath):
605 605 path = pathutil.canonpath(repo.root, repo.getcwd(), relpath)
606 return os.path.join(repo.wjoin(lfutil.standin(path)))
606 return repo.wvfs.join(lfutil.standin(path))
607 607
608 608 fullpats = scmutil.expandpats(pats)
609 609 dest = fullpats[-1]
610 610
611 611 if os.path.isdir(dest):
612 612 if not os.path.isdir(makestandin(dest)):
613 613 os.makedirs(makestandin(dest))
614 614
615 615 try:
616 616 # When we call orig below it creates the standins but we don't add
617 617 # them to the dir state until later so lock during that time.
618 618 wlock = repo.wlock()
619 619
620 620 manifest = repo[None].manifest()
621 621 def overridematch(ctx, pats=(), opts=None, globbed=False,
622 622 default='relpath', badfn=None):
623 623 if opts is None:
624 624 opts = {}
625 625 newpats = []
626 626 # The patterns were previously mangled to add the standin
627 627 # directory; we need to remove that now
628 628 for pat in pats:
629 629 if match_.patkind(pat) is None and lfutil.shortname in pat:
630 630 newpats.append(pat.replace(lfutil.shortname, ''))
631 631 else:
632 632 newpats.append(pat)
633 633 match = oldmatch(ctx, newpats, opts, globbed, default, badfn=badfn)
634 634 m = copy.copy(match)
635 635 lfile = lambda f: lfutil.standin(f) in manifest
636 636 m._files = [lfutil.standin(f) for f in m._files if lfile(f)]
637 637 m._fileroots = set(m._files)
638 638 origmatchfn = m.matchfn
639 639 m.matchfn = lambda f: (lfutil.isstandin(f) and
640 640 (f in manifest) and
641 641 origmatchfn(lfutil.splitstandin(f)) or
642 642 None)
643 643 return m
644 644 oldmatch = installmatchfn(overridematch)
645 645 listpats = []
646 646 for pat in pats:
647 647 if match_.patkind(pat) is not None:
648 648 listpats.append(pat)
649 649 else:
650 650 listpats.append(makestandin(pat))
651 651
652 652 try:
653 653 origcopyfile = util.copyfile
654 654 copiedfiles = []
655 655 def overridecopyfile(src, dest):
656 656 if (lfutil.shortname in src and
657 657 dest.startswith(repo.wjoin(lfutil.shortname))):
658 658 destlfile = dest.replace(lfutil.shortname, '')
659 659 if not opts['force'] and os.path.exists(destlfile):
660 660 raise IOError('',
661 661 _('destination largefile already exists'))
662 662 copiedfiles.append((src, dest))
663 663 origcopyfile(src, dest)
664 664
665 665 util.copyfile = overridecopyfile
666 666 result += orig(ui, repo, listpats, opts, rename)
667 667 finally:
668 668 util.copyfile = origcopyfile
669 669
670 670 lfdirstate = lfutil.openlfdirstate(ui, repo)
671 671 for (src, dest) in copiedfiles:
672 672 if (lfutil.shortname in src and
673 673 dest.startswith(repo.wjoin(lfutil.shortname))):
674 674 srclfile = src.replace(repo.wjoin(lfutil.standin('')), '')
675 675 destlfile = dest.replace(repo.wjoin(lfutil.standin('')), '')
676 destlfiledir = os.path.dirname(repo.wjoin(destlfile)) or '.'
676 destlfiledir = repo.wvfs.dirname(repo.wjoin(destlfile)) or '.'
677 677 if not os.path.isdir(destlfiledir):
678 678 os.makedirs(destlfiledir)
679 679 if rename:
680 680 os.rename(repo.wjoin(srclfile), repo.wjoin(destlfile))
681 681
682 682 # The file is gone, but this deletes any empty parent
683 683 # directories as a side-effect.
684 684 util.unlinkpath(repo.wjoin(srclfile), True)
685 685 lfdirstate.remove(srclfile)
686 686 else:
687 687 util.copyfile(repo.wjoin(srclfile),
688 688 repo.wjoin(destlfile))
689 689
690 690 lfdirstate.add(destlfile)
691 691 lfdirstate.write()
692 692 except error.Abort as e:
693 693 if str(e) != _('no files to copy'):
694 694 raise e
695 695 else:
696 696 nolfiles = True
697 697 finally:
698 698 restorematchfn()
699 699 wlock.release()
700 700
701 701 if nolfiles and nonormalfiles:
702 702 raise error.Abort(_('no files to copy'))
703 703
704 704 return result
705 705
706 706 # When the user calls revert, we have to be careful to not revert any
707 707 # changes to other largefiles accidentally. This means we have to keep
708 708 # track of the largefiles that are being reverted so we only pull down
709 709 # the necessary largefiles.
710 710 #
711 711 # Standins are only updated (to match the hash of largefiles) before
712 712 # commits. Update the standins then run the original revert, changing
713 713 # the matcher to hit standins instead of largefiles. Based on the
714 714 # resulting standins update the largefiles.
715 715 def overriderevert(orig, ui, repo, ctx, parents, *pats, **opts):
716 716 # Because we put the standins in a bad state (by updating them)
717 717 # and then return them to a correct state we need to lock to
718 718 # prevent others from changing them in their incorrect state.
719 719 with repo.wlock():
720 720 lfdirstate = lfutil.openlfdirstate(ui, repo)
721 721 s = lfutil.lfdirstatestatus(lfdirstate, repo)
722 722 lfdirstate.write()
723 723 for lfile in s.modified:
724 724 lfutil.updatestandin(repo, lfutil.standin(lfile))
725 725 for lfile in s.deleted:
726 if (os.path.exists(repo.wjoin(lfutil.standin(lfile)))):
727 os.unlink(repo.wjoin(lfutil.standin(lfile)))
726 if (repo.wvfs.exists(lfutil.standin(lfile))):
727 repo.wvfs.unlink(lfutil.standin(lfile))
728 728
729 729 oldstandins = lfutil.getstandinsstate(repo)
730 730
731 731 def overridematch(mctx, pats=(), opts=None, globbed=False,
732 732 default='relpath', badfn=None):
733 733 if opts is None:
734 734 opts = {}
735 735 match = oldmatch(mctx, pats, opts, globbed, default, badfn=badfn)
736 736 m = copy.copy(match)
737 737
738 738 # revert supports recursing into subrepos, and though largefiles
739 739 # currently doesn't work correctly in that case, this match is
740 740 # called, so the lfdirstate above may not be the correct one for
741 741 # this invocation of match.
742 742 lfdirstate = lfutil.openlfdirstate(mctx.repo().ui, mctx.repo(),
743 743 False)
744 744
745 745 def tostandin(f):
746 746 standin = lfutil.standin(f)
747 747 if standin in ctx or standin in mctx:
748 748 return standin
749 749 elif standin in repo[None] or lfdirstate[f] == 'r':
750 750 return None
751 751 return f
752 752 m._files = [tostandin(f) for f in m._files]
753 753 m._files = [f for f in m._files if f is not None]
754 754 m._fileroots = set(m._files)
755 755 origmatchfn = m.matchfn
756 756 def matchfn(f):
757 757 if lfutil.isstandin(f):
758 758 return (origmatchfn(lfutil.splitstandin(f)) and
759 759 (f in ctx or f in mctx))
760 760 return origmatchfn(f)
761 761 m.matchfn = matchfn
762 762 return m
763 763 oldmatch = installmatchfn(overridematch)
764 764 try:
765 765 orig(ui, repo, ctx, parents, *pats, **opts)
766 766 finally:
767 767 restorematchfn()
768 768
769 769 newstandins = lfutil.getstandinsstate(repo)
770 770 filelist = lfutil.getlfilestoupdate(oldstandins, newstandins)
771 771 # lfdirstate should be 'normallookup'-ed for updated files,
772 772 # because reverting doesn't touch dirstate for 'normal' files
773 773 # when target revision is explicitly specified: in such case,
774 774 # 'n' and valid timestamp in dirstate doesn't ensure 'clean'
775 775 # of target (standin) file.
776 776 lfcommands.updatelfiles(ui, repo, filelist, printmessage=False,
777 777 normallookup=True)
778 778
779 779 # after pulling changesets, we need to take some extra care to get
780 780 # largefiles updated remotely
781 781 def overridepull(orig, ui, repo, source=None, **opts):
782 782 revsprepull = len(repo)
783 783 if not source:
784 784 source = 'default'
785 785 repo.lfpullsource = source
786 786 result = orig(ui, repo, source, **opts)
787 787 revspostpull = len(repo)
788 788 lfrevs = opts.get('lfrev', [])
789 789 if opts.get('all_largefiles'):
790 790 lfrevs.append('pulled()')
791 791 if lfrevs and revspostpull > revsprepull:
792 792 numcached = 0
793 793 repo.firstpulled = revsprepull # for pulled() revset expression
794 794 try:
795 795 for rev in scmutil.revrange(repo, lfrevs):
796 796 ui.note(_('pulling largefiles for revision %s\n') % rev)
797 797 (cached, missing) = lfcommands.cachelfiles(ui, repo, rev)
798 798 numcached += len(cached)
799 799 finally:
800 800 del repo.firstpulled
801 801 ui.status(_("%d largefiles cached\n") % numcached)
802 802 return result
803 803
804 804 revsetpredicate = registrar.revsetpredicate()
805 805
806 806 @revsetpredicate('pulled()')
807 807 def pulledrevsetsymbol(repo, subset, x):
808 808 """Changesets that just has been pulled.
809 809
810 810 Only available with largefiles from pull --lfrev expressions.
811 811
812 812 .. container:: verbose
813 813
814 814 Some examples:
815 815
816 816 - pull largefiles for all new changesets::
817 817
818 818 hg pull -lfrev "pulled()"
819 819
820 820 - pull largefiles for all new branch heads::
821 821
822 822 hg pull -lfrev "head(pulled()) and not closed()"
823 823
824 824 """
825 825
826 826 try:
827 827 firstpulled = repo.firstpulled
828 828 except AttributeError:
829 829 raise error.Abort(_("pulled() only available in --lfrev"))
830 830 return revset.baseset([r for r in subset if r >= firstpulled])
831 831
832 832 def overrideclone(orig, ui, source, dest=None, **opts):
833 833 d = dest
834 834 if d is None:
835 835 d = hg.defaultdest(source)
836 836 if opts.get('all_largefiles') and not hg.islocal(d):
837 837 raise error.Abort(_(
838 838 '--all-largefiles is incompatible with non-local destination %s') %
839 839 d)
840 840
841 841 return orig(ui, source, dest, **opts)
842 842
843 843 def hgclone(orig, ui, opts, *args, **kwargs):
844 844 result = orig(ui, opts, *args, **kwargs)
845 845
846 846 if result is not None:
847 847 sourcerepo, destrepo = result
848 848 repo = destrepo.local()
849 849
850 850 # When cloning to a remote repo (like through SSH), no repo is available
851 851 # from the peer. Therefore the largefiles can't be downloaded and the
852 852 # hgrc can't be updated.
853 853 if not repo:
854 854 return result
855 855
856 856 # If largefiles is required for this repo, permanently enable it locally
857 857 if 'largefiles' in repo.requirements:
858 858 fp = repo.vfs('hgrc', 'a', text=True)
859 859 try:
860 860 fp.write('\n[extensions]\nlargefiles=\n')
861 861 finally:
862 862 fp.close()
863 863
864 864 # Caching is implicitly limited to 'rev' option, since the dest repo was
865 865 # truncated at that point. The user may expect a download count with
866 866 # this option, so attempt whether or not this is a largefile repo.
867 867 if opts.get('all_largefiles'):
868 868 success, missing = lfcommands.downloadlfiles(ui, repo, None)
869 869
870 870 if missing != 0:
871 871 return None
872 872
873 873 return result
874 874
875 875 def overriderebase(orig, ui, repo, **opts):
876 876 if not util.safehasattr(repo, '_largefilesenabled'):
877 877 return orig(ui, repo, **opts)
878 878
879 879 resuming = opts.get('continue')
880 880 repo._lfcommithooks.append(lfutil.automatedcommithook(resuming))
881 881 repo._lfstatuswriters.append(lambda *msg, **opts: None)
882 882 try:
883 883 return orig(ui, repo, **opts)
884 884 finally:
885 885 repo._lfstatuswriters.pop()
886 886 repo._lfcommithooks.pop()
887 887
888 888 def overridearchivecmd(orig, ui, repo, dest, **opts):
889 889 repo.unfiltered().lfstatus = True
890 890
891 891 try:
892 892 return orig(ui, repo.unfiltered(), dest, **opts)
893 893 finally:
894 894 repo.unfiltered().lfstatus = False
895 895
896 896 def hgwebarchive(orig, web, req, tmpl):
897 897 web.repo.lfstatus = True
898 898
899 899 try:
900 900 return orig(web, req, tmpl)
901 901 finally:
902 902 web.repo.lfstatus = False
903 903
904 904 def overridearchive(orig, repo, dest, node, kind, decode=True, matchfn=None,
905 905 prefix='', mtime=None, subrepos=None):
906 906 # For some reason setting repo.lfstatus in hgwebarchive only changes the
907 907 # unfiltered repo's attr, so check that as well.
908 908 if not repo.lfstatus and not repo.unfiltered().lfstatus:
909 909 return orig(repo, dest, node, kind, decode, matchfn, prefix, mtime,
910 910 subrepos)
911 911
912 912 # No need to lock because we are only reading history and
913 913 # largefile caches, neither of which are modified.
914 914 if node is not None:
915 915 lfcommands.cachelfiles(repo.ui, repo, node)
916 916
917 917 if kind not in archival.archivers:
918 918 raise error.Abort(_("unknown archive type '%s'") % kind)
919 919
920 920 ctx = repo[node]
921 921
922 922 if kind == 'files':
923 923 if prefix:
924 924 raise error.Abort(
925 925 _('cannot give prefix when archiving to files'))
926 926 else:
927 927 prefix = archival.tidyprefix(dest, kind, prefix)
928 928
929 929 def write(name, mode, islink, getdata):
930 930 if matchfn and not matchfn(name):
931 931 return
932 932 data = getdata()
933 933 if decode:
934 934 data = repo.wwritedata(name, data)
935 935 archiver.addfile(prefix + name, mode, islink, data)
936 936
937 937 archiver = archival.archivers[kind](dest, mtime or ctx.date()[0])
938 938
939 939 if repo.ui.configbool("ui", "archivemeta", True):
940 940 write('.hg_archival.txt', 0o644, False,
941 941 lambda: archival.buildmetadata(ctx))
942 942
943 943 for f in ctx:
944 944 ff = ctx.flags(f)
945 945 getdata = ctx[f].data
946 946 if lfutil.isstandin(f):
947 947 if node is not None:
948 948 path = lfutil.findfile(repo, getdata().strip())
949 949
950 950 if path is None:
951 951 raise error.Abort(
952 952 _('largefile %s not found in repo store or system cache')
953 953 % lfutil.splitstandin(f))
954 954 else:
955 955 path = lfutil.splitstandin(f)
956 956
957 957 f = lfutil.splitstandin(f)
958 958
959 959 getdata = lambda: util.readfile(path)
960 960 write(f, 'x' in ff and 0o755 or 0o644, 'l' in ff, getdata)
961 961
962 962 if subrepos:
963 963 for subpath in sorted(ctx.substate):
964 964 sub = ctx.workingsub(subpath)
965 965 submatch = match_.subdirmatcher(subpath, matchfn)
966 966 sub._repo.lfstatus = True
967 967 sub.archive(archiver, prefix, submatch)
968 968
969 969 archiver.done()
970 970
971 971 def hgsubrepoarchive(orig, repo, archiver, prefix, match=None):
972 972 if not repo._repo.lfstatus:
973 973 return orig(repo, archiver, prefix, match)
974 974
975 975 repo._get(repo._state + ('hg',))
976 976 rev = repo._state[1]
977 977 ctx = repo._repo[rev]
978 978
979 979 if ctx.node() is not None:
980 980 lfcommands.cachelfiles(repo.ui, repo._repo, ctx.node())
981 981
982 982 def write(name, mode, islink, getdata):
983 983 # At this point, the standin has been replaced with the largefile name,
984 984 # so the normal matcher works here without the lfutil variants.
985 985 if match and not match(f):
986 986 return
987 987 data = getdata()
988 988
989 989 archiver.addfile(prefix + repo._path + '/' + name, mode, islink, data)
990 990
991 991 for f in ctx:
992 992 ff = ctx.flags(f)
993 993 getdata = ctx[f].data
994 994 if lfutil.isstandin(f):
995 995 if ctx.node() is not None:
996 996 path = lfutil.findfile(repo._repo, getdata().strip())
997 997
998 998 if path is None:
999 999 raise error.Abort(
1000 1000 _('largefile %s not found in repo store or system cache')
1001 1001 % lfutil.splitstandin(f))
1002 1002 else:
1003 1003 path = lfutil.splitstandin(f)
1004 1004
1005 1005 f = lfutil.splitstandin(f)
1006 1006
1007 1007 getdata = lambda: util.readfile(os.path.join(prefix, path))
1008 1008
1009 1009 write(f, 'x' in ff and 0o755 or 0o644, 'l' in ff, getdata)
1010 1010
1011 1011 for subpath in sorted(ctx.substate):
1012 1012 sub = ctx.workingsub(subpath)
1013 1013 submatch = match_.subdirmatcher(subpath, match)
1014 1014 sub._repo.lfstatus = True
1015 1015 sub.archive(archiver, prefix + repo._path + '/', submatch)
1016 1016
1017 1017 # If a largefile is modified, the change is not reflected in its
1018 1018 # standin until a commit. cmdutil.bailifchanged() raises an exception
1019 1019 # if the repo has uncommitted changes. Wrap it to also check if
1020 1020 # largefiles were changed. This is used by bisect, backout and fetch.
1021 1021 def overridebailifchanged(orig, repo, *args, **kwargs):
1022 1022 orig(repo, *args, **kwargs)
1023 1023 repo.lfstatus = True
1024 1024 s = repo.status()
1025 1025 repo.lfstatus = False
1026 1026 if s.modified or s.added or s.removed or s.deleted:
1027 1027 raise error.Abort(_('uncommitted changes'))
1028 1028
1029 1029 def postcommitstatus(orig, repo, *args, **kwargs):
1030 1030 repo.lfstatus = True
1031 1031 try:
1032 1032 return orig(repo, *args, **kwargs)
1033 1033 finally:
1034 1034 repo.lfstatus = False
1035 1035
1036 1036 def cmdutilforget(orig, ui, repo, match, prefix, explicitonly):
1037 1037 normalmatcher = composenormalfilematcher(match, repo[None].manifest())
1038 1038 bad, forgot = orig(ui, repo, normalmatcher, prefix, explicitonly)
1039 1039 m = composelargefilematcher(match, repo[None].manifest())
1040 1040
1041 1041 try:
1042 1042 repo.lfstatus = True
1043 1043 s = repo.status(match=m, clean=True)
1044 1044 finally:
1045 1045 repo.lfstatus = False
1046 1046 forget = sorted(s.modified + s.added + s.deleted + s.clean)
1047 1047 forget = [f for f in forget if lfutil.standin(f) in repo[None].manifest()]
1048 1048
1049 1049 for f in forget:
1050 1050 if lfutil.standin(f) not in repo.dirstate and not \
1051 1051 repo.wvfs.isdir(lfutil.standin(f)):
1052 1052 ui.warn(_('not removing %s: file is already untracked\n')
1053 1053 % m.rel(f))
1054 1054 bad.append(f)
1055 1055
1056 1056 for f in forget:
1057 1057 if ui.verbose or not m.exact(f):
1058 1058 ui.status(_('removing %s\n') % m.rel(f))
1059 1059
1060 1060 # Need to lock because standin files are deleted then removed from the
1061 1061 # repository and we could race in-between.
1062 1062 with repo.wlock():
1063 1063 lfdirstate = lfutil.openlfdirstate(ui, repo)
1064 1064 for f in forget:
1065 1065 if lfdirstate[f] == 'a':
1066 1066 lfdirstate.drop(f)
1067 1067 else:
1068 1068 lfdirstate.remove(f)
1069 1069 lfdirstate.write()
1070 1070 standins = [lfutil.standin(f) for f in forget]
1071 1071 for f in standins:
1072 1072 util.unlinkpath(repo.wjoin(f), ignoremissing=True)
1073 1073 rejected = repo[None].forget(standins)
1074 1074
1075 1075 bad.extend(f for f in rejected if f in m.files())
1076 1076 forgot.extend(f for f in forget if f not in rejected)
1077 1077 return bad, forgot
1078 1078
1079 1079 def _getoutgoings(repo, other, missing, addfunc):
1080 1080 """get pairs of filename and largefile hash in outgoing revisions
1081 1081 in 'missing'.
1082 1082
1083 1083 largefiles already existing on 'other' repository are ignored.
1084 1084
1085 1085 'addfunc' is invoked with each unique pairs of filename and
1086 1086 largefile hash value.
1087 1087 """
1088 1088 knowns = set()
1089 1089 lfhashes = set()
1090 1090 def dedup(fn, lfhash):
1091 1091 k = (fn, lfhash)
1092 1092 if k not in knowns:
1093 1093 knowns.add(k)
1094 1094 lfhashes.add(lfhash)
1095 1095 lfutil.getlfilestoupload(repo, missing, dedup)
1096 1096 if lfhashes:
1097 1097 lfexists = basestore._openstore(repo, other).exists(lfhashes)
1098 1098 for fn, lfhash in knowns:
1099 1099 if not lfexists[lfhash]: # lfhash doesn't exist on "other"
1100 1100 addfunc(fn, lfhash)
1101 1101
1102 1102 def outgoinghook(ui, repo, other, opts, missing):
1103 1103 if opts.pop('large', None):
1104 1104 lfhashes = set()
1105 1105 if ui.debugflag:
1106 1106 toupload = {}
1107 1107 def addfunc(fn, lfhash):
1108 1108 if fn not in toupload:
1109 1109 toupload[fn] = []
1110 1110 toupload[fn].append(lfhash)
1111 1111 lfhashes.add(lfhash)
1112 1112 def showhashes(fn):
1113 1113 for lfhash in sorted(toupload[fn]):
1114 1114 ui.debug(' %s\n' % (lfhash))
1115 1115 else:
1116 1116 toupload = set()
1117 1117 def addfunc(fn, lfhash):
1118 1118 toupload.add(fn)
1119 1119 lfhashes.add(lfhash)
1120 1120 def showhashes(fn):
1121 1121 pass
1122 1122 _getoutgoings(repo, other, missing, addfunc)
1123 1123
1124 1124 if not toupload:
1125 1125 ui.status(_('largefiles: no files to upload\n'))
1126 1126 else:
1127 1127 ui.status(_('largefiles to upload (%d entities):\n')
1128 1128 % (len(lfhashes)))
1129 1129 for file in sorted(toupload):
1130 1130 ui.status(lfutil.splitstandin(file) + '\n')
1131 1131 showhashes(file)
1132 1132 ui.status('\n')
1133 1133
1134 1134 def summaryremotehook(ui, repo, opts, changes):
1135 1135 largeopt = opts.get('large', False)
1136 1136 if changes is None:
1137 1137 if largeopt:
1138 1138 return (False, True) # only outgoing check is needed
1139 1139 else:
1140 1140 return (False, False)
1141 1141 elif largeopt:
1142 1142 url, branch, peer, outgoing = changes[1]
1143 1143 if peer is None:
1144 1144 # i18n: column positioning for "hg summary"
1145 1145 ui.status(_('largefiles: (no remote repo)\n'))
1146 1146 return
1147 1147
1148 1148 toupload = set()
1149 1149 lfhashes = set()
1150 1150 def addfunc(fn, lfhash):
1151 1151 toupload.add(fn)
1152 1152 lfhashes.add(lfhash)
1153 1153 _getoutgoings(repo, peer, outgoing.missing, addfunc)
1154 1154
1155 1155 if not toupload:
1156 1156 # i18n: column positioning for "hg summary"
1157 1157 ui.status(_('largefiles: (no files to upload)\n'))
1158 1158 else:
1159 1159 # i18n: column positioning for "hg summary"
1160 1160 ui.status(_('largefiles: %d entities for %d files to upload\n')
1161 1161 % (len(lfhashes), len(toupload)))
1162 1162
1163 1163 def overridesummary(orig, ui, repo, *pats, **opts):
1164 1164 try:
1165 1165 repo.lfstatus = True
1166 1166 orig(ui, repo, *pats, **opts)
1167 1167 finally:
1168 1168 repo.lfstatus = False
1169 1169
1170 1170 def scmutiladdremove(orig, repo, matcher, prefix, opts=None, dry_run=None,
1171 1171 similarity=None):
1172 1172 if opts is None:
1173 1173 opts = {}
1174 1174 if not lfutil.islfilesrepo(repo):
1175 1175 return orig(repo, matcher, prefix, opts, dry_run, similarity)
1176 1176 # Get the list of missing largefiles so we can remove them
1177 1177 lfdirstate = lfutil.openlfdirstate(repo.ui, repo)
1178 1178 unsure, s = lfdirstate.status(match_.always(repo.root, repo.getcwd()), [],
1179 1179 False, False, False)
1180 1180
1181 1181 # Call into the normal remove code, but the removing of the standin, we want
1182 1182 # to have handled by original addremove. Monkey patching here makes sure
1183 1183 # we don't remove the standin in the largefiles code, preventing a very
1184 1184 # confused state later.
1185 1185 if s.deleted:
1186 1186 m = copy.copy(matcher)
1187 1187
1188 1188 # The m._files and m._map attributes are not changed to the deleted list
1189 1189 # because that affects the m.exact() test, which in turn governs whether
1190 1190 # or not the file name is printed, and how. Simply limit the original
1191 1191 # matches to those in the deleted status list.
1192 1192 matchfn = m.matchfn
1193 1193 m.matchfn = lambda f: f in s.deleted and matchfn(f)
1194 1194
1195 1195 removelargefiles(repo.ui, repo, True, m, **opts)
1196 1196 # Call into the normal add code, and any files that *should* be added as
1197 1197 # largefiles will be
1198 1198 added, bad = addlargefiles(repo.ui, repo, True, matcher, **opts)
1199 1199 # Now that we've handled largefiles, hand off to the original addremove
1200 1200 # function to take care of the rest. Make sure it doesn't do anything with
1201 1201 # largefiles by passing a matcher that will ignore them.
1202 1202 matcher = composenormalfilematcher(matcher, repo[None].manifest(), added)
1203 1203 return orig(repo, matcher, prefix, opts, dry_run, similarity)
1204 1204
1205 1205 # Calling purge with --all will cause the largefiles to be deleted.
1206 1206 # Override repo.status to prevent this from happening.
1207 1207 def overridepurge(orig, ui, repo, *dirs, **opts):
1208 1208 # XXX Monkey patching a repoview will not work. The assigned attribute will
1209 1209 # be set on the unfiltered repo, but we will only lookup attributes in the
1210 1210 # unfiltered repo if the lookup in the repoview object itself fails. As the
1211 1211 # monkey patched method exists on the repoview class the lookup will not
1212 1212 # fail. As a result, the original version will shadow the monkey patched
1213 1213 # one, defeating the monkey patch.
1214 1214 #
1215 1215 # As a work around we use an unfiltered repo here. We should do something
1216 1216 # cleaner instead.
1217 1217 repo = repo.unfiltered()
1218 1218 oldstatus = repo.status
1219 1219 def overridestatus(node1='.', node2=None, match=None, ignored=False,
1220 1220 clean=False, unknown=False, listsubrepos=False):
1221 1221 r = oldstatus(node1, node2, match, ignored, clean, unknown,
1222 1222 listsubrepos)
1223 1223 lfdirstate = lfutil.openlfdirstate(ui, repo)
1224 1224 unknown = [f for f in r.unknown if lfdirstate[f] == '?']
1225 1225 ignored = [f for f in r.ignored if lfdirstate[f] == '?']
1226 1226 return scmutil.status(r.modified, r.added, r.removed, r.deleted,
1227 1227 unknown, ignored, r.clean)
1228 1228 repo.status = overridestatus
1229 1229 orig(ui, repo, *dirs, **opts)
1230 1230 repo.status = oldstatus
1231 1231 def overriderollback(orig, ui, repo, **opts):
1232 1232 with repo.wlock():
1233 1233 before = repo.dirstate.parents()
1234 1234 orphans = set(f for f in repo.dirstate
1235 1235 if lfutil.isstandin(f) and repo.dirstate[f] != 'r')
1236 1236 result = orig(ui, repo, **opts)
1237 1237 after = repo.dirstate.parents()
1238 1238 if before == after:
1239 1239 return result # no need to restore standins
1240 1240
1241 1241 pctx = repo['.']
1242 1242 for f in repo.dirstate:
1243 1243 if lfutil.isstandin(f):
1244 1244 orphans.discard(f)
1245 1245 if repo.dirstate[f] == 'r':
1246 1246 repo.wvfs.unlinkpath(f, ignoremissing=True)
1247 1247 elif f in pctx:
1248 1248 fctx = pctx[f]
1249 1249 repo.wwrite(f, fctx.data(), fctx.flags())
1250 1250 else:
1251 1251 # content of standin is not so important in 'a',
1252 1252 # 'm' or 'n' (coming from the 2nd parent) cases
1253 1253 lfutil.writestandin(repo, f, '', False)
1254 1254 for standin in orphans:
1255 1255 repo.wvfs.unlinkpath(standin, ignoremissing=True)
1256 1256
1257 1257 lfdirstate = lfutil.openlfdirstate(ui, repo)
1258 1258 orphans = set(lfdirstate)
1259 1259 lfiles = lfutil.listlfiles(repo)
1260 1260 for file in lfiles:
1261 1261 lfutil.synclfdirstate(repo, lfdirstate, file, True)
1262 1262 orphans.discard(file)
1263 1263 for lfile in orphans:
1264 1264 lfdirstate.drop(lfile)
1265 1265 lfdirstate.write()
1266 1266 return result
1267 1267
1268 1268 def overridetransplant(orig, ui, repo, *revs, **opts):
1269 1269 resuming = opts.get('continue')
1270 1270 repo._lfcommithooks.append(lfutil.automatedcommithook(resuming))
1271 1271 repo._lfstatuswriters.append(lambda *msg, **opts: None)
1272 1272 try:
1273 1273 result = orig(ui, repo, *revs, **opts)
1274 1274 finally:
1275 1275 repo._lfstatuswriters.pop()
1276 1276 repo._lfcommithooks.pop()
1277 1277 return result
1278 1278
1279 1279 def overridecat(orig, ui, repo, file1, *pats, **opts):
1280 1280 ctx = scmutil.revsingle(repo, opts.get('rev'))
1281 1281 err = 1
1282 1282 notbad = set()
1283 1283 m = scmutil.match(ctx, (file1,) + pats, opts)
1284 1284 origmatchfn = m.matchfn
1285 1285 def lfmatchfn(f):
1286 1286 if origmatchfn(f):
1287 1287 return True
1288 1288 lf = lfutil.splitstandin(f)
1289 1289 if lf is None:
1290 1290 return False
1291 1291 notbad.add(lf)
1292 1292 return origmatchfn(lf)
1293 1293 m.matchfn = lfmatchfn
1294 1294 origbadfn = m.bad
1295 1295 def lfbadfn(f, msg):
1296 1296 if not f in notbad:
1297 1297 origbadfn(f, msg)
1298 1298 m.bad = lfbadfn
1299 1299
1300 1300 origvisitdirfn = m.visitdir
1301 1301 def lfvisitdirfn(dir):
1302 1302 if dir == lfutil.shortname:
1303 1303 return True
1304 1304 ret = origvisitdirfn(dir)
1305 1305 if ret:
1306 1306 return ret
1307 1307 lf = lfutil.splitstandin(dir)
1308 1308 if lf is None:
1309 1309 return False
1310 1310 return origvisitdirfn(lf)
1311 1311 m.visitdir = lfvisitdirfn
1312 1312
1313 1313 for f in ctx.walk(m):
1314 1314 fp = cmdutil.makefileobj(repo, opts.get('output'), ctx.node(),
1315 1315 pathname=f)
1316 1316 lf = lfutil.splitstandin(f)
1317 1317 if lf is None or origmatchfn(f):
1318 1318 # duplicating unreachable code from commands.cat
1319 1319 data = ctx[f].data()
1320 1320 if opts.get('decode'):
1321 1321 data = repo.wwritedata(f, data)
1322 1322 fp.write(data)
1323 1323 else:
1324 1324 hash = lfutil.readstandin(repo, lf, ctx.rev())
1325 1325 if not lfutil.inusercache(repo.ui, hash):
1326 1326 store = basestore._openstore(repo)
1327 1327 success, missing = store.get([(lf, hash)])
1328 1328 if len(success) != 1:
1329 1329 raise error.Abort(
1330 1330 _('largefile %s is not in cache and could not be '
1331 1331 'downloaded') % lf)
1332 1332 path = lfutil.usercachepath(repo.ui, hash)
1333 1333 fpin = open(path, "rb")
1334 1334 for chunk in util.filechunkiter(fpin, 128 * 1024):
1335 1335 fp.write(chunk)
1336 1336 fpin.close()
1337 1337 fp.close()
1338 1338 err = 0
1339 1339 return err
1340 1340
1341 1341 def mergeupdate(orig, repo, node, branchmerge, force,
1342 1342 *args, **kwargs):
1343 1343 matcher = kwargs.get('matcher', None)
1344 1344 # note if this is a partial update
1345 1345 partial = matcher and not matcher.always()
1346 1346 with repo.wlock():
1347 1347 # branch | | |
1348 1348 # merge | force | partial | action
1349 1349 # -------+-------+---------+--------------
1350 1350 # x | x | x | linear-merge
1351 1351 # o | x | x | branch-merge
1352 1352 # x | o | x | overwrite (as clean update)
1353 1353 # o | o | x | force-branch-merge (*1)
1354 1354 # x | x | o | (*)
1355 1355 # o | x | o | (*)
1356 1356 # x | o | o | overwrite (as revert)
1357 1357 # o | o | o | (*)
1358 1358 #
1359 1359 # (*) don't care
1360 1360 # (*1) deprecated, but used internally (e.g: "rebase --collapse")
1361 1361
1362 1362 lfdirstate = lfutil.openlfdirstate(repo.ui, repo)
1363 1363 unsure, s = lfdirstate.status(match_.always(repo.root,
1364 1364 repo.getcwd()),
1365 1365 [], False, False, False)
1366 1366 pctx = repo['.']
1367 1367 for lfile in unsure + s.modified:
1368 1368 lfileabs = repo.wvfs.join(lfile)
1369 if not os.path.exists(lfileabs):
1369 if not repo.wvfs.exists(lfileabs):
1370 1370 continue
1371 1371 lfhash = lfutil.hashrepofile(repo, lfile)
1372 1372 standin = lfutil.standin(lfile)
1373 1373 lfutil.writestandin(repo, standin, lfhash,
1374 1374 lfutil.getexecutable(lfileabs))
1375 1375 if (standin in pctx and
1376 1376 lfhash == lfutil.readstandin(repo, lfile, '.')):
1377 1377 lfdirstate.normal(lfile)
1378 1378 for lfile in s.added:
1379 1379 lfutil.updatestandin(repo, lfutil.standin(lfile))
1380 1380 lfdirstate.write()
1381 1381
1382 1382 oldstandins = lfutil.getstandinsstate(repo)
1383 1383
1384 1384 result = orig(repo, node, branchmerge, force, *args, **kwargs)
1385 1385
1386 1386 newstandins = lfutil.getstandinsstate(repo)
1387 1387 filelist = lfutil.getlfilestoupdate(oldstandins, newstandins)
1388 1388 if branchmerge or force or partial:
1389 1389 filelist.extend(s.deleted + s.removed)
1390 1390
1391 1391 lfcommands.updatelfiles(repo.ui, repo, filelist=filelist,
1392 1392 normallookup=partial)
1393 1393
1394 1394 return result
1395 1395
1396 1396 def scmutilmarktouched(orig, repo, files, *args, **kwargs):
1397 1397 result = orig(repo, files, *args, **kwargs)
1398 1398
1399 1399 filelist = [lfutil.splitstandin(f) for f in files if lfutil.isstandin(f)]
1400 1400 if filelist:
1401 1401 lfcommands.updatelfiles(repo.ui, repo, filelist=filelist,
1402 1402 printmessage=False, normallookup=True)
1403 1403
1404 1404 return result
General Comments 0
You need to be logged in to leave comments. Login now