]> git.proxmox.com Git - libgit2.git/blame - src/tree.c
New upstream version 0.27.7+dfsg.1
[libgit2.git] / src / tree.c
CommitLineData
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
225fe215 8#include "tree.h"
eae0bfdc
PP
9
10#include "commit.h"
44908fe7
VM
11#include "git2/repository.h"
12#include "git2/object.h"
f240acce 13#include "fileops.h"
114f5a6c
RB
14#include "tree-cache.h"
15#include "index.h"
225fe215 16
c8f5ff8f 17#define DEFAULT_TREE_SIZE 16
9de27ad0 18#define MAX_FILEMODE_BYTES 6
c8f5ff8f 19
aadad405
ET
20#define TREE_ENTRY_CHECK_NAMELEN(n) \
21 if (n > UINT16_MAX) { giterr_set(GITERR_INVALID, "tree entry path too long"); }
22
a7dbac0b 23static bool valid_filemode(const int filemode)
8e9bfa4c 24{
a7dbac0b 25 return (filemode == GIT_FILEMODE_TREE
26 || filemode == GIT_FILEMODE_BLOB
a7dbac0b 27 || filemode == GIT_FILEMODE_BLOB_EXECUTABLE
28 || filemode == GIT_FILEMODE_LINK
29 || filemode == GIT_FILEMODE_COMMIT);
0ad6efa1
VM
30}
31
f1c75b94
CMN
32GIT_INLINE(git_filemode_t) normalize_filemode(git_filemode_t filemode)
33{
34 /* Tree bits set, but it's not a commit */
f240acce 35 if (GIT_MODE_TYPE(filemode) == GIT_FILEMODE_TREE)
f1c75b94
CMN
36 return GIT_FILEMODE_TREE;
37
f240acce 38 /* If any of the x bits are set */
a7fcc44d 39 if (GIT_PERMS_IS_EXEC(filemode))
f1c75b94
CMN
40 return GIT_FILEMODE_BLOB_EXECUTABLE;
41
42 /* 16XXXX means commit */
f240acce 43 if (GIT_MODE_TYPE(filemode) == GIT_FILEMODE_COMMIT)
f1c75b94
CMN
44 return GIT_FILEMODE_COMMIT;
45
c8fb2e15 46 /* 12XXXX means symlink */
f240acce 47 if (GIT_MODE_TYPE(filemode) == GIT_FILEMODE_LINK)
f1c75b94
CMN
48 return GIT_FILEMODE_LINK;
49
50 /* Otherwise, return a blob */
51 return GIT_FILEMODE_BLOB;
52}
53
dce7b1a4 54static int valid_entry_name(git_repository *repo, const char *filename)
28c1451a 55{
cfeef7ce 56 return *filename != '\0' &&
4b3ec53c 57 git_path_isvalid(repo, filename, 0,
dce7b1a4 58 GIT_PATH_REJECT_TRAVERSAL | GIT_PATH_REJECT_DOT_GIT | GIT_PATH_REJECT_SLASH);
28c1451a
VM
59}
60
0c468633 61static int entry_sort_cmp(const void *a, const void *b)
28c1451a 62{
0c468633
RB
63 const git_tree_entry *e1 = (const git_tree_entry *)a;
64 const git_tree_entry *e2 = (const git_tree_entry *)b;
65
1744fafe 66 return git_path_cmp(
98527b5b 67 e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
0c468633
RB
68 e2->filename, e2->filename_len, git_tree_entry__is_tree(e2),
69 git__strncmp);
98527b5b
RB
70}
71
0c468633 72int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2)
98527b5b 73{
0c468633 74 return entry_sort_cmp(e1, e2);
98527b5b
RB
75}
76
0c468633 77int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2)
98527b5b 78{
0c468633
RB
79 return git_path_cmp(
80 e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
81 e2->filename, e2->filename_len, git_tree_entry__is_tree(e2),
82 git__strncasecmp);
28c1451a
VM
83}
84
ed970748 85/**
4ed9e939
CMN
86 * Allocate a new self-contained entry, with enough space after it to
87 * store the filename and the id.
ed970748 88 */
4ed9e939 89static git_tree_entry *alloc_entry(const char *filename, size_t filename_len, const git_oid *id)
ed970748
CMN
90{
91 git_tree_entry *entry = NULL;
2580077f 92 size_t tree_len;
ed970748 93
aadad405 94 TREE_ENTRY_CHECK_NAMELEN(filename_len);
95ae3520 95
4ed9e939
CMN
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))
95ae3520
CMN
99 return NULL;
100
4ed9e939 101 entry = git__calloc(1, tree_len);
95ae3520 102 if (!entry)
ed970748
CMN
103 return NULL;
104
4ed9e939 105 {
60a194aa
CMN
106 char *filename_ptr;
107 void *id_ptr;
108
109 filename_ptr = ((char *) entry) + sizeof(git_tree_entry);
110 memcpy(filename_ptr, filename, filename_len);
111 entry->filename = filename_ptr;
112
113 id_ptr = filename_ptr + filename_len + 1;
114 git_oid_cpy(id_ptr, id);
115 entry->oid = id_ptr;
116 }
117
aadad405 118 entry->filename_len = (uint16_t)filename_len;
ed970748
CMN
119
120 return entry;
121}
122
761aa2aa
VM
123struct tree_key_search {
124 const char *filename;
ee42bb0e 125 uint16_t filename_len;
761aa2aa
VM
126};
127
28c1451a 128static int homing_search_cmp(const void *key, const void *array_member)
2a884588 129{
0cbbdc26
KS
130 const struct tree_key_search *ksearch = key;
131 const git_tree_entry *entry = array_member;
2a884588 132
aadad405
ET
133 const uint16_t len1 = ksearch->filename_len;
134 const uint16_t len2 = entry->filename_len;
e6629d83 135
28c1451a
VM
136 return memcmp(
137 ksearch->filename,
138 entry->filename,
139 len1 < len2 ? len1 : len2
140 );
9de27ad0
SJ
141}
142
28c1451a
VM
143/*
144 * Search for an entry in a given tree.
145 *
146 * Note that this search is performed in two steps because
147 * of the way tree entries are sorted internally in git:
148 *
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.
152 *
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.
156 *
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.
162 */
16248ee2 163static int tree_key_search(
e2e4bae9
ET
164 size_t *at_pos,
165 const git_tree *tree,
166 const char *filename,
167 size_t filename_len)
2a884588 168{
28c1451a
VM
169 struct tree_key_search ksearch;
170 const git_tree_entry *entry;
11d9f6b3 171 size_t homing, i;
2a884588 172
aadad405
ET
173 TREE_ENTRY_CHECK_NAMELEN(filename_len);
174
28c1451a 175 ksearch.filename = filename;
aadad405 176 ksearch.filename_len = (uint16_t)filename_len;
e6629d83 177
28c1451a
VM
178 /* Initial homing search; find an entry on the tree with
179 * the same prefix as the filename we're looking for */
e2e4bae9
ET
180
181 if (git_array_search(&homing,
182 tree->entries, &homing_search_cmp, &ksearch) < 0)
b60d95c7 183 return GIT_ENOTFOUND; /* just a signal error; not passed back to user */
28c1451a
VM
184
185 /* We found a common prefix. Look forward as long as
186 * there are entries that share the common prefix */
e2e4bae9
ET
187 for (i = homing; i < tree->entries.size; ++i) {
188 entry = git_array_get(tree->entries, i);
28c1451a 189
181bbf14 190 if (homing_search_cmp(&ksearch, entry) < 0)
28c1451a
VM
191 break;
192
0e2fcca8 193 if (entry->filename_len == filename_len &&
11d9f6b3
PK
194 memcmp(filename, entry->filename, filename_len) == 0) {
195 if (at_pos)
196 *at_pos = i;
197
198 return 0;
199 }
8cf2de07 200 }
e6629d83 201
28c1451a
VM
202 /* If we haven't found our filename yet, look backwards
203 * too as long as we have entries with the same prefix */
11d9f6b3
PK
204 if (homing > 0) {
205 i = homing - 1;
e6629d83 206
11d9f6b3 207 do {
e2e4bae9 208 entry = git_array_get(tree->entries, i);
e6629d83 209
11d9f6b3
PK
210 if (homing_search_cmp(&ksearch, entry) > 0)
211 break;
212
213 if (entry->filename_len == filename_len &&
214 memcmp(filename, entry->filename, filename_len) == 0) {
215 if (at_pos)
216 *at_pos = i;
217
218 return 0;
219 }
220 } while (i-- > 0);
28c1451a
VM
221 }
222
223 /* The filename doesn't exist at all */
904b67e6 224 return GIT_ENOTFOUND;
e6629d83
VM
225}
226
0e2fcca8
VM
227void git_tree_entry_free(git_tree_entry *entry)
228{
4ed9e939 229 if (entry == NULL)
1c3edb30 230 return;
231
0e2fcca8
VM
232 git__free(entry);
233}
234
529f342a 235int git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source)
0e2fcca8 236{
4ed9e939
CMN
237 git_tree_entry *cpy;
238
529f342a 239 assert(source);
0e2fcca8 240
4ed9e939
CMN
241 cpy = alloc_entry(source->filename, source->filename_len, source->oid);
242 if (cpy == NULL)
60a194aa 243 return -1;
9487585d 244
4ed9e939
CMN
245 cpy->attr = source->attr;
246
247 *dest = cpy;
529f342a 248 return 0;
0e2fcca8
VM
249}
250
116bbdf0 251void git_tree__free(void *_tree)
225fe215 252{
116bbdf0 253 git_tree *tree = _tree;
003c2690 254
60a194aa 255 git_odb_object_free(tree->odb_obj);
e2e4bae9 256 git_array_clear(tree->entries);
3286c408 257 git__free(tree);
225fe215
VM
258}
259
9d7ac675 260git_filemode_t git_tree_entry_filemode(const git_tree_entry *entry)
003c2690 261{
13f670a5
CMN
262 return normalize_filemode(entry->attr);
263}
264
265git_filemode_t git_tree_entry_filemode_raw(const git_tree_entry *entry)
266{
267 return entry->attr;
003c2690
VM
268}
269
0ad6efa1 270const char *git_tree_entry_name(const git_tree_entry *entry)
003c2690 271{
c4b5bedc 272 assert(entry);
2a884588
VM
273 return entry->filename;
274}
003c2690 275
0ad6efa1 276const git_oid *git_tree_entry_id(const git_tree_entry *entry)
2a884588 277{
c4b5bedc 278 assert(entry);
60a194aa 279 return entry->oid;
003c2690
VM
280}
281
ff9a4c13
RG
282git_otype git_tree_entry_type(const git_tree_entry *entry)
283{
284 assert(entry);
285
286 if (S_ISGITLINK(entry->attr))
287 return GIT_OBJ_COMMIT;
288 else if (S_ISDIR(entry->attr))
289 return GIT_OBJ_TREE;
290 else
291 return GIT_OBJ_BLOB;
292}
293
9d0011fd
VM
294int git_tree_entry_to_object(
295 git_object **object_out,
296 git_repository *repo,
297 const git_tree_entry *entry)
003c2690 298{
1795f879 299 assert(entry && object_out);
60a194aa 300 return git_object_lookup(object_out, repo, entry->oid, GIT_OBJ_ANY);
122c3405
VM
301}
302
16248ee2 303static const git_tree_entry *entry_fromname(
58206c9a 304 const git_tree *tree, const char *name, size_t name_len)
2a884588 305{
11d9f6b3
PK
306 size_t idx;
307
e2e4bae9 308 if (tree_key_search(&idx, tree, name, name_len) < 0)
c4034e63
VM
309 return NULL;
310
e2e4bae9 311 return git_array_get(tree->entries, idx);
2a884588
VM
312}
313
16248ee2 314const git_tree_entry *git_tree_entry_byname(
58206c9a 315 const git_tree *tree, const char *filename)
0e2fcca8
VM
316{
317 assert(tree && filename);
aadad405 318
0e2fcca8
VM
319 return entry_fromname(tree, filename, strlen(filename));
320}
321
16248ee2 322const git_tree_entry *git_tree_entry_byindex(
58206c9a 323 const git_tree *tree, size_t idx)
003c2690 324{
c4b5bedc 325 assert(tree);
e2e4bae9 326 return git_array_get(tree->entries, idx);
003c2690
VM
327}
328
f000ee4e
CMN
329const git_tree_entry *git_tree_entry_byid(
330 const git_tree *tree, const git_oid *id)
2031760c 331{
16248ee2
RB
332 size_t i;
333 const git_tree_entry *e;
2031760c
RB
334
335 assert(tree);
336
e2e4bae9 337 git_array_foreach(tree->entries, i, e) {
60a194aa 338 if (memcmp(&e->oid->id, &id->id, sizeof(id->id)) == 0)
2031760c
RB
339 return e;
340 }
341
342 return NULL;
343}
344
58206c9a 345int git_tree__prefix_position(const git_tree *tree, const char *path)
41a82592 346{
41a82592 347 struct tree_key_search ksearch;
aadad405 348 size_t at_pos, path_len;
41a82592 349
91e7d263
RB
350 if (!path)
351 return 0;
352
aadad405
ET
353 path_len = strlen(path);
354 TREE_ENTRY_CHECK_NAMELEN(path_len);
355
41a82592 356 ksearch.filename = path;
aadad405 357 ksearch.filename_len = (uint16_t)path_len;
41a82592
RB
358
359 /* Find tree entry with appropriate prefix */
e2e4bae9
ET
360 git_array_search(
361 &at_pos, tree->entries, &homing_search_cmp, &ksearch);
41a82592 362
e2e4bae9
ET
363 for (; at_pos < tree->entries.size; ++at_pos) {
364 const git_tree_entry *entry = git_array_get(tree->entries, at_pos);
41a82592
RB
365 if (homing_search_cmp(&ksearch, entry) < 0)
366 break;
367 }
368
369 for (; at_pos > 0; --at_pos) {
e2e4bae9
ET
370 const git_tree_entry *entry =
371 git_array_get(tree->entries, at_pos - 1);
372
41a82592
RB
373 if (homing_search_cmp(&ksearch, entry) > 0)
374 break;
375 }
376
16248ee2 377 return (int)at_pos;
41a82592
RB
378}
379
e120123e 380size_t git_tree_entrycount(const git_tree *tree)
003c2690 381{
c4b5bedc 382 assert(tree);
e2e4bae9 383 return tree->entries.size;
003c2690
VM
384}
385
5fb98206
JW
386unsigned int git_treebuilder_entrycount(git_treebuilder *bld)
387{
388 assert(bld);
fcc60066
CMN
389
390 return git_strmap_num_entries(bld->map);
5fb98206
JW
391}
392
cfeef7ce 393static int tree_error(const char *str, const char *path)
d8603ed9 394{
cfeef7ce
RB
395 if (path)
396 giterr_set(GITERR_TREE, "%s - %s", str, path);
397 else
398 giterr_set(GITERR_TREE, "%s", str);
3aa351ea
CMN
399 return -1;
400}
2a884588 401
0174f21b
CMN
402static int parse_mode(unsigned int *modep, const char *buffer, const char **buffer_out)
403{
404 unsigned char c;
405 unsigned int mode = 0;
406
407 if (*buffer == ' ')
408 return -1;
409
410 while ((c = *buffer++) != ' ') {
411 if (c < '0' || c > '7')
412 return -1;
413 mode = (mode << 3) + (c - '0');
414 }
415 *modep = mode;
416 *buffer_out = buffer;
417
418 return 0;
419}
420
116bbdf0 421int git_tree__parse(void *_tree, git_odb_object *odb_obj)
3aa351ea 422{
116bbdf0 423 git_tree *tree = _tree;
60a194aa
CMN
424 const char *buffer;
425 const char *buffer_end;
426
427 if (git_odb_object_dup(&tree->odb_obj, odb_obj) < 0)
428 return -1;
429
430 buffer = git_odb_object_data(tree->odb_obj);
431 buffer_end = buffer + git_odb_object_size(tree->odb_obj);
78606263 432
e2e4bae9
ET
433 git_array_init_to_size(tree->entries, DEFAULT_TREE_SIZE);
434 GITERR_CHECK_ARRAY(tree->entries);
e4def81a 435
003c2690
VM
436 while (buffer < buffer_end) {
437 git_tree_entry *entry;
2580077f
CMN
438 size_t filename_len;
439 const char *nul;
0174f21b 440 unsigned int attr;
d8603ed9 441
0174f21b 442 if (parse_mode(&attr, buffer, &buffer) < 0 || !buffer)
eae0bfdc 443 return tree_error("failed to parse tree: can't parse filemode", NULL);
8e9bfa4c 444
2580077f 445 if ((nul = memchr(buffer, 0, buffer_end - buffer)) == NULL)
eae0bfdc 446 return tree_error("failed to parse tree: object is corrupted", NULL);
58519018 447
4974e3a5 448 if ((filename_len = nul - buffer) == 0)
eae0bfdc 449 return tree_error("failed to parse tree: can't parse filename", NULL);
4974e3a5
PS
450
451 if ((buffer_end - (nul + 1)) < GIT_OID_RAWSZ)
eae0bfdc 452 return tree_error("failed to parse tree: can't parse OID", NULL);
4974e3a5 453
e2e4bae9 454 /* Allocate the entry */
0e2fcca8 455 {
e2e4bae9 456 entry = git_array_alloc(tree->entries);
0e2fcca8
VM
457 GITERR_CHECK_ALLOC(entry);
458
0e2fcca8 459 entry->attr = attr;
4ed9e939
CMN
460 entry->filename_len = filename_len;
461 entry->filename = buffer;
462 entry->oid = (git_oid *) ((char *) buffer + filename_len + 1);
0e2fcca8 463 }
d8603ed9 464
2580077f 465 buffer += filename_len + 1;
d8603ed9 466 buffer += GIT_OID_RAWSZ;
d8603ed9
VM
467 }
468
3aa351ea 469 return 0;
d8603ed9 470}
58519018 471
a8122b5d 472static size_t find_next_dir(const char *dirname, git_index *index, size_t start)
8255c69b 473{
a8122b5d 474 size_t dirlen, i, entries = git_index_entrycount(index);
8255c69b
CMN
475
476 dirlen = strlen(dirname);
477 for (i = start; i < entries; ++i) {
f45d51ff 478 const git_index_entry *entry = git_index_get_byindex(index, i);
8255c69b
CMN
479 if (strlen(entry->path) < dirlen ||
480 memcmp(entry->path, dirname, dirlen) ||
481 (dirlen > 0 && entry->path[dirlen] != '/')) {
482 break;
483 }
484 }
485
486 return i;
487}
488
0e2fcca8
VM
489static int append_entry(
490 git_treebuilder *bld,
491 const char *filename,
492 const git_oid *id,
6c7cee42
RD
493 git_filemode_t filemode,
494 bool validate)
9ef9e8c3
VM
495{
496 git_tree_entry *entry;
4d3f1f97 497 int error = 0;
9ef9e8c3 498
6c7cee42 499 if (validate && !valid_entry_name(bld->repo, filename))
eae0bfdc
PP
500 return tree_error("failed to insert entry: invalid name for a tree entry", filename);
501
6c7cee42 502 if (validate && git_oid_iszero(id))
eae0bfdc 503 return tree_error("failed to insert entry: invalid null OID for a tree entry", filename);
0d778b1a 504
4ed9e939 505 entry = alloc_entry(filename, strlen(filename), id);
3aa351ea 506 GITERR_CHECK_ALLOC(entry);
9ef9e8c3 507
a7dbac0b 508 entry->attr = (uint16_t)filemode;
9ef9e8c3 509
73028af8 510 git_strmap_insert(bld->map, entry->filename, entry, &error);
4d3f1f97 511 if (error < 0) {
966fb207 512 git_tree_entry_free(entry);
4d3f1f97 513 giterr_set(GITERR_TREE, "failed to append entry %s to the tree builder", filename);
3aa351ea 514 return -1;
e2237179 515 }
9ef9e8c3 516
3aa351ea 517 return 0;
9ef9e8c3
VM
518}
519
9462c471
VM
520static int write_tree(
521 git_oid *oid,
522 git_repository *repo,
523 git_index *index,
524 const char *dirname,
87aaefe2
MT
525 size_t start,
526 git_buf *shared_buf)
47d8ec56 527{
4a619797 528 git_treebuilder *bld = NULL;
a8122b5d 529 size_t i, entries = git_index_entrycount(index);
4a619797
CMN
530 int error;
531 size_t dirname_len = strlen(dirname);
8255c69b
CMN
532 const git_tree_cache *cache;
533
534 cache = git_tree_cache_get(index->tree, dirname);
c2f8b215 535 if (cache != NULL && cache->entry_count >= 0){
8255c69b 536 git_oid_cpy(oid, &cache->oid);
a8122b5d 537 return (int)find_next_dir(dirname, index, start);
8255c69b 538 }
29e1789b 539
208a2c8a 540 if ((error = git_treebuilder_new(&bld, repo, NULL)) < 0 || bld == NULL)
3aa351ea 541 return -1;
932d1baf 542
4a619797
CMN
543 /*
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.
547 */
548 for (i = start; i < entries; ++i) {
f45d51ff 549 const git_index_entry *entry = git_index_get_byindex(index, i);
16248ee2 550 const char *filename, *next_slash;
4a619797
CMN
551
552 /*
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
559 */
560 if (strlen(entry->path) < dirname_len ||
561 memcmp(entry->path, dirname, dirname_len) ||
562 (dirname_len > 0 && entry->path[dirname_len] != '/')) {
47d8ec56 563 break;
4a619797 564 }
932d1baf 565
4a619797
CMN
566 filename = entry->path + dirname_len;
567 if (*filename == '/')
568 filename++;
569 next_slash = strchr(filename, '/');
570 if (next_slash) {
571 git_oid sub_oid;
572 int written;
573 char *subdir, *last_comp;
574
575 subdir = git__strndup(entry->path, next_slash - entry->path);
3aa351ea 576 GITERR_CHECK_ALLOC(subdir);
29e1789b 577
4a619797 578 /* Write out the subtree */
87aaefe2 579 written = write_tree(&sub_oid, repo, index, subdir, i, shared_buf);
4a619797 580 if (written < 0) {
cfeef7ce 581 git__free(subdir);
3aa351ea 582 goto on_error;
4a619797
CMN
583 } else {
584 i = written - 1; /* -1 because of the loop increment */
29e1789b 585 }
932d1baf 586
4a619797
CMN
587 /*
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.
592 */
593 last_comp = strrchr(subdir, '/');
594 if (last_comp) {
595 last_comp++; /* Get rid of the '/' */
596 } else {
597 last_comp = subdir;
598 }
cfeef7ce 599
6c7cee42 600 error = append_entry(bld, last_comp, &sub_oid, S_IFDIR, true);
3286c408 601 git__free(subdir);
cfeef7ce 602 if (error < 0)
3aa351ea 603 goto on_error;
4a619797 604 } else {
6c7cee42 605 error = append_entry(bld, filename, &entry->id, entry->mode, true);
cfeef7ce 606 if (error < 0)
3aa351ea 607 goto on_error;
47d8ec56 608 }
29e1789b 609 }
932d1baf 610
87aaefe2 611 if (git_treebuilder_write_with_buffer(oid, bld, shared_buf) < 0)
3aa351ea 612 goto on_error;
29e1789b 613
4a619797 614 git_treebuilder_free(bld);
a8122b5d 615 return (int)i;
4a619797 616
3aa351ea
CMN
617on_error:
618 git_treebuilder_free(bld);
619 return -1;
29e1789b
VM
620}
621
16248ee2
RB
622int git_tree__write_index(
623 git_oid *oid, git_index *index, git_repository *repo)
29e1789b 624{
3aa351ea 625 int ret;
7465e873 626 git_tree *tree;
87aaefe2 627 git_buf shared_buf = GIT_BUF_INIT;
3f0d0c85 628 bool old_ignore_case = false;
29e1789b 629
276ea401 630 assert(oid && index && repo);
47d8ec56 631
f92bcaea 632 if (git_index_has_conflicts(index)) {
633 giterr_set(GITERR_INDEX,
909d5494 634 "cannot create a tree from a not fully merged index.");
f92bcaea 635 return GIT_EUNMERGED;
636 }
637
c2f8b215 638 if (index->tree != NULL && index->tree->entry_count >= 0) {
8255c69b 639 git_oid_cpy(oid, &index->tree->oid);
3aa351ea 640 return 0;
8255c69b
CMN
641 }
642
3f0d0c85 643 /* The tree cache didn't help us; we'll have to write
cb53669e 644 * out a tree. If the index is ignore_case, we must
3f0d0c85
PK
645 * make it case-sensitive for the duration of the tree-write
646 * operation. */
647
648 if (index->ignore_case) {
649 old_ignore_case = true;
cb53669e 650 git_index__set_ignore_case(index, false);
3f0d0c85
PK
651 }
652
87aaefe2
MT
653 ret = write_tree(oid, repo, index, "", 0, &shared_buf);
654 git_buf_free(&shared_buf);
3f0d0c85
PK
655
656 if (old_ignore_case)
cb53669e 657 git_index__set_ignore_case(index, true);
3f0d0c85 658
7465e873
CMN
659 index->tree = NULL;
660
661 if (ret < 0)
662 return ret;
663
664 git_pool_clear(&index->tree_pool);
665
666 if ((ret = git_tree_lookup(&tree, repo, oid)) < 0)
667 return ret;
668
669 /* Read the tree cache into the index */
670 ret = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool);
671 git_tree_free(tree);
672
673 return ret;
47d8ec56 674}
0ad6efa1 675
208a2c8a 676int git_treebuilder_new(
dce7b1a4
ET
677 git_treebuilder **builder_p,
678 git_repository *repo,
679 const git_tree *source)
0ad6efa1
VM
680{
681 git_treebuilder *bld;
4d3f1f97 682 size_t i;
0ad6efa1 683
dce7b1a4 684 assert(builder_p && repo);
0ad6efa1
VM
685
686 bld = git__calloc(1, sizeof(git_treebuilder));
3aa351ea 687 GITERR_CHECK_ALLOC(bld);
0ad6efa1 688
dce7b1a4
ET
689 bld->repo = repo;
690
4d3f1f97 691 if (git_strmap_alloc(&bld->map) < 0) {
966fb207 692 git__free(bld);
4d3f1f97
CMN
693 return -1;
694 }
0ad6efa1 695
0ad6efa1 696 if (source != NULL) {
e2237179 697 git_tree_entry *entry_src;
0ad6efa1 698
e2e4bae9 699 git_array_foreach(source->entries, i, entry_src) {
66439b0b 700 if (append_entry(
701 bld, entry_src->filename,
60a194aa 702 entry_src->oid,
6c7cee42
RD
703 entry_src->attr,
704 false) < 0)
3aa351ea 705 goto on_error;
0ad6efa1
VM
706 }
707 }
708
709 *builder_p = bld;
3aa351ea
CMN
710 return 0;
711
712on_error:
713 git_treebuilder_free(bld);
714 return -1;
0ad6efa1
VM
715}
716
2bbc7d3e
ET
717static git_otype otype_from_mode(git_filemode_t filemode)
718{
719 switch (filemode) {
720 case GIT_FILEMODE_TREE:
721 return GIT_OBJ_TREE;
722 case GIT_FILEMODE_COMMIT:
723 return GIT_OBJ_COMMIT;
724 default:
725 return GIT_OBJ_BLOB;
726 }
727}
728
0e2fcca8
VM
729int git_treebuilder_insert(
730 const git_tree_entry **entry_out,
731 git_treebuilder *bld,
732 const char *filename,
733 const git_oid *id,
a7dbac0b 734 git_filemode_t filemode)
0ad6efa1
VM
735{
736 git_tree_entry *entry;
4d3f1f97 737 int error;
978fbb4c 738 git_strmap_iter pos;
0ad6efa1
VM
739
740 assert(bld && id && filename);
741
a7dbac0b 742 if (!valid_filemode(filemode))
eae0bfdc 743 return tree_error("failed to insert entry: invalid filemode for file", filename);
0ad6efa1 744
dce7b1a4 745 if (!valid_entry_name(bld->repo, filename))
eae0bfdc
PP
746 return tree_error("failed to insert entry: invalid name for a tree entry", filename);
747
748 if (git_oid_iszero(id))
749 return tree_error("failed to insert entry: invalid null OID", filename);
f4ad64c1 750
ea5bf6bb
CMN
751 if (filemode != GIT_FILEMODE_COMMIT &&
752 !git_object__is_valid(bld->repo, id, otype_from_mode(filemode)))
eae0bfdc 753 return tree_error("failed to insert entry: invalid object specified", filename);
2bbc7d3e 754
978fbb4c
CMN
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);
4ed9e939 758 git_oid_cpy((git_oid *) entry->oid, id);
978fbb4c 759 } else {
4ed9e939 760 entry = alloc_entry(filename, strlen(filename), id);
978fbb4c 761 GITERR_CHECK_ALLOC(entry);
93ab370b 762
73028af8 763 git_strmap_insert(bld->map, entry->filename, entry, &error);
0ad6efa1 764
978fbb4c
CMN
765 if (error < 0) {
766 git_tree_entry_free(entry);
767 giterr_set(GITERR_TREE, "failed to insert %s", filename);
768 return -1;
769 }
4d3f1f97
CMN
770 }
771
11d9f6b3
PK
772 entry->attr = filemode;
773
774 if (entry_out)
0ad6efa1
VM
775 *entry_out = entry;
776
3aa351ea 777 return 0;
0ad6efa1
VM
778}
779
0cbbdc26 780static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename)
0ad6efa1 781{
4d3f1f97
CMN
782 git_tree_entry *entry = NULL;
783 git_strmap_iter pos;
0ad6efa1
VM
784
785 assert(bld && filename);
786
4d3f1f97
CMN
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);
0ad6efa1
VM
790
791 return entry;
792}
793
0cbbdc26
KS
794const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename)
795{
796 return treebuilder_get(bld, filename);
797}
798
0ad6efa1
VM
799int git_treebuilder_remove(git_treebuilder *bld, const char *filename)
800{
4d3f1f97 801 git_tree_entry *entry = treebuilder_get(bld, filename);
0ad6efa1 802
4d3f1f97 803 if (entry == NULL)
eae0bfdc 804 return tree_error("failed to remove entry: file isn't in the tree", filename);
0ad6efa1 805
4d3f1f97 806 git_strmap_delete(bld->map, filename);
978fbb4c 807 git_tree_entry_free(entry);
4d3f1f97 808
3aa351ea 809 return 0;
0ad6efa1
VM
810}
811
dce7b1a4 812int git_treebuilder_write(git_oid *oid, git_treebuilder *bld)
87aaefe2
MT
813{
814 int error;
815 git_buf buffer = GIT_BUF_INIT;
816
817 error = git_treebuilder_write_with_buffer(oid, bld, &buffer);
818
819 git_buf_free(&buffer);
820 return error;
821}
822
823int git_treebuilder_write_with_buffer(git_oid *oid, git_treebuilder *bld, git_buf *tree)
0ad6efa1 824{
e2237179 825 int error = 0;
fcc60066 826 size_t i, entrycount;
9462c471 827 git_odb *odb;
4d3f1f97 828 git_tree_entry *entry;
4f9327fa 829 git_vector entries = GIT_VECTOR_INIT;
0ad6efa1
VM
830
831 assert(bld);
87aaefe2
MT
832 assert(tree);
833
834 git_buf_clear(tree);
0ad6efa1 835
fcc60066 836 entrycount = git_strmap_num_entries(bld->map);
4f9327fa
PS
837 if ((error = git_vector_init(&entries, entrycount, entry_sort_cmp)) < 0)
838 goto out;
0ad6efa1 839
87aaefe2 840 if (tree->asize == 0 &&
4f9327fa
PS
841 (error = git_buf_grow(tree, entrycount * 72)) < 0)
842 goto out;
87aaefe2 843
4d3f1f97 844 git_strmap_foreach_value(bld->map, entry, {
4f9327fa
PS
845 if ((error = git_vector_insert(&entries, entry)) < 0)
846 goto out;
4d3f1f97 847 });
afeecf4f 848
4d3f1f97
CMN
849 git_vector_sort(&entries);
850
4d3f1f97 851 for (i = 0; i < entries.length && !error; ++i) {
8d1e71f5 852 entry = git_vector_get(&entries, i);
0ad6efa1 853
87aaefe2
MT
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);
0ad6efa1 857
06abbb7f 858 if (git_buf_oom(tree)) {
e2237179 859 error = -1;
06abbb7f
PS
860 goto out;
861 }
e2237179 862 }
9462c471 863
06abbb7f 864 if ((error = git_repository_odb__weakptr(&odb, bld->repo)) == 0)
87aaefe2 865 error = git_odb_write(oid, odb, tree->ptr, tree->size, GIT_OBJ_TREE);
0ad6efa1 866
4f9327fa 867out:
f5c874a4
CMN
868 git_vector_free(&entries);
869
e2237179 870 return error;
0ad6efa1
VM
871}
872
978fbb4c 873void git_treebuilder_filter(
e120123e
RB
874 git_treebuilder *bld,
875 git_treebuilder_filter_cb filter,
876 void *payload)
0ad6efa1 877{
4d3f1f97 878 const char *filename;
e2237179 879 git_tree_entry *entry;
0ad6efa1
VM
880
881 assert(bld && filter);
882
4d3f1f97
CMN
883 git_strmap_foreach(bld->map, filename, entry, {
884 if (filter(entry, payload)) {
4d3f1f97 885 git_strmap_delete(bld->map, filename);
978fbb4c 886 git_tree_entry_free(entry);
4d3f1f97
CMN
887 }
888 });
0ad6efa1
VM
889}
890
891void git_treebuilder_clear(git_treebuilder *bld)
892{
e2237179
RB
893 git_tree_entry *e;
894
0ad6efa1
VM
895 assert(bld);
896
4d3f1f97 897 git_strmap_foreach_value(bld->map, e, git_tree_entry_free(e));
978fbb4c 898 git_strmap_clear(bld->map);
0ad6efa1
VM
899}
900
901void git_treebuilder_free(git_treebuilder *bld)
902{
b0b3b4e3 903 if (bld == NULL)
904 return;
905
0ad6efa1 906 git_treebuilder_clear(bld);
4d3f1f97 907 git_strmap_free(bld->map);
3286c408 908 git__free(bld);
0ad6efa1
VM
909}
910
0e2fcca8
VM
911static size_t subpath_len(const char *path)
912{
913 const char *slash_pos = strchr(path, '/');
914 if (slash_pos == NULL)
915 return strlen(path);
916
917 return slash_pos - path;
918}
919
920int git_tree_entry_bypath(
921 git_tree_entry **entry_out,
58206c9a 922 const git_tree *root,
0e2fcca8 923 const char *path)
3fa735ca 924{
e172cf08 925 int error = 0;
3fa735ca 926 git_tree *subtree;
0e2fcca8
VM
927 const git_tree_entry *entry;
928 size_t filename_len;
3fa735ca 929
0e2fcca8
VM
930 /* Find how long is the current path component (i.e.
931 * the filename between two slashes */
932 filename_len = subpath_len(path);
3fa735ca 933
0e2fcca8 934 if (filename_len == 0) {
909d5494 935 giterr_set(GITERR_TREE, "invalid tree path given");
0e2fcca8 936 return GIT_ENOTFOUND;
3aa351ea 937 }
3fa735ca 938
0e2fcca8 939 entry = entry_fromname(root, path, filename_len);
3fa735ca 940
3aa351ea
CMN
941 if (entry == NULL) {
942 giterr_set(GITERR_TREE,
901434b0 943 "the path '%.*s' does not exist in the given tree", (int) filename_len, path);
904b67e6 944 return GIT_ENOTFOUND;
3aa351ea 945 }
0ad6efa1 946
0e2fcca8 947 switch (path[filename_len]) {
2031760c 948 case '/':
0e2fcca8
VM
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,
901434b0 953 "the path '%.*s' exists but is not a tree", (int) filename_len, path);
dc1f4b32 954 return GIT_ENOTFOUND;
0e2fcca8
VM
955 }
956
eae0bfdc 957 /* If there's only a slash left in the path, we
0e2fcca8
VM
958 * return the current entry; otherwise, we keep
959 * walking down the path */
960 if (path[filename_len + 1] != '\0')
961 break;
eae0bfdc 962 /* fall through */
0e2fcca8
VM
963 case '\0':
964 /* If there are no more components in the path, return
965 * this entry */
529f342a 966 return git_tree_entry_dup(entry_out, entry);
0e2fcca8 967 }
9432af36 968
60a194aa 969 if (git_tree_lookup(&subtree, root->object.repo, entry->oid) < 0)
0e2fcca8 970 return -1;
3fa735ca 971
0e2fcca8
VM
972 error = git_tree_entry_bypath(
973 entry_out,
9432af36 974 subtree,
0e2fcca8 975 path + filename_len + 1
9432af36 976 );
3fa735ca 977
45e79e37 978 git_tree_free(subtree);
3fa735ca 979 return error;
980}
981
c6f42953 982static int tree_walk(
f45d51ff 983 const git_tree *tree,
9432af36 984 git_treewalk_cb callback,
97769280 985 git_buf *path,
c6f42953
MS
986 void *payload,
987 bool preorder)
da37654d 988{
e172cf08 989 int error = 0;
16248ee2 990 size_t i;
e2237179 991 const git_tree_entry *entry;
da37654d 992
e2e4bae9 993 git_array_foreach(tree->entries, i, entry) {
a6bf1687 994 if (preorder) {
26c1cb91
RB
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");
998 break;
999 }
1000 if (error > 0) { /* positive value skips this entry */
4e01e302 1001 error = 0;
a6bf1687 1002 continue;
4e01e302 1003 }
a6bf1687 1004 }
da37654d 1005
9d0011fd 1006 if (git_tree_entry__is_tree(entry)) {
da37654d 1007 git_tree *subtree;
fa6420f7 1008 size_t path_len = git_buf_len(path);
da37654d 1009
60a194aa 1010 error = git_tree_lookup(&subtree, tree->object.repo, entry->oid);
25e0b157 1011 if (error < 0)
97769280 1012 break;
da37654d 1013
97769280
RB
1014 /* append the next entry to the path */
1015 git_buf_puts(path, entry->filename);
1016 git_buf_putc(path, '/');
da37654d 1017
cb8a7961 1018 if (git_buf_oom(path))
25e0b157
RB
1019 error = -1;
1020 else
1021 error = tree_walk(subtree, callback, path, payload, preorder);
da37654d 1022
d7fc2eb2 1023 git_tree_free(subtree);
2031760c
RB
1024 if (error != 0)
1025 break;
da37654d 1026
97769280 1027 git_buf_truncate(path, path_len);
da37654d 1028 }
c6f42953 1029
25e0b157 1030 if (!preorder) {
26c1cb91
RB
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");
1034 break;
1035 }
25e0b157
RB
1036 error = 0;
1037 }
da37654d
VM
1038 }
1039
2031760c 1040 return error;
da37654d
VM
1041}
1042
e120123e 1043int git_tree_walk(
f45d51ff 1044 const git_tree *tree,
e120123e
RB
1045 git_treewalk_mode mode,
1046 git_treewalk_cb callback,
1047 void *payload)
da37654d 1048{
e172cf08 1049 int error = 0;
97769280 1050 git_buf root_path = GIT_BUF_INIT;
da37654d 1051
e2237179 1052 if (mode != GIT_TREEWALK_POST && mode != GIT_TREEWALK_PRE) {
909d5494 1053 giterr_set(GITERR_INVALID, "invalid walking mode for tree walk");
e120123e 1054 return -1;
da37654d 1055 }
97769280 1056
e2237179
RB
1057 error = tree_walk(
1058 tree, callback, &root_path, payload, (mode == GIT_TREEWALK_PRE));
1059
97769280
RB
1060 git_buf_free(&root_path);
1061
1062 return error;
da37654d 1063}
a1fdea28 1064
9464f9eb
CMN
1065static int compare_entries(const void *_a, const void *_b)
1066{
1067 const git_tree_update *a = (git_tree_update *) _a;
1068 const git_tree_update *b = (git_tree_update *) _b;
1069
1070 return strcmp(a->path, b->path);
1071}
1072
1073static int on_dup_entry(void **old, void *new)
1074{
1075 GIT_UNUSED(old); GIT_UNUSED(new);
1076
1077 giterr_set(GITERR_TREE, "duplicate entries given for update");
1078 return -1;
1079}
1080
1081/*
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.
1085 */
1086typedef struct {
1087 git_treebuilder *bld;
1088 git_tree *tree;
1089 char *name;
1090} tree_stack_entry;
1091
1092/** Count how many slashes (i.e. path components) there are in this string */
1093GIT_INLINE(size_t) count_slashes(const char *path)
1094{
1095 size_t count = 0;
1096 const char *slash;
1097
1098 while ((slash = strchr(path, '/')) != NULL) {
1099 count++;
1100 path = slash + 1;
1101 }
1102
1103 return count;
1104}
1105
1106static bool next_component(git_buf *out, const char *in)
1107{
1108 const char *slash = strchr(in, '/');
1109
1110 git_buf_clear(out);
1111
1112 if (slash)
1113 git_buf_put(out, in, slash - in);
1114
1115 return !!slash;
1116}
1117
1118static int create_popped_tree(tree_stack_entry *current, tree_stack_entry *popped, git_buf *component)
1119{
1120 int error;
1121 git_oid new_tree;
1122
1123 git_tree_free(popped->tree);
a2cb4713
CMN
1124
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);
1130 return error;
1131 }
1132
9464f9eb
CMN
1133 error = git_treebuilder_write(&new_tree, popped->bld);
1134 git_treebuilder_free(popped->bld);
1135
1136 if (error < 0) {
1137 git__free(popped->name);
1138 return error;
1139 }
1140
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);
1145
1146 GITERR_CHECK_ALLOC(component->ptr);
1147
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");
1154 return -1;
1155 }
1156 }
1157
1158 return git_treebuilder_insert(NULL, current->bld, component->ptr, &new_tree, GIT_FILEMODE_TREE);
1159}
1160
1161int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseline, size_t nupdates, const git_tree_update *updates)
1162{
1163 git_array_t(tree_stack_entry) stack = GIT_ARRAY_INIT;
1164 tree_stack_entry *root_elem;
1165 git_vector entries;
1166 int error;
1167 size_t i;
1168 git_buf component = GIT_BUF_INIT;
1169
1170 if ((error = git_vector_init(&entries, nupdates, compare_entries)) < 0)
1171 return error;
1172
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)
1176 goto cleanup;
1177 }
1178
1179 root_elem = git_array_alloc(stack);
1180 GITERR_CHECK_ALLOC(root_elem);
1181 memset(root_elem, 0, sizeof(*root_elem));
1182
1183 if (baseline && (error = git_tree_dup(&root_elem->tree, baseline)) < 0)
1184 goto cleanup;
1185
1186 if ((error = git_treebuilder_new(&root_elem->bld, repo, root_elem->tree)) < 0)
1187 goto cleanup;
1188
1189 for (i = 0; i < nupdates; i++) {
b85929c5
CMN
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);
9464f9eb
CMN
1192 size_t common_prefix = 0, steps_up, j;
1193 const char *path;
1194
1195 /* Figure out how much we need to change from the previous tree */
1196 if (last_update)
1197 common_prefix = git_path_common_dirlen(last_update->path, update->path);
1198
1199 /*
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
1203 * one.
1204 */
1205 steps_up = last_update == NULL ? 0 : count_slashes(&last_update->path[common_prefix]);
1206
1207 for (j = 0; j < steps_up; j++) {
1208 tree_stack_entry *current, *popped = git_array_pop(stack);
1209 assert(popped);
1210
1211 current = git_array_last(stack);
1212 assert(current);
1213
1214 if ((error = create_popped_tree(current, popped, &component)) < 0)
1215 goto cleanup;
1216 }
1217
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;
1223
1224 last = git_array_last(stack);
1225 entry = last->tree ? git_tree_entry_byname(last->tree, component.ptr) : NULL;
89776585
CMN
1226 if (!entry)
1227 entry = treebuilder_get(last->bld, component.ptr);
1228
9464f9eb
CMN
1229 if (entry && git_tree_entry_type(entry) != GIT_OBJ_TREE) {
1230 giterr_set(GITERR_TREE, "D/F conflict when updating tree");
1231 error = -1;
1232 goto cleanup;
1233 }
1234
1235 new_entry = git_array_alloc(stack);
1236 GITERR_CHECK_ALLOC(new_entry);
1237 memset(new_entry, 0, sizeof(*new_entry));
1238
1239 new_entry->tree = NULL;
1240 if (entry && (error = git_tree_lookup(&new_entry->tree, repo, git_tree_entry_id(entry))) < 0)
1241 goto cleanup;
1242
1243 if ((error = git_treebuilder_new(&new_entry->bld, repo, new_entry->tree)) < 0)
1244 goto cleanup;
1245
1246 new_entry->name = git__strdup(component.ptr);
1247 GITERR_CHECK_ALLOC(new_entry->name);
1248
1249 /* Get to the start of the next component */
1250 path += component.size + 1;
1251 }
1252
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:
1256 {
1257 /* Make sure we're replacing something of the same type */
1258 tree_stack_entry *last = git_array_last(stack);
53412305 1259 char *basename = git_path_basename(update->path);
9464f9eb
CMN
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)) {
53412305 1262 git__free(basename);
909d5494 1263 giterr_set(GITERR_TREE, "cannot replace '%s' with '%s' at '%s'",
9464f9eb
CMN
1264 git_object_type2string(git_tree_entry_type(e)),
1265 git_object_type2string(git_object__type_from_filemode(update->filemode)),
1266 update->path);
53412305
CMN
1267 error = -1;
1268 goto cleanup;
9464f9eb
CMN
1269 }
1270
1271 error = git_treebuilder_insert(NULL, last->bld, basename, &update->id, update->filemode);
53412305 1272 git__free(basename);
9464f9eb
CMN
1273 break;
1274 }
1275 case GIT_TREE_UPDATE_REMOVE:
53412305
CMN
1276 {
1277 char *basename = git_path_basename(update->path);
1278 error = git_treebuilder_remove(git_array_last(stack)->bld, basename);
1279 git__free(basename);
9464f9eb 1280 break;
53412305 1281 }
9464f9eb 1282 default:
909d5494 1283 giterr_set(GITERR_TREE, "unknown action for update");
9464f9eb
CMN
1284 error = -1;
1285 goto cleanup;
1286 }
1287
1288 if (error < 0)
1289 goto cleanup;
1290 }
1291
1292 /* We're done, go up the stack again and write out the tree */
1293 {
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 */
1298 if (!current)
1299 break;
1300
1301 if ((error = create_popped_tree(current, popped, &component)) < 0)
1302 goto cleanup;
1303 }
1304
1305 /* Write out the root tree */
1306 git__free(popped->name);
1307 git_tree_free(popped->tree);
1308
1309 error = git_treebuilder_write(out, popped->bld);
1310 git_treebuilder_free(popped->bld);
1311 if (error < 0)
1312 goto cleanup;
1313 }
1314
1315cleanup:
1316 {
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);
1321 git__free(e->name);
1322 }
1323 }
1324
53412305 1325 git_buf_free(&component);
9464f9eb
CMN
1326 git_array_clear(stack);
1327 git_vector_free(&entries);
1328 return error;
1329}