[PATCH] purge: add --ignored-only/-i option to delete only ignored files

Greg Ward greg at gerg.ca
Tue Mar 20 20:50:51 CDT 2012


# HG changeset patch
# User Greg Ward <greg at gerg.ca>
# Date 1332294424 14400
# Node ID bfde5bbbc3bb0d804a70cad737f0bf95086a448c
# Parent  12e3f93b1cbc7c930e941f0adfe632c8ad70b73d
purge: add --ignored-only/-i option to delete only ignored files

This is handy when you trust .hgignore to specify all of your build
products, but you do not trust yourself to always remember to "hg add"
new files. This treats unknown files as valuable, since they might
become added files soon.

diff --git a/hgext/purge.py b/hgext/purge.py
--- a/hgext/purge.py
+++ b/hgext/purge.py
@@ -34,6 +34,7 @@
 @command('purge|clean',
     [('a', 'abort-on-err', None, _('abort if an error occurs')),
     ('',  'all', None, _('purge ignored files too')),
+    ('i',  'ignored-only', None, _('purge only ignored files (not unknown)')),
     ('p', 'print', None, _('print filenames instead of deleting them')),
     ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
                             ' (implies -p/--print)')),
@@ -45,7 +46,7 @@
     Delete files not known to Mercurial. This is useful to test local
     and uncommitted changes in an otherwise-clean source tree.
 
-    This means that purge will delete:
+    By default, purge will delete:
 
     - Unknown files: files marked with "?" by :hg:`status`
     - Empty directories: in fact Mercurial ignores directories unless
@@ -57,6 +58,11 @@
     - Ignored files (unless --all is specified)
     - New files added to the repository (with :hg:`add`)
 
+    With `--all`, purge will remove both unknown and ignored files.
+
+    With `--ignored-only`, purge will instead remove only ignored
+    files, leaving unknown files alone.
+
     If directories are given on the command line, only files in these
     directories are considered.
 
@@ -97,7 +103,9 @@
     directories = []
     match = scmutil.match(repo[None], dirs, opts)
     match.dir = directories.append
-    status = repo.status(match=match, ignored=opts['all'], unknown=True)
+    ignored = (opts['all'] or opts['ignored_only'])
+    unknown = not opts['ignored_only']
+    status = repo.status(match=match, ignored=ignored, unknown=unknown)
 
     for f in sorted(status[4] + status[5]):
         ui.note(_('Removing file %s\n') % f)
diff --git a/tests/test-purge.t b/tests/test-purge.t
--- a/tests/test-purge.t
+++ b/tests/test-purge.t
@@ -136,6 +136,35 @@
   directory
   r1
 
+delete only ignored files
+  $ touch ignored untracked directory/untracked
+  $ hg purge -p --ignored-only
+  ignored
+  $ hg purge -v --ignored-only
+  Removing file ignored
+  $ ls
+  directory
+  r1
+  untracked
+  $ hg status -ui
+  ? directory/untracked
+  ? untracked
+
+delete only files in an ignored directory
+  $ mkdir ignored
+  $ touch ignored/stuff1 ignored/stuff2 
+  $ hg purge -p -i
+  ignored/stuff1
+  ignored/stuff2
+  $ hg purge -v -i
+  Removing file ignored/stuff1
+  Removing file ignored/stuff2
+  Removing directory ignored
+  $ hg status -ui
+  ? directory/untracked
+  ? untracked
+  $ hg purge
+
 abort with missing files until we support name mangling filesystems
 
   $ touch untracked_file


More information about the Mercurial-devel mailing list