I have a git post receive hook that will trigger a build on my build system. I need to create a string of the form "$repo-name + $branch" in the hook script.
I can parse the branch, but how can I get the repository name from git?
Thanks!
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.
By default hooks are stored in . git/hooks outside of the working tree and are thus not shared between users of the repository. The hooks can be included in a directory within the repository and then each developer can set Git up to use them.
By default the hooks directory is $GIT_DIR/hooks , but that can be changed via the core. hooksPath configuration variable (see git-config[1]). Before Git invokes a hook, it changes its working directory to either $GIT_DIR in a bare repository or the root of the working tree in a non-bare repository.
No. Hooks are per-repository and are never pushed.
The "repository name" isn't a well-defined idea in git, I think. Perhaps what would be most useful is to return whatever.git
in the case of a bare repository or whatever
in the case of a repository with a working tree. I've tested that this bit of Bourne shell deals with both cases properly from within a post-receive
hook:
if [ $(git rev-parse --is-bare-repository) = true ]
then
REPOSITORY_BASENAME=$(basename "$PWD")
else
REPOSITORY_BASENAME=$(basename $(readlink -nf "$PWD"/..))
fi
echo REPOSITORY_BASENAME is $REPOSITORY_BASENAME
Update: if you want to remove the .git
extension in the bare repository case, you could add a line to the first case to strip it off:
REPOSITORY_BASENAME=$(basename "$PWD")
REPOSITORY_BASENAME=${REPOSITORY_BASENAME%.git}
You can inspect $GIT_DIR, or $GIT_WORK_TREE and get the repo name from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With