Patchwork D3826: grep: change default behaviour of grep

login
register
mail settings
Submitter phabricator
Date July 10, 2018, 9:29 a.m.
Message ID <131ef16a2a45e9eb60eb34dcaf519473@localhost.localdomain>
Download mbox | patch
Permalink /patch/32731/
State Not Applicable
Headers show

Comments

phabricator - July 10, 2018, 9:29 a.m.
sangeet259 updated this revision to Diff 9492.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3826?vs=9482&id=9492

REVISION DETAIL
  https://phab.mercurial-scm.org/D3826

AFFECTED FILES
  mercurial/commands.py
  tests/test-grep.t

CHANGE DETAILS




To: sangeet259, #hg-reviewers, durin42
Cc: durin42, yuja, mercurial-devel
Yuya Nishihara - July 10, 2018, 1:34 p.m.
Queued, thanks.

>    $ hg up -q null
> -  $ hg grep -f port
> -  [1]
> +  $ hg grep -r tip:0 -f port

I've changed this to `-r 'reverse(:.)'` since it was the default for `-f`.

> -    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.

Perhaps this needs an English tweak, but I'm not a right person. So I only
fixed trivial stuffs.
phabricator - July 10, 2018, 1:36 p.m.
yuja added a comment.


  Queued, thanks.
  
  >   $ hg up -q null
  > 
  > - $ hg grep -f port
  > - [1] +  $ hg grep -r tip:0 -f port
  
  I've changed this to `-r 'reverse(:.)'` since it was the default for `-f`.
  
  > - 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.
  
  Perhaps this needs an English tweak, but I'm not a right person. So I only
  fixed trivial stuffs.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3826

To: sangeet259, #hg-reviewers, durin42
Cc: durin42, yuja, mercurial-devel

Patch

diff --git a/tests/test-grep.t b/tests/test-grep.t
--- a/tests/test-grep.t
+++ b/tests/test-grep.t
@@ -23,32 +23,32 @@ 
 
 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
 
 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
@@ -60,7 +60,7 @@ 
 
 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,17 +216,17 @@ 
 
 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
   $ hg commit -m 4 -u spam -d '5 0'
 
 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,11 @@ 
   port:0:1:+:spam:import
 
   $ hg up -q null
-  $ hg grep -f port
-  [1]
+  $ hg grep -r tip:0 -f port
+  port2:6:export
+  port2:6:vaportight
+  port2:6:import/export
+  port2:6:deport
 
 Test wdir
 (at least, this shouldn't crash)
@@ -264,9 +267,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 +282,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 +296,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 +306,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 +323,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 +427,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
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -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):