How does git get after getting the repo name it works?

I have a git post get hook that will invoke the build in my build system. I need to create the string "$ repo-name + $ branch" in the hook script.

I can parse the branch, but how can I get the repository name from git?

Thank!

+23
git
Apr 7 2018-11-11T00:
source share
4 answers

"Repository name" is not a well-defined idea in git, I think. Perhaps the most useful is to return whatever.git in the case of an open repository or whatever in the case of a repository with a working tree. I tested that this Bourne shell bit deals with both cases due to the 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 case of open storage, you can add a line in the first case to disable it:

  REPOSITORY_BASENAME=$(basename "$PWD") REPOSITORY_BASENAME=${REPOSITORY_BASENAME%.git} 
+37
Apr 7 2018-11-11T00:
source share

You can check $ GIT_DIR or $ GIT_WORK_TREE and get the repo name there.

+1
Apr 07 2018-11-11T00:
source share

You can make git rev-parse --show-toplevel , which will give you the path to the top-level directory and pull the name out of it (reponame.git is common for remote access repositories).

$PWD may also have the same information, but I'm not sure.

0
Apr 7 2018-11-11T00:
source share

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

0
Mar 21 '17 at 13:38 on
source share



All Articles