tag.c 13 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8
 */

#include "tag.h"
9 10

#include "commit.h"
11
#include "signature.h"
12
#include "wildmatch.h"
13 14
#include "git2/object.h"
#include "git2/repository.h"
15
#include "git2/signature.h"
16
#include "git2/odb_backend.h"
17

18
void git_tag__free(void *_tag)
19
{
20
	git_tag *tag = _tag;
21
	git_signature_free(tag->tagger);
22 23 24
	git__free(tag->message);
	git__free(tag->tag_name);
	git__free(tag);
25 26
}

Russell Belfer committed
27
int git_tag_target(git_object **target, const git_tag *t)
28
{
Edward Thomson committed
29
	GIT_ASSERT_ARG(t);
30
	return git_object_lookup(target, t->object.repo, &t->target, t->type);
31 32
}

Russell Belfer committed
33
const git_oid *git_tag_target_id(const git_tag *t)
34
{
Edward Thomson committed
35
	GIT_ASSERT_ARG_WITH_RETVAL(t, NULL);
36 37 38
	return &t->target;
}

39
git_object_t git_tag_target_type(const git_tag *t)
40
{
Edward Thomson committed
41
	GIT_ASSERT_ARG_WITH_RETVAL(t, GIT_OBJECT_INVALID);
42 43 44
	return t->type;
}

Russell Belfer committed
45
const char *git_tag_name(const git_tag *t)
46
{
Edward Thomson committed
47
	GIT_ASSERT_ARG_WITH_RETVAL(t, NULL);
48 49 50
	return t->tag_name;
}

Russell Belfer committed
51
const git_signature *git_tag_tagger(const git_tag *t)
52 53 54 55
{
	return t->tagger;
}

Russell Belfer committed
56
const char *git_tag_message(const git_tag *t)
57
{
Edward Thomson committed
58
	GIT_ASSERT_ARG_WITH_RETVAL(t, NULL);
59 60 61
	return t->message;
}

62 63
static int tag_error(const char *str)
{
64
	git_error_set(GIT_ERROR_TAG, "failed to parse tag: %s", str);
65
	return GIT_EINVALID;
66 67
}

68
static int tag_parse(git_tag *tag, const char *buffer, const char *buffer_end)
69 70 71 72
{
	static const char *tag_types[] = {
		NULL, "commit\n", "tree\n", "blob\n", "tag\n"
	};
73
	size_t text_len, alloc_len;
74 75
	const char *search;
	unsigned int i;
76
	int error;
77

78
	if (git_oid__parse(&tag->target, &buffer, buffer_end, "object ") < 0)
79
		return tag_error("object field invalid");
80 81

	if (buffer + 5 >= buffer_end)
82
		return tag_error("object too short");
83 84

	if (memcmp(buffer, "type ", 5) != 0)
85
		return tag_error("type field not found");
86 87
	buffer += 5;

88
	tag->type = GIT_OBJECT_INVALID;
89 90 91 92 93

	for (i = 1; i < ARRAY_SIZE(tag_types); ++i) {
		size_t type_length = strlen(tag_types[i]);

		if (buffer + type_length >= buffer_end)
94
			return tag_error("object too short");
95 96 97 98 99 100 101 102

		if (memcmp(buffer, tag_types[i], type_length) == 0) {
			tag->type = i;
			buffer += type_length;
			break;
		}
	}

103
	if (tag->type == GIT_OBJECT_INVALID)
104
		return tag_error("invalid object type");
105 106

	if (buffer + 4 >= buffer_end)
107
		return tag_error("object too short");
108 109

	if (memcmp(buffer, "tag ", 4) != 0)
110
		return tag_error("tag field not found");
111

112 113 114 115
	buffer += 4;

	search = memchr(buffer, '\n', buffer_end - buffer);
	if (search == NULL)
116
		return tag_error("object too short");
117 118 119

	text_len = search - buffer;

120
	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, text_len, 1);
