在 git hook 中,当前工作目录是否保证在 git 存储库中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7065224/
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
In a git hook is the current working directory guaranteed to be within the git repository?
提问by Dickon Reed
Experimentally it seems that git hooks get run with the current directory set to be the root of the repository. However, I can't see any guarantee about that in the git documentation. Should I rely on the current working directory to locate the git repository, or is there a better way to work out the git repository associated with the hook?
从实验上看,git hooks 似乎在当前目录设置为存储库的根目录的情况下运行。但是,我在 git 文档中看不到任何保证。我应该依靠当前的工作目录来定位 git 存储库,还是有更好的方法来计算与钩子关联的 git 存储库?
采纳答案by manojlds
It is based on the value set for environment variable GIT_DIR
. It is set to the root of the repository when the hook starts running. Many hooks, especially those doing a pull from another repo, unset ( and reset) this environment variable as needed.
它基于为环境变量设置的值GIT_DIR
。当钩子开始运行时,它被设置为存储库的根目录。许多钩子,尤其是那些从另一个 repo 中拉取的钩子,根据需要取消设置(和重置)这个环境变量。
回答by Ohad Schneider
The current answers appear to be outdated. As of 2.9.0, the docs state the following:
当前的答案似乎已经过时。从 2.9.0 开始,文档说明如下:
Before Git invokes a hook, it changes its working directory to either the root of the working tree in a non-bare repository, or to the $GIT_DIR in a bare repository.
在 Git 调用钩子之前,它会将其工作目录更改为非裸存储库中工作树的根目录,或者更改为裸存储库中的 $GIT_DIR。
回答by holygeek
You can use the environment variable $GIT_DIR
. $GIT_DIR
points at the .git
directory.
您可以使用环境变量$GIT_DIR
。$GIT_DIR
指向.git
目录。