ls-tree: major rewrite to do pathspec
git-ls-tree should be rewritten to use a pathspec the same way everybody else does. Right now it's the odd man out: if you do git-ls-tree HEAD divers/char drivers/ it will show the same files _twice_, which is not how pathspecs in general work. How about this patch? It breaks some of the git-ls-tree tests, but it makes git-ls-tree work a lot more like other git pathspec commands, and it removes more than 150 lines by re-using the recursive tree traversal (but the "-d" flag is gone for good, so I'm not pushing this too hard). Linus
Linus Torvalds committed
Nov 26, 2005 at 09:38 UTC
3c5e8468a93cdb427b4c4a129339e80e1813e5c0
3 files changed
+55
-215
ls-tree.c
+22
-205
@@ -13,217 +13,32 @@ static int line_termination = '\n';
13
#define LS_TREE_ONLY 2
14
static int ls_options = 0;
15
16
-static struct tree_entry_list root_entry;
17
-
18
-static void prepare_root(unsigned char *sha1)
19
-{
20
- unsigned char rsha[20];
21
- unsigned long size;
22
- void *buf;
23
- struct tree *root_tree;
24
-
25
- buf = read_object_with_reference(sha1, "tree", &size, rsha);
26
- free(buf);
27
- if (!buf)
28
- die("Could not read %s", sha1_to_hex(sha1));
29
-
30
- root_tree = lookup_tree(rsha);
31
- if (!root_tree)
32
- die("Could not read %s", sha1_to_hex(sha1));
33
-
34
- /* Prepare a fake entry */
35
- root_entry.directory = 1;
36
- root_entry.executable = root_entry.symlink = 0;
37
- root_entry.mode = S_IFDIR;
38
- root_entry.name = "";
39
- root_entry.item.tree = root_tree;
40
- root_entry.parent = NULL;
41
-}
42
-
43
-static int prepare_children(struct tree_entry_list *elem)
44
-{
45
- if (!elem->directory)
46
- return -1;
47
- if (!elem->item.tree->object.parsed) {
48
- struct tree_entry_list *e;
49
- if (parse_tree(elem->item.tree))
50
- return -1;
51
- /* Set up the parent link */
52
- for (e = elem->item.tree->entries; e; e = e->next)
53
- e->parent = elem;
54
- }
55
- return 0;
56
-}
57
-
58
-static struct tree_entry_list *find_entry(const char *path, char *pathbuf)
59
-{
60
- const char *next, *slash;
61
- int len;
62
- struct tree_entry_list *elem = &root_entry, *oldelem = NULL;
63
-
64
- *(pathbuf) = '\0';
65
-
66
- /* Find tree element, descending from root, that
67
- * corresponds to the named path, lazily expanding
68
- * the tree if possible.
69
- */
70
-
71
- while (path) {
72
- /* The fact we still have path means that the caller
73
- * wants us to make sure that elem at this point is a
74
- * directory, and possibly descend into it. Even what
75
- * is left is just trailing slashes, we loop back to
76
- * here, and this call to prepare_children() will
77
- * catch elem not being a tree. Nice.
78
- */
79
- if (prepare_children(elem))
80
- return NULL;
81
-
82
- slash = strchr(path, '/');
83
- if (!slash) {
84
- len = strlen(path);
85
- next = NULL;
86
- }
87
- else {
88
- next = slash + 1;
89
- len = slash - path;
90
- }
91
- if (len) {
92
- if (oldelem) {
93
- pathbuf += sprintf(pathbuf, "%s/", oldelem->name);
94
- }
95
-
96
- /* (len == 0) if the original path was "drivers/char/"
97
- * and we have run already two rounds, having elem
98
- * pointing at the drivers/char directory.
99
- */
100
- elem = elem->item.tree->entries;
101
- while (elem) {
102
- if ((strlen(elem->name) == len) &&
103
- !strncmp(elem->name, path, len)) {
104
- /* found */
105
- break;
106
- }
107
- elem = elem->next;
108
- }
109
- if (!elem)
110
- return NULL;
111
-
112
- oldelem = elem;
113
- }
114
- path = next;
115
- }
116
-
117
- return elem;
118
-}
119
-
120
-static const char *entry_type(struct tree_entry_list *e)
121
-{
122
- return (e->directory ? "tree" : "blob");
123
-}
124
-
125
-static const char *entry_hex(struct tree_entry_list *e)
126
-{
127
- return sha1_to_hex(e->directory
128
- ? e->item.tree->object.sha1
129
- : e->item.blob->object.sha1);
130
-}
131
-
132
-/* forward declaration for mutually recursive routines */
133
-static int show_entry(struct tree_entry_list *, int, char *pathbuf);
16
+static const char ls_tree_usage[] =
17
+ "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
18
135
-static int show_children(struct tree_entry_list *e, int level, char *pathbuf)
19
+static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
20
{
137
- int oldlen = strlen(pathbuf);
21
+ const char *type = "blob";
22
+ int retval = 0;
23
139
- if (e != &root_entry)
140
- sprintf(pathbuf + oldlen, "%s/", e->name);
141
-
142
- if (prepare_children(e))
143
- die("internal error: ls-tree show_children called with non tree");
144
- e = e->item.tree->entries;
145
- while (e) {
146
- show_entry(e, level, pathbuf);
147
- e = e->next;
24
+ if (S_ISDIR(mode)) {
25
+ type = "tree";
26
+ if (ls_options & LS_RECURSIVE)
27
+ retval = READ_TREE_RECURSIVE;
28
}
29
150
- pathbuf[oldlen] = '\0';
151
-
152
- return 0;
30
+ printf("%06o %s %s\t%.*s%s%c", mode, type, sha1_to_hex(sha1), baselen, base, pathname, line_termination);
31
+ return retval;
32
}
33
155
-static int show_entry(struct tree_entry_list *e, int level, char *pathbuf)
34
+int main(int argc, const char **argv)
35
{
157
- int err = 0;
158
-
159
- if (e != &root_entry) {
160
- int pathlen = strlen(pathbuf);
161
- printf("%06o %s %s ",
162
- e->mode, entry_type(e), entry_hex(e));
163
- write_name_quoted(pathbuf, pathlen, e->name,
164
- line_termination, stdout);
165
- putchar(line_termination);
166
- }
167
-
168
- if (e->directory) {
169
- /* If this is a directory, we have the following cases:
170
- * (1) This is the top-level request (explicit path from the
171
- * command line, or "root" if there is no command line).
172
- * a. Without any flag. We show direct children. We do not
173
- * recurse into them.
174
- * b. With -r. We do recurse into children.
175
- * c. With -d. We do not recurse into children.
176
- * (2) We came here because our caller is either (1-a) or
177
- * (1-b).
178
- * a. Without any flag. We do not show our children (which
179
- * are grandchildren for the original request).
180
- * b. With -r. We continue to recurse into our children.
181
- * c. With -d. We should not have come here to begin with.
182
- */
183
- if (level == 0 && !(ls_options & LS_TREE_ONLY))
184
- /* case (1)-a and (1)-b */
185
- err = err | show_children(e, level+1, pathbuf);
186
- else if (level && ls_options & LS_RECURSIVE)
187
- /* case (2)-b */
188
- err = err | show_children(e, level+1, pathbuf);
189
- }
190
- return err;
191
-}
192
-
193
-static int list_one(const char *path)
194
-{
195
- int err = 0;
196
- char pathbuf[MAXPATHLEN + 1];
197
- struct tree_entry_list *e = find_entry(path, pathbuf);
198
- if (!e) {
199
- /* traditionally ls-tree does not complain about
200
- * missing path. We may change this later to match
201
- * what "/bin/ls -a" does, which is to complain.
202
- */
203
- return err;
204
- }
205
- err = err | show_entry(e, 0, pathbuf);
206
- return err;
207
-}
208
-
209
-static int list(char **path)
210
-{
211
- int i;
212
- int err = 0;
213
- for (i = 0; path[i]; i++)
214
- err = err | list_one(path[i]);
215
- return err;
216
-}
217
-
218
-static const char ls_tree_usage[] =
219
- "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
220
-
221
-int main(int argc, char **argv)
222
-{
223
- static char *path0[] = { "", NULL };
224
- char **path;
36
+ const char **path, *prefix;
37
unsigned char sha1[20];
38
+ char *buf;
39
+ unsigned long size;
40
41
+ prefix = setup_git_directory();
42
while (1 < argc && argv[1][0] == '-') {
43
switch (argv[1][1]) {
44
case 'z':
@@ -246,9 +61,11 @@ int main(int argc, char **argv)
61
if (get_sha1(argv[1], sha1) < 0)
62
usage(ls_tree_usage);
63
249
- path = (argc == 2) ? path0 : (argv + 2);
250
- prepare_root(sha1);
251
- if (list(path) < 0)
252
- die("list failed");
64
+ path = get_pathspec(prefix, argv + 2);
65
+ buf = read_object_with_reference(sha1, "tree", &size, NULL);
66
+ if (!buf)
67
+ die("not a tree object");
68
+ read_tree_recursive(buf, size, "", 0, 0, path, show_tree);
69
+
70
return 0;
71
}
tree.c
+24
-10
@@ -9,9 +9,16 @@ const char *tree_type = "tree";
9
10
static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
11
{
12
- int len = strlen(pathname);
13
- unsigned int size = cache_entry_size(baselen + len);
14
- struct cache_entry *ce = xmalloc(size);
12
+ int len;
13
+ unsigned int size;
14
+ struct cache_entry *ce;
15
+
16
+ if (S_ISDIR(mode))
17
+ return READ_TREE_RECURSIVE;
18
+
19
+ len = strlen(pathname);
20
+ size = cache_entry_size(baselen + len);
21
+ ce = xmalloc(size);
22
23
memset(ce, 0, size);
24
@@ -67,9 +74,10 @@ static int match_tree_entry(const char *base, int baselen, const char *path, uns
74
return 0;
75
}
76
70
-static int read_tree_recursive(void *buffer, unsigned long size,
71
- const char *base, int baselen,
72
- int stage, const char **match)
77
+int read_tree_recursive(void *buffer, unsigned long size,
78
+ const char *base, int baselen,
79
+ int stage, const char **match,
80
+ read_tree_fn_t fn)
81
{
82
while (size) {
83
int len = strlen(buffer)+1;
@@ -86,6 +94,14 @@ static int read_tree_recursive(void *buffer, unsigned long size,
94
if (!match_tree_entry(base, baselen, path, mode, match))
95
continue;
96
97
+ switch (fn(sha1, base, baselen, path, mode, stage)) {
98
+ case 0:
99
+ continue;
100
+ case READ_TREE_RECURSIVE:
101
+ break;;
102
+ default:
103
+ return -1;
104
+ }
105
if (S_ISDIR(mode)) {
106
int retval;
107
int pathlen = strlen(path);
@@ -106,22 +122,20 @@ static int read_tree_recursive(void *buffer, unsigned long size,
122
retval = read_tree_recursive(eltbuf, eltsize,
123
newbase,
124
baselen + pathlen + 1,
109
- stage, match);
125
+ stage, match, fn);
126
free(eltbuf);
127
free(newbase);
128
if (retval)
129
return -1;
130
continue;
131
}
116
- if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
117
- return -1;
132
}
133
return 0;
134
}
135
136
int read_tree(void *buffer, unsigned long size, int stage, const char **match)
137
{
124
- return read_tree_recursive(buffer, size, "", 0, stage, match);
138
+ return read_tree_recursive(buffer, size, "", 0, stage, match, read_one_entry);
139
}
140
141
struct tree *lookup_tree(const unsigned char *sha1)
tree.h
+9
@@ -35,4 +35,13 @@ int parse_tree(struct tree *tree);
35
/* Parses and returns the tree in the given ent, chasing tags and commits. */
36
struct tree *parse_tree_indirect(const unsigned char *sha1);
37
38
+#define READ_TREE_RECURSIVE 1
39
+typedef int (*read_tree_fn_t)(unsigned char *, const char *, int, const char *, unsigned int, int);
40
+
41
+extern int read_tree_recursive(void *buffer, unsigned long size,
42
+ const char *base, int baselen,
43
+ int stage, const char **match,
44
+ read_tree_fn_t fn);
45
+
46
+
47
#endif /* TREE_H */