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

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sun, 26 Jun 2016 19:16:14 +0200

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 <[email-masked]>
---
 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