Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can git post-receive hook get name of repo it is running on?

Tags:

git

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!

like image 677
Jacko Avatar asked Apr 07 '11 14:04

Jacko


People also ask

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.

Are Git hooks stored in repo?

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.

What directory do Git hooks run in?

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.

Do Git hooks get pushed?

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


2 Answers

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}
like image 160
Mark Longair Avatar answered Oct 01 '22 22:10

Mark Longair


You can inspect $GIT_DIR, or $GIT_WORK_TREE and get the repo name from there.

like image 31
holygeek Avatar answered Oct 01 '22 21:10

holygeek