grep: enable passing wdir as a revision
authorSangeet Kumar Mishra <mail2sangeetmishra@gmail.com>
Wed, 30 May 2018 17:37:17 +0530
changeset 38217 16f93a3b8b05
parent 38216 22edd5321489
child 38218 36ba5dba372d
grep: enable passing wdir as a revision When you pass wdir() to the -r flag, it catches the WdirUnsupported error and falls back to an alternate path. Differential Revision: https://phab.mercurial-scm.org/D3673
mercurial/commands.py
tests/test-grep.t
--- a/mercurial/commands.py	Fri Jun 01 23:33:17 2018 -0400
+++ b/mercurial/commands.py	Wed May 30 17:37:17 2018 +0530
@@ -2513,7 +2513,7 @@
                     yield ('+', b[i])
 
     def display(fm, fn, ctx, pstates, states):
-        rev = ctx.rev()
+        rev = scmutil.intrev(ctx)
         if fm.isplain():
             formatuser = ui.shortuser
         else:
@@ -2526,7 +2526,10 @@
         @util.cachefunc
         def binary():
             flog = getfile(fn)
-            return stringutil.binary(flog.read(ctx.filenode(fn)))
+            try:
+                return stringutil.binary(flog.read(ctx.filenode(fn)))
+            except error.WdirUnsupported:
+                return ctx[fn].isbinary()
 
         fieldnamemap = {'filename': 'file', 'linenumber': 'line_number'}
         if opts.get('all'):
@@ -2535,7 +2538,8 @@
             iter = [('', l) for l in states]
         for change, l in iter:
             fm.startitem()
-            fm.data(node=fm.hexfunc(ctx.node()))
+            fm.data(node=fm.hexfunc(scmutil.binnode(ctx)))
+
             cols = [
                 ('filename', fn, True),
                 ('rev', rev, True),
@@ -2601,8 +2605,10 @@
                 fnode = ctx.filenode(fn)
             except error.LookupError:
                 continue
-
-            copied = flog.renamed(fnode)
+            try:
+                copied = flog.renamed(fnode)
+            except error.WdirUnsupported:
+                copied = ctx[fn].renamed()
             copy = follow and copied and copied[0]
             if copy:
                 copies.setdefault(rev, {})[fn] = copy
@@ -2613,7 +2619,11 @@
             files.append(fn)
 
             if fn not in matches[rev]:
-                grepbody(fn, rev, flog.read(fnode))
+                try:
+                    content = flog.read(fnode)
+                except error.WdirUnsupported:
+                    content = ctx[fn].data()
+                grepbody(fn, rev, content)
 
             pfn = copy or fn
             if pfn not in matches[parent]:
--- a/tests/test-grep.t	Fri Jun 01 23:33:17 2018 -0400
+++ b/tests/test-grep.t	Wed May 30 17:37:17 2018 +0530
@@ -250,8 +250,11 @@
   $ hg stat
   M port2
   $ hg grep -r 'wdir()' port
-  abort: working directory revision cannot be specified
-  [255]
+  port2:2147483647:export
+  port2:2147483647:vaportight
+  port2:2147483647:import/export
+  port2:2147483647:deport
+  port2:2147483647:wport
 
   $ cd ..
   $ hg init t2
@@ -368,3 +371,14 @@
   binfile.bin:0:+: Binary file matches
 
   $ cd ..
+
+Fix_Wdir(): test that passing wdir() t -r flag does greps on the
+files modified in the working directory
+
+  $ cd a
+  $ echo "abracadara" >> a
+  $ hg add a
+  $ hg grep -r "wdir()" "abra"
+  a:2147483647:abracadara
+
+  $ cd ..