tag.c 11.2 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 9 10
 */

#include "common.h"
#include "commit.h"
#include "tag.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 64 65 66 67
static int tag_error(const char *str)
{
	giterr_set(GITERR_TAG, "Failed to parse tag. %s", str);
	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 75
	unsigned int i;
	size_t text_len;
76 77
	char *search;

78 79
	if (git_oid__parse(&tag->target, &buffer, buffer_end, "object ") < 0)
		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 120

	text_len = search - buffer;

	tag->tag_name = git__malloc(text_len + 1);
121
	GITERR_CHECK_ALLOC(tag->tag_name);
122

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

	buffer = search + 1;

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

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

137 138 139 140
	tag->message = NULL;
	if (buffer < buffer_end) {
		if( *buffer != '\n' )
			return tag_error("No new line before message");
141

142
		text_len = buffer_end - ++buffer;
143

144 145
		tag->message = git__malloc(text_len + 1);
		GITERR_CHECK_ALLOC(tag->message);
146

147 148 149
		memcpy(tag->message, buffer, text_len);
		tag->message[text_len] = '\0';
	}
150

151
	return 0;
152 153
}

154 155 156 157 158 159 160 161 162
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);
}

163 164 165 166 167
static int retrieve_tag_reference(
	git_reference **tag_reference_out,
	git_buf *ref_name_out,
	git_repository *repo,
	const char *tag_name)
168
{
nulltoken committed
169 170 171
	git_reference *tag_ref;
	int error;

172 173
	*tag_reference_out = NULL;

174 175
	if (git_buf_joinpath(ref_name_out, GIT_REFS_TAGS_DIR, tag_name) < 0)
		return -1;
176 177

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

181
	*tag_reference_out = tag_ref;
nulltoken committed
182

183 184 185 186 187 188 189 190 191 192 193 194
	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;

195
	return git_reference_name_to_id(oid, repo, ref_name_out->ptr);
Vicent Marti committed
196
}
197

198 199 200 201 202 203 204 205
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)
{
206
	git_buf tag = GIT_BUF_INIT;
207
	git_odb *odb;
208 209 210 211 212 213 214

	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');

215
	if (git_buf_puts(&tag, message) < 0)
216
		goto on_error;
217

218 219
	if (git_repository_odb__weakptr(&odb, repo) < 0)
		goto on_error;
220

221 222
	if (git_odb_write(oid, odb, tag.ptr, tag.size, GIT_OBJ_TAG) < 0)
		goto on_error;
223

224 225
	git_buf_free(&tag);
	return 0;
226

227 228
on_error:
	git_buf_free(&tag);
229
	giterr_set(GITERR_OBJECT, "Failed to create tag annotation.");
230
	return -1;
231 232 233
}

static int git_tag_create__internal(
Vicent Marti committed
234 235 236
		git_oid *oid,
		git_repository *repo,
		const char *tag_name,
237
		const git_object *target,
Vicent Marti committed
238
		const git_signature *tagger,
239
		const char *message,
240 241
		int allow_ref_overwrite,
		int create_tag_annotation)
Vicent Marti committed
242
{
243
	git_reference *new_ref = NULL;
244
	git_buf ref_name = GIT_BUF_INIT;
245

246
	int error;
Vicent Marti committed
247

248 249 250
	assert(repo && tag_name && target);
	assert(!create_tag_annotation || (tagger && message));

251 252 253 254
	if (git_object_owner(target) != repo) {
		giterr_set(GITERR_INVALID, "The given target does not belong to this repository");
		return -1;
	}
255

256
	error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag_name);
257
	if (error < 0 && error != GIT_ENOTFOUND)
258
		goto cleanup;
259

260
	/** Ensure the tag name doesn't conflict with an already existing
Vicent Marti committed
261
	 *	reference unless overwriting has explictly been requested **/
262 263 264
	if (error == 0 && !allow_ref_overwrite) {
		git_buf_free(&ref_name);
		giterr_set(GITERR_TAG, "Tag already exists");
265
		return GIT_EEXISTS;
266 267
	}

268
	if (create_tag_annotation) {
269 270
		if (write_tag_annotation(oid, repo, tag_name, target, tagger, message) < 0)
			return -1;
271 272
	} else
		git_oid_cpy(oid, git_object_id(target));
Vicent Marti committed
273

274
	error = git_reference_create(&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite);
275

276
cleanup:
277
	git_reference_free(new_ref);
278 279
	git_buf_free(&ref_name);
	return error;
280 281
}

282
int git_tag_create(
283 284 285 286 287 288 289
	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)
290 291 292 293
{
	return git_tag_create__internal(oid, repo, tag_name, target, tagger, message, allow_ref_overwrite, 1);
}

294 295 296 297 298 299 300 301 302 303 304 305 306
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);
}

307
int git_tag_create_lightweight(
308 309 310 311 312
	git_oid *oid,
	git_repository *repo,
	const char *tag_name,
	const git_object *target,
	int allow_ref_overwrite)
313 314 315 316
{
	return git_tag_create__internal(oid, repo, tag_name, target, NULL, NULL, allow_ref_overwrite, 0);
}