121
	tag->tag_name = git__malloc(alloc_len);
122
	GIT_ERROR_CHECK_ALLOC(tag->tag_name);
123

124 125 126 127 128
	memcpy(tag->tag_name, buffer, text_len);
	tag->tag_name[text_len] = '\0';

	buffer = search + 1;

129
	tag->tagger = NULL;
130
	if (buffer < buffer_end && *buffer != '\n') {
131
		tag->tagger = git__malloc(sizeof(git_signature));
132
		GIT_ERROR_CHECK_ALLOC(tag->tagger);
133

134 135
		if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ", '\n')) < 0)
			return error;
136
	}
137

138 139
	tag->message = NULL;
	if (buffer < buffer_end) {
140
		/* If we're not at the end of the header, search for it */
141 142 143
		if(*buffer != '\n') {
			search = git__memmem(buffer, buffer_end - buffer,
					     "\n\n", 2);
144 145 146 147 148
			if (search)
				buffer = search + 1;
			else
				return tag_error("tag contains no message");
		}
149

150
		text_len = buffer_end - ++buffer;
151

152
		GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, text_len, 1);
153
		tag->message = git__malloc(alloc_len);
154
		GIT_ERROR_CHECK_ALLOC(tag->message);
155

156 157 158
		memcpy(tag->message, buffer, text_len);
		tag->message[text_len] = '\0';
	}
159

160
	return 0;
161 162
}

163 164 165 166 167
int git_tag__parse_raw(void *_tag, const char *data, size_t size)
{
	return tag_parse(_tag, data, data + size);
}

168 169 170 171 172 173 174 175 176
int git_tag__parse(void *_tag, git_odb_object *odb_obj)
{
	git_tag *tag = _tag;
	const char *buffer = git_odb_object_data(odb_obj);
	const char *buffer_end = buffer + git_odb_object_size(odb_obj);

	return tag_parse(tag, buffer, buffer_end);
}

177 178
static int retrieve_tag_reference(
	git_reference **tag_reference_out,
179
	git_str *ref_name_out,
180 181
	git_repository *repo,
	const char *tag_name)
182
{
nulltoken committed
183 184 185
	git_reference *tag_ref;
	int error;

186 187
	*tag_reference_out = NULL;

188
	if (git_str_joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name) < 0)
189
		return -1;
190 191

	error = git_reference_lookup(&tag_ref, repo, ref_name_out->ptr);
192
	if (error < 0)
193
		return error; /* Be it not foundo or corrupted */
nulltoken committed
194

195
	*tag_reference_out = tag_ref;
nulltoken committed
196

197 198 199 200 201
	return 0;
}

static int retrieve_tag_reference_oid(
	git_oid *oid,
202
	git_str *ref_name_out,
203 204 205
	git_repository *repo,
	const char *tag_name)
{
206
	if (git_str_joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name) < 0)
207 208
		return -1;

209
	return git_reference_name_to_id(oid, repo, ref_name_out->ptr);
Vicent Marti committed
210
}
211

212 213 214 215 216 217 218 219
static int write_tag_annotation(
		git_oid *oid,
		git_repository *repo,
		const char *tag_name,
		const git_object *target,
		const git_signature *tagger,
		const char *message)
{
220
	git_str tag = GIT_STR_INIT;
221
	git_odb *odb;
222 223

	git_oid__writebuf(&tag, "object ", git_object_id(target));
224 225
	git_str_printf(&tag, "type %s\n", git_object_type2string(git_object_type(target)));
	git_str_printf(&tag, "tag %s\n", tag_name);
226
	git_signature__writebuf(&tag, "tagger ", tagger);
227
	git_str_putc(&tag, '\n');
228

229
	if (git_str_puts(&tag, message) < 0)
230
		goto on_error;
231

232 233
	if (git_repository_odb__weakptr(&odb, repo) < 0)
		goto on_error;
234

235
	if (git_odb_write(oid, odb, tag.ptr, tag.size, GIT_OBJECT_TAG) < 0)
236
		goto on_error;
237

238
	git_str_dispose(&tag);
239
	return 0;
240

241
on_error:
242
	git_str_dispose(&tag);
243
	git_error_set(GIT_ERROR_OBJECT, "failed to create tag annotation");
244
	return -1;
245 246 247
}

static int git_tag_create__internal(
Vicent Marti committed
248 249 250
		git_oid *oid,
		git_repository *repo,
		const char *tag_name,
251
		const git_object *target,
Vicent Marti committed
252
		const git_signature *tagger,
253
		const char *message,
254 255
		int allow_ref_overwrite,
		int create_tag_annotation)
Vicent Marti committed
256
{
257
	git_reference *new_ref = NULL;
258
	git_str ref_name = GIT_STR_INIT;
259

260
	int error;
Vicent Marti committed
261

Edward Thomson committed
262 263 264 265
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(tag_name);
	GIT_ASSERT_ARG(target);
	GIT_ASSERT_ARG(!create_tag_annotation || (tagger && message));
266

267
	if (git_object_owner(target) != repo) {
268
		git_error_set(GIT_ERROR_INVALID, "the given target does not belong to this repository");
269 270
		return -1;
	}
271

272
	error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag_name);
273
	if (error < 0 && error != GIT_ENOTFOUND)
274
		goto cleanup;
275

276
	/** Ensure the tag name doesn't conflict with an already existing
Will Stamper committed
277
	 *	reference unless overwriting has explicitly been requested **/
278
	if (error == 0 && !allow_ref_overwrite) {
279
		git_str_dispose(&ref_name);
280
		git_error_set(GIT_ERROR_TAG, "tag already exists");
281
		return GIT_EEXISTS;
282 283
	}

284
	if (create_tag_annotation) {
285 286
		if (write_tag_annotation(oid, repo, tag_name, target, tagger, message) < 0)
			return -1;
287 288
	} else
		git_oid_cpy(oid, git_object_id(target));
Vicent Marti committed
289

290
	error = git_reference_create(&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL);
291

292
cleanup:
293
	git_reference_free(new_ref);
294
	git_str_dispose(&ref_name);
295
	return error;
296 297
}

298
int git_tag_create(
299 300 301 302 303 304 305
	git_oid *oid,
	git_repository *repo,
	const char *tag_name,
	const git_object *target,
	const git_signature *tagger,
	const char *message,
	int allow_ref_overwrite)
306 307 308 309
{
	return git_tag_create__internal(oid, repo, tag_name, target, tagger, message, allow_ref_overwrite, 1);
}

310 311 312 313 314 315 316 317
int git_tag_annotation_create(
	git_oid *oid,
	git_repository *repo,
	const char *tag_name,
	const git_object *target,
	const git_signature *tagger,
	const char *message)
{
Edward Thomson committed
318 319 320 321 322 323
	GIT_ASSERT_ARG(oid);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(tag_name);
	GIT_ASSERT_ARG(target);
	GIT_ASSERT_ARG(tagger);
	GIT_ASSERT_ARG(message);
324 325 326 327

	return write_tag_annotation(oid, repo, tag_name, target, tagger, message);
}

328
int git_tag_create_lightweight(
329 330 331 332 333
	git_oid *oid,
	git_repository *repo,
	const char *tag_name,
	const git_object *target,
	int allow_ref_overwrite)
334 335 336 337
{
	return git_tag_create__internal(oid, repo, tag_name, target, NULL, NULL, allow_ref_overwrite, 0);
}

