{"thread":{"id":"66716","subject":"[PATCH v5] builtin/diff-files: convert ad hoc option parsing to parse-options API","startedAt":"2026-10-30T13:13:38Z","lastAt":"2026-10-30T17:22:38Z","messageCount":2,"participants":["Edson","Junio C Hamano"],"isPatch":true,"patchVersion":5,"patchTotal":null},"messages":[{"id":"556916","messageId":"20261030131338.1513-1-edsonwasswa54@gmail.com","threadId":"66716","inReplyTo":null,"subject":"[PATCH v5] builtin/diff-files: convert ad hoc option parsing to parse-options API","fromName":"Edson","fromEmail":"edsonwasswa54@gmail.com","sentAt":"2026-10-30T13:13:38Z","receivedAt":"2026-10-30T13:13:38Z","isPatch":true,"sender":{"key":"edsonwasswa54@gmail.com","avatar":null},"body":"From: Wasswa Edson <edsonwasswa54@gmail.com>\n\nThe cmd_diff_files() function manually parses --base, --ours, --theirs,\nand -q options using a hand-rolled loop with strcmp() calls. This is\ninconsistent with the rest of Git's codebase and lacks features provided\nby the parse-options API such as auto-generated help text, git completion\nsupport, and option abbreviation.\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\ncoding standards.\n\nSigned-off-by: Wasswa Edson <edsonwasswa54@gmail.com>\n---\n builtin/diff-files.c | 39 ++++++++++++++++++++-------------------\n 1 file changed, 20 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/diff-files.c b/builtin/diff-files.c\nindex ea91347ce2..e9e4907311 100644\n--- a/builtin/diff-files.c\n+++ b/builtin/diff-files.c\n@@ -14,11 +14,12 @@\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-\"\\n\"\n-COMMON_DIFF_OPTIONS_HELP;\n+static const char * const diff_files_usage[] = {\n+\t\"git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]\",\n+\tNULL\n+};\n \n int cmd_diff_files(int argc,\n \t\t   const char **argv,\n@@ -28,8 +29,19 @@ int cmd_diff_files(int argc,\n \tstruct rev_info rev;\n \tint result;\n \tunsigned options = 0;\n+\tstruct option diff_files_options[] = {\n+\t\tOPT_SET_INT_F(0, \"base\", &rev.max_count,\n+\t\t\t      N_(\"show base version\"), 1, PARSE_OPT_NONEG),\n+\t\tOPT_SET_INT_F(0, \"ours\", &rev.max_count,\n+\t\t\t      N_(\"show our version\"), 2, PARSE_OPT_NONEG),\n+\t\tOPT_SET_INT_F(0, \"theirs\", &rev.max_count,\n+\t\t\t      N_(\"show their version\"), 3, PARSE_OPT_NONEG),\n+\t\tOPT_BIT('q', NULL, &options,\n+\t\t       N_(\"be quiet\"), DIFF_SILENT_ON_REMOVED),\n+\t\tOPT_END()\n+\t};\n \n-\tshow_usage_if_asked(argc, argv, diff_files_usage);\n+\tshow_usage_with_options_if_asked(argc, argv, diff_files_usage, diff_files_options);\n \n \trepo_config(the_repository, git_diff_basic_config, NULL); /* no \"diff\" UI options */\n \n@@ -48,19 +60,8 @@ 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-\t}\n+\targc = parse_options(argc, argv, prefix, diff_files_options,\n+\t\t\t     diff_files_usage, 0);\n \tif (!rev.diffopt.output_format)\n \t\trev.diffopt.output_format = DIFF_FORMAT_RAW;\n \trev.diffopt.rotate_to_strict = 1;\n@@ -73,7 +74,7 @@ int cmd_diff_files(int argc,\n \tif (rev.pending.nr ||\n \t    rev.min_age != -1 || rev.max_age != -1 ||\n \t    3 < rev.max_count)\n-\t\tusage(diff_files_usage);\n+\t\tusage_with_options(diff_files_usage, diff_files_options);\n \n \t/*\n \t * \"diff-files --base -p\" should not combine merges because it\n-- \n2.46.0.windows.1\n\n\n"},{"id":"556940","messageId":"xmqqmrrvw1b5.fsf@gitster.g","threadId":"66716","inReplyTo":"20261030131338.1513-1-edsonwasswa54@gmail.com","subject":"Re: [PATCH v5] builtin/diff-files: convert ad hoc option parsing to parse-options API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-30T17:22:38Z","receivedAt":"2026-10-30T17:22:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Your message has no In-Reply-To (or References) headers, even though\nthis is [v5] and should clearly have its own history.\n\nThis hurts both automation and human readers, as people cannot find\nand compare the previous round (v4) from the message of v5.\n\nPlease sort it out before sending more patches, as messages with\nbroken threading will take more resources and time out of your\nrecipients.\n\nThanks.\n\n"}]}