Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep for stuff in multiple git repositories

Tags:

git

gitosis

So I've inherited a fairly large code base from some other developers, with code stored in various git repositories.

Sometimes, it's hard to know which project a particular piece of code might lie in, or if that piece of code even exists in git.

What I want to be able to do is grep ALL the projects for some particular piece of text.

I'm using gitosis, so all the git repositories are stored in /home/git/repositories with a structure like:

/home/git/repositories
  |- project1
    |- HEAD
    |- branches
    |- config
    |- description
    |- hooks
    |- info
    |- objects
    |- refs
  |- project2
    |- ...

I've tried doing a recursive grep for stuff in the objects directories like this:

grep -Hr "text" /home/git/repositories/*/objects

This fails to work as I intend of course, because the objects are stored in git's custom format.

What do?

like image 848
jdeuce Avatar asked Jul 18 '12 14:07

jdeuce


People also ask

Can you grep github?

The `git grep` is a useful command for searching the specific content in the git repository. The searching can be done in different ways by using the different options of this command.


2 Answers

Use git grep with a ref or --no-index:

cd /home/git/repositories
for i in *; do ( cd $i; git grep text HEAD ); done
like image 186
William Pursell Avatar answered Oct 16 '22 22:10

William Pursell


I know its old question but if you use command line you can add this to bash_profile or bashrc

ggrep() {
    find . -type d -name .git | while read line; do
        (
        cd $line/..
        cwd=$(pwd)
        echo "$(tput setaf 2)$cwd$(tput sgr0)"
        git grep -n "$@"
        )
    done
}

basic gist of above function is to search of all directories that contains .git and output first that directory then file along with line number where that token occurs

then go to /home/git/repositories and search using

ggrep "InvalidToken"

it will output like this

/home/git/org/repo1
/home/git/org/repo2
/home/git/org/repo3
/home/git/org/repo3
lib/v3/Utility.pm:59:         code              => 'InvalidToken',
lib/v3/Utility.pm:142:        code              => "InvalidToken",

you can also pass flags like ggrep -i "search" (for case insensitive search)

like image 5
Raunak Kathuria Avatar answered Oct 16 '22 22:10

Raunak Kathuria