gitweb: (gr)avatar support
Introduce avatar support: the feature adds the appropriate img tag next to author and committer in commit(diff), history, shortlog, log and tag views. Multiple avatar providers are possible, but only gravatar is implemented at the moment. Gravatar support depends on Digest::MD5, which is a core package since Perl 5.8. If gravatars are activated but Digest::MD5 cannot be found, the feature will be automatically disabled. No avatar provider is selected by default, except in the t9500 test. Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Giuseppe Bilotta committed
Jun 30, 2009 at 00:00 UTC
e9fdd74e5374dd1efe1577057d14a2836b4cae78
3 files changed
+86
-3
gitweb/gitweb.css
+4
@@ -28,6 +28,10 @@ img.logo {
28
border-width: 0px;
29
}
30
31
+img.avatar {
32
+ vertical-align: middle;
33
+}
34
+
35
div.page_header {
36
height: 25px;
37
padding: 8px;
gitweb/gitweb.perl
+80
-3
@@ -195,6 +195,14 @@ our %known_snapshot_format_aliases = (
195
'x-zip' => undef, '' => undef,
196
);
197
198
+# Pixel sizes for icons and avatars. If the default font sizes or lineheights
199
+# are changed, it may be appropriate to change these values too via
200
+# $GITWEB_CONFIG.
201
+our %avatar_size = (
202
+ 'default' => 16,
203
+ 'double' => 32
204
+);
205
+
206
# You define site-wide feature defaults here; override them with
207
# $GITWEB_CONFIG as necessary.
208
our %feature = (
@@ -365,6 +373,24 @@ our %feature = (
373
'sub' => \&feature_patches,
374
'override' => 0,
375
'default' => [16]},
376
+
377
+ # Avatar support. When this feature is enabled, views such as
378
+ # shortlog or commit will display an avatar associated with
379
+ # the email of the committer(s) and/or author(s).
380
+
381
+ # Currently only the gravatar provider is available, and it
382
+ # depends on Digest::MD5. If an unknown provider is specified,
383
+ # the feature is disabled.
384
+
385
+ # To enable system wide have in $GITWEB_CONFIG
386
+ # $feature{'avatar'}{'default'} = ['gravatar'];
387
+ # To have project specific config enable override in $GITWEB_CONFIG
388
+ # $feature{'avatar'}{'override'} = 1;
389
+ # and in project config gitweb.avatar = gravatar;
390
+ 'avatar' => {
391
+ 'sub' => \&feature_avatar,
392
+ 'override' => 0,
393
+ 'default' => ['']},
394
);
395
396
sub gitweb_get_feature {
@@ -433,6 +459,12 @@ sub feature_patches {
459
return ($_[0]);
460
}
461
462
+sub feature_avatar {
463
+ my @val = (git_get_project_config('avatar'));
464
+
465
+ return @val ? @val : @_;
466
+}
467
+
468
# checking HEAD file with -e is fragile if the repository was
469
# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
470
# and then pruned.
@@ -814,6 +846,17 @@ $git_dir = "$projectroot/$project" if $project;
846
our @snapshot_fmts = gitweb_get_feature('snapshot');
847
@snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
848
849
+# check that the avatar feature is set to a known provider name,
850
+# and for each provider check if the dependencies are satisfied.
851
+# if the provider name is invalid or the dependencies are not met,
852
+# reset $git_avatar to the empty string.
853
+our ($git_avatar) = gitweb_get_feature('avatar');
854
+if ($git_avatar eq 'gravatar') {
855
+ $git_avatar = '' unless (eval { require Digest::MD5; 1; });
856
+} else {
857
+ $git_avatar = '';
858
+}
859
+
860
# dispatch
861
if (!defined $action) {
862
if (defined $hash) {
@@ -1469,6 +1512,34 @@ sub format_subject_html {
1512
}
1513
}
1514
1515
+# Insert an avatar for the given $email at the given $size if the feature
1516
+# is enabled.
1517
+sub git_get_avatar {
1518
+ my ($email, %opts) = @_;
1519
+ my $pre_white = ($opts{-pad_before} ? " " : "");
1520
+ my $post_white = ($opts{-pad_after} ? " " : "");
1521
+ $opts{-size} ||= 'default';
1522
+ my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1523
+ my $url = "";
1524
+ if ($git_avatar eq 'gravatar') {
1525
+ $url = "http://www.gravatar.com/avatar/" .
1526
+ Digest::MD5::md5_hex(lc $email) . "?s=$size";
1527
+ }
1528
+ # Currently only gravatars are supported, but other forms such as
1529
+ # picons can be added by putting an else up here and defining $url
1530
+ # as needed. If no variant puts something in $url, we assume avatars
1531
+ # are completely disabled/unavailable.
1532
+ if ($url) {
1533
+ return $pre_white .
1534
+ "<img width=\"$size\" " .
1535
+ "class=\"avatar\" " .
1536
+ "src=\"$url\" " .
1537
+ "/>" . $post_white;
1538
+ } else {
1539
+ return "";
1540
+ }
1541
+}
1542
+
1543
# format the author name of the given commit with the given tag
1544
# the author name is chopped and escaped according to the other
1545
# optional parameters (see chop_str).
@@ -1476,7 +1547,9 @@ sub format_author_html {
1547
my $tag = shift;
1548
my $co = shift;
1549
my $author = chop_and_escape_str($co->{'author_name'}, @_);
1479
- return "<$tag class=\"author\">" . $author . "</$tag>";
1550
+ return "<$tag class=\"author\">" .
1551
+ git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1552
+ $author . "</$tag>";
1553
}
1554
1555
# format git diff header line, i.e. "diff --(git|combined|cc) ..."
@@ -3252,7 +3325,8 @@ sub git_print_authorship {
3325
esc_html($co->{'author_name'}) .
3326
" [$ad{'rfc2822'}";
3327
print_local_time(%ad) if ($opts{-localtime});
3255
- print "]</$tag>\n";
3328
+ print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3329
+ . "</$tag>\n";
3330
}
3331
3332
# Outputs table rows containing the full author or committer information,
@@ -3267,7 +3341,10 @@ sub git_print_authorship_rows {
3341
@people = ('author', 'committer') unless @people;
3342
foreach my $who (@people) {
3343
my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3270
- print "<tr><td>$who</td><td>" . esc_html($co->{$who}) . "</td></tr>\n".
3344
+ print "<tr><td>$who</td><td>" . esc_html($co->{$who}) . "</td>" .
3345
+ "<td rowspan=\"2\">" .
3346
+ git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3347
+ "</td></tr>\n" .
3348
"<tr>" .
3349
"<td></td><td> $wd{'rfc2822'}";
3350
print_local_time(%wd);
t/t9500-gitweb-standalone-no-errors.sh
+2
@@ -660,6 +660,7 @@ cat >>gitweb_config.perl <<EOF
660
661
\$feature{'blame'}{'override'} = 1;
662
\$feature{'snapshot'}{'override'} = 1;
663
+\$feature{'avatar'}{'override'} = 1;
664
EOF
665
666
test_expect_success \
@@ -671,6 +672,7 @@ test_expect_success \
672
'config override: tree view, features disabled in repo config' \
673
'git config gitweb.blame no &&
674
git config gitweb.snapshot none &&
675
+ git config gitweb.avatar gravatar &&
676
gitweb_run "p=.git;a=tree"'
677
test_debug 'cat gitweb.log'
678