( )⚙ D3826 grep: change default behaviour of grep

This is an archive of the discontinued Mercurial Phabricator instance.

grep: change default behaviour of grep
ClosedPublic

Authored by sangeet259 on Jun 21 2018, 2:42 PM.

Details

Summary

with this patch grep searches on the working directory by default
and looks for all files tracked by the working directory and greps on them

OLD BEHAVIOUR

$ hg init a
$ cd a
$ echo "some text">>file1
$ hg add file1
$ hg commit -m "adds file1"
$ hg mv file1 file2
$ hg grep "some"
file2:1:some text
file1:0:some text

This behaviour is undesirable since file1 is not in the current history and was
renamed as file2, so the second result was redundant and confusing

NEW BEHAVIOUR

$ hg init a
$ cd a
$ echo "some text">>file1
$ hg add file1
$ hg commit -m "adds file1"
$ hg mv file1 file2
$ hg grep "some"
file2:2147483647:some text

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

sangeet259 created this revision.Jun 21 2018, 2:42 PM
sangeet259 updated this revision to Diff 9252.Jun 21 2018, 2:44 PM
sangeet259 retitled this revision from grep : change default behaviour of grep to grep: change default behaviour of grep.Jun 21 2018, 2:48 PM

I need some help with the tests failing here.

yuja added a subscriber: yuja.Jun 23 2018, 11:23 PM

@@ -2431,6 +2431,10 @@

Returns 0 if a match is found, 1 otherwise.
"""
opts = pycompat.byteskwargs(opts)

+
+ if len(opts.get('rev')) ==1:
+ opts['allfiles'] = True

So, -rA implies --allfiles but -rA -rB doesn't? which seems worse than
the current situation.

And we'll need an option to get back the original behavior.

sangeet259 updated this revision to Diff 9461.Jul 5 2018, 3:12 PM
sangeet259 updated this revision to Diff 9462.Jul 5 2018, 3:30 PM
yuja added a comment.Jul 6 2018, 11:02 PM

+ if not opts.get('rev'):
+ opts.get('rev').append("wdir()")
+ opts['allfiles'] = True

So, this breaks hg grep --diff|--all?

And this is a behavior change. We should at least document how we can get
back the original behavior.

sangeet259 updated this revision to Diff 9470.Jul 7 2018, 11:44 PM
sangeet259 edited the summary of this revision. (Show Details)Jul 9 2018, 5:50 AM
sangeet259 updated this revision to Diff 9472.
sangeet259 edited the summary of this revision. (Show Details)Jul 9 2018, 6:02 AM
sangeet259 updated this revision to Diff 9473.
sangeet259 updated this revision to Diff 9474.
yuja added a comment.Jul 9 2018, 8:23 AM

Looks mostly good. Can you rebase this onto the current tip and update the
help?

$ hg grep '.*'
  • port:4:export
  • port:4:vaportight
  • port:4:import/export

+ port:2147483647:export
+ port:2147483647:vaportight
+ port:2147483647:import/export

[snip]

It's probably better to leave the existing tests to test the old behavior
by adding some arguments (maybe -r tip:0) unless we're sure that the new
behavior covers things that should be tested.

+ if not opts.get('rev') and not diff:
+ opts.get('rev').append("wdir()")

Nit: If you want to care for missing opts['rev'], this should be
opts['rev'] = ["wdir()"].

sangeet259 edited the summary of this revision. (Show Details)Jul 9 2018, 12:38 PM
sangeet259 updated this revision to Diff 9480.
sangeet259 updated this revision to Diff 9481.Jul 9 2018, 12:45 PM
sangeet259 updated this revision to Diff 9482.Jul 9 2018, 12:51 PM
durin42 requested changes to this revision.Jul 9 2018, 4:53 PM
durin42 added a subscriber: durin42.
durin42 added inline comments.
mercurial/commands.py
2517–2518

I think you need to update this line in the help? That is, by default we now search the working directory, and you have to say something else to search history.

This revision now requires changes to proceed.Jul 9 2018, 4:53 PM
sangeet259 updated this revision to Diff 9492.Jul 10 2018, 5:29 AM
yuja added a comment.Jul 10 2018, 9:36 AM

Queued, thanks.

$ hg up -q null
  • $ hg grep -f port
  • [1]

+ $ hg grep -r tip:0 -f port

I've changed this to -r 'reverse(:.)' since it was the default for -f.

  • By default, grep prints the most recent revision number for each
  • 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
  • --diff flag.

+ By default, grep searches the expression on the working directory
+ , to search history and show the most recent revision number for each
+ file in which it finds a match, use hg grep -r tip:0.
+ 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 --diff flag.

Perhaps this needs an English tweak, but I'm not a right person. So I only
fixed trivial stuffs.

This revision was automatically updated to reflect the committed changes.