mercurial/cmdutil.py
changeset 5608 784eadabd985
parent 5607 e9bae5c80ab4
child 5609 a783d3627144
equal deleted inserted replaced
5607:e9bae5c80ab4 5608:784eadabd985
   319     def copyfile(abssrc, relsrc, otarget, exact):
   319     def copyfile(abssrc, relsrc, otarget, exact):
   320         abstarget = util.canonpath(repo.root, cwd, otarget)
   320         abstarget = util.canonpath(repo.root, cwd, otarget)
   321         reltarget = repo.pathto(abstarget, cwd)
   321         reltarget = repo.pathto(abstarget, cwd)
   322         target = repo.wjoin(abstarget)
   322         target = repo.wjoin(abstarget)
   323         src = repo.wjoin(abssrc)
   323         src = repo.wjoin(abssrc)
       
   324         state = repo.dirstate[abstarget]
   324 
   325 
   325         # check for collisions
   326         # check for collisions
   326         prevsrc = targets.get(abstarget)
   327         prevsrc = targets.get(abstarget)
   327         if prevsrc is not None:
   328         if prevsrc is not None:
   328             ui.warn(_('%s: not overwriting - %s collides with %s\n') %
   329             ui.warn(_('%s: not overwriting - %s collides with %s\n') %
   329                     (reltarget, repo.pathto(abssrc, cwd),
   330                     (reltarget, repo.pathto(abssrc, cwd),
   330                      repo.pathto(prevsrc, cwd)))
   331                      repo.pathto(prevsrc, cwd)))
   331             return
   332             return
   332 
   333 
   333         # check for overwrites
   334         # check for overwrites
   334         if (not after and os.path.exists(target) or
   335         exists = os.path.exists(target)
   335             after and repo.dirstate[abstarget] in 'mn'):
   336         if (not after and exists or after and state in 'mn'):
   336             if not opts['force']:
   337             if not opts['force']:
   337                 ui.warn(_('%s: not overwriting - file exists\n') %
   338                 ui.warn(_('%s: not overwriting - file exists\n') %
   338                         reltarget)
   339                         reltarget)
   339                 return
   340                 return
   340             if not after and not dryrun:
       
   341                 os.unlink(target)
       
   342 
   341 
   343         if after:
   342         if after:
   344             if not os.path.exists(target):
   343             if not exists:
   345                 return
   344                 return
   346         else:
   345         elif not dryrun:
   347             targetdir = os.path.dirname(target) or '.'
       
   348             if not os.path.isdir(targetdir) and not dryrun:
       
   349                 os.makedirs(targetdir)
       
   350             try:
   346             try:
   351                 restore = repo.dirstate[abstarget] == 'r'
   347                 if exists:
   352                 if restore and not dryrun:
   348                     os.unlink(target)
   353                     repo.undelete([abstarget])
   349                 targetdir = os.path.dirname(target) or '.'
   354                 try:
   350                 if not os.path.isdir(targetdir):
   355                     if not dryrun:
   351                     os.makedirs(targetdir)
   356                         util.copyfile(src, target)
   352                 util.copyfile(src, target)
   357                     restore = False
       
   358                 finally:
       
   359                     if restore:
       
   360                         repo.remove([abstarget])
       
   361             except IOError, inst:
   353             except IOError, inst:
   362                 if inst.errno == errno.ENOENT:
   354                 if inst.errno == errno.ENOENT:
   363                     ui.warn(_('%s: deleted in working copy\n') % relsrc)
   355                     ui.warn(_('%s: deleted in working copy\n') % relsrc)
   364                 else:
   356                 else:
   365                     ui.warn(_('%s: cannot copy - %s\n') %
   357                     ui.warn(_('%s: cannot copy - %s\n') %
   366                             (relsrc, inst.strerror))
   358                             (relsrc, inst.strerror))
   367                     return True # report a failure
   359                     return True # report a failure
   368 
   360 
   369         if ui.verbose or not exact:
   361         if ui.verbose or not exact:
   370             ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
   362             ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
       
   363 
   371         targets[abstarget] = abssrc
   364         targets[abstarget] = abssrc
   372 
   365 
   373         # fix up dirstate
   366         # fix up dirstate
   374         origsrc = repo.dirstate.copied(abssrc) or abssrc
   367         origsrc = repo.dirstate.copied(abssrc) or abssrc
   375         if abstarget == origsrc: # copying back a copy?
   368         if abstarget == origsrc: # copying back a copy?
   376             if repo.dirstate[abstarget] not in 'mn':
   369             if state not in 'mn' and not dryrun:
   377                 if not dryrun:
   370                 repo.dirstate.normallookup(abstarget)
   378                     repo.add([abstarget])
       
   379         else:
   371         else:
   380             if repo.dirstate[origsrc] == 'a':
   372             if repo.dirstate[origsrc] == 'a':
   381                 if not ui.quiet:
   373                 if not ui.quiet:
   382                     ui.warn(_("%s has not been committed yet, so no copy "
   374                     ui.warn(_("%s has not been committed yet, so no copy "
   383                               "data will be stored for %s.\n")
   375                               "data will be stored for %s.\n")