2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 #include "object-store.h"
14 #include "parse-options.h"
17 static const char * const ls_tree_usage
[] = {
18 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
22 static void expand_objectsize(struct strbuf
*line
, const struct object_id
*oid
,
23 const enum object_type type
, unsigned int padded
)
25 if (type
== OBJ_BLOB
) {
27 if (oid_object_info(the_repository
, oid
, &size
) < 0)
28 die(_("could not get object info about '%s'"),
31 strbuf_addf(line
, "%7"PRIuMAX
, (uintmax_t)size
);
33 strbuf_addf(line
, "%"PRIuMAX
, (uintmax_t)size
);
35 strbuf_addf(line
, "%7s", "-");
37 strbuf_addstr(line
, "-");
41 struct ls_tree_options
{
42 unsigned null_termination
:1;
44 enum ls_tree_path_options
{
45 LS_RECURSIVE
= 1 << 0,
46 LS_TREE_ONLY
= 1 << 1,
47 LS_SHOW_TREES
= 1 << 2,
49 struct pathspec pathspec
;
51 const char *ls_tree_prefix
;
55 struct show_tree_data
{
56 struct ls_tree_options
*options
;
58 enum object_type type
;
59 const struct object_id
*oid
;
64 static size_t expand_show_tree(struct strbuf
*sb
, const char *start
,
67 struct show_tree_data
*data
= context
;
68 struct ls_tree_options
*options
= data
->options
;
72 size_t len
= strbuf_expand_literal_cb(sb
, start
, NULL
);
77 die(_("bad ls-tree format: element '%s' does not start with '('"), start
);
79 end
= strchr(start
+ 1, ')');
81 die(_("bad ls-tree format: element '%s' does not end in ')'"), start
);
83 len
= end
- start
+ 1;
84 if (skip_prefix(start
, "(objectmode)", &p
)) {
85 strbuf_addf(sb
, "%06o", data
->mode
);
86 } else if (skip_prefix(start
, "(objecttype)", &p
)) {
87 strbuf_addstr(sb
, type_name(data
->type
));
88 } else if (skip_prefix(start
, "(objectsize:padded)", &p
)) {
89 expand_objectsize(sb
, data
->oid
, data
->type
, 1);
90 } else if (skip_prefix(start
, "(objectsize)", &p
)) {
91 expand_objectsize(sb
, data
->oid
, data
->type
, 0);
92 } else if (skip_prefix(start
, "(objectname)", &p
)) {
93 strbuf_add_unique_abbrev(sb
, data
->oid
, options
->abbrev
);
94 } else if (skip_prefix(start
, "(path)", &p
)) {
95 const char *name
= data
->base
->buf
;
96 const char *prefix
= options
->chomp_prefix
? options
->ls_tree_prefix
: NULL
;
97 struct strbuf sbuf
= STRBUF_INIT
;
98 size_t baselen
= data
->base
->len
;
100 strbuf_addstr(data
->base
, data
->pathname
);
101 name
= relative_path(data
->base
->buf
, prefix
, &sbuf
);
102 quote_c_style(name
, sb
, NULL
, 0);
103 strbuf_setlen(data
->base
, baselen
);
104 strbuf_release(&sbuf
);
106 errlen
= (unsigned long)len
;
107 die(_("bad ls-tree format: %%%.*s"), errlen
, start
);
112 static int show_recursive(struct ls_tree_options
*options
, const char *base
,
113 size_t baselen
, const char *pathname
)
117 if (options
->ls_options
& LS_RECURSIVE
)
120 if (!options
->pathspec
.nr
)
123 for (i
= 0; i
< options
->pathspec
.nr
; i
++) {
124 const char *spec
= options
->pathspec
.items
[i
].match
;
127 if (strncmp(base
, spec
, baselen
))
129 len
= strlen(pathname
);
131 speclen
= strlen(spec
);
134 if (spec
[len
] && spec
[len
] != '/')
136 if (memcmp(pathname
, spec
, len
))
143 static int show_tree_fmt(const struct object_id
*oid
, struct strbuf
*base
,
144 const char *pathname
, unsigned mode
, void *context
)
146 struct ls_tree_options
*options
= context
;
148 struct strbuf sb
= STRBUF_INIT
;
149 enum object_type type
= object_type(mode
);
150 struct show_tree_data cb_data
= {
155 .pathname
= pathname
,
159 if (type
== OBJ_TREE
&& show_recursive(options
, base
->buf
, base
->len
, pathname
))
160 recurse
= READ_TREE_RECURSIVE
;
161 if (type
== OBJ_TREE
&& recurse
&& !(options
->ls_options
& LS_SHOW_TREES
))
163 if (type
== OBJ_BLOB
&& (options
->ls_options
& LS_TREE_ONLY
))
166 strbuf_expand(&sb
, options
->format
, expand_show_tree
, &cb_data
);
167 strbuf_addch(&sb
, options
->null_termination
? '\0' : '\n');
168 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
173 static int show_tree_common(struct ls_tree_options
*options
, int *recurse
,
174 struct strbuf
*base
, const char *pathname
,
175 enum object_type type
)
180 if (type
== OBJ_BLOB
) {
181 if (options
->ls_options
& LS_TREE_ONLY
)
183 } else if (type
== OBJ_TREE
&&
184 show_recursive(options
, base
->buf
, base
->len
, pathname
)) {
185 *recurse
= READ_TREE_RECURSIVE
;
186 if (!(options
->ls_options
& LS_SHOW_TREES
))
193 static void show_tree_common_default_long(struct ls_tree_options
*options
,
195 const char *pathname
,
196 const size_t baselen
)
198 const char *prefix
= options
->chomp_prefix
? options
->ls_tree_prefix
: NULL
;
200 strbuf_addstr(base
, pathname
);
202 if (options
->null_termination
) {
203 struct strbuf sb
= STRBUF_INIT
;
204 const char *name
= relative_path(base
->buf
, prefix
, &sb
);
211 write_name_quoted_relative(base
->buf
, prefix
, stdout
, '\n');
214 strbuf_setlen(base
, baselen
);
217 static int show_tree_default(const struct object_id
*oid
, struct strbuf
*base
,
218 const char *pathname
, unsigned mode
,
221 struct ls_tree_options
*options
= context
;
224 enum object_type type
= object_type(mode
);
226 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
230 printf("%06o %s %s\t", mode
, type_name(object_type(mode
)),
231 find_unique_abbrev(oid
, options
->abbrev
));
232 show_tree_common_default_long(options
, base
, pathname
, base
->len
);
236 static int show_tree_long(const struct object_id
*oid
, struct strbuf
*base
,
237 const char *pathname
, unsigned mode
,
240 struct ls_tree_options
*options
= context
;
244 enum object_type type
= object_type(mode
);
246 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
250 if (type
== OBJ_BLOB
) {
252 if (oid_object_info(the_repository
, oid
, &size
) == OBJ_BAD
)
253 xsnprintf(size_text
, sizeof(size_text
), "BAD");
255 xsnprintf(size_text
, sizeof(size_text
),
256 "%" PRIuMAX
, (uintmax_t)size
);
258 xsnprintf(size_text
, sizeof(size_text
), "-");
261 printf("%06o %s %s %7s\t", mode
, type_name(type
),
262 find_unique_abbrev(oid
, options
->abbrev
), size_text
);
263 show_tree_common_default_long(options
, base
, pathname
, base
->len
);
267 static int show_tree_name_only(const struct object_id
*oid
, struct strbuf
*base
,
268 const char *pathname
, unsigned mode
,
271 struct ls_tree_options
*options
= context
;
274 const size_t baselen
= base
->len
;
275 enum object_type type
= object_type(mode
);
278 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
282 prefix
= options
->chomp_prefix
? options
->ls_tree_prefix
: NULL
;
283 strbuf_addstr(base
, pathname
);
284 if (options
->null_termination
) {
285 struct strbuf sb
= STRBUF_INIT
;
286 const char *name
= relative_path(base
->buf
, prefix
, &sb
);
293 write_name_quoted_relative(base
->buf
, prefix
, stdout
, '\n');
295 strbuf_setlen(base
, baselen
);
299 static int show_tree_object(const struct object_id
*oid
, struct strbuf
*base
,
300 const char *pathname
, unsigned mode
,
303 struct ls_tree_options
*options
= context
;
306 enum object_type type
= object_type(mode
);
309 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
313 str
= find_unique_abbrev(oid
, options
->abbrev
);
314 if (options
->null_termination
) {
323 enum ls_tree_cmdmode
{
331 struct ls_tree_cmdmode_to_fmt
{
332 enum ls_tree_cmdmode mode
;
333 const char *const fmt
;
337 static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format
[] = {
339 .mode
= MODE_DEFAULT
,
340 .fmt
= "%(objectmode) %(objecttype) %(objectname)%x09%(path)",
341 .fn
= show_tree_default
,
345 .fmt
= "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
346 .fn
= show_tree_long
,
349 .mode
= MODE_NAME_ONLY
, /* And MODE_NAME_STATUS */
351 .fn
= show_tree_name_only
,
354 .mode
= MODE_OBJECT_ONLY
,
355 .fmt
= "%(objectname)",
356 .fn
= show_tree_object
360 .fn
= show_tree_default
,
364 int cmd_ls_tree(int argc
, const char **argv
, const char *prefix
)
366 struct object_id oid
;
368 int i
, full_tree
= 0;
369 read_tree_fn_t fn
= NULL
;
370 enum ls_tree_cmdmode cmdmode
= MODE_DEFAULT
;
371 int null_termination
= 0;
372 struct ls_tree_options options
= { 0 };
373 const struct option ls_tree_options
[] = {
374 OPT_BIT('d', NULL
, &options
.ls_options
, N_("only show trees"),
376 OPT_BIT('r', NULL
, &options
.ls_options
, N_("recurse into subtrees"),
378 OPT_BIT('t', NULL
, &options
.ls_options
, N_("show trees when recursing"),
380 OPT_BOOL('z', NULL
, &null_termination
,
381 N_("terminate entries with NUL byte")),
382 OPT_CMDMODE('l', "long", &cmdmode
, N_("include object size"),
384 OPT_CMDMODE(0, "name-only", &cmdmode
, N_("list only filenames"),
386 OPT_CMDMODE(0, "name-status", &cmdmode
, N_("list only filenames"),
388 OPT_CMDMODE(0, "object-only", &cmdmode
, N_("list only objects"),
390 OPT_SET_INT(0, "full-name", &options
.chomp_prefix
,
391 N_("use full path names"), 0),
392 OPT_BOOL(0, "full-tree", &full_tree
,
393 N_("list entire tree; not just current directory "
394 "(implies --full-name)")),
395 OPT_STRING_F(0, "format", &options
.format
, N_("format"),
396 N_("format to use for the output"),
398 OPT__ABBREV(&options
.abbrev
),
401 struct ls_tree_cmdmode_to_fmt
*m2f
= ls_tree_cmdmode_format
;
404 git_config(git_default_config
, NULL
);
405 options
.ls_tree_prefix
= prefix
;
407 options
.chomp_prefix
= strlen(prefix
);
409 argc
= parse_options(argc
, argv
, prefix
, ls_tree_options
,
411 options
.null_termination
= null_termination
;
414 options
.ls_tree_prefix
= prefix
= NULL
;
415 options
.chomp_prefix
= 0;
418 * We wanted to detect conflicts between --name-only and
419 * --name-status, but once we're done with that subsequent
420 * code should only need to check the primary name.
422 if (cmdmode
== MODE_NAME_STATUS
)
423 cmdmode
= MODE_NAME_ONLY
;
425 /* -d -r should imply -t, but -d by itself should not have to. */
426 if ( (LS_TREE_ONLY
|LS_RECURSIVE
) ==
427 ((LS_TREE_ONLY
|LS_RECURSIVE
) & options
.ls_options
))
428 options
.ls_options
|= LS_SHOW_TREES
;
430 if (options
.format
&& cmdmode
)
432 _("--format can't be combined with other format-altering options"),
433 ls_tree_usage
, ls_tree_options
);
435 usage_with_options(ls_tree_usage
, ls_tree_options
);
436 if (get_oid(argv
[0], &oid
))
437 die("Not a valid object name %s", argv
[0]);
440 * show_recursive() rolls its own matching code and is
441 * generally ignorant of 'struct pathspec'. The magic mask
442 * cannot be lifted until it is converted to use
443 * match_pathspec() or tree_entry_interesting()
445 parse_pathspec(&options
.pathspec
, PATHSPEC_ALL_MAGIC
&
446 ~(PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
),
449 for (i
= 0; i
< options
.pathspec
.nr
; i
++)
450 options
.pathspec
.items
[i
].nowildcard_len
= options
.pathspec
.items
[i
].len
;
451 options
.pathspec
.has_wildcard
= 0;
452 tree
= parse_tree_indirect(&oid
);
454 die("not a tree object");
456 * The generic show_tree_fmt() is slower than show_tree(), so
457 * take the fast path if possible.
461 fn
= options
.format
? show_tree_fmt
: show_tree_default
;
462 } else if (options
.format
&& !strcmp(options
.format
, m2f
->fmt
)) {
465 } else if (!options
.format
&& cmdmode
== m2f
->mode
) {
474 ret
= !!read_tree(the_repository
, tree
, &options
.pathspec
, fn
, &options
);
475 clear_pathspec(&options
.pathspec
);