2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 static int line_termination = '\n';
12 #define LS_RECURSIVE 1
13 #define LS_TREE_ONLY 2
14 #define LS_SHOW_TREES 4
15 static int ls_options = 0;
16 const char **pathspec;
18 static const char ls_tree_usage[] =
19 "git-ls-tree [-d] [-r] [-t] [-z] <tree-ish> [path...]";
21 static int show_recursive(const char *base, int baselen, const char *pathname)
25 if (ls_options & LS_RECURSIVE)
33 const char *spec = *s++;
38 if (strncmp(base, spec, baselen))
40 len = strlen(pathname);
42 speclen = strlen(spec);
45 if (memcmp(pathname, spec, len))
51 static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
54 const char *type = "blob";
57 if (show_recursive(base, baselen, pathname)) {
58 retval = READ_TREE_RECURSIVE;
59 if (!(ls_options & LS_SHOW_TREES))
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);
71 int main(int argc, const char **argv)
74 unsigned char sha1[20];
78 prefix = setup_git_directory();
79 while (1 < argc && argv[1][0] == '-') {
85 ls_options |= LS_RECURSIVE;
88 ls_options |= LS_TREE_ONLY;
91 ls_options |= LS_SHOW_TREES;
100 usage(ls_tree_usage);
101 if (get_sha1(argv[1], sha1) < 0)
102 usage(ls_tree_usage);
104 pathspec = get_pathspec(prefix, argv + 2);
105 buf = read_object_with_reference(sha1, "tree", &size, NULL);
107 die("not a tree object");
108 read_tree_recursive(buf, size, "", 0, 0, pathspec, show_tree);