add -p: prompt for single characters
[git/spearce.git] / builtin-ls-tree.c
blob5b63e6eada5cd6de764acef694da624a70ce6dab
1 /*
2 * GIT - The information manager from hell
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"
13 static 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
19 static int abbrev;
20 static int ls_options;
21 static const char **pathspec;
22 static int chomp_prefix;
23 static const char *ls_tree_prefix;
25 static 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...]";
28 static int show_recursive(const char *base, int baselen, const char *pathname)
30 const char **s;
32 if (ls_options & LS_RECURSIVE)
33 return 1;
35 s = pathspec;
36 if (!s)
37 return 0;
39 for (;;) {
40 const char *spec = *s++;
41 int len, speclen;
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;
58 static int show_tree(const unsigned char *sha1, const char *base, int baselen,
59 const char *pathname, unsigned mode, int stage, void *context)
61 int retval = 0;
62 const char *type = blob_type;
63 unsigned long size;
65 if (S_ISGITLINK(mode)) {
67 * Maybe we want to have some recursive version here?
69 * Something similar to this incomplete example:
71 if (show_subprojects(base, baselen, pathname)) {
72 struct child_process ls_tree;
74 ls_tree.dir = base;
75 ls_tree.argv = ls-tree;
76 start_command(&ls_tree);
80 type = commit_type;
81 } else if (S_ISDIR(mode)) {
82 if (show_recursive(base, baselen, pathname)) {
83 retval = READ_TREE_RECURSIVE;
84 if (!(ls_options & LS_SHOW_TREES))
85 return retval;
87 type = tree_type;
89 else if (ls_options & LS_TREE_ONLY)
90 return 0;
92 if (chomp_prefix &&
93 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
94 return 0;
96 if (!(ls_options & LS_NAME_ONLY)) {
97 if (ls_options & LS_SHOW_SIZE) {
98 if (!strcmp(type, blob_type)) {
99 sha1_object_info(sha1, &size);
100 printf("%06o %s %s %7lu\t", mode, type,
101 abbrev ? find_unique_abbrev(sha1, abbrev)
102 : sha1_to_hex(sha1),
103 size);
104 } else
105 printf("%06o %s %s %7c\t", mode, type,
106 abbrev ? find_unique_abbrev(sha1, abbrev)
107 : sha1_to_hex(sha1),
108 '-');
109 } else
110 printf("%06o %s %s\t", mode, type,
111 abbrev ? find_unique_abbrev(sha1, abbrev)
112 : sha1_to_hex(sha1));
114 write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
115 pathname, stdout, line_termination);
116 return retval;
119 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
121 unsigned char sha1[20];
122 struct tree *tree;
124 git_config(git_default_config, NULL);
125 ls_tree_prefix = prefix;
126 if (prefix && *prefix)
127 chomp_prefix = strlen(prefix);
128 while (1 < argc && argv[1][0] == '-') {
129 switch (argv[1][1]) {
130 case 'z':
131 line_termination = 0;
132 break;
133 case 'r':
134 ls_options |= LS_RECURSIVE;
135 break;
136 case 'd':
137 ls_options |= LS_TREE_ONLY;
138 break;
139 case 't':
140 ls_options |= LS_SHOW_TREES;
141 break;
142 case 'l':
143 ls_options |= LS_SHOW_SIZE;
144 break;
145 case '-':
146 if (!strcmp(argv[1]+2, "name-only") ||
147 !strcmp(argv[1]+2, "name-status")) {
148 ls_options |= LS_NAME_ONLY;
149 break;
151 if (!strcmp(argv[1]+2, "long")) {
152 ls_options |= LS_SHOW_SIZE;
153 break;
155 if (!strcmp(argv[1]+2, "full-name")) {
156 chomp_prefix = 0;
157 break;
159 if (!strcmp(argv[1]+2, "full-tree")) {
160 ls_tree_prefix = prefix = NULL;
161 chomp_prefix = 0;
162 break;
164 if (!prefixcmp(argv[1]+2, "abbrev=")) {
165 abbrev = strtoul(argv[1]+9, NULL, 10);
166 if (abbrev && abbrev < MINIMUM_ABBREV)
167 abbrev = MINIMUM_ABBREV;
168 else if (abbrev > 40)
169 abbrev = 40;
170 break;
172 if (!strcmp(argv[1]+2, "abbrev")) {
173 abbrev = DEFAULT_ABBREV;
174 break;
176 /* otherwise fallthru */
177 default:
178 usage(ls_tree_usage);
180 argc--; argv++;
182 /* -d -r should imply -t, but -d by itself should not have to. */
183 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
184 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
185 ls_options |= LS_SHOW_TREES;
187 if (argc < 2)
188 usage(ls_tree_usage);
189 if (get_sha1(argv[1], sha1))
190 die("Not a valid object name %s", argv[1]);
192 pathspec = get_pathspec(prefix, argv + 2);
193 tree = parse_tree_indirect(sha1);
194 if (!tree)
195 die("not a tree object");
196 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree, NULL);
198 return 0;