]> git.proxmox.com Git - libgit2.git/blob - src/tree.c
Merge pull request #3701 from jfultz/fix-0.24.0-changelog
[libgit2.git] / src / tree.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #include "common.h"
9 #include "commit.h"
10 #include "tree.h"
11 #include "git2/repository.h"
12 #include "git2/object.h"
13 #include "fileops.h"
14 #include "tree-cache.h"
15 #include "index.h"
16
17 #define DEFAULT_TREE_SIZE 16
18 #define MAX_FILEMODE_BYTES 6
19
20 #define TREE_ENTRY_CHECK_NAMELEN(n) \
21 if (n > UINT16_MAX) { giterr_set(GITERR_INVALID, "tree entry path too long"); }
22
23 GIT__USE_STRMAP
24
25 static bool valid_filemode(const int filemode)
26 {
27 return (filemode == GIT_FILEMODE_TREE
28 || filemode == GIT_FILEMODE_BLOB
29 || filemode == GIT_FILEMODE_BLOB_EXECUTABLE
30 || filemode == GIT_FILEMODE_LINK
31 || filemode == GIT_FILEMODE_COMMIT);
32 }
33
34 GIT_INLINE(git_filemode_t) normalize_filemode(git_filemode_t filemode)
35 {
36 /* Tree bits set, but it's not a commit */
37 if (GIT_MODE_TYPE(filemode) == GIT_FILEMODE_TREE)
38 return GIT_FILEMODE_TREE;
39
40 /* If any of the x bits are set */
41 if (GIT_PERMS_IS_EXEC(filemode))
42 return GIT_FILEMODE_BLOB_EXECUTABLE;
43
44 /* 16XXXX means commit */
45 if (GIT_MODE_TYPE(filemode) == GIT_FILEMODE_COMMIT)
46 return GIT_FILEMODE_COMMIT;
47
48 /* 12XXXX means commit */
49 if (GIT_MODE_TYPE(filemode) == GIT_FILEMODE_LINK)
50 return GIT_FILEMODE_LINK;
51
52 /* Otherwise, return a blob */
53 return GIT_FILEMODE_BLOB;
54 }
55
56 static int valid_entry_name(git_repository *repo, const char *filename)
57 {
58 return *filename != '\0' &&
59 git_path_isvalid(repo, filename,
60 GIT_PATH_REJECT_TRAVERSAL | GIT_PATH_REJECT_DOT_GIT | GIT_PATH_REJECT_SLASH);
61 }
62
63 static int entry_sort_cmp(const void *a, const void *b)
64 {
65 const git_tree_entry *e1 = (const git_tree_entry *)a;
66 const git_tree_entry *e2 = (const git_tree_entry *)b;
67
68 return git_path_cmp(
69 e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
70 e2->filename, e2->filename_len, git_tree_entry__is_tree(e2),
71 git__strncmp);
72 }
73
74 int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2)
75 {
76 return entry_sort_cmp(e1, e2);
77 }
78
79 int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2)
80 {
81 return git_path_cmp(
82 e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
83 e2->filename, e2->filename_len, git_tree_entry__is_tree(e2),
84 git__strncasecmp);
85 }
86
87 /**
88 * Allocate a new self-contained entry, with enough space after it to
89 * store the filename and the id.
90 */
91 static git_tree_entry *alloc_entry(const char *filename, size_t filename_len, const git_oid *id)
92 {
93 git_tree_entry *entry = NULL;
94 size_t tree_len;
95
96 TREE_ENTRY_CHECK_NAMELEN(filename_len);
97
98 if (GIT_ADD_SIZET_OVERFLOW(&tree_len, sizeof(git_tree_entry), filename_len) ||
99 GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1) ||
100 GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, GIT_OID_RAWSZ))
101 return NULL;
102
103 entry = git__calloc(1, tree_len);
104 if (!entry)
105 return NULL;
106
107 {
108 char *filename_ptr;
109 void *id_ptr;
110
111 filename_ptr = ((char *) entry) + sizeof(git_tree_entry);
112 memcpy(filename_ptr, filename, filename_len);
113 entry->filename = filename_ptr;
114
115 id_ptr = filename_ptr + filename_len + 1;
116 git_oid_cpy(id_ptr, id);
117 entry->oid = id_ptr;
118 }
119
120 entry->filename_len = (uint16_t)filename_len;
121
122 return entry;
123 }
124
125 struct tree_key_search {
126 const char *filename;
127 uint16_t filename_len;
128 };
129
130 static int homing_search_cmp(const void *key, const void *array_member)
131 {
132 const struct tree_key_search *ksearch = key;
133 const git_tree_entry *entry = array_member;
134
135 const uint16_t len1 = ksearch->filename_len;
136 const uint16_t len2 = entry->filename_len;
137
138 return memcmp(
139 ksearch->filename,
140 entry->filename,
141 len1 < len2 ? len1 : len2
142 );
143 }
144
145 /*
146 * Search for an entry in a given tree.
147 *
148 * Note that this search is performed in two steps because
149 * of the way tree entries are sorted internally in git:
150 *
151 * Entries in a tree are not sorted alphabetically; two entries
152 * with the same root prefix will have different positions
153 * depending on whether they are folders (subtrees) or normal files.
154 *
155 * Consequently, it is not possible to find an entry on the tree
156 * with a binary search if you don't know whether the filename
157 * you're looking for is a folder or a normal file.
158 *
159 * To work around this, we first perform a homing binary search
160 * on the tree, using the minimal length root prefix of our filename.
161 * Once the comparisons for this homing search start becoming
162 * ambiguous because of folder vs file sorting, we look linearly
163 * around the area for our target file.
164 */
165 static int tree_key_search(
166 size_t *at_pos, git_vector *entries, const char *filename, size_t filename_len)
167 {
168 struct tree_key_search ksearch;
169 const git_tree_entry *entry;
170 size_t homing, i;
171
172 TREE_ENTRY_CHECK_NAMELEN(filename_len);
173
174 ksearch.filename = filename;
175 ksearch.filename_len = (uint16_t)filename_len;
176
177 /* Initial homing search; find an entry on the tree with
178 * the same prefix as the filename we're looking for */
179 if (git_vector_bsearch2(&homing, entries, &homing_search_cmp, &ksearch) < 0)
180 return GIT_ENOTFOUND; /* just a signal error; not passed back to user */
181
182 /* We found a common prefix. Look forward as long as
183 * there are entries that share the common prefix */
184 for (i = homing; i < entries->length; ++i) {
185 entry = entries->contents[i];
186
187 if (homing_search_cmp(&ksearch, entry) < 0)
188 break;
189
190 if (entry->filename_len == filename_len &&
191 memcmp(filename, entry->filename, filename_len) == 0) {
192 if (at_pos)
193 *at_pos = i;
194
195 return 0;
196 }
197 }
198
199 /* If we haven't found our filename yet, look backwards
200 * too as long as we have entries with the same prefix */
201 if (homing > 0) {
202 i = homing - 1;
203
204 do {
205 entry = entries->contents[i];
206
207 if (homing_search_cmp(&ksearch, entry) > 0)
208 break;
209
210 if (entry->filename_len == filename_len &&
211 memcmp(filename, entry->filename, filename_len) == 0) {
212 if (at_pos)
213 *at_pos = i;
214
215 return 0;
216 }
217 } while (i-- > 0);
218 }
219
220 /* The filename doesn't exist at all */
221 return GIT_ENOTFOUND;
222 }
223
224 void git_tree_entry_free(git_tree_entry *entry)
225 {
226 if (entry == NULL)
227 return;
228
229 git__free(entry);
230 }
231
232 int git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source)
233 {
234 git_tree_entry *cpy;
235
236 assert(source);
237
238 cpy = alloc_entry(source->filename, source->filename_len, source->oid);
239 if (cpy == NULL)
240 return -1;
241
242 cpy->attr = source->attr;
243
244 *dest = cpy;
245 return 0;
246 }
247
248 void git_tree__free(void *_tree)
249 {
250 git_tree *tree = _tree;
251
252 git_odb_object_free(tree->odb_obj);
253 git_vector_free(&tree->entries);
254 git_array_clear(tree->entries_arr);
255 git__free(tree);
256 }
257
258 git_filemode_t git_tree_entry_filemode(const git_tree_entry *entry)
259 {
260 return normalize_filemode(entry->attr);
261 }
262
263 git_filemode_t git_tree_entry_filemode_raw(const git_tree_entry *entry)
264 {
265 return entry->attr;
266 }
267
268 const char *git_tree_entry_name(const git_tree_entry *entry)
269 {
270 assert(entry);
271 return entry->filename;
272 }
273
274 const git_oid *git_tree_entry_id(const git_tree_entry *entry)
275 {
276 assert(entry);
277 return entry->oid;
278 }
279
280 git_otype git_tree_entry_type(const git_tree_entry *entry)
281 {
282 assert(entry);
283
284 if (S_ISGITLINK(entry->attr))
285 return GIT_OBJ_COMMIT;
286 else if (S_ISDIR(entry->attr))
287 return GIT_OBJ_TREE;
288 else
289 return GIT_OBJ_BLOB;
290 }
291
292 int git_tree_entry_to_object(
293 git_object **object_out,
294 git_repository *repo,
295 const git_tree_entry *entry)
296 {
297 assert(entry && object_out);
298 return git_object_lookup(object_out, repo, entry->oid, GIT_OBJ_ANY);
299 }
300
301 static const git_tree_entry *entry_fromname(
302 const git_tree *tree, const char *name, size_t name_len)
303 {
304 size_t idx;
305
306 /* be safe when we cast away constness - i.e. don't trigger a sort */
307 assert(git_vector_is_sorted(&tree->entries));
308
309 if (tree_key_search(&idx, (git_vector *)&tree->entries, name, name_len) < 0)
310 return NULL;
311
312 return git_vector_get(&tree->entries, idx);
313 }
314
315 const git_tree_entry *git_tree_entry_byname(
316 const git_tree *tree, const char *filename)
317 {
318 assert(tree && filename);
319
320 return entry_fromname(tree, filename, strlen(filename));
321 }
322
323 const git_tree_entry *git_tree_entry_byindex(
324 const git_tree *tree, size_t idx)
325 {
326 assert(tree);
327 return git_vector_get(&tree->entries, idx);
328 }
329
330 const git_tree_entry *git_tree_entry_byid(
331 const git_tree *tree, const git_oid *id)
332 {
333 size_t i;
334 const git_tree_entry *e;
335
336 assert(tree);
337
338 git_vector_foreach(&tree->entries, i, e) {
339 if (memcmp(&e->oid->id, &id->id, sizeof(id->id)) == 0)
340 return e;
341 }
342
343 return NULL;
344 }
345
346 int git_tree__prefix_position(const git_tree *tree, const char *path)
347 {
348 const git_vector *entries = &tree->entries;
349 struct tree_key_search ksearch;
350 size_t at_pos, path_len;
351
352 if (!path)
353 return 0;
354
355 path_len = strlen(path);
356 TREE_ENTRY_CHECK_NAMELEN(path_len);
357
358 ksearch.filename = path;
359 ksearch.filename_len = (uint16_t)path_len;
360
361 /* be safe when we cast away constness - i.e. don't trigger a sort */
362 assert(git_vector_is_sorted(&tree->entries));
363
364 /* Find tree entry with appropriate prefix */
365 git_vector_bsearch2(
366 &at_pos, (git_vector *)entries, &homing_search_cmp, &ksearch);
367
368 for (; at_pos < entries->length; ++at_pos) {
369 const git_tree_entry *entry = entries->contents[at_pos];
370 if (homing_search_cmp(&ksearch, entry) < 0)
371 break;
372 }
373
374 for (; at_pos > 0; --at_pos) {
375 const git_tree_entry *entry = entries->contents[at_pos - 1];
376 if (homing_search_cmp(&ksearch, entry) > 0)
377 break;
378 }
379
380 return (int)at_pos;
381 }
382
383 size_t git_tree_entrycount(const git_tree *tree)
384 {
385 assert(tree);
386 return tree->entries.length;
387 }
388
389 unsigned int git_treebuilder_entrycount(git_treebuilder *bld)
390 {
391 assert(bld);
392
393 return git_strmap_num_entries(bld->map);
394 }
395
396 static int tree_error(const char *str, const char *path)
397 {
398 if (path)
399 giterr_set(GITERR_TREE, "%s - %s", str, path);
400 else
401 giterr_set(GITERR_TREE, "%s", str);
402 return -1;
403 }
404
405 static int parse_mode(unsigned int *modep, const char *buffer, const char **buffer_out)
406 {
407 unsigned char c;
408 unsigned int mode = 0;
409
410 if (*buffer == ' ')
411 return -1;
412
413 while ((c = *buffer++) != ' ') {
414 if (c < '0' || c > '7')
415 return -1;
416 mode = (mode << 3) + (c - '0');
417 }
418 *modep = mode;
419 *buffer_out = buffer;
420
421 return 0;
422 }
423
424 int git_tree__parse(void *_tree, git_odb_object *odb_obj)
425 {
426 size_t i;
427 git_tree *tree = _tree;
428 const char *buffer;
429 const char *buffer_end;
430
431 if (git_odb_object_dup(&tree->odb_obj, odb_obj) < 0)
432 return -1;
433
434 buffer = git_odb_object_data(tree->odb_obj);
435 buffer_end = buffer + git_odb_object_size(tree->odb_obj);
436
437 git_array_init_to_size(tree->entries_arr, DEFAULT_TREE_SIZE);
438 GITERR_CHECK_ARRAY(tree->entries_arr);
439
440 while (buffer < buffer_end) {
441 git_tree_entry *entry;
442 size_t filename_len;
443 const char *nul;
444 unsigned int attr;
445
446 if (parse_mode(&attr, buffer, &buffer) < 0 || !buffer)
447 return tree_error("Failed to parse tree. Can't parse filemode", NULL);
448
449 if ((nul = memchr(buffer, 0, buffer_end - buffer)) == NULL)
450 return tree_error("Failed to parse tree. Object is corrupted", NULL);
451
452 filename_len = nul - buffer;
453 /** Allocate the entry and store it in the entries vector */
454 {
455 entry = git_array_alloc(tree->entries_arr);
456 GITERR_CHECK_ALLOC(entry);
457
458 entry->attr = attr;
459 entry->filename_len = filename_len;
460 entry->filename = buffer;
461 entry->oid = (git_oid *) ((char *) buffer + filename_len + 1);
462 }
463
464 buffer += filename_len + 1;
465 buffer += GIT_OID_RAWSZ;
466 }
467
468 /* Add the entries to the vector here, as we may reallocate during the loop */
469 if (git_vector_init(&tree->entries, tree->entries_arr.size, entry_sort_cmp) < 0)
470 return -1;
471
472 for (i = 0; i < tree->entries_arr.size; i++) {
473 if (git_vector_insert(&tree->entries, git_array_get(tree->entries_arr, i)) < 0)
474 return -1;
475 }
476
477 /* The tree is sorted by definition. Bad inputs give bad outputs */
478 tree->entries.flags |= GIT_VECTOR_SORTED;
479
480 return 0;
481 }
482
483 static size_t find_next_dir(const char *dirname, git_index *index, size_t start)
484 {
485 size_t dirlen, i, entries = git_index_entrycount(index);
486
487 dirlen = strlen(dirname);
488 for (i = start; i < entries; ++i) {
489 const git_index_entry *entry = git_index_get_byindex(index, i);
490 if (strlen(entry->path) < dirlen ||
491 memcmp(entry->path, dirname, dirlen) ||
492 (dirlen > 0 && entry->path[dirlen] != '/')) {
493 break;
494 }
495 }
496
497 return i;
498 }
499
500 static int append_entry(
501 git_treebuilder *bld,
502 const char *filename,
503 const git_oid *id,
504 git_filemode_t filemode)
505 {
506 git_tree_entry *entry;
507 int error = 0;
508
509 if (!valid_entry_name(bld->repo, filename))
510 return tree_error("Failed to insert entry. Invalid name for a tree entry", filename);
511
512 entry = alloc_entry(filename, strlen(filename), id);
513 GITERR_CHECK_ALLOC(entry);
514
515 entry->attr = (uint16_t)filemode;
516
517 git_strmap_insert(bld->map, entry->filename, entry, error);
518 if (error < 0) {
519 git_tree_entry_free(entry);
520 giterr_set(GITERR_TREE, "failed to append entry %s to the tree builder", filename);
521 return -1;
522 }
523
524 return 0;
525 }
526
527 static int write_tree(
528 git_oid *oid,
529 git_repository *repo,
530 git_index *index,
531 const char *dirname,
532 size_t start)
533 {
534 git_treebuilder *bld = NULL;
535 size_t i, entries = git_index_entrycount(index);
536 int error;
537 size_t dirname_len = strlen(dirname);
538 const git_tree_cache *cache;
539
540 cache = git_tree_cache_get(index->tree, dirname);
541 if (cache != NULL && cache->entry_count >= 0){
542 git_oid_cpy(oid, &cache->oid);
543 return (int)find_next_dir(dirname, index, start);
544 }
545
546 if ((error = git_treebuilder_new(&bld, repo, NULL)) < 0 || bld == NULL)
547 return -1;
548
549 /*
550 * This loop is unfortunate, but necessary. The index doesn't have
551 * any directores, so we need to handle that manually, and we
552 * need to keep track of the current position.
553 */
554 for (i = start; i < entries; ++i) {
555 const git_index_entry *entry = git_index_get_byindex(index, i);
556 const char *filename, *next_slash;
557
558 /*
559 * If we've left our (sub)tree, exit the loop and return. The
560 * first check is an early out (and security for the
561 * third). The second check is a simple prefix comparison. The
562 * third check catches situations where there is a directory
563 * win32/sys and a file win32mmap.c. Without it, the following
564 * code believes there is a file win32/mmap.c
565 */
566 if (strlen(entry->path) < dirname_len ||
567 memcmp(entry->path, dirname, dirname_len) ||
568 (dirname_len > 0 && entry->path[dirname_len] != '/')) {
569 break;
570 }
571
572 filename = entry->path + dirname_len;
573 if (*filename == '/')
574 filename++;
575 next_slash = strchr(filename, '/');
576 if (next_slash) {
577 git_oid sub_oid;
578 int written;
579 char *subdir, *last_comp;
580
581 subdir = git__strndup(entry->path, next_slash - entry->path);
582 GITERR_CHECK_ALLOC(subdir);
583
584 /* Write out the subtree */
585 written = write_tree(&sub_oid, repo, index, subdir, i);
586 if (written < 0) {
587 git__free(subdir);
588 goto on_error;
589 } else {
590 i = written - 1; /* -1 because of the loop increment */
591 }
592
593 /*
594 * We need to figure out what we want toinsert
595 * into this tree. If we're traversing
596 * deps/zlib/, then we only want to write
597 * 'zlib' into the tree.
598 */
599 last_comp = strrchr(subdir, '/');
600 if (last_comp) {
601 last_comp++; /* Get rid of the '/' */
602 } else {
603 last_comp = subdir;
604 }
605
606 error = append_entry(bld, last_comp, &sub_oid, S_IFDIR);
607 git__free(subdir);
608 if (error < 0)
609 goto on_error;
610 } else {
611 error = append_entry(bld, filename, &entry->id, entry->mode);
612 if (error < 0)
613 goto on_error;
614 }
615 }
616
617 if (git_treebuilder_write(oid, bld) < 0)
618 goto on_error;
619
620 git_treebuilder_free(bld);
621 return (int)i;
622
623 on_error:
624 git_treebuilder_free(bld);
625 return -1;
626 }
627
628 int git_tree__write_index(
629 git_oid *oid, git_index *index, git_repository *repo)
630 {
631 int ret;
632 git_tree *tree;
633 bool old_ignore_case = false;
634
635 assert(oid && index && repo);
636
637 if (git_index_has_conflicts(index)) {
638 giterr_set(GITERR_INDEX,
639 "Cannot create a tree from a not fully merged index.");
640 return GIT_EUNMERGED;
641 }
642
643 if (index->tree != NULL && index->tree->entry_count >= 0) {
644 git_oid_cpy(oid, &index->tree->oid);
645 return 0;
646 }
647
648 /* The tree cache didn't help us; we'll have to write
649 * out a tree. If the index is ignore_case, we must
650 * make it case-sensitive for the duration of the tree-write
651 * operation. */
652
653 if (index->ignore_case) {
654 old_ignore_case = true;
655 git_index__set_ignore_case(index, false);
656 }
657
658 ret = write_tree(oid, repo, index, "", 0);
659
660 if (old_ignore_case)
661 git_index__set_ignore_case(index, true);
662
663 index->tree = NULL;
664
665 if (ret < 0)
666 return ret;
667
668 git_pool_clear(&index->tree_pool);
669
670 if ((ret = git_tree_lookup(&tree, repo, oid)) < 0)
671 return ret;
672
673 /* Read the tree cache into the index */
674 ret = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool);
675 git_tree_free(tree);
676
677 return ret;
678 }
679
680 int git_treebuilder_new(
681 git_treebuilder **builder_p,
682 git_repository *repo,
683 const git_tree *source)
684 {
685 git_treebuilder *bld;
686 size_t i;
687
688 assert(builder_p && repo);
689
690 bld = git__calloc(1, sizeof(git_treebuilder));
691 GITERR_CHECK_ALLOC(bld);
692
693 bld->repo = repo;
694
695 if (git_strmap_alloc(&bld->map) < 0) {
696 git__free(bld);
697 return -1;
698 }
699
700 if (source != NULL) {
701 git_tree_entry *entry_src;
702
703 git_vector_foreach(&source->entries, i, entry_src) {
704 if (append_entry(
705 bld, entry_src->filename,
706 entry_src->oid,
707 entry_src->attr) < 0)
708 goto on_error;
709 }
710 }
711
712 *builder_p = bld;
713 return 0;
714
715 on_error:
716 git_treebuilder_free(bld);
717 return -1;
718 }
719
720 static git_otype otype_from_mode(git_filemode_t filemode)
721 {
722 switch (filemode) {
723 case GIT_FILEMODE_TREE:
724 return GIT_OBJ_TREE;
725 case GIT_FILEMODE_COMMIT:
726 return GIT_OBJ_COMMIT;
727 default:
728 return GIT_OBJ_BLOB;
729 }
730 }
731
732 int git_treebuilder_insert(
733 const git_tree_entry **entry_out,
734 git_treebuilder *bld,
735 const char *filename,
736 const git_oid *id,
737 git_filemode_t filemode)
738 {
739 git_tree_entry *entry;
740 int error;
741 git_strmap_iter pos;
742
743 assert(bld && id && filename);
744
745 if (!valid_filemode(filemode))
746 return tree_error("Failed to insert entry. Invalid filemode for file", filename);
747
748 if (!valid_entry_name(bld->repo, filename))
749 return tree_error("Failed to insert entry. Invalid name for a tree entry", filename);
750
751 if (filemode != GIT_FILEMODE_COMMIT &&
752 !git_object__is_valid(bld->repo, id, otype_from_mode(filemode)))
753 return tree_error("Failed to insert entry; invalid object specified", filename);
754
755 pos = git_strmap_lookup_index(bld->map, filename);
756 if (git_strmap_valid_index(bld->map, pos)) {
757 entry = git_strmap_value_at(bld->map, pos);
758 git_oid_cpy((git_oid *) entry->oid, id);
759 } else {
760 entry = alloc_entry(filename, strlen(filename), id);
761 GITERR_CHECK_ALLOC(entry);
762
763 git_strmap_insert(bld->map, entry->filename, entry, error);
764
765 if (error < 0) {
766 git_tree_entry_free(entry);
767 giterr_set(GITERR_TREE, "failed to insert %s", filename);
768 return -1;
769 }
770 }
771
772 entry->attr = filemode;
773
774 if (entry_out)
775 *entry_out = entry;
776
777 return 0;
778 }
779
780 static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename)
781 {
782 git_tree_entry *entry = NULL;
783 git_strmap_iter pos;
784
785 assert(bld && filename);
786
787 pos = git_strmap_lookup_index(bld->map, filename);
788 if (git_strmap_valid_index(bld->map, pos))
789 entry = git_strmap_value_at(bld->map, pos);
790
791 return entry;
792 }
793
794 const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename)
795 {
796 return treebuilder_get(bld, filename);
797 }
798
799 int git_treebuilder_remove(git_treebuilder *bld, const char *filename)
800 {
801 git_tree_entry *entry = treebuilder_get(bld, filename);
802
803 if (entry == NULL)
804 return tree_error("Failed to remove entry. File isn't in the tree", filename);
805
806 git_strmap_delete(bld->map, filename);
807 git_tree_entry_free(entry);
808
809 return 0;
810 }
811
812 int git_treebuilder_write(git_oid *oid, git_treebuilder *bld)
813 {
814 int error = 0;
815 size_t i, entrycount;
816 git_buf tree = GIT_BUF_INIT;
817 git_odb *odb;
818 git_tree_entry *entry;
819 git_vector entries;
820
821 assert(bld);
822
823 entrycount = git_strmap_num_entries(bld->map);
824 if (git_vector_init(&entries, entrycount, entry_sort_cmp) < 0)
825 return -1;
826
827 git_strmap_foreach_value(bld->map, entry, {
828 if (git_vector_insert(&entries, entry) < 0)
829 return -1;
830 });
831
832 git_vector_sort(&entries);
833
834 /* Grow the buffer beforehand to an estimated size */
835 error = git_buf_grow(&tree, entrycount * 72);
836
837 for (i = 0; i < entries.length && !error; ++i) {
838 git_tree_entry *entry = git_vector_get(&entries, i);
839
840 git_buf_printf(&tree, "%o ", entry->attr);
841 git_buf_put(&tree, entry->filename, entry->filename_len + 1);
842 git_buf_put(&tree, (char *)entry->oid->id, GIT_OID_RAWSZ);
843
844 if (git_buf_oom(&tree))
845 error = -1;
846 }
847
848 git_vector_free(&entries);
849
850 if (!error &&
851 !(error = git_repository_odb__weakptr(&odb, bld->repo)))
852 error = git_odb_write(oid, odb, tree.ptr, tree.size, GIT_OBJ_TREE);
853
854 git_buf_free(&tree);
855 return error;
856 }
857
858 void git_treebuilder_filter(
859 git_treebuilder *bld,
860 git_treebuilder_filter_cb filter,
861 void *payload)
862 {
863 const char *filename;
864 git_tree_entry *entry;
865
866 assert(bld && filter);
867
868 git_strmap_foreach(bld->map, filename, entry, {
869 if (filter(entry, payload)) {
870 git_strmap_delete(bld->map, filename);
871 git_tree_entry_free(entry);
872 }
873 });
874 }
875
876 void git_treebuilder_clear(git_treebuilder *bld)
877 {
878 git_tree_entry *e;
879
880 assert(bld);
881
882 git_strmap_foreach_value(bld->map, e, git_tree_entry_free(e));
883 git_strmap_clear(bld->map);
884 }
885
886 void git_treebuilder_free(git_treebuilder *bld)
887 {
888 if (bld == NULL)
889 return;
890
891 git_treebuilder_clear(bld);
892 git_strmap_free(bld->map);
893 git__free(bld);
894 }
895
896 static size_t subpath_len(const char *path)
897 {
898 const char *slash_pos = strchr(path, '/');
899 if (slash_pos == NULL)
900 return strlen(path);
901
902 return slash_pos - path;
903 }
904
905 int git_tree_entry_bypath(
906 git_tree_entry **entry_out,
907 const git_tree *root,
908 const char *path)
909 {
910 int error = 0;
911 git_tree *subtree;
912 const git_tree_entry *entry;
913 size_t filename_len;
914
915 /* Find how long is the current path component (i.e.
916 * the filename between two slashes */
917 filename_len = subpath_len(path);
918
919 if (filename_len == 0) {
920 giterr_set(GITERR_TREE, "Invalid tree path given");
921 return GIT_ENOTFOUND;
922 }
923
924 entry = entry_fromname(root, path, filename_len);
925
926 if (entry == NULL) {
927 giterr_set(GITERR_TREE,
928 "the path '%.*s' does not exist in the given tree", filename_len, path);
929 return GIT_ENOTFOUND;
930 }
931
932 switch (path[filename_len]) {
933 case '/':
934 /* If there are more components in the path...
935 * then this entry *must* be a tree */
936 if (!git_tree_entry__is_tree(entry)) {
937 giterr_set(GITERR_TREE,
938 "the path '%.*s' exists but is not a tree", filename_len, path);
939 return GIT_ENOTFOUND;
940 }
941
942 /* If there's only a slash left in the path, we
943 * return the current entry; otherwise, we keep
944 * walking down the path */
945 if (path[filename_len + 1] != '\0')
946 break;
947
948 case '\0':
949 /* If there are no more components in the path, return
950 * this entry */
951 return git_tree_entry_dup(entry_out, entry);
952 }
953
954 if (git_tree_lookup(&subtree, root->object.repo, entry->oid) < 0)
955 return -1;
956
957 error = git_tree_entry_bypath(
958 entry_out,
959 subtree,
960 path + filename_len + 1
961 );
962
963 git_tree_free(subtree);
964 return error;
965 }
966
967 static int tree_walk(
968 const git_tree *tree,
969 git_treewalk_cb callback,
970 git_buf *path,
971 void *payload,
972 bool preorder)
973 {
974 int error = 0;
975 size_t i;
976 const git_tree_entry *entry;
977
978 git_vector_foreach(&tree->entries, i, entry) {
979 if (preorder) {
980 error = callback(path->ptr, entry, payload);
981 if (error < 0) { /* negative value stops iteration */
982 giterr_set_after_callback_function(error, "git_tree_walk");
983 break;
984 }
985 if (error > 0) { /* positive value skips this entry */
986 error = 0;
987 continue;
988 }
989 }
990
991 if (git_tree_entry__is_tree(entry)) {
992 git_tree *subtree;
993 size_t path_len = git_buf_len(path);
994
995 error = git_tree_lookup(&subtree, tree->object.repo, entry->oid);
996 if (error < 0)
997 break;
998
999 /* append the next entry to the path */
1000 git_buf_puts(path, entry->filename);
1001 git_buf_putc(path, '/');
1002
1003 if (git_buf_oom(path))
1004 error = -1;
1005 else
1006 error = tree_walk(subtree, callback, path, payload, preorder);
1007
1008 git_tree_free(subtree);
1009 if (error != 0)
1010 break;
1011
1012 git_buf_truncate(path, path_len);
1013 }
1014
1015 if (!preorder) {
1016 error = callback(path->ptr, entry, payload);
1017 if (error < 0) { /* negative value stops iteration */
1018 giterr_set_after_callback_function(error, "git_tree_walk");
1019 break;
1020 }
1021 error = 0;
1022 }
1023 }
1024
1025 return error;
1026 }
1027
1028 int git_tree_walk(
1029 const git_tree *tree,
1030 git_treewalk_mode mode,
1031 git_treewalk_cb callback,
1032 void *payload)
1033 {
1034 int error = 0;
1035 git_buf root_path = GIT_BUF_INIT;
1036
1037 if (mode != GIT_TREEWALK_POST && mode != GIT_TREEWALK_PRE) {
1038 giterr_set(GITERR_INVALID, "Invalid walking mode for tree walk");
1039 return -1;
1040 }
1041
1042 error = tree_walk(
1043 tree, callback, &root_path, payload, (mode == GIT_TREEWALK_PRE));
1044
1045 git_buf_free(&root_path);
1046
1047 return error;
1048 }
1049