Skip to content

Instantly share code, notes, and snippets.

@cstorey
Created November 23, 2014 22:00
Show Gist options
  • Save cstorey/619a6ca4789879b8e974 to your computer and use it in GitHub Desktop.
Save cstorey/619a6ca4789879b8e974 to your computer and use it in GitHub Desktop.
A trivial script to visualise a git history as a graphviz diagram.
import os, sys
from pygit2 import Repository, GIT_SORT_TOPOLOGICAL, GIT_SORT_REVERSE
import pygit2
from graphviz import Digraph
repository_path = pygit2.discover_repository(os.getcwd())
repo = Repository(repository_path)
def nfmt(target):
return "N{0}".format(target.id)
g = Digraph(comment=repo.head.name, edge_attr={'dir': 'back'})
for i, commit in enumerate(repo.walk(repo.head.target, GIT_SORT_TOPOLOGICAL)):
g.node(nfmt(commit), commit.message.split('\n')[0].strip())
for p in commit.parents:
g.edge(nfmt(commit), nfmt(p))
# if i > 2000:
# break
print g.source.encode('utf8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment