pythongitpython

Reading commits in reverse using GitPython


Is there a way to iterate commits in reverse using the GitPython lib, that is, from the oldest one to the newest, in a similar manner to:

>>> from git import Repo
>>> repo = Repo('/path/to/repo')
>>> for commit in reversed(repo.iter_commits()):
...     print commit
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument to reversed() must be a sequence

Without having to include everything in memory first, as my case is dealing with lots of commits (e.g. the linux kernel)?


Solution

  • Looking at the documentation it appears that iter_commits is passing its kwargs to git-rev-list. Looking at its documentation reveals that it accepts a --reverse flag, so one can only guess that repo.iter_commits(reverse=True) would work.