]>
Commit | Line | Data |
---|---|---|
85ab50f9 | 1 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
8f1d2e6f | 2 | #include "cache.h" |
af3785dc | 3 | #include "cache-tree.h" |
175785e5 | 4 | #include "tree.h" |
cbd53a21 | 5 | #include "object-store.h" |
175785e5 | 6 | #include "blob.h" |
77675e2a DB |
7 | #include "commit.h" |
8 | #include "tag.h" | |
14ba97f8 | 9 | #include "alloc.h" |
136f2e54 | 10 | #include "tree-walk.h" |
175785e5 DB |
11 | |
12 | const char *tree_type = "tree"; | |
13 | ||
85ab50f9 | 14 | static int read_one_entry_opt(struct index_state *istate, |
df46d77e | 15 | const struct object_id *oid, |
85ab50f9 BW |
16 | const char *base, int baselen, |
17 | const char *pathname, | |
18 | unsigned mode, int stage, int opt) | |
94537c78 | 19 | { |
3c5e8468 LT |
20 | int len; |
21 | unsigned int size; | |
22 | struct cache_entry *ce; | |
23 | ||
24 | if (S_ISDIR(mode)) | |
25 | return READ_TREE_RECURSIVE; | |
26 | ||
27 | len = strlen(pathname); | |
28 | size = cache_entry_size(baselen + len); | |
90321c10 | 29 | ce = xcalloc(1, size); |
94537c78 LT |
30 | |
31 | ce->ce_mode = create_ce_mode(mode); | |
b60e188c TG |
32 | ce->ce_flags = create_ce_flags(stage); |
33 | ce->ce_namelen = baselen + len; | |
94537c78 LT |
34 | memcpy(ce->name, base, baselen); |
35 | memcpy(ce->name + baselen, pathname, len+1); | |
df46d77e | 36 | oidcpy(&ce->oid, oid); |
85ab50f9 | 37 | return add_index_entry(istate, ce, opt); |
af3785dc JH |
38 | } |
39 | ||
df46d77e | 40 | static int read_one_entry(const struct object_id *oid, struct strbuf *base, |
6a0b0b6d NTND |
41 | const char *pathname, unsigned mode, int stage, |
42 | void *context) | |
af3785dc | 43 | { |
85ab50f9 | 44 | struct index_state *istate = context; |
df46d77e | 45 | return read_one_entry_opt(istate, oid, base->buf, base->len, pathname, |
6a0b0b6d | 46 | mode, stage, |
af3785dc JH |
47 | ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); |
48 | } | |
49 | ||
50 | /* | |
51 | * This is used when the caller knows there is no existing entries at | |
52 | * the stage that will conflict with the entry being added. | |
53 | */ | |
df46d77e | 54 | static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base, |
6a0b0b6d NTND |
55 | const char *pathname, unsigned mode, int stage, |
56 | void *context) | |
af3785dc | 57 | { |
85ab50f9 | 58 | struct index_state *istate = context; |
df46d77e | 59 | return read_one_entry_opt(istate, oid, base->buf, base->len, pathname, |
6a0b0b6d | 60 | mode, stage, |
af3785dc | 61 | ADD_CACHE_JUST_APPEND); |
94537c78 LT |
62 | } |
63 | ||
ffd31f66 | 64 | static int read_tree_1(struct tree *tree, struct strbuf *base, |
18e4f405 | 65 | int stage, const struct pathspec *pathspec, |
ffd31f66 | 66 | read_tree_fn_t fn, void *context) |
94537c78 | 67 | { |
0790a42a | 68 | struct tree_desc desc; |
4c068a98 | 69 | struct name_entry entry; |
f26efc58 | 70 | struct object_id oid; |
d688cf07 NTND |
71 | int len, oldlen = base->len; |
72 | enum interesting retval = entry_not_interesting; | |
0790a42a | 73 | |
521698b1 DB |
74 | if (parse_tree(tree)) |
75 | return -1; | |
0790a42a | 76 | |
6fda5e51 | 77 | init_tree_desc(&desc, tree->buffer, tree->size); |
0790a42a | 78 | |
4c068a98 | 79 | while (tree_entry(&desc, &entry)) { |
d688cf07 | 80 | if (retval != all_entries_interesting) { |
ffd31f66 | 81 | retval = tree_entry_interesting(&entry, base, 0, pathspec); |
d688cf07 | 82 | if (retval == all_entries_not_interesting) |
ffd31f66 | 83 | break; |
d688cf07 | 84 | if (retval == entry_not_interesting) |
ffd31f66 NTND |
85 | continue; |
86 | } | |
0ca14a57 | 87 | |
df46d77e | 88 | switch (fn(entry.oid, base, |
ffd31f66 | 89 | entry.path, entry.mode, stage, context)) { |
3c5e8468 LT |
90 | case 0: |
91 | continue; | |
92 | case READ_TREE_RECURSIVE: | |
ba19a808 | 93 | break; |
3c5e8468 LT |
94 | default: |
95 | return -1; | |
96 | } | |
d3bee161 | 97 | |
ffd31f66 | 98 | if (S_ISDIR(entry.mode)) |
f26efc58 | 99 | oidcpy(&oid, entry.oid); |
ffd31f66 NTND |
100 | else if (S_ISGITLINK(entry.mode)) { |
101 | struct commit *commit; | |
d3bee161 | 102 | |
bc83266a | 103 | commit = lookup_commit(entry.oid); |
d3bee161 | 104 | if (!commit) |
ffd31f66 | 105 | die("Commit %s in submodule path %s%s not found", |
7d924c91 | 106 | oid_to_hex(entry.oid), |
ffd31f66 | 107 | base->buf, entry.path); |
d3bee161 LH |
108 | |
109 | if (parse_commit(commit)) | |
ffd31f66 | 110 | die("Invalid commit %s in submodule path %s%s", |
7d924c91 | 111 | oid_to_hex(entry.oid), |
ffd31f66 NTND |
112 | base->buf, entry.path); |
113 | ||
2e27bd77 | 114 | oidcpy(&oid, get_commit_tree_oid(commit)); |
94537c78 | 115 | } |
ffd31f66 NTND |
116 | else |
117 | continue; | |
118 | ||
0de16337 | 119 | len = tree_entry_len(&entry); |
ffd31f66 NTND |
120 | strbuf_add(base, entry.path, len); |
121 | strbuf_addch(base, '/'); | |
740ee055 | 122 | retval = read_tree_1(lookup_tree(&oid), |
ffd31f66 NTND |
123 | base, stage, pathspec, |
124 | fn, context); | |
125 | strbuf_setlen(base, oldlen); | |
126 | if (retval) | |
127 | return -1; | |
94537c78 LT |
128 | } |
129 | return 0; | |
130 | } | |
131 | ||
ffd31f66 NTND |
132 | int read_tree_recursive(struct tree *tree, |
133 | const char *base, int baselen, | |
18e4f405 | 134 | int stage, const struct pathspec *pathspec, |
ffd31f66 NTND |
135 | read_tree_fn_t fn, void *context) |
136 | { | |
137 | struct strbuf sb = STRBUF_INIT; | |
f0096c06 | 138 | int ret; |
ffd31f66 | 139 | |
ffd31f66 | 140 | strbuf_add(&sb, base, baselen); |
f0096c06 | 141 | ret = read_tree_1(tree, &sb, stage, pathspec, fn, context); |
ffd31f66 | 142 | strbuf_release(&sb); |
ffd31f66 NTND |
143 | return ret; |
144 | } | |
145 | ||
af3785dc JH |
146 | static int cmp_cache_name_compare(const void *a_, const void *b_) |
147 | { | |
148 | const struct cache_entry *ce1, *ce2; | |
149 | ||
150 | ce1 = *((const struct cache_entry **)a_); | |
151 | ce2 = *((const struct cache_entry **)b_); | |
b60e188c TG |
152 | return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1), |
153 | ce2->name, ce2->ce_namelen, ce_stage(ce2)); | |
af3785dc JH |
154 | } |
155 | ||
85ab50f9 BW |
156 | int read_tree(struct tree *tree, int stage, struct pathspec *match, |
157 | struct index_state *istate) | |
94537c78 | 158 | { |
af3785dc JH |
159 | read_tree_fn_t fn = NULL; |
160 | int i, err; | |
161 | ||
162 | /* | |
163 | * Currently the only existing callers of this function all | |
164 | * call it with stage=1 and after making sure there is nothing | |
165 | * at that stage; we could always use read_one_entry_quick(). | |
166 | * | |
167 | * But when we decide to straighten out git-read-tree not to | |
168 | * use unpack_trees() in some cases, this will probably start | |
169 | * to matter. | |
170 | */ | |
171 | ||
172 | /* | |
173 | * See if we have cache entry at the stage. If so, | |
174 | * do it the original slow way, otherwise, append and then | |
175 | * sort at the end. | |
176 | */ | |
85ab50f9 BW |
177 | for (i = 0; !fn && i < istate->cache_nr; i++) { |
178 | const struct cache_entry *ce = istate->cache[i]; | |
af3785dc JH |
179 | if (ce_stage(ce) == stage) |
180 | fn = read_one_entry; | |
181 | } | |
182 | ||
183 | if (!fn) | |
184 | fn = read_one_entry_quick; | |
85ab50f9 | 185 | err = read_tree_recursive(tree, "", 0, stage, match, fn, istate); |
af3785dc JH |
186 | if (fn == read_one_entry || err) |
187 | return err; | |
188 | ||
189 | /* | |
190 | * Sort the cache entry -- we need to nuke the cache tree, though. | |
191 | */ | |
85ab50f9 BW |
192 | cache_tree_free(&istate->cache_tree); |
193 | QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare); | |
af3785dc | 194 | return 0; |
94537c78 LT |
195 | } |
196 | ||
740ee055 | 197 | struct tree *lookup_tree(const struct object_id *oid) |
175785e5 | 198 | { |
740ee055 | 199 | struct object *obj = lookup_object(oid->hash); |
100c5f3b | 200 | if (!obj) |
68f95d38 | 201 | return create_object(the_repository, oid->hash, |
cf7203bd | 202 | alloc_tree_node(the_repository)); |
8ff226a9 | 203 | return object_as_type(obj, OBJ_TREE, 0); |
175785e5 DB |
204 | } |
205 | ||
2d9c58c6 LT |
206 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) |
207 | { | |
175785e5 DB |
208 | if (item->object.parsed) |
209 | return 0; | |
210 | item->object.parsed = 1; | |
136f2e54 LT |
211 | item->buffer = buffer; |
212 | item->size = size; | |
213 | ||
2d9c58c6 LT |
214 | return 0; |
215 | } | |
216 | ||
9cc2b07a | 217 | int parse_tree_gently(struct tree *item, int quiet_on_missing) |
bd2c39f5 | 218 | { |
21666f1a | 219 | enum object_type type; |
bd2c39f5 NP |
220 | void *buffer; |
221 | unsigned long size; | |
bd2c39f5 NP |
222 | |
223 | if (item->object.parsed) | |
224 | return 0; | |
b4f5aca4 | 225 | buffer = read_object_file(&item->object.oid, &type, &size); |
bd2c39f5 | 226 | if (!buffer) |
9cc2b07a JK |
227 | return quiet_on_missing ? -1 : |
228 | error("Could not read %s", | |
f2fd0760 | 229 | oid_to_hex(&item->object.oid)); |
21666f1a | 230 | if (type != OBJ_TREE) { |
bd2c39f5 NP |
231 | free(buffer); |
232 | return error("Object %s not a tree", | |
f2fd0760 | 233 | oid_to_hex(&item->object.oid)); |
bd2c39f5 | 234 | } |
136f2e54 | 235 | return parse_tree_buffer(item, buffer, size); |
bd2c39f5 | 236 | } |
77675e2a | 237 | |
6e454b9a JK |
238 | void free_tree_buffer(struct tree *tree) |
239 | { | |
6a83d902 | 240 | FREE_AND_NULL(tree->buffer); |
6e454b9a JK |
241 | tree->size = 0; |
242 | tree->object.parsed = 0; | |
243 | } | |
244 | ||
a9dbc179 | 245 | struct tree *parse_tree_indirect(const struct object_id *oid) |
77675e2a | 246 | { |
c251c83d | 247 | struct object *obj = parse_object(oid); |
77675e2a DB |
248 | do { |
249 | if (!obj) | |
250 | return NULL; | |
1974632c | 251 | if (obj->type == OBJ_TREE) |
77675e2a | 252 | return (struct tree *) obj; |
1974632c | 253 | else if (obj->type == OBJ_COMMIT) |
2e27bd77 | 254 | obj = &(get_commit_tree(((struct commit *)obj))->object); |
1974632c | 255 | else if (obj->type == OBJ_TAG) |
77675e2a DB |
256 | obj = ((struct tag *) obj)->tagged; |
257 | else | |
258 | return NULL; | |
259 | if (!obj->parsed) | |
c251c83d | 260 | parse_object(&obj->oid); |
77675e2a DB |
261 | } while (1); |
262 | } |