use mode of the tree in git-diff, if <tree>:<file> syntax is used
[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 static int abbrev;
19 static int ls_options;
20 static const char **pathspec;
21 static int chomp_prefix;
22 static const char *ls_tree_prefix;
23
24 static const char ls_tree_usage[] =
25         "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
26
27 static 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 }
56
57 static int show_tree(const unsigned char *sha1, const char *base, int baselen,
58                      const char *pathname, unsigned mode, int stage)
59 {
60         int retval = 0;
61         const char *type = blob_type;
62
63         if (S_ISDIRLNK(mode)) {
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)) {
81                 if (show_recursive(base, baselen, pathname)) {
82                         retval = READ_TREE_RECURSIVE;
83                         if (!(ls_options & LS_SHOW_TREES))
84                                 return retval;
85                 }
86                 type = tree_type;
87         }
88         else if (ls_options & LS_TREE_ONLY)
89                 return 0;
90
91         if (chomp_prefix &&
92             (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
93                 return 0;
94
95         if (!(ls_options & LS_NAME_ONLY))
96                 printf("%06o %s %s\t", mode, type,
97                                 abbrev ? find_unique_abbrev(sha1,abbrev)
98                                         : sha1_to_hex(sha1));
99         write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
100                           pathname,
101                           line_termination, stdout);
102         putchar(line_termination);
103         return retval;
104 }
105
106 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
107 {
108         unsigned char sha1[20];
109         struct tree *tree;
110
111         git_config(git_default_config);
112         ls_tree_prefix = prefix;
113         if (prefix && *prefix)
114                 chomp_prefix = strlen(prefix);
115         while (1 < argc && argv[1][0] == '-') {
116                 switch (argv[1][1]) {
117                 case 'z':
118                         line_termination = 0;
119                         break;
120                 case 'r':
121                         ls_options |= LS_RECURSIVE;
122                         break;
123                 case 'd':
124                         ls_options |= LS_TREE_ONLY;
125                         break;
126                 case 't':
127                         ls_options |= LS_SHOW_TREES;
128                         break;
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                         }
135                         if (!strcmp(argv[1]+2, "full-name")) {
136                                 chomp_prefix = 0;
137                                 break;
138                         }
139                         if (!prefixcmp(argv[1]+2, "abbrev=")) {
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                         }
151                         /* otherwise fallthru */
152                 default:
153                         usage(ls_tree_usage);
154                 }
155                 argc--; argv++;
156         }
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;
161
162         if (argc < 2)
163                 usage(ls_tree_usage);
164         if (get_sha1(argv[1], sha1))
165                 die("Not a valid object name %s", argv[1]);
166
167         pathspec = get_pathspec(prefix, argv + 2);
168         tree = parse_tree_indirect(sha1);
169         if (!tree)
170                 die("not a tree object");
171         read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
172
173         return 0;
174 }