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