ls-tree.c 5.6 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"
P
Petr Baudis 已提交
9

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

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

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

57 58 59
static struct tree_entry_list *find_entry_0(struct tree_entry_list *elem,
					    const char *path,
					    const char *path_end)
P
Petr Baudis 已提交
60
{
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	const char *ep;
	int len;

	while (path < path_end) {
		if (prepare_children(elem))
			return NULL;

		/* In elem->tree->entries, find the one that has name
		 * that matches what is between path and ep.
		 */
		elem = elem->item.tree->entries;

		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;
83
		}
84 85 86 87 88
		if (path_end <= ep || !elem)
			return elem;
		while (*ep == '/' && ep < path_end)
			ep++;
		path = ep;
J
Junio C Hamano 已提交
89
	}
90
	return NULL;
J
Junio C Hamano 已提交
91 92
}

93 94
static struct tree_entry_list *find_entry(const char *path,
					  const char *path_end)
95
{
96 97 98 99 100 101 102 103 104 105
	/* 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);
}
106

107 108 109 110 111 112 113 114 115
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('/');
116
	}
117 118
	printf("%s", e->name);
}
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 133
/* forward declaration for mutually recursive routines */
static int show_entry(struct tree_entry_list *, int);
J
Junio C Hamano 已提交
134

135 136 137 138 139 140 141 142 143 144 145
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;
}
146

147 148 149
static int show_entry(struct tree_entry_list *e, int level)
{
	int err = 0; 
150

151 152 153 154 155 156
	if (e != &root_entry) {
		printf("%06o %s %s	", e->mode, entry_type(e),
		       entry_hex(e));
		show_entry_name(e);
		putchar(line_termination);
	}
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171
	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.
172
		 */
173 174 175 176 177 178
		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);
P
Petr Baudis 已提交
179
	}
180
	return err;
J
Junio C Hamano 已提交
181 182
}

183
static int list_one(const char *path, const char *path_end)
184
{
185 186 187 188 189 190 191 192 193 194 195
	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;
196 197
}

198
static int list(char **path)
J
Junio C Hamano 已提交
199
{
200 201 202 203 204 205 206 207 208
	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;
P
Petr Baudis 已提交
209 210
}

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

P
Petr Baudis 已提交
214 215
int main(int argc, char **argv)
{
216 217
	static char *path0[] = { "", NULL };
	char **path;
P
Petr Baudis 已提交
218 219
	unsigned char sha1[20];

J
Junio C Hamano 已提交
220 221 222 223 224 225
	while (1 < argc && argv[1][0] == '-') {
		switch (argv[1][1]) {
		case 'z':
			line_termination = 0;
			break;
		case 'r':
226 227 228 229
			ls_options |= LS_RECURSIVE;
			break;
		case 'd':
			ls_options |= LS_TREE_ONLY;
J
Junio C Hamano 已提交
230 231
			break;
		default:
232
			usage(ls_tree_usage);
J
Junio C Hamano 已提交
233 234 235 236
		}
		argc--; argv++;
	}

237
	if (argc < 2)
238
		usage(ls_tree_usage);
239
	if (get_sha1(argv[1], sha1) < 0)
240
		usage(ls_tree_usage);
241 242 243 244

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