Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 1 | #include "tree.h" |
| 2 | #include "blob.h" |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 3 | #include "commit.h" |
| 4 | #include "tag.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 5 | #include "cache.h" |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | const char *tree_type = "tree"; |
| 9 | |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 10 | static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) |
| 11 | { |
| 12 | int len = strlen(pathname); |
| 13 | unsigned int size = cache_entry_size(baselen + len); |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 14 | struct cache_entry *ce = xmalloc(size); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 15 | |
| 16 | memset(ce, 0, size); |
| 17 | |
| 18 | ce->ce_mode = create_ce_mode(mode); |
| 19 | ce->ce_flags = create_ce_flags(baselen + len, stage); |
| 20 | memcpy(ce->name, base, baselen); |
| 21 | memcpy(ce->name + baselen, pathname, len+1); |
| 22 | memcpy(ce->sha1, sha1, 20); |
Junio C Hamano | b155725 | 2005-06-25 02:25:29 -0700 | [diff] [blame] | 23 | 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] | 24 | } |
| 25 | |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 26 | 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] | 27 | { |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 28 | const char *match; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 29 | int pathlen; |
| 30 | |
| 31 | if (!paths) |
| 32 | return 1; |
| 33 | pathlen = strlen(path); |
| 34 | while ((match = *paths++) != NULL) { |
| 35 | int matchlen = strlen(match); |
| 36 | |
| 37 | if (baselen >= matchlen) { |
| 38 | /* If it doesn't match, move along... */ |
| 39 | if (strncmp(base, match, matchlen)) |
| 40 | continue; |
| 41 | /* The base is a subdirectory of a path which was specified. */ |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | /* Does the base match? */ |
| 46 | if (strncmp(base, match, baselen)) |
| 47 | continue; |
| 48 | |
| 49 | match += baselen; |
| 50 | matchlen -= baselen; |
| 51 | |
| 52 | if (pathlen > matchlen) |
| 53 | continue; |
| 54 | |
| 55 | if (matchlen > pathlen) { |
| 56 | if (match[pathlen] != '/') |
| 57 | continue; |
| 58 | if (!S_ISDIR(mode)) |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | if (strncmp(path, match, pathlen)) |
| 63 | continue; |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 64 | |
| 65 | return 1; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 70 | static int read_tree_recursive(void *buffer, unsigned long size, |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 71 | const char *base, int baselen, |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 72 | int stage, const char **match) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 73 | { |
| 74 | while (size) { |
| 75 | int len = strlen(buffer)+1; |
| 76 | unsigned char *sha1 = buffer + len; |
| 77 | char *path = strchr(buffer, ' ')+1; |
| 78 | unsigned int mode; |
| 79 | |
| 80 | if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1) |
| 81 | return -1; |
| 82 | |
| 83 | buffer = sha1 + 20; |
| 84 | size -= len + 20; |
| 85 | |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 86 | if (!match_tree_entry(base, baselen, path, mode, match)) |
| 87 | continue; |
| 88 | |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 89 | if (S_ISDIR(mode)) { |
| 90 | int retval; |
| 91 | int pathlen = strlen(path); |
Jonas Fonseca | 1c9da46 | 2005-04-27 00:00:01 +0200 | [diff] [blame] | 92 | char *newbase; |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 93 | void *eltbuf; |
| 94 | char elttype[20]; |
| 95 | unsigned long eltsize; |
| 96 | |
| 97 | eltbuf = read_sha1_file(sha1, elttype, &eltsize); |
Jonas Fonseca | 1c9da46 | 2005-04-27 00:00:01 +0200 | [diff] [blame] | 98 | if (!eltbuf || strcmp(elttype, "tree")) { |
| 99 | if (eltbuf) free(eltbuf); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 100 | return -1; |
Jonas Fonseca | 1c9da46 | 2005-04-27 00:00:01 +0200 | [diff] [blame] | 101 | } |
| 102 | newbase = xmalloc(baselen + 1 + pathlen); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 103 | memcpy(newbase, base, baselen); |
| 104 | memcpy(newbase + baselen, path, pathlen); |
| 105 | newbase[baselen + pathlen] = '/'; |
| 106 | retval = read_tree_recursive(eltbuf, eltsize, |
| 107 | newbase, |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 108 | baselen + pathlen + 1, |
| 109 | stage, match); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 110 | free(eltbuf); |
| 111 | free(newbase); |
| 112 | if (retval) |
| 113 | return -1; |
| 114 | continue; |
| 115 | } |
| 116 | if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0) |
| 117 | return -1; |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 122 | int read_tree(void *buffer, unsigned long size, int stage, const char **match) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 123 | { |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 124 | return read_tree_recursive(buffer, size, "", 0, stage, match); |
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) { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 131 | struct tree *ret = xmalloc(sizeof(struct tree)); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 132 | memset(ret, 0, sizeof(struct tree)); |
| 133 | created_object(sha1, &ret->object); |
Linus Torvalds | d32987b | 2005-04-24 14:17:13 -0700 | [diff] [blame] | 134 | ret->object.type = tree_type; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 135 | return ret; |
| 136 | } |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 137 | if (!obj->type) |
| 138 | obj->type = tree_type; |
Linus Torvalds | c35dfe8 | 2005-04-24 14:22:09 -0700 | [diff] [blame] | 139 | if (obj->type != tree_type) { |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 140 | error("Object %s is a %s, not a tree", |
| 141 | sha1_to_hex(sha1), obj->type); |
| 142 | return NULL; |
| 143 | } |
| 144 | return (struct tree *) obj; |
| 145 | } |
| 146 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 147 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 148 | { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 149 | void *bufptr = buffer; |
Daniel Barkalow | 0869216 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 150 | struct tree_entry_list **list_p; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 151 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 152 | if (item->object.parsed) |
| 153 | return 0; |
| 154 | item->object.parsed = 1; |
Daniel Barkalow | 0869216 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 155 | list_p = &item->entries; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 156 | while (size) { |
| 157 | struct object *obj; |
Daniel Barkalow | 0869216 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 158 | struct tree_entry_list *entry; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 159 | int len = 1+strlen(bufptr); |
| 160 | unsigned char *file_sha1 = bufptr + len; |
| 161 | char *path = strchr(bufptr, ' '); |
| 162 | unsigned int mode; |
| 163 | if (size < len + 20 || !path || |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 164 | sscanf(bufptr, "%o", &mode) != 1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 165 | return -1; |
| 166 | |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 167 | entry = xmalloc(sizeof(struct tree_entry_list)); |
Daniel Barkalow | 0869216 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 168 | entry->name = strdup(path + 1); |
Linus Torvalds | 42ea9cb | 2005-05-05 16:18:48 -0700 | [diff] [blame] | 169 | entry->directory = S_ISDIR(mode) != 0; |
| 170 | entry->executable = (mode & S_IXUSR) != 0; |
| 171 | entry->symlink = S_ISLNK(mode) != 0; |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 172 | entry->zeropad = *(char *)bufptr == '0'; |
Linus Torvalds | 42ea9cb | 2005-05-05 16:18:48 -0700 | [diff] [blame] | 173 | entry->mode = mode; |
Daniel Barkalow | 0869216 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 174 | entry->next = NULL; |
| 175 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 176 | bufptr += len + 20; |
| 177 | size -= len + 20; |
| 178 | |
Daniel Barkalow | 0869216 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 179 | if (entry->directory) { |
| 180 | entry->item.tree = lookup_tree(file_sha1); |
| 181 | obj = &entry->item.tree->object; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 182 | } else { |
Daniel Barkalow | 0869216 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 183 | entry->item.blob = lookup_blob(file_sha1); |
| 184 | obj = &entry->item.blob->object; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 185 | } |
Linus Torvalds | 235ac40 | 2005-04-24 14:31:57 -0700 | [diff] [blame] | 186 | if (obj) |
| 187 | add_ref(&item->object, obj); |
Junio C Hamano | 6af1f01 | 2005-05-28 00:05:38 -0700 | [diff] [blame] | 188 | entry->parent = NULL; /* needs to be filled by the user */ |
Daniel Barkalow | 0869216 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 189 | *list_p = entry; |
| 190 | list_p = &entry->next; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 191 | } |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 192 | return 0; |
| 193 | } |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 194 | |
| 195 | int parse_tree(struct tree *item) |
| 196 | { |
| 197 | char type[20]; |
| 198 | void *buffer; |
| 199 | unsigned long size; |
| 200 | int ret; |
| 201 | |
| 202 | if (item->object.parsed) |
| 203 | return 0; |
| 204 | buffer = read_sha1_file(item->object.sha1, type, &size); |
| 205 | if (!buffer) |
| 206 | return error("Could not read %s", |
| 207 | sha1_to_hex(item->object.sha1)); |
| 208 | if (strcmp(type, tree_type)) { |
| 209 | free(buffer); |
| 210 | return error("Object %s not a tree", |
| 211 | sha1_to_hex(item->object.sha1)); |
| 212 | } |
| 213 | ret = parse_tree_buffer(item, buffer, size); |
| 214 | free(buffer); |
| 215 | return ret; |
| 216 | } |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 217 | |
| 218 | struct tree *parse_tree_indirect(const unsigned char *sha1) |
| 219 | { |
| 220 | struct object *obj = parse_object(sha1); |
| 221 | do { |
| 222 | if (!obj) |
| 223 | return NULL; |
| 224 | if (obj->type == tree_type) |
| 225 | return (struct tree *) obj; |
| 226 | else if (obj->type == commit_type) |
| 227 | obj = &(((struct commit *) obj)->tree->object); |
| 228 | else if (obj->type == tag_type) |
| 229 | obj = ((struct tag *) obj)->tagged; |
| 230 | else |
| 231 | return NULL; |
| 232 | if (!obj->parsed) |
| 233 | parse_object(obj->sha1); |
| 234 | } while (1); |
| 235 | } |