2012-07-18 95 views
5

所以我从其他一些开发人员继承了一个相当大的代码库,代码存储在各种git存储库中。在多个git存储库中的grep

有时,很难知道某个特定代码可能位于哪个项目中,或者该代码是否存在于git中。

我想要做的就是grep所有项目的某些特定的文本。

我使用的gitosis,所以所有的Git仓库储存在/ home/git的/仓库用如下的结构:

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

我试过在对象目录做的东西,递归的grep像这样:

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

这个失败,因为我打算当然,因为对象存储在git的自定义格式。

什么?

回答

5

使用git grep与ref或--no-index

cd /home/git/repositories 
for i in *; do (cd $i; git grep text HEAD); done 
+0

如果你正在寻找一个特定的代码,而不是提交消息中的文本,您也可以在这里使用'git log -S“”'。它被称为[镐搜索](http://gitfu.wordpress.com/2008/06/03/the-pickaxe-finding-changes-was-never-easier/) – Christopher 2012-07-18 14:32:51

+0

问题是/ home中的文件夹/ git/repositories实际上并不是git工作树,所以git grep不适用于它们。 – jdeuce 2012-07-18 14:35:56

+0

@python_noob如果你指定一个ref(例如HEAD) – 2012-07-18 14:42:01

-1

使用multi。它一次性通过多个存储库专门写入git grep

$ ls 
vim spring-framework gradle phantomjs 
$ multi -i "fantastic" 
vim 
==================================================== 
runtime/doc/quotes.txt:VIM 4.5 is really a fantastic editor. It has sooooo many features and more 
runtime/doc/quotes.txt:fantastic it is! (Tony Nugent, Australia) 
spring-framework 
==================================================== 
gradle 
==================================================== 
subprojects/docs/src/docs/userguide/ant.xml:  simply by relying on Groovy, and the fantastic <literal>AntBuilder</literal>. 
subprojects/docs/src/docs/userguide/buildScriptsTutorial.xml:   relying on Groovy. Groovy is shipped with the fantastic <literal>AntBuilder</literal>. Using Ant tasks 
subprojects/docs/src/docs/userguide/ideSupport.xml:   if you do this you have a fantastic IDE support for developing Gradle scripts. Of course if you use 
phantomjs 
==================================================== 
test/ghostdriver-test/fixtures/common/macbeth.html:<A NAME=1.3.55>Are ye fantastical, or that indeed</A><br> 
test/ghostdriver-test/fixtures/common/macbeth.html:<A NAME=1.3.148>My thought, whose murder yet is but fantastical,</A><br> 
1

我知道它的老问题,但如果你使用命令行,你可以添加这bash_profilebashrc

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

上述功能的基本要点是搜索包含.git和输出首先目录该目录随后将文件与该令牌发生的行号一起文件编号

然后转到/home/git/repositories并使用

ggrep "InvalidToken"

它会像这样

/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", 

输出还可以通过标志像ggrep -i "search"(不区分大小写搜索)