post-update 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/sh
  2. #
  3. # This hook does two things:
  4. #
  5. # 1. update the "info" files that allow the list of references to be
  6. # queries over dumb transports such as http
  7. #
  8. # 2. if this repository looks like it is a non-bare repository, and
  9. # the checked-out branch is pushed to, then update the working copy.
  10. # This makes "push" function somewhat similarly to darcs and bzr.
  11. #
  12. # To enable this hook, make this file executable by "chmod +x post-update".
  13. umask 002
  14. git-update-server-info
  15. is_bare=$(git-config --get --bool core.bare)
  16. if [ -z "$is_bare" ]
  17. then
  18. # for compatibility's sake, guess
  19. git_dir_full=$(cd $GIT_DIR; pwd)
  20. case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
  21. fi
  22. update_wc() {
  23. ref=$1
  24. echo "Push to checked out branch $ref" >&2
  25. if [ ! -f $GIT_DIR/logs/HEAD ]
  26. then
  27. echo "E:push to non-bare repository requires a HEAD reflog" >&2
  28. exit 1
  29. fi
  30. if (cd $GIT_WORK_TREE; git-diff-files -q --exit-code >/dev/null)
  31. then
  32. wc_dirty=0
  33. else
  34. echo "W:unstaged changes found in working copy" >&2
  35. wc_dirty=1
  36. desc="working copy"
  37. fi
  38. if git diff-index --cached HEAD@{1} >/dev/null
  39. then
  40. index_dirty=0
  41. else
  42. echo "W:uncommitted, staged changes found" >&2
  43. index_dirty=1
  44. if [ -n "$desc" ]
  45. then
  46. desc="$desc and index"
  47. else
  48. desc="index"
  49. fi
  50. fi
  51. if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
  52. then
  53. new=$(git rev-parse HEAD)
  54. echo "W:stashing dirty $desc - see git-stash(1)" >&2
  55. ( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
  56. git-update-ref --no-deref HEAD HEAD@{1}
  57. cd $GIT_WORK_TREE
  58. git stash save "dirty $desc before update to $new";
  59. git-symbolic-ref HEAD "$ref"
  60. )
  61. fi
  62. # eye candy - show the WC updates :)
  63. echo "Updating working copy" >&2
  64. (cd $GIT_WORK_TREE
  65. git-diff-index -R --name-status HEAD >&2
  66. git-reset --hard HEAD)
  67. }
  68. if [ "$is_bare" = "false" ]
  69. then
  70. active_branch=`git-symbolic-ref HEAD`
  71. export GIT_DIR=$(cd $GIT_DIR; pwd)
  72. GIT_WORK_TREE=${GIT_WORK_TREE-..}
  73. for ref
  74. do
  75. if [ "$ref" = "$active_branch" ]
  76. then
  77. update_wc $ref
  78. fi
  79. done
  80. fi