Patchwork D2938: grep: make grep search on working directory by default

login
register
mail settings
Submitter phabricator
Date March 23, 2018, 8:15 p.m.
Message ID <f8615e20f41f8e1f4c867c3883c2bff9@localhost.localdomain>
Download mbox | patch
Permalink /patch/29804/
State Not Applicable
Headers show

Comments

phabricator - March 23, 2018, 8:15 p.m.
sangeet259 updated this revision to Diff 7262.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D2938?vs=7261&id=7262

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

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

CHANGE DETAILS




To: sangeet259, #hg-reviewers
Cc: 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
@@ -340,4 +340,25 @@ 
   $ hg grep "MaCam" --all
   binfile.bin:0:+: Binary file matches
 
-  $ cd ..
+Test that grep searches only on working directory 
+  $ cd ..
+  $ hg init t5
+  $ cd t5
+  $ echo "mercurial revsets are awesome" > firstfile
+  $ hg add firstfile
+  $ hg commit -m  'adds firstfile'
+  $ hg rm firstfile
+  $ hg commit -m 'removes firstfile'
+  $ echo "mercurial revsets are awesome and makes life easier" > secondfile
+  $ echo "some generic text" > thirdfile
+  $ hg add secondfile thirdfile
+  $ hg commit -m 'adds two new files'
+  $ hg grep 'revsets'
+  secondfile:None:mercurial revsets are awesome and makes life easier
+  $ echo "another generic string" > fourthone
+
+Search on added but not commit i.e dirty working directory
+  $ hg add fourthone
+  $ hg grep "generic"
+  fourthone:None:another generic string
+  thirdfile:None:some generic text
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -2469,21 +2469,34 @@ 
         @util.cachefunc
         def binary():
             flog = getfile(fn)
-            return util.binary(flog.read(ctx.filenode(fn)))
+            try:
+                return util.binary(flog.read(ctx.filenode(fn)))
+            except AttributeError:
+                return util.binary(ctx.filectx(fn).data())
 
         fieldnamemap = {'filename': 'file', 'linenumber': 'line_number'}
         if opts.get('all'):
             iter = difflinestates(pstates, states)
         else:
             iter = [('', l) for l in states]
         for change, l in iter:
             fm.startitem()
-            fm.data(node=fm.hexfunc(ctx.node()))
-            cols = [
+            try:
+                fm.data(node=fm.hexfunc(ctx.node()))
+            except TypeError:
+                pass
+            if not bool(opts.get('all')) and not bool(opts.get('rev')):
+                cols = [
                 ('filename', fn, True),
-                ('rev', rev, True),
+                ('rev', rev, False),
                 ('linenumber', l.linenum, opts.get('line_number')),
             ]
+            else:
+                cols = [
+                    ('filename', fn, True),
+                    ('rev', rev, True),
+                    ('linenumber', l.linenum, opts.get('line_number')),
+                ]
             if opts.get('all'):
                 cols.append(('change', change, True))
             cols.extend([
@@ -2568,26 +2581,46 @@ 
 
     ui.pager('grep')
     fm = ui.formatter('grep', opts)
-    for ctx in cmdutil.walkchangerevs(repo, match, opts, prep):
-        rev = ctx.rev()
-        parent = ctx.p1().rev()
-        for fn in sorted(revfiles.get(rev, [])):
-            states = matches[rev][fn]
-            copy = copies.get(rev, {}).get(fn)
-            if fn in skip:
-                if copy:
-                    skip[copy] = True
+    # This if part handles the default situation,\
+    # when nothing is passed in -r or --all
+    if not bool(opts.get('rev')) and not bool(opts.get('all')):
+        rev = scmutil.revsingle(repo, opts.get('rev'), None).node()
+        m = scmutil.match(repo[rev], pats, opts, default='relglob')
+        m.bad = lambda x, y: False
+        ctx = repo[rev]
+        ds = ctx.repo().dirstate
+
+        for fn in ctx.matches(m):
+            if rev is None and ds[fn] == 'r':
                 continue
-            pstates = matches.get(parent, {}).get(copy or fn, [])
-            if pstates or states:
-                r = display(fm, fn, ctx, pstates, states)
+            data = ctx.filectx(fn).data()
+            states = []
+            for lnum, cstart, cend, line in matchlines(data):
+                states.append(linestate(line, lnum, cstart, cend))
+            if states:
+                r = display(fm, fn, ctx, [], states)
                 found = found or r
-                if r and not opts.get('all'):
-                    skip[fn] = True
+    else :
+        for ctx in cmdutil.walkchangerevs(repo, match, opts, prep):
+            rev = ctx.rev()
+            parent = ctx.p1().rev()
+            for fn in sorted(revfiles.get(rev, [])):
+                states = matches[rev][fn]
+                copy = copies.get(rev, {}).get(fn)
+                if fn in skip:
                     if copy:
                         skip[copy] = True
-        del matches[rev]
-        del revfiles[rev]
+                    continue
+                pstates = matches.get(parent, {}).get(copy or fn, [])
+                if pstates or states:
+                    r = display(fm, fn, ctx, pstates, states)
+                    found = found or r
+                    if r and not opts.get('all'):
+                        skip[fn] = True
+                        if copy:
+                            skip[copy] = True
+            del matches[rev]
+            del revfiles[rev]
     fm.end()
 
     return not found