{"thread":{"id":"66691","subject":"[PATCH] builtin/diff-files: convert ad hoc option parsing to parse-options API","startedAt":"2026-10-28T14:13:54Z","lastAt":"2026-10-28T17:04:21Z","messageCount":2,"participants":["Edson","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"556665","messageId":"20261028141354.1155-1-edsonwasswa54@gmail.com","threadId":"66691","inReplyTo":null,"subject":"[PATCH] builtin/diff-files: convert ad hoc option parsing to parse-options API","fromName":"Edson","fromEmail":"edsonwasswa54@gmail.com","sentAt":"2026-10-28T14:13:54Z","receivedAt":"2026-10-28T14:13:54Z","isPatch":true,"sender":{"key":"edsonwasswa54@gmail.com","avatar":null},"body":"The cmd_diff_files() function manually parses --base, --ours, --theirs,\nand -q options using a hand-rolled loop with strcmp() calls. This is\nerror-prone and inconsistent with the rest of Git's codebase.\n\nConvert this to use the parse-options API by:\n- Adding option definitions using OPT_* macros\n- Removing the manual parsing loop\n- Using parse_options() for consistent argument handling\n\nThe --base, --ours, and --theirs options use PARSE_OPT_NONEG to prevent\nnegated forms (--no-base, etc.) which are not meaningful for merge stage\nselection. This follows the pattern used in builtin/checkout.c for similar\nstage selection options.\n\nThis makes the code more maintainable and brings it in line with Git's coding standards. It also enables auto-generated help text, git completion support, and option abbreviation.\n\nSigned-off-by: Edson <edsonwasswa54@gmail.com>\n---\n builtin/diff-files.c | 28 ++++++++++++++++------------\n 1 file changed, 16 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin/diff-files.c b/builtin/diff-files.c\nindex ea91347ce2..a0c3981407 100644\n--- a/builtin/diff-files.c\n+++ b/builtin/diff-files.c\n@@ -14,6 +14,7 @@\n #include \"commit.h\"\n #include \"preload-index.h\"\n #include \"revision.h\"\n+#include \"parse-options.h\"\n \n static const char diff_files_usage[] =\n \"git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]\"\n@@ -48,18 +49,21 @@ int cmd_diff_files(int argc,\n \tprefix = precompose_argv_prefix(argc, argv, prefix);\n \n \targc = setup_revisions(argc, argv, &rev, NULL);\n-\twhile (1 < argc && argv[1][0] == '-') {\n-\t\tif (!strcmp(argv[1], \"--base\"))\n-\t\t\trev.max_count = 1;\n-\t\telse if (!strcmp(argv[1], \"--ours\"))\n-\t\t\trev.max_count = 2;\n-\t\telse if (!strcmp(argv[1], \"--theirs\"))\n-\t\t\trev.max_count = 3;\n-\t\telse if (!strcmp(argv[1], \"-q\"))\n-\t\t\toptions |= DIFF_SILENT_ON_REMOVED;\n-\t\telse\n-\t\t\tusage(diff_files_usage);\n-\t\targv++; argc--;\n+\n+\t{\n+\t\tstatic struct option diff_files_options[] = {\n+\t\t\tOPT_SET_INT_F(0, \"base\", &rev.max_count,\n+\t\t\t\t      N_(\"show base version\"), 1, PARSE_OPT_NONEG),\n+\t\t\tOPT_SET_INT_F(0, \"ours\", &rev.max_count,\n+\t\t\t\t      N_(\"show our version\"), 2, PARSE_OPT_NONEG),\n+\t\t\tOPT_SET_INT_F(0, \"theirs\", &rev.max_count,\n+\t\t\t\t      N_(\"show their version\"), 3, PARSE_OPT_NONEG),\n+\t\t\tOPT_BIT('q', NULL, &options,\n+\t\t\t       N_(\"be quiet\"), DIFF_SILENT_ON_REMOVED),\n+\t\t\tOPT_END()\n+\t\t};\n+\t\targc = parse_options(argc, argv, prefix, diff_files_options,\n+\t\t\t\t     diff_files_usage, 0);\n \t}\n \tif (!rev.diffopt.output_format)\n \t\trev.diffopt.output_format = DIFF_FORMAT_RAW;\n-- \n2.46.0.windows.1\n\n\n"},{"id":"556687","messageId":"xmqq4ie5bvu2.fsf@gitster.g","threadId":"66691","inReplyTo":"20261028141354.1155-1-edsonwasswa54@gmail.com","subject":"Re: [PATCH] builtin/diff-files: convert ad hoc option parsing to parse-options API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-28T17:04:21Z","receivedAt":"2026-10-28T17:04:21Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Edson <edsonwasswa54@gmail.com> writes:\n\n> The cmd_diff_files() function manually parses --base, --ours, --theirs,\n> and -q options using a hand-rolled loop with strcmp() calls. This is\n> error-prone and inconsistent with the rest of Git's codebase.\n\nThe first sentence is correct, but is it error-prone how?\n\n> Convert this to use the parse-options API by:\n> - Adding option definitions using OPT_* macros\n> - Removing the manual parsing loop\n> - Using parse_options() for consistent argument handling\n>\n> The --base, --ours, and --theirs options use PARSE_OPT_NONEG to prevent\n> negated forms (--no-base, etc.) which are not meaningful for merge stage\n> selection. This follows the pattern used in builtin/checkout.c for similar\n> stage selection options.\n>\n> This makes the code more maintainable and brings it in line with Git's coding standards. It also enables auto-generated help text, git completion support, and option abbreviation.\n>\n> Signed-off-by: Edson <edsonwasswa54@gmail.com>\n\nEarlier messages to the list from this e-mail address identified the\nsender as \"Wasswa Edson\".  Are these the same person?\n\nSee Documentation/SubmittingPatches [[dco]] and [[real-name]].\n\n\n> ---\n>  builtin/diff-files.c | 28 ++++++++++++++++------------\n>  1 file changed, 16 insertions(+), 12 deletions(-)\n>\n> diff --git a/builtin/diff-files.c b/builtin/diff-files.c\n> index ea91347ce2..a0c3981407 100644\n> --- a/builtin/diff-files.c\n> +++ b/builtin/diff-files.c\n> @@ -14,6 +14,7 @@\n>  #include \"commit.h\"\n>  #include \"preload-index.h\"\n>  #include \"revision.h\"\n> +#include \"parse-options.h\"\n>  \n>  static const char diff_files_usage[] =\n>  \"git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]\"\n\nUsing parse-options API means you'd need to be careful about how\nyou write the usage string, as the parse_options() call itself will\nlist options and its meanings.  Doesn't the above need adjusting?\n\n> @@ -48,18 +49,21 @@ int cmd_diff_files(int argc,\n>  \tprefix = precompose_argv_prefix(argc, argv, prefix);\n>  \n>  \targc = setup_revisions(argc, argv, &rev, NULL);\n> -\twhile (1 < argc && argv[1][0] == '-') {\n> -\t\tif (!strcmp(argv[1], \"--base\"))\n> -\t\t\trev.max_count = 1;\n> -\t\telse if (!strcmp(argv[1], \"--ours\"))\n> -\t\t\trev.max_count = 2;\n> -\t\telse if (!strcmp(argv[1], \"--theirs\"))\n> -\t\t\trev.max_count = 3;\n> -\t\telse if (!strcmp(argv[1], \"-q\"))\n> -\t\t\toptions |= DIFF_SILENT_ON_REMOVED;\n> -\t\telse\n> -\t\t\tusage(diff_files_usage);\n> -\t\targv++; argc--;\n> +\n> +\t{\n> +\t\tstatic struct option diff_files_options[] = {\n> +\t\t\tOPT_SET_INT_F(0, \"base\", &rev.max_count,\n> +\t\t\t\t      N_(\"show base version\"), 1, PARSE_OPT_NONEG),\n> +\t\t\tOPT_SET_INT_F(0, \"ours\", &rev.max_count,\n> +\t\t\t\t      N_(\"show our version\"), 2, PARSE_OPT_NONEG),\n> +\t\t\tOPT_SET_INT_F(0, \"theirs\", &rev.max_count,\n> +\t\t\t\t      N_(\"show their version\"), 3, PARSE_OPT_NONEG),\n> +\t\t\tOPT_BIT('q', NULL, &options,\n> +\t\t\t       N_(\"be quiet\"), DIFF_SILENT_ON_REMOVED),\n> +\t\t\tOPT_END()\n> +\t\t};\n> +\t\targc = parse_options(argc, argv, prefix, diff_files_options,\n> +\t\t\t\t     diff_files_usage, 0);\n>  \t}\n\nIs it necessary to introduce an extra block that only increases the\nindent level?  I do not think so.  Move the \"struct option []\" up in\nthe beginning of the function.  If we really have to use the\nparse_options() API, that is.\n\nIt also is unusual in modern code to have \"struct option[]\" array as\na function scope static, even though older code like cmd_fsck()\nmakes it static.\n\n>  \tif (!rev.diffopt.output_format)\n>  \t\trev.diffopt.output_format = DIFF_FORMAT_RAW;\n\n"}]}