index: refactor common `read_index` functionality Most of `git_index_read_index` is common to reading any iterator. Refactor it out in case we want to implement `read_tree` in terms of it in the future.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
diff --git a/src/index.c b/src/index.c
index 32f585f..94427b9 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2925,38 +2925,38 @@ cleanup:
return error;
}
-int git_index_read_index(
+static int git_index_read_iterator(
git_index *index,
- const git_index *new_index)
+ git_iterator *new_iterator,
+ size_t new_length_hint)
{
git_vector new_entries = GIT_VECTOR_INIT,
remove_entries = GIT_VECTOR_INIT;
git_idxmap *new_entries_map = NULL;
git_iterator *index_iterator = NULL;
- git_iterator *new_iterator = NULL;
git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *old_entry, *new_entry;
git_index_entry *entry;
size_t i;
int error;
- if ((error = git_vector_init(&new_entries, new_index->entries.length, index->entries._cmp)) < 0 ||
+ assert((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE));
+
+ if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 ||
(error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 ||
(error = git_idxmap_alloc(&new_entries_map)) < 0)
goto done;
- if (index->ignore_case)
- kh_resize(idxicase, (khash_t(idxicase) *) new_entries_map, new_index->entries.length);
- else
- kh_resize(idx, new_entries_map, new_index->entries.length);
+ if (index->ignore_case && new_length_hint)
+ kh_resize(idxicase, (khash_t(idxicase) *) new_entries_map, new_length_hint);
+ else if (new_length_hint)
+ kh_resize(idx, new_entries_map, new_length_hint);
opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
- if ((error = git_iterator_for_index(&index_iterator, git_index_owner(index), index, &opts)) < 0 ||
- (error = git_iterator_for_index(&new_iterator, git_index_owner(new_index), (git_index *)new_index, &opts)) < 0)
- goto done;
-
- if (((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
+ if ((error = git_iterator_for_index(&index_iterator,
+ git_index_owner(index), index, &opts)) < 0 ||
+ ((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
error != GIT_ITEROVER) ||
((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
error != GIT_ITEROVER))
@@ -3050,6 +3050,8 @@ int git_index_read_index(
index_entry_free(entry);
}
+ clear_uptodate(index);
+
error = 0;
done:
@@ -3057,6 +3059,27 @@ done:
git_vector_free(&new_entries);
git_vector_free(&remove_entries);
git_iterator_free(index_iterator);
+ return error;
+}
+
+int git_index_read_index(
+ git_index *index,
+ const git_index *new_index)
+{
+ git_iterator *new_iterator = NULL;
+ git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
+ int error;
+
+ opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
+
+ if ((error = git_iterator_for_index(&new_iterator,
+ git_index_owner(new_index), (git_index *)new_index, &opts)) < 0)
+ goto done;
+
+ error = git_index_read_iterator(index, new_iterator,
+ new_index->entries.length);
+
+done:
git_iterator_free(new_iterator);
return error;
}