bash 在非零退出后使用“git submodule foreach”命令继续循环子模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19728933/
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
Continue looping over submodules with the "git submodule foreach" command after a non-zero exit
提问by Casey Flynn
I have a project that contains many submodules. I want to loop over each submodule with the following command:
我有一个包含许多子模块的项目。我想使用以下命令遍历每个子模块:
git submodule foreach npm install
git submodule foreach npm install
And I want the script to continue looping over each submodule even if one submodule returns an error (non zero return code). Currently, a non-zero return code from running this command in any submodule will cause git to stop looping over the remaining submodules.
而且我希望脚本继续循环遍历每个子模块,即使一个子模块返回错误(非零返回代码)。目前,在任何子模块中运行此命令的非零返回码将导致 git 停止在其余子模块上循环。
Any recommendations on how to accomplish this?
关于如何实现这一点的任何建议?
回答by gniourf_gniourf
Just make your command always return a 0
code like so:
只需让您的命令始终返回如下0
代码:
git submodule foreach 'npm install || :'
This is taken from the manual: git help submodule
:
这是从手册中获取的git help 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. Any submodules defined in the superproject but
not checked out are ignored by this command. Unless given --quiet,
foreach prints the name of each submodule before evaluating the
command. If --recursive is given, submodules are traversed
recursively (i.e. the given shell command is evaluated in nested
submodules as well). A non-zero return from the command in any
submodule causes the processing to terminate. This can be
overridden by adding || : to the end of the command.
As an example, git submodule foreach 'echo $path `git rev-parse
HEAD`' will show the path and currently checked out commit for each
submodule.
The command :
from help :
in bash
:
:
来自help :
in的命令bash
:
:: :
Null command.
No effect; the command does nothing.
Exit Status:
Always succeeds.
Always succeeds:)
总是成功:)
回答by Idriss Neumann
You could see this topic.
你可以看到这个话题。
I don't use GIT, but if you can locate the .gitmodules files, it may be easy to loop for each submodules :
我不使用 GIT,但是如果您可以找到 .gitmodules 文件,则可能很容易为每个子模块循环:
<command to find all of your submodules>|while read; do
# ... (default use $REPLY as an item)
done
Or :
或者 :
while read; do
# ...
done <<< "$(command to find all of your submodules)"
See this reminder on how to read a command output with a loop in Bash.