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