How to use git log --oneline in gitpython

↘锁芯ラ 提交于 2019-12-10 11:54:35

问题


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?


回答1:


You may want to try PyDriller (a wrapper around GitPython), it's easier:

for commit in RepositoryMining("path_to_repo", from_commit="STARTING COMMIT", to_commit="ENDING_COMMIT").traverse_commits():
    print(commit.msg)

If you want commits of a specific branch, add the parameter only_in_branch="BRANCH_NAME". Docs: http://pydriller.readthedocs.io/en/latest/




回答2:


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]))



回答3:


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



来源:https://stackoverflow.com/questions/55004770/how-to-use-git-log-oneline-in-gitpython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!