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

[PATCH 5/5] diff: parse U/u/unified options with an optional integer arg

From
EWEric Wong <normalperson@yhbt.net>
Date
May 14, 2006, 15:19 UTC
Message-ID
<11476199631085-git-send-email-normalperson@yhbt.net>
In-Reply-To
<11476199622462-git-send-email-normalperson@yhbt.net>

Looks like Linus and I both think GIT_DIFF_OPTS="--unified=5" is silly, but it still continues to work.

This was originally bundled into my first series gitopt patches, and now Linus has a more correct/complete one that affects combine-diff and works with uppercase -U This patch combines the Linus one with my gitopt version.

-u (lowercase) now accepts an optional arg, like -U (GNU diff -u also has this behavior).

This uses the built-in gitopt parsers to do optional integer argument handling.

Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 combine-diff.c |    1 +
 diff.c         |   11 ++++++++---
 diff.h         |    1 +
 3 files changed, 10 insertions(+), 3 deletions(-)
102e3227de7c7f08a69096d5094ee31128bf9819
diff --git a/combine-diff.c b/combine-diff.c
index 8a8fe38..64b20cc 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -608,6 +608,7 @@ static int show_patch_diff(struct combin
 	int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
 	mmfile_t result_file;
 
+	context = opt->context;
 	/* Read the result of merge first */
 	if (!working_tree_file)
 		result = grab_blob(elem->sha1, &result_size);
diff --git a/diff.c b/diff.c
index 7d88dc5..6ee612f 100644
--- a/diff.c
+++ b/diff.c
@@ -15,7 +15,7 @@ #include "gitopt.h"
 static int diff_scoreopt_parse(const int id, const char *opt);
 
 enum diff_ost_ids {
-	opt_p = GITOPT_DIFF_BASE, opt_raw, opt_patch_with_raw,
+	opt_u = GITOPT_DIFF_BASE, opt_p, opt_raw, opt_patch_with_raw,
 	opt_stat, opt_patch_with_stat,
 	opt_z, opt_l, opt_full_index, opt_name_only, opt_name_status,
 	opt_R, opt_S, opt_s, opt_O, opt_diff_filter,
@@ -26,7 +26,8 @@ enum diff_ost_ids {
 
 const struct opt_spec diff_ost[] = {
 	{ 0,			'p',	0,	0,	opt_p },
-	{ "unified",		'u',	0,	0,	opt_p },
+	{ "unified",		'u',	0,	ARG_OPTINT,	opt_u },
+	{ 0,			'U',	0,	ARG_OPTINT,	opt_u },
 	{ "raw",		0,	0,	0,	opt_raw },
 	{ "patch-with-raw",	0,	0,	0,	opt_patch_with_raw },
 	{ "stat",		0,	0,	0,	opt_stat },
@@ -600,7 +601,7 @@ static void builtin_diff(const char *nam
 
 		ecbdata.label_path = lbl;
 		xpp.flags = XDF_NEED_MINIMAL;
-		xecfg.ctxlen = 3;
+		xecfg.ctxlen = o->context;
 		xecfg.flags = XDL_EMIT_FUNCNAMES;
 		if (!diffopts)
 			;
@@ -1224,6 +1225,7 @@ void diff_setup(struct diff_options *opt
 	options->line_termination = '\n';
 	options->break_opt = -1;
 	options->rename_limit = -1;
+	options->context = 3;
 
 	options->change = diff_change;
 	options->add_remove = diff_addremove;
@@ -1269,6 +1271,9 @@ int diff_opt_handler(struct gitopt_itera
 	struct diff_options *options = (struct diff_options *)args;
 
 	switch (id) {
+	case opt_u:
+		if (gi->ea->argc > 1)
+			options->context = strtol(gi->ea->argv[1], NULL, 10);
 	case opt_p:
 		options->output_format = DIFF_FORMAT_PATCH;
 		break;
diff --git a/diff.h b/diff.h
index fa44d1b..b9d7573 100644
--- a/diff.h
+++ b/diff.h
@@ -33,6 +33,7 @@ struct diff_options {
 		 full_index:1,
 		 silent_on_remove:1,
 		 find_copies_harder:1;
+	int context;
 	int break_opt;
 	int detect_rename;
 	int line_termination;
-- 
1.3.2.g102e322
Previous: Eric WongNext: Linus Torvalds
Message 10 of 12 in “gitopt - command-line parsing enhancements (take #2)”
  1. gitopt - command-line parsing enhancements (take #2)Eric Wong, May 14, 2006
  2. 1/5 gitopt: a new command-line option parser for gitEric Wong, May 14, 2006
  3. 2/5 gitopt: convert ls-files, ls-tree, update-indexEric Wong, May 14, 2006
  4. 3/5 gitopt: convert setup_revisions() and friendsEric Wong, May 14, 2006
  5. 4/5 commit: allow --pretty= args to be abbreviatedEric Wong, May 14, 2006
  6. Junio C HamanoMay 14, 2006
  7. commit: allow --pretty= args to be abbreviatedEric Wong, May 15, 2006
  8. Junio C HamanoMay 15, 2006
  9. Eric WongMay 16, 2006
  10. 5/5 diff: parse U/u/unified options with an optional integer argEric Wong, May 14, 2006
  11. Linus TorvaldsMay 14, 2006
  12. Eric WongMay 14, 2006

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.