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