]>
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 | ||
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 | 23 | static 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 |
32 | GIT_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 | 54 | static int valid_entry_name(git_repository *repo, const char *filename) |
28c1451a | 55 | { |
cfeef7ce | 56 | return *filename != '\0' && |
c25aa7cd | 57 | git_path_validate(repo, filename, 0, |
dce7b1a4 | 58 | GIT_PATH_REJECT_TRAVERSAL | GIT_PATH_REJECT_DOT_GIT | GIT_PATH_REJECT_SLASH); |
28c1451a VM |
59 | } |
60 | ||
0c468633 | 61 | static 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 | 72 | int 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 | 81 | static 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 |
115 | struct tree_key_search { |
116 | const char *filename; | |
ee42bb0e | 117 | uint16_t filename_len; |
761aa2aa VM |
118 | }; |
119 | ||
28c1451a | 120 | static 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 | 155 | static 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 |
219 | void 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 | 227 | int git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source) |
0e2fcca8 | 228 | { |
4ed9e939 CMN |
229 | git_tree_entry *cpy; |
230 | ||
c25aa7cd | 231 | GIT_ASSERT_ARG(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 | 243 | void 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 | 252 | git_filemode_t git_tree_entry_filemode(const git_tree_entry *entry) |
003c2690 | 253 | { |
13f670a5 CMN |
254 | return normalize_filemode(entry->attr); |
255 | } | |
256 | ||
257 | git_filemode_t git_tree_entry_filemode_raw(const git_tree_entry *entry) | |
258 | { | |
259 | return entry->attr; | |
003c2690 VM |
260 | } |
261 | ||
0ad6efa1 | 262 | const char *git_tree_entry_name(const git_tree_entry *entry) |
003c2690 | 263 | { |
c25aa7cd | 264 | GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL); |
2a884588 VM |
265 | return entry->filename; |
266 | } | |
003c2690 | 267 | |
0ad6efa1 | 268 | const git_oid *git_tree_entry_id(const git_tree_entry *entry) |
2a884588 | 269 | { |
c25aa7cd | 270 | GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL); |
60a194aa | 271 | return entry->oid; |
003c2690 VM |
272 | } |
273 | ||
ac3d33df | 274 | git_object_t git_tree_entry_type(const git_tree_entry *entry) |
ff9a4c13 | 275 | { |
c25aa7cd | 276 | GIT_ASSERT_ARG_WITH_RETVAL(entry, GIT_OBJECT_INVALID); |
ff9a4c13 RG |
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 |
286 | int git_tree_entry_to_object( |
287 | git_object **object_out, | |
288 | git_repository *repo, | |
289 | const git_tree_entry *entry) | |
003c2690 | 290 | { |
c25aa7cd PP |
291 | GIT_ASSERT_ARG(entry); |
292 | GIT_ASSERT_ARG(object_out); | |
293 | ||
ac3d33df | 294 | return git_object_lookup(object_out, repo, entry->oid, GIT_OBJECT_ANY); |
122c3405 VM |
295 | } |
296 | ||
16248ee2 | 297 | static const git_tree_entry *entry_fromname( |
58206c9a | 298 | const git_tree *tree, const char *name, size_t name_len) |
2a884588 | 299 | { |
11d9f6b3 PK |
300 | size_t idx; |
301 | ||
e2e4bae9 | 302 | if (tree_key_search(&idx, tree, name, name_len) < 0) |
c4034e63 VM |
303 | return NULL; |
304 | ||
e2e4bae9 | 305 | return git_array_get(tree->entries, idx); |
2a884588 VM |
306 | } |
307 | ||
16248ee2 | 308 | const git_tree_entry *git_tree_entry_byname( |
58206c9a | 309 | const git_tree *tree, const char *filename) |
0e2fcca8 | 310 | { |
c25aa7cd PP |
311 | GIT_ASSERT_ARG_WITH_RETVAL(tree, NULL); |
312 | GIT_ASSERT_ARG_WITH_RETVAL(filename, NULL); | |
aadad405 | 313 | |
0e2fcca8 VM |
314 | return entry_fromname(tree, filename, strlen(filename)); |
315 | } | |
316 | ||
16248ee2 | 317 | const git_tree_entry *git_tree_entry_byindex( |
58206c9a | 318 | const git_tree *tree, size_t idx) |
003c2690 | 319 | { |
c25aa7cd | 320 | GIT_ASSERT_ARG_WITH_RETVAL(tree, NULL); |
e2e4bae9 | 321 | return git_array_get(tree->entries, idx); |
003c2690 VM |
322 | } |
323 | ||
f000ee4e CMN |
324 | const git_tree_entry *git_tree_entry_byid( |
325 | const git_tree *tree, const git_oid *id) | |
2031760c | 326 | { |
16248ee2 RB |
327 | size_t i; |
328 | const git_tree_entry *e; | |
2031760c | 329 | |
c25aa7cd | 330 | GIT_ASSERT_ARG_WITH_RETVAL(tree, NULL); |
2031760c | 331 | |
e2e4bae9 | 332 | git_array_foreach(tree->entries, i, e) { |
60a194aa | 333 | if (memcmp(&e->oid->id, &id->id, sizeof(id->id)) == 0) |
2031760c RB |
334 | return e; |
335 | } | |
336 | ||
337 | return NULL; | |
338 | } | |
339 | ||
e120123e | 340 | size_t git_tree_entrycount(const git_tree *tree) |
003c2690 | 341 | { |
c25aa7cd | 342 | GIT_ASSERT_ARG_WITH_RETVAL(tree, 0); |
e2e4bae9 | 343 | return tree->entries.size; |
003c2690 VM |
344 | } |
345 | ||
22a2d3d5 | 346 | size_t git_treebuilder_entrycount(git_treebuilder *bld) |
5fb98206 | 347 | { |
c25aa7cd | 348 | GIT_ASSERT_ARG_WITH_RETVAL(bld, 0); |
fcc60066 | 349 | |
22a2d3d5 | 350 | return git_strmap_size(bld->map); |
5fb98206 JW |
351 | } |
352 | ||
cfeef7ce | 353 | static int tree_error(const char *str, const char *path) |
d8603ed9 | 354 | { |
cfeef7ce | 355 | if (path) |
ac3d33df | 356 | git_error_set(GIT_ERROR_TREE, "%s - %s", str, path); |
cfeef7ce | 357 | else |
ac3d33df | 358 | git_error_set(GIT_ERROR_TREE, "%s", str); |
3aa351ea CMN |
359 | return -1; |
360 | } | |
2a884588 | 361 | |
ac3d33df | 362 | static int parse_mode(uint16_t *mode_out, const char *buffer, size_t buffer_len, const char **buffer_out) |
0174f21b | 363 | { |
ac3d33df JK |
364 | int32_t mode; |
365 | int error; | |
0174f21b | 366 | |
ac3d33df | 367 | if (!buffer_len || git__isspace(*buffer)) |
0174f21b CMN |
368 | return -1; |
369 | ||
ac3d33df JK |
370 | if ((error = git__strntol32(&mode, buffer, buffer_len, buffer_out, 8)) < 0) |
371 | return error; | |
372 | ||
373 | if (mode < 0 || mode > UINT16_MAX) | |
374 | return -1; | |
375 | ||
376 | *mode_out = mode; | |
0174f21b CMN |
377 | |
378 | return 0; | |
379 | } | |
380 | ||
ac3d33df | 381 | int git_tree__parse_raw(void *_tree, const char *data, size_t size) |
3aa351ea | 382 | { |
116bbdf0 | 383 | git_tree *tree = _tree; |
60a194aa CMN |
384 | const char *buffer; |
385 | const char *buffer_end; | |
386 | ||
ac3d33df JK |
387 | buffer = data; |
388 | buffer_end = buffer + size; | |
78606263 | 389 | |
ac3d33df | 390 | tree->odb_obj = NULL; |
e2e4bae9 | 391 | git_array_init_to_size(tree->entries, DEFAULT_TREE_SIZE); |
ac3d33df | 392 | GIT_ERROR_CHECK_ARRAY(tree->entries); |
e4def81a | 393 | |
003c2690 VM |
394 | while (buffer < buffer_end) { |
395 | git_tree_entry *entry; | |
2580077f CMN |
396 | size_t filename_len; |
397 | const char *nul; | |
ac3d33df | 398 | uint16_t attr; |
d8603ed9 | 399 | |
ac3d33df | 400 | if (parse_mode(&attr, buffer, buffer_end - buffer, &buffer) < 0 || !buffer) |
eae0bfdc | 401 | return tree_error("failed to parse tree: can't parse filemode", NULL); |
8e9bfa4c | 402 | |
ac3d33df JK |
403 | if (buffer >= buffer_end || (*buffer++) != ' ') |
404 | return tree_error("failed to parse tree: missing space after filemode", NULL); | |
405 | ||
2580077f | 406 | if ((nul = memchr(buffer, 0, buffer_end - buffer)) == NULL) |
eae0bfdc | 407 | return tree_error("failed to parse tree: object is corrupted", NULL); |
58519018 | 408 | |
ac3d33df | 409 | if ((filename_len = nul - buffer) == 0 || filename_len > UINT16_MAX) |
eae0bfdc | 410 | return tree_error("failed to parse tree: can't parse filename", NULL); |
4974e3a5 PS |
411 | |
412 | if ((buffer_end - (nul + 1)) < GIT_OID_RAWSZ) | |
eae0bfdc | 413 | return tree_error("failed to parse tree: can't parse OID", NULL); |
4974e3a5 | 414 | |
e2e4bae9 | 415 | /* Allocate the entry */ |
0e2fcca8 | 416 | { |
e2e4bae9 | 417 | entry = git_array_alloc(tree->entries); |
ac3d33df | 418 | GIT_ERROR_CHECK_ALLOC(entry); |
0e2fcca8 | 419 | |
0e2fcca8 | 420 | entry->attr = attr; |
ac3d33df | 421 | entry->filename_len = (uint16_t)filename_len; |
4ed9e939 CMN |
422 | entry->filename = buffer; |
423 | entry->oid = (git_oid *) ((char *) buffer + filename_len + 1); | |
0e2fcca8 | 424 | } |
d8603ed9 | 425 | |
2580077f | 426 | buffer += filename_len + 1; |
d8603ed9 | 427 | buffer += GIT_OID_RAWSZ; |
d8603ed9 VM |
428 | } |
429 | ||
3aa351ea | 430 | return 0; |
d8603ed9 | 431 | } |
58519018 | 432 | |
ac3d33df JK |
433 | int git_tree__parse(void *_tree, git_odb_object *odb_obj) |
434 | { | |
435 | git_tree *tree = _tree; | |
436 | ||
437 | if ((git_tree__parse_raw(tree, | |
438 | git_odb_object_data(odb_obj), | |
439 | git_odb_object_size(odb_obj))) < 0) | |
440 | return -1; | |
441 | ||
442 | if (git_odb_object_dup(&tree->odb_obj, odb_obj) < 0) | |
443 | return -1; | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
a8122b5d | 448 | static size_t find_next_dir(const char *dirname, git_index *index, size_t start) |
8255c69b | 449 | { |
a8122b5d | 450 | size_t dirlen, i, entries = git_index_entrycount(index); |
8255c69b CMN |
451 | |
452 | dirlen = strlen(dirname); | |
453 | for (i = start; i < entries; ++i) { | |
f45d51ff | 454 | const git_index_entry *entry = git_index_get_byindex(index, i); |
8255c69b CMN |
455 | if (strlen(entry->path) < dirlen || |
456 | memcmp(entry->path, dirname, dirlen) || | |
457 | (dirlen > 0 && entry->path[dirlen] != '/')) { | |
458 | break; | |
459 | } | |
460 | } | |
461 | ||
462 | return i; | |
463 | } | |
464 | ||
ac3d33df JK |
465 | static git_object_t otype_from_mode(git_filemode_t filemode) |
466 | { | |
467 | switch (filemode) { | |
468 | case GIT_FILEMODE_TREE: | |
469 | return GIT_OBJECT_TREE; | |
470 | case GIT_FILEMODE_COMMIT: | |
471 | return GIT_OBJECT_COMMIT; | |
472 | default: | |
473 | return GIT_OBJECT_BLOB; | |
474 | } | |
475 | } | |
476 | ||
477 | static int check_entry(git_repository *repo, const char *filename, const git_oid *id, git_filemode_t filemode) | |
478 | { | |
479 | if (!valid_filemode(filemode)) | |
480 | return tree_error("failed to insert entry: invalid filemode for file", filename); | |
481 | ||
482 | if (!valid_entry_name(repo, filename)) | |
483 | return tree_error("failed to insert entry: invalid name for a tree entry", filename); | |
484 | ||
22a2d3d5 | 485 | if (git_oid_is_zero(id)) |
ac3d33df JK |
486 | return tree_error("failed to insert entry: invalid null OID", filename); |
487 | ||
488 | if (filemode != GIT_FILEMODE_COMMIT && | |
489 | !git_object__is_valid(repo, id, otype_from_mode(filemode))) | |
490 | return tree_error("failed to insert entry: invalid object specified", filename); | |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
c25aa7cd PP |
495 | static int git_treebuilder__write_with_buffer( |
496 | git_oid *oid, | |
497 | git_treebuilder *bld, | |
498 | git_buf *buf) | |
499 | { | |
500 | int error = 0; | |
501 | size_t i, entrycount; | |
502 | git_odb *odb; | |
503 | git_tree_entry *entry; | |
504 | git_vector entries = GIT_VECTOR_INIT; | |
505 | ||
506 | git_buf_clear(buf); | |
507 | ||
508 | entrycount = git_strmap_size(bld->map); | |
509 | if ((error = git_vector_init(&entries, entrycount, entry_sort_cmp)) < 0) | |
510 | goto out; | |
511 | ||
512 | if (buf->asize == 0 && | |
513 | (error = git_buf_grow(buf, entrycount * 72)) < 0) | |
514 | goto out; | |
515 | ||
516 | git_strmap_foreach_value(bld->map, entry, { | |
517 | if ((error = git_vector_insert(&entries, entry)) < 0) | |
518 | goto out; | |
519 | }); | |
520 | ||
521 | git_vector_sort(&entries); | |
522 | ||
523 | for (i = 0; i < entries.length && !error; ++i) { | |
524 | entry = git_vector_get(&entries, i); | |
525 | ||
526 | git_buf_printf(buf, "%o ", entry->attr); | |
527 | git_buf_put(buf, entry->filename, entry->filename_len + 1); | |
528 | git_buf_put(buf, (char *)entry->oid->id, GIT_OID_RAWSZ); | |
529 | ||
530 | if (git_buf_oom(buf)) { | |
531 | error = -1; | |
532 | goto out; | |
533 | } | |
534 | } | |
535 | ||
536 | if ((error = git_repository_odb__weakptr(&odb, bld->repo)) == 0) | |
537 | error = git_odb_write(oid, odb, buf->ptr, buf->size, GIT_OBJECT_TREE); | |
538 | ||
539 | out: | |
540 | git_vector_free(&entries); | |
541 | ||
542 | return error; | |
543 | } | |
544 | ||
0e2fcca8 VM |
545 | static int append_entry( |
546 | git_treebuilder *bld, | |
547 | const char *filename, | |
548 | const git_oid *id, | |
6c7cee42 RD |
549 | git_filemode_t filemode, |
550 | bool validate) | |
9ef9e8c3 VM |
551 | { |
552 | git_tree_entry *entry; | |
4d3f1f97 | 553 | int error = 0; |
9ef9e8c3 | 554 | |
ac3d33df JK |
555 | if (validate && ((error = check_entry(bld->repo, filename, id, filemode)) < 0)) |
556 | return error; | |
0d778b1a | 557 | |
4ed9e939 | 558 | entry = alloc_entry(filename, strlen(filename), id); |
ac3d33df | 559 | GIT_ERROR_CHECK_ALLOC(entry); |
9ef9e8c3 | 560 | |
a7dbac0b | 561 | entry->attr = (uint16_t)filemode; |
9ef9e8c3 | 562 | |
22a2d3d5 | 563 | if ((error = git_strmap_set(bld->map, entry->filename, entry)) < 0) { |
966fb207 | 564 | git_tree_entry_free(entry); |
ac3d33df | 565 | git_error_set(GIT_ERROR_TREE, "failed to append entry %s to the tree builder", filename); |
3aa351ea | 566 | return -1; |
e2237179 | 567 | } |
9ef9e8c3 | 568 | |
3aa351ea | 569 | return 0; |
9ef9e8c3 VM |
570 | } |
571 | ||
9462c471 VM |
572 | static int write_tree( |
573 | git_oid *oid, | |
574 | git_repository *repo, | |
575 | git_index *index, | |
576 | const char *dirname, | |
87aaefe2 MT |
577 | size_t start, |
578 | git_buf *shared_buf) | |
47d8ec56 | 579 | { |
4a619797 | 580 | git_treebuilder *bld = NULL; |
a8122b5d | 581 | size_t i, entries = git_index_entrycount(index); |
4a619797 CMN |
582 | int error; |
583 | size_t dirname_len = strlen(dirname); | |
8255c69b CMN |
584 | const git_tree_cache *cache; |
585 | ||
586 | cache = git_tree_cache_get(index->tree, dirname); | |
c2f8b215 | 587 | if (cache != NULL && cache->entry_count >= 0){ |
8255c69b | 588 | git_oid_cpy(oid, &cache->oid); |
a8122b5d | 589 | return (int)find_next_dir(dirname, index, start); |
8255c69b | 590 | } |
29e1789b | 591 | |
208a2c8a | 592 | if ((error = git_treebuilder_new(&bld, repo, NULL)) < 0 || bld == NULL) |
3aa351ea | 593 | return -1; |
932d1baf | 594 | |
4a619797 CMN |
595 | /* |
596 | * This loop is unfortunate, but necessary. The index doesn't have | |
597 | * any directores, so we need to handle that manually, and we | |
598 | * need to keep track of the current position. | |
599 | */ | |
600 | for (i = start; i < entries; ++i) { | |
f45d51ff | 601 | const git_index_entry *entry = git_index_get_byindex(index, i); |
16248ee2 | 602 | const char *filename, *next_slash; |
4a619797 CMN |
603 | |
604 | /* | |
605 | * If we've left our (sub)tree, exit the loop and return. The | |
606 | * first check is an early out (and security for the | |
607 | * third). The second check is a simple prefix comparison. The | |
608 | * third check catches situations where there is a directory | |
609 | * win32/sys and a file win32mmap.c. Without it, the following | |
610 | * code believes there is a file win32/mmap.c | |
611 | */ | |
612 | if (strlen(entry->path) < dirname_len || | |
613 | memcmp(entry->path, dirname, dirname_len) || | |
614 | (dirname_len > 0 && entry->path[dirname_len] != '/')) { | |
47d8ec56 | 615 | break; |
4a619797 | 616 | } |
932d1baf | 617 | |
4a619797 CMN |
618 | filename = entry->path + dirname_len; |
619 | if (*filename == '/') | |
620 | filename++; | |
621 | next_slash = strchr(filename, '/'); | |
622 | if (next_slash) { | |
623 | git_oid sub_oid; | |
624 | int written; | |
625 | char *subdir, *last_comp; | |
626 | ||
627 | subdir = git__strndup(entry->path, next_slash - entry->path); | |
ac3d33df | 628 | GIT_ERROR_CHECK_ALLOC(subdir); |
29e1789b | 629 | |
4a619797 | 630 | /* Write out the subtree */ |
87aaefe2 | 631 | written = write_tree(&sub_oid, repo, index, subdir, i, shared_buf); |
4a619797 | 632 | if (written < 0) { |
cfeef7ce | 633 | git__free(subdir); |
3aa351ea | 634 | goto on_error; |
4a619797 CMN |
635 | } else { |
636 | i = written - 1; /* -1 because of the loop increment */ | |
29e1789b | 637 | } |
932d1baf | 638 | |
4a619797 CMN |
639 | /* |
640 | * We need to figure out what we want toinsert | |
641 | * into this tree. If we're traversing | |
642 | * deps/zlib/, then we only want to write | |
643 | * 'zlib' into the tree. | |
644 | */ | |
645 | last_comp = strrchr(subdir, '/'); | |
646 | if (last_comp) { | |
647 | last_comp++; /* Get rid of the '/' */ | |
648 | } else { | |
649 | last_comp = subdir; | |
650 | } | |
cfeef7ce | 651 | |
6c7cee42 | 652 | error = append_entry(bld, last_comp, &sub_oid, S_IFDIR, true); |
3286c408 | 653 | git__free(subdir); |
cfeef7ce | 654 | if (error < 0) |
3aa351ea | 655 | goto on_error; |
4a619797 | 656 | } else { |
6c7cee42 | 657 | error = append_entry(bld, filename, &entry->id, entry->mode, true); |
cfeef7ce | 658 | if (error < 0) |
3aa351ea | 659 | goto on_error; |
47d8ec56 | 660 | } |
29e1789b | 661 | } |
932d1baf | 662 | |
c25aa7cd | 663 | if (git_treebuilder__write_with_buffer(oid, bld, shared_buf) < 0) |
3aa351ea | 664 | goto on_error; |
29e1789b | 665 | |
4a619797 | 666 | git_treebuilder_free(bld); |
a8122b5d | 667 | return (int)i; |
4a619797 | 668 | |
3aa351ea CMN |
669 | on_error: |
670 | git_treebuilder_free(bld); | |
671 | return -1; | |
29e1789b VM |
672 | } |
673 | ||
16248ee2 RB |
674 | int git_tree__write_index( |
675 | git_oid *oid, git_index *index, git_repository *repo) | |
29e1789b | 676 | { |
3aa351ea | 677 | int ret; |
7465e873 | 678 | git_tree *tree; |
87aaefe2 | 679 | git_buf shared_buf = GIT_BUF_INIT; |
3f0d0c85 | 680 | bool old_ignore_case = false; |
29e1789b | 681 | |
c25aa7cd PP |
682 | GIT_ASSERT_ARG(oid); |
683 | GIT_ASSERT_ARG(index); | |
684 | GIT_ASSERT_ARG(repo); | |
47d8ec56 | 685 | |
f92bcaea | 686 | if (git_index_has_conflicts(index)) { |
ac3d33df | 687 | git_error_set(GIT_ERROR_INDEX, |
909d5494 | 688 | "cannot create a tree from a not fully merged index."); |
f92bcaea | 689 | return GIT_EUNMERGED; |
690 | } | |
691 | ||
c2f8b215 | 692 | if (index->tree != NULL && index->tree->entry_count >= 0) { |
8255c69b | 693 | git_oid_cpy(oid, &index->tree->oid); |
3aa351ea | 694 | return 0; |
8255c69b CMN |
695 | } |
696 | ||
3f0d0c85 | 697 | /* The tree cache didn't help us; we'll have to write |
cb53669e | 698 | * out a tree. If the index is ignore_case, we must |
3f0d0c85 PK |
699 | * make it case-sensitive for the duration of the tree-write |
700 | * operation. */ | |
701 | ||
702 | if (index->ignore_case) { | |
703 | old_ignore_case = true; | |
cb53669e | 704 | git_index__set_ignore_case(index, false); |
3f0d0c85 PK |
705 | } |
706 | ||
87aaefe2 | 707 | ret = write_tree(oid, repo, index, "", 0, &shared_buf); |
ac3d33df | 708 | git_buf_dispose(&shared_buf); |
3f0d0c85 PK |
709 | |
710 | if (old_ignore_case) | |
cb53669e | 711 | git_index__set_ignore_case(index, true); |
3f0d0c85 | 712 | |
7465e873 CMN |
713 | index->tree = NULL; |
714 | ||
715 | if (ret < 0) | |
716 | return ret; | |
717 | ||
718 | git_pool_clear(&index->tree_pool); | |
719 | ||
720 | if ((ret = git_tree_lookup(&tree, repo, oid)) < 0) | |
721 | return ret; | |
722 | ||
723 | /* Read the tree cache into the index */ | |
724 | ret = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool); | |
725 | git_tree_free(tree); | |
726 | ||
727 | return ret; | |
47d8ec56 | 728 | } |
0ad6efa1 | 729 | |
208a2c8a | 730 | int git_treebuilder_new( |
dce7b1a4 ET |
731 | git_treebuilder **builder_p, |
732 | git_repository *repo, | |
733 | const git_tree *source) | |
0ad6efa1 VM |
734 | { |
735 | git_treebuilder *bld; | |
4d3f1f97 | 736 | size_t i; |
0ad6efa1 | 737 | |
c25aa7cd PP |
738 | GIT_ASSERT_ARG(builder_p); |
739 | GIT_ASSERT_ARG(repo); | |
0ad6efa1 VM |
740 | |
741 | bld = git__calloc(1, sizeof(git_treebuilder)); | |
ac3d33df | 742 | GIT_ERROR_CHECK_ALLOC(bld); |
0ad6efa1 | 743 | |
dce7b1a4 ET |
744 | bld->repo = repo; |
745 | ||
22a2d3d5 | 746 | if (git_strmap_new(&bld->map) < 0) { |
966fb207 | 747 | git__free(bld); |
4d3f1f97 CMN |
748 | return -1; |
749 | } | |
0ad6efa1 | 750 | |
0ad6efa1 | 751 | if (source != NULL) { |
e2237179 | 752 | git_tree_entry *entry_src; |
0ad6efa1 | 753 | |
e2e4bae9 | 754 | git_array_foreach(source->entries, i, entry_src) { |
66439b0b | 755 | if (append_entry( |
756 | bld, entry_src->filename, | |
60a194aa | 757 | entry_src->oid, |
6c7cee42 RD |
758 | entry_src->attr, |
759 | false) < 0) | |
3aa351ea | 760 | goto on_error; |
0ad6efa1 VM |
761 | } |
762 | } | |
763 | ||
764 | *builder_p = bld; | |
3aa351ea CMN |
765 | return 0; |
766 | ||
767 | on_error: | |
768 | git_treebuilder_free(bld); | |
769 | return -1; | |
0ad6efa1 VM |
770 | } |
771 | ||
0e2fcca8 VM |
772 | int git_treebuilder_insert( |
773 | const git_tree_entry **entry_out, | |
774 | git_treebuilder *bld, | |
775 | const char *filename, | |
776 | const git_oid *id, | |
a7dbac0b | 777 | git_filemode_t filemode) |
0ad6efa1 VM |
778 | { |
779 | git_tree_entry *entry; | |
4d3f1f97 | 780 | int error; |
0ad6efa1 | 781 | |
c25aa7cd PP |
782 | GIT_ASSERT_ARG(bld); |
783 | GIT_ASSERT_ARG(id); | |
784 | GIT_ASSERT_ARG(filename); | |
0ad6efa1 | 785 | |
ac3d33df JK |
786 | if ((error = check_entry(bld->repo, filename, id, filemode)) < 0) |
787 | return error; | |
2bbc7d3e | 788 | |
22a2d3d5 | 789 | if ((entry = git_strmap_get(bld->map, filename)) != NULL) { |
4ed9e939 | 790 | git_oid_cpy((git_oid *) entry->oid, id); |
978fbb4c | 791 | } else { |
4ed9e939 | 792 | entry = alloc_entry(filename, strlen(filename), id); |
ac3d33df | 793 | GIT_ERROR_CHECK_ALLOC(entry); |
93ab370b | 794 | |
22a2d3d5 | 795 | if ((error = git_strmap_set(bld->map, entry->filename, entry)) < 0) { |
978fbb4c | 796 | git_tree_entry_free(entry); |
ac3d33df | 797 | git_error_set(GIT_ERROR_TREE, "failed to insert %s", filename); |
978fbb4c CMN |
798 | return -1; |
799 | } | |
4d3f1f97 CMN |
800 | } |
801 | ||
11d9f6b3 PK |
802 | entry->attr = filemode; |
803 | ||
804 | if (entry_out) | |
0ad6efa1 VM |
805 | *entry_out = entry; |
806 | ||
3aa351ea | 807 | return 0; |
0ad6efa1 VM |
808 | } |
809 | ||
0cbbdc26 | 810 | static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename) |
0ad6efa1 | 811 | { |
c25aa7cd PP |
812 | GIT_ASSERT_ARG_WITH_RETVAL(bld, NULL); |
813 | GIT_ASSERT_ARG_WITH_RETVAL(filename, NULL); | |
814 | ||
22a2d3d5 | 815 | return git_strmap_get(bld->map, filename); |
0ad6efa1 VM |
816 | } |
817 | ||
0cbbdc26 KS |
818 | const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename) |
819 | { | |
820 | return treebuilder_get(bld, filename); | |
821 | } | |
822 | ||
0ad6efa1 VM |
823 | int git_treebuilder_remove(git_treebuilder *bld, const char *filename) |
824 | { | |
4d3f1f97 | 825 | git_tree_entry *entry = treebuilder_get(bld, filename); |
0ad6efa1 | 826 | |
4d3f1f97 | 827 | if (entry == NULL) |
eae0bfdc | 828 | return tree_error("failed to remove entry: file isn't in the tree", filename); |
0ad6efa1 | 829 | |
4d3f1f97 | 830 | git_strmap_delete(bld->map, filename); |
978fbb4c | 831 | git_tree_entry_free(entry); |
4d3f1f97 | 832 | |
3aa351ea | 833 | return 0; |
0ad6efa1 VM |
834 | } |
835 | ||
dce7b1a4 | 836 | int git_treebuilder_write(git_oid *oid, git_treebuilder *bld) |
87aaefe2 | 837 | { |
c25aa7cd PP |
838 | GIT_ASSERT_ARG(oid); |
839 | GIT_ASSERT_ARG(bld); | |
4d3f1f97 | 840 | |
c25aa7cd | 841 | return git_treebuilder__write_with_buffer(oid, bld, &bld->write_cache); |
0ad6efa1 VM |
842 | } |
843 | ||
22a2d3d5 | 844 | int git_treebuilder_filter( |
e120123e RB |
845 | git_treebuilder *bld, |
846 | git_treebuilder_filter_cb filter, | |
847 | void *payload) | |
0ad6efa1 | 848 | { |
4d3f1f97 | 849 | const char *filename; |
e2237179 | 850 | git_tree_entry *entry; |
0ad6efa1 | 851 | |
c25aa7cd PP |
852 | GIT_ASSERT_ARG(bld); |
853 | GIT_ASSERT_ARG(filter); | |
0ad6efa1 | 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 | }); | |
22a2d3d5 UG |
861 | |
862 | return 0; | |
0ad6efa1 VM |
863 | } |
864 | ||
22a2d3d5 | 865 | int git_treebuilder_clear(git_treebuilder *bld) |
0ad6efa1 | 866 | { |
e2237179 RB |
867 | git_tree_entry *e; |
868 | ||
c25aa7cd | 869 | GIT_ASSERT_ARG(bld); |
0ad6efa1 | 870 | |
4d3f1f97 | 871 | git_strmap_foreach_value(bld->map, e, git_tree_entry_free(e)); |
978fbb4c | 872 | git_strmap_clear(bld->map); |
22a2d3d5 UG |
873 | |
874 | return 0; | |
0ad6efa1 VM |
875 | } |
876 | ||
877 | void git_treebuilder_free(git_treebuilder *bld) | |
878 | { | |
b0b3b4e3 | 879 | if (bld == NULL) |
880 | return; | |
881 | ||
c25aa7cd | 882 | git_buf_dispose(&bld->write_cache); |
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 | 911 | if (filename_len == 0) { |
ac3d33df | 912 | git_error_set(GIT_ERROR_TREE, "invalid tree path given"); |
0e2fcca8 | 913 | return GIT_ENOTFOUND; |
3aa351ea | 914 | } |
3fa735ca | 915 | |
0e2fcca8 | 916 | entry = entry_fromname(root, path, filename_len); |
3fa735ca | 917 | |
3aa351ea | 918 | if (entry == NULL) { |
ac3d33df | 919 | git_error_set(GIT_ERROR_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)) { | |
ac3d33df | 929 | git_error_set(GIT_ERROR_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 | ||
eae0bfdc | 934 | /* If there's only a slash left in the path, we |
0e2fcca8 VM |
935 | * return the current entry; otherwise, we keep |
936 | * walking down the path */ | |
937 | if (path[filename_len + 1] != '\0') | |
938 | break; | |
eae0bfdc | 939 | /* fall through */ |
0e2fcca8 VM |
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 */ | |
ac3d33df | 974 | git_error_set_after_callback_function(error, "git_tree_walk"); |
26c1cb91 RB |
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 */ | |
ac3d33df | 1010 | git_error_set_after_callback_function(error, "git_tree_walk"); |
26c1cb91 RB |
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) { |
ac3d33df | 1030 | git_error_set(GIT_ERROR_INVALID, "invalid walking mode for tree walk"); |
e120123e | 1031 | return -1; |
da37654d | 1032 | } |
97769280 | 1033 | |
e2237179 RB |
1034 | error = tree_walk( |
1035 | tree, callback, &root_path, payload, (mode == GIT_TREEWALK_PRE)); | |
1036 | ||
ac3d33df | 1037 | git_buf_dispose(&root_path); |
97769280 RB |
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 | ||
ac3d33df | 1054 | git_error_set(GIT_ERROR_TREE, "duplicate entries given for update"); |
9464f9eb CMN |
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 | ||
ac3d33df | 1123 | GIT_ERROR_CHECK_ALLOC(component->ptr); |
9464f9eb CMN |
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); | |
ac3d33df JK |
1129 | if (to_replace && git_tree_entry_type(to_replace) != GIT_OBJECT_TREE) { |
1130 | git_error_set(GIT_ERROR_TREE, "D/F conflict when updating tree"); | |
9464f9eb CMN |
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); | |
ac3d33df | 1157 | GIT_ERROR_CHECK_ALLOC(root_elem); |
9464f9eb CMN |
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); | |
c25aa7cd | 1186 | GIT_ASSERT(popped); |
9464f9eb CMN |
1187 | |
1188 | current = git_array_last(stack); | |
c25aa7cd | 1189 | GIT_ASSERT(current); |
9464f9eb CMN |
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 | ||
ac3d33df JK |
1206 | if (entry && git_tree_entry_type(entry) != GIT_OBJECT_TREE) { |
1207 | git_error_set(GIT_ERROR_TREE, "D/F conflict when updating tree"); | |
9464f9eb CMN |
1208 | error = -1; |
1209 | goto cleanup; | |
1210 | } | |
1211 | ||
1212 | new_entry = git_array_alloc(stack); | |
ac3d33df | 1213 | GIT_ERROR_CHECK_ALLOC(new_entry); |
9464f9eb CMN |
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); | |
ac3d33df | 1224 | GIT_ERROR_CHECK_ALLOC(new_entry->name); |
9464f9eb CMN |
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); |
ac3d33df | 1240 | git_error_set(GIT_ERROR_TREE, "cannot replace '%s' with '%s' at '%s'", |
9464f9eb CMN |
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 | 1253 | { |
c25aa7cd | 1254 | tree_stack_entry *last = git_array_last(stack); |
53412305 | 1255 | char *basename = git_path_basename(update->path); |
c25aa7cd | 1256 | error = git_treebuilder_remove(last->bld, basename); |
53412305 | 1257 | git__free(basename); |
9464f9eb | 1258 | break; |
53412305 | 1259 | } |
9464f9eb | 1260 | default: |
ac3d33df | 1261 | git_error_set(GIT_ERROR_TREE, "unknown action for update"); |
9464f9eb CMN |
1262 | error = -1; |
1263 | goto cleanup; | |
1264 | } | |
1265 | ||
1266 | if (error < 0) | |
1267 | goto cleanup; | |
1268 | } | |
1269 | ||
1270 | /* We're done, go up the stack again and write out the tree */ | |
1271 | { | |
1272 | tree_stack_entry *current = NULL, *popped = NULL; | |
1273 | while ((popped = git_array_pop(stack)) != NULL) { | |
1274 | current = git_array_last(stack); | |
1275 | /* We've reached the top, current is the root tree */ | |
1276 | if (!current) | |
1277 | break; | |
1278 | ||
1279 | if ((error = create_popped_tree(current, popped, &component)) < 0) | |
1280 | goto cleanup; | |
1281 | } | |
1282 | ||
1283 | /* Write out the root tree */ | |
1284 | git__free(popped->name); | |
1285 | git_tree_free(popped->tree); | |
1286 | ||
1287 | error = git_treebuilder_write(out, popped->bld); | |
1288 | git_treebuilder_free(popped->bld); | |
1289 | if (error < 0) | |
1290 | goto cleanup; | |
1291 | } | |
1292 | ||
1293 | cleanup: | |
1294 | { | |
1295 | tree_stack_entry *e; | |
1296 | while ((e = git_array_pop(stack)) != NULL) { | |
1297 | git_treebuilder_free(e->bld); | |
1298 | git_tree_free(e->tree); | |
1299 | git__free(e->name); | |
1300 | } | |
1301 | } | |
1302 | ||
ac3d33df | 1303 | git_buf_dispose(&component); |
9464f9eb CMN |
1304 | git_array_clear(stack); |
1305 | git_vector_free(&entries); | |
1306 | return error; | |
1307 | } | |
c25aa7cd PP |
1308 | |
1309 | /* Deprecated Functions */ | |
1310 | ||
1311 | #ifndef GIT_DEPRECATE_HARD | |
1312 | ||
1313 | int git_treebuilder_write_with_buffer(git_oid *oid, git_treebuilder *bld, git_buf *buf) | |
1314 | { | |
1315 | GIT_UNUSED(buf); | |
1316 | ||
1317 | return git_treebuilder_write(oid, bld); | |
1318 | } | |
1319 | ||
1320 | #endif |