Skip to content

Commit a33270f

Browse files
authored
Merge pull request #5060 from Flamefire/submodule-script
Add script to gather git submodules
2 parents 00eb94f + 99bb5a8 commit a33270f

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
##
3+
# Copyright 2025-2026 Ghent University
4+
#
5+
# This file is part of EasyBuild,
6+
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
7+
# with support of Ghent University (http://ugent.be/hpc),
8+
# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
9+
# Flemish Research Foundation (FWO) (http://www.fwo.be/en)
10+
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
11+
#
12+
# https://github.com/easybuilders/easybuild
13+
#
14+
# EasyBuild is free software: you can redistribute it and/or modify
15+
# it under the terms of the GNU General Public License as published by
16+
# the Free Software Foundation v2.
17+
#
18+
# EasyBuild is distributed in the hope that it will be useful,
19+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
# GNU General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU General Public License
24+
# along with EasyBuild. If not, see <http://www.gnu.org/licenses/>.
25+
##
26+
"""
27+
Utility to gather submodules of an existing, recursively cloned repository
28+
in the current working directory.
29+
30+
The output is a dictionary mapping submodule names to their repository slug, commit hash and relative path.
31+
32+
author: Alexander Grund (TU Dresden)
33+
"""
34+
35+
36+
echo "Print information on all submodules for the git repository in $PWD"
37+
echo
38+
echo "Format: '<name>': ('<owner>/<name>', '<commit>', '<path>'),"
39+
echo
40+
41+
function print_submodules {
42+
local base_path=$1
43+
git -C "$base_path" submodule status | while read -r line; do
44+
# Example line: " 3a6b7dc libs/foo (heads/main)"
45+
commit=$(echo "$line" | awk '{print $1}')
46+
path=$(echo "$line" | awk '{print $2}')
47+
48+
if [[ -z $base_path ]]; then
49+
sub_folder=$path
50+
else
51+
sub_folder=$base_path/$path
52+
fi
53+
54+
# Extract owner/repo from URL
55+
url=$(git config --file "${base_path:-$PWD}/.gitmodules" --get "submodule.$path.url")
56+
repo_slug=$(echo "$url" | sed -E 's#.*[:/]([^/:]+/[^/.]+)(\.git)?$#\1#')
57+
58+
name=$(basename "$path")
59+
short_commit=$(git -C "$sub_folder" rev-parse --short "$commit" 2>/dev/null || echo "$commit")
60+
61+
printf "'%s': ('%s', '%s', '%s'),\n" "$name" "$repo_slug" "$short_commit" "$sub_folder"
62+
print_submodules "$sub_folder"
63+
done
64+
}
65+
66+
print_submodules "" | sort

0 commit comments

Comments
 (0)