0

I would like to build a script that pulls simple statistics from my repository. I would like to exclude merge commits and revert commits from those statistics.

There was a question asked 8 years ago about distinguishing between regular commit and merge commit:

Telling if a Git commit is a Merge/Revert commit

The answer is great when it comes to merge commit (i.e. the commit that has more than one parent) but there is no satisfactory answer for distinguishing if commit is a revert commit.

I'm looking at GitPython package and can't find anything on their documentation that would help.

Any help would be greatly appreciated.

Alternatively if there is something already out there (either script/library/product) that helps with git statistics (and excludes merge commits and revert commits) I would also accept such answer.

Greg0ry
  • 931
  • 8
  • 25

2 Answers2

3

A "revert commit" isn't a special entity in git. It's a cli feature meant to make it simple to rollback a previous commit(s) without rewriting the history.

It's just a regular commit with a reverted diff of the the commit(s) to be reverted. There is no chance to identify such a revert commit automatically.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • I'd think maybe something changed in 8 years :-) Thanks for the answer. If there is no feasible way to tell if commit is reverting another commit then would you maybe know of any tool/product that helps with pulling reliable git statistics? – Greg0ry Jan 06 '19 at 10:33
  • You could look at the commit message. Unless manually changed, it will look like "This reverts ...". Of course that's not reliable but might be good enough for your use case. About the statistics, dunno what you mean. – hek2mgl Jan 06 '19 at 10:36
  • I will accept this as an answer although (sadly) I still have no definite idea on how to work out when commit gets reverted... Maybe some combination of git diff and git blame would do - seems this is going to be hard to solve. – Greg0ry Jan 06 '19 at 14:00
  • Why do you need this? What's the use case? – hek2mgl Jan 06 '19 at 15:50
1

As is already stated by herk2mgl's answer a revert commit is just an ordinary commit.
However, if you standardize your revert commit messages you will be able to recognize revert commits by checking the message for your revert specific text.

For this approach you need to make sure that everybody who works on your repository uses that typical message only for revert commits, and not for 'general' commits.

Community
  • 1
  • 1
Aron Hoogeveen
  • 437
  • 5
  • 16
  • How do you want to ensure that if you agree that it isn't possible to distinguish them from regular commits? – hek2mgl Jan 06 '19 at 11:05
  • You could agree with a general format of the commit message (and then mention that format in the wiki, if you are using GitHub, or readme file). For example you could layout the revert commit message like following: ```Revert commit #nnn``` with #nnn for example the first seven characters of the hash of the commit that is being reverted – Aron Hoogeveen Jan 06 '19 at 11:31
  • 2
    Want I want to point out is that relying on the special format of the commit message is not reliable and therefore not advisable. You can't _make sure_ that everybody will follow the convention because you have no way to identify those commits. I didn't mentioned that in my answer by intention. – hek2mgl Jan 06 '19 at 12:06