names.c 5.7 KB
Newer Older
Edward Thomson committed
1 2
#include "clar_libgit2.h"
#include "index.h"
3
#include "git2/sys/index.h"
Edward Thomson committed
4 5 6 7 8 9 10 11 12
#include "git2/repository.h"
#include "../reset/reset_helpers.h"

static git_repository *repo;
static git_index *repo_index;

#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"

13
/* Fixture setup and teardown */
Edward Thomson committed
14 15 16 17 18 19 20 21 22 23
void test_index_names__initialize(void)
{
	repo = cl_git_sandbox_init("mergedrepo");
	git_repository_index(&repo_index, repo);
}

void test_index_names__cleanup(void)
{
	git_index_free(repo_index);
	repo_index = NULL;
nulltoken committed
24

Edward Thomson committed
25 26 27
	cl_git_sandbox_cleanup();
}

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
static void index_add_conflicts(void)
{
	git_index_entry entry = {{0}};
	const char *paths[][3] = {
		{ "ancestor", "ours", "theirs" },
		{ "ancestor2", "ours2", "theirs2" },
		{ "ancestor3", "ours3", "theirs3" } };
	const char **conflict;
	size_t i;

	for (i = 0; i < ARRAY_SIZE(paths); i++) {
		conflict = paths[i];

		/* ancestor */
		entry.path = conflict[0];
		entry.mode = GIT_FILEMODE_BLOB;
44
		GIT_INDEX_ENTRY_STAGE_SET(&entry, GIT_INDEX_STAGE_ANCESTOR);
45 46 47 48 49 50
		git_oid_fromstr(&entry.id, "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81");
		cl_git_pass(git_index_add(repo_index, &entry));

		/* ours */
		entry.path = conflict[1];
		entry.mode = GIT_FILEMODE_BLOB;
51
		GIT_INDEX_ENTRY_STAGE_SET(&entry, GIT_INDEX_STAGE_OURS);
52 53 54 55 56 57
		git_oid_fromstr(&entry.id, "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81");
		cl_git_pass(git_index_add(repo_index, &entry));

		/* theirs */
		entry.path = conflict[2];
		entry.mode = GIT_FILEMODE_BLOB;
58
		GIT_INDEX_ENTRY_STAGE_SET(&entry, GIT_INDEX_STAGE_THEIRS);
59 60 61 62 63
		git_oid_fromstr(&entry.id, "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81");
		cl_git_pass(git_index_add(repo_index, &entry));
	}
}

Edward Thomson committed
64 65 66 67
void test_index_names__add(void)
{
	const git_index_name_entry *conflict_name;

68
	index_add_conflicts();
Edward Thomson committed
69 70 71
	cl_git_pass(git_index_name_add(repo_index, "ancestor", "ours", "theirs"));
	cl_git_pass(git_index_name_add(repo_index, "ancestor2", "ours2", NULL));
	cl_git_pass(git_index_name_add(repo_index, "ancestor3", NULL, "theirs3"));
nulltoken committed
72

Edward Thomson committed
73
	cl_assert(git_index_name_entrycount(repo_index) == 3);
nulltoken committed
74

Edward Thomson committed
75 76 77 78
	conflict_name = git_index_name_get_byindex(repo_index, 0);
	cl_assert(strcmp(conflict_name->ancestor, "ancestor") == 0);
	cl_assert(strcmp(conflict_name->ours, "ours") == 0);
	cl_assert(strcmp(conflict_name->theirs, "theirs") == 0);
nulltoken committed
79

Edward Thomson committed
80 81 82 83 84 85 86 87 88
	conflict_name = git_index_name_get_byindex(repo_index, 1);
	cl_assert(strcmp(conflict_name->ancestor, "ancestor2") == 0);
	cl_assert(strcmp(conflict_name->ours, "ours2") == 0);
	cl_assert(conflict_name->theirs == NULL);

	conflict_name = git_index_name_get_byindex(repo_index, 2);
	cl_assert(strcmp(conflict_name->ancestor, "ancestor3") == 0);
	cl_assert(conflict_name->ours == NULL);
	cl_assert(strcmp(conflict_name->theirs, "theirs3") == 0);
89 90

	cl_git_pass(git_index_write(repo_index));
Edward Thomson committed
91 92 93 94 95
}

