-
Notifications
You must be signed in to change notification settings - Fork 103
src: add grep utility to explore feature #178
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/explore.sh
Outdated
if [[ "$1" =~ ^--log$ && ! -z "$2" ]]; then | ||
return 1 | ||
fi | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an extra space here
|
||
cmd_manager "$flag" "git grep -e \"$regex\" -nI $path" | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You forgot to add the function documentation.
src/explore.sh
Outdated
return 1 | ||
fi | ||
|
||
if [[ "$1" =~ ^--grep$ && ! -z "$2" ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case, it could be nice if we also support -g
, i.e., kw e -g lala
or kw e --grep lala
.
src/explore.sh
Outdated
# If user only set regex value | ||
path=${path:-"."} | ||
|
||
cmd_manager "$flag" "grep -nrwI $path -e \"$regex\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you decided to use cmd_manager
you can use the flag TEST_MODE
for implementing a new test that validate the command correctness. Take a look at mk_test
, test mk_kernel_uninstall_Test
for reference.
tests/explore_test.sh
Outdated
git commit -m "Commit number $commit" &> /dev/null | ||
done | ||
|
||
echo "##regex_sample##" > e_grep_test.txt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make this test a little bit more robust, I recommend you to add a full text in tests/samples
and use it for validating your grep function.
Also, since you're working at this part of the code could you also take a look at this issue: #92 |
src/explore.sh
Outdated
# If user only set regex value | ||
path=${path:-"."} | ||
|
||
cmd_manager "$flag" "grep -nrwI $path -e \"$regex\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently don use the -w
flag in the "git grepping mode". So, for consistency, I think we should remove it here or add it in the git-grep search as well. Personally, I prefer not to use -w
, since we wouldn't allow users to search for "non-words". And they can always resort to something like "\bpattern\b"
, to simulate the same effect, without -w
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the homogeneity for the feature as a whole. So, I will remove -w in my next version
Hi, @melissawen and @rodrigosiqueira . First of all, thanks for the new feature :) Two ideas came to my mind, regarding implementation. Please, let me know what you think about them:
function in_git_work_tree()
{
local ret="$(git rev-parse --is-inside-work-tree 2>&1)"
if [[ $? == 0 && "$ret" == "true" ]]; then
return 0
fi
return 1
} |
Hi @matheustavares, I appreciate your suggestion with --no-index. For me, I can refactor this patch (or do another one) to use this parameter, as I don't see another employment for grep utility other than searching in files that are not under git control. |
Hi @melissawen and @matheustavares Great idea Matheus! I believe that Additionally and IMHO it does not hurt to provide an extra option for letting users explicitly say that they want to search something not under git control. For example, some times, I want to search on some metadata in the Linux kernel (under git control) that I know that is not under git control. |
I agree. I think the user should decide to search in files under or not under git control. But I think we can handle it only using git tool, with --no-index (without adding grep tool). Is it good? |
I recommend that you follow Matheus's suggestion, but also add the option for users to ignore git repository (optional). Finally, I also agree to use --no-index. |
Oh, there is one caveat I just noticed: |
Oops, I accidentally hit "Close and Comment". Sorry for that! Reopening... |
Hi, Just feedback: |
Hi @melissawen. Yes, |
Hi @matheustavares, the original function failed my tests. However, I adjusted it to deal with the problem :) I will send everything after applying all the suggestions. Thanks :) |
ca6099e
to
01c1841
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @melissawen ,
First of all, thanks for updating your patch! You had a lot of progress :)
You can see all my comments per change in your commit, please take a careful look in all of them; I tried to provide a detailed explanation. Aside from the inline comments, I want to add:
- If you use vim, try to enable the option
set list
to see extra spaces in your code. Alternatively, you can usegit log -p
after commit your change and double-check for extra space at the end of lines. - Add extra tests that exercise the following cases:
- Search in a non-git directory: In the setup phase, create a folder without git repo and write a test that validates
kw e something
. This test going to exercise thegit grep --no-index -e
- Search in a git repository project: You can create a setup case in which you create a git repository, commit a change, and try to search for something. This will validate
git grep -e
Hint: When you write a test, try to think about how you going to pass through each line of your code. How you going to pass in each if
, elif
, else
, and so forth.
src/explore.sh
Outdated
# This function manages the search inside files and under the git repository | ||
# control. | ||
# This function is responsible for checking if the folder is under git control. | ||
function in_git_work_tree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the comments about this function implementation below.
src/explore.sh
Outdated
|
||
# If user only set regex value | ||
path=${path:-"."} | ||
if [[ $(in_git_work_tree) ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition is always false because $(in_git_work_tree)
is empty. Inside in_git_work_tree
you did not return anything, exit 1
or exit 0
only change the value inside the variable $?
and with $(in_git_work_tree)
you're not checking for this value. If you want to test what I mean, just create tags (make tags
) in your kernel repository and try to use your implementation for searching vkms_vblank_simulate
; in my case I got:
kw e vkms_vblank_simulate drivers/gpu/drm/vkms/
drivers/gpu/drm/vkms/vkms_crtc.c:10:static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer)
drivers/gpu/drm/vkms/vkms_crtc.c:65: out->vblank_hrtimer.function = &vkms_vblank_simulate;
tags:4871065:vkms_vblank_simulate drivers/gpu/drm/vkms/vkms_crtc.c /^static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer)$/;" f typeref:enum:hrtimer_restart file: signature:(struct hrtimer * timer)
rtimer_restart file: signature:(struct hrtimer * timer)
My suggestion for fixing this issue:
# This function is responsible for checking if the folder is under git control.
function in_git_work_tree
{
output=$(git rev-parse --is-inside-work-tree 2>/dev/null)
ret="$?"
# If git rev-parse --is-inside-work-tree executes without issue it will
# return 0, however, we can get false as an output.
if [[ "$ret" -eq 0 && "$output" -eq "true" ]]; then
return 0
fi
return 1
}
# This function detects if the files are under the git repository
# control and properly handles the search using git grep tool.
#
# @regex Specifies the regex that we want to search in the files
# @path Narrow down the search
# @flag How to display a command, the default value is "SILENT". For more
# options see `src/kwlib.sh` function `cmd_manager`
function explore_files_under_git()
{
local regex="$1"
local path="$2"
local flag="$3"
# Silent by default
flag=${flag:-"SILENT"}
# If user only set regex value
path=${path:-"."}
output=$(in_git_work_tree)
if [[ "$?" != 0 ]]; then
cmd_manager "$flag" "git grep --no-index -e \"$regex\" -nI $patch"
else
cmd_manager "$flag" "git grep -e \"$regex\" -nI $path"
fi
}
src/explore.sh
Outdated
|
||
# This function manages the search inside files and under the git repository | ||
# control. | ||
# This function is responsible for checking if the folder is under git control. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document the return value, for example
...
# Returns:
# Explain the return
src/explore.sh
Outdated
path=${path:-"."} | ||
|
||
cmd_manager "$flag" "git grep -e \"$regex\" -nI $path" | ||
cmd_manager "$flag" "grep -nrI $path -e \"$regex\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the flag --color
to make the final results a little bit better.
documentation/man/kw.rst
Outdated
directory. | ||
|
||
e, explore [--log] [*EXPRESSION*] [-p] [*DIRECTORY|FILE*] | ||
e, explore [--log | --grep, -g] [*EXPRESSION*] [-p] [*DIRECTORY|FILE*] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should extend the below line with ~~~~~
, otherwise you will break the documentation style. For example, try kw man
after installing your changes and you will see that this section is broken.
suite_addTest "explore_files_under_git_repo_Test" | ||
suite_addTest "explore_git_log_Test" | ||
suite_addTest "explore_parser_Test" | ||
suite_addTest "explore_grep_Test" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In your code you used cmd_manager
, this feature provides support for TEST_MODE
which enables us to test the command sequence. I recommend you to add extra tests taking advantage of this feature, I wrote the below example for helping you to understand how to do it:
function explore_grep_cmd_Test
{
local ID
local -r current_path="$PWD"
local expected_cmd="grep --color -nrI . -e \"GNU grep\""
cd "$test_path"
ID=1
output=$(explore --grep "GNU grep" "." "TEST_MODE")
assertEquals "($ID)" "$expected_cmd" "$output"
cd "$current_path"
}
tests/explore_test.sh
Outdated
local file_name | ||
local regex_sample |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You did not use these variables, you can drop it.
632c264
to
8d00c2c
Compare
This patch detects if a directory isn't under git control to expand coverage of git grep and also adds gnu grep option for catching files in hidden directories Closes kworkflow#61 Co-developed-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Melissa Wen <melissa.srw@gmail.com>
8d00c2c
to
986a1b1
Compare
Hi @melissawen Thanks a lot for your patch! It is already merged, see at: |
This patch expands the feature -explore- adding the grep utility
for searching plain-text data in files that are not under git.
Related to #61
Signed-off-by: Melissa Wen melissa.srw@gmail.com