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