]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-ls-tree.c
Add script for importing bits-and-pieces to Git.
[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[] =
e1a59774 26 "git ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--full-tree] [--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,
671f0707 59 const char *pathname, unsigned mode, int stage, void *context)
6af1f019 60{
0f8f45cb 61 int retval = 0;
8e440259 62 const char *type = blob_type;
ab1630a3 63
302b9282 64 if (S_ISGITLINK(mode)) {
f35a6d3b
LT
65 /*
66 * Maybe we want to have some recursive version here?
67 *
7d0b18a4 68 * Something similar to this incomplete example:
f35a6d3b 69 *
d3bee161
LH
70 if (show_subprojects(base, baselen, pathname))
71 retval = READ_TREE_RECURSIVE;
f35a6d3b 72 *
f35a6d3b
LT
73 */
74 type = commit_type;
75 } else if (S_ISDIR(mode)) {
0f8f45cb
LT
76 if (show_recursive(base, baselen, pathname)) {
77 retval = READ_TREE_RECURSIVE;
78 if (!(ls_options & LS_SHOW_TREES))
79 return retval;
e2466376 80 }
8e440259 81 type = tree_type;
6af1f019 82 }
f5984671
JH
83 else if (ls_options & LS_TREE_ONLY)
84 return 0;
ab1630a3 85
a69dd585 86 if (chomp_prefix &&
a633fca0 87 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
a69dd585
JH
88 return 0;
89
a5bbda8b
JN
90 if (!(ls_options & LS_NAME_ONLY)) {
91 if (ls_options & LS_SHOW_SIZE) {
e392a852 92 char size_text[24];
a5bbda8b 93 if (!strcmp(type, blob_type)) {
e392a852
AR
94 unsigned long size;
95 if (sha1_object_info(sha1, &size) == OBJ_BAD)
96 strcpy(size_text, "BAD");
97 else
98 snprintf(size_text, sizeof(size_text),
99 "%lu", size);
a5bbda8b 100 } else
e392a852
AR
101 strcpy(size_text, "-");
102 printf("%06o %s %s %7s\t", mode, type,
103 abbrev ? find_unique_abbrev(sha1, abbrev)
104 : sha1_to_hex(sha1),
105 size_text);
a5bbda8b
JN
106 } else
107 printf("%06o %s %s\t", mode, type,
108 abbrev ? find_unique_abbrev(sha1, abbrev)
109 : sha1_to_hex(sha1));
110 }
663af342
PH
111 write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
112 pathname, stdout, line_termination);
0f8f45cb 113 return retval;
6af1f019 114}
0f2303f7 115
a633fca0 116int cmd_ls_tree(int argc, const char **argv, const char *prefix)
6af1f019 117{
7912c070 118 unsigned char sha1[20];
521698b1 119 struct tree *tree;
7912c070 120
ef90d6d4 121 git_config(git_default_config, NULL);
a633fca0 122 ls_tree_prefix = prefix;
a69dd585
JH
123 if (prefix && *prefix)
124 chomp_prefix = strlen(prefix);
aa1c48df
JH
125 while (1 < argc && argv[1][0] == '-') {
126 switch (argv[1][1]) {
127 case 'z':
128 line_termination = 0;
129 break;
130 case 'r':
6af1f019
JH
131 ls_options |= LS_RECURSIVE;
132 break;
133 case 'd':
134 ls_options |= LS_TREE_ONLY;
aa1c48df 135 break;
0f8f45cb
LT
136 case 't':
137 ls_options |= LS_SHOW_TREES;
138 break;
a5bbda8b
JN
139 case 'l':
140 ls_options |= LS_SHOW_SIZE;
141 break;
c639a554
JH
142 case '-':
143 if (!strcmp(argv[1]+2, "name-only") ||
144 !strcmp(argv[1]+2, "name-status")) {
145 ls_options |= LS_NAME_ONLY;
146 break;
147 }
a5bbda8b
JN
148 if (!strcmp(argv[1]+2, "long")) {
149 ls_options |= LS_SHOW_SIZE;
150 break;
151 }
a69dd585
JH
152 if (!strcmp(argv[1]+2, "full-name")) {
153 chomp_prefix = 0;
154 break;
155 }
d4789c60
JH
156 if (!strcmp(argv[1]+2, "full-tree")) {
157 ls_tree_prefix = prefix = NULL;
158 chomp_prefix = 0;
159 break;
160 }
1968d77d 161 if (!prefixcmp(argv[1]+2, "abbrev=")) {
cb85bfe5
EW
162 abbrev = strtoul(argv[1]+9, NULL, 10);
163 if (abbrev && abbrev < MINIMUM_ABBREV)
164 abbrev = MINIMUM_ABBREV;
165 else if (abbrev > 40)
166 abbrev = 40;
167 break;
168 }
169 if (!strcmp(argv[1]+2, "abbrev")) {
170 abbrev = DEFAULT_ABBREV;
171 break;
172 }
c639a554 173 /* otherwise fallthru */
aa1c48df 174 default:
0f2303f7 175 usage(ls_tree_usage);
aa1c48df
JH
176 }
177 argc--; argv++;
178 }
f5984671
JH
179 /* -d -r should imply -t, but -d by itself should not have to. */
180 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
181 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
182 ls_options |= LS_SHOW_TREES;
aa1c48df 183
6d3a5077 184 if (argc < 2)
0f2303f7 185 usage(ls_tree_usage);
31fff305
DL
186 if (get_sha1(argv[1], sha1))
187 die("Not a valid object name %s", argv[1]);
6af1f019 188
e2466376 189 pathspec = get_pathspec(prefix, argv + 2);
521698b1
DB
190 tree = parse_tree_indirect(sha1);
191 if (!tree)
3c5e8468 192 die("not a tree object");
671f0707 193 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree, NULL);
3c5e8468 194
7912c070
PB
195 return 0;
196}