ls-tree.c 5.8 KB
Newer Older
P
Petr Baudis 已提交
1 2 3 4 5 6
/*
 * GIT - The information manager from hell
 *
 * Copyright (C) Linus Torvalds, 2005
 */
#include "cache.h"
7 8
#include "blob.h"
#include "tree.h"
9
#include "quote.h"
P
Petr Baudis 已提交
10

L
Linus Torvalds 已提交
11
static int line_termination = '\n';
12 13 14
#define LS_RECURSIVE 1
#define LS_TREE_ONLY 2
static int ls_options = 0;
J
Junio C Hamano 已提交
15

16
static struct tree_entry_list root_entry;
J
Junio C Hamano 已提交
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
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;
}
42

43
static int prepare_children(struct tree_entry_list *elem)
44
{
45 46 47 48 49 50 51 52 53
	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;
54 55 56 57
	}
	return 0;
}

58
static struct tree_entry_list *find_entry(const char *path, char *pathbuf)
P
Petr Baudis 已提交
59
{
60
	const char *next, *slash;
61
	int len;
62 63 64
	struct tree_entry_list *elem = &root_entry, *oldelem = NULL;

	*(pathbuf) = '\0';
65

66 67 68 69 70 71 72 73 74 75 76 77 78
	/* 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.
		 */
79 80 81
		if (prepare_children(elem))
			return NULL;

82 83 84
		slash = strchr(path, '/');
		if (!slash) {
			len = strlen(path);
L
Linus Torvalds 已提交
85
			next = NULL;
86 87 88 89 90 91
		}
		else {
			next = slash + 1;
			len = slash - path;
		}
		if (len) {
92 93 94 95
			if (oldelem) {
				pathbuf += sprintf(pathbuf, "%s/", oldelem->name);
			}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
			/* (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;
111 112

			oldelem = elem;
113
		}
114
		path = next;
J
Junio C Hamano 已提交
115 116
	}

117
	return elem;
118
}
119

120 121 122 123
static const char *entry_type(struct tree_entry_list *e)
{
	return (e->directory ? "tree" : "blob");
}
124

125
static const char *entry_hex(struct tree_entry_list *e)
J
Junio C Hamano 已提交
126
{
127 128 129 130
	return sha1_to_hex(e->directory
			   ? e->item.tree->object.sha1
			   : e->item.blob->object.sha1);
}
131

132
/* forward declaration for mutually recursive routines */
133
static int show_entry(struct tree_entry_list *, int, char *pathbuf);
J
Junio C Hamano 已提交
134

135
static int show_children(struct tree_entry_list *e, int level, char *pathbuf)
136
{
137 138 139 140 141
	int oldlen = strlen(pathbuf);

	if (e != &root_entry)
		sprintf(pathbuf + oldlen, "%s/", e->name);

142 143 144 145
	if (prepare_children(e))
		die("internal error: ls-tree show_children called with non tree");
	e = e->item.tree->entries;
	while (e) {
146
		show_entry(e, level, pathbuf);
147 148
		e = e->next;
	}
149 150 151

	pathbuf[oldlen] = '\0';

152 153
	return 0;
}
154

155
static int show_entry(struct tree_entry_list *e, int level, char *pathbuf)
156 157
{
	int err = 0; 
158

159
	if (e != &root_entry) {
160
		int pathlen = strlen(pathbuf);
161 162
		printf("%06o %s %s	",
		       e->mode, entry_type(e), entry_hex(e));
163 164
		write_name_quoted(pathbuf, pathlen, e->name,
				  line_termination, stdout);
165 166
		putchar(line_termination);
	}
167

168 169 170 171 172 173 174 175 176 177 178 179 180 181
	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.
182
		 */
183 184
		if (level == 0 && !(ls_options & LS_TREE_ONLY))
			/* case (1)-a and (1)-b */
185
			err = err | show_children(e, level+1, pathbuf);
186 187
		else if (level && ls_options & LS_RECURSIVE)
			/* case (2)-b */
188
			err = err | show_children(e, level+1, pathbuf);
P
Petr Baudis 已提交
189
	}
190
	return err;
J
Junio C Hamano 已提交
191 192
}

193
static int list_one(const char *path)
194
{
195
	int err = 0;
196 197
	char pathbuf[MAXPATHLEN + 1];
	struct tree_entry_list *e = find_entry(path, pathbuf);
198 199 200 201 202 203 204
	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;
	}
205
	err = err | show_entry(e, 0, pathbuf);
206
	return err;
207 208
}

209
static int list(char **path)
J
Junio C Hamano 已提交
210
{
211 212
	int i;
	int err = 0;
213 214
	for (i = 0; path[i]; i++)
		err = err | list_one(path[i]);
215
	return err;
P
Petr Baudis 已提交
216 217
}

218
static const char ls_tree_usage[] =
219
	"git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
J
Junio C Hamano 已提交
220

P
Petr Baudis 已提交
221 222
int main(int argc, char **argv)
{
223 224
	static char *path0[] = { "", NULL };
	char **path;
P
Petr Baudis 已提交
225 226
	unsigned char sha1[20];

J
Junio C Hamano 已提交
227 228 229 230 231 232
	while (1 < argc && argv[1][0] == '-') {
		switch (argv[1][1]) {
		case 'z':
			line_termination = 0;
			break;
		case 'r':
233 234 235 236
			ls_options |= LS_RECURSIVE;
			break;
		case 'd':
			ls_options |= LS_TREE_ONLY;
J
Junio C Hamano 已提交
237 238
			break;
		default:
239
			usage(ls_tree_usage);
J
Junio C Hamano 已提交
240 241 242 243
		}
		argc--; argv++;
	}

244
	if (argc < 2)
245
		usage(ls_tree_usage);
246
	if (get_sha1(argv[1], sha1) < 0)
247
		usage(ls_tree_usage);
248 249 250 251

	path = (argc == 2) ? path0 : (argv + 2);
	prepare_root(sha1);
	if (list(path) < 0)
252
		die("list failed");
P
Petr Baudis 已提交
253 254
	return 0;
}