Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 2 | #include "cache-tree.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 3 | #include "tree.h" |
| 4 | #include "blob.h" |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 5 | #include "commit.h" |
| 6 | #include "tag.h" |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 7 | #include "tree-walk.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 8 | |
| 9 | const char *tree_type = "tree"; |
| 10 | |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 12 | { |
Linus Torvalds | 3c5e846 | 2005-11-26 09:38:20 -0800 | [diff] [blame] | 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); |
Peter Eriksen | 90321c1 | 2006-04-03 19:30:46 +0100 | [diff] [blame] | 22 | ce = xcalloc(1, size); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 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); |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 28 | hashcpy(ce->sha1, sha1); |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 29 | return add_cache_entry(ce, opt); |
| 30 | } |
| 31 | |
René Scharfe | 671f070 | 2008-07-14 21:22:12 +0200 | [diff] [blame] | 32 | static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context) |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 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 | */ |
René Scharfe | 671f070 | 2008-07-14 21:22:12 +0200 | [diff] [blame] | 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) |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 43 | { |
| 44 | return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage, |
| 45 | ADD_CACHE_JUST_APPEND); |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 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) |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 51 | { |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 52 | struct tree_desc desc; |
| 53 | struct name_entry entry; |
| 54 | unsigned char sha1[20]; |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 55 | int len, oldlen = base->len; |
| 56 | enum interesting retval = entry_not_interesting; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 57 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 58 | if (parse_tree(tree)) |
| 59 | return -1; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 60 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 61 | init_tree_desc(&desc, tree->buffer, tree->size); |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 62 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 63 | while (tree_entry(&desc, &entry)) { |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 64 | if (retval != all_entries_interesting) { |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 65 | retval = tree_entry_interesting(&entry, base, 0, pathspec); |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 66 | if (retval == all_entries_not_interesting) |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 67 | break; |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 68 | if (retval == entry_not_interesting) |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 69 | continue; |
| 70 | } |
| 71 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 72 | switch (fn(entry.sha1, base->buf, base->len, |
| 73 | entry.path, entry.mode, stage, context)) { |
| 74 | case 0: |
| 75 | continue; |
| 76 | case READ_TREE_RECURSIVE: |
| 77 | break; |
| 78 | default: |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | if (S_ISDIR(entry.mode)) |
| 83 | hashcpy(sha1, entry.sha1); |
| 84 | else if (S_ISGITLINK(entry.mode)) { |
| 85 | struct commit *commit; |
| 86 | |
| 87 | commit = lookup_commit(entry.sha1); |
| 88 | if (!commit) |
| 89 | die("Commit %s in submodule path %s%s not found", |
| 90 | sha1_to_hex(entry.sha1), |
| 91 | base->buf, entry.path); |
| 92 | |
| 93 | if (parse_commit(commit)) |
| 94 | die("Invalid commit %s in submodule path %s%s", |
| 95 | sha1_to_hex(entry.sha1), |
| 96 | base->buf, entry.path); |
| 97 | |
| 98 | hashcpy(sha1, commit->tree->object.sha1); |
| 99 | } |
| 100 | else |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 101 | continue; |
Linus Torvalds | 3e58763 | 2005-07-14 11:39:27 -0700 | [diff] [blame] | 102 | |
Nguyễn Thái Ngọc Duy | 0de1633 | 2011-10-24 17:36:09 +1100 | [diff] [blame] | 103 | len = tree_entry_len(&entry); |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 104 | strbuf_add(base, entry.path, len); |
| 105 | strbuf_addch(base, '/'); |
| 106 | retval = read_tree_1(lookup_tree(sha1), |
| 107 | base, stage, pathspec, |
| 108 | fn, context); |
| 109 | strbuf_setlen(base, oldlen); |
| 110 | if (retval) |
| 111 | return -1; |
Linus Torvalds | 0ca14a5 | 2005-07-14 11:26:31 -0700 | [diff] [blame] | 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
Daniel Barkalow | 521698b | 2006-01-26 01:13:36 -0500 | [diff] [blame] | 116 | int read_tree_recursive(struct tree *tree, |
Linus Torvalds | 3c5e846 | 2005-11-26 09:38:20 -0800 | [diff] [blame] | 117 | const char *base, int baselen, |
Nguyễn Thái Ngọc Duy | f0096c0 | 2011-03-25 16:34:19 +0700 | [diff] [blame] | 118 | int stage, struct pathspec *pathspec, |
René Scharfe | 671f070 | 2008-07-14 21:22:12 +0200 | [diff] [blame] | 119 | read_tree_fn_t fn, void *context) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 120 | { |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 121 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | f0096c0 | 2011-03-25 16:34:19 +0700 | [diff] [blame] | 122 | int ret; |
Linus Torvalds | 0790a42 | 2006-05-29 12:17:28 -0700 | [diff] [blame] | 123 | |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 124 | strbuf_add(&sb, base, baselen); |
Nguyễn Thái Ngọc Duy | f0096c0 | 2011-03-25 16:34:19 +0700 | [diff] [blame] | 125 | ret = read_tree_1(tree, &sb, stage, pathspec, fn, context); |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 126 | strbuf_release(&sb); |
Nguyễn Thái Ngọc Duy | ffd31f6 | 2011-03-25 16:34:18 +0700 | [diff] [blame] | 127 | return ret; |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 130 | static int cmp_cache_name_compare(const void *a_, const void *b_) |
| 131 | { |
| 132 | const struct cache_entry *ce1, *ce2; |
| 133 | |
| 134 | ce1 = *((const struct cache_entry **)a_); |
| 135 | ce2 = *((const struct cache_entry **)b_); |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 136 | return cache_name_compare(ce1->name, ce1->ce_flags, |
| 137 | ce2->name, ce2->ce_flags); |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Nguyễn Thái Ngọc Duy | f0096c0 | 2011-03-25 16:34:19 +0700 | [diff] [blame] | 140 | int read_tree(struct tree *tree, int stage, struct pathspec *match) |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 141 | { |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 142 | read_tree_fn_t fn = NULL; |
| 143 | int i, err; |
| 144 | |
| 145 | /* |
| 146 | * Currently the only existing callers of this function all |
| 147 | * call it with stage=1 and after making sure there is nothing |
| 148 | * at that stage; we could always use read_one_entry_quick(). |
| 149 | * |
| 150 | * But when we decide to straighten out git-read-tree not to |
| 151 | * use unpack_trees() in some cases, this will probably start |
| 152 | * to matter. |
| 153 | */ |
| 154 | |
| 155 | /* |
| 156 | * See if we have cache entry at the stage. If so, |
| 157 | * do it the original slow way, otherwise, append and then |
| 158 | * sort at the end. |
| 159 | */ |
| 160 | for (i = 0; !fn && i < active_nr; i++) { |
| 161 | struct cache_entry *ce = active_cache[i]; |
| 162 | if (ce_stage(ce) == stage) |
| 163 | fn = read_one_entry; |
| 164 | } |
| 165 | |
| 166 | if (!fn) |
| 167 | fn = read_one_entry_quick; |
René Scharfe | 671f070 | 2008-07-14 21:22:12 +0200 | [diff] [blame] | 168 | err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL); |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 169 | if (fn == read_one_entry || err) |
| 170 | return err; |
| 171 | |
| 172 | /* |
| 173 | * Sort the cache entry -- we need to nuke the cache tree, though. |
| 174 | */ |
| 175 | cache_tree_free(&active_cache_tree); |
| 176 | qsort(active_cache, active_nr, sizeof(active_cache[0]), |
| 177 | cmp_cache_name_compare); |
| 178 | return 0; |
Linus Torvalds | 94537c7 | 2005-04-22 16:42:37 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 181 | struct tree *lookup_tree(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 182 | { |
| 183 | struct object *obj = lookup_object(sha1); |
Linus Torvalds | 100c5f3 | 2007-04-16 22:11:43 -0700 | [diff] [blame] | 184 | if (!obj) |
| 185 | return create_object(sha1, OBJ_TREE, alloc_tree_node()); |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 186 | if (!obj->type) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 187 | obj->type = OBJ_TREE; |
| 188 | if (obj->type != OBJ_TREE) { |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 189 | error("Object %s is a %s, not a tree", |
| 190 | sha1_to_hex(sha1), typename(obj->type)); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
| 193 | return (struct tree *) obj; |
| 194 | } |
| 195 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 196 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 197 | { |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 198 | if (item->object.parsed) |
| 199 | return 0; |
| 200 | item->object.parsed = 1; |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 201 | item->buffer = buffer; |
| 202 | item->size = size; |
| 203 | |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | } |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 206 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 207 | int parse_tree(struct tree *item) |
| 208 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 209 | enum object_type type; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 210 | void *buffer; |
| 211 | unsigned long size; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 212 | |
| 213 | if (item->object.parsed) |
| 214 | return 0; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 215 | buffer = read_sha1_file(item->object.sha1, &type, &size); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 216 | if (!buffer) |
| 217 | return error("Could not read %s", |
| 218 | sha1_to_hex(item->object.sha1)); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 219 | if (type != OBJ_TREE) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 220 | free(buffer); |
| 221 | return error("Object %s not a tree", |
| 222 | sha1_to_hex(item->object.sha1)); |
| 223 | } |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 224 | return parse_tree_buffer(item, buffer, size); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 225 | } |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 226 | |
| 227 | struct tree *parse_tree_indirect(const unsigned char *sha1) |
| 228 | { |
| 229 | struct object *obj = parse_object(sha1); |
| 230 | do { |
| 231 | if (!obj) |
| 232 | return NULL; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 233 | if (obj->type == OBJ_TREE) |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 234 | return (struct tree *) obj; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 235 | else if (obj->type == OBJ_COMMIT) |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 236 | obj = &(((struct commit *) obj)->tree->object); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 237 | else if (obj->type == OBJ_TAG) |
Daniel Barkalow | 77675e2 | 2005-09-05 02:03:51 -0400 | [diff] [blame] | 238 | obj = ((struct tag *) obj)->tagged; |
| 239 | else |
| 240 | return NULL; |
| 241 | if (!obj->parsed) |
| 242 | parse_object(obj->sha1); |
| 243 | } while (1); |
| 244 | } |