2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
13 static int line_termination
= '\n';
14 #define LS_RECURSIVE 1
15 #define LS_TREE_ONLY 2
16 #define LS_SHOW_TREES 4
17 #define LS_NAME_ONLY 8
18 #define LS_SHOW_SIZE 16
20 static int ls_options
;
21 static const char **pathspec
;
22 static int chomp_prefix
;
23 static const char *ls_tree_prefix
;
25 static const char ls_tree_usage
[] =
26 "git-ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
28 static int show_recursive(const char *base
, int baselen
, const char *pathname
)
32 if (ls_options
& LS_RECURSIVE
)
40 const char *spec
= *s
++;
45 if (strncmp(base
, spec
, baselen
))
47 len
= strlen(pathname
);
49 speclen
= strlen(spec
);
52 if (memcmp(pathname
, spec
, len
))
58 static int show_tree(const unsigned char *sha1
, const char *base
, int baselen
,
59 const char *pathname
, unsigned mode
, int stage
)
62 const char *type
= blob_type
;
65 if (S_ISDIRLNK(mode
)) {
67 * Maybe we want to have some recursive version here?
71 if (show_subprojects(base, baselen, pathname)) {
82 } else if (S_ISDIR(mode
)) {
83 if (show_recursive(base
, baselen
, pathname
)) {
84 retval
= READ_TREE_RECURSIVE
;
85 if (!(ls_options
& LS_SHOW_TREES
))
90 else if (ls_options
& LS_TREE_ONLY
)
94 (baselen
< chomp_prefix
|| memcmp(ls_tree_prefix
, base
, chomp_prefix
)))
97 if (!(ls_options
& LS_NAME_ONLY
)) {
98 if (ls_options
& LS_SHOW_SIZE
) {
99 if (!strcmp(type
, blob_type
)) {
100 sha1_object_info(sha1
, &size
);
101 printf("%06o %s %s %7lu\t", mode
, type
,
102 abbrev
? find_unique_abbrev(sha1
, abbrev
)
106 printf("%06o %s %s %7c\t", mode
, type
,
107 abbrev
? find_unique_abbrev(sha1
, abbrev
)
111 printf("%06o %s %s\t", mode
, type
,
112 abbrev
? find_unique_abbrev(sha1
, abbrev
)
113 : sha1_to_hex(sha1
));
115 write_name_quoted(base
+ chomp_prefix
, baselen
- chomp_prefix
,
117 line_termination
, stdout
);
118 putchar(line_termination
);
122 int cmd_ls_tree(int argc
, const char **argv
, const char *prefix
)
124 unsigned char sha1
[20];
127 git_config(git_default_config
);
128 ls_tree_prefix
= prefix
;
129 if (prefix
&& *prefix
)
130 chomp_prefix
= strlen(prefix
);
131 while (1 < argc
&& argv
[1][0] == '-') {
132 switch (argv
[1][1]) {
134 line_termination
= 0;
137 ls_options
|= LS_RECURSIVE
;
140 ls_options
|= LS_TREE_ONLY
;
143 ls_options
|= LS_SHOW_TREES
;
146 ls_options
|= LS_SHOW_SIZE
;
149 if (!strcmp(argv
[1]+2, "name-only") ||
150 !strcmp(argv
[1]+2, "name-status")) {
151 ls_options
|= LS_NAME_ONLY
;
154 if (!strcmp(argv
[1]+2, "long")) {
155 ls_options
|= LS_SHOW_SIZE
;
158 if (!strcmp(argv
[1]+2, "full-name")) {
162 if (!prefixcmp(argv
[1]+2, "abbrev=")) {
163 abbrev
= strtoul(argv
[1]+9, NULL
, 10);
164 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
165 abbrev
= MINIMUM_ABBREV
;
166 else if (abbrev
> 40)
170 if (!strcmp(argv
[1]+2, "abbrev")) {
171 abbrev
= DEFAULT_ABBREV
;
174 /* otherwise fallthru */
176 usage(ls_tree_usage
);
180 /* -d -r should imply -t, but -d by itself should not have to. */
181 if ( (LS_TREE_ONLY
|LS_RECURSIVE
) ==
182 ((LS_TREE_ONLY
|LS_RECURSIVE
) & ls_options
))
183 ls_options
|= LS_SHOW_TREES
;
186 usage(ls_tree_usage
);
187 if (get_sha1(argv
[1], sha1
))
188 die("Not a valid object name %s", argv
[1]);
190 pathspec
= get_pathspec(prefix
, argv
+ 2);
191 tree
= parse_tree_indirect(sha1
);
193 die("not a tree object");
194 read_tree_recursive(tree
, "", 0, 0, pathspec
, show_tree
);