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