【问题标题】:grep for stuff in multiple git repositoriesgrep 查找多个 git 存储库中的内容
【发布时间】:2012-07-18 14:24:05
【问题描述】:

所以我从其他一些开发人员那里继承了相当大的代码库,代码存储在各种 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 的自定义格式存储的。

做什么?

【问题讨论】:

    标签: git gitosis


    【解决方案1】:

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

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

    【讨论】:

    • 如果你正在寻找一段特定的代码,而不是提交消息中的文本,你也可以在这里使用git log -S"<string>"。它被称为pickaxe search.
    • 问题是 /home/git/repositories 中的文件夹实际上并不是 git 工作树,所以 git grep 对它们不起作用。
    • @python_noob git grep 如果你指定了一个引用(例如,HEAD),它确实可以在一个裸仓库中工作
    • 嗯,这很令人沮丧。我只是去寻找一种比简单的外壳包装器更好的方法,而我找到的唯一答案是我 7 年前的答案!现在肯定有一些更好的工具!
    • 我已提交功能请求:public-inbox.org/git/…
    【解决方案2】:

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

    【讨论】:

    • 不错的解决方案。我使用的一个观察结果是,因为我知道存储库都是我当前文件夹的直接后代,所以我将 -maxdepth 2 添加到 find 命令的参数中,这对我来说执行得更快。
    • 我必须添加 --no-pager 才能看到此答案中描述的输出(一次完成,而不是逐个文件夹查看结果)
    【解决方案3】:

    使用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>
    

    【讨论】:

    • Multi 在 2021 年仍然是这个用例的绝佳工具。
    猜你喜欢
    • 2023-01-13
    • 2012-01-01
    • 2011-11-05
    • 2013-03-27
    • 2019-05-06
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多