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

Re: [PATCH 1/5 v4] diff: parse detached options like -S foo

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 2, 2010, 16:46 UTC
Message-ID
<7v62ztnesc.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1280478669-22973-1-git-send-email-Matthieu.Moy@imag.fr>
Matthieu Moy <Matthieu.Moy@imag.fr> writes:
Show 36 quoted lines
> Change the option parsing logic in revision.c to accept detached forms
> like `-S foo' in addition to `-Sfoo'. The rest of git already accepted
> this form, but revision.c still used its own option parsing.
>
> This patch does not handle --stat-name-width and --stat-width, which are
> special-cases where diff_long_opt do not apply. They are handled in a
> separate patch to ease review.
>
> Original patch by Matthieu Moy, plus refactoring by Jonathan Nieder.
>
> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
> ---
>  diff.c                       |   87 ++++++++++++++++++++++++++++++++++--------
>  diff.h                       |    7 +++
>  t/t4013-diff-various.sh      |    5 ++
>  t/t4013/diff.log_-S_F_master |    7 +++
>  t/t4202-log.sh               |   12 ++---
>  5 files changed, 95 insertions(+), 23 deletions(-)
>  create mode 100644 t/t4013/diff.log_-S_F_master
>
> diff --git a/diff.c b/diff.c
> index 17873f3..d89ea20 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -2990,9 +2990,50 @@ static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *va
>  
>  static int diff_scoreopt_parse(const char *opt);
>  
> +static inline int short_opt(char opt, const char **argv,
> +			    const char **optarg)
> +{
> +	const char *arg = argv[0];
> +	if (arg[0] != '-' || arg[1] != opt)
> +		return 0;
> +	if (arg[2] != '\0') {
> +		*optarg = arg + strlen("-c");

Just a style thing, but I think "arg + 2" is much easier to read in this particular case, as it won't risk tempting readers to go "Huh? What does that 'c' mean"? I know the code wants to skip over an option that is a single dash followed by a single byte and 'c' is just a placeholder for that single unknown byte, but when the reader reaches that realization, the reader already knows that the code wants to skip exactly two bytes, no? strlen("--") in diff_long_opt() is much easier to justify, though. I like the reduction of "arg + N"s that follow prefixcmp(arg, "--whatever")s in diff_opt_parse() this patch gives us in general. I just think the above "-c" is overdoing it.

Do we have an option that can take a zero-length string as its value and do something meaningful? I don't think of any offhand ("log -S'' -p" is not it---it may be meaningful but it is not useful), but this code would start giving "-p" instead of "" to the option in such a case.

A longer option always required '=' before the value, so it won't share the above issue (if it is an issue, that is).

Other than that minor style issue and a nagging -X<empty> worry, the resulting code looks a lot nicer than the original.

Thanks.
Previous: Matthieu MoyNext: Matthieu Moy
Message 9 of 25 in “log and diff: accept detached forms (--option value)”
  1. 0/5 log and diff: accept detached forms (--option value)Matthieu Moy, Jul 29, 2010
  2. 1/5 diff: parse detached options like -S fooMatthieu Moy, Jul 29, 2010
  3. 2/5 diff: split off a function for --stat-* option parsingMatthieu Moy, Jul 29, 2010
  4. 3/5 diff: parse detached options --stat-width n, --stat-name-width nMatthieu Moy, Jul 29, 2010
  5. 4/5 log: parse detached options like git log --grep fooMatthieu Moy, Jul 29, 2010
  6. Jonathan NiederJul 29, 2010
  7. Matthieu MoyJul 30, 2010
  8. 1/5 diff: parse detached options like -S fooMatthieu Moy, Jul 30, 2010
  9. Junio C HamanoAug 2, 2010
  10. Matthieu MoyAug 2, 2010
  11. Jonathan NiederAug 2, 2010
  12. 2/5 diff: split off a function for --stat-* option parsingMatthieu Moy, Jul 30, 2010
  13. 3/5 diff: parse detached options --stat-width n, --stat-name-width nMatthieu Moy, Jul 30, 2010
  14. Junio C HamanoAug 2, 2010
  15. Matthieu MoyAug 2, 2010
  16. 4/5 log: parse detached options like git log --grep fooMatthieu Moy, Jul 30, 2010
  17. Junio C HamanoAug 2, 2010
  18. Matthieu MoyAug 2, 2010
  19. Junio C HamanoAug 2, 2010
  20. Matthieu MoyAug 3, 2010
  21. Junio C HamanoAug 3, 2010
  22. Matthieu MoyAug 3, 2010
  23. Miles BaderAug 4, 2010
  24. 5/5 log: parse detached option for --globMatthieu Moy, Jul 30, 2010
  25. 5/5 log: parse detached option for --globMatthieu Moy, Jul 29, 2010

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.