[PATCH 03 of 14] util: unify unlinkpath

Ryan McElroy rm at fb.com
Mon Mar 20 22:10:46 EDT 2017


# HG changeset patch
# User Ryan McElroy <rmcelroy at fb.com>
# Date 1490059858 25200
#      Mon Mar 20 18:30:58 2017 -0700
# Node ID d35d6cde9c2e428a7e77f35d2cd745696bec072a
# Parent  14339b2761ba49e914ec2be4446e2005ce024e8a
util: unify unlinkpath

Previously, there were two slightly different versions of unlinkpath between
windows and posix, but these differences were eliminated in previous patches.
Now we can unify these two code paths inside of the util module.

diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -533,19 +533,6 @@ def gethgcmd():
 def makedir(path, notindexed):
     os.mkdir(path)
 
-def unlinkpath(f, ignoremissing=False):
-    """unlink and remove the directory if it is empty"""
-    try:
-        unlink(f)
-    except OSError as e:
-        if not (ignoremissing and e.errno == errno.ENOENT):
-            raise
-    # try removing directories that might now be empty
-    try:
-        removedirs(os.path.dirname(f))
-    except OSError:
-        pass
-
 def lookupreg(key, name=None, scope=None):
     return None
 
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -136,7 +136,6 @@ statislink = platform.statislink
 testpid = platform.testpid
 umask = platform.umask
 unlink = platform.unlink
-unlinkpath = platform.unlinkpath
 username = platform.username
 
 # Python compatibility
@@ -1605,6 +1604,19 @@ class atomictempfile(object):
         else:
             self.close()
 
+def unlinkpath(f, ignoremissing=False):
+    """unlink and remove the directory if it is empty"""
+    try:
+        unlink(f)
+    except OSError as e:
+        if not (ignoremissing and e.errno == errno.ENOENT):
+            raise
+    # try removing directories that might now be empty
+    try:
+        removedirs(os.path.dirname(f))
+    except OSError:
+        pass
+
 def makedirs(name, mode=None, notindexed=False):
     """recursive directory creation with parent mode inheritance
 
diff --git a/mercurial/windows.py b/mercurial/windows.py
--- a/mercurial/windows.py
+++ b/mercurial/windows.py
@@ -385,19 +385,6 @@ def removedirs(name):
             break
         head, tail = os.path.split(head)
 
-def unlinkpath(f, ignoremissing=False):
-    """unlink and remove the directory if it is empty"""
-    try:
-        unlink(f)
-    except OSError as e:
-        if not (ignoremissing and e.errno == errno.ENOENT):
-            raise
-    # try removing directories that might now be empty
-    try:
-        removedirs(os.path.dirname(f))
-    except OSError:
-        pass
-
 def rename(src, dst):
     '''atomically rename file src to dst, replacing dst if it exists'''
     try:


More information about the Mercurial-devel mailing list