Bash:Git 子模块 foreach?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8364738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:08:09  来源:igfitidea点击:

Bash: Git submodule foreach?

gitbashforeachgit-submodules

提问by ma11hew28

I have supaliased to submodule foreach 'git co master; git up'(co& upare aliases for checkout& pull --rebase, respectively.).

我曾sup化名为submodule foreach 'git co master; git up'coup是别名checkoutpull --rebase,分别)。

How do add a condition so that if the submodule name is Libraries/JSONKit, it checks out the branch named experimental, instead of master?

如何添加条件,以便如果子模块名称为Libraries/JSONKit,则检查名为 的分支experimental,而不是master

采纳答案by ma11hew28

Add the following to .git/config:

将以下内容添加到.git/config

[alias]
    sup = "submodule foreach 'if [ $name == \"Libraries/JSONKit\" ]; then git co experimental; else git co master; fi; git up'"

回答by larsks

The script passwd to git submoduleis run with it's working directory set to the top of the given submodule...so you can simply look at pwdto see if you're in you're in the particular submodule. However, if you take some time to read the git submoduledocumentation, it turns out to be even easier:

脚本 passwdgit submodule运行时将其工作目录设置为给定子模块的顶部...因此您可以简单pwd地查看是否在特定子模块中。但是,如果您花一些时间阅读git submodule文档,结果会更容易:

foreach
    Evaluates an arbitrary shell command in each checked out submodule. The 
    command has access to the variables $name, $path, $sha1
    and $toplevel: $name is the name of the relevant submodule section in 
    .gitmodules, $path is the name of the submodule directory
    relative to the superproject, $sha1 is the commit as recorded in the superproject, 
    and $toplevel is the absolute path to the top-level of the superproject.

So you can do something like this:

所以你可以做这样的事情:

git submodule foreach '[ "$path" = "Libraries/JSONKit" ] \
  && branch=experimental \
  || branch=master; git co $branch'

回答by VonC

larsks's answeruses:

larsks回答使用:

git submodule foreach '[ "$path" = "Libraries/JSONKit" ]

But it should now (2020) be

但现在(2020 年)应该是

git submodule foreach '[ "$sm_path" = "Libraries/JSONKit" ]

With Git 2.26 (Q1 2020), The bulk of "git submodule foreach" has been rewritten in C, and its documentation evolves.

在 Git 2.26(2020 年第一季度)中,大部分“ git submodule foreach”已用 C 重写,其文档也在不断发展。

See commit fc1b924(10 May 2018), and commit b6f7ac8, commit f0fd0dc, commit c033a2f(09 May 2018) by Prathamesh Chavan (pratham-pc).
(Merged by Junio C Hamano -- gitster--in commit ea27893, 25 Jun 2018)

请参阅Prathamesh Chavan ( ) 的提交 fc1b924(2018 年 5 月 10 日)和提交 b6f7ac8提交 f0fd0dc提交 c033a2f(2018 年 5 月 9 日(由Junio C Hamano合并-- --ea27893 提交中,2018 年 6 月 25 日)pratham-pc
gitster

submodule foreach: document '$sm_path' instead of '$path'

As using a variable '$path' may be harmful to users due to capitalization issues, see 64394e3ae9("git submodule.sh: Don't use $path variable in eval_gettextstring", 2012-04-17, Git v1.7.11-rc0 -- mergelisted in batch #4).
Adjust the documentation to advocate for using $sm_path, which contains the same value.
We still make the 'path' variable available, and document it as a deprecated synonymof 'sm_path'.

Discussed-with: Ramsay Jones

submodule foreach: 文档 ' $sm_path' 而不是 '$path'

由于大写问题,使用变量 '$path' 可能对用户有害,请参阅64394e3ae9(“. shgit submodule:不要在eval_gettext字符串中使用 $path 变量”,2012-04-17,Git v1.7.11-rc0 --合并第 4 批中列出)。
调整文档以提倡使用$sm_path,其中包含相同的值。
我们仍然使 ' path' 变量可用,并将其记录为不推荐使用的 ' sm_path'同义词

讨论对象:拉姆齐·琼斯

The Documentation/git-submodule#foreachnow includes:

Documentation/git-submodule#foreach现在包括:

foreach [--recursive] <command>:

Evaluates an arbitrary shell command in each checked out submodule.

The command has access to the variables $name, $sm_path, $sha1and $toplevel:

  • $nameis the name of the relevant submodule section in .gitmodules.
  • $sm_pathis the path of the submodule as recorded in the immediate superproject,
  • $sha1is the commit as recorded in the immediate superproject, and
  • $toplevelis the absolute path to the top-level of the immediate superproject.

Note that to avoid conflicts with '$PATH' on Windows, the '$path' variable is now a deprecated synonym of '$sm_path' variable.

foreach [--recursive] <command>

评估每个检出子模块中的任意 shell 命令。

该命令可以访问变量$name$sm_path$sha1$toplevel

  • $name是 中相关子模块部分的名称.gitmodules
  • $sm_path是直接超级项目中记录的子模块的路径,
  • $sha1是记录在直接超级项目中的提交,和
  • $toplevel是直接超级项目顶层的绝对路径。

请注意,与“避免冲突$PATH”在Windows上,“ $path”变量现在是“的一个过时的代名词$sm_path”变量