Calling git -C /path/to/dir from python fails, runs from console
Question: I am writing a git serverside hook that needs to check if there are any
modifications to a different git folder (different from the bare git
repository in which that hook resides). To that end, I have written a `pre-
receive` hook and I am trying to use `git status --porcelain`. The script has
to select the folder which needed checking, but for now i decided to hardcode
all the paths, until I make it work.

So far, when i run this hook as just a python script, it gives me the correct
result, but when i run it as a hook, it gives me an error.

**UPD** It might also be worth mentioning that the script is executed on an
Ubuntu Linux machine. Might be there any limitations on what can be run from
pyton or what processes can be started by other processes?**/UPD**

Here is the script itself:

    
    
    #!/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)
    

Here is how i call it from 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
    

Here's the response when I try to push something to the remote:

    
    
    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'
    


Answer: Fixed the problem by adding more keys to the git command, namely specifying
the path to the git dir and the working tree. `

    
    
        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 )

`

This article was extremely helpful:
<http://longair.net/blog/2011/04/09/missing-git-hooks-documentation/>

