Merge branch 'ps/cat-file-null-output'
[git/debian.git] / builtin / ls-tree.c
blob077977a46100327e9d611e8badb9d0c9190de33f
1 /*
2 * GIT - The information manager from hell
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"
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 int chomp_prefix;
54 const char *ls_tree_prefix;
55 const char *format;
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;
67 static size_t expand_show_tree(struct strbuf *sb, const char *start,
68 void *context)
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);
77 if (len)
78 return len;
79 if (*start != '(')
80 die(_("bad ls-tree format: element '%s' does not start with '('"), start);
82 end = strchr(start + 1, ')');
83 if (!end)
84 die(_("bad ls-tree format: element '%s' does not end in ')'"), start);
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;
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);
112 return len;
115 static int show_recursive(struct ls_tree_options *options, const char *base,
116 size_t baselen, const char *pathname)
118 int i;
120 if (options->ls_options & LS_RECURSIVE)
121 return 1;
123 if (!options->pathspec.nr)
124 return 0;
126 for (i = 0; i < options->pathspec.nr; i++) {
127 const char *spec = options->pathspec.items[i].match;
128 size_t len, speclen;
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;
143 return 0;
146 static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
147 const char *pathname, unsigned mode, void *context)
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,
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;
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;
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)
180 int ret = -1;
181 *recurse = 0;
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;
193 return ret;
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)
201 const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
203 strbuf_addstr(base, pathname);
205 if (options->null_termination) {
206 struct strbuf sb = STRBUF_INIT;
207 const char *name = relative_path(base->buf, prefix, &sb);
209 fputs(name, stdout);
210 fputc('\0', stdout);
212 strbuf_release(&sb);
213 } else {
214 write_name_quoted_relative(base->buf, prefix, stdout, '\n');
217 strbuf_setlen(base, baselen);
220 static int show_tree_default(const struct object_id *oid, struct strbuf *base,
221 const char *pathname, unsigned mode,
222 void *context)
224 struct ls_tree_options *options = context;
225 int early;
226 int recurse;
227 enum object_type type = object_type(mode);
229 early = show_tree_common(options, &recurse, base, pathname, type);
230 if (early >= 0)
231 return early;
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;
239 static int show_tree_long(const struct object_id *oid, struct strbuf *base,
240 const char *pathname, unsigned mode,
241 void *context)
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);
249 early = show_tree_common(options, &recurse, base, pathname, type);
250 if (early >= 0)
251 return early;
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), "-");
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;
271 static int show_tree_name_only(const struct object_id *oid, struct strbuf *base,
272 const char *pathname, unsigned mode,
273 void *context)
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;
282 early = show_tree_common(options, &recurse, base, pathname, type);
283 if (early >= 0)
284 return early;
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);
292 fputs(name, stdout);
293 fputc('\0', stdout);
295 strbuf_release(&sb);
296 } else {
297 write_name_quoted_relative(base->buf, prefix, stdout, '\n');
299 strbuf_setlen(base, baselen);
300 return recurse;
303 static int show_tree_object(const struct object_id *oid, struct strbuf *base,
304 const char *pathname, unsigned mode,
305 void *context)
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;
313 early = show_tree_common(options, &recurse, base, pathname, type);
314 if (early >= 0)
315 return early;
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);
324 return recurse;
327 enum ls_tree_cmdmode {
328 MODE_DEFAULT = 0,
329 MODE_LONG,
330 MODE_NAME_ONLY,
331 MODE_NAME_STATUS,
332 MODE_OBJECT_ONLY,
335 struct ls_tree_cmdmode_to_fmt {
336 enum ls_tree_cmdmode mode;
337 const char *const fmt;
338 read_tree_fn_t fn;
341 static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = {
343 .mode = MODE_DEFAULT,
344 .fmt = "%(objectmode) %(objecttype) %(objectname)%x09%(path)",
345 .fn = show_tree_default,
348 .mode = MODE_LONG,
349 .fmt = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
350 .fn = show_tree_long,
353 .mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */
354 .fmt = "%(path)",
355 .fn = show_tree_name_only,
358 .mode = MODE_OBJECT_ONLY,
359 .fmt = "%(objectname)",
360 .fn = show_tree_object
363 /* fallback */
364 .fn = show_tree_default,
368 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
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()
405 struct ls_tree_cmdmode_to_fmt *m2f = ls_tree_cmdmode_format;
406 int ret;
408 git_config(git_default_config, NULL);
409 options.ls_tree_prefix = prefix;
410 if (prefix)
411 options.chomp_prefix = strlen(prefix);
413 argc = parse_options(argc, argv, prefix, ls_tree_options,
414 ls_tree_usage, 0);
415 options.null_termination = null_termination;
417 if (full_tree) {
418 options.ls_tree_prefix = prefix = NULL;
419 options.chomp_prefix = 0;
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.
426 if (cmdmode == MODE_NAME_STATUS)
427 cmdmode = MODE_NAME_ONLY;
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;
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]);
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()
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");
460 * The generic show_tree_fmt() is slower than show_tree(), so
461 * take the fast path if possible.
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;
475 break;
478 ret = !!read_tree(the_repository, tree, &options.pathspec, fn, &options);
479 clear_pathspec(&options.pathspec);
480 return ret;