2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
13 static int line_termination = '\n';
14 #define LS_RECURSIVE 1
15 #define LS_TREE_ONLY 2
16 #define LS_SHOW_TREES 4
17 #define LS_NAME_ONLY 8
19 static int ls_options;
20 static const char **pathspec;
21 static int chomp_prefix;
22 static const char *ls_tree_prefix;
24 static const char ls_tree_usage[] =
25 "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
27 static int show_recursive(const char *base, int baselen, const char *pathname)
31 if (ls_options & LS_RECURSIVE)
39 const char *spec = *s++;
44 if (strncmp(base, spec, baselen))
46 len = strlen(pathname);
48 speclen = strlen(spec);
51 if (memcmp(pathname, spec, len))
57 static int show_tree(const unsigned char *sha1, const char *base, int baselen,
58 const char *pathname, unsigned mode, int stage)
61 const char *type = blob_type;
63 if (S_ISDIRLNK(mode)) {
65 * Maybe we want to have some recursive version here?
69 if (show_subprojects(base, baselen, pathname)) {
80 } else if (S_ISDIR(mode)) {
81 if (show_recursive(base, baselen, pathname)) {
82 retval = READ_TREE_RECURSIVE;
83 if (!(ls_options & LS_SHOW_TREES))
88 else if (ls_options & LS_TREE_ONLY)
92 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
95 if (!(ls_options & LS_NAME_ONLY))
96 printf("%06o %s %s\t", mode, type,
97 abbrev ? find_unique_abbrev(sha1,abbrev)
99 write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
101 line_termination, stdout);
102 putchar(line_termination);
106 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
108 unsigned char sha1[20];
111 git_config(git_default_config);
112 ls_tree_prefix = prefix;
113 if (prefix && *prefix)
114 chomp_prefix = strlen(prefix);
115 while (1 < argc && argv[1][0] == '-') {
116 switch (argv[1][1]) {
118 line_termination = 0;
121 ls_options |= LS_RECURSIVE;
124 ls_options |= LS_TREE_ONLY;
127 ls_options |= LS_SHOW_TREES;
130 if (!strcmp(argv[1]+2, "name-only") ||
131 !strcmp(argv[1]+2, "name-status")) {
132 ls_options |= LS_NAME_ONLY;
135 if (!strcmp(argv[1]+2, "full-name")) {
139 if (!prefixcmp(argv[1]+2, "abbrev=")) {
140 abbrev = strtoul(argv[1]+9, NULL, 10);
141 if (abbrev && abbrev < MINIMUM_ABBREV)
142 abbrev = MINIMUM_ABBREV;
143 else if (abbrev > 40)
147 if (!strcmp(argv[1]+2, "abbrev")) {
148 abbrev = DEFAULT_ABBREV;
151 /* otherwise fallthru */
153 usage(ls_tree_usage);
157 /* -d -r should imply -t, but -d by itself should not have to. */
158 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
159 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
160 ls_options |= LS_SHOW_TREES;
163 usage(ls_tree_usage);
164 if (get_sha1(argv[1], sha1))
165 die("Not a valid object name %s", argv[1]);
167 pathspec = get_pathspec(prefix, argv + 2);
168 tree = parse_tree_indirect(sha1);
170 die("not a tree object");
171 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);