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 <bobbypowers@gmail.com> Reviewed-by: René Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bobby Powers committed May 16, 2012 at 10:28 UTC f3999e03274df6b98a98a32912f5e171d6eea35f
2 files changed +25
diff-no-index.c
+6
@@ -68,6 +68,7 @@ static int queue_diff(struct diff_options *o,
68 struct string_list p1 = STRING_LIST_INIT_DUP;
69 struct string_list p2 = STRING_LIST_INIT_DUP;
70 int i1, i2, ret = 0;
71 + size_t len1 = 0, len2 = 0;
72
73 if (name1 && read_directory(name1, &p1))
74 return -1;
@@ -80,18 +81,23 @@ static int queue_diff(struct diff_options *o,
81 strbuf_addstr(&buffer1, name1);
82 if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/')
83 strbuf_addch(&buffer1, '/');
84 + len1 = buffer1.len;
85 }
86
87 if (name2) {
88 strbuf_addstr(&buffer2, name2);
89 if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
90 strbuf_addch(&buffer2, '/');
91 + len2 = buffer2.len;
92 }
93
94 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
95 const char *n1, *n2;
96 int comp;
97
98 + strbuf_setlen(&buffer1, len1);
99 + strbuf_setlen(&buffer2, len2);
100 +
101 if (i1 == p1.nr)
102 comp = 1;
103 else if (i2 == p2.nr)
t/t4053-diff-no-index.sh new
+19
@@ -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