Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local executing hook after a git push?

Tags:

git

githooks

I've looked at the githooks manpage but unless I'm missing something I don't see an option for local, post-push git hooks. I'd like to have one that updates the api docs on my web server (for which I already have a script) after I push the master branch to the GitHub repo. Of course I could just write my own script that combines the git push and the api docs run, but that feels somewhat inelegant.

like image 335
scotchi Avatar asked Nov 25 '09 13:11

scotchi


People also ask

Do git hooks get pushed?

No. Hooks are per-repository and are never pushed.

How are git hooks executed?

The pre-receive hook is executed every time somebody uses git push to push commits to the repository. It should always reside in the remote repository that is the destination of the push, not in the originating repository.

How do you run pre-commit hooks locally?

If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook.


1 Answers

Another solution to this problem is to have a wrapper for git push that executes .git/hooks/pre-push and .git/hooks/post-push scripts before and after the git push call. A possible wrapper could 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 then be called as git push-wh if you want to push with hooks.

like image 142
Frank S. Thomas Avatar answered Oct 06 '22 13:10

Frank S. Thomas