grep: change default behaviour to search working directory files (BC)
authorSangeet Kumar Mishra <mail2sangeetmishra@gmail.com>
Fri, 06 Jul 2018 00:39:21 +0530
changeset 38631 9ef10437bb88
parent 38630 e1987261dd05
child 38632 eb8504715071
grep: change default behaviour to search working directory files (BC) With this patch, grep searches on the working directory by default and looks for all files tracked by the working directory and greps on them. ### OLD BEHAVIOUR $ hg init a $ cd a $ echo "some text">>file1 $ hg add file1 $ hg commit -m "adds file1" $ hg mv file1 file2 $ hg grep "some" `file2:1:some text` `file1:0:some text` This behaviour is undesirable since file1 is not in the current history and was renamed as file2, so the second result was redundant and confusing. ### NEW BEHAVIOUR $ hg init a $ cd a $ echo "some text">>file1 $ hg add file1 $ hg commit -m "adds file1" $ hg mv file1 file2 $ hg grep "some" `file2:2147483647:some text` Differential Revision: https://phab.mercurial-scm.org/D3826
mercurial/commands.py
tests/test-grep.t
--- a/mercurial/commands.py	Tue Jul 10 13:18:34 2018 +0200
+++ b/mercurial/commands.py	Fri Jul 06 00:39:21 2018 +0530
@@ -2515,11 +2515,12 @@
     Search revision history for a regular expression in the specified
     files or the entire project.
 
-    By default, grep prints the most recent revision number for each
-    file in which it finds a match. To get it to print every revision
-    that contains a change in match status ("-" for a match that becomes
-    a non-match, or "+" for a non-match that becomes a match), use the
-    --diff flag.
+    By default, grep searches the expression on the working directory.
+    To search history and show the most recent revision number for each
+    file in which it finds a match, use :hg:`grep -r tip:0`.
+    To get it to print every revision that contains a change in match status
+    ("-" for a match that becomes a non-match, or "+" for a non-match that
+    becomes a match), use the --diff flag.
 
     PATTERN can be any Python (roughly Perl-compatible) regular
     expression.
@@ -2544,6 +2545,10 @@
     if opts.get('print0'):
         sep = eol = '\0'
 
+    if not opts.get('rev') and not diff:
+        opts['rev'] = ["wdir()"]
+        opts['allfiles'] = True
+
     getfile = util.lrucachefunc(repo.file)
 
     def matchlines(body):
--- a/tests/test-grep.t	Tue Jul 10 13:18:34 2018 +0200
+++ b/tests/test-grep.t	Fri Jul 06 00:39:21 2018 +0530
@@ -23,11 +23,11 @@
 
 simple
 
-  $ hg grep '.*'
+  $ hg grep -r tip:0 '.*'
   port:4:export
   port:4:vaportight
   port:4:import/export
-  $ hg grep port port
+  $ hg grep -r tip:0 port port
   port:4:export
   port:4:vaportight
   port:4:import/export
@@ -35,32 +35,32 @@
 simple with color
 
   $ hg --config extensions.color= grep --config color.mode=ansi \
