Skip to content

Commit cace16f

Browse files
peffJunio C Hamano
authored andcommitted
Add a sample program 'blameview' to show how to use git-blame --incremental
Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 4b3b1e1 commit cace16f

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

contrib/blameview/README

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This is a sample program to use 'git-blame --incremental', based
2+
on this message.
3+
4+
From: Jeff King <peff@peff.net>
5+
Subject: Re: More precise tag following
6+
To: Linus Torvalds <torvalds@linux-foundation.org>
7+
Cc: git@vger.kernel.org
8+
Date: Sat, 27 Jan 2007 18:52:38 -0500
9+
Message-ID: <20070127235238.GA28706@coredump.intra.peff.net>
10+

contrib/blameview/blameview.perl

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/perl
2+
3+
use Gtk2 -init;
4+
use Gtk2::SimpleList;
5+
6+
my $fn = shift or die "require filename to blame";
7+
8+
Gtk2::Rc->parse_string(<<'EOS');
9+
style "treeview_style"
10+
{
11+
GtkTreeView::vertical-separator = 0
12+
}
13+
class "GtkTreeView" style "treeview_style"
14+
EOS
15+
16+
my $window = Gtk2::Window->new('toplevel');
17+
$window->signal_connect(destroy => sub { Gtk2->main_quit });
18+
my $scrolled_window = Gtk2::ScrolledWindow->new;
19+
$window->add($scrolled_window);
20+
my $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+
31+
open(my $fh, '<', $fn)
32+
or die "unable to open $fn: $!";
33+
while(<$fh>) {
34+
chomp;
35+
$fileview->{data}->[$.] = ['HEAD', '?', "$fn:$.", $_];
36+
}
37+
38+
my $blame;
39+
open($blame, '-|', qw(git blame --incremental --), $fn)
40+
or die "cannot start git-blame $fn";
41+
42+
Glib::IO->add_watch(fileno($blame), 'in', \&read_blame_line);
43+
44+
$window->show_all;
45+
Gtk2->main;
46+
exit 0;
47+
48+
my %commitinfo = ();
49+
50+
sub 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+
69+
my $buf;
70+
my $current;
71+
sub 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+
107+
sub 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+
}

0 commit comments

Comments
 (0)