Patchwork D12623: clone: use better names for temp files

login
register
mail settings
Submitter phabricator
Date May 10, 2022, 7:31 p.m.
Message ID <differential-rev-PHID-DREV-5fvbkvqmiokmyjohnymw-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50975/
State New
Headers show

Comments

phabricator - May 10, 2022, 7:31 p.m.
aalekseyev created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D12623

AFFECTED FILES
  mercurial/streamclone.py
  mercurial/util.py

CHANGE DETAILS




To: aalekseyev, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -2592,6 +2592,14 @@ 
             self.close()
 
 
+def tryrmdir(f):
+    try:
+        removedirs(f)
+    except OSError as e:
+        if e.errno != errno.ENOENT and e.errno != errno.ENOTEMPTY:
+            raise
+
+
 def unlinkpath(f, ignoremissing=False, rmdir=True):
     # type: (bytes, bool, bool) -> None
     """unlink and remove the directory if it is empty"""
diff --git a/mercurial/streamclone.py b/mercurial/streamclone.py
--- a/mercurial/streamclone.py
+++ b/mercurial/streamclone.py
@@ -558,13 +558,20 @@ 
 @contextlib.contextmanager
 def maketempcopies():
     """return a function to temporary copy file"""
+
     files = []
+    dst_dir = pycompat.mkdtemp(prefix=b'hg-clone-')
     try:
 
         def copy(src):
-            fd, dst = pycompat.mkstemp()
+            fd, dst = pycompat.mkstemp(
+                prefix=os.path.basename(src), dir=dst_dir
+            )
             os.close(fd)
             files.append(dst)
+            # hardlink=True doees nothing here because
+            # dst exists, and [copyfiles] doesn't overwrite
+            # existing files with hardlinks.
             util.copyfiles(src, dst, hardlink=True)
             return dst
 
@@ -572,6 +579,7 @@ 
     finally:
         for tmp in files:
             util.tryunlink(tmp)
+        util.tryrmdir(dst_dir)
 
 
 def _makemap(repo):