简体   繁体   English

通过Python中的Git挂钩更新Git存储库

[英]Update a Git Repository through a Git Hook in Python

I'm using python to write a post-receive hook that will hopefully serve for automatic deployment of all of the updated files in my project. 我正在使用python编写一个接收后钩子,该钩子有望用于自动部署项目中的所有更新文件。 Essentially, every time the "deploy" branch is pushed, it will upload the changed files over FTP to my server. 本质上,每次按下“部署”分支时,它将通过FTP将更改的文件上传到我的服务器。

Here's what I have so far: 这是我到目前为止的内容:

def deploy(old, new):
        fileList = subprocess.Popen(['git', 'diff', '--name-only', old, new], stdout=subprocess.PIPE)
        files = fileList.stdout.read().split('\n')[:-1]

        # Switch to the regular repository and pull to it.
        os.chdir("/home/git/testrepo")
        subprocess.Popen(['git', 'pull'], cwd="/home/git/testrepo")

        for file in files:
                print file

for line in sys.stdin.xreadlines():
        old, new, ref = line.strip().split(' ')
        if ref == "refs/heads/deploy":
                print "Deploying the new commits now."
                deploy(old, new)
        else:
                print "No need to deploy."

The repository that contains this hook is a bare repository. 包含此挂钩的存储库是一个裸存储库。 I then have another repository under /home/git/testrepo/ that is a clone of this repository. 然后,我在/home/git/testrepo/下有另一个存储库,它是该存储库的副本。

In this code, I try to change my working directory to that repository and then initiate a pull. 在这段代码中,我尝试将工作目录更改为该存储库,然后启动拉取。 This, however, does not work. 但是,这不起作用。 Instead, I get the following message when I push and the hook executes: "fatal: Not a git repository: '.'". 相反,当我按下并执行钩子时,会收到以下消息:“致命:不是git存储库:'。'”。

Any ideas on how I can successfully pull to this repository, so that I can then upload its files to my other server? 关于如何成功提取到该存储库,以便随后将其文件上传到其他服务器的任何想法? Every method that I've tried has failed. 我尝试过的每种方法都失败了。

The git diff ... is failing because it's not aware you're inside a bare repo. git diff ...失败,因为它不知道您在裸仓库中。

Try git --bare diff ... instead or setting $GIT_DIR . 尝试git --bare diff ...或者设置$GIT_DIR

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

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