# HG changeset patch # User Matt Mackall # Date 1163446017 21600 # Node ID 4cfb72bcb9789f4d3b61389b457e7b4f65da02e9 # Parent dc3504af7722a6c52f4d33a78093215f3021af6c util: add copyfile function diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -8,7 +8,7 @@ from demandload import demandload from node import * from i18n import gettext as _ -demandload(globals(), "os re sys signal shutil imp urllib pdb shlex") +demandload(globals(), "os re sys signal imp urllib pdb shlex") demandload(globals(), "fancyopts ui hg util lock revlog templater bundlerepo") demandload(globals(), "difflib patch tempfile time") demandload(globals(), "traceback errno version atexit sets bz2") @@ -1004,14 +1004,11 @@ def docopy(ui, repo, pats, opts, wlock): repo.undelete([abstarget], wlock) try: if not opts.get('dry_run'): - shutil.copyfile(relsrc, reltarget) - shutil.copymode(relsrc, reltarget) + util.copyfile(relsrc, reltarget) restore = False finally: if restore: repo.remove([abstarget], wlock) - except shutil.Error, inst: - raise util.Abort(str(inst)) except IOError, inst: if inst.errno == errno.ENOENT: ui.warn(_('%s: deleted in working copy\n') % relsrc) @@ -2419,8 +2416,7 @@ def revert(ui, repo, *pats, **opts): ui.note(_('saving current version of %s as %s\n') % (rel, bakname)) if not opts.get('dry_run'): - shutil.copyfile(rel, bakname) - shutil.copymode(rel, bakname) + util.copyfile(rel, bakname) if ui.verbose or not exact: ui.status(xlist[1] % rel) for table, hitlist, misslist, backuphit, backupmiss in disptable: diff --git a/mercurial/patch.py b/mercurial/patch.py --- a/mercurial/patch.py +++ b/mercurial/patch.py @@ -26,11 +26,8 @@ def copyfile(src, dst, basedir=None): targetdir = os.path.dirname(absdst) if not os.path.isdir(targetdir): os.makedirs(targetdir) - try: - shutil.copyfile(abssrc, absdst) - shutil.copymode(abssrc, absdst) - except shutil.Error, inst: - raise util.Abort(str(inst)) + + util.copyfile(abssrc, absdst) # public functions diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -460,6 +460,14 @@ def unlink(f): except OSError: pass +def copyfile(src, dest): + "copy a file, preserving mode" + try: + shutil.copyfile(src, dest) + shutil.copymode(src, dest) + except shutil.Error, inst: + raise util.Abort(str(inst)) + def copyfiles(src, dst, hardlink=None): """Copy a directory tree using hardlinks if possible"""