# HG changeset patch # User Md. O. Shayan # Date 1302370997 -19800 # Node ID 332e400764e50646d26512cd9202e088f989dbb6 # Parent 67f20625703fc358e53a6e7cf3102ccffba5bb2b grep: don't print data from binary files for matches (issue2614) diff -r 67f20625703f -r 332e400764e5 mercurial/commands.py --- 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')), diff -r 67f20625703f -r 332e400764e5 tests/test-debugcomplete.t --- 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 diff -r 67f20625703f -r 332e400764e5 tests/test-grep.t --- 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