使用 "git submodule foreach" 可以跳过子模块列表吗?

using "git submodule foreach" can you skip a list of submodules?

假设我有 10 个 submoules:

module/1
module/2
module/3
module/4
module/5
module/6
module/7
module/8
module/9
module/10

其中 module/ 是顶级存储库。

我想做 git submodule foreach 'git status',但我不想为子模块 4、6 和 7 做。

有没有办法做到这一点,比如:

git submodule foreach --exclude="4 6 7" 'git status'

我尝试在命令方块中使用

git submodule foreach '
    if [[ $list_of_ignores =~ *"$displayname"* ]] ; then echo ignore; fi
'

更新 - 删除了 --exclude="4 6 7" 不小心放在里面的

但我收到错误提示 eval [[: not found - 我假设这是因为它使用 /bin/sh 而不是 /bin/bash? - 不确定...

正如文档所说,foreach 执行 shell 命令,

foreach [--recursive] <command>
    Evaluates an arbitrary shell command in each checked out submodule. The 
    command has access to the variables $name, $sm_path, $displaypath, $sha1
    and $toplevel

所以使用 shell:

 git submodule foreach 'case $name in 4|6|7) ;; *) git status ;; esac'

如果您觉得语法很奇怪,请查看 bash 的 case 语句的语法。以上,如果用带换行符的脚本编写,将是:

case $name in # $name is available to `submodule foreach`
    4|5|6)
     ;;
    *)     # default "catchall"
     git status 
    ;;
esac

这可能是一个糟糕的解决方法,但它适用于我的特定情况。

git submodule foreach --recursive 只会遍历现有文件夹(也是非递归的),所以我通常只是删除文件夹以跳过 (首先确保一切都是 committed/stashed !).

所以在以下子模块结构的情况下:

tree
.
├── 1
├── 2
│   ├── 3
│   └── 4
├── 5
│   ├── 6
│   │   ├── 7
│   └── 8
└── 9

如果我想在除 5 和子模块之外的每个子模块上执行 foo 命令,我只需删除 5 文件夹:

rm -rf 5
git submodule --recursive foo             # 5, 6, 7, 8 won't be touched.
git submodule --update --init --recursive # Restore the removed folders.