]>
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 | ||
48 | /* 12XXXX means commit */ | |
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 | |
2580077f | 450 | filename_len = nul - buffer; |
e2e4bae9 | 451 | /* Allocate the entry */ |
0e2fcca8 | 452 | { |
e2e4bae9 | 453 | entry = git_array_alloc(tree->entries); |
0e2fcca8 VM |
454 | GITERR_CHECK_ALLOC(entry); |
455 | ||
0e2fcca8 | 456 | entry->attr = attr; |
4ed9e939 CMN |
457 | entry->filename_len = filename_len; |
458 | entry->filename = buffer; | |
459 | entry->oid = (git_oid *) ((char *) buffer + filename_len + 1); | |
0e2fcca8 | 460 | } |
d8603ed9 | 461 | |
2580077f | 462 | buffer += filename_len + 1; |
d8603ed9 | 463 | buffer += GIT_OID_RAWSZ; |
d8603ed9 VM |
464 | } |
465 | ||
3aa351ea | 466 | return 0; |
d8603ed9 | 467 | } |
58519018 | 468 | |
a8122b5d | 469 | static size_t find_next_dir(const char *dirname, git_index *index, size_t start) |
8255c69b | 470 | { |
a8122b5d | 471 | size_t dirlen, i, entries = git_index_entrycount(index); |
8255c69b CMN |
472 | |
473 | dirlen = strlen(dirname); | |
474 | for (i = start; i < entries; ++i) { | |
f45d51ff | 475 | const git_index_entry *entry = git_index_get_byindex(index, i); |
8255c69b CMN |
476 | if (strlen(entry->path) < dirlen || |
477 | memcmp(entry->path, dirname, dirlen) || | |
478 | (dirlen > 0 && entry->path[dirlen] != '/')) { | |
479 | break; | |
480 | } | |
481 | } | |
482 | ||
483 | return i; | |
484 | } | |
485 | ||
0e2fcca8 VM |
486 | static int append_entry( |
487 | git_treebuilder *bld, | |
488 | const char *filename, | |
489 | const git_oid *id, | |
a7dbac0b | 490 | git_filemode_t filemode) |
9ef9e8c3 VM |
491 | { |
492 | git_tree_entry *entry; | |
4d3f1f97 | 493 | int error = 0; |
9ef9e8c3 | 494 | |
dce7b1a4 | 495 | if (!valid_entry_name(bld->repo, filename)) |
cfeef7ce | 496 | return tree_error("Failed to insert entry. Invalid name for a tree entry", filename); |
0d778b1a | 497 | |
4ed9e939 | 498 | entry = alloc_entry(filename, strlen(filename), id); |
3aa351ea | 499 | GITERR_CHECK_ALLOC(entry); |
9ef9e8c3 | 500 | |
a7dbac0b | 501 | entry->attr = (uint16_t)filemode; |
9ef9e8c3 | 502 | |
4d3f1f97 CMN |
503 | git_strmap_insert(bld->map, entry->filename, entry, error); |
504 | if (error < 0) { | |
966fb207 | 505 | git_tree_entry_free(entry); |
4d3f1f97 | 506 | giterr_set(GITERR_TREE, "failed to append entry %s to the tree builder", filename); |
3aa351ea | 507 | return -1; |
e2237179 | 508 | } |
9ef9e8c3 | 509 | |
3aa351ea | 510 | return 0; |
9ef9e8c3 VM |
511 | } |
512 | ||
9462c471 VM |
513 | static int write_tree( |
514 | git_oid *oid, | |
515 | git_repository *repo, | |
516 | git_index *index, | |
517 | const char *dirname, | |
a8122b5d | 518 | size_t start) |
47d8ec56 | 519 | { |
4a619797 | 520 | git_treebuilder *bld = NULL; |
a8122b5d | 521 | size_t i, entries = git_index_entrycount(index); |
4a619797 CMN |
522 | int error; |
523 | size_t dirname_len = strlen(dirname); | |
8255c69b CMN |
524 | const git_tree_cache *cache; |
525 | ||
526 | cache = git_tree_cache_get(index->tree, dirname); | |
c2f8b215 | 527 | if (cache != NULL && cache->entry_count >= 0){ |
8255c69b | 528 | git_oid_cpy(oid, &cache->oid); |
a8122b5d | 529 | return (int)find_next_dir(dirname, index, start); |
8255c69b | 530 | } |
29e1789b | 531 | |
208a2c8a | 532 | if ((error = git_treebuilder_new(&bld, repo, NULL)) < 0 || bld == NULL) |
3aa351ea | 533 | return -1; |
932d1baf | 534 | |
4a619797 CMN |
535 | /* |
536 | * This loop is unfortunate, but necessary. The index doesn't have | |
537 | * any directores, so we need to handle that manually, and we | |
538 | * need to keep track of the current position. | |
539 | */ | |
540 | for (i = start; i < entries; ++i) { | |
f45d51ff | 541 | const git_index_entry *entry = git_index_get_byindex(index, i); |
16248ee2 | 542 | const char *filename, *next_slash; |
4a619797 CMN |
543 | |
544 | /* | |
545 | * If we've left our (sub)tree, exit the loop and return. The | |
546 | * first check is an early out (and security for the | |
547 | * third). The second check is a simple prefix comparison. The | |
548 | * third check catches situations where there is a directory | |
549 | * win32/sys and a file win32mmap.c. Without it, the following | |
550 | * code believes there is a file win32/mmap.c | |
551 | */ | |
552 | if (strlen(entry->path) < dirname_len || | |
553 | memcmp(entry->path, dirname, dirname_len) || | |
554 | (dirname_len > 0 && entry->path[dirname_len] != '/')) { | |
47d8ec56 | 555 | break; |
4a619797 | 556 | } |
932d1baf | 557 | |
4a619797 CMN |
558 | filename = entry->path + dirname_len; |
559 | if (*filename == '/') | |
560 | filename++; | |
561 | next_slash = strchr(filename, '/'); | |
562 | if (next_slash) { | |
563 | git_oid sub_oid; | |
564 | int written; | |
565 | char *subdir, *last_comp; | |
566 | ||
567 | subdir = git__strndup(entry->path, next_slash - entry->path); | |
3aa351ea | 568 | GITERR_CHECK_ALLOC(subdir); |
29e1789b | 569 | |
4a619797 | 570 | /* Write out the subtree */ |
9462c471 | 571 | written = write_tree(&sub_oid, repo, index, subdir, i); |
4a619797 | 572 | if (written < 0) { |
cfeef7ce | 573 | git__free(subdir); |
3aa351ea | 574 | goto on_error; |
4a619797 CMN |
575 | } else { |
576 | i = written - 1; /* -1 because of the loop increment */ | |
29e1789b | 577 | } |
932d1baf | 578 | |
4a619797 CMN |
579 | /* |
580 | * We need to figure out what we want toinsert | |
581 | * into this tree. If we're traversing | |
582 | * deps/zlib/, then we only want to write | |
583 | * 'zlib' into the tree. | |
584 | */ | |
585 | last_comp = strrchr(subdir, '/'); | |
586 | if (last_comp) { | |
587 | last_comp++; /* Get rid of the '/' */ | |
588 | } else { | |
589 | last_comp = subdir; | |
590 | } | |
cfeef7ce | 591 | |
9ef9e8c3 | 592 | error = append_entry(bld, last_comp, &sub_oid, S_IFDIR); |
3286c408 | 593 | git__free(subdir); |
cfeef7ce | 594 | if (error < 0) |
3aa351ea | 595 | goto on_error; |
4a619797 | 596 | } else { |
d541170c | 597 | error = append_entry(bld, filename, &entry->id, entry->mode); |
cfeef7ce | 598 | if (error < 0) |
3aa351ea | 599 | goto on_error; |
47d8ec56 | 600 | } |
29e1789b | 601 | } |
932d1baf | 602 | |
dce7b1a4 | 603 | if (git_treebuilder_write(oid, bld) < 0) |
3aa351ea | 604 | goto on_error; |
29e1789b | 605 | |
4a619797 | 606 | git_treebuilder_free(bld); |
a8122b5d | 607 | return (int)i; |
4a619797 | 608 | |
3aa351ea CMN |
609 | on_error: |
610 | git_treebuilder_free(bld); | |
611 | return -1; | |
29e1789b VM |
612 | } |
613 | ||
16248ee2 RB |
614 | int git_tree__write_index( |
615 | git_oid *oid, git_index *index, git_repository *repo) | |
29e1789b | 616 | { |
3aa351ea | 617 | int ret; |
7465e873 | 618 | git_tree *tree; |
3f0d0c85 | 619 | bool old_ignore_case = false; |
29e1789b | 620 | |
276ea401 | 621 | assert(oid && index && repo); |
47d8ec56 | 622 | |
f92bcaea | 623 | if (git_index_has_conflicts(index)) { |
624 | giterr_set(GITERR_INDEX, | |
625 | "Cannot create a tree from a not fully merged index."); | |
626 | return GIT_EUNMERGED; | |
627 | } | |
628 | ||
c2f8b215 | 629 | if (index->tree != NULL && index->tree->entry_count >= 0) { |
8255c69b | 630 | git_oid_cpy(oid, &index->tree->oid); |
3aa351ea | 631 | return 0; |
8255c69b CMN |
632 | } |
633 | ||
3f0d0c85 | 634 | /* The tree cache didn't help us; we'll have to write |
cb53669e | 635 | * out a tree. If the index is ignore_case, we must |
3f0d0c85 PK |
636 | * make it case-sensitive for the duration of the tree-write |
637 | * operation. */ | |
638 | ||
639 | if (index->ignore_case) { | |
640 | old_ignore_case = true; | |
cb53669e | 641 | git_index__set_ignore_case(index, false); |
3f0d0c85 PK |
642 | } |
643 | ||
3aa351ea | 644 | ret = write_tree(oid, repo, index, "", 0); |
3f0d0c85 PK |
645 | |
646 | if (old_ignore_case) | |
cb53669e | 647 | git_index__set_ignore_case(index, true); |
3f0d0c85 | 648 | |
7465e873 CMN |
649 | index->tree = NULL; |
650 | ||
651 | if (ret < 0) | |
652 | return ret; | |
653 | ||
654 | git_pool_clear(&index->tree_pool); | |
655 | ||
656 | if ((ret = git_tree_lookup(&tree, repo, oid)) < 0) | |
657 | return ret; | |
658 | ||
659 | /* Read the tree cache into the index */ | |
660 | ret = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool); | |
661 | git_tree_free(tree); | |
662 | ||
663 | return ret; | |
47d8ec56 | 664 | } |
0ad6efa1 | 665 | |
208a2c8a | 666 | int git_treebuilder_new( |
dce7b1a4 ET |
667 | git_treebuilder **builder_p, |
668 | git_repository *repo, | |
669 | const git_tree *source) | |
0ad6efa1 VM |
670 | { |
671 | git_treebuilder *bld; | |
4d3f1f97 | 672 | size_t i; |
0ad6efa1 | 673 | |
dce7b1a4 | 674 | assert(builder_p && repo); |
0ad6efa1 VM |
675 | |
676 | bld = git__calloc(1, sizeof(git_treebuilder)); | |
3aa351ea | 677 | GITERR_CHECK_ALLOC(bld); |
0ad6efa1 | 678 | |
dce7b1a4 ET |
679 | bld->repo = repo; |
680 | ||
4d3f1f97 | 681 | if (git_strmap_alloc(&bld->map) < 0) { |
966fb207 | 682 | git__free(bld); |
4d3f1f97 CMN |
683 | return -1; |
684 | } | |
0ad6efa1 | 685 | |
0ad6efa1 | 686 | if (source != NULL) { |
e2237179 | 687 | git_tree_entry *entry_src; |
0ad6efa1 | 688 | |
e2e4bae9 | 689 | git_array_foreach(source->entries, i, entry_src) { |
66439b0b | 690 | if (append_entry( |
691 | bld, entry_src->filename, | |
60a194aa | 692 | entry_src->oid, |
f1c75b94 | 693 | entry_src->attr) < 0) |
3aa351ea | 694 | goto on_error; |
0ad6efa1 VM |
695 | } |
696 | } | |
697 | ||
698 | *builder_p = bld; | |
3aa351ea CMN |
699 | return 0; |
700 | ||
701 | on_error: | |
702 | git_treebuilder_free(bld); | |
703 | return -1; | |
0ad6efa1 VM |
704 | } |
705 | ||
2bbc7d3e ET |
706 | static git_otype otype_from_mode(git_filemode_t filemode) |
707 | { | |
708 | switch (filemode) { | |
709 | case GIT_FILEMODE_TREE: | |
710 | return GIT_OBJ_TREE; | |
711 | case GIT_FILEMODE_COMMIT: | |
712 | return GIT_OBJ_COMMIT; | |
713 | default: | |
714 | return GIT_OBJ_BLOB; | |
715 | } | |
716 | } | |
717 | ||
0e2fcca8 VM |
718 | int git_treebuilder_insert( |
719 | const git_tree_entry **entry_out, | |
720 | git_treebuilder *bld, | |
721 | const char *filename, | |
722 | const git_oid *id, | |
a7dbac0b | 723 | git_filemode_t filemode) |
0ad6efa1 VM |
724 | { |
725 | git_tree_entry *entry; | |
4d3f1f97 | 726 | int error; |
978fbb4c | 727 | git_strmap_iter pos; |
0ad6efa1 VM |
728 | |
729 | assert(bld && id && filename); | |
730 | ||
a7dbac0b | 731 | if (!valid_filemode(filemode)) |
cfeef7ce | 732 | return tree_error("Failed to insert entry. Invalid filemode for file", filename); |
0ad6efa1 | 733 | |
dce7b1a4 | 734 | if (!valid_entry_name(bld->repo, filename)) |
cfeef7ce | 735 | return tree_error("Failed to insert entry. Invalid name for a tree entry", filename); |
f4ad64c1 | 736 | |
ea5bf6bb CMN |
737 | if (filemode != GIT_FILEMODE_COMMIT && |
738 | !git_object__is_valid(bld->repo, id, otype_from_mode(filemode))) | |
2bbc7d3e ET |
739 | return tree_error("Failed to insert entry; invalid object specified", filename); |
740 | ||
978fbb4c CMN |
741 | pos = git_strmap_lookup_index(bld->map, filename); |
742 | if (git_strmap_valid_index(bld->map, pos)) { | |
743 | entry = git_strmap_value_at(bld->map, pos); | |
4ed9e939 | 744 | git_oid_cpy((git_oid *) entry->oid, id); |
978fbb4c | 745 | } else { |
4ed9e939 | 746 | entry = alloc_entry(filename, strlen(filename), id); |
978fbb4c | 747 | GITERR_CHECK_ALLOC(entry); |
93ab370b | 748 | |
978fbb4c | 749 | git_strmap_insert(bld->map, entry->filename, entry, error); |
0ad6efa1 | 750 | |
978fbb4c CMN |
751 | if (error < 0) { |
752 | git_tree_entry_free(entry); | |
753 | giterr_set(GITERR_TREE, "failed to insert %s", filename); | |
754 | return -1; | |
755 | } | |
4d3f1f97 CMN |
756 | } |
757 | ||
11d9f6b3 PK |
758 | entry->attr = filemode; |
759 | ||
760 | if (entry_out) | |
0ad6efa1 VM |
761 | *entry_out = entry; |
762 | ||
3aa351ea | 763 | return 0; |
0ad6efa1 VM |
764 | } |
765 | ||
0cbbdc26 | 766 | static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename) |
0ad6efa1 | 767 | { |
4d3f1f97 CMN |
768 | git_tree_entry *entry = NULL; |
769 | git_strmap_iter pos; | |
0ad6efa1 VM |
770 | |
771 | assert(bld && filename); | |
772 | ||
4d3f1f97 CMN |
773 | pos = git_strmap_lookup_index(bld->map, filename); |
774 | if (git_strmap_valid_index(bld->map, pos)) | |
775 | entry = git_strmap_value_at(bld->map, pos); | |
0ad6efa1 VM |
776 | |
777 | return entry; | |
778 | } | |
779 | ||
0cbbdc26 KS |
780 | const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename) |
781 | { | |
782 | return treebuilder_get(bld, filename); | |
783 | } | |
784 | ||
0ad6efa1 VM |
785 | int git_treebuilder_remove(git_treebuilder *bld, const char *filename) |
786 | { | |
4d3f1f97 | 787 | git_tree_entry *entry = treebuilder_get(bld, filename); |
0ad6efa1 | 788 | |
4d3f1f97 | 789 | if (entry == NULL) |
cfeef7ce | 790 | return tree_error("Failed to remove entry. File isn't in the tree", filename); |
0ad6efa1 | 791 | |
4d3f1f97 | 792 | git_strmap_delete(bld->map, filename); |
978fbb4c | 793 | git_tree_entry_free(entry); |
4d3f1f97 | 794 | |
3aa351ea | 795 | return 0; |
0ad6efa1 VM |
796 | } |
797 | ||
dce7b1a4 | 798 | int git_treebuilder_write(git_oid *oid, git_treebuilder *bld) |
0ad6efa1 | 799 | { |
e2237179 | 800 | int error = 0; |
fcc60066 | 801 | size_t i, entrycount; |
afeecf4f | 802 | git_buf tree = GIT_BUF_INIT; |
9462c471 | 803 | git_odb *odb; |
4d3f1f97 CMN |
804 | git_tree_entry *entry; |
805 | git_vector entries; | |
0ad6efa1 VM |
806 | |
807 | assert(bld); | |
808 | ||
fcc60066 CMN |
809 | entrycount = git_strmap_num_entries(bld->map); |
810 | if (git_vector_init(&entries, entrycount, entry_sort_cmp) < 0) | |
4d3f1f97 | 811 | return -1; |
0ad6efa1 | 812 | |
4d3f1f97 CMN |
813 | git_strmap_foreach_value(bld->map, entry, { |
814 | if (git_vector_insert(&entries, entry) < 0) | |
815 | return -1; | |
816 | }); | |
afeecf4f | 817 | |
4d3f1f97 CMN |
818 | git_vector_sort(&entries); |
819 | ||
820 | /* Grow the buffer beforehand to an estimated size */ | |
fcc60066 | 821 | error = git_buf_grow(&tree, entrycount * 72); |
0ad6efa1 | 822 | |
4d3f1f97 CMN |
823 | for (i = 0; i < entries.length && !error; ++i) { |
824 | git_tree_entry *entry = git_vector_get(&entries, i); | |
0ad6efa1 | 825 | |
afeecf4f VM |
826 | git_buf_printf(&tree, "%o ", entry->attr); |
827 | git_buf_put(&tree, entry->filename, entry->filename_len + 1); | |
60a194aa | 828 | git_buf_put(&tree, (char *)entry->oid->id, GIT_OID_RAWSZ); |
0ad6efa1 | 829 | |
e2237179 RB |
830 | if (git_buf_oom(&tree)) |
831 | error = -1; | |
832 | } | |
9462c471 | 833 | |
4d3f1f97 | 834 | |
e2237179 | 835 | if (!error && |
dce7b1a4 | 836 | !(error = git_repository_odb__weakptr(&odb, bld->repo))) |
e2237179 | 837 | error = git_odb_write(oid, odb, tree.ptr, tree.size, GIT_OBJ_TREE); |
0ad6efa1 | 838 | |
3aa351ea | 839 | git_buf_free(&tree); |
f5c874a4 CMN |
840 | git_vector_free(&entries); |
841 | ||
e2237179 | 842 | return error; |
0ad6efa1 VM |
843 | } |
844 | ||
978fbb4c | 845 | void git_treebuilder_filter( |
e120123e RB |
846 | git_treebuilder *bld, |
847 | git_treebuilder_filter_cb filter, | |
848 | void *payload) | |
0ad6efa1 | 849 | { |
4d3f1f97 | 850 | const char *filename; |
e2237179 | 851 | git_tree_entry *entry; |
0ad6efa1 VM |
852 | |
853 | assert(bld && filter); | |
854 | ||
4d3f1f97 CMN |
855 | git_strmap_foreach(bld->map, filename, entry, { |
856 | if (filter(entry, payload)) { | |
4d3f1f97 | 857 | git_strmap_delete(bld->map, filename); |
978fbb4c | 858 | git_tree_entry_free(entry); |
4d3f1f97 CMN |
859 | } |
860 | }); | |
0ad6efa1 VM |
861 | } |
862 | ||
863 | void git_treebuilder_clear(git_treebuilder *bld) | |
864 | { | |
e2237179 RB |
865 | git_tree_entry *e; |
866 | ||
0ad6efa1 VM |
867 | assert(bld); |
868 | ||
4d3f1f97 | 869 | git_strmap_foreach_value(bld->map, e, git_tree_entry_free(e)); |
978fbb4c | 870 | git_strmap_clear(bld->map); |
0ad6efa1 VM |
871 | } |
872 | ||
873 | void git_treebuilder_free(git_treebuilder *bld) | |
874 | { | |
b0b3b4e3 | 875 | if (bld == NULL) |
876 | return; | |
877 | ||
0ad6efa1 | 878 | git_treebuilder_clear(bld); |
4d3f1f97 | 879 | git_strmap_free(bld->map); |
3286c408 | 880 | git__free(bld); |
0ad6efa1 VM |
881 | } |
882 | ||
0e2fcca8 VM |
883 | static size_t subpath_len(const char *path) |
884 | { | |
885 | const char *slash_pos = strchr(path, '/'); | |
886 | if (slash_pos == NULL) | |
887 | return strlen(path); | |
888 | ||
889 | return slash_pos - path; | |
890 | } | |
891 | ||
892 | int git_tree_entry_bypath( | |
893 | git_tree_entry **entry_out, | |
58206c9a | 894 | const git_tree *root, |
0e2fcca8 | 895 | const char *path) |
3fa735ca | 896 | { |
e172cf08 | 897 | int error = 0; |
3fa735ca | 898 | git_tree *subtree; |
0e2fcca8 VM |
899 | const git_tree_entry *entry; |
900 | size_t filename_len; | |
3fa735ca | 901 | |
0e2fcca8 VM |
902 | /* Find how long is the current path component (i.e. |
903 | * the filename between two slashes */ | |
904 | filename_len = subpath_len(path); | |
3fa735ca | 905 | |
0e2fcca8 VM |
906 | if (filename_len == 0) { |
907 | giterr_set(GITERR_TREE, "Invalid tree path given"); | |
908 | return GIT_ENOTFOUND; | |
3aa351ea | 909 | } |
3fa735ca | 910 | |
0e2fcca8 | 911 | entry = entry_fromname(root, path, filename_len); |
3fa735ca | 912 | |
3aa351ea CMN |
913 | if (entry == NULL) { |
914 | giterr_set(GITERR_TREE, | |
84511143 | 915 | "the path '%.*s' does not exist in the given tree", filename_len, path); |
904b67e6 | 916 | return GIT_ENOTFOUND; |
3aa351ea | 917 | } |
0ad6efa1 | 918 | |
0e2fcca8 | 919 | switch (path[filename_len]) { |
2031760c | 920 | case '/': |
0e2fcca8 VM |
921 | /* If there are more components in the path... |
922 | * then this entry *must* be a tree */ | |
923 | if (!git_tree_entry__is_tree(entry)) { | |
924 | giterr_set(GITERR_TREE, | |
84511143 | 925 | "the path '%.*s' exists but is not a tree", filename_len, path); |
dc1f4b32 | 926 | return GIT_ENOTFOUND; |
0e2fcca8 VM |
927 | } |
928 | ||
929 | /* If there's only a slash left in the path, we | |
930 | * return the current entry; otherwise, we keep | |
931 | * walking down the path */ | |
932 | if (path[filename_len + 1] != '\0') | |
933 | break; | |
934 | ||
935 | case '\0': | |
936 | /* If there are no more components in the path, return | |
937 | * this entry */ | |
529f342a | 938 | return git_tree_entry_dup(entry_out, entry); |
0e2fcca8 | 939 | } |
9432af36 | 940 | |
60a194aa | 941 | if (git_tree_lookup(&subtree, root->object.repo, entry->oid) < 0) |
0e2fcca8 | 942 | return -1; |
3fa735ca | 943 | |
0e2fcca8 VM |
944 | error = git_tree_entry_bypath( |
945 | entry_out, | |
9432af36 | 946 | subtree, |
0e2fcca8 | 947 | path + filename_len + 1 |
9432af36 | 948 | ); |
3fa735ca | 949 | |
45e79e37 | 950 | git_tree_free(subtree); |
3fa735ca | 951 | return error; |
952 | } | |
953 | ||
c6f42953 | 954 | static int tree_walk( |
f45d51ff | 955 | const git_tree *tree, |
9432af36 | 956 | git_treewalk_cb callback, |
97769280 | 957 | git_buf *path, |
c6f42953 MS |
958 | void *payload, |
959 | bool preorder) | |
da37654d | 960 | { |
e172cf08 | 961 | int error = 0; |
16248ee2 | 962 | size_t i; |
e2237179 | 963 | const git_tree_entry *entry; |
da37654d | 964 | |
e2e4bae9 | 965 | git_array_foreach(tree->entries, i, entry) { |
a6bf1687 | 966 | if (preorder) { |
26c1cb91 RB |
967 | error = callback(path->ptr, entry, payload); |
968 | if (error < 0) { /* negative value stops iteration */ | |
969 | giterr_set_after_callback_function(error, "git_tree_walk"); | |
970 | break; | |
971 | } | |
972 | if (error > 0) { /* positive value skips this entry */ | |
4e01e302 | 973 | error = 0; |
a6bf1687 | 974 | continue; |
4e01e302 | 975 | } |
a6bf1687 | 976 | } |
da37654d | 977 | |
9d0011fd | 978 | if (git_tree_entry__is_tree(entry)) { |
da37654d | 979 | git_tree *subtree; |
fa6420f7 | 980 | size_t path_len = git_buf_len(path); |
da37654d | 981 | |
60a194aa | 982 | error = git_tree_lookup(&subtree, tree->object.repo, entry->oid); |
25e0b157 | 983 | if (error < 0) |
97769280 | 984 | break; |
da37654d | 985 | |
97769280 RB |
986 | /* append the next entry to the path */ |
987 | git_buf_puts(path, entry->filename); | |
988 | git_buf_putc(path, '/'); | |
da37654d | 989 | |
cb8a7961 | 990 | if (git_buf_oom(path)) |
25e0b157 RB |
991 | error = -1; |
992 | else | |
993 | error = tree_walk(subtree, callback, path, payload, preorder); | |
da37654d | 994 | |
d7fc2eb2 | 995 | git_tree_free(subtree); |
2031760c RB |
996 | if (error != 0) |
997 | break; | |
da37654d | 998 | |
97769280 | 999 | git_buf_truncate(path, path_len); |
da37654d | 1000 | } |
c6f42953 | 1001 | |
25e0b157 | 1002 | if (!preorder) { |
26c1cb91 RB |
1003 | error = callback(path->ptr, entry, payload); |
1004 | if (error < 0) { /* negative value stops iteration */ | |
1005 | giterr_set_after_callback_function(error, "git_tree_walk"); | |
1006 | break; | |
1007 | } | |
25e0b157 RB |
1008 | error = 0; |
1009 | } | |
da37654d VM |
1010 | } |
1011 | ||
2031760c | 1012 | return error; |
da37654d VM |
1013 | } |
1014 | ||
e120123e | 1015 | int git_tree_walk( |
f45d51ff | 1016 | const git_tree *tree, |
e120123e RB |
1017 | git_treewalk_mode mode, |
1018 | git_treewalk_cb callback, | |
1019 | void *payload) | |
da37654d | 1020 | { |
e172cf08 | 1021 | int error = 0; |
97769280 | 1022 | git_buf root_path = GIT_BUF_INIT; |
da37654d | 1023 | |
e2237179 | 1024 | if (mode != GIT_TREEWALK_POST && mode != GIT_TREEWALK_PRE) { |
e120123e RB |
1025 | giterr_set(GITERR_INVALID, "Invalid walking mode for tree walk"); |
1026 | return -1; | |
da37654d | 1027 | } |
97769280 | 1028 | |
e2237179 RB |
1029 | error = tree_walk( |
1030 | tree, callback, &root_path, payload, (mode == GIT_TREEWALK_PRE)); | |
1031 | ||
97769280 RB |
1032 | git_buf_free(&root_path); |
1033 | ||
1034 | return error; | |
da37654d | 1035 | } |
a1fdea28 | 1036 |