Skip to content

Commit b82445d

Browse files
harry-hovgitster
authored andcommitted
ref-filter: support different email formats
Currently, ref-filter only supports printing email with angle brackets. Let's add support for two more email options. - trim : for email without angle brackets. - localpart : for the part before the @ sign out of trimmed email Mentored-by: Christian Couder <[email protected]> Mentored-by: Heba Waly <[email protected]> Signed-off-by: Hariom Verma <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 878e727 commit b82445d

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ These are intended for working on a mix of annotated and lightweight tags.
230230

231231
Fields that have name-email-date tuple as its value (`author`,
232232
`committer`, and `tagger`) can be suffixed with `name`, `email`,
233-
and `date` to extract the named component.
233+
and `date` to extract the named component. For email fields (`authoremail`,
234+
`committeremail` and `taggeremail`), `:trim` can be appended to get the email
235+
without angle brackets, and `:localpart` to get the part before the `@` symbol
236+
out of the trimmed email.
234237

235238
The message in a commit or a tag object is `contents`, from which
236239
`contents:<part>` can be used to extract various parts out of:

ref-filter.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ static struct used_atom {
140140
enum { O_FULL, O_LENGTH, O_SHORT } option;
141141
unsigned int length;
142142
} objectname;
143+
struct email_option {
144+
enum { EO_RAW, EO_TRIM, EO_LOCALPART } option;
145+
} email_option;
143146
struct refname_atom refname;
144147
char *head;
145148
} u;
@@ -377,6 +380,20 @@ static int objectname_atom_parser(const struct ref_format *format, struct used_a
377380
return 0;
378381
}
379382

