2
0

git-submodule.sh 2.9 KB

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