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