[tweakdefaults] fix handling of grep file pattern arguments
Summary:
Previously grep only searched in the current directory, even if you gave it a
pattern like "../". This fixes the code to correctly honor the arguments, and
to accept mercurial file patterns just like most other mercurial commands.
This does change the file parsing behavior somewhat: previously "hg grep" would
treat all arguments as glob patterns. Now you explicitly have to say
"glob:<pattern>" to get glob matching. However, I suspect this won't impact
most users, since they would normally let their shell perform glob expansion
before the arguments are given to mercurial.
Test Plan:
Included new unit tests for grep, and updated the existing tests that were
affected by the change in glob behavior.
Reviewers: #sourcecontrol, durham, carenthomas, ttung, rmcelroy
Reviewed By: rmcelroy
Subscribers: quark, mwilliams, matthieu, meyering, yfeldblum, rmcelroy, net-systems-diffs@, yogeshwer, mjpieters
Differential Revision: https://phabricator.intern.facebook.com/D3376605
Signature: t1:3376605:1464841648:98bfb47866dec802ca34f55c821796f36532211f
2016-06-22 06:44:06 +03:00
|
|
|
$ extpath=`dirname $TESTDIR`
|
|
|
|
$ cp $extpath/tweakdefaults.py $TESTTMP # use $TESTTMP substitution in message
|
|
|
|
$ cat >> $HGRCPATH << EOF
|
|
|
|
> [extensions]
|
|
|
|
> tweakdefaults=$TESTTMP/tweakdefaults.py
|
|
|
|
> rebase=
|
|
|
|
> EOF
|
|
|
|
|
|
|
|
Set up the repository with some simple files
|
|
|
|
$ hg init repo
|
|
|
|
$ cd repo
|
|
|
|
$ mkdir grepdir
|
|
|
|
$ cd grepdir
|
|
|
|
$ echo 'foobarbaz' > grepfile1
|
|
|
|
$ echo 'foobarboo' > grepfile2
|
|
|
|
$ mkdir subdir1
|
|
|
|
$ echo 'foobar_subdir' > subdir1/subfile1
|
|
|
|
$ mkdir subdir2
|
|
|
|
$ echo 'foobar_dirsub' > subdir2/subfile2
|
|
|
|
$ hg add grepfile1
|
|
|
|
$ hg add grepfile2
|
|
|
|
$ hg add subdir1/subfile1
|
|
|
|
$ hg add subdir2/subfile2
|
|
|
|
$ hg commit -m "Added some files"
|
|
|
|
$ echo 'foobarbazboo' > untracked1
|
|
|
|
|
|
|
|
Make sure grep finds patterns in tracked files, and excludes untracked files
|
|
|
|
$ hg grep -n foobar
|
|
|
|
grepfile1:1:foobarbaz
|
|
|
|
grepfile2:1:foobarboo
|
|
|
|
subdir1/subfile1:1:foobar_subdir
|
|
|
|
subdir2/subfile2:1:foobar_dirsub
|
|
|
|
$ hg grep -n barbaz
|
|
|
|
grepfile1:1:foobarbaz
|
|
|
|
$ hg grep -n barbaz .
|
|
|
|
grepfile1:1:foobarbaz
|
|
|
|
|
|
|
|
Test searching in subdirectories, from the repository root
|
|
|
|
$ hg grep -n foobar subdir1
|
|
|
|
subdir1/subfile1:1:foobar_subdir
|
|
|
|
$ hg grep -n foobar sub*
|
|
|
|
subdir1/subfile1:1:foobar_subdir
|
|
|
|
subdir2/subfile2:1:foobar_dirsub
|
|
|
|
|
|
|
|
Test searching in a sibling subdirectory, using a relative path
|
|
|
|
$ cd subdir1
|
|
|
|
$ hg grep -n foobar ../subdir2
|
|
|
|
../subdir2/subfile2:1:foobar_dirsub
|
|
|
|
$ hg grep -n foobar
|
|
|
|
subfile1:1:foobar_subdir
|
|
|
|
$ hg grep -n foobar .
|
|
|
|
subfile1:1:foobar_subdir
|
|
|
|
$ cd ..
|
|
|
|
|
|
|
|
Test mercurial file patterns
|
|
|
|
$ hg grep -n foobar 'glob:*rep*'
|
|
|
|
grepfile1:1:foobarbaz
|
|
|
|
grepfile2:1:foobarboo
|
[tweakdefaults] stop running xargs+grep through a shell
Summary:
Directly execute the xargs command, rather than using a shell. This is
slightly more efficient, but also ensures we won't have security problems or
other issues based on the user's grep.command config setting. Even if it
contains shell redirects, semicolons, or other special characters, we will no
longer pass them to a shell to interpret them.
We do use shlex.split() go process the grep.command setting, to allow the user
to specify a command plus arguments.
Test Plan: Included new unit tests.
Reviewers: #sourcecontrol, durham, ttung, rmcelroy
Reviewed By: rmcelroy
Subscribers: akushner, rmcelroy, sid0, net-systems-diffs@, yogeshwer, mjpieters
Differential Revision: https://phabricator.intern.facebook.com/D3376617
Signature: t1:3376617:1464841542:f58fc0a05d62b649edc70bc261009875385186c9
2016-06-22 06:44:08 +03:00
|
|
|
|
|
|
|
Test using alternative grep commands
|
|
|
|
$ hg grep -i FooBarB
|
|
|
|
grepfile1:foobarbaz
|
|
|
|
grepfile2:foobarboo
|
|
|
|
$ hg grep FooBarB
|
|
|
|
[123]
|
|
|
|
$ hg grep --config grep.command='grep -i' FooBarB
|
|
|
|
grepfile1:foobarbaz
|
|
|
|
grepfile2:foobarboo
|
|
|
|
$ hg grep --config grep.command='echo searching' FooBarB subdir1
|
|
|
|
searching * -- subdir1/subfile1 (glob)
|
|
|
|
$ hg grep --config grep.command='echo foo ; false' FooBarB subdir2
|
|
|
|
foo ; false * -- subdir2/subfile2 (glob)
|