Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 2 | #include "tree.h" |
| 3 | #include "blob.h" |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 4 | #include "commit.h" |
| 5 | #include "tag.h" |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 6 | #include "tree-walk.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 7 | |
| 8 | const char *tree_type = "tree"; |
| 9 | |
Linus Torvalds | 3a7c352 | 2006-05-29 12:16:46 -0700 | [diff] [blame] | 10 | static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 11 | { |
Linus Torvalds | 3c5e846 | 2005-11-26 09:38:20 -0800 | [diff] [blame] | 12 | int len; |
| 13 | unsigned int size; |
| 14 | struct cache_entry *ce; |
| 15 | |
| 16 | if (S_ISDIR(mode)) |
| 17 | return READ_TREE_RECURSIVE; |
| 18 | |
| 19 | len = strlen(pathname); |
| 20 | size = cache_entry_size(baselen + len); |
Peter Eriksen | 90321c1 | 2006-04-03 19:30:46 +0100 | [diff] [blame] | 21 | ce = xcalloc(1, size); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 22 | |
| 23 | ce->ce_mode = create_ce_mode(mode); |
| 24 | ce->ce_flags = create_ce_flags(baselen + len, stage); |
| 25 | memcpy(ce->name, base, baselen); |
| 26 | memcpy(ce->name + baselen, pathname, len+1); |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 27 | hashcpy(ce->sha1, sha1); |
Junio C Hamano | b155725 | 2005-06-25 02:25:29 -0700 | [diff] [blame] | 28 | return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 31 | static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths) |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 32 | { |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 33 | const char *match; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 34 | int pathlen; |
| 35 | |
| 36 | if (!paths) |
| 37 | return 1; |
| 38 | pathlen = strlen(path); |
| 39 | while ((match = *paths++) != NULL) { |
| 40 | int matchlen = strlen(match); |
| 41 | |
| 42 | if (baselen >= matchlen) { |
| 43 | /* If it doesn't match, move along... */ |
| 44 | if (strncmp(base, match, matchlen)) |
| 45 | continue; |
| 46 | /* The base is a subdirectory of a path which was specified. */ |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | /* Does the base match? */ |
| 51 | if (strncmp(base, match, baselen)) |
| 52 | continue; |
| 53 | |
| 54 | match += baselen; |
| 55 | matchlen -= baselen; |
| 56 | |
| 57 | if (pathlen > matchlen) |
| 58 | continue; |
| 59 | |
| 60 | if (matchlen > pathlen) { |
| 61 | if (match[pathlen] != '/') |
| 62 | continue; |
| 63 | if (!S_ISDIR(mode)) |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | if (strncmp(path, match, pathlen)) |
| 68 | continue; |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 69 | |
| 70 | return 1; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
Daniel Barkalow | 521698b | 2006-01-26 01:13:36 -0500 | [diff] [blame] | 75 | int read_tree_recursive(struct tree *tree, |
Linus Torvalds | 3c5e846 | 2005-11-26 09:38:20 -0800 | [diff] [blame] | 76 | const char *base, int baselen, |
| 77 | int stage, const char **match, |
| 78 | read_tree_fn_t fn) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 79 | { |
Linus Torvalds | 0790a42 | 2006-05-29 12:17:28 -0700 | [diff] [blame] | 80 | struct tree_desc desc; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 81 | struct name_entry entry; |
Linus Torvalds | 0790a42 | 2006-05-29 12:17:28 -0700 | [diff] [blame] | 82 | |
Daniel Barkalow | 521698b | 2006-01-26 01:13:36 -0500 | [diff] [blame] | 83 | if (parse_tree(tree)) |
| 84 | return -1; |
Linus Torvalds | 0790a42 | 2006-05-29 12:17:28 -0700 | [diff] [blame] | 85 | |
| 86 | desc.buf = tree->buffer; |
| 87 | desc.size = tree->size; |
| 88 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 89 | while (tree_entry(&desc, &entry)) { |
| 90 | if (!match_tree_entry(base, baselen, entry.path, entry.mode, match)) |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 91 | continue; |
| 92 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 93 | switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) { |
Linus Torvalds | 3c5e846 | 2005-11-26 09:38:20 -0800 | [diff] [blame] | 94 | case 0: |
| 95 | continue; |
| 96 | case READ_TREE_RECURSIVE: |
| 97 | break;; |
| 98 | default: |
| 99 | return -1; |
| 100 | } |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 101 | if (S_ISDIR(entry.mode)) { |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 102 | int retval; |
Jonas Fonseca | 1c9da46 | 2005-04-27 00:00:01 +0200 | [diff] [blame] | 103 | char *newbase; |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 104 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 105 | newbase = xmalloc(baselen + 1 + entry.pathlen); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 106 | memcpy(newbase, base, baselen); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 107 | memcpy(newbase + baselen, entry.path, entry.pathlen); |
| 108 | newbase[baselen + entry.pathlen] = '/'; |
| 109 | retval = read_tree_recursive(lookup_tree(entry.sha1), |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 110 | newbase, |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 111 | baselen + entry.pathlen + 1, |
Linus Torvalds | 3c5e846 | 2005-11-26 09:38:20 -0800 | [diff] [blame] | 112 | stage, match, fn); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 113 | free(newbase); |
| 114 | if (retval) |
| 115 | return -1; |
| 116 | continue; |
| 117 | } |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
Daniel Barkalow | 521698b | 2006-01-26 01:13:36 -0500 | [diff] [blame] | 122 | int read_tree(struct tree *tree, int stage, const char **match) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 123 | { |
Daniel Barkalow | 521698b | 2006-01-26 01:13:36 -0500 | [diff] [blame] | 124 | return read_tree_recursive(tree, "", 0, stage, match, read_one_entry); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 127 | struct tree *lookup_tree(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 128 | { |
| 129 | struct object *obj = lookup_object(sha1); |
| 130 | if (!obj) { |
Linus Torvalds | 855419f | 2006-06-19 10:44:15 -0700 | [diff] [blame] | 131 | struct tree *ret = alloc_tree_node(); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 132 | created_object(sha1, &ret->object); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 133 | ret->object.type = OBJ_TREE; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 134 | return ret; |
| 135 | } |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 136 | if (!obj->type) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 137 | obj->type = OBJ_TREE; |
| 138 | if (obj->type != OBJ_TREE) { |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 139 | error("Object %s is a %s, not a tree", |
| 140 | sha1_to_hex(sha1), typename(obj->type)); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 141 | return NULL; |
| 142 | } |
| 143 | return (struct tree *) obj; |
| 144 | } |
| 145 | |
David Rientjes | 74b504f | 2006-08-14 13:40:06 -0700 | [diff] [blame] | 146 | static void track_tree_refs(struct tree *item) |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 147 | { |
| 148 | int n_refs = 0, i; |
| 149 | struct object_refs *refs; |
| 150 | struct tree_desc desc; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 151 | struct name_entry entry; |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 152 | |
| 153 | /* Count how many entries there are.. */ |
| 154 | desc.buf = item->buffer; |
| 155 | desc.size = item->size; |
| 156 | while (desc.size) { |
| 157 | n_refs++; |
| 158 | update_tree_entry(&desc); |
| 159 | } |
| 160 | |
| 161 | /* Allocate object refs and walk it again.. */ |
| 162 | i = 0; |
| 163 | refs = alloc_object_refs(n_refs); |
| 164 | desc.buf = item->buffer; |
| 165 | desc.size = item->size; |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 166 | while (tree_entry(&desc, &entry)) { |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 167 | struct object *obj; |
| 168 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 169 | if (S_ISDIR(entry.mode)) |
| 170 | obj = &lookup_tree(entry.sha1)->object; |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 171 | else |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 172 | obj = &lookup_blob(entry.sha1)->object; |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 173 | refs->ref[i++] = obj; |
| 174 | } |
| 175 | set_object_refs(&item->object, refs); |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 178 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 179 | { |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 180 | if (item->object.parsed) |
| 181 | return 0; |
| 182 | item->object.parsed = 1; |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 183 | item->buffer = buffer; |
| 184 | item->size = size; |
| 185 | |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 186 | if (track_object_refs) |
| 187 | track_tree_refs(item); |
| 188 | return 0; |
| 189 | } |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 190 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 191 | int parse_tree(struct tree *item) |
| 192 | { |
| 193 | char type[20]; |
| 194 | void *buffer; |
| 195 | unsigned long size; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 196 | |
| 197 | if (item->object.parsed) |
| 198 | return 0; |
| 199 | buffer = read_sha1_file(item->object.sha1, type, &size); |
| 200 | if (!buffer) |
| 201 | return error("Could not read %s", |
| 202 | sha1_to_hex(item->object.sha1)); |
| 203 | if (strcmp(type, tree_type)) { |
| 204 | free(buffer); |
| 205 | return error("Object %s not a tree", |
| 206 | sha1_to_hex(item->object.sha1)); |
| 207 | } |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 208 | return parse_tree_buffer(item, buffer, size); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 209 | } |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 210 | |
| 211 | struct tree *parse_tree_indirect(const unsigned char *sha1) |
| 212 | { |
| 213 | struct object *obj = parse_object(sha1); |
| 214 | do { |
| 215 | if (!obj) |
| 216 | return NULL; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 217 | if (obj->type == OBJ_TREE) |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 218 | return (struct tree *) obj; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 219 | else if (obj->type == OBJ_COMMIT) |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 220 | obj = &(((struct commit *) obj)->tree->object); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 221 | else if (obj->type == OBJ_TAG) |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 222 | obj = ((struct tag *) obj)->tagged; |
| 223 | else |
| 224 | return NULL; |
| 225 | if (!obj->parsed) |
| 226 | parse_object(obj->sha1); |
| 227 | } while (1); |
| 228 | } |