Commit f45ec1a0 by Edward Thomson

index refactoring

parent 81eecc34
......@@ -371,7 +371,7 @@ int main (int argc, char** argv)
// All these properties are exported publicly in the `git_index_entry` struct
ecount = git_index_entrycount(index);
for (i = 0; i < ecount; ++i) {
git_index_entry *e = git_index_get(index, i);
git_index_entry *e = git_index_get_byindex(index, i);
printf("path: %s\n", e->path);
printf("mtime: %d\n", (int)e->mtime.seconds);
......
......@@ -19,12 +19,13 @@ int main (int argc, char** argv)
ecount = git_index_entrycount(index);
for (i = 0; i < ecount; ++i) {
git_index_entry *e = git_index_get(index, i);
git_index_entry *e = git_index_get_byindex(index, i);
oid = e->oid;
git_oid_fmt(out, &oid);
printf("File Path: %s\n", e->path);
printf(" Stage: %d\n", git_index_entry_stage(e));
printf(" Blob SHA: %s\n", out);
printf("File Size: %d\n", (int)e->file_size);
printf(" Device: %d\n", (int)e->dev);
......
......@@ -307,7 +307,7 @@ static int load_attr_blob_from_index(
(error = git_index_find(index, relfile)) < 0)
return error;
entry = git_index_get(index, error);
entry = git_index_get_byindex(index, error);
if (old_oid && git_oid_cmp(old_oid, &entry->oid) == 0)
return GIT_ENOTFOUND;
......
......@@ -33,9 +33,12 @@ struct git_index {
git_tree_cache *tree;
git_vector unmerged;
git_vector reuc;
git_vector_cmp entries_cmp_path;
git_vector_cmp entries_search;
git_vector_cmp entries_search_path;
git_vector_cmp reuc_search;
};
extern void git_index__init_entry_from_stat(struct stat *st, git_index_entry *entry);
......
......@@ -336,7 +336,7 @@ static int index_iterator__current(
git_iterator *self, const git_index_entry **entry)
{
index_iterator *ii = (index_iterator *)self;
git_index_entry *ie = git_index_get(ii->index, ii->current);
git_index_entry *ie = git_index_get_byindex(ii->index, ii->current);
if (ie != NULL &&
ii->base.end != NULL &&
......
......@@ -183,13 +183,13 @@ static int update_index_cb(
if (!data->include_ignored)
break;
return git_index_add(data->index, delta->new_file.path, 0);
return git_index_add_from_workdir(data->index, delta->new_file.path);
case GIT_DELTA_UNTRACKED:
if (!data->include_untracked)
break;
return git_index_add(data->index, delta->new_file.path, 0);
return git_index_add_from_workdir(data->index, delta->new_file.path);
case GIT_DELTA_ADDED:
/* Fall through */
......@@ -197,7 +197,7 @@ static int update_index_cb(
if (!data->include_changed)
break;
return git_index_add(data->index, delta->new_file.path, 0);
return git_index_add_from_workdir(data->index, delta->new_file.path);
case GIT_DELTA_DELETED:
if (!data->include_changed)
......@@ -206,7 +206,7 @@ static int update_index_cb(
if ((pos = git_index_find(data->index, delta->new_file.path)) < 0)
return -1;
if (git_index_remove(data->index, pos) < 0)
if (git_index_remove(data->index, delta->new_file.path, 0) < 0)
return -1;
default:
......
......@@ -332,7 +332,7 @@ int git_submodule_add_finalize(git_submodule *sm)
assert(sm);
if ((error = git_repository_index__weakptr(&index, sm->owner)) < 0 ||
(error = git_index_add(index, GIT_MODULES_FILE, 0)) < 0)
(error = git_index_add_from_workdir(index, GIT_MODULES_FILE)) < 0)
return error;
return git_submodule_add_to_index(sm, true);
......@@ -393,7 +393,7 @@ int git_submodule_add_to_index(git_submodule *sm, int write_index)
git_commit_free(head);
/* add it */
error = git_index_add2(index, &entry);
error = git_index_add(index, &entry);
/* write it, if requested */
if (!error && write_index) {
......@@ -733,7 +733,7 @@ int git_submodule_reload(git_submodule *submodule)
pos = git_index_find(index, submodule->path);
if (pos >= 0) {
git_index_entry *entry = git_index_get(index, pos);
git_index_entry *entry = git_index_get_byindex(index, pos);
if (S_ISGITLINK(entry->mode)) {
if ((error = submodule_load_from_index(repo, entry)) < 0)
......
......@@ -353,7 +353,7 @@ static unsigned int find_next_dir(const char *dirname, git_index *index, unsigne
dirlen = strlen(dirname);
for (i = start; i < entries; ++i) {
git_index_entry *entry = git_index_get(index, i);
git_index_entry *entry = git_index_get_byindex(index, i);
if (strlen(entry->path) < dirlen ||
memcmp(entry->path, dirname, dirlen) ||
(dirlen > 0 && entry->path[dirlen] != '/')) {
......@@ -415,7 +415,7 @@ static int write_tree(
* need to keep track of the current position.
*/
for (i = start; i < entries; ++i) {
git_index_entry *entry = git_index_get(index, i);
git_index_entry *entry = git_index_get_byindex(index, i);
char *filename, *next_slash;
/*
......
......@@ -223,6 +223,20 @@ void git_vector_uniq(git_vector *v)
v->length -= j - i - 1;
}
void git_vector_remove_matching(git_vector *v, int (*match)(git_vector *v, size_t idx))
{
unsigned int i, j;
for (i = 0, j = 0; j < v->length; ++j) {
v->contents[i] = v->contents[j];
if (!match(v, i))
i++;
}
v->length = i;
}
void git_vector_clear(git_vector *v)
{
assert(v);
......
......@@ -75,5 +75,6 @@ int git_vector_insert_sorted(git_vector *v, void *element,
int git_vector_remove(git_vector *v, unsigned int idx);
void git_vector_pop(git_vector *v);
void git_vector_uniq(git_vector *v);
void git_vector_remove_matching(git_vector *v, int (*match)(git_vector *v, size_t idx));
#endif
......@@ -270,12 +270,12 @@ static void assert_proper_normalization(git_index *index, const char *filename,
git_index_entry *entry;
add_to_workdir(filename, CONTENT);
cl_git_pass(git_index_add(index, filename, 0));
cl_git_pass(git_index_add_from_workdir(index, filename));
index_pos = git_index_find(index, filename);
cl_assert(index_pos >= 0);
entry = git_index_get(index, index_pos);
entry = git_index_get_byindex(index, index_pos);
cl_assert_equal_i(0, git_oid_streq(&entry->oid, expected_sha));
}
......
......@@ -189,3 +189,87 @@ void test_core_vector__5(void)
git_vector_free(&x);
}
static int remove_ones(git_vector *v, size_t idx)
{
return (git_vector_get(v, idx) == (void *)0x001);
}
/* Test removal based on callback */
void test_core_vector__remove_matching(void)
{
git_vector x;
size_t i;
void *compare;
git_vector_init(&x, 1, NULL);
git_vector_insert(&x, (void*) 0x001);
cl_assert(x.length == 1);
git_vector_remove_matching(&x, remove_ones);
cl_assert(x.length == 0);
git_vector_insert(&x, (void*) 0x001);
git_vector_insert(&x, (void*) 0x001);
git_vector_insert(&x, (void*) 0x001);
cl_assert(x.length == 3);
git_vector_remove_matching(&x, remove_ones);
cl_assert(x.length == 0);
git_vector_insert(&x, (void*) 0x002);
git_vector_insert(&x, (void*) 0x001);
git_vector_insert(&x, (void*) 0x002);
git_vector_insert(&x, (void*) 0x001);
cl_assert(x.length == 4);
git_vector_remove_matching(&x, remove_ones);
cl_assert(x.length == 2);
git_vector_foreach(&x, i, compare) {
cl_assert(compare != (void *)0x001);
}
git_vector_clear(&x);
git_vector_insert(&x, (void*) 0x001);
git_vector_insert(&x, (void*) 0x002);
git_vector_insert(&x, (void*) 0x002);
git_vector_insert(&x, (void*) 0x001);
cl_assert(x.length == 4);
git_vector_remove_matching(&x, remove_ones);
cl_assert(x.length == 2);
git_vector_foreach(&x, i, compare) {
cl_assert(compare != (void *)0x001);
}
git_vector_clear(&x);
git_vector_insert(&x, (void*) 0x002);
git_vector_insert(&x, (void*) 0x001);
git_vector_insert(&x, (void*) 0x002);
git_vector_insert(&x, (void*) 0x001);
cl_assert(x.length == 4);
git_vector_remove_matching(&x, remove_ones);
cl_assert(x.length == 2);
git_vector_foreach(&x, i, compare) {
cl_assert(compare != (void *)0x001);
}
git_vector_clear(&x);
git_vector_insert(&x, (void*) 0x002);
git_vector_insert(&x, (void*) 0x003);
git_vector_insert(&x, (void*) 0x002);
git_vector_insert(&x, (void*) 0x003);
cl_assert(x.length == 4);
git_vector_remove_matching(&x, remove_ones);
cl_assert(x.length == 4);
git_vector_free(&x);
}
#include "clar_libgit2.h"
#include "index.h"
#include "git2/repository.h"
static git_repository *repo;
static git_index *repo_index;
#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
#define CONFLICTS_ONE_ANCESTOR_OID "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81"
#define CONFLICTS_ONE_OUR_OID "6aea5f295304c36144ad6e9247a291b7f8112399"
#define CONFLICTS_ONE_THEIR_OID "516bd85f78061e09ccc714561d7b504672cb52da"
#define CONFLICTS_TWO_ANCESTOR_OID "84af62840be1b1c47b778a8a249f3ff45155038c"
#define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2"
#define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e"
#define TEST_ANCESTOR_OID "f00ff00ff00ff00ff00ff00ff00ff00ff00ff00f"
#define TEST_OUR_OID "b44bb44bb44bb44bb44bb44bb44bb44bb44bb44b"
#define TEST_THEIR_OID "0123456789abcdef0123456789abcdef01234567"
// Fixture setup and teardown
void test_index_conflicts__initialize(void)
{
repo = cl_git_sandbox_init("mergedrepo");
git_repository_index(&repo_index, repo);
}
void test_index_conflicts__cleanup(void)
{
git_index_free(repo_index);
cl_git_sandbox_cleanup();
}
void test_index_conflicts__add(void)
{
git_index_entry ancestor_entry, our_entry, their_entry;
cl_assert(git_index_entrycount(repo_index) == 8);
memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
memset(&our_entry, 0x0, sizeof(git_index_entry));
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.path = "test-one.txt";
ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
git_oid_fromstr(&ancestor_entry.oid, TEST_ANCESTOR_OID);
our_entry.path = "test-one.txt";
ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT);
git_oid_fromstr(&our_entry.oid, TEST_OUR_OID);
their_entry.path = "test-one.txt";
ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT);
git_oid_fromstr(&their_entry.oid, TEST_THEIR_OID);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));
cl_assert(git_index_entrycount(repo_index) == 11);
}
void test_index_conflicts__add_fixes_incorrect_stage(void)
{
git_index_entry ancestor_entry, our_entry, their_entry;
git_index_entry *conflict_entry[3];
cl_assert(git_index_entrycount(repo_index) == 8);
memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
memset(&our_entry, 0x0, sizeof(git_index_entry));
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.path = "test-one.txt";
ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT);
git_oid_fromstr(&ancestor_entry.oid, TEST_ANCESTOR_OID);
our_entry.path = "test-one.txt";
ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
git_oid_fromstr(&our_entry.oid, TEST_OUR_OID);
their_entry.path = "test-one.txt";
ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT);
git_oid_fromstr(&their_entry.oid, TEST_THEIR_OID);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));
cl_assert(git_index_entrycount(repo_index) == 11);
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], repo_index, "test-one.txt"));
cl_assert(git_index_entry_stage(conflict_entry[0]) == 1);
cl_assert(git_index_entry_stage(conflict_entry[1]) == 2);
cl_assert(git_index_entry_stage(conflict_entry[2]) == 3);
}
void test_index_conflicts__get(void)
{
git_index_entry *conflict_entry[3];
git_oid oid;
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
&conflict_entry[2], repo_index, "conflicts-one.txt"));
cl_assert(strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0);
git_oid_fromstr(&oid, CONFLICTS_ONE_ANCESTOR_OID);
cl_assert(git_oid_cmp(&conflict_entry[0]->oid, &oid) == 0);
git_oid_fromstr(&oid, CONFLICTS_ONE_OUR_OID);
cl_assert(git_oid_cmp(&conflict_entry[1]->oid, &oid) == 0);
git_oid_fromstr(&oid, CONFLICTS_ONE_THEIR_OID);
cl_assert(git_oid_cmp(&conflict_entry[2]->oid, &oid) == 0);
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
&conflict_entry[2], repo_index, "conflicts-two.txt"));
cl_assert(strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0);
git_oid_fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID);
cl_assert(git_oid_cmp(&conflict_entry[0]->oid, &oid) == 0);
git_oid_fromstr(&oid, CONFLICTS_TWO_OUR_OID);
cl_assert(git_oid_cmp(&conflict_entry[1]->oid, &oid) == 0);
git_oid_fromstr(&oid, CONFLICTS_TWO_THEIR_OID);
cl_assert(git_oid_cmp(&conflict_entry[2]->oid, &oid) == 0);
}
void test_index_conflicts__remove(void)
{
git_index_entry *entry;
size_t i;
cl_assert(git_index_entrycount(repo_index) == 8);
cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-one.txt"));
cl_assert(git_index_entrycount(repo_index) == 5);
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0);
}
cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-two.txt"));
cl_assert(git_index_entrycount(repo_index) == 2);
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
cl_assert(strcmp(entry->path, "conflicts-two.txt") != 0);
}
}
void test_index_conflicts__moved_to_reuc(void)
{
git_index_entry *entry;
size_t i;
cl_assert(git_index_entrycount(repo_index) == 8);
cl_git_mkfile("./mergedrepo/conflicts-one.txt", "new-file\n");
cl_git_pass(git_index_add_from_workdir(repo_index, "conflicts-one.txt"));
cl_assert(git_index_entrycount(repo_index) == 6);
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
if (strcmp(entry->path, "conflicts-one.txt") == 0)
cl_assert(git_index_entry_stage(entry) == 0);
}
}
void test_index_conflicts__remove_all_conflicts(void)
{
size_t i;
git_index_entry *entry;
cl_assert(git_index_entrycount(repo_index) == 8);
git_index_conflict_cleanup(repo_index);
cl_assert(git_index_entrycount(repo_index) == 2);
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
cl_assert(git_index_entry_stage(entry) == 0);
}
}
void test_index_conflicts__partial(void)
{
git_index_entry ancestor_entry, our_entry, their_entry;
git_index_entry *conflict_entry[3];
cl_assert(git_index_entrycount(repo_index) == 8);
memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
memset(&our_entry, 0x0, sizeof(git_index_entry));
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.path = "test-one.txt";
ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
git_oid_fromstr(&ancestor_entry.oid, TEST_ANCESTOR_OID);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, NULL, NULL));
cl_assert(git_index_entrycount(repo_index) == 9);
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
&conflict_entry[2], repo_index, "test-one.txt"));
cl_assert(git_oid_cmp(&ancestor_entry.oid, &conflict_entry[0]->oid) == 0);
cl_assert(conflict_entry[1] == NULL);
cl_assert(conflict_entry[2] == NULL);
}
......@@ -25,7 +25,7 @@ void test_index_filemodes__read(void)
cl_assert_equal_i(6, git_index_entrycount(index));
for (i = 0; i < 6; ++i) {
git_index_entry *entry = git_index_get(index, i);
git_index_entry *entry = git_index_get_byindex(index, i);
cl_assert(entry != NULL);
cl_assert(((entry->mode & 0100) ? 1 : 0) == expected[i]);
}
......@@ -56,35 +56,15 @@ static void add_and_check_mode(
int pos;
git_index_entry *entry;
cl_git_pass(git_index_add(index, filename, 0));
cl_git_pass(git_index_add_from_workdir(index, filename));
pos = git_index_find(index, filename);
cl_assert(pos >= 0);
entry = git_index_get(index, pos);
entry = git_index_get_byindex(index, pos);
cl_assert(entry->mode == expect_mode);
}
static void append_and_check_mode(
git_index *index, const char *filename, unsigned int expect_mode)
{
unsigned int before, after;
git_index_entry *entry;
before = git_index_entrycount(index);
cl_git_pass(git_index_append(index, filename, 0));
after = git_index_entrycount(index);
cl_assert_equal_i(before + 1, after);
/* bypass git_index_get since that resorts the index */
entry = (git_index_entry *)git_vector_get(&index->entries, after - 1);
cl_assert_equal_s(entry->path, filename);
cl_assert(expect_mode == entry->mode);
}
void test_index_filemodes__untrusted(void)
{
git_config *cfg;
......@@ -114,23 +94,7 @@ void test_index_filemodes__untrusted(void)
replace_file_with_mode("exec_on", "filemodes/exec_on.1", 0755);
add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 5 - append 0644 over existing 0644 -> expect 0644 */
replace_file_with_mode("exec_off", "filemodes/exec_off.2", 0644);
append_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB);
/* 6 - append 0644 over existing 0755 -> expect 0755 */
replace_file_with_mode("exec_on", "filemodes/exec_on.2", 0644);
append_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 7 - append 0755 over existing 0644 -> expect 0644 */
replace_file_with_mode("exec_off", "filemodes/exec_off.3", 0755);
append_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB);
/* 8 - append 0755 over existing 0755 -> expect 0755 */
replace_file_with_mode("exec_on", "filemodes/exec_on.3", 0755);
append_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 9 - add new 0644 -> expect 0644 */
/* 5 - add new 0644 -> expect 0644 */
cl_git_write2file("filemodes/new_off", "blah",
O_WRONLY | O_CREAT | O_TRUNC, 0644);
add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB);
......@@ -139,7 +103,7 @@ void test_index_filemodes__untrusted(void)
* that doesn't support filemodes correctly, so skip it.
*/
if (can_filemode) {
/* 10 - add 0755 -> expect 0755 */
/* 6 - add 0755 -> expect 0755 */
cl_git_write2file("filemodes/new_on", "blah",
O_WRONLY | O_CREAT | O_TRUNC, 0755);
add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB_EXECUTABLE);
......@@ -182,28 +146,12 @@ void test_index_filemodes__trusted(void)
replace_file_with_mode("exec_on", "filemodes/exec_on.1", 0755);
add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 5 - append 0644 over existing 0644 -> expect 0644 */
replace_file_with_mode("exec_off", "filemodes/exec_off.2", 0644);
append_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB);
/* 6 - append 0644 over existing 0755 -> expect 0644 */
replace_file_with_mode("exec_on", "filemodes/exec_on.2", 0644);
append_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB);
/* 7 - append 0755 over existing 0644 -> expect 0755 */
replace_file_with_mode("exec_off", "filemodes/exec_off.3", 0755);
append_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 8 - append 0755 over existing 0755 -> expect 0755 */
replace_file_with_mode("exec_on", "filemodes/exec_on.3", 0755);
append_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 9 - add new 0644 -> expect 0644 */
/* 5 - add new 0644 -> expect 0644 */
cl_git_write2file("filemodes/new_off", "blah",
O_WRONLY | O_CREAT | O_TRUNC, 0644);
add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB);
/* 10 - add 0755 -> expect 0755 */
/* 6 - add 0755 -> expect 0755 */
cl_git_write2file("filemodes/new_on", "blah",
O_WRONLY | O_CREAT | O_TRUNC, 0755);
add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB_EXECUTABLE);
......
......@@ -24,9 +24,9 @@ void test_index_read_tree__read_write_involution(void)
cl_git_mkfile("./read_tree/abc/d", NULL);
cl_git_mkfile("./read_tree/abc_d", NULL);
cl_git_pass(git_index_add(index, "abc-d", 0));
cl_git_pass(git_index_add(index, "abc_d", 0));
cl_git_pass(git_index_add(index, "abc/d", 0));
cl_git_pass(git_index_add_from_workdir(index, "abc-d"));
cl_git_pass(git_index_add_from_workdir(index, "abc_d"));
cl_git_pass(git_index_add_from_workdir(index, "abc/d"));
/* write-tree */
cl_git_pass(git_tree_create_fromindex(&expected, index));
......
......@@ -19,28 +19,28 @@ void test_index_rename__single_file(void)
cl_git_mkfile("./rename/lame.name.txt", "new_file\n");
/* This should add a new blob to the object database in 'd4/fa8600b4f37d7516bef4816ae2c64dbf029e3a' */
cl_git_pass(git_index_add(index, "lame.name.txt", 0));
cl_git_pass(git_index_add_from_workdir(index, "lame.name.txt"));
cl_assert(git_index_entrycount(index) == 1);
cl_git_pass(git_oid_fromstr(&expected, "d4fa8600b4f37d7516bef4816ae2c64dbf029e3a"));
position = git_index_find(index, "lame.name.txt");
entry = git_index_get(index, position);
entry = git_index_get_byindex(index, position);
cl_assert(git_oid_cmp(&expected, &entry->oid) == 0);
/* This removes the entry from the index, but not from the object database */
cl_git_pass(git_index_remove(index, position));
cl_git_pass(git_index_remove(index, "lame.name.txt", 0));
cl_assert(git_index_entrycount(index) == 0);
p_rename("./rename/lame.name.txt", "./rename/fancy.name.txt");
cl_git_pass(git_index_add(index, "fancy.name.txt", 0));
cl_git_pass(git_index_add_from_workdir(index, "fancy.name.txt"));
cl_assert(git_index_entrycount(index) == 1);
position = git_index_find(index, "fancy.name.txt");
entry = git_index_get(index, position);
entry = git_index_get_byindex(index, position);
cl_assert(git_oid_cmp(&expected, &entry->oid) == 0);
git_index_free(index);
......
#include "clar_libgit2.h"
#include "index.h"
#include "git2/repository.h"
static git_repository *repo;
static git_index *repo_index;
#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
#define ONE_ANCESTOR_OID "478871385b9cd03908c5383acfd568bef023c6b3"
#define ONE_OUR_OID "4458b8bc9e72b6c8755ae456f60e9844d0538d8c"
#define ONE_THEIR_OID "8b72416545c7e761b64cecad4f1686eae4078aa8"
#define TWO_ANCESTOR_OID "9d81f82fccc7dcd7de7a1ffead1815294c2e092c"
#define TWO_OUR_OID "8f3c06cff9a83757cec40c80bc9bf31a2582bde9"
#define TWO_THEIR_OID "887b153b165d32409c70163e0f734c090f12f673"
// Fixture setup and teardown
void test_index_reuc__initialize(void)
{
repo = cl_git_sandbox_init("mergedrepo");
git_repository_index(&repo_index, repo);
}
void test_index_reuc__cleanup(void)
{
git_index_free(repo_index);
cl_git_sandbox_cleanup();
}
void test_index_reuc__read_bypath(void)
{
const git_index_reuc_entry *reuc;
git_oid oid;
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "two.txt"));
cl_assert(strcmp(reuc->path, "two.txt") == 0);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
git_oid_fromstr(&oid, TWO_OUR_OID);
cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
git_oid_fromstr(&oid, TWO_THEIR_OID);
cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "one.txt"));
cl_assert(strcmp(reuc->path, "one.txt") == 0);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid_fromstr(&oid, ONE_ANCESTOR_OID);
cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
git_oid_fromstr(&oid, ONE_OUR_OID);
cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
git_oid_fromstr(&oid, ONE_THEIR_OID);
cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}
void test_index_reuc__ignore_case(void)
{
const git_index_reuc_entry *reuc;
git_oid oid;
int index_caps;
index_caps = git_index_caps(repo_index);
index_caps &= ~GIT_INDEXCAP_IGNORE_CASE;
cl_git_pass(git_index_set_caps(repo_index, index_caps));
cl_assert(!git_index_reuc_get_bypath(repo_index, "TWO.txt"));
index_caps |= GIT_INDEXCAP_IGNORE_CASE;
cl_git_pass(git_index_set_caps(repo_index, index_caps));
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "TWO.txt"));
cl_assert(strcmp(reuc->path, "two.txt") == 0);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
git_oid_fromstr(&oid, TWO_OUR_OID);
cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
git_oid_fromstr(&oid, TWO_THEIR_OID);
cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}
void test_index_reuc__read_byindex(void)
{
const git_index_reuc_entry *reuc;
git_oid oid;
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));
cl_assert(strcmp(reuc->path, "one.txt") == 0);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid_fromstr(&oid, ONE_ANCESTOR_OID);
cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
git_oid_fromstr(&oid, ONE_OUR_OID);
cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
git_oid_fromstr(&oid, ONE_THEIR_OID);
cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 1));
cl_assert(strcmp(reuc->path, "two.txt") == 0);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
git_oid_fromstr(&oid, TWO_OUR_OID);
cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
git_oid_fromstr(&oid, TWO_THEIR_OID);
cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}
void test_index_reuc__updates_existing(void)
{
const git_index_reuc_entry *reuc;
git_oid ancestor_oid, our_oid, their_oid, oid;
int index_caps;
git_index_clear(repo_index);
index_caps = git_index_caps(repo_index);
index_caps |= GIT_INDEXCAP_IGNORE_CASE;
cl_git_pass(git_index_set_caps(repo_index, index_caps));
git_oid_fromstr(&ancestor_oid, TWO_ANCESTOR_OID);
git_oid_fromstr(&our_oid, TWO_OUR_OID);
git_oid_fromstr(&their_oid, TWO_THEIR_OID);
cl_git_pass(git_index_reuc_add(repo_index, "two.txt",
0100644, &ancestor_oid,
0100644, &our_oid,
0100644, &their_oid));
cl_git_pass(git_index_reuc_add(repo_index, "TWO.txt",
0100644, &our_oid,
0100644, &their_oid,
0100644, &ancestor_oid));
cl_assert_equal_i(1, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));
cl_assert(strcmp(reuc->path, "TWO.txt") == 0);
git_oid_fromstr(&oid, TWO_OUR_OID);
cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
git_oid_fromstr(&oid, TWO_THEIR_OID);
cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}
void test_index_reuc__remove(void)
{
git_oid oid;
const git_index_reuc_entry *reuc;
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
cl_git_pass(git_index_reuc_remove(repo_index, 0));
cl_git_fail(git_index_reuc_remove(repo_index, 1));
cl_assert_equal_i(1, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));
cl_assert(strcmp(reuc->path, "two.txt") == 0);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
git_oid_fromstr(&oid, TWO_OUR_OID);
cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
git_oid_fromstr(&oid, TWO_THEIR_OID);
cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}
void test_index_reuc__write(void)
{
git_oid ancestor_oid, our_oid, their_oid;
const git_index_reuc_entry *reuc;
git_index_clear(repo_index);
/* Write out of order to ensure sorting is correct */
git_oid_fromstr(&ancestor_oid, TWO_ANCESTOR_OID);
git_oid_fromstr(&our_oid, TWO_OUR_OID);
git_oid_fromstr(&their_oid, TWO_THEIR_OID);
cl_git_pass(git_index_reuc_add(repo_index, "two.txt",
0100644, &ancestor_oid,
0100644, &our_oid,
0100644, &their_oid));
git_oid_fromstr(&ancestor_oid, ONE_ANCESTOR_OID);
git_oid_fromstr(&our_oid, ONE_OUR_OID);
git_oid_fromstr(&their_oid, ONE_THEIR_OID);
cl_git_pass(git_index_reuc_add(repo_index, "one.txt",
0100644, &ancestor_oid,
0100644, &our_oid,
0100644, &their_oid));
cl_git_pass(git_index_write(repo_index));
cl_git_pass(git_index_read(repo_index));
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
/* ensure sort order was round-tripped correct */
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));
cl_assert(strcmp(reuc->path, "one.txt") == 0);
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 1));
cl_assert(strcmp(reuc->path, "two.txt") == 0);
}
#include "clar_libgit2.h"
#include "index.h"
#include "git2/repository.h"
static git_repository *repo;
static git_index *repo_index;
#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
// Fixture setup and teardown
void test_index_stage__initialize(void)
{
repo = cl_git_sandbox_init("mergedrepo");
git_repository_index(&repo_index, repo);
}
void test_index_stage__cleanup(void)
{
git_index_free(repo_index);
cl_git_sandbox_cleanup();
}
void test_index_stage__add_always_adds_stage_0(void)
{
int entry_idx;
git_index_entry *entry;
cl_git_mkfile("./mergedrepo/new-file.txt", "new-file\n");
cl_git_pass(git_index_add_from_workdir(repo_index, "new-file.txt"));
cl_assert((entry_idx = git_index_find(repo_index, "new-file.txt")) >= 0);
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 0);
}
void test_index_stage__find_gets_first_stage(void)
{
int entry_idx;
git_index_entry *entry;
cl_assert((entry_idx = git_index_find(repo_index, "one.txt")) >= 0);
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 0);
cl_assert((entry_idx = git_index_find(repo_index, "two.txt")) >= 0);
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 0);
cl_assert((entry_idx = git_index_find(repo_index, "conflicts-one.txt")) >= 0);
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 1);
cl_assert((entry_idx = git_index_find(repo_index, "conflicts-two.txt")) >= 0);
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 1);
}
......@@ -231,15 +231,19 @@ void test_index_tests__add(void)
cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
/* Add the new file to the index */
cl_git_pass(git_index_add(index, "test.txt", 0));
cl_git_pass(git_index_add_from_workdir(index, "test.txt"));
/* Wow... it worked! */
cl_assert(git_index_entrycount(index) == 1);
entry = git_index_get(index, 0);
entry = git_index_get_byindex(index, 0);
/* And the built-in hashing mechanism worked as expected */
cl_assert(git_oid_cmp(&id1, &entry->oid) == 0);
/* Test access by path instead of index */
cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
cl_assert(git_oid_cmp(&id1, &entry->oid) == 0);
git_index_free(index);
git_repository_free(repo);
}
......
......@@ -71,9 +71,9 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
*/
cl_git_mkfile("treebuilder/test.txt", "test\n");
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add(index, "test.txt", 0));
cl_git_pass(git_index_add_from_workdir(index, "test.txt"));
entry = git_index_get(index, 0);
entry = git_index_get_byindex(index, 0);
cl_assert(git_oid_cmp(&expected_blob_oid, &entry->oid) == 0);
......
e2809157a7766f272e4cfe26e61ef2678a5357ff
Merge branch 'branch'
Conflicts:
conflicts-one.txt
conflicts-two.txt
3a34580a35add43a4cf361e8e9a30060a905c876
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
Unnamed repository; edit this file 'description' to name the repository.
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
0000000000000000000000000000000000000000 9a05ccb4e0f948de03128e095f39dae6976751c5 Edward Thomson <ethomson@edwardthomson.com> 1351371828 -0500 commit (initial): initial
9a05ccb4e0f948de03128e095f39dae6976751c5 9a05ccb4e0f948de03128e095f39dae6976751c5 Edward Thomson <ethomson@edwardthomson.com> 1351371835 -0500 checkout: moving from master to branch
9a05ccb4e0f948de03128e095f39dae6976751c5 e2809157a7766f272e4cfe26e61ef2678a5357ff Edward Thomson <ethomson@edwardthomson.com> 1351371872 -0500 commit: branch
e2809157a7766f272e4cfe26e61ef2678a5357ff 9a05ccb4e0f948de03128e095f39dae6976751c5 Edward Thomson <ethomson@edwardthomson.com> 1351371873 -0500 checkout: moving from branch to master
9a05ccb4e0f948de03128e095f39dae6976751c5 3a34580a35add43a4cf361e8e9a30060a905c876 Edward Thomson <ethomson@edwardthomson.com> 1351372106 -0500 commit: master
0000000000000000000000000000000000000000 9a05ccb4e0f948de03128e095f39dae6976751c5 Edward Thomson <ethomson@edwardthomson.com> 1351371835 -0500 branch: Created from HEAD
9a05ccb4e0f948de03128e095f39dae6976751c5 e2809157a7766f272e4cfe26e61ef2678a5357ff Edward Thomson <ethomson@edwardthomson.com> 1351371872 -0500 commit: branch
0000000000000000000000000000000000000000 9a05ccb4e0f948de03128e095f39dae6976751c5 Edward Thomson <ethomson@edwardthomson.com> 1351371828 -0500 commit (initial): initial
9a05ccb4e0f948de03128e095f39dae6976751c5 3a34580a35add43a4cf361e8e9a30060a905c876 Edward Thomson <ethomson@edwardthomson.com> 1351372106 -0500 commit: master
e2809157a7766f272e4cfe26e61ef2678a5357ff
3a34580a35add43a4cf361e8e9a30060a905c876
<<<<<<< HEAD
This is most certainly a conflict!
=======
This is a conflict!!!
>>>>>>> branch
<<<<<<< HEAD
This is without question another conflict!
=======
This is another conflict!!!
>>>>>>> branch
This is file one!
This is file one.
This is file one.
This is file one.
This is file one.
This is file one.
This is file one.
This is file one.
This is file one.
This is file one!
This is file two!
This is file two.
This is file two.
This is file two.
This is file two.
This is file two.
This is file two.
This is file two.
This is file two.
This is file two.
This is file two.
This is file two!
......@@ -30,7 +30,7 @@ static void push_three_states(void)
cl_git_mkfile("stash/zero.txt", "content\n");
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add(index, "zero.txt", 0));
cl_git_pass(git_index_add_from_workdir(index, "zero.txt"));
commit_staged_files(&oid, index, signature);
cl_git_mkfile("stash/one.txt", "content\n");
......
......@@ -246,8 +246,8 @@ void test_stash_save__cannot_stash_when_there_are_no_local_change(void)
* 'what' and 'who' are being committed.
* 'when' remain untracked.
*/
git_index_add(index, "what", 0);
git_index_add(index, "who", 0);
git_index_add_from_workdir(index, "what");
git_index_add_from_workdir(index, "who");
cl_git_pass(git_index_write(index));
commit_staged_files(&commit_oid, index, signature);
git_index_free(index);
......
......@@ -44,10 +44,10 @@ void setup_stash(git_repository *repo, git_signature *signature)
cl_git_mkfile("stash/.gitignore", "*.ignore\n");
cl_git_pass(git_index_add(index, "what", 0));
cl_git_pass(git_index_add(index, "how", 0));
cl_git_pass(git_index_add(index, "who", 0));
cl_git_pass(git_index_add(index, ".gitignore", 0));
cl_git_pass(git_index_add_from_workdir(index, "what"));
cl_git_pass(git_index_add_from_workdir(index, "how"));
cl_git_pass(git_index_add_from_workdir(index, "who"));
cl_git_pass(git_index_add_from_workdir(index, ".gitignore"));
cl_git_pass(git_index_write(index));
commit_staged_files(&commit_oid, index, signature);
......@@ -56,8 +56,8 @@ void setup_stash(git_repository *repo, git_signature *signature)
cl_git_rewritefile("stash/how", "not so small and\n"); /* e6d64adb2c7f3eb8feb493b556cc8070dca379a3 */
cl_git_rewritefile("stash/who", "funky world\n"); /* a0400d4954659306a976567af43125a0b1aa8595 */
cl_git_pass(git_index_add(index, "what", 0));
cl_git_pass(git_index_add(index, "how", 0));
cl_git_pass(git_index_add_from_workdir(index, "what"));
cl_git_pass(git_index_add_from_workdir(index, "how"));
cl_git_pass(git_index_write(index));
cl_git_rewritefile("stash/what", "see you later\n"); /* bc99dc98b3eba0e9157e94769cd4d49cb49de449 */
......
......@@ -438,7 +438,7 @@ void test_status_worktree__first_commit_in_progress(void)
cl_assert(result.status == GIT_STATUS_WT_NEW);
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add(index, "testfile.txt", 0));
cl_git_pass(git_index_add_from_workdir(index, "testfile.txt"));
cl_git_pass(git_index_write(index));
memset(&result, 0, sizeof(result));
......@@ -570,7 +570,7 @@ void test_status_worktree__bracket_in_filename(void)
/* add the file to the index */
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add(index, FILE_WITH_BRACKET, 0));
cl_git_pass(git_index_add_from_workdir(index, FILE_WITH_BRACKET));
cl_git_pass(git_index_write(index));
memset(&result, 0, sizeof(result));
......@@ -648,7 +648,7 @@ void test_status_worktree__space_in_filename(void)
/* add the file to the index */
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add(index, FILE_WITH_SPACE, 0));
cl_git_pass(git_index_add_from_workdir(index, FILE_WITH_SPACE));
cl_git_pass(git_index_write(index));
memset(&result, 0, sizeof(result));
......@@ -816,7 +816,7 @@ void test_status_worktree__new_staged_file_must_handle_crlf(void)
cl_git_mkfile("getting_started/testfile.txt", "content\r\n"); // Content with CRLF
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add(index, "testfile.txt", 0));
cl_git_pass(git_index_add_from_workdir(index, "testfile.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_status_file(&status, repo, "testfile.txt"));
......
......@@ -105,7 +105,7 @@ void test_submodule_status__ignore_none(void)
cl_git_pass(git_repository_index(&index, g_repo));
pos = git_index_find(index, "sm_changed_head");
cl_assert(pos >= 0);
cl_git_pass(git_index_remove(index, pos));
cl_git_pass(git_index_remove(index, "sm_changed_head", 0));
cl_git_pass(git_index_write(index));
git_index_free(index);
......
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