[PATCH 2 of 2] grep: add -a/--text
Craig Leres
leres at ee.lbl.gov
Sun Apr 10 12:10:28 CDT 2011
# HG changeset patch
# User Craig Leres <leres at ee.lbl.gov>
# Date 1301365570 25200
# Node ID b4ae14524edee1b46b9296577e0585e2dc42a47f
# Parent fd97715da1bbcc27d5781420649e0112d37b0743
grep: add -a/--text
diff -r fd97715da1bb -r b4ae14524ede mercurial/commands.py
--- a/mercurial/commands.py Mon Mar 28 19:25:53 2011 -0700
+++ b/mercurial/commands.py Mon Mar 28 19:26:10 2011 -0700
@@ -1733,6 +1733,9 @@
becomes a non-match, or "+" for a non-match that becomes a match),
use the --all flag.
+ Without the -a/--text option, grep will avoid outputing matches
+ from files it detects as binary.
+
Returns 0 if a match is found, 1 otherwise.
"""
reflags = 0
@@ -1785,7 +1788,8 @@
for lnum, cstart, cend, line in matchlines(body):
s = linestate(line, lnum, cstart, cend)
m.append(s)
- if m and ui.formatted() and (fn, rev) not in binaries:
+ if (m and not opts.get('text') and ui.formatted() and
+ (fn, rev) not in binaries):
binaries[(fn, rev)] = util.binary(body)
def difflinestates(a, b):
@@ -1814,7 +1818,8 @@
iter = [('', l) for l in states]
binary = binaries.get((fn, rev), False)
# Potentially suppress per line output
- suppress = ui.formatted() and (opts.get('all') or follow)
+ suppress = (ui.formatted() and not opts.get('text') and
+ (opts.get('all') or follow))
for change, l in iter:
if ui.formatted() and binary:
ui.warn(_('Binary file %s rev %d matches\n') % (fn, rev))
@@ -4479,6 +4484,7 @@
('f', 'follow', None,
_('follow changeset history,'
' or file history across copies and renames')),
+ ('a', 'text', None, _('treat all files as text')),
('i', 'ignore-case', None, _('ignore case when matching')),
('l', 'files-with-matches', None,
_('print only filenames and revisions that match')),
diff -r fd97715da1bb -r b4ae14524ede tests/test-grep.t
--- a/tests/test-grep.t Mon Mar 28 19:25:53 2011 -0700
+++ b/tests/test-grep.t Mon Mar 28 19:26:10 2011 -0700
@@ -86,6 +86,14 @@
port:1:+:export
port:0:+:import
+binary and formatted and text
+
+ $ hg grep -r5 port port --text --config ui.formatted=true
+ port:5:export
+ port:5:vaportight
+ port:5:import/export
+ port:5:\x00portable (esc)
+
follow
$ hg grep --traceback -f 'import$' port2
More information about the Mercurial-devel
mailing list