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 2
20 #define LS_SHOW_TREES 4
21 #define LS_NAME_ONLY 8
22 #define LS_SHOW_SIZE 16
24 static int ls_options
;
25 static struct pathspec pathspec
;
26 static int chomp_prefix
;
27 static const char *ls_tree_prefix
;
29 static const char * const ls_tree_usage
[] = {
30 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
34 static int show_recursive(const char *base
, int baselen
, const char *pathname
)
38 if (ls_options
& LS_RECURSIVE
)
44 for (i
= 0; i
< pathspec
.nr
; i
++) {
45 const char *spec
= pathspec
.items
[i
].match
;
48 if (strncmp(base
, spec
, baselen
))
50 len
= strlen(pathname
);
52 speclen
= strlen(spec
);
55 if (spec
[len
] && spec
[len
] != '/')
57 if (memcmp(pathname
, spec
, len
))
64 static int show_tree(const struct object_id
*oid
, struct strbuf
*base
,
65 const char *pathname
, unsigned mode
, int stage
, void *context
)
69 const char *type
= blob_type
;
71 if (S_ISGITLINK(mode
)) {
73 * Maybe we want to have some recursive version here?
75 * Something similar to this incomplete example:
77 if (show_subprojects(base, baselen, pathname))
78 retval = READ_TREE_RECURSIVE;
82 } else if (S_ISDIR(mode
)) {
83 if (show_recursive(base
->buf
, base
->len
, pathname
)) {
84 retval
= READ_TREE_RECURSIVE
;
85 if (!(ls_options
& LS_SHOW_TREES
))
90 else if (ls_options
& LS_TREE_ONLY
)
93 if (!(ls_options
& LS_NAME_ONLY
)) {
94 if (ls_options
& LS_SHOW_SIZE
) {
96 if (!strcmp(type
, blob_type
)) {
98 if (oid_object_info(the_repository
, oid
, &size
) == OBJ_BAD
)
99 xsnprintf(size_text
, sizeof(size_text
),
102 xsnprintf(size_text
, sizeof(size_text
),
105 xsnprintf(size_text
, sizeof(size_text
), "-");
106 printf("%06o %s %s %7s\t", mode
, type
,
107 find_unique_abbrev(oid
, abbrev
),
110 printf("%06o %s %s\t", mode
, type
,
111 find_unique_abbrev(oid
, abbrev
));
114 strbuf_addstr(base
, pathname
);
115 write_name_quoted_relative(base
->buf
,
116 chomp_prefix
? ls_tree_prefix
: NULL
,
117 stdout
, line_termination
);
118 strbuf_setlen(base
, baselen
);
122 int cmd_ls_tree(int argc
, const char **argv
, const char *prefix
)
124 struct object_id oid
;
126 int i
, full_tree
= 0;
127 const struct option ls_tree_options
[] = {
128 OPT_BIT('d', NULL
, &ls_options
, N_("only show trees"),
130 OPT_BIT('r', NULL
, &ls_options
, N_("recurse into subtrees"),
132 OPT_BIT('t', NULL
, &ls_options
, N_("show trees when recursing"),
134 OPT_SET_INT('z', NULL
, &line_termination
,
135 N_("terminate entries with NUL byte"), 0),
136 OPT_BIT('l', "long", &ls_options
, N_("include object size"),
138 OPT_BIT(0, "name-only", &ls_options
, N_("list only filenames"),
140 OPT_BIT(0, "name-status", &ls_options
, N_("list only filenames"),
142 OPT_SET_INT(0, "full-name", &chomp_prefix
,
143 N_("use full path names"), 0),
144 OPT_BOOL(0, "full-tree", &full_tree
,
145 N_("list entire tree; not just current directory "
146 "(implies --full-name)")),
147 OPT__ABBREV(&abbrev
),
151 git_config(git_default_config
, NULL
);
152 ls_tree_prefix
= prefix
;
153 if (prefix
&& *prefix
)
154 chomp_prefix
= strlen(prefix
);
156 argc
= parse_options(argc
, argv
, prefix
, ls_tree_options
,
159 ls_tree_prefix
= prefix
= NULL
;
162 /* -d -r should imply -t, but -d by itself should not have to. */
163 if ( (LS_TREE_ONLY
|LS_RECURSIVE
) ==
164 ((LS_TREE_ONLY
|LS_RECURSIVE
) & ls_options
))
165 ls_options
|= LS_SHOW_TREES
;
168 usage_with_options(ls_tree_usage
, ls_tree_options
);
169 if (get_oid(argv
[0], &oid
))
170 die("Not a valid object name %s", argv
[0]);
173 * show_recursive() rolls its own matching code and is
174 * generally ignorant of 'struct pathspec'. The magic mask
175 * cannot be lifted until it is converted to use
176 * match_pathspec() or tree_entry_interesting()
178 parse_pathspec(&pathspec
, PATHSPEC_ALL_MAGIC
&
179 ~(PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
),
182 for (i
= 0; i
< pathspec
.nr
; i
++)
183 pathspec
.items
[i
].nowildcard_len
= pathspec
.items
[i
].len
;
184 pathspec
.has_wildcard
= 0;
185 tree
= parse_tree_indirect(&oid
);
187 die("not a tree object");
188 return !!read_tree_recursive(tree
, "", 0, 0, &pathspec
, show_tree
, NULL
);