git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Jakub Narębski" <jnareb@gmail.com>
Cc: git@vger.kernel.org, Emma Brooks <me@pluvano.com>
Subject: Re: [RFC PATCH] gitweb: Map names/emails with mailmap
Date: Thu, 30 Jul 2020 09:20:40 -0700	[thread overview]
Message-ID: <xmqqime56kkn.fsf@gitster.c.googlers.com> (raw)
In-Reply-To: <20200730041217.6893-1-me@pluvano.com> (Emma Brooks's message of "Thu, 30 Jul 2020 04:12:17 +0000")

[jc: Cc'ing Jakub, hoping he's still our resident gitweb expert, as
an "RFC" requests help from experts]

Emma Brooks <me@pluvano.com> writes:

> Add an option to map names and emails to their canonical forms via a
> .mailmap file. This is enabled by default, consistent with the behavior
> of Git itself.
>
> Signed-off-by: Emma Brooks <me@pluvano.com>
> ---
>
> This works, but needs some polish. The read_mailmap code is not
> particularly clever.
>
>  Documentation/gitweb.conf.txt |  5 +++
>  gitweb/gitweb.perl            | 79 +++++++++++++++++++++++++++++++++--
>  2 files changed, 80 insertions(+), 4 deletions(-)
>
> diff --git a/Documentation/gitweb.conf.txt b/Documentation/gitweb.conf.txt
> index 7963a79ba9..2d7551a6a5 100644
> --- a/Documentation/gitweb.conf.txt
> +++ b/Documentation/gitweb.conf.txt
> @@ -751,6 +751,11 @@ default font sizes or lineheights are changed (e.g. via adding extra
>  CSS stylesheet in `@stylesheets`), it may be appropriate to change
>  these values.
>  
> +mailmap::
> +	Use mailmap to find the canonical name/email for
> +	committers/authors (see linkgit:git-shortlog[1]). Enabled by
> +	default.
> +
>  highlight::
>  	Server-side syntax highlight support in "blob" view.  It requires
>  	`$highlight_bin` program to be available (see the description of
> diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
> index 0959a782ec..00256704a7 100755
> --- a/gitweb/gitweb.perl
> +++ b/gitweb/gitweb.perl
> @@ -505,6 +505,12 @@ sub evaluate_uri {
>  		'override' => 0,
>  		'default' => ['']},
>  
> +	# Enable reading mailmap to determine canonical author
> +	# information. Enabled by default.
> +	'mailmap' => {
> +		'override' => 0,
> +		'default' => [1]},
> +
>  	# Enable displaying how much time and how many git commands
>  	# it took to generate and display page.  Disabled by default.
>  	# Project specific override is not supported.
> @@ -3490,6 +3496,61 @@ sub parse_tag {
>  	return %tag
>  }
>  
> +# Contents of mailmap stored as a referance to a hash with keys in the format
> +# of "name <email>" or "<email>", and values that are hashes containing a
> +# replacement "name" and/or "email". If set (even if empty) the mailmap has
> +# already been read.
> +my $mailmap;
> +
> +sub read_mailmap {
> +	my %mailmap = ();
> +	open my $fd, '-|', git_cmd(), 'cat-file', 'blob', 'HEAD:.mailmap'
> +		or die_error(500, 'Failed to read mailmap');
> +	foreach (split '\n', <$fd>) {
> +		next if (/^#/);
> +		if (/(.*)\s+ <([^<>]+)>\s+ ((?:.*\s+)? <[^<>]+>) (?:\s+\#)/x ||
> +		    /(.*)\s+ <([^<>]+)>\s+ ((?:.*\s+)? <[^<>]+>)/x) {
> +			# New Name <new@email> <old@email>
> +			# New Name <new@email> Old Name <old@email>
> +			$mailmap{$3} = ();
> +			$mailmap{$3}{name} = $1;
> +			$mailmap{$3}{email} = $2;
> +		} elsif (/(?: <([^<>]+)>\s+ | (.+)\s+ ) (<[^<>]+>) (?:\s+\#)/x ||
> +		         /(?: <([^<>]+)>\s+ | (.+)\s+ ) (<[^<>]+>)/x) {
> +			# New Name <old@email>
> +			# <new@email> <old@email>
> +			$mailmap{$3} = ();
> +			if ($1) {
> +				$mailmap{$3}{email} = $1;
> +			} else {
> +				$mailmap{$3}{name} = $2;
> +			}
> +		}
> +	}
> +	return \%mailmap;
> +}
> +
> +# Map author name and email based on mailmap. A more specific match
> +# ("name <email>") is preferred to a less specific one ("<email>").
> +sub map_author {
> +	my $name = shift;
> +	my $email = shift;
> +
> +	if (!$mailmap) {
> +		$mailmap = read_mailmap;
> +	}
> +
> +	if ($mailmap->{"$name <$email>"}) {
> +		$name = $mailmap->{"$name <$email>"}{name} || $name;
> +		$email = $mailmap->{"$name <$email>"}{email} || $email;
> +	} elsif ($mailmap->{"<$email>"}) {
> +		$name = $mailmap->{"<$email>"}{name} || $name;
> +		$email = $mailmap->{"<$email>"}{email} || $email;
> +	}
> +
> +	return ($name, $email);
> +}
> +
>  sub parse_commit_text {
>  	my ($commit_text, $withparents) = @_;
>  	my @commit_lines = split '\n', $commit_text;
> @@ -3517,8 +3578,13 @@ sub parse_commit_text {
>  			$co{'author_epoch'} = $2;
>  			$co{'author_tz'} = $3;
>  			if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
> -				$co{'author_name'}  = $1;
> -				$co{'author_email'} = $2;
> +				my ($name, $email) = @_;
> +				if (gitweb_check_feature('mailmap')) {
> +					($name, $email) = map_author($1, $2);
> +					$co{'author'} = "$name <$email>";
> +				}
> +				$co{'author_name'}  = $name;
> +				$co{'author_email'} = $email;
>  			} else {
>  				$co{'author_name'} = $co{'author'};
>  			}
> @@ -3527,8 +3593,13 @@ sub parse_commit_text {
>  			$co{'committer_epoch'} = $2;
>  			$co{'committer_tz'} = $3;
>  			if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
> -				$co{'committer_name'}  = $1;
> -				$co{'committer_email'} = $2;
> +				my ($name, $email) = @_;
> +				if (gitweb_check_feature('mailmap')) {
> +					($name, $email) = map_author($1, $2);
> +					$co{'committer'} = "$name <$email>";
> +				}
> +				$co{'committer_name'}  = $name;
> +				$co{'committer_email'} = $email;
>  			} else {
>  				$co{'committer_name'} = $co{'committer'};
>  			}

  reply	other threads:[~2020-07-30 16:20 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-30  4:12 [RFC PATCH] gitweb: Map names/emails with mailmap Emma Brooks
2020-07-30 16:20 ` Junio C Hamano [this message]
2020-07-31  1:01 ` Jeff King
2020-07-31  2:10   ` Junio C Hamano
2020-08-08 21:34 ` [PATCH] " Emma Brooks
2020-08-09 23:04   ` [PATCH v2] gitweb: map " Emma Brooks
2020-08-10  0:49     ` Eric Sunshine
2020-08-10  3:12       ` Emma Brooks
2020-08-10  5:41         ` Eric Sunshine
2020-08-10 10:02     ` Jeff King
2020-08-11  4:17       ` Emma Brooks
2020-08-11  4:48         ` Eric Sunshine
2020-08-11  4:55         ` Jeff King
2020-09-05  2:55           ` Emma Brooks
2020-09-05  3:26             ` Junio C Hamano
2020-09-07 22:10               ` Emma Brooks
2020-08-11  6:17         ` Eric Wong
2020-08-11  6:33           ` Joe Perches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqime56kkn.fsf@gitster.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=jnareb@gmail.com \
    --cc=me@pluvano.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).