[PATCH 1 of 1] grep: don't print data from binary files for matches (issue2614)

Md. O. Shayan mdoshayan at gmail.com
Sat Apr 9 13:10:10 CDT 2011


# HG changeset patch
# User Md. O. Shayan <mdoshayan at gmail.com>
# Date 1302370997 -19800
# Node ID 19915a1586f506dbe82be60b390f37ff96723b8b
# Parent  6cbe250742aac8668aa24afce0bf614c7c1cdc78
grep: don't print data from binary files for matches (issue2614)

diff -r 6cbe250742aa -r 19915a1586f5 mercurial/commands.py
--- a/mercurial/commands.py	Fri Apr 08 14:43:00 2011 +0530
+++ 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')),
diff -r 6cbe250742aa -r 19915a1586f5 tests/test-grep-bin.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-grep-bin.t	Sat Apr 09 23:13:17 2011 +0530
@@ -0,0 +1,7 @@
+  $ 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


More information about the Mercurial-devel mailing list