2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 static int line_termination = '\n';
13 #define LS_RECURSIVE 1
14 #define LS_TREE_ONLY 2
15 #define LS_SHOW_TREES 4
16 #define LS_NAME_ONLY 8
18 static int ls_options;
19 static const char **pathspec;
20 static int chomp_prefix;
21 static const char *ls_tree_prefix;
23 static const char ls_tree_usage[] =
24 "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
26 static int show_recursive(const char *base, int baselen, const char *pathname)
30 if (ls_options & LS_RECURSIVE)
38 const char *spec = *s++;
43 if (strncmp(base, spec, baselen))
45 len = strlen(pathname);
47 speclen = strlen(spec);
50 if (memcmp(pathname, spec, len))
56 static int show_tree(const unsigned char *sha1, const char *base, int baselen,
57 const char *pathname, unsigned mode, int stage)
60 const char *type = blob_type;
63 if (show_recursive(base, baselen, pathname)) {
64 retval = READ_TREE_RECURSIVE;
65 if (!(ls_options & LS_SHOW_TREES))
70 else if (ls_options & LS_TREE_ONLY)
74 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
77 if (!(ls_options & LS_NAME_ONLY))
78 printf("%06o %s %s\t", mode, type,
79 abbrev ? find_unique_abbrev(sha1,abbrev)
81 write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
83 line_termination, stdout);
84 putchar(line_termination);
88 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
90 unsigned char sha1[20];
93 git_config(git_default_config);
94 ls_tree_prefix = prefix;
95 if (prefix && *prefix)
96 chomp_prefix = strlen(prefix);
97 while (1 < argc && argv[1][0] == '-') {
100 line_termination = 0;
103 ls_options |= LS_RECURSIVE;
106 ls_options |= LS_TREE_ONLY;
109 ls_options |= LS_SHOW_TREES;
112 if (!strcmp(argv[1]+2, "name-only") ||
113 !strcmp(argv[1]+2, "name-status")) {
114 ls_options |= LS_NAME_ONLY;
117 if (!strcmp(argv[1]+2, "full-name")) {
121 if (!prefixcmp(argv[1]+2, "abbrev=")) {
122 abbrev = strtoul(argv[1]+9, NULL, 10);
123 if (abbrev && abbrev < MINIMUM_ABBREV)
124 abbrev = MINIMUM_ABBREV;
125 else if (abbrev > 40)
129 if (!strcmp(argv[1]+2, "abbrev")) {
130 abbrev = DEFAULT_ABBREV;
133 /* otherwise fallthru */
135 usage(ls_tree_usage);
139 /* -d -r should imply -t, but -d by itself should not have to. */
140 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
141 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
142 ls_options |= LS_SHOW_TREES;
145 usage(ls_tree_usage);
146 if (get_sha1(argv[1], sha1))
147 die("Not a valid object name %s", argv[1]);
149 pathspec = get_pathspec(prefix, argv + 2);
150 tree = parse_tree_indirect(sha1);
152 die("not a tree object");
153 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);