]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-ls-tree.c
rename dirlink to gitlink.
[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
96f1e58f
DR
18static int abbrev;
19static int ls_options;
aae01bda 20static const char **pathspec;
96f1e58f 21static int chomp_prefix;
a633fca0 22static const char *ls_tree_prefix;
aa1c48df 23
3c5e8468 24static const char ls_tree_usage[] =
cb85bfe5 25 "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
0f8f45cb
LT
26
27static int show_recursive(const char *base, int baselen, const char *pathname)
28{
29 const char **s;
30
31 if (ls_options & LS_RECURSIVE)
32 return 1;
33
34 s = pathspec;
35 if (!s)
36 return 0;
37
38 for (;;) {
39 const char *spec = *s++;
40 int len, speclen;
41
42 if (!spec)
43 return 0;
44 if (strncmp(base, spec, baselen))
45 continue;
46 len = strlen(pathname);
47 spec += baselen;
48 speclen = strlen(spec);
49 if (speclen <= len)
50 continue;
51 if (memcmp(pathname, spec, len))
52 continue;
53 return 1;
54 }
55}
aa1c48df 56
097dc3d8 57static int show_tree(const unsigned char *sha1, const char *base, int baselen,
a69dd585 58 const char *pathname, unsigned mode, int stage)
6af1f019 59{
0f8f45cb 60 int retval = 0;
8e440259 61 const char *type = blob_type;
ab1630a3 62
302b9282 63 if (S_ISGITLINK(mode)) {
f35a6d3b
LT
64 /*
65 * Maybe we want to have some recursive version here?
66 *
67 * Something like:
68 *
69 if (show_subprojects(base, baselen, pathname)) {
70 if (fork()) {
71 chdir(base);
72 exec ls-tree;
73 }
74 waitpid();
75 }
76 *
77 * ..or similar..
78 */
79 type = commit_type;
80 } else if (S_ISDIR(mode)) {
0f8f45cb
LT
81 if (show_recursive(base, baselen, pathname)) {
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
a69dd585 91 if (chomp_prefix &&
a633fca0 92 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
a69dd585
JH
93 return 0;
94
c639a554 95 if (!(ls_options & LS_NAME_ONLY))
cb85bfe5
EW
96 printf("%06o %s %s\t", mode, type,
97 abbrev ? find_unique_abbrev(sha1,abbrev)
98 : sha1_to_hex(sha1));
a69dd585
JH
99 write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
100 pathname,
101 line_termination, stdout);
32b5904b 102 putchar(line_termination);
0f8f45cb 103 return retval;
6af1f019 104}
0f2303f7 105
a633fca0 106int cmd_ls_tree(int argc, const char **argv, const char *prefix)
6af1f019 107{
7912c070 108 unsigned char sha1[20];
521698b1 109 struct tree *tree;
7912c070 110
84a9b58c 111 git_config(git_default_config);
a633fca0 112 ls_tree_prefix = prefix;
a69dd585
JH
113 if (prefix && *prefix)
114 chomp_prefix = strlen(prefix);
aa1c48df
JH
115 while (1 < argc && argv[1][0] == '-') {
116 switch (argv[1][1]) {
117 case 'z':
118 line_termination = 0;
119 break;
120 case 'r':
6af1f019
JH
121 ls_options |= LS_RECURSIVE;
122 break;
123 case 'd':
124 ls_options |= LS_TREE_ONLY;
aa1c48df 125 break;
0f8f45cb
LT
126 case 't':
127 ls_options |= LS_SHOW_TREES;
128 break;
c639a554
JH
129 case '-':
130 if (!strcmp(argv[1]+2, "name-only") ||
131 !strcmp(argv[1]+2, "name-status")) {
132 ls_options |= LS_NAME_ONLY;
133 break;
134 }
a69dd585
JH
135 if (!strcmp(argv[1]+2, "full-name")) {
136 chomp_prefix = 0;
137 break;
138 }
1968d77d 139 if (!prefixcmp(argv[1]+2, "abbrev=")) {
cb85bfe5
EW
140 abbrev = strtoul(argv[1]+9, NULL, 10);
141 if (abbrev && abbrev < MINIMUM_ABBREV)
142 abbrev = MINIMUM_ABBREV;
143 else if (abbrev > 40)
144 abbrev = 40;
145 break;
146 }
147 if (!strcmp(argv[1]+2, "abbrev")) {
148 abbrev = DEFAULT_ABBREV;
149 break;
150 }
c639a554 151 /* otherwise fallthru */
aa1c48df 152 default:
0f2303f7 153 usage(ls_tree_usage);
aa1c48df
JH
154 }
155 argc--; argv++;
156 }
f5984671
JH
157 /* -d -r should imply -t, but -d by itself should not have to. */
158 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
159 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
160 ls_options |= LS_SHOW_TREES;
aa1c48df 161
6d3a5077 162 if (argc < 2)
0f2303f7 163 usage(ls_tree_usage);
31fff305
DL
164 if (get_sha1(argv[1], sha1))
165 die("Not a valid object name %s", argv[1]);
6af1f019 166
e2466376 167 pathspec = get_pathspec(prefix, argv + 2);
521698b1
DB
168 tree = parse_tree_indirect(sha1);
169 if (!tree)
3c5e8468 170 die("not a tree object");
521698b1 171 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
3c5e8468 172
7912c070
PB
173 return 0;
174}