]>
Commit | Line | Data |
---|---|---|
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" | |
22ddf719 | 9 | #include "quote.h" |
7912c070 | 10 | |
e99d59ff | 11 | static int line_termination = '\n'; |
6af1f019 JH |
12 | #define LS_RECURSIVE 1 |
13 | #define LS_TREE_ONLY 2 | |
14 | static int ls_options = 0; | |
aa1c48df | 15 | |
6af1f019 | 16 | static struct tree_entry_list root_entry; |
aa1c48df | 17 | |
6af1f019 JH |
18 | static void prepare_root(unsigned char *sha1) |
19 | { | |
20 | unsigned char rsha[20]; | |
21 | unsigned long size; | |
22 | void *buf; | |
23 | struct tree *root_tree; | |
24 | ||
25 | 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)); | |
29 | ||
30 | root_tree = lookup_tree(rsha); | |
31 | if (!root_tree) | |
32 | die("Could not read %s", sha1_to_hex(sha1)); | |
33 | ||
34 | /* 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 | } | |
6d3a5077 | 42 | |
6af1f019 | 43 | static int prepare_children(struct tree_entry_list *elem) |
6d3a5077 | 44 | { |
6af1f019 JH |
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; | |
6d3a5077 | 54 | } |
6d3a5077 JM |
55 | return 0; |
56 | } | |
57 | ||
ab1630a3 | 58 | static struct tree_entry_list *find_entry(const char *path, char *pathbuf) |
7912c070 | 59 | { |
66204988 | 60 | const char *next, *slash; |
6af1f019 | 61 | int len; |
ab1630a3 RF |
62 | struct tree_entry_list *elem = &root_entry, *oldelem = NULL; |
63 | ||
64 | *(pathbuf) = '\0'; | |
6af1f019 | 65 | |
66204988 JH |
66 | /* Find tree element, descending from root, that |
67 | * corresponds to the named path, lazily expanding | |
68 | * the tree if possible. | |
69 | */ | |
70 | ||
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 | */ | |
6af1f019 JH |
79 | if (prepare_children(elem)) |
80 | return NULL; | |
81 | ||
66204988 JH |
82 | slash = strchr(path, '/'); |
83 | if (!slash) { | |
84 | len = strlen(path); | |
6da4016a | 85 | next = NULL; |
66204988 JH |
86 | } |
87 | else { | |
88 | next = slash + 1; | |
89 | len = slash - path; | |
90 | } | |
91 | if (len) { | |
ab1630a3 RF |
92 | if (oldelem) { |
93 | pathbuf += sprintf(pathbuf, "%s/", oldelem->name); | |
94 | } | |
95 | ||
66204988 JH |
96 | /* (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; | |
ab1630a3 RF |
111 | |
112 | oldelem = elem; | |
6d3a5077 | 113 | } |
66204988 | 114 | path = next; |
aa1c48df | 115 | } |
aa1c48df | 116 | |
66204988 | 117 | return elem; |
6af1f019 | 118 | } |
6d3a5077 | 119 | |
6af1f019 JH |
120 | static const char *entry_type(struct tree_entry_list *e) |
121 | { | |
122 | return (e->directory ? "tree" : "blob"); | |
123 | } | |
6d3a5077 | 124 | |
6af1f019 | 125 | static const char *entry_hex(struct tree_entry_list *e) |
aa1c48df | 126 | { |
6af1f019 JH |
127 | return sha1_to_hex(e->directory |
128 | ? e->item.tree->object.sha1 | |
129 | : e->item.blob->object.sha1); | |
130 | } | |
6d3a5077 | 131 | |
6af1f019 | 132 | /* forward declaration for mutually recursive routines */ |
ab1630a3 | 133 | static int show_entry(struct tree_entry_list *, int, char *pathbuf); |
aa1c48df | 134 | |
ab1630a3 | 135 | static int show_children(struct tree_entry_list *e, int level, char *pathbuf) |
6af1f019 | 136 | { |
ab1630a3 RF |
137 | int oldlen = strlen(pathbuf); |
138 | ||
139 | if (e != &root_entry) | |
140 | sprintf(pathbuf + oldlen, "%s/", e->name); | |
141 | ||
6af1f019 JH |
142 | 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) { | |
ab1630a3 | 146 | show_entry(e, level, pathbuf); |
6af1f019 JH |
147 | e = e->next; |
148 | } | |
ab1630a3 RF |
149 | |
150 | pathbuf[oldlen] = '\0'; | |
151 | ||
6af1f019 JH |
152 | return 0; |
153 | } | |
0f2303f7 | 154 | |
ab1630a3 | 155 | static int show_entry(struct tree_entry_list *e, int level, char *pathbuf) |
6af1f019 JH |
156 | { |
157 | int err = 0; | |
6d3a5077 | 158 | |
6af1f019 | 159 | if (e != &root_entry) { |
9ef2b3cb | 160 | int pathlen = strlen(pathbuf); |
22ddf719 JH |
161 | printf("%06o %s %s ", |
162 | e->mode, entry_type(e), entry_hex(e)); | |
9ef2b3cb JH |
163 | write_name_quoted(pathbuf, pathlen, e->name, |
164 | line_termination, stdout); | |
6af1f019 JH |
165 | putchar(line_termination); |
166 | } | |
6d3a5077 | 167 | |
6af1f019 JH |
168 | if (e->directory) { |
169 | /* If this is a directory, we have the following cases: | |
170 | * (1) This is the top-level request (explicit path from the | |
171 | * command line, or "root" if there is no command line). | |
172 | * a. Without any flag. We show direct children. We do not | |
173 | * recurse into them. | |
174 | * b. With -r. We do recurse into children. | |
175 | * c. With -d. We do not recurse into children. | |
176 | * (2) We came here because our caller is either (1-a) or | |
177 | * (1-b). | |
178 | * a. Without any flag. We do not show our children (which | |
179 | * are grandchildren for the original request). | |
180 | * b. With -r. We continue to recurse into our children. | |
181 | * c. With -d. We should not have come here to begin with. | |
6d3a5077 | 182 | */ |
6af1f019 JH |
183 | if (level == 0 && !(ls_options & LS_TREE_ONLY)) |
184 | /* case (1)-a and (1)-b */ | |
ab1630a3 | 185 | err = err | show_children(e, level+1, pathbuf); |
6af1f019 JH |
186 | else if (level && ls_options & LS_RECURSIVE) |
187 | /* case (2)-b */ | |
ab1630a3 | 188 | err = err | show_children(e, level+1, pathbuf); |
7912c070 | 189 | } |
6af1f019 | 190 | return err; |
aa1c48df JH |
191 | } |
192 | ||
66204988 | 193 | static int list_one(const char *path) |
6d3a5077 | 194 | { |
6af1f019 | 195 | int err = 0; |
ab1630a3 RF |
196 | char pathbuf[MAXPATHLEN + 1]; |
197 | struct tree_entry_list *e = find_entry(path, pathbuf); | |
6af1f019 JH |
198 | if (!e) { |
199 | /* traditionally ls-tree does not complain about | |
200 | * missing path. We may change this later to match | |
201 | * what "/bin/ls -a" does, which is to complain. | |
202 | */ | |
203 | return err; | |
204 | } | |
ab1630a3 | 205 | err = err | show_entry(e, 0, pathbuf); |
6af1f019 | 206 | return err; |
6d3a5077 JM |
207 | } |
208 | ||
b191fa72 | 209 | static int list(const char **path) |
aa1c48df | 210 | { |
6af1f019 JH |
211 | int i; |
212 | int err = 0; | |
66204988 JH |
213 | for (i = 0; path[i]; i++) |
214 | err = err | list_one(path[i]); | |
6af1f019 | 215 | return err; |
7912c070 PB |
216 | } |
217 | ||
4d1f1190 | 218 | static const char ls_tree_usage[] = |
6af1f019 | 219 | "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]"; |
aa1c48df | 220 | |
b191fa72 | 221 | int main(int argc, const char **argv) |
7912c070 | 222 | { |
b191fa72 JH |
223 | static const char *path0[] = { "", NULL }; |
224 | const char **path; | |
7912c070 | 225 | unsigned char sha1[20]; |
b191fa72 JH |
226 | int nongit = 0; |
227 | const char *prefix = setup_git_directory_gently(&nongit); | |
228 | ||
229 | if (prefix) | |
230 | path0[0] = prefix; | |
7912c070 | 231 | |
aa1c48df JH |
232 | while (1 < argc && argv[1][0] == '-') { |
233 | switch (argv[1][1]) { | |
234 | case 'z': | |
235 | line_termination = 0; | |
236 | break; | |
237 | case 'r': | |
6af1f019 JH |
238 | ls_options |= LS_RECURSIVE; |
239 | break; | |
240 | case 'd': | |
241 | ls_options |= LS_TREE_ONLY; | |
aa1c48df JH |
242 | break; |
243 | default: | |
0f2303f7 | 244 | usage(ls_tree_usage); |
aa1c48df JH |
245 | } |
246 | argc--; argv++; | |
247 | } | |
248 | ||
6d3a5077 | 249 | if (argc < 2) |
0f2303f7 | 250 | usage(ls_tree_usage); |
3c249c95 | 251 | if (get_sha1(argv[1], sha1) < 0) |
0f2303f7 | 252 | usage(ls_tree_usage); |
6af1f019 | 253 | |
b191fa72 JH |
254 | if (argc == 2) |
255 | path = path0; | |
256 | else | |
257 | path = get_pathspec(prefix, argv + 2); | |
258 | ||
6af1f019 JH |
259 | prepare_root(sha1); |
260 | if (list(path) < 0) | |
2de381f9 | 261 | die("list failed"); |
7912c070 PB |
262 | return 0; |
263 | } |