Skip to content

Instantly share code, notes, and snippets.

@dg
Last active October 27, 2021 19:50
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dg/f404a54592142d162ec71918e091752b to your computer and use it in GitHub Desktop.
Git log to JSON
# install pygit2: pip install pygit2
import pygit2
import json
repo = pygit2.Repository('path/to/repository')
last = repo[repo.head.target]
data = []
for commit in repo.walk(last.id):
data.append({
'hash': str(commit.id),
'message': commit.message,
'author': {
'name': commit.author.name,
'email': commit.author.email,
'time': commit.author.time,
},
'committer': {
'name': commit.committer.name,
'email': commit.committer.email,
'time': commit.committer.time,
},
'changes': len(repo.diff(commit.tree, commit.parents[0])) if commit.parents else 0,
})
json = json.dumps(data, indent=4)
print(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment