I'm writing a git post-receive hook using Python and Git-Python that gathers information about the commits contained in a push, then updates our bug tracker and IM with a summary. I'm having trouble in the case where a push creates a branch (i.e. the fromrev
parameter to post-receive is all zeroes) and also spans several commits on that branch. I'm walking the list of parents backwards from the torev
commit, but I can't figure out how to tell which commit is the first one in the branch, i.e. when to stop looking.
On the command line I can do
git rev-list this-branch ^not-that-branch ^master
which will give me exactly the list of commits in this-branch
, and no others. I've tried to replicate this using the Commit.iter_parents
method which is documented to take the same parameters as git-rev-list but it doesn't like positional parameters as far as I can see, and I can't find a set of keyword params that work.
I read the doco for Dulwich but it wasn't clear whether it would do anything very differently from Git-Python.
My (simplified) code looks like this. When a push starts a new branch it currently only looks at the first commit and then stops:
import git
repo = git.Repo('.')
for line in input:
(fromrev, torev, refname) = line.rstrip().split(' ')
commit = repo.commit(torev)
maxdepth = 25 # just so we don't go too far back in the tree
if fromrev == ('0' * 40):
maxdepth = 1
depth = 0
while depth < maxdepth:
if commit.hexsha == fromrev:
# Reached the start of the push
break
print '{sha} by {name}: {msg}'.format(
sha = commit.hexsha[:7], user = commit.author.name, commit.summary)
commit = commit.parents[0]
depth += 1
git log master..branch --oneline | tail -1
Just need to figure out how to translate it into code :P – Brannan