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 <chriscool@tuxfamily.org> Mentored-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Hariom Verma <hariom18599@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
878e727637
commit
b82445dc27
54
ref-filter.c
54
ref-filter.c
@ -140,6 +140,9 @@ static struct used_atom {
|
||||
enum { O_FULL, O_LENGTH, O_SHORT } option;
|
||||
unsigned int length;
|
||||
} objectname;
|
||||
struct email_option {
|
||||
enum { EO_RAW, EO_TRIM, EO_LOCALPART } option;
|
||||
} email_option;
|
||||
struct refname_atom refname;
|
||||
char *head;
|
||||
} u;
|
||||
@ -377,6 +380,20 @@ static int objectname_atom_parser(const struct ref_format *format, struct used_a
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int person_email_atom_parser(const struct ref_format *format, struct used_atom *atom,
|
||||
const char *arg, struct strbuf *err)
|
||||
{
|
||||
if (!arg)
|
||||
atom->u.email_option.option = EO_RAW;
|
||||
else if (!strcmp(arg, "trim"))
|
||||
atom->u.email_option.option = EO_TRIM;
|
||||
else if (!strcmp(arg, "localpart"))
|
||||
atom->u.email_option.option = EO_LOCALPART;
|
||||
else
|
||||
return strbuf_addf_ret(err, -1, _("unrecognized email option: %s"), arg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom,
|
||||
const char *arg, struct strbuf *err)
|
||||
{
|
||||
@ -488,15 +505,15 @@ static struct {
|
||||
{ "tag", SOURCE_OBJ },
|
||||
{ "author", SOURCE_OBJ },
|
||||
{ "authorname", SOURCE_OBJ },
|
||||
{ "authoremail", SOURCE_OBJ },
|
||||
{ "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
|
||||
{ "authordate", SOURCE_OBJ, FIELD_TIME },
|
||||
{ "committer", SOURCE_OBJ },
|
||||
{ "committername", SOURCE_OBJ },
|
||||
{ "committeremail", SOURCE_OBJ },
|
||||
{ "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
|
||||
{ "committerdate", SOURCE_OBJ, FIELD_TIME },
|
||||
{ "tagger", SOURCE_OBJ },
|
||||
{ "taggername", SOURCE_OBJ },
|
||||
{ "taggeremail", SOURCE_OBJ },
|
||||
{ "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
|
||||
{ "taggerdate", SOURCE_OBJ, FIELD_TIME },
|
||||
{ "creator", SOURCE_OBJ },
|
||||
{ "creatordate", SOURCE_OBJ, FIELD_TIME },
|
||||
@ -1037,16 +1054,35 @@ static const char *copy_name(const char *buf)
|
||||
return xstrdup("");
|
||||
}
|
||||
|
||||
static const char *copy_email(const char *buf)
|
||||
static const char *copy_email(const char *buf, struct used_atom *atom)
|
||||
{
|
||||
const char *email = strchr(buf, '<');
|
||||
const char *eoemail;
|
||||
if (!email)
|
||||
return xstrdup("");
|
||||
eoemail = strchr(email, '>');
|
||||
switch (atom->u.email_option.option) {
|
||||
case EO_RAW:
|
||||
eoemail = strchr(email, '>');
|
||||
if (eoemail)
|
||||
eoemail++;
|
||||
break;
|
||||
case EO_TRIM:
|
||||
email++;
|
||||
eoemail = strchr(email, '>');
|
||||
break;
|
||||
case EO_LOCALPART:
|
||||
email++;
|
||||
eoemail = strchr(email, '@');
|
||||
if (!eoemail)
|
||||
eoemail = strchr(email, '>');
|
||||
break;
|
||||
default:
|
||||
BUG("unknown email option");
|
||||
}
|
||||
|
||||
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)
|
||||
@ -1116,7 +1152,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)
|
||||
@ -1127,8 +1163,8 @@ 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"))
|
||||
v->s = copy_email(wholine);
|
||||
else if (starts_with(name + wholen, "email"))
|
||||
v->s = copy_email(wholine, &used_atom[i]);
|
||||
else if (starts_with(name + wholen, "date"))
|
||||
grab_date(wholine, v, name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user