1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
diff a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt
--- a/Documentation/git-ls-tree.txt
+++ b/Documentation/git-ls-tree.txt
@@ -4,23 +4,26 @@ v0.1, May 2005
NAME
----
-git-ls-tree - Displays a tree object in human readable form
+git-ls-tree - Lists the contents of a tree object.
SYNOPSIS
--------
-'git-ls-tree' [-r] [-z] <tree-ish> [paths...]
+'git-ls-tree' [-d] [-r] [-z] <tree-ish> [paths...]
DESCRIPTION
-----------
-Converts the tree object to a human readable (and script processable)
-form.
+Lists the contents of a tree object, like what "/bin/ls -a" does
+in the current working directory.
OPTIONS
-------
<tree-ish>::
Id of a tree.
+-d::
+ show only the named tree entry itself, not its children
+
-r::
recurse into sub-trees
@@ -28,18 +31,19 @@ OPTIONS
\0 line termination on output
paths::
- Optionally, restrict the output of git-ls-tree to specific
- paths. Directories will only list their tree blob ids.
- Implies -r.
+ When paths are given, shows them. Otherwise implicitly
+ uses the root level of the tree as the sole path argument.
+
Output Format
-------------
- <mode>\t <type>\t <object>\t <file>
+ <mode> SP <type> SP <object> TAB <file>
Author
------
Written by Linus Torvalds <torvalds@osdl.org>
+Completely rewritten from scratch by Junio C Hamano <junkio@cox.net>
Documentation
--------------
diff a/ls-tree.c b/ls-tree.c
--- a/ls-tree.c
+++ b/ls-tree.c
@@ -4,188 +4,217 @@
* Copyright (C) Linus Torvalds, 2005
*/
#include "cache.h"
+#include "blob.h"
+#include "tree.h"
static int line_termination = '\n';
-static int recursive = 0;
+#define LS_RECURSIVE 1
+#define LS_TREE_ONLY 2
+static int ls_options = 0;
-struct path_prefix {
- struct path_prefix *prev;
- const char *name;
-};
-
-#define DEBUG(fmt, ...)
-
-static int string_path_prefix(char *buff, size_t blen, struct path_prefix *prefix)
-{
- int len = 0;
- if (prefix) {
- if (prefix->prev) {
- len = string_path_prefix(buff,blen,prefix->prev);
- buff += len;
- blen -= len;
- if (blen > 0) {
- *buff = '/';
- len++;
- buff++;
- blen--;
- }
- }
- strncpy(buff,prefix->name,blen);
- return len + strlen(prefix->name);
- }
+static struct tree_entry_list root_entry;
- return 0;
+static void prepare_root(unsigned char *sha1)
+{
+ unsigned char rsha[20];
+ unsigned long size;
+ void *buf;
+ struct tree *root_tree;
+
+ buf = read_object_with_reference(sha1, "tree", &size, rsha);
+ free(buf);
+ if (!buf)
+ die("Could not read %s", sha1_to_hex(sha1));
+
+ root_tree = lookup_tree(rsha);
+ if (!root_tree)
+ die("Could not read %s", sha1_to_hex(sha1));
+
+ /* Prepare a fake entry */
+ root_entry.directory = 1;
+ root_entry.executable = root_entry.symlink = 0;
+ root_entry.mode = S_IFDIR;
+ root_entry.name = "";
+ root_entry.item.tree = root_tree;
+ root_entry.parent = NULL;
}
-static void print_path_prefix(struct path_prefix *prefix)
+static int prepare_children(struct tree_entry_list *elem)
{
- if (prefix) {
- if (prefix->prev) {
- print_path_prefix(prefix->prev);
- putchar('/');
- }
- fputs(prefix->name, stdout);
+ if (!elem->directory)
+ return -1;
+ if (!elem->item.tree->object.parsed) {
+ struct tree_entry_list *e;
+ if (parse_tree(elem->item.tree))
+ return -1;
+ /* Set up the parent link */
+ for (e = elem->item.tree->entries; e; e = e->next)
+ e->parent = elem;
}
+ return 0;
}
-/*
- * return:
- * -1 if prefix is *not* a subset of path
- * 0 if prefix == path
- * 1 if prefix is a subset of path
- */
-static int pathcmp(const char *path, struct path_prefix *prefix)
-{
- char buff[PATH_MAX];
- int len,slen;
+static struct tree_entry_list *find_entry_0(struct tree_entry_list *elem,
+ const char *path,
+ const char *path_end)
+{
+ const char *ep;
+ int len;
+
+ while (path < path_end) {
+ if (prepare_children(elem))
+ return NULL;
- if (prefix == NULL)
- return 1;
+ /* In elem->tree->entries, find the one that has name
+ * that matches what is between path and ep.
+ */
+ elem = elem->item.tree->entries;
- len = string_path_prefix(buff, sizeof buff, prefix);
- slen = strlen(path);
+ ep = strchr(path, '/');
+ if (!ep || path_end <= ep)
+ ep = path_end;
+ len = ep - path;
+
+ while (elem) {
+ if ((strlen(elem->name) == len) &&
+ !strncmp(elem->name, path, len))
+ break;
+ elem = elem->next;
+ }
+ if (path_end <= ep || !elem)
+ return elem;
+ while (*ep == '/' && ep < path_end)
+ ep++;
+ path = ep;
+ }
+ return NULL;
+}
- if (slen < len)
- return -1;
+static struct tree_entry_list *find_entry(const char *path,
+ const char *path_end)
+{
+ /* Find tree element, descending from root, that
+ * corresponds to the named path, lazily expanding
+ * the tree if possible.
+ */
+ if (path == path_end) {
+ /* Special. This is the root level */
+ return &root_entry;
+ }
+ return find_entry_0(&root_entry, path, path_end);
+}
- if (strncmp(path,buff,len) == 0) {
- if (slen == len)
- return 0;
- else
- return 1;
+static void show_entry_name(struct tree_entry_list *e)
+{
+ /* This is yucky. The root level is there for
+ * our convenience but we really want to do a
+ * forest.
+ */
+ if (e->parent && e->parent != &root_entry) {
+ show_entry_name(e->parent);
+ putchar('/');
}
+ printf("%s", e->name);
+}
- return -1;
-}
+static const char *entry_type(struct tree_entry_list *e)
+{
+ return (e->directory ? "tree" : "blob");
+}
-/*
- * match may be NULL, or a *sorted* list of paths
- */
-static void list_recursive(void *buffer,
- const char *type,
- unsigned long size,
- struct path_prefix *prefix,
- char **match, int matches)
-{
- struct path_prefix this_prefix;
- this_prefix.prev = prefix;
-
- if (strcmp(type, "tree"))
- die("expected a 'tree' node");
-
- if (matches)
- recursive = 1;
-
- while (size) {
- int namelen = strlen(buffer)+1;
- void *eltbuf = NULL;
- char elttype[20];
- unsigned long eltsize;
- unsigned char *sha1 = buffer + namelen;
- char *path = strchr(buffer, ' ') + 1;
- unsigned int mode;
- const char *matched = NULL;
- int mtype = -1;
- int mindex;
-
- if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
- die("corrupt 'tree' file");
- buffer = sha1 + 20;
- size -= namelen + 20;
-
- this_prefix.name = path;
- for ( mindex = 0; mindex < matches; mindex++) {
- mtype = pathcmp(match[mindex],&this_prefix);
- if (mtype >= 0) {
- matched = match[mindex];
- break;
- }
- }
+static const char *entry_hex(struct tree_entry_list *e)
+{
+ return sha1_to_hex(e->directory
+ ? e->item.tree->object.sha1
+ : e->item.blob->object.sha1);
+}
- /*
- * If we're not matching, or if this is an exact match,
- * print out the info
- */
- if (!matches || (matched != NULL && mtype == 0)) {
- printf("%06o %s %s\t", mode,
- S_ISDIR(mode) ? "tree" : "blob",
- sha1_to_hex(sha1));
- print_path_prefix(&this_prefix);
- putchar(line_termination);
- }
+/* forward declaration for mutually recursive routines */
+static int show_entry(struct tree_entry_list *, int);
- if (! recursive || ! S_ISDIR(mode))
- continue;
+static int show_children(struct tree_entry_list *e, int level)
+{
+ if (prepare_children(e))
+ die("internal error: ls-tree show_children called with non tree");
+ e = e->item.tree->entries;
+ while (e) {
+ show_entry(e, level);
+ e = e->next;
+ }
+ return 0;
+}
- if (matches && ! matched)
- continue;
+static int show_entry(struct tree_entry_list *e, int level)
+{
+ int err = 0;
- if (! (eltbuf = read_sha1_file(sha1, elttype, &eltsize)) ) {
- error("cannot read %s", sha1_to_hex(sha1));
- continue;
- }
+ if (e != &root_entry) {
+ printf("%06o %s %s ", e->mode, entry_type(e),
+ entry_hex(e));
+ show_entry_name(e);
+ putchar(line_termination);
+ }
- /* If this is an exact directory match, we may have
- * directory files following this path. Match on them.
- * Otherwise, we're at a patch subcomponent, and we need
- * to try to match again.
+ if (e->directory) {
+ /* If this is a directory, we have the following cases:
+ * (1) This is the top-level request (explicit path from the
+ * command line, or "root" if there is no command line).
+ * a. Without any flag. We show direct children. We do not
+ * recurse into them.
+ * b. With -r. We do recurse into children.
+ * c. With -d. We do not recurse into children.
+ * (2) We came here because our caller is either (1-a) or
+ * (1-b).
+ * a. Without any flag. We do not show our children (which
+ * are grandchildren for the original request).
+ * b. With -r. We continue to recurse into our children.
+ * c. With -d. We should not have come here to begin with.
*/
- if (mtype == 0)
- mindex++;
-
- list_recursive(eltbuf, elttype, eltsize, &this_prefix, &match[mindex], matches-mindex);
- free(eltbuf);
+ if (level == 0 && !(ls_options & LS_TREE_ONLY))
+ /* case (1)-a and (1)-b */
+ err = err | show_children(e, level+1);
+ else if (level && ls_options & LS_RECURSIVE)
+ /* case (2)-b */
+ err = err | show_children(e, level+1);
}
+ return err;
}
-static int qcmp(const void *a, const void *b)
+static int list_one(const char *path, const char *path_end)
{
- return strcmp(*(char **)a, *(char **)b);
+ int err = 0;
+ struct tree_entry_list *e = find_entry(path, path_end);
+ if (!e) {
+ /* traditionally ls-tree does not complain about
+ * missing path. We may change this later to match
+ * what "/bin/ls -a" does, which is to complain.
+ */
+ return err;
+ }
+ err = err | show_entry(e, 0);
+ return err;
}
-static int list(unsigned char *sha1,char **path)
+static int list(char **path)
{
- void *buffer;
- unsigned long size;
- int npaths;
-
- for (npaths = 0; path[npaths] != NULL; npaths++)
- ;
-
- qsort(path,npaths,sizeof(char *),qcmp);
-
- buffer = read_object_with_reference(sha1, "tree", &size, NULL);
- if (!buffer)
- die("unable to read sha1 file");
- list_recursive(buffer, "tree", size, NULL, path, npaths);
- free(buffer);
- return 0;
+ int i;
+ int err = 0;
+ for (i = 0; path[i]; i++) {
+ int len = strlen(path[i]);
+ while (0 <= len && path[i][len] == '/')
+ len--;
+ err = err | list_one(path[i], path[i] + len);
+ }
+ return err;
}
-static const char *ls_tree_usage = "git-ls-tree [-r] [-z] <key> [paths...]";
+static const char *ls_tree_usage =
+ "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
int main(int argc, char **argv)
{
+ static char *path0[] = { "", NULL };
+ char **path;
unsigned char sha1[20];
while (1 < argc && argv[1][0] == '-') {
@@ -194,7 +223,10 @@ int main(int argc, char **argv)
line_termination = 0;
break;
case 'r':
- recursive = 1;
+ ls_options |= LS_RECURSIVE;
+ break;
+ case 'd':
+ ls_options |= LS_TREE_ONLY;
break;
default:
usage(ls_tree_usage);
@@ -206,7 +238,10 @@ int main(int argc, char **argv)
usage(ls_tree_usage);
if (get_sha1(argv[1], sha1) < 0)
usage(ls_tree_usage);
- if (list(sha1, &argv[2]) < 0)
+
+ path = (argc == 2) ? path0 : (argv + 2);
+ prepare_root(sha1);
+ if (list(path) < 0)
die("list failed");
return 0;
}
diff a/t/t3100-ls-tree-restrict.sh b/t/t3100-ls-tree-restrict.sh
--- a/t/t3100-ls-tree-restrict.sh
+++ b/t/t3100-ls-tree-restrict.sh
@@ -74,8 +74,8 @@ test_expect_success \
'ls-tree filtered' \
'git-ls-tree $tree path1 path0 >current &&
cat >expected <<\EOF &&
-100644 blob X path0
120000 blob X path1
+100644 blob X path0
EOF
test_output'
@@ -85,7 +85,6 @@ test_expect_success \
cat >expected <<\EOF &&
040000 tree X path2
040000 tree X path2/baz
-100644 blob X path2/baz/b
120000 blob X path2/bazbo
100644 blob X path2/foo
EOF
diff a/tree.c b/tree.c
--- a/tree.c
+++ b/tree.c
@@ -133,7 +133,7 @@ int parse_tree_buffer(struct tree *item,
}
if (obj)
add_ref(&item->object, obj);
-
+ entry->parent = NULL; /* needs to be filled by the user */
*list_p = entry;
list_p = &entry->next;
}
diff a/tree.h b/tree.h
--- a/tree.h
+++ b/tree.h
@@ -16,6 +16,7 @@ struct tree_entry_list {
struct tree *tree;
struct blob *blob;
} item;
+ struct tree_entry_list *parent;
};
struct tree {
|