11 } elsif ( @ARGV == 2 ) {
15 die "Usage blameview [<rev>] <filename>";
18 Gtk2
::Rc
->parse_string(<<'EOS');
19 style "treeview_style"
21 GtkTreeView::vertical-separator = 0
23 class "GtkTreeView" style "treeview_style"
26 my $window = Gtk2
::Window
->new('toplevel');
27 $window->signal_connect(destroy
=> sub { Gtk2
->main_quit });
28 my $vpan = Gtk2
::VPaned
->new();
30 my $scrolled_window = Gtk2
::ScrolledWindow
->new;
31 $vpan->pack1($scrolled_window, 1, 1);
32 my $fileview = Gtk2
::SimpleList
->new(
37 $scrolled_window->add($fileview);
38 $fileview->get_column(0)->set_spacing(0);
39 $fileview->set_size_request(1024, 768);
40 $fileview->set_rules_hint(1);
41 $fileview->signal_connect (row_activated
=> sub {
42 my ($sl, $path, $column) = @_;
43 my $row_ref = $sl->get_row_data_from_path ($path);
44 system("blameview @$row_ref[0]~1 $fn &");
47 my $commitwindow = Gtk2
::ScrolledWindow
->new();
48 $commitwindow->set_policy ('GTK_POLICY_AUTOMATIC','GTK_POLICY_AUTOMATIC');
49 $vpan->pack2($commitwindow, 1, 1);
50 my $commit_text = Gtk2
::TextView
->new();
51 my $commit_buffer = Gtk2
::TextBuffer
->new();
52 $commit_text->set_buffer($commit_buffer);
53 $commitwindow->add($commit_text);
55 $fileview->signal_connect (cursor_changed
=> sub {
57 my ($path, $focus_column) = $sl->get_cursor();
58 my $row_ref = $sl->get_row_data_from_path ($path);
60 open($c_fh, '-|', "git cat-file commit @$row_ref[0]")
61 or die "unable to find commit @$row_ref[0]";
63 $commit_buffer->set_text("@buffer");
68 open($fh, '-|', "git cat-file blob $hash:$fn")
69 or die "unable to open $fn: $!";
73 $fileview->{data
}->[$.] = ['HEAD', "$fn:$.", $_];
77 open($blame, '-|', qw(git blame --incremental --), $fn, $hash)
78 or die "cannot start git-blame $fn";
80 Glib
::IO
->add_watch(fileno($blame), 'in', \
&read_blame_line
);
88 sub flush_blame_line
{
91 return unless defined $attr;
93 my ($commit, $s_lno, $lno, $cnt) =
94 @
{$attr}{qw(COMMIT S_LNO LNO CNT)};
96 my ($filename, $author, $author_time, $author_tz) =
97 @
{$commitinfo{$commit}}{qw(FILENAME AUTHOR AUTHOR-TIME AUTHOR-TZ)};
98 my $info = $author . ' ' . format_time
($author_time, $author_tz);
100 for(my $i = 0; $i < $cnt; $i++) {
101 @
{$fileview->{data
}->[$lno+$i-1]}[0,1,2] =
102 (substr($commit, 0, 8), $filename . ':' . ($s_lno+$i));
108 sub read_blame_line
{
110 my $r = sysread($blame, $buf, 1024, length($buf));
111 die "I/O error" unless defined $r;
114 flush_blame_line
($current);
119 while ($buf =~ s/([^\n]*)\n//) {
122 if (($commit, $s_lno, $lno, $cnt) =
123 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+) (\d+)$/)) {
124 flush_blame_line
($current);
134 # extended attribute values
135 if ($line =~ /^(author|author-mail|author-time|author-tz|committer|committer-mail|committer-time|committer-tz|summary|filename) (.*)$/) {
136 my $commit = $current->{COMMIT
};
137 $commitinfo{$commit}{uc($1)} = $2;
148 my $minutes = $tz < 0 ?
0-$tz : $tz;
149 $minutes = ($minutes / 100)*60 + ($minutes % 100);
150 $minutes = $tz < 0 ?
0-$minutes : $minutes;
151 $time += $minutes * 60;
152 my @t = gmtime($time);
153 return sprintf('%04d-%02d-%02d %02d:%02d:%02d %s',
154 $t[5] + 1900, @t[4,3,2,1,0], $tz);