2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #include "object-name.h"
11 #include "object-store-ll.h"
15 #include "parse-options.h"
18 static const char * const ls_tree_usage
[] = {
19 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
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
) {
28 if (oid_object_info(the_repository
, oid
, &size
) < 0)
29 die(_("could not get object info about '%s'"),
32 strbuf_addf(line
, "%7"PRIuMAX
, (uintmax_t)size
);
34 strbuf_addf(line
, "%"PRIuMAX
, (uintmax_t)size
);
36 strbuf_addf(line
, "%7s", "-");
38 strbuf_addstr(line
, "-");
42 struct ls_tree_options
{
43 unsigned null_termination
:1;
45 enum ls_tree_path_options
{
46 LS_RECURSIVE
= 1 << 0,
47 LS_TREE_ONLY
= 1 << 1,
48 LS_SHOW_TREES
= 1 << 2,
50 struct pathspec pathspec
;
55 static int show_recursive(struct ls_tree_options
*options
, const char *base
,
56 size_t baselen
, const char *pathname
)
60 if (options
->ls_options
& LS_RECURSIVE
)
63 if (!options
->pathspec
.nr
)
66 for (i
= 0; i
< options
->pathspec
.nr
; i
++) {
67 const char *spec
= options
->pathspec
.items
[i
].match
;
70 if (strncmp(base
, spec
, baselen
))
72 len
= strlen(pathname
);
74 speclen
= strlen(spec
);
77 if (spec
[len
] && spec
[len
] != '/')
79 if (memcmp(pathname
, spec
, len
))
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
;
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
))
99 if (type
== OBJ_BLOB
&& (options
->ls_options
& LS_TREE_ONLY
))
102 while (strbuf_expand_step(&sb
, &format
)) {
105 if (skip_prefix(format
, "%", &format
))
106 strbuf_addch(&sb
, '%');
107 else if ((len
= strbuf_expand_literal(&sb
, format
)))
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
)) {
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
);
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
);
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
)
146 if (type
== OBJ_BLOB
) {
147 if (options
->ls_options
& LS_TREE_ONLY
)
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
))
159 static void show_tree_common_default_long(struct ls_tree_options
*options
,
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
);
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
,
187 struct ls_tree_options
*options
= context
;
190 enum object_type type
= object_type(mode
);
192 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
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
);
202 static int show_tree_long(const struct object_id
*oid
, struct strbuf
*base
,
203 const char *pathname
, unsigned mode
,
206 struct ls_tree_options
*options
= context
;
210 enum object_type type
= object_type(mode
);
212 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
216 if (type
== OBJ_BLOB
) {
218 if (oid_object_info(the_repository
, oid
, &size
) == OBJ_BAD
)
219 xsnprintf(size_text
, sizeof(size_text
), "BAD");
221 xsnprintf(size_text
, sizeof(size_text
),
222 "%" PRIuMAX
, (uintmax_t)size
);
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
),
230 show_tree_common_default_long(options
, base
, pathname
, base
->len
);
234 static int show_tree_name_only(const struct object_id
*oid UNUSED
,
236 const char *pathname
, unsigned mode
,
239 struct ls_tree_options
*options
= context
;
242 const size_t baselen
= base
->len
;
243 enum object_type type
= object_type(mode
);
246 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
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
);
261 write_name_quoted_relative(base
->buf
, prefix
, stdout
, '\n');
263 strbuf_setlen(base
, baselen
);
267 static int show_tree_object(const struct object_id
*oid
, struct strbuf
*base
,
268 const char *pathname
, unsigned mode
,
271 struct ls_tree_options
*options
= context
;
274 enum object_type type
= object_type(mode
);
277 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
281 str
= repo_find_unique_abbrev(the_repository
, oid
, options
->abbrev
);
282 if (options
->null_termination
) {
291 enum ls_tree_cmdmode
{
299 struct ls_tree_cmdmode_to_fmt
{
300 enum ls_tree_cmdmode mode
;
301 const char *const fmt
;
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
,
313 .fmt
= "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
314 .fn
= show_tree_long
,
317 .mode
= MODE_NAME_ONLY
, /* And MODE_NAME_STATUS */
319 .fn
= show_tree_name_only
,
322 .mode
= MODE_OBJECT_ONLY
,
323 .fmt
= "%(objectname)",
324 .fn
= show_tree_object
328 .fn
= show_tree_default
,
332 int cmd_ls_tree(int argc
, const char **argv
, const char *prefix
)
334 struct object_id oid
;
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"),
345 OPT_BIT('r', NULL
, &options
.ls_options
, N_("recurse into subtrees"),
347 OPT_BIT('t', NULL
, &options
.ls_options
, N_("show trees when recursing"),
349 OPT_BOOL('z', NULL
, &null_termination
,
350 N_("terminate entries with NUL byte")),
351 OPT_CMDMODE('l', "long", &cmdmode
, N_("include object size"),
353 OPT_CMDMODE(0, "name-only", &cmdmode
, N_("list only filenames"),
355 OPT_CMDMODE(0, "name-status", &cmdmode
, N_("list only filenames"),
357 OPT_CMDMODE(0, "object-only", &cmdmode
, N_("list only objects"),
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"),
366 OPT__ABBREV(&options
.abbrev
),
369 struct ls_tree_cmdmode_to_fmt
*m2f
= ls_tree_cmdmode_format
;
370 struct object_context obj_context
;
373 git_config(git_default_config
, NULL
);
375 argc
= parse_options(argc
, argv
, prefix
, ls_tree_options
,
377 options
.null_termination
= null_termination
;
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
)
398 _("--format can't be combined with other format-altering options"),
399 ls_tree_usage
, ls_tree_options
);
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
,
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
),
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
);
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.
429 fn
= options
.format
? show_tree_fmt
: show_tree_default
;
430 } else if (options
.format
&& !strcmp(options
.format
, m2f
->fmt
)) {
433 } else if (!options
.format
&& cmdmode
== m2f
->mode
) {
442 ret
= !!read_tree(the_repository
, tree
, &options
.pathspec
, fn
, &options
);
443 clear_pathspec(&options
.pathspec
);