Skip to content

Commit e9fdd74

Browse files
Oblomovgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ba92473 commit e9fdd74

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

gitweb/gitweb.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ img.logo {
2828
border-width: 0px;
2929
}
3030

31+
img.avatar {
32+
vertical-align: middle;
33+
}
34+
3135
div.page_header {
3236
height: 25px;
3337
padding: 8px;

gitweb/gitweb.perl

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ BEGIN
195195
'x-zip' => undef, '' => undef,
196196
);
197197

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+
198206
# You define site-wide feature defaults here; override them with
199207
# $GITWEB_CONFIG as necessary.
200208
our %feature = (
@@ -365,6 +373,24 @@ BEGIN
365373
'sub' => \&feature_patches,
366374
'override' => 0,
367375
'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' => ['']},
368394
);
369395

370396
sub gitweb_get_feature {
@@ -433,6 +459,12 @@ sub feature_patches {
433459
return ($_[0]);
434460
}
435461

462+
sub feature_avatar {
463+
my @val = (git_get_project_config('avatar'));
464+
465+
return @val ? @val : @_;
466+
}
467+
436468
# checking HEAD file with -e is fragile if the repository was
437469
# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
438470
# and then pruned.
@@ -814,6 +846,17 @@ sub evaluate_path_info {
814846
our @snapshot_fmts = gitweb_get_feature('snapshot');
815847
@snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
816848

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+
817860
# dispatch
818861
if (!defined $action) {
819862
if (defined $hash) {
@@ -1469,14 +1512,44 @@ sub format_subject_html {
14691512
}
14701513
}
14711514

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} ? "&nbsp;" : "");
1520+
my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
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+
14721543
# format the author name of the given commit with the given tag
14731544
# the author name is chopped and escaped according to the other
14741545
# optional parameters (see chop_str).
14751546
sub format_author_html {
14761547
my $tag = shift;
14771548
my $co = shift;
14781549
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>";
14801553
}
14811554

14821555
# format git diff header line, i.e. "diff --(git|combined|cc) ..."
@@ -3252,7 +3325,8 @@ sub git_print_authorship {
32523325
esc_html($co->{'author_name'}) .
32533326
" [$ad{'rfc2822'}";
32543327
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";
32563330
}
32573331

32583332
# Outputs table rows containing the full author or committer information,
@@ -3267,7 +3341,10 @@ sub git_print_authorship_rows {
32673341
@people = ('author', 'committer') unless @people;
32683342
foreach my $who (@people) {
32693343
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" .
32713348
"<tr>" .
32723349
"<td></td><td> $wd{'rfc2822'}";
32733350
print_local_time(%wd);

t/t9500-gitweb-standalone-no-errors.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ cat >>gitweb_config.perl <<EOF
660660
661661
\$feature{'blame'}{'override'} = 1;
662662
\$feature{'snapshot'}{'override'} = 1;
663+
\$feature{'avatar'}{'override'} = 1;
663664
EOF
664665

665666
test_expect_success \
@@ -671,6 +672,7 @@ test_expect_success \
671672
'config override: tree view, features disabled in repo config' \
672673
'git config gitweb.blame no &&
673674
git config gitweb.snapshot none &&
675+
git config gitweb.avatar gravatar &&
674676
gitweb_run "p=.git;a=tree"'
675677
test_debug 'cat gitweb.log'
676678

0 commit comments

Comments
 (0)