在非零退出后,使用"git submodule foreach"命令继续循环子模块

Cas*_*ynn 41 git bash git-submodules

我有一个包含许多子模块的项目.我想用以下命令遍历每个子模块:

git submodule foreach npm install

我希望脚本继续循环遍历每个子模块,即使一个子模块返回错误(非零返回码).目前,在任何子模块中运行此命令的非零返回码将导致git停止在其余子模块上循环.

关于如何实现这一目标的任何建议?

gni*_*urf 90

只需让命令总是返回如下0代码:

git submodule foreach 'npm install || :'
Run Code Online (Sandbox Code Playgroud)

这取自手册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.
Run Code Online (Sandbox Code Playgroud)

该命令:来自help :bash:

:: :
    Null command.

    No effect; the command does nothing.

    Exit Status:
    Always succeeds.
Run Code Online (Sandbox Code Playgroud)

永远成功 :)

  • `git submodule foreach'npm install || 真的'会做同样的事情. (7认同)
  • @MarceloFilho:尝试使用 git bash。 (2认同)

Sim*_*mon 5

Windows批处理中,您可以通过添加 & 符号和另一个不执行任何操作的命令将返回代码设置为 0:

git submodule foreach "npm install & echo done"
Run Code Online (Sandbox Code Playgroud)

编辑:最初的答案是使用分号来执行此操作,这是不正确的。