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

[PATCH 1/3] textconv: refactor calls to run_textconv

From
Jeff King <peff@peff.net>
Date
Mar 28, 2010, 14:53 UTC
Message-ID
<20100328145342.GA27970@coredump.intra.peff.net>
In-Reply-To
<20100328145301.GA26213@coredump.intra.peff.net>

This patch adds a fill_textconv wrapper, which centralizes some minor logic like error checking and handling the case of no-textconv.

In addition to dropping the number of lines, this will make it easier in future patches to handle multiple types of textconv.

Signed-off-by: Jeff King <peff@peff.net>
---
 diff.c |   58 ++++++++++++++++++++++++----------------------------------
 1 files changed, 24 insertions(+), 34 deletions(-)
diff --git a/diff.c b/diff.c
index f5d93e9..3ddc05e 100644
--- a/diff.c
+++ b/diff.c
@@ -43,7 +43,8 @@ static char diff_colors[][COLOR_MAXLEN] = {
 };
 
 static void diff_filespec_load_driver(struct diff_filespec *one);
-static char *run_textconv(const char *, struct diff_filespec *, size_t *);
+static size_t fill_textconv(const char *cmd,
+		struct diff_filespec *df, char **outbuf);
 
 static int parse_diff_color_slot(const char *var, int ofs)
 {
@@ -477,7 +478,7 @@ static void emit_rewrite_diff(const char *name_a,
 	const char *reset = diff_get_color(color_diff, DIFF_RESET);
 	static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
 	const char *a_prefix, *b_prefix;
-	const char *data_one, *data_two;
+	char *data_one, *data_two;
 	size_t size_one, size_two;
 	struct emit_callback ecbdata;
 
@@ -501,24 +502,8 @@ static void emit_rewrite_diff(const char *name_a,
 
 	diff_populate_filespec(one, 0);
 	diff_populate_filespec(two, 0);
-	if (textconv_one) {
-		data_one = run_textconv(textconv_one, one, &size_one);
-		if (!data_one)
-			die("unable to read files to diff");
-	}
-	else {
-		data_one = one->data;
-		size_one = one->size;
-	}
-	if (textconv_two) {
-		data_two = run_textconv(textconv_two, two, &size_two);
-		if (!data_two)
-			die("unable to read files to diff");
-	}
-	else {
-		data_two = two->data;
-		size_two = two->size;
-	}
+	size_one = fill_textconv(textconv_one, one, &data_one);
+	size_two = fill_textconv(textconv_two, two, &data_two);
 
 	memset(&ecbdata, 0, sizeof(ecbdata));
 	ecbdata.color_diff = color_diff;
@@ -1713,20 +1698,8 @@ static void builtin_diff(const char *name_a,
 			strbuf_reset(&header);
 		}
 
-		if (textconv_one) {
-			size_t size;
-			mf1.ptr = run_textconv(textconv_one, one, &size);
-			if (!mf1.ptr)
-				die("unable to read files to diff");
-			mf1.size = size;
-		}
-		if (textconv_two) {
-			size_t size;
-			mf2.ptr = run_textconv(textconv_two, two, &size);
-			if (!mf2.ptr)
-				die("unable to read files to diff");
-			mf2.size = size;
-		}
+		mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
+		mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
 
 		pe = diff_funcname_pattern(one);
 		if (!pe)
@@ -3907,3 +3880,20 @@ static char *run_textconv(const char *pgm, struct diff_filespec *spec,
 
 	return strbuf_detach(&buf, outsize);
 }
+
+static size_t fill_textconv(const char *cmd,
+			    struct diff_filespec *df,
+			    char **outbuf)
+{
+	size_t size;
+
+	if (!cmd) {
+		*outbuf = df->data;
+		return df->size;
+	}
+
+	*outbuf = run_textconv(cmd, df, &size);
+	if (!*outbuf)
+		die("unable to read files to diff");
+	return size;
+}
-- 
1.7.0.3.294.g8e730.dirty
Previous: Jeff KingNext: Jeff King
Message 2 of 19 in “fast textconv”
  1. 0/3 fast textconvJeff King, Mar 28, 2010
  2. 1/3 textconv: refactor calls to run_textconvJeff King, Mar 28, 2010
  3. 2/3 textconv: refactor to handle multiple textconv typesJeff King, Mar 28, 2010
  4. 3/3 diff: add "fasttextconv" config optionJeff King, Mar 28, 2010
  5. Johannes SixtMar 28, 2010
  6. Jeff KingMar 30, 2010
  7. diff: fix textconv error zombiesJohannes Sixt, Mar 30, 2010
  8. Junio C HamanoMar 30, 2010
  9. Johannes SixtMar 30, 2010
  10. Jeff KingMar 30, 2010
  11. Michael J GruberMar 28, 2010
  12. Jeff KingMar 28, 2010
  13. Jeff KingMar 28, 2010
  14. Jeff KingMar 28, 2010
  15. Jeff KingMar 28, 2010
  16. Sverre RabbelierMar 28, 2010
  17. Jeff KingMar 30, 2010
  18. Junio C HamanoMar 30, 2010
  19. Jeff KingMar 30, 2010

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.