Skip to content

Instantly share code, notes, and snippets.



Last active Jan 18, 2020
What would you like to do?
from git import Repo
import os
import json
import click
class GitLogger:
"""to provide a log as dict of commits which are json printable"""
def __init__(self, path):
"""Create a GitStepper with the path to the git repository (not a bare repository)"""
self.repo = Repo(path)
def log(self):
"""return a dict of commits"""
commits = (self.repo.commit(logEntry) for logEntry in self.repo.iter_commits())
return (self.to_dict(x) for x in commits)
def to_dict(self,commit):
"""create a dict out of a commit that is easy to json serialize"""
return {
"author_email" :,
"author_name" :,
"authored_date" : commit.authored_datetime.isoformat(),
"changes": commit.stats.files,
"committed_date" : commit.committed_datetime.isoformat(),
"committer_email" :,
"committer_name" :,
"encoding" : commit.encoding,
"hash" : commit.hexsha,
"message" : commit.message ,
"summary" : commit.summary,
"size" : commit.size,
"stats_total" :,
"parents" : [parent.hexsha for parent in commit.parents],
@click.argument("path", type=click.Path(exists=True), envvar='PWD')
def main(path):
for entry in GitLogger(path).log():
if __name__ == '__main__':

This comment has been minimized.

Copy link

@khalidelhaji khalidelhaji commented Aug 8, 2019



This comment has been minimized.

Copy link

@nilleb nilleb commented Jan 18, 2020

huge judos to you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.