wrapper: use trace2 counters to collect fsync stats
[git.git] / builtin / ls-tree.c
blob53073d64cb01aee43d199de098a6b270a48ba66e
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "builtin.h"
7 #include "config.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "object-name.h"
11 #include "object-store-ll.h"
12 #include "blob.h"
13 #include "tree.h"
14 #include "commit.h"
15 #include "path.h"
16 #include "quote.h"
17 #include "parse-options.h"
18 #include "pathspec.h"
20 static const char * const ls_tree_usage[] = {
21 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
22 NULL
25 static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
26 const enum object_type type, unsigned int padded)
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, "-");
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 const char *prefix;
54 const char *format;
57 static int show_recursive(struct ls_tree_options *options, const char *base,
58 size_t baselen, const char *pathname)
60 int i;
62 if (options->ls_options & LS_RECURSIVE)
63 return 1;
65 if (!options->pathspec.nr)
66 return 0;
68 for (i = 0; i < options->pathspec.nr; i++) {
69 const char *spec = options->pathspec.items[i].match;
70 size_t len, speclen;
72 if (strncmp(base, spec, baselen))
73 continue;
74 len = strlen(pathname);
75 spec += baselen;
76 speclen = strlen(spec);
77 if (speclen <= len)
78 continue;
79 if (spec[len] && spec[len] != '/')
80 continue;
81 if (memcmp(pathname, spec, len))
82 continue;
83 return 1;
85 return 0;
88 static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
89 const char *pathname, unsigned mode, void *context)
91 struct ls_tree_options *options = context;
92 int recurse = 0;
93 struct strbuf sb = STRBUF_INIT;
94 enum object_type type = object_type(mode);
95 const char *format = options->format;
97 if (type == OBJ_TREE && show_recursive(options, base->buf, base->len, pathname))
98 recurse = READ_TREE_RECURSIVE;
99 if (type == OBJ_TREE && recurse && !(options->ls_options & LS_SHOW_TREES))
100 return recurse;
101 if (type == OBJ_BLOB && (options->ls_options & LS_TREE_ONLY))
102 return 0;
104 while (strbuf_expand_step(&sb, &format)) {
105 const char *end;
106 size_t len;
108 if (skip_prefix(format, "%", &format))
109 strbuf_addch(&sb, '%');
110 else if ((len = strbuf_expand_literal(&sb, format)))
111 format += len;
112 else if (*format != '(')
113 die(_("bad ls-tree format: element '%s' "
114 "does not start with '('"), format);
115 else if (!(end = strchr(format + 1, ')')))
116 die(_("bad ls-tree format: element '%s' "
117 "does not end in ')'"), format);
118 else if (skip_prefix(format, "(objectmode)", &format))
119 strbuf_addf(&sb, "%06o", mode);
120 else if (skip_prefix(format, "(objecttype)", &format))
121 strbuf_addstr(&sb, type_name(type));
122 else if (skip_prefix(format, "(objectsize:padded)", &format))
123 expand_objectsize(&sb, oid, type, 1);
124 else if (skip_prefix(format, "(objectsize)", &format))
125 expand_objectsize(&sb, oid, type, 0);
126 else if (skip_prefix(format, "(objectname)", &format))
127 strbuf_add_unique_abbrev(&sb, oid, options->abbrev);
128 else if (skip_prefix(format, "(path)", &format)) {
129 const char *name;
130 const char *prefix = options->prefix;
131 struct strbuf sbuf = STRBUF_INIT;
132 size_t baselen = base->len;
134 strbuf_addstr(base, pathname);
135 name = relative_path(base->buf, prefix, &sbuf);
136 quote_c_style(name, &sb, NULL, 0);
137 strbuf_setlen(base, baselen);
138 strbuf_release(&sbuf);
139 } else
140 die(_("bad ls-tree format: %%%.*s"),
141 (int)(end - format + 1), format);
143 strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
144 fwrite(sb.buf, sb.len, 1, stdout);
145 strbuf_release(&sb);
146 return recurse;
149 static int show_tree_common(struct ls_tree_options *options, int *recurse,
150 struct strbuf *base, const char *pathname,
151 enum object_type type)
153 int ret = -1;
154 *recurse = 0;
156 if (type == OBJ_BLOB) {
157 if (options->ls_options & LS_TREE_ONLY)
158 ret = 0;
159 } else if (type == OBJ_TREE &&
160 show_recursive(options, base->buf, base->len, pathname)) {
161 *recurse = READ_TREE_RECURSIVE;
162 if (!(options->ls_options & LS_SHOW_TREES))
163 ret = *recurse;
166 return ret;
169 static void show_tree_common_default_long(struct ls_tree_options *options,
170 struct strbuf *base,
171 const char *pathname,
172 const size_t baselen)
174 const char *prefix = options->prefix;
176 strbuf_addstr(base, pathname);
178 if (options->null_termination) {
179 struct strbuf sb = STRBUF_INIT;
180 const char *name = relative_path(base->buf, prefix, &sb);
182 fputs(name, stdout);
183 fputc('\0', stdout);
185 strbuf_release(&sb);
186 } else {
187 write_name_quoted_relative(base->buf, prefix, stdout, '\n');
190 strbuf_setlen(base, baselen);
193 static int show_tree_default(const struct object_id *oid, struct strbuf *base,
194 const char *pathname, unsigned mode,
195 void *context)
197 struct ls_tree_options *options = context;
198 int early;
199 int recurse;
200 enum object_type type = object_type(mode);
202 early = show_tree_common(options, &recurse, base, pathname, type);
203 if (early >= 0)
204 return early;
206 printf("%06o %s %s\t", mode, type_name(object_type(mode)),
207 repo_find_unique_abbrev(the_repository, oid, options->abbrev));
208 show_tree_common_default_long(options, base, pathname, base->len);
209 return recurse;
212 static int show_tree_long(const struct object_id *oid, struct strbuf *base,
213 const char *pathname, unsigned mode,
214 void *context)
216 struct ls_tree_options *options = context;
217 int early;
218 int recurse;
219 char size_text[24];
220 enum object_type type = object_type(mode);
222 early = show_tree_common(options, &recurse, base, pathname, type);
223 if (early >= 0)
224 return early;
226 if (type == OBJ_BLOB) {
227 unsigned long size;
228 if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
229 xsnprintf(size_text, sizeof(size_text), "BAD");
230 else
231 xsnprintf(size_text, sizeof(size_text),
232 "%" PRIuMAX, (uintmax_t)size);
233 } else {
234 xsnprintf(size_text, sizeof(size_text), "-");
237 printf("%06o %s %s %7s\t", mode, type_name(type),
238 repo_find_unique_abbrev(the_repository, oid, options->abbrev),
239 size_text);
240 show_tree_common_default_long(options, base, pathname, base->len);
241 return recurse;
244 static int show_tree_name_only(const struct object_id *oid, struct strbuf *base,
245 const char *pathname, unsigned mode,
246 void *context)
248 struct ls_tree_options *options = context;
249 int early;
250 int recurse;
251 const size_t baselen = base->len;
252 enum object_type type = object_type(mode);
253 const char *prefix;
255 early = show_tree_common(options, &recurse, base, pathname, type);
256 if (early >= 0)
257 return early;
259 prefix = options->prefix;
260 strbuf_addstr(base, pathname);
261 if (options->null_termination) {
262 struct strbuf sb = STRBUF_INIT;
263 const char *name = relative_path(base->buf, prefix, &sb);
265 fputs(name, stdout);
266 fputc('\0', stdout);
268 strbuf_release(&sb);
269 } else {
270 write_name_quoted_relative(base->buf, prefix, stdout, '\n');
272 strbuf_setlen(base, baselen);
273 return recurse;
276 static int show_tree_object(const struct object_id *oid, struct strbuf *base,
277 const char *pathname, unsigned mode,
278 void *context)
280 struct ls_tree_options *options = context;
281 int early;
282 int recurse;
283 enum object_type type = object_type(mode);
284 const char *str;
286 early = show_tree_common(options, &recurse, base, pathname, type);
287 if (early >= 0)
288 return early;
290 str = repo_find_unique_abbrev(the_repository, oid, options->abbrev);
291 if (options->null_termination) {
292 fputs(str, stdout);
293 fputc('\0', stdout);
294 } else {
295 puts(str);
297 return recurse;
300 enum ls_tree_cmdmode {
301 MODE_DEFAULT = 0,
302 MODE_LONG,
303 MODE_NAME_ONLY,
304 MODE_NAME_STATUS,
305 MODE_OBJECT_ONLY,
308 struct ls_tree_cmdmode_to_fmt {
309 enum ls_tree_cmdmode mode;
310 const char *const fmt;
311 read_tree_fn_t fn;
314 static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = {
316 .mode = MODE_DEFAULT,
317 .fmt = "%(objectmode) %(objecttype) %(objectname)%x09%(path)",
318 .fn = show_tree_default,
321 .mode = MODE_LONG,
322 .fmt = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
323 .fn = show_tree_long,
326 .mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */
327 .fmt = "%(path)",
328 .fn = show_tree_name_only,
331 .mode = MODE_OBJECT_ONLY,
332 .fmt = "%(objectname)",
333 .fn = show_tree_object
336 /* fallback */
337 .fn = show_tree_default,
341 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
343 struct object_id oid;
344 struct tree *tree;
345 int i, full_tree = 0;
346 int chomp_prefix = prefix && *prefix;
347 read_tree_fn_t fn = NULL;
348 enum ls_tree_cmdmode cmdmode = MODE_DEFAULT;
349 int null_termination = 0;
350 struct ls_tree_options options = { 0 };
351 const struct option ls_tree_options[] = {
352 OPT_BIT('d', NULL, &options.ls_options, N_("only show trees"),
353 LS_TREE_ONLY),
354 OPT_BIT('r', NULL, &options.ls_options, N_("recurse into subtrees"),
355 LS_RECURSIVE),
356 OPT_BIT('t', NULL, &options.ls_options, N_("show trees when recursing"),
357 LS_SHOW_TREES),
358 OPT_BOOL('z', NULL, &null_termination,
359 N_("terminate entries with NUL byte")),
360 OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"),
361 MODE_LONG),
362 OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"),
363 MODE_NAME_ONLY),
364 OPT_CMDMODE(0, "name-status", &cmdmode, N_("list only filenames"),
365 MODE_NAME_STATUS),
366 OPT_CMDMODE(0, "object-only", &cmdmode, N_("list only objects"),
367 MODE_OBJECT_ONLY),
368 OPT_SET_INT(0, "full-name", &chomp_prefix,
369 N_("use full path names"), 0),
370 OPT_BOOL(0, "full-tree", &full_tree,
371 N_("list entire tree; not just current directory "
372 "(implies --full-name)")),
373 OPT_STRING_F(0, "format", &options.format, N_("format"),
374 N_("format to use for the output"),
375 PARSE_OPT_NONEG),
376 OPT__ABBREV(&options.abbrev),
377 OPT_END()
379 struct ls_tree_cmdmode_to_fmt *m2f = ls_tree_cmdmode_format;
380 int ret;
382 git_config(git_default_config, NULL);
384 argc = parse_options(argc, argv, prefix, ls_tree_options,
385 ls_tree_usage, 0);
386 options.null_termination = null_termination;
388 if (full_tree)
389 prefix = NULL;
390 options.prefix = chomp_prefix ? prefix : NULL;
393 * We wanted to detect conflicts between --name-only and
394 * --name-status, but once we're done with that subsequent
395 * code should only need to check the primary name.
397 if (cmdmode == MODE_NAME_STATUS)
398 cmdmode = MODE_NAME_ONLY;
400 /* -d -r should imply -t, but -d by itself should not have to. */
401 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
402 ((LS_TREE_ONLY|LS_RECURSIVE) & options.ls_options))
403 options.ls_options |= LS_SHOW_TREES;
405 if (options.format && cmdmode)
406 usage_msg_opt(
407 _("--format can't be combined with other format-altering options"),
408 ls_tree_usage, ls_tree_options);
409 if (argc < 1)
410 usage_with_options(ls_tree_usage, ls_tree_options);
411 if (repo_get_oid(the_repository, argv[0], &oid))
412 die("Not a valid object name %s", argv[0]);
415 * show_recursive() rolls its own matching code and is
416 * generally ignorant of 'struct pathspec'. The magic mask
417 * cannot be lifted until it is converted to use
418 * match_pathspec() or tree_entry_interesting()
420 parse_pathspec(&options.pathspec, PATHSPEC_ALL_MAGIC &
421 ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
422 PATHSPEC_PREFER_CWD,
423 prefix, argv + 1);
424 for (i = 0; i < options.pathspec.nr; i++)
425 options.pathspec.items[i].nowildcard_len = options.pathspec.items[i].len;
426 options.pathspec.has_wildcard = 0;
427 tree = parse_tree_indirect(&oid);
428 if (!tree)
429 die("not a tree object");
431 * The generic show_tree_fmt() is slower than show_tree(), so
432 * take the fast path if possible.
434 while (m2f) {
435 if (!m2f->fmt) {
436 fn = options.format ? show_tree_fmt : show_tree_default;
437 } else if (options.format && !strcmp(options.format, m2f->fmt)) {
438 cmdmode = m2f->mode;
439 fn = m2f->fn;
440 } else if (!options.format && cmdmode == m2f->mode) {
441 fn = m2f->fn;
442 } else {
443 m2f++;
444 continue;
446 break;
449 ret = !!read_tree(the_repository, tree, &options.pathspec, fn, &options);
450 clear_pathspec(&options.pathspec);
451 return ret;