Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit f3999e0

Browse files
bpowersgitster
authored andcommitted
diff --no-index: reset temporary buffer lengths on directory iteration
Commit 875b91b (diff --no-index: use strbuf for temporary pathnames, 2012-04-25) introduced a regression when using diff --no-index with directories. When iterating through a directory, the switch to strbuf from heap-allocated char arrays caused paths to form like 'dir/file1', 'dir/file1file2', rather than 'dir/file1', 'dir/file2' as expected. Avoid this by resetting the paths variables to their original length before each iteration. Signed-off-by: Bobby Powers <[email protected]> Reviewed-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 875b91b commit f3999e0

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

diff-no-index.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static int queue_diff(struct diff_options *o,
6868
struct string_list p1 = STRING_LIST_INIT_DUP;
6969
struct string_list p2 = STRING_LIST_INIT_DUP;
7070
int i1, i2, ret = 0;
71+
size_t len1 = 0, len2 = 0;
7172

7273
if (name1 && read_directory(name1, &p1))
7374
return -1;
@@ -80,18 +81,23 @@ static int queue_diff(struct diff_options *o,
8081
strbuf_addstr(&buffer1, name1);
8182
if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/')
8283
strbuf_addch(&buffer1, '/');
84+
len1 = buffer1.len;
8385
}
8486

8587
if (name2) {
8688
strbuf_addstr(&buffer2, name2);
8789
if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
8890
strbuf_addch(&buffer2, '/');
91+
len2 = buffer2.len;
8992
}
9093

9194
for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
9295
const char *n1, *n2;
9396
int comp;
9497

98+
strbuf_setlen(&buffer1, len1);
99+
strbuf_setlen(&buffer2, len2);
100+
95101
if (i1 == p1.nr)
96102
comp = 1;
97103
else if (i2 == p2.nr)

t/t4053-diff-no-index.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
test_description='diff --no-index'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup' '
8+
mkdir a &&
9+
mkdir b &&
10+
echo 1 >a/1 &&
11+
echo 2 >a/2
12+
'
13+
14+
test_expect_success 'git diff --no-index directories' '
15+
git diff --no-index a b >cnt
16+
test $? = 1 && test_line_count = 14 cnt
17+
'
18+
19+
test_done

0 commit comments

Comments
 (0)