void test_index_names__roundtrip(void)
{
	const git_index_name_entry *conflict_name;
nulltoken committed
96

Edward Thomson committed
97 98 99
	cl_git_pass(git_index_name_add(repo_index, "ancestor", "ours", "theirs"));
	cl_git_pass(git_index_name_add(repo_index, "ancestor2", "ours2", NULL));
	cl_git_pass(git_index_name_add(repo_index, "ancestor3", NULL, "theirs3"));
nulltoken committed
100

Edward Thomson committed
101 102 103
	cl_git_pass(git_index_write(repo_index));
	git_index_clear(repo_index);
	cl_assert(git_index_name_entrycount(repo_index) == 0);
nulltoken committed
104

105
	cl_git_pass(git_index_read(repo_index, true));
Edward Thomson committed
106
	cl_assert(git_index_name_entrycount(repo_index) == 3);
nulltoken committed
107

Edward Thomson committed
108 109 110 111
	conflict_name = git_index_name_get_byindex(repo_index, 0);
	cl_assert(strcmp(conflict_name->ancestor, "ancestor") == 0);
	cl_assert(strcmp(conflict_name->ours, "ours") == 0);
	cl_assert(strcmp(conflict_name->theirs, "theirs") == 0);
nulltoken committed
112

Edward Thomson committed
113 114 115 116
	conflict_name = git_index_name_get_byindex(repo_index, 1);
	cl_assert(strcmp(conflict_name->ancestor, "ancestor2") == 0);
	cl_assert(strcmp(conflict_name->ours, "ours2") == 0);
	cl_assert(conflict_name->theirs == NULL);
nulltoken committed
117

Edward Thomson committed
118 119 120 121
	conflict_name = git_index_name_get_byindex(repo_index, 2);
	cl_assert(strcmp(conflict_name->ancestor, "ancestor3") == 0);
	cl_assert(conflict_name->ours == NULL);
	cl_assert(strcmp(conflict_name->theirs, "theirs3") == 0);
122 123 124 125 126 127
}

void test_index_names__cleaned_on_reset_hard(void)
{
	git_object *target;

128
	cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
129 130

	test_index_names__add();
131
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
132 133 134 135 136 137 138 139 140
	cl_assert(git_index_name_entrycount(repo_index) == 0);

	git_object_free(target);
}

void test_index_names__cleaned_on_reset_mixed(void)
{
	git_object *target;

141
	cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
142 143

	test_index_names__add();
144
	cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL));
145 146 147 148 149 150 151 152 153
	cl_assert(git_index_name_entrycount(repo_index) == 0);

	git_object_free(target);
}

void test_index_names__cleaned_on_checkout_tree(void)
{
	git_oid oid;
	git_object *obj;
154
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
155

156
	opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
157 158

	test_index_names__add();
159
	cl_git_pass(git_reference_name_to_id(&oid, repo, "refs/heads/master"));
160
	cl_git_pass(git_object_lookup(&obj, repo, &oid, GIT_OBJECT_ANY));
161
	cl_git_pass(git_checkout_tree(repo, obj, &opts));
162
	cl_assert_equal_sz(0, git_index_name_entrycount(repo_index));
163 164 165 166 167 168

	git_object_free(obj);
}

void test_index_names__cleaned_on_checkout_head(void)
{
169
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
170

171
	opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
172 173

	test_index_names__add();
174
	cl_git_pass(git_checkout_head(repo, &opts));
175
	cl_assert_equal_sz(0, git_index_name_entrycount(repo_index));
176 177 178 179
}

void test_index_names__retained_on_checkout_index(void)
{
180
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
181

182
	opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
nulltoken committed
183

184
	test_index_names__add();
185
	cl_git_pass(git_checkout_index(repo, repo_index, &opts));
186
	cl_assert(git_index_name_entrycount(repo_index) > 0);
Edward Thomson committed
187
}