git-submodule.sh 3.1 KB

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