diff mbox series

[v2,3/4] builtin/diff: parse --no-index using parse_options()

Message ID ea6717e7b3a8b89fc3750505678321803cde78dc.1599723087.git.liu.denton@gmail.com (mailing list archive)
State Superseded
Headers show
Series builtin/diff: learn --merge-base | expand

Commit Message

Denton Liu Sept. 10, 2020, 7:32 a.m. UTC
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(-)

Comments

Junio C Hamano Sept. 10, 2020, 6:35 p.m. UTC | #1
Denton Liu <liu.denton@gmail.com> writes:

> 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(),
> +	};

What's the reasoning behind teaching the "--merge-base" option only
to "diff" and not allowing "diff-index" and "diff-tree" to also
benefit from the new support?  It should be taught to be handled by
diff.c::diff_opt_parse() instead, like all other "diff" options.  I
simply do not see what makes "--merge-base" so special compared to
other options like "-R", "--color-words", etc.

And with that, this step will become totally unnecessary, because
the options[] array will still have only the "no-index" option and
nothing else.

UNLESS we are planning to use this array to parse all diff options
here, whether it is for the no-index mode or the normal invocation
of diff, but that is unlikely to happen---we'd still share many
options between the plumbing "diff-*" family and Porcelain "diff"
commands.

Thanks.
Denton Liu Sept. 13, 2020, 8:31 a.m. UTC | #2
Hi Junio,

On Thu, Sep 10, 2020 at 11:35:42AM -0700, Junio C Hamano wrote:
> Denton Liu <liu.denton@gmail.com> writes:
> 
> > 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(),
> > +	};
> 
> What's the reasoning behind teaching the "--merge-base" option only
> to "diff" and not allowing "diff-index" and "diff-tree" to also
> benefit from the new support?

This is a good idea. Initially, I was hesitant because the manpages for
diff-index and diff-tree both list the argument as <treeish> but I think
it makes sense that these commands should accept --merge-base too.

> It should be taught to be handled by
> diff.c::diff_opt_parse() instead, like all other "diff" options.  I
> simply do not see what makes "--merge-base" so special compared to
> other options like "-R", "--color-words", etc.

Unfortunately, despite the above, I don't think we'll be able to handle
this in diff_opt_parse(). --merge-base won't be common to all diff
modes. It'll only work with diff-index and diff-tree, so it'll have to
be handled in those modes specifically.

Thanks,
Denton
Junio C Hamano Sept. 13, 2020, 9:45 p.m. UTC | #3
Denton Liu <liu.denton@gmail.com> writes:

>> It should be taught to be handled by
>> diff.c::diff_opt_parse() instead, like all other "diff" options.  I
>> simply do not see what makes "--merge-base" so special compared to
>> other options like "-R", "--color-words", etc.
>
> Unfortunately, despite the above, I don't think we'll be able to handle
> this in diff_opt_parse(). --merge-base won't be common to all diff
> modes. It'll only work with diff-index and diff-tree, so it'll have to
> be handled in those modes specifically.

Yes, I think we can follow precedences, like the "--cached" option,
"builtin/diff-index.c::cmd_diff_index()" knows about it and it is
also handled by "builtin/diff.c::builtin_diff_index()".

Thanks.
diff mbox series

Patch

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, "-"))