Update callers of check_ref_format()
[git] / builtin-ls-tree.c
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
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;
24
25 static const char ls_tree_usage[] =
26         "git-ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
27
28 static 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
58 static int show_tree(const unsigned char *sha1, const char *base, int baselen,
59                      const char *pathname, unsigned mode, int stage)
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 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)) {
83                 if (show_recursive(base, baselen, pathname)) {
84                         retval = READ_TREE_RECURSIVE;
85                         if (!(ls_options & LS_SHOW_TREES))
86                                 return retval;
87                 }
88                 type = tree_type;
89         }
90         else if (ls_options & LS_TREE_ONLY)
91                 return 0;
92
93         if (chomp_prefix &&
94             (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
95                 return 0;
96
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         }
115         write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
116                           pathname, stdout, line_termination);
117         return retval;
118 }
119
120 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
121 {
122         unsigned char sha1[20];
123         struct tree *tree;
124
125         git_config(git_default_config);
126         ls_tree_prefix = prefix;
127         if (prefix && *prefix)
128                 chomp_prefix = strlen(prefix);
129         while (1 < argc && argv[1][0] == '-') {
130                 switch (argv[1][1]) {
131                 case 'z':
132                         line_termination = 0;
133                         break;
134                 case 'r':
135                         ls_options |= LS_RECURSIVE;
136                         break;
137                 case 'd':
138                         ls_options |= LS_TREE_ONLY;
139                         break;
140                 case 't':
141                         ls_options |= LS_SHOW_TREES;
142                         break;
143                 case 'l':
144                         ls_options |= LS_SHOW_SIZE;
145                         break;
146                 case '-':
147                         if (!strcmp(argv[1]+2, "name-only") ||
148                             !strcmp(argv[1]+2, "name-status")) {
149                                 ls_options |= LS_NAME_ONLY;
150                                 break;
151                         }
152                         if (!strcmp(argv[1]+2, "long")) {
153                                 ls_options |= LS_SHOW_SIZE;
154                                 break;
155                         }
156                         if (!strcmp(argv[1]+2, "full-name")) {
157                                 chomp_prefix = 0;
158                                 break;
159                         }
160                         if (!prefixcmp(argv[1]+2, "abbrev=")) {
161                                 abbrev = strtoul(argv[1]+9, NULL, 10);
162                                 if (abbrev && abbrev < MINIMUM_ABBREV)
163                                         abbrev = MINIMUM_ABBREV;
164                                 else if (abbrev > 40)
165                                         abbrev = 40;
166                                 break;
167                         }
168                         if (!strcmp(argv[1]+2, "abbrev")) {
169                                 abbrev = DEFAULT_ABBREV;
170                                 break;
171                         }
172                         /* otherwise fallthru */
173                 default:
174                         usage(ls_tree_usage);
175                 }
176                 argc--; argv++;
177         }
178         /* -d -r should imply -t, but -d by itself should not have to. */
179         if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
180             ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
181                 ls_options |= LS_SHOW_TREES;
182
183         if (argc < 2)
184                 usage(ls_tree_usage);
185         if (get_sha1(argv[1], sha1))
186                 die("Not a valid object name %s", argv[1]);
187
188         pathspec = get_pathspec(prefix, argv + 2);
189         tree = parse_tree_indirect(sha1);
190         if (!tree)
191                 die("not a tree object");
192         read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
193
194         return 0;
195 }