1
6af1f0192ff8740fe77db7cf02c739ccfbdf119c (from 2bc2564145835996734d6ed5d1880f85b17233d6)
2
diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt
3
--- a/Documentation/git-ls-tree.txt
4
+++ b/Documentation/git-ls-tree.txt
5
@@ -4,23 +4,26 @@ v0.1, May 2005
9
-git-ls-tree - Displays a tree object in human readable form
10
+git-ls-tree - Lists the contents of a tree object.
15
-'git-ls-tree' [-r] [-z] <tree-ish> [paths...]
16
+'git-ls-tree' [-d] [-r] [-z] <tree-ish> [paths...]
20
-Converts the tree object to a human readable (and script processable)
22
+Lists the contents of a tree object, like what "/bin/ls -a" does
23
+in the current working directory.
31
+ show only the named tree entry itself, not its children
34
recurse into sub-trees
36
@@ -28,18 +31,19 @@ OPTIONS
37
\0 line termination on output
40
- Optionally, restrict the output of git-ls-tree to specific
41
- paths. Directories will only list their tree blob ids.
43
+ When paths are given, shows them. Otherwise implicitly
44
+ uses the root level of the tree as the sole path argument.
49
- <mode>\t <type>\t <object>\t <file>
50
+ <mode> SP <type> SP <object> TAB <file>
55
Written by Linus Torvalds <torvalds@osdl.org>
56
+Completely rewritten from scratch by Junio C Hamano <junkio@cox.net>
60
diff --git a/ls-tree.c b/ls-tree.c
61
dissimilarity index 82%
66
- * GIT - The information manager from hell
68
- * Copyright (C) Linus Torvalds, 2005
72
-static int line_termination = '\n';
73
-static int recursive = 0;
76
- struct path_prefix *prev;
80
-#define DEBUG(fmt, ...)
82
-static int string_path_prefix(char *buff, size_t blen, struct path_prefix *prefix)
87
- len = string_path_prefix(buff,blen,prefix->prev);
97
- strncpy(buff,prefix->name,blen);
98
- return len + strlen(prefix->name);
104
-static void print_path_prefix(struct path_prefix *prefix)
108
- print_path_prefix(prefix->prev);
111
- fputs(prefix->name, stdout);
117
- * -1 if prefix is *not* a subset of path
118
- * 0 if prefix == path
119
- * 1 if prefix is a subset of path
121
-static int pathcmp(const char *path, struct path_prefix *prefix)
123
- char buff[PATH_MAX];
129
- len = string_path_prefix(buff, sizeof buff, prefix);
130
- slen = strlen(path);
135
- if (strncmp(path,buff,len) == 0) {
146
- * match may be NULL, or a *sorted* list of paths
148
-static void list_recursive(void *buffer,
151
- struct path_prefix *prefix,
152
- char **match, int matches)
154
- struct path_prefix this_prefix;
155
- this_prefix.prev = prefix;
157
- if (strcmp(type, "tree"))
158
- die("expected a 'tree' node");
164
- int namelen = strlen(buffer)+1;
165
- void *eltbuf = NULL;
167
- unsigned long eltsize;
168
- unsigned char *sha1 = buffer + namelen;
169
- char *path = strchr(buffer, ' ') + 1;
171
- const char *matched = NULL;
175
- if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
176
- die("corrupt 'tree' file");
178
- size -= namelen + 20;
180
- this_prefix.name = path;
181
- for ( mindex = 0; mindex < matches; mindex++) {
182
- mtype = pathcmp(match[mindex],&this_prefix);
184
- matched = match[mindex];
190
- * If we're not matching, or if this is an exact match,
191
- * print out the info
193
- if (!matches || (matched != NULL && mtype == 0)) {
194
- printf("%06o %s %s\t", mode,
195
- S_ISDIR(mode) ? "tree" : "blob",
197
- print_path_prefix(&this_prefix);
198
- putchar(line_termination);
201
- if (! recursive || ! S_ISDIR(mode))
204
- if (matches && ! matched)
207
- if (! (eltbuf = read_sha1_file(sha1, elttype, &eltsize)) ) {
208
- error("cannot read %s", sha1_to_hex(sha1));
212
- /* If this is an exact directory match, we may have
213
- * directory files following this path. Match on them.
214
- * Otherwise, we're at a patch subcomponent, and we need
215
- * to try to match again.
220
- list_recursive(eltbuf, elttype, eltsize, &this_prefix, &match[mindex], matches-mindex);
225
-static int qcmp(const void *a, const void *b)
227
- return strcmp(*(char **)a, *(char **)b);
230
-static int list(unsigned char *sha1,char **path)
236
- for (npaths = 0; path[npaths] != NULL; npaths++)
239
- qsort(path,npaths,sizeof(char *),qcmp);
241
- buffer = read_object_with_reference(sha1, "tree", &size, NULL);
243
- die("unable to read sha1 file");
244
- list_recursive(buffer, "tree", size, NULL, path, npaths);
249
-static const char *ls_tree_usage = "git-ls-tree [-r] [-z] <key> [paths...]";
251
-int main(int argc, char **argv)
253
- unsigned char sha1[20];
255
- while (1 < argc && argv[1][0] == '-') {
256
- switch (argv[1][1]) {
258
- line_termination = 0;
264
- usage(ls_tree_usage);
270
- usage(ls_tree_usage);
271
- if (get_sha1(argv[1], sha1) < 0)
272
- usage(ls_tree_usage);
273
- if (list(sha1, &argv[2]) < 0)
278
+ * GIT - The information manager from hell
280
+ * Copyright (C) Linus Torvalds, 2005
286
+static int line_termination = '\n';
287
+#define LS_RECURSIVE 1
288
+#define LS_TREE_ONLY 2
289
+static int ls_options = 0;
291
+static struct tree_entry_list root_entry;
293
+static void prepare_root(unsigned char *sha1)
295
+ unsigned char rsha[20];
298
+ struct tree *root_tree;
300
+ buf = read_object_with_reference(sha1, "tree", &size, rsha);
303
+ die("Could not read %s", sha1_to_hex(sha1));
305
+ root_tree = lookup_tree(rsha);
307
+ die("Could not read %s", sha1_to_hex(sha1));
309
+ /* Prepare a fake entry */
310
+ root_entry.directory = 1;
311
+ root_entry.executable = root_entry.symlink = 0;
312
+ root_entry.mode = S_IFDIR;
313
+ root_entry.name = "";
314
+ root_entry.item.tree = root_tree;
315
+ root_entry.parent = NULL;
318
+static int prepare_children(struct tree_entry_list *elem)
320
+ if (!elem->directory)
322
+ if (!elem->item.tree->object.parsed) {
323
+ struct tree_entry_list *e;
324
+ if (parse_tree(elem->item.tree))
326
+ /* Set up the parent link */
327
+ for (e = elem->item.tree->entries; e; e = e->next)
333
+static struct tree_entry_list *find_entry_0(struct tree_entry_list *elem,
335
+ const char *path_end)
340
+ while (path < path_end) {
341
+ if (prepare_children(elem))
344
+ /* In elem->tree->entries, find the one that has name
345
+ * that matches what is between path and ep.
347
+ elem = elem->item.tree->entries;
349
+ ep = strchr(path, '/');
350
+ if (!ep || path_end <= ep)
355
+ if ((strlen(elem->name) == len) &&
356
+ !strncmp(elem->name, path, len))
360
+ if (path_end <= ep || !elem)
362
+ while (*ep == '/' && ep < path_end)
369
+static struct tree_entry_list *find_entry(const char *path,
370
+ const char *path_end)
372
+ /* Find tree element, descending from root, that
373
+ * corresponds to the named path, lazily expanding
374
+ * the tree if possible.
376
+ if (path == path_end) {
377
+ /* Special. This is the root level */
380
+ return find_entry_0(&root_entry, path, path_end);
383
+static void show_entry_name(struct tree_entry_list *e)
385
+ /* This is yucky. The root level is there for
386
+ * our convenience but we really want to do a
389
+ if (e->parent && e->parent != &root_entry) {
390
+ show_entry_name(e->parent);
393
+ printf("%s", e->name);
396
+static const char *entry_type(struct tree_entry_list *e)
398
+ return (e->directory ? "tree" : "blob");
401
+static const char *entry_hex(struct tree_entry_list *e)
403
+ return sha1_to_hex(e->directory
404
+ ? e->item.tree->object.sha1
405
+ : e->item.blob->object.sha1);
408
+/* forward declaration for mutually recursive routines */
409
+static int show_entry(struct tree_entry_list *, int);
411
+static int show_children(struct tree_entry_list *e, int level)
413
+ if (prepare_children(e))
414
+ die("internal error: ls-tree show_children called with non tree");
415
+ e = e->item.tree->entries;
417
+ show_entry(e, level);
423
+static int show_entry(struct tree_entry_list *e, int level)
427
+ if (e != &root_entry) {
428
+ printf("%06o %s %s ", e->mode, entry_type(e),
431
+ putchar(line_termination);
435
+ /* If this is a directory, we have the following cases:
436
+ * (1) This is the top-level request (explicit path from the
437
+ * command line, or "root" if there is no command line).
438
+ * a. Without any flag. We show direct children. We do not
439
+ * recurse into them.
440
+ * b. With -r. We do recurse into children.
441
+ * c. With -d. We do not recurse into children.
442
+ * (2) We came here because our caller is either (1-a) or
444
+ * a. Without any flag. We do not show our children (which
445
+ * are grandchildren for the original request).
446
+ * b. With -r. We continue to recurse into our children.
447
+ * c. With -d. We should not have come here to begin with.
449
+ if (level == 0 && !(ls_options & LS_TREE_ONLY))
450
+ /* case (1)-a and (1)-b */
451
+ err = err | show_children(e, level+1);
452
+ else if (level && ls_options & LS_RECURSIVE)
454
+ err = err | show_children(e, level+1);
459
+static int list_one(const char *path, const char *path_end)
462
+ struct tree_entry_list *e = find_entry(path, path_end);
464
+ /* traditionally ls-tree does not complain about
465
+ * missing path. We may change this later to match
466
+ * what "/bin/ls -a" does, which is to complain.
470
+ err = err | show_entry(e, 0);
474
+static int list(char **path)
478
+ for (i = 0; path[i]; i++) {
479
+ int len = strlen(path[i]);
480
+ while (0 <= len && path[i][len] == '/')
482
+ err = err | list_one(path[i], path[i] + len);
487
+static const char *ls_tree_usage =
488
+ "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
490
+int main(int argc, char **argv)
492
+ static char *path0[] = { "", NULL };
494
+ unsigned char sha1[20];
496
+ while (1 < argc && argv[1][0] == '-') {
497
+ switch (argv[1][1]) {
499
+ line_termination = 0;
502
+ ls_options |= LS_RECURSIVE;
505
+ ls_options |= LS_TREE_ONLY;
508
+ usage(ls_tree_usage);
514
+ usage(ls_tree_usage);
515
+ if (get_sha1(argv[1], sha1) < 0)
516
+ usage(ls_tree_usage);
518
+ path = (argc == 2) ? path0 : (argv + 2);
524
diff --git a/t/t3100-ls-tree-restrict.sh b/t/t3100-ls-tree-restrict.sh
525
--- a/t/t3100-ls-tree-restrict.sh
526
+++ b/t/t3100-ls-tree-restrict.sh
527
@@ -74,8 +74,8 @@ test_expect_success \
529
'git-ls-tree $tree path1 path0 >current &&
530
cat >expected <<\EOF &&
537
@@ -85,7 +85,6 @@ test_expect_success \
538
cat >expected <<\EOF &&
540
040000 tree X path2/baz
541
-100644 blob X path2/baz/b
542
120000 blob X path2/bazbo
543
100644 blob X path2/foo
545
diff --git a/tree.c b/tree.c
548
@@ -133,7 +133,7 @@ int parse_tree_buffer(struct tree *item,
551
add_ref(&item->object, obj);
553
+ entry->parent = NULL; /* needs to be filled by the user */
555
list_p = &entry->next;
557
diff --git a/tree.h b/tree.h
560
@@ -16,6 +16,7 @@ struct tree_entry_list {
564
+ struct tree_entry_list *parent;