tree.c: make read_tree*() take 'struct repository *'
[git/git.git] / builtin / ls-tree.c
CommitLineData
7912c070
PB
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
b2141fc1 7#include "config.h"
cbd53a21 8#include "object-store.h"
6af1f019
JH
9#include "blob.h"
10#include "tree.h"
f35a6d3b 11#include "commit.h"
22ddf719 12#include "quote.h"
aae01bda 13#include "builtin.h"
61fdbcf9 14#include "parse-options.h"
64acde94 15#include "pathspec.h"
7912c070 16
e99d59ff 17static int line_termination = '\n';
6af1f019
JH
18#define LS_RECURSIVE 1
19#define LS_TREE_ONLY 2
0f8f45cb 20#define LS_SHOW_TREES 4
c639a554 21#define LS_NAME_ONLY 8
a5bbda8b 22#define LS_SHOW_SIZE 16
96f1e58f
DR
23static int abbrev;
24static int ls_options;
f0096c06 25static struct pathspec pathspec;
96f1e58f 26static int chomp_prefix;
a633fca0 27static const char *ls_tree_prefix;
aa1c48df 28
61fdbcf9 29static const char * const ls_tree_usage[] = {
373f9221 30 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
61fdbcf9
SB
31 NULL
32};
0f8f45cb
LT
33
34static int show_recursive(const char *base, int baselen, const char *pathname)
35{
e1e24edc 36 int i;
0f8f45cb
LT
37
38 if (ls_options & LS_RECURSIVE)
39 return 1;
40
e1e24edc 41 if (!pathspec.nr)
0f8f45cb
LT
42 return 0;
43
e1e24edc
BW
44 for (i = 0; i < pathspec.nr; i++) {
45 const char *spec = pathspec.items[i].match;
0f8f45cb
LT
46 int len, speclen;
47
0f8f45cb
LT
48 if (strncmp(base, spec, baselen))
49 continue;
50 len = strlen(pathname);
51 spec += baselen;
52 speclen = strlen(spec);
53 if (speclen <= len)
54 continue;
b294ed63
JH
55 if (spec[len] && spec[len] != '/')
56 continue;
0f8f45cb
LT
57 if (memcmp(pathname, spec, len))
58 continue;
59 return 1;
60 }
e1e24edc 61 return 0;
0f8f45cb 62}
aa1c48df 63
df46d77e 64static int show_tree(const struct object_id *oid, struct strbuf *base,
671f0707 65 const char *pathname, unsigned mode, int stage, void *context)
6af1f019 66{
0f8f45cb 67 int retval = 0;
1cf9952d 68 int baselen;
8e440259 69 const char *type = blob_type;
ab1630a3 70
302b9282 71 if (S_ISGITLINK(mode)) {
f35a6d3b
LT
72 /*
73 * Maybe we want to have some recursive version here?
74 *
7d0b18a4 75 * Something similar to this incomplete example:
f35a6d3b 76 *
d3bee161
LH
77 if (show_subprojects(base, baselen, pathname))
78 retval = READ_TREE_RECURSIVE;
f35a6d3b 79 *
f35a6d3b
LT
80 */
81 type = commit_type;
82 } else if (S_ISDIR(mode)) {
6a0b0b6d 83 if (show_recursive(base->buf, base->len, pathname)) {
0f8f45cb
LT
84 retval = READ_TREE_RECURSIVE;
85 if (!(ls_options & LS_SHOW_TREES))
86 return retval;
e2466376 87 }
8e440259 88 type = tree_type;
6af1f019 89 }
f5984671
JH
90 else if (ls_options & LS_TREE_ONLY)
91 return 0;
ab1630a3 92
a5bbda8b
JN
93 if (!(ls_options & LS_NAME_ONLY)) {
94 if (ls_options & LS_SHOW_SIZE) {
e392a852 95 char size_text[24];
a5bbda8b 96 if (!strcmp(type, blob_type)) {
e392a852 97 unsigned long size;
0df8e965 98 if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
5096d490
JK
99 xsnprintf(size_text, sizeof(size_text),
100 "BAD");
e392a852 101 else
5096d490
JK
102 xsnprintf(size_text, sizeof(size_text),
103 "%lu", size);
a5bbda8b 104 } else
5096d490 105 xsnprintf(size_text, sizeof(size_text), "-");
e392a852 106 printf("%06o %s %s %7s\t", mode, type,
aab9583f 107 find_unique_abbrev(oid, abbrev),
e392a852 108 size_text);
a5bbda8b
JN
109 } else
110 printf("%06o %s %s\t", mode, type,
aab9583f 111 find_unique_abbrev(oid, abbrev));
a5bbda8b 112 }
1cf9952d
NTND
113 baselen = base->len;
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);
0f8f45cb 119 return retval;
6af1f019 120}
0f2303f7 121
a633fca0 122int cmd_ls_tree(int argc, const char **argv, const char *prefix)
6af1f019 123{
a9b5f5bf 124 struct object_id oid;
521698b1 125 struct tree *tree;
f0096c06 126 int i, full_tree = 0;
61fdbcf9 127 const struct option ls_tree_options[] = {
373f9221 128 OPT_BIT('d', NULL, &ls_options, N_("only show trees"),
61fdbcf9 129 LS_TREE_ONLY),
373f9221 130 OPT_BIT('r', NULL, &ls_options, N_("recurse into subtrees"),
61fdbcf9 131 LS_RECURSIVE),
373f9221 132 OPT_BIT('t', NULL, &ls_options, N_("show trees when recursing"),
61fdbcf9
SB
133 LS_SHOW_TREES),
134 OPT_SET_INT('z', NULL, &line_termination,
373f9221
NTND
135 N_("terminate entries with NUL byte"), 0),
136 OPT_BIT('l', "long", &ls_options, N_("include object size"),
61fdbcf9 137 LS_SHOW_SIZE),
373f9221 138 OPT_BIT(0, "name-only", &ls_options, N_("list only filenames"),
61fdbcf9 139 LS_NAME_ONLY),
373f9221 140 OPT_BIT(0, "name-status", &ls_options, N_("list only filenames"),
61fdbcf9
SB
141 LS_NAME_ONLY),
142 OPT_SET_INT(0, "full-name", &chomp_prefix,
373f9221 143 N_("use full path names"), 0),
d5d09d47
SB
144 OPT_BOOL(0, "full-tree", &full_tree,
145 N_("list entire tree; not just current directory "
146 "(implies --full-name)")),
61fdbcf9
SB
147 OPT__ABBREV(&abbrev),
148 OPT_END()
149 };
7912c070 150
ef90d6d4 151 git_config(git_default_config, NULL);
a633fca0 152 ls_tree_prefix = prefix;
a69dd585
JH
153 if (prefix && *prefix)
154 chomp_prefix = strlen(prefix);
61fdbcf9
SB
155
156 argc = parse_options(argc, argv, prefix, ls_tree_options,
157 ls_tree_usage, 0);
158 if (full_tree) {
159 ls_tree_prefix = prefix = NULL;
160 chomp_prefix = 0;
aa1c48df 161 }
f5984671
JH
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;
aa1c48df 166
61fdbcf9
SB
167 if (argc < 1)
168 usage_with_options(ls_tree_usage, ls_tree_options);
a9b5f5bf 169 if (get_oid(argv[0], &oid))
61fdbcf9 170 die("Not a valid object name %s", argv[0]);
6af1f019 171
0fdc2ae5
NTND
172 /*
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
854b0959 176 * match_pathspec() or tree_entry_interesting()
0fdc2ae5 177 */
e1e24edc
BW
178 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC &
179 ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
0fdc2ae5
NTND
180 PATHSPEC_PREFER_CWD,
181 prefix, argv + 1);
f0096c06 182 for (i = 0; i < pathspec.nr; i++)
170260ae 183 pathspec.items[i].nowildcard_len = pathspec.items[i].len;
33e0f62b 184 pathspec.has_wildcard = 0;
a9dbc179 185 tree = parse_tree_indirect(&oid);
521698b1 186 if (!tree)
3c5e8468 187 die("not a tree object");
e092073d
NTND
188 return !!read_tree_recursive(the_repository, tree, "", 0, 0,
189 &pathspec, show_tree, NULL);
7912c070 190}