[PATCH V4] forget: add --dry-run mode

Sushil khanchi sushilkhanchi97 at gmail.com
Tue Mar 13 05:34:28 EDT 2018


# HG changeset patch
# User Sushil khanchi <sushilkhanchi97 at gmail.com>
# Date 1520665399 -19800
#      Sat Mar 10 12:33:19 2018 +0530
# Node ID 349b51e3bb04862ff7e516f57509ccd974e1b6a2
# Parent  4c71a26a4009d88590c9ae3d64a5912fd556d82e
forget: add --dry-run mode

diff -r 4c71a26a4009 -r 349b51e3bb04 mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Sun Mar 04 21:16:36 2018 -0500
+++ b/mercurial/cmdutil.py	Sat Mar 10 12:33:19 2018 +0530
@@ -1996,7 +1996,7 @@
         for subpath in ctx.substate:
             ctx.sub(subpath).addwebdirpath(serverpath, webconf)
 
-def forget(ui, repo, match, prefix, explicitonly):
+def forget(ui, repo, match, prefix, explicitonly, dryrun):
     join = lambda f: os.path.join(prefix, f)
     bad = []
     badfn = lambda x, y: bad.append(x) or match.bad(x, y)
@@ -2012,7 +2012,7 @@
         sub = wctx.sub(subpath)
         try:
             submatch = matchmod.subdirmatcher(subpath, match)
-            subbad, subforgot = sub.forget(submatch, prefix)
+            subbad, subforgot = sub.forget(submatch, prefix, dryrun=dryrun)
             bad.extend([subpath + '/' + f for f in subbad])
             forgot.extend([subpath + '/' + f for f in subforgot])
         except error.LookupError:
@@ -2039,7 +2039,7 @@
         if ui.verbose or not match.exact(f):
             ui.status(_('removing %s\n') % match.rel(f))
 
-    rejected = wctx.forget(forget, prefix)
+    rejected = wctx.forget(forget, prefix, dryrun=dryrun)
     bad.extend(f for f in rejected if f in match.files())
     forgot.extend(f for f in forget if f not in rejected)
     return bad, forgot
diff -r 4c71a26a4009 -r 349b51e3bb04 mercurial/commands.py
--- a/mercurial/commands.py	Sun Mar 04 21:16:36 2018 -0500
+++ b/mercurial/commands.py	Sat Mar 10 12:33:19 2018 +0530
@@ -2036,7 +2036,10 @@
     with ui.formatter('files', opts) as fm:
         return cmdutil.files(ui, ctx, m, fm, fmt, opts.get('subrepos'))
 
- at command('^forget', walkopts, _('[OPTION]... FILE...'), inferrepo=True)
+ at command(
+    '^forget',
+    walkopts + dryrunopts,
+    _('[OPTION]... FILE...'), inferrepo=True)
 def forget(ui, repo, *pats, **opts):
     """forget the specified files on the next commit
 
@@ -2071,7 +2074,9 @@
         raise error.Abort(_('no files specified'))
 
     m = scmutil.match(repo[None], pats, opts)
-    rejected = cmdutil.forget(ui, repo, m, prefix="", explicitonly=False)[0]
+    dryrun = opts.get(r'dry_run')
+    rejected = cmdutil.forget(ui, repo, m, prefix="",
+                              explicitonly=False, dryrun=dryrun)[0]
     return rejected and 1 or 0
 
 @command(
diff -r 4c71a26a4009 -r 349b51e3bb04 mercurial/context.py
--- a/mercurial/context.py	Sun Mar 04 21:16:36 2018 -0500
+++ b/mercurial/context.py	Sat Mar 10 12:33:19 2018 +0530
@@ -1564,7 +1564,7 @@
                     ds.add(f)
             return rejected
 
-    def forget(self, files, prefix=""):
+    def forget(self, files, prefix="", dryrun=False):
         with self._repo.wlock():
             ds = self._repo.dirstate
             uipath = lambda f: ds.pathto(pathutil.join(prefix, f))
@@ -1573,10 +1573,11 @@
                 if f not in self._repo.dirstate:
                     self._repo.ui.warn(_("%s not tracked!\n") % uipath(f))
                     rejected.append(f)
-                elif self._repo.dirstate[f] != 'a':
-                    self._repo.dirstate.remove(f)
-                else:
-                    self._repo.dirstate.drop(f)
+                elif not dryrun:
+                    if self._repo.dirstate[f] != 'a':
+                        self._repo.dirstate.remove(f)
+                    else:
+                        self._repo.dirstate.drop(f)
             return rejected
 
     def undelete(self, list):
diff -r 4c71a26a4009 -r 349b51e3bb04 mercurial/subrepo.py
--- a/mercurial/subrepo.py	Sun Mar 04 21:16:36 2018 -0500
+++ b/mercurial/subrepo.py	Sat Mar 10 12:33:19 2018 +0530
@@ -811,9 +811,10 @@
         return ctx.walk(match)
 
     @annotatesubrepoerror
-    def forget(self, match, prefix):
+    def forget(self, match, prefix, dryrun):
         return cmdutil.forget(self.ui, self._repo, match,
-                              self.wvfs.reljoin(prefix, self._path), True)
+                              self.wvfs.reljoin(prefix, self._path),
+                              True, dryrun=dryrun)
 
     @annotatesubrepoerror
     def removefiles(self, matcher, prefix, after, force, subrepos, warnings):
diff -r 4c71a26a4009 -r 349b51e3bb04 tests/test-commit.t
--- a/tests/test-commit.t	Sun Mar 04 21:16:36 2018 -0500
+++ b/tests/test-commit.t	Sat Mar 10 12:33:19 2018 +0530
@@ -832,3 +832,18 @@
 
   $ cd ..
 
+test --dry-run mode in forget
+
+  $ hg init testdir_forget
+  $ cd testdir_forget
+  $ echo foo > foo
+  $ hg add foo
+  $ hg commit -m "foo"
+  $ hg forget foo --dry-run -v
+  removing foo
+  $ hg diff
+  $ hg forget not_exist -n
+  not_exist: $ENOENT$
+  [1]
+
+  $ cd ..


More information about the Mercurial-devel mailing list