]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-ls-tree.c
Merge branch 'master' of git://repo.or.cz/git-gui
[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"
7912c070 12
e99d59ff 13static int line_termination = '\n';
6af1f019
JH
14#define LS_RECURSIVE 1
15#define LS_TREE_ONLY 2
0f8f45cb 16#define LS_SHOW_TREES 4
c639a554 17#define LS_NAME_ONLY 8
a5bbda8b 18#define LS_SHOW_SIZE 16
96f1e58f
DR
19static int abbrev;
20static int ls_options;
aae01bda 21static const char **pathspec;
96f1e58f 22static int chomp_prefix;
a633fca0 23static const char *ls_tree_prefix;
aa1c48df 24
3c5e8468 25static const char ls_tree_usage[] =
a5bbda8b 26 "git-ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
0f8f45cb
LT
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}
aa1c48df 57
097dc3d8 58static int show_tree(const unsigned char *sha1, const char *base, int baselen,
a69dd585 59 const char *pathname, unsigned mode, int stage)
6af1f019 60{
0f8f45cb 61 int retval = 0;
8e440259 62 const char *type = blob_type;
a5bbda8b 63 unsigned long size;
ab1630a3 64
302b9282 65 if (S_ISGITLINK(mode)) {
f35a6d3b
LT
66 /*
67 * Maybe we want to have some recursive version here?
68 *
69 * Something like:
70 *
71 if (show_subprojects(base, baselen, pathname)) {
72 if (fork()) {
73 chdir(base);
74 exec ls-tree;
75 }
76 waitpid();
77 }
78 *
79 * ..or similar..
80 */
81 type = commit_type;
82 } else if (S_ISDIR(mode)) {
0f8f45cb
LT
83 if (show_recursive(base, baselen, pathname)) {
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
a69dd585 93 if (chomp_prefix &&
a633fca0 94 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
a69dd585
JH
95 return 0;
96
a5bbda8b
JN
97 if (!(ls_options & LS_NAME_ONLY)) {
98 if (ls_options & LS_SHOW_SIZE) {
99 if (!strcmp(type, blob_type)) {
100 sha1_object_info(sha1, &size);
101 printf("%06o %s %s %7lu\t", mode, type,
102 abbrev ? find_unique_abbrev(sha1, abbrev)
103 : sha1_to_hex(sha1),
104 size);
105 } else
106 printf("%06o %s %s %7c\t", mode, type,
107 abbrev ? find_unique_abbrev(sha1, abbrev)
108 : sha1_to_hex(sha1),
109 '-');
110 } else
111 printf("%06o %s %s\t", mode, type,
112 abbrev ? find_unique_abbrev(sha1, abbrev)
113 : sha1_to_hex(sha1));
114 }
a69dd585
JH
115 write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
116 pathname,
117 line_termination, stdout);
32b5904b 118 putchar(line_termination);
0f8f45cb 119 return retval;
6af1f019 120}
0f2303f7 121
a633fca0 122int cmd_ls_tree(int argc, const char **argv, const char *prefix)
6af1f019 123{
7912c070 124 unsigned char sha1[20];
521698b1 125 struct tree *tree;
7912c070 126
84a9b58c 127 git_config(git_default_config);
a633fca0 128 ls_tree_prefix = prefix;
a69dd585
JH
129 if (prefix && *prefix)
130 chomp_prefix = strlen(prefix);
aa1c48df
JH
131 while (1 < argc && argv[1][0] == '-') {
132 switch (argv[1][1]) {
133 case 'z':
134 line_termination = 0;
135 break;
136 case 'r':
6af1f019
JH
137 ls_options |= LS_RECURSIVE;
138 break;
139 case 'd':
140 ls_options |= LS_TREE_ONLY;
aa1c48df 141 break;
0f8f45cb
LT
142 case 't':
143 ls_options |= LS_SHOW_TREES;
144 break;
a5bbda8b
JN
145 case 'l':
146 ls_options |= LS_SHOW_SIZE;
147 break;
c639a554
JH
148 case '-':
149 if (!strcmp(argv[1]+2, "name-only") ||
150 !strcmp(argv[1]+2, "name-status")) {
151 ls_options |= LS_NAME_ONLY;
152 break;
153 }
a5bbda8b
JN
154 if (!strcmp(argv[1]+2, "long")) {
155 ls_options |= LS_SHOW_SIZE;
156 break;
157 }
a69dd585
JH
158 if (!strcmp(argv[1]+2, "full-name")) {
159 chomp_prefix = 0;
160 break;
161 }
1968d77d 162 if (!prefixcmp(argv[1]+2, "abbrev=")) {
cb85bfe5
EW
163 abbrev = strtoul(argv[1]+9, NULL, 10);
164 if (abbrev && abbrev < MINIMUM_ABBREV)
165 abbrev = MINIMUM_ABBREV;
166 else if (abbrev > 40)
167 abbrev = 40;
168 break;
169 }
170 if (!strcmp(argv[1]+2, "abbrev")) {
171 abbrev = DEFAULT_ABBREV;
172 break;
173 }
c639a554 174 /* otherwise fallthru */
aa1c48df 175 default:
0f2303f7 176 usage(ls_tree_usage);
aa1c48df
JH
177 }
178 argc--; argv++;
179 }
f5984671
JH
180 /* -d -r should imply -t, but -d by itself should not have to. */
181 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
182 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
183 ls_options |= LS_SHOW_TREES;
aa1c48df 184
6d3a5077 185 if (argc < 2)
0f2303f7 186 usage(ls_tree_usage);
31fff305
DL
187 if (get_sha1(argv[1], sha1))
188 die("Not a valid object name %s", argv[1]);
6af1f019 189
e2466376 190 pathspec = get_pathspec(prefix, argv + 2);
521698b1
DB
191 tree = parse_tree_indirect(sha1);
192 if (!tree)
3c5e8468 193 die("not a tree object");
521698b1 194 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
3c5e8468 195
7912c070
PB
196 return 0;
197}