check-commit.sh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. COMMIT_MESSAGE_SUBJECT_FORMAT="%s"
  3. set -eu
  4. res=0
  5. die() { echo "$@" >&2; exit 1; }
  6. fail() { echo "*** $@ ***" >&2; res=1; }
  7. git_log_format() {
  8. local pattern="$1"
  9. local reference="$2"
  10. git log -1 --pretty=format:"$pattern" "$reference"
  11. }
  12. check_subject() {
  13. local commit="$1"
  14. local subject="$2"
  15. # get the prefix
  16. if ! [[ "${subject}" =~ ^([^:]+):.*$ ]] ; then
  17. fail "[${commit}] prefix not detected:'${subject}'"
  18. return
  19. fi
  20. prefix="${BASH_REMATCH[1]}"
  21. if [ -z "${prefix}" ] ; then
  22. fail "[${commit}] commit subject has no prefix:'${subject}'"
  23. return
  24. fi
  25. # core or lib
  26. if [ -d "src/${prefix}" ] ; then
  27. echo "[${commit}] prefix is core or lib, OK[${prefix}]"
  28. return
  29. fi
  30. # prefix is a module
  31. if [ -d "src/modules/${prefix}" ] ; then
  32. echo "[${commit}] prefix is module, OK[${prefix}]"
  33. return
  34. fi
  35. # utils?
  36. if [ -d "${prefix}" ] ; then
  37. echo "[${commit}] prefix is a dir in the repo, OK[${prefix}]"
  38. return
  39. fi
  40. # file (e.g., ChangeLog, etc/kamailio.cfg)
  41. if [ -f "${prefix}" ] ; then
  42. echo "[${commit}] prefix is a file in the repo, OK[${prefix}]"
  43. return
  44. fi
  45. # shortcut to src/ file (e.g., src/Makefile.defs)
  46. if [ -f "src/${prefix}" ] ; then
  47. echo "[${commit}] prefix is a file in the src repo, OK[${prefix}]"
  48. return
  49. fi
  50. # github configs
  51. if [[ "${prefix}" =~ ^github$ ]] ; then
  52. echo "[${commit}] prefix is github config, OK[${prefix}]"
  53. return
  54. fi
  55. fail "[${commit}] unknown prefix:'${prefix}'"
  56. }
  57. target="${GITHUB_BASE_REF:-master}"
  58. if [ "${CI:-}" ]; then
  59. git rev-parse -q --no-revs --verify "origin/${target}" || \
  60. git rev-parse -q --no-revs --verify "${target}" || \
  61. git fetch origin --depth=1 "${target}"
  62. git rev-parse -q --no-revs --verify "origin/${target}" || \
  63. git rev-parse -q --no-revs --verify "${target}" || \
  64. git fetch origin --depth=1 tag "${target}"
  65. # Ensure that the target revision has some history
  66. target_sha=$(git rev-parse -q --verify "origin/${target}" || git rev-parse -q --verify "${target}")
  67. git fetch -q "--depth=${FETCH_DEPTH:-50}" origin "+${target_sha}"
  68. else
  69. target_sha=$(git rev-parse -q --verify "${target}") || die "fatal: couldn't find ref ${target}"
  70. fi
  71. ref=${ref:-HEAD}
  72. # Ensure that the ref revision has some history
  73. git fetch -q "--depth=${FETCH_DEPTH:-50}" origin "+${ref}"
  74. src_sha=$(git rev-parse -q --verify "${ref}") || die "fatal: couldn't find ref ${ref}"
  75. echo "Checking $(git rev-list --count "${src_sha}" "^${target_sha}") commits since revision ${target_sha}"
  76. for commit in $(git rev-list --reverse "${src_sha}" "^${target_sha}"); do
  77. COMMIT_MESSAGE_SUBJECT=$(git_log_format "${COMMIT_MESSAGE_SUBJECT_FORMAT}" "${commit}")
  78. check_subject "${commit}" "${COMMIT_MESSAGE_SUBJECT}"
  79. done
  80. echo "Result: ${res}"
  81. exit $res