git-ls-tree: add "-t" option to always show the tree entries
[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
e2466376
LT
15static int ls_options = 0;
16const char **pathspec;
aa1c48df 17
3c5e8468 18static const char ls_tree_usage[] =
0f8f45cb
LT
19 "git-ls-tree [-d] [-r] [-t] [-z] <tree-ish> [path...]";
20
21static int show_recursive(const char *base, int baselen, const char *pathname)
22{
23 const char **s;
24
25 if (ls_options & LS_RECURSIVE)
26 return 1;
27
28 s = pathspec;
29 if (!s)
30 return 0;
31
32 for (;;) {
33 const char *spec = *s++;
34 int len, speclen;
35
36 if (!spec)
37 return 0;
38 if (strncmp(base, spec, baselen))
39 continue;
40 len = strlen(pathname);
41 spec += baselen;
42 speclen = strlen(spec);
43 if (speclen <= len)
44 continue;
45 if (memcmp(pathname, spec, len))
46 continue;
47 return 1;
48 }
49}
aa1c48df 50
3c5e8468 51static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
6af1f019 52{
0f8f45cb 53 int retval = 0;
3c5e8468 54 const char *type = "blob";
ab1630a3 55
3c5e8468 56 if (S_ISDIR(mode)) {
0f8f45cb
LT
57 if (show_recursive(base, baselen, pathname)) {
58 retval = READ_TREE_RECURSIVE;
59 if (!(ls_options & LS_SHOW_TREES))
60 return retval;
e2466376 61 }
b45c569b 62 type = "tree";
6af1f019 63 }
ab1630a3 64
32b5904b
JH
65 printf("%06o %s %s\t", mode, type, sha1_to_hex(sha1));
66 write_name_quoted(base, baselen, pathname, line_termination, stdout);
67 putchar(line_termination);
0f8f45cb 68 return retval;
6af1f019 69}
0f2303f7 70
3c5e8468 71int main(int argc, const char **argv)
6af1f019 72{
e2466376 73 const char *prefix;
7912c070 74 unsigned char sha1[20];
3c5e8468
LT
75 char *buf;
76 unsigned long size;
7912c070 77
3c5e8468 78 prefix = setup_git_directory();
aa1c48df
JH
79 while (1 < argc && argv[1][0] == '-') {
80 switch (argv[1][1]) {
81 case 'z':
82 line_termination = 0;
83 break;
84 case 'r':
6af1f019
JH
85 ls_options |= LS_RECURSIVE;
86 break;
87 case 'd':
88 ls_options |= LS_TREE_ONLY;
aa1c48df 89 break;
0f8f45cb
LT
90 case 't':
91 ls_options |= LS_SHOW_TREES;
92 break;
aa1c48df 93 default:
0f2303f7 94 usage(ls_tree_usage);
aa1c48df
JH
95 }
96 argc--; argv++;
97 }
98
6d3a5077 99 if (argc < 2)
0f2303f7 100 usage(ls_tree_usage);
3c249c95 101 if (get_sha1(argv[1], sha1) < 0)
0f2303f7 102 usage(ls_tree_usage);
6af1f019 103
e2466376 104 pathspec = get_pathspec(prefix, argv + 2);
3c5e8468
LT
105 buf = read_object_with_reference(sha1, "tree", &size, NULL);
106 if (!buf)
107 die("not a tree object");
e2466376 108 read_tree_recursive(buf, size, "", 0, 0, pathspec, show_tree);
3c5e8468 109
7912c070
PB
110 return 0;
111}