Skip to content

Instantly share code, notes, and snippets.

@mrVanDalo mrVanDalo/gitog2json
Last active Aug 8, 2019

Embed
What would you like to do?
gitlog2json
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" : commit.author.email,
"author_name" : commit.author.name,
"authored_date" : commit.authored_datetime.isoformat(),
"changes": commit.stats.files,
"committed_date" : commit.committed_datetime.isoformat(),
"committer_email" : commit.committer.email,
"committer_name" : commit.committer.name,
"encoding" : commit.encoding,
"hash" : commit.hexsha,
"message" : commit.message ,
"summary" : commit.summary,
"size" : commit.size,
"stats_total" : commit.stats.total,
"parents" : [parent.hexsha for parent in commit.parents],
}
@click.command()
@click.argument("path", type=click.Path(exists=True), envvar='PWD')
def main(path):
for entry in GitLogger(path).log():
print(json.dumps(entry))
if __name__ == '__main__':
main()
@khalidelhaji

This comment has been minimized.

Copy link

commented Aug 8, 2019

Dope!

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.