26

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!

4 Answers 4

43

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}
1
  • 1
    Great. That works. How can I remove the ".git" from a bare repo?
    – Jacko
    Apr 7, 2011 at 16:42
1

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

1
  • 3
    GIT_DIR is just set to . and GIT_WORK_TREE isn't set in the environment for hooks. Apr 7, 2011 at 16:12
0

You could do git rev-parse --show-toplevel, which will give you the path of the top-level directory, and pull the name out of that (reponame.git is conventional for remotely-accessible repos).

$PWD might also have the same information, but I'm unsure.

1
  • 1
    git rev-parse --show-toplevel won't work in a bare repository. ($PWD will be the top level of the working tree for a repository with a working tree, or the repository directory itself for a bare repository, so you need to do a little more work to get a helpful name...) Apr 7, 2011 at 16:07
0

If one is using Gitolite, the GL_REPO variable is available in the post-receive environment by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.