如何使用gitpython获取提交作者的姓名和电子邮件? [英] How to get commit author name and email with gitpython?
问题描述
当我运行 git log
时,每次提交都会得到以下行:作者:名称<电子邮件>".如何为本地存储库的Python提交提供完全相同的格式?当我运行下面的代码时,我只会得到作者的名字.
When I run git log
, I get the this line for each commit: "Author: name < email >". How do I get the exact same format for a commit in Python for a local repo? When I run the code below, I get just the author name.
from git import Repo
repo_path = 'mockito'
repo = Repo(repo_path)
commits_list = list(repo.iter_commits())
for i in range(5):
commit = commits_list[i]
print(commit.hexsha)
print(commit.author)
print(commit.committer)
推荐答案
根据 gitgitAPI文档,提交对象-类git.objects.commit.Commit
的实例-具有 author
和 committer
属性,它们是
According to the gitpython API documentation, a commit object—an instance of class git.objects.commit.Commit
—has author
and committer
attributes that are instances of class git.util.Actor
, which in turn has fields conf_email
, conf_name
, email
, and name
.
因此(未试用):
print(commit.author.name, commit.author.email)
可能会为您提供所需的两个字段,尽管您可能希望以某种方式设置它们的格式.
will likely get you the two fields you want, though you may wish to format them in some way.
编辑:由于我没有,我将遵循吉诺·曼宾的答案已安装gitpython对此进行测试.
Edit: I'll defer to Gino Mempin's answer since I don't have gitpython installed to test this.
这篇关于如何使用gitpython获取提交作者的姓名和电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!