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

[PATCH] diff: Fix rename pretty-print when suffix and prefix overlap

From
Antoine Pelisse <apelisse@gmail.com>
Date
Feb 28, 2013, 21:55 UTC
Message-ID
<1362088540-14564-1-git-send-email-apelisse@gmail.com>
In-Reply-To
<CALWbr2yviqF68zF7mBbhaXW7oFar0YRqROBWXwqjo7UNgZNVBQ@mail.gmail.com>

When considering a rename for two files that have a suffix and a prefix that can overlap, a confusing line is shown. As an example, renaming "a/b/b/c" to "a/b/c" shows "a/b/{ => }/b/c", instead of "a/b/{b => }/c"

Currently, what we do is calculate the common prefix ("a/b/"), and the common suffix ("/b/c"), but the same "/b/" is actually counted both in prefix and suffix. Then when calculating the size of the non-common part, we end-up with a negative value which is reset to 0, thus the "{ => }".

Do not allow the common suffix to overlap the common prefix and stop when reaching a "/" that would be in both.

Also add some test file to place corner-cases we could met (and this one) with rename pretty print.

Signed-off-by: Antoine Pelisse <apelisse@gmail.com>
---
 diff.c                   |   11 +++++++++-
 t/t4056-rename-pretty.sh |   54 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100755 t/t4056-rename-pretty.sh
diff --git a/diff.c b/diff.c
index 9038f19..e1d82c9 100644
--- a/diff.c
+++ b/diff.c
@@ -1177,7 +1177,16 @@ static char *pprint_rename(const char *a, const char *b)
 	old = a + len_a;
 	new = b + len_b;
 	sfx_length = 0;
-	while (a <= old && b <= new && *old == *new) {
+	/*
+	 * Note:
+	 * if pfx_length is 0, old/new will never reach a - 1 because it
+	 * would mean the whole string is common suffix. But then, the
+	 * whole string would also be a common prefix, and we would not
+	 * have pfx_length equals 0.
+	 */
+	while (a + pfx_length - 1 <= old &&
+	       b + pfx_length - 1 <= new &&
+	       *old == *new) {
 		if (*old == '/')
 			sfx_length = len_a - (old - a);
 		old--;
diff --git a/t/t4056-rename-pretty.sh b/t/t4056-rename-pretty.sh
new file mode 100755
index 0000000..806046f
--- /dev/null
+++ b/t/t4056-rename-pretty.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+test_description='Rename pretty print
+
+'
+
+. ./test-lib.sh
+
+test_expect_success nothing_common '
+	mkdir -p a/b/ &&
+	: >a/b/c &&
+	git add a/b/c &&
+	git commit -m. &&
+	mkdir -p c/b/ &&
+	git mv a/b/c c/b/a &&
+	git commit -m. &&
+	git show -M --summary >output &&
+	test_i18ngrep "a/b/c => c/b/a" output
+'
+
+test_expect_success common_prefix '
+	mkdir -p c/d &&
+	git mv c/b/a c/d/e &&
+	git commit -m. &&
+	git show -M --summary >output &&
+	test_i18ngrep "c/{b/a => d/e}" output
+'
+
+test_expect_success common_suffix '
+	mkdir d &&
+	git mv c/d/e d/e &&
+	git commit -m. &&
+	git show -M --summary >output &&
+	test_i18ngrep "{c/d => d}/e" output
+'
+
+test_expect_success common_suffix_prefix '
+	mkdir d/f &&
+	git mv d/e d/f/e &&
+	git commit -m. &&
+	git show -M --summary >output &&
+	test_i18ngrep "d/{ => f}/e" output
+'
+
+test_expect_success common_overlap '
+	mkdir d/f/f &&
+	git mv d/f/e d/f/f/e &&
+	git commit -m. &&
+	git show -M --summary >output &&
+	test_i18ngrep "d/f/{ => f}/e" output
+'
+
+
+test_done
--
1.7.9.5
Previous: Antoine PelisseNext: Thomas Rast
Message 6 of 16 in “diff: Fix rename pretty-print when suffix and prefix overlap”
  1. diff: Fix rename pretty-print when suffix and prefix overlapAntoine Pelisse, Feb 23, 2013
  2. Junio C HamanoFeb 24, 2013
  3. Antoine PelisseFeb 25, 2013
  4. Philip OakleyFeb 25, 2013
  5. Antoine PelisseFeb 25, 2013
  6. diff: Fix rename pretty-print when suffix and prefix overlapAntoine Pelisse, Feb 28, 2013
  7. Thomas RastFeb 28, 2013
  8. Antoine PelisseFeb 28, 2013
  9. Junio C HamanoFeb 28, 2013
  10. tests: make sure rename pretty print worksAntoine Pelisse, Mar 2, 2013
  11. Junio C HamanoMar 3, 2013
  12. tests: make sure rename pretty print worksAntoine Pelisse, Mar 6, 2013
  13. Junio C HamanoMar 6, 2013
  14. diff: prevent pprint_rename from underrunning inputThomas Rast, Feb 26, 2013
  15. Junio C HamanoFeb 26, 2013
  16. Antoine PelisseFeb 27, 2013

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.