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

[PATCH 1/3] diff.c: refactor strip_prefix()

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 26, 2016, 17:16 UTC
Message-ID
<20160626171616.27948-2-pclouds@gmail.com>
In-Reply-To
<20160626171616.27948-1-pclouds@gmail.com>

By passing "struct diff_options *" to strip_prefix(), we can do some more intelligent and repeated logic at one place. The removal of "if (opt->prefix_length)" is just the beginning.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 diff.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
diff --git a/diff.c b/diff.c
index fa78fc1..b4949a2 100644
--- a/diff.c
+++ b/diff.c
@@ -3147,8 +3147,15 @@ static void diff_fill_sha1_info(struct diff_filespec *one)
 		hashclr(one->sha1);
 }
 
-static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
+static void strip_prefix(struct diff_options *opt,
+			 const char **namep,
+			 const char **otherp)
 {
+	int prefix_length = opt->prefix_length;
+
+	if (!prefix_length)
+		return;
+
 	/* Strip the prefix but do not molest /dev/null and absolute paths */
 	if (*namep && **namep != '/') {
 		*namep += prefix_length;
@@ -3175,8 +3182,7 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
 	name  = p->one->path;
 	other = (strcmp(name, p->two->path) ? p->two->path : NULL);
 	attr_path = name;
-	if (o->prefix_length)
-		strip_prefix(o->prefix_length, &name, &other);
+	strip_prefix(o, &name, &other);
 
 	if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
 		pgm = NULL;
@@ -3230,8 +3236,7 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
 	name = p->one->path;
 	other = (strcmp(name, p->two->path) ? p->two->path : NULL);
 
-	if (o->prefix_length)
-		strip_prefix(o->prefix_length, &name, &other);
+	strip_prefix(o, &name, &other);
 
 	diff_fill_sha1_info(p->one);
 	diff_fill_sha1_info(p->two);
@@ -3254,8 +3259,7 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
 	other = (strcmp(name, p->two->path) ? p->two->path : NULL);
 	attr_path = other ? other : name;
 
-	if (o->prefix_length)
-		strip_prefix(o->prefix_length, &name, &other);
+	strip_prefix(o, &name, &other);
 
 	diff_fill_sha1_info(p->one);
 	diff_fill_sha1_info(p->two);
@@ -4133,14 +4137,14 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
 		const char *name_a, *name_b;
 		name_a = p->one->path;
 		name_b = p->two->path;
-		strip_prefix(opt->prefix_length, &name_a, &name_b);
+		strip_prefix(opt, &name_a, &name_b);
 		write_name_quoted(name_a, opt->file, inter_name_termination);
 		write_name_quoted(name_b, opt->file, line_termination);
 	} else {
 		const char *name_a, *name_b;
 		name_a = p->one->mode ? p->one->path : p->two->path;
 		name_b = NULL;
-		strip_prefix(opt->prefix_length, &name_a, &name_b);
+		strip_prefix(opt, &name_a, &name_b);
 		write_name_quoted(name_a, opt->file, line_termination);
 	}
 }
@@ -4345,7 +4349,7 @@ static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
 		const char *name_a, *name_b;
 		name_a = p->two->path;
 		name_b = NULL;
-		strip_prefix(opt->prefix_length, &name_a, &name_b);
+		strip_prefix(opt, &name_a, &name_b);
 		write_name_quoted(name_a, opt->file, opt->line_termination);
 	}
 }
-- 
2.8.2.531.gd073806
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 2 of 9 in “diff: add --relative-names”
  1. 0/3 diff: add --relative-namesNguyễn Thái Ngọc Duy, Jun 26, 2016
  2. 1/3 diff.c: refactor strip_prefix()Nguyễn Thái Ngọc Duy, Jun 26, 2016
  3. 2/3 diff.c: separate "prefix" from RELATIVE_NAME (aka --relative)Nguyễn Thái Ngọc Duy, Jun 26, 2016
  4. 3/3 diff.c: add --relative-names to be used with --name-onlyNguyễn Thái Ngọc Duy, Jun 26, 2016
  5. Eric SunshineJun 26, 2016
  6. Jeff KingJun 27, 2016
  7. Duy NguyenJun 27, 2016
  8. Jeff KingJun 27, 2016
  9. Duy NguyenJun 27, 2016

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.