check_packed_git_idx(): check integrity of the idx file itself.
[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
e2466376
LT
16static int ls_options = 0;
17const char **pathspec;
aa1c48df 18
3c5e8468 19static const char ls_tree_usage[] =
c639a554 20 "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] <tree-ish> [path...]";
0f8f45cb
LT
21
22static 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}
aa1c48df 51
3c5e8468 52static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
6af1f019 53{
0f8f45cb 54 int retval = 0;
3c5e8468 55 const char *type = "blob";
ab1630a3 56
3c5e8468 57 if (S_ISDIR(mode)) {
0f8f45cb
LT
58 if (show_recursive(base, baselen, pathname)) {
59 retval = READ_TREE_RECURSIVE;
60 if (!(ls_options & LS_SHOW_TREES))
61 return retval;
e2466376 62 }
b45c569b 63 type = "tree";
6af1f019 64 }
f5984671
JH
65 else if (ls_options & LS_TREE_ONLY)
66 return 0;
ab1630a3 67
c639a554
JH
68 if (!(ls_options & LS_NAME_ONLY))
69 printf("%06o %s %s\t", mode, type, sha1_to_hex(sha1));
32b5904b
JH
70 write_name_quoted(base, baselen, pathname, line_termination, stdout);
71 putchar(line_termination);
0f8f45cb 72 return retval;
6af1f019 73}
0f2303f7 74
3c5e8468 75int main(int argc, const char **argv)
6af1f019 76{
e2466376 77 const char *prefix;
7912c070 78 unsigned char sha1[20];
3c5e8468
LT
79 char *buf;
80 unsigned long size;
7912c070 81
3c5e8468 82 prefix = setup_git_directory();
aa1c48df
JH
83 while (1 < argc && argv[1][0] == '-') {
84 switch (argv[1][1]) {
85 case 'z':
86 line_termination = 0;
87 break;
88 case 'r':
6af1f019
JH
89 ls_options |= LS_RECURSIVE;
90 break;
91 case 'd':
92 ls_options |= LS_TREE_ONLY;
aa1c48df 93 break;
0f8f45cb
LT
94 case 't':
95 ls_options |= LS_SHOW_TREES;
96 break;
c639a554
JH
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 */
aa1c48df 104 default:
0f2303f7 105 usage(ls_tree_usage);
aa1c48df
JH
106 }
107 argc--; argv++;
108 }
f5984671
JH
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;
aa1c48df 113
6d3a5077 114 if (argc < 2)
0f2303f7 115 usage(ls_tree_usage);
3c249c95 116 if (get_sha1(argv[1], sha1) < 0)
0f2303f7 117 usage(ls_tree_usage);
6af1f019 118
e2466376 119 pathspec = get_pathspec(prefix, argv + 2);
3c5e8468
LT
120 buf = read_object_with_reference(sha1, "tree", &size, NULL);
121 if (!buf)
122 die("not a tree object");
e2466376 123 read_tree_recursive(buf, size, "", 0, 0, pathspec, show_tree);
3c5e8468 124
7912c070
PB
125 return 0;
126}