]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-ls-tree.c
Merge branch 'ml/maint-grep-doc'
[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"
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"
7912c070 13
e99d59ff 14static int line_termination = '\n';
6af1f019
JH
15#define LS_RECURSIVE 1
16#define LS_TREE_ONLY 2
0f8f45cb 17#define LS_SHOW_TREES 4
c639a554 18#define LS_NAME_ONLY 8
a5bbda8b 19#define LS_SHOW_SIZE 16
96f1e58f
DR
20static int abbrev;
21static int ls_options;
aae01bda 22static const char **pathspec;
96f1e58f 23static int chomp_prefix;
a633fca0 24static const char *ls_tree_prefix;
aa1c48df 25
61fdbcf9
SB
26static const char * const ls_tree_usage[] = {
27 "git ls-tree [<options>] <tree-ish> [path...]",
28 NULL
29};
0f8f45cb
LT
30
31static int show_recursive(const char *base, int baselen, const char *pathname)
32{
33 const char **s;
34
35 if (ls_options & LS_RECURSIVE)
36 return 1;
37
38 s = pathspec;
39 if (!s)
40 return 0;
41
42 for (;;) {
43 const char *spec = *s++;
44 int len, speclen;
45
46 if (!spec)
47 return 0;
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;
55 if (memcmp(pathname, spec, len))
56 continue;
57 return 1;
58 }
59}
aa1c48df 60
097dc3d8 61static int show_tree(const unsigned char *sha1, const char *base, int baselen,
671f0707 62 const char *pathname, unsigned mode, int stage, void *context)
6af1f019 63{
0f8f45cb 64 int retval = 0;
8e440259 65 const char *type = blob_type;
ab1630a3 66
302b9282 67 if (S_ISGITLINK(mode)) {
f35a6d3b
LT
68 /*
69 * Maybe we want to have some recursive version here?
70 *
7d0b18a4 71 * Something similar to this incomplete example:
f35a6d3b 72 *
d3bee161
LH
73 if (show_subprojects(base, baselen, pathname))
74 retval = READ_TREE_RECURSIVE;
f35a6d3b 75 *
f35a6d3b
LT
76 */
77 type = commit_type;
78 } else if (S_ISDIR(mode)) {
0f8f45cb
LT
79 if (show_recursive(base, baselen, pathname)) {
80 retval = READ_TREE_RECURSIVE;
81 if (!(ls_options & LS_SHOW_TREES))
82 return retval;
e2466376 83 }
8e440259 84 type = tree_type;
6af1f019 85 }
f5984671
JH
86 else if (ls_options & LS_TREE_ONLY)
87 return 0;
ab1630a3 88
a69dd585 89 if (chomp_prefix &&
a633fca0 90 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
a69dd585
JH
91 return 0;
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
AR
97 unsigned long size;
98 if (sha1_object_info(sha1, &size) == OBJ_BAD)
99 strcpy(size_text, "BAD");
100 else
101 snprintf(size_text, sizeof(size_text),
102 "%lu", size);
a5bbda8b 103 } else
e392a852
AR
104 strcpy(size_text, "-");
105 printf("%06o %s %s %7s\t", mode, type,
106 abbrev ? find_unique_abbrev(sha1, abbrev)
107 : sha1_to_hex(sha1),
108 size_text);
a5bbda8b
JN
109 } else
110 printf("%06o %s %s\t", mode, type,
111 abbrev ? find_unique_abbrev(sha1, abbrev)
112 : sha1_to_hex(sha1));
113 }
663af342
PH
114 write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
115 pathname, stdout, line_termination);
0f8f45cb 116 return retval;
6af1f019 117}
0f2303f7 118
a633fca0 119int cmd_ls_tree(int argc, const char **argv, const char *prefix)
6af1f019 120{
7912c070 121 unsigned char sha1[20];
521698b1 122 struct tree *tree;
61fdbcf9
SB
123 int full_tree = 0;
124 const struct option ls_tree_options[] = {
125 OPT_BIT('d', NULL, &ls_options, "only show trees",
126 LS_TREE_ONLY),
127 OPT_BIT('r', NULL, &ls_options, "recurse into subtrees",
128 LS_RECURSIVE),
129 OPT_BIT('t', NULL, &ls_options, "show trees when recursing",
130 LS_SHOW_TREES),
131 OPT_SET_INT('z', NULL, &line_termination,
132 "terminate entries with NUL byte", 0),
133 OPT_BIT('l', "long", &ls_options, "include object size",
134 LS_SHOW_SIZE),
135 OPT_BIT(0, "name-only", &ls_options, "list only filenames",
136 LS_NAME_ONLY),
137 OPT_BIT(0, "name-status", &ls_options, "list only filenames",
138 LS_NAME_ONLY),
139 OPT_SET_INT(0, "full-name", &chomp_prefix,
140 "use full path names", 0),
141 OPT_BOOLEAN(0, "full-tree", &full_tree,
142 "list entire tree; not just current directory "
143 "(implies --full-name)"),
144 OPT__ABBREV(&abbrev),
145 OPT_END()
146 };
7912c070 147
ef90d6d4 148 git_config(git_default_config, NULL);
a633fca0 149 ls_tree_prefix = prefix;
a69dd585
JH
150 if (prefix && *prefix)
151 chomp_prefix = strlen(prefix);
61fdbcf9
SB
152
153 argc = parse_options(argc, argv, prefix, ls_tree_options,
154 ls_tree_usage, 0);
155 if (full_tree) {
156 ls_tree_prefix = prefix = NULL;
157 chomp_prefix = 0;
aa1c48df 158 }
f5984671
JH
159 /* -d -r should imply -t, but -d by itself should not have to. */
160 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
161 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
162 ls_options |= LS_SHOW_TREES;
aa1c48df 163
61fdbcf9
SB
164 if (argc < 1)
165 usage_with_options(ls_tree_usage, ls_tree_options);
166 if (get_sha1(argv[0], sha1))
167 die("Not a valid object name %s", argv[0]);
6af1f019 168
61fdbcf9 169 pathspec = get_pathspec(prefix, argv + 1);
521698b1
DB
170 tree = parse_tree_indirect(sha1);
171 if (!tree)
3c5e8468 172 die("not a tree object");
671f0707 173 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree, NULL);
3c5e8468 174
7912c070
PB
175 return 0;
176}