]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/ls-tree.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / builtin / ls-tree.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "config.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "object-name.h"
11 #include "object-store.h"
12 #include "blob.h"
13 #include "tree.h"
14 #include "commit.h"
15 #include "quote.h"
16 #include "builtin.h"
17 #include "parse-options.h"
18 #include "pathspec.h"
19
20 static const char * const ls_tree_usage[] = {
21 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
22 NULL
23 };
24
25 static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
26 const enum object_type type, unsigned int padded)
27 {
28 if (type == OBJ_BLOB) {
29 unsigned long size;
30 if (oid_object_info(the_repository, oid, &size) < 0)
31 die(_("could not get object info about '%s'"),
32 oid_to_hex(oid));
33 if (padded)
34 strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
35 else
36 strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
37 } else if (padded) {
38 strbuf_addf(line, "%7s", "-");
39 } else {
40 strbuf_addstr(line, "-");
41 }
42 }
43
44 struct ls_tree_options {
45 unsigned null_termination:1;
46 int abbrev;
47 enum ls_tree_path_options {
48 LS_RECURSIVE = 1 << 0,
49 LS_TREE_ONLY = 1 << 1,
50 LS_SHOW_TREES = 1 << 2,
51 } ls_options;
52 struct pathspec pathspec;
53 int chomp_prefix;
54 const char *ls_tree_prefix;
55 const char *format;
56 };
57
58 struct show_tree_data {
59 struct ls_tree_options *options;
60 unsigned mode;
61 enum object_type type;
62 const struct object_id *oid;
63 const char *pathname;
64 struct strbuf *base;
65 };
66
67 static size_t expand_show_tree(struct strbuf *sb, const char *start,
68 void *context)
69 {
70 struct show_tree_data *data = context;
71 struct ls_tree_options *options = data->options;
72 const char *end;
73 const char *p;
74 unsigned int errlen;
75 size_t len = strbuf_expand_literal_cb(sb, start, NULL);
76
77 if (len)
78 return len;
79 if (*start != '(')
80 die(_("bad ls-tree format: element '%s' does not start with '('"), start);
81
82 end = strchr(start + 1, ')');
83 if (!end)
84 die(_("bad ls-tree format: element '%s' does not end in ')'"), start);
85
86 len = end - start + 1;
87 if (skip_prefix(start, "(objectmode)", &p)) {
88 strbuf_addf(sb, "%06o", data->mode);
89 } else if (skip_prefix(start, "(objecttype)", &p)) {
90 strbuf_addstr(sb, type_name(data->type));
91 } else if (skip_prefix(start, "(objectsize:padded)", &p)) {
92 expand_objectsize(sb, data->oid, data->type, 1);
93 } else if (skip_prefix(start, "(objectsize)", &p)) {
94 expand_objectsize(sb, data->oid, data->type, 0);
95 } else if (skip_prefix(start, "(objectname)", &p)) {
96 strbuf_add_unique_abbrev(sb, data->oid, options->abbrev);
97 } else if (skip_prefix(start, "(path)", &p)) {
98 const char *name = data->base->buf;
99 const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
100 struct strbuf sbuf = STRBUF_INIT;
101 size_t baselen = data->base->len;
102
103 strbuf_addstr(data->base, data->pathname);
104 name = relative_path(data->base->buf, prefix, &sbuf);
105 quote_c_style(name, sb, NULL, 0);
106 strbuf_setlen(data->base, baselen);
107 strbuf_release(&sbuf);
108 } else {
109 errlen = (unsigned long)len;
110 die(_("bad ls-tree format: %%%.*s"), errlen, start);
111 }
112 return len;
113 }
114
115 static int show_recursive(struct ls_tree_options *options, const char *base,
116 size_t baselen, const char *pathname)
117 {
118 int i;
119
120 if (options->ls_options & LS_RECURSIVE)
121 return 1;
122
123 if (!options->pathspec.nr)
124 return 0;
125
126 for (i = 0; i < options->pathspec.nr; i++) {
127 const char *spec = options->pathspec.items[i].match;
128 size_t len, speclen;
129
130 if (strncmp(base, spec, baselen))
131 continue;
132 len = strlen(pathname);
133 spec += baselen;
134 speclen = strlen(spec);
135 if (speclen <= len)
136 continue;
137 if (spec[len] && spec[len] != '/')
138 continue;
139 if (memcmp(pathname, spec, len))
140 continue;
141 return 1;
142 }
143 return 0;
144 }
145
146 static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
147 const char *pathname, unsigned mode, void *context)
148 {
149 struct ls_tree_options *options = context;
150 int recurse = 0;
151 struct strbuf sb = STRBUF_INIT;
152 enum object_type type = object_type(mode);
153 struct show_tree_data cb_data = {
154 .options = options,
155 .mode = mode,
156 .type = type,
157 .oid = oid,
158 .pathname = pathname,
159 .base = base,
160 };
161
162 if (type == OBJ_TREE && show_recursive(options, base->buf, base->len, pathname))
163 recurse = READ_TREE_RECURSIVE;
164 if (type == OBJ_TREE && recurse && !(options->ls_options & LS_SHOW_TREES))
165 return recurse;
166 if (type == OBJ_BLOB && (options->ls_options & LS_TREE_ONLY))
167 return 0;
168
169 strbuf_expand(&sb, options->format, expand_show_tree, &cb_data);
170 strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
171 fwrite(sb.buf, sb.len, 1, stdout);
172 strbuf_release(&sb);
173 return recurse;
174 }
175
176 static int show_tree_common(struct ls_tree_options *options, int *recurse,
177 struct strbuf *base, const char *pathname,
178 enum object_type type)
179 {
180 int ret = -1;
181 *recurse = 0;
182
183 if (type == OBJ_BLOB) {
184 if (options->ls_options & LS_TREE_ONLY)
185 ret = 0;
186 } else if (type == OBJ_TREE &&
187 show_recursive(options, base->buf, base->len, pathname)) {
188 *recurse = READ_TREE_RECURSIVE;
189 if (!(options->ls_options & LS_SHOW_TREES))
190 ret = *recurse;
191 }
192
193 return ret;
194 }
195
196 static void show_tree_common_default_long(struct ls_tree_options *options,
197 struct strbuf *base,
198 const char *pathname,
199 const size_t baselen)
200 {
201 const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
202
203 strbuf_addstr(base, pathname);
204
205 if (options->null_termination) {
206 struct strbuf sb = STRBUF_INIT;
207 const char *name = relative_path(base->buf, prefix, &sb);
208
209 fputs(name, stdout);
210 fputc('\0', stdout);
211
212 strbuf_release(&sb);
213 } else {
214 write_name_quoted_relative(base->buf, prefix, stdout, '\n');
215 }
216
217 strbuf_setlen(base, baselen);
218 }
219
220 static int show_tree_default(const struct object_id *oid, struct strbuf *base,
221 const char *pathname, unsigned mode,
222 void *context)
223 {
224 struct ls_tree_options *options = context;
225 int early;
226 int recurse;
227 enum object_type type = object_type(mode);
228
229 early = show_tree_common(options, &recurse, base, pathname, type);
230 if (early >= 0)
231 return early;
232
233 printf("%06o %s %s\t", mode, type_name(object_type(mode)),
234 repo_find_unique_abbrev(the_repository, oid, options->abbrev));
235 show_tree_common_default_long(options, base, pathname, base->len);
236 return recurse;
237 }
238
239 static int show_tree_long(const struct object_id *oid, struct strbuf *base,
240 const char *pathname, unsigned mode,
241 void *context)
242 {
243 struct ls_tree_options *options = context;
244 int early;
245 int recurse;
246 char size_text[24];
247 enum object_type type = object_type(mode);
248
249 early = show_tree_common(options, &recurse, base, pathname, type);
250 if (early >= 0)
251 return early;
252
253 if (type == OBJ_BLOB) {
254 unsigned long size;
255 if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
256 xsnprintf(size_text, sizeof(size_text), "BAD");
257 else
258 xsnprintf(size_text, sizeof(size_text),
259 "%" PRIuMAX, (uintmax_t)size);
260 } else {
261 xsnprintf(size_text, sizeof(size_text), "-");
262 }
263
264 printf("%06o %s %s %7s\t", mode, type_name(type),
265 repo_find_unique_abbrev(the_repository, oid, options->abbrev),
266 size_text);
267 show_tree_common_default_long(options, base, pathname, base->len);
268 return recurse;
269 }
270
271 static int show_tree_name_only(const struct object_id *oid, struct strbuf *base,
272 const char *pathname, unsigned mode,
273 void *context)
274 {
275 struct ls_tree_options *options = context;
276 int early;
277 int recurse;
278 const size_t baselen = base->len;
279 enum object_type type = object_type(mode);
280 const char *prefix;
281
282 early = show_tree_common(options, &recurse, base, pathname, type);
283 if (early >= 0)
284 return early;
285
286 prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
287 strbuf_addstr(base, pathname);
288 if (options->null_termination) {
289 struct strbuf sb = STRBUF_INIT;
290 const char *name = relative_path(base->buf, prefix, &sb);
291
292 fputs(name, stdout);
293 fputc('\0', stdout);
294
295 strbuf_release(&sb);
296 } else {
297 write_name_quoted_relative(base->buf, prefix, stdout, '\n');
298 }
299 strbuf_setlen(base, baselen);
300 return recurse;
301 }
302
303 static int show_tree_object(const struct object_id *oid, struct strbuf *base,
304 const char *pathname, unsigned mode,
305 void *context)
306 {
307 struct ls_tree_options *options = context;
308 int early;
309 int recurse;
310 enum object_type type = object_type(mode);
311 const char *str;
312
313 early = show_tree_common(options, &recurse, base, pathname, type);
314 if (early >= 0)
315 return early;
316
317 str = repo_find_unique_abbrev(the_repository, oid, options->abbrev);
318 if (options->null_termination) {
319 fputs(str, stdout);
320 fputc('\0', stdout);
321 } else {
322 puts(str);
323 }
324 return recurse;
325 }
326
327 enum ls_tree_cmdmode {
328 MODE_DEFAULT = 0,
329 MODE_LONG,
330 MODE_NAME_ONLY,
331 MODE_NAME_STATUS,
332 MODE_OBJECT_ONLY,
333 };
334
335 struct ls_tree_cmdmode_to_fmt {
336 enum ls_tree_cmdmode mode;
337 const char *const fmt;
338 read_tree_fn_t fn;
339 };
340
341 static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = {
342 {
343 .mode = MODE_DEFAULT,
344 .fmt = "%(objectmode) %(objecttype) %(objectname)%x09%(path)",
345 .fn = show_tree_default,
346 },
347 {
348 .mode = MODE_LONG,
349 .fmt = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
350 .fn = show_tree_long,
351 },
352 {
353 .mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */
354 .fmt = "%(path)",
355 .fn = show_tree_name_only,
356 },
357 {
358 .mode = MODE_OBJECT_ONLY,
359 .fmt = "%(objectname)",
360 .fn = show_tree_object
361 },
362 {
363 /* fallback */
364 .fn = show_tree_default,
365 },
366 };
367
368 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
369 {
370 struct object_id oid;
371 struct tree *tree;
372 int i, full_tree = 0;
373 read_tree_fn_t fn = NULL;
374 enum ls_tree_cmdmode cmdmode = MODE_DEFAULT;
375 int null_termination = 0;
376 struct ls_tree_options options = { 0 };
377 const struct option ls_tree_options[] = {
378 OPT_BIT('d', NULL, &options.ls_options, N_("only show trees"),
379 LS_TREE_ONLY),
380 OPT_BIT('r', NULL, &options.ls_options, N_("recurse into subtrees"),
381 LS_RECURSIVE),
382 OPT_BIT('t', NULL, &options.ls_options, N_("show trees when recursing"),
383 LS_SHOW_TREES),
384 OPT_BOOL('z', NULL, &null_termination,
385 N_("terminate entries with NUL byte")),
386 OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"),
387 MODE_LONG),
388 OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"),
389 MODE_NAME_ONLY),
390 OPT_CMDMODE(0, "name-status", &cmdmode, N_("list only filenames"),
391 MODE_NAME_STATUS),
392 OPT_CMDMODE(0, "object-only", &cmdmode, N_("list only objects"),
393 MODE_OBJECT_ONLY),
394 OPT_SET_INT(0, "full-name", &options.chomp_prefix,
395 N_("use full path names"), 0),
396 OPT_BOOL(0, "full-tree", &full_tree,
397 N_("list entire tree; not just current directory "
398 "(implies --full-name)")),
399 OPT_STRING_F(0, "format", &options.format, N_("format"),
400 N_("format to use for the output"),
401 PARSE_OPT_NONEG),
402 OPT__ABBREV(&options.abbrev),
403 OPT_END()
404 };
405 struct ls_tree_cmdmode_to_fmt *m2f = ls_tree_cmdmode_format;
406 int ret;
407
408 git_config(git_default_config, NULL);
409 options.ls_tree_prefix = prefix;
410 if (prefix)
411 options.chomp_prefix = strlen(prefix);
412
413 argc = parse_options(argc, argv, prefix, ls_tree_options,
414 ls_tree_usage, 0);
415 options.null_termination = null_termination;
416
417 if (full_tree) {
418 options.ls_tree_prefix = prefix = NULL;
419 options.chomp_prefix = 0;
420 }
421 /*
422 * We wanted to detect conflicts between --name-only and
423 * --name-status, but once we're done with that subsequent
424 * code should only need to check the primary name.
425 */
426 if (cmdmode == MODE_NAME_STATUS)
427 cmdmode = MODE_NAME_ONLY;
428
429 /* -d -r should imply -t, but -d by itself should not have to. */
430 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
431 ((LS_TREE_ONLY|LS_RECURSIVE) & options.ls_options))
432 options.ls_options |= LS_SHOW_TREES;
433
434 if (options.format && cmdmode)
435 usage_msg_opt(
436 _("--format can't be combined with other format-altering options"),
437 ls_tree_usage, ls_tree_options);
438 if (argc < 1)
439 usage_with_options(ls_tree_usage, ls_tree_options);
440 if (repo_get_oid(the_repository, argv[0], &oid))
441 die("Not a valid object name %s", argv[0]);
442
443 /*
444 * show_recursive() rolls its own matching code and is
445 * generally ignorant of 'struct pathspec'. The magic mask
446 * cannot be lifted until it is converted to use
447 * match_pathspec() or tree_entry_interesting()
448 */
449 parse_pathspec(&options.pathspec, PATHSPEC_ALL_MAGIC &
450 ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
451 PATHSPEC_PREFER_CWD,
452 prefix, argv + 1);
453 for (i = 0; i < options.pathspec.nr; i++)
454 options.pathspec.items[i].nowildcard_len = options.pathspec.items[i].len;
455 options.pathspec.has_wildcard = 0;
456 tree = parse_tree_indirect(&oid);
457 if (!tree)
458 die("not a tree object");
459 /*
460 * The generic show_tree_fmt() is slower than show_tree(), so
461 * take the fast path if possible.
462 */
463 while (m2f) {
464 if (!m2f->fmt) {
465 fn = options.format ? show_tree_fmt : show_tree_default;
466 } else if (options.format && !strcmp(options.format, m2f->fmt)) {
467 cmdmode = m2f->mode;
468 fn = m2f->fn;
469 } else if (!options.format && cmdmode == m2f->mode) {
470 fn = m2f->fn;
471 } else {
472 m2f++;
473 continue;
474 }
475 break;
476 }
477
478 ret = !!read_tree(the_repository, tree, &options.pathspec, fn, &options);
479 clear_pathspec(&options.pathspec);
480 return ret;
481 }