2

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.