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