purge: fix b777dd8f7836 (remove read-only files)
authorPatrick Mezard <pmezard@gmail.com>
Sat, 11 Apr 2009 00:13:18 +0200
changeset 8044 c1e2b7407dc3
parent 8043 b777dd8f7836
child 8046 f3ef8a352d83
purge: fix b777dd8f7836 (remove read-only files) - use try/except to avoid unnecessary work - edit only mode bits
hgext/purge.py
tests/test-purge
--- a/hgext/purge.py	Fri Apr 10 21:20:28 2009 +0200
+++ b/hgext/purge.py	Sat Apr 11 00:13:18 2009 +0200
@@ -73,11 +73,15 @@
             ui.write('%s%s' % (name, eol))
 
     def removefile(path):
-        # read-only files cannot be unlinked under Windows
-        s = os.stat(path)
-        if (s.st_dev & stat.S_IWRITE) == 0:
-            os.chmod(path, s.st_mode | stat.S_IWRITE)
-        os.remove(path)
+        try:
+            os.remove(path)
+        except OSError:
+            # read-only files cannot be unlinked under Windows
+            s = os.stat(path)
+            if (s.st_mode & stat.S_IWRITE) != 0:
+                raise
+            os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE)
+            os.remove(path)
 
     directories = []
     match = cmdutil.match(repo, dirs, opts)
--- a/tests/test-purge	Fri Apr 10 21:20:28 2009 +0200
+++ b/tests/test-purge	Sat Apr 11 00:13:18 2009 +0200
@@ -38,7 +38,7 @@
 python <<EOF
 import os, stat
 f= 'untracked_file_readonly'
-os.chmod(f, os.stat(f).st_mode & ~stat.S_IWRITE)
+os.chmod(f, stat.S_IMODE(os.stat(f).st_mode) & ~stat.S_IWRITE)
 EOF
 hg purge -p
 hg purge -v