refactor some unlink/remove code and make sure we prune empty dir
authorBenoit Boissinot <benoit.boissinot@ens-lyon.org>
Wed, 19 Oct 2005 00:10:52 -0700
changeset 1415 c6e6ca96a033
parent 1414 32fde51910c0
child 1416 19d2776f1725
refactor some unlink/remove code and make sure we prune empty dir
mercurial/commands.py
mercurial/localrepo.py
mercurial/util.py
--- a/mercurial/commands.py	Wed Oct 19 00:05:08 2005 -0700
+++ b/mercurial/commands.py	Wed Oct 19 00:10:52 2005 -0700
@@ -1441,12 +1441,7 @@
         if okaytoremove(abs, rel, exact):
             if ui.verbose or not exact: ui.status(_('removing %s\n') % rel)
             names.append(abs)
-    for name in names:
-        try:
-            os.unlink(name)
-        except OSError, inst:
-            if inst.errno != errno.ENOENT: raise
-    repo.remove(names)
+    repo.remove(names, unlink=True)
 
 def rename(ui, repo, *pats, **opts):
     """rename files; equivalent of copy + remove"""
@@ -1454,12 +1449,8 @@
     names = []
     for abs, rel, exact in copied:
         if ui.verbose or not exact: ui.status(_('removing %s\n') % rel)
-        try:
-            os.unlink(rel)
-        except OSError, inst:
-            if inst.errno != errno.ENOENT: raise
         names.append(abs)
-    repo.remove(names)
+    repo.remove(names, unlink=True)
     return errs
 
 def revert(ui, repo, *names, **opts):
--- a/mercurial/localrepo.py	Wed Oct 19 00:05:08 2005 -0700
+++ b/mercurial/localrepo.py	Wed Oct 19 00:10:52 2005 -0700
@@ -536,7 +536,13 @@
             else:
                 self.dirstate.forget([f])
 
-    def remove(self, list):
+    def remove(self, list, unlink=False):
+        if unlink:
+            for f in list:
+                try:
+                    util.unlink(self.wjoin(f))
+                except OSError, inst:
+                    if inst.errno != errno.ENOENT: raise
         for f in list:
             p = self.wjoin(f)
             if os.path.exists(p):
@@ -1264,14 +1270,11 @@
         for f in remove:
             self.ui.note(_("removing %s\n") % f)
             try:
-                os.unlink(self.wjoin(f))
+                util.unlink(self.wjoin(f))
             except OSError, inst:
                 if inst.errno != errno.ENOENT:
                     self.ui.warn(_("update failed to remove %s: %s!\n") %
                                  (f, inst.strerror))
-            # try removing directories that might now be empty
-            try: os.removedirs(os.path.dirname(self.wjoin(f)))
-            except: pass
         if moddirstate:
             if branch_merge:
                 self.dirstate.update(remove, 'r')
--- a/mercurial/util.py	Wed Oct 19 00:05:08 2005 -0700
+++ b/mercurial/util.py	Wed Oct 19 00:10:52 2005 -0700
@@ -310,6 +310,13 @@
         os.unlink(dst)
         os.rename(src, dst)
 
+def unlink(f):
+    """unlink and remove the directory if it is empty"""
+    os.unlink(f)
+    # try removing directories that might now be empty
+    try: os.removedirs(os.path.dirname(f))
+    except: pass
+
 def copyfiles(src, dst, hardlink=None):
     """Copy a directory tree using hardlinks if possible"""