从python调用git -C / path / to / dir失败,从控制台运行

时间:2014-03-26 12:34:46

标签: python git ubuntu

我正在编写一个git服务器端钩子,需要检查是否有对不同git文件夹的任何修改(不同于该钩子所在的裸git存储库)。为此,我写了一个pre-receive钩子,我正在尝试使用git status --porcelain。脚本必须选择需要检查的文件夹,但是现在我决定对所有路径进行硬编码,直到我完成它为止。

到目前为止,当我将这个钩子作为一个python脚本运行时,它给了我正确的结果,但是当我把它作为钩子运行时,它给了我一个错误。

UPD 还可能值得一提的是,该脚本是在Ubuntu Linux机器上执行的。可以从pyton运行什么或者其他进程可以启动哪些进程有任何限制? / UPD

这是脚本本身:

#!/usr/bin/env python

import sys
import subprocess
import string
import os

print "PRE-RECEIVE HOOK >>>"

def git(args):
    dirname = os.path.dirname("/home/git/repos/branchone/gitremote/")
    print "Dirname = " + dirname
    pr = subprocess.Popen( ["git","-C", dirname,  "status", "--porcelain"], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    (details, error) = pr.communicate()
    print details, error
    details = details.strip()
    if error != "":
        print "Errors: " + error
        return None
    return details

def pwd():
    pwd = subprocess.Popen("pwd", stdout = subprocess.PIPE)
    details = pwd.stdout.read().strip()
    print "PWD:. " + details

def main():
    git([])
    pwd()

if \__name__ == "\__main__":
    main()

sys.exit(2)

以下是我如何用cosole调用它:

root@remine-vm:/home/git/repos/gitremote.git# ./hooks/pre-receive
PRE-RECEIVE HOOK >>>
Dirname = /home/git/repos/branchone/gitremote
 M eden
?? fruit
?? snake

这是我尝试将某些东西推到遥控器时的响应:

Push: Not all refs have been pushed.
$ git.exe push --porcelain --progress --recurse-submodules=check origin refs/heads/branchone:refs/heads/branchone
'branchone' rejected (non-fast-forward)
Counting objects: 7, done.
Delta compression using up to 8 threads.
Total 6 (delta 0), reused 0 (delta 0)
remote: PRE-RECEIVE HOOK >>>
remote: Dirname = /home/git/repos/branchone/gitremote
remote:  fatal: Not a git repository: '.'
remote: 
remote: Errors: fatal: Not a git repository: '.'
remote: 
remote: PWD:. /home/git/repos/gitremote.git
error: failed to push some refs to 'root@redmine-vm:/home/git/repos/gitremote.git'

1 个答案:

答案 0 :(得分:0)

通过向git命令添加更多键来修复问题,即指定git目录和工作树的路径。

    pr = subprocess.Popen( ["git","--git-dir", "/home/git/repos/branchone/gitremote/.git", "--work-tree", "/home/git/repos/branchone/gitremote", "-C", dirname, "status", "--porcelain"], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE )

这篇文章非常有帮助:http://longair.net/blog/2011/04/09/missing-git-hooks-documentation/