git - 如何 git grep 主存储库及其所有子模块?

标签 git grep git-submodules

我在一个博客上找到这个来 grep 项目和子模块:

[alias]
  sgrep = "!f() { git grep \"$1\"; git submodule foreach \"git grep '$1'; true\" | grep -B 1 \"$1\"; }; f"

我更新了我的 ~/.gitconfig 以包含它,但是当我使用别名时,它说:

致命:/home/myname/.gitconfig 中的错误配置文件第 9 行

我做错了什么?

最佳答案

使用 Git 2.12(2017 年第一季度),您将不需要任何 git submodule foreach把戏。
简单地:

 git grep -e "bar" --recurse-submodules

参见 commit e6fac7f , commit 74ed437 , commit 0281e48 , commit 4538eef , commit 9ebf689 , commit f9f4256 , commit 5688c28 (2016 年 12 月 16 日),以及 commit 4ac9006 , commit 7241764 , commit a1ae484 , commit 05b458c (2016 年 12 月 12 日)Brandon Williams ( mbrandonw ) .
参见 commit e9a379c (2016 年 12 月 21 日)Johannes Sixt ( j6t ) .
(由 Junio C Hamano -- gitster -- merge 于 commit 55d128a ,2017 年 1 月 18 日)

grep: optionally recurse into submodules

Allow grep to recognize submodules and recursively search for patterns in each submodule.
This is done by forking off a process to recursively call grep on each submodule.

Recursion only occurs for submodules which have been initialized and checked out by the parent project. If a submodule hasn't been initialized and checked out it is simply skipped.

In order to support the existing multi-threading infrastructure in grep, output from each child process is captured in a strbuf so that it can be later printed to the console in an ordered fashion.

To limit the number of theads that are created, each child process has half the number of threads as its parents (minimum of 1), otherwise we potentially have a fork-bomb.

git grep man page现在包括:

--recurse-submodules

Recursively search in each submodule that has been initialized and checked out in the repository.
When used in combination with the <tree> option the prefix of all submodule output will be the name of the parent project's <tree> object.


在 Git 2.14.x/2.15(2017 年第 3 季度)中,“git grep --recurse-submodules”已被重新设计以提供跨子模块边界的更一致的输出(并且无需 fork 单独的进程即可完成其工作)。

参见 commit f9ee2fc , commit 2184d4b , commit 34e2ba0 , commit 91b8348 , commit 8fa2915 , commit f20e7c1 , commit b22e51c , commit 4c0eeaf (2017 年 8 月 2 日),和 commit ba43964 , commit 3f13877 (2017 年 7 月 18 日)作者:Brandon Williams ( mbrandonw ) .
(由 Junio C Hamano -- gitster -- merge 于 commit 5aa0b6c ,2017 年 8 月 22 日)

这意味着内部 --parent-basename <basename> git grep 的选项不再存在。


确保使用 Git 2.21(2019 年第一季度),因为有关 cywin 路径的错误已得到修复:请参阅“Cygwin uses relative and absolute path for environment variables”。


实际上,之前 Git 2.23.1/2.24(2019 年第 4 季度),查看工作树文件的“git grep --recurse-submodules”查看子模块中索引中的内容,相反工作树中的文件数

已修复。

参见 commit 6a289d4 (2019 年 7 月 30 日)作者:Matheus Tavares ( matheustavares ) .
(由 Junio C Hamano -- gitster -- merge 于 commit 3071797 ,2019 年 8 月 22 日)

grep: fix worktree case in submodules

Running git-grep with --recurse-submodules results in a cached grep for the submodules even when --cached is not used.
This makes all modifications in submodules' tracked files be always ignored when grepping.

Solve that making git grep respect the cached option when invoking grep_cache() inside grep_submodule().
Also, add tests to ensure that the desired behavior is performed.


"git grep --no-index "不应受到 .gitmodules 内容的影响文件,但是当给出“--recurse-submodules”或设置“submodule.recurse”变量时,它确实如此。

没有更多,使用 Git 2.25.1(2020 年 2 月)。
现在这些设置在“--no-index”模式下被忽略。

参见 commit c56c48d (2020 年 1 月 30 日)作者:Philippe Blain ( phil-blain ) .
(由 Junio C Hamano -- gitster -- merge 于 commit 556ccd4 ,2020 年 2 月 12 日)
See discussion .

grep: ignore --recurse-submodules if --no-index is given

Helped-by: Junio C Hamano
Signed-off-by: Philippe Blain

Since grep learned to recurse into submodules in 0281e487fd ("grep: optionally recurse into submodules", 2016-12-16, Git v2.12.0-rc0 -- merge listed in batch #6), using --recurse-submodules along with --no-index makes Git die().

This is unfortunate because if submodule.recurse is set in a user's ~/.gitconfig, invoking git grep --no-index either inside or outside a Git repository results in:

fatal: option not supported with --recurse-submodules

Let's allow using these options together, so that setting submodule.recurse globally does not prevent using git grep --no-index.

Using --recurse-submodules should not have any effect if --no-index is used inside a repository, as Git will recurse into the checked out submodule directories just like into regular directories.

关于git - 如何 git grep 主存储库及其所有子模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18810904/

相关文章:

git - 暂存区内的底层 git merge 进程是什么?

git - 如何列出 git稀疏 checkout 中的所有文件?

linux - 如何获取不包含某些模式的行

awk - 从多个文本文件中的矩阵中的第 n 行提取第 m 个条目并将值写入新的文本文件

git - 如何将子模块添加到 gh-pages 分支并正确构建静态页面?

git - Docker-compose:总是收到无法找到指定的 Dockerfile

git - 尝试在 CentOS 5 上安装 GIT,有点迷路

git - 如何在我的 mac osx 终端上显示我的 git 分支和提交状态?

regex - 从一个文件到另一个文件的搜索模式

git - 如何将一个 git repo 的构建输出集成到另一个