mercurial/commands.py
changeset 1633 94c179a92f4a
parent 1626 f2b1df3dbcbb
child 1634 f49f602fae92
equal deleted inserted replaced
1632:3f214984fd9e 1633:94c179a92f4a
   854         repo.copy(abssrc, abstarget)
   854         repo.copy(abssrc, abstarget)
   855         copied.append((abssrc, relsrc, exact))
   855         copied.append((abssrc, relsrc, exact))
   856 
   856 
   857     def targetpathfn(pat, dest, srcs):
   857     def targetpathfn(pat, dest, srcs):
   858         if os.path.isdir(pat):
   858         if os.path.isdir(pat):
   859             if pat.endswith(os.sep):
   859             abspfx = util.canonpath(repo.root, cwd, pat)
   860                 pat = pat[:-len(os.sep)]
       
   861             if destdirexists:
   860             if destdirexists:
   862                 striplen = len(os.path.split(pat)[0])
   861                 striplen = len(os.path.split(abspfx)[0])
   863             else:
   862             else:
   864                 striplen = len(pat)
   863                 striplen = len(abspfx)
   865             if striplen:
   864             if striplen:
   866                 striplen += len(os.sep)
   865                 striplen += len(os.sep)
   867             res = lambda p: os.path.join(dest, p[striplen:])
   866             res = lambda p: os.path.join(dest, p[striplen:])
   868         elif destdirexists:
   867         elif destdirexists:
   869             res = lambda p: os.path.join(dest, os.path.basename(p))
   868             res = lambda p: os.path.join(dest, os.path.basename(p))
   873 
   872 
   874     def targetpathafterfn(pat, dest, srcs):
   873     def targetpathafterfn(pat, dest, srcs):
   875         if util.patkind(pat, None)[0]:
   874         if util.patkind(pat, None)[0]:
   876             # a mercurial pattern
   875             # a mercurial pattern
   877             res = lambda p: os.path.join(dest, os.path.basename(p))
   876             res = lambda p: os.path.join(dest, os.path.basename(p))
   878         elif len(util.canonpath(repo.root, cwd, pat)) < len(srcs[0][0]):
       
   879             # A directory. Either the target path contains the last
       
   880             # component of the source path or it does not.
       
   881             def evalpath(striplen):
       
   882                 score = 0
       
   883                 for s in srcs:
       
   884                     t = os.path.join(dest, s[1][striplen:])
       
   885                     if os.path.exists(t):
       
   886                         score += 1
       
   887                 return score
       
   888 
       
   889             if pat.endswith(os.sep):
       
   890                 pat = pat[:-len(os.sep)]
       
   891             striplen = len(pat) + len(os.sep)
       
   892             if os.path.isdir(os.path.join(dest, os.path.split(pat)[1])):
       
   893                 score = evalpath(striplen)
       
   894                 striplen1 = len(os.path.split(pat)[0])
       
   895                 if striplen1:
       
   896                     striplen1 += len(os.sep)
       
   897                 if evalpath(striplen1) > score:
       
   898                     striplen = striplen1
       
   899             res = lambda p: os.path.join(dest, p[striplen:])
       
   900         else:
   877         else:
   901             # a file
   878             abspfx = util.canonpath(repo.root, cwd, pat)
   902             if destdirexists:
   879             if len(abspfx) < len(srcs[0][0]):
   903                 res = lambda p: os.path.join(dest, os.path.basename(p))
   880                 # A directory. Either the target path contains the last
       
   881                 # component of the source path or it does not.
       
   882                 def evalpath(striplen):
       
   883                     score = 0
       
   884                     for s in srcs:
       
   885                         t = os.path.join(dest, s[0][striplen:])
       
   886                         if os.path.exists(t):
       
   887                             score += 1
       
   888                     return score
       
   889 
       
   890                 striplen = len(abspfx)
       
   891                 if striplen:
       
   892                     striplen += len(os.sep)
       
   893                 if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
       
   894                     score = evalpath(striplen)
       
   895                     striplen1 = len(os.path.split(abspfx)[0])
       
   896                     if striplen1:
       
   897                         striplen1 += len(os.sep)
       
   898                     if evalpath(striplen1) > score:
       
   899                         striplen = striplen1
       
   900                 res = lambda p: os.path.join(dest, p[striplen:])
   904             else:
   901             else:
   905                 res = lambda p: dest
   902                 # a file
       
   903                 if destdirexists:
       
   904                     res = lambda p: os.path.join(dest, os.path.basename(p))
       
   905                 else:
       
   906                     res = lambda p: dest
   906         return res
   907         return res
   907 
   908 
   908 
   909 
   909     pats = list(pats)
   910     pats = list(pats)
   910     if not pats:
   911     if not pats:
   932     if not copylist:
   933     if not copylist:
   933         raise util.Abort(_('no files to copy'))
   934         raise util.Abort(_('no files to copy'))
   934 
   935 
   935     for targetpath, srcs in copylist:
   936     for targetpath, srcs in copylist:
   936         for abssrc, relsrc, exact in srcs:
   937         for abssrc, relsrc, exact in srcs:
   937             copy(abssrc, relsrc, targetpath(relsrc), exact)
   938             copy(abssrc, relsrc, targetpath(abssrc), exact)
   938 
   939 
   939     if errors:
   940     if errors:
   940         ui.warn(_('(consider using --after)\n'))
   941         ui.warn(_('(consider using --after)\n'))
   941     return errors, copied
   942     return errors, copied
   942 
   943