I have a central Git bare repository. When a push is made to that repo I want to run a post-receive hook. What that hook will do is create a message on a Basecamp project (using their API). I want info on the update that was just performed. Right now I think git log -2 --stat is good enough but would like a little more info (branch that was updated, file created, files removed). Can anyone help with the command(s) I need to do to get all the info? Performing multiple commands is fine with with me, there probably isn’t a single command that will get me all the information.
I have a central Git bare repository. When a push is made to that
Share
You could find the latest commit by examining and sorting the files under
.git/refs/heads: every time a new commit is made, the correspondingrefs/headsfile is changed, i.e. when committing tomaster,refs/heads/masteris updated.So, let’s develop a solution.
First task: find all branches (i.e. all files under
refs/headsand print out when they were last changed. You’re talking about hooks, so we give the path relative to the.git/hooksdirectory:This produces a list of all branches along with their change date. See the man page of
findfor an explanation of the parameters.Second Task: sort the obtained list
Third Task: we need the newest element in that list. Since
sortsorts from old to new, our desired item is at the bottom of the list. Get this element withtail(only one item, therefore pass the-1flag):Fourth Task: drop the date in the obtained line. From our
printfstatement we know that date and path are separated with a space. Feed this as delimiter intocut(-d " ") and tell it we need the second field (i.e. the file path,-f 2). For convenience, we’ll store this file path in a variable called$LATESTHEAD:Fifth Task: Now we know the filename, but we need the content. This is the latest revistion that could be passed to
git log.catdoes the job. Store the latest revision in$LATESTREVNow, you could use
$LATESTREVto do any dirty things you want.Perhaps not the most elegant solution (probably someone will come up and tell you a much easier one-liner) but works for me.