2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
11 #include "git2/repository.h"
12 #include "git2/object.h"
14 #include "tree-cache.h"
17 #define DEFAULT_TREE_SIZE 16
18 #define MAX_FILEMODE_BYTES 6
20 #define TREE_ENTRY_CHECK_NAMELEN(n) \
21 if (n > UINT16_MAX) { giterr_set(GITERR_INVALID, "tree entry path too long"); }
23 static bool valid_filemode(const int filemode
)
25 return (filemode
== GIT_FILEMODE_TREE
26 || filemode
== GIT_FILEMODE_BLOB
27 || filemode
== GIT_FILEMODE_BLOB_EXECUTABLE
28 || filemode
== GIT_FILEMODE_LINK
29 || filemode
== GIT_FILEMODE_COMMIT
);
32 GIT_INLINE(git_filemode_t
) normalize_filemode(git_filemode_t filemode
)
34 /* Tree bits set, but it's not a commit */
35 if (GIT_MODE_TYPE(filemode
) == GIT_FILEMODE_TREE
)
36 return GIT_FILEMODE_TREE
;
38 /* If any of the x bits are set */
39 if (GIT_PERMS_IS_EXEC(filemode
))
40 return GIT_FILEMODE_BLOB_EXECUTABLE
;
42 /* 16XXXX means commit */
43 if (GIT_MODE_TYPE(filemode
) == GIT_FILEMODE_COMMIT
)
44 return GIT_FILEMODE_COMMIT
;
46 /* 12XXXX means symlink */
47 if (GIT_MODE_TYPE(filemode
) == GIT_FILEMODE_LINK
)
48 return GIT_FILEMODE_LINK
;
50 /* Otherwise, return a blob */
51 return GIT_FILEMODE_BLOB
;
54 static int valid_entry_name(git_repository
*repo
, const char *filename
)
56 return *filename
!= '\0' &&
57 git_path_isvalid(repo
, filename
, 0,
58 GIT_PATH_REJECT_TRAVERSAL
| GIT_PATH_REJECT_DOT_GIT
| GIT_PATH_REJECT_SLASH
);
61 static int entry_sort_cmp(const void *a
, const void *b
)
63 const git_tree_entry
*e1
= (const git_tree_entry
*)a
;
64 const git_tree_entry
*e2
= (const git_tree_entry
*)b
;
67 e1
->filename
, e1
->filename_len
, git_tree_entry__is_tree(e1
),
68 e2
->filename
, e2
->filename_len
, git_tree_entry__is_tree(e2
),
72 int git_tree_entry_cmp(const git_tree_entry
*e1
, const git_tree_entry
*e2
)
74 return entry_sort_cmp(e1
, e2
);
77 int git_tree_entry_icmp(const git_tree_entry
*e1
, const git_tree_entry
*e2
)
80 e1
->filename
, e1
->filename_len
, git_tree_entry__is_tree(e1
),
81 e2
->filename
, e2
->filename_len
, git_tree_entry__is_tree(e2
),
86 * Allocate a new self-contained entry, with enough space after it to
87 * store the filename and the id.
89 static git_tree_entry
*alloc_entry(const char *filename
, size_t filename_len
, const git_oid
*id
)
91 git_tree_entry
*entry
= NULL
;
94 TREE_ENTRY_CHECK_NAMELEN(filename_len
);
96 if (GIT_ADD_SIZET_OVERFLOW(&tree_len
, sizeof(git_tree_entry
), filename_len
) ||
97 GIT_ADD_SIZET_OVERFLOW(&tree_len
, tree_len
, 1) ||
98 GIT_ADD_SIZET_OVERFLOW(&tree_len
, tree_len
, GIT_OID_RAWSZ
))
101 entry
= git__calloc(1, tree_len
);
109 filename_ptr
= ((char *) entry
) + sizeof(git_tree_entry
);
110 memcpy(filename_ptr
, filename
, filename_len
);
111 entry
->filename
= filename_ptr
;
113 id_ptr
= filename_ptr
+ filename_len
+ 1;
114 git_oid_cpy(id_ptr
, id
);
118 entry
->filename_len
= (uint16_t)filename_len
;
123 struct tree_key_search
{
124 const char *filename
;
125 uint16_t filename_len
;
128 static int homing_search_cmp(const void *key
, const void *array_member
)
130 const struct tree_key_search
*ksearch
= key
;
131 const git_tree_entry
*entry
= array_member
;
133 const uint16_t len1
= ksearch
->filename_len
;
134 const uint16_t len2
= entry
->filename_len
;
139 len1
< len2
? len1
: len2
144 * Search for an entry in a given tree.
146 * Note that this search is performed in two steps because
147 * of the way tree entries are sorted internally in git:
149 * Entries in a tree are not sorted alphabetically; two entries
150 * with the same root prefix will have different positions
151 * depending on whether they are folders (subtrees) or normal files.
153 * Consequently, it is not possible to find an entry on the tree
154 * with a binary search if you don't know whether the filename
155 * you're looking for is a folder or a normal file.
157 * To work around this, we first perform a homing binary search
158 * on the tree, using the minimal length root prefix of our filename.
159 * Once the comparisons for this homing search start becoming
160 * ambiguous because of folder vs file sorting, we look linearly
161 * around the area for our target file.
163 static int tree_key_search(
165 const git_tree
*tree
,
166 const char *filename
,
169 struct tree_key_search ksearch
;
170 const git_tree_entry
*entry
;
173 TREE_ENTRY_CHECK_NAMELEN(filename_len
);
175 ksearch
.filename
= filename
;
176 ksearch
.filename_len
= (uint16_t)filename_len
;
178 /* Initial homing search; find an entry on the tree with
179 * the same prefix as the filename we're looking for */
181 if (git_array_search(&homing
,
182 tree
->entries
, &homing_search_cmp
, &ksearch
) < 0)
183 return GIT_ENOTFOUND
; /* just a signal error; not passed back to user */
185 /* We found a common prefix. Look forward as long as
186 * there are entries that share the common prefix */
187 for (i
= homing
; i
< tree
->entries
.size
; ++i
) {
188 entry
= git_array_get(tree
->entries
, i
);
190 if (homing_search_cmp(&ksearch
, entry
) < 0)
193 if (entry
->filename_len
== filename_len
&&
194 memcmp(filename
, entry
->filename
, filename_len
) == 0) {
202 /* If we haven't found our filename yet, look backwards
203 * too as long as we have entries with the same prefix */
208 entry
= git_array_get(tree
->entries
, i
);
210 if (homing_search_cmp(&ksearch
, entry
) > 0)
213 if (entry
->filename_len
== filename_len
&&
214 memcmp(filename
, entry
->filename
, filename_len
) == 0) {
223 /* The filename doesn't exist at all */
224 return GIT_ENOTFOUND
;
227 void git_tree_entry_free(git_tree_entry
*entry
)
235 int git_tree_entry_dup(git_tree_entry
**dest
, const git_tree_entry
*source
)
241 cpy
= alloc_entry(source
->filename
, source
->filename_len
, source
->oid
);
245 cpy
->attr
= source
->attr
;
251 void git_tree__free(void *_tree
)
253 git_tree
*tree
= _tree
;
255 git_odb_object_free(tree
->odb_obj
);
256 git_array_clear(tree
->entries
);
260 git_filemode_t
git_tree_entry_filemode(const git_tree_entry
*entry
)
262 return normalize_filemode(entry
->attr
);
265 git_filemode_t
git_tree_entry_filemode_raw(const git_tree_entry
*entry
)
270 const char *git_tree_entry_name(const git_tree_entry
*entry
)
273 return entry
->filename
;
276 const git_oid
*git_tree_entry_id(const git_tree_entry
*entry
)
282 git_otype
git_tree_entry_type(const git_tree_entry
*entry
)
286 if (S_ISGITLINK(entry
->attr
))
287 return GIT_OBJ_COMMIT
;
288 else if (S_ISDIR(entry
->attr
))
294 int git_tree_entry_to_object(
295 git_object
**object_out
,
296 git_repository
*repo
,
297 const git_tree_entry
*entry
)
299 assert(entry
&& object_out
);
300 return git_object_lookup(object_out
, repo
, entry
->oid
, GIT_OBJ_ANY
);
303 static const git_tree_entry
*entry_fromname(
304 const git_tree
*tree
, const char *name
, size_t name_len
)
308 if (tree_key_search(&idx
, tree
, name
, name_len
) < 0)
311 return git_array_get(tree
->entries
, idx
);
314 const git_tree_entry
*git_tree_entry_byname(
315 const git_tree
*tree
, const char *filename
)
317 assert(tree
&& filename
);
319 return entry_fromname(tree
, filename
, strlen(filename
));
322 const git_tree_entry
*git_tree_entry_byindex(
323 const git_tree
*tree
, size_t idx
)
326 return git_array_get(tree
->entries
, idx
);
329 const git_tree_entry
*git_tree_entry_byid(
330 const git_tree
*tree
, const git_oid
*id
)
333 const git_tree_entry
*e
;
337 git_array_foreach(tree
->entries
, i
, e
) {
338 if (memcmp(&e
->oid
->id
, &id
->id
, sizeof(id
->id
)) == 0)
345 int git_tree__prefix_position(const git_tree
*tree
, const char *path
)
347 struct tree_key_search ksearch
;
348 size_t at_pos
, path_len
;
353 path_len
= strlen(path
);
354 TREE_ENTRY_CHECK_NAMELEN(path_len
);
356 ksearch
.filename
= path
;
357 ksearch
.filename_len
= (uint16_t)path_len
;
359 /* Find tree entry with appropriate prefix */
361 &at_pos
, tree
->entries
, &homing_search_cmp
, &ksearch
);
363 for (; at_pos
< tree
->entries
.size
; ++at_pos
) {
364 const git_tree_entry
*entry
= git_array_get(tree
->entries
, at_pos
);
365 if (homing_search_cmp(&ksearch
, entry
) < 0)
369 for (; at_pos
> 0; --at_pos
) {
370 const git_tree_entry
*entry
=
371 git_array_get(tree
->entries
, at_pos
- 1);
373 if (homing_search_cmp(&ksearch
, entry
) > 0)
380 size_t git_tree_entrycount(const git_tree
*tree
)
383 return tree
->entries
.size
;
386 unsigned int git_treebuilder_entrycount(git_treebuilder
*bld
)
390 return git_strmap_num_entries(bld
->map
);
393 static int tree_error(const char *str
, const char *path
)
396 giterr_set(GITERR_TREE
, "%s - %s", str
, path
);
398 giterr_set(GITERR_TREE
, "%s", str
);
402 static int parse_mode(unsigned int *modep
, const char *buffer
, const char **buffer_out
)
405 unsigned int mode
= 0;
410 while ((c
= *buffer
++) != ' ') {
411 if (c
< '0' || c
> '7')
413 mode
= (mode
<< 3) + (c
- '0');
416 *buffer_out
= buffer
;
421 int git_tree__parse(void *_tree
, git_odb_object
*odb_obj
)
423 git_tree
*tree
= _tree
;
425 const char *buffer_end
;
427 if (git_odb_object_dup(&tree
->odb_obj
, odb_obj
) < 0)
430 buffer
= git_odb_object_data(tree
->odb_obj
);
431 buffer_end
= buffer
+ git_odb_object_size(tree
->odb_obj
);
433 git_array_init_to_size(tree
->entries
, DEFAULT_TREE_SIZE
);
434 GITERR_CHECK_ARRAY(tree
->entries
);
436 while (buffer
< buffer_end
) {
437 git_tree_entry
*entry
;
442 if (parse_mode(&attr
, buffer
, &buffer
) < 0 || !buffer
)
443 return tree_error("failed to parse tree: can't parse filemode", NULL
);
445 if ((nul
= memchr(buffer
, 0, buffer_end
- buffer
)) == NULL
)
446 return tree_error("failed to parse tree: object is corrupted", NULL
);
448 if ((filename_len
= nul
- buffer
) == 0)
449 return tree_error("failed to parse tree: can't parse filename", NULL
);
451 if ((buffer_end
- (nul
+ 1)) < GIT_OID_RAWSZ
)
452 return tree_error("failed to parse tree: can't parse OID", NULL
);
454 /* Allocate the entry */
456 entry
= git_array_alloc(tree
->entries
);
457 GITERR_CHECK_ALLOC(entry
);
460 entry
->filename_len
= filename_len
;
461 entry
->filename
= buffer
;
462 entry
->oid
= (git_oid
*) ((char *) buffer
+ filename_len
+ 1);
465 buffer
+= filename_len
+ 1;
466 buffer
+= GIT_OID_RAWSZ
;
472 static size_t find_next_dir(const char *dirname
, git_index
*index
, size_t start
)
474 size_t dirlen
, i
, entries
= git_index_entrycount(index
);
476 dirlen
= strlen(dirname
);
477 for (i
= start
; i
< entries
; ++i
) {
478 const git_index_entry
*entry
= git_index_get_byindex(index
, i
);
479 if (strlen(entry
->path
) < dirlen
||
480 memcmp(entry
->path
, dirname
, dirlen
) ||
481 (dirlen
> 0 && entry
->path
[dirlen
] != '/')) {
489 static int append_entry(
490 git_treebuilder
*bld
,
491 const char *filename
,
493 git_filemode_t filemode
,
496 git_tree_entry
*entry
;
499 if (validate
&& !valid_entry_name(bld
->repo
, filename
))
500 return tree_error("failed to insert entry: invalid name for a tree entry", filename
);
502 if (validate
&& git_oid_iszero(id
))
503 return tree_error("failed to insert entry: invalid null OID for a tree entry", filename
);
505 entry
= alloc_entry(filename
, strlen(filename
), id
);
506 GITERR_CHECK_ALLOC(entry
);
508 entry
->attr
= (uint16_t)filemode
;
510 git_strmap_insert(bld
->map
, entry
->filename
, entry
, &error
);
512 git_tree_entry_free(entry
);
513 giterr_set(GITERR_TREE
, "failed to append entry %s to the tree builder", filename
);
520 static int write_tree(
522 git_repository
*repo
,
528 git_treebuilder
*bld
= NULL
;
529 size_t i
, entries
= git_index_entrycount(index
);
531 size_t dirname_len
= strlen(dirname
);
532 const git_tree_cache
*cache
;
534 cache
= git_tree_cache_get(index
->tree
, dirname
);
535 if (cache
!= NULL
&& cache
->entry_count
>= 0){
536 git_oid_cpy(oid
, &cache
->oid
);
537 return (int)find_next_dir(dirname
, index
, start
);
540 if ((error
= git_treebuilder_new(&bld
, repo
, NULL
)) < 0 || bld
== NULL
)
544 * This loop is unfortunate, but necessary. The index doesn't have
545 * any directores, so we need to handle that manually, and we
546 * need to keep track of the current position.
548 for (i
= start
; i
< entries
; ++i
) {
549 const git_index_entry
*entry
= git_index_get_byindex(index
, i
);
550 const char *filename
, *next_slash
;
553 * If we've left our (sub)tree, exit the loop and return. The
554 * first check is an early out (and security for the
555 * third). The second check is a simple prefix comparison. The
556 * third check catches situations where there is a directory
557 * win32/sys and a file win32mmap.c. Without it, the following
558 * code believes there is a file win32/mmap.c
560 if (strlen(entry
->path
) < dirname_len
||
561 memcmp(entry
->path
, dirname
, dirname_len
) ||
562 (dirname_len
> 0 && entry
->path
[dirname_len
] != '/')) {
566 filename
= entry
->path
+ dirname_len
;
567 if (*filename
== '/')
569 next_slash
= strchr(filename
, '/');
573 char *subdir
, *last_comp
;
575 subdir
= git__strndup(entry
->path
, next_slash
- entry
->path
);
576 GITERR_CHECK_ALLOC(subdir
);
578 /* Write out the subtree */
579 written
= write_tree(&sub_oid
, repo
, index
, subdir
, i
, shared_buf
);
584 i
= written
- 1; /* -1 because of the loop increment */
588 * We need to figure out what we want toinsert
589 * into this tree. If we're traversing
590 * deps/zlib/, then we only want to write
591 * 'zlib' into the tree.
593 last_comp
= strrchr(subdir
, '/');
595 last_comp
++; /* Get rid of the '/' */
600 error
= append_entry(bld
, last_comp
, &sub_oid
, S_IFDIR
, true);
605 error
= append_entry(bld
, filename
, &entry
->id
, entry
->mode
, true);
611 if (git_treebuilder_write_with_buffer(oid
, bld
, shared_buf
) < 0)
614 git_treebuilder_free(bld
);
618 git_treebuilder_free(bld
);
622 int git_tree__write_index(
623 git_oid
*oid
, git_index
*index
, git_repository
*repo
)
627 git_buf shared_buf
= GIT_BUF_INIT
;
628 bool old_ignore_case
= false;
630 assert(oid
&& index
&& repo
);
632 if (git_index_has_conflicts(index
)) {
633 giterr_set(GITERR_INDEX
,
634 "cannot create a tree from a not fully merged index.");
635 return GIT_EUNMERGED
;
638 if (index
->tree
!= NULL
&& index
->tree
->entry_count
>= 0) {
639 git_oid_cpy(oid
, &index
->tree
->oid
);
643 /* The tree cache didn't help us; we'll have to write
644 * out a tree. If the index is ignore_case, we must
645 * make it case-sensitive for the duration of the tree-write
648 if (index
->ignore_case
) {
649 old_ignore_case
= true;
650 git_index__set_ignore_case(index
, false);
653 ret
= write_tree(oid
, repo
, index
, "", 0, &shared_buf
);
654 git_buf_free(&shared_buf
);
657 git_index__set_ignore_case(index
, true);
664 git_pool_clear(&index
->tree_pool
);
666 if ((ret
= git_tree_lookup(&tree
, repo
, oid
)) < 0)
669 /* Read the tree cache into the index */
670 ret
= git_tree_cache_read_tree(&index
->tree
, tree
, &index
->tree_pool
);
676 int git_treebuilder_new(
677 git_treebuilder
**builder_p
,
678 git_repository
*repo
,
679 const git_tree
*source
)
681 git_treebuilder
*bld
;
684 assert(builder_p
&& repo
);
686 bld
= git__calloc(1, sizeof(git_treebuilder
));
687 GITERR_CHECK_ALLOC(bld
);
691 if (git_strmap_alloc(&bld
->map
) < 0) {
696 if (source
!= NULL
) {
697 git_tree_entry
*entry_src
;
699 git_array_foreach(source
->entries
, i
, entry_src
) {
701 bld
, entry_src
->filename
,
713 git_treebuilder_free(bld
);
717 static git_otype
otype_from_mode(git_filemode_t filemode
)
720 case GIT_FILEMODE_TREE
:
722 case GIT_FILEMODE_COMMIT
:
723 return GIT_OBJ_COMMIT
;
729 int git_treebuilder_insert(
730 const git_tree_entry
**entry_out
,
731 git_treebuilder
*bld
,
732 const char *filename
,
734 git_filemode_t filemode
)
736 git_tree_entry
*entry
;
740 assert(bld
&& id
&& filename
);
742 if (!valid_filemode(filemode
))
743 return tree_error("failed to insert entry: invalid filemode for file", filename
);
745 if (!valid_entry_name(bld
->repo
, filename
))
746 return tree_error("failed to insert entry: invalid name for a tree entry", filename
);
748 if (git_oid_iszero(id
))
749 return tree_error("failed to insert entry: invalid null OID", filename
);
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
);
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
);
760 entry
= alloc_entry(filename
, strlen(filename
), id
);
761 GITERR_CHECK_ALLOC(entry
);
763 git_strmap_insert(bld
->map
, entry
->filename
, entry
, &error
);
766 git_tree_entry_free(entry
);
767 giterr_set(GITERR_TREE
, "failed to insert %s", filename
);
772 entry
->attr
= filemode
;
780 static git_tree_entry
*treebuilder_get(git_treebuilder
*bld
, const char *filename
)
782 git_tree_entry
*entry
= NULL
;
785 assert(bld
&& filename
);
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
);
794 const git_tree_entry
*git_treebuilder_get(git_treebuilder
*bld
, const char *filename
)
796 return treebuilder_get(bld
, filename
);
799 int git_treebuilder_remove(git_treebuilder
*bld
, const char *filename
)
801 git_tree_entry
*entry
= treebuilder_get(bld
, filename
);
804 return tree_error("failed to remove entry: file isn't in the tree", filename
);
806 git_strmap_delete(bld
->map
, filename
);
807 git_tree_entry_free(entry
);
812 int git_treebuilder_write(git_oid
*oid
, git_treebuilder
*bld
)
815 git_buf buffer
= GIT_BUF_INIT
;
817 error
= git_treebuilder_write_with_buffer(oid
, bld
, &buffer
);
819 git_buf_free(&buffer
);
823 int git_treebuilder_write_with_buffer(git_oid
*oid
, git_treebuilder
*bld
, git_buf
*tree
)
826 size_t i
, entrycount
;
828 git_tree_entry
*entry
;
829 git_vector entries
= GIT_VECTOR_INIT
;
836 entrycount
= git_strmap_num_entries(bld
->map
);
837 if ((error
= git_vector_init(&entries
, entrycount
, entry_sort_cmp
)) < 0)
840 if (tree
->asize
== 0 &&
841 (error
= git_buf_grow(tree
, entrycount
* 72)) < 0)
844 git_strmap_foreach_value(bld
->map
, entry
, {
845 if ((error
= git_vector_insert(&entries
, entry
)) < 0)
849 git_vector_sort(&entries
);
851 for (i
= 0; i
< entries
.length
&& !error
; ++i
) {
852 entry
= git_vector_get(&entries
, i
);
854 git_buf_printf(tree
, "%o ", entry
->attr
);
855 git_buf_put(tree
, entry
->filename
, entry
->filename_len
+ 1);
856 git_buf_put(tree
, (char *)entry
->oid
->id
, GIT_OID_RAWSZ
);
858 if (git_buf_oom(tree
)) {
864 if ((error
= git_repository_odb__weakptr(&odb
, bld
->repo
)) == 0)
865 error
= git_odb_write(oid
, odb
, tree
->ptr
, tree
->size
, GIT_OBJ_TREE
);
868 git_vector_free(&entries
);
873 void git_treebuilder_filter(
874 git_treebuilder
*bld
,
875 git_treebuilder_filter_cb filter
,
878 const char *filename
;
879 git_tree_entry
*entry
;
881 assert(bld
&& filter
);
883 git_strmap_foreach(bld
->map
, filename
, entry
, {
884 if (filter(entry
, payload
)) {
885 git_strmap_delete(bld
->map
, filename
);
886 git_tree_entry_free(entry
);
891 void git_treebuilder_clear(git_treebuilder
*bld
)
897 git_strmap_foreach_value(bld
->map
, e
, git_tree_entry_free(e
));
898 git_strmap_clear(bld
->map
);
901 void git_treebuilder_free(git_treebuilder
*bld
)
906 git_treebuilder_clear(bld
);
907 git_strmap_free(bld
->map
);
911 static size_t subpath_len(const char *path
)
913 const char *slash_pos
= strchr(path
, '/');
914 if (slash_pos
== NULL
)
917 return slash_pos
- path
;
920 int git_tree_entry_bypath(
921 git_tree_entry
**entry_out
,
922 const git_tree
*root
,
927 const git_tree_entry
*entry
;
930 /* Find how long is the current path component (i.e.
931 * the filename between two slashes */
932 filename_len
= subpath_len(path
);
934 if (filename_len
== 0) {
935 giterr_set(GITERR_TREE
, "invalid tree path given");
936 return GIT_ENOTFOUND
;
939 entry
= entry_fromname(root
, path
, filename_len
);
942 giterr_set(GITERR_TREE
,
943 "the path '%.*s' does not exist in the given tree", (int) filename_len
, path
);
944 return GIT_ENOTFOUND
;
947 switch (path
[filename_len
]) {
949 /* If there are more components in the path...
950 * then this entry *must* be a tree */
951 if (!git_tree_entry__is_tree(entry
)) {
952 giterr_set(GITERR_TREE
,
953 "the path '%.*s' exists but is not a tree", (int) filename_len
, path
);
954 return GIT_ENOTFOUND
;
957 /* If there's only a slash left in the path, we
958 * return the current entry; otherwise, we keep
959 * walking down the path */
960 if (path
[filename_len
+ 1] != '\0')
964 /* If there are no more components in the path, return
966 return git_tree_entry_dup(entry_out
, entry
);
969 if (git_tree_lookup(&subtree
, root
->object
.repo
, entry
->oid
) < 0)
972 error
= git_tree_entry_bypath(
975 path
+ filename_len
+ 1
978 git_tree_free(subtree
);
982 static int tree_walk(
983 const git_tree
*tree
,
984 git_treewalk_cb callback
,
991 const git_tree_entry
*entry
;
993 git_array_foreach(tree
->entries
, i
, entry
) {
995 error
= callback(path
->ptr
, entry
, payload
);
996 if (error
< 0) { /* negative value stops iteration */
997 giterr_set_after_callback_function(error
, "git_tree_walk");
1000 if (error
> 0) { /* positive value skips this entry */
1006 if (git_tree_entry__is_tree(entry
)) {
1008 size_t path_len
= git_buf_len(path
);
1010 error
= git_tree_lookup(&subtree
, tree
->object
.repo
, entry
->oid
);
1014 /* append the next entry to the path */
1015 git_buf_puts(path
, entry
->filename
);
1016 git_buf_putc(path
, '/');
1018 if (git_buf_oom(path
))
1021 error
= tree_walk(subtree
, callback
, path
, payload
, preorder
);
1023 git_tree_free(subtree
);
1027 git_buf_truncate(path
, path_len
);
1031 error
= callback(path
->ptr
, entry
, payload
);
1032 if (error
< 0) { /* negative value stops iteration */
1033 giterr_set_after_callback_function(error
, "git_tree_walk");
1044 const git_tree
*tree
,
1045 git_treewalk_mode mode
,
1046 git_treewalk_cb callback
,
1050 git_buf root_path
= GIT_BUF_INIT
;
1052 if (mode
!= GIT_TREEWALK_POST
&& mode
!= GIT_TREEWALK_PRE
) {
1053 giterr_set(GITERR_INVALID
, "invalid walking mode for tree walk");
1058 tree
, callback
, &root_path
, payload
, (mode
== GIT_TREEWALK_PRE
));
1060 git_buf_free(&root_path
);
1065 static int compare_entries(const void *_a
, const void *_b
)
1067 const git_tree_update
*a
= (git_tree_update
*) _a
;
1068 const git_tree_update
*b
= (git_tree_update
*) _b
;
1070 return strcmp(a
->path
, b
->path
);
1073 static int on_dup_entry(void **old
, void *new)
1075 GIT_UNUSED(old
); GIT_UNUSED(new);
1077 giterr_set(GITERR_TREE
, "duplicate entries given for update");
1082 * We keep the previous tree and the new one at each level of the
1083 * stack. When we leave a level we're done with that tree and we can
1084 * write it out to the odb.
1087 git_treebuilder
*bld
;
1092 /** Count how many slashes (i.e. path components) there are in this string */
1093 GIT_INLINE(size_t) count_slashes(const char *path
)
1098 while ((slash
= strchr(path
, '/')) != NULL
) {
1106 static bool next_component(git_buf
*out
, const char *in
)
1108 const char *slash
= strchr(in
, '/');
1113 git_buf_put(out
, in
, slash
- in
);
1118 static int create_popped_tree(tree_stack_entry
*current
, tree_stack_entry
*popped
, git_buf
*component
)
1123 git_tree_free(popped
->tree
);
1125 /* If the tree would be empty, remove it from the one higher up */
1126 if (git_treebuilder_entrycount(popped
->bld
) == 0) {
1127 git_treebuilder_free(popped
->bld
);
1128 error
= git_treebuilder_remove(current
->bld
, popped
->name
);
1129 git__free(popped
->name
);
1133 error
= git_treebuilder_write(&new_tree
, popped
->bld
);
1134 git_treebuilder_free(popped
->bld
);
1137 git__free(popped
->name
);
1141 /* We've written out the tree, now we have to put the new value into its parent */
1142 git_buf_clear(component
);
1143 git_buf_puts(component
, popped
->name
);
1144 git__free(popped
->name
);
1146 GITERR_CHECK_ALLOC(component
->ptr
);
1148 /* Error out if this would create a D/F conflict in this update */
1149 if (current
->tree
) {
1150 const git_tree_entry
*to_replace
;
1151 to_replace
= git_tree_entry_byname(current
->tree
, component
->ptr
);
1152 if (to_replace
&& git_tree_entry_type(to_replace
) != GIT_OBJ_TREE
) {
1153 giterr_set(GITERR_TREE
, "D/F conflict when updating tree");
1158 return git_treebuilder_insert(NULL
, current
->bld
, component
->ptr
, &new_tree
, GIT_FILEMODE_TREE
);
1161 int git_tree_create_updated(git_oid
*out
, git_repository
*repo
, git_tree
*baseline
, size_t nupdates
, const git_tree_update
*updates
)
1163 git_array_t(tree_stack_entry
) stack
= GIT_ARRAY_INIT
;
1164 tree_stack_entry
*root_elem
;
1168 git_buf component
= GIT_BUF_INIT
;
1170 if ((error
= git_vector_init(&entries
, nupdates
, compare_entries
)) < 0)
1173 /* Sort the entries for treversal */
1174 for (i
= 0 ; i
< nupdates
; i
++) {
1175 if ((error
= git_vector_insert_sorted(&entries
, (void *) &updates
[i
], on_dup_entry
)) < 0)
1179 root_elem
= git_array_alloc(stack
);
1180 GITERR_CHECK_ALLOC(root_elem
);
1181 memset(root_elem
, 0, sizeof(*root_elem
));
1183 if (baseline
&& (error
= git_tree_dup(&root_elem
->tree
, baseline
)) < 0)
1186 if ((error
= git_treebuilder_new(&root_elem
->bld
, repo
, root_elem
->tree
)) < 0)
1189 for (i
= 0; i
< nupdates
; i
++) {
1190 const git_tree_update
*last_update
= i
== 0 ? NULL
: git_vector_get(&entries
, i
-1);
1191 const git_tree_update
*update
= git_vector_get(&entries
, i
);
1192 size_t common_prefix
= 0, steps_up
, j
;
1195 /* Figure out how much we need to change from the previous tree */
1197 common_prefix
= git_path_common_dirlen(last_update
->path
, update
->path
);
1200 * The entries are sorted, so when we find we're no
1201 * longer in the same directory, we need to abandon
1202 * the old tree (steps up) and dive down to the next
1205 steps_up
= last_update
== NULL
? 0 : count_slashes(&last_update
->path
[common_prefix
]);
1207 for (j
= 0; j
< steps_up
; j
++) {
1208 tree_stack_entry
*current
, *popped
= git_array_pop(stack
);
1211 current
= git_array_last(stack
);
1214 if ((error
= create_popped_tree(current
, popped
, &component
)) < 0)
1218 /* Now that we've created the trees we popped from the stack, let's go back down */
1219 path
= &update
->path
[common_prefix
];
1220 while (next_component(&component
, path
)) {
1221 tree_stack_entry
*last
, *new_entry
;
1222 const git_tree_entry
*entry
;
1224 last
= git_array_last(stack
);
1225 entry
= last
->tree
? git_tree_entry_byname(last
->tree
, component
.ptr
) : NULL
;
1227 entry
= treebuilder_get(last
->bld
, component
.ptr
);
1229 if (entry
&& git_tree_entry_type(entry
) != GIT_OBJ_TREE
) {
1230 giterr_set(GITERR_TREE
, "D/F conflict when updating tree");
1235 new_entry
= git_array_alloc(stack
);
1236 GITERR_CHECK_ALLOC(new_entry
);
1237 memset(new_entry
, 0, sizeof(*new_entry
));
1239 new_entry
->tree
= NULL
;
1240 if (entry
&& (error
= git_tree_lookup(&new_entry
->tree
, repo
, git_tree_entry_id(entry
))) < 0)
1243 if ((error
= git_treebuilder_new(&new_entry
->bld
, repo
, new_entry
->tree
)) < 0)
1246 new_entry
->name
= git__strdup(component
.ptr
);
1247 GITERR_CHECK_ALLOC(new_entry
->name
);
1249 /* Get to the start of the next component */
1250 path
+= component
.size
+ 1;
1253 /* After all that, we're finally at the place where we want to perform the update */
1254 switch (update
->action
) {
1255 case GIT_TREE_UPDATE_UPSERT
:
1257 /* Make sure we're replacing something of the same type */
1258 tree_stack_entry
*last
= git_array_last(stack
);
1259 char *basename
= git_path_basename(update
->path
);
1260 const git_tree_entry
*e
= git_treebuilder_get(last
->bld
, basename
);
1261 if (e
&& git_tree_entry_type(e
) != git_object__type_from_filemode(update
->filemode
)) {
1262 git__free(basename
);
1263 giterr_set(GITERR_TREE
, "cannot replace '%s' with '%s' at '%s'",
1264 git_object_type2string(git_tree_entry_type(e
)),
1265 git_object_type2string(git_object__type_from_filemode(update
->filemode
)),
1271 error
= git_treebuilder_insert(NULL
, last
->bld
, basename
, &update
->id
, update
->filemode
);
1272 git__free(basename
);
1275 case GIT_TREE_UPDATE_REMOVE
:
1277 char *basename
= git_path_basename(update
->path
);
1278 error
= git_treebuilder_remove(git_array_last(stack
)->bld
, basename
);
1279 git__free(basename
);
1283 giterr_set(GITERR_TREE
, "unknown action for update");
1292 /* We're done, go up the stack again and write out the tree */
1294 tree_stack_entry
*current
= NULL
, *popped
= NULL
;
1295 while ((popped
= git_array_pop(stack
)) != NULL
) {
1296 current
= git_array_last(stack
);
1297 /* We've reached the top, current is the root tree */
1301 if ((error
= create_popped_tree(current
, popped
, &component
)) < 0)
1305 /* Write out the root tree */
1306 git__free(popped
->name
);
1307 git_tree_free(popped
->tree
);
1309 error
= git_treebuilder_write(out
, popped
->bld
);
1310 git_treebuilder_free(popped
->bld
);
1317 tree_stack_entry
*e
;
1318 while ((e
= git_array_pop(stack
)) != NULL
) {
1319 git_treebuilder_free(e
->bld
);
1320 git_tree_free(e
->tree
);
1325 git_buf_free(&component
);
1326 git_array_clear(stack
);
1327 git_vector_free(&entries
);