write.c 7.73 KB
Newer Older
Ben Straub committed
1 2 3 4 5 6 7 8 9 10 11
#include "clar_libgit2.h"

static const char* tagger_name = "Vicent Marti";
static const char* tagger_email = "vicent@github.com";
static const char* tagger_message = "This is my tag.\n\nThere are many tags, but this one is mine\n";

static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";

static git_repository *g_repo;

12
/* Fixture setup and teardown */
13
void test_object_tag_write__initialize(void)
Ben Straub committed
14 15 16 17
{
   g_repo = cl_git_sandbox_init("testrepo");
}

18
void test_object_tag_write__cleanup(void)
Ben Straub committed
19 20 21 22
{
   cl_git_sandbox_cleanup();
}

23
void test_object_tag_write__basic(void)
Ben Straub committed
24
{
25
	/* write a tag to the repository and read it again */
Ben Straub committed
26 27 28 29 30 31 32 33
	git_tag *tag;
	git_oid target_id, tag_id;
	git_signature *tagger;
	const git_signature *tagger1;
	git_reference *ref_tag;
	git_object *target;

	git_oid_fromstr(&target_id, tagged_commit);
34
	cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
Ben Straub committed
35 36 37 38

	/* create signature */
	cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));

39 40 41 42
	cl_git_pass(
		git_tag_create(&tag_id, g_repo,
		  "the-tag", target, tagger, tagger_message, 0)
	);
Ben Straub committed
43 44 45 46 47

	git_object_free(target);
	git_signature_free(tagger);

	cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id));
Russell Belfer committed
48
	cl_assert(git_oid_cmp(git_tag_target_id(tag), &target_id) == 0);
Ben Straub committed
49 50 51 52

	/* Check attributes were set correctly */
	tagger1 = git_tag_tagger(tag);
	cl_assert(tagger1 != NULL);
Vicent Martí committed
53 54
	cl_assert_equal_s(tagger1->name, tagger_name);
	cl_assert_equal_s(tagger1->email, tagger_email);
Ben Straub committed
55 56 57
	cl_assert(tagger1->when.time == 123456789);
	cl_assert(tagger1->when.offset == 60);

Vicent Martí committed
58
	cl_assert_equal_s(git_tag_message(tag), tagger_message);
Ben Straub committed
59 60

	cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/the-tag"));
61
	cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0);
Ben Straub committed
62
	cl_git_pass(git_reference_delete(ref_tag));
63
	git_reference_free(ref_tag);
Ben Straub committed
64 65 66 67

	git_tag_free(tag);
}

68
void test_object_tag_write__overwrite(void)
Ben Straub committed
69
{
70
	/* Attempt to write a tag bearing the same name than an already existing tag */
Ben Straub committed
71 72 73 74 75
	git_oid target_id, tag_id;
	git_signature *tagger;
	git_object *target;

	git_oid_fromstr(&target_id, tagged_commit);
76
	cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
Ben Straub committed
77 78 79 80

	/* create signature */
	cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));

81
	cl_assert_equal_i(GIT_EEXISTS, git_tag_create(
Ben Straub committed
82 83 84 85 86 87 88 89 90 91 92 93
                              &tag_id, /* out id */
                              g_repo,
                              "e90810b",
                              target,
                              tagger,
                              tagger_message,
                              0));

	git_object_free(target);
	git_signature_free(tagger);
}

94
void test_object_tag_write__replace(void)
Ben Straub committed
95
{
96
	/* Replace an already existing tag */
Ben Straub committed
97 98 99 100 101 102
	git_oid target_id, tag_id, old_tag_id;
	git_signature *tagger;
	git_reference *ref_tag;
	git_object *target;

	git_oid_fromstr(&target_id, tagged_commit);
103
	cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
Ben Straub committed
104 105

	cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
106
	git_oid_cpy(&old_tag_id, git_reference_target(ref_tag));
Ben Straub committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	git_reference_free(ref_tag);

	/* create signature */
	cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));

	cl_git_pass(git_tag_create(
                              &tag_id, /* out id */
                              g_repo,
                              "e90810b",
                              target,
                              tagger,
                              tagger_message,
                              1));

	git_object_free(target);
	git_signature_free(tagger);

	cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
125 126
	cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0);
	cl_assert(git_oid_cmp(git_reference_target(ref_tag), &old_tag_id) != 0);
Ben Straub committed
127 128 129 130

	git_reference_free(ref_tag);
}

131
void test_object_tag_write__lightweight(void)
Ben Straub committed
132
{
133
	/* write a lightweight tag to the repository and read it again */
Ben Straub committed
134 135 136 137 138
	git_oid target_id, object_id;
	git_reference *ref_tag;
	git_object *target;

	git_oid_fromstr(&target_id, tagged_commit);
139
	cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
Ben Straub committed
140 141 142 143 144 145 146 147 148 149 150 151 152

	cl_git_pass(git_tag_create_lightweight(
                                          &object_id,
                                          g_repo,
                                          "light-tag",
                                          target,
                                          0));

	git_object_free(target);

	cl_assert(git_oid_cmp(&object_id, &target_id) == 0);

	cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/light-tag"));
153
	cl_assert(git_oid_cmp(git_reference_target(ref_tag), &target_id) == 0);
Ben Straub committed
154 155 156 157 158 159

	cl_git_pass(git_tag_delete(g_repo, "light-tag"));

	git_reference_free(ref_tag);
}

160
void test_object_tag_write__lightweight_over_existing(void)
Ben Straub committed
161
{
162
	/* Attempt to write a lightweight tag bearing the same name than an already existing tag */
Ben Straub committed
163 164 165 166
	git_oid target_id, object_id, existing_object_id;
	git_object *target;

	git_oid_fromstr(&target_id, tagged_commit);
167
	cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
Ben Straub committed
168

169
	cl_assert_equal_i(GIT_EEXISTS, git_tag_create_lightweight(
Ben Straub committed
170 171 172 173 174 175 176 177 178 179 180 181
                                          &object_id,
                                          g_repo,
                                          "e90810b",
                                          target,
                                          0));

	git_oid_fromstr(&existing_object_id, tag2_id);
	cl_assert(git_oid_cmp(&object_id, &existing_object_id) == 0);

	git_object_free(target);
}

182
void test_object_tag_write__delete(void)
Ben Straub committed
183
{
184
	/* Delete an already existing tag */
Ben Straub committed
185 186 187 188 189 190 191 192
	git_reference *ref_tag;

	cl_git_pass(git_tag_delete(g_repo, "e90810b"));

	cl_git_fail(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));

	git_reference_free(ref_tag);
}
193 194 195 196 197 198 199 200

void test_object_tag_write__creating_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
	git_oid target_id, tag_id;
	git_signature *tagger;
	git_object *target;

	git_oid_fromstr(&target_id, tagged_commit);
201
	cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

	cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));

	cl_assert_equal_i(GIT_EINVALIDSPEC,
		git_tag_create(&tag_id, g_repo,
		  "Inv@{id", target, tagger, tagger_message, 0)
	);

	cl_assert_equal_i(GIT_EINVALIDSPEC,
		git_tag_create_lightweight(&tag_id, g_repo,
		  "Inv@{id", target, 0)
	);

	git_object_free(target);
	git_signature_free(tagger);
}

void test_object_tag_write__deleting_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
	cl_assert_equal_i(GIT_EINVALIDSPEC, git_tag_delete(g_repo, "Inv@{id"));
}
223 224 225 226 227 228 229 230 231 232

void create_annotation(git_oid *tag_id, const char *name)
{
	git_object *target;
	git_oid target_id;
	git_signature *tagger;

	cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));

	git_oid_fromstr(&target_id, tagged_commit);
233
	cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
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

	cl_git_pass(git_tag_annotation_create(tag_id, g_repo, name, target, tagger, "boom!"));
	git_object_free(target);
	git_signature_free(tagger);
}

void test_object_tag_write__creating_an_annotation_stores_the_new_object_in_the_odb(void)
{
	git_oid tag_id;
	git_tag *tag;

	create_annotation(&tag_id, "new_tag");

	cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id));
	cl_assert_equal_s("new_tag", git_tag_name(tag));

	git_tag_free(tag);
}

void test_object_tag_write__creating_an_annotation_does_not_create_a_reference(void)
{
	git_oid tag_id;
	git_reference *tag_ref;

	create_annotation(&tag_id, "new_tag");
	cl_git_fail_with(git_reference_lookup(&tag_ref, g_repo, "refs/tags/new_tag"), GIT_ENOTFOUND);
}