Advertisement
mechanicker

git pre-receive hook for mirroring

Jul 18th, 2020
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.29 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # Time-stamp: <2020-07-20 08:35:15 dhruva>
  3. #------------------------------------------------------------------------------
  4. # git repository mirroring based on pre-receive hook
  5. #
  6. # Inspired by:
  7. # https://docs.gitlab.com/ee/user/project/repository/repository_mirroring.html
  8. #------------------------------------------------------------------------------
  9. set -e
  10.  
  11. # Circumvent push to mirror from quarantine directory
  12. unset GIT_QUARANTINE_PATH
  13.  
  14. error=0
  15.  
  16. # Perfrom rollback if previous failed to ensure mirror and primary are in-sync
  17. function rollback()
  18. {
  19.     if [ -f ./rollback.sh ]; then
  20.         echo "executing rollback"
  21.         chmod +x ./rollback.sh
  22.         ./rollback.sh
  23.         rm -f rollback.sh
  24.     fi
  25. }
  26.  
  27. # Recover from failed previous push
  28. rollback
  29.  
  30. # Disable bailing on error since we want to rollback
  31. set +e
  32. while read oldrev newrev refname; do
  33.     mirror_cmd="git push --quiet --force ../mirror.bare ${newrev}:${refname}"
  34.     echo ${mirror_cmd} >> rollback.sh
  35.     eval ${mirror_cmd} 2>&1
  36.     error=$?
  37.     if [ ${error} != 0 ]; then
  38.         echo "failed to mirror repository, will attempt rollback"
  39.         break
  40.     fi
  41. done < "${1:-/dev/stdin}"
  42.  
  43. # Restore bailing on error since we want to preserve rollback file on error
  44. set -e
  45. if [ ${error} != 0 ]; then
  46.     rollback
  47. fi
  48.  
  49. rm -f rollback.sh
  50.  
  51. exit ${error}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement