import re
import os
import sys
import json
from subprocess import Popen, PIPE
from httplib2 import Http

# Set this to point to your local gitpubsub server
postURL = "http://localhost:2069/json"

pwd = os.getcwd()
if len(sys.argv) <= 3:
    print("Usage: post-receive [old] [new] [ref]")
    exit()

old, new, ref = sys.argv[1:4]
m = re.match(r"^.*/([^/]+)$", pwd)
if not m:
    print("Could not figure out which project this is :(", project)
    exit()

project = m.group(1)
print("Posting commit message for project " + project)

process = Popen(["git", "show", "--name-only", new], stdout=PIPE)
#process = Popen(["ls", "-la"], stdout=PIPE)
exit_code = os.waitpid(process.pid, 0)
output = process.communicate()[0]

commit = {'ref': ref, 'repository': "git", 'hash': new, 'project': project}

headers, commit['log'] = output.split("\n\n", 1)

parsed = dict(re.findall(r"(?P<name>[^:\n]+): (?P<value>[^\r\n]+)", headers))

author = re.match(r"^(.+) <(.+)>$", parsed.get("Author", "?? <??@??>"))
if author:
    commit['author'] = author.group(1)
    commit['email'] = author.group(2)
else:
    commit['author'] = "Unknown"
    commit['email'] = "unknown@unknown"


data = json.dumps(commit) + "\n\n"
print(data)
Http().request(postURL, "PUT", data)


@@ -595,53 +595,8 @@
 PE)%0A
-#process = Popen(%5B%22ls%22, %22-la%22%5D, stdout=PIPE)%0A
 exit
@@ -746,55 +746,140 @@
 t%7D%0A%0A
-headers, commit%5B'log'%5D = output.split(%22%5Cn%5Cn%22, 1
+arr = output.split(%22%5Cn%5Cn%22, 3)%0Aheaders = arr%5B0%5D%0Alog = arr%5B1%5D%0Aif len(arr) == 3:%0A    commit%5B'files'%5D = re.findall(r%22(%5B%5E%5Cr%5Cn%5D+)%22, arr%5B2%5D
 )%0A%0Ap

Add file tracking to post receive hook