Local capture after pressing git?

I looked at the githooks manpage , but if I did not miss something, I do not see the possibility for local, post-push git hooks. I would like to have one that updates api docs on my web server (for which I already have a script) after I push the main branch to the GitHub repository. Of course, I could just write my own script that combines push git and api docs, but that seems a bit unelegant.

+48
git githooks
Nov 25 '09 at 13:58
source share
5 answers

From Git 1.8.2, a new hook occurs before the push operation: a preliminary push . If the script returns a value other than 0, the push operation will be canceled.

Mentioned in release notes: https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt#L167

Example: https://github.com/git/git/blob/87c86dd14abe8db7d00b0df5661ef8cf147a72a3/templates/hooks--pre-push.sample

+11
Nov 27 '13 at 4:46
source share

Another solution to this problem is to have a shell for git push that runs the .git/hooks/pre-push and .git/hooks/post-push scripts before and after the git push call. A possible shell might look like this:

 #!/bin/sh GIT_DIR_="$(git rev-parse --git-dir)" BRANCH="$(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD))" PRE_PUSH="$GIT_DIR_/hooks/pre-push" POST_PUSH="$GIT_DIR_/hooks/post-push" test -x "$PRE_PUSH" && exec "$PRE_PUSH" "$BRANCH" "$@" git push "$@" test $? -eq 0 && test -x "$POST_PUSH" && exec "$POST_PUSH" "$BRANCH" "$@" 

Saved as git-push-wh somewhere in your PATH , it can be called as git push-wh if you want to click hooks.

+42
Sep 28 '10 at 11:28
source share

This type of hook is not supported by Git. It goes beyond the legitimate reasons for a git hook as indicated by the git companion.

The opening remark in the above related post says almost directly to your case:

I especially don't like hooks that act locally after the start of an operation and act exclusively on local data. Perhaps because I still find Git toolkits suitable for a higher level of scripting more than other people do.

PS "Single Push" hint

  • There are too many caveats for a full explanation, but if you can understand all this, you must deal with the details.

The optional pushurl for a local repo with an alternates object repository can give you a little overhead way to push push hook locally. But really, the effort is much more than git push upstream && update-web-server (possibly in a shell alias, Git alias or script).

+31
Nov 25 '09 at 20:05
source share

I recently ran into the same problem. I need a hook so that a push from my git submodule captures the new submodule link in the "superproject".

As Chris said, the best way is to simply use the git alias, for example:

 $ git config alias.xpush '!git push $1 $2 && update-server.sh' # (remember the backslash before the ! if your shell requires it) 

This adds the following to your .git / config file:

 [alias] xpush = !git push $1 $2 && update-server.sh 

So, if you type:

 $ git xpush 

your changes will be rolled over, and then update-server.sh will be executed.

+29
Aug 12 '10 at 10:09
source share

I use the function for this:

 current_branch() { local ref=$(git symbolic-ref HEAD 2> /dev/null) || return echo ${ref#refs/heads/} } gp() { local post_push="$(git rev-parse --git-dir)/hooks/post-push" git push "$@" && { [[ -x "$post_push" ]] && "$post_push" "$(current_branch)" "$@" } } compdef _git gp=git-push 

The compdef part is for ZSH.

+2
May 23 '14 at 18:53
source share



All Articles