grep: don't print data from binary files for matches (issue2614)
authorMd. O. Shayan <mdoshayan@gmail.com>
Sat, 09 Apr 2011 23:13:17 +0530
changeset 13920 332e400764e5
parent 13919 67f20625703f
child 13921 4788923a2b33
grep: don't print data from binary files for matches (issue2614)
mercurial/commands.py
tests/test-debugcomplete.t
tests/test-grep.t
--- a/mercurial/commands.py	Sat Apr 09 23:53:23 2011 +0300
+++ b/mercurial/commands.py	Sat Apr 09 23:13:17 2011 +0530
@@ -1871,6 +1871,10 @@
         datefunc = ui.quiet and util.shortdate or util.datestr
         found = False
         filerevmatches = {}
+        def binary():
+            flog = getfile(fn)
+            return util.binary(flog.read(ctx.filenode(fn)))
+
         if opts.get('all'):
             iter = difflinestates(pstates, states)
         else:
@@ -1897,9 +1901,12 @@
                 after = l.line[l.colend:]
             ui.write(sep.join(cols))
             if before is not None:
-                ui.write(sep + before)
-                ui.write(match, label='grep.match')
-                ui.write(after)
+                if not opts.get('text') and binary():
+                    ui.write(sep + " Binary file matches")
+                else:
+                    ui.write(sep + before)
+                    ui.write(match, label='grep.match')
+                    ui.write(after)
             ui.write(eol)
             found = True
         return found
@@ -4567,6 +4574,7 @@
         (grep,
          [('0', 'print0', None, _('end fields with NUL')),
           ('', 'all', None, _('print all revisions that match')),
+          ('a', 'text', None, _('treat all files as text')),
           ('f', 'follow', None,
            _('follow changeset history,'
              ' or file history across copies and renames')),
--- a/tests/test-debugcomplete.t	Sat Apr 09 23:53:23 2011 +0300
+++ b/tests/test-debugcomplete.t	Sat Apr 09 23:13:17 2011 +0530
@@ -235,7 +235,7 @@
   debugsub: rev
   debugwalk: include, exclude
   debugwireargs: three, four, ssh, remotecmd, insecure
-  grep: print0, all, follow, ignore-case, files-with-matches, line-number, rev, user, date, include, exclude
+  grep: print0, all, text, follow, ignore-case, files-with-matches, line-number, rev, user, date, include, exclude
   heads: rev, topo, active, closed, style, template
   help: 
   identify: rev, num, id, branch, tags, bookmarks
--- a/tests/test-grep.t	Sat Apr 09 23:53:23 2011 +0300
+++ b/tests/test-grep.t	Sat Apr 09 23:13:17 2011 +0530
@@ -166,3 +166,11 @@
   $ hg grep --all red
   color:3:-:red
   color:1:+:red
+
+  $ hg init a
+  $ cd a
+  $ cp $TESTDIR/binfile.bin .
+  $ hg add binfile.bin
+  $ hg ci -m 'add binfile.bin'
+  $ hg grep "MaCam" --all
+  binfile.bin:0:+: Binary file matches