Skip to contentSkip to conversation

gitweb: gravatar support

PATCH Superseded Giuseppe Bilotta 4 participants 12 messages Jun 15, 2016

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 cover gitweb: gravatar support

Fifth iteration of the implementation of gravatar support in gitweb.

Hopefully, this time I didn't forget any of the recommended suggestions. It surely looks much more flexible and extensible to my eyes 8-). I also found the time to implement the url cache for gravatar; I didn't benchmark its effects, but it does feel faster when loading shortlog pages.

Giuseppe Bilotta (3):
gitweb: refactor author name insertion
gitweb: gravatar support
gitweb: gravatar url cache

 gitweb/gitweb.css  |    9 +++-
 gitweb/gitweb.perl |  141 ++++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 117 insertions(+), 33 deletions(-)
#1 lore raw

PATCH v5 1/3 gitweb: refactor author name insertion

The refactoring allows easier customization of the output by means of CSS and improves extensibility on the CGI side too.

Layout is preserved for all views except for 'commitdiff', which now uses the same format as 'commit'.

---
 gitweb/gitweb.css  |    5 ++-
 gitweb/gitweb.perl |   79 +++++++++++++++++++++++++++++++---------------------
 2 files changed, 51 insertions(+), 33 deletions(-)
gitweb/gitweb.css +4 -1
index a01eac8..68b22ff 100644
@@ -132,11 +132,14 @@ div.list_head {
 	font-style: italic;
 }
 
