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

[PATCH 2/3] diff.c: separate "prefix" from RELATIVE_NAME (aka --relative)

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

So far "prefix" and "prefix_length" in struct diff_options are tied to RELATIVE_NAME flag, making it tricky to add new features that need the prefix.

This change essentially follows the foot steps in cd676a5 (diff --relative: output paths as relative to the current subdirectory - 2008-02-12) and makes sure that the prefix stripping/filtering only happens when RELATIVE_NAME flag is set. The stripping is much simpler because all stripping now goes through strip_prefix(). So the patch is mostly about filtering.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 diff.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/diff.c b/diff.c
index b4949a2..bd5d190 100644
--- a/diff.c
+++ b/diff.c
@@ -3153,7 +3153,7 @@ static void strip_prefix(struct diff_options *opt,
 {
 	int prefix_length = opt->prefix_length;
 
-	if (!prefix_length)
+	if (!prefix_length || !DIFF_OPT_TST(opt, RELATIVE_NAME))
 		return;
 
 	/* Strip the prefix but do not molest /dev/null and absolute paths */
@@ -3335,8 +3335,6 @@ void diff_setup_done(struct diff_options *options)
 	if (DIFF_OPT_TST(options, FIND_COPIES_HARDER))
 		options->detect_rename = DIFF_DETECT_COPY;
 
-	if (!DIFF_OPT_TST(options, RELATIVE_NAME))
-		options->prefix = NULL;
 	if (options->prefix)
 		options->prefix_length = strlen(options->prefix);
 	else
@@ -4976,7 +4974,8 @@ void diff_addremove(struct diff_options *options,
 		addremove = (addremove == '+' ? '-' :
 			     addremove == '-' ? '+' : addremove);
 
-	if (options->prefix &&
+	if (DIFF_OPT_TST(options, RELATIVE_NAME) &&
+	    options->prefix &&
 	    strncmp(concatpath, options->prefix, options->prefix_length))
 		return;
 
@@ -5021,7 +5020,8 @@ void diff_change(struct diff_options *options,
 			new_dirty_submodule = tmp;
 	}
 
-	if (options->prefix &&
+	if (DIFF_OPT_TST(options, RELATIVE_NAME) &&
+	    options->prefix &&
 	    strncmp(concatpath, options->prefix, options->prefix_length))
 		return;
 
@@ -5048,7 +5048,8 @@ struct diff_filepair *diff_unmerge(struct diff_options *options, const char *pat
 	struct diff_filepair *pair;
 	struct diff_filespec *one, *two;
 
-	if (options->prefix &&
+	if (DIFF_OPT_TST(options, RELATIVE_NAME) &&
+	    options->prefix &&
 	    strncmp(path, options->prefix, options->prefix_length))
 		return NULL;
 
-- 
2.8.2.531.gd073806
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 3 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.