s/checkout-cache/checkout-index/g for Documentation/git-ls-files.txt
[git/git.git] / ls-tree.c
CommitLineData
7912c070
PB
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
6af1f019
JH
7#include "blob.h"
8#include "tree.h"
7912c070 9
e99d59ff 10static int line_termination = '\n';
6af1f019
JH
11#define LS_RECURSIVE 1
12#define LS_TREE_ONLY 2
13static int ls_options = 0;
aa1c48df 14
6af1f019 15static struct tree_entry_list root_entry;
aa1c48df 16
6af1f019
JH
17static void prepare_root(unsigned char *sha1)
18{
19 unsigned char rsha[20];
20 unsigned long size;
21 void *buf;
22 struct tree *root_tree;
23
24 buf = read_object_with_reference(sha1, "tree", &size, rsha);
25 free(buf);
26 if (!buf)
27 die("Could not read %s", sha1_to_hex(sha1));
28
29 root_tree = lookup_tree(rsha);
30 if (!root_tree)
31 die("Could not read %s", sha1_to_hex(sha1));
32
33 /* Prepare a fake entry */
34 root_entry.directory = 1;
35 root_entry.executable = root_entry.symlink = 0;
36 root_entry.mode = S_IFDIR;
37 root_entry.name = "";
38 root_entry.item.tree = root_tree;
39 root_entry.parent = NULL;
40}
6d3a5077 41
6af1f019 42static int prepare_children(struct tree_entry_list *elem)
6d3a5077 43{
6af1f019
JH
44 if (!elem->directory)
45 return -1;
46 if (!elem->item.tree->object.parsed) {
47 struct tree_entry_list *e;
48 if (parse_tree(elem->item.tree))
49 return -1;
50 /* Set up the parent link */
51 for (e = elem->item.tree->entries; e; e = e->next)
52 e->parent = elem;
6d3a5077 53 }
6d3a5077
JM
54 return 0;
55}
56
66204988 57static struct tree_entry_list *find_entry(const char *path)
7912c070 58{
66204988 59 const char *next, *slash;
6af1f019 60 int len;
66204988 61 struct tree_entry_list *elem = &root_entry;
6af1f019 62
66204988
JH
63 /* Find tree element, descending from root, that
64 * corresponds to the named path, lazily expanding
65 * the tree if possible.
66 */
67
68 while (path) {
69 /* The fact we still have path means that the caller
70 * wants us to make sure that elem at this point is a
71 * directory, and possibly descend into it. Even what
72 * is left is just trailing slashes, we loop back to
73 * here, and this call to prepare_children() will
74 * catch elem not being a tree. Nice.
75 */
6af1f019
JH
76 if (prepare_children(elem))
77 return NULL;
78
66204988
JH
79 slash = strchr(path, '/');
80 if (!slash) {
81 len = strlen(path);
6da4016a 82 next = NULL;
66204988
JH
83 }
84 else {
85 next = slash + 1;
86 len = slash - path;
87 }
88 if (len) {
89 /* (len == 0) if the original path was "drivers/char/"
90 * and we have run already two rounds, having elem
91 * pointing at the drivers/char directory.
92 */
93 elem = elem->item.tree->entries;
94 while (elem) {
95 if ((strlen(elem->name) == len) &&
96 !strncmp(elem->name, path, len)) {
97 /* found */
98 break;
99 }
100 elem = elem->next;
101 }
102 if (!elem)
103 return NULL;
6d3a5077 104 }
66204988 105 path = next;
aa1c48df 106 }
aa1c48df 107
66204988 108 return elem;
6af1f019 109}
6d3a5077 110
6af1f019
JH
111static void show_entry_name(struct tree_entry_list *e)
112{
113 /* This is yucky. The root level is there for
114 * our convenience but we really want to do a
115 * forest.
116 */
117 if (e->parent && e->parent != &root_entry) {
118 show_entry_name(e->parent);
119 putchar('/');
6d3a5077 120 }
6af1f019
JH
121 printf("%s", e->name);
122}
6d3a5077 123
6af1f019
JH
124static const char *entry_type(struct tree_entry_list *e)
125{
126 return (e->directory ? "tree" : "blob");
127}
6d3a5077 128
6af1f019 129static const char *entry_hex(struct tree_entry_list *e)
aa1c48df 130{
6af1f019
JH
131 return sha1_to_hex(e->directory
132 ? e->item.tree->object.sha1
133 : e->item.blob->object.sha1);
134}
6d3a5077 135
6af1f019
JH
136/* forward declaration for mutually recursive routines */
137static int show_entry(struct tree_entry_list *, int);
aa1c48df 138
6af1f019
JH
139static int show_children(struct tree_entry_list *e, int level)
140{
141 if (prepare_children(e))
142 die("internal error: ls-tree show_children called with non tree");
143 e = e->item.tree->entries;
144 while (e) {
145 show_entry(e, level);
146 e = e->next;
147 }
148 return 0;
149}
0f2303f7 150
6af1f019
JH
151static int show_entry(struct tree_entry_list *e, int level)
152{
153 int err = 0;
6d3a5077 154
6af1f019
JH
155 if (e != &root_entry) {
156 printf("%06o %s %s ", e->mode, entry_type(e),
157 entry_hex(e));
158 show_entry_name(e);
159 putchar(line_termination);
160 }
6d3a5077 161
6af1f019
JH
162 if (e->directory) {
163 /* If this is a directory, we have the following cases:
164 * (1) This is the top-level request (explicit path from the
165 * command line, or "root" if there is no command line).
166 * a. Without any flag. We show direct children. We do not
167 * recurse into them.
168 * b. With -r. We do recurse into children.
169 * c. With -d. We do not recurse into children.
170 * (2) We came here because our caller is either (1-a) or
171 * (1-b).
172 * a. Without any flag. We do not show our children (which
173 * are grandchildren for the original request).
174 * b. With -r. We continue to recurse into our children.
175 * c. With -d. We should not have come here to begin with.
6d3a5077 176 */
6af1f019
JH
177 if (level == 0 && !(ls_options & LS_TREE_ONLY))
178 /* case (1)-a and (1)-b */
179 err = err | show_children(e, level+1);
180 else if (level && ls_options & LS_RECURSIVE)
181 /* case (2)-b */
182 err = err | show_children(e, level+1);
7912c070 183 }
6af1f019 184 return err;
aa1c48df
JH
185}
186
66204988 187static int list_one(const char *path)
6d3a5077 188{
6af1f019 189 int err = 0;
66204988 190 struct tree_entry_list *e = find_entry(path);
6af1f019
JH
191 if (!e) {
192 /* traditionally ls-tree does not complain about
193 * missing path. We may change this later to match
194 * what "/bin/ls -a" does, which is to complain.
195 */
196 return err;
197 }
198 err = err | show_entry(e, 0);
199 return err;
6d3a5077
JM
200}
201
6af1f019 202static int list(char **path)
aa1c48df 203{
6af1f019
JH
204 int i;
205 int err = 0;
66204988
JH
206 for (i = 0; path[i]; i++)
207 err = err | list_one(path[i]);
6af1f019 208 return err;
7912c070
PB
209}
210
4d1f1190 211static const char ls_tree_usage[] =
6af1f019 212 "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
aa1c48df 213
7912c070
PB
214int main(int argc, char **argv)
215{
6af1f019
JH
216 static char *path0[] = { "", NULL };
217 char **path;
7912c070
PB
218 unsigned char sha1[20];
219
aa1c48df
JH
220 while (1 < argc && argv[1][0] == '-') {
221 switch (argv[1][1]) {
222 case 'z':
223 line_termination = 0;
224 break;
225 case 'r':
6af1f019
JH
226 ls_options |= LS_RECURSIVE;
227 break;
228 case 'd':
229 ls_options |= LS_TREE_ONLY;
aa1c48df
JH
230 break;
231 default:
0f2303f7 232 usage(ls_tree_usage);
aa1c48df
JH
233 }
234 argc--; argv++;
235 }
236
6d3a5077 237 if (argc < 2)
0f2303f7 238 usage(ls_tree_usage);
3c249c95 239 if (get_sha1(argv[1], sha1) < 0)
0f2303f7 240 usage(ls_tree_usage);
6af1f019
JH
241
242 path = (argc == 2) ? path0 : (argv + 2);
243 prepare_root(sha1);
244 if (list(path) < 0)
2de381f9 245 die("list failed");
7912c070
PB
246 return 0;
247}