为什么不设置GIT_WORK_TREE在post-commit钩子中工作? [英] Why doesn't setting GIT_WORK_TREE work in a post-commit hook?

查看:135
本文介绍了为什么不设置GIT_WORK_TREE在post-commit钩子中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在每次成功提交后使用以下 post-commit 钩子部署到特定目录:

 #!/ bin / sh 
export GIT_WORK_TREE = / var / www / example /
export GIT_DIR = / home / mark / test / .git /
git checkout -f

但是,提交后出现以下错误:

  $ git commit -m'提交示例'
致命:无法创建'/ var / www / example /。 git / index.lock':没有这样的文件或目录
[master 0938e48]示例提交。

...好像 GIT_WORK_TREE 设置正在被忽略。为什么设置这个环境变量似乎不起作用?我使用的是git 1.7.4.1版本。

解决方案

这里的问题是在 post-commit 钩子(还有
pre-commit prepare-commit-msg commit-msgt
GIT_INDEX_FILE 环境变量设置为 .git /索引。 (此
未在 githooks
文档
中记录,但是我在其他地方发布了
关于设置环境变量和git
钩子中的
当前目录。)


$ b $ <<> c $ c> GIT_INDEX_FILE 环境变量在 git 手册页的ENVIRONMENT VARIABLES部分描述为
,如下所示:


GIT_INDEX_FILE



该环境[variable]允许指定备用索引文件。如果未指定,则使用缺省值 $ GIT_DIR / index


...出于某种原因,在这种情况下,相对于 GIT_WORK_TREE ,使用 GIT_INDEX_FILE


为了让钩子按照您的预期工作,您只需要取消设置
GIT_INDEX_FILE ,那么你的钩子看起来像:

 #!/ bin / sh 
unset GIT_INDEX_FILE
export GIT_WORK_TREE = / var / www / example /
export GIT_DIR = / home / mark / test / .git /
git checkout -f


I'm trying to use the following post-commit hook to deploy to a particular directory after each successful commit:

#!/bin/sh
export GIT_WORK_TREE=/var/www/example/
export GIT_DIR=/home/mark/test/.git/
git checkout -f

However, after committing I get the following error:

$ git commit -m 'An example commit.'
fatal: Unable to create '/var/www/example/.git/index.lock': No such file or directory
[master 0938e48] An example commit.

... as if the GIT_WORK_TREE setting is being ignored. Why does setting this environment variable appear to be not working? I'm using git version 1.7.4.1.

解决方案

The problem here is that in post-commit hooks (and also pre-commit, prepare-commit-msg and commit-msgt) the GIT_INDEX_FILE environment variable is set to .git/index. (This isn't documented in the githooks documentation, but I've posted elsewhere about the settings of environment variables and the current directory in git hooks.)

The effect of the GIT_INDEX_FILE environment variable is described in the ENVIRONMENT VARIABLES section of the git man page as:

GIT_INDEX_FILE

This environment [variable] allows the specification of an alternate index file. If not specified, the default of $GIT_DIR/index is used.

... and for some reason, in this situation, GIT_INDEX_FILE is being used relative to GIT_WORK_TREE.

To make the hook work as you would expect, you just need to unset GIT_INDEX_FILE, so your hook would look like:

 #!/bin/sh
 unset GIT_INDEX_FILE
 export GIT_WORK_TREE=/var/www/example/
 export GIT_DIR=/home/mark/test/.git/
 git checkout -f

这篇关于为什么不设置GIT_WORK_TREE在post-commit钩子中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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