Is there a python interface to git shortlog?

Question:

I am trying to get logging information out of git into python. I looked at pygit2 and gitpython, but neither seems to give a high level interface similar to git shortlog. Is there a python library that provides such an interface, or should I just call out to the git executable?

Asked By: Andreas Mueller

||

Answers:

I am assuming you mean pygit2 was one of the libraries you looked at. That is a lower level libraries that let you write a higher level library on top of that (it’s plenty high-level enough already, is the 3 lines of code to get the basic log output too much?). It isn’t even hard to do what you want – read the relevant documentation you could come up with something like:

>>> from pygit2 import Repository
>>> from pygit2 import GIT_SORT_TIME
>>> from pygit2 import GIT_SORT_REVERSE
>>> repo = Repository('/path/to/repo')
>>> rev = repo.revparse_single('HEAD').hex
>>> iterator = repo.walk(rev, GIT_SORT_TIME | GIT_SORT_REVERSE)
>>> results = [(commit.committer.name, commit.message.splitlines()[0])
...     for commit in iterator]
>>> results
[(u'User', u'first commit.'), (u'User', u'demo file.'), ... (u'User', u'okay?')]

If you want to group the output to be the same as git shortlog that’s not even hard either, all the data you need is already in the iterator so just use it to put the data into the format you need.

Answered By: metatoaster
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.