【问题标题】:Continue looping over submodules with the "git submodule foreach" command after a non-zero exit在非零退出后使用“git submodule foreach”命令继续循环子模块
【发布时间】:2013-11-12 18:46:06
【问题描述】:

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

git submodule foreach npm install

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

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

【问题讨论】:

  • git 子模块 foreach 'yarn'

标签: git bash git-submodules


【解决方案1】:

只需让您的命令始终返回 0 代码,如下所示:

git submodule foreach 'npm install || :'

这摘自手册: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.

help :bash 中的命令 :

:: :
    Null command.

    No effect; the command does nothing.

    Exit Status:
    Always succeeds.

总是成功 :)

【讨论】:

  • git submodule foreach 'npm install || true' 也会这样做。
  • 它不适用于 Windows。本来应该?即使使用 bash `` 也只会尝试执行第一个并且无论如何都会中断。
  • @MarceloFilho:尝试使用 git bash。
  • 使用 git bash @MaBe... :/
  • @MarceloFilho:请打开一个新问题,描述问题(以便每个人都可以重现),并链接此问题。
【解决方案2】:

你可以看到这个topic

我不使用 GIT,但如果你能找到 .gitmodules 文件,可能很容易循环每个子模块:

<command to find all of your submodules>|while read; do
    # ... (default use $REPLY as an item)
done

或者:

while read; do
    # ...
done <<< "$(command to find all of your submodules)"

看到这个reminder on how to read a command output with a loop in Bash

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2022-08-09
    • 2022-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多