-  >     --color=always port port
+  >     --color=always port port -r tip:0
   \x1b[0;35mport\x1b[0m\x1b[0;36m:\x1b[0m\x1b[0;32m4\x1b[0m\x1b[0;36m:\x1b[0mex\x1b[0;31;1mport\x1b[0m (esc)
   \x1b[0;35mport\x1b[0m\x1b[0;36m:\x1b[0m\x1b[0;32m4\x1b[0m\x1b[0;36m:\x1b[0mva\x1b[0;31;1mport\x1b[0might (esc)
   \x1b[0;35mport\x1b[0m\x1b[0;36m:\x1b[0m\x1b[0;32m4\x1b[0m\x1b[0;36m:\x1b[0mim\x1b[0;31;1mport\x1b[0m/ex\x1b[0;31;1mport\x1b[0m (esc)
 
 simple templated
 
-  $ hg grep port \
+  $ hg grep port -r tip:0 \
   > -T '{file}:{rev}:{node|short}:{texts % "{if(matched, text|upper, text)}"}\n'
   port:4:914fa752cdea:exPORT
   port:4:914fa752cdea:vaPORTight
   port:4:914fa752cdea:imPORT/exPORT
 
-  $ hg grep port -T '{file}:{rev}:{texts}\n'
+  $ hg grep port -r tip:0 -T '{file}:{rev}:{texts}\n'
   port:4:export
   port:4:vaportight
   port:4:import/export
 
-  $ hg grep port -T '{file}:{tags}:{texts}\n'
+  $ hg grep port -r tip:0 -T '{file}:{tags}:{texts}\n'
   port:tip:export
   port:tip:vaportight
   port:tip:import/export
 
 simple JSON (no "change" field)
 
-  $ hg grep -Tjson port
+  $ hg grep -r tip:0 -Tjson port
   [
    {
     "date": [4, 0],
@@ -93,7 +93,7 @@
 
 simple JSON without matching lines
 
-  $ hg grep -Tjson -l port
+  $ hg grep -r tip:0 -Tjson -l port
   [
    {
     "date": [4, 0],
@@ -216,9 +216,9 @@
 
 other
 
-  $ hg grep -l port port
+  $ hg grep -r tip:0 -l port port
   port:4
-  $ hg grep import port
+  $ hg grep -r tip:0 import port
   port:4:import/export
 
   $ hg cp port port2
@@ -226,7 +226,7 @@
 
 follow
 
-  $ hg grep --traceback -f 'import\n\Z' port2
+  $ hg grep -r tip:0 --traceback -f 'import\n\Z' port2
   port:0:import
   
   $ echo deport >> port2
@@ -244,8 +244,8 @@
   port:0:1:+:spam:import
 
   $ hg up -q null
-  $ hg grep -f port
-  [1]
+  $ hg grep -r 'reverse(:.)' -f port
+  port:0:import
 
 Test wdir
 (at least, this shouldn't crash)
@@ -264,9 +264,9 @@
   $ cd ..
   $ hg init t2
   $ cd t2
-  $ hg grep foobar foo
+  $ hg grep -r tip:0 foobar foo
   [1]
-  $ hg grep foobar
+  $ hg grep -r tip:0 foobar
   [1]
   $ echo blue >> color
   $ echo black >> color
@@ -279,7 +279,7 @@
   $ echo orange >> color
   $ echo blue >> color
   $ hg ci -m 3
-  $ hg grep orange
+  $ hg grep -r tip:0 orange
   color:3:orange
   $ hg grep --all orange
   color:3:+:orange
@@ -293,7 +293,7 @@
 
 test substring match: '^' should only match at the beginning
 
-  $ hg grep '^.' --config extensions.color= --color debug
+  $ hg grep -r tip:0 '^.' --config extensions.color= --color debug
   [grep.filename|color][grep.sep|:][grep.rev|3][grep.sep|:][grep.match|b]lack
   [grep.filename|color][grep.sep|:][grep.rev|3][grep.sep|:][grep.match|o]range
   [grep.filename|color][grep.sep|:][grep.rev|3][grep.sep|:][grep.match|b]lue
@@ -303,7 +303,7 @@
   $ $PYTHON -c 'fp = open("noeol", "wb"); fp.write(b"no infinite loop"); fp.close();'
   $ hg ci -Amnoeol
   adding noeol
-  $ hg grep loop
+  $ hg grep -r tip:0 loop
   noeol:4:no infinite loop
 
   $ cd ..
@@ -320,7 +320,7 @@
   adding color
   $ hg rename color colour
   $ hg ci -Am rename
-  $ hg grep octarine
+  $ hg grep -r tip:0 octarine
   colour:1:octarine
   color:0:octarine
 
@@ -424,3 +424,14 @@
   a:2147483647:abracadara
 
   $ cd ..
+
+Change Default of grep, that is, the files not in current working directory
+should not be grepp-ed on
+  $ hg init ab
+  $ cd ab
+  $ echo "some text">>file1
+  $ hg add file1
+  $ hg commit -m "adds file1"
+  $ hg mv file1 file2
+  $ hg grep "some"
+  file2:2147483647:some text