简体   繁体   English

从post-receive钩子运行git命令

[英]Running git commands from post-receive hook

Explaining why I want to do this is complicated, but what I would like to do is to equip a particular git repository with a post-receive hook that does the following: 解释为什么我要这样做很复杂,但是我想做的是为特定的git存储库配备一个执行以下操作的post-receive钩子:

  1. Move to a test directory 移至测试目录
  2. Clone the project that was just updated. 克隆刚刚更新的项目。
  3. Checkout a specific branch. 签出特定分支。
  4. Do some tests 做一些测试

The post-receive code looks like this (simplified): let "repo" be the name of the git repository, let "testdir" be the name of a sibling directory, initially empty, and let "dev" be the name of the branch. 接收后的代码如下所示(简化):令“ repo”为git存储库的名称,令“ testdir”为同级目录的名称,最初为空,而使“ dev”为分支的名称。

cd ../testdir
git clone --local ../repo .
git checkout dev

However, when the code in the script gets to "git checkout", git responds with "fatal: not a git repository: '.'" 但是,当脚本中的代码进入“ git checkout”时,git会返回“致命:不是git存储库:'。”

I have no idea why git thinks "testdir" isn't a git repository. 我不知道为什么git认为“ testdir”不是git存储库。 If I run that commands from a command line (rather than from within post-receive), then they work correctly. 如果我从命令行(而不是在接收后)运行该命令,那么它们将正常工作。 I don't know why the behavior would be different from within post-receive. 我不知道为什么这种行为与接收后的行为会有所不同。 Any ideas? 有任何想法吗?

You need to unset GIT_DIR in your post-receive hook. 您需要在post-receive挂钩中取消设置GIT_DIR The problem is that at the time your hook script runs, GIT_DIR=. 问题是在您的挂钩脚本运行时, GIT_DIR=. , which after your cd and clone operation is no longer useful. ,在进行cd和克隆操作之后,该功能将不再有用。

I set up a local test environment, and when my post-receive script looks like this: 我设置了一个本地测试环境,当我post-receive脚本如下所示:

#!/bin/sh

cd ../testdir
git clone --local ../upstream.git .
git checkout dev

I get this: 我得到这个:

remote: fatal: Not a git repository: '.'

But if I unset the GIT_DIR variable: 但是,如果我未设置GIT_DIR变量:

#!/bin/sh

unset GIT_DIR
cd ../testdir
git clone --local ../upstream.git .
git checkout dev

Everything works. 一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM