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

前端 未结 4 1681
我在风中等你
我在风中等你 2020-12-28 14:40

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

4条回答
  •  清酒与你
    2020-12-28 15:07

    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}
    

提交回复
热议问题