繁体   English   中英

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

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

我正在使用python编写一个接收后钩子,该钩子有望用于自动部署项目中的所有更新文件。 本质上,每次按下“部署”分支时,它将通过FTP将更改的文件上传到我的服务器。

这是我到目前为止的内容:

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

包含此挂钩的存储库是一个裸存储库。 然后,我在/home/git/testrepo/下有另一个存储库,它是该存储库的副本。

在这段代码中,我尝试将工作目录更改为该存储库,然后启动拉取。 但是,这不起作用。 相反,当我按下并执行钩子时,会收到以下消息:“致命:不是git存储库:'。'”。

关于如何成功提取到该存储库,以便随后将其文件上传到其他服务器的任何想法? 我尝试过的每种方法都失败了。

git diff ...失败,因为它不知道您在裸仓库中。

尝试git --bare diff ...或者设置$GIT_DIR

暂无
暂无

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

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