git-update-mirror 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2009 Nagios Plugins Development Team
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. set -e
  18. set -u
  19. prefix='/home/git'
  20. PATH="$prefix/opt/git/bin:/bin:/usr/bin"
  21. export PATH
  22. gitnotify="$prefix/libexec/git-notify"
  23. recipient='Nagios Plugins Commit List <nagiosplug-checkins@lists.sourceforge.net>'
  24. maxcommits=100
  25. maxdiffsize=$((300 * 1024))
  26. gitweburl='http://repo.or.cz/w'
  27. tempprefix='/dev/shm'
  28. fourtyzeros=$(printf '%.40u' 0)
  29. myself=${0##*/}
  30. checkrefs()
  31. {
  32. turn=$1
  33. git show-ref | while read object ref
  34. do
  35. refdir="$tempdir/${ref%/*}"
  36. reffile="$tempdir/$ref"
  37. if [ $turn -eq 2 -a -f "$reffile" ] \
  38. && grep "^1 $object$" "$reffile" >'/dev/null'
  39. then # The ref has not been modified.
  40. rm -f "$reffile"
  41. else
  42. mkdir -p "$refdir"
  43. echo "$turn $object" >>"$reffile"
  44. fi
  45. done
  46. }
  47. if [ $# -lt 1 ]
  48. then
  49. echo >&2 "Usage: $myself <repository> ..."
  50. exit 1
  51. fi
  52. tempdir=$(mktemp -d "$tempprefix/$myself.XXXXXX")
  53. tempfile=$(mktemp "$tempprefix/$myself.XXXXXX")
  54. trap 'rm -rf "$tempdir" "$tempfile"' EXIT
  55. for repository in "$@"
  56. do
  57. cd "$repository"
  58. checkrefs 1
  59. if ! git remote update --prune >"$tempfile" 2>&1
  60. then
  61. cat >&2 "$tempfile"
  62. exit 1
  63. fi
  64. git fetch --quiet --tags
  65. checkrefs 2
  66. find "$tempdir" -type 'f' -print | while read reffile
  67. do
  68. ref=${reffile#$tempdir/}
  69. old=$(awk '$1 == "1" { print $2; exit }' "$reffile")
  70. new=$(awk '$1 == "2" { print $2; exit }' "$reffile")
  71. old=${old:-$fourtyzeros}
  72. new=${new:-$fourtyzeros}
  73. echo "$old" "$new" "$ref"
  74. done | $gitnotify \
  75. -m "$recipient" \
  76. -n "$maxcommits" \
  77. -s "$maxdiffsize" \
  78. -u "$gitweburl"
  79. cd "$OLDPWD"
  80. done