【问题标题】:How can I find the root git directory reliably from a git hook?如何从 git 挂钩中可靠地找到根 git 目录?
【发布时间】:2018-05-03 12:56:33
【问题描述】:

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

D:\Repositories\ProductA\
D:\Repositories\ProductA\CommonSubmodule
D:\Repositories\ProductA\SomeOtherSubmodule
D:\Repositories\ProductA\SomeOtherSubmodule\
D:\Repositories\ProductB\SomeOtherSubmodule\
D:\Repositories\ProductB\CommonSubmodule\

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

#!/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_checkout 上下文中,如何在不编码绝对路径的情况下获取根目录 (D:\Repositories)?

D:\Repositories\

另外,如何获取产品根目录,例如

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

【问题讨论】:

    标签: bash git githooks


    【解决方案1】:

    从我在Git repo root folder 上得到的答案中,检查 a 的返回值

    git rev-parse --git-dir
    

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

    【讨论】:

    • 在我的命令行上执行此操作,在 CommonSubmodule 的工作目录中会导致 D:/Repositories/ProductA/.git/modules/CommonSubmodule
    • 是的,对模块部分进行树线划分,然后您将获得根文件夹
    • @RyanTheLeach 不要介意投反对票,我仍然认为这是对您问题的有效答案。
    • @RyanTheLeach 我知道,我的意思是,“请(你)不要介意反对票”;)或者“不要把反对票看得太重要”
    • 根据我在 SO 中看到的与 git hooks 相关的其他答案,我很想相信你,但我是一个彻头彻尾的 bash 菜鸟,我一直在试图找出如何可靠地操纵文件路径,但据我所知,人们只是倾向于使用字符串操纵来破解它?你提到的“树线”是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多