Merge branch 'jc/mailinfo'
[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 "quote.h"
10 #include "builtin.h"
11
12 static int line_termination = '\n';
13 #define LS_RECURSIVE 1
14 #define LS_TREE_ONLY 2
15 #define LS_SHOW_TREES 4
16 #define LS_NAME_ONLY 8
17 static int abbrev = 0;
18 static int ls_options = 0;
19 static const char **pathspec;
20 static int chomp_prefix = 0;
21 static const char *prefix;
22
23 static const char ls_tree_usage[] =
24         "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
25
26 static int show_recursive(const char *base, int baselen, const char *pathname)
27 {
28         const char **s;
29
30         if (ls_options & LS_RECURSIVE)
31                 return 1;
32
33         s = pathspec;
34         if (!s)
35                 return 0;
36
37         for (;;) {
38                 const char *spec = *s++;
39                 int len, speclen;
40
41                 if (!spec)
42                         return 0;
43                 if (strncmp(base, spec, baselen))
44                         continue;
45                 len = strlen(pathname);
46                 spec += baselen;
47                 speclen = strlen(spec);
48                 if (speclen <= len)
49                         continue;
50                 if (memcmp(pathname, spec, len))
51                         continue;
52                 return 1;
53         }
54 }
55
56 static int show_tree(unsigned char *sha1, const char *base, int baselen,
57                      const char *pathname, unsigned mode, int stage)
58 {
59         int retval = 0;
60         const char *type = blob_type;
61
62         if (S_ISDIR(mode)) {
63                 if (show_recursive(base, baselen, pathname)) {
64                         retval = READ_TREE_RECURSIVE;
65                         if (!(ls_options & LS_SHOW_TREES))
66                                 return retval;
67                 }
68                 type = tree_type;
69         }
70         else if (ls_options & LS_TREE_ONLY)
71                 return 0;
72
73         if (chomp_prefix &&
74             (baselen < chomp_prefix || memcmp(prefix, base, chomp_prefix)))
75                 return 0;
76
77         if (!(ls_options & LS_NAME_ONLY))
78                 printf("%06o %s %s\t", mode, type,
79                                 abbrev ? find_unique_abbrev(sha1,abbrev)
80                                         : sha1_to_hex(sha1));
81         write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
82                           pathname,
83                           line_termination, stdout);
84         putchar(line_termination);
85         return retval;
86 }
87
88 int cmd_ls_tree(int argc, const char **argv, char **envp)
89 {
90         unsigned char sha1[20];
91         struct tree *tree;
92
93         prefix = setup_git_directory();
94         git_config(git_default_config);
95         if (prefix && *prefix)
96                 chomp_prefix = strlen(prefix);
97         while (1 < argc && argv[1][0] == '-') {
98                 switch (argv[1][1]) {
99                 case 'z':
100                         line_termination = 0;
101                         break;
102                 case 'r':
103                         ls_options |= LS_RECURSIVE;
104                         break;
105                 case 'd':
106                         ls_options |= LS_TREE_ONLY;
107                         break;
108                 case 't':
109                         ls_options |= LS_SHOW_TREES;
110                         break;
111                 case '-':
112                         if (!strcmp(argv[1]+2, "name-only") ||
113                             !strcmp(argv[1]+2, "name-status")) {
114                                 ls_options |= LS_NAME_ONLY;
115                                 break;
116                         }
117                         if (!strcmp(argv[1]+2, "full-name")) {
118                                 chomp_prefix = 0;
119                                 break;
120                         }
121                         if (!strncmp(argv[1]+2, "abbrev=",7)) {
122                                 abbrev = strtoul(argv[1]+9, NULL, 10);
123                                 if (abbrev && abbrev < MINIMUM_ABBREV)
124                                         abbrev = MINIMUM_ABBREV;
125                                 else if (abbrev > 40)
126                                         abbrev = 40;
127                                 break;
128                         }
129                         if (!strcmp(argv[1]+2, "abbrev")) {
130                                 abbrev = DEFAULT_ABBREV;
131                                 break;
132                         }
133                         /* otherwise fallthru */
134                 default:
135                         usage(ls_tree_usage);
136                 }
137                 argc--; argv++;
138         }
139         /* -d -r should imply -t, but -d by itself should not have to. */
140         if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
141             ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
142                 ls_options |= LS_SHOW_TREES;
143
144         if (argc < 2)
145                 usage(ls_tree_usage);
146         if (get_sha1(argv[1], sha1))
147                 die("Not a valid object name %s", argv[1]);
148
149         pathspec = get_pathspec(prefix, argv + 2);
150         tree = parse_tree_indirect(sha1);
151         if (!tree)
152                 die("not a tree object");
153         read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
154
155         return 0;
156 }