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 #define LS_NAME_ONLY 8
16 static int ls_options = 0;
17 const char **pathspec;
19 static const char ls_tree_usage[] =
20 "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] <tree-ish> [path...]";
22 static int show_recursive(const char *base, int baselen, const char *pathname)
26 if (ls_options & LS_RECURSIVE)
34 const char *spec = *s++;
39 if (strncmp(base, spec, baselen))
41 len = strlen(pathname);
43 speclen = strlen(spec);
46 if (memcmp(pathname, spec, len))
52 static int show_tree(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
55 const char *type = "blob";
58 if (show_recursive(base, baselen, pathname)) {
59 retval = READ_TREE_RECURSIVE;
60 if (!(ls_options & LS_SHOW_TREES))
65 else if (ls_options & LS_TREE_ONLY)
68 if (!(ls_options & LS_NAME_ONLY))
69 printf("%06o %s %s\t", mode, type, sha1_to_hex(sha1));
70 write_name_quoted(base, baselen, pathname, line_termination, stdout);
71 putchar(line_termination);
75 int main(int argc, const char **argv)
78 unsigned char sha1[20];
82 prefix = setup_git_directory();
83 while (1 < argc && argv[1][0] == '-') {
89 ls_options |= LS_RECURSIVE;
92 ls_options |= LS_TREE_ONLY;
95 ls_options |= LS_SHOW_TREES;
98 if (!strcmp(argv[1]+2, "name-only") ||
99 !strcmp(argv[1]+2, "name-status")) {
100 ls_options |= LS_NAME_ONLY;
103 /* otherwise fallthru */
105 usage(ls_tree_usage);
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;
115 usage(ls_tree_usage);
116 if (get_sha1(argv[1], sha1) < 0)
117 usage(ls_tree_usage);
119 pathspec = get_pathspec(prefix, argv + 2);
120 buf = read_object_with_reference(sha1, "tree", &size, NULL);
122 die("not a tree object");
123 read_tree_recursive(buf, size, "", 0, 0, pathspec, show_tree);