grep用于多个git存储库中的内容
因此,我从其他一些开发人员那里继承了相当大的代码库,代码存储在各种git存储库中 有时,很难知道某段代码可能位于哪个项目中,或者该段代码是否存在于git中 我想能够做的是为某些特定的文本片段对所有项目进行grep 我使用的是gitosis,因此所有git存储库都存储在/home/git/repositories中,其结构如下:grep用于多个git存储库中的内容,git,gitosis,Git,Gitosis,因此,我从其他一些开发人员那里继承了相当大的代码库,代码存储在各种git存储库中 有时,很难知道某段代码可能位于哪个项目中,或者该段代码是否存在于git中 我想能够做的是为某些特定的文本片段对所有项目进行grep 我使用的是gitosis,因此所有git存储库都存储在/home/git/repositories中,其结构如下: /home/git/repositories |- project1 |- HEAD |- branches |- config |-
/home/git/repositories
|- project1
|- HEAD
|- branches
|- config
|- description
|- hooks
|- info
|- objects
|- refs
|- project2
|- ...
我尝试过对objects目录中的内容执行递归grep,如下所示:
grep -Hr "text" /home/git/repositories/*/objects
当然,这不能像我想的那样工作,因为对象是以git的自定义格式存储的
怎么办?使用带有ref的
git grep
,或--无索引
:
cd /home/git/repositories
for i in *; do ( cd $i; git grep text HEAD ); done
使用。它是专门通过多个存储库一次编写到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>
$ls
vim spring框架gradle phantomjs
$multi-i“太棒了”
维姆
====================================================
runtime/doc/quotes.txt:VIM 4.5真是一个很棒的编辑器。它有太多的功能和更多
runtime/doc/quotes.txt:太棒了!(托尼·纽金特,澳大利亚)
弹簧框架
====================================================
格拉德尔
====================================================
子项目/docs/src/docs/userguide/ant.xml:只需依赖Groovy和神奇的AntBuilder即可。
子项目/docs/src/docs/userguide/buildScriptsTutorial.xml:依赖Groovy。Groovy附带了奇妙的AntBuilder。使用Ant任务
subprojects/docs/src/docs/userguide/ideSupport.xml:如果您这样做,您就拥有了开发Gradle脚本的极好的IDE支持。当然,如果你使用
幻影
====================================================
test/ghostdriver test/fixtures/common/macbeth.html:你是幻想,还是真的
test/ghostdriver test/fixtures/common/macbeth.html:我的想法,谁被谋杀了还只是幻想,
我知道它的老问题,但如果您使用命令行,您可以将其添加到bash\u配置文件
或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
}
上述函数的基本要点是搜索包含.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”
(用于不区分大小写的搜索)如果您正在查找特定的代码段,而不是提交消息中的文本,您也可以在此处使用git log-S”“
。问题是/home/git/repositories中的文件夹实际上不是git工作树,所以git grep不能对它们工作。@python\u noob git grep可以在裸repo中工作,如果你指定一个ref(例如HEAD),这很令人沮丧。我只是在寻找一种比简单的外壳包装更好的方法,我找到的唯一答案是我自己7年前的答案!现在肯定有更好的工具!我已经提交了一个功能请求:很好的解决方案。从我的使用中可以观察到,因为我知道存储库都是我当前文件夹的直接后代,所以我将-maxdepth 2
添加到find
命令的参数中,这使我执行起来更快。我必须添加--无寻呼机
以查看此答案中描述的输出(一次查看所有结果,而不是逐个文件夹查看结果)对于2021年的这个用例来说,Multi仍然是一个很好的工具。