threads / patch / 52150

v4pretty: add "%aL" etc. to show local-part of email addresses

Subject: [PATCH v4] pretty: add "%aL" etc. to show local-part of email addresses

## tl;dr

5 messages between Oct 29, 2019 and Nov 6, 2019. Diffs are folded; open one to read it.

replies: 4people: 2as markdown or json

Prarit Bhargava· Oct 29, 2019, 12:09 UTC · lore

In many projects the number of contributors is low enough that users know each other and the full email address doesn't need to be displayed. Displaying only the author's username saves a lot of columns on the screen.

Existing 'e/E' (as in "%ae" and "%aE") placeholders would show the author's address as "prarit@redhat.com", which would waste columns to show the same domain-part for all contributors when used in redhat-only project. Introduce 'l/L' placeholders that strip '@' and domain part from the e-mail address.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
---
Changes in v2:
- Changed option to 'L' based on https://www.ietf.org/rfc/rfc2822.txt
  definition of 'local-part' of email addresses.
- added additional information to documentation for %cL and %cl
- added mailmap output test
- modified code to use mailmap output for "L" option
- modified code to check if email address field has '@' symbol
- modified tests based on input from Peff
Changes in v3:
- Adjust Documentation descriptions for %aL and %cL
- remove exports of TEST* variables from test-lib.sh
- move testing to t6006 and t4203
Changes in v4:
- Change subject and patch description
- Update Documentation descriptions for %aL and %cL
 Documentation/pretty-formats.txt |  6 ++++++
 pretty.c                         |  9 ++++++++-
 t/t4203-mailmap.sh               | 28 ++++++++++++++++++++++++++++
 t/t6006-rev-list-format.sh       |  8 ++++++--
 t/test-lib.sh                    |  8 ++++++--
 5 files changed, 54 insertions(+), 5 deletions(-)
Show changes to 5 files +54 −5

Documentation/pretty-formats.txt, pretty.c, t/t4203-mailmap.sh, t/t6006-rev-list-format.sh, t/test-lib.sh

diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index b87e2e83e6d0..31c6e8d2b86a 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -163,6 +163,9 @@ The placeholders are:
 '%ae':: author email
 '%aE':: author email (respecting .mailmap, see linkgit:git-shortlog[1]
 	or linkgit:git-blame[1])
+'%al':: author email local-part (the part before the '@' sign)
+'%aL':: author local-part (see '%al') respecting .mailmap, see
+	linkgit:git-shortlog[1] or linkgit:git-blame[1])
 '%ad':: author date (format respects --date= option)
 '%aD':: author date, RFC2822 style
 '%ar':: author date, relative
@@ -175,6 +178,9 @@ The placeholders are:
 '%ce':: committer email
 '%cE':: committer email (respecting .mailmap, see
 	linkgit:git-shortlog[1] or linkgit:git-blame[1])
+'%cl':: author email local-part (the part before the '@' sign)
+'%cL':: author local-part (see '%cl') respecting .mailmap, see
+	linkgit:git-shortlog[1] or linkgit:git-blame[1])
 '%cd':: committer date (format respects --date= option)
 '%cD':: committer date, RFC2822 style
 '%cr':: committer date, relative
