git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/5] ref-filter: support different email formats

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 27, 2020, 22:51 UTC
Message-ID
<xmqqeeowfu75.fsf@gitster.c.googlers.com>
In-Reply-To
<aeb116c5aaaa23dfefbc7a6f4ac743a6f5a3ade8.1595882588.git.gitgitgadget@gmail.com>
"Hariom Verma via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Hariom Verma <hariom18599@gmail.com>
>
> Currently, ref-filter only supports printing email with arrow brackets.

This is the first time I heard the term "arrow bracket". Aren't they more commonly called angle brackets?

> Let's add support for two more email options.
> - trim : print email without arrow brackets.
Why would this be useful?
> - localpart : prints the part before the @ sign
Meaning I'd get "<gitster" for me?

Building small pieces of new feature in each patch is good, and adding tests to each step is also good. Why not do the same for docs?

> +static struct email_option{
Missing SP.
Show 8 quoted lines
> +	enum { EO_INVALID, EO_RAW, EO_TRIM, EO_LOCALPART } option;
> +} email_option;
> +
> @@ -1040,10 +1044,26 @@ static const char *copy_email(const char *buf)
>  	const char *eoemail;
>  	if (!email)
>  		return xstrdup("");
> -	eoemail = strchr(email, '>');

The original code prepares to see NULL from this strchr(); that is why it checks eoemail for NULL and returns an empty string---the data is broken (i.e. not an address in angle brackets), which this code cannot do anything about---in the later part of the code.

