index: introduce git_index_read_index
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
diff --git a/src/index.c b/src/index.c
index 0d2a03e..7c0c310 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2451,6 +2451,104 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
return error;
}
+int git_index_read_index(
+ git_index *index,
+ const git_index *new_index)
+{
+ git_vector new_entries = GIT_VECTOR_INIT,
+ remove_entries = GIT_VECTOR_INIT;
+ git_iterator *index_iterator = NULL;
+ git_iterator *new_iterator = NULL;
+ 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 ||
+ (error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0)
+ goto done;
+
+ if ((error = git_iterator_for_index(&index_iterator,
+ index, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0 ||
+ (error = git_iterator_for_index(&new_iterator,
+ (git_index *)new_index, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0)
+ goto done;
+
+ if (((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
+ error != GIT_ITEROVER) ||
+ ((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
+ error != GIT_ITEROVER))
+ goto done;
+
+ while (true) {
+ int diff;
+
+ if (old_entry && new_entry)
+ diff = git_index_entry_cmp(old_entry, new_entry);
+ else if (!old_entry && new_entry)
+ diff = 1;
+ else if (old_entry && !new_entry)
+ diff = -1;
+ else
+ break;
+
+ if (diff < 0) {
+ git_vector_insert(&remove_entries, (git_index_entry *)old_entry);
+ } else if (diff > 0) {
+ if ((error = index_entry_dup(&entry, git_index_owner(index), new_entry)) < 0)
+ goto done;
+
+ git_vector_insert(&new_entries, entry);
+ } else {
+ /* Path and stage are equal, if the OID is equal, keep it to
+ * keep the stat cache data.
+ */
+ if (git_oid_equal(&old_entry->id, &new_entry->id)) {
+ git_vector_insert(&new_entries, (git_index_entry *)old_entry);
+ } else {
+ if ((error = index_entry_dup(&entry, git_index_owner(index), new_entry)) < 0)
+ goto done;
+
+ git_vector_insert(&new_entries, entry);
+ git_vector_insert(&remove_entries, (git_index_entry *)old_entry);
+ }
+ }
+
+ if (diff <= 0) {
+ if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 &&
+ error != GIT_ITEROVER)
+ goto done;
+ }
+
+ if (diff >= 0) {
+ if ((error = git_iterator_advance(&new_entry, new_iterator)) < 0 &&
+ error != GIT_ITEROVER)
+ goto done;
+ }
+ }
+
+ git_index_name_clear(index);
+ git_index_reuc_clear(index);
+
+ git_vector_swap(&new_entries, &index->entries);
+
+ git_vector_foreach(&remove_entries, i, entry) {
+ if (index->tree)
+ git_tree_cache_invalidate_path(index->tree, entry->path);
+
+ index_entry_free(entry);
+ }
+
+ error = 0;
+
+done:
+ git_vector_free(&new_entries);
+ git_vector_free(&remove_entries);
+ git_iterator_free(index_iterator);
+ git_iterator_free(new_iterator);
+ return error;
+}
+
git_repository *git_index_owner(const git_index *index)
{
return INDEX_OWNER(index);
diff --git a/src/index.h b/src/index.h
index 6d2904f..0f6f4e8 100644
--- a/src/index.h
+++ b/src/index.h
@@ -93,6 +93,8 @@ extern int git_index_snapshot_find(
size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
const char *path, size_t path_len, int stage);
+/* Replace an index with a new index */
+int git_index_read_index(git_index *index, const git_index *new_index);
typedef struct {
git_index *index;
diff --git a/tests/index/read_index.c b/tests/index/read_index.c
new file mode 100644
index 0000000..82a771d
--- /dev/null
+++ b/tests/index/read_index.c
@@ -0,0 +1,73 @@
+#include "clar_libgit2.h"
+#include "posix.h"
+#include "index.h"
+
+static git_repository *_repo;
+static git_index *_index;
+
+void test_index_read_index__initialize(void)
+{
+ git_object *head;
+ git_reference *head_ref;
+
+ _repo = cl_git_sandbox_init("testrepo");
+ cl_git_pass(git_revparse_ext(&head, &head_ref, _repo, "HEAD"));
+ cl_git_pass(git_reset(_repo, head, GIT_RESET_HARD, NULL));
+ cl_git_pass(git_repository_index(&_index, _repo));
+
+ git_reference_free(head_ref);
+ git_object_free(head);
+}
+
+void test_index_read_index__cleanup(void)
+{
+ git_index_free(_index);
+ cl_git_sandbox_cleanup();
+}
+
+void test_index_read_index__maintains_stat_cache(void)
+{
+ git_index *new_index;
+ git_oid index_id;
+ git_index_entry new_entry;
+ const git_index_entry *e;
+ git_tree *tree;
+ size_t i;
+
+ cl_assert_equal_i(4, git_index_entrycount(_index));
+
+ /* write-tree */
+ cl_git_pass(git_index_write_tree(&index_id, _index));
+
+ /* read-tree, then read index */
+ git_tree_lookup(&tree, _repo, &index_id);
+ cl_git_pass(git_index_new(&new_index));
+ cl_git_pass(git_index_read_tree(new_index, tree));
+ git_tree_free(tree);
+
+ /* add a new entry that will not have stat data */
+ memset(&new_entry, 0, sizeof(git_index_entry));
+ new_entry.path = "Hello";
+ git_oid_fromstr(&new_entry.id, "0123456789012345678901234567890123456789");
+ new_entry.file_size = 1234;
+ new_entry.mode = 0100644;
+ cl_git_pass(git_index_add(new_index, &new_entry));
+ cl_assert_equal_i(5, git_index_entrycount(new_index));
+
+ cl_git_pass(git_index_read_index(_index, new_index));
+ git_index_free(new_index);
+
+ cl_assert_equal_i(5, git_index_entrycount(_index));
+
+ for (i = 0; i < git_index_entrycount(_index); i++) {
+ e = git_index_get_byindex(_index, i);
+
+ if (strcmp(e->path, "Hello") == 0) {
+ cl_assert_equal_i(0, e->ctime.seconds);
+ cl_assert_equal_i(0, e->mtime.seconds);
+ } else {
+ cl_assert(0 != e->ctime.seconds);
+ cl_assert(0 != e->mtime.seconds);
+ }
+ }
+}