comparison mercurial/commands.py @ 38427:7fbb5d76c555

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
author Sangeet Kumar Mishra <mail2sangeetmishra@gmail.com>
date Sun, 17 Jun 2018 15:52:08 +0530
parents 2ceea1554d1e
children 5d9b765dbe15
comparison
equal deleted inserted replaced
38426:c1f4364f9336 38427:7fbb5d76c555
2389 return 0 2389 return 0
2390 2390
2391 @command('grep', 2391 @command('grep',
2392 [('0', 'print0', None, _('end fields with NUL')), 2392 [('0', 'print0', None, _('end fields with NUL')),
2393 ('', 'all', None, _('print all revisions that match')), 2393 ('', 'all', None, _('print all revisions that match')),
2394 ('', 'diff', None, _('print all revisions when the term was introduced '
2395 'or removed')),
2394 ('a', 'text', None, _('treat all files as text')), 2396 ('a', 'text', None, _('treat all files as text')),
2395 ('f', 'follow', None, 2397 ('f', 'follow', None,
2396 _('follow changeset history,' 2398 _('follow changeset history,'
2397 ' or file history across copies and renames')), 2399 ' or file history across copies and renames')),
2398 ('i', 'ignore-case', None, _('ignore case when matching')), 2400 ('i', 'ignore-case', None, _('ignore case when matching')),
2417 2419
2418 By default, grep prints the most recent revision number for each 2420 By default, grep prints the most recent revision number for each
2419 file in which it finds a match. To get it to print every revision 2421 file in which it finds a match. To get it to print every revision
2420 that contains a change in match status ("-" for a match that becomes 2422 that contains a change in match status ("-" for a match that becomes
2421 a non-match, or "+" for a non-match that becomes a match), use the 2423 a non-match, or "+" for a non-match that becomes a match), use the
2422 --all flag. 2424 --diff/--all flag.
2423 2425
2424 PATTERN can be any Python (roughly Perl-compatible) regular 2426 PATTERN can be any Python (roughly Perl-compatible) regular
2425 expression. 2427 expression.
2426 2428
2427 If no FILEs are specified (and -f/--follow isn't set), all files in 2429 If no FILEs are specified (and -f/--follow isn't set), all files in
2429 current branch or have been deleted in a prior changeset. 2431 current branch or have been deleted in a prior changeset.
2430 2432
2431 Returns 0 if a match is found, 1 otherwise. 2433 Returns 0 if a match is found, 1 otherwise.
2432 """ 2434 """
2433 opts = pycompat.byteskwargs(opts) 2435 opts = pycompat.byteskwargs(opts)
2436 diff = opts.get('all') or opts.get('diff')
2434 reflags = re.M 2437 reflags = re.M
2435 if opts.get('ignore_case'): 2438 if opts.get('ignore_case'):
2436 reflags |= re.I 2439 reflags |= re.I
2437 try: 2440 try:
2438 regexp = util.re.compile(pattern, reflags) 2441 regexp = util.re.compile(pattern, reflags)
2525 return stringutil.binary(flog.read(ctx.filenode(fn))) 2528 return stringutil.binary(flog.read(ctx.filenode(fn)))
2526 except error.WdirUnsupported: 2529 except error.WdirUnsupported:
2527 return ctx[fn].isbinary() 2530 return ctx[fn].isbinary()
2528 2531
2529 fieldnamemap = {'filename': 'file', 'linenumber': 'line_number'} 2532 fieldnamemap = {'filename': 'file', 'linenumber': 'line_number'}
2530 if opts.get('all'): 2533 if diff:
2531 iter = difflinestates(pstates, states) 2534 iter = difflinestates(pstates, states)
2532 else: 2535 else:
2533 iter = [('', l) for l in states] 2536 iter = [('', l) for l in states]
2534 for change, l in iter: 2537 for change, l in iter:
2535 fm.startitem() 2538 fm.startitem()
2538 cols = [ 2541 cols = [
2539 ('filename', fn, True), 2542 ('filename', fn, True),
2540 ('rev', rev, True), 2543 ('rev', rev, True),
2541 ('linenumber', l.linenum, opts.get('line_number')), 2544 ('linenumber', l.linenum, opts.get('line_number')),
2542 ] 2545 ]
2543 if opts.get('all'): 2546 if diff:
2544 cols.append(('change', change, True)) 2547 cols.append(('change', change, True))
2545 cols.extend([ 2548 cols.extend([
2546 ('user', formatuser(ctx.user()), opts.get('user')), 2549 ('user', formatuser(ctx.user()), opts.get('user')),
2547 ('date', fm.formatdate(ctx.date(), datefmt), opts.get('date')), 2550 ('date', fm.formatdate(ctx.date(), datefmt), opts.get('date')),
2548 ]) 2551 ])
2642 continue 2645 continue
2643 pstates = matches.get(parent, {}).get(copy or fn, []) 2646 pstates = matches.get(parent, {}).get(copy or fn, [])
2644 if pstates or states: 2647 if pstates or states:
2645 r = display(fm, fn, ctx, pstates, states) 2648 r = display(fm, fn, ctx, pstates, states)
2646 found = found or r 2649 found = found or r
2647 if r and not opts.get('all'): 2650 if r and not diff:
2648 skip[fn] = True 2651 skip[fn] = True
2649 if copy: 2652 if copy:
2650 skip[copy] = True 2653 skip[copy] = True
2651 del revfiles[rev] 2654 del revfiles[rev]
2652 # We will keep the matches dict for the duration of the window 2655 # We will keep the matches dict for the duration of the window