delete.c 2.93 KB
Newer Older
Ben Straub committed
1 2
#include "clar_libgit2.h"

3
#include "fileops.h"
Ben Straub committed
4
#include "git2/reflog.h"
5
#include "git2/refdb.h"
Ben Straub committed
6
#include "reflog.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 24 25 26 27 28 29 30 31 32 33 34

static const char *packed_test_head_name = "refs/heads/packed-test";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";

static git_repository *g_repo;



void test_refs_delete__initialize(void)
{
   g_repo = cl_git_sandbox_init("testrepo");
}

void test_refs_delete__cleanup(void)
{
   cl_git_sandbox_cleanup();
}



void test_refs_delete__packed_loose(void)
{
   // deleting a ref which is both packed and loose should remove both tracks in the filesystem
	git_reference *looked_up_ref, *another_looked_up_ref;
	git_buf temp_path = GIT_BUF_INIT;

	/* Ensure the loose reference exists on the file system */
35
	cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), packed_test_head_name));
Vicent Martí committed
36
	cl_assert(git_path_exists(temp_path.ptr));
Ben Straub committed
37 38 39 40 41

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

	/* Ensure it's the loose version that has been found */
42
	cl_assert(reference_is_packed(looked_up_ref) == 0);
Ben Straub committed
43 44 45

	/* Now that the reference is deleted... */
	cl_git_pass(git_reference_delete(looked_up_ref));
46
	git_reference_free(looked_up_ref);
Ben Straub committed
47 48 49 50 51

	/* Looking up the reference once again should not retrieve it */
	cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));

	/* Ensure the loose reference doesn't exist any longer on the file system */
Vicent Martí committed
52
	cl_assert(!git_path_exists(temp_path.ptr));
Ben Straub committed
53 54 55 56 57 58 59 60 61

	git_reference_free(another_looked_up_ref);
	git_buf_free(&temp_path);
}

void test_refs_delete__packed_only(void)
{
   // can delete a just packed reference
	git_reference *ref;
62
	git_refdb *refdb;
Ben Straub committed
63 64 65 66 67 68
	git_oid id;
	const char *new_ref = "refs/heads/new_ref";

	git_oid_fromstr(&id, current_master_tip);

	/* Create and write the new object id reference */
69
	cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &id, 0, NULL));
Ben Straub committed
70 71 72 73 74 75
	git_reference_free(ref);

	/* Lookup the reference */
	cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));

	/* Ensure it's a loose reference */
76
	cl_assert(reference_is_packed(ref) == 0);
Ben Straub committed
77 78

	/* Pack all existing references */
79 80
	cl_git_pass(git_repository_refdb(&refdb, g_repo));
	cl_git_pass(git_refdb_compress(refdb));
Ben Straub committed
81 82

	/* Reload the reference from disk */
83 84
	git_reference_free(ref);
	cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
Ben Straub committed
85 86

	/* Ensure it's a packed reference */
87
	cl_assert(reference_is_packed(ref) == 1);
Ben Straub committed
88 89 90

	/* This should pass */
	cl_git_pass(git_reference_delete(ref));
91
	git_reference_free(ref);
92
	git_refdb_free(refdb);
Ben Straub committed
93
}
94 95 96 97 98 99 100 101 102 103 104 105 106 107

void test_refs_delete__remove(void)
{
	git_reference *ref;

	/* Check that passing no old values lets us delete */

	cl_git_pass(git_reference_lookup(&ref, g_repo, packed_test_head_name));
	git_reference_free(ref);

	cl_git_pass(git_reference_remove(g_repo, packed_test_head_name));

	cl_git_fail(git_reference_lookup(&ref, g_repo, packed_test_head_name));
}