简体   繁体   中英

Update a Git Repository through a Git Hook in Python

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM