]> git.proxmox.com Git - libgit2.git/blob - src/tree.c
Merge pull request #474 from schu/clang-unused
[libgit2.git] / src / tree.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
3 *
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.
6 */
7
8 #include "common.h"
9 #include "commit.h"
10 #include "tree.h"
11 #include "git2/repository.h"
12 #include "git2/object.h"
13
14 #define DEFAULT_TREE_SIZE 16
15 #define MAX_FILEMODE 0777777
16 #define MAX_FILEMODE_BYTES 6
17
18 static int valid_attributes(const int attributes)
19 {
20 return attributes >= 0 && attributes <= MAX_FILEMODE;
21 }
22
23 static int valid_entry_name(const char *filename)
24 {
25 return strlen(filename) > 0 && strchr(filename, '/') == NULL;
26 }
27
28 static int entry_sort_cmp(const void *a, const void *b)
29 {
30 const git_tree_entry *entry_a = (const git_tree_entry *)(a);
31 const git_tree_entry *entry_b = (const git_tree_entry *)(b);
32
33 return git_path_cmp(
34 entry_a->filename, entry_a->filename_len, entry_is_tree(entry_a),
35 entry_b->filename, entry_b->filename_len, entry_is_tree(entry_b));
36 }
37
38
39 struct tree_key_search {
40 const char *filename;
41 size_t filename_len;
42 };
43
44 static int homing_search_cmp(const void *key, const void *array_member)
45 {
46 const struct tree_key_search *ksearch = key;
47 const git_tree_entry *entry = array_member;
48
49 const size_t len1 = ksearch->filename_len;
50 const size_t len2 = entry->filename_len;
51
52 return memcmp(
53 ksearch->filename,
54 entry->filename,
55 len1 < len2 ? len1 : len2
56 );
57 }
58
59 /*
60 * Search for an entry in a given tree.
61 *
62 * Note that this search is performed in two steps because
63 * of the way tree entries are sorted internally in git:
64 *
65 * Entries in a tree are not sorted alphabetically; two entries
66 * with the same root prefix will have different positions
67 * depending on whether they are folders (subtrees) or normal files.
68 *
69 * Consequently, it is not possible to find an entry on the tree
70 * with a binary search if you don't know whether the filename
71 * you're looking for is a folder or a normal file.
72 *
73 * To work around this, we first perform a homing binary search
74 * on the tree, using the minimal length root prefix of our filename.
75 * Once the comparisons for this homing search start becoming
76 * ambiguous because of folder vs file sorting, we look linearly
77 * around the area for our target file.
78 */
79 static int tree_key_search(git_vector *entries, const char *filename)
80 {
81 struct tree_key_search ksearch;
82 const git_tree_entry *entry;
83
84 int homing, i;
85
86 ksearch.filename = filename;
87 ksearch.filename_len = strlen(filename);
88
89 /* Initial homing search; find an entry on the tree with
90 * the same prefix as the filename we're looking for */
91 homing = git_vector_bsearch2(entries, &homing_search_cmp, &ksearch);
92 if (homing < 0)
93 return homing;
94
95 /* We found a common prefix. Look forward as long as
96 * there are entries that share the common prefix */
97 for (i = homing; i < (int)entries->length; ++i) {
98 entry = entries->contents[i];
99
100 if (homing_search_cmp(&ksearch, entry) != 0)
101 break;
102
103 if (strcmp(filename, entry->filename) == 0)
104 return i;
105 }
106
107 /* If we haven't found our filename yet, look backwards
108 * too as long as we have entries with the same prefix */
109 for (i = homing - 1; i >= 0; --i) {
110 entry = entries->contents[i];
111
112 if (homing_search_cmp(&ksearch, entry) != 0)
113 break;
114
115 if (strcmp(filename, entry->filename) == 0)
116 return i;
117 }
118
119 /* The filename doesn't exist at all */
120 return GIT_ENOTFOUND;
121 }
122
123 void git_tree__free(git_tree *tree)
124 {
125 unsigned int i;
126
127 for (i = 0; i < tree->entries.length; ++i) {
128 git_tree_entry *e;
129 e = git_vector_get(&tree->entries, i);
130
131 git__free(e->filename);
132 git__free(e);
133 }
134
135 git_vector_free(&tree->entries);
136 git__free(tree);
137 }
138
139 const git_oid *git_tree_id(git_tree *c)
140 {
141 return git_object_id((git_object *)c);
142 }
143
144 unsigned int git_tree_entry_attributes(const git_tree_entry *entry)
145 {
146 return entry->attr;
147 }
148
149 const char *git_tree_entry_name(const git_tree_entry *entry)
150 {
151 assert(entry);
152 return entry->filename;
153 }
154
155 const git_oid *git_tree_entry_id(const git_tree_entry *entry)
156 {
157 assert(entry);
158 return &entry->oid;
159 }
160
161 git_otype git_tree_entry_type(const git_tree_entry *entry)
162 {
163 assert(entry);
164
165 if (S_ISGITLINK(entry->attr))
166 return GIT_OBJ_COMMIT;
167 else if (S_ISDIR(entry->attr))
168 return GIT_OBJ_TREE;
169 else
170 return GIT_OBJ_BLOB;
171 }
172
173 int git_tree_entry_2object(git_object **object_out, git_repository *repo, const git_tree_entry *entry)
174 {
175 assert(entry && object_out);
176 return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY);
177 }
178
179 const git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
180 {
181 int idx;
182
183 assert(tree && filename);
184
185 idx = tree_key_search(&tree->entries, filename);
186 if (idx == GIT_ENOTFOUND)
187 return NULL;
188
189 return git_vector_get(&tree->entries, idx);
190 }
191
192 const git_tree_entry *git_tree_entry_byindex(git_tree *tree, unsigned int idx)
193 {
194 assert(tree);
195 return git_vector_get(&tree->entries, idx);
196 }
197
198 unsigned int git_tree_entrycount(git_tree *tree)
199 {
200 assert(tree);
201 return tree->entries.length;
202 }
203
204 static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buffer_end)
205 {
206 int error = GIT_SUCCESS;
207
208 if (git_vector_init(&tree->entries, DEFAULT_TREE_SIZE, entry_sort_cmp) < GIT_SUCCESS)
209 return GIT_ENOMEM;
210
211 while (buffer < buffer_end) {
212 git_tree_entry *entry;
213 int tmp;
214
215 entry = git__calloc(1, sizeof(git_tree_entry));
216 if (entry == NULL) {
217 error = GIT_ENOMEM;
218 break;
219 }
220
221 if (git_vector_insert(&tree->entries, entry) < GIT_SUCCESS)
222 return GIT_ENOMEM;
223
224 if (git__strtol32(&tmp, buffer, &buffer, 8) < GIT_SUCCESS ||
225 !buffer || !valid_attributes(tmp))
226 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse tree. Can't parse attributes");
227
228 entry->attr = tmp;
229
230 if (*buffer++ != ' ') {
231 error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse tree. Object it corrupted");
232 break;
233 }
234
235 if (memchr(buffer, 0, buffer_end - buffer) == NULL) {
236 error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse tree. Object it corrupted");
237 break;
238 }
239
240 entry->filename = git__strdup(buffer);
241 entry->filename_len = strlen(buffer);
242
243 while (buffer < buffer_end && *buffer != 0)
244 buffer++;
245
246 buffer++;
247
248 git_oid_fromraw(&entry->oid, (const unsigned char *)buffer);
249 buffer += GIT_OID_RAWSZ;
250 }
251
252 return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse buffer");
253 }
254
255 int git_tree__parse(git_tree *tree, git_odb_object *obj)
256 {
257 assert(tree);
258 return tree_parse_buffer(tree, (char *)obj->raw.data, (char *)obj->raw.data + obj->raw.len);
259 }
260
261 static unsigned int find_next_dir(const char *dirname, git_index *index, unsigned int start)
262 {
263 unsigned int i, entries = git_index_entrycount(index);
264 size_t dirlen;
265
266 dirlen = strlen(dirname);
267 for (i = start; i < entries; ++i) {
268 git_index_entry *entry = git_index_get(index, i);
269 if (strlen(entry->path) < dirlen ||
270 memcmp(entry->path, dirname, dirlen) ||
271 (dirlen > 0 && entry->path[dirlen] != '/')) {
272 break;
273 }
274 }
275
276 return i;
277 }
278
279 static int append_entry(git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes)
280 {
281 git_tree_entry *entry;
282
283 if ((entry = git__malloc(sizeof(git_tree_entry))) == NULL)
284 return GIT_ENOMEM;
285
286 memset(entry, 0x0, sizeof(git_tree_entry));
287 entry->filename = git__strdup(filename);
288 entry->filename_len = strlen(entry->filename);
289
290 git_oid_cpy(&entry->oid, id);
291 entry->attr = attributes;
292
293 if (git_vector_insert(&bld->entries, entry) < 0)
294 return GIT_ENOMEM;
295
296 return GIT_SUCCESS;
297 }
298
299 static int write_tree(
300 git_oid *oid,
301 git_repository *repo,
302 git_index *index,
303 const char *dirname,
304 unsigned int start)
305 {
306 git_treebuilder *bld = NULL;
307
308 unsigned int i, entries = git_index_entrycount(index);
309 int error;
310 size_t dirname_len = strlen(dirname);
311 const git_tree_cache *cache;
312
313 cache = git_tree_cache_get(index->tree, dirname);
314 if (cache != NULL && cache->entries >= 0){
315 git_oid_cpy(oid, &cache->oid);
316 return find_next_dir(dirname, index, start);
317 }
318
319 error = git_treebuilder_create(&bld, NULL);
320 if (bld == NULL) {
321 return GIT_ENOMEM;
322 }
323
324 /*
325 * This loop is unfortunate, but necessary. The index doesn't have
326 * any directores, so we need to handle that manually, and we
327 * need to keep track of the current position.
328 */
329 for (i = start; i < entries; ++i) {
330 git_index_entry *entry = git_index_get(index, i);
331 char *filename, *next_slash;
332
333 /*
334 * If we've left our (sub)tree, exit the loop and return. The
335 * first check is an early out (and security for the
336 * third). The second check is a simple prefix comparison. The
337 * third check catches situations where there is a directory
338 * win32/sys and a file win32mmap.c. Without it, the following
339 * code believes there is a file win32/mmap.c
340 */
341 if (strlen(entry->path) < dirname_len ||
342 memcmp(entry->path, dirname, dirname_len) ||
343 (dirname_len > 0 && entry->path[dirname_len] != '/')) {
344 break;
345 }
346
347 filename = entry->path + dirname_len;
348 if (*filename == '/')
349 filename++;
350 next_slash = strchr(filename, '/');
351 if (next_slash) {
352 git_oid sub_oid;
353 int written;
354 char *subdir, *last_comp;
355
356 subdir = git__strndup(entry->path, next_slash - entry->path);
357 if (subdir == NULL) {
358 error = GIT_ENOMEM;
359 goto cleanup;
360 }
361
362 /* Write out the subtree */
363 written = write_tree(&sub_oid, repo, index, subdir, i);
364 if (written < 0) {
365 error = git__rethrow(written, "Failed to write subtree %s", subdir);
366 } else {
367 i = written - 1; /* -1 because of the loop increment */
368 }
369
370 /*
371 * We need to figure out what we want toinsert
372 * into this tree. If we're traversing
373 * deps/zlib/, then we only want to write
374 * 'zlib' into the tree.
375 */
376 last_comp = strrchr(subdir, '/');
377 if (last_comp) {
378 last_comp++; /* Get rid of the '/' */
379 } else {
380 last_comp = subdir;
381 }
382 error = append_entry(bld, last_comp, &sub_oid, S_IFDIR);
383 git__free(subdir);
384 if (error < GIT_SUCCESS) {
385 error = git__rethrow(error, "Failed to insert dir");
386 goto cleanup;
387 }
388 } else {
389 error = append_entry(bld, filename, &entry->oid, entry->mode);
390 if (error < GIT_SUCCESS) {
391 error = git__rethrow(error, "Failed to insert file");
392 }
393 }
394 }
395
396 error = git_treebuilder_write(oid, repo, bld);
397 if (error < GIT_SUCCESS)
398 error = git__rethrow(error, "Failed to write tree to db");
399
400 cleanup:
401 git_treebuilder_free(bld);
402
403 if (error < GIT_SUCCESS)
404 return error;
405 else
406 return i;
407 }
408
409 int git_tree_create_fromindex(git_oid *oid, git_index *index)
410 {
411 git_repository *repo;
412 int error;
413
414 repo = (git_repository *)GIT_REFCOUNT_OWNER(index);
415
416 if (repo == NULL)
417 return git__throw(GIT_EBAREINDEX,
418 "Failed to create tree. "
419 "The index file is not backed up by an existing repository");
420
421 if (index->tree != NULL && index->tree->entries >= 0) {
422 git_oid_cpy(oid, &index->tree->oid);
423 return GIT_SUCCESS;
424 }
425
426 /* The tree cache didn't help us */
427 error = write_tree(oid, repo, index, "", 0);
428 return (error < GIT_SUCCESS) ? git__rethrow(error, "Failed to create tree") : GIT_SUCCESS;
429 }
430
431 static void sort_entries(git_treebuilder *bld)
432 {
433 git_vector_sort(&bld->entries);
434 }
435
436 int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source)
437 {
438 git_treebuilder *bld;
439 unsigned int i, source_entries = DEFAULT_TREE_SIZE;
440
441 assert(builder_p);
442
443 bld = git__calloc(1, sizeof(git_treebuilder));
444 if (bld == NULL)
445 return GIT_ENOMEM;
446
447 if (source != NULL)
448 source_entries = source->entries.length;
449
450 if (git_vector_init(&bld->entries, source_entries, entry_sort_cmp) < GIT_SUCCESS) {
451 git__free(bld);
452 return GIT_ENOMEM;
453 }
454
455 if (source != NULL) {
456 for (i = 0; i < source->entries.length; ++i) {
457 git_tree_entry *entry_src = source->entries.contents[i];
458
459 if (append_entry(bld, entry_src->filename, &entry_src->oid, entry_src->attr) < 0) {
460 git_treebuilder_free(bld);
461 return GIT_ENOMEM;
462 }
463 }
464 }
465
466 *builder_p = bld;
467 return GIT_SUCCESS;
468 }
469
470 int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes)
471 {
472 git_tree_entry *entry;
473 int pos;
474
475 assert(bld && id && filename);
476
477 if (!valid_attributes(attributes))
478 return git__throw(GIT_ERROR, "Failed to insert entry. Invalid attributes");
479
480 if (!valid_entry_name(filename))
481 return git__throw(GIT_ERROR, "Failed to insert entry. Invalid name for a tree entry");
482
483 pos = tree_key_search(&bld->entries, filename);
484
485 if (pos >= 0) {
486 entry = git_vector_get(&bld->entries, pos);
487 if (entry->removed)
488 entry->removed = 0;
489 } else {
490 if ((entry = git__malloc(sizeof(git_tree_entry))) == NULL)
491 return GIT_ENOMEM;
492
493 memset(entry, 0x0, sizeof(git_tree_entry));
494 entry->filename = git__strdup(filename);
495 entry->filename_len = strlen(entry->filename);
496 }
497
498 git_oid_cpy(&entry->oid, id);
499 entry->attr = attributes;
500
501 if (pos == GIT_ENOTFOUND) {
502 if (git_vector_insert(&bld->entries, entry) < 0)
503 return GIT_ENOMEM;
504 }
505
506 if (entry_out != NULL)
507 *entry_out = entry;
508
509 return GIT_SUCCESS;
510 }
511
512 static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename)
513 {
514 int idx;
515 git_tree_entry *entry;
516
517 assert(bld && filename);
518
519 idx = tree_key_search(&bld->entries, filename);
520 if (idx < 0)
521 return NULL;
522
523 entry = git_vector_get(&bld->entries, idx);
524 if (entry->removed)
525 return NULL;
526
527 return entry;
528 }
529
530 const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename)
531 {
532 return treebuilder_get(bld, filename);
533 }
534
535 int git_treebuilder_remove(git_treebuilder *bld, const char *filename)
536 {
537 git_tree_entry *remove_ptr = treebuilder_get(bld, filename);
538
539 if (remove_ptr == NULL || remove_ptr->removed)
540 return git__throw(GIT_ENOTFOUND, "Failed to remove entry. File isn't in the tree");
541
542 remove_ptr->removed = 1;
543 return GIT_SUCCESS;
544 }
545
546 int git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld)
547 {
548 unsigned int i;
549 int error;
550 git_buf tree = GIT_BUF_INIT;
551 git_odb *odb;
552
553 assert(bld);
554
555 sort_entries(bld);
556
557 /* Grow the buffer beforehand to an estimated size */
558 git_buf_grow(&tree, bld->entries.length * 72);
559
560 for (i = 0; i < bld->entries.length; ++i) {
561 git_tree_entry *entry = bld->entries.contents[i];
562
563 if (entry->removed)
564 continue;
565
566 git_buf_printf(&tree, "%o ", entry->attr);
567 git_buf_put(&tree, entry->filename, entry->filename_len + 1);
568 git_buf_put(&tree, (char *)entry->oid.id, GIT_OID_RAWSZ);
569 }
570
571 if ((error = git_buf_lasterror(&tree)) < GIT_SUCCESS) {
572 git_buf_free(&tree);
573 return git__rethrow(error, "Not enough memory to build the tree data");
574 }
575
576 error = git_repository_odb__weakptr(&odb, repo);
577 if (error < GIT_SUCCESS) {
578 git_buf_free(&tree);
579 return error;
580 }
581
582 error = git_odb_write(oid, odb, tree.ptr, tree.size, GIT_OBJ_TREE);
583 git_buf_free(&tree);
584
585 return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write tree");
586 }
587
588 void git_treebuilder_filter(git_treebuilder *bld, int (*filter)(const git_tree_entry *, void *), void *payload)
589 {
590 unsigned int i;
591
592 assert(bld && filter);
593
594 for (i = 0; i < bld->entries.length; ++i) {
595 git_tree_entry *entry = bld->entries.contents[i];
596 if (!entry->removed && filter(entry, payload))
597 entry->removed = 1;
598 }
599 }
600
601 void git_treebuilder_clear(git_treebuilder *bld)
602 {
603 unsigned int i;
604 assert(bld);
605
606 for (i = 0; i < bld->entries.length; ++i) {
607 git_tree_entry *e = bld->entries.contents[i];
608 git__free(e->filename);
609 git__free(e);
610 }
611
612 git_vector_clear(&bld->entries);
613 }
614
615 void git_treebuilder_free(git_treebuilder *bld)
616 {
617 git_treebuilder_clear(bld);
618 git_vector_free(&bld->entries);
619 git__free(bld);
620 }
621
622 static int tree_frompath(
623 git_tree **parent_out,
624 git_tree *root,
625 git_buf *treeentry_path,
626 int offset)
627 {
628 char *slash_pos = NULL;
629 const git_tree_entry* entry;
630 int error = GIT_SUCCESS;
631 git_tree *subtree;
632
633 if (!*(treeentry_path->ptr + offset))
634 return git__rethrow(GIT_EINVALIDPATH,
635 "Invalid relative path to a tree entry '%s'.", treeentry_path->ptr);
636
637 slash_pos = (char *)strchr(treeentry_path->ptr + offset, '/');
638
639 if (slash_pos == NULL)
640 return git_tree_lookup(
641 parent_out,
642 root->object.repo,
643 git_object_id((const git_object *)root)
644 );
645
646 if (slash_pos == treeentry_path->ptr + offset)
647 return git__rethrow(GIT_EINVALIDPATH,
648 "Invalid relative path to a tree entry '%s'.", treeentry_path->ptr);
649
650 *slash_pos = '\0';
651
652 entry = git_tree_entry_byname(root, treeentry_path->ptr + offset);
653
654 if (slash_pos != NULL)
655 *slash_pos = '/';
656
657 if (entry == NULL)
658 return git__rethrow(GIT_ENOTFOUND,
659 "No tree entry can be found from "
660 "the given tree and relative path '%s'.", treeentry_path->ptr);
661
662
663 error = git_tree_lookup(&subtree, root->object.repo, &entry->oid);
664 if (error < GIT_SUCCESS)
665 return error;
666
667 error = tree_frompath(
668 parent_out,
669 subtree,
670 treeentry_path,
671 (slash_pos - treeentry_path->ptr) + 1
672 );
673
674 git_tree_free(subtree);
675 return error;
676 }
677
678 int git_tree_get_subtree(
679 git_tree **subtree,
680 git_tree *root,
681 const char *subtree_path)
682 {
683 int error;
684 git_buf buffer = GIT_BUF_INIT;
685
686 assert(subtree && root && subtree_path);
687
688 if ((error = git_buf_sets(&buffer, subtree_path)) == GIT_SUCCESS)
689 error = tree_frompath(subtree, root, &buffer, 0);
690
691 git_buf_free(&buffer);
692
693 return error;
694 }
695
696 static int tree_walk_post(
697 git_tree *tree,
698 git_treewalk_cb callback,
699 git_buf *path,
700 void *payload)
701 {
702 int error = GIT_SUCCESS;
703 unsigned int i;
704
705 for (i = 0; i < tree->entries.length; ++i) {
706 git_tree_entry *entry = tree->entries.contents[i];
707
708 if (callback(path->ptr, entry, payload) < 0)
709 continue;
710
711 if (entry_is_tree(entry)) {
712 git_tree *subtree;
713 size_t path_len = path->size;
714
715 if ((error = git_tree_lookup(
716 &subtree, tree->object.repo, &entry->oid)) < 0)
717 break;
718
719 /* append the next entry to the path */
720 git_buf_puts(path, entry->filename);
721 git_buf_putc(path, '/');
722 if ((error = git_buf_lasterror(path)) < GIT_SUCCESS)
723 break;
724
725 error = tree_walk_post(subtree, callback, path, payload);
726 if (error < GIT_SUCCESS)
727 break;
728
729 git_buf_truncate(path, path_len);
730 git_tree_free(subtree);
731 }
732 }
733
734 return error;
735 }
736
737 int git_tree_walk(git_tree *tree, git_treewalk_cb callback, int mode, void *payload)
738 {
739 int error = GIT_SUCCESS;
740 git_buf root_path = GIT_BUF_INIT;
741
742 switch (mode) {
743 case GIT_TREEWALK_POST:
744 error = tree_walk_post(tree, callback, &root_path, payload);
745 break;
746
747 case GIT_TREEWALK_PRE:
748 error = git__throw(GIT_ENOTIMPLEMENTED,
749 "Preorder tree walking is still not implemented");
750 break;
751
752 default:
753 error = git__throw(GIT_EINVALIDARGS,
754 "Invalid walking mode for tree walk");
755 break;
756 }
757
758 git_buf_free(&root_path);
759
760 return error;
761 }
762
763 static int tree_entry_cmp(const git_tree_entry *a, const git_tree_entry *b)
764 {
765 int ret;
766
767 ret = a->attr - b->attr;
768 if (ret != 0)
769 return ret;
770
771 return git_oid_cmp(&a->oid, &b->oid);
772 }
773
774 static void mark_del(git_tree_diff_data *diff, git_tree_entry *entry)
775 {
776 diff->old_attr = entry->attr;
777 git_oid_cpy(&diff->old_oid, &entry->oid);
778 diff->path = entry->filename;
779 diff->status |= GIT_STATUS_DELETED;
780 }
781
782 static void mark_add(git_tree_diff_data *diff, git_tree_entry *entry)
783 {
784 diff->new_attr = entry->attr;
785 git_oid_cpy(&diff->new_oid, &entry->oid);
786 diff->path = entry->filename;
787 diff->status |= GIT_STATUS_ADDED;
788 }
789
790 static int signal_additions(git_tree *tree, int start, int end, git_tree_diff_cb cb, void *data)
791 {
792 git_tree_diff_data diff;
793 git_tree_entry *entry;
794 int i, error;
795
796 if (end < 0)
797 end = git_tree_entrycount(tree);
798
799 for (i = start; i < end; ++i) {
800 memset(&diff, 0x0, sizeof(git_tree_diff_data));
801 entry = git_vector_get(&tree->entries, i);
802 mark_add(&diff, entry);
803
804 error = cb(&diff, data);
805 if (error < GIT_SUCCESS)
806 return error;
807 }
808
809 return GIT_SUCCESS;
810 }
811
812 static int signal_addition(git_tree_entry *entry, git_tree_diff_cb cb, void *data)
813 {
814 git_tree_diff_data diff;
815
816 memset(&diff, 0x0, sizeof(git_tree_diff_data));
817
818 mark_add(&diff, entry);
819
820 return cb(&diff, data);
821 }
822
823 static int signal_deletions(git_tree *tree, int start, int end, git_tree_diff_cb cb, void *data)
824 {
825 git_tree_diff_data diff;
826 git_tree_entry *entry;
827 int i, error;
828
829 if (end < 0)
830 end = git_tree_entrycount(tree);
831
832 for (i = start; i < end; ++i) {
833 memset(&diff, 0x0, sizeof(git_tree_diff_data));
834 entry = git_vector_get(&tree->entries, i);
835 mark_del(&diff, entry);
836
837 error = cb(&diff, data);
838 if (error < GIT_SUCCESS)
839 return error;
840 }
841
842 return GIT_SUCCESS;
843 }
844
845 static int signal_deletion(git_tree_entry *entry, git_tree_diff_cb cb, void *data)
846 {
847 git_tree_diff_data diff;
848
849 memset(&diff, 0x0, sizeof(git_tree_diff_data));
850
851 mark_del(&diff, entry);
852
853 return cb(&diff, data);
854 }
855
856 static int signal_modification(git_tree_entry *a, git_tree_entry *b,
857 git_tree_diff_cb cb, void *data)
858 {
859 git_tree_diff_data diff;
860
861 memset(&diff, 0x0, sizeof(git_tree_diff_data));
862
863 mark_del(&diff, a);
864 mark_add(&diff, b);
865
866 return cb(&diff, data);
867 }
868
869 int git_tree_diff(git_tree *a, git_tree *b, git_tree_diff_cb cb, void *data)
870 {
871 unsigned int i_a = 0, i_b = 0; /* Counters for trees a and b */
872 git_tree_entry *entry_a = NULL, *entry_b = NULL;
873 git_tree_diff_data diff;
874 int error = GIT_SUCCESS, cmp;
875
876 while (1) {
877 entry_a = a == NULL ? NULL : git_vector_get(&a->entries, i_a);
878 entry_b = b == NULL ? NULL : git_vector_get(&b->entries, i_b);
879
880 if (!entry_a && !entry_b)
881 goto exit;
882
883 memset(&diff, 0x0, sizeof(git_tree_diff_data));
884
885 /*
886 * We've run out of tree on one side so the rest of the
887 * entries on the tree with remaining entries are all
888 * deletions or additions.
889 */
890 if (entry_a && !entry_b)
891 return signal_deletions(a, i_a, -1, cb, data);
892 if (!entry_a && entry_b)
893 return signal_additions(b, i_b, -1, cb, data);
894
895 /*
896 * Both trees are sorted with git's almost-alphabetical
897 * sorting, so a comparison value < 0 means the entry was
898 * deleted in the right tree. > 0 means the entry was added.
899 */
900 cmp = entry_sort_cmp(entry_a, entry_b);
901
902 if (cmp == 0) {
903 i_a++;
904 i_b++;
905
906 /* If everything's the same, jump to next pair */
907 if (!tree_entry_cmp(entry_a, entry_b))
908 continue;
909
910 /* If they're not both dirs or both files, it's add + del */
911 if (S_ISDIR(entry_a->attr) != S_ISDIR(entry_b->attr)) {
912 if ((error = signal_addition(entry_a, cb, data)) < 0)
913 goto exit;
914 if ((error = signal_deletion(entry_b, cb, data)) < 0)
915 goto exit;
916 }
917
918 /* Otherwise consider it a modification */
919 if ((error = signal_modification(entry_a, entry_b, cb, data)) < 0)
920 goto exit;
921
922 } else if (cmp < 0) {
923 i_a++;
924 if ((error = signal_deletion(entry_a, cb, data)) < 0)
925 goto exit;
926 } else if (cmp > 0) {
927 i_b++;
928 if ((error = signal_addition(entry_b, cb, data)) < 0)
929 goto exit;
930 }
931 }
932
933 exit:
934 return error;
935 }
936
937 struct diff_index_cbdata {
938 git_index *index;
939 unsigned int i;
940 git_tree_diff_cb cb;
941 void *data;
942 };
943
944 static int cmp_tentry_ientry(git_tree_entry *tentry, git_index_entry *ientry)
945 {
946 int cmp;
947
948 cmp = tentry->attr - ientry->mode;
949 if (cmp != 0)
950 return cmp;
951
952 return git_oid_cmp(&tentry->oid, &ientry->oid);
953 }
954
955 static void make_tentry(git_tree_entry *tentry, git_index_entry *ientry)
956 {
957 char *last_slash;
958
959 memset(tentry, 0x0, sizeof(git_tree_entry));
960 tentry->attr = ientry->mode;
961
962 last_slash = strrchr(ientry->path, '/');
963 if (last_slash)
964 last_slash++;
965 else
966 last_slash = ientry->path;
967 tentry->filename = last_slash;
968
969 git_oid_cpy(&tentry->oid, &ientry->oid);
970 tentry->filename_len = strlen(tentry->filename);
971 }
972
973 static int diff_index_cb(const char *root, git_tree_entry *tentry, void *data)
974 {
975 struct diff_index_cbdata *cbdata = (struct diff_index_cbdata *) data;
976 git_index_entry *ientry = git_index_get(cbdata->index, cbdata->i);
977 git_tree_entry fake_entry;
978 git_buf fn_buf = GIT_BUF_INIT;
979 int cmp, error = GIT_SUCCESS;
980
981 if (entry_is_tree(tentry))
982 return GIT_SUCCESS;
983
984 git_buf_puts(&fn_buf, root);
985 git_buf_puts(&fn_buf, tentry->filename);
986
987 if (!ientry) {
988 error = signal_deletion(tentry, cbdata->cb, cbdata->data);
989 git_buf_free(&fn_buf);
990 goto exit;
991 }
992
993 /* Like with 'git diff-index', the index is the right side*/
994 cmp = strcmp(git_buf_cstr(&fn_buf), ientry->path);
995 git_buf_free(&fn_buf);
996 if (cmp == 0) {
997 cbdata->i++;
998 if (!cmp_tentry_ientry(tentry, ientry))
999 goto exit;
1000 /* modification */
1001 make_tentry(&fake_entry, ientry);
1002 if ((error = signal_modification(tentry, &fake_entry, cbdata->cb, cbdata->data)) < 0)
1003 goto exit;
1004 } else if (cmp < 0) {
1005 /* deletion */
1006 memcpy(&fake_entry, tentry, sizeof(git_tree_entry));
1007 if ((error = signal_deletion(tentry, cbdata->cb, cbdata->data)) < 0)
1008 goto exit;
1009 } else {
1010 /* addition */
1011 cbdata->i++;
1012 make_tentry(&fake_entry, ientry);
1013 if ((error = signal_addition(&fake_entry, cbdata->cb, cbdata->data)) < 0)
1014 goto exit;
1015 /*
1016 * The index has an addition. This means that we need to use
1017 * the next entry in the index without advancing the tree
1018 * walker, so call ourselves with the same tree state.
1019 */
1020 if ((error = diff_index_cb(root, tentry, data)) < 0)
1021 goto exit;
1022 }
1023
1024 exit:
1025 return error;
1026 }
1027
1028 int git_tree_diff_index_recursive(git_tree *tree, git_index *index, git_tree_diff_cb cb, void *data)
1029 {
1030 struct diff_index_cbdata cbdata;
1031 git_buf dummy_path = GIT_BUF_INIT;
1032
1033 cbdata.index = index;
1034 cbdata.i = 0;
1035 cbdata.cb = cb;
1036 cbdata.data = data;
1037
1038 return tree_walk_post(tree, diff_index_cb, &dummy_path, &cbdata);
1039 }