Patchwork [1,of,2] copyfile: allow optional hardlinking

login
register
mail settings
Submitter Pierre-Yves David
Date Jan. 17, 2015, 12:47 a.m.
Message ID <28ab98266efd77174436.1421455663@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/7506/
State Accepted
Commit 4e451d1359deb1ec236261721ff412eba1e168fb
Headers show

Comments

Pierre-Yves David - Jan. 17, 2015, 12:47 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1420490349 28800
#      Mon Jan 05 12:39:09 2015 -0800
# Node ID 28ab98266efd7717443607d4ecb30d036ef03452
# Parent  81349f4b47f4c793873290852808ff095ab24c00
copyfile: allow optional hardlinking

Some code paths use 'copyfiles' (full tree) for a single file to take advantage
of the best-effort-hard-linking parameter. We add similar parameter and logic
to 'copyfile' (single file) for this purpose.

The single file version have the advantage to overwrite the destination file if
it exists.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -714,14 +714,20 @@  def checksignature(func):
                 raise error.SignatureError
             raise
 
     return check
 
-def copyfile(src, dest):
+def copyfile(src, dest, hardlink=False):
     "copy a file, preserving mode and atime/mtime"
     if os.path.lexists(dest):
         unlink(dest)
+    if hardlink:
+        try:
+            oslink(src, dest)
+            return
+        except (IOError, OSError):
+            pass # fall back to normal copy
     if os.path.islink(src):
         os.symlink(os.readlink(src), dest)
     else:
         try:
             shutil.copyfile(src, dest)