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