Locating a function in a git repository

末鹿安然 提交于 2020-04-11 15:20:08

问题


I am new to Github and Jupyter notebook.
I want to find a specific function in a git repository, how do I do it?
I have come to know that I could use Git grep, but I don't know the command.
Also, can I use Jupyter notebook to do this or do I have to use the terminal?


回答1:


I don't know about Jupyter Notebook, but use the terminal. Make sure you have already git cloned the repository and are inside its directory in the terminal. Then do git grep from the terminal.

Search for text within files:

1. With git grep (you must be inside a git repo):

Case-sensitive search. The -n shows the line number where the result is found too.

git grep -n "my regular expression search string"

Case insensitive search (add -i here):

git grep -ni "my case insensitive regular expression search string"

Find a function usage (left parenthesis below is optional)

git grep -n "myFunc("

Grep does regular expression search. Google it for details. Regular expressions are a very powerful way to do very specific string matching. As you learn the basics, practice testing and checking your regular expression (regex) searches with this tool here: https://regex101.com/.

2. With regular grep (works anywhere, but operates about 100x slower than git grep):

Same as above, except add -r for 'r'ecursive (to search into directories). If you want to follow symbolic links too, use -R instead of -r.

Examples:

Case-sensitive search. The -n shows the line number where the result is found too.

grep -rn "my regular expression search string"

Case insensitive search (add -i here):

grep -rni "my case insensitive regular expression search string"

Find a function usage (left parenthesis below is optional)

grep -rn "myFunc("

Find a function (following symbolic links: add -R):

grep -Rn "myFunc("

Bonus: find a file by name:

Just pipe the output of find to be the input to grep with the pipe operator (|):

find | grep -ni "my_file_name"

Or, use -L with find to follow symbolic links:

find -L | grep -ni "my_file_name"

Notice the -i above is for case-insensitive filename searches. Remove it to match the case too.

References:

  1. Random experience, Googling, and talking to people over the years
  2. Read the man (manual) pages:
    1. man git grep
    2. man grep
    3. man find
  3. https://regex101.com/

Related:

  1. GitHub: How to do case sensitive search for the code in repository?


来源:https://stackoverflow.com/questions/60843047/locating-a-function-in-a-git-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!