Skip to content

Instantly share code, notes, and snippets.

@pjlsergeant
Created November 27, 2012 12:42
Show Gist options
  • Save pjlsergeant/4154044 to your computer and use it in GitHub Desktop.
Save pjlsergeant/4154044 to your computer and use it in GitHub Desktop.
GraphViz Git Commit Tree
#!perl
# Draw the commit graph of a git repository, using GraphViz. Supply the directory
# the repo is in as the first argument. Writes a file called 'repo.svg' to the CWD.
use strict; use warnings;
use Cwd;
use Git::PurePerl;
use GraphViz2;
my $directory = $ARGV[0] || getcwd();
# Load the GIT repo
my $repo = Git::PurePerl->new( directory => $directory );
my $head = $repo->master;
# Start a new Graph
my ($graph) = GraphViz2->new(
edge => { color => 'grey' },
global => { directed => 1 },
graph => { label => 'Repo Commit Tree', rankdir => 'TB' },
node => { shape => 'oval' },
);
# Recurse up the commit graph
add_commit( $head );
my %seen;
sub add_commit {
my $commit = shift;
my $name = substr( $commit->{'sha1'}, 0, 6 );
# Skip nodes we've already seen
return $name if $seen{ $name }++;
# Add the commit if we haven't already seen it
$graph->add_node( name => $name );
# Link back to parents
for my $parent ( $commit->parents ) {
no warnings 'recursion';
my $parent_name = add_commit( $parent );
$graph->add_edge( from => $parent_name, to => $name );
}
return $name;
}
$graph->run( output_file => 'repo.svg' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment