tag.c 11.6 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 "message.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
{
29
	assert(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
{
35 36 37 38
	assert(t);
	return &t->target;
}

Russell Belfer committed
39
git_otype git_tag_target_type(const git_tag *t)
40
{
41
	assert(t);
42 43 44
	return t->type;
}

Russell Belfer committed
45
const char *git_tag_name(const git_tag *t)
46
{
47
	assert(t);
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
{
58
	assert(t);
59 60 61
	return t->message;
}

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

68
static int tag_parse(git_tag *tag, const char *buffer, const char *buffer_end)
69 70 71 72 73
{
	static const char *tag_types[] = {
		NULL, "commit\n", "tree\n", "blob\n", "tag\n"
	};

74
	unsigned int i;
75
	size_t text_len, alloc_len;
76 77
	char *search;

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 88 89 90 91 92 93
	buffer += 5;

	tag->type = GIT_OBJ_BAD;

	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 103

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

	if (tag->type == GIT_OBJ_BAD)
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 121
	GITERR_CHECK_ALLOC_ADD(&alloc_len, text_len, 1);
	tag->tag_name = git__malloc(alloc_len);
122
	GITERR_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
		GITERR_CHECK_ALLOC(tag->tagger);
133

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

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

149
		text_len = buffer_end - ++buffer;
150

151 152
		GITERR_CHECK_ALLOC_ADD(&alloc_len, text_len, 1);
		tag->message = git__malloc(alloc_len);
153
		GITERR_CHECK_ALLOC(tag->message);
154

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

159
	return 0;
160 161
}

162 163 164 165 166 167 168 169 170
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);
}

171 172 173 174 175
static int retrieve_tag_reference(
	git_reference **tag_reference_out,
	git_buf *ref_name_out,
	git_repository *repo,
	const char *tag_name)
176
{
nulltoken committed
177 178 179
	git_reference *tag_ref;
	int error;

180 181
	*tag_reference_out = NULL;

182 183
	if (git_buf_joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name) < 0)
		return -1;
184 185

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

189
	*tag_reference_out = tag_ref;
nulltoken committed
190

191 192 193 194 195 196 197 198 199 200 201 202
	return 0;
}

static int retrieve_tag_reference_oid(
	git_oid *oid,
	git_buf *ref_name_out,
	git_repository *repo,
	const char *tag_name)
{
	if (git_buf_joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name) < 0)
		return -1;

203
	return git_reference_name_to_id(oid, repo, ref_name_out->ptr);
Vicent Marti committed
204
}
205

206 207 208 209 210 211 212 213
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)
{
214
	git_buf tag = GIT_BUF_INIT;
215
	git_odb *odb;
216 217 218 219 220 221 222

	git_oid__writebuf(&tag, "object ", git_object_id(target));
	git_buf_printf(&tag, "type %s\n", git_object_type2string(git_object_type(target)));
	git_buf_printf(&tag, "tag %s\n", tag_name);
	git_signature__writebuf(&tag, "tagger ", tagger);
	git_buf_putc(&tag, '\n');

223
	if (git_buf_puts(&tag, message) < 0)
224
		goto on_error;
225

226 227
	if (git_repository_odb__weakptr(&odb, repo) < 0)
		goto on_error;
228

229 230
	if (git_odb_write(oid, odb, tag.ptr, tag.size, GIT_OBJ_TAG) < 0)
		goto on_error;
231

232
	git_buf_dispose(&tag);
233
	return 0;
234

235
on_error:
236
	git_buf_dispose(&tag);
237
	giterr_set(GITERR_OBJECT, "failed to create tag annotation");
238
	return -1;
239 240 241
}

static int git_tag_create__internal(
Vicent Marti committed
242 243 244
		git_oid *oid,
		git_repository *repo,
		const char *tag_name,
245
		const git_object *target,
Vicent Marti committed
246
		const git_signature *tagger,
247
		const char *message,
248 249
		int allow_ref_overwrite,
		int create_tag_annotation)
Vicent Marti committed
250
{
251
	git_reference *new_ref = NULL;
252
	git_buf ref_name = GIT_BUF_INIT;
253

254
	int error;
Vicent Marti committed
255

256 257 258
	assert(repo && tag_name && target);
	assert(!create_tag_annotation || (tagger && message));

259
	if (git_object_owner(target) != repo) {
260
		giterr_set(GITERR_INVALID, "the given target does not belong to this repository");
261 262
		return -1;
	}
263

264
	error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag_name);
265
	if (error < 0 && error != GIT_ENOTFOUND)
266
		goto cleanup;
267

268
	/** Ensure the tag name doesn't conflict with an already existing
Will Stamper committed
269
	 *	reference unless overwriting has explicitly been requested **/
270
	if (error == 0 && !allow_ref_overwrite) {
271
		git_buf_dispose(&ref_name);
272
		giterr_set(GITERR_TAG, "tag already exists");
273
		return GIT_EEXISTS;
274 275
	}

276
	if (create_tag_annotation) {
277 278
		if (write_tag_annotation(oid, repo, tag_name, target, tagger, message) < 0)
			return -1;
279 280
	} else
		git_oid_cpy(oid, git_object_id(target));
Vicent Marti committed
281

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

284
cleanup:
285
	git_reference_free(new_ref);
286
	git_buf_dispose(&ref_name);
287
	return error;
288 289
}

290
int git_tag_create(
291 292 293 294 295 296 297
	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)
298 299 300 301
{
	return git_tag_create__internal(oid, repo, tag_name, target, tagger, message, allow_ref_overwrite, 1);
}

302 303 304 305 306 307 308 309 310 311 312 313 314
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)
{
	assert(oid && repo && tag_name && target && tagger && message);

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

315
int git_tag_create_lightweight(
316 317 318 319 320
	git_oid *oid,
	git_repository *repo,
	const char *tag_name,
	const git_object *target,
	int allow_ref_overwrite)
321 322 323 324
{
	return git_tag_create__internal(oid, repo, tag_name, target, NULL, NULL, allow_ref_overwrite, 0);
}

325
int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *buffer, int allow_ref_overwrite)
326 327
{
	git_tag tag;
328
	int error;
329
	git_odb *odb;
330
	git_odb_stream *stream;
331
	git_odb_object *target_obj;
332

333
	git_reference *new_ref = NULL;
334
	git_buf ref_name = GIT_BUF_INIT;
335

336
	assert(oid && buffer);
337

338
	memset(&tag, 0, sizeof(tag));
339

340 341
	if (git_repository_odb__weakptr(&odb, repo) < 0)
		return -1;
342

343
	/* validate the buffer */
344
	if (tag_parse(&tag, buffer, buffer + strlen(buffer)) < 0)
345
		return -1;
346 347

	/* validate the target */
348 349
	if (git_odb_read(&target_obj, odb, &tag.target) < 0)
		goto on_error;
350

Vicent Marti committed
351
	if (tag.type != target_obj->cached.type) {
352
		giterr_set(GITERR_TAG, "the type for the given target is invalid");
353
		goto on_error;
354
	}
355

356
	error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag.tag_name);
357
	if (error < 0 && error != GIT_ENOTFOUND)
358 359 360 361 362 363 364
		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);
365 366

	/** Ensure the tag name doesn't conflict with an already existing
Dmitriy Olshevskiy committed
367
	 *	reference unless overwriting has explicitly been requested **/
368
	if (error == 0 && !allow_ref_overwrite) {
369
		giterr_set(GITERR_TAG, "tag already exists");
370
		return GIT_EEXISTS;
371
	}
372

373
	/* write the buffer */
374 375 376
	if ((error = git_odb_open_wstream(
			&stream, odb, strlen(buffer), GIT_OBJ_TAG)) < 0)
		return error;
377

378 379
	if (!(error = git_odb_stream_write(stream, buffer, strlen(buffer))))
		error = git_odb_stream_finalize_write(oid, stream);
380

381
	git_odb_stream_free(stream);
382

383
	if (error < 0) {
384
		git_buf_dispose(&ref_name);
385
		return error;
386
	}
387

388
	error = git_reference_create(
389
		&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite, NULL);
390

391
	git_reference_free(new_ref);
392
	git_buf_dispose(&ref_name);
393 394

	return error;
395 396 397 398 399 400 401

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

nulltoken committed
404 405 406
int git_tag_delete(git_repository *repo, const char *tag_name)
{
	git_reference *tag_ref;
407
	git_buf ref_name = GIT_BUF_INIT;
408
	int error;
409 410 411

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

412
	git_buf_dispose(&ref_name);
nulltoken committed
413

414
	if (error < 0)
415
		return error;
nulltoken committed
416

417 418 419
	error = git_reference_delete(tag_ref);

	git_reference_free(tag_ref);
nulltoken committed
420

421
	return error;
422 423
}

424
typedef struct {
Michael Schubert committed
425 426 427 428 429 430 431
	git_repository *repo;
	git_tag_foreach_cb cb;
	void *cb_data;
} tag_cb_data;

static int tags_cb(const char *ref, void *data)
{
432
	int error;
Michael Schubert committed
433 434 435 436 437 438
	git_oid oid;
	tag_cb_data *d = (tag_cb_data *)data;

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

439
	if (!(error = git_reference_name_to_id(&oid, d->repo, ref))) {
440 441
		if ((error = d->cb(ref, &oid, d->cb_data)) != 0)
			giterr_set_after_callback_function(error, "git_tag_foreach");
442
	}
Michael Schubert committed
443

444
	return error;
Michael Schubert committed
445 446 447 448 449 450 451 452 453 454 455
}

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

	assert(repo && cb);

	data.cb = cb;
	data.cb_data = cb_data;
	data.repo = repo;
456

457
	return git_reference_foreach_name(repo, &tags_cb, &data);
Michael Schubert committed
458 459 460 461 462
}

typedef struct {
	git_vector *taglist;
	const char *pattern;
463 464
} tag_filter_data;

465
#define GIT_REFS_TAGS_DIR_LEN strlen(GIT_REFS_TAGS_DIR)
466

Michael Schubert committed
467
static int tag_list_cb(const char *tag_name, git_oid *oid, void *data)
Vicent Marti committed
468
{
Michael Schubert committed
469 470
	tag_filter_data *filter = (tag_filter_data *)data;
	GIT_UNUSED(oid);
471

472 473 474 475
	if (!*filter->pattern ||
		p_fnmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
	{
		char *matched = git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN);
476 477
		GITERR_CHECK_ALLOC(matched);

478 479
		return git_vector_insert(filter->taglist, matched);
	}
Vicent Marti committed
480

481
	return 0;
Vicent Marti committed
482 483
}

484
int git_tag_list_match(git_strarray *tag_names, const char *pattern, git_repository *repo)
Vicent Marti committed
485 486
{
	int error;
487
	tag_filter_data filter;
Vicent Marti committed
488 489
	git_vector taglist;

490 491
	assert(tag_names && repo && pattern);

492 493
	if ((error = git_vector_init(&taglist, 8, NULL)) < 0)
		return error;
Vicent Marti committed
494

495 496 497
	filter.taglist = &taglist;
	filter.pattern = pattern;

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

500
	if (error < 0)
Vicent Marti committed
501 502
		git_vector_free(&taglist);

503 504 505
	tag_names->strings =
		(char **)git_vector_detach(&tag_names->count, NULL, &taglist);

506
	return 0;
Vicent Marti committed
507
}
508 509 510 511

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

Russell Belfer committed
514
int git_tag_peel(git_object **tag_target, const git_tag *tag)
515
{
Russell Belfer committed
516
	return git_object_peel(tag_target, (const git_object *)tag, GIT_OBJ_ANY);
517
}