2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #include "object-name.h"
11 #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
)) {
108 if (skip_prefix(format
, "%", &format
))
109 strbuf_addch(&sb
, '%');
110 else if ((len
= strbuf_expand_literal(&sb
, format
)))
112 else if (*format
!= '(')
113 die(_("bad ls-tree format: element '%s' "
114 "does not start with '('"), format
);
115 else if (!(end
= strchr(format
+ 1, ')')))
116 die(_("bad ls-tree format: element '%s' "
117 "does not end in ')'"), format
);
118 else if (skip_prefix(format
, "(objectmode)", &format
))
119 strbuf_addf(&sb
, "%06o", mode
);
120 else if (skip_prefix(format
, "(objecttype)", &format
))
121 strbuf_addstr(&sb
, type_name(type
));
122 else if (skip_prefix(format
, "(objectsize:padded)", &format
))
123 expand_objectsize(&sb
, oid
, type
, 1);
124 else if (skip_prefix(format
, "(objectsize)", &format
))
125 expand_objectsize(&sb
, oid
, type
, 0);
126 else if (skip_prefix(format
, "(objectname)", &format
))
127 strbuf_add_unique_abbrev(&sb
, oid
, options
->abbrev
);
128 else if (skip_prefix(format
, "(path)", &format
)) {
130 const char *prefix
= options
->prefix
;
131 struct strbuf sbuf
= STRBUF_INIT
;
132 size_t baselen
= base
->len
;
134 strbuf_addstr(base
, pathname
);
135 name
= relative_path(base
->buf
, prefix
, &sbuf
);
136 quote_c_style(name
, &sb
, NULL
, 0);
137 strbuf_setlen(base
, baselen
);
138 strbuf_release(&sbuf
);
140 die(_("bad ls-tree format: %%%.*s"),
141 (int)(end
- format
+ 1), format
);
143 strbuf_addch(&sb
, options
->null_termination
? '\0' : '\n');
144 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
149 static int show_tree_common(struct ls_tree_options
*options
, int *recurse
,
150 struct strbuf
*base
, const char *pathname
,
151 enum object_type type
)
156 if (type
== OBJ_BLOB
) {
157 if (options
->ls_options
& LS_TREE_ONLY
)
159 } else if (type
== OBJ_TREE
&&
160 show_recursive(options
, base
->buf
, base
->len
, pathname
)) {
161 *recurse
= READ_TREE_RECURSIVE
;
162 if (!(options
->ls_options
& LS_SHOW_TREES
))
169 static void show_tree_common_default_long(struct ls_tree_options
*options
,
171 const char *pathname
,
172 const size_t baselen
)
174 const char *prefix
= options
->prefix
;
176 strbuf_addstr(base
, pathname
);
178 if (options
->null_termination
) {
179 struct strbuf sb
= STRBUF_INIT
;
180 const char *name
= relative_path(base
->buf
, prefix
, &sb
);
187 write_name_quoted_relative(base
->buf
, prefix
, stdout
, '\n');
190 strbuf_setlen(base
, baselen
);
193 static int show_tree_default(const struct object_id
*oid
, struct strbuf
*base
,
194 const char *pathname
, unsigned mode
,
197 struct ls_tree_options
*options
= context
;
200 enum object_type type
= object_type(mode
);
202 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
206 printf("%06o %s %s\t", mode
, type_name(object_type(mode
)),
207 repo_find_unique_abbrev(the_repository
, oid
, options
->abbrev
));
208 show_tree_common_default_long(options
, base
, pathname
, base
->len
);
212 static int show_tree_long(const struct object_id
*oid
, struct strbuf
*base
,
213 const char *pathname
, unsigned mode
,
216 struct ls_tree_options
*options
= context
;
220 enum object_type type
= object_type(mode
);
222 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
226 if (type
== OBJ_BLOB
) {
228 if (oid_object_info(the_repository
, oid
, &size
) == OBJ_BAD
)
229 xsnprintf(size_text
, sizeof(size_text
), "BAD");
231 xsnprintf(size_text
, sizeof(size_text
),
232 "%" PRIuMAX
, (uintmax_t)size
);
234 xsnprintf(size_text
, sizeof(size_text
), "-");
237 printf("%06o %s %s %7s\t", mode
, type_name(type
),
238 repo_find_unique_abbrev(the_repository
, oid
, options
->abbrev
),
240 show_tree_common_default_long(options
, base
, pathname
, base
->len
);
244 static int show_tree_name_only(const struct object_id
*oid
, struct strbuf
*base
,
245 const char *pathname
, unsigned mode
,
248 struct ls_tree_options
*options
= context
;
251 const size_t baselen
= base
->len
;
252 enum object_type type
= object_type(mode
);
255 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
259 prefix
= options
->prefix
;
260 strbuf_addstr(base
, pathname
);
261 if (options
->null_termination
) {
262 struct strbuf sb
= STRBUF_INIT
;
263 const char *name
= relative_path(base
->buf
, prefix
, &sb
);
270 write_name_quoted_relative(base
->buf
, prefix
, stdout
, '\n');
272 strbuf_setlen(base
, baselen
);
276 static int show_tree_object(const struct object_id
*oid
, struct strbuf
*base
,
277 const char *pathname
, unsigned mode
,
280 struct ls_tree_options
*options
= context
;
283 enum object_type type
= object_type(mode
);
286 early
= show_tree_common(options
, &recurse
, base
, pathname
, type
);
290 str
= repo_find_unique_abbrev(the_repository
, oid
, options
->abbrev
);
291 if (options
->null_termination
) {
300 enum ls_tree_cmdmode
{
308 struct ls_tree_cmdmode_to_fmt
{
309 enum ls_tree_cmdmode mode
;
310 const char *const fmt
;
314 static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format
[] = {
316 .mode
= MODE_DEFAULT
,
317 .fmt
= "%(objectmode) %(objecttype) %(objectname)%x09%(path)",
318 .fn
= show_tree_default
,
322 .fmt
= "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
323 .fn
= show_tree_long
,
326 .mode
= MODE_NAME_ONLY
, /* And MODE_NAME_STATUS */
328 .fn
= show_tree_name_only
,
331 .mode
= MODE_OBJECT_ONLY
,
332 .fmt
= "%(objectname)",
333 .fn
= show_tree_object
337 .fn
= show_tree_default
,
341 int cmd_ls_tree(int argc
, const char **argv
, const char *prefix
)
343 struct object_id oid
;
345 int i
, full_tree
= 0;
346 int chomp_prefix
= prefix
&& *prefix
;
347 read_tree_fn_t fn
= NULL
;
348 enum ls_tree_cmdmode cmdmode
= MODE_DEFAULT
;
349 int null_termination
= 0;
350 struct ls_tree_options options
= { 0 };
351 const struct option ls_tree_options
[] = {
352 OPT_BIT('d', NULL
, &options
.ls_options
, N_("only show trees"),
354 OPT_BIT('r', NULL
, &options
.ls_options
, N_("recurse into subtrees"),
356 OPT_BIT('t', NULL
, &options
.ls_options
, N_("show trees when recursing"),
358 OPT_BOOL('z', NULL
, &null_termination
,
359 N_("terminate entries with NUL byte")),
360 OPT_CMDMODE('l', "long", &cmdmode
, N_("include object size"),
362 OPT_CMDMODE(0, "name-only", &cmdmode
, N_("list only filenames"),
364 OPT_CMDMODE(0, "name-status", &cmdmode
, N_("list only filenames"),
366 OPT_CMDMODE(0, "object-only", &cmdmode
, N_("list only objects"),
368 OPT_SET_INT(0, "full-name", &chomp_prefix
,
369 N_("use full path names"), 0),
370 OPT_BOOL(0, "full-tree", &full_tree
,
371 N_("list entire tree; not just current directory "
372 "(implies --full-name)")),
373 OPT_STRING_F(0, "format", &options
.format
, N_("format"),
374 N_("format to use for the output"),
376 OPT__ABBREV(&options
.abbrev
),
379 struct ls_tree_cmdmode_to_fmt
*m2f
= ls_tree_cmdmode_format
;
382 git_config(git_default_config
, NULL
);
384 argc
= parse_options(argc
, argv
, prefix
, ls_tree_options
,
386 options
.null_termination
= null_termination
;
390 options
.prefix
= chomp_prefix
? prefix
: NULL
;
393 * We wanted to detect conflicts between --name-only and
394 * --name-status, but once we're done with that subsequent
395 * code should only need to check the primary name.
397 if (cmdmode
== MODE_NAME_STATUS
)
398 cmdmode
= MODE_NAME_ONLY
;
400 /* -d -r should imply -t, but -d by itself should not have to. */
401 if ( (LS_TREE_ONLY
|LS_RECURSIVE
) ==
402 ((LS_TREE_ONLY
|LS_RECURSIVE
) & options
.ls_options
))
403 options
.ls_options
|= LS_SHOW_TREES
;
405 if (options
.format
&& cmdmode
)
407 _("--format can't be combined with other format-altering options"),
408 ls_tree_usage
, ls_tree_options
);
410 usage_with_options(ls_tree_usage
, ls_tree_options
);
411 if (repo_get_oid(the_repository
, argv
[0], &oid
))
412 die("Not a valid object name %s", argv
[0]);
415 * show_recursive() rolls its own matching code and is
416 * generally ignorant of 'struct pathspec'. The magic mask
417 * cannot be lifted until it is converted to use
418 * match_pathspec() or tree_entry_interesting()
420 parse_pathspec(&options
.pathspec
, PATHSPEC_ALL_MAGIC
&
421 ~(PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
),
424 for (i
= 0; i
< options
.pathspec
.nr
; i
++)
425 options
.pathspec
.items
[i
].nowildcard_len
= options
.pathspec
.items
[i
].len
;
426 options
.pathspec
.has_wildcard
= 0;
427 tree
= parse_tree_indirect(&oid
);
429 die("not a tree object");
431 * The generic show_tree_fmt() is slower than show_tree(), so
432 * take the fast path if possible.
436 fn
= options
.format
? show_tree_fmt
: show_tree_default
;
437 } else if (options
.format
&& !strcmp(options
.format
, m2f
->fmt
)) {
440 } else if (!options
.format
&& cmdmode
== m2f
->mode
) {
449 ret
= !!read_tree(the_repository
, tree
, &options
.pathspec
, fn
, &options
);
450 clear_pathspec(&options
.pathspec
);