git submodule foreach -q --recursive git rev-parse --git-dir |
awk '{split($0, a, "/modules/", s); print $0, "- Level", length(s)-1}'
Explanation:
git submodule foreach -q --recursive
Run a command on all submodules recursive.
git rev-parse --git-dir
Run this command for every submodule — show the path to its .git/modules
directory. Example:
$ git submodule foreach -q --recursive git rev-parse --git-dir
$root/.git/modules/mod1
$root/.git/modules/mod2
$root/.git/modules/mod3
$root/.git/modules/mod3/modules/subdir/submodule
All modules in the example are level 0 modules, the last one is level 1. Counting slashes wouldn't help as there are subdirectories (a subsubmodule buried deeper than the root of a submodule). Let's count /modules/
with awk
:
awk '{split($0, a, "/modules/", s); print $0, "- Level", length(s)-1}'
Every input line is split by /modules/
and the list of separators is put into the array s
. Then we just count the number of /modules/
and nicely print the result:
git submodule foreach -q --recursive git rev-parse --git-dir |
awk '{split($0, a, "/modules/", s); print $0, "- Level", length(s)-1}'
$root/.git/modules/mod1 - Level 0
$root/.git/modules/mod2 - Level 0
$root/.git/modules/mod3 - Level 0
$root/.git/modules/mod3/modules/subdir/submodule - Level 1
Print submodules workdirs:
$ git submodule foreach -q --recursive 'pwd; git rev-parse --git-dir' |
awk 'NR%2==1 {submodule=$0} NR%2==0 {split($0, a, "/modules/", s); print submodule, "- Level", length(s)-1}':
$root/mod1 - Level 0
$root/third-party/mod2 - Level 0
$root/third-party/mod3 - Level 0
$root/third-party/mod3/subdir/submodule - Level 1