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
18 #define LS_SHOW_SIZE 16
20 static int ls_options;
21 static const char **pathspec;
22 static int chomp_prefix;
23 static const char *ls_tree_prefix;
25 static const char ls_tree_usage[] =
26 "git ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--full-tree] [--abbrev[=<n>]] <tree-ish> [path...]";
28 static int show_recursive(const char *base, int baselen, const char *pathname)
32 if (ls_options & LS_RECURSIVE)
40 const char *spec = *s++;
45 if (strncmp(base, spec, baselen))
47 len = strlen(pathname);
49 speclen = strlen(spec);
52 if (memcmp(pathname, spec, len))
58 static int show_tree(const unsigned char *sha1, const char *base, int baselen,
59 const char *pathname, unsigned mode, int stage, void *context)
62 const char *type = blob_type;
64 if (S_ISGITLINK(mode)) {
66 * Maybe we want to have some recursive version here?
68 * Something similar to this incomplete example:
70 if (show_subprojects(base, baselen, pathname))
71 retval = READ_TREE_RECURSIVE;
75 } else if (S_ISDIR(mode)) {
76 if (show_recursive(base, baselen, pathname)) {
77 retval = READ_TREE_RECURSIVE;
78 if (!(ls_options & LS_SHOW_TREES))
83 else if (ls_options & LS_TREE_ONLY)
87 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
90 if (!(ls_options & LS_NAME_ONLY)) {
91 if (ls_options & LS_SHOW_SIZE) {
93 if (!strcmp(type, blob_type)) {
95 if (sha1_object_info(sha1, &size) == OBJ_BAD)
96 strcpy(size_text, "BAD");
98 snprintf(size_text, sizeof(size_text),
101 strcpy(size_text, "-");
102 printf("%06o %s %s %7s\t", mode, type,
103 abbrev ? find_unique_abbrev(sha1, abbrev)
107 printf("%06o %s %s\t", mode, type,
108 abbrev ? find_unique_abbrev(sha1, abbrev)
109 : sha1_to_hex(sha1));
111 write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
112 pathname, stdout, line_termination);
116 int cmd_ls_tree(int argc, const char **argv, const char *prefix)
118 unsigned char sha1[20];
121 git_config(git_default_config, NULL);
122 ls_tree_prefix = prefix;
123 if (prefix && *prefix)
124 chomp_prefix = strlen(prefix);
125 while (1 < argc && argv[1][0] == '-') {
126 switch (argv[1][1]) {
128 line_termination = 0;
131 ls_options |= LS_RECURSIVE;
134 ls_options |= LS_TREE_ONLY;
137 ls_options |= LS_SHOW_TREES;
140 ls_options |= LS_SHOW_SIZE;
143 if (!strcmp(argv[1]+2, "name-only") ||
144 !strcmp(argv[1]+2, "name-status")) {
145 ls_options |= LS_NAME_ONLY;
148 if (!strcmp(argv[1]+2, "long")) {
149 ls_options |= LS_SHOW_SIZE;
152 if (!strcmp(argv[1]+2, "full-name")) {
156 if (!strcmp(argv[1]+2, "full-tree")) {
157 ls_tree_prefix = prefix = NULL;
161 if (!prefixcmp(argv[1]+2, "abbrev=")) {
162 abbrev = strtoul(argv[1]+9, NULL, 10);
163 if (abbrev && abbrev < MINIMUM_ABBREV)
164 abbrev = MINIMUM_ABBREV;
165 else if (abbrev > 40)
169 if (!strcmp(argv[1]+2, "abbrev")) {
170 abbrev = DEFAULT_ABBREV;
173 /* otherwise fallthru */
175 usage(ls_tree_usage);
179 /* -d -r should imply -t, but -d by itself should not have to. */
180 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
181 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
182 ls_options |= LS_SHOW_TREES;
185 usage(ls_tree_usage);
186 if (get_sha1(argv[1], sha1))
187 die("Not a valid object name %s", argv[1]);
189 pathspec = get_pathspec(prefix, argv + 2);
190 tree = parse_tree_indirect(sha1);
192 die("not a tree object");
193 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree, NULL);