tree.c: update read_tree_recursive callback to pass strbuf as base
[git/git.git] / builtin / ls-tree.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "blob.h"
8 #include "tree.h"
9 #include "commit.h"
10 #include "quote.h"
11 #include "builtin.h"
12 #include "parse-options.h"
13 #include "pathspec.h"
14
15 static int line_termination = '\n';
16 #define LS_RECURSIVE 1
17 #define LS_TREE_ONLY 2
18 #define LS_SHOW_TREES 4
19 #define LS_NAME_ONLY 8
20 #define LS_SHOW_SIZE 16
21 static int abbrev;
22 static int ls_options;
23 static struct pathspec pathspec;
24 static int chomp_prefix;
25 static const char *ls_tree_prefix;
26
27 static const char * const ls_tree_usage[] = {
28 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
29 NULL
30 };
31
32 static int show_recursive(const char *base, int baselen, const char *pathname)
33 {
34 const char **s;
35
36 if (ls_options & LS_RECURSIVE)
37 return 1;
38
39 s = pathspec._raw;
40 if (!s)
41 return 0;
42
43 for (;;) {
44 const char *spec = *s++;
45 int len, speclen;
46
47 if (!spec)
48 return 0;
49 if (strncmp(base, spec, baselen))
50 continue;
51 len = strlen(pathname);
52 spec += baselen;
53 speclen = strlen(spec);
54 if (speclen <= len)
55 continue;
56 if (spec[len] && spec[len] != '/')
57 continue;
58 if (memcmp(pathname, spec, len))
59 continue;
60 return 1;
61 }
62 }
63
64 static int show_tree(const unsigned char *sha1, struct strbuf *base,
65 const char *pathname, unsigned mode, int stage, void *context)
66 {
67 int retval = 0;
68 const char *type = blob_type;
69
70 if (S_ISGITLINK(mode)) {
71 /*
72 * Maybe we want to have some recursive version here?
73 *
74 * Something similar to this incomplete example:
75 *
76 if (show_subprojects(base, baselen, pathname))
77 retval = READ_TREE_RECURSIVE;
78 *
79 */
80 type = commit_type;
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))
85 return retval;
86 }
87 type = tree_type;
88 }
89 else if (ls_options & LS_TREE_ONLY)
90 return 0;
91
92 if (chomp_prefix &&
93 (base->len < chomp_prefix ||
94 memcmp(ls_tree_prefix, base->buf, chomp_prefix)))
95 return 0;
96
97 if (!(ls_options & LS_NAME_ONLY)) {
98 if (ls_options & LS_SHOW_SIZE) {
99 char size_text[24];
100 if (!strcmp(type, blob_type)) {
101 unsigned long size;
102 if (sha1_object_info(sha1, &size) == OBJ_BAD)
103 strcpy(size_text, "BAD");
104 else
105 snprintf(size_text, sizeof(size_text),
106 "%lu", size);
107 } else
108 strcpy(size_text, "-");
109 printf("%06o %s %s %7s\t", mode, type,
110 find_unique_abbrev(sha1, abbrev),
111 size_text);
112 } else
113 printf("%06o %s %s\t", mode, type,
114 find_unique_abbrev(sha1, abbrev));
115 }
116 write_name_quotedpfx(base->buf + chomp_prefix, base->len - chomp_prefix,
117 pathname, stdout, line_termination);
118 return retval;
119 }
120
121 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
122 {
123 unsigned char sha1[20];
124 struct tree *tree;
125 int i, full_tree = 0;
126 const struct option ls_tree_options[] = {
127 OPT_BIT('d', NULL, &ls_options, N_("only show trees"),
128 LS_TREE_ONLY),
129 OPT_BIT('r', NULL, &ls_options, N_("recurse into subtrees"),
130 LS_RECURSIVE),
131 OPT_BIT('t', NULL, &ls_options, N_("show trees when recursing"),
132 LS_SHOW_TREES),
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"),
136 LS_SHOW_SIZE),
137 OPT_BIT(0, "name-only", &ls_options, N_("list only filenames"),
138 LS_NAME_ONLY),
139 OPT_BIT(0, "name-status", &ls_options, N_("list only filenames"),
140 LS_NAME_ONLY),
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),
147 OPT_END()
148 };
149
150 git_config(git_default_config, NULL);
151 ls_tree_prefix = prefix;
152 if (prefix && *prefix)
153 chomp_prefix = strlen(prefix);
154
155 argc = parse_options(argc, argv, prefix, ls_tree_options,
156 ls_tree_usage, 0);
157 if (full_tree) {
158 ls_tree_prefix = prefix = NULL;
159 chomp_prefix = 0;
160 }
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;
165
166 if (argc < 1)
167 usage_with_options(ls_tree_usage, ls_tree_options);
168 if (get_sha1(argv[0], sha1))
169 die("Not a valid object name %s", argv[0]);
170
171 /*
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()
176 */
177 parse_pathspec(&pathspec, PATHSPEC_GLOB | PATHSPEC_ICASE,
178 PATHSPEC_PREFER_CWD,
179 prefix, argv + 1);
180 for (i = 0; i < pathspec.nr; i++)
181 pathspec.items[i].nowildcard_len = pathspec.items[i].len;
182 pathspec.has_wildcard = 0;
183 tree = parse_tree_indirect(sha1);
184 if (!tree)
185 die("not a tree object");
186 return !!read_tree_recursive(tree, "", 0, 0, &pathspec, show_tree, NULL);
187 }