Skip to content

Instantly share code, notes, and snippets.

@davidfischer
Last active December 18, 2015 11:19
Show Gist options
  • Save davidfischer/5774538 to your computer and use it in GitHub Desktop.
Save davidfischer/5774538 to your computer and use it in GitHub Desktop.
Converts git commits to JSON using pygit2
import json
import sys
from datetime import datetime
import pygit2
def main(repository):
repo = pygit2.Repository(repository)
commits = []
for commit in repo.walk(repo.head.oid, pygit2.GIT_SORT_TIME):
commits.append({
'hash': commit.hex,
'message': commit.message,
'commit_date': datetime.utcfromtimestamp(
commit.commit_time).strftime('%Y-%m-%dT%H:%M:%SZ'),
'author_name': commit.author.name,
'author_email': commit.author.email,
'parents': [c.hex for c in commit.parents],
})
print(json.dumps(commits, indent=2))
if __name__ == '__main__':
if len(sys.argv) != 2:
print("USAGE: {0} <repository>".format(__file__))
sys.exit(0)
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment