Patchwork D9818: purge: add a --confirm option

login
register
mail settings
Submitter phabricator
Date Jan. 18, 2021, 9:26 a.m.
Message ID <differential-rev-PHID-DREV-wrwpvn7lxia6soyltypc-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48129/
State Superseded
Headers show

Comments

phabricator - Jan. 18, 2021, 9:26 a.m.
marmoute created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9818

AFFECTED FILES
  hgext/purge.py
  mercurial/merge.py
  tests/test-purge.t

CHANGE DETAILS




To: marmoute, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-purge.t b/tests/test-purge.t
--- a/tests/test-purge.t
+++ b/tests/test-purge.t
@@ -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
diff --git a/mercurial/merge.py b/mercurial/merge.py
--- a/mercurial/merge.py
+++ b/mercurial/merge.py
@@ -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:
diff --git a/hgext/purge.py b/hgext/purge.py
--- a/hgext/purge.py
+++ b/hgext/purge.py
@@ -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: