[PATCH] purge: add --include and --exclude options

Emanuele Aina emanuele.aina at tiscali.it
Sun May 20 09:14:37 UTC 2007


# HG changeset patch
# User Emanuele Aina <em at nerd.ocracy.org>
# Date 1178895944 -7200
# Node ID f97c5a7ffbe33cedc80b8515153ca7e52fb25946
# Parent  ba22e867cb23fb11f83c37705743e050afd66a6a
purge: add --include and --exclude options

diff -r ba22e867cb23 -r f97c5a7ffbe3 hgext/purge.py
--- a/hgext/purge.py	Mon May 07 21:44:11 2007 +0900
+++ b/hgext/purge.py	Fri May 11 17:05:44 2007 +0200
@@ -32,7 +32,7 @@ import os
  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 @@ def dopurge(ui, repo, dirs=None, act=Tru
      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 @@ def dopurge(ui, repo, dirs=None, act=Tru
              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 @@ def purge(ui, repo, *dirs, **opts):
          # --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 @@ cmdtable = {
            ('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]...'))
  }
diff -r ba22e867cb23 -r f97c5a7ffbe3 tests/test-purge
--- a/tests/test-purge	Mon May 07 21:44:11 2007 +0900
+++ b/tests/test-purge	Fri May 11 17:05:44 2007 +0200
@@ -97,3 +97,34 @@ hg purge -v --force
  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
diff -r ba22e867cb23 -r f97c5a7ffbe3 tests/test-purge.out
--- a/tests/test-purge.out	Mon May 07 21:44:11 2007 +0900
+++ b/tests/test-purge.out	Fri May 11 17:05:44 2007 +0200
@@ -56,3 +56,17 @@ Removing file untracked_file
  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






More information about the Mercurial-devel mailing list