如何使用gitpython获取提交作者的姓名和电子邮件?

问题描述 投票:0回答:1

当我运行git log时,每次提交都会得到以下行:“作者:姓名”。如何为本地存储库的Python提交提供完全相同的格式?当我运行下面的代码时,我只会得到作者的名字。

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)
python git gitpython
1个回答
3
投票

根据the gitpython API documentationcommit object-class git.objects.commit.Commit的实例-具有authorcommitter属性,它们是class git.util.Actor的实例,而这些属性又具有字段class git.util.Actorconf_emailconf_nameemail

因此(未试用):

name

尽管您可能希望以某种方式设置它们的格式,但可能会为您提供两个所需的字段。

Edit:因为我没有安装gitpython来进行测试,所以我将遵循print(commit.author.name, commit.author.email)


2
投票

似乎gitpython的Gino Mempin's answer对象没有作者电子邮件的属性。

您还可以将gitpython用于Commit。您可以使用call git commands directly命令,传入commit HASH(来自git show),然后传入git show选项,仅提供作者的姓名和电子邮件(当然,您也可以传递所需的其他格式选项)。 >

使用普通git:

commit.hexsha

将gitpython用于--format

--format
© www.soinside.com 2019 - 2024. All rights reserved.