> +	switch (email_option.option) {
> +	case EO_RAW:
> +		eoemail = strchr(email, '>') + 1;

And this breaks the carefully laid out error handling by the original code. Adding 1 to NULL is quite undefined.

Show 8 quoted lines
> +		break;
> +	case EO_TRIM:
> +		email++;
> +		eoemail = strchr(email, '>');
> +		break;
> +	case EO_LOCALPART:
> +		email++;
> +		eoemail = strchr(email, '@');

The undocumented design here is that you want to return "hariom" for "<hariom@gmail.com>", i.e. out of the "trimmed" e-mail, the part before the at-sign is returned.

If the data were "<hariom>", you'd still want to return "hariom" no? But because you do not check for NULL, you end up returning an empty string.

I think you want to cut at the first '@' or '>', whichever comes first.

> +		break;
> +	case EO_INVALID:
> +	default:

Invalid and unhandled ones are silently ignored and not treated as an error? I would have thought that at least the "default" one would be a BUG(), as you covered all the possible values for the enum with case arms. I wouldn't be surprised if seeing EO_INVALID is also a BUG(), i.e. the control flow that led to the caller to call this function with EO_INVALID in email_option.option is likely to be broken. It's not like you return "" to protect yourself when fed a bad data from objects---a bad value in .option can only come here if the parser you wrote for "--format=<string>" produced a wrong result.

Show 32 quoted lines
> +		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;

The ref-filter formatting language already knows many "colon plus modifier" suffix like "refname:short" and "contents:body", but I do not think we have ugly repetition like the above code to parse them. Perhaps the addition for "email:<whatever>" can benefit from studying and mimicking existing practices a bit more?

Previous: Hariom Verma via GitGitGadgetNext: Hariom verma
Message 3 of 51 in “[GSoC] Improvements to ref-filter”
  1. 0/5 [GSoC] Improvements to ref-filterHariom Verma via GitGitGadget, Jul 27, 2020
  2. 1/5 ref-filter: support different email formatsHariom Verma via GitGitGadget, Jul 27, 2020
  3. Junio C HamanoJul 27, 2020
  4. Hariom vermaJul 28, 2020
  5. Junio C HamanoJul 28, 2020
  6. Đoàn Trần Công DanhJul 28, 2020
  7. Junio C HamanoJul 28, 2020
  8. 2/5 ref-filter: add `short` option for 'tree' and 'parent'Hariom Verma via GitGitGadget, Jul 27, 2020
  9. Junio C HamanoJul 27, 2020
  10. 3/5 pretty: refactor `format_sanitized_subject()`Hariom Verma via GitGitGadget, Jul 27, 2020
  11. 4/5 format-support: move `format_sanitized_subject()` from prettyHariom Verma via GitGitGadget, Jul 27, 2020
  12. 5/5 ref-filter: add `sanitize` option for 'subject' atomHariom Verma via GitGitGadget, Jul 27, 2020
  13. 0/9 [GSoC] Improvements to ref-filterHariom Verma via GitGitGadget, Aug 5, 2020
  14. 1/9 ref-filter: support different email formatsHariom Verma via GitGitGadget, Aug 5, 2020
  15. 2/9 ref-filter: refactor `grab_objectname()`Hariom Verma via GitGitGadget, Aug 5, 2020
  16. 3/9 ref-filter: modify error messages in `grab_objectname()`Hariom Verma via GitGitGadget, Aug 5, 2020
  17. 4/9 ref-filter: rename `objectname` related functions and fieldsHariom Verma via GitGitGadget, Aug 5, 2020
  18. 5/9 ref-filter: add `short` modifier to 'tree' atomHariom Verma via GitGitGadget, Aug 5, 2020
  19. 6/9 ref-filter: add `short` modifier to 'parent' atomHariom Verma via GitGitGadget, Aug 5, 2020
  20. 8/9 format-support: move `format_sanitized_subject()` from prettyHariom Verma via GitGitGadget, Aug 5, 2020
  21. 9/9 ref-filter: add `sanitize` option for 'subject' atomHariom Verma via GitGitGadget, Aug 5, 2020
  22. 7/9 pretty: refactor `format_sanitized_subject()`Hariom Verma via GitGitGadget, Aug 5, 2020
  23. Junio C HamanoAug 5, 2020
  24. Hariom vermaAug 6, 2020
  25. 0/9 [Resend][GSoC] Improvements to ref-filterHariom Verma via GitGitGadget, Aug 17, 2020
  26. 2/9 ref-filter: refactor `grab_objectname()`Hariom Verma via GitGitGadget, Aug 17, 2020
  27. 3/9 ref-filter: modify error messages in `grab_objectname()`Hariom Verma via GitGitGadget, Aug 17, 2020
  28. 1/9 ref-filter: support different email formatsHariom Verma via GitGitGadget, Aug 17, 2020
  29. 4/9 ref-filter: rename `objectname` related functions and fieldsHariom Verma via GitGitGadget, Aug 17, 2020
  30. 7/9 pretty: refactor `format_sanitized_subject()`Hariom Verma via GitGitGadget, Aug 17, 2020
  31. Junio C HamanoAug 17, 2020
  32. Hariom vermaAug 19, 2020
  33. Junio C HamanoAug 19, 2020
  34. Junio C HamanoAug 19, 2020
  35. Hariom vermaAug 20, 2020
  36. Hariom vermaAug 20, 2020
  37. 6/9 ref-filter: add `short` modifier to 'parent' atomHariom Verma via GitGitGadget, Aug 17, 2020
  38. 5/9 ref-filter: add `short` modifier to 'tree' atomHariom Verma via GitGitGadget, Aug 17, 2020
  39. 8/9 format-support: move `format_sanitized_subject()` from prettyHariom Verma via GitGitGadget, Aug 17, 2020
  40. Junio C HamanoAug 17, 2020
  41. 9/9 ref-filter: add `sanitize` option for 'subject' atomHariom Verma via GitGitGadget, Aug 17, 2020
  42. Junio C HamanoAug 17, 2020
  43. 0/8 [GSoC] Improvements to ref-filterHariom Verma via GitGitGadget, Aug 21, 2020
  44. 1/8 ref-filter: support different email formatsHariom Verma via GitGitGadget, Aug 21, 2020
  45. 2/8 ref-filter: refactor `grab_objectname()`Hariom Verma via GitGitGadget, Aug 21, 2020
  46. 3/8 ref-filter: modify error messages in `grab_objectname()`Hariom Verma via GitGitGadget, Aug 21, 2020
  47. 5/8 ref-filter: add `short` modifier to 'tree' atomHariom Verma via GitGitGadget, Aug 21, 2020
  48. 6/8 ref-filter: add `short` modifier to 'parent' atomHariom Verma via GitGitGadget, Aug 21, 2020
  49. 8/8 ref-filter: add `sanitize` option for 'subject' atomHariom Verma via GitGitGadget, Aug 21, 2020
  50. 4/8 ref-filter: rename `objectname` related functions and fieldsHariom Verma via GitGitGadget, Aug 21, 2020
  51. 7/8 pretty: refactor `format_sanitized_subject()`Hariom Verma via GitGitGadget, Aug 21, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.