rename.c 13.4 KB
Newer Older
Ben Straub committed
1 2
#include "clar_libgit2.h"

3
#include "fileops.h"
Ben Straub committed
4 5
#include "git2/reflog.h"
#include "reflog.h"
6
#include "refs.h"
7
#include "ref_helpers.h"
Ben Straub committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

static const char *loose_tag_ref_name = "refs/tags/e90810b";
static const char *packed_head_name = "refs/heads/packed";
static const char *packed_test_head_name = "refs/heads/packed-test";
static const char *ref_one_name = "refs/heads/one/branch";
static const char *ref_one_name_new = "refs/heads/two/branch";
static const char *ref_two_name = "refs/heads/two";
static const char *ref_master_name = "refs/heads/master";
static const char *ref_two_name_new = "refs/heads/two/two";

static git_repository *g_repo;



void test_refs_rename__initialize(void)
{
24
	g_repo = cl_git_sandbox_init("testrepo");
25
	cl_git_pass(git_repository_set_ident(g_repo, "me", "foo@example.com"));
Ben Straub committed
26 27 28 29
}

void test_refs_rename__cleanup(void)
{
30
	cl_git_sandbox_cleanup();
Ben Straub committed
31 32 33 34 35 36
}



void test_refs_rename__loose(void)
{
37
	// rename a loose reference
38
	git_reference *looked_up_ref, *new_ref, *another_looked_up_ref;
Ben Straub committed
39 40 41 42
	git_buf temp_path = GIT_BUF_INIT;
	const char *new_name = "refs/tags/Nemo/knows/refs.kung-fu";

	/* Ensure the ref doesn't exist on the file system */
43
	cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), new_name));
Vicent Martí committed
44
	cl_assert(!git_path_exists(temp_path.ptr));
Ben Straub committed
45 46 47 48 49

	/* Retrieval of the reference to rename */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, loose_tag_ref_name));

	/* ... which is indeed loose */
50
	cl_assert(reference_is_packed(looked_up_ref) == 0);
Ben Straub committed
51 52

	/* Now that the reference is renamed... */
53
	cl_git_pass(git_reference_rename(&new_ref, looked_up_ref, new_name, 0, NULL));
54 55
	cl_assert_equal_s(new_ref->name, new_name);
	git_reference_free(looked_up_ref);
Ben Straub committed
56 57 58 59 60 61

	/* ...It can't be looked-up with the old name... */
	cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, loose_tag_ref_name));

	/* ...but the new name works ok... */
	cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, new_name));
62
	cl_assert_equal_s(new_ref->name, new_name);
Ben Straub committed
63

64 65 66
	/* .. the new ref is loose... */
	cl_assert(reference_is_packed(another_looked_up_ref) == 0);
	cl_assert(reference_is_packed(new_ref) == 0);
Ben Straub committed
67 68

	/* ...and the ref can be found in the file system */
69
	cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), new_name));
Vicent Martí committed
70
	cl_assert(git_path_exists(temp_path.ptr));
Ben Straub committed
71

72
	git_reference_free(new_ref);
Ben Straub committed
73 74 75 76 77 78
	git_reference_free(another_looked_up_ref);
	git_buf_free(&temp_path);
}

void test_refs_rename__packed(void)
{
79
	// rename a packed reference (should make it loose)
80
	git_reference *looked_up_ref, *new_ref, *another_looked_up_ref;
Ben Straub committed
81 82 83 84
	git_buf temp_path = GIT_BUF_INIT;
	const char *brand_new_name = "refs/heads/brand_new_name";

	/* Ensure the ref doesn't exist on the file system */
85
	cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), packed_head_name));
Vicent Martí committed
86
	cl_assert(!git_path_exists(temp_path.ptr));
Ben Straub committed
87 88 89 90 91

	/* The reference can however be looked-up... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));

	/* .. and it's packed */
92
	cl_assert(reference_is_packed(looked_up_ref) != 0);
Ben Straub committed
93 94

	/* Now that the reference is renamed... */
95
	cl_git_pass(git_reference_rename(&new_ref, looked_up_ref, brand_new_name, 0, NULL));
