您可以尝试使用命令行开关设置 git 存储库:
proc = subprocess.Popen(['git', '--git-dir', '/home/git/repos/testing.git', 'log', '-n1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
--git-dir
需要指向一个实际的 git 目录(.git
在工作树中)。请注意,对于某些命令,您also需要设置一个--work-tree
选项也。
设置目录的另一种方法是使用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
但显然这对于你的情况来说是不正确的(它可能是相对的);上面的代码将其设置为完整的显式路径。
See the git manpage https://www.kernel.org/pub/software/scm/git/docs/v1.7.10.1/git.html.
编辑:显然它只适用于指定 cwd 并覆盖的 OPGIT_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)