3

I am just trying to create a git hook which sends a notification about all the commits to our ms-teams channel on every "post-recieve".

The git hook itself works, but with the command git log -1 --pretty=format:'%s'
I only get the last commit-message, which is not very useful if you push multiple commits to master.

Is there a way to get the data (message, author, date of commit, commit number, usw.) of all affected commits on every push?

I have looked up a lot of questions here on Stack Overflow, but haven't found a proper solution for my problem.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
smnstlzr
  • 91
  • 8
  • Does [this answer](https://stackoverflow.com/questions/39006695/how-to-get-all-hashes-that-are-being-committed-in-a-pre-receive-hook) for pre-receive also work for post-receive? – Lasse V. Karlsen Jan 31 '20 at 09:07

2 Answers2

3

Typically speaking, your origin/master will be the position of master on the remote origin after your last synchronisation. master will be your current branch. The commits between these will be the ones you're interested in and you can get them using git log origin/master..origin and you can do whatever you want with these.

Now, in your case, you need to run this on the server after a receive. I'm not sure that post-receive will have this information (since the references will already have been updated). However pre-receive hook receives a list of all the commits that are being pushed. Perhaps hooking up this to your notification function would help better. The answer linked to from Lasse V. Karlsen's comment has details on how to do this.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
0

I had about the same need, on my side. I needed the list of comments made on each commit after a certain tag to generate a kind of changelog list.

You know that post-receive gets the old and new values of all refs in addition to their names.

The parameters sent to your script will be of the form: <oldrev> <newrev> <refname>

So you'll just have to do: git log $1..$2

Taur
  • 514
  • 3
  • 8