+.author_date, .author {
+	font-style: italic;
+}
+
 div.author_date {
 	padding: 8px;
 	border: solid #d9d8d1;
 	border-width: 0px 0px 1px 0px;
-	font-style: italic;
 }
 
 a.list {
gitweb/gitweb.perl +47 -32

148 lines not on this page · raw message

Signature · 1 line
1.6.3.rc1.192.gdbfcb
#2 lore raw

PATCH v5 2/3 gitweb: gravatar support

Introduce gravatar support by adding the appropriate img tag next to author and committer in commit(diff), history, shortlog and log view.

The feature is disabled by default, and depends on Digest::MD5, which is a core package since Perl 5.8. If Digest::MD5 cannot be found, enabling this feature results in a no-op.

The avatar insertion code is made generic enough to allow other avatar sources to be added easily.

---
 gitweb/gitweb.css  |    4 +++
 gitweb/gitweb.perl |   64 +++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 62 insertions(+), 6 deletions(-)
gitweb/gitweb.css +4 -0
index 68b22ff..ddf982b 100644
@@ -28,6 +28,10 @@ img.logo {
 	border-width: 0px;
 }
 
+img.avatar {
+	vertical-align:middle;
+}
+
 div.page_header {
 	height: 25px;
 	padding: 8px;
gitweb/gitweb.perl +58 -6

112 lines not on this page · raw message

Signature · 1 line
1.6.3.rc1.192.gdbfcb
#3 lore raw

PATCH v5 3/3 gitweb: gravatar url cache

Views which contain many occurrences of the same email address (e.g. shortlog view) benefit from not having to recalculate the MD5 of the email address every time.

---
 gitweb/gitweb.perl |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
gitweb/gitweb.perl +12 -2
index 6e807fe..10ca0fe 100755
@@ -3249,6 +3249,17 @@ sub git_print_header_div {
 	      "\n</div>\n";
 }
 
+our %gravatar_url_cache = ();
+
+sub gravatar_url {
+	my $email = lc shift;
+	my $size = shift;
+	$gravatar_url_cache{$email} ||=
+		"http://www.gravatar.com/avatar.php?gravatar_id=" .
+			Digest::MD5::md5_hex($email) . "&amp;size=";
+	return $gravatar_url_cache{$email} . $size;
+}
+
 # Insert an avatar for the given $email at the given $size if the feature
 # is enabled.
 sub git_get_avatar {
@@ -3258,8 +3269,7 @@ sub git_get_avatar {
 	my $size = $avatar_size{$params{'size'}} || $avatar_size{'default'};
 	my $url = "";
 	if ($git_gravatar_enabled) {
-		$url = "http://www.gravatar.com/avatar.php?gravatar_id=" .
-			Digest::MD5::md5_hex(lc $email) . "&amp;size=$size";
+		$url = gravatar_url($email, $size);
 	}
 	# Currently only gravatars are supported, but other forms such as
 	# picons can be added by putting an else up here and defining $url
Signature · 1 line
1.6.3.rc1.192.gdbfcb
#4 lore raw

Re: [PATCHv5 3/3] gitweb: gravatar url cache

Quoted 4 lines · Giuseppe Bilotta

Views which contain many occurrences of the same email address (e.g. shortlog view) benefit from not having to recalculate the MD5 of the email address every time.
---

Sign-off?

I think the cache is placed at the wrong level (it doesn't have to be a GRavatar_url_cache, but can be a general avatar_url_cache).

IOW,

	our %avatar_url_cache = ();
        sub git_get_avatar {
        	...
                my $url;
                if (!exists $avatar_url_cache{$email}) {
                	if ($git_gravatar_enabled) {
                        	$url = ... gravatar stuff ...;
			} else if ($git_whatever_enabled) {
                        	$url = ... other stuff ...;
			}
                        $avatar_url_cache{$email} = $url;
		}
                $url = $avatar_url_cache{$email};
                if (defined $url) {
                	return ... prefix then $url then suffix ...;
                } else {
                	return "";
                }
	}

But the basic idea is sound, I think.

#5 lore raw

Re: [PATCHv5 3/3] gitweb: gravatar url cache

Quoted 8 lines · Junio C Hamano
Quoted 4 lines · Giuseppe Bilotta

Views which contain many occurrences of the same email address (e.g. shortlog view) benefit from not having to recalculate the MD5 of the email address every time.
---

Sign-off?

Duh.

Quoted 2 lines

I think the cache is placed at the wrong level (it doesn't have to be a GRavatar_url_cache, but can be a general avatar_url_cache).

I'm not sure about it. The URL depends on email and size (can you use arrays as hash keys in Perl?) , and the email part might be the same but the size part might differ across separate calls (in theory; in practice avatars in a view are presently all the same size; but for example if we were to autodetect email addresses in commit messages, we might have both single- and double- sided avatars in the same page). By hashing on email+size only we would lose the benefit of cache when using the same avatar at separate sizes.

Signature · 1 line
Giuseppe "Oblomov" Bilotta
#6 lore raw

Re: [PATCHv5 3/3] gitweb: gravatar url cache

Quoted 15 lines · Giuseppe Bilotta
Quoted 8 lines · Junio C Hamano
Quoted 4 lines · Giuseppe Bilotta

Views which contain many occurrences of the same email address (e.g. shortlog view) benefit from not having to recalculate the MD5 of the email address every time.
---

Sign-off?

Duh.

Quoted 2 lines

I think the cache is placed at the wrong level (it doesn't have to be a GRavatar_url_cache, but can be a general avatar_url_cache).

Quoted 8 lines

I'm not sure about it. The URL depends on email and size (can you use arrays as hash keys in Perl?) , and the email part might be the same but the size part might differ across separate calls (in theory; in practice avatars in a view are presently all the same size; but for example if we were to autodetect email addresses in commit messages, we might have both single- and double- sided avatars in the same page). By hashing on email+size only we would lose the benefit of cache when using the same avatar at separate sizes.

You can use nested hash, like:

	$avatar_cache{$email}{$size}

or

	$avatar_cache{$size}{$email}

but that is completely orthogonal to the issue of using a single cache shared across services, isn't it?

#7 lore raw

Re: [PATCHv5 3/3] gitweb: gravatar url cache

In reply to Giuseppe Bilotta
Quoted 5 lines · Giuseppe Bilotta
Quoted 2 lines · Junio C Hamano

I think the cache is placed at the wrong level (it doesn't have to be a GRavatar_url_cache, but can be a general avatar_url_cache).

Quoted 9 lines

I'm not sure about it. The URL depends on email and size (can you use arrays as hash keys in Perl?) , and the email part might be the same but the size part might differ across separate calls (in theory; in practice avatars in a view are presently all the same size; but for example if we were to autodetect email addresses in commit messages, we might have both single- and double- sided avatars in the same page). By hashing on email+size only we would lose the benefit of cache when using the same avatar at separate sizes.

You could have a hash key of "$email_$size", or something similar to fake an array hash key.

#8 lore raw

Re: [PATCHv5 3/3] gitweb: gravatar url cache

Quoted 16 lines · Jacob Helwig
Quoted 5 lines · Giuseppe Bilotta
Quoted 2 lines · Junio C Hamano

I think the cache is placed at the wrong level (it doesn't have to be a GRavatar_url_cache, but can be a general avatar_url_cache).

Quoted 8 lines

I'm not sure about it. The URL depends on email and size (can you use arrays as hash keys in Perl?) , and the email part might be the same but the size part might differ across separate calls (in theory; in practice avatars in a view are presently all the same size; but for example if we were to autodetect email addresses in commit messages, we might have both single- and double- sided avatars in the same page). By hashing on email+size only we would lose the benefit of cache when using the same avatar at separate sizes.

Quoted 2 lines

You could have a hash key of "$email_$size", or something similar to fake an array hash key.

The point is not so much the form to give to the key but rather the fact that hashing on both means the URL has to be recomputed when the same email appears with both sizes. Considering that (at least for gravatars) the computational intensive part comes from the MD5 of the email, this means a waste of cycles.

By letting the cache be per-avatar, each avatar kind can choose to hash on whatever it needs to. But I got an idea on how to improve on this.

Signature · 1 line
Giuseppe "Oblomov" Bilotta
#9 lore raw

PATCH v5 3/3 gitweb: gravatar url cache

Views which contain many occurrences of the same email address (e.g. shortlog view) benefit from not having to recalculate the MD5 of the email address every time.

---
 gitweb/gitweb.perl |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)
gitweb/gitweb.perl +22 -2
index 6e807fe..6771a9d 100755
@@ -3249,6 +3249,27 @@ sub git_print_header_div {
 	      "\n</div>\n";
 }
 
+# Rather than recomputing the url for an email multiple times, we cache it
+# after the first hit. This gives a visible benefit in views where the avatar
+# for the same email is used repeatedly (e.g. shortlog).
+# The cache is shared by all avatar engines (currently gravatar only), which
+# are free to use it as preferred. Since only one avatar engine is used for any
+# given page, there's no risk for cache conflicts.
+our %avatar_cache = ();
+
+# Compute the gravatar url for a given email, if it's not in the cache already.
+# Gravatar stores only the part of the URL before the size, since that's the
+# one computationally more expensive. This also allows reuse of the cache for
+# different sizes (for this particular engine).
+sub gravatar_url {
+	my $email = lc shift;
+	my $size = shift;
+	$avatar_cache{$email} ||=
+		"http://www.gravatar.com/avatar.php?gravatar_id=" .
+			Digest::MD5::md5_hex($email) . "&amp;size=";
+	return $avatar_cache{$email} . $size;
+}
+
 # Insert an avatar for the given $email at the given $size if the feature
 # is enabled.
 sub git_get_avatar {
@@ -3258,8 +3279,7 @@ sub git_get_avatar {
 	my $size = $avatar_size{$params{'size'}} || $avatar_size{'default'};
 	my $url = "";
 	if ($git_gravatar_enabled) {
-		$url = "http://www.gravatar.com/avatar.php?gravatar_id=" .
-			Digest::MD5::md5_hex(lc $email) . "&amp;size=$size";
+		$url = gravatar_url($email, $size);
 	}
 	# Currently only gravatars are supported, but other forms such as
 	# picons can be added by putting an else up here and defining $url
Signature · 1 line
1.6.3.rc1.192.gdbfcb
#10 lore raw

Re: [PATCHv5 1/3] gitweb: refactor author name insertion

In reply to Giuseppe Bilotta
Quoted 8 lines · Giuseppe Bilotta

The refactoring allows easier customization of the output by means of CSS and improves extensibility on the CGI side too.

Layout is preserved for all views except for 'commitdiff', which now uses the same format as 'commit'.

---

Could you please state here, in the comments area, what are the differences between v4 (previous) and v5 (current) version of patch, and if there are any? It helps with patch review...

Quoted 3 lines
 gitweb/gitweb.css  |    5 ++-
 gitweb/gitweb.perl |   79 +++++++++++++++++++++++++++++++---------------------
 2 files changed, 51 insertions(+), 33 deletions(-)
Signature · 2 lines
Jakub Narebski
Poland
#11 lore raw

Re: [PATCHv5 1/3] gitweb: refactor author name insertion

2009/6/25 Jakub Narebski <[email protected]>:

Quoted 4 lines

Could you please state here, in the comments area, what are the differences between v4 (previous) and v5 (current) version of patch, and if there are any?  It helps with patch review...

Sorry. This patch was untouched because I only received your comments on it this morning. I'm working on it right now so expect and new patch series soon.

Signature · 1 line
Giuseppe "Oblomov" Bilotta