【问题标题】:Update a Git Repository through a Git Hook in Python通过 Python 中的 Git Hook 更新 Git 存储库
【发布时间】:2012-01-28 19:39:26
【问题描述】:

我正在使用 python 编写一个 post-receive 钩子,它有望用于自动部署我项目中所有更新的文件。本质上,每次推送“部署”分支时,它都会通过 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-dir”作为我所有 git 命令的参数来设置 git 目录来工作。但是,我仍然有兴趣为我的原始问题找到一个实际的解决方案。

标签: python git githooks git-bare


【解决方案1】:

git diff ... 失败了,因为它不知道你在一个裸仓库中。

改用git --bare diff ... 或设置$GIT_DIR

【讨论】:

  • git diff ... 实际上工作正常。即使我尝试将我的工作目录更改为常规存储库的目录,也无法正常工作。
  • 啊,好吧,可能是在运行钩子之前设置了$GIT_DIR。这样,我想 git pull 将使用 $GIT_DIR 的当前值,这是不正确的。在执行pull 或使用--git-dir(如您所说)之前设置它会更正它。我还没有测试过,所以可能又错了:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
相关资源
最近更新 更多