338
int git_tag_create_from_buffer(git_oid *oid, git_repository *repo, const char *buffer, int allow_ref_overwrite)
339 340
{
	git_tag tag;
341
	int error;
342
	git_odb *odb;
343
	git_odb_stream *stream;
344
	git_odb_object *target_obj;
345

346
	git_reference *new_ref = NULL;
347
	git_str ref_name = GIT_STR_INIT;
348

Edward Thomson committed
349 350
	GIT_ASSERT_ARG(oid);
	GIT_ASSERT_ARG(buffer);
351

352
	memset(&tag, 0, sizeof(tag));
353

354 355
	if (git_repository_odb__weakptr(&odb, repo) < 0)
		return -1;
356

357
	/* validate the buffer */
358
	if (tag_parse(&tag, buffer, buffer + strlen(buffer)) < 0)
359
		return -1;
360 361

	/* validate the target */
362 363
	if (git_odb_read(&target_obj, odb, &tag.target) < 0)
		goto on_error;
364

Vicent Marti committed
365
	if (tag.type != target_obj->cached.type) {
366
		git_error_set(GIT_ERROR_TAG, "the type for the given target is invalid");
367
		goto on_error;
368
	}
369

370
	error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag.tag_name);
371
	if (error < 0 && error != GIT_ENOTFOUND)
372 373 374 375 376 377 378
		goto on_error;

	/* We don't need these objects after this */
	git_signature_free(tag.tagger);
	git__free(tag.tag_name);
	git__free(tag.message);
	git_odb_object_free(target_obj);
379 380

	/** Ensure the tag name doesn't conflict with an already existing
Dmitriy Olshevskiy committed
381
	 *	reference unless overwriting has explicitly been requested **/
382
	if (error == 0 && !allow_ref_overwrite) {
383
		git_error_set(GIT_ERROR_TAG, "tag already exists");
384
		return GIT_EEXISTS;
385
	}
386

387
	/* write the buffer */
388
	if ((error = git_odb_open_wstream(
389
			&stream, odb, strlen(buffer), GIT_OBJECT_TAG)) < 0)
390
		return error;
391

392 393
	if (!(error = git_odb_stream_write(stream, buffer, strlen(buffer))))
		error = git_odb_stream_finalize_write(oid, stream);
394

395
	git_odb_stream_free(stream);
396

397
	if (error < 0) {
398
		git_str_dispose(&ref_name);
399
		return error;
400
	}
401

402
	error = git_reference_create(
403
		&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL);
404

405
	git_reference_free(new_ref);
406
	git_str_dispose(&ref_name);
407 408

	return error;
409 410 411 412 413 414 415

on_error:
	git_signature_free(tag.tagger);
	git__free(tag.tag_name);
	git__free(tag.message);
	git_odb_object_free(target_obj);
	return -1;
416 417
}

nulltoken committed
418 419 420
int git_tag_delete(git_repository *repo, const char *tag_name)
{
	git_reference *tag_ref;
421
	git_str ref_name = GIT_STR_INIT;
422
	int error;
423 424 425

	error = retrieve_tag_reference(&tag_ref, &ref_name, repo, tag_name);

426
	git_str_dispose(&ref_name);
nulltoken committed
427

428
	if (error < 0)
429
		return error;
nulltoken committed
430

431 432 433
	error = git_reference_delete(tag_ref);

	git_reference_free(tag_ref);
nulltoken committed
434

435
	return error;
436 437
}

438
typedef struct {
Michael Schubert committed
439 440 441 442 443 444 445
	git_repository *repo;
	git_tag_foreach_cb cb;
	void *cb_data;
} tag_cb_data;

static int tags_cb(const char *ref, void *data)
{
446
	int error;
Michael Schubert committed
447 448 449 450 451 452
	git_oid oid;
	tag_cb_data *d = (tag_cb_data *)data;

	if (git__prefixcmp(ref, GIT_REFS_TAGS_DIR) != 0)
		return 0; /* no tag */

453
	if (!(error = git_reference_name_to_id(&oid, d->repo, ref))) {
454
		if ((error = d->cb(ref, &oid, d->cb_data)) != 0)
455
			git_error_set_after_callback_function(error, "git_tag_foreach");
456
	}
Michael Schubert committed
457

458
	return error;
Michael Schubert committed
459 460 461 462 463 464
}

int git_tag_foreach(git_repository *repo, git_tag_foreach_cb cb, void *cb_data)
{
	tag_cb_data data;

Edward Thomson committed
465 466
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(cb);
Michael Schubert committed
467 468 469 470

	data.cb = cb;
	data.cb_data = cb_data;
	data.repo = repo;
471

472
	return git_reference_foreach_name(repo, &tags_cb, &data);
Michael Schubert committed
473 474 475 476 477
}

typedef struct {
	git_vector *taglist;
	const char *pattern;
478 479
} tag_filter_data;

480
#define GIT_REFS_TAGS_DIR_LEN strlen(GIT_REFS_TAGS_DIR)
481

Michael Schubert committed
482
static int tag_list_cb(const char *tag_name, git_oid *oid, void *data)
Vicent Marti committed
483
{
Michael Schubert committed
484 485
	tag_filter_data *filter = (tag_filter_data *)data;
	GIT_UNUSED(oid);
486

487
	if (!*filter->pattern ||
488
	    wildmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
489 490
	{
		char *matched = git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN);
491
		GIT_ERROR_CHECK_ALLOC(matched);
492

493 494
		return git_vector_insert(filter->taglist, matched);
	}
Vicent Marti committed
495

496
	return 0;
Vicent Marti committed
497 498
}

499
int git_tag_list_match(git_strarray *tag_names, const char *pattern, git_repository *repo)
Vicent Marti committed
500 501
{
	int error;
502
	tag_filter_data filter;
Vicent Marti committed
503 504
	git_vector taglist;

Edward Thomson committed
505 506 507
	GIT_ASSERT_ARG(tag_names);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(pattern);
508

509 510
	if ((error = git_vector_init(&taglist, 8, NULL)) < 0)
		return error;
Vicent Marti committed
511

512 513 514
	filter.taglist = &taglist;
	filter.pattern = pattern;

Michael Schubert committed
515
	error = git_tag_foreach(repo, &tag_list_cb, (void *)&filter);
516

517
	if (error < 0)
Vicent Marti committed
518 519
		git_vector_free(&taglist);

520 521 522
	tag_names->strings =
		(char **)git_vector_detach(&tag_names->count, NULL, &taglist);

523
	return 0;
Vicent Marti committed
524
}
525 526 527 528

int git_tag_list(git_strarray *tag_names, git_repository *repo)
{
	return git_tag_list_match(tag_names, "", repo);
529
}
530

Russell Belfer committed
531
int git_tag_peel(git_object **tag_target, const git_tag *tag)
532
{
533
	return git_object_peel(tag_target, (const git_object *)tag, GIT_OBJECT_ANY);
534
}
535

536 537
int git_tag_name_is_valid(int *valid, const char *name)
{
538
	git_str ref_name = GIT_STR_INIT;
539 540 541 542
	int error = 0;

	GIT_ASSERT(valid);

543 544
	*valid = 0;

545 546 547 548 549 550 551
	/*
	 * Discourage tag name starting with dash,
	 * https://github.com/git/git/commit/4f0accd638b8d2
	 */
	if (!name || name[0] == '-')
		goto done;

552 553
	if ((error = git_str_puts(&ref_name, GIT_REFS_TAGS_DIR)) < 0 ||
	    (error = git_str_puts(&ref_name, name)) < 0)
554 555 556 557 558
		goto done;

	error = git_reference_name_is_valid(valid, ref_name.ptr);

done:
559
	git_str_dispose(&ref_name);
560 561 562
	return error;
}

563 564
/* Deprecated Functions */

565
#ifndef GIT_DEPRECATE_HARD
566 567 568 569
int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *buffer, int allow_ref_overwrite)
{
	return git_tag_create_from_buffer(oid, repo, buffer, allow_ref_overwrite);
}
570
#endif