xref: /openbmc/qemu/scripts/git-submodule.sh (revision d2dfe0b5)
1#!/bin/sh
2#
3# This code is licensed under the GPL version 2 or later.  See
4# the COPYING file in the top-level directory.
5
6substat=".git-submodule-status"
7
8command=$1
9shift
10maybe_modules="$@"
11
12# if --with-git-submodules=ignore, do nothing
13test "$command" = "ignore" && exit 0
14
15test -z "$GIT" && GIT=$(command -v git)
16
17cd "$(dirname "$0")/.."
18
19update_error() {
20    echo "$0: $*"
21    echo
22    echo "Unable to automatically checkout GIT submodules '$modules'."
23    echo "If you require use of an alternative GIT binary (for example to"
24    echo "enable use of a transparent proxy), please disable automatic"
25    echo "GIT submodule checkout with:"
26    echo
27    echo " $ ./configure --with-git-submodules=validate"
28    echo
29    echo "and then manually update submodules prior to running make, with:"
30    echo
31    echo " $ GIT='tsocks git' scripts/git-submodule.sh update $modules"
32    echo
33    exit 1
34}
35
36validate_error() {
37    if test "$1" = "validate"; then
38        echo "GIT submodules checkout is out of date, and submodules"
39        echo "configured for validate only. Please run"
40        echo "  scripts/git-submodule.sh update $maybe_modules"
41        echo "from the source directory or call configure with"
42        echo "  --with-git-submodules=update"
43        echo "To disable GIT submodules validation, use"
44        echo "  --with-git-submodules=ignore"
45    fi
46    exit 1
47}
48
49check_updated() {
50    local CURSTATUS OLDSTATUS
51    CURSTATUS=$($GIT submodule status $module)
52    OLDSTATUS=$(grep $module $substat)
53    test "$CURSTATUS" = "$OLDSTATUS"
54}
55
56if test -n "$maybe_modules" && ! test -e ".git"
57then
58    echo "$0: unexpectedly called with submodules but no git checkout exists"
59    exit 1
60fi
61
62if test -n "$maybe_modules" && test -z "$GIT"
63then
64    echo "$0: unexpectedly called with submodules but git binary not found"
65    exit 1
66fi
67
68modules=""
69for m in $maybe_modules
70do
71    $GIT submodule status $m 1> /dev/null 2>&1
72    if test $? = 0
73    then
74        modules="$modules $m"
75    else
76        echo "warn: ignoring non-existent submodule $m"
77    fi
78done
79
80case "$command" in
81status|validate)
82    test -f "$substat" || validate_error "$command"
83    test -z "$maybe_modules" && exit 0
84    for module in $modules; do
85        check_updated $module || validate_error "$command"
86    done
87    exit 0
88    ;;
89update)
90    test -e $substat || touch $substat
91    test -z "$maybe_modules" && exit 0
92
93    $GIT submodule update --init $modules 1>/dev/null
94    test $? -ne 0 && update_error "failed to update modules"
95    for module in $modules; do
96        check_updated $module || echo Updated "$module"
97    done
98
99    (while read -r; do
100        for module in $modules; do
101            case $REPLY in
102                *" $module "*) continue 2 ;;
103            esac
104        done
105        printf '%s\n' "$REPLY"
106    done
107    $GIT submodule status $modules
108    test $? -ne 0 && update_error "failed to save git submodule status" >&2) < $substat > $substat.new
109    mv -f $substat.new $substat
110    ;;
111esac
112
113exit 0
114