diff --git a/pretty.c b/pretty.c
index b32f0369531c..93eb6e837071 100644
--- a/pretty.c
+++ b/pretty.c
@@ -696,7 +696,7 @@ static size_t format_person_part(struct strbuf *sb, char part,
 	mail = s.mail_begin;
 	maillen = s.mail_end - s.mail_begin;
 
-	if (part == 'N' || part == 'E') /* mailmap lookup */
+	if (part == 'N' || part == 'E' || part == 'L') /* mailmap lookup */
 		mailmap_name(&mail, &maillen, &name, &namelen);
 	if (part == 'n' || part == 'N') {	/* name */
 		strbuf_add(sb, name, namelen);
@@ -706,6 +706,13 @@ static size_t format_person_part(struct strbuf *sb, char part,
 		strbuf_add(sb, mail, maillen);
 		return placeholder_len;
 	}
+	if (part == 'l' || part == 'L') {	/* local-part */
+		const char *at = memchr(mail, '@', maillen);
+		if (at)
+			maillen = at - mail;
+		strbuf_add(sb, mail, maillen);
+		return placeholder_len;
+	}
 
 	if (!s.date_begin)
 		goto skip;
diff --git a/t/t4203-mailmap.sh b/t/t4203-mailmap.sh
index e8f9c0f5bc8c..586c3a86b1d2 100755
--- a/t/t4203-mailmap.sh
+++ b/t/t4203-mailmap.sh
@@ -412,6 +412,34 @@ test_expect_success 'Log output (complex mapping)' '
 	test_cmp expect actual
 '
 
+cat >expect << EOF
+Author email cto@coompany.xx has local-part cto
+Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+Author email me@company.xx has local-part me
+Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+Author email me@company.xx has local-part me
+Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+Author email nick2@company.xx has local-part nick2
+Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+Author email bugs@company.xx has local-part bugs
+Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+Author email bugs@company.xx has local-part bugs
+Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+Author email author@example.com has local-part author
+Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+EOF
+
+test_expect_success 'Log output (local-part email address)' '
+	git log --pretty=format:"Author email %ae has local-part %al%nCommitter email %ce has local-part %cl%n" >actual &&
+	test_cmp expect actual
+'
+
 cat >expect << EOF
 Author: CTO <cto@company.xx>
 Author: Santa Claus <santa.claus@northpole.xx>
diff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh
index 1f7d3f7acc9c..ebdc49c4965e 100755
--- a/t/t6006-rev-list-format.sh
+++ b/t/t6006-rev-list-format.sh
@@ -109,31 +109,35 @@ commit $head1
 EOF
 
 # we don't test relative here
-test_format author %an%n%ae%n%ad%n%aD%n%at <<EOF
+test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
 commit $head2
 $GIT_AUTHOR_NAME
 $GIT_AUTHOR_EMAIL
+$TEST_AUTHOR_LOCALNAME
 Thu Apr 7 15:13:13 2005 -0700
 Thu, 7 Apr 2005 15:13:13 -0700
 1112911993
 commit $head1
 $GIT_AUTHOR_NAME
 $GIT_AUTHOR_EMAIL
+$TEST_AUTHOR_LOCALNAME
 Thu Apr 7 15:13:13 2005 -0700
 Thu, 7 Apr 2005 15:13:13 -0700
 1112911993
 EOF
 
-test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<EOF
+test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
 commit $head2
 $GIT_COMMITTER_NAME
 $GIT_COMMITTER_EMAIL
+$TEST_COMMITTER_LOCALNAME
 Thu Apr 7 15:13:13 2005 -0700
 Thu, 7 Apr 2005 15:13:13 -0700
 1112911993
 commit $head1
 $GIT_COMMITTER_NAME
 $GIT_COMMITTER_EMAIL
+$TEST_COMMITTER_LOCALNAME
 Thu Apr 7 15:13:13 2005 -0700
 Thu, 7 Apr 2005 15:13:13 -0700
 1112911993
diff --git a/t/test-lib.sh b/t/test-lib.sh
index e06fa02a0eec..46c44408432e 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -404,9 +404,13 @@ unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
 unset XDG_CACHE_HOME
 unset XDG_CONFIG_HOME
 unset GITPERLLIB
-GIT_AUTHOR_EMAIL=author@example.com
+TEST_AUTHOR_LOCALNAME=author
+TEST_AUTHOR_DOMAIN=example.com
+GIT_AUTHOR_EMAIL=${TEST_AUTHOR_LOCALNAME}@${TEST_AUTHOR_DOMAIN}
 GIT_AUTHOR_NAME='A U Thor'
-GIT_COMMITTER_EMAIL=committer@example.com
+TEST_COMMITTER_LOCALNAME=committer
+TEST_COMMITTER_DOMAIN=example.com
+GIT_COMMITTER_EMAIL=${TEST_COMMITTER_LOCALNAME}@${TEST_COMMITTER_DOMAIN}
 GIT_COMMITTER_NAME='C O Mitter'
 GIT_MERGE_VERBOSITY=5
 GIT_MERGE_AUTOEDIT=no
-- 
2.21.0
Prarit Bhargava· Nov 3, 2019, 13:28 UTC · re: Prarit Bhargava · lore

Re: [PATCH v4] pretty: add "%aL" etc. to show local-part of email addresses

Just re-pinging to make sure this didn't get lost.
Thanks,
P.
On 10/29/19 8:09 AM, Prarit Bhargava wrote:
Show 190 quoted lines
> In many projects the number of contributors is low enough that users know
> each other and the full email address doesn't need to be displayed.
> Displaying only the author's username saves a lot of columns on the screen.
> 
> Existing 'e/E' (as in "%ae" and "%aE") placeholders would show the
> author's address as "prarit@redhat.com", which would waste columns to show
> the same domain-part for all contributors when used in redhat-only
> project.  Introduce 'l/L' placeholders that strip '@' and domain part from
> the e-mail address.
> 
> Signed-off-by: Prarit Bhargava <prarit@redhat.com>
> ---
> Changes in v2:
> - Changed option to 'L' based on https://www.ietf.org/rfc/rfc2822.txt
>   definition of 'local-part' of email addresses.
> - added additional information to documentation for %cL and %cl
> - added mailmap output test
> - modified code to use mailmap output for "L" option
> - modified code to check if email address field has '@' symbol
> - modified tests based on input from Peff
> 
> Changes in v3:
> - Adjust Documentation descriptions for %aL and %cL
> - remove exports of TEST* variables from test-lib.sh
> - move testing to t6006 and t4203
> 
> Changes in v4:
> - Change subject and patch description
> - Update Documentation descriptions for %aL and %cL
> 
>  Documentation/pretty-formats.txt |  6 ++++++
>  pretty.c                         |  9 ++++++++-
>  t/t4203-mailmap.sh               | 28 ++++++++++++++++++++++++++++
>  t/t6006-rev-list-format.sh       |  8 ++++++--
>  t/test-lib.sh                    |  8 ++++++--
>  5 files changed, 54 insertions(+), 5 deletions(-)
> 
> diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
> index b87e2e83e6d0..31c6e8d2b86a 100644
> --- a/Documentation/pretty-formats.txt
> +++ b/Documentation/pretty-formats.txt
> @@ -163,6 +163,9 @@ The placeholders are:
>  '%ae':: author email
>  '%aE':: author email (respecting .mailmap, see linkgit:git-shortlog[1]
>  	or linkgit:git-blame[1])
> +'%al':: author email local-part (the part before the '@' sign)
> +'%aL':: author local-part (see '%al') respecting .mailmap, see
> +	linkgit:git-shortlog[1] or linkgit:git-blame[1])
>  '%ad':: author date (format respects --date= option)
>  '%aD':: author date, RFC2822 style
>  '%ar':: author date, relative
> @@ -175,6 +178,9 @@ The placeholders are:
>  '%ce':: committer email
>  '%cE':: committer email (respecting .mailmap, see
>  	linkgit:git-shortlog[1] or linkgit:git-blame[1])
> +'%cl':: author email local-part (the part before the '@' sign)
> +'%cL':: author local-part (see '%cl') respecting .mailmap, see
> +	linkgit:git-shortlog[1] or linkgit:git-blame[1])
>  '%cd':: committer date (format respects --date= option)
>  '%cD':: committer date, RFC2822 style
>  '%cr':: committer date, relative
> diff --git a/pretty.c b/pretty.c
> index b32f0369531c..93eb6e837071 100644
> --- a/pretty.c
> +++ b/pretty.c
> @@ -696,7 +696,7 @@ static size_t format_person_part(struct strbuf *sb, char part,
>  	mail = s.mail_begin;
>  	maillen = s.mail_end - s.mail_begin;
>  
> -	if (part == 'N' || part == 'E') /* mailmap lookup */
> +	if (part == 'N' || part == 'E' || part == 'L') /* mailmap lookup */
>  		mailmap_name(&mail, &maillen, &name, &namelen);
>  	if (part == 'n' || part == 'N') {	/* name */
>  		strbuf_add(sb, name, namelen);
> @@ -706,6 +706,13 @@ static size_t format_person_part(struct strbuf *sb, char part,
>  		strbuf_add(sb, mail, maillen);
>  		return placeholder_len;
>  	}
> +	if (part == 'l' || part == 'L') {	/* local-part */
> +		const char *at = memchr(mail, '@', maillen);
> +		if (at)
> +			maillen = at - mail;
> +		strbuf_add(sb, mail, maillen);
> +		return placeholder_len;
> +	}
>  
>  	if (!s.date_begin)
>  		goto skip;
> diff --git a/t/t4203-mailmap.sh b/t/t4203-mailmap.sh
> index e8f9c0f5bc8c..586c3a86b1d2 100755
> --- a/t/t4203-mailmap.sh
> +++ b/t/t4203-mailmap.sh
> @@ -412,6 +412,34 @@ test_expect_success 'Log output (complex mapping)' '
>  	test_cmp expect actual
>  '
>  
> +cat >expect << EOF
> +Author email cto@coompany.xx has local-part cto
> +Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
> +
> +Author email me@company.xx has local-part me
> +Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
> +
> +Author email me@company.xx has local-part me
> +Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
> +
> +Author email nick2@company.xx has local-part nick2
> +Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
> +
> +Author email bugs@company.xx has local-part bugs
> +Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
> +
> +Author email bugs@company.xx has local-part bugs
> +Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
> +
> +Author email author@example.com has local-part author
> +Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
> +EOF
> +
> +test_expect_success 'Log output (local-part email address)' '
> +	git log --pretty=format:"Author email %ae has local-part %al%nCommitter email %ce has local-part %cl%n" >actual &&
> +	test_cmp expect actual
> +'
> +
>  cat >expect << EOF
>  Author: CTO <cto@company.xx>
>  Author: Santa Claus <santa.claus@northpole.xx>
> diff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh
> index 1f7d3f7acc9c..ebdc49c4965e 100755
> --- a/t/t6006-rev-list-format.sh
> +++ b/t/t6006-rev-list-format.sh
> @@ -109,31 +109,35 @@ commit $head1
>  EOF
>  
>  # we don't test relative here
> -test_format author %an%n%ae%n%ad%n%aD%n%at <<EOF
> +test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
>  commit $head2
>  $GIT_AUTHOR_NAME
>  $GIT_AUTHOR_EMAIL
> +$TEST_AUTHOR_LOCALNAME
>  Thu Apr 7 15:13:13 2005 -0700
>  Thu, 7 Apr 2005 15:13:13 -0700
>  1112911993
>  commit $head1
>  $GIT_AUTHOR_NAME
>  $GIT_AUTHOR_EMAIL
> +$TEST_AUTHOR_LOCALNAME
>  Thu Apr 7 15:13:13 2005 -0700
>  Thu, 7 Apr 2005 15:13:13 -0700
>  1112911993
>  EOF
>  
> -test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<EOF
> +test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
>  commit $head2
>  $GIT_COMMITTER_NAME
>  $GIT_COMMITTER_EMAIL
> +$TEST_COMMITTER_LOCALNAME
>  Thu Apr 7 15:13:13 2005 -0700
>  Thu, 7 Apr 2005 15:13:13 -0700
>  1112911993
>  commit $head1
>  $GIT_COMMITTER_NAME
>  $GIT_COMMITTER_EMAIL
> +$TEST_COMMITTER_LOCALNAME
>  Thu Apr 7 15:13:13 2005 -0700
>  Thu, 7 Apr 2005 15:13:13 -0700
>  1112911993
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index e06fa02a0eec..46c44408432e 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -404,9 +404,13 @@ unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
>  unset XDG_CACHE_HOME
>  unset XDG_CONFIG_HOME
>  unset GITPERLLIB
> -GIT_AUTHOR_EMAIL=author@example.com
> +TEST_AUTHOR_LOCALNAME=author
> +TEST_AUTHOR_DOMAIN=example.com
> +GIT_AUTHOR_EMAIL=${TEST_AUTHOR_LOCALNAME}@${TEST_AUTHOR_DOMAIN}
>  GIT_AUTHOR_NAME='A U Thor'
> -GIT_COMMITTER_EMAIL=committer@example.com
> +TEST_COMMITTER_LOCALNAME=committer
> +TEST_COMMITTER_DOMAIN=example.com
> +GIT_COMMITTER_EMAIL=${TEST_COMMITTER_LOCALNAME}@${TEST_COMMITTER_DOMAIN}
>  GIT_COMMITTER_NAME='C O Mitter'
>  GIT_MERGE_VERBOSITY=5
>  GIT_MERGE_AUTOEDIT=no
> 
Junio C Hamano· Nov 4, 2019, 04:15 UTC · re: Prarit Bhargava · lore

Re: [PATCH v4] pretty: add "%aL" etc. to show local-part of email addresses

Prarit Bhargava <prarit@redhat.com> writes:
> Just re-pinging to make sure this didn't get lost.
I think it has been on 'next' for several days aleady.
Prarit Bhargava· Nov 4, 2019, 12:35 UTC · re: Junio C Hamano · lore

Re: [PATCH v4] pretty: add "%aL" etc. to show local-part of email addresses

On 11/3/19 11:15 PM, Junio C Hamano wrote:
Show 6 quoted lines
> Prarit Bhargava <prarit@redhat.com> writes:
> 
>> Just re-pinging to make sure this didn't get lost.
> 
> I think it has been on 'next' for several days aleady.
> 
My apologies Junio.  I didn't realize that.
Thanks :)
P.
Junio C Hamano· Nov 6, 2019, 02:01 UTC · re: Prarit Bhargava · lore

Re: [PATCH v4] pretty: add "%aL" etc. to show local-part of email addresses

Prarit Bhargava <prarit@redhat.com> writes:
Show 11 quoted lines
> On 11/3/19 11:15 PM, Junio C Hamano wrote:
>> Prarit Bhargava <prarit@redhat.com> writes:
>> 
>>> Just re-pinging to make sure this didn't get lost.
>> 
>> I think it has been on 'next' for several days aleady.
>> 
>
> My apologies Junio.  I didn't realize that.
>
> Thanks :)

No apologies needed; we are all human and not perfect, and I do appreciate the pinging as I often end up saying "I didn't realize I haven't picked it up".

Thanks.

← back to recent threads