purge: add -i flag to delete ignored files instead of untracked files
authorValentin Gatien-Baron <valentin.gatienbaron@gmail.com>
Sat, 08 Feb 2020 10:22:47 -0500
changeset 44289 9f8eddd2723f
parent 44288 cd8f248fead4
child 44290 d8b53385b1bc
purge: add -i flag to delete ignored files instead of untracked files It's convenient for deleting build artifacts. Using --all instead would delete other things too. Differential Revision: https://phab.mercurial-scm.org/D8096
hgext/purge.py
mercurial/merge.py
relnotes/next
tests/test-purge.t
--- a/hgext/purge.py	Thu Jan 30 19:50:43 2020 -0500
+++ b/hgext/purge.py	Sat Feb 08 10:22:47 2020 -0500
@@ -48,6 +48,7 @@
     [
         (b'a', b'abort-on-err', None, _(b'abort if an error occurs')),
         (b'', b'all', None, _(b'purge ignored files too')),
+        (b'i', b'ignored', None, _(b'purge only ignored files')),
         (b'', b'dirs', None, _(b'purge empty directories')),
         (b'', b'files', None, _(b'purge files')),
         (b'p', b'print', None, _(b'print filenames instead of deleting them')),
@@ -80,7 +81,7 @@
     But it will leave untouched:
 
     - Modified and unmodified tracked files
-    - Ignored files (unless --all is specified)
+    - Ignored files (unless -i or --all is specified)
     - New files added to the repository (with :hg:`add`)
 
     The --files and --dirs options can be used to direct purge to delete
@@ -96,12 +97,19 @@
     option.
     '''
     opts = pycompat.byteskwargs(opts)
+    cmdutil.check_at_most_one_arg(opts, b'all', b'ignored')
 
     act = not opts.get(b'print')
     eol = b'\n'
     if opts.get(b'print0'):
         eol = b'\0'
         act = False  # --print0 implies --print
+    if opts.get(b'all', False):
+        ignored = True
+        unknown = True
+    else:
+        ignored = opts.get(b'ignored', False)
+        unknown = not ignored
 
     removefiles = opts.get(b'files')
     removedirs = opts.get(b'dirs')
@@ -115,7 +123,8 @@
     paths = mergemod.purge(
         repo,
         match,
-        ignored=opts.get(b'all', False),
+        unknown=unknown,
+        ignored=ignored,
         removeemptydirs=removedirs,
         removefiles=removefiles,
         abortonerror=opts.get(b'abort_on_err'),
--- a/mercurial/merge.py	Thu Jan 30 19:50:43 2020 -0500
+++ b/mercurial/merge.py	Sat Feb 08 10:22:47 2020 -0500
@@ -2698,6 +2698,7 @@
 def purge(
     repo,
     matcher,
+    unknown=True,
     ignored=False,
     removeemptydirs=True,
     removefiles=True,
@@ -2709,7 +2710,9 @@
     ``matcher`` is a matcher configured to scan the working directory -
     potentially a subset.
 
-    ``ignored`` controls whether ignored files should also be purged.
+    ``unknown`` controls whether unknown files should be purged.
+
+    ``ignored`` controls whether ignored files should be purged.
 
     ``removeemptydirs`` controls whether empty directories should be removed.
 
@@ -2746,7 +2749,7 @@
             directories = []
             matcher.traversedir = directories.append
 
-        status = repo.status(match=matcher, ignored=ignored, unknown=True)
+        status = repo.status(match=matcher, ignored=ignored, unknown=unknown)
 
         if removefiles:
             for f in sorted(status.unknown + status.ignored):
--- a/relnotes/next	Thu Jan 30 19:50:43 2020 -0500
+++ b/relnotes/next	Sat Feb 08 10:22:47 2020 -0500
@@ -1,5 +1,7 @@
 == New Features ==
 
+ * `hg purge`/`hg clean` can now delete ignored files instead of
+   untracked files, with the new -i flag.
 
 == New Experimental Features ==
 
--- a/tests/test-purge.t	Thu Jan 30 19:50:43 2020 -0500
+++ b/tests/test-purge.t	Sat Feb 08 10:22:47 2020 -0500
@@ -120,19 +120,32 @@
   directory/untracked_file
   $ rm directory/untracked_file
 
-skip ignored files if --all not specified
+skip ignored files if -i or --all not specified
 
   $ touch ignored
   $ hg purge -p
   $ hg purge -v
+  $ touch untracked_file
   $ ls
   directory
   ignored
   r1
+  untracked_file
+  $ hg purge -p -i
+  ignored
+  $ hg purge -v -i
+  removing file ignored
+  $ ls
+  directory
+  r1
+  untracked_file
+  $ touch ignored
   $ hg purge -p --all
   ignored
+  untracked_file
   $ hg purge -v --all
   removing file ignored
+  removing file untracked_file
   $ ls
   directory
   r1