2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #include "object-name.h"
11 #include "object-store.h"
17 #include "parse-options.h"
20 static const char * const ls_tree_usage
[] = {
21 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
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
) {
30 if (oid_object_info(the_repository
, oid
, &size
) < 0)
31 die(_("could not get object info about '%s'"),
34 strbuf_addf(line
, "%7"PRIuMAX
, (uintmax_t)size
);
36 strbuf_addf(line
, "%"PRIuMAX
, (uintmax_t)size
);
38 strbuf_addf(line
, "%7s", "-");
40 strbuf_addstr(line
, "-");
44 struct ls_tree_options
{
45 unsigned null_termination
:1;
47 enum ls_tree_path_options
{
48 LS_RECURSIVE
= 1 << 0,
49 LS_TREE_ONLY
= 1 << 1,
50 LS_SHOW_TREES
= 1 << 2,
52 struct pathspec pathspec
;
54 const char *ls_tree_prefix
;
58 struct show_tree_data
{
59 struct ls_tree_options
*options
;
61 enum object_type type
;
62 const struct object_id
*oid
;
67 static size_t expand_show_tree(struct strbuf
*sb
, const char *start
,
70 struct show_tree_data
*data
= context
;
71 struct ls_tree_options
*options
= data
->options
;
75 size_t len
= strbuf_expand_literal_cb(sb
, start
, NULL
);
80 die(_("bad ls-tree format: element '%s' does not start with '('"), start
);
82 end
= strchr(start
+ 1, ')');
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
);
109 errlen
= (unsigned long)len
;
110 die(_("bad ls-tree format: %%%.*s"), errlen
, start
);
115 static int show_recursive(struct ls_tree_options
*options
, const char *base
,
116 size_t baselen
, const char *pathname
)
120 if (options
->ls_options
& LS_RECURSIVE
)
123 if (!options
->pathspec
.nr
)
126 for (i
= 0; i
< options
->pathspec
.nr
; i
++) {
127 const char *spec
= options
->pathspec
.items
[i
].match
;
130 if (strncmp(base
, spec
, baselen
))
132 len
= strlen(pathname
);
134 speclen
= strlen(spec
);
137 if (spec
[len
] && spec
[len
] != '/')
139 if (memcmp(pathname
, spec
, len
))
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
;
151 struct strbuf sb
= STRBUF_INIT
;
152 enum object_type type
= object_type(mode
);
153 struct show_tree_data cb_data
= {
158 .pathname
= pathname
,
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
))
166 if (type
== OBJ_BLOB
&& (options
->ls_options
& LS_TREE_ONLY
))
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
);
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
)
183 if (type
== OBJ_BLOB
) {
184 if (options
->ls_options
& LS_TREE_ONLY
)
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
))
196 static void show_tree_common_default_long(struct ls_tree_options
*options
,
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
);
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
,
224 struct ls_tree_options
*options
= context
;
227 enum object_type type
= object_type(mode
);
229 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
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
);
239 static int show_tree_long(const struct object_id
*oid
, struct strbuf
*base
,
240 const char *pathname
, unsigned mode
,
243 struct ls_tree_options
*options
= context
;
247 enum object_type type
= object_type(mode
);
249 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
253 if (type
== OBJ_BLOB
) {
255 if (oid_object_info(the_repository
, oid
, &size
) == OBJ_BAD
)
256 xsnprintf(size_text
, sizeof(size_text
), "BAD");
258 xsnprintf(size_text
, sizeof(size_text
),
259 "%" PRIuMAX
, (uintmax_t)size
);
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
),
267 show_tree_common_default_long(options
, base
, pathname
, base
->len
);
271 static int show_tree_name_only(const struct object_id
*oid
, struct strbuf
*base
,
272 const char *pathname
, unsigned mode
,
275 struct ls_tree_options
*options
= context
;
278 const size_t baselen
= base
->len
;
279 enum object_type type
= object_type(mode
);
282 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
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
);
297 write_name_quoted_relative(base
->buf
, prefix
, stdout
, '\n');
299 strbuf_setlen(base
, baselen
);
303 static int show_tree_object(const struct object_id
*oid
, struct strbuf
*base
,
304 const char *pathname
, unsigned mode
,
307 struct ls_tree_options
*options
= context
;
310 enum object_type type
= object_type(mode
);
313 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
317 str
= repo_find_unique_abbrev(the_repository
, oid
, options
->abbrev
);
318 if (options
->null_termination
) {
327 enum ls_tree_cmdmode
{
335 struct ls_tree_cmdmode_to_fmt
{
336 enum ls_tree_cmdmode mode
;
337 const char *const fmt
;
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
,
349 .fmt
= "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
350 .fn
= show_tree_long
,
353 .mode
= MODE_NAME_ONLY
, /* And MODE_NAME_STATUS */
355 .fn
= show_tree_name_only
,
358 .mode
= MODE_OBJECT_ONLY
,
359 .fmt
= "%(objectname)",
360 .fn
= show_tree_object
364 .fn
= show_tree_default
,
368 int cmd_ls_tree(int argc
, const char **argv
, const char *prefix
)
370 struct object_id oid
;
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"),
380 OPT_BIT('r', NULL
, &options
.ls_options
, N_("recurse into subtrees"),
382 OPT_BIT('t', NULL
, &options
.ls_options
, N_("show trees when recursing"),
384 OPT_BOOL('z', NULL
, &null_termination
,
385 N_("terminate entries with NUL byte")),
386 OPT_CMDMODE('l', "long", &cmdmode
, N_("include object size"),
388 OPT_CMDMODE(0, "name-only", &cmdmode
, N_("list only filenames"),
390 OPT_CMDMODE(0, "name-status", &cmdmode
, N_("list only filenames"),
392 OPT_CMDMODE(0, "object-only", &cmdmode
, N_("list only objects"),
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"),
402 OPT__ABBREV(&options
.abbrev
),
405 struct ls_tree_cmdmode_to_fmt
*m2f
= ls_tree_cmdmode_format
;
408 git_config(git_default_config
, NULL
);
409 options
.ls_tree_prefix
= prefix
;
411 options
.chomp_prefix
= strlen(prefix
);
413 argc
= parse_options(argc
, argv
, prefix
, ls_tree_options
,
415 options
.null_termination
= null_termination
;
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
)
436 _("--format can't be combined with other format-altering options"),
437 ls_tree_usage
, ls_tree_options
);
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
),
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
);
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.
465 fn
= options
.format
? show_tree_fmt
: show_tree_default
;
466 } else if (options
.format
&& !strcmp(options
.format
, m2f
->fmt
)) {
469 } else if (!options
.format
&& cmdmode
== m2f
->mode
) {
478 ret
= !!read_tree(the_repository
, tree
, &options
.pathspec
, fn
, &options
);
479 clear_pathspec(&options
.pathspec
);