Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/commands.py @ 50408:4a73df6eb67d
grep: avoid reassigning byteskwargs to strkwargs
PyCharm flagged each of these `get()` calls with bytes. We still pass the bytes
form to the formatter to avoid changing the API, until all callers can be
changed.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Wed, 01 Mar 2023 11:45:57 -0500 |
parents | e27a5a012323 |
children | d68462736492 |
comparison
equal
deleted
inserted
replaced
50407:4fafc6642bee | 50408:4a73df6eb67d |
---|---|
3527 | 3527 |
3528 Returns 0 if a match is found, 1 otherwise. | 3528 Returns 0 if a match is found, 1 otherwise. |
3529 | 3529 |
3530 """ | 3530 """ |
3531 cmdutil.check_incompatible_arguments(opts, 'all_files', ['all', 'diff']) | 3531 cmdutil.check_incompatible_arguments(opts, 'all_files', ['all', 'diff']) |
3532 opts = pycompat.byteskwargs(opts) | 3532 |
3533 diff = opts.get(b'all') or opts.get(b'diff') | 3533 diff = opts.get('all') or opts.get('diff') |
3534 follow = opts.get(b'follow') | 3534 follow = opts.get('follow') |
3535 if opts.get(b'all_files') is None and not diff: | 3535 if opts.get('all_files') is None and not diff: |
3536 opts[b'all_files'] = True | 3536 opts['all_files'] = True |
3537 plaingrep = ( | 3537 plaingrep = ( |
3538 opts.get(b'all_files') | 3538 opts.get('all_files') and not opts.get('rev') and not opts.get('follow') |
3539 and not opts.get(b'rev') | |
3540 and not opts.get(b'follow') | |
3541 ) | 3539 ) |
3542 all_files = opts.get(b'all_files') | 3540 all_files = opts.get('all_files') |
3543 if plaingrep: | 3541 if plaingrep: |
3544 opts[b'rev'] = [b'wdir()'] | 3542 opts['rev'] = [b'wdir()'] |
3545 | 3543 |
3546 reflags = re.M | 3544 reflags = re.M |
3547 if opts.get(b'ignore_case'): | 3545 if opts.get('ignore_case'): |
3548 reflags |= re.I | 3546 reflags |= re.I |
3549 try: | 3547 try: |
3550 regexp = util.re.compile(pattern, reflags) | 3548 regexp = util.re.compile(pattern, reflags) |
3551 except re.error as inst: | 3549 except re.error as inst: |
3552 ui.warn( | 3550 ui.warn( |
3553 _(b"grep: invalid match pattern: %s\n") | 3551 _(b"grep: invalid match pattern: %s\n") |
3554 % stringutil.forcebytestr(inst) | 3552 % stringutil.forcebytestr(inst) |
3555 ) | 3553 ) |
3556 return 1 | 3554 return 1 |
3557 sep, eol = b':', b'\n' | 3555 sep, eol = b':', b'\n' |
3558 if opts.get(b'print0'): | 3556 if opts.get('print0'): |
3559 sep = eol = b'\0' | 3557 sep = eol = b'\0' |
3560 | 3558 |
3561 searcher = grepmod.grepsearcher( | 3559 searcher = grepmod.grepsearcher( |
3562 ui, repo, regexp, all_files=all_files, diff=diff, follow=follow | 3560 ui, repo, regexp, all_files=all_files, diff=diff, follow=follow |
3563 ) | 3561 ) |
3601 (b'rev', b'%d', rev, not plaingrep, b''), | 3599 (b'rev', b'%d', rev, not plaingrep, b''), |
3602 ( | 3600 ( |
3603 b'linenumber', | 3601 b'linenumber', |
3604 b'%d', | 3602 b'%d', |
3605 l.linenum, | 3603 l.linenum, |
3606 opts.get(b'line_number'), | 3604 opts.get('line_number'), |
3607 b'', | 3605 b'', |
3608 ), | 3606 ), |
3609 ] | 3607 ] |
3610 if diff: | 3608 if diff: |
3611 cols.append( | 3609 cols.append( |
3623 [ | 3621 [ |
3624 ( | 3622 ( |
3625 b'user', | 3623 b'user', |
3626 b'%s', | 3624 b'%s', |
3627 formatuser(ctx.user()), | 3625 formatuser(ctx.user()), |
3628 opts.get(b'user'), | 3626 opts.get('user'), |
3629 b'', | 3627 b'', |
3630 ), | 3628 ), |
3631 ( | 3629 ( |
3632 b'date', | 3630 b'date', |
3633 b'%s', | 3631 b'%s', |
3634 fm.formatdate(ctx.date(), datefmt), | 3632 fm.formatdate(ctx.date(), datefmt), |
3635 opts.get(b'date'), | 3633 opts.get('date'), |
3636 b'', | 3634 b'', |
3637 ), | 3635 ), |
3638 ] | 3636 ] |
3639 ) | 3637 ) |
3640 for name, fmt, data, cond, extra_label in cols: | 3638 for name, fmt, data, cond, extra_label in cols: |
3641 if cond: | 3639 if cond: |
3642 fm.plain(sep, label=b'grep.sep') | 3640 fm.plain(sep, label=b'grep.sep') |
3643 field = fieldnamemap.get(name, name) | 3641 field = fieldnamemap.get(name, name) |
3644 label = extra_label + (b'grep.%s' % name) | 3642 label = extra_label + (b'grep.%s' % name) |
3645 fm.condwrite(cond, field, fmt, data, label=label) | 3643 fm.condwrite(cond, field, fmt, data, label=label) |
3646 if not opts.get(b'files_with_matches'): | 3644 if not opts.get('files_with_matches'): |
3647 fm.plain(sep, label=b'grep.sep') | 3645 fm.plain(sep, label=b'grep.sep') |
3648 if not opts.get(b'text') and binary(): | 3646 if not opts.get('text') and binary(): |
3649 fm.plain(_(b" Binary file matches")) | 3647 fm.plain(_(b" Binary file matches")) |
3650 else: | 3648 else: |
3651 displaymatches(fm.nested(b'texts', tmpl=b'{text}'), l) | 3649 displaymatches(fm.nested(b'texts', tmpl=b'{text}'), l) |
3652 fm.plain(eol) | 3650 fm.plain(eol) |
3653 found = True | 3651 found = True |
3654 if opts.get(b'files_with_matches'): | 3652 if opts.get('files_with_matches'): |
3655 break | 3653 break |
3656 return found | 3654 return found |
3657 | 3655 |
3658 def displaymatches(fm, l): | 3656 def displaymatches(fm, l): |
3659 p = 0 | 3657 p = 0 |
3675 found = False | 3673 found = False |
3676 | 3674 |
3677 wopts = logcmdutil.walkopts( | 3675 wopts = logcmdutil.walkopts( |
3678 pats=pats, | 3676 pats=pats, |
3679 opts=opts, | 3677 opts=opts, |
3680 revspec=opts[b'rev'], | 3678 revspec=opts['rev'], |
3681 include_pats=opts[b'include'], | 3679 include_pats=opts['include'], |
3682 exclude_pats=opts[b'exclude'], | 3680 exclude_pats=opts['exclude'], |
3683 follow=follow, | 3681 follow=follow, |
3684 force_changelog_traversal=all_files, | 3682 force_changelog_traversal=all_files, |
3685 filter_revisions_by_pats=not all_files, | 3683 filter_revisions_by_pats=not all_files, |
3686 ) | 3684 ) |
3687 revs, makefilematcher = logcmdutil.makewalker(repo, wopts) | 3685 revs, makefilematcher = logcmdutil.makewalker(repo, wopts) |
3688 | 3686 |
3689 ui.pager(b'grep') | 3687 ui.pager(b'grep') |
3690 fm = ui.formatter(b'grep', opts) | 3688 fm = ui.formatter(b'grep', pycompat.byteskwargs(opts)) |
3691 for fn, ctx, pstates, states in searcher.searchfiles(revs, makefilematcher): | 3689 for fn, ctx, pstates, states in searcher.searchfiles(revs, makefilematcher): |
3692 r = display(fm, fn, ctx, pstates, states) | 3690 r = display(fm, fn, ctx, pstates, states) |
3693 found = found or r | 3691 found = found or r |
3694 if r and not diff and not all_files: | 3692 if r and not diff and not all_files: |
3695 searcher.skipfile(fn, ctx.rev()) | 3693 searcher.skipfile(fn, ctx.rev()) |