ls-tree: --name-only
[git] / 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
11 static int line_termination = '\n';
12 #define LS_RECURSIVE 1
13 #define LS_TREE_ONLY 2
14 #define LS_SHOW_TREES 4
15 #define LS_NAME_ONLY 8
16 static int ls_options = 0;
17 const char **pathspec;
18
19 static const char ls_tree_usage[] =
20         "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] <tree-ish> [path...]";
21
22 static int show_recursive(const char *base, int baselen, const char *pathname)
23 {
24         const char **s;
25
26         if (ls_options & LS_RECURSIVE)
27                 return 1;
28
29         s = pathspec;
30         if (!s)
31                 return 0;
32
33         for (;;) {
34                 const char *spec = *s++;
35                 int len, speclen;
36
37                 if (!spec)
38                         return 0;
39                 if (strncmp(base, spec, baselen))
40                         continue;
41                 len = strlen(pathname);
42                 spec += baselen;
43                 speclen = strlen(spec);
44                 if (speclen <= len)
45                         continue;
46                 if (memcmp(pathname, spec, len))
47                         continue;
48                 return 1;
49         }
50 }
51
52 static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
53 {
54         int retval = 0;
55         const char *type = "blob";
56
57         if (S_ISDIR(mode)) {
58                 if (show_recursive(base, baselen, pathname)) {
59                         retval = READ_TREE_RECURSIVE;
60                         if (!(ls_options & LS_SHOW_TREES))
61                                 return retval;
62                 }
63                 type = "tree";
64         }
65         else if (ls_options & LS_TREE_ONLY)
66                 return 0;
67
68         if (!(ls_options & LS_NAME_ONLY))
69                 printf("%06o %s %s\t", mode, type, sha1_to_hex(sha1));
70         write_name_quoted(base, baselen, pathname, line_termination, stdout);
71         putchar(line_termination);
72         return retval;
73 }
74
75 int main(int argc, const char **argv)
76 {
77         const char *prefix;
78         unsigned char sha1[20];
79         char *buf;
80         unsigned long size;
81
82         prefix = setup_git_directory();
83         while (1 < argc && argv[1][0] == '-') {
84                 switch (argv[1][1]) {
85                 case 'z':
86                         line_termination = 0;
87                         break;
88                 case 'r':
89                         ls_options |= LS_RECURSIVE;
90                         break;
91                 case 'd':
92                         ls_options |= LS_TREE_ONLY;
93                         break;
94                 case 't':
95                         ls_options |= LS_SHOW_TREES;
96                         break;
97                 case '-':
98                         if (!strcmp(argv[1]+2, "name-only") ||
99                             !strcmp(argv[1]+2, "name-status")) {
100                                 ls_options |= LS_NAME_ONLY;
101                                 break;
102                         }
103                         /* otherwise fallthru */
104                 default:
105                         usage(ls_tree_usage);
106                 }
107                 argc--; argv++;
108         }
109         /* -d -r should imply -t, but -d by itself should not have to. */
110         if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
111             ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
112                 ls_options |= LS_SHOW_TREES;
113
114         if (argc < 2)
115                 usage(ls_tree_usage);
116         if (get_sha1(argv[1], sha1) < 0)
117                 usage(ls_tree_usage);
118
119         pathspec = get_pathspec(prefix, argv + 2);
120         buf = read_object_with_reference(sha1, "tree", &size, NULL);
121         if (!buf)
122                 die("not a tree object");
123         read_tree_recursive(buf, size, "", 0, 0, pathspec, show_tree);
124
125         return 0;
126 }