From: Edson <edsonwasswa54@gmail.com>
To: git@vger.kernel.org
Cc: edsonwasswa54@gmail.com
Subject: [PATCH v5] builtin/diff-files: convert ad hoc option parsing to parse-options API
Date: Fri, 30 Oct 2026 16:13:38 +0300 [thread overview]
Message-ID: <20261030131338.1513-1-edsonwasswa54@gmail.com> (raw)
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>
---
builtin/diff-files.c | 39 ++++++++++++++++++++-------------------
1 file changed, 20 insertions(+), 19 deletions(-)
diff --git a/builtin/diff-files.c b/builtin/diff-files.c
index ea91347ce2..e9e4907311 100644
--- a/builtin/diff-files.c
+++ b/builtin/diff-files.c
@@ -14,11 +14,12 @@
#include "commit.h"
#include "preload-index.h"
#include "revision.h"
+#include "parse-options.h"
-static const char diff_files_usage[] =
-"git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]"
-"\n"
-COMMON_DIFF_OPTIONS_HELP;
+static const char * const diff_files_usage[] = {
+ "git diff-files [-q] [-0 | -1 | -2 | -3 | -c | --cc] [<common-diff-options>] [<path>...]",
+ NULL
+};
int cmd_diff_files(int argc,
const char **argv,
@@ -28,8 +29,19 @@ int cmd_diff_files(int argc,
struct rev_info rev;
int result;
unsigned options = 0;
+ 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()
+ };
- show_usage_if_asked(argc, argv, diff_files_usage);
+ show_usage_with_options_if_asked(argc, argv, diff_files_usage, diff_files_options);
repo_config(the_repository, git_diff_basic_config, NULL); /* no "diff" UI options */
@@ -48,19 +60,8 @@ 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--;
- }
+ argc = parse_options(argc, argv, prefix, diff_files_options,
+ diff_files_usage, 0);
if (!rev.diffopt.output_format)
rev.diffopt.output_format = DIFF_FORMAT_RAW;
rev.diffopt.rotate_to_strict = 1;
@@ -73,7 +74,7 @@ int cmd_diff_files(int argc,
if (rev.pending.nr ||
rev.min_age != -1 || rev.max_age != -1 ||
3 < rev.max_count)
- usage(diff_files_usage);
+ usage_with_options(diff_files_usage, diff_files_options);
/*
* "diff-files --base -p" should not combine merges because it
--
2.46.0.windows.1
next reply other threads:[~2026-10-30 13:13 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 13:13 Edson [this message]
2026-10-30 17:22 ` [PATCH v5] builtin/diff-files: convert ad hoc option parsing to parse-options API Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261030131338.1513-1-edsonwasswa54@gmail.com \
--to=edsonwasswa54@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox