Last active
January 2, 2021 01:24
-
-
Save mrVanDalo/6a1d1aed4bd613fbdf1fa751fca47c6a to your computer and use it in GitHub Desktop.
gitlog2json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah I'm no python expert, I actually barley use it. I would just try and error my way through the kwargs.
Sounds reasonable. I used the
(.. for key in ... )
form because the documentation says it's lazy. I was working with quite big repositories. Creating a list upfront was consuming to much RAM (in my cases). But if it works for you, all good :D