hgext/largefiles/overrides.py
branchstable
changeset 15279 018608160299
parent 15255 7ab05d752405
child 15294 db7b09e689f1
equal deleted inserted replaced
15278:2ed335669e18 15279:018608160299
   318     # only match normal files and run it, then replace it to just
   318     # only match normal files and run it, then replace it to just
   319     # match largefiles and run it again.
   319     # match largefiles and run it again.
   320     nonormalfiles = False
   320     nonormalfiles = False
   321     nolfiles = False
   321     nolfiles = False
   322     try:
   322     try:
   323         installnormalfilesmatchfn(repo[None].manifest())
   323         try:
   324         result = orig(ui, repo, pats, opts, rename)
   324             installnormalfilesmatchfn(repo[None].manifest())
   325     except util.Abort, e:
   325             result = orig(ui, repo, pats, opts, rename)
   326         if str(e) != 'no files to copy':
   326         except util.Abort, e:
   327             raise e
   327             if str(e) != 'no files to copy':
   328         else:
   328                 raise e
   329             nonormalfiles = True
   329             else:
   330         result = 0
   330                 nonormalfiles = True
       
   331             result = 0
   331     finally:
   332     finally:
   332         restorematchfn()
   333         restorematchfn()
   333 
   334 
   334     # The first rename can cause our current working directory to be removed.
   335     # The first rename can cause our current working directory to be removed.
   335     # In that case there is nothing left to copy/rename so just quit.
   336     # In that case there is nothing left to copy/rename so just quit.
   337         repo.getcwd()
   338         repo.getcwd()
   338     except OSError:
   339     except OSError:
   339         return result
   340         return result
   340 
   341 
   341     try:
   342     try:
   342         # When we call orig below it creates the standins but we don't add them
   343         try:
   343         # to the dir state until later so lock during that time.
   344             # When we call orig below it creates the standins but we don't add them
   344         wlock = repo.wlock()
   345             # to the dir state until later so lock during that time.
   345 
   346             wlock = repo.wlock()
   346         manifest = repo[None].manifest()
   347 
   347         oldmatch = None # for the closure
   348             manifest = repo[None].manifest()
   348         def override_match(repo, pats=[], opts={}, globbed=False,
   349             oldmatch = None # for the closure
   349                 default='relpath'):
   350             def override_match(repo, pats=[], opts={}, globbed=False,
   350             newpats = []
   351                     default='relpath'):
   351             # The patterns were previously mangled to add the standin
   352                 newpats = []
   352             # directory; we need to remove that now
   353                 # The patterns were previously mangled to add the standin
       
   354                 # directory; we need to remove that now
       
   355                 for pat in pats:
       
   356                     if match_.patkind(pat) is None and lfutil.shortname in pat:
       
   357                         newpats.append(pat.replace(lfutil.shortname, ''))
       
   358                     else:
       
   359                         newpats.append(pat)
       
   360                 match = oldmatch(repo, newpats, opts, globbed, default)
       
   361                 m = copy.copy(match)
       
   362                 lfile = lambda f: lfutil.standin(f) in manifest
       
   363                 m._files = [lfutil.standin(f) for f in m._files if lfile(f)]
       
   364                 m._fmap = set(m._files)
       
   365                 orig_matchfn = m.matchfn
       
   366                 m.matchfn = lambda f: (lfutil.isstandin(f) and
       
   367                                     lfile(lfutil.splitstandin(f)) and
       
   368                                     orig_matchfn(lfutil.splitstandin(f)) or
       
   369                                     None)
       
   370                 return m
       
   371             oldmatch = installmatchfn(override_match)
       
   372             listpats = []
   353             for pat in pats:
   373             for pat in pats:
   354                 if match_.patkind(pat) is None and lfutil.shortname in pat:
   374                 if match_.patkind(pat) is not None:
   355                     newpats.append(pat.replace(lfutil.shortname, ''))
   375                     listpats.append(pat)
   356                 else:
   376                 else:
   357                     newpats.append(pat)
   377                     listpats.append(makestandin(pat))
   358             match = oldmatch(repo, newpats, opts, globbed, default)
   378 
   359             m = copy.copy(match)
   379             try:
   360             lfile = lambda f: lfutil.standin(f) in manifest
   380                 origcopyfile = util.copyfile
   361             m._files = [lfutil.standin(f) for f in m._files if lfile(f)]
   381                 copiedfiles = []
   362             m._fmap = set(m._files)
   382                 def override_copyfile(src, dest):
   363             orig_matchfn = m.matchfn
   383                     if lfutil.shortname in src and lfutil.shortname in dest:
   364             m.matchfn = lambda f: (lfutil.isstandin(f) and
   384                         destlfile = dest.replace(lfutil.shortname, '')
   365                                    lfile(lfutil.splitstandin(f)) and
   385                         if not opts['force'] and os.path.exists(destlfile):
   366                                    orig_matchfn(lfutil.splitstandin(f)) or
   386                             raise IOError('',
   367                                    None)
   387                                 _('destination largefile already exists'))
   368             return m
   388                     copiedfiles.append((src, dest))
   369         oldmatch = installmatchfn(override_match)
   389                     origcopyfile(src, dest)
   370         listpats = []
   390 
   371         for pat in pats:
   391                 util.copyfile = override_copyfile
   372             if match_.patkind(pat) is not None:
   392                 result += orig(ui, repo, listpats, opts, rename)
   373                 listpats.append(pat)
   393             finally:
       
   394                 util.copyfile = origcopyfile
       
   395 
       
   396             lfdirstate = lfutil.openlfdirstate(ui, repo)
       
   397             for (src, dest) in copiedfiles:
       
   398                 if lfutil.shortname in src and lfutil.shortname in dest:
       
   399                     srclfile = src.replace(lfutil.shortname, '')
       
   400                     destlfile = dest.replace(lfutil.shortname, '')
       
   401                     destlfiledir = os.path.dirname(destlfile) or '.'
       
   402                     if not os.path.isdir(destlfiledir):
       
   403                         os.makedirs(destlfiledir)
       
   404                     if rename:
       
   405                         os.rename(srclfile, destlfile)
       
   406                         lfdirstate.remove(os.path.relpath(srclfile,
       
   407                             repo.root))
       
   408                     else:
       
   409                         util.copyfile(srclfile, destlfile)
       
   410                     lfdirstate.add(os.path.relpath(destlfile,
       
   411                         repo.root))
       
   412             lfdirstate.write()
       
   413         except util.Abort, e:
       
   414             if str(e) != 'no files to copy':
       
   415                 raise e
   374             else:
   416             else:
   375                 listpats.append(makestandin(pat))
   417                 nolfiles = True
   376 
       
   377         try:
       
   378             origcopyfile = util.copyfile
       
   379             copiedfiles = []
       
   380             def override_copyfile(src, dest):
       
   381                 if lfutil.shortname in src and lfutil.shortname in dest:
       
   382                     destlfile = dest.replace(lfutil.shortname, '')
       
   383                     if not opts['force'] and os.path.exists(destlfile):
       
   384                         raise IOError('',
       
   385                             _('destination largefile already exists'))
       
   386                 copiedfiles.append((src, dest))
       
   387                 origcopyfile(src, dest)
       
   388 
       
   389             util.copyfile = override_copyfile
       
   390             result += orig(ui, repo, listpats, opts, rename)
       
   391         finally:
       
   392             util.copyfile = origcopyfile
       
   393 
       
   394         lfdirstate = lfutil.openlfdirstate(ui, repo)
       
   395         for (src, dest) in copiedfiles:
       
   396             if lfutil.shortname in src and lfutil.shortname in dest:
       
   397                 srclfile = src.replace(lfutil.shortname, '')
       
   398                 destlfile = dest.replace(lfutil.shortname, '')
       
   399                 destlfiledir = os.path.dirname(destlfile) or '.'
       
   400                 if not os.path.isdir(destlfiledir):
       
   401                     os.makedirs(destlfiledir)
       
   402                 if rename:
       
   403                     os.rename(srclfile, destlfile)
       
   404                     lfdirstate.remove(os.path.relpath(srclfile,
       
   405                         repo.root))
       
   406                 else:
       
   407                     util.copyfile(srclfile, destlfile)
       
   408                 lfdirstate.add(os.path.relpath(destlfile,
       
   409                     repo.root))
       
   410         lfdirstate.write()
       
   411     except util.Abort, e:
       
   412         if str(e) != 'no files to copy':
       
   413             raise e
       
   414         else:
       
   415             nolfiles = True
       
   416     finally:
   418     finally:
   417         restorematchfn()
   419         restorematchfn()
   418         wlock.release()
   420         wlock.release()
   419 
   421 
   420     if nolfiles and nonormalfiles:
   422     if nolfiles and nonormalfiles: