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

[PATCH 5/6] range-diff: move the diffopt initialization down one layer

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 4, 2021, 20:07 UTC
Message-ID
<9fa945db5f135d3b829aef70da490184f898e276.1612469275.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.869.git.1612469275.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

It is actually only the `output()` function that uses those diffopts. By moving the diffopt initialization down into that function, it is encapsulated better.

Incidentally, it will also make it easier to implement the `--left-only` and `--right-only` options in `git range-diff` because the `output()` function is now receiving all range-diff options as a parameter, not just the diffopts.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 range-diff.c | 64 +++++++++++++++++++++++++---------------------------
 1 file changed, 31 insertions(+), 33 deletions(-)
diff --git a/range-diff.c b/range-diff.c
index 58528c43a3e8..5455cbb9521b 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -464,12 +464,35 @@ static void patch_diff(const char *a, const char *b,
 	diff_flush(diffopt);
 }
 
+static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
+{
+	return data;
+}
+
 static void output(struct string_list *a, struct string_list *b,
-		   struct diff_options *diffopt)
+		   struct range_diff_options *range_diff_opts)
 {
 	struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
 	int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
 	int i = 0, j = 0;
+	struct diff_options opts;
+	struct strbuf indent = STRBUF_INIT;
+
+	if (range_diff_opts->diffopt)
+		memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
+	else
+		diff_setup(&opts);
+
+	if (!opts.output_format)
+		opts.output_format = DIFF_FORMAT_PATCH;
+	opts.flags.suppress_diff_headers = 1;
+	opts.flags.dual_color_diffed_diffs =
+		range_diff_opts->dual_color;
+	opts.flags.suppress_hunk_header_line_count = 1;
+	opts.output_prefix = output_prefix_cb;
+	strbuf_addstr(&indent, "    ");
+	opts.output_prefix_data = &indent;
+	diff_setup_done(&opts);
 
 	/*
 	 * We assume the user is really more interested in the second argument
@@ -490,7 +513,7 @@ static void output(struct string_list *a, struct string_list *b,
 
 		/* Show unmatched LHS commit whose predecessors were shown. */
 		if (i < a->nr && a_util->matching < 0) {
-			output_pair_header(diffopt, patch_no_width,
+			output_pair_header(&opts, patch_no_width,
 					   &buf, &dashes, a_util, NULL);
 			i++;
 			continue;
@@ -498,7 +521,7 @@ static void output(struct string_list *a, struct string_list *b,
 
 		/* Show unmatched RHS commits. */
 		while (j < b->nr && b_util->matching < 0) {
-			output_pair_header(diffopt, patch_no_width,
+			output_pair_header(&opts, patch_no_width,
 					   &buf, &dashes, NULL, b_util);
 			b_util = ++j < b->nr ? b->items[j].util : NULL;
 		}
@@ -506,22 +529,18 @@ static void output(struct string_list *a, struct string_list *b,
 		/* Show matching LHS/RHS pair. */
 		if (j < b->nr) {
 			a_util = a->items[b_util->matching].util;
-			output_pair_header(diffopt, patch_no_width,
+			output_pair_header(&opts, patch_no_width,
 					   &buf, &dashes, a_util, b_util);
-			if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
+			if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
 				patch_diff(a->items[b_util->matching].string,
-					   b->items[j].string, diffopt);
+					   b->items[j].string, &opts);
 			a_util->shown = 1;
 			j++;
 		}
 	}
 	strbuf_release(&buf);
 	strbuf_release(&dashes);
-}
-
-static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
-{
-	return data;
+	strbuf_release(&indent);
 }
 
 int show_range_diff(const char *range1, const char *range2,
@@ -538,31 +557,10 @@ int show_range_diff(const char *range1, const char *range2,
 		res = error(_("could not parse log for '%s'"), range2);
 
 	if (!res) {
-		struct diff_options opts;
-		struct strbuf indent = STRBUF_INIT;
-
-		if (range_diff_opts->diffopt)
-			memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
-		else
-			diff_setup(&opts);
-
-		if (!opts.output_format)
-			opts.output_format = DIFF_FORMAT_PATCH;
-		opts.flags.suppress_diff_headers = 1;
-		opts.flags.dual_color_diffed_diffs =
-			range_diff_opts->dual_color;
-		opts.flags.suppress_hunk_header_line_count = 1;
-		opts.output_prefix = output_prefix_cb;
-		strbuf_addstr(&indent, "    ");
-		opts.output_prefix_data = &indent;
-		diff_setup_done(&opts);
-
 		find_exact_matches(&branch1, &branch2);
 		get_correspondences(&branch1, &branch2,
 				    range_diff_opts->creation_factor);
-		output(&branch1, &branch2, &opts);
-
-		strbuf_release(&indent);
+		output(&branch1, &branch2, range_diff_opts);
 	}
 
 	string_list_clear(&branch1, 1);
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Junio C Hamano
Message 9 of 22 in “Optionally restrict range-diff output to "left" or "right" range only”
  1. 0/6 Optionally restrict range-diff output to "left" or "right" range onlyJohannes Schindelin via GitGitGadget, Feb 4, 2021
  2. 3/6 range-diff: simplify code spawning `git log`Johannes Schindelin via GitGitGadget, Feb 4, 2021
  3. 1/6 range-diff: avoid leaking memory in two error code pathsJohannes Schindelin via GitGitGadget, Feb 4, 2021
  4. 2/6 range-diff: libify the read_patches() function againJohannes Schindelin via GitGitGadget, Feb 4, 2021
  5. 4/6 range-diff: combine all options in a single data structureJohannes Schindelin via GitGitGadget, Feb 4, 2021
  6. Eric SunshineFeb 4, 2021
  7. Johannes SchindelinFeb 5, 2021
  8. 6/6 range-diff: offer --left-only/--right-only optionsJohannes Schindelin via GitGitGadget, Feb 4, 2021
  9. 5/6 range-diff: move the diffopt initialization down one layerJohannes Schindelin via GitGitGadget, Feb 4, 2021
  10. Junio C HamanoFeb 4, 2021
  11. Taylor BlauFeb 4, 2021
  12. Junio C HamanoFeb 5, 2021
  13. Jeff KingFeb 5, 2021
  14. Johannes SchindelinFeb 8, 2021
  15. Taylor BlauFeb 5, 2021
  16. 0/6 Optionally restrict range-diff output to "left" or "right" range onlyJohannes Schindelin via GitGitGadget, Feb 5, 2021
  17. 3/6 range-diff: simplify code spawning `git log`Johannes Schindelin via GitGitGadget, Feb 5, 2021
  18. 5/6 range-diff: move the diffopt initialization down one layerJohannes Schindelin via GitGitGadget, Feb 5, 2021
  19. 2/6 range-diff: libify the read_patches() function againJohannes Schindelin via GitGitGadget, Feb 5, 2021
  20. 6/6 range-diff: offer --left-only/--right-only optionsJohannes Schindelin via GitGitGadget, Feb 5, 2021
  21. 1/6 range-diff: avoid leaking memory in two error code pathsJohannes Schindelin via GitGitGadget, Feb 5, 2021
  22. 4/6 range-diff: combine all options in a single data structureJohannes Schindelin via GitGitGadget, Feb 5, 2021

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.