- char type[20];
-
- buffer = read_sha1_file(sha1, type, &size);
- if (!buffer)
- die("unable to read sha1 file");
- if (strcmp(type, "tree"))
- die("expected a 'tree' node");
- while (size) {
- int len = strlen(buffer)+1;
- unsigned char *sha1 = buffer + len;
- char *path = strchr(buffer, ' ')+1;
- unsigned int mode;
- unsigned char *type;
-
- if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
- die("corrupt 'tree' file");
- buffer = sha1 + 20;
- size -= len + 20;
- /* XXX: We do some ugly mode heuristics here.
- * It seems not worth it to read each file just to get this
- * and the file size. -- pasky@ucw.cz */
- type = S_ISDIR(mode) ? "tree" : "blob";
- printf("%03o\t%s\t%s\t%s\n", mode, type, sha1_to_hex(sha1), path);
+ 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 int prepare_children(struct tree_entry_list *elem)
+{
+ 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;
+}
+
+static struct tree_entry_list *find_entry(const char *path)
+{
+ const char *next, *slash;
+ int len;
+ struct tree_entry_list *elem = &root_entry;
+
+ /* Find tree element, descending from root, that
+ * corresponds to the named path, lazily expanding
+ * the tree if possible.
+ */
+
+ while (path) {
+ /* The fact we still have path means that the caller
+ * wants us to make sure that elem at this point is a
+ * directory, and possibly descend into it. Even what
+ * is left is just trailing slashes, we loop back to
+ * here, and this call to prepare_children() will
+ * catch elem not being a tree. Nice.
+ */
+ if (prepare_children(elem))
+ return NULL;
+
+ slash = strchr(path, '/');
+ if (!slash) {
+ len = strlen(path);
+ next = 0;
+ }
+ else {
+ next = slash + 1;
+ len = slash - path;
+ }
+ if (len) {
+ /* (len == 0) if the original path was "drivers/char/"
+ * and we have run already two rounds, having elem
+ * pointing at the drivers/char directory.
+ */
+ elem = elem->item.tree->entries;
+ while (elem) {
+ if ((strlen(elem->name) == len) &&
+ !strncmp(elem->name, path, len)) {
+ /* found */
+ break;
+ }
+ elem = elem->next;
+ }
+ if (!elem)
+ return NULL;
+ }
+ path = next;
+ }
+
+ return elem;
+}
+
+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);
+}
+
+static const char *entry_type(struct tree_entry_list *e)
+{
+ return (e->directory ? "tree" : "blob");
+}
+
+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);
+}
+
+/* forward declaration for mutually recursive routines */
+static int show_entry(struct tree_entry_list *, int);
+
+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;