Commit 35d39761 by Edward Thomson

index: introduce git_index_read_index

parent 73dce1f6
......@@ -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);
......
......@@ -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;
......
#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);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment