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 int line_termination
= '\n';
18 #define LS_RECURSIVE 1
19 #define LS_TREE_ONLY (1 << 1)
20 #define LS_SHOW_TREES (1 << 2)
22 static int ls_options
;
23 static struct pathspec pathspec
;
24 static int chomp_prefix
;
25 static const char *ls_tree_prefix
;
26 static const char *format
;
27 struct show_tree_data
{
29 enum object_type type
;
30 const struct object_id
*oid
;
35 static const char * const ls_tree_usage
[] = {
36 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
40 static enum ls_tree_cmdmode
{
48 static void expand_objectsize(struct strbuf
*line
, const struct object_id
*oid
,
49 const enum object_type type
, unsigned int padded
)
51 if (type
== OBJ_BLOB
) {
53 if (oid_object_info(the_repository
, oid
, &size
) < 0)
54 die(_("could not get object info about '%s'"),
57 strbuf_addf(line
, "%7"PRIuMAX
, (uintmax_t)size
);
59 strbuf_addf(line
, "%"PRIuMAX
, (uintmax_t)size
);
61 strbuf_addf(line
, "%7s", "-");
63 strbuf_addstr(line
, "-");
67 static size_t expand_show_tree(struct strbuf
*sb
, const char *start
,
70 struct show_tree_data
*data
= context
;
74 size_t len
= strbuf_expand_literal_cb(sb
, start
, NULL
);
79 die(_("bad ls-tree format: element '%s' does not start with '('"), start
);
81 end
= strchr(start
+ 1, ')');
83 die(_("bad ls-tree format: element '%s' does not end in ')'"), start
);
85 len
= end
- start
+ 1;
86 if (skip_prefix(start
, "(objectmode)", &p
)) {
87 strbuf_addf(sb
, "%06o", data
->mode
);
88 } else if (skip_prefix(start
, "(objecttype)", &p
)) {
89 strbuf_addstr(sb
, type_name(data
->type
));
90 } else if (skip_prefix(start
, "(objectsize:padded)", &p
)) {
91 expand_objectsize(sb
, data
->oid
, data
->type
, 1);
92 } else if (skip_prefix(start
, "(objectsize)", &p
)) {
93 expand_objectsize(sb
, data
->oid
, data
->type
, 0);
94 } else if (skip_prefix(start
, "(objectname)", &p
)) {
95 strbuf_add_unique_abbrev(sb
, data
->oid
, abbrev
);
96 } else if (skip_prefix(start
, "(path)", &p
)) {
98 const char *prefix
= chomp_prefix
? ls_tree_prefix
: NULL
;
99 struct strbuf sbuf
= STRBUF_INIT
;
100 size_t baselen
= data
->base
->len
;
102 strbuf_addstr(data
->base
, data
->pathname
);
103 name
= relative_path(data
->base
->buf
, prefix
, &sbuf
);
104 quote_c_style(name
, sb
, NULL
, 0);
105 strbuf_setlen(data
->base
, baselen
);
106 strbuf_release(&sbuf
);
108 errlen
= (unsigned long)len
;
109 die(_("bad ls-tree format: %%%.*s"), errlen
, start
);
114 static int show_recursive(const char *base
, size_t baselen
, const char *pathname
)
118 if (ls_options
& LS_RECURSIVE
)
124 for (i
= 0; i
< pathspec
.nr
; i
++) {
125 const char *spec
= pathspec
.items
[i
].match
;
128 if (strncmp(base
, spec
, baselen
))
130 len
= strlen(pathname
);
132 speclen
= strlen(spec
);
135 if (spec
[len
] && spec
[len
] != '/')
137 if (memcmp(pathname
, spec
, len
))
144 static int show_tree_fmt(const struct object_id
*oid
, struct strbuf
*base
,
145 const char *pathname
, unsigned mode
, void *context UNUSED
)
148 struct strbuf sb
= STRBUF_INIT
;
149 enum object_type type
= object_type(mode
);
151 struct show_tree_data data
= {
155 .pathname
= pathname
,
159 if (type
== OBJ_TREE
&& show_recursive(base
->buf
, base
->len
, pathname
))
160 recurse
= READ_TREE_RECURSIVE
;
161 if (type
== OBJ_TREE
&& recurse
&& !(ls_options
& LS_SHOW_TREES
))
163 if (type
== OBJ_BLOB
&& (ls_options
& LS_TREE_ONLY
))
166 strbuf_expand(&sb
, format
, expand_show_tree
, &data
);
167 strbuf_addch(&sb
, line_termination
);
168 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
173 static int show_tree_common(struct show_tree_data
*data
, int *recurse
,
174 const struct object_id
*oid
, struct strbuf
*base
,
175 const char *pathname
, unsigned mode
)
177 enum object_type type
= object_type(mode
);
184 data
->pathname
= pathname
;
187 if (type
== OBJ_BLOB
) {
188 if (ls_options
& LS_TREE_ONLY
)
190 } else if (type
== OBJ_TREE
&&
191 show_recursive(base
->buf
, base
->len
, pathname
)) {
192 *recurse
= READ_TREE_RECURSIVE
;
193 if (!(ls_options
& LS_SHOW_TREES
))
200 static void show_tree_common_default_long(struct strbuf
*base
,
201 const char *pathname
,
202 const size_t baselen
)
204 strbuf_addstr(base
, pathname
);
205 write_name_quoted_relative(base
->buf
,
206 chomp_prefix
? ls_tree_prefix
: NULL
, stdout
,
208 strbuf_setlen(base
, baselen
);
211 static int show_tree_default(const struct object_id
*oid
, struct strbuf
*base
,
212 const char *pathname
, unsigned mode
,
213 void *context UNUSED
)
217 struct show_tree_data data
= { 0 };
219 early
= show_tree_common(&data
, &recurse
, oid
, base
, pathname
, mode
);
223 printf("%06o %s %s\t", data
.mode
, type_name(data
.type
),
224 find_unique_abbrev(data
.oid
, abbrev
));
225 show_tree_common_default_long(base
, pathname
, data
.base
->len
);
229 static int show_tree_long(const struct object_id
*oid
, struct strbuf
*base
,
230 const char *pathname
, unsigned mode
,
231 void *context UNUSED
)
235 struct show_tree_data data
= { 0 };
238 early
= show_tree_common(&data
, &recurse
, oid
, base
, pathname
, mode
);
242 if (data
.type
== OBJ_BLOB
) {
244 if (oid_object_info(the_repository
, data
.oid
, &size
) == OBJ_BAD
)
245 xsnprintf(size_text
, sizeof(size_text
), "BAD");
247 xsnprintf(size_text
, sizeof(size_text
),
248 "%" PRIuMAX
, (uintmax_t)size
);
250 xsnprintf(size_text
, sizeof(size_text
), "-");
253 printf("%06o %s %s %7s\t", data
.mode
, type_name(data
.type
),
254 find_unique_abbrev(data
.oid
, abbrev
), size_text
);
255 show_tree_common_default_long(base
, pathname
, data
.base
->len
);
259 static int show_tree_name_only(const struct object_id
*oid
, struct strbuf
*base
,
260 const char *pathname
, unsigned mode
,
261 void *context UNUSED
)
265 const size_t baselen
= base
->len
;
266 struct show_tree_data data
= { 0 };
268 early
= show_tree_common(&data
, &recurse
, oid
, base
, pathname
, mode
);
272 strbuf_addstr(base
, pathname
);
273 write_name_quoted_relative(base
->buf
,
274 chomp_prefix
? ls_tree_prefix
: NULL
,
275 stdout
, line_termination
);
276 strbuf_setlen(base
, baselen
);
280 static int show_tree_object(const struct object_id
*oid
, struct strbuf
*base
,
281 const char *pathname
, unsigned mode
,
282 void *context UNUSED
)
286 struct show_tree_data data
= { 0 };
288 early
= show_tree_common(&data
, &recurse
, oid
, base
, pathname
, mode
);
292 printf("%s%c", find_unique_abbrev(oid
, abbrev
), line_termination
);
296 struct ls_tree_cmdmode_to_fmt
{
297 enum ls_tree_cmdmode mode
;
298 const char *const fmt
;
302 static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format
[] = {
304 .mode
= MODE_DEFAULT
,
305 .fmt
= "%(objectmode) %(objecttype) %(objectname)%x09%(path)",
306 .fn
= show_tree_default
,
310 .fmt
= "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
311 .fn
= show_tree_long
,
314 .mode
= MODE_NAME_ONLY
, /* And MODE_NAME_STATUS */
316 .fn
= show_tree_name_only
,
319 .mode
= MODE_OBJECT_ONLY
,
320 .fmt
= "%(objectname)",
321 .fn
= show_tree_object
325 .fn
= show_tree_default
,
329 int cmd_ls_tree(int argc
, const char **argv
, const char *prefix
)
331 struct object_id oid
;
333 int i
, full_tree
= 0;
334 read_tree_fn_t fn
= NULL
;
335 const struct option ls_tree_options
[] = {
336 OPT_BIT('d', NULL
, &ls_options
, N_("only show trees"),
338 OPT_BIT('r', NULL
, &ls_options
, N_("recurse into subtrees"),
340 OPT_BIT('t', NULL
, &ls_options
, N_("show trees when recursing"),
342 OPT_SET_INT('z', NULL
, &line_termination
,
343 N_("terminate entries with NUL byte"), 0),
344 OPT_CMDMODE('l', "long", &cmdmode
, N_("include object size"),
346 OPT_CMDMODE(0, "name-only", &cmdmode
, N_("list only filenames"),
348 OPT_CMDMODE(0, "name-status", &cmdmode
, N_("list only filenames"),
350 OPT_CMDMODE(0, "object-only", &cmdmode
, N_("list only objects"),
352 OPT_SET_INT(0, "full-name", &chomp_prefix
,
353 N_("use full path names"), 0),
354 OPT_BOOL(0, "full-tree", &full_tree
,
355 N_("list entire tree; not just current directory "
356 "(implies --full-name)")),
357 OPT_STRING_F(0, "format", &format
, N_("format"),
358 N_("format to use for the output"),
360 OPT__ABBREV(&abbrev
),
363 struct ls_tree_cmdmode_to_fmt
*m2f
= ls_tree_cmdmode_format
;
365 git_config(git_default_config
, NULL
);
366 ls_tree_prefix
= prefix
;
368 chomp_prefix
= strlen(prefix
);
370 argc
= parse_options(argc
, argv
, prefix
, ls_tree_options
,
373 ls_tree_prefix
= prefix
= NULL
;
377 * We wanted to detect conflicts between --name-only and
378 * --name-status, but once we're done with that subsequent
379 * code should only need to check the primary name.
381 if (cmdmode
== MODE_NAME_STATUS
)
382 cmdmode
= MODE_NAME_ONLY
;
384 /* -d -r should imply -t, but -d by itself should not have to. */
385 if ( (LS_TREE_ONLY
|LS_RECURSIVE
) ==
386 ((LS_TREE_ONLY
|LS_RECURSIVE
) & ls_options
))
387 ls_options
|= LS_SHOW_TREES
;
389 if (format
&& cmdmode
)
391 _("--format can't be combined with other format-altering options"),
392 ls_tree_usage
, ls_tree_options
);
394 usage_with_options(ls_tree_usage
, ls_tree_options
);
395 if (get_oid(argv
[0], &oid
))
396 die("Not a valid object name %s", argv
[0]);
399 * show_recursive() rolls its own matching code and is
400 * generally ignorant of 'struct pathspec'. The magic mask
401 * cannot be lifted until it is converted to use
402 * match_pathspec() or tree_entry_interesting()
404 parse_pathspec(&pathspec
, PATHSPEC_ALL_MAGIC
&
405 ~(PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
),
408 for (i
= 0; i
< pathspec
.nr
; i
++)
409 pathspec
.items
[i
].nowildcard_len
= pathspec
.items
[i
].len
;
410 pathspec
.has_wildcard
= 0;
411 tree
= parse_tree_indirect(&oid
);
413 die("not a tree object");
415 * The generic show_tree_fmt() is slower than show_tree(), so
416 * take the fast path if possible.
420 fn
= format
? show_tree_fmt
: show_tree_default
;
421 } else if (format
&& !strcmp(format
, m2f
->fmt
)) {
424 } else if (!format
&& cmdmode
== m2f
->mode
) {
433 return !!read_tree(the_repository
, tree
, &pathspec
, fn
, NULL
);