2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_REPOSITORY_VARIABLE
12 #include "object-name.h"
13 #include "object-store-ll.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
;
57 static int show_recursive(struct ls_tree_options
*options
, const char *base
,
58 size_t baselen
, const char *pathname
)
62 if (options
->ls_options
& LS_RECURSIVE
)
65 if (!options
->pathspec
.nr
)
68 for (i
= 0; i
< options
->pathspec
.nr
; i
++) {
69 const char *spec
= options
->pathspec
.items
[i
].match
;
72 if (strncmp(base
, spec
, baselen
))
74 len
= strlen(pathname
);
76 speclen
= strlen(spec
);
79 if (spec
[len
] && spec
[len
] != '/')
81 if (memcmp(pathname
, spec
, len
))
88 static int show_tree_fmt(const struct object_id
*oid
, struct strbuf
*base
,
89 const char *pathname
, unsigned mode
, void *context
)
91 struct ls_tree_options
*options
= context
;
93 struct strbuf sb
= STRBUF_INIT
;
94 enum object_type type
= object_type(mode
);
95 const char *format
= options
->format
;
97 if (type
== OBJ_TREE
&& show_recursive(options
, base
->buf
, base
->len
, pathname
))
98 recurse
= READ_TREE_RECURSIVE
;
99 if (type
== OBJ_TREE
&& recurse
&& !(options
->ls_options
& LS_SHOW_TREES
))
101 if (type
== OBJ_BLOB
&& (options
->ls_options
& LS_TREE_ONLY
))
104 while (strbuf_expand_step(&sb
, &format
)) {
107 if (skip_prefix(format
, "%", &format
))
108 strbuf_addch(&sb
, '%');
109 else if ((len
= strbuf_expand_literal(&sb
, format
)))
111 else if (skip_prefix(format
, "(objectmode)", &format
))
112 strbuf_addf(&sb
, "%06o", mode
);
113 else if (skip_prefix(format
, "(objecttype)", &format
))
114 strbuf_addstr(&sb
, type_name(type
));
115 else if (skip_prefix(format
, "(objectsize:padded)", &format
))
116 expand_objectsize(&sb
, oid
, type
, 1);
117 else if (skip_prefix(format
, "(objectsize)", &format
))
118 expand_objectsize(&sb
, oid
, type
, 0);
119 else if (skip_prefix(format
, "(objectname)", &format
))
120 strbuf_add_unique_abbrev(&sb
, oid
, options
->abbrev
);
121 else if (skip_prefix(format
, "(path)", &format
)) {
123 const char *prefix
= options
->prefix
;
124 struct strbuf sbuf
= STRBUF_INIT
;
125 size_t baselen
= base
->len
;
127 strbuf_addstr(base
, pathname
);
128 name
= relative_path(base
->buf
, prefix
, &sbuf
);
129 quote_c_style(name
, &sb
, NULL
, 0);
130 strbuf_setlen(base
, baselen
);
131 strbuf_release(&sbuf
);
133 strbuf_expand_bad_format(format
, "ls-tree");
135 strbuf_addch(&sb
, options
->null_termination
? '\0' : '\n');
136 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
141 static int show_tree_common(struct ls_tree_options
*options
, int *recurse
,
142 struct strbuf
*base
, const char *pathname
,
143 enum object_type type
)
148 if (type
== OBJ_BLOB
) {
149 if (options
->ls_options
& LS_TREE_ONLY
)
151 } else if (type
== OBJ_TREE
&&
152 show_recursive(options
, base
->buf
, base
->len
, pathname
)) {
153 *recurse
= READ_TREE_RECURSIVE
;
154 if (!(options
->ls_options
& LS_SHOW_TREES
))
161 static void show_tree_common_default_long(struct ls_tree_options
*options
,
163 const char *pathname
,
164 const size_t baselen
)
166 const char *prefix
= options
->prefix
;
168 strbuf_addstr(base
, pathname
);
170 if (options
->null_termination
) {
171 struct strbuf sb
= STRBUF_INIT
;
172 const char *name
= relative_path(base
->buf
, prefix
, &sb
);
179 write_name_quoted_relative(base
->buf
, prefix
, stdout
, '\n');
182 strbuf_setlen(base
, baselen
);
185 static int show_tree_default(const struct object_id
*oid
, struct strbuf
*base
,
186 const char *pathname
, unsigned mode
,
189 struct ls_tree_options
*options
= context
;
192 enum object_type type
= object_type(mode
);
194 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
198 printf("%06o %s %s\t", mode
, type_name(object_type(mode
)),
199 repo_find_unique_abbrev(the_repository
, oid
, options
->abbrev
));
200 show_tree_common_default_long(options
, base
, pathname
, base
->len
);
204 static int show_tree_long(const struct object_id
*oid
, struct strbuf
*base
,
205 const char *pathname
, unsigned mode
,
208 struct ls_tree_options
*options
= context
;
212 enum object_type type
= object_type(mode
);
214 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
218 if (type
== OBJ_BLOB
) {
220 if (oid_object_info(the_repository
, oid
, &size
) == OBJ_BAD
)
221 xsnprintf(size_text
, sizeof(size_text
), "BAD");
223 xsnprintf(size_text
, sizeof(size_text
),
224 "%" PRIuMAX
, (uintmax_t)size
);
226 xsnprintf(size_text
, sizeof(size_text
), "-");
229 printf("%06o %s %s %7s\t", mode
, type_name(type
),
230 repo_find_unique_abbrev(the_repository
, oid
, options
->abbrev
),
232 show_tree_common_default_long(options
, base
, pathname
, base
->len
);
236 static int show_tree_name_only(const struct object_id
*oid UNUSED
,
238 const char *pathname
, unsigned mode
,
241 struct ls_tree_options
*options
= context
;
244 const size_t baselen
= base
->len
;
245 enum object_type type
= object_type(mode
);
248 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
252 prefix
= options
->prefix
;
253 strbuf_addstr(base
, pathname
);
254 if (options
->null_termination
) {
255 struct strbuf sb
= STRBUF_INIT
;
256 const char *name
= relative_path(base
->buf
, prefix
, &sb
);
263 write_name_quoted_relative(base
->buf
, prefix
, stdout
, '\n');
265 strbuf_setlen(base
, baselen
);
269 static int show_tree_object(const struct object_id
*oid
, struct strbuf
*base
,
270 const char *pathname
, unsigned mode
,
273 struct ls_tree_options
*options
= context
;
276 enum object_type type
= object_type(mode
);
279 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
283 str
= repo_find_unique_abbrev(the_repository
, oid
, options
->abbrev
);
284 if (options
->null_termination
) {
293 enum ls_tree_cmdmode
{
301 struct ls_tree_cmdmode_to_fmt
{
302 enum ls_tree_cmdmode mode
;
303 const char *const fmt
;
307 static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format
[] = {
309 .mode
= MODE_DEFAULT
,
310 .fmt
= "%(objectmode) %(objecttype) %(objectname)%x09%(path)",
311 .fn
= show_tree_default
,
315 .fmt
= "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
316 .fn
= show_tree_long
,
319 .mode
= MODE_NAME_ONLY
, /* And MODE_NAME_STATUS */
321 .fn
= show_tree_name_only
,
324 .mode
= MODE_OBJECT_ONLY
,
325 .fmt
= "%(objectname)",
326 .fn
= show_tree_object
330 .fn
= show_tree_default
,
334 int cmd_ls_tree(int argc
,
337 struct repository
*repo UNUSED
)
339 struct object_id oid
;
341 int i
, full_tree
= 0;
342 int full_name
= !prefix
|| !*prefix
;
343 read_tree_fn_t fn
= NULL
;
344 enum ls_tree_cmdmode cmdmode
= MODE_DEFAULT
;
345 int null_termination
= 0;
346 struct ls_tree_options options
= { 0 };
347 const struct option ls_tree_options
[] = {
348 OPT_BIT('d', NULL
, &options
.ls_options
, N_("only show trees"),
350 OPT_BIT('r', NULL
, &options
.ls_options
, N_("recurse into subtrees"),
352 OPT_BIT('t', NULL
, &options
.ls_options
, N_("show trees when recursing"),
354 OPT_BOOL('z', NULL
, &null_termination
,
355 N_("terminate entries with NUL byte")),
356 OPT_CMDMODE('l', "long", &cmdmode
, N_("include object size"),
358 OPT_CMDMODE(0, "name-only", &cmdmode
, N_("list only filenames"),
360 OPT_CMDMODE(0, "name-status", &cmdmode
, N_("list only filenames"),
362 OPT_CMDMODE(0, "object-only", &cmdmode
, N_("list only objects"),
364 OPT_BOOL(0, "full-name", &full_name
, N_("use full path names")),
365 OPT_BOOL(0, "full-tree", &full_tree
,
366 N_("list entire tree; not just current directory "
367 "(implies --full-name)")),
368 OPT_STRING_F(0, "format", &options
.format
, N_("format"),
369 N_("format to use for the output"),
371 OPT__ABBREV(&options
.abbrev
),
374 struct ls_tree_cmdmode_to_fmt
*m2f
= ls_tree_cmdmode_format
;
375 struct object_context obj_context
= {0};
378 git_config(git_default_config
, NULL
);
380 argc
= parse_options(argc
, argv
, prefix
, ls_tree_options
,
382 options
.null_termination
= null_termination
;
386 options
.prefix
= full_name
? NULL
: prefix
;
389 * We wanted to detect conflicts between --name-only and
390 * --name-status, but once we're done with that subsequent
391 * code should only need to check the primary name.
393 if (cmdmode
== MODE_NAME_STATUS
)
394 cmdmode
= MODE_NAME_ONLY
;
396 /* -d -r should imply -t, but -d by itself should not have to. */
397 if ( (LS_TREE_ONLY
|LS_RECURSIVE
) ==
398 ((LS_TREE_ONLY
|LS_RECURSIVE
) & options
.ls_options
))
399 options
.ls_options
|= LS_SHOW_TREES
;
401 if (options
.format
&& cmdmode
)
403 _("--format can't be combined with other format-altering options"),
404 ls_tree_usage
, ls_tree_options
);
406 usage_with_options(ls_tree_usage
, ls_tree_options
);
407 if (get_oid_with_context(the_repository
, argv
[0],
408 GET_OID_HASH_ANY
, &oid
,
410 die("Not a valid object name %s", argv
[0]);
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
416 * match_pathspec() or tree_entry_interesting()
418 parse_pathspec(&options
.pathspec
, PATHSPEC_ALL_MAGIC
&
419 ~(PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
),
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;
425 tree
= parse_tree_indirect(&oid
);
427 die("not a tree object");
429 * The generic show_tree_fmt() is slower than show_tree(), so
430 * take the fast path if possible.
434 fn
= options
.format
? show_tree_fmt
: show_tree_default
;
435 } else if (options
.format
&& !strcmp(options
.format
, m2f
->fmt
)) {
438 } else if (!options
.format
&& cmdmode
== m2f
->mode
) {
447 ret
= !!read_tree(the_repository
, tree
, &options
.pathspec
, fn
, &options
);
448 clear_pathspec(&options
.pathspec
);
449 object_context_release(&obj_context
);