以编程方式获取bash中git目录的路径(包括子模块) [英] Programmatically get path to git directory in bash (including submodules)

查看:44
本文介绍了以编程方式获取bash中git目录的路径(包括子模块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式找到.git目录的路径(包括子模块)?

Is it possible to programmatically find the path to the .git directory including for submodules?

我正在尝试编写一个安装git钩子的脚本,但是我找不到一种直接的方法来找到该项目的.git目录.理想情况下,它应该涵盖以下情况:

I'm trying to write a script that installs a git hook but I can't find a straightforward way to find the .git directory for the project. Ideally it should cover the following cases:

  1. 在项目的根目录中
  2. 在项目的子目录中
  3. 在项目内子模块的根目录中
  4. 在项目中子模块的子目录中

我可以手动完成所有这些操作,但我不想解析子模块的.git文件.

I could do all of this manually but I'd rather not parse the .git file of a submodule.

我也会对python解决方案感兴趣.

I'd be interested in a solution in python as well.

推荐答案

git rev-parse --git-dir 似乎就是您想要的.我只是用一个子模块对其进行了测试:

git rev-parse --git-dir seems to be what you want. I just tested it with a submodule:

cd mymodule
git rev-parse --git-dir  # --> .git
git submodule add ssh://.../path/to/mysubmodule
cd mysubmodule
git rev-parse --git-dir  # --> /home/.../mymodule/.git/modules/mysubmodule

这让我感到惊讶,因为在 mysubmodule 中有一个 .git ,但它实际上是一个文件,具有以下内容:

This surprised me, since there was a .git in mysubmodule, but it turned out to be a file, with the following contents:

gitdir: ../.git/modules/mysubmodule

这篇关于以编程方式获取bash中git目录的路径(包括子模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