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
13static int line_termination = '\n';
14#define LS_RECURSIVE 1
15#define LS_TREE_ONLY 2
16#define LS_SHOW_TREES 4
17#define LS_NAME_ONLY 8
18#define LS_SHOW_SIZE 16
19static int abbrev;
20static int ls_options;
21static const char **pathspec;
22static int chomp_prefix;
23static const char *ls_tree_prefix;
24
25static const char ls_tree_usage[] =
26 "git ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--full-tree] [--abbrev[=<n>]] <tree-ish> [path...]";
27
28static int show_recursive(const char *base, int baselen, const char *pathname)
29{
30 const char **s;
31
32 if (ls_options & LS_RECURSIVE)
33 return 1;
34
35 s = pathspec;
36 if (!s)
37 return 0;
38
39 for (;;) {
40 const char *spec = *s++;
41 int len, speclen;
42
43 if (!spec)
44 return 0;
45 if (strncmp(base, spec, baselen))
46 continue;
47 len = strlen(pathname);
48 spec += baselen;
49 speclen = strlen(spec);
50 if (speclen <= len)
51 continue;
52 if (memcmp(pathname, spec, len))
53 continue;
54 return 1;
55 }
56}
57
58static int show_tree(const unsigned char *sha1, const char *base, int baselen,
59 const char *pathname, unsigned mode, int stage, void *context)
60{
61 int retval = 0;
62 const char *type = blob_type;
63 unsigned long size;
64
65 if (S_ISGITLINK(mode)) {
66 /*
67 * Maybe we want to have some recursive version here?
68 *
69 * Something similar to this incomplete example:
70 *
71 if (show_subprojects(base, baselen, pathname))
72 retval = READ_TREE_RECURSIVE;
73 *
74 */
75 type = commit_type;
76 } else if (S_ISDIR(mode)) {
77 if (show_recursive(base, baselen, pathname)) {
78 retval = READ_TREE_RECURSIVE;
79 if (!(ls_options & LS_SHOW_TREES))
80 return retval;
81 }
82 type = tree_type;
83 }
84 else if (ls_options & LS_TREE_ONLY)
85 return 0;
86
87 if (chomp_prefix &&
88 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
89 return 0;
90
91 if (!(ls_options & LS_NAME_ONLY)) {
92 if (ls_options & LS_SHOW_SIZE) {
93 if (!strcmp(type, blob_type)) {
94 sha1_object_info(sha1, &size);
95 printf("%06o %s %s %7lu\t", mode, type,
96 abbrev ? find_unique_abbrev(sha1, abbrev)
97 : sha1_to_hex(sha1),
98 size);
99 } else
100 printf("%06o %s %s %7c\t", mode, type,
101 abbrev ? find_unique_abbrev(sha1, abbrev)
102 : sha1_to_hex(sha1),
103 '-');
104 } else
105 printf("%06o %s %s\t", mode, type,
106 abbrev ? find_unique_abbrev(sha1, abbrev)
107 : sha1_to_hex(sha1));
108 }
109 write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
110 pathname, stdout, line_termination);
111 return retval;
112}
113
114int cmd_ls_tree(int argc, const char **argv, const char *prefix)
115{
116 unsigned char sha1[20];
117 struct tree *tree;
118
119 git_config(git_default_config, NULL);
120 ls_tree_prefix = prefix;
121 if (prefix && *prefix)
122 chomp_prefix = strlen(prefix);
123 while (1 < argc && argv[1][0] == '-') {
124 switch (argv[1][1]) {
125 case 'z':
126 line_termination = 0;
127 break;
128 case 'r':
129 ls_options |= LS_RECURSIVE;
130 break;
131 case 'd':
132 ls_options |= LS_TREE_ONLY;
133 break;
134 case 't':
135 ls_options |= LS_SHOW_TREES;
136 break;
137 case 'l':
138 ls_options |= LS_SHOW_SIZE;
139 break;
140 case '-':
141 if (!strcmp(argv[1]+2, "name-only") ||
142 !strcmp(argv[1]+2, "name-status")) {
143 ls_options |= LS_NAME_ONLY;
144 break;
145 }
146 if (!strcmp(argv[1]+2, "long")) {
147 ls_options |= LS_SHOW_SIZE;
148 break;
149 }
150 if (!strcmp(argv[1]+2, "full-name")) {
151 chomp_prefix = 0;
152 break;
153 }
154 if (!strcmp(argv[1]+2, "full-tree")) {
155 ls_tree_prefix = prefix = NULL;
156 chomp_prefix = 0;
157 break;
158 }
159 if (!prefixcmp(argv[1]+2, "abbrev=")) {
160 abbrev = strtoul(argv[1]+9, NULL, 10);
161 if (abbrev && abbrev < MINIMUM_ABBREV)
162 abbrev = MINIMUM_ABBREV;
163 else if (abbrev > 40)
164 abbrev = 40;
165 break;
166 }
167 if (!strcmp(argv[1]+2, "abbrev")) {
168 abbrev = DEFAULT_ABBREV;
169 break;
170 }
171 /* otherwise fallthru */
172 default:
173 usage(ls_tree_usage);
174 }
175 argc--; argv++;
176 }
177 /* -d -r should imply -t, but -d by itself should not have to. */
178 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
179 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
180 ls_options |= LS_SHOW_TREES;
181
182 if (argc < 2)
183 usage(ls_tree_usage);
184 if (get_sha1(argv[1], sha1))
185 die("Not a valid object name %s", argv[1]);
186
187 pathspec = get_pathspec(prefix, argv + 2);
188 tree = parse_tree_indirect(sha1);
189 if (!tree)
190 die("not a tree object");
191 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree, NULL);
192
193 return 0;
194}