96 97
	cl_assert_equal_s(new_ref->name, brand_new_name);
	git_reference_free(looked_up_ref);
Ben Straub committed
98 99 100 101 102 103

	/* ...It can't be looked-up with the old name... */
	cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_head_name));

	/* ...but the new name works ok... */
	cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, brand_new_name));
Vicent Martí committed
104
	cl_assert_equal_s(another_looked_up_ref->name, brand_new_name);
Ben Straub committed
105 106

	/* .. the ref is no longer packed... */
107 108
	cl_assert(reference_is_packed(another_looked_up_ref) == 0);
	cl_assert(reference_is_packed(new_ref) == 0);
Ben Straub committed
109 110

	/* ...and the ref now happily lives in the file system */
111
	cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), brand_new_name));
Vicent Martí committed
112
	cl_assert(git_path_exists(temp_path.ptr));
Ben Straub committed
113

114
	git_reference_free(new_ref);
Ben Straub committed
115 116 117 118 119 120
	git_reference_free(another_looked_up_ref);
	git_buf_free(&temp_path);
}

void test_refs_rename__packed_doesnt_pack_others(void)
{
121
	// renaming a packed reference does not pack another reference which happens to be in both loose and pack state
122
	git_reference *looked_up_ref, *another_looked_up_ref, *renamed_ref;
Ben Straub committed
123 124 125 126
	git_buf temp_path = GIT_BUF_INIT;
	const char *brand_new_name = "refs/heads/brand_new_name";

	/* Ensure the other reference exists on the file system */
127
	cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), packed_test_head_name));
Vicent Martí committed
128
	cl_assert(git_path_exists(temp_path.ptr));
Ben Straub committed
129 130 131 132 133

	/* Lookup the other reference */
	cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));

	/* Ensure it's loose */
134
	cl_assert(reference_is_packed(another_looked_up_ref) == 0);
Ben Straub committed
135 136 137 138 139 140
	git_reference_free(another_looked_up_ref);

	/* Lookup the reference to rename */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));

	/* Ensure it's packed */
141
	cl_assert(reference_is_packed(looked_up_ref) != 0);
Ben Straub committed
142 143

	/* Now that the reference is renamed... */
144
	cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, brand_new_name, 0, NULL));
145
	git_reference_free(looked_up_ref);
Ben Straub committed
146 147 148 149 150

	/* Lookup the other reference */
	cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));

	/* Ensure it's loose */
151
	cl_assert(reference_is_packed(another_looked_up_ref) == 0);
Ben Straub committed
152 153

	/* Ensure the other ref still exists on the file system */
Vicent Martí committed
154
	cl_assert(git_path_exists(temp_path.ptr));
Ben Straub committed
155

156
	git_reference_free(renamed_ref);
Ben Straub committed
157 158 159 160 161 162
	git_reference_free(another_looked_up_ref);
	git_buf_free(&temp_path);
}

void test_refs_rename__name_collision(void)
{
163
	// can not rename a reference with the name of an existing reference
164
	git_reference *looked_up_ref, *renamed_ref;
Ben Straub committed
165 166 167 168 169

	/* An existing reference... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));

	/* Can not be renamed to the name of another existing reference. */
170
	cl_git_fail(git_reference_rename(&renamed_ref, looked_up_ref, packed_test_head_name, 0, NULL));
Ben Straub committed
171 172 173 174
	git_reference_free(looked_up_ref);

	/* Failure to rename it hasn't corrupted its state */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
Vicent Martí committed
175
	cl_assert_equal_s(looked_up_ref->name, packed_head_name);
Ben Straub committed
176 177 178 179 180 181

	git_reference_free(looked_up_ref);
}

