xref: /openbmc/qemu/scripts/git-submodule.sh (revision bc0ec52e)
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 not running in a git checkout, 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 --disable-download"
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 "  --enable-download"
43    fi
44    exit 1
45}
46
47check_updated() {
48    local CURSTATUS OLDSTATUS
49    CURSTATUS=$($GIT submodule status $module)
50    OLDSTATUS=$(grep $module $substat)
51    test "$CURSTATUS" = "$OLDSTATUS"
52}
53
54if test -n "$maybe_modules" && ! test -e ".git"
55then
56    echo "$0: unexpectedly called with submodules but no git checkout exists"
57    exit 1
58fi
59
60if test -n "$maybe_modules" && test -z "$GIT"
61then
62    echo "$0: unexpectedly called with submodules but git binary not found"
63    exit 1
64fi
65
66modules=""
67for m in $maybe_modules
68do
69    $GIT submodule status $m 1> /dev/null 2>&1
70    if test $? = 0
71    then
72        modules="$modules $m"
73    else
74        echo "warn: ignoring non-existent submodule $m"
75    fi
76done
77
78case "$command" in
79status|validate)
80    test -f "$substat" || validate_error "$command"
81    test -z "$maybe_modules" && exit 0
82    for module in $modules; do
83        check_updated $module || validate_error "$command"
84    done
85    exit 0
86    ;;
87update)
88    test -e $substat || touch $substat
89    test -z "$maybe_modules" && exit 0
90
91    $GIT submodule update --init $modules 1>/dev/null
92    test $? -ne 0 && update_error "failed to update modules"
93    for module in $modules; do
94        check_updated $module || echo Updated "$module"
95    done
96
97    (while read -r; do
98        for module in $modules; do
99            case $REPLY in
100                *" $module "*) continue 2 ;;
101            esac
102        done
103        printf '%s\n' "$REPLY"
104    done
105    $GIT submodule status $modules
106    test $? -ne 0 && update_error "failed to save git submodule status" >&2) < $substat > $substat.new
107    mv -f $substat.new $substat
108    ;;
109esac
110
111exit 0
112