From: Denton Liu <liu.denton@gmail.com>
To: Git Mailing List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2 3/4] builtin/diff: parse --no-index using parse_options()
Date: Thu, 10 Sep 2020 00:32:19 -0700 [thread overview]
Message-ID: <ea6717e7b3a8b89fc3750505678321803cde78dc.1599723087.git.liu.denton@gmail.com> (raw)
In-Reply-To: <cover.1599723087.git.liu.denton@gmail.com>
Instead of parsing the `--no-index` option with a plain strcmp, use
parse_options() to parse options. This allows us to easily add more
options in a future commit.
As a result of this change, `--no-index` is removed from `argv` so, as a
result, we no longer need to handle it in diff_no_index().
Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
builtin/diff.c | 33 ++++++++++++++++++++++-----------
diff-no-index.c | 15 +++------------
2 files changed, 25 insertions(+), 23 deletions(-)
diff --git a/builtin/diff.c b/builtin/diff.c
index cb98811c21..0e086ed7c4 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -373,6 +373,13 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
int nongit = 0, no_index = 0;
int result = 0;
struct symdiff sdiff;
+ struct option options[] = {
+ OPT_SET_INT_F(0, "no-index", &no_index,
+ N_("compare the given paths on the filesystem"),
+ DIFF_NO_INDEX_EXPLICIT,
+ PARSE_OPT_NONEG),
+ OPT_END(),
+ };
/*
* We could get N tree-ish in the rev.pending_objects list.
@@ -406,21 +413,25 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
* Other cases are errors.
*/
- /* Were we asked to do --no-index explicitly? */
- for (i = 1; i < argc; i++) {
- if (!strcmp(argv[i], "--")) {
- i++;
- break;
- }
- if (!strcmp(argv[i], "--no-index"))
- no_index = DIFF_NO_INDEX_EXPLICIT;
- if (argv[i][0] != '-')
- break;
- }
+ argc = parse_options(argc, argv, prefix, options, NULL,
+ PARSE_OPT_KEEP_DASHDASH |
+ PARSE_OPT_KEEP_ARGV0 |
+ PARSE_OPT_KEEP_UNKNOWN |
+ PARSE_OPT_NO_INTERNAL_HELP);
prefix = setup_git_directory_gently(&nongit);
if (!no_index) {
+ int i;
+ for (i = 1; i < argc; i++) {
+ if (!strcmp(argv[i], "--")) {
+ i++;
+ break;
+ }
+ if (argv[i][0] != '-')
+ break;
+ }
+
/*
* Treat git diff with at least one path outside of the
* repo the same as if the command would have been executed
diff --git a/diff-no-index.c b/diff-no-index.c
index 7814eabfe0..da82e2d090 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -243,28 +243,19 @@ int diff_no_index(struct rev_info *revs,
int implicit_no_index,
int argc, const char **argv)
{
- int i, no_index;
+ int i;
const char *paths[2];
struct strbuf replacement = STRBUF_INIT;
const char *prefix = revs->prefix;
- struct option no_index_options[] = {
- OPT_BOOL_F(0, "no-index", &no_index, "",
- PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
- OPT_END(),
- };
- struct option *options;
- options = parse_options_concat(no_index_options,
- revs->diffopt.parseopts);
- argc = parse_options(argc, argv, revs->prefix, options,
+ argc = parse_options(argc, argv, revs->prefix, revs->diffopt.parseopts,
diff_no_index_usage, 0);
if (argc != 2) {
if (implicit_no_index)
warning(_("Not a git repository. Use --no-index to "
"compare two paths outside a working tree"));
- usage_with_options(diff_no_index_usage, options);
+ usage_with_options(diff_no_index_usage, revs->diffopt.parseopts);
}
- FREE_AND_NULL(options);
for (i = 0; i < 2; i++) {
const char *p = argv[argc - 2 + i];
if (!strcmp(p, "-"))
--
2.28.0.618.gf4bc123cb7
next prev parent reply other threads:[~2020-09-10 7:33 UTC|newest]
Thread overview: 58+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-05 19:08 [PATCH 0/4] builtin/diff: learn --merge-base Denton Liu
2020-09-05 19:08 ` [PATCH 1/4] t4068: remove unnecessary >tmp Denton Liu
2020-09-05 19:08 ` [PATCH 2/4] git-diff.txt: backtick quote command text Denton Liu
2020-09-05 19:08 ` [PATCH 3/4] builtin/diff: parse --no-index using parse_options() Denton Liu
2020-09-05 19:08 ` [PATCH 4/4] builtin/diff: learn --merge-base Denton Liu
2020-09-06 21:58 ` Junio C Hamano
2020-09-10 7:32 ` [PATCH v2 0/4] " Denton Liu
2020-09-10 7:32 ` [PATCH v2 1/4] t4068: remove unnecessary >tmp Denton Liu
2020-09-10 7:32 ` [PATCH v2 2/4] git-diff.txt: backtick quote command text Denton Liu
2020-09-10 7:32 ` Denton Liu [this message]
2020-09-10 18:35 ` [PATCH v2 3/4] builtin/diff: parse --no-index using parse_options() Junio C Hamano
2020-09-13 8:31 ` Denton Liu
2020-09-13 21:45 ` Junio C Hamano
2020-09-10 7:32 ` [PATCH v2 4/4] builtin/diff: learn --merge-base Denton Liu
2020-09-17 7:44 ` [PATCH v3 00/10] " Denton Liu
2020-09-17 7:44 ` [PATCH v3 01/10] t4068: remove unnecessary >tmp Denton Liu
2020-09-17 7:44 ` [PATCH v3 02/10] git-diff-index.txt: make --cached description a proper sentence Denton Liu
2020-09-17 7:44 ` [PATCH v3 03/10] git-diff.txt: backtick quote command text Denton Liu
2020-09-17 7:44 ` [PATCH v3 04/10] contrib/completion: extract common diff/difftool options Denton Liu
2020-09-17 7:44 ` [PATCH v3 05/10] diff-lib: accept option flags in run_diff_index() Denton Liu
2020-09-17 17:00 ` Junio C Hamano
2020-09-17 7:44 ` [PATCH v3 06/10] diff-lib: define diff_get_merge_base() Denton Liu
2020-09-17 17:16 ` Junio C Hamano
2020-09-18 10:34 ` Denton Liu
2020-09-19 0:33 ` Junio C Hamano
2020-09-17 7:44 ` [PATCH v3 07/10] t4068: add --merge-base tests Denton Liu
2020-09-17 7:44 ` [PATCH v3 08/10] builtin/diff-index: learn --merge-base Denton Liu
2020-09-17 17:28 ` Junio C Hamano
2020-09-17 18:13 ` Jeff King
2020-09-18 5:11 ` Junio C Hamano
2020-09-18 18:12 ` Jeff King
2020-09-17 7:44 ` [PATCH v3 09/10] builtin/diff-tree: " Denton Liu
2020-09-17 18:23 ` Junio C Hamano
2020-09-18 10:48 ` Denton Liu
2020-09-18 16:52 ` Junio C Hamano
2020-09-20 11:01 ` Denton Liu
2020-09-21 16:05 ` Junio C Hamano
2020-09-21 17:27 ` Denton Liu
2020-09-21 21:09 ` Junio C Hamano
2020-09-21 21:19 ` Junio C Hamano
2020-09-21 21:54 ` Denton Liu
2020-09-21 22:18 ` Junio C Hamano
2020-09-23 9:47 ` Denton Liu
2020-09-25 21:02 ` Junio C Hamano
2020-09-26 1:52 ` Denton Liu
2020-09-17 7:44 ` [PATCH v3 10/10] contrib/completion: complete `git diff --merge-base` Denton Liu
2020-09-20 11:22 ` [PATCH v4 00/10] builtin/diff: learn --merge-base Denton Liu
2020-09-20 11:22 ` [PATCH v4 01/10] t4068: remove unnecessary >tmp Denton Liu
2020-09-20 11:22 ` [PATCH v4 02/10] git-diff-index.txt: make --cached description a proper sentence Denton Liu
2020-09-20 11:22 ` [PATCH v4 03/10] git-diff.txt: backtick quote command text Denton Liu
2020-09-20 11:22 ` [PATCH v4 04/10] contrib/completion: extract common diff/difftool options Denton Liu
2020-09-20 11:22 ` [PATCH v4 05/10] diff-lib: accept option flags in run_diff_index() Denton Liu
2020-09-20 11:22 ` [PATCH v4 06/10] diff-lib: define diff_get_merge_base() Denton Liu
2020-09-20 11:22 ` [PATCH v4 07/10] t4068: add --merge-base tests Denton Liu
2020-09-20 11:22 ` [PATCH v4 08/10] builtin/diff-index: learn --merge-base Denton Liu
2020-09-29 19:53 ` Martin Ågren
2020-09-20 11:22 ` [PATCH v4 09/10] builtin/diff-tree: " Denton Liu
2020-09-20 11:22 ` [PATCH v4 10/10] contrib/completion: complete `git diff --merge-base` Denton Liu
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ea6717e7b3a8b89fc3750505678321803cde78dc.1599723087.git.liu.denton@gmail.com \
--to=liu.denton@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).