# HG changeset patch # User Md. O. Shayan # Date 2011-04-09 17:43:17 # Node ID 332e400764e50646d26512cd9202e088f989dbb6 # Parent 67f20625703fc358e53a6e7cf3102ccffba5bb2b grep: don't print data from binary files for matches (issue2614) diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -1871,6 +1871,10 @@ def grep(ui, repo, pattern, *pats, **opt 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 @@ def grep(ui, repo, pattern, *pats, **opt 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 @@ table = { (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 --git a/tests/test-debugcomplete.t b/tests/test-debugcomplete.t --- a/tests/test-debugcomplete.t +++ b/tests/test-debugcomplete.t @@ -235,7 +235,7 @@ Show all commands + options 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 --git a/tests/test-grep.t b/tests/test-grep.t --- a/tests/test-grep.t +++ b/tests/test-grep.t @@ -166,3 +166,11 @@ of just using revision numbers. $ 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