【问题标题】:Why doesn't setting GIT_WORK_TREE work in a post-commit hook?为什么在提交后挂钩中设置 GIT_WORK_TREE 不起作用?
【发布时间】:2011-10-04 09:01:07
【问题描述】:

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

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

但是,提交后我收到以下错误:

$ 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.

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

【问题讨论】:

    标签: git githooks git-checkout


    【解决方案1】:

    这里的问题是post-commit 钩子(以及 pre-commitprepare-commit-msgcommit-msgt) GIT_INDEX_FILE 环境变量设置为 .git/index。 (这 没有记录在githooks documentation 中,但我已经 发布在别处about the settings of environment variables and the current directory in git hooks。)

    GIT_INDEX_FILE环境变量的作用说明 在 git 手册页的 ENVIRONMENT VARIABLES 部分中:

    GIT_INDEX_FILE

    此环境 [变量] 允许指定备用索引文件。如果未指定,则使用默认值$GIT_DIR/index

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

    要使钩子按预期工作,您只需取消设置 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
    

    【讨论】:

    • 这也可以用来使 worktree 和 gitdir 选项在 git stash 和其他命令上工作吗? git --worktree --gitdir <some command> 似乎只适用于某些命令。
    • @Adam Dymitruk:我发现 --work-tree=DIRECTORY--git-dir=DIRECTORY 在钩子之外可靠地工作,不管命令如何,只要我(a)设置它们,并且(b)设置它们两者都是绝对路径,只是为了避免记住关于如何计算最终工作树和存储库目录的令人困惑的规则......
    • 这完全不在一个钩子里。有些工作,有些不工作..我必须确认它们中的一些是否因为相对路径而不起作用..但这种行为会很奇怪,因为我使用的是相同的路径并且只是改变命令。
    • @Adam Dymitruk:确实 - 看到一个例子会很有趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多