forget: add --confirm option
authorSushil khanchi <sushilkhanchi97@gmail.com>
Thu, 22 Mar 2018 16:11:42 +0530
changeset 37756 e7bf5a73e4e1
parent 37755 886754323bed
child 37757 2a8ad00b8aed
forget: add --confirm option Also added confirmopts in cmdutil.py Differential Revision: https://phab.mercurial-scm.org/D2934
hgext/largefiles/overrides.py
mercurial/cmdutil.py
mercurial/commands.py
mercurial/subrepo.py
tests/test-add.t
tests/test-completion.t
--- a/hgext/largefiles/overrides.py	Fri Apr 13 23:12:07 2018 -0400
+++ b/hgext/largefiles/overrides.py	Thu Mar 22 16:11:42 2018 +0530
@@ -1079,9 +1079,10 @@
     finally:
         repo.lfstatus = False
 
-def cmdutilforget(orig, ui, repo, match, prefix, explicitonly, dryrun):
+def cmdutilforget(orig, ui, repo, match, prefix, explicitonly, dryrun, confirm):
     normalmatcher = composenormalfilematcher(match, repo[None].manifest())
-    bad, forgot = orig(ui, repo, normalmatcher, prefix, explicitonly, dryrun)
+    bad, forgot = orig(ui, repo, normalmatcher, prefix, explicitonly, dryrun,
+                       confirm)
     m = composelargefilematcher(match, repo[None].manifest())
 
     try:
--- a/mercurial/cmdutil.py	Fri Apr 13 23:12:07 2018 -0400
+++ b/mercurial/cmdutil.py	Thu Mar 22 16:11:42 2018 +0530
@@ -63,6 +63,11 @@
      _('do not perform actions, just print output')),
 ]
 
+confirmopts = [
+    ('', 'confirm', None,
+     _('ask before applying actions')),
+]
+
 remoteopts = [
     ('e', 'ssh', '',
      _('specify ssh command to use'), _('CMD')),
@@ -2022,7 +2027,9 @@
         for subpath in ctx.substate:
             ctx.sub(subpath).addwebdirpath(serverpath, webconf)
 
-def forget(ui, repo, match, prefix, explicitonly, dryrun):
+def forget(ui, repo, match, prefix, explicitonly, dryrun, confirm):
+    if dryrun and confirm:
+        raise error.Abort(_("cannot specify both --dry-run and --confirm"))
     join = lambda f: os.path.join(prefix, f)
     bad = []
     badfn = lambda x, y: bad.append(x) or match.bad(x, y)
@@ -2038,7 +2045,8 @@
         sub = wctx.sub(subpath)
         try:
             submatch = matchmod.subdirmatcher(subpath, match)
-            subbad, subforgot = sub.forget(submatch, prefix, dryrun=dryrun)
+            subbad, subforgot = sub.forget(submatch, prefix,
+                                           dryrun=dryrun, confirm=confirm)
             bad.extend([subpath + '/' + f for f in subbad])
             forgot.extend([subpath + '/' + f for f in subforgot])
         except error.LookupError:
@@ -2061,8 +2069,34 @@
                                 % match.rel(f))
                     bad.append(f)
 
+    if confirm:
+        responses = _('[Ynsa?]'
+                      '$$ &Yes, forget this file'
+                      '$$ &No, skip this file'
+                      '$$ &Skip remaining files'
+                      '$$ Include &all remaining files'
+                      '$$ &? (display help)')
+        for filename in forget[:]:
+            r = ui.promptchoice(_('forget %s %s') % (filename, responses))
+            if r == 4: # ?
+                while r == 4:
+                    for c, t in ui.extractchoices(responses)[1]:
+                        ui.write('%s - %s\n' % (c, encoding.lower(t)))
+                    r = ui.promptchoice(_('forget %s %s') % (filename,
+                                                                 responses))
+            if r == 0: # yes
+                continue
+            elif r == 1: # no
+                forget.remove(filename)
+            elif r == 2: # Skip
+                fnindex = forget.index(filename)
+                del forget[fnindex:]
+                break
+            elif r == 3: # All
+                break
+
     for f in forget:
-        if ui.verbose or not match.exact(f):
+        if ui.verbose or not match.exact(f) or confirm:
             ui.status(_('removing %s\n') % match.rel(f))
 
     if not dryrun:
--- a/mercurial/commands.py	Fri Apr 13 23:12:07 2018 -0400
+++ b/mercurial/commands.py	Thu Mar 22 16:11:42 2018 +0530
@@ -112,6 +112,7 @@
 ]
 
 dryrunopts = cmdutil.dryrunopts
+confirmopts = cmdutil.confirmopts
 remoteopts = cmdutil.remoteopts
 walkopts = cmdutil.walkopts
 commitopts = cmdutil.commitopts
@@ -2060,7 +2061,7 @@
 
 @command(
     '^forget',
-    walkopts + dryrunopts,
+    walkopts + dryrunopts + confirmopts,
     _('[OPTION]... FILE...'), inferrepo=True)
 def forget(ui, repo, *pats, **opts):
     """forget the specified files on the next commit
@@ -2096,9 +2097,10 @@
         raise error.Abort(_('no files specified'))
 
     m = scmutil.match(repo[None], pats, opts)
-    dryrun = opts.get('dry_run')
+    dryrun, confirm = opts.get('dry_run'), opts.get('confirm')
     rejected = cmdutil.forget(ui, repo, m, prefix="",
-                              explicitonly=False, dryrun=dryrun)[0]
+                              explicitonly=False, dryrun=dryrun,
+                              confirm=confirm)[0]
     return rejected and 1 or 0
 
 @command(
--- a/mercurial/subrepo.py	Fri Apr 13 23:12:07 2018 -0400
+++ b/mercurial/subrepo.py	Thu Mar 22 16:11:42 2018 +0530
@@ -352,7 +352,7 @@
         matched by the match function
         '''
 
-    def forget(self, match, prefix, dryrun):
+    def forget(self, match, prefix, dryrun, confirm):
         return ([], [])
 
     def removefiles(self, matcher, prefix, after, force, subrepos,
@@ -815,10 +815,10 @@
         return ctx.walk(match)
 
     @annotatesubrepoerror
-    def forget(self, match, prefix, dryrun):
+    def forget(self, match, prefix, dryrun, confirm):
         return cmdutil.forget(self.ui, self._repo, match,
                               self.wvfs.reljoin(prefix, self._path),
-                              True, dryrun=dryrun)
+                              True, dryrun=dryrun, confirm=confirm)
 
     @annotatesubrepoerror
     def removefiles(self, matcher, prefix, after, force, subrepos,
--- a/tests/test-add.t	Fri Apr 13 23:12:07 2018 -0400
+++ b/tests/test-add.t	Thu Mar 22 16:11:42 2018 +0530
@@ -272,3 +272,58 @@
   [1]
 
   $ cd ..
+
+test --confirm option in forget
+
+  $ hg init forgetconfirm
+  $ cd forgetconfirm
+  $ echo foo > foo
+  $ hg commit -qAm "foo"
+  $ echo bar > bar
+  $ hg commit -qAm "bar"
+  $ hg forget foo --dry-run --confirm
+  abort: cannot specify both --dry-run and --confirm
+  [255]
+
+  $ hg forget foo --config ui.interactive=True --confirm << EOF
+  > ?
+  > n
+  > EOF
+  forget foo [Ynsa?] ?
+  y - yes, forget this file
+  n - no, skip this file
+  s - skip remaining files
+  a - include all remaining files
+  ? - ? (display help)
+  forget foo [Ynsa?] n
+
+  $ hg forget foo bar --config ui.interactive=True --confirm << EOF
+  > y
+  > n
+  > EOF
+  forget bar [Ynsa?] y
+  forget foo [Ynsa?] n
+  removing bar
+  $ hg status
+  R bar
+  $ hg up -qC .
+
+  $ hg forget foo bar --config ui.interactive=True --confirm << EOF
+  > s
+  > EOF
+  forget bar [Ynsa?] s
+  $ hg st
+  $ hg up -qC .
+
+  $ hg forget foo bar --config ui.interactive=True --confirm << EOF
+  > a
+  > EOF
+  forget bar [Ynsa?] a
+  removing bar
+  removing foo
+  $ hg status
+  R bar
+  R foo
+  $ hg up -qC .
+
+  $ cd ..
--- a/tests/test-completion.t	Fri Apr 13 23:12:07 2018 -0400
+++ b/tests/test-completion.t	Thu Mar 22 16:11:42 2018 +0530
@@ -232,7 +232,7 @@
   commit: addremove, close-branch, amend, secret, edit, interactive, include, exclude, message, logfile, date, user, subrepos
   diff: rev, change, text, git, binary, nodates, noprefix, show-function, reverse, ignore-all-space, ignore-space-change, ignore-blank-lines, ignore-space-at-eol, unified, stat, root, include, exclude, subrepos
   export: output, switch-parent, rev, text, git, binary, nodates, template
-  forget: include, exclude, dry-run
+  forget: include, exclude, dry-run, confirm
   init: ssh, remotecmd, insecure
   log: follow, follow-first, date, copies, keyword, rev, line-range, removed, only-merges, user, only-branch, branch, prune, patch, git, limit, no-merges, stat, graph, style, template, include, exclude
   merge: force, rev, preview, abort, tool