gpt4 book ai didi

GIT:查找文件列表(例如使用 git ls-files),包括子模块

转载 作者:太空狗 更新时间:2023-10-29 12:51:17 24 4
gpt4 key购买 nike

我一直在尝试弄清楚如何获取 git 存储库中所有文件的列表,包括那些包含在子模块中的文件。目前,git ls-files 将提供顶级子模块目录,但不提供子模块中包含的文件。在进一步调查中,我发现使用 git submodule,您可以递归地找到所有子模块,然后使用 git ls-files:

git submodule --quiet foreach --recursive "git ls-files"

唯一的问题是结果是来自子模块的路径,但我需要来自 repo 的完整路径。所以对于以下

例如/some/path/to/gitrepo/source/submodule/[file1, file2]

我看到的是:

file1
file2

我想看到的是:

source/submodule/file1
source/submodule/file2

有没有办法做到这一点?在文档中,有一些预定义的变量($name、$path、$sha1 和 $toplevel),但我不确定如何使用这些变量来获得所需的结果。

最佳答案

Git 2.11+(2016 年第 4 季度)可以使用另一种方法

git ls-files --recurse-submodules

参见 commit 75a6315 , commit 07c01b9 , commit e77aa33 , commit 74866d7 (2016 年 10 月 7 日)Brandon Williams (mbrandonw) .
(由 Junio C Hamano -- gitster -- merge 于 commit 1c2b1f7 ,2016 年 10 月 26 日)

ls-files: optionally recurse into submodules

"git ls-files" learned "--recurse-submodules" option that can be used to get a listing of tracked files across submodules (i.e. this only works with "--cached" option, not for listing untracked or ignored files).

This would be a useful tool to sit on the upstream side of a pipe that is read with xargs to work on all working tree files from the top-level superproject.

如图in this test ,输出将包括文件的完整路径,从主父存储库开始。

git ls-files documentation现在包括:

--recurse-submodules

Recursively calls ls-files on each submodule in the repository.
Currently there is only support for the --cached mode.


Git 2.13(2017 年第 2 季度)增加了 ls-files --recurse-submodules 的稳健性:

参见 commit 2cfe66a , commit 2e5d650 (2017 年 4 月 13 日)Jacob Keller (jacob-keller) .
(由 Junio C Hamano -- gitster -- merge 于 commit 2d646e3 ,2017 年 4 月 24 日)

ls-files: fix recurse-submodules with nested submodules

Since commit e77aa33 ("ls-files: optionally recurse intosubmodules", 2016-10-07, git 2.11) ls-files has known how to recurse intosubmodules when displaying files.

Unfortunately this fails for certain cases, including when nesting morethan one submodule, called from within a submodule that itself hassubmodules, or when the GIT_DIR environemnt variable is set.

Prior to commit b58a68c ("setup: allow for prefix to be passed togit commands", 2017-03-17, git 2.13-rc0) this resulted in an error indicating that--prefix and --super-prefix were incompatible.

After this commit, instead, the process loops forever with a GIT_DIR setto the parent and continuously reads the parent submodule files and recursing forever.

Fix this by preparing the environment properly for submodules when setting up the child process. This is similar to how other commands such as grep behave.


正如 Git 2.29(2020 年第 4 季度)所述,配置 submodule.recurse 将不起作用。

参见 commit 7d15fdb (2020 年 10 月 4 日)Philippe Blain (phil-blain) .
(由 Junio C Hamano -- gitster -- merge 于 commit 9d19e17 ,2020 年 10 月 5 日)

gitsubmodules doc: invoke 'ls-files' with '--recurse-submodules'

Signed-off-by: Philippe Blain

git ls-files(man) was never taught to respect the submodule.recurse configuration variable, and it is too late now to change that, but still the command is mentioned in 'gitsubmodules(7)' as if it does respect that config.

Adjust the call in 'gitsubmodules(7)' by calling 'ls-files' with the '--recurse-submodules' option.

gitsubmodules 现在包含在其 man page 中:

git ls-files --recurse-submodules

[NOTE]
git ls-files also requires its own --recurse-submodules flag.


With Git 2.36 (Q2 2022) ,还支持 git ls-files --stage --recurse-submodule


在 Git 2.40(2023 年第一季度)中,停止使用 git --super-prefix 并将其使用范围缩小到 submodule--helper。

参见 commit 4002ec3 , commit f5a6be9 , commit 04f1fab , commit 99a32d8 , commit 677c981 , commit bb61a96 , commit f0a5e5a , commit 49eb1d3 (2022 年 12 月 20 日)Ævar Arnfjörð Bjarmason (avar) .
参见 commit 0d1806e (2022 年 12 月 20 日)Glen Choo (chooglen) .
(由 Junio C Hamano -- gitster -- merge 于 commit d4c5400 ,2023 年 1 月 5 日)

read-tree: add "--super-prefix" option, eliminate global

Signed-off-by: Ævar Arnfjörð Bjarmason

The "--super-prefix" option to "git" was initially added in commit 74866d7 ("git: make super-prefix option", 2016-10-07, Git v2.11.0-rc0 -- merge listed in batch #11) for:

  • use with "ls-files" (commit e77aa33 ("ls-files: optionally recurse into submodules", 2016-10-07, Git v2.11.0-rc0 -- merge listed in batch #11)), and shortly thereafter
  • "submodule--helper" (commit 89c8626 ("submodule helper: support super prefix", 2016-12-08, Git v2.12.0-rc0 -- merge listed in batch #5)) and
  • "grep" (0281e48 ("grep: optionally recurse into submodules", 2016-12-16, Git v2.12.0-rc0 -- merge listed in batch #6)).

It wasn't until commit 3d41542 ("unpack-trees: support super-prefix option", 2017-01-17, Git v2.12.0-rc0 -- merge) that "read-tree" made use of it.

At the time it made sense, but since then we've made "ls-files" recurse in-process in commit 188dce1 ("ls-files: use repository object", 2017-06-22, Git v2.14.0-rc0 -- merge listed in batch #14), "grep" in commit f9ee2fc ("grep: recurse in-process using 'struct repository'", 2017-08-02, Git v2.15.0-rc0 -- merge listed in batch #2), and finally "submodule--helper" in the preceding commits.

Let's also remove it from "read-tree", which allows us to remove the option to "git" itself.

We can do this because the only remaining user of it is the submodule API, which will now invoke "read-tree" with its new "--super-prefix" option.
It will only do so when the "submodule_move_head()" function is called.

That "submodule_move_head()" function was then only invoked by "read-tree" itself, but now rather than setting an environment variable to pass "--super-prefix" between cmd_read_tree() we: - Set a new "super_prefix" in "struct unpack_trees_options".

git 现在包含在其 man page 中:

[--config-env==] []

关于GIT:查找文件列表(例如使用 git ls-files),包括子模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36848544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com