How to use git log --oneline in gitpython
Asked Answered
V

2

2

I'm trying to extract list of commit messages by giving a start sha & end sha. It's easy in git using git log. But am trying to do it through gitpython library. Could someone help me to achieve this?

in Git the command is like this :

git log --oneline d3513dbb9f5..598d268f

how do i do it with gitpython?

Vision answered 5/3, 2019 at 14:10 Comment(0)
E
3

The GitPython Repo.iter_commits() function (docs) has support for ref-parse-style commit ranges. So you can do:

import git
repo = git.Repo("/path/to/your/repo")
commits = repo.iter_commits("d3513dbb9f5..598d268f")

Everything after that depends on the exact formatting you want to get. If you want something similar to git log --oneline, that would do the trick (it is a simplified form, the tag/branch names are not shown):

for commit in commits:
    print("%s %s" % (commit.hexsha, commit.message.splitlines()[0]))
Estis answered 4/9, 2019 at 13:31 Comment(0)
R
1

You can use pure gitpython:

import git
repo = git.Repo("/home/user/.emacs.d") # my .emacs repo just for example
logs = repo.git.log("--oneline", "f5035ce..f63d26b")

will give you:

>>> logs
'f63d26b Fix urxvt name to match debian repo\n571f449 Add more key for helm-org-rifle\nbea2697 Drop bm package'

if you want nice output, use pretty print:

from pprint import pprint as pp
>>> pp(logs)
('f63d26b Fix urxvt name to match debian repo\n'
 '571f449 Add more key for helm-org-rifle\n'
 'bea2697 Drop bm package')

Take a note that logs is str if you want to make it a list, just use logs.splitlines()

Gitpython had pretty much all similar API with git. E.g repo.git.log for git log and repo.git.show for git show. Learn more in Gitpython API Reference

Radke answered 6/4, 2019 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.