Created
October 3, 2012 16:21
-
-
Save relrod/3828007 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
# (c) 2012 Red Hat, Inc. | |
# Authored by Ricky Elrod | |
# But when it breaks, don't yell at him because that's mean. | |
# update hook for FI repos -> zodbot. | |
import os | |
import sys | |
import subprocess | |
import shlex | |
hook = sys.argv[0] | |
ref = sys.argv[1] | |
old = sys.argv[2] | |
new = sys.argv[3] | |
repodir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))).split( | |
'/')[-1] | |
def run_command(command): | |
""" Run a command and return a hash with the resulting stdout/stderr.""" | |
escaped = shlex.split(command) | |
cmd = subprocess.Popen(escaped, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
stdout, stderr = cmd.communicate() | |
return {"stdout": stdout, "stderr": stderr} | |
def parse_commit(commit): | |
""" | |
So we're given a commit in the form of: | |
--- | |
Ricky Elrod - test-repo:a045150 ---- add some more test files... | |
A foobar/asdf/testfile.1 | |
A foobar/testfile.2 | |
--- | |
Essentially, we rip out the first line and set it aside. | |
Then all the other lines will begin with M/C/R/A/D/U. | |
Replace those letters with fancy little symbols (like + for A). | |
Combine them together in a list/array. | |
Show the first 4 and if more exist, append '...' to the list. | |
Lastly, replace the "----" in the original line above with these. | |
""" | |
lines = commit.split("\n") | |
message = lines.pop(0) | |
files = [] | |
# The remaining lines are files changed. | |
for changed_file in filter(None, lines): | |
print changed_file | |
status, filename = changed_file.split() | |
if status == "M" or status == "R": | |
symbol = "*" | |
elif status == "C" or status == "A": | |
symbol = "+" | |
elif status == "D": | |
symbol = "-" | |
else: | |
symbol = "?" | |
files.append(symbol + filename) | |
# Show the first 4 files changed, and if there are more, add a '...' | |
fileslist = ' '.join(files[0:4]) | |
if len(files) > 4: | |
fileslist += ' ...' | |
# Replace the ---- with the files list... | |
return message.replace('----', '[' + fileslist + ']', 1) | |
# Get a list of commits to report. | |
revs = run_command("git rev-list ^%s %s" % (old, new))["stdout"].split("\n") | |
revs = filter(None, revs) | |
for commit_hash in revs: | |
# Get the commit in a format that we can deal with | |
commit = run_command( | |
"git show --name-status " + commit_hash + " --oneline " | |
"--format='%an - " + repodir + ":%h ---- %s'") | |
parsed_commit = parse_commit(commit["stdout"]) | |
print parsed_commit | |
# Just to test -- causes the hook to fail every time, so I can `git push` a | |
# bunch of times to test changes. | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment