Skip to content

Commit 8f780ca

Browse files
committed
Merge branch 'ap/maint-diff-rename-avoid-overlap' into maint-1.8.1
* ap/maint-diff-rename-avoid-overlap: tests: make sure rename pretty print works diff: prevent pprint_rename from underrunning input diff: Fix rename pretty-print when suffix and prefix overlap
2 parents 0311e37 + b174eb4 commit 8f780ca

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

diff.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ static char *pprint_rename(const char *a, const char *b)
12561256
const char *new = b;
12571257
struct strbuf name = STRBUF_INIT;
12581258
int pfx_length, sfx_length;
1259+
int pfx_adjust_for_slash;
12591260
int len_a = strlen(a);
12601261
int len_b = strlen(b);
12611262
int a_midlen, b_midlen;
@@ -1282,7 +1283,18 @@ static char *pprint_rename(const char *a, const char *b)
12821283
old = a + len_a;
12831284
new = b + len_b;
12841285
sfx_length = 0;
1285-
while (a <= old && b <= new && *old == *new) {
1286+
/*
1287+
* If there is a common prefix, it must end in a slash. In
1288+
* that case we let this loop run 1 into the prefix to see the
1289+
* same slash.
1290+
*
1291+
* If there is no common prefix, we cannot do this as it would
1292+
* underrun the input strings.
1293+
*/
1294+
pfx_adjust_for_slash = (pfx_length ? 1 : 0);
1295+
while (a + pfx_length - pfx_adjust_for_slash <= old &&
1296+
b + pfx_length - pfx_adjust_for_slash <= new &&
1297+
*old == *new) {
12861298
if (*old == '/')
12871299
sfx_length = len_a - (old - a);
12881300
old--;

t/t4001-diff-rename.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,58 @@ test_expect_success 'setup for many rename source candidates' '
102102
grep warning actual.err
103103
'
104104

105+
test_expect_success 'rename pretty print with nothing in common' '
106+
mkdir -p a/b/ &&
107+
: >a/b/c &&
108+
git add a/b/c &&
109+
git commit -m "create a/b/c" &&
110+
mkdir -p c/b/ &&
111+
git mv a/b/c c/b/a &&
112+
git commit -m "a/b/c -> c/b/a" &&
113+
git diff -M --summary HEAD^ HEAD >output &&
114+
test_i18ngrep " a/b/c => c/b/a " output &&
115+
git diff -M --stat HEAD^ HEAD >output &&
116+
test_i18ngrep " a/b/c => c/b/a " output
117+
'
118+
119+
test_expect_success 'rename pretty print with common prefix' '
120+
mkdir -p c/d &&
121+
git mv c/b/a c/d/e &&
122+
git commit -m "c/b/a -> c/d/e" &&
123+
git diff -M --summary HEAD^ HEAD >output &&
124+
test_i18ngrep " c/{b/a => d/e} " output &&
125+
git diff -M --stat HEAD^ HEAD >output &&
126+
test_i18ngrep " c/{b/a => d/e} " output
127+
'
128+
129+
test_expect_success 'rename pretty print with common suffix' '
130+
mkdir d &&
131+
git mv c/d/e d/e &&
132+
git commit -m "c/d/e -> d/e" &&
133+
git diff -M --summary HEAD^ HEAD >output &&
134+
test_i18ngrep " {c/d => d}/e " output &&
135+
git diff -M --stat HEAD^ HEAD >output &&
136+
test_i18ngrep " {c/d => d}/e " output
137+
'
138+
139+
test_expect_success 'rename pretty print with common prefix and suffix' '
140+
mkdir d/f &&
141+
git mv d/e d/f/e &&
142+
git commit -m "d/e -> d/f/e" &&
143+
git diff -M --summary HEAD^ HEAD >output &&
144+
test_i18ngrep " d/{ => f}/e " output &&
145+
git diff -M --stat HEAD^ HEAD >output &&
146+
test_i18ngrep " d/{ => f}/e " output
147+
'
148+
149+
test_expect_success 'rename pretty print common prefix and suffix overlap' '
150+
mkdir d/f/f &&
151+
git mv d/f/e d/f/f/e &&
152+
git commit -m "d/f/e d/f/f/e" &&
153+
git diff -M --summary HEAD^ HEAD >output &&
154+
test_i18ngrep " d/f/{ => f}/e " output &&
155+
git diff -M --stat HEAD^ HEAD >output &&
156+
test_i18ngrep " d/f/{ => f}/e " output
157+
'
158+
105159
test_done

0 commit comments

Comments
 (0)