383+
static int person_email_atom_parser(const struct ref_format *format, struct used_atom *atom,
384+
const char *arg, struct strbuf *err)
385+
{
386+
if (!arg)
387+
atom->u.email_option.option = EO_RAW;
388+
else if (!strcmp(arg, "trim"))
389+
atom->u.email_option.option = EO_TRIM;
390+
else if (!strcmp(arg, "localpart"))
391+
atom->u.email_option.option = EO_LOCALPART;
392+
else
393+
return strbuf_addf_ret(err, -1, _("unrecognized email option: %s"), arg);
394+
return 0;
395+
}
396+
380397
static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom,
381398
const char *arg, struct strbuf *err)
382399
{
@@ -488,15 +505,15 @@ static struct {
488505
{ "tag", SOURCE_OBJ },
489506
{ "author", SOURCE_OBJ },
490507
{ "authorname", SOURCE_OBJ },
491-
{ "authoremail", SOURCE_OBJ },
508+
{ "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
492509
{ "authordate", SOURCE_OBJ, FIELD_TIME },
493510
{ "committer", SOURCE_OBJ },
494511
{ "committername", SOURCE_OBJ },
495-
{ "committeremail", SOURCE_OBJ },
512+
{ "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
496513
{ "committerdate", SOURCE_OBJ, FIELD_TIME },
497514
{ "tagger", SOURCE_OBJ },
498515
{ "taggername", SOURCE_OBJ },
499-
{ "taggeremail", SOURCE_OBJ },
516+
{ "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
500517
{ "taggerdate", SOURCE_OBJ, FIELD_TIME },
501518
{ "creator", SOURCE_OBJ },
502519
{ "creatordate", SOURCE_OBJ, FIELD_TIME },
@@ -1037,16 +1054,35 @@ static const char *copy_name(const char *buf)
10371054
return xstrdup("");
10381055
}
10391056

1040-
static const char *copy_email(const char *buf)
1057+
static const char *copy_email(const char *buf, struct used_atom *atom)
10411058
{
10421059
const char *email = strchr(buf, '<');
10431060
const char *eoemail;
10441061
if (!email)
10451062
return xstrdup("");
1046-
eoemail = strchr(email, '>');
1063+
switch (atom->u.email_option.option) {
1064+
case EO_RAW:
1065+
eoemail = strchr(email, '>');
1066+
if (eoemail)
1067+
eoemail++;
1068+
break;
1069+
case EO_TRIM:
1070+
email++;
1071+
eoemail = strchr(email, '>');
1072+
break;
1073+
case EO_LOCALPART:
1074+
email++;
1075+
eoemail = strchr(email, '@');
1076+
if (!eoemail)
1077+
eoemail = strchr(email, '>');
1078+
break;
1079+
default:
1080+
BUG("unknown email option");
1081+
}
1082+
10471083
if (!eoemail)
10481084
return xstrdup("");
1049-
return xmemdupz(email, eoemail + 1 - email);
1085+
return xmemdupz(email, eoemail - email);
10501086
}
10511087

10521088
static char *copy_subject(const char *buf, unsigned long len)
@@ -1116,7 +1152,7 @@ static void grab_person(const char *who, struct atom_value *val, int deref, void
11161152
continue;
11171153
if (name[wholen] != 0 &&
11181154
strcmp(name + wholen, "name") &&
1119-
strcmp(name + wholen, "email") &&
1155+
!starts_with(name + wholen, "email") &&
11201156
!starts_with(name + wholen, "date"))
11211157
continue;
11221158
if (!wholine)
@@ -1127,8 +1163,8 @@ static void grab_person(const char *who, struct atom_value *val, int deref, void
11271163
v->s = copy_line(wholine);
11281164
else if (!strcmp(name + wholen, "name"))
11291165
v->s = copy_name(wholine);
1130-
else if (!strcmp(name + wholen, "email"))
1131-
v->s = copy_email(wholine);
1166+
else if (starts_with(name + wholen, "email"))
1167+
v->s = copy_email(wholine, &used_atom[i]);
11321168
else if (starts_with(name + wholen, "date"))
11331169
grab_date(wholine, v, name);
11341170
}

t/t6300-for-each-ref.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,21 @@ test_atom head '*objecttype' ''
125125
test_atom head author 'A U Thor <[email protected]> 1151968724 +0200'
126126
test_atom head authorname 'A U Thor'
127127
test_atom head authoremail '<[email protected]>'
128+
test_atom head authoremail:trim '[email protected]'
129+
test_atom head authoremail:localpart 'author'
128130
test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
129131
test_atom head committer 'C O Mitter <[email protected]> 1151968723 +0200'
130132
test_atom head committername 'C O Mitter'
131133
test_atom head committeremail '<[email protected]>'
134+
test_atom head committeremail:trim '[email protected]'
135+
test_atom head committeremail:localpart 'committer'
132136
test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
133137
test_atom head tag ''
134138
test_atom head tagger ''
135139
test_atom head taggername ''
136140
test_atom head taggeremail ''
141+
test_atom head taggeremail:trim ''
142+
test_atom head taggeremail:localpart ''
137143
test_atom head taggerdate ''
138144
test_atom head creator 'C O Mitter <[email protected]> 1151968723 +0200'
139145
test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
@@ -170,15 +176,21 @@ test_atom tag '*objecttype' 'commit'
170176
test_atom tag author ''
171177
test_atom tag authorname ''
172178
test_atom tag authoremail ''
179+
test_atom tag authoremail:trim ''
180+
test_atom tag authoremail:localpart ''
173181
test_atom tag authordate ''
174182
test_atom tag committer ''
175183
test_atom tag committername ''
176184
test_atom tag committeremail ''
185+
test_atom tag committeremail:trim ''
186+
test_atom tag committeremail:localpart ''
177187
test_atom tag committerdate ''
178188
test_atom tag tag 'testtag'
179189
test_atom tag tagger 'C O Mitter <[email protected]> 1151968725 +0200'
180190
test_atom tag taggername 'C O Mitter'
181191
test_atom tag taggeremail '<[email protected]>'
192+
test_atom tag taggeremail:trim '[email protected]'
193+
test_atom tag taggeremail:localpart 'committer'
182194
test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
183195
test_atom tag creator 'C O Mitter <[email protected]> 1151968725 +0200'
184196
test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
@@ -564,10 +576,14 @@ test_atom refs/tags/taggerless tag 'taggerless'
564576
test_atom refs/tags/taggerless tagger ''
565577
test_atom refs/tags/taggerless taggername ''
566578
test_atom refs/tags/taggerless taggeremail ''
579+
test_atom refs/tags/taggerless taggeremail:trim ''
580+
test_atom refs/tags/taggerless taggeremail:localpart ''
567581
test_atom refs/tags/taggerless taggerdate ''
568582
test_atom refs/tags/taggerless committer ''
569583
test_atom refs/tags/taggerless committername ''
570584
test_atom refs/tags/taggerless committeremail ''
585+
test_atom refs/tags/taggerless committeremail:trim ''
586+
test_atom refs/tags/taggerless committeremail:localpart ''
571587
test_atom refs/tags/taggerless committerdate ''
572588
test_atom refs/tags/taggerless subject 'Broken tag'
573589

0 commit comments

Comments
 (0)