Skip to content

Commit 0a71122

Browse files
committed
Add pre-commit hook and scripts to manage submodule SHAs to ensure submodules consistency when using remotes/devtools install_github w or wo ref
1 parent 46df2bb commit 0a71122

4 files changed

Lines changed: 81 additions & 3 deletions

File tree

.gitmodules-shas

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file records the specific commit SHAs for each submodule
2+
# It is automatically generated and should be updated when submodules are updated
3+
# Format: <submodule_path> <commit_sha>
4+
src/libK b23966bf6c44edbc098aa4f0c2aeb2e3f8261645
5+
src/slapack 72a7f3753e02b0471e93296f2b22877c6fee7328

tools/pre-commit-submodule-hook.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
# Pre-commit hook to ensure .gitmodules-shas is updated when submodules change
3+
# To install this hook: cp tools/pre-commit-submodule-hook.sh .git/hooks/pre-commit
4+
5+
set -e
6+
7+
# Check if any submodule changes are being committed
8+
if git diff --cached --name-only | grep -q "^src/libK\|^src/slapack"; then
9+
echo "Submodule changes detected, updating .gitmodules-shas..."
10+
11+
# Update the .gitmodules-shas file
12+
./tools/update_submodule_shas.sh
13+
14+
# Stage the updated file
15+
git add .gitmodules-shas
16+
17+
echo "✓ .gitmodules-shas updated and staged"
18+
fi

tools/setup.sh

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ if [ ! -f "$LIBKRIGING_SRC_PATH/CMakeLists.txt" ]; then
3232
# Manually clone each submodule
3333
echo " → Not a git repository, manually cloning submodules..."
3434

35+
# Check if .gitmodules-shas file exists (contains specific commit SHAs)
36+
SHAS_FILE=".gitmodules-shas"
37+
if [ -f "$SHAS_FILE" ]; then
38+
echo " → Found $SHAS_FILE with pinned submodule versions"
39+
fi
40+
3541
# Parse .gitmodules and clone each submodule
3642
while IFS= read -r line; do
3743
if echo "$line" | grep -q "^\[submodule"; then
@@ -45,9 +51,30 @@ if [ ! -f "$LIBKRIGING_SRC_PATH/CMakeLists.txt" ]; then
4551

4652
# When we have both path and URL, clone the submodule
4753
if [ -n "$current_path" ] && [ -n "$current_url" ]; then
48-
echo " → Cloning $current_url into $current_path..."
49-
rm -rf "$current_path"
50-
git clone --depth 1 --recursive "$current_url" "$current_path"
54+
# Check if we have a specific commit SHA for this submodule
55+
target_sha=""
56+
if [ -f "$SHAS_FILE" ]; then
57+
target_sha=$(grep "^${current_path} " "$SHAS_FILE" | awk '{print $2}')
58+
fi
59+
60+
if [ -n "$target_sha" ]; then
61+
echo " → Cloning $current_url into $current_path at commit $target_sha..."
62+
rm -rf "$current_path"
63+
# Clone and checkout specific commit
64+
git clone --no-checkout "$current_url" "$current_path"
65+
cd "$current_path"
66+
git checkout "$target_sha"
67+
# Initialize nested submodules if any
68+
if [ -f ".gitmodules" ]; then
69+
git submodule update --init --recursive
70+
fi
71+
cd - > /dev/null
72+
else
73+
echo " → Cloning $current_url into $current_path (latest)..."
74+
rm -rf "$current_path"
75+
git clone --depth 1 --recursive "$current_url" "$current_path"
76+
fi
77+
5178
current_path=""
5279
current_url=""
5380
fi

tools/update_submodule_shas.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
# This script updates the .gitmodules-shas file with the current submodule commit SHAs
3+
# Run this script whenever submodules are updated before creating a new tag/release
4+
5+
set -eo pipefail
6+
7+
echo "Updating .gitmodules-shas with current submodule commits..."
8+
9+
# Get the list of submodules and their commits
10+
git ls-tree HEAD $(git config --file .gitmodules --get-regexp path | awk '{ print $2 }') > .gitmodules-shas.tmp
11+
12+
# Add header and format
13+
{
14+
echo "# This file records the specific commit SHAs for each submodule"
15+
echo "# It is automatically generated and should be updated when submodules are updated"
16+
echo "# Format: <submodule_path> <commit_sha>"
17+
awk '{print $4, $3}' .gitmodules-shas.tmp
18+
} > .gitmodules-shas
19+
20+
rm -f .gitmodules-shas.tmp
21+
22+
echo "✓ Updated .gitmodules-shas:"
23+
cat .gitmodules-shas
24+
25+
echo ""
26+
echo "⚠ Don't forget to commit this file:"
27+
echo " git add .gitmodules-shas"
28+
echo " git commit -m 'Update submodule SHAs'"

0 commit comments

Comments
 (0)