remove: add dry-run functionality
authorSushil khanchi <sushilkhanchi97@gmail.com>
Wed, 21 Mar 2018 23:36:11 +0530
changeset 37150 335e19c6b7fa
parent 37149 cc0a6ea95d98
child 37151 05c13e5aa9ec
remove: add dry-run functionality
hgext/largefiles/overrides.py
mercurial/cmdutil.py
mercurial/commands.py
mercurial/subrepo.py
tests/test-completion.t
tests/test-help.t
tests/test-remove.t
--- a/hgext/largefiles/overrides.py	Sat Mar 17 01:53:44 2018 -0400
+++ b/hgext/largefiles/overrides.py	Wed Mar 21 23:36:11 2018 +0530
@@ -178,7 +178,7 @@
         added = [f for f in lfnames if f not in bad]
     return added, bad
 
-def removelargefiles(ui, repo, isaddremove, matcher, **opts):
+def removelargefiles(ui, repo, isaddremove, matcher, dryrun, **opts):
     after = opts.get(r'after')
     m = composelargefilematcher(matcher, repo[None].manifest())
     try:
@@ -223,11 +223,11 @@
                     name = m.rel(f)
                 ui.status(_('removing %s\n') % name)
 
-            if not opts.get(r'dry_run'):
+            if not dryrun:
                 if not after:
                     repo.wvfs.unlinkpath(f, ignoremissing=True)
 
-        if opts.get(r'dry_run'):
+        if dryrun:
             return result
 
         remove = [lfutil.standin(f) for f in remove]
@@ -271,10 +271,12 @@
     bad.extend(f for f in lbad)
     return bad
 
-def cmdutilremove(orig, ui, repo, matcher, prefix, after, force, subrepos):
+def cmdutilremove(orig, ui, repo, matcher, prefix, after, force, subrepos,
+                  dryrun):
     normalmatcher = composenormalfilematcher(matcher, repo[None].manifest())
-    result = orig(ui, repo, normalmatcher, prefix, after, force, subrepos)
-    return removelargefiles(ui, repo, False, matcher, after=after,
+    result = orig(ui, repo, normalmatcher, prefix, after, force, subrepos,
+                  dryrun)
+    return removelargefiles(ui, repo, False, matcher, dryrun, after=after,
                             force=force) or result
 
 def overridestatusfn(orig, repo, rev2, **opts):
@@ -1238,7 +1240,8 @@
         matchfn = m.matchfn
         m.matchfn = lambda f: f in s.deleted and matchfn(f)
 
-        removelargefiles(repo.ui, repo, True, m, **pycompat.strkwargs(opts))
+        removelargefiles(repo.ui, repo, True, m, opts.get('dry_run'),
+                         **pycompat.strkwargs(opts))
     # Call into the normal add code, and any files that *should* be added as
     # largefiles will be
     added, bad = addlargefiles(repo.ui, repo, True, matcher,
--- a/mercurial/cmdutil.py	Sat Mar 17 01:53:44 2018 -0400
+++ b/mercurial/cmdutil.py	Wed Mar 21 23:36:11 2018 +0530
@@ -2083,7 +2083,7 @@
 
     return ret
 
-def remove(ui, repo, m, prefix, after, force, subrepos, warnings=None):
+def remove(ui, repo, m, prefix, after, force, subrepos, dryrun, warnings=None):
     join = lambda f: os.path.join(prefix, f)
     ret = 0
     s = repo.status(match=m, clean=True)
@@ -2108,7 +2108,7 @@
             sub = wctx.sub(subpath)
             try:
                 if sub.removefiles(submatch, prefix, after, force, subrepos,
-                                   warnings):
+                                   dryrun, warnings):
                     ret = 1
             except error.LookupError:
                 warnings.append(_("skipping missing subrepository: %s\n")
@@ -2188,13 +2188,14 @@
             ui.status(_('removing %s\n') % m.rel(f))
     ui.progress(_('deleting'), None)
 
-    with repo.wlock():
-        if not after:
-            for f in list:
-                if f in added:
-                    continue # we never unlink added files on remove
-                repo.wvfs.unlinkpath(f, ignoremissing=True)
-        repo[None].forget(list)
+    if not dryrun:
+        with repo.wlock():
+            if not after:
+                for f in list:
+                    if f in added:
+                        continue # we never unlink added files on remove
+                    repo.wvfs.unlinkpath(f, ignoremissing=True)
+            repo[None].forget(list)
 
     if warn:
         for warning in warnings:
--- a/mercurial/commands.py	Sat Mar 17 01:53:44 2018 -0400
+++ b/mercurial/commands.py	Wed Mar 21 23:36:11 2018 +0530
@@ -4213,7 +4213,7 @@
     [('A', 'after', None, _('record delete for missing files')),
     ('f', 'force', None,
      _('forget added files, delete modified files')),
-    ] + subrepoopts + walkopts,
+    ] + subrepoopts + walkopts + dryrunopts,
     _('[OPTION]... FILE...'),
     inferrepo=True)
 def remove(ui, repo, *pats, **opts):
