Bash 如何从git钩子可靠地找到根git目录?

Bash 如何从git钩子可靠地找到根git目录?,bash,git,githooks,Bash,Git,Githooks,给定2个git存储库(产品A和产品B),以及子模块(CommonSubmodule、SomeOtherSubmodule) 我在网上找到了一个脚本,允许通过post_签出挂钩记录分支 #!/bin/sh previous_head_ref=$1 new_head_ref=$2 is_branch_checkout=$3 if [[ "$previous_head_ref" != "$new_head_ref" ]] && [[ "$is_branch_checkout" ==

给定2个git存储库(产品A和产品B),以及子模块(CommonSubmodule、SomeOtherSubmodule)

我在网上找到了一个脚本,允许通过post_签出挂钩记录分支

#!/bin/sh

previous_head_ref=$1
new_head_ref=$2
is_branch_checkout=$3

if [[ "$previous_head_ref" != "$new_head_ref" ]] && [[ "$is_branch_checkout" == 1 ]]; then
    branch=$(git rev-parse --abbrev-ref HEAD)
    #if [[ "develop" != "$branch" ]]; then
        path="$(dirname "$0")/.."
        logfile="$path/x_branch_log"
        ts=$(date +%s)
        echo "$branch|1|$ts" >> $logfile
        echo "Logging $branch|1|$ts to $logfile"
        echo PWD is $PWD
    #fi
fi
在post_签出上下文中,如何获得根目录(D:\Repositories),而不编码绝对路径,而不管钩子安装在子模块中有多深

D:\Repositories\
此外,如何获取根产品目录,例如

D:\Repositories\ProductA\
D:\Repositories\ProductB\

根据我在上的答案上得到的评论,检查a的返回值

git rev-parse --git-dir

您可能需要迭代(如果您在父repo的子模块的子模块中),但它应该返回父repo的根文件夹。

在我的命令行上,在CommonSubmodule的工作目录中执行此操作会导致D:/Repositories/ProductA/.git/modules/CommonSubmoduleYes,treeline the module part,你得到了你的根folder@RyanTheLeach不要介意下注,我仍然认为这是对你的问题的有效回答。我知道,我是说,“请,(你)不介意下注。”或者“不要太重视否决票”我很想相信你,因为我已经看到了关于git钩子的其他答案,但我是一个彻头彻尾的傻瓜,我一直在试图找出如何可靠地操纵文件路径,但据我所知,人们只是倾向于使用字符串操纵将其组合起来?你提到的“树线”是什么?
git rev-parse --git-dir