317
int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *buffer, int allow_ref_overwrite)
318 319
{
	git_tag tag;
320
	int error;
321
	git_odb *odb;
322
	git_odb_stream *stream;
323
	git_odb_object *target_obj;
324

325
	git_reference *new_ref = NULL;
326
	git_buf ref_name = GIT_BUF_INIT;
327

328
	assert(oid && buffer);
329

330
	memset(&tag, 0, sizeof(tag));
331

332 333
	if (git_repository_odb__weakptr(&odb, repo) < 0)
		return -1;
334

335
	/* validate the buffer */
336
	if (tag_parse(&tag, buffer, buffer + strlen(buffer)) < 0)
337
		return -1;
338 339

	/* validate the target */
340 341
	if (git_odb_read(&target_obj, odb, &tag.target) < 0)
		goto on_error;
342

Vicent Marti committed
343
	if (tag.type != target_obj->cached.type) {
344 345
		giterr_set(GITERR_TAG, "The type for the given target is invalid");
		goto on_error;
346
	}
347

348
	error = retrieve_tag_reference_oid(oid, &ref_name, repo, tag.tag_name);
349
	if (error < 0 && error != GIT_ENOTFOUND)
350 351 352 353 354 355 356
		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);
357 358

	/** Ensure the tag name doesn't conflict with an already existing
Vicent Marti committed
359
	 *	reference unless overwriting has explictly been requested **/
360 361
	if (error == 0 && !allow_ref_overwrite) {
		giterr_set(GITERR_TAG, "Tag already exists");
362
		return GIT_EEXISTS;
363
	}
364

365
	/* write the buffer */
366 367
	if (git_odb_open_wstream(&stream, odb, strlen(buffer), GIT_OBJ_TAG) < 0)
		return -1;
368

369
	git_odb_stream_write(stream, buffer, strlen(buffer));
370

371 372
	error = git_odb_stream_finalize_write(oid, stream);
	git_odb_stream_free(stream);
373

374 375 376 377
	if (error < 0) {
		git_buf_free(&ref_name);
		return -1;
	}
378

379
	error = git_reference_create(&new_ref, repo, ref_name.ptr, oid, allow_ref_overwrite);
380

381
	git_reference_free(new_ref);
382 383 384
	git_buf_free(&ref_name);

	return error;
385 386 387 388 389 390 391

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

nulltoken committed
394 395 396
int git_tag_delete(git_repository *repo, const char *tag_name)
{
	git_reference *tag_ref;
397
	git_buf ref_name = GIT_BUF_INIT;
398
	int error;
399 400 401 402

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

	git_buf_free(&ref_name);
nulltoken committed
403

404
	if (error < 0)
405
		return error;
nulltoken committed
406

407 408
	if ((error = git_reference_delete(tag_ref)) == 0)
		git_reference_free(tag_ref);
nulltoken committed
409

410
	return error;
411 412
}

413
typedef struct {
Michael Schubert committed
414 415 416 417 418 419 420 421 422 423 424 425 426
	git_repository *repo;
	git_tag_foreach_cb cb;
	void *cb_data;
} tag_cb_data;

static int tags_cb(const char *ref, void *data)
{
	git_oid oid;
	tag_cb_data *d = (tag_cb_data *)data;

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

427
	if (git_reference_name_to_id(&oid, d->repo, ref) < 0)
Michael Schubert committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
		return -1;

	return d->cb(ref, &oid, d->cb_data);
}

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;

443
	return git_reference_foreach_name(repo, &tags_cb, &data);
Michael Schubert committed
444 445 446 447 448
}

typedef struct {
	git_vector *taglist;
	const char *pattern;
449 450
} tag_filter_data;

451
#define GIT_REFS_TAGS_DIR_LEN strlen(GIT_REFS_TAGS_DIR)
452

Michael Schubert committed
453
static int tag_list_cb(const char *tag_name, git_oid *oid, void *data)
Vicent Marti committed
454
{
Michael Schubert committed
455 456
	tag_filter_data *filter = (tag_filter_data *)data;
	GIT_UNUSED(oid);
457

458
	if (!*filter->pattern || p_fnmatch(filter->pattern, tag_name + GIT_REFS_TAGS_DIR_LEN, 0) == 0)
459
		return git_vector_insert(filter->taglist, git__strdup(tag_name + GIT_REFS_TAGS_DIR_LEN));
Vicent Marti committed
460

461
	return 0;
Vicent Marti committed
462 463
}

464
int git_tag_list_match(git_strarray *tag_names, const char *pattern, git_repository *repo)
Vicent Marti committed
465 466
{
	int error;
467
	tag_filter_data filter;
Vicent Marti committed
468 469
	git_vector taglist;

470 471
	assert(tag_names && repo && pattern);

472
	if (git_vector_init(&taglist, 8, NULL) < 0)
473
		return -1;
Vicent Marti committed
474

475 476 477
	filter.taglist = &taglist;
	filter.pattern = pattern;

Michael Schubert committed
478
	error = git_tag_foreach(repo, &tag_list_cb, (void *)&filter);
479
	if (error < 0) {
Vicent Marti committed
480
		git_vector_free(&taglist);
481
		return -1;
Vicent Marti committed
482 483 484 485
	}

	tag_names->strings = (char **)taglist.contents;
	tag_names->count = taglist.length;
486
	return 0;
Vicent Marti committed
487
}
488 489 490 491

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

Russell Belfer committed
494
int git_tag_peel(git_object **tag_target, const git_tag *tag)
495
{
Russell Belfer committed
496
	return git_object_peel(tag_target, (const git_object *)tag, GIT_OBJ_ANY);
497
}