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

Re: [PATCH v3] builtin/diff-files: convert ad hoc option parsing to parse-options API

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 28, 2026, 22:24 UTC
Message-ID
<xmqq4ie579ac.fsf@gitster.g>
In-Reply-To
<20261028192647.976-1-edsonwasswa54@gmail.com>
Edson <edsonwasswa54@gmail.com> writes:
Show 22 quoted lines
> From: Wasswa Edson <edsonwasswa54@gmail.com>
>
> The cmd_diff_files() function manually parses --base, --ours, --theirs,
> and -q options using a hand-rolled loop with strcmp() calls. This is
> inconsistent with the rest of Git's codebase and lacks features provided
> by the parse-options API such as auto-generated help text, git completion
> support, and option abbreviation.
>
> Convert this to use the parse-options API by:
> - Adding option definitions using OPT_* macros
> - Removing the manual parsing loop
> - Using parse_options() for consistent argument handling
>
> The --base, --ours, and --theirs options use PARSE_OPT_NONEG to prevent
> negated forms (--no-base, etc.) which are not meaningful for merge stage
> selection. This follows the pattern used in builtin/checkout.c for similar
> stage selection options.
>
> This makes the code more maintainable and brings it in line with Git's
> coding standards.
>
> Signed-off-by: Wasswa Edson <edsonwasswa54@gmail.com>
Nicely described.
$ make DEVELOPER=YesPlease builtin/diff-files.o
is your friend.
Show 32 quoted lines
>  
>  static const char diff_files_usage[] =
>  "git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]"
> @@ -48,19 +49,20 @@ int cmd_diff_files(int argc,
>  	prefix = precompose_argv_prefix(argc, argv, prefix);
>  
>  	argc = setup_revisions(argc, argv, &rev, NULL);
> -	while (1 < argc && argv[1][0] == '-') {
> -		if (!strcmp(argv[1], "--base"))
> -			rev.max_count = 1;
> -		else if (!strcmp(argv[1], "--ours"))
> -			rev.max_count = 2;
> -		else if (!strcmp(argv[1], "--theirs"))
> -			rev.max_count = 3;
> -		else if (!strcmp(argv[1], "-q"))
> -			options |= DIFF_SILENT_ON_REMOVED;
> -		else
> -			usage(diff_files_usage);
> -		argv++; argc--;
> -	}
> +
> +	struct option diff_files_options[] = {
> +		OPT_SET_INT_F(0, "base", &rev.max_count,
> +			      N_("show base version"), 1, PARSE_OPT_NONEG),
> +		OPT_SET_INT_F(0, "ours", &rev.max_count,
> +			      N_("show our version"), 2, PARSE_OPT_NONEG),
> +		OPT_SET_INT_F(0, "theirs", &rev.max_count,
> +			      N_("show their version"), 3, PARSE_OPT_NONEG),
> +		OPT_BIT('q', NULL, &options,
> +		       N_("be quiet"), DIFF_SILENT_ON_REMOVED),
> +		OPT_END()
> +	};
Move this a lot higher.
builtin/diff-files.c:53:9: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
   53 |         struct option diff_files_options[] = {
> +	argc = parse_options(argc, argv, prefix, diff_files_options,
> +			     diff_files_usage, 0);

parse_options() takes a NULL-terminated array of constant strings, but diff_files_usage is a single constant string.

builtin/diff-files.c:65:30: error: passing argument 5 of ‘parse_options’ from incompatible pointer type [-Wincompatible-pointer-types]
   65 |                              diff_files_usage, 0);
      |                              ^~~~~~~~~~~~~~~~
      |                              |
      |                              const char *
In file included from builtin/diff-files.c:17:
./parse-options.h:424:38: note: expected ‘const char * const*’ but argument is of type ‘const char *’
  424 |                   const char * const usagestr[],
      |                   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~

As I said in my earlier message, conversion of parse-options API requires adjustment of usage string.

>  	if (!rev.diffopt.output_format)
>  		rev.diffopt.output_format = DIFF_FORMAT_RAW;
>  	rev.diffopt.rotate_to_strict = 1;
Previous: EdsonNext: Edson
Message 3 of 8 in “builtin/diff-files: convert ad hoc option parsing to parse-options API”
  1. builtin/diff-files: convert ad hoc option parsing to parse-options APIEdson, Oct 28, 2026
  2. builtin/diff-files: convert ad hoc option parsing to parse-options APIEdson, Oct 28, 2026
  3. Junio C HamanoOct 28, 2026
  4. builtin/diff-files: convert ad hoc option parsing to parse-options APIEdson, Oct 29, 2026
  5. Junio C HamanoOct 29, 2026
  6. builtin/diff-files: convert ad hoc option parsing to parse-options APIEdson, Nov 1, 2026
  7. Junio C HamanoNov 2, 2026
  8. Wasswa EdsonNov 2, 2026

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.