| 1 | #include "cache.h" |
| 2 | #include "cache-tree.h" |
| 3 | #include "tree.h" |
| 4 | #include "blob.h" |
| 5 | #include "commit.h" |
| 6 | #include "tag.h" |
| 7 | #include "tree-walk.h" |
| 8 | |
| 9 | const char *tree_type = "tree"; |
| 10 | |
| 11 | static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt) |
| 12 | { |
| 13 | int len; |
| 14 | unsigned int size; |
| 15 | struct cache_entry *ce; |
| 16 | |
| 17 | if (S_ISDIR(mode)) |
| 18 | return READ_TREE_RECURSIVE; |
| 19 | |
| 20 | len = strlen(pathname); |
| 21 | size = cache_entry_size(baselen + len); |
| 22 | ce = xcalloc(1, size); |
| 23 | |
| 24 | ce->ce_mode = create_ce_mode(mode); |
| 25 | ce->ce_flags = create_ce_flags(baselen + len, stage); |
| 26 | memcpy(ce->name, base, baselen); |
| 27 | memcpy(ce->name + baselen, pathname, len+1); |
| 28 | hashcpy(ce->sha1, sha1); |
| 29 | return add_cache_entry(ce, opt); |
| 30 | } |
| 31 | |
| 32 | static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context) |
| 33 | { |
| 34 | return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage, |
| 35 | ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * This is used when the caller knows there is no existing entries at |
| 40 | * the stage that will conflict with the entry being added. |
| 41 | */ |
| 42 | static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context) |
| 43 | { |
| 44 | return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage, |
| 45 | ADD_CACHE_JUST_APPEND); |
| 46 | } |
| 47 | |
| 48 | static int read_tree_1(struct tree *tree, struct strbuf *base, |
| 49 | int stage, struct pathspec *pathspec, |
| 50 | read_tree_fn_t fn, void *context) |
| 51 | { |
| 52 | struct tree_desc desc; |
| 53 | struct name_entry entry; |
| 54 | unsigned char sha1[20]; |
| 55 | int len, retval = 0, oldlen = base->len; |
| 56 | |
| 57 | if (parse_tree(tree)) |
| 58 | return -1; |
| 59 | |
| 60 | init_tree_desc(&desc, tree->buffer, tree->size); |
| 61 | |
| 62 | while (tree_entry(&desc, &entry)) { |
| 63 | if (retval != 2) { |
| 64 | retval = tree_entry_interesting(&entry, base, 0, pathspec); |
| 65 | if (retval < 0) |
| 66 | break; |
| 67 | if (retval == 0) |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | switch (fn(entry.sha1, base->buf, base->len, |
| 72 | entry.path, entry.mode, stage, context)) { |
| 73 | case 0: |
| 74 | continue; |
| 75 | case READ_TREE_RECURSIVE: |
| 76 | break; |
| 77 | default: |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | if (S_ISDIR(entry.mode)) |
| 82 | hashcpy(sha1, entry.sha1); |
| 83 | else if (S_ISGITLINK(entry.mode)) { |
| 84 | struct commit *commit; |
| 85 | |
| 86 | commit = lookup_commit(entry.sha1); |
| 87 | if (!commit) |
| 88 | die("Commit %s in submodule path %s%s not found", |
| 89 | sha1_to_hex(entry.sha1), |
| 90 | base->buf, entry.path); |
| 91 | |
| 92 | if (parse_commit(commit)) |
| 93 | die("Invalid commit %s in submodule path %s%s", |
| 94 | sha1_to_hex(entry.sha1), |
| 95 | base->buf, entry.path); |
| 96 | |
| 97 | hashcpy(sha1, commit->tree->object.sha1); |
| 98 | } |
| 99 | else |
| 100 | continue; |
| 101 | |
| 102 | len = tree_entry_len(entry.path, entry.sha1); |
| 103 | strbuf_add(base, entry.path, len); |
| 104 | strbuf_addch(base, '/'); |
| 105 | retval = read_tree_1(lookup_tree(sha1), |
| 106 | base, stage, pathspec, |
| 107 | fn, context); |
| 108 | strbuf_setlen(base, oldlen); |
| 109 | if (retval) |
| 110 | return -1; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int read_tree_recursive(struct tree *tree, |
| 116 | const char *base, int baselen, |
| 117 | int stage, const char **match, |
| 118 | read_tree_fn_t fn, void *context) |
| 119 | { |
| 120 | struct strbuf sb = STRBUF_INIT; |
| 121 | struct pathspec pathspec; |
| 122 | int i, ret; |
| 123 | |
| 124 | init_pathspec(&pathspec, match); |
| 125 | for (i = 0; i < pathspec.nr; i++) |
| 126 | pathspec.items[i].has_wildcard = 0; |
| 127 | strbuf_add(&sb, base, baselen); |
| 128 | ret = read_tree_1(tree, &sb, stage, &pathspec, fn, context); |
| 129 | strbuf_release(&sb); |
| 130 | free_pathspec(&pathspec); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | static int cmp_cache_name_compare(const void *a_, const void *b_) |
| 135 | { |
| 136 | const struct cache_entry *ce1, *ce2; |
| 137 | |
| 138 | ce1 = *((const struct cache_entry **)a_); |
| 139 | ce2 = *((const struct cache_entry **)b_); |
| 140 | return cache_name_compare(ce1->name, ce1->ce_flags, |
| 141 | ce2->name, ce2->ce_flags); |
| 142 | } |
| 143 | |
| 144 | int read_tree(struct tree *tree, int stage, const char **match) |
| 145 | { |
| 146 | read_tree_fn_t fn = NULL; |
| 147 | int i, err; |
| 148 | |
| 149 | /* |
| 150 | * Currently the only existing callers of this function all |
| 151 | * call it with stage=1 and after making sure there is nothing |
| 152 | * at that stage; we could always use read_one_entry_quick(). |
| 153 | * |
| 154 | * But when we decide to straighten out git-read-tree not to |
| 155 | * use unpack_trees() in some cases, this will probably start |
| 156 | * to matter. |
| 157 | */ |
| 158 | |
| 159 | /* |
| 160 | * See if we have cache entry at the stage. If so, |
| 161 | * do it the original slow way, otherwise, append and then |
| 162 | * sort at the end. |
| 163 | */ |
| 164 | for (i = 0; !fn && i < active_nr; i++) { |
| 165 | struct cache_entry *ce = active_cache[i]; |
| 166 | if (ce_stage(ce) == stage) |
| 167 | fn = read_one_entry; |
| 168 | } |
| 169 | |
| 170 | if (!fn) |
| 171 | fn = read_one_entry_quick; |
| 172 | err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL); |
| 173 | if (fn == read_one_entry || err) |
| 174 | return err; |
| 175 | |
| 176 | /* |
| 177 | * Sort the cache entry -- we need to nuke the cache tree, though. |
| 178 | */ |
| 179 | cache_tree_free(&active_cache_tree); |
| 180 | qsort(active_cache, active_nr, sizeof(active_cache[0]), |
| 181 | cmp_cache_name_compare); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | struct tree *lookup_tree(const unsigned char *sha1) |
| 186 | { |
| 187 | struct object *obj = lookup_object(sha1); |
| 188 | if (!obj) |
| 189 | return create_object(sha1, OBJ_TREE, alloc_tree_node()); |
| 190 | if (!obj->type) |
| 191 | obj->type = OBJ_TREE; |
| 192 | if (obj->type != OBJ_TREE) { |
| 193 | error("Object %s is a %s, not a tree", |
| 194 | sha1_to_hex(sha1), typename(obj->type)); |
| 195 | return NULL; |
| 196 | } |
| 197 | return (struct tree *) obj; |
| 198 | } |
| 199 | |
| 200 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) |
| 201 | { |
| 202 | if (item->object.parsed) |
| 203 | return 0; |
| 204 | item->object.parsed = 1; |
| 205 | item->buffer = buffer; |
| 206 | item->size = size; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int parse_tree(struct tree *item) |
| 212 | { |
| 213 | enum object_type type; |
| 214 | void *buffer; |
| 215 | unsigned long size; |
| 216 | |
| 217 | if (item->object.parsed) |
| 218 | return 0; |
| 219 | buffer = read_sha1_file(item->object.sha1, &type, &size); |
| 220 | if (!buffer) |
| 221 | return error("Could not read %s", |
| 222 | sha1_to_hex(item->object.sha1)); |
| 223 | if (type != OBJ_TREE) { |
| 224 | free(buffer); |
| 225 | return error("Object %s not a tree", |
| 226 | sha1_to_hex(item->object.sha1)); |
| 227 | } |
| 228 | return parse_tree_buffer(item, buffer, size); |
| 229 | } |
| 230 | |
| 231 | struct tree *parse_tree_indirect(const unsigned char *sha1) |
| 232 | { |
| 233 | struct object *obj = parse_object(sha1); |
| 234 | do { |
| 235 | if (!obj) |
| 236 | return NULL; |
| 237 | if (obj->type == OBJ_TREE) |
| 238 | return (struct tree *) obj; |
| 239 | else if (obj->type == OBJ_COMMIT) |
| 240 | obj = &(((struct commit *) obj)->tree->object); |
| 241 | else if (obj->type == OBJ_TAG) |
| 242 | obj = ((struct tag *) obj)->tagged; |
| 243 | else |
| 244 | return NULL; |
| 245 | if (!obj->parsed) |
| 246 | parse_object(obj->sha1); |
| 247 | } while (1); |
| 248 | } |