void test_refs_rename__invalid_name(void)
{
182
	// can not rename a reference with an invalid name
183
	git_reference *looked_up_ref, *renamed_ref;
Ben Straub committed
184 185 186 187 188

	/* An existing oid reference... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));

	/* Can not be renamed with an invalid name. */
189 190
	cl_assert_equal_i(
		GIT_EINVALIDSPEC,
191
		git_reference_rename(&renamed_ref, looked_up_ref, "Hello! I'm a very invalid name.", 0, NULL));
192 193 194 195

	/* Can not be renamed outside of the refs hierarchy
	 * unless it's ALL_CAPS_AND_UNDERSCORES.
	 */
196
	cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_rename(&renamed_ref, looked_up_ref, "i-will-sudo-you", 0, NULL));
Ben Straub committed
197 198 199 200

	/* Failure to rename it hasn't corrupted its state */
	git_reference_free(looked_up_ref);
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
Vicent Martí committed
201
	cl_assert_equal_s(looked_up_ref->name, packed_test_head_name);
Ben Straub committed
202 203 204 205 206 207

	git_reference_free(looked_up_ref);
}

void test_refs_rename__force_loose_packed(void)
{
208
	// can force-rename a packed reference with the name of an existing loose and packed reference
209
	git_reference *looked_up_ref, *renamed_ref;
Ben Straub committed
210 211 212 213
	git_oid oid;

	/* An existing reference... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
214
	git_oid_cpy(&oid, git_reference_target(looked_up_ref));
Ben Straub committed
215 216

	/* Can be force-renamed to the name of another existing reference. */
217
	cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, packed_test_head_name, 1, NULL));
Ben Straub committed
218
	git_reference_free(looked_up_ref);
219
	git_reference_free(renamed_ref);
Ben Straub committed
220 221 222

	/* Check we actually renamed it */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
Vicent Martí committed
223
	cl_assert_equal_s(looked_up_ref->name, packed_test_head_name);
224
	cl_assert_equal_oid(&oid, git_reference_target(looked_up_ref));
Ben Straub committed
225 226 227 228 229 230 231 232
	git_reference_free(looked_up_ref);

	/* And that the previous one doesn't exist any longer */
	cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
}

void test_refs_rename__force_loose(void)
{
233
	// can force-rename a loose reference with the name of an existing loose reference
234
	git_reference *looked_up_ref, *renamed_ref;
Ben Straub committed
235 236 237 238
	git_oid oid;

	/* An existing reference... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/br2"));
239
	git_oid_cpy(&oid, git_reference_target(looked_up_ref));
Ben Straub committed
240 241

	/* Can be force-renamed to the name of another existing reference. */
242
	cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, "refs/heads/test", 1, NULL));
Ben Straub committed
243
	git_reference_free(looked_up_ref);
244
	git_reference_free(renamed_ref);
Ben Straub committed
245 246 247

	/* Check we actually renamed it */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/test"));
Vicent Martí committed
248
	cl_assert_equal_s(looked_up_ref->name,  "refs/heads/test");
249
	cl_assert_equal_oid(&oid, git_reference_target(looked_up_ref));
Ben Straub committed
250 251 252 253 254 255 256 257 258 259 260
	git_reference_free(looked_up_ref);

	/* And that the previous one doesn't exist any longer */
	cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/br2"));

	git_reference_free(looked_up_ref);
}


void test_refs_rename__overwrite(void)
{
261
	// can not overwrite name of existing reference
Ben Straub committed
262
	git_reference *ref, *ref_one, *ref_one_new, *ref_two;
263
	git_refdb *refdb;
Ben Straub committed
264 265 266 267 268
	git_oid id;

	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
	cl_assert(git_reference_type(ref) & GIT_REF_OID);

269
	git_oid_cpy(&id, git_reference_target(ref));
Ben Straub committed
270 271

	/* Create loose references */
272 273
	cl_git_pass(git_reference_create(&ref_one, g_repo, ref_one_name, &id, 0, NULL));
	cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name, &id, 0, NULL));
Ben Straub committed
274 275

	/* Pack everything */
276 277
	cl_git_pass(git_repository_refdb(&refdb, g_repo));
	cl_git_pass(git_refdb_compress(refdb));
Ben Straub committed
278 279

	/* Attempt to create illegal reference */
280
	cl_git_fail(git_reference_create(&ref_one_new, g_repo, ref_one_name_new, &id, 0, NULL));
