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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14073053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:56:48  来源:igfitidea点击:

Find path to git hooks directory on the shell

git

提问by cweiske

How do I find the full path to the .git/hooksdirectory of the current Git repository?

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



I see the following issues:

我看到以下问题:

Deep in the directory structure

深入目录结构

I may be anywhere deep in the git repository directory structure, e.g.

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

/home/user/project/.git/hooks

and I am in folder

我在文件夹中

/home/user/project/foo/bar/baz/

Submodules

子模块

Newer git versions put submodule meta data not into .git/but in the main repository and .gitis only a file, e.g.

较新的 git 版本将子模块元数据放入.git/主存储库中,而不是放入主存储库中,.git并且只是一个文件,例如

$ cat /home/user/project/vendor/foo/.git
gitdir: ../../.git/modules/vendor/foo

So in this case, the hooks dir is in

所以在这种情况下,钩子目录在

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

回答by qqx

You can use git rev-parse --git-dirto get the path to the repository used for your current directory. This will deal with gitlinks (using a file in place of the .gitdirectory) as well as having use of the $GIT_DIRenvironment variable. The hooks directory will always be inside of that so you can use:

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

`git rev-parse --git-dir`/hooks

to get the path to the hooks directory.

获取钩子目录的路径。

This is documented in the git rev-parse manpage

这记录在git rev-parse 联机帮助页中

回答by VonC

Starting git 2.9 (June 2016), you will need to look at the new config core.hooksPathto really be sure of the hooks folder.

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

git rev-parse --git-dir/hooks won't be enough anymore.

git rev-parse --git-dir/hooks 已经不够用了。

See commit 867ad08, commit de0824e, commit bf7d977, commit 49fa52f(04 May 2016) by ?var Arnfj?re Bjarmason (avar).
(Merged by Junio C Hamano -- gitster--in commit 6675f50, 17 May 2016)

请参阅?var Arnfj?re Bjarmason ( ) 的提交 867ad08提交 de0824e提交 bf7d977提交 49fa52f(2016 年 5 月 4 日(由Junio C Hamano合并-- --提交 6675f50 中,2016 年 5 月 17 日)avar
gitster

So make sure you check if git config core.hooksPathis defined or not, as it will override the default hooks folder location.

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

The git configdocumentationnow includes:

git config文档现在包括:

core.hooksPath

By default Git will look for your hooks in the '$GIT_DIR/hooks' directory.
Set this to different path, e.g. '/etc/git/hooks', and Git will try to find your hooks in that directory, e.g. '/etc/git/hooks/pre-receive' instead of in '$GIT_DIR/hooks/pre-receive'.

The path can be either absolute or relative. A relative path is taken as relative to the directory where the hooks are run.

默认情况下,Git 会在 ' $GIT_DIR/hooks' 目录中查找您的钩子。
将此设置为不同的路径,例如“ /etc/git/hooks”,Git 将尝试在该目录中找到您的钩子,例如“ /etc/git/hooks/pre-receive”而不是“ $GIT_DIR/hooks/pre-receive”。

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



Git 2.10 (Q3 2016) uses that new core.hooksPathsetting.

Git 2.10(2016 年第三季度)使用该新core.hooksPath设置。

So to get the effectivehookspath, you need to run:

因此,要获得有效hooks路径,您需要运行:

git rev-parse --git-path hooks

From the git rev-parseman page:

git rev-parse手册页

--git-path <path>
    Resolve "$GIT_DIR/<path>" and takes other path relocation variables such
    as $GIT_OBJECT_DIRECTORY, $GIT_INDEX_FILE... into account. For example,
    if $GIT_OBJECT_DIRECTORY is set to /foo/bar then "git rev-parse
    --git-path objects/abc" returns /foo/bar/abc.

See commit 9445b49(16 Aug 2016) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster--in commit d05d0e9, 19 Aug 2016)

请参阅Johannes Schindelin ( ) 的提交 9445b49(2016 年 8 月 16 日(由Junio C Hamano合并-- --d05d0e9 提交中,2016 年 8 月 19 日)dscho
gitster

回答by Till

In addition to --git-diryou can also use the following to find the root of a git repository since this is more generic, I am just adding it here for completion:

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

echo $(git rev-parse --show-toplevel)

Then append /.git/hooksto it? I haven't tried it with sub-modules though.

然后附加/.git/hooks到它?不过,我还没有尝试过使用子模块。