通过git钩子执行python子进程 [英] Executing python subprocess via git hook

查看:387
本文介绍了通过git钩子执行python子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Git仓库上运行Gitolite,并在Python中使用后接收钩子。我需要在git repository目录下执行git命令。有几行代码:

  proc = subprocess.Popen(['git','log','-n1' ],cwd ='/ home / git / repos / testing.git'stdout = subprocess.PIPE,stderr = subprocess.PIPE)
proc.communicate()

完成新的提交并将其推送到资源库后,脚本会执行并说

  fatal:不是git仓库:'。'

如果我运行



pre $ proc = subprocess.Popen(['pwd'],cwd ='/ home / git / repos / testing.git'stdout = subprocess。 PIPE,stderr = subprocess.PIPE)

正如预期的那样, home / git / repos / testing.git)



如果我从bash手动运行这个脚本,它能正常工作并显示正确的git log输出。我做错了什么?

解决方案

您可以尝试使用命令行开关设置git存储库:

  proc = subprocess.Popen(['git','--git-dir','/ home / git / repos / testing。 git','log','-n1'],stdout = subprocess.PIPE,stderr = subprocess.PIPE)


$ b $在工作树中需要指向一个实际的git目录( .git ) )。请注意,对于某些命令,您也需要设置 - 工作树选项。



设置目录的另一种方式是使用 GIT_DIR 环境变量:

  import os 
env = os.environ.copy()
env ['GIT_DIR'] ='/home/git/repos/testing.git'
proc = subprocess。 Popen((['git','log','-n1',stdout = subprocess.PIPE,stderr = subprocess.PIPE,env = env)

很显然,钩子已经设置了 GIT_DIR 但显然这对您的情况不正确(可能是相对的);上面的代码设置它到一个完整的,明确的路径。



请参阅 git 手册页

编辑:显然它只适用于OP时指定一个cwd和重写 GIT_DIR var:

  import os 
repo ='/ home / git / rep os / testing.git'
env = os.environ.copy()
env ['GIT_DIR'] = repo
proc = subprocess.Popen((['git','log' ,'-n1',stdout = subprocess.PIPE,stderr = subprocess.PIPE,env = env,cwd = repo)


I'm running Gitolite over the Git repository and I have post-receive hook there written in Python. I need to execute "git" command at git repository directory. There are few lines of code:

proc = subprocess.Popen(['git', 'log', '-n1'], cwd='/home/git/repos/testing.git' stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.communicate()

After I make new commit and push to repository, scripts executes and says

fatal: Not a git repository: '.'

If I run

proc = subprocess.Popen(['pwd'], cwd='/home/git/repos/testing.git' stdout=subprocess.PIPE, stderr=subprocess.PIPE)

it says, as expected, correct path to git repository (/home/git/repos/testing.git)

If I run this script manually from bash, it works correct and show correct output of "git log". What I'm doing wrong?

解决方案

You could try to set the git repository using a command-line switch:

proc = subprocess.Popen(['git', '--git-dir', '/home/git/repos/testing.git', 'log', '-n1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

--git-dir needs to point to an actual git directory (.git in a working tree). Note that for some commands you also need to set a --work-tree option too.

Another way to set the directory is using the GIT_DIR environment variable:

import os
env = os.environ.copy()
env['GIT_DIR'] = '/home/git/repos/testing.git'
proc = subprocess.Popen((['git', 'log', '-n1', stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)

Apparently hooks already set GIT_DIR but obviously this is incorrect for your case (it could be relative); the above code sets it to a full, explicit path.

See the git manpage.

Edit: Apparently it only works for the OP when specifying both a cwd and overriding the GIT_DIR var:

import os
repo = '/home/git/repos/testing.git'
env = os.environ.copy()
env['GIT_DIR'] = repo
proc = subprocess.Popen((['git', 'log', '-n1', stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, cwd=repo)

这篇关于通过git钩子执行python子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