]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-ls-tree.c
Fix an "implicit function definition" warning.
[thirdparty/git.git] / builtin-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"
aae01bda 10#include "builtin.h"
7912c070 11
e99d59ff 12static int line_termination = '\n';
6af1f019
JH
13#define LS_RECURSIVE 1
14#define LS_TREE_ONLY 2
0f8f45cb 15#define LS_SHOW_TREES 4
c639a554 16#define LS_NAME_ONLY 8
96f1e58f
DR
17static int abbrev;
18static int ls_options;
aae01bda 19static const char **pathspec;
96f1e58f 20static int chomp_prefix;
a633fca0 21static const char *ls_tree_prefix;
aa1c48df 22
3c5e8468 23static const char ls_tree_usage[] =
cb85bfe5 24 "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
0f8f45cb
LT
25
26static 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}
aa1c48df 55
097dc3d8 56static int show_tree(const unsigned char *sha1, const char *base, int baselen,
a69dd585 57 const char *pathname, unsigned mode, int stage)
6af1f019 58{
0f8f45cb 59 int retval = 0;
8e440259 60 const char *type = blob_type;
ab1630a3 61
3c5e8468 62 if (S_ISDIR(mode)) {
0f8f45cb
LT
63 if (show_recursive(base, baselen, pathname)) {
64 retval = READ_TREE_RECURSIVE;
65 if (!(ls_options & LS_SHOW_TREES))
66 return retval;
e2466376 67 }
8e440259 68 type = tree_type;
6af1f019 69 }
f5984671
JH
70 else if (ls_options & LS_TREE_ONLY)
71 return 0;
ab1630a3 72
a69dd585 73 if (chomp_prefix &&
a633fca0 74 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
a69dd585
JH
75 return 0;
76
c639a554 77 if (!(ls_options & LS_NAME_ONLY))
cb85bfe5
EW
78 printf("%06o %s %s\t", mode, type,
79 abbrev ? find_unique_abbrev(sha1,abbrev)
80 : sha1_to_hex(sha1));
a69dd585
JH
81 write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
82 pathname,
83 line_termination, stdout);
32b5904b 84 putchar(line_termination);
0f8f45cb 85 return retval;
6af1f019 86}
0f2303f7 87
a633fca0 88int cmd_ls_tree(int argc, const char **argv, const char *prefix)
6af1f019 89{
7912c070 90 unsigned char sha1[20];
521698b1 91 struct tree *tree;
7912c070 92
84a9b58c 93 git_config(git_default_config);
a633fca0 94 ls_tree_prefix = prefix;
a69dd585
JH
95 if (prefix && *prefix)
96 chomp_prefix = strlen(prefix);
aa1c48df
JH
97 while (1 < argc && argv[1][0] == '-') {
98 switch (argv[1][1]) {
99 case 'z':
100 line_termination = 0;
101 break;
102 case 'r':
6af1f019
JH
103 ls_options |= LS_RECURSIVE;
104 break;
105 case 'd':
106 ls_options |= LS_TREE_ONLY;
aa1c48df 107 break;
0f8f45cb
LT
108 case 't':
109 ls_options |= LS_SHOW_TREES;
110 break;
c639a554
JH
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 }
a69dd585
JH
117 if (!strcmp(argv[1]+2, "full-name")) {
118 chomp_prefix = 0;
119 break;
120 }
cb85bfe5
EW
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 }
c639a554 133 /* otherwise fallthru */
aa1c48df 134 default:
0f2303f7 135 usage(ls_tree_usage);
aa1c48df
JH
136 }
137 argc--; argv++;
138 }
f5984671
JH
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;
aa1c48df 143
6d3a5077 144 if (argc < 2)
0f2303f7 145 usage(ls_tree_usage);
31fff305
DL
146 if (get_sha1(argv[1], sha1))
147 die("Not a valid object name %s", argv[1]);
6af1f019 148
e2466376 149 pathspec = get_pathspec(prefix, argv + 2);
521698b1
DB
150 tree = parse_tree_indirect(sha1);
151 if (!tree)
3c5e8468 152 die("not a tree object");
521698b1 153 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
3c5e8468 154
7912c070
PB
155 return 0;
156}