purge: remove read-only files under Windows (issue583)
authorPatrick Mezard <pmezard@gmail.com>
Fri, 10 Apr 2009 21:20:28 +0200
changeset 8043 b777dd8f7836
parent 8042 fd35e9d72879
child 8044 c1e2b7407dc3
purge: remove read-only files under Windows (issue583) Initial version by Benoit Boissinot <bboissin@gmail.com>
hgext/purge.py
tests/test-purge
tests/test-purge.out
--- a/hgext/purge.py	Fri Apr 10 13:06:02 2009 +0200
+++ b/hgext/purge.py	Fri Apr 10 21:20:28 2009 +0200
@@ -29,7 +29,7 @@
 
 from mercurial import util, commands, cmdutil
 from mercurial.i18n import _
-import os
+import os, stat
 
 def purge(ui, repo, *dirs, **opts):
     '''removes files not tracked by Mercurial
@@ -72,6 +72,13 @@
         else:
             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)
+
     directories = []
     match = cmdutil.match(repo, dirs, opts)
     match.dir = directories.append
@@ -79,7 +86,7 @@
 
     for f in util.sort(status[4] + status[5]):
         ui.note(_('Removing file %s\n') % f)
-        remove(os.remove, f)
+        remove(removefile, f)
 
     for f in util.sort(directories)[::-1]:
         if match(f) and not os.listdir(repo.wjoin(f)):
--- a/tests/test-purge	Fri Apr 10 13:06:02 2009 +0200
+++ b/tests/test-purge	Fri Apr 10 21:20:28 2009 +0200
@@ -34,6 +34,12 @@
 
 echo % delete an untracked file
 touch untracked_file
+touch untracked_file_readonly
+python <<EOF
+import os, stat
+f= 'untracked_file_readonly'
+os.chmod(f, os.stat(f).st_mode & ~stat.S_IWRITE)
+EOF
 hg purge -p
 hg purge -v
 ls
--- a/tests/test-purge.out	Fri Apr 10 13:06:02 2009 +0200
+++ b/tests/test-purge.out	Fri Apr 10 21:20:28 2009 +0200
@@ -15,7 +15,9 @@
 r1
 % delete an untracked file
 untracked_file
+untracked_file_readonly
 Removing file untracked_file
+Removing file untracked_file_readonly
 directory
 r1
 % delete an untracked file in a tracked directory