purge: add --include and --exclude options
authorEmanuele Aina <em@nerd.ocracy.org>
Fri, 11 May 2007 17:05:44 +0200
changeset 4463 a73cf208b2a0
parent 4462 12e4d9524951
child 4470 b593dcd96367
purge: add --include and --exclude options
hgext/purge.py
tests/test-purge
tests/test-purge.out
--- a/hgext/purge.py	Tue May 29 16:42:05 2007 +0200
+++ b/hgext/purge.py	Fri May 11 17:05:44 2007 +0200
@@ -32,7 +32,7 @@
 import os
 
 def dopurge(ui, repo, dirs=None, act=True, abort_on_err=False, eol='\n',
-            force=False):
+            force=False, include=None, exclude=None):
     def error(msg):
         if abort_on_err:
             raise util.Abort(msg)
@@ -51,7 +51,8 @@
     directories = []
     files = []
     missing = []
-    roots, match, anypats = util.cmdmatcher(repo.root, repo.getcwd(), dirs)
+    roots, match, anypats = util.cmdmatcher(repo.root, repo.getcwd(), dirs,
+                                            include, exclude)
     for src, f, st in repo.dirstate.statwalk(files=roots, match=match,
                                              ignored=True, directories=True):
         if src == 'd':
@@ -71,7 +72,7 @@
             remove(os.remove, f)
 
     for f in directories[::-1]:
-        if not os.listdir(repo.wjoin(f)):
+        if match(f) and not os.listdir(repo.wjoin(f)):
             ui.note(_('Removing directory %s\n') % f)
             remove(os.rmdir, f)
 
@@ -144,7 +145,9 @@
         # --print0 implies --print
         act = False
     force = bool(opts['force'])
-    dopurge(ui, repo, dirs, act, abort_on_err, eol, force)
+    include = opts['include']
+    exclude = opts['exclude']
+    dopurge(ui, repo, dirs, act, abort_on_err, eol, force, include, exclude)
 
 
 cmdtable = {
@@ -154,6 +157,8 @@
           ('f', 'force', None, _('purge even when missing files are detected')),
           ('p', 'print', None, _('print the file names instead of deleting them')),
           ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
-                                  ' (implies -p)'))],
+                                  ' (implies -p)')),
+          ('I', 'include', [], _('include names matching the given patterns')),
+          ('X', 'exclude', [], _('exclude names matching the given patterns'))],
          _('hg purge [OPTION]... [DIR]...'))
 }
--- a/tests/test-purge	Tue May 29 16:42:05 2007 +0200
+++ b/tests/test-purge	Fri May 11 17:05:44 2007 +0200
@@ -97,3 +97,34 @@
 hg purge -v --force
 hg revert --all --quiet
 ls
+
+echo % skip excluded files
+touch excluded_file
+hg purge -p -X excluded_file
+hg purge -v -X excluded_file
+ls
+rm excluded_file
+
+echo % skip files in excluded dirs
+mkdir excluded_dir
+touch excluded_dir/file
+hg purge -p -X excluded_dir
+hg purge -v -X excluded_dir
+ls
+ls excluded_dir
+rm -R excluded_dir
+
+echo % skip excluded empty dirs
+mkdir excluded_dir
+hg purge -p -X excluded_dir
+hg purge -v -X excluded_dir
+ls
+rmdir excluded_dir
+
+echo % skip patterns
+mkdir .svn
+touch .svn/foo
+mkdir directory/.svn
+touch directory/.svn/foo
+hg purge -p -X .svn -X '*/.svn'
+hg purge -p -X re:.*.svn
--- a/tests/test-purge.out	Tue May 29 16:42:05 2007 +0200
+++ b/tests/test-purge.out	Fri May 11 17:05:44 2007 +0200
@@ -56,3 +56,17 @@
 Removing file untracked_file
 directory
 r1
+% skip excluded files
+directory
+excluded_file
+r1
+% skip files in excluded dirs
+directory
+excluded_dir
+r1
+file
+% skip excluded empty dirs
+directory
+excluded_dir
+r1
+% skip patterns