From: Hariom Verma <hariom18599@xxxxxxxxx> Currently, ref-filter only supports printing email with arrow brackets. Let's add support for two more email options. - trim : print email without arrow brackets. - localpart : prints the part before the @ sign Mentored-by: Christian Couder <chriscool@xxxxxxxxxxxxx> Mentored-by: Heba Waly <heba.waly@xxxxxxxxx> Signed-off-by: Hariom Verma <hariom18599@xxxxxxxxx> --- ref-filter.c | 36 ++++++++++++++++++++++++++++++++---- t/t6300-for-each-ref.sh | 16 ++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 8447cb09be..8563088eb1 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -102,6 +102,10 @@ static struct ref_to_worktree_map { struct worktree **worktrees; } ref_to_worktree_map; +static struct email_option{ + enum { EO_INVALID, EO_RAW, EO_TRIM, EO_LOCALPART } option; +} email_option; + /* * An atom is a valid field atom listed below, possibly prefixed with * a "*" to denote deref_tag(). @@ -1040,10 +1044,26 @@ static const char *copy_email(const char *buf) const char *eoemail; if (!email) return xstrdup(""); - eoemail = strchr(email, '>'); + switch (email_option.option) { + case EO_RAW: + eoemail = strchr(email, '>') + 1; + break; + case EO_TRIM: + email++; + eoemail = strchr(email, '>'); + break; + case EO_LOCALPART: + email++; + eoemail = strchr(email, '@'); + break; + case EO_INVALID: + default: + return xstrdup(""); + } + if (!eoemail) return xstrdup(""); - return xmemdupz(email, eoemail + 1 - email); + return xmemdupz(email, eoemail - email); } static char *copy_subject(const char *buf, unsigned long len) @@ -1113,7 +1133,7 @@ static void grab_person(const char *who, struct atom_value *val, int deref, void continue; if (name[wholen] != 0 && strcmp(name + wholen, "name") && - strcmp(name + wholen, "email") && + !starts_with(name + wholen, "email") && !starts_with(name + wholen, "date")) continue; if (!wholine) @@ -1124,8 +1144,16 @@ static void grab_person(const char *who, struct atom_value *val, int deref, void v->s = copy_line(wholine); else if (!strcmp(name + wholen, "name")) v->s = copy_name(wholine); - else if (!strcmp(name + wholen, "email")) + else if (starts_with(name + wholen, "email")) { + email_option.option = EO_INVALID; + if (!strcmp(name + wholen, "email")) + email_option.option = EO_RAW; + if (!strcmp(name + wholen, "email:trim")) + email_option.option = EO_TRIM; + if (!strcmp(name + wholen, "email:localpart")) + email_option.option = EO_LOCALPART; v->s = copy_email(wholine); + } else if (starts_with(name + wholen, "date")) grab_date(wholine, v, name); } diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh index da59fadc5d..b8a2cb8439 100755 --- a/t/t6300-for-each-ref.sh +++ b/t/t6300-for-each-ref.sh @@ -106,15 +106,21 @@ test_atom head '*objecttype' '' test_atom head author 'A U Thor <author@xxxxxxxxxxx> 1151968724 +0200' test_atom head authorname 'A U Thor' test_atom head authoremail '<author@xxxxxxxxxxx>' +test_atom head authoremail:trim 'author@xxxxxxxxxxx' +test_atom head authoremail:localpart 'author' test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200' test_atom head committer 'C O Mitter <committer@xxxxxxxxxxx> 1151968723 +0200' test_atom head committername 'C O Mitter' test_atom head committeremail '<committer@xxxxxxxxxxx>' +test_atom head committeremail:trim 'committer@xxxxxxxxxxx' +test_atom head committeremail:localpart 'committer' test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200' test_atom head tag '' test_atom head tagger '' test_atom head taggername '' test_atom head taggeremail '' +test_atom head taggeremail:trim '' +test_atom head taggeremail:localpart '' test_atom head taggerdate '' test_atom head creator 'C O Mitter <committer@xxxxxxxxxxx> 1151968723 +0200' test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200' @@ -151,15 +157,21 @@ test_atom tag '*objecttype' 'commit' test_atom tag author '' test_atom tag authorname '' test_atom tag authoremail '' +test_atom tag authoremail:trim '' +test_atom tag authoremail:localpart '' test_atom tag authordate '' test_atom tag committer '' test_atom tag committername '' test_atom tag committeremail '' +test_atom tag committeremail:trim '' +test_atom tag committeremail:localpart '' test_atom tag committerdate '' test_atom tag tag 'testtag' test_atom tag tagger 'C O Mitter <committer@xxxxxxxxxxx> 1151968725 +0200' test_atom tag taggername 'C O Mitter' test_atom tag taggeremail '<committer@xxxxxxxxxxx>' +test_atom tag taggeremail:trim 'committer@xxxxxxxxxxx' +test_atom tag taggeremail:localpart 'committer' test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200' test_atom tag creator 'C O Mitter <committer@xxxxxxxxxxx> 1151968725 +0200' test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200' @@ -545,10 +557,14 @@ test_atom refs/tags/taggerless tag 'taggerless' test_atom refs/tags/taggerless tagger '' test_atom refs/tags/taggerless taggername '' test_atom refs/tags/taggerless taggeremail '' +test_atom refs/tags/taggerless taggeremail:trim '' +test_atom refs/tags/taggerless taggeremail:localpart '' test_atom refs/tags/taggerless taggerdate '' test_atom refs/tags/taggerless committer '' test_atom refs/tags/taggerless committername '' test_atom refs/tags/taggerless committeremail '' +test_atom refs/tags/taggerless committeremail:trim '' +test_atom refs/tags/taggerless committeremail:localpart '' test_atom refs/tags/taggerless committerdate '' test_atom refs/tags/taggerless subject 'Broken tag' -- gitgitgadget