]>
Commit | Line | Data |
---|---|---|
225fe215 | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
225fe215 | 3 | * |
bb742ede VM |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
225fe215 VM |
6 | */ |
7 | ||
8 | #include "common.h" | |
9 | #include "commit.h" | |
225fe215 | 10 | #include "tree.h" |
44908fe7 VM |
11 | #include "git2/repository.h" |
12 | #include "git2/object.h" | |
225fe215 | 13 | |
c8f5ff8f | 14 | #define DEFAULT_TREE_SIZE 16 |
9de27ad0 | 15 | #define MAX_FILEMODE_BYTES 6 |
c8f5ff8f | 16 | |
a7dbac0b | 17 | static bool valid_filemode(const int filemode) |
8e9bfa4c | 18 | { |
a7dbac0b | 19 | return (filemode == GIT_FILEMODE_TREE |
20 | || filemode == GIT_FILEMODE_BLOB | |
a7dbac0b | 21 | || filemode == GIT_FILEMODE_BLOB_EXECUTABLE |
22 | || filemode == GIT_FILEMODE_LINK | |
23 | || filemode == GIT_FILEMODE_COMMIT); | |
0ad6efa1 VM |
24 | } |
25 | ||
f1c75b94 CMN |
26 | GIT_INLINE(git_filemode_t) normalize_filemode(git_filemode_t filemode) |
27 | { | |
28 | /* Tree bits set, but it's not a commit */ | |
29 | if (filemode & GIT_FILEMODE_TREE && !(filemode & 0100000)) | |
30 | return GIT_FILEMODE_TREE; | |
31 | ||
32 | /* If any of the x bits is set */ | |
33 | if (filemode & 0111) | |
34 | return GIT_FILEMODE_BLOB_EXECUTABLE; | |
35 | ||
36 | /* 16XXXX means commit */ | |
37 | if ((filemode & GIT_FILEMODE_COMMIT) == GIT_FILEMODE_COMMIT) | |
38 | return GIT_FILEMODE_COMMIT; | |
39 | ||
40 | /* 12XXXX means commit */ | |
41 | if ((filemode & GIT_FILEMODE_LINK) == GIT_FILEMODE_LINK) | |
42 | return GIT_FILEMODE_LINK; | |
43 | ||
44 | /* Otherwise, return a blob */ | |
45 | return GIT_FILEMODE_BLOB; | |
46 | } | |
47 | ||
28c1451a VM |
48 | static int valid_entry_name(const char *filename) |
49 | { | |
cfeef7ce RB |
50 | return *filename != '\0' && |
51 | strchr(filename, '/') == NULL && | |
52 | (*filename != '.' || | |
53 | (strcmp(filename, ".") != 0 && | |
54 | strcmp(filename, "..") != 0 && | |
55 | strcmp(filename, DOT_GIT) != 0)); | |
28c1451a VM |
56 | } |
57 | ||
98527b5b | 58 | int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2) |
28c1451a | 59 | { |
1744fafe | 60 | return git_path_cmp( |
98527b5b RB |
61 | e1->filename, e1->filename_len, git_tree_entry__is_tree(e1), |
62 | e2->filename, e2->filename_len, git_tree_entry__is_tree(e2)); | |
63 | } | |
64 | ||
65 | int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2) | |
66 | { | |
67 | return git_path_icmp( | |
68 | e1->filename, e1->filename_len, git_tree_entry__is_tree(e1), | |
69 | e2->filename, e2->filename_len, git_tree_entry__is_tree(e2)); | |
70 | } | |
71 | ||
72 | static int entry_sort_cmp(const void *a, const void *b) | |
73 | { | |
74 | return git_tree_entry_cmp((const git_tree_entry *)a, (const git_tree_entry *)b); | |
28c1451a VM |
75 | } |
76 | ||
0e2fcca8 VM |
77 | static git_tree_entry *alloc_entry(const char *filename) |
78 | { | |
79 | git_tree_entry *entry = NULL; | |
80 | size_t filename_len = strlen(filename); | |
81 | ||
82 | entry = git__malloc(sizeof(git_tree_entry) + filename_len + 1); | |
83 | if (!entry) | |
84 | return NULL; | |
85 | ||
86 | memset(entry, 0x0, sizeof(git_tree_entry)); | |
87 | memcpy(entry->filename, filename, filename_len); | |
88 | entry->filename[filename_len] = 0; | |
89 | entry->filename_len = filename_len; | |
90 | ||
91 | return entry; | |
92 | } | |
28c1451a | 93 | |
761aa2aa VM |
94 | struct tree_key_search { |
95 | const char *filename; | |
96 | size_t filename_len; | |
97 | }; | |
98 | ||
28c1451a | 99 | static int homing_search_cmp(const void *key, const void *array_member) |
2a884588 | 100 | { |
0cbbdc26 KS |
101 | const struct tree_key_search *ksearch = key; |
102 | const git_tree_entry *entry = array_member; | |
2a884588 | 103 | |
28c1451a VM |
104 | const size_t len1 = ksearch->filename_len; |
105 | const size_t len2 = entry->filename_len; | |
e6629d83 | 106 | |
28c1451a VM |
107 | return memcmp( |
108 | ksearch->filename, | |
109 | entry->filename, | |
110 | len1 < len2 ? len1 : len2 | |
111 | ); | |
9de27ad0 SJ |
112 | } |
113 | ||
28c1451a VM |
114 | /* |
115 | * Search for an entry in a given tree. | |
116 | * | |
117 | * Note that this search is performed in two steps because | |
118 | * of the way tree entries are sorted internally in git: | |
119 | * | |
120 | * Entries in a tree are not sorted alphabetically; two entries | |
121 | * with the same root prefix will have different positions | |
122 | * depending on whether they are folders (subtrees) or normal files. | |
123 | * | |
124 | * Consequently, it is not possible to find an entry on the tree | |
125 | * with a binary search if you don't know whether the filename | |
126 | * you're looking for is a folder or a normal file. | |
127 | * | |
128 | * To work around this, we first perform a homing binary search | |
129 | * on the tree, using the minimal length root prefix of our filename. | |
130 | * Once the comparisons for this homing search start becoming | |
131 | * ambiguous because of folder vs file sorting, we look linearly | |
132 | * around the area for our target file. | |
133 | */ | |
16248ee2 | 134 | static int tree_key_search( |
11d9f6b3 | 135 | size_t *at_pos, git_vector *entries, const char *filename, size_t filename_len) |
2a884588 | 136 | { |
28c1451a VM |
137 | struct tree_key_search ksearch; |
138 | const git_tree_entry *entry; | |
11d9f6b3 | 139 | size_t homing, i; |
2a884588 | 140 | |
28c1451a | 141 | ksearch.filename = filename; |
0e2fcca8 | 142 | ksearch.filename_len = filename_len; |
e6629d83 | 143 | |
28c1451a VM |
144 | /* Initial homing search; find an entry on the tree with |
145 | * the same prefix as the filename we're looking for */ | |
11d9f6b3 PK |
146 | if (git_vector_bsearch2(&homing, entries, &homing_search_cmp, &ksearch) < 0) |
147 | return GIT_ENOTFOUND; | |
28c1451a VM |
148 | |
149 | /* We found a common prefix. Look forward as long as | |
150 | * there are entries that share the common prefix */ | |
11d9f6b3 | 151 | for (i = homing; i < entries->length; ++i) { |
28c1451a VM |
152 | entry = entries->contents[i]; |
153 | ||
181bbf14 | 154 | if (homing_search_cmp(&ksearch, entry) < 0) |
28c1451a VM |
155 | break; |
156 | ||
0e2fcca8 | 157 | if (entry->filename_len == filename_len && |
11d9f6b3 PK |
158 | memcmp(filename, entry->filename, filename_len) == 0) { |
159 | if (at_pos) | |
160 | *at_pos = i; | |
161 | ||
162 | return 0; | |
163 | } | |
8cf2de07 | 164 | } |
e6629d83 | 165 | |
28c1451a VM |
166 | /* If we haven't found our filename yet, look backwards |
167 | * too as long as we have entries with the same prefix */ | |
11d9f6b3 PK |
168 | if (homing > 0) { |
169 | i = homing - 1; | |
e6629d83 | 170 | |
11d9f6b3 PK |
171 | do { |
172 | entry = entries->contents[i]; | |
e6629d83 | 173 | |
11d9f6b3 PK |
174 | if (homing_search_cmp(&ksearch, entry) > 0) |
175 | break; | |
176 | ||
177 | if (entry->filename_len == filename_len && | |
178 | memcmp(filename, entry->filename, filename_len) == 0) { | |
179 | if (at_pos) | |
180 | *at_pos = i; | |
181 | ||
182 | return 0; | |
183 | } | |
184 | } while (i-- > 0); | |
28c1451a VM |
185 | } |
186 | ||
187 | /* The filename doesn't exist at all */ | |
904b67e6 | 188 | return GIT_ENOTFOUND; |
e6629d83 VM |
189 | } |
190 | ||
0e2fcca8 VM |
191 | void git_tree_entry_free(git_tree_entry *entry) |
192 | { | |
1c3edb30 | 193 | if (entry == NULL) |
194 | return; | |
195 | ||
0e2fcca8 VM |
196 | git__free(entry); |
197 | } | |
198 | ||
46ea40d9 | 199 | git_tree_entry *git_tree_entry_dup(const git_tree_entry *entry) |
0e2fcca8 VM |
200 | { |
201 | size_t total_size; | |
202 | git_tree_entry *copy; | |
203 | ||
204 | assert(entry); | |
205 | ||
206 | total_size = sizeof(git_tree_entry) + entry->filename_len + 1; | |
207 | ||
208 | copy = git__malloc(total_size); | |
209 | if (!copy) | |
210 | return NULL; | |
211 | ||
212 | memcpy(copy, entry, total_size); | |
e120123e | 213 | |
0e2fcca8 VM |
214 | return copy; |
215 | } | |
216 | ||
72a3fe42 | 217 | void git_tree__free(git_tree *tree) |
225fe215 | 218 | { |
c4034e63 | 219 | unsigned int i; |
003c2690 | 220 | |
c4034e63 | 221 | for (i = 0; i < tree->entries.length; ++i) { |
0e2fcca8 VM |
222 | git_tree_entry *e = git_vector_get(&tree->entries, i); |
223 | git_tree_entry_free(e); | |
58519018 | 224 | } |
003c2690 | 225 | |
c8f5ff8f | 226 | git_vector_free(&tree->entries); |
3286c408 | 227 | git__free(tree); |
225fe215 VM |
228 | } |
229 | ||
9950d27a | 230 | const git_oid *git_tree_id(const git_tree *t) |
d45b4a9a | 231 | { |
9950d27a RB |
232 | return git_object_id((const git_object *)t); |
233 | } | |
234 | ||
235 | git_repository *git_tree_owner(const git_tree *t) | |
236 | { | |
237 | return git_object_owner((const git_object *)t); | |
225fe215 VM |
238 | } |
239 | ||
9d7ac675 | 240 | git_filemode_t git_tree_entry_filemode(const git_tree_entry *entry) |
003c2690 | 241 | { |
9d7ac675 | 242 | return (git_filemode_t)entry->attr; |
003c2690 VM |
243 | } |
244 | ||
0ad6efa1 | 245 | const char *git_tree_entry_name(const git_tree_entry *entry) |
003c2690 | 246 | { |
c4b5bedc | 247 | assert(entry); |
2a884588 VM |
248 | return entry->filename; |
249 | } | |
003c2690 | 250 | |
0ad6efa1 | 251 | const git_oid *git_tree_entry_id(const git_tree_entry *entry) |
2a884588 | 252 | { |
c4b5bedc | 253 | assert(entry); |
2a884588 | 254 | return &entry->oid; |
003c2690 VM |
255 | } |
256 | ||
ff9a4c13 RG |
257 | git_otype git_tree_entry_type(const git_tree_entry *entry) |
258 | { | |
259 | assert(entry); | |
260 | ||
261 | if (S_ISGITLINK(entry->attr)) | |
262 | return GIT_OBJ_COMMIT; | |
263 | else if (S_ISDIR(entry->attr)) | |
264 | return GIT_OBJ_TREE; | |
265 | else | |
266 | return GIT_OBJ_BLOB; | |
267 | } | |
268 | ||
9d0011fd VM |
269 | int git_tree_entry_to_object( |
270 | git_object **object_out, | |
271 | git_repository *repo, | |
272 | const git_tree_entry *entry) | |
003c2690 | 273 | { |
1795f879 | 274 | assert(entry && object_out); |
72a3fe42 | 275 | return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY); |
122c3405 VM |
276 | } |
277 | ||
16248ee2 RB |
278 | static const git_tree_entry *entry_fromname( |
279 | git_tree *tree, const char *name, size_t name_len) | |
2a884588 | 280 | { |
11d9f6b3 PK |
281 | size_t idx; |
282 | ||
283 | if (tree_key_search(&idx, &tree->entries, name, name_len) < 0) | |
c4034e63 VM |
284 | return NULL; |
285 | ||
286 | return git_vector_get(&tree->entries, idx); | |
2a884588 VM |
287 | } |
288 | ||
16248ee2 RB |
289 | const git_tree_entry *git_tree_entry_byname( |
290 | git_tree *tree, const char *filename) | |
0e2fcca8 VM |
291 | { |
292 | assert(tree && filename); | |
293 | return entry_fromname(tree, filename, strlen(filename)); | |
294 | } | |
295 | ||
16248ee2 RB |
296 | const git_tree_entry *git_tree_entry_byindex( |
297 | git_tree *tree, size_t idx) | |
003c2690 | 298 | { |
c4b5bedc | 299 | assert(tree); |
e5c80097 | 300 | return git_vector_get(&tree->entries, idx); |
003c2690 VM |
301 | } |
302 | ||
16248ee2 RB |
303 | const git_tree_entry *git_tree_entry_byoid( |
304 | const git_tree *tree, const git_oid *oid) | |
2031760c | 305 | { |
16248ee2 RB |
306 | size_t i; |
307 | const git_tree_entry *e; | |
2031760c RB |
308 | |
309 | assert(tree); | |
310 | ||
311 | git_vector_foreach(&tree->entries, i, e) { | |
312 | if (memcmp(&e->oid.id, &oid->id, sizeof(oid->id)) == 0) | |
313 | return e; | |
314 | } | |
315 | ||
316 | return NULL; | |
317 | } | |
318 | ||
9d0011fd | 319 | int git_tree__prefix_position(git_tree *tree, const char *path) |
41a82592 RB |
320 | { |
321 | git_vector *entries = &tree->entries; | |
322 | struct tree_key_search ksearch; | |
16248ee2 | 323 | size_t at_pos; |
41a82592 | 324 | |
91e7d263 RB |
325 | if (!path) |
326 | return 0; | |
327 | ||
41a82592 RB |
328 | ksearch.filename = path; |
329 | ksearch.filename_len = strlen(path); | |
330 | ||
331 | /* Find tree entry with appropriate prefix */ | |
11d9f6b3 | 332 | git_vector_bsearch2(&at_pos, entries, &homing_search_cmp, &ksearch); |
41a82592 RB |
333 | |
334 | for (; at_pos < entries->length; ++at_pos) { | |
335 | const git_tree_entry *entry = entries->contents[at_pos]; | |
336 | if (homing_search_cmp(&ksearch, entry) < 0) | |
337 | break; | |
338 | } | |
339 | ||
340 | for (; at_pos > 0; --at_pos) { | |
341 | const git_tree_entry *entry = entries->contents[at_pos - 1]; | |
342 | if (homing_search_cmp(&ksearch, entry) > 0) | |
343 | break; | |
344 | } | |
345 | ||
16248ee2 | 346 | return (int)at_pos; |
41a82592 RB |
347 | } |
348 | ||
e120123e | 349 | size_t git_tree_entrycount(const git_tree *tree) |
003c2690 | 350 | { |
c4b5bedc | 351 | assert(tree); |
e120123e | 352 | return tree->entries.length; |
003c2690 VM |
353 | } |
354 | ||
5fb98206 JW |
355 | unsigned int git_treebuilder_entrycount(git_treebuilder *bld) |
356 | { | |
357 | assert(bld); | |
93ab370b | 358 | return (unsigned int)bld->entrycount; |
5fb98206 JW |
359 | } |
360 | ||
cfeef7ce | 361 | static int tree_error(const char *str, const char *path) |
d8603ed9 | 362 | { |
cfeef7ce RB |
363 | if (path) |
364 | giterr_set(GITERR_TREE, "%s - %s", str, path); | |
365 | else | |
366 | giterr_set(GITERR_TREE, "%s", str); | |
3aa351ea CMN |
367 | return -1; |
368 | } | |
2a884588 | 369 | |
3aa351ea CMN |
370 | static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buffer_end) |
371 | { | |
372 | if (git_vector_init(&tree->entries, DEFAULT_TREE_SIZE, entry_sort_cmp) < 0) | |
373 | return -1; | |
e4def81a | 374 | |
003c2690 VM |
375 | while (buffer < buffer_end) { |
376 | git_tree_entry *entry; | |
0e2fcca8 | 377 | int attr; |
d8603ed9 | 378 | |
f1c75b94 | 379 | if (git__strtol32(&attr, buffer, &buffer, 8) < 0 || !buffer) |
cfeef7ce | 380 | return tree_error("Failed to parse tree. Can't parse filemode", NULL); |
8e9bfa4c | 381 | |
f1c75b94 CMN |
382 | attr = normalize_filemode(attr); /* make sure to normalize the filemode */ |
383 | ||
3aa351ea | 384 | if (*buffer++ != ' ') |
cfeef7ce | 385 | return tree_error("Failed to parse tree. Object is corrupted", NULL); |
d8603ed9 | 386 | |
3aa351ea | 387 | if (memchr(buffer, 0, buffer_end - buffer) == NULL) |
cfeef7ce | 388 | return tree_error("Failed to parse tree. Object is corrupted", NULL); |
58519018 | 389 | |
0e2fcca8 VM |
390 | /** Allocate the entry and store it in the entries vector */ |
391 | { | |
392 | entry = alloc_entry(buffer); | |
393 | GITERR_CHECK_ALLOC(entry); | |
394 | ||
395 | if (git_vector_insert(&tree->entries, entry) < 0) | |
396 | return -1; | |
397 | ||
398 | entry->attr = attr; | |
399 | } | |
d8603ed9 | 400 | |
2a884588 VM |
401 | while (buffer < buffer_end && *buffer != 0) |
402 | buffer++; | |
003c2690 | 403 | |
2a884588 | 404 | buffer++; |
d8603ed9 | 405 | |
fa48608e | 406 | git_oid_fromraw(&entry->oid, (const unsigned char *)buffer); |
d8603ed9 | 407 | buffer += GIT_OID_RAWSZ; |
d8603ed9 VM |
408 | } |
409 | ||
3aa351ea | 410 | return 0; |
d8603ed9 | 411 | } |
58519018 | 412 | |
72a3fe42 | 413 | int git_tree__parse(git_tree *tree, git_odb_object *obj) |
58519018 | 414 | { |
72a3fe42 VM |
415 | assert(tree); |
416 | return tree_parse_buffer(tree, (char *)obj->raw.data, (char *)obj->raw.data + obj->raw.len); | |
58519018 VM |
417 | } |
418 | ||
a8122b5d | 419 | static size_t find_next_dir(const char *dirname, git_index *index, size_t start) |
8255c69b | 420 | { |
a8122b5d | 421 | size_t dirlen, i, entries = git_index_entrycount(index); |
8255c69b CMN |
422 | |
423 | dirlen = strlen(dirname); | |
424 | for (i = start; i < entries; ++i) { | |
f45d51ff | 425 | const git_index_entry *entry = git_index_get_byindex(index, i); |
8255c69b CMN |
426 | if (strlen(entry->path) < dirlen || |
427 | memcmp(entry->path, dirname, dirlen) || | |
428 | (dirlen > 0 && entry->path[dirlen] != '/')) { | |
429 | break; | |
430 | } | |
431 | } | |
432 | ||
433 | return i; | |
434 | } | |
435 | ||
0e2fcca8 VM |
436 | static int append_entry( |
437 | git_treebuilder *bld, | |
438 | const char *filename, | |
439 | const git_oid *id, | |
a7dbac0b | 440 | git_filemode_t filemode) |
9ef9e8c3 VM |
441 | { |
442 | git_tree_entry *entry; | |
443 | ||
0d778b1a | 444 | if (!valid_entry_name(filename)) |
cfeef7ce | 445 | return tree_error("Failed to insert entry. Invalid name for a tree entry", filename); |
0d778b1a | 446 | |
0e2fcca8 | 447 | entry = alloc_entry(filename); |
3aa351ea | 448 | GITERR_CHECK_ALLOC(entry); |
9ef9e8c3 | 449 | |
9ef9e8c3 | 450 | git_oid_cpy(&entry->oid, id); |
a7dbac0b | 451 | entry->attr = (uint16_t)filemode; |
9ef9e8c3 VM |
452 | |
453 | if (git_vector_insert(&bld->entries, entry) < 0) | |
3aa351ea | 454 | return -1; |
9ef9e8c3 | 455 | |
93ab370b | 456 | bld->entrycount++; |
3aa351ea | 457 | return 0; |
9ef9e8c3 VM |
458 | } |
459 | ||
9462c471 VM |
460 | static int write_tree( |
461 | git_oid *oid, | |
462 | git_repository *repo, | |
463 | git_index *index, | |
464 | const char *dirname, | |
a8122b5d | 465 | size_t start) |
47d8ec56 | 466 | { |
4a619797 | 467 | git_treebuilder *bld = NULL; |
a8122b5d | 468 | size_t i, entries = git_index_entrycount(index); |
4a619797 CMN |
469 | int error; |
470 | size_t dirname_len = strlen(dirname); | |
8255c69b CMN |
471 | const git_tree_cache *cache; |
472 | ||
473 | cache = git_tree_cache_get(index->tree, dirname); | |
474 | if (cache != NULL && cache->entries >= 0){ | |
475 | git_oid_cpy(oid, &cache->oid); | |
a8122b5d | 476 | return (int)find_next_dir(dirname, index, start); |
8255c69b | 477 | } |
29e1789b | 478 | |
a8122b5d | 479 | if ((error = git_treebuilder_create(&bld, NULL)) < 0 || bld == NULL) |
3aa351ea | 480 | return -1; |
932d1baf | 481 | |
4a619797 CMN |
482 | /* |
483 | * This loop is unfortunate, but necessary. The index doesn't have | |
484 | * any directores, so we need to handle that manually, and we | |
485 | * need to keep track of the current position. | |
486 | */ | |
487 | for (i = start; i < entries; ++i) { | |
f45d51ff | 488 | const git_index_entry *entry = git_index_get_byindex(index, i); |
16248ee2 | 489 | const char *filename, *next_slash; |
4a619797 CMN |
490 | |
491 | /* | |
492 | * If we've left our (sub)tree, exit the loop and return. The | |
493 | * first check is an early out (and security for the | |
494 | * third). The second check is a simple prefix comparison. The | |
495 | * third check catches situations where there is a directory | |
496 | * win32/sys and a file win32mmap.c. Without it, the following | |
497 | * code believes there is a file win32/mmap.c | |
498 | */ | |
499 | if (strlen(entry->path) < dirname_len || | |
500 | memcmp(entry->path, dirname, dirname_len) || | |
501 | (dirname_len > 0 && entry->path[dirname_len] != '/')) { | |
47d8ec56 | 502 | break; |
4a619797 | 503 | } |
932d1baf | 504 | |
4a619797 CMN |
505 | filename = entry->path + dirname_len; |
506 | if (*filename == '/') | |
507 | filename++; | |
508 | next_slash = strchr(filename, '/'); | |
509 | if (next_slash) { | |
510 | git_oid sub_oid; | |
511 | int written; | |
512 | char *subdir, *last_comp; | |
513 | ||
514 | subdir = git__strndup(entry->path, next_slash - entry->path); | |
3aa351ea | 515 | GITERR_CHECK_ALLOC(subdir); |
29e1789b | 516 | |
4a619797 | 517 | /* Write out the subtree */ |
9462c471 | 518 | written = write_tree(&sub_oid, repo, index, subdir, i); |
4a619797 | 519 | if (written < 0) { |
cfeef7ce RB |
520 | tree_error("Failed to write subtree", subdir); |
521 | git__free(subdir); | |
3aa351ea | 522 | goto on_error; |
4a619797 CMN |
523 | } else { |
524 | i = written - 1; /* -1 because of the loop increment */ | |
29e1789b | 525 | } |
932d1baf | 526 | |
4a619797 CMN |
527 | /* |
528 | * We need to figure out what we want toinsert | |
529 | * into this tree. If we're traversing | |
530 | * deps/zlib/, then we only want to write | |
531 | * 'zlib' into the tree. | |
532 | */ | |
533 | last_comp = strrchr(subdir, '/'); | |
534 | if (last_comp) { | |
535 | last_comp++; /* Get rid of the '/' */ | |
536 | } else { | |
537 | last_comp = subdir; | |
538 | } | |
cfeef7ce | 539 | |
9ef9e8c3 | 540 | error = append_entry(bld, last_comp, &sub_oid, S_IFDIR); |
3286c408 | 541 | git__free(subdir); |
cfeef7ce | 542 | if (error < 0) |
3aa351ea | 543 | goto on_error; |
4a619797 | 544 | } else { |
9ef9e8c3 | 545 | error = append_entry(bld, filename, &entry->oid, entry->mode); |
cfeef7ce | 546 | if (error < 0) |
3aa351ea | 547 | goto on_error; |
47d8ec56 | 548 | } |
29e1789b | 549 | } |
932d1baf | 550 | |
3aa351ea CMN |
551 | if (git_treebuilder_write(oid, repo, bld) < 0) |
552 | goto on_error; | |
29e1789b | 553 | |
4a619797 | 554 | git_treebuilder_free(bld); |
a8122b5d | 555 | return (int)i; |
4a619797 | 556 | |
3aa351ea CMN |
557 | on_error: |
558 | git_treebuilder_free(bld); | |
559 | return -1; | |
29e1789b VM |
560 | } |
561 | ||
16248ee2 RB |
562 | int git_tree__write_index( |
563 | git_oid *oid, git_index *index, git_repository *repo) | |
29e1789b | 564 | { |
3aa351ea | 565 | int ret; |
29e1789b | 566 | |
276ea401 | 567 | assert(oid && index && repo); |
47d8ec56 | 568 | |
f92bcaea | 569 | if (git_index_has_conflicts(index)) { |
570 | giterr_set(GITERR_INDEX, | |
571 | "Cannot create a tree from a not fully merged index."); | |
572 | return GIT_EUNMERGED; | |
573 | } | |
574 | ||
8255c69b CMN |
575 | if (index->tree != NULL && index->tree->entries >= 0) { |
576 | git_oid_cpy(oid, &index->tree->oid); | |
3aa351ea | 577 | return 0; |
8255c69b CMN |
578 | } |
579 | ||
4a619797 | 580 | /* The tree cache didn't help us */ |
3aa351ea CMN |
581 | ret = write_tree(oid, repo, index, "", 0); |
582 | return ret < 0 ? ret : 0; | |
47d8ec56 | 583 | } |
0ad6efa1 VM |
584 | |
585 | static void sort_entries(git_treebuilder *bld) | |
586 | { | |
587 | git_vector_sort(&bld->entries); | |
588 | } | |
589 | ||
590 | int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source) | |
591 | { | |
592 | git_treebuilder *bld; | |
b8457baa | 593 | size_t i, source_entries = DEFAULT_TREE_SIZE; |
0ad6efa1 VM |
594 | |
595 | assert(builder_p); | |
596 | ||
597 | bld = git__calloc(1, sizeof(git_treebuilder)); | |
3aa351ea | 598 | GITERR_CHECK_ALLOC(bld); |
0ad6efa1 VM |
599 | |
600 | if (source != NULL) | |
601 | source_entries = source->entries.length; | |
602 | ||
3aa351ea CMN |
603 | if (git_vector_init(&bld->entries, source_entries, entry_sort_cmp) < 0) |
604 | goto on_error; | |
0ad6efa1 VM |
605 | |
606 | if (source != NULL) { | |
0ad6efa1 VM |
607 | for (i = 0; i < source->entries.length; ++i) { |
608 | git_tree_entry *entry_src = source->entries.contents[i]; | |
0ad6efa1 | 609 | |
66439b0b | 610 | if (append_entry( |
611 | bld, entry_src->filename, | |
612 | &entry_src->oid, | |
f1c75b94 | 613 | entry_src->attr) < 0) |
3aa351ea | 614 | goto on_error; |
0ad6efa1 VM |
615 | } |
616 | } | |
617 | ||
618 | *builder_p = bld; | |
3aa351ea CMN |
619 | return 0; |
620 | ||
621 | on_error: | |
622 | git_treebuilder_free(bld); | |
623 | return -1; | |
0ad6efa1 VM |
624 | } |
625 | ||
0e2fcca8 VM |
626 | int git_treebuilder_insert( |
627 | const git_tree_entry **entry_out, | |
628 | git_treebuilder *bld, | |
629 | const char *filename, | |
630 | const git_oid *id, | |
a7dbac0b | 631 | git_filemode_t filemode) |
0ad6efa1 VM |
632 | { |
633 | git_tree_entry *entry; | |
11d9f6b3 | 634 | size_t pos; |
0ad6efa1 VM |
635 | |
636 | assert(bld && id && filename); | |
637 | ||
a7dbac0b | 638 | if (!valid_filemode(filemode)) |
cfeef7ce | 639 | return tree_error("Failed to insert entry. Invalid filemode for file", filename); |
0ad6efa1 | 640 | |
28c1451a | 641 | if (!valid_entry_name(filename)) |
cfeef7ce | 642 | return tree_error("Failed to insert entry. Invalid name for a tree entry", filename); |
f4ad64c1 | 643 | |
11d9f6b3 | 644 | if (!tree_key_search(&pos, &bld->entries, filename, strlen(filename))) { |
0ad6efa1 | 645 | entry = git_vector_get(&bld->entries, pos); |
93ab370b | 646 | if (entry->removed) { |
0ad6efa1 | 647 | entry->removed = 0; |
93ab370b RB |
648 | bld->entrycount++; |
649 | } | |
0ad6efa1 | 650 | } else { |
0e2fcca8 | 651 | entry = alloc_entry(filename); |
3aa351ea | 652 | GITERR_CHECK_ALLOC(entry); |
0ad6efa1 | 653 | |
0ad6efa1 | 654 | if (git_vector_insert(&bld->entries, entry) < 0) |
3aa351ea | 655 | return -1; |
93ab370b RB |
656 | |
657 | bld->entrycount++; | |
0ad6efa1 VM |
658 | } |
659 | ||
11d9f6b3 PK |
660 | git_oid_cpy(&entry->oid, id); |
661 | entry->attr = filemode; | |
662 | ||
663 | if (entry_out) | |
0ad6efa1 VM |
664 | *entry_out = entry; |
665 | ||
3aa351ea | 666 | return 0; |
0ad6efa1 VM |
667 | } |
668 | ||
0cbbdc26 | 669 | static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename) |
0ad6efa1 | 670 | { |
11d9f6b3 | 671 | size_t idx; |
0ad6efa1 VM |
672 | git_tree_entry *entry; |
673 | ||
674 | assert(bld && filename); | |
675 | ||
11d9f6b3 | 676 | if (tree_key_search(&idx, &bld->entries, filename, strlen(filename)) < 0) |
0ad6efa1 VM |
677 | return NULL; |
678 | ||
679 | entry = git_vector_get(&bld->entries, idx); | |
680 | if (entry->removed) | |
681 | return NULL; | |
682 | ||
683 | return entry; | |
684 | } | |
685 | ||
0cbbdc26 KS |
686 | const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename) |
687 | { | |
688 | return treebuilder_get(bld, filename); | |
689 | } | |
690 | ||
0ad6efa1 VM |
691 | int git_treebuilder_remove(git_treebuilder *bld, const char *filename) |
692 | { | |
0cbbdc26 | 693 | git_tree_entry *remove_ptr = treebuilder_get(bld, filename); |
0ad6efa1 VM |
694 | |
695 | if (remove_ptr == NULL || remove_ptr->removed) | |
cfeef7ce | 696 | return tree_error("Failed to remove entry. File isn't in the tree", filename); |
0ad6efa1 VM |
697 | |
698 | remove_ptr->removed = 1; | |
93ab370b | 699 | bld->entrycount--; |
3aa351ea | 700 | return 0; |
0ad6efa1 VM |
701 | } |
702 | ||
703 | int git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld) | |
704 | { | |
afeecf4f | 705 | unsigned int i; |
afeecf4f | 706 | git_buf tree = GIT_BUF_INIT; |
9462c471 | 707 | git_odb *odb; |
0ad6efa1 VM |
708 | |
709 | assert(bld); | |
710 | ||
711 | sort_entries(bld); | |
712 | ||
afeecf4f VM |
713 | /* Grow the buffer beforehand to an estimated size */ |
714 | git_buf_grow(&tree, bld->entries.length * 72); | |
715 | ||
0ad6efa1 VM |
716 | for (i = 0; i < bld->entries.length; ++i) { |
717 | git_tree_entry *entry = bld->entries.contents[i]; | |
718 | ||
719 | if (entry->removed) | |
720 | continue; | |
721 | ||
afeecf4f VM |
722 | git_buf_printf(&tree, "%o ", entry->attr); |
723 | git_buf_put(&tree, entry->filename, entry->filename_len + 1); | |
724 | git_buf_put(&tree, (char *)entry->oid.id, GIT_OID_RAWSZ); | |
0ad6efa1 VM |
725 | } |
726 | ||
3aa351ea CMN |
727 | if (git_buf_oom(&tree)) |
728 | goto on_error; | |
0ad6efa1 | 729 | |
3aa351ea CMN |
730 | if (git_repository_odb__weakptr(&odb, repo) < 0) |
731 | goto on_error; | |
0ad6efa1 | 732 | |
9462c471 | 733 | |
3aa351ea CMN |
734 | if (git_odb_write(oid, odb, tree.ptr, tree.size, GIT_OBJ_TREE) < 0) |
735 | goto on_error; | |
9462c471 | 736 | |
afeecf4f | 737 | git_buf_free(&tree); |
3aa351ea | 738 | return 0; |
0ad6efa1 | 739 | |
3aa351ea CMN |
740 | on_error: |
741 | git_buf_free(&tree); | |
742 | return -1; | |
0ad6efa1 VM |
743 | } |
744 | ||
e120123e RB |
745 | void git_treebuilder_filter( |
746 | git_treebuilder *bld, | |
747 | git_treebuilder_filter_cb filter, | |
748 | void *payload) | |
0ad6efa1 | 749 | { |
c5d8745f | 750 | unsigned int i; |
0ad6efa1 VM |
751 | |
752 | assert(bld && filter); | |
753 | ||
754 | for (i = 0; i < bld->entries.length; ++i) { | |
755 | git_tree_entry *entry = bld->entries.contents[i]; | |
93ab370b | 756 | if (!entry->removed && filter(entry, payload)) { |
0ad6efa1 | 757 | entry->removed = 1; |
93ab370b RB |
758 | bld->entrycount--; |
759 | } | |
0ad6efa1 VM |
760 | } |
761 | } | |
762 | ||
763 | void git_treebuilder_clear(git_treebuilder *bld) | |
764 | { | |
c5d8745f | 765 | unsigned int i; |
0ad6efa1 VM |
766 | assert(bld); |
767 | ||
768 | for (i = 0; i < bld->entries.length; ++i) { | |
769 | git_tree_entry *e = bld->entries.contents[i]; | |
0e2fcca8 | 770 | git_tree_entry_free(e); |
0ad6efa1 VM |
771 | } |
772 | ||
773 | git_vector_clear(&bld->entries); | |
93ab370b | 774 | bld->entrycount = 0; |
0ad6efa1 VM |
775 | } |
776 | ||
777 | void git_treebuilder_free(git_treebuilder *bld) | |
778 | { | |
b0b3b4e3 | 779 | if (bld == NULL) |
780 | return; | |
781 | ||
0ad6efa1 VM |
782 | git_treebuilder_clear(bld); |
783 | git_vector_free(&bld->entries); | |
3286c408 | 784 | git__free(bld); |
0ad6efa1 VM |
785 | } |
786 | ||
0e2fcca8 VM |
787 | static size_t subpath_len(const char *path) |
788 | { | |
789 | const char *slash_pos = strchr(path, '/'); | |
790 | if (slash_pos == NULL) | |
791 | return strlen(path); | |
792 | ||
793 | return slash_pos - path; | |
794 | } | |
795 | ||
796 | int git_tree_entry_bypath( | |
797 | git_tree_entry **entry_out, | |
9432af36 | 798 | git_tree *root, |
0e2fcca8 | 799 | const char *path) |
3fa735ca | 800 | { |
e172cf08 | 801 | int error = 0; |
3fa735ca | 802 | git_tree *subtree; |
0e2fcca8 VM |
803 | const git_tree_entry *entry; |
804 | size_t filename_len; | |
3fa735ca | 805 | |
0e2fcca8 VM |
806 | /* Find how long is the current path component (i.e. |
807 | * the filename between two slashes */ | |
808 | filename_len = subpath_len(path); | |
3fa735ca | 809 | |
0e2fcca8 VM |
810 | if (filename_len == 0) { |
811 | giterr_set(GITERR_TREE, "Invalid tree path given"); | |
812 | return GIT_ENOTFOUND; | |
3aa351ea | 813 | } |
3fa735ca | 814 | |
0e2fcca8 | 815 | entry = entry_fromname(root, path, filename_len); |
3fa735ca | 816 | |
3aa351ea CMN |
817 | if (entry == NULL) { |
818 | giterr_set(GITERR_TREE, | |
0e2fcca8 | 819 | "The path '%s' does not exist in the given tree", path); |
904b67e6 | 820 | return GIT_ENOTFOUND; |
3aa351ea | 821 | } |
0ad6efa1 | 822 | |
0e2fcca8 | 823 | switch (path[filename_len]) { |
2031760c | 824 | case '/': |
0e2fcca8 VM |
825 | /* If there are more components in the path... |
826 | * then this entry *must* be a tree */ | |
827 | if (!git_tree_entry__is_tree(entry)) { | |
828 | giterr_set(GITERR_TREE, | |
829 | "The path '%s' does not exist in the given tree", path); | |
dc1f4b32 | 830 | return GIT_ENOTFOUND; |
0e2fcca8 VM |
831 | } |
832 | ||
833 | /* If there's only a slash left in the path, we | |
834 | * return the current entry; otherwise, we keep | |
835 | * walking down the path */ | |
836 | if (path[filename_len + 1] != '\0') | |
837 | break; | |
838 | ||
839 | case '\0': | |
840 | /* If there are no more components in the path, return | |
841 | * this entry */ | |
46ea40d9 | 842 | *entry_out = git_tree_entry_dup(entry); |
0e2fcca8 VM |
843 | return 0; |
844 | } | |
9432af36 | 845 | |
3aa351ea | 846 | if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0) |
0e2fcca8 | 847 | return -1; |
3fa735ca | 848 | |
0e2fcca8 VM |
849 | error = git_tree_entry_bypath( |
850 | entry_out, | |
9432af36 | 851 | subtree, |
0e2fcca8 | 852 | path + filename_len + 1 |
9432af36 | 853 | ); |
3fa735ca | 854 | |
45e79e37 | 855 | git_tree_free(subtree); |
3fa735ca | 856 | return error; |
857 | } | |
858 | ||
c6f42953 | 859 | static int tree_walk( |
f45d51ff | 860 | const git_tree *tree, |
9432af36 | 861 | git_treewalk_cb callback, |
97769280 | 862 | git_buf *path, |
c6f42953 MS |
863 | void *payload, |
864 | bool preorder) | |
da37654d | 865 | { |
e172cf08 | 866 | int error = 0; |
16248ee2 | 867 | size_t i; |
da37654d VM |
868 | |
869 | for (i = 0; i < tree->entries.length; ++i) { | |
16248ee2 | 870 | const git_tree_entry *entry = tree->entries.contents[i]; |
da37654d | 871 | |
a6bf1687 CMN |
872 | if (preorder) { |
873 | error = callback(path->ptr, entry, payload); | |
874 | if (error > 0) | |
875 | continue; | |
876 | if (error < 0) | |
877 | return GIT_EUSER; | |
878 | } | |
da37654d | 879 | |
9d0011fd | 880 | if (git_tree_entry__is_tree(entry)) { |
da37654d | 881 | git_tree *subtree; |
fa6420f7 | 882 | size_t path_len = git_buf_len(path); |
da37654d | 883 | |
9432af36 VM |
884 | if ((error = git_tree_lookup( |
885 | &subtree, tree->object.repo, &entry->oid)) < 0) | |
97769280 | 886 | break; |
da37654d | 887 | |
97769280 RB |
888 | /* append the next entry to the path */ |
889 | git_buf_puts(path, entry->filename); | |
890 | git_buf_putc(path, '/'); | |
da37654d | 891 | |
cb8a7961 | 892 | if (git_buf_oom(path)) |
3aa351ea | 893 | return -1; |
da37654d | 894 | |
2031760c RB |
895 | error = tree_walk(subtree, callback, path, payload, preorder); |
896 | if (error != 0) | |
897 | break; | |
da37654d | 898 | |
97769280 | 899 | git_buf_truncate(path, path_len); |
45e79e37 | 900 | git_tree_free(subtree); |
da37654d | 901 | } |
c6f42953 | 902 | |
a6bf1687 | 903 | if (!preorder && callback(path->ptr, entry, payload) < 0) { |
b0d37669 | 904 | error = GIT_EUSER; |
2031760c | 905 | break; |
b0d37669 | 906 | } |
da37654d VM |
907 | } |
908 | ||
2031760c | 909 | return error; |
da37654d VM |
910 | } |
911 | ||
e120123e | 912 | int git_tree_walk( |
f45d51ff | 913 | const git_tree *tree, |
e120123e RB |
914 | git_treewalk_mode mode, |
915 | git_treewalk_cb callback, | |
916 | void *payload) | |
da37654d | 917 | { |
e172cf08 | 918 | int error = 0; |
97769280 | 919 | git_buf root_path = GIT_BUF_INIT; |
da37654d | 920 | |
da37654d | 921 | switch (mode) { |
e120123e RB |
922 | case GIT_TREEWALK_POST: |
923 | error = tree_walk(tree, callback, &root_path, payload, false); | |
924 | break; | |
da37654d | 925 | |
e120123e RB |
926 | case GIT_TREEWALK_PRE: |
927 | error = tree_walk(tree, callback, &root_path, payload, true); | |
928 | break; | |
da37654d | 929 | |
e120123e RB |
930 | default: |
931 | giterr_set(GITERR_INVALID, "Invalid walking mode for tree walk"); | |
932 | return -1; | |
da37654d | 933 | } |
97769280 RB |
934 | |
935 | git_buf_free(&root_path); | |
936 | ||
937 | return error; | |
da37654d | 938 | } |
a1fdea28 | 939 |