1#!/usr/bin/perl
2
3use Gtk2 -init;
4use Gtk2::SimpleList;
5
6my $fn = shift or die "require filename to blame";
7
8Gtk2::Rc->parse_string(<<'EOS');
9style "treeview_style"
10{
11 GtkTreeView::vertical-separator = 0
12}
13class "GtkTreeView" style "treeview_style"
14EOS
15
16my $window = Gtk2::Window->new('toplevel');
17$window->signal_connect(destroy => sub { Gtk2->main_quit });
18my $scrolled_window = Gtk2::ScrolledWindow->new;
19$window->add($scrolled_window);
20my $fileview = Gtk2::SimpleList->new(
21 'Commit' => 'text',
22 'CommitInfo' => 'text',
23 'FileLine' => 'text',
24 'Data' => 'text'
25);
26$scrolled_window->add($fileview);
27$fileview->get_column(0)->set_spacing(0);
28$fileview->set_size_request(1024, 768);
29$fileview->set_rules_hint(1);
30
31open(my $fh, '<', $fn)
32 or die "unable to open $fn: $!";
33while(<$fh>) {
34 chomp;
35 $fileview->{data}->[$.] = ['HEAD', '?', "$fn:$.", $_];
36}
37
38my $blame;
39open($blame, '-|', qw(git blame --incremental --), $fn)
40 or die "cannot start git-blame $fn";
41
42Glib::IO->add_watch(fileno($blame), 'in', \&read_blame_line);
43
44$window->show_all;
45Gtk2->main;
46exit 0;
47
48my %commitinfo = ();
49
50sub flush_blame_line {
51 my ($attr) = @_;
52
53 return unless defined $attr;
54
55 my ($commit, $s_lno, $lno, $cnt) =
56 @{$attr}{qw(COMMIT S_LNO LNO CNT)};
57
58 my ($filename, $author, $author_time, $author_tz) =
59 @{$commitinfo{$commit}}{qw(FILENAME AUTHOR AUTHOR-TIME AUTHOR-TZ)};
60 my $info = $author . ' ' . format_time($author_time, $author_tz);
61
62 for(my $i = 0; $i < $cnt; $i++) {
63 @{$fileview->{data}->[$lno+$i-1]}[0,1,2] =
64 (substr($commit, 0, 8), $info,
65 $filename . ':' . ($s_lno+$i));
66 }
67}
68
69my $buf;
70my $current;
71sub read_blame_line {
72
73 my $r = sysread($blame, $buf, 1024, length($buf));
74 die "I/O error" unless defined $r;
75
76 if ($r == 0) {
77 flush_blame_line($current);
78 $current = undef;
79 return 0;
80 }
81
82 while ($buf =~ s/([^\n]*)\n//) {
83 my $line = $1;
84
85 if (($commit, $s_lno, $lno, $cnt) =
86 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+) (\d+)$/)) {
87 flush_blame_line($current);
88 $current = +{
89 COMMIT => $1,
90 S_LNO => $2,
91 LNO => $3,
92 CNT => $4,
93 };
94 next;
95 }
96
97 # extended attribute values
98 if ($line =~ /^(author|author-mail|author-time|author-tz|committer|committer-mail|committer-time|committer-tz|summary|filename) (.*)$/) {
99 my $commit = $current->{COMMIT};
100 $commitinfo{$commit}{uc($1)} = $2;
101 next;
102 }
103 }
104 return 1;
105}
106
107sub format_time {
108 my $time = shift;
109 my $tz = shift;
110
111 my $minutes = $tz < 0 ? 0-$tz : $tz;
112 $minutes = ($minutes / 100)*60 + ($minutes % 100);
113 $minutes = $tz < 0 ? 0-$minutes : $minutes;
114 $time += $minutes * 60;
115 my @t = gmtime($time);
116 return sprintf('%04d-%02d-%02d %02d:%02d:%02d %s',
117 $t[5] + 1900, @t[4,3,2,1,0], $tz);
118}