purge: add a --confirm option
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 18 Jan 2021 10:24:03 +0100
changeset 46366 135056e8b5a8
parent 46365 63dfaca9087f
child 46367 57370e7deb7b
purge: add a --confirm option The options provide a prompt to the user before permanent deletion are made. The prompt is currently not aware of directory deletion. I'll fix this in the next changesets. Differential Revision: https://phab.mercurial-scm.org/D9818
hgext/purge.py
mercurial/merge.py
tests/test-purge.t
--- a/hgext/purge.py	Mon May 11 18:45:45 2020 -0400
+++ b/hgext/purge.py	Mon Jan 18 10:24:03 2021 +0100
@@ -61,6 +61,7 @@
                 b' (implies -p/--print)'
             ),
         ),
+        (b'', b'confirm', None, _(b'ask before permanently deleting files')),
     ]
     + cmdutil.walkopts,
     _(b'hg purge [OPTION]... [DIR]...'),
@@ -113,6 +114,7 @@
 
     removefiles = opts.get(b'files')
     removedirs = opts.get(b'dirs')
+    confirm = opts.get(b'confirm')
 
     if not removefiles and not removedirs:
         removefiles = True
@@ -129,6 +131,7 @@
         removefiles=removefiles,
         abortonerror=opts.get(b'abort_on_err'),
         noop=not act,
+        confirm=confirm,
     )
 
     for path in paths:
--- a/mercurial/merge.py	Mon May 11 18:45:45 2020 -0400
+++ b/mercurial/merge.py	Mon Jan 18 10:24:03 2021 +0100
@@ -2324,6 +2324,7 @@
     removefiles=True,
     abortonerror=False,
     noop=False,
+    confirm=False,
 ):
     """Purge the working directory of untracked files.
 
@@ -2344,6 +2345,8 @@
     ``noop`` controls whether to actually remove files. If not defined, actions
     will be taken.
 
+    ``confirm`` ask confirmation before actually removing anything.
+
     Returns an iterable of relative paths in the working directory that were
     or would be removed.
     """
@@ -2371,6 +2374,25 @@
 
         status = repo.status(match=matcher, ignored=ignored, unknown=unknown)
 
+        if confirm:
+            nb_ignored = len(status.ignored)
+            nb_unkown = len(status.unknown)
+            if nb_unkown and nb_ignored:
+                msg = _(b"permanently delete %d unkown and %d ignored files?")
+                msg %= (nb_unkown, nb_ignored)
+            elif nb_unkown:
+                msg = _(b"permanently delete %d unkown files?")
+                msg %= nb_unkown
+            elif nb_ignored:
+                msg = _(b"permanently delete %d ignored files?")
+                msg %= nb_ignored
+            else:
+                # XXX we might be missing directory there
+                return res
+            msg += b" (yN)$$ &Yes $$ &No"
+            if repo.ui.promptchoice(msg, default=1) == 1:
+                raise error.CanceledError(_(b'removal cancelled'))
+
         if removefiles:
             for f in sorted(status.unknown + status.ignored):
                 if not noop:
--- a/tests/test-purge.t	Mon May 11 18:45:45 2020 -0400
+++ b/tests/test-purge.t	Mon Jan 18 10:24:03 2021 +0100
@@ -62,6 +62,10 @@
   $ hg purge -p
   untracked_file
   untracked_file_readonly
+  $ hg purge --confirm
+  permanently delete 2 unkown files? (yN) n
+  abort: removal cancelled
+  [250]
   $ hg purge -v
   removing file untracked_file
   removing file untracked_file_readonly
@@ -121,6 +125,10 @@
   $ cd directory
   $ hg purge -p ../untracked_directory
   untracked_directory/nested_directory
+  $ hg purge --confirm
+  permanently delete 1 unkown files? (yN) n
+  abort: removal cancelled
+  [250]
   $ hg purge -v ../untracked_directory
   removing directory untracked_directory/nested_directory
   removing directory untracked_directory
@@ -138,6 +146,7 @@
 
   $ touch ignored
   $ hg purge -p
+  $ hg purge --confirm
   $ hg purge -v
   $ touch untracked_file
   $ ls
@@ -147,6 +156,10 @@
   untracked_file
   $ hg purge -p -i
   ignored
+  $ hg purge --confirm -i
+  permanently delete 1 ignored files? (yN) n
+  abort: removal cancelled
+  [250]
   $ hg purge -v -i
   removing file ignored
   $ ls -A
@@ -159,6 +172,10 @@
   $ hg purge -p --all
   ignored
   untracked_file
+  $ hg purge --confirm --all
+  permanently delete 1 unkown and 1 ignored files? (yN) n
+  abort: removal cancelled
+  [250]
   $ hg purge -v --all
   removing file ignored
   removing file untracked_file