11

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

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

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

我正在使用 gitosis,因此所有 git 存储库都存储在 /home/git/repositories 中,其结构如下:

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

我尝试对对象目录中的内容进行递归 grep,如下所示:

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

当然,这无法按我的意图工作,因为对象以 git 的自定义格式存储。

做什么?

4

3 回答 3

9

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

cd /home/git/repositories
for i in *; do ( cd $i; git grep text HEAD ); done
于 2012-07-18T14:28:39.167 回答
4

我知道它的老问题,但如果你使用命令行,你可以将它添加到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 "$@"
        )
    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"(用于不区分大小写的搜索)

于 2016-03-26T19:46:46.720 回答
2

使用。它是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>
于 2015-09-03T16:56:46.640 回答