/
post-merge-checkout
48 lines (40 loc) · 1.51 KB
/
post-merge-checkout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
# Check if any submodule has been updated in HEAD after a merge (or
# pull) or a branch checkout. If so, ask if user wants to run
# git-submodule update.
# --Chaitanya Gupta
SCRIPT_NAME=$(basename "$0")
# If run as a post-checkout script hook, and the previous and current
# HEAD are the same or if its a file checkout, don't proceed
if [[ "$SCRIPT_NAME" = "post-checkout" && "$1" = "$2" || "$3" = "0" ]]; then
exit 0
fi
echo "Checking for new or changed submodules..."
# Jump to the current project's root directory (the one containing
# .git/)
ROOT_DIR=$(git rev-parse --show-cdup)
# Finding existing submodules that have been modified
SUBMODULES=$(grep path ${ROOT_DIR}.gitmodules | sed 's/^.*path = //')
MOD_SUBMODULES=$(git diff --name-only --ignore-submodules=dirty | grep -F "$SUBMODULES")
# Find new submodules
NEW_SUBMODULES=$(git submodule status | egrep '^-' | cut -f2 -d' ')
# If no changed submodules, exit with status code 0, else prompt the
# user and exit accordingly
if [[ -n "$MOD_SUBMODULES" || -n "$NEW_SUBMODULES" ]]; then
echo "The following submodules have been updated in HEAD:"
if [[ -n "$MOD_SUBMODULES" ]]; then
echo "$MOD_SUBMODULES"
fi
if [[ -n "$NEW_SUBMODULES" ]]; then
echo "$NEW_SUBMODULES"
fi
echo -n "Update submodules? [n] "
read -n 1 reply </dev/tty
echo
if [[ "$reply" == "y" || "$reply" == "Y" ]]; then
if [[ -n "$NEW_SUBMODULES" ]]; then
git submodule init
fi
git submodule update
fi
fi