Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 21st, 2012  |  syntax: None  |  size: 2.45 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. git-update-server-info
  14. is_bare=$(git-config --get --bool core.bare)
  15. if [ -z "$is_bare" ]
  16. then
  17.       # for compatibility's sake, guess
  18.       git_dir_full=$(cd $GIT_DIR; pwd)
  19.       case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
  20. fi
  21. update_wc() {
  22.       ref=$1
  23.       echo "Push to checked out branch $ref" >&2
  24.       if [ ! -f $GIT_DIR/logs/HEAD ]
  25.       then
  26.              echo "E:push to non-bare repository requires a HEAD reflog" >&2
  27.              exit 1
  28.       fi
  29.       if (cd $GIT_WORK_TREE; git-diff-files -q --exit-code >/dev/null)
  30.       then
  31.              wc_dirty=0
  32.       else
  33.              echo "W:unstaged changes found in working copy" >&2
  34.              wc_dirty=1
  35.              desc="working copy"
  36.       fi
  37.       if git diff-index --cached HEAD@{1} >/dev/null
  38.       then
  39.              index_dirty=0
  40.       else
  41.              echo "W:uncommitted, staged changes found" >&2
  42.              index_dirty=1
  43.              if [ -n "$desc" ]
  44.              then
  45.                    desc="$desc and index"
  46.              else
  47.                    desc="index"
  48.              fi
  49.       fi
  50.       if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
  51.       then
  52.              new=$(git rev-parse HEAD)
  53.              echo "W:stashing dirty $desc - see git-stash(1)" >&2
  54.              ( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
  55.              git-update-ref --no-deref HEAD HEAD@{1}
  56.              cd $GIT_WORK_TREE
  57.              git stash save "dirty $desc before update to $new";
  58.              git-symbolic-ref HEAD "$ref"
  59.              )
  60.       fi
  61.       # eye candy - show the WC updates :)
  62.       echo "Updating working copy" >&2
  63.       (cd $GIT_WORK_TREE
  64.       git-diff-index -R --name-status HEAD >&2
  65.       git-reset --hard HEAD)
  66. }
  67. if [ "$is_bare" = "false" ]
  68. then
  69.       active_branch=`git-symbolic-ref HEAD`
  70.       export GIT_DIR=$(cd $GIT_DIR; pwd)
  71.       GIT_WORK_TREE=${GIT_WORK_TREE-..}
  72.       for ref
  73.       do
  74.              if [ "$ref" = "$active_branch" ]
  75.              then
  76.                    update_wc $ref
  77.              fi
  78.       done
  79. fi