2

I'm using python to write a post-receive hook that will hopefully serve for automatic deployment of all of the updated files in my project. Essentially, every time the "deploy" branch is pushed, it will upload the changed files over FTP to my server.

Here's what I have so far:

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."

The repository that contains this hook is a bare repository. I then have another repository under /home/git/testrepo/ that is a clone of this repository.

In this code, I try to change my working directory to that repository and then initiate a pull. This, however, does not work. Instead, I get the following message when I push and the hook executes: "fatal: Not a git repository: '.'".

Any ideas on how I can successfully pull to this repository, so that I can then upload its files to my other server? Every method that I've tried has failed.

m0tive
  • 2,796
  • 1
  • 22
  • 36
Serplat
  • 4,367
  • 2
  • 18
  • 20
  • For now I have it working by setting the git directory using "--git-dir" as an argument to all of my git commands. However, I'm still interested in finding an actual solution to my original issue. – Serplat Dec 27 '11 at 23:48

1 Answers1

0

The git diff ... is failing because it's not aware you're inside a bare repo.

Try git --bare diff ... instead or setting $GIT_DIR.

m0tive
  • 2,796
  • 1
  • 22
  • 36
  • The `git diff ...` actually works fine. It's the pull that wasn't working properly, even though I had tried to change my working directory to that of a regular repository. – Serplat Dec 28 '11 at 18:34
  • 1
    Ah alright, it might be caused by `$GIT_DIR` being set before running the hook. That way, I imagin the `git pull` will use the current value of `$GIT_DIR` which would be incorrect. Setting that before doing the `pull` or using `--git-dir` (as you said) will correct it. I haven't tested this out, so may be wrong again :-) – m0tive Dec 29 '11 at 11:51