Merge branch 'js/merge-tree-3-trees'
[git.git] / builtin / ls-tree.c
blob7bf84b235ce69e63aaf0f11f6dfb82bce0a42eeb
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 "tree.h"
13 #include "path.h"
14 #include "quote.h"
15 #include "parse-options.h"
16 #include "pathspec.h"
18 static const char * const ls_tree_usage[] = {
19 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
20 NULL
23 static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
24 const enum object_type type, unsigned int padded)
26 if (type == OBJ_BLOB) {
27 unsigned long size;
28 if (oid_object_info(the_repository, oid, &size) < 0)
29 die(_("could not get object info about '%s'"),
30 oid_to_hex(oid));
31 if (padded)
32 strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
33 else
34 strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
35 } else if (padded) {
36 strbuf_addf(line, "%7s", "-");
37 } else {
38 strbuf_addstr(line, "-");
42 struct ls_tree_options {
43 unsigned null_termination:1;
44 int abbrev;
45 enum ls_tree_path_options {
46 LS_RECURSIVE = 1 << 0,
47 LS_TREE_ONLY = 1 << 1,
48 LS_SHOW_TREES = 1 << 2,
49 } ls_options;
50 struct pathspec pathspec;
51 const char *prefix;
52 const char *format;
55 static int show_recursive(struct ls_tree_options *options, const char *base,
56 size_t baselen, const char *pathname)
58 int i;
60 if (options->ls_options & LS_RECURSIVE)
61 return 1;
63 if (!options->pathspec.nr)
64 return 0;
66 for (i = 0; i < options->pathspec.nr; i++) {
67 const char *spec = options->pathspec.items[i].match;
68 size_t len, speclen;
70 if (strncmp(base, spec, baselen))
71 continue;
72 len = strlen(pathname);
73 spec += baselen;
74 speclen = strlen(spec);
75 if (speclen <= len)
76 continue;
77 if (spec[len] && spec[len] != '/')
78 continue;
79 if (memcmp(pathname, spec, len))
80 continue;
81 return 1;
83 return 0;
86 static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
87 const char *pathname, unsigned mode, void *context)
89 struct ls_tree_options *options = context;
90 int recurse = 0;
91 struct strbuf sb = STRBUF_INIT;
92 enum object_type type = object_type(mode);
93 const char *format = options->format;
95 if (type == OBJ_TREE && show_recursive(options, base->buf, base->len, pathname))
96 recurse = READ_TREE_RECURSIVE;
97 if (type == OBJ_TREE && recurse && !(options->ls_options & LS_SHOW_TREES))
98 return recurse;
99 if (type == OBJ_BLOB && (options->ls_options & LS_TREE_ONLY))
100 return 0;
102 while (strbuf_expand_step(&sb, &format)) {
103 size_t len;
105 if (skip_prefix(format, "%", &format))
106 strbuf_addch(&sb, '%');
107 else if ((len = strbuf_expand_literal(&sb, format)))
108 format += len;
109 else if (skip_prefix(format, "(objectmode)", &format))
110 strbuf_addf(&sb, "%06o", mode);
111 else if (skip_prefix(format, "(objecttype)", &format))
112 strbuf_addstr(&sb, type_name(type));
113 else if (skip_prefix(format, "(objectsize:padded)", &format))
114 expand_objectsize(&sb, oid, type, 1);
115 else if (skip_prefix(format, "(objectsize)", &format))
116 expand_objectsize(&sb, oid, type, 0);
117 else if (skip_prefix(format, "(objectname)", &format))
118 strbuf_add_unique_abbrev(&sb, oid, options->abbrev);
119 else if (skip_prefix(format, "(path)", &format)) {
120 const char *name;
121 const char *prefix = options->prefix;
122 struct strbuf sbuf = STRBUF_INIT;
123 size_t baselen = base->len;
125 strbuf_addstr(base, pathname);
126 name = relative_path(base->buf, prefix, &sbuf);
127 quote_c_style(name, &sb, NULL, 0);
128 strbuf_setlen(base, baselen);
129 strbuf_release(&sbuf);
130 } else
131 strbuf_expand_bad_format(format, "ls-tree");
133 strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
134 fwrite(sb.buf, sb.len, 1, stdout);
135 strbuf_release(&sb);
136 return recurse;
139 static int show_tree_common(struct ls_tree_options *options, int *recurse,
140 struct strbuf *base, const char *pathname,
141 enum object_type type)
143 int ret = -1;
144 *recurse = 0;
146 if (type == OBJ_BLOB) {
147 if (options->ls_options & LS_TREE_ONLY)
148 ret = 0;
149 } else if (type == OBJ_TREE &&
150 show_recursive(options, base->buf, base->len, pathname)) {
151 *recurse = READ_TREE_RECURSIVE;
152 if (!(options->ls_options & LS_SHOW_TREES))
153 ret = *recurse;
156 return ret;
159 static void show_tree_common_default_long(struct ls_tree_options *options,
160 struct strbuf *base,
161 const char *pathname,
162 const size_t baselen)
164 const char *prefix = options->prefix;
166 strbuf_addstr(base, pathname);
168 if (options->null_termination) {
169 struct strbuf sb = STRBUF_INIT;
170 const char *name = relative_path(base->buf, prefix, &sb);
172 fputs(name, stdout);
173 fputc('\0', stdout);
175 strbuf_release(&sb);
176 } else {
177 write_name_quoted_relative(base->buf, prefix, stdout, '\n');
180 strbuf_setlen(base, baselen);
183 static int show_tree_default(const struct object_id *oid, struct strbuf *base,
184 const char *pathname, unsigned mode,
185 void *context)
187 struct ls_tree_options *options = context;
188 int early;
189 int recurse;
190 enum object_type type = object_type(mode);
192 early = show_tree_common(options, &recurse, base, pathname, type);
193 if (early >= 0)
194 return early;
196 printf("%06o %s %s\t", mode, type_name(object_type(mode)),
197 repo_find_unique_abbrev(the_repository, oid, options->abbrev));
198 show_tree_common_default_long(options, base, pathname, base->len);
199 return recurse;
202 static int show_tree_long(const struct object_id *oid, struct strbuf *base,
203 const char *pathname, unsigned mode,
204 void *context)
206 struct ls_tree_options *options = context;
207 int early;
208 int recurse;
209 char size_text[24];
210 enum object_type type = object_type(mode);
212 early = show_tree_common(options, &recurse, base, pathname, type);
213 if (early >= 0)
214 return early;
216 if (type == OBJ_BLOB) {
217 unsigned long size;
218 if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
219 xsnprintf(size_text, sizeof(size_text), "BAD");
220 else
221 xsnprintf(size_text, sizeof(size_text),
222 "%" PRIuMAX, (uintmax_t)size);
223 } else {
224 xsnprintf(size_text, sizeof(size_text), "-");
227 printf("%06o %s %s %7s\t", mode, type_name(type),
228 repo_find_unique_abbrev(the_repository, oid, options->abbrev),
229 size_text);
230 show_tree_common_default_long(options, base, pathname, base->len);
231 return recurse;
234 static int show_tree_name_only(const struct object_id *oid UNUSED,
235 struct strbuf *base,
236 const char *pathname, unsigned mode,
237 void *context)
239 struct ls_tree_options *options = context;
240 int early;
241 int recurse;
242 const size_t baselen = base->len;
243 enum object_type type = object_type(mode);
244 const char *prefix;
246 early = show_tree_common(options, &recurse, base, pathname, type);
247 if (early >= 0)
248 return early;
250 prefix = options->prefix;
251 strbuf_addstr(base, pathname);
252 if (options->null_termination) {
253 struct strbuf sb = STRBUF_INIT;
254 const char *name = relative_path(base->buf, prefix, &sb);
256 fputs(name, stdout);
257 fputc('\0', stdout);
259 strbuf_release(&sb);
260 } else {
261 write_name_quoted_relative(base->buf, prefix, stdout, '\n');
263 strbuf_setlen(base, baselen);
264 return recurse;
267 static int show_tree_object(const struct object_id *oid, struct strbuf *base,
268 const char *pathname, unsigned mode,
269 void *context)
271 struct ls_tree_options *options = context;
272 int early;
273 int recurse;
274 enum object_type type = object_type(mode);
275 const char *str;
277 early = show_tree_common(options, &recurse, base, pathname, type);
278 if (early >= 0)
279 return early;
281 str = repo_find_unique_abbrev(the_repository, oid, options->abbrev);
282 if (options->null_termination) {
283 fputs(str, stdout);
284 fputc('\0', stdout);
285 } else {
286 puts(str);
288 return recurse;
291 enum ls_tree_cmdmode {
292 MODE_DEFAULT = 0,
293 MODE_LONG,
294 MODE_NAME_ONLY,
295 MODE_NAME_STATUS,
296 MODE_OBJECT_ONLY,
299 struct ls_tree_cmdmode_to_fmt {
300 enum ls_tree_cmdmode mode;
301 const char *const fmt;
302 read_tree_fn_t fn;
305 static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = {
307 .mode = MODE_DEFAULT,
308 .fmt = "%(objectmode) %(objecttype) %(objectname)%x09%(path)",
309 .fn = show_tree_default,
312 .mode = MODE_LONG,
313 .fmt = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
314 .fn = show_tree_long,
317 .mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */
318 .fmt = "%(path)",
319 .fn = show_tree_name_only,
322 .mode = MODE_OBJECT_ONLY,
323 .fmt = "%(objectname)",
324 .fn = show_tree_object
327 /* fallback */
328 .fn = show_tree_default,
332 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
334 struct object_id oid;
335 struct tree *tree;
336 int i, full_tree = 0;
337 int full_name = !prefix || !*prefix;
338 read_tree_fn_t fn = NULL;
339 enum ls_tree_cmdmode cmdmode = MODE_DEFAULT;
340 int null_termination = 0;
341 struct ls_tree_options options = { 0 };
342 const struct option ls_tree_options[] = {
343 OPT_BIT('d', NULL, &options.ls_options, N_("only show trees"),
344 LS_TREE_ONLY),
345 OPT_BIT('r', NULL, &options.ls_options, N_("recurse into subtrees"),
346 LS_RECURSIVE),
347 OPT_BIT('t', NULL, &options.ls_options, N_("show trees when recursing"),
348 LS_SHOW_TREES),
349 OPT_BOOL('z', NULL, &null_termination,
350 N_("terminate entries with NUL byte")),
351 OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"),
352 MODE_LONG),
353 OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"),
354 MODE_NAME_ONLY),
355 OPT_CMDMODE(0, "name-status", &cmdmode, N_("list only filenames"),
356 MODE_NAME_STATUS),
357 OPT_CMDMODE(0, "object-only", &cmdmode, N_("list only objects"),
358 MODE_OBJECT_ONLY),
359 OPT_BOOL(0, "full-name", &full_name, N_("use full path names")),
360 OPT_BOOL(0, "full-tree", &full_tree,
361 N_("list entire tree; not just current directory "
362 "(implies --full-name)")),
363 OPT_STRING_F(0, "format", &options.format, N_("format"),
364 N_("format to use for the output"),
365 PARSE_OPT_NONEG),
366 OPT__ABBREV(&options.abbrev),
367 OPT_END()
369 struct ls_tree_cmdmode_to_fmt *m2f = ls_tree_cmdmode_format;
370 struct object_context obj_context;
371 int ret;
373 git_config(git_default_config, NULL);
375 argc = parse_options(argc, argv, prefix, ls_tree_options,
376 ls_tree_usage, 0);
377 options.null_termination = null_termination;
379 if (full_tree)
380 prefix = NULL;
381 options.prefix = full_name ? NULL : prefix;
384 * We wanted to detect conflicts between --name-only and
385 * --name-status, but once we're done with that subsequent
386 * code should only need to check the primary name.
388 if (cmdmode == MODE_NAME_STATUS)
389 cmdmode = MODE_NAME_ONLY;
391 /* -d -r should imply -t, but -d by itself should not have to. */
392 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
393 ((LS_TREE_ONLY|LS_RECURSIVE) & options.ls_options))
394 options.ls_options |= LS_SHOW_TREES;
396 if (options.format && cmdmode)
397 usage_msg_opt(
398 _("--format can't be combined with other format-altering options"),
399 ls_tree_usage, ls_tree_options);
400 if (argc < 1)
401 usage_with_options(ls_tree_usage, ls_tree_options);
402 if (get_oid_with_context(the_repository, argv[0],
403 GET_OID_HASH_ANY, &oid,
404 &obj_context))
405 die("Not a valid object name %s", argv[0]);
408 * show_recursive() rolls its own matching code and is
409 * generally ignorant of 'struct pathspec'. The magic mask
410 * cannot be lifted until it is converted to use
411 * match_pathspec() or tree_entry_interesting()
413 parse_pathspec(&options.pathspec, PATHSPEC_ALL_MAGIC &
414 ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
415 PATHSPEC_PREFER_CWD,
416 prefix, argv + 1);
417 for (i = 0; i < options.pathspec.nr; i++)
418 options.pathspec.items[i].nowildcard_len = options.pathspec.items[i].len;
419 options.pathspec.has_wildcard = 0;
420 tree = parse_tree_indirect(&oid);
421 if (!tree)
422 die("not a tree object");
424 * The generic show_tree_fmt() is slower than show_tree(), so
425 * take the fast path if possible.
427 while (m2f) {
428 if (!m2f->fmt) {
429 fn = options.format ? show_tree_fmt : show_tree_default;
430 } else if (options.format && !strcmp(options.format, m2f->fmt)) {
431 cmdmode = m2f->mode;
432 fn = m2f->fn;
433 } else if (!options.format && cmdmode == m2f->mode) {
434 fn = m2f->fn;
435 } else {
436 m2f++;
437 continue;
439 break;
442 ret = !!read_tree(the_repository, tree, &options.pathspec, fn, &options);
443 clear_pathspec(&options.pathspec);
444 return ret;