Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands: add grep #678

Merged
merged 4 commits into from
Aug 31, 2023

Conversation

mbolivar-ampere
Copy link
Collaborator

@mbolivar-ampere mbolivar-ampere commented Aug 25, 2023

This has been requested various times, but its lack never bothered me up until now, etc. Anyway, here it is.

Fixes: #337

EXAMPLES
--------

To get "git grep foo" results from all cloned, active projects:

  west grep foo

To do the same with:

- git grep --untracked: west grep --untracked foo
- ripgrep:              west grep --tool ripgrep foo
- grep --recursive:     west grep --tool grep foo

To switch the default tool to:

- ripgrep:  west config grep.tool ripgrep
- grep:     west config grep.tool grep

GREP TOOLS
----------

This command runs a "grep tool" command in the top directory of each project.
Supported tools:

- git-grep (default)
- ripgrep
- grep

Set the "grep.tool" configuration option to change the default.

Use "--tool" to switch the tool from its default.

TOOL PATH
---------

Use --tool-path to override the path to the tool. For example:

  west grep --tool ripgrep --tool-path /my/special/ripgrep

Without --tool-path, the "grep.<TOOL>-path" configuration option
is checked next. For example, to set the default path to ripgrep:

  west config grep.ripgrep-path /my/special/ripgrep

If the option is not set, "west grep" searches for the tool as follows:

- git-grep: search for "git" (and run as "git grep")
- ripgrep: search for "rg", then "ripgrep"
- grep: search for "grep"

TOOL ARGUMENTS
--------------

The "grep.<TOOL>-args" configuration options, if set, contain arguments
that are always passed to the tool. The defaults for these are:

- git-grep: (none)
- ripgrep: (none)
- grep: "--recursive"

Command line options or arguments not recognized by "west grep" are
passed to the tool after that. This applies to --foo here, for example:

  west grep --foo --project=myproject

To force arguments to be given to the tool instead of west, put them
after a "--", like the --project and --tool-path options here:

  west grep -- --project=myproject --tool-path=mypath

Arguments before '--' that aren't recognized by west grep are still
passed to the grep tool.

To pass '--' to the grep tool, pass one for 'west grep' first.
For example, to search for '--foo' with grep, you can run:

  west grep --tool grep -- -- --foo

The first '--' separates west grep args from tool args. The second '--'
separates options from positional arguments in the 'grep' command line.

COLORS
------

By default, west will force the tool to print colored output as long
as the "color.ui" configuration option is true. If color.ui is false,
west forces the tool not to print colored output.

Since all supported tools have similar --color options, you can
override this behavior on the command line, for example with:

  west grep --color=never

To do this permanently, set "grep.color":

  west config grep.color never

Copy link
Collaborator

@marc-hb marc-hb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who is this new mbolivar-ampere guy? :-)

Could you bother adding just ONE, super basic, simple, minimal, incomplete and mostly hardcoded test? It would:

  • already provide infinitely more test coverage than no test at all
  • get the ball rolling and makes it vastly easier to expand later, especially for people not familiar with west's test suite (= everyone but you).

The perfect is the enemy of the good.

src/west/app/project.py Outdated Show resolved Hide resolved
src/west/app/project.py Outdated Show resolved Hide resolved
src/west/app/project.py Outdated Show resolved Hide resolved
src/west/app/project.py Show resolved Hide resolved
src/west/app/project.py Outdated Show resolved Hide resolved
@mbolivar-ampere
Copy link
Collaborator Author

@marc-hb for sure I'll be adding tests before merging. I'll update the --quiet and internal-reminder-when-adding-more-tools as well. Thanks for review -- other comments responded to.

src/west/app/project.py Show resolved Hide resolved
src/west/app/project.py Outdated Show resolved Hide resolved
src/west/app/project.py Outdated Show resolved Hide resolved
@mbolivar-ampere
Copy link
Collaborator Author

@marc-hb please revisit

Copy link
Collaborator

@marc-hb marc-hb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor suggestions and questions left after a pretty thorough review, everything else LGTM

src/west/commands.py Show resolved Hide resolved
src/west/app/project.py Show resolved Hide resolved
src/west/app/project.py Show resolved Hide resolved
src/west/app/project.py Outdated Show resolved Hide resolved
src/west/app/project.py Outdated Show resolved Hide resolved
src/west/app/project.py Show resolved Hide resolved
tests/test_project.py Outdated Show resolved Hide resolved
tests/test_project.py Show resolved Hide resolved
The WestCommand API for running subprocesses is OK, but
is missing some useful features; add them now:

- The current recommendation in the subprocess module is
  to use subprocess.run(), so add a helper for doing that
  since it's missing. (The name asymmetry is because
  there is already a WestCommand.run() claimed.)

- Allow subprocess invocations to take any kwargs we want

Signed-off-by: Martí Bolívar <[email protected]>
This is useful when we want to be able to control whether a newline is
appended or not. The die() method is intentionally omitted: this is a
"scream and exit the process" method, and always flushing any
line-buffered I/O makes sense to enforce in this context.

Signed-off-by: Martí Bolívar <[email protected]>
This has been requested various times. I haven't done it because 'west
forall -c "git grep"' has been a reasonable workaround for me, and I
never had time or need to think about what an ergonomic alternative
might look like.

That's changed now and I wanted something that would only print output
for projects where a result was found, similarly to the way "west
diff" only prints for projects with nonempty "git diff", etc.

Add a "grep" command that does similar, defaulting to use of "git
grep" to get the job done. Also support (my favorite) ripgrep and
plain "grep --recursive". See the GREP_EPILOG variable in the patch
for detailed usage information.

Signed-off-by: Martí Bolívar <[email protected]>
Get some minimal coverage.

Signed-off-by: Martí Bolívar <[email protected]>
@mbolivar-ampere
Copy link
Collaborator Author

@marc-hb comments addressed and one additional bug in grep.<TOOL>-args handling found and fixed. Thanks for a great review!

@mbolivar-ampere mbolivar-ampere merged commit 4ba3d99 into zephyrproject-rtos:main Aug 31, 2023
9 checks passed
@marc-hb
Copy link
Collaborator

marc-hb commented Aug 31, 2023

and... it hangs forever :-)

Hotfix submitted in #681

Looks like personal configs are still "polluting" the test suite...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New "west grep" shortcut for west forall -c 'git grep <pattern>'
2 participants