Ben Straub committed
281 282 283 284 285 286 287 288

	/* Illegal reference couldn't be created so this is supposed to fail */
	cl_git_fail(git_reference_lookup(&ref_one_new, g_repo, ref_one_name_new));

	git_reference_free(ref);
	git_reference_free(ref_one);
	git_reference_free(ref_one_new);
	git_reference_free(ref_two);
289
	git_refdb_free(refdb);
Ben Straub committed
290 291 292 293 294
}


void test_refs_rename__prefix(void)
{
295
	// can be renamed to a new name prefixed with the old name
296
	git_reference *ref, *ref_two, *looked_up_ref, *renamed_ref;
Ben Straub committed
297 298 299 300 301
	git_oid id;

	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
	cl_assert(git_reference_type(ref) & GIT_REF_OID);

302
	git_oid_cpy(&id, git_reference_target(ref));
Ben Straub committed
303 304

	/* Create loose references */
305
	cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name, &id, 0, NULL));
Ben Straub committed
306 307 308 309 310

	/* An existing reference... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name));

	/* Can be rename to a new name starting with the old name. */
311
	cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, ref_two_name_new, 0, NULL));
Ben Straub committed
312
	git_reference_free(looked_up_ref);
313
	git_reference_free(renamed_ref);
Ben Straub committed
314 315 316

	/* Check we actually renamed it */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new));
Vicent Martí committed
317
	cl_assert_equal_s(looked_up_ref->name, ref_two_name_new);
Ben Straub committed
318 319 320 321 322 323 324 325 326 327
	git_reference_free(looked_up_ref);
	cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name));

	git_reference_free(ref);
	git_reference_free(ref_two);
	git_reference_free(looked_up_ref);
}

void test_refs_rename__move_up(void)
{
328
	// can move a reference to a upper reference hierarchy
329
	git_reference *ref, *ref_two, *looked_up_ref, *renamed_ref;
330
	git_oid id;
Ben Straub committed
331

332 333
	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
	cl_assert(git_reference_type(ref) & GIT_REF_OID);
Ben Straub committed
334

335
	git_oid_cpy(&id, git_reference_target(ref));
Ben Straub committed
336

337
	/* Create loose references */
338
	cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name_new, &id, 0, NULL));
339
	git_reference_free(ref_two);
Ben Straub committed
340

341 342
	/* An existing reference... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new));
Ben Straub committed
343

344
	/* Can be renamed upward the reference tree. */
345
	cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, ref_two_name, 0, NULL));
346
	git_reference_free(looked_up_ref);
347
	git_reference_free(renamed_ref);
Ben Straub committed
348

349 350 351 352
	/* Check we actually renamed it */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name));
	cl_assert_equal_s(looked_up_ref->name, ref_two_name);
	git_reference_free(looked_up_ref);
353

354 355 356
	cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new));
	git_reference_free(ref);
	git_reference_free(looked_up_ref);
Ben Straub committed
357
}
358 359 360

void test_refs_rename__propagate_eexists(void)
{
361
	git_reference *ref, *new_ref;
362 363 364

	cl_git_pass(git_reference_lookup(&ref, g_repo, packed_head_name));

365
	cl_assert_equal_i(GIT_EEXISTS, git_reference_rename(&new_ref, ref, packed_test_head_name, 0, NULL));
366 367 368

	git_reference_free(ref);
}
369 370 371 372 373 374 375 376 377

void test_refs_rename__writes_to_reflog(void)
{
	git_reference *ref, *new_ref;
	git_reflog *log;
	const git_reflog_entry *entry;

	cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
	cl_git_pass(git_reference_rename(&new_ref, ref, ref_one_name_new, false,
378
				"message"));
379 380 381
	cl_git_pass(git_reflog_read(&log, g_repo, git_reference_name(new_ref)));
	entry = git_reflog_entry_byindex(log, 0);
	cl_assert_equal_s("message", git_reflog_entry_message(entry));
382
	cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
383 384 385 386 387

	git_reflog_free(log);
	git_reference_free(ref);
	git_reference_free(new_ref);
}