Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get commit author name and email with gitpython?

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)
like image 931
Alon Avatar asked Apr 22 '26 16:04

Alon


1 Answers

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.

Hence (untested):

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.

Edit: I'll defer to Gino Mempin's answer since I don't have gitpython installed to test this.

like image 76
torek Avatar answered Apr 24 '26 05:04

torek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!