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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
#include "clar_libgit2.h"
#include "index.h"
#include "git2/sys/index.h"
#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"
#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);
repo_index = NULL;
cl_git_sandbox_cleanup();
}
void test_index_reuc__add(void)
{
git_oid ancestor_oid, our_oid, their_oid;
const git_index_reuc_entry *reuc;
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, "newfile.txt",
0100644, &ancestor_oid,
0100644, &our_oid,
0100644, &their_oid));
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "newfile.txt"));
cl_assert_equal_s("newfile.txt", reuc->path);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
cl_assert(git_oid_cmp(&reuc->oid[0], &ancestor_oid) == 0);
cl_assert(git_oid_cmp(&reuc->oid[1], &our_oid) == 0);
cl_assert(git_oid_cmp(&reuc->oid[2], &their_oid) == 0);
}
void test_index_reuc__add_no_ancestor(void)
{
git_oid ancestor_oid, our_oid, their_oid;
const git_index_reuc_entry *reuc;
memset(&ancestor_oid, 0x0, sizeof(git_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, "newfile.txt",
0, NULL,
0100644, &our_oid,
0100644, &their_oid));
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "newfile.txt"));
cl_assert_equal_s("newfile.txt", reuc->path);
cl_assert(reuc->mode[0] == 0);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
cl_assert(git_oid_cmp(&reuc->oid[0], &ancestor_oid) == 0);
cl_assert(git_oid_cmp(&reuc->oid[1], &our_oid) == 0);
cl_assert(git_oid_cmp(&reuc->oid[2], &their_oid) == 0);
}
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_equal_s("two.txt", reuc->path);
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_equal_s("one.txt", reuc->path);
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_equal_s("two.txt", reuc->path);
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_equal_s("one.txt", reuc->path);
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_equal_s("two.txt", reuc->path);
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_equal_s("TWO.txt", reuc->path);
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_equal_s("two.txt", reuc->path);
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_equal_s("one.txt", reuc->path);
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 1));
cl_assert_equal_s("two.txt", reuc->path);
}
static int reuc_entry_exists(void)
{
return (git_index_reuc_get_bypath(repo_index, "newfile.txt") != NULL);
}
void test_index_reuc__cleaned_on_reset_hard(void)
{
git_object *target;
retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
test_index_reuc__add();
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD));
cl_assert(reuc_entry_exists() == false);
git_object_free(target);
}
void test_index_reuc__cleaned_on_reset_mixed(void)
{
git_object *target;
retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
test_index_reuc__add();
cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED));
cl_assert(reuc_entry_exists() == false);
git_object_free(target);
}
void test_index_reuc__retained_on_reset_soft(void)
{
git_object *target;
retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
git_reset(repo, target, GIT_RESET_HARD);
test_index_reuc__add();
cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
cl_assert(reuc_entry_exists() == true);
git_object_free(target);
}
void test_index_reuc__cleaned_on_checkout_tree(void)
{
git_oid oid;
git_object *obj;
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
test_index_reuc__add();
git_reference_name_to_id(&oid, repo, "refs/heads/master");
git_object_lookup(&obj, repo, &oid, GIT_OBJ_ANY);
git_checkout_tree(repo, obj, &opts);
cl_assert(reuc_entry_exists() == false);
git_object_free(obj);
}
void test_index_reuc__cleaned_on_checkout_head(void)
{
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
test_index_reuc__add();
git_checkout_head(repo, &opts);
cl_assert(reuc_entry_exists() == false);
}
void test_index_reuc__retained_on_checkout_index(void)
{
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
test_index_reuc__add();
git_checkout_index(repo, repo_index, &opts);
cl_assert(reuc_entry_exists() == true);
}