2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
13 #include "parse-options.h"
16 static int line_termination = '\n';
17 #define LS_RECURSIVE 1
18 #define LS_TREE_ONLY 2
19 #define LS_SHOW_TREES 4
20 #define LS_NAME_ONLY 8
21 #define LS_SHOW_SIZE 16
23 static int ls_options;
24 static struct pathspec pathspec;
25 static int chomp_prefix;
26 static const char *ls_tree_prefix;
28 static const char * const ls_tree_usage[] = {
29 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
33 static int show_recursive(const char *base, int baselen, const char *pathname)
37 if (ls_options & LS_RECURSIVE)
43 for (i = 0; i < pathspec.nr; i++) {
44 const char *spec = pathspec.items[i].match;
47 if (strncmp(base, spec, baselen))
49 len = strlen(pathname);
51 speclen = strlen(spec);
54 if (spec[len] && spec[len] != '/')
56 if (memcmp(pathname, spec, len))
63 static int show_tree(const struct object_id *oid, struct strbuf *base,
64 const char *pathname, unsigned mode, int stage, void *context)
68 const char *type = blob_type;
70 if (S_ISGITLINK(mode)) {
72 * Maybe we want to have some recursive version here?
74 * Something similar to this incomplete example:
76 if (show_subprojects(base, baselen, pathname))
77 retval = READ_TREE_RECURSIVE;
81 } else if (S_ISDIR(mode)) {
82 if (show_recursive(base->buf, base->len, pathname)) {
83 retval = READ_TREE_RECURSIVE;
84 if (!(ls_options & LS_SHOW_TREES))
89 else if (ls_options & LS_TREE_ONLY)
92 if (!(ls_options & LS_NAME_ONLY)) {
93 if (ls_options & LS_SHOW_SIZE) {
95 if (!strcmp(type, blob_type)) {
97 if (oid_object_info(oid, &size) == OBJ_BAD)
98 xsnprintf(size_text, sizeof(size_text),
101 xsnprintf(size_text, sizeof(size_text),
104 xsnprintf(size_text, sizeof(size_text), "-");
105 printf("%06o %s %s %7s\t", mode, type,
106 find_unique_abbrev(oid, abbrev),
109 printf("%06o %s %s\t", mode, type,
110 find_unique_abbrev(oid, abbrev));
113 strbuf_addstr(base, pathname);
114 write_name_quoted_relative(base->buf,
115 chomp_prefix ? ls_tree_prefix : NULL,
116 stdout, line_termination);
117 strbuf_setlen(base, baselen);
121 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
123 struct object_id oid;
125 int i, full_tree = 0;
126 const struct option ls_tree_options[] = {
127 OPT_BIT('d', NULL, &ls_options, N_("only show trees"),
129 OPT_BIT('r', NULL, &ls_options, N_("recurse into subtrees"),
131 OPT_BIT('t', NULL, &ls_options, N_("show trees when recursing"),
133 OPT_SET_INT('z', NULL, &line_termination,
134 N_("terminate entries with NUL byte"), 0),
135 OPT_BIT('l', "long", &ls_options, N_("include object size"),
137 OPT_BIT(0, "name-only", &ls_options, N_("list only filenames"),
139 OPT_BIT(0, "name-status", &ls_options, N_("list only filenames"),
141 OPT_SET_INT(0, "full-name", &chomp_prefix,
142 N_("use full path names"), 0),
143 OPT_BOOL(0, "full-tree", &full_tree,
144 N_("list entire tree; not just current directory "
145 "(implies --full-name)")),
146 OPT__ABBREV(&abbrev),
150 git_config(git_default_config, NULL);
151 ls_tree_prefix = prefix;
152 if (prefix && *prefix)
153 chomp_prefix = strlen(prefix);
155 argc = parse_options(argc, argv, prefix, ls_tree_options,
158 ls_tree_prefix = prefix = NULL;
161 /* -d -r should imply -t, but -d by itself should not have to. */
162 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
163 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
164 ls_options |= LS_SHOW_TREES;
167 usage_with_options(ls_tree_usage, ls_tree_options);
168 if (get_oid(argv[0], &oid))
169 die("Not a valid object name %s", argv[0]);
172 * show_recursive() rolls its own matching code and is
173 * generally ignorant of 'struct pathspec'. The magic mask
174 * cannot be lifted until it is converted to use
175 * match_pathspec() or tree_entry_interesting()
177 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC &
178 ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
181 for (i = 0; i < pathspec.nr; i++)
182 pathspec.items[i].nowildcard_len = pathspec.items[i].len;
183 pathspec.has_wildcard = 0;
184 tree = parse_tree_indirect(&oid);
186 die("not a tree object");
187 return !!read_tree_recursive(tree, "", 0, 0, &pathspec, show_tree, NULL);