update_scripts.sh 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. set -eo pipefail
  3. shopt -s globstar
  4. export LC_ALL=C
  5. cd "$(dirname "$0")"/..
  6. for scr in scripts.d/**/*.sh; do
  7. echo "Processing ${scr}"
  8. (
  9. source "$scr"
  10. if [[ -n "$SCRIPT_SKIP" ]]; then
  11. exit 0
  12. fi
  13. for i in "" $(seq 2 9); do
  14. REPO_VAR="SCRIPT_REPO$i"
  15. COMMIT_VAR="SCRIPT_COMMIT$i"
  16. REV_VAR="SCRIPT_REV$i"
  17. HGREV_VAR="SCRIPT_HGREV$i"
  18. BRANCH_VAR="SCRIPT_BRANCH$i"
  19. TAGFILTER_VAR="SCRIPT_TAGFILTER$i"
  20. CUR_REPO="${!REPO_VAR}"
  21. CUR_COMMIT="${!COMMIT_VAR}"
  22. CUR_REV="${!REV_VAR}"
  23. CUR_HGREV="${!HGREV_VAR}"
  24. CUR_BRANCH="${!BRANCH_VAR}"
  25. CUR_TAGFILTER="${!TAGFILTER_VAR}"
  26. if [[ -z "${CUR_REPO}" ]]; then
  27. if [[ -z "$i" ]]; then
  28. # Mark scripts without repo source for manual check
  29. echo "xxx_CHECKME_xxx" >> "$scr"
  30. echo "Needs manual check."
  31. fi
  32. break
  33. fi
  34. if [[ -n "${CUR_REV}" ]]; then # SVN
  35. echo "Checking svn rev for ${CUR_REPO}..."
  36. NEW_REV="$(svn info --password="" "${CUR_REPO}" | grep ^Revision: | cut -d" " -f2 | xargs)"
  37. echo "Got ${NEW_REV} (current: ${CUR_REV})"
  38. if [[ "${NEW_REV}" != "${CUR_REV}" ]]; then
  39. echo "Updating ${scr}"
  40. sed -i "s/^${REV_VAR}=.*/${REV_VAR}=\"${NEW_REV}\"/" "${scr}"
  41. fi
  42. elif [[ -n "${CUR_HGREV}" ]]; then # HG
  43. hg init tmphgrepo
  44. trap "rm -rf tmphgrepo" EXIT
  45. cd tmphgrepo
  46. NEW_HGREV="$(hg in -f -n -l 1 "${CUR_REPO}" | grep changeset | cut -d: -f3 | xargs)"
  47. cd ..
  48. rm -rf tmphgrepo
  49. echo "Got ${NEW_HGREV} (current: ${CUR_HGREV})"
  50. if [[ "${NEW_HGREV}" != "${CUR_HGREV}" ]]; then
  51. echo "Updating ${scr}"
  52. sed -i "s/^${HGREV_VAR}=.*/${HGREV_VAR}=\"${NEW_HGREV}\"/" "${scr}"
  53. fi
  54. elif [[ -n "${CUR_COMMIT}" ]]; then # GIT
  55. if [[ -n "${CUR_TAGFILTER}" ]]; then
  56. NEW_COMMIT="$(git -c 'versionsort.suffix=-' ls-remote --exit-code --tags --refs --sort "v:refname" "${CUR_REPO}" "refs/tags/${CUR_TAGFILTER}" | tail -n1 | cut -d/ -f3- | xargs)"
  57. else
  58. if [[ -z "${CUR_BRANCH}" ]]; then
  59. # Fetch default branch name
  60. CUR_BRANCH="$(git remote show "${CUR_REPO}" | grep "HEAD branch:" | cut -d":" -f2 | xargs)"
  61. echo "Found default branch ${CUR_BRANCH}"
  62. fi
  63. NEW_COMMIT="$(git ls-remote --exit-code --heads --refs "${CUR_REPO}" refs/heads/"${CUR_BRANCH}" | cut -f1)"
  64. fi
  65. echo "Got ${NEW_COMMIT} (current: ${CUR_COMMIT})"
  66. if [[ "${NEW_COMMIT}" != "${CUR_COMMIT}" ]]; then
  67. echo "Updating ${scr}"
  68. sed -i "s/^${COMMIT_VAR}=.*/${COMMIT_VAR}=\"${NEW_COMMIT}\"/" "${scr}"
  69. fi
  70. else
  71. # Mark scripts with unknown layout for manual check
  72. echo "xxx_CHECKME_UNKNOWN_xxx" >> "$scr"
  73. echo "Unknown layout. Needs manual check."
  74. break
  75. fi
  76. done
  77. )
  78. echo
  79. done