Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
| 6 | #include "cache.h" |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 7 | #include "blob.h" |
| 8 | #include "tree.h" |
Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 9 | |
Linus Torvalds | e99d59f | 2005-05-20 18:46:10 | [diff] [blame] | 10 | static int line_termination = '\n'; |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 11 | #define LS_RECURSIVE 1 |
| 12 | #define LS_TREE_ONLY 2 |
| 13 | static int ls_options = 0; |
Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 14 | |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 15 | static struct tree_entry_list root_entry; |
Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 16 | |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 17 | static void prepare_root(unsigned char *sha1) |
Jason McMullan | 6d3a507 | 2005-05-26 17:52:50 | [diff] [blame] | 18 | { |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 19 | unsigned char rsha[20]; |
Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 20 | unsigned long size; |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 21 | void *buf; |
| 22 | struct tree *root_tree; |
Jason McMullan | 6d3a507 | 2005-05-26 17:52:50 | [diff] [blame] | 23 | |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 24 | buf = read_object_with_reference(sha1, "tree", &size, rsha); |
| 25 | free(buf); |
| 26 | if (!buf) |
| 27 | die("Could not read %s", sha1_to_hex(sha1)); |
Jason McMullan | 6d3a507 | 2005-05-26 17:52:50 | [diff] [blame] | 28 | |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 29 | root_tree = lookup_tree(rsha); |
| 30 | if (!root_tree) |
| 31 | die("Could not read %s", sha1_to_hex(sha1)); |
Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 32 | |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 33 | /* Prepare a fake entry */ |
| 34 | root_entry.directory = 1; |
| 35 | root_entry.executable = root_entry.symlink = 0; |
| 36 | root_entry.mode = S_IFDIR; |
| 37 | root_entry.name = ""; |
| 38 | root_entry.item.tree = root_tree; |
| 39 | root_entry.parent = NULL; |
| 40 | } |
| 41 | |
| 42 | static int prepare_children(struct tree_entry_list *elem) |
| 43 | { |
| 44 | if (!elem->directory) |
| 45 | return -1; |
| 46 | if (!elem->item.tree->object.parsed) { |
| 47 | struct tree_entry_list *e; |
| 48 | if (parse_tree(elem->item.tree)) |
| 49 | return -1; |
| 50 | /* Set up the parent link */ |
| 51 | for (e = elem->item.tree->entries; e; e = e->next) |
| 52 | e->parent = elem; |
| 53 | } |
Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 57 | static struct tree_entry_list *find_entry(const char *path, char *pathbuf) |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 58 | { |
Junio C Hamano | 6620498 | 2005-06-01 01:46:47 | [diff] [blame] | 59 | const char *next, *slash; |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 60 | int len; |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 61 | struct tree_entry_list *elem = &root_entry, *oldelem = NULL; |
| 62 | |
| 63 | *(pathbuf) = '\0'; |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 64 | |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 65 | /* Find tree element, descending from root, that |
| 66 | * corresponds to the named path, lazily expanding |
| 67 | * the tree if possible. |
| 68 | */ |
Junio C Hamano | 6620498 | 2005-06-01 01:46:47 | [diff] [blame] | 69 | |
| 70 | while (path) { |
| 71 | /* The fact we still have path means that the caller |
| 72 | * wants us to make sure that elem at this point is a |
| 73 | * directory, and possibly descend into it. Even what |
| 74 | * is left is just trailing slashes, we loop back to |
| 75 | * here, and this call to prepare_children() will |
| 76 | * catch elem not being a tree. Nice. |
| 77 | */ |
| 78 | if (prepare_children(elem)) |
| 79 | return NULL; |
| 80 | |
| 81 | slash = strchr(path, '/'); |
| 82 | if (!slash) { |
| 83 | len = strlen(path); |
Linus Torvalds | 6da4016 | 2005-07-03 17:10:45 | [diff] [blame] | 84 | next = NULL; |
Junio C Hamano | 6620498 | 2005-06-01 01:46:47 | [diff] [blame] | 85 | } |
| 86 | else { |
| 87 | next = slash + 1; |
| 88 | len = slash - path; |
| 89 | } |
| 90 | if (len) { |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 91 | if (oldelem) { |
| 92 | pathbuf += sprintf(pathbuf, "%s/", oldelem->name); |
| 93 | } |
| 94 | |
Junio C Hamano | 6620498 | 2005-06-01 01:46:47 | [diff] [blame] | 95 | /* (len == 0) if the original path was "drivers/char/" |
| 96 | * and we have run already two rounds, having elem |
| 97 | * pointing at the drivers/char directory. |
| 98 | */ |
| 99 | elem = elem->item.tree->entries; |
| 100 | while (elem) { |
| 101 | if ((strlen(elem->name) == len) && |
| 102 | !strncmp(elem->name, path, len)) { |
| 103 | /* found */ |
| 104 | break; |
| 105 | } |
| 106 | elem = elem->next; |
| 107 | } |
| 108 | if (!elem) |
| 109 | return NULL; |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 110 | |
| 111 | oldelem = elem; |
Junio C Hamano | 6620498 | 2005-06-01 01:46:47 | [diff] [blame] | 112 | } |
| 113 | path = next; |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 114 | } |
Junio C Hamano | 6620498 | 2005-06-01 01:46:47 | [diff] [blame] | 115 | |
| 116 | return elem; |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 117 | } |
| 118 | |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 119 | static const char *entry_type(struct tree_entry_list *e) |
| 120 | { |
| 121 | return (e->directory ? "tree" : "blob"); |
| 122 | } |
| 123 | |
| 124 | static const char *entry_hex(struct tree_entry_list *e) |
| 125 | { |
| 126 | return sha1_to_hex(e->directory |
| 127 | ? e->item.tree->object.sha1 |
| 128 | : e->item.blob->object.sha1); |
| 129 | } |
| 130 | |
| 131 | /* forward declaration for mutually recursive routines */ |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 132 | static int show_entry(struct tree_entry_list *, int, char *pathbuf); |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 133 | |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 134 | static int show_children(struct tree_entry_list *e, int level, char *pathbuf) |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 135 | { |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 136 | int oldlen = strlen(pathbuf); |
| 137 | |
| 138 | if (e != &root_entry) |
| 139 | sprintf(pathbuf + oldlen, "%s/", e->name); |
| 140 | |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 141 | if (prepare_children(e)) |
| 142 | die("internal error: ls-tree show_children called with non tree"); |
| 143 | e = e->item.tree->entries; |
| 144 | while (e) { |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 145 | show_entry(e, level, pathbuf); |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 146 | e = e->next; |
| 147 | } |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 148 | |
| 149 | pathbuf[oldlen] = '\0'; |
| 150 | |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 154 | static int show_entry(struct tree_entry_list *e, int level, char *pathbuf) |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 155 | { |
| 156 | int err = 0; |
| 157 | |
| 158 | if (e != &root_entry) { |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 159 | printf("%06o %s %s %s%s", e->mode, entry_type(e), |
| 160 | entry_hex(e), pathbuf, e->name); |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 161 | putchar(line_termination); |
| 162 | } |
| 163 | |
| 164 | if (e->directory) { |
| 165 | /* If this is a directory, we have the following cases: |
| 166 | * (1) This is the top-level request (explicit path from the |
| 167 | * command line, or "root" if there is no command line). |
| 168 | * a. Without any flag. We show direct children. We do not |
| 169 | * recurse into them. |
| 170 | * b. With -r. We do recurse into children. |
| 171 | * c. With -d. We do not recurse into children. |
| 172 | * (2) We came here because our caller is either (1-a) or |
| 173 | * (1-b). |
| 174 | * a. Without any flag. We do not show our children (which |
| 175 | * are grandchildren for the original request). |
| 176 | * b. With -r. We continue to recurse into our children. |
| 177 | * c. With -d. We should not have come here to begin with. |
| 178 | */ |
| 179 | if (level == 0 && !(ls_options & LS_TREE_ONLY)) |
| 180 | /* case (1)-a and (1)-b */ |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 181 | err = err | show_children(e, level+1, pathbuf); |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 182 | else if (level && ls_options & LS_RECURSIVE) |
| 183 | /* case (2)-b */ |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 184 | err = err | show_children(e, level+1, pathbuf); |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 185 | } |
| 186 | return err; |
| 187 | } |
| 188 | |
Junio C Hamano | 6620498 | 2005-06-01 01:46:47 | [diff] [blame] | 189 | static int list_one(const char *path) |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 190 | { |
| 191 | int err = 0; |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 192 | char pathbuf[MAXPATHLEN + 1]; |
| 193 | struct tree_entry_list *e = find_entry(path, pathbuf); |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 194 | if (!e) { |
| 195 | /* traditionally ls-tree does not complain about |
| 196 | * missing path. We may change this later to match |
| 197 | * what "/bin/ls -a" does, which is to complain. |
| 198 | */ |
| 199 | return err; |
| 200 | } |
robfitz@273k.net | ab1630a | 2005-10-07 23:54:06 | [diff] [blame] | 201 | err = err | show_entry(e, 0, pathbuf); |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 202 | return err; |
| 203 | } |
| 204 | |
| 205 | static int list(char **path) |
| 206 | { |
| 207 | int i; |
| 208 | int err = 0; |
Junio C Hamano | 6620498 | 2005-06-01 01:46:47 | [diff] [blame] | 209 | for (i = 0; path[i]; i++) |
| 210 | err = err | list_one(path[i]); |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 211 | return err; |
| 212 | } |
| 213 | |
Petr Baudis | 4d1f119 | 2005-07-29 09:01:26 | [diff] [blame] | 214 | static const char ls_tree_usage[] = |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 215 | "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]"; |
Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 216 | |
Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 217 | int main(int argc, char **argv) |
| 218 | { |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 219 | static char *path0[] = { "", NULL }; |
| 220 | char **path; |
Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 221 | unsigned char sha1[20]; |
| 222 | |
Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 223 | while (1 < argc && argv[1][0] == '-') { |
| 224 | switch (argv[1][1]) { |
| 225 | case 'z': |
| 226 | line_termination = 0; |
| 227 | break; |
| 228 | case 'r': |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 229 | ls_options |= LS_RECURSIVE; |
| 230 | break; |
| 231 | case 'd': |
| 232 | ls_options |= LS_TREE_ONLY; |
Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 233 | break; |
| 234 | default: |
Junio C Hamano | 0f2303f | 2005-04-16 20:57:39 | [diff] [blame] | 235 | usage(ls_tree_usage); |
Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 236 | } |
| 237 | argc--; argv++; |
| 238 | } |
| 239 | |
Jason McMullan | 6d3a507 | 2005-05-26 17:52:50 | [diff] [blame] | 240 | if (argc < 2) |
Junio C Hamano | 0f2303f | 2005-04-16 20:57:39 | [diff] [blame] | 241 | usage(ls_tree_usage); |
Linus Torvalds | 3c249c9 | 2005-05-01 23:36:56 | [diff] [blame] | 242 | if (get_sha1(argv[1], sha1) < 0) |
Junio C Hamano | 0f2303f | 2005-04-16 20:57:39 | [diff] [blame] | 243 | usage(ls_tree_usage); |
Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 244 | |
| 245 | path = (argc == 2) ? path0 : (argv + 2); |
| 246 | prepare_root(sha1); |
| 247 | if (list(path) < 0) |
Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 248 | die("list failed"); |
Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 249 | return 0; |
| 250 | } |