THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.7KB

  1. #!/bin/sh
  2. #
  3. # This hook checks that all local sources specified in the staged APKBUILDs
  4. # are staged too or already committed and that checksums are correct.
  5. #
  6. set -eu
  7. # Maximal allowed size (in bytes) of a file.
  8. FILE_SIZE_LIMIT=262144 # 256 kiB
  9. if ! command -v sha512sum >/dev/null; then
  10. # macOS / BSDs (?) don't have sha512sum, but shasum.
  11. alias sha512sum='shasum -a 512'
  12. fi
  13. error() {
  14. printf '\033[0;31mpre-commit:\033[0m %s\n' "$1" >&2 # red
  15. }
  16. # Prints paths of created or modified files being committed.
  17. changed_files() {
  18. git diff-index \
  19. --name-only \
  20. --cached \
  21. --diff-filter=ACMR HEAD \
  22. -- "$@"
  23. }
  24. # Prints file names and checksums (in format <SHA-512>:<filename>) of local
  25. # sources specified in the APKBUILD ($1).
  26. abuild_local_sources() {
  27. apkbuild="$1"
  28. set +eu
  29. . "$apkbuild" || {
  30. error "$apkbuild is invalid"
  31. return 1
  32. }
  33. set -eu
  34. status=0
  35. : ${source:=""}
  36. for src in $source; do
  37. # Skip remote sources.
  38. case "$src" in */*) continue;; esac
  39. echo "$sha512sums" | awk -v src="$src" '
  40. { if ($2 == src) { ok=1; print($2 ":" $1) } }
  41. END { if (ok != 1) exit 1 }' || {
  42. status=1
  43. error "${apkbuild%/*}: file \"$src\" is missing in \$sha512sums (hint: run abuild checksum)"
  44. }
  45. done
  46. return $status
  47. }
  48. # check if given object is a symlink
  49. is_symlink() {
  50. test "$(git ls-files --stage "$1" | awk '{print $1}')" = "120000"
  51. }
  52. # Checks that all local sources specified in the APKBUILD file ($1) are
  53. # available in git tree and checksums are correct.
  54. check_local_sources() {
  55. local apkbuild="$1"
  56. local startdir="${apkbuild%/*}"
  57. local status=0
  58. local checksum_act checksum_exp content filename line sources
  59. sources=$(abuild_local_sources "$apkbuild")
  60. for line in $sources; do
  61. filename=${line%%:*}
  62. checksum_exp=${line#*:}
  63. if ! git cat-file -e ":$startdir/$filename" 2>/dev/null; then
  64. error "$startdir: missing file \"$filename\""
  65. status=1
  66. continue
  67. fi
  68. if is_symlink ":$startdir/$filename"; then
  69. continue
  70. fi
  71. checksum_act=$(git show ":$startdir/$filename" | sha512sum)
  72. if [ "$checksum_act" != "$checksum_exp -" ]; then
  73. error "$startdir: bad checksum for file \"$filename\" (hint: run abuild checksum)"
  74. status=1
  75. fi
  76. done
  77. return $status
  78. }
  79. # Checks if the file ($1) being committed is not bigger than FILE_SIZE_LIMIT.
  80. check_file_size() {
  81. local path="$1"
  82. local size
  83. size=$(git cat-file -s ":$path")
  84. if [ $size -gt $FILE_SIZE_LIMIT ]; then
  85. local size_kb=$(( size / 1024 ))
  86. error "file \"$path\" is quite big ($(( size / 1024 )) kiB), better to upload it to dev.alpinelinux.org"
  87. return 1
  88. fi
  89. }
  90. for apkbuild in $(changed_files '**/APKBUILD'); do
  91. check_local_sources "$apkbuild"
  92. done
  93. for path in $(changed_files); do
  94. check_file_size "$path"
  95. done