Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在shell上查找git hooks目录的路径_Git - Fatal编程技术网

在shell上查找git hooks目录的路径

在shell上查找git hooks目录的路径,git,Git,如何找到当前git存储库的.git/hooks目录的完整路径 我看到以下问题: 深入目录结构 我可能在git存储库目录结构的任何深处,例如 /home/user/project/.git/hooks 我在文件夹里 /home/user/project/foo/bar/baz/ 子模块 较新的git版本不会将子模块元数据放入.git/中,而是放在主存储库中,.git只是一个文件,例如 $ cat /home/user/project/vendor/foo/.git gitdir: ../..

如何找到当前git存储库的
.git/hooks
目录的完整路径


我看到以下问题:

深入目录结构 我可能在git存储库目录结构的任何深处,例如

/home/user/project/.git/hooks
我在文件夹里

/home/user/project/foo/bar/baz/
子模块 较新的git版本不会将子模块元数据放入
.git/
中,而是放在主存储库中,
.git
只是一个文件,例如

$ cat /home/user/project/vendor/foo/.git
gitdir: ../../.git/modules/vendor/foo
在本例中,hooks dir处于

/home/user/project/.git/modules/vendor/foo/hooks

您可以使用
git rev parse--git dir
获取用于当前目录的存储库的路径。这将处理gitlinks(使用文件代替
.git
目录)以及使用
$git\u DIR
环境变量。hooks目录将始终位于其中,因此您可以使用:

`git rev-parse --git-dir`/hooks
获取hooks目录的路径

除了
--git dir
之外,您还可以使用以下内容查找git存储库的根目录,因为这是更通用的,我只是在这里添加它以供完成:

echo $(git rev-parse --show-toplevel)
然后将
/.git/hooks
附加到它?不过,我还没有在子模块中尝试过它。

从git 2.9(2016年6月)开始,您需要查看新的config
core.hooksPath
,才能真正确定hooks文件夹

git rev parse--git dir
/hooks将不再足够

参见,,(2016年5月4日)作者。
(于2016年5月17日被合并)

因此,请确保检查是否定义了
git config core.hooksPath
,因为它将覆盖默认的hooks文件夹位置

目前的方案包括:

core.hooksPath
默认情况下,Git将在“
$Git\u DIR/hooks
”目录中查找钩子。
将此设置为不同的路径,例如,
/etc/git/hooks
,git将尝试在该目录中查找钩子,例如,
/etc/git/hooks/pre-receive
,而不是“
$git_DIR/hooks/pre-receive

路径可以是绝对路径,也可以是相对路径。相对路径是相对于运行挂钩的目录的路径


Git 2.10(2016年第3季度)使用了新的
core.hooksPath
设置

因此,要获得有效的钩子路径,您需要运行:

git rev-parse --git-path hooks
git rev parse

--git路径
解析“$GIT_DIR/”并获取其他路径重定位变量,如
作为$GIT\u对象\u目录,$GIT\u索引\u文件。。。考虑到。例如
如果$GIT_OBJECT_DIRECTORY设置为/foo/bar,则“GIT rev parse
--git path objects/abc“返回/foo/bar/abc。
参见(2016年8月16日)作者。

(由in合并,2016年8月19日)

如果您在项目目录中 您可以通过键入to terminal来了解当前钩子路径

git config core.hooksPath
如果该值为空,则可以设置.githooks文件夹的路径 您可以通过键入以下命令来完成此操作

git config core.hooksPath .githooks/

现在您会发现钩子路径已更改,您可以再次通过第一个命令进行确认。

这对子模块没有帮助。这是否可以用于
git submodule foreach
?例如,类似于
git子模块foreach cp.git/hooks/commit msg$(git rev parse--git dir)/hooks
?我看到的问题是,由于shell首先扩展git rev parse,它的计算结果是superproject的git dir。@WoodrowBarlow这可能最好作为一个新问题而不是注释来解决,因为这里的注释在长度和格式功能上都非常有限。但是您是否尝试过在
foreach
之后的所有内容周围加上单引号?这应该会在需要时推迟评估。这很有效(尽管我也有一个不同的问题)!谢谢。这不再正确,请使用
git rev parse--git path hooks/hook name
查找钩子的存储位置。至少对于父repo(子模块还不确定),请确保使用git 2.9(2016年6月)或更高版本检查
git config core.hooksPath
。请参阅git config core.hooksPath.git/hooks
git config core.hooksPath .githooks/