blob: d9f15e349cb833401eea38d21fb050b10f9678d4 [file] [log] [blame]
Petr Baudis7912c072005-04-13 09:02:341/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
Junio C Hamano6af1f012005-05-28 07:05:387#include "blob.h"
8#include "tree.h"
Junio C Hamano22ddf712005-10-15 04:56:469#include "quote.h"
Petr Baudis7912c072005-04-13 09:02:3410
Linus Torvaldse99d59f2005-05-20 18:46:1011static int line_termination = '\n';
Junio C Hamano6af1f012005-05-28 07:05:3812#define LS_RECURSIVE 1
13#define LS_TREE_ONLY 2
14static int ls_options = 0;
Junio C Hamanoaa1c48d2005-04-15 15:37:0515
Junio C Hamano6af1f012005-05-28 07:05:3816static struct tree_entry_list root_entry;
Junio C Hamanoaa1c48d2005-04-15 15:37:0517
Junio C Hamano6af1f012005-05-28 07:05:3818static void prepare_root(unsigned char *sha1)
Jason McMullan6d3a5072005-05-26 17:52:5019{
Junio C Hamano6af1f012005-05-28 07:05:3820 unsigned char rsha[20];
Petr Baudis7912c072005-04-13 09:02:3421 unsigned long size;
Junio C Hamano6af1f012005-05-28 07:05:3822 void *buf;
23 struct tree *root_tree;
Jason McMullan6d3a5072005-05-26 17:52:5024
Junio C Hamano6af1f012005-05-28 07:05:3825 buf = read_object_with_reference(sha1, "tree", &size, rsha);
26 free(buf);
27 if (!buf)
28 die("Could not read %s", sha1_to_hex(sha1));
Jason McMullan6d3a5072005-05-26 17:52:5029
Junio C Hamano6af1f012005-05-28 07:05:3830 root_tree = lookup_tree(rsha);
31 if (!root_tree)
32 die("Could not read %s", sha1_to_hex(sha1));
Petr Baudis7912c072005-04-13 09:02:3433
Junio C Hamano6af1f012005-05-28 07:05:3834 /* Prepare a fake entry */
35 root_entry.directory = 1;
36 root_entry.executable = root_entry.symlink = 0;
37 root_entry.mode = S_IFDIR;
38 root_entry.name = "";
39 root_entry.item.tree = root_tree;
40 root_entry.parent = NULL;
41}
42
43static int prepare_children(struct tree_entry_list *elem)
44{
45 if (!elem->directory)
46 return -1;
47 if (!elem->item.tree->object.parsed) {
48 struct tree_entry_list *e;
49 if (parse_tree(elem->item.tree))
50 return -1;
51 /* Set up the parent link */
52 for (e = elem->item.tree->entries; e; e = e->next)
53 e->parent = elem;
54 }
Petr Baudis7912c072005-04-13 09:02:3455 return 0;
56}
57
robfitz@273k.netab1630a2005-10-07 23:54:0658static struct tree_entry_list *find_entry(const char *path, char *pathbuf)
Junio C Hamano6af1f012005-05-28 07:05:3859{
Junio C Hamano66204982005-06-01 01:46:4760 const char *next, *slash;
Junio C Hamano6af1f012005-05-28 07:05:3861 int len;
robfitz@273k.netab1630a2005-10-07 23:54:0662 struct tree_entry_list *elem = &root_entry, *oldelem = NULL;
63
64 *(pathbuf) = '\0';
Junio C Hamano6af1f012005-05-28 07:05:3865
Junio C Hamano6af1f012005-05-28 07:05:3866 /* Find tree element, descending from root, that
67 * corresponds to the named path, lazily expanding
68 * the tree if possible.
69 */
Junio C Hamano66204982005-06-01 01:46:4770
71 while (path) {
72 /* The fact we still have path means that the caller
73 * wants us to make sure that elem at this point is a
74 * directory, and possibly descend into it. Even what
75 * is left is just trailing slashes, we loop back to
76 * here, and this call to prepare_children() will
77 * catch elem not being a tree. Nice.
78 */
79 if (prepare_children(elem))
80 return NULL;
81
82 slash = strchr(path, '/');
83 if (!slash) {
84 len = strlen(path);
Linus Torvalds6da40162005-07-03 17:10:4585 next = NULL;
Junio C Hamano66204982005-06-01 01:46:4786 }
87 else {
88 next = slash + 1;
89 len = slash - path;
90 }
91 if (len) {
robfitz@273k.netab1630a2005-10-07 23:54:0692 if (oldelem) {
93 pathbuf += sprintf(pathbuf, "%s/", oldelem->name);
94 }
95
Junio C Hamano66204982005-06-01 01:46:4796 /* (len == 0) if the original path was "drivers/char/"
97 * and we have run already two rounds, having elem
98 * pointing at the drivers/char directory.
99 */
100 elem = elem->item.tree->entries;
101 while (elem) {
102 if ((strlen(elem->name) == len) &&
103 !strncmp(elem->name, path, len)) {
104 /* found */
105 break;
106 }
107 elem = elem->next;
108 }
109 if (!elem)
110 return NULL;
robfitz@273k.netab1630a2005-10-07 23:54:06111
112 oldelem = elem;
Junio C Hamano66204982005-06-01 01:46:47113 }
114 path = next;
Junio C Hamano6af1f012005-05-28 07:05:38115 }
Junio C Hamano66204982005-06-01 01:46:47116
117 return elem;
Junio C Hamano6af1f012005-05-28 07:05:38118}
119
Junio C Hamano6af1f012005-05-28 07:05:38120static const char *entry_type(struct tree_entry_list *e)
121{
122 return (e->directory ? "tree" : "blob");
123}
124
125static const char *entry_hex(struct tree_entry_list *e)
126{
127 return sha1_to_hex(e->directory
128 ? e->item.tree->object.sha1
129 : e->item.blob->object.sha1);
130}
131
132/* forward declaration for mutually recursive routines */
robfitz@273k.netab1630a2005-10-07 23:54:06133static int show_entry(struct tree_entry_list *, int, char *pathbuf);
Junio C Hamano6af1f012005-05-28 07:05:38134
robfitz@273k.netab1630a2005-10-07 23:54:06135static int show_children(struct tree_entry_list *e, int level, char *pathbuf)
Junio C Hamano6af1f012005-05-28 07:05:38136{
robfitz@273k.netab1630a2005-10-07 23:54:06137 int oldlen = strlen(pathbuf);
138
139 if (e != &root_entry)
140 sprintf(pathbuf + oldlen, "%s/", e->name);
141
Junio C Hamano6af1f012005-05-28 07:05:38142 if (prepare_children(e))
143 die("internal error: ls-tree show_children called with non tree");
144 e = e->item.tree->entries;
145 while (e) {
robfitz@273k.netab1630a2005-10-07 23:54:06146 show_entry(e, level, pathbuf);
Junio C Hamano6af1f012005-05-28 07:05:38147 e = e->next;
148 }
robfitz@273k.netab1630a2005-10-07 23:54:06149
150 pathbuf[oldlen] = '\0';
151
Junio C Hamano6af1f012005-05-28 07:05:38152 return 0;
153}
154
robfitz@273k.netab1630a2005-10-07 23:54:06155static int show_entry(struct tree_entry_list *e, int level, char *pathbuf)
Junio C Hamano6af1f012005-05-28 07:05:38156{
157 int err = 0;
158
159 if (e != &root_entry) {
Junio C Hamano22ddf712005-10-15 04:56:46160 printf("%06o %s %s ",
161 e->mode, entry_type(e), entry_hex(e));
162 write_name_quoted(pathbuf, e->name, line_termination, stdout);
Junio C Hamano6af1f012005-05-28 07:05:38163 putchar(line_termination);
164 }
165
166 if (e->directory) {
167 /* If this is a directory, we have the following cases:
168 * (1) This is the top-level request (explicit path from the
169 * command line, or "root" if there is no command line).
170 * a. Without any flag. We show direct children. We do not
171 * recurse into them.
172 * b. With -r. We do recurse into children.
173 * c. With -d. We do not recurse into children.
174 * (2) We came here because our caller is either (1-a) or
175 * (1-b).
176 * a. Without any flag. We do not show our children (which
177 * are grandchildren for the original request).
178 * b. With -r. We continue to recurse into our children.
179 * c. With -d. We should not have come here to begin with.
180 */
181 if (level == 0 && !(ls_options & LS_TREE_ONLY))
182 /* case (1)-a and (1)-b */
robfitz@273k.netab1630a2005-10-07 23:54:06183 err = err | show_children(e, level+1, pathbuf);
Junio C Hamano6af1f012005-05-28 07:05:38184 else if (level && ls_options & LS_RECURSIVE)
185 /* case (2)-b */
robfitz@273k.netab1630a2005-10-07 23:54:06186 err = err | show_children(e, level+1, pathbuf);
Junio C Hamano6af1f012005-05-28 07:05:38187 }
188 return err;
189}
190
Junio C Hamano66204982005-06-01 01:46:47191static int list_one(const char *path)
Junio C Hamano6af1f012005-05-28 07:05:38192{
193 int err = 0;
robfitz@273k.netab1630a2005-10-07 23:54:06194 char pathbuf[MAXPATHLEN + 1];
195 struct tree_entry_list *e = find_entry(path, pathbuf);
Junio C Hamano6af1f012005-05-28 07:05:38196 if (!e) {
197 /* traditionally ls-tree does not complain about
198 * missing path. We may change this later to match
199 * what "/bin/ls -a" does, which is to complain.
200 */
201 return err;
202 }
robfitz@273k.netab1630a2005-10-07 23:54:06203 err = err | show_entry(e, 0, pathbuf);
Junio C Hamano6af1f012005-05-28 07:05:38204 return err;
205}
206
207static int list(char **path)
208{
209 int i;
210 int err = 0;
Junio C Hamano66204982005-06-01 01:46:47211 for (i = 0; path[i]; i++)
212 err = err | list_one(path[i]);
Junio C Hamano6af1f012005-05-28 07:05:38213 return err;
214}
215
Petr Baudis4d1f1192005-07-29 09:01:26216static const char ls_tree_usage[] =
Junio C Hamano6af1f012005-05-28 07:05:38217 "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
Junio C Hamanoaa1c48d2005-04-15 15:37:05218
Petr Baudis7912c072005-04-13 09:02:34219int main(int argc, char **argv)
220{
Junio C Hamano6af1f012005-05-28 07:05:38221 static char *path0[] = { "", NULL };
222 char **path;
Petr Baudis7912c072005-04-13 09:02:34223 unsigned char sha1[20];
224
Junio C Hamanoaa1c48d2005-04-15 15:37:05225 while (1 < argc && argv[1][0] == '-') {
226 switch (argv[1][1]) {
227 case 'z':
228 line_termination = 0;
229 break;
230 case 'r':
Junio C Hamano6af1f012005-05-28 07:05:38231 ls_options |= LS_RECURSIVE;
232 break;
233 case 'd':
234 ls_options |= LS_TREE_ONLY;
Junio C Hamanoaa1c48d2005-04-15 15:37:05235 break;
236 default:
Junio C Hamano0f2303f2005-04-16 20:57:39237 usage(ls_tree_usage);
Junio C Hamanoaa1c48d2005-04-15 15:37:05238 }
239 argc--; argv++;
240 }
241
Jason McMullan6d3a5072005-05-26 17:52:50242 if (argc < 2)
Junio C Hamano0f2303f2005-04-16 20:57:39243 usage(ls_tree_usage);
Linus Torvalds3c249c92005-05-01 23:36:56244 if (get_sha1(argv[1], sha1) < 0)
Junio C Hamano0f2303f2005-04-16 20:57:39245 usage(ls_tree_usage);
Junio C Hamano6af1f012005-05-28 07:05:38246
247 path = (argc == 2) ? path0 : (argv + 2);
248 prepare_root(sha1);
249 if (list(path) < 0)
Petr Baudis2de381f2005-04-13 09:28:48250 die("list failed");
Petr Baudis7912c072005-04-13 09:02:34251 return 0;
252}