From d020e27fdaeb7d04247cdf5a0ff84d97626d5f5a Mon Sep 17 00:00:00 2001 From: Antoine Pelisse Date: Sat, 23 Feb 2013 17:48:45 +0100 Subject: [PATCH 1/3] diff: Fix rename pretty-print when suffix and prefix overlap 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". 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. Signed-off-by: Antoine Pelisse Signed-off-by: Junio C Hamano --- diff.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/diff.c b/diff.c index 9038f190ec..e1d82c9672 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--; From dd281f09b7eab86e4983b37d011cbfb0f593f6b8 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Tue, 26 Feb 2013 21:47:01 +0100 Subject: [PATCH 2/3] diff: prevent pprint_rename from underrunning input The logic described in d020e27 (diff: Fix rename pretty-print when suffix and prefix overlap, 2013-02-23) is wrong: The proof in the comment is valid only if both strings are the same length. *One* of old/new can reach a-1 (b-1, resp.) if 'a' is a suffix of 'b' (or vice versa). Since the intent was to let the loop run down to the '/' at the end of the common prefix, fix it by making that distinction explicit: if there is no prefix, allow no underrun. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- diff.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/diff.c b/diff.c index e1d82c9672..d641c2676f 100644 --- a/diff.c +++ b/diff.c @@ -1151,6 +1151,7 @@ static char *pprint_rename(const char *a, const char *b) const char *new = b; struct strbuf name = STRBUF_INIT; int pfx_length, sfx_length; + int pfx_adjust_for_slash; int len_a = strlen(a); int len_b = strlen(b); int a_midlen, b_midlen; @@ -1178,14 +1179,16 @@ static char *pprint_rename(const char *a, const char *b) new = b + len_b; sfx_length = 0; /* - * 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. + * If there is a common prefix, it must end in a slash. In + * that case we let this loop run 1 into the prefix to see the + * same slash. + * + * If there is no common prefix, we cannot do this as it would + * underrun the input strings. */ - while (a + pfx_length - 1 <= old && - b + pfx_length - 1 <= new && + pfx_adjust_for_slash = (pfx_length ? 1 : 0); + while (a + pfx_length - pfx_adjust_for_slash <= old && + b + pfx_length - pfx_adjust_for_slash <= new && *old == *new) { if (*old == '/') sfx_length = len_a - (old - a); From b174eb42d08eeb32ae7341ff46b05c20d38abc2b Mon Sep 17 00:00:00 2001 From: Antoine Pelisse Date: Wed, 6 Mar 2013 22:36:12 +0100 Subject: [PATCH 3/3] tests: make sure rename pretty print works Add basic use cases and corner cases tests for "git diff -M --summary/stat". Signed-off-by: Antoine Pelisse Signed-off-by: Junio C Hamano --- t/t4001-diff-rename.sh | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/t/t4001-diff-rename.sh b/t/t4001-diff-rename.sh index 844277cfa6..2f327b7495 100755 --- a/t/t4001-diff-rename.sh +++ b/t/t4001-diff-rename.sh @@ -102,4 +102,58 @@ test_expect_success 'setup for many rename source candidates' ' grep warning actual.err ' +test_expect_success 'rename pretty print with nothing in common' ' + mkdir -p a/b/ && + : >a/b/c && + git add a/b/c && + git commit -m "create a/b/c" && + mkdir -p c/b/ && + git mv a/b/c c/b/a && + git commit -m "a/b/c -> c/b/a" && + git diff -M --summary HEAD^ HEAD >output && + test_i18ngrep " a/b/c => c/b/a " output && + git diff -M --stat HEAD^ HEAD >output && + test_i18ngrep " a/b/c => c/b/a " output +' + +test_expect_success 'rename pretty print with common prefix' ' + mkdir -p c/d && + git mv c/b/a c/d/e && + git commit -m "c/b/a -> c/d/e" && + git diff -M --summary HEAD^ HEAD >output && + test_i18ngrep " c/{b/a => d/e} " output && + git diff -M --stat HEAD^ HEAD >output && + test_i18ngrep " c/{b/a => d/e} " output +' + +test_expect_success 'rename pretty print with common suffix' ' + mkdir d && + git mv c/d/e d/e && + git commit -m "c/d/e -> d/e" && + git diff -M --summary HEAD^ HEAD >output && + test_i18ngrep " {c/d => d}/e " output && + git diff -M --stat HEAD^ HEAD >output && + test_i18ngrep " {c/d => d}/e " output +' + +test_expect_success 'rename pretty print with common prefix and suffix' ' + mkdir d/f && + git mv d/e d/f/e && + git commit -m "d/e -> d/f/e" && + git diff -M --summary HEAD^ HEAD >output && + test_i18ngrep " d/{ => f}/e " output && + git diff -M --stat HEAD^ HEAD >output && + test_i18ngrep " d/{ => f}/e " output +' + +test_expect_success 'rename pretty print common prefix and suffix overlap' ' + mkdir d/f/f && + git mv d/f/e d/f/f/e && + git commit -m "d/f/e d/f/f/e" && + git diff -M --summary HEAD^ HEAD >output && + test_i18ngrep " d/f/{ => f}/e " output && + git diff -M --stat HEAD^ HEAD >output && + test_i18ngrep " d/f/{ => f}/e " output +' + test_done