blob: 335f7f5fdf89e3ff33e3282bd126829d845c8dc1 [file] [log] [blame]
Daniel P. Berrangeaef45d52017-09-29 11:11:56 +01001#!/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
Daniel P. Berrangeaef45d52017-09-29 11:11:56 +01006substat=".git-submodule-status"
7
8command=$1
9shift
Daniel P. Berrange37b5e742017-10-27 10:49:58 +010010maybe_modules="$@"
Daniel P. Berrangeaef45d52017-09-29 11:11:56 +010011
Paolo Bonzini8edddaa2023-06-18 23:10:39 +020012test -z "$maybe_modules" && exit 0
Paolo Bonzini50cfed82023-05-25 12:36:28 +020013test -z "$GIT" && GIT=$(command -v git)
Daniel P. Berrangecc84d632017-10-20 15:02:43 +010014
Dan Streetman7d7dbf92021-01-19 12:20:46 -050015cd "$(dirname "$0")/.."
16
Paolo Bonzini8edddaa2023-06-18 23:10:39 +020017no_git_error=
18if ! test -e ".git"; then
19 no_git_error='no git checkout exists'
20elif test -z "$GIT"; then
21 no_git_error='git binary not found'
22fi
23
24is_git() {
25 test -z "$no_git_error"
26}
27
Dan Streetman7d7dbf92021-01-19 12:20:46 -050028update_error() {
Daniel P. Berrangecc84d632017-10-20 15:02:43 +010029 echo "$0: $*"
30 echo
31 echo "Unable to automatically checkout GIT submodules '$modules'."
32 echo "If you require use of an alternative GIT binary (for example to"
Paolo Bonzini50cfed82023-05-25 12:36:28 +020033 echo "enable use of a transparent proxy), please disable automatic"
34 echo "GIT submodule checkout with:"
Daniel P. Berrangef62bbee2017-10-26 13:52:26 +010035 echo
Paolo Bonzini6f3ae232023-05-30 16:03:50 +020036 echo " $ ./configure --disable-download"
Daniel P. Berrangef62bbee2017-10-26 13:52:26 +010037 echo
38 echo "and then manually update submodules prior to running make, with:"
39 echo
Paolo Bonzini50cfed82023-05-25 12:36:28 +020040 echo " $ GIT='tsocks git' scripts/git-submodule.sh update $modules"
Daniel P. Berrangef62bbee2017-10-26 13:52:26 +010041 echo
Daniel P. Berrangecc84d632017-10-20 15:02:43 +010042 exit 1
43}
44
Dan Streetman7d7dbf92021-01-19 12:20:46 -050045validate_error() {
Paolo Bonzini8edddaa2023-06-18 23:10:39 +020046 if is_git && test "$1" = "validate"; then
Dan Streetman7d7dbf92021-01-19 12:20:46 -050047 echo "GIT submodules checkout is out of date, and submodules"
48 echo "configured for validate only. Please run"
49 echo " scripts/git-submodule.sh update $maybe_modules"
50 echo "from the source directory or call configure with"
Paolo Bonzini6f3ae232023-05-30 16:03:50 +020051 echo " --enable-download"
Dan Streetman7d7dbf92021-01-19 12:20:46 -050052 fi
53 exit 1
54}
55
Paolo Bonzinid1201162023-05-30 17:27:48 +020056check_updated() {
57 local CURSTATUS OLDSTATUS
58 CURSTATUS=$($GIT submodule status $module)
59 OLDSTATUS=$(grep $module $substat)
60 test "$CURSTATUS" = "$OLDSTATUS"
61}
62
Paolo Bonzini8edddaa2023-06-18 23:10:39 +020063if is_git; then
64 test -e $substat || touch $substat
65 modules=""
66 for m in $maybe_modules
67 do
68 $GIT submodule status $m 1> /dev/null 2>&1
69 if test $? = 0
70 then
71 modules="$modules $m"
72 grep $m $substat > /dev/null 2>&1 || $GIT submodule status $module >> $substat
73 else
74 echo "warn: ignoring non-existent submodule $m"
75 fi
76 done
77else
78 modules=$maybe_modules
Daniel P. Berrangédd84a902022-04-26 14:06:49 +010079fi
80
Daniel P. Berrangeaef45d52017-09-29 11:11:56 +010081case "$command" in
Dan Streetman7d7dbf92021-01-19 12:20:46 -050082status|validate)
Juan Quintela1a920d22020-01-29 11:21:13 +010083 for module in $modules; do
Paolo Bonzini8edddaa2023-06-18 23:10:39 +020084 if is_git; then
85 check_updated $module || validate_error "$command"
86 elif ! (set xyz "$module"/* && test -e "$2"); then
87 # The directory does not exist or it contains no files
88 echo "$0: sources not available for $module and $no_git_error"
89 validate_error "$command"
90 fi
Juan Quintela1a920d22020-01-29 11:21:13 +010091 done
Daniel P. Berrangeaef45d52017-09-29 11:11:56 +010092 ;;
Paolo Bonzini8edddaa2023-06-18 23:10:39 +020093
Daniel P. Berrangeaef45d52017-09-29 11:11:56 +010094update)
Paolo Bonzini8edddaa2023-06-18 23:10:39 +020095 is_git || {
96 echo "$0: unexpectedly called with submodules but $no_git_error"
97 exit 1
98 }
Daniel P. Berrange49ad3cf2017-10-30 09:29:29 +010099
Daniel P. Berrangecc84d632017-10-20 15:02:43 +0100100 $GIT submodule update --init $modules 1>/dev/null
Dan Streetman7d7dbf92021-01-19 12:20:46 -0500101 test $? -ne 0 && update_error "failed to update modules"
Paolo Bonzinid1201162023-05-30 17:27:48 +0200102 for module in $modules; do
103 check_updated $module || echo Updated "$module"
104 done
Daniel P. Berrangecc84d632017-10-20 15:02:43 +0100105
Paolo Bonzinifdb8fd82023-05-30 17:10:29 +0200106 (while read -r; do
107 for module in $modules; do
108 case $REPLY in
109 *" $module "*) continue 2 ;;
110 esac
111 done
112 printf '%s\n' "$REPLY"
113 done
114 $GIT submodule status $modules
115 test $? -ne 0 && update_error "failed to save git submodule status" >&2) < $substat > $substat.new
116 mv -f $substat.new $substat
Daniel P. Berrangeaef45d52017-09-29 11:11:56 +0100117 ;;
118esac
Daniel P. Berrangecc84d632017-10-20 15:02:43 +0100119
120exit 0