@matheusbsbs / matheus-bagliano / commits / ab1630a3ed
Fork 0

Fix wrong filename listing bug in git-ls-tree.

This patch fixes a bug in git-ls-tree in which the wrong filenames are listed if the exact same file and directory contents are present in another location in the tree. Added a new series of test cases for directory and filename handling. Signed-off-by: Robert Fitzsimons <robfitz@273k.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

robfitz@273k.net committed Oct 7, 2005 at 16:54 UTC ab1630a3ed31d6ec5ae83769c78ed542fe3c4a28
2 files changed +189 -26
ls-tree.c
+29 -26
@@ -54,11 +54,13 @@ static int prepare_children(struct tree_entry_list *elem)
54 return 0;
55 }
56
57 -static struct tree_entry_list *find_entry(const char *path)
57 +static struct tree_entry_list *find_entry(const char *path, char *pathbuf)
58 {
59 const char *next, *slash;
60 int len;
61 - struct tree_entry_list *elem = &root_entry;
61 + struct tree_entry_list *elem = &root_entry, *oldelem = NULL;
62 +
63 + *(pathbuf) = '\0';
64
65 /* Find tree element, descending from root, that
66 * corresponds to the named path, lazily expanding
@@ -86,6 +88,10 @@ static struct tree_entry_list *find_entry(const char *path)
88 len = slash - path;
89 }
90 if (len) {
91 + if (oldelem) {
92 + pathbuf += sprintf(pathbuf, "%s/", oldelem->name);
93 + }
94 +
95 /* (len == 0) if the original path was "drivers/char/"
96 * and we have run already two rounds, having elem
97 * pointing at the drivers/char directory.
@@ -101,6 +107,8 @@ static struct tree_entry_list *find_entry(const char *path)
107 }
108 if (!elem)
109 return NULL;
110 +
111 + oldelem = elem;
112 }
113 path = next;
114 }
@@ -108,19 +116,6 @@ static struct tree_entry_list *find_entry(const char *path)
116 return elem;
117 }
118
111 -static void show_entry_name(struct tree_entry_list *e)
112 -{
113 - /* This is yucky. The root level is there for
114 - * our convenience but we really want to do a
115 - * forest.
116 - */
117 - if (e->parent && e->parent != &root_entry) {
118 - show_entry_name(e->parent);
119 - putchar('/');
120 - }
121 - printf("%s", e->name);
122 -}
123 -
119 static const char *entry_type(struct tree_entry_list *e)
120 {
121 return (e->directory ? "tree" : "blob");
@@ -134,28 +129,35 @@ static const char *entry_hex(struct tree_entry_list *e)
129 }
130
131 /* forward declaration for mutually recursive routines */
137 -static int show_entry(struct tree_entry_list *, int);
132 +static int show_entry(struct tree_entry_list *, int, char *pathbuf);
133
139 -static int show_children(struct tree_entry_list *e, int level)
134 +static int show_children(struct tree_entry_list *e, int level, char *pathbuf)
135 {
136 + int oldlen = strlen(pathbuf);
137 +
138 + if (e != &root_entry)
139 + sprintf(pathbuf + oldlen, "%s/", e->name);
140 +
141 if (prepare_children(e))
142 die("internal error: ls-tree show_children called with non tree");
143 e = e->item.tree->entries;
144 while (e) {
145 - show_entry(e, level);
145 + show_entry(e, level, pathbuf);
146 e = e->next;
147 }
148 +
149 + pathbuf[oldlen] = '\0';
150 +
151 return 0;
152 }
153
151 -static int show_entry(struct tree_entry_list *e, int level)
154 +static int show_entry(struct tree_entry_list *e, int level, char *pathbuf)
155 {
156 int err = 0;
157
158 if (e != &root_entry) {
156 - printf("%06o %s %s ", e->mode, entry_type(e),
157 - entry_hex(e));
158 - show_entry_name(e);
159 + printf("%06o %s %s %s%s", e->mode, entry_type(e),
160 + entry_hex(e), pathbuf, e->name);
161 putchar(line_termination);
162 }
163
@@ -176,10 +178,10 @@ static int show_entry(struct tree_entry_list *e, int level)
178 */
179 if (level == 0 && !(ls_options & LS_TREE_ONLY))
180 /* case (1)-a and (1)-b */
179 - err = err | show_children(e, level+1);
181 + err = err | show_children(e, level+1, pathbuf);
182 else if (level && ls_options & LS_RECURSIVE)
183 /* case (2)-b */
182 - err = err | show_children(e, level+1);
184 + err = err | show_children(e, level+1, pathbuf);
185 }
186 return err;
187 }
@@ -187,7 +189,8 @@ static int show_entry(struct tree_entry_list *e, int level)
189 static int list_one(const char *path)
190 {
191 int err = 0;
190 - struct tree_entry_list *e = find_entry(path);
192 + char pathbuf[MAXPATHLEN + 1];
193 + struct tree_entry_list *e = find_entry(path, pathbuf);
194 if (!e) {
195 /* traditionally ls-tree does not complain about
196 * missing path. We may change this later to match
@@ -195,7 +198,7 @@ static int list_one(const char *path)
198 */
199 return err;
200 }
198 - err = err | show_entry(e, 0);
201 + err = err | show_entry(e, 0, pathbuf);
202 return err;
203 }
204
t/t3101-ls-tree-dirname.sh new
+160
@@ -0,0 +1,160 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2005 Junio C Hamano
4 +# Copyright (c) 2005 Robert Fitzsimons
5 +#
6 +
7 +test_description='git-ls-tree directory and filenames handling.
8 +
9 +This test runs git-ls-tree with the following in a tree.
10 +
11 + 1.txt - a file
12 + 2.txt - a file
13 + path0/a/b/c/1.txt - a file in a directory
14 + path1/b/c/1.txt - a file in a directory
15 + path2/1.txt - a file in a directory
16 + path3/1.txt - a file in a directory
17 + path3/2.txt - a file in a directory
18 +
19 +Test the handling of mulitple directories which have matching file
20 +entries. Also test odd filename and missing entries handling.
21 +'
22 +. ./test-lib.sh
23 +
24 +test_expect_success \
25 + 'setup' \
26 + 'echo 111 >1.txt &&
27 + echo 222 >2.txt &&
28 + mkdir path0 path0/a path0/a/b path0/a/b/c &&
29 + echo 111 >path0/a/b/c/1.txt &&
30 + mkdir path1 path1/b path1/b/c &&
31 + echo 111 >path1/b/c/1.txt &&
32 + mkdir path2 &&
33 + echo 111 >path2/1.txt &&
34 + mkdir path3 &&
35 + echo 111 >path3/1.txt &&
36 + echo 222 >path3/2.txt &&
37 + find *.txt path* \( -type f -o -type l \) -print |
38 + xargs git-update-index --add &&
39 + tree=`git-write-tree` &&
40 + echo $tree'
41 +
42 +_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
43 +_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
44 +test_output () {
45 + sed -e "s/ $_x40 / X /" <current >check
46 + diff -u expected check
47 +}
48 +
49 +test_expect_success \
50 + 'ls-tree plain' \
51 + 'git-ls-tree $tree >current &&
52 + cat >expected <<\EOF &&
53 +100644 blob X 1.txt
54 +100644 blob X 2.txt
55 +040000 tree X path0
56 +040000 tree X path1
57 +040000 tree X path2
58 +040000 tree X path3
59 +EOF
60 + test_output'
61 +
62 +test_expect_success \
63 + 'ls-tree recursive' \
64 + 'git-ls-tree -r $tree >current &&
65 + cat >expected <<\EOF &&
66 +100644 blob X 1.txt
67 +100644 blob X 2.txt
68 +040000 tree X path0
69 +040000 tree X path0/a
70 +040000 tree X path0/a/b
71 +040000 tree X path0/a/b/c
72 +100644 blob X path0/a/b/c/1.txt
73 +040000 tree X path1
74 +040000 tree X path1/b
75 +040000 tree X path1/b/c
76 +100644 blob X path1/b/c/1.txt
77 +040000 tree X path2
78 +100644 blob X path2/1.txt
79 +040000 tree X path3
80 +100644 blob X path3/1.txt
81 +100644 blob X path3/2.txt
82 +EOF
83 + test_output'
84 +
85 +test_expect_success \
86 + 'ls-tree filter 1.txt' \
87 + 'git-ls-tree $tree 1.txt >current &&
88 + cat >expected <<\EOF &&
89 +100644 blob X 1.txt
90 +EOF
91 + test_output'
92 +
93 +test_expect_success \
94 + 'ls-tree filter path1/b/c/1.txt' \
95 + 'git-ls-tree $tree path1/b/c/1.txt >current &&
96 + cat >expected <<\EOF &&
97 +100644 blob X path1/b/c/1.txt
98 +EOF
99 + test_output'
100 +
101 +test_expect_success \
102 + 'ls-tree filter all 1.txt files' \
103 + 'git-ls-tree $tree 1.txt path0/a/b/c/1.txt path1/b/c/1.txt path2/1.txt path3/1.txt >current &&
104 + cat >expected <<\EOF &&
105 +100644 blob X 1.txt
106 +100644 blob X path0/a/b/c/1.txt
107 +100644 blob X path1/b/c/1.txt
108 +100644 blob X path2/1.txt
109 +100644 blob X path3/1.txt
110 +EOF
111 + test_output'
112 +
113 +test_expect_success \
114 + 'ls-tree filter directories' \
115 + 'git-ls-tree $tree path3 path2 path0/a/b/c path1/b/c path0/a >current &&
116 + cat >expected <<\EOF &&
117 +040000 tree X path3
118 +100644 blob X path3/1.txt
119 +100644 blob X path3/2.txt
120 +040000 tree X path2
121 +100644 blob X path2/1.txt
122 +040000 tree X path0/a/b/c
123 +100644 blob X path0/a/b/c/1.txt
124 +040000 tree X path1/b/c
125 +100644 blob X path1/b/c/1.txt
126 +040000 tree X path0/a
127 +040000 tree X path0/a/b
128 +EOF
129 + test_output'
130 +
131 +test_expect_success \
132 + 'ls-tree filter odd names' \
133 + 'git-ls-tree $tree 1.txt /1.txt //1.txt path3/1.txt /path3/1.txt //path3//1.txt path3 /path3/ path3// >current &&
134 + cat >expected <<\EOF &&
135 +100644 blob X 1.txt
136 +100644 blob X 1.txt
137 +100644 blob X 1.txt
138 +100644 blob X path3/1.txt
139 +100644 blob X path3/1.txt
140 +100644 blob X path3/1.txt
141 +040000 tree X path3
142 +100644 blob X path3/1.txt
143 +100644 blob X path3/2.txt
144 +040000 tree X path3
145 +100644 blob X path3/1.txt
146 +100644 blob X path3/2.txt
147 +040000 tree X path3
148 +100644 blob X path3/1.txt
149 +100644 blob X path3/2.txt
150 +EOF
151 + test_output'
152 +
153 +test_expect_success \
154 + 'ls-tree filter missing files and extra slashes' \
155 + 'git-ls-tree $tree 1.txt/ abc.txt path3//23.txt path3/2.txt/// >current &&
156 + cat >expected <<\EOF &&
157 +EOF
158 + test_output'
159 +
160 +test_done