changeset 36939:45bfcd16f27e

forget: add --dry-run mode
author Sushil khanchi <sushilkhanchi97@gmail.com>
date Sat, 10 Mar 2018 12:33:19 +0530
parents 8fd9b56e8d7c
children b0ffcb540357
files hgext/largefiles/overrides.py mercurial/cmdutil.py mercurial/commands.py mercurial/subrepo.py tests/test-add.t tests/test-commit.t tests/test-completion.t
diffstat 7 files changed, 36 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/largefiles/overrides.py	Fri Mar 09 15:53:41 2018 +0100
+++ b/hgext/largefiles/overrides.py	Sat Mar 10 12:33:19 2018 +0530
@@ -1077,9 +1077,9 @@
     finally:
         repo.lfstatus = False
 
-def cmdutilforget(orig, ui, repo, match, prefix, explicitonly):
+def cmdutilforget(orig, ui, repo, match, prefix, explicitonly, dryrun):
     normalmatcher = composenormalfilematcher(match, repo[None].manifest())
-    bad, forgot = orig(ui, repo, normalmatcher, prefix, explicitonly)
+    bad, forgot = orig(ui, repo, normalmatcher, prefix, explicitonly, dryrun)
     m = composelargefilematcher(match, repo[None].manifest())
 
     try:
--- a/mercurial/cmdutil.py	Fri Mar 09 15:53:41 2018 +0100
+++ 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,9 +2039,10 @@
         if ui.verbose or not match.exact(f):
             ui.status(_('removing %s\n') % match.rel(f))
 
-    rejected = wctx.forget(forget, prefix)
-    bad.extend(f for f in rejected if f in match.files())
-    forgot.extend(f for f in forget if f not in rejected)
+    if not dryrun:
+        rejected = wctx.forget(forget, prefix)
+        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
 
 def files(ui, ctx, m, fm, fmt, subrepos):
--- a/mercurial/commands.py	Fri Mar 09 15:53:41 2018 +0100
+++ 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'))
 
-@command('^forget', walkopts, _('[OPTION]... FILE...'), inferrepo=True)
+@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(
--- a/mercurial/subrepo.py	Fri Mar 09 15:53:41 2018 +0100
+++ b/mercurial/subrepo.py	Sat Mar 10 12:33:19 2018 +0530
@@ -348,7 +348,7 @@
         matched by the match function
         '''
 
-    def forget(self, match, prefix):
+    def forget(self, match, prefix, dryrun):
         return ([], [])
 
     def removefiles(self, matcher, prefix, after, force, subrepos, warnings):
@@ -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):
--- a/tests/test-add.t	Fri Mar 09 15:53:41 2018 +0100
+++ b/tests/test-add.t	Sat Mar 10 12:33:19 2018 +0530
@@ -256,3 +256,19 @@
 #endif
 
   $ 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 ..
--- a/tests/test-commit.t	Fri Mar 09 15:53:41 2018 +0100
+++ b/tests/test-commit.t	Sat Mar 10 12:33:19 2018 +0530
@@ -831,4 +831,3 @@
   second line
 
   $ cd ..
-
--- a/tests/test-completion.t	Fri Mar 09 15:53:41 2018 +0100
+++ b/tests/test-completion.t	Sat Mar 10 12:33:19 2018 +0530
@@ -231,7 +231,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
-  forget: include, exclude
+  forget: include, exclude, dry-run
   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