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