@@ -4257,12 +4257,14 @@
 
     opts = pycompat.byteskwargs(opts)
     after, force = opts.get('after'), opts.get('force')
+    dryrun = opts.get('dry_run')
     if not pats and not after:
         raise error.Abort(_('no files specified'))
 
     m = scmutil.match(repo[None], pats, opts)
     subrepos = opts.get('subrepos')
-    return cmdutil.remove(ui, repo, m, "", after, force, subrepos)
+    return cmdutil.remove(ui, repo, m, "", after, force, subrepos,
+                          dryrun=dryrun)
 
 @command('rename|move|mv',
     [('A', 'after', None, _('record a rename that has already occurred')),
--- a/mercurial/subrepo.py	Sat Mar 17 01:53:44 2018 -0400
+++ b/mercurial/subrepo.py	Wed Mar 21 23:36:11 2018 +0530
@@ -355,7 +355,8 @@
     def forget(self, match, prefix, dryrun):
         return ([], [])
 
-    def removefiles(self, matcher, prefix, after, force, subrepos, warnings):
+    def removefiles(self, matcher, prefix, after, force, subrepos,
+                    dryrun, warnings):
         """remove the matched files from the subrepository and the filesystem,
         possibly by force and/or after the file has been removed from the
         filesystem.  Return 0 on success, 1 on any warning.
@@ -821,10 +822,11 @@
                               True, dryrun=dryrun)
 
     @annotatesubrepoerror
-    def removefiles(self, matcher, prefix, after, force, subrepos, warnings):
+    def removefiles(self, matcher, prefix, after, force, subrepos,
+                    dryrun, warnings):
         return cmdutil.remove(self.ui, self._repo, matcher,
                               self.wvfs.reljoin(prefix, self._path),
-                              after, force, subrepos)
+                              after, force, subrepos, dryrun)
 
     @annotatesubrepoerror
     def revert(self, substate, *pats, **opts):
--- a/tests/test-completion.t	Sat Mar 17 01:53:44 2018 -0400
+++ b/tests/test-completion.t	Wed Mar 21 23:36:11 2018 +0530
@@ -238,7 +238,7 @@
   merge: force, rev, preview, abort, tool
   pull: update, force, rev, bookmark, branch, ssh, remotecmd, insecure
   push: force, rev, bookmark, branch, new-branch, pushvars, ssh, remotecmd, insecure
-  remove: after, force, subrepos, include, exclude
+  remove: after, force, subrepos, include, exclude, dry-run
   serve: accesslog, daemon, daemon-postexec, errorlog, port, address, prefix, name, web-conf, webdir-conf, pid-file, stdio, cmdserver, templates, style, ipv6, certificate, subrepos
   status: all, modified, added, removed, deleted, clean, unknown, ignored, no-status, terse, copies, print0, rev, change, include, exclude, subrepos, template
   summary: remote
--- a/tests/test-help.t	Sat Mar 17 01:53:44 2018 -0400
+++ b/tests/test-help.t	Wed Mar 21 23:36:11 2018 +0530
@@ -2857,6 +2857,9 @@
   <tr><td>-X</td>
   <td>--exclude PATTERN [+]</td>
   <td>exclude names matching the given patterns</td></tr>
+  <tr><td>-n</td>
+  <td>--dry-run</td>
+  <td>do not perform actions, just print output</td></tr>
   </table>
   <p>
   global options ([+] can be repeated):
--- a/tests/test-remove.t	Sat Mar 17 01:53:44 2018 -0400
+++ b/tests/test-remove.t	Wed Mar 21 23:36:11 2018 +0530
@@ -505,3 +505,32 @@
   deleting [===========================================>] 1/1\r (no-eol) (esc)
                                                               \r (no-eol) (esc)
   [1]
+
+test dry-run for remove
+
+  $ hg init testdryrun
+  $ cd testdryrun
+  $ echo a>a
+  $ hg ci -qAm1
+  $ hg remove a -nv
+  \r (no-eol) (esc)
+  deleting [===========================================>] 1/1\r (no-eol) (esc)
+                                                              \r (no-eol) (esc)
+  \r (no-eol) (esc)
+  deleting [===========================================>] 1/1\r (no-eol) (esc)
+                                                              \r (no-eol) (esc)
+  removing a
+  $ hg diff
+
+  $ cat >> .hg/hgrc <<EOF
+  > [extensions]
+  > largefiles=
+  > EOF
+  $ echo 'B as largefile' > B
+  $ hg add --large B
+  $ hg ci -m "B"
+  $ hg remove B -nv
+  removing B
+  $ hg st
+
+  $ cd ..