git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 73/75] range-diff: use parse_options() instead of diff_opt_parse()

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 27, 2018, 16:25 UTC
Message-ID
<20181227162536.15895-14-pclouds@gmail.com>
In-Reply-To
<20181227162536.15895-1-pclouds@gmail.com>

Diff's internal option parsing is now done with 'struct option', which makes it possible to combine all diff options to range-diff and parse everything all at once. Parsing code becomes simpler, and we get a looong 'git range-diff -h'

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/range-diff.c | 26 ++++++--------------------
 1 file changed, 6 insertions(+), 20 deletions(-)
diff --git a/builtin/range-diff.c b/builtin/range-diff.c
index f01a0be851..784bd19321 100644
--- a/builtin/range-diff.c
+++ b/builtin/range-diff.c
@@ -16,42 +16,27 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
 	int creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
 	struct diff_options diffopt = { NULL };
 	int simple_color = -1;
-	struct option options[] = {
+	struct option range_diff_options[] = {
 		OPT_INTEGER(0, "creation-factor", &creation_factor,
 			    N_("Percentage by which creation is weighted")),
 		OPT_BOOL(0, "no-dual-color", &simple_color,
 			    N_("use simple diff colors")),
 		OPT_END()
 	};
-	int i, j, res = 0;
+	struct option *options;
+	int res = 0;
 	struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
 
 	git_config(git_diff_ui_config, NULL);
 
 	repo_diff_setup(the_repository, &diffopt);
 
+	options = parse_options_concat(range_diff_options, diffopt.parseopts);
 	argc = parse_options(argc, argv, NULL, options,
-			     builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
-			     PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
-
-	for (i = j = 1; i < argc && strcmp("--", argv[i]); ) {
-		int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
+			     builtin_range_diff_usage, 0);
 
-		if (!c)
-			argv[j++] = argv[i++];
-		else
-			i += c;
-	}
-	while (i < argc)
-		argv[j++] = argv[i++];
-	argc = j;
 	diff_setup_done(&diffopt);
 
-	/* Make sure that there are no unparsed options */
-	argc = parse_options(argc, argv, NULL,
-			     options + ARRAY_SIZE(options) - 1, /* OPT_END */
-			     builtin_range_diff_usage, 0);
-
 	/* force color when --dual-color was used */
 	if (!simple_color)
 		diffopt.use_color = 1;
@@ -90,6 +75,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)
 		error(_("need two commit ranges"));
 		usage_with_options(builtin_range_diff_usage, options);
 	}
+	FREE_AND_NULL(options);
 
 	res = show_range_diff(range1.buf, range2.buf, creation_factor,
 			      simple_color < 1, &diffopt);
-- 
2.20.0.482.g66447595a7
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 16 of 18 in “Convert diff opt parser to parse_options()”
  1. 00/75 Convert diff opt parser to parse_options()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  2. 01/75 parse-options.h: remove extern on function prototypesNguyễn Thái Ngọc Duy, Dec 27, 2018
  3. 02/75 parse-options: add one-shot modeNguyễn Thái Ngọc Duy, Dec 27, 2018
  4. 04/75 parse-options: disable option abbreviation with PARSE_OPT_KEEP_UNKNOWNNguyễn Thái Ngọc Duy, Dec 27, 2018
  5. 03/75 parse-options: allow keep-unknown + stop-at-non-opt combinationNguyễn Thái Ngọc Duy, Dec 27, 2018
  6. 05/75 parse-options: add OPT_BITOP()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  7. 07/75 parse-options: avoid magic return codesNguyễn Thái Ngọc Duy, Dec 27, 2018
  8. 06/75 parse-options: stop abusing 'callback' for lowlevel callbacksNguyễn Thái Ngọc Duy, Dec 27, 2018
  9. 08/75 parse-options: allow ll_callback with OPTION_CALLBACKNguyễn Thái Ngọc Duy, Dec 27, 2018
  10. 11/75 diff.c: prepare to use parse_options() for parsingNguyễn Thái Ngọc Duy, Dec 27, 2018
  11. 74/75 diff --no-index: use parse_options() instead of diff_opt_parse()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  12. Eric SunshineDec 27, 2018
  13. 10/75 diff.h: avoid bit fields in struct diff_flagsNguyễn Thái Ngọc Duy, Dec 27, 2018
  14. Stefan BellerJan 9, 2019
  15. 75/75 am: avoid diff_opt_parse()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  16. 73/75 range-diff: use parse_options() instead of diff_opt_parse()Nguyễn Thái Ngọc Duy, Dec 27, 2018
  17. 09/75 diff.h: keep forward struct declarations sortedNguyễn Thái Ngọc Duy, Dec 27, 2018
  18. 12/75 diff.c: convert -u|-p|--patchNguyễn Thái Ngọc Duy, Dec 27, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.