D2934: forget: add --confirm option

khanchi97 (Sushil khanchi) phabricator at mercurial-scm.org
Mon Apr 16 22:36:40 EDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rHGe7bf5a73e4e1: forget: add --confirm option (authored by khanchi97, committed by ).

CHANGED PRIOR TO COMMIT
  https://phab.mercurial-scm.org/D2934?vs=7285&id=8362#toc

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D2934?vs=7285&id=8362

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

AFFECTED FILES
  hgext/largefiles/overrides.py
  mercurial/cmdutil.py
  mercurial/commands.py
  mercurial/subrepo.py
  tests/test-add.t
  tests/test-completion.t

CHANGE DETAILS

diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -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
diff --git a/tests/test-add.t b/tests/test-add.t
--- a/tests/test-add.t
+++ b/tests/test-add.t
@@ -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 ..
diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -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,
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -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(
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -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:
diff --git a/hgext/largefiles/overrides.py b/hgext/largefiles/overrides.py
--- a/hgext/largefiles/overrides.py
+++ b/hgext/largefiles/overrides.py
@@ -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:



To: khanchi97, #hg-reviewers, av6, pulkit, durin42
Cc: durin42, mharbison72, yuja, pulkit, av6, mercurial-devel


More information about the Mercurial-devel mailing list