Commit 0afd3f8e authored by Sangeet Kumar Mishra's avatar Sangeet Kumar Mishra
Browse files

grep: add --diff flag

Adds a diff flag, which works exactly same as all, in fact since
--all searches diffs, there diff is a better name for it.
The all flag is still here for backward compatibility reasons.
Some major tests for all has been picked and added for diff.

Differential Revision: https://phab.mercurial-scm.org/D3763
parent c1f4364f9336
......@@ -2391,6 +2391,8 @@ def _stopgraft(ui, repo, graftstate):
@command('grep',
[('0', 'print0', None, _('end fields with NUL')),
('', 'all', None, _('print all revisions that match')),
('', 'diff', None, _('print all revisions when the term was introduced '
'or removed')),
('a', 'text', None, _('treat all files as text')),
('f', 'follow', None,
_('follow changeset history,'
......@@ -2419,7 +2421,7 @@ def grep(ui, repo, pattern, *pats, **opts):
file in which it finds a match. To get it to print every revision
that contains a change in match status ("-" for a match that becomes
a non-match, or "+" for a non-match that becomes a match), use the
--all flag.
--diff/--all flag.
PATTERN can be any Python (roughly Perl-compatible) regular
expression.
......@@ -2431,6 +2433,7 @@ def grep(ui, repo, pattern, *pats, **opts):
Returns 0 if a match is found, 1 otherwise.
"""
opts = pycompat.byteskwargs(opts)
diff = opts.get('all') or opts.get('diff')
reflags = re.M
if opts.get('ignore_case'):
reflags |= re.I
......@@ -2527,7 +2530,7 @@ def grep(ui, repo, pattern, *pats, **opts):
return ctx[fn].isbinary()
fieldnamemap = {'filename': 'file', 'linenumber': 'line_number'}
if opts.get('all'):
if diff:
iter = difflinestates(pstates, states)
else:
iter = [('', l) for l in states]
......@@ -2540,7 +2543,7 @@ def grep(ui, repo, pattern, *pats, **opts):
('rev', rev, True),
('linenumber', l.linenum, opts.get('line_number')),
]
if opts.get('all'):
if diff:
cols.append(('change', change, True))
cols.extend([
('user', formatuser(ctx.user()), opts.get('user')),
......@@ -2644,7 +2647,7 @@ def grep(ui, repo, pattern, *pats, **opts):
if pstates or states:
r = display(fm, fn, ctx, pstates, states)
found = found or r
if r and not opts.get('all'):
if r and not diff:
skip[fn] = True
if copy:
skip[copy] = True
......
......@@ -313,7 +313,7 @@ Show all commands + options
debugwireproto: localssh, peer, noreadstderr, nologhandshake, ssh, remotecmd, insecure
files: rev, print0, include, exclude, template, subrepos
graft: rev, continue, stop, edit, log, force, currentdate, currentuser, date, user, tool, dry-run
grep: print0, all, text, follow, ignore-case, files-with-matches, line-number, rev, allfiles, user, date, template, include, exclude
grep: print0, all, diff, text, follow, ignore-case, files-with-matches, line-number, rev, allfiles, user, date, template, include, exclude
heads: rev, topo, active, closed, style, template
help: extension, command, keyword, system
identify: rev, num, id, branch, tags, bookmarks, ssh, remotecmd, insecure, template
......
......@@ -281,6 +281,11 @@ Test wdir
color:2:-:orange
color:1:+:orange
$ hg grep --diff orange
color:3:+:orange
color:2:-:orange
color:1:+:orange
test substring match: '^' should only match at the beginning
$ hg grep '^.' --config extensions.color= --color debug
......@@ -349,6 +354,10 @@ of just using revision numbers.
color:3:-:red
color:1:+:red
$ hg grep --diff red
color:3:-:red
color:1:+:red
Issue3885: test that changing revision order does not alter the
revisions printed, just their order.
......@@ -360,6 +369,14 @@ revisions printed, just their order.
color:3:-:red
color:1:+:red
$ hg grep --diff red -r "all()"
color:1:+:red
color:3:-:red
$ hg grep --diff red -r "reverse(all())"
color:3:-:red
color:1:+:red
$ cd ..
$ hg init a
......@@ -370,6 +387,9 @@ revisions printed, just their order.
$ hg grep "MaCam" --all
binfile.bin:0:+: Binary file matches
$ hg grep "MaCam" --diff
binfile.bin:0:+: Binary file matches
$ cd ..
Test for showing working of allfiles flag
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment