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

#include "common.h"

10
#include "git2/describe.h"
11 12 13
#include "git2/strarray.h"
#include "git2/diff.h"
#include "git2/status.h"
14 15 16 17 18

#include "commit.h"
#include "commit_list.h"
#include "oidmap.h"
#include "refs.h"
19
#include "repository.h"
20 21 22
#include "revwalk.h"
#include "tag.h"
#include "vector.h"
23
#include "wildmatch.h"
24 25 26 27 28 29 30 31 32 33 34

/* Ported from https://github.com/git/git/blob/89dde7882f71f846ccd0359756d27bebc31108de/builtin/describe.c */

struct commit_name {
	git_tag *tag;
	unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
	unsigned name_checked:1;
	git_oid sha1;
	char *path;

	/* Khash workaround. They original key has to still be reachable */
35
	git_oid peeled;
36 37 38 39
};

static void *oidmap_value_bykey(git_oidmap *map, const git_oid *key)
{
40
	return git_oidmap_get(map, key);
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

static struct commit_name *find_commit_name(
	git_oidmap *names,
	const git_oid *peeled)
{
	return (struct commit_name *)(oidmap_value_bykey(names, peeled));
}

static int replace_name(
	git_tag **tag,
	git_repository *repo,
	struct commit_name *e,
	unsigned int prio,
	const git_oid *sha1)
{
	git_time_t e_time = 0, t_time = 0;

	if (!e || e->prio < prio)
		return 1;

	if (e->prio == 2 && prio == 2) {
		/* Multiple annotated tags point to the same commit.
		 * Select one to keep based upon their tagger date.
		 */
		git_tag *t = NULL;

		if (!e->tag) {
			if (git_tag_lookup(&t, repo, &e->sha1) < 0)
				return 1;
			e->tag = t;
		}

		if (git_tag_lookup(&t, repo, sha1) < 0)
			return 0;

		*tag = t;

		if (e->tag->tagger)
			e_time = e->tag->tagger->when.time;

		if (t->tagger)
			t_time = t->tagger->when.time;

		if (e_time < t_time)
			return 1;
	}

	return 0;
}

static int add_to_known_names(
	git_repository *repo,
	git_oidmap *names,
	const char *path,
	const git_oid *peeled,
	unsigned int prio,
	const git_oid *sha1)
{
	struct commit_name *e = find_commit_name(names, peeled);
	bool found = (e != NULL);

	git_tag *tag = NULL;
	if (replace_name(&tag, repo, e, prio, sha1)) {
		if (!found) {
			e = git__malloc(sizeof(struct commit_name));
107
			GIT_ERROR_CHECK_ALLOC(e);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

			e->path = NULL;
			e->tag = NULL;
		}

		if (e->tag)
			git_tag_free(e->tag);
		e->tag = tag;
		e->prio = prio;
		e->name_checked = 0;
		git_oid_cpy(&e->sha1, sha1);
		git__free(e->path);
		e->path = git__strdup(path);
		git_oid_cpy(&e->peeled, peeled);

123 124
		if (!found && git_oidmap_set(names, &e->peeled, e) < 0)
			return -1;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	}
	else
		git_tag_free(tag);

	return 0;
}

static int retrieve_peeled_tag_or_object_oid(
	git_oid *peeled_out,
	git_oid *ref_target_out,
	git_repository *repo,
	const char *refname)
{
	git_reference *ref;
	git_object *peeled = NULL;
	int error;

	if ((error = git_reference_lookup_resolved(&ref, repo, refname, -1)) < 0)
		return error;

145
	if ((error = git_reference_peel(&peeled, ref, GIT_OBJECT_ANY)) < 0)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		goto cleanup;

	git_oid_cpy(ref_target_out, git_reference_target(ref));
	git_oid_cpy(peeled_out, git_object_id(peeled));

	if (git_oid_cmp(ref_target_out, peeled_out) != 0)
		error = 1; /* The reference was pointing to a annotated tag */
	else
		error = 0; /* Any other object */

cleanup:
	git_reference_free(ref);
	git_object_free(peeled);
	return error;
}

162
struct git_describe_result {
163 164 165 166 167 168 169
	int dirty;
	int exact_match;
	int fallback_to_id;
	git_oid commit_id;
	git_repository *repo;
	struct commit_name *name;
	struct possible_tag *tag;
170
};
171

172 173
struct get_name_data
{
174
	git_describe_options *opts;
175 176
	git_repository *repo;
	git_oidmap *names;
177
	git_describe_result *result;
178 179
};

180 181 182 183 184
static int commit_name_dup(struct commit_name **out, struct commit_name *in)
{
	struct commit_name *name;

	name = git__malloc(sizeof(struct commit_name));
185
	GIT_ERROR_CHECK_ALLOC(name);
186 187 188 189 190

	memcpy(name, in,  sizeof(struct commit_name));
	name->tag = NULL;
	name->path = NULL;

191
	if (in->tag && git_tag_dup(&name->tag, in->tag) < 0)
192 193 194
		return -1;

	name->path = git__strdup(in->path);
195
	GIT_ERROR_CHECK_ALLOC(name->path);
196 197 198 199 200

	*out = name;
	return 0;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static int get_name(const char *refname, void *payload)
{
	struct get_name_data *data;
	bool is_tag, is_annotated, all;
	git_oid peeled, sha1;
	unsigned int prio;
	int error = 0;

	data = (struct get_name_data *)payload;
	is_tag = !git__prefixcmp(refname, GIT_REFS_TAGS_DIR);
	all = data->opts->describe_strategy == GIT_DESCRIBE_ALL;

	/* Reject anything outside refs/tags/ unless --all */
	if (!all && !is_tag)
		return 0;

	/* Accept only tags that match the pattern, if given */
218
	if (data->opts->pattern && (!is_tag || wildmatch(data->opts->pattern,
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
		refname + strlen(GIT_REFS_TAGS_DIR), 0)))
				return 0;

	/* Is it annotated? */
	if ((error = retrieve_peeled_tag_or_object_oid(
		&peeled, &sha1, data->repo, refname)) < 0)
		return error;

	is_annotated = error;

	/*
	 * By default, we only use annotated tags, but with --tags
	 * we fall back to lightweight ones (even without --tags,
	 * we still remember lightweight ones, only to give hints
	 * in an error message).  --all allows any refs to be used.
	 */
	if (is_annotated)
		prio = 2;
	else if (is_tag)
		prio = 1;
	else
		prio = 0;

	add_to_known_names(data->repo, data->names,
		all ? refname + strlen(GIT_REFS_DIR) : refname + strlen(GIT_REFS_TAGS_DIR),
		&peeled, prio, &sha1);
	return 0;
}

struct possible_tag {
	struct commit_name *name;
	int depth;
	int found_order;
	unsigned flag_within;
};

255 256 257
static int possible_tag_dup(struct possible_tag **out, struct possible_tag *in)
{
	struct possible_tag *tag;
Jacques Germishuys committed
258
	int error;
259 260

	tag = git__malloc(sizeof(struct possible_tag));
261
	GIT_ERROR_CHECK_ALLOC(tag);
262 263 264 265

	memcpy(tag, in, sizeof(struct possible_tag));
	tag->name = NULL;

Jacques Germishuys committed
266 267 268 269 270
	if ((error = commit_name_dup(&tag->name, in->name)) < 0) {
		git__free(tag);
		*out = NULL;
		return error;
	}
271 272 273 274 275

	*out = tag;
	return 0;
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
static int compare_pt(const void *a_, const void *b_)
{
	struct possible_tag *a = (struct possible_tag *)a_;
	struct possible_tag *b = (struct possible_tag *)b_;
	if (a->depth != b->depth)
		return a->depth - b->depth;
	if (a->found_order != b->found_order)
		return a->found_order - b->found_order;
	return 0;
}

#define SEEN (1u << 0)

static unsigned long finish_depth_computation(
	git_pqueue *list,
	git_revwalk *walk,
	struct possible_tag *best)
{
	unsigned long seen_commits = 0;
	int error, i;

	while (git_pqueue_size(list) > 0) {
		git_commit_list_node *c = git_pqueue_pop(list);
		seen_commits++;
		if (c->flags & best->flag_within) {
			size_t index = 0;
			while (git_pqueue_size(list) > index) {
				git_commit_list_node *i = git_pqueue_get(list, index);
				if (!(i->flags & best->flag_within))
					break;
				index++;
			}
			if (index > git_pqueue_size(list))
				break;
		} else
			best->depth++;
		for (i = 0; i < c->out_degree; i++) {
			git_commit_list_node *p = c->parents[i];
			if ((error = git_commit_list_parse(walk, p)) < 0)
				return error;
			if (!(p->flags & SEEN))
				if ((error = git_pqueue_insert(list, p)) < 0)
					return error;
			p->flags |= c->flags;
		}
	}
	return seen_commits;
}

static int display_name(git_buf *buf, git_repository *repo, struct commit_name *n)
{
	if (n->prio == 2 && !n->tag) {
		if (git_tag_lookup(&n->tag, repo, &n->sha1) < 0) {
329
			git_error_set(GIT_ERROR_TAG, "annotated tag '%s' not available", n->path);
330 331 332 333 334 335
			return -1;
		}
	}

	if (n->tag && !n->name_checked) {
		if (!git_tag_name(n->tag)) {
336
			git_error_set(GIT_ERROR_TAG, "annotated tag '%s' has no embedded name", n->path);
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
			return -1;
		}

		/* TODO: Cope with warnings
		if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
			warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
		*/

		n->name_checked = 1;
	}

	if (n->tag)
		git_buf_printf(buf, "%s", git_tag_name(n->tag));
	else
		git_buf_printf(buf, "%s", n->path);

	return 0;
}

static int find_unique_abbrev_size(
	int *out,
358
	git_repository *repo,
359
	const git_oid *oid_in,
360
	unsigned int abbreviated_size)
361
{
362 363 364 365 366 367 368
	size_t size = abbreviated_size;
	git_odb *odb;
	git_oid dummy;
	int error;

	if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
		return error;
369

370 371 372 373 374
	while (size < GIT_OID_HEXSZ) {
		if ((error = git_odb_exists_prefix(&dummy, odb, oid_in, size)) == 0) {
			*out = (int) size;
			return 0;
		}
375

376 377 378 379 380 381 382 383 384
		/* If the error wasn't that it's not unique, then it's a proper error */
		if (error != GIT_EAMBIGUOUS)
			return error;

		/* Try again with a larger size */
		size++;
	}

	/* If we didn't find any shorter prefix, we have to do the whole thing */
385
	*out = GIT_OID_HEXSZ;
386

387 388 389 390 391 392
	return 0;
}

static int show_suffix(
	git_buf *buf,
	int depth,
393
	git_repository *repo,
394
	const git_oid* id,
395
	unsigned int abbrev_size)
396
{
397
	int error, size = 0;
398 399 400

	char hex_oid[GIT_OID_HEXSZ];

401
	if ((error = find_unique_abbrev_size(&size, repo, id, abbrev_size)) < 0)
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
		return error;

	git_oid_fmt(hex_oid, id);

	git_buf_printf(buf, "-%d-g", depth);

	git_buf_put(buf, hex_oid, size);

	return git_buf_oom(buf) ? -1 : 0;
}

#define MAX_CANDIDATES_TAGS FLAG_BITS - 1

static int describe_not_found(const git_oid *oid, const char *message_format) {
	char oid_str[GIT_OID_HEXSZ + 1];
	git_oid_tostr(oid_str, sizeof(oid_str), oid);

419
	git_error_set(GIT_ERROR_DESCRIBE, message_format, oid_str);
420 421 422 423 424
	return GIT_ENOTFOUND;
}

static int describe(
	struct get_name_data *data,
425
	git_commit *commit)
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
{
	struct commit_name *n;
	struct possible_tag *best;
	bool all, tags;
	git_revwalk *walk = NULL;
	git_pqueue list;
	git_commit_list_node *cmit, *gave_up_on = NULL;
	git_vector all_matches = GIT_VECTOR_INIT;
	unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
	unsigned long seen_commits = 0;	/* TODO: Check long */
	unsigned int unannotated_cnt = 0;
	int error;

	if (git_vector_init(&all_matches, MAX_CANDIDATES_TAGS, compare_pt) < 0)
		return -1;

	if ((error = git_pqueue_init(&list, 0, 2, git_commit_list_time_cmp)) < 0)
		goto cleanup;

	all = data->opts->describe_strategy == GIT_DESCRIBE_ALL;
	tags = data->opts->describe_strategy == GIT_DESCRIBE_TAGS;

448 449
	git_oid_cpy(&data->result->commit_id, git_commit_id(commit));

450 451 452 453 454
	n = find_commit_name(data->names, git_commit_id(commit));
	if (n && (tags || all || n->prio == 2)) {
		/*
		 * Exact match to an existing ref.
		 */
455 456
		data->result->exact_match = 1;
		if ((error = commit_name_dup(&data->result->name, n)) < 0)
457 458
			goto cleanup;

459
		goto cleanup;
460 461 462 463 464
	}

	if (!data->opts->max_candidates_tags) {
		error = describe_not_found(
			git_commit_id(commit),
465
			"cannot describe - no tag exactly matches '%s'");
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

		goto cleanup;
	}

	if ((error = git_revwalk_new(&walk, git_commit_owner(commit))) < 0)
		goto cleanup;

	if ((cmit = git_revwalk__commit_lookup(walk, git_commit_id(commit))) == NULL)
		goto cleanup;

	if ((error = git_commit_list_parse(walk, cmit)) < 0)
		goto cleanup;

	cmit->flags = SEEN;

	if ((error = git_pqueue_insert(&list, cmit)) < 0)
		goto cleanup;

	while (git_pqueue_size(&list) > 0)
	{
		int i;

		git_commit_list_node *c = (git_commit_list_node *)git_pqueue_pop(&list);
		seen_commits++;

		n = find_commit_name(data->names, &c->oid);

		if (n) {
			if (!tags && !all && n->prio < 2) {
				unannotated_cnt++;
			} else if (match_cnt < data->opts->max_candidates_tags) {
				struct possible_tag *t = git__malloc(sizeof(struct commit_name));
498
				GIT_ERROR_CHECK_ALLOC(t);
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
				if ((error = git_vector_insert(&all_matches, t)) < 0)
					goto cleanup;

				match_cnt++;

				t->name = n;
				t->depth = seen_commits - 1;
				t->flag_within = 1u << match_cnt;
				t->found_order = match_cnt;
				c->flags |= t->flag_within;
				if (n->prio == 2)
					annotated_cnt++;
			}
			else {
				gave_up_on = c;
				break;
			}
		}

		for (cur_match = 0; cur_match < match_cnt; cur_match++) {
			struct possible_tag *t = git_vector_get(&all_matches, cur_match);
			if (!(c->flags & t->flag_within))
				t->depth++;
		}

		if (annotated_cnt && (git_pqueue_size(&list) == 0)) {
			/*
			if (debug) {
				char oid_str[GIT_OID_HEXSZ + 1];
				git_oid_tostr(oid_str, sizeof(oid_str), &c->oid);

				fprintf(stderr, "finished search at %s\n", oid_str);
			}
			*/
			break;
		}
		for (i = 0; i < c->out_degree; i++) {
			git_commit_list_node *p = c->parents[i];
			if ((error = git_commit_list_parse(walk, p)) < 0)
				goto cleanup;
			if (!(p->flags & SEEN))
				if ((error = git_pqueue_insert(&list, p)) < 0)
					goto cleanup;
			p->flags |= c->flags;

			if (data->opts->only_follow_first_parent)
				break;
		}
	}

	if (!match_cnt) {
		if (data->opts->show_commit_oid_as_fallback) {
551 552
			data->result->fallback_to_id = 1;
			git_oid_cpy(&data->result->commit_id, &cmit->oid);
553

554
			goto cleanup;
555 556
		}
		if (unannotated_cnt) {
557
			error = describe_not_found(git_commit_id(commit),
558 559 560
				"cannot describe - "
				"no annotated tags can describe '%s'; "
			    "however, there were unannotated tags.");
561 562 563
			goto cleanup;
		}
		else {
564
			error = describe_not_found(git_commit_id(commit),
565 566
				"cannot describe - "
				"no tags can describe '%s'.");
567 568 569 570 571 572 573 574 575
			goto cleanup;
		}
	}

	git_vector_sort(&all_matches);

	best = (struct possible_tag *)git_vector_get(&all_matches, 0);

	if (gave_up_on) {
576 577
		if ((error = git_pqueue_insert(&list, gave_up_on)) < 0)
			goto cleanup;
578 579 580 581 582
		seen_commits--;
	}
	if ((error = finish_depth_computation(
		&list, walk, best)) < 0)
		goto cleanup;
583

584
	seen_commits += error;
585 586
	if ((error = possible_tag_dup(&data->result->tag, best)) < 0)
		goto cleanup;
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

	/*
	{
		static const char *prio_names[] = {
			"head", "lightweight", "annotated",
		};

		char oid_str[GIT_OID_HEXSZ + 1];

		if (debug) {
			for (cur_match = 0; cur_match < match_cnt; cur_match++) {
				struct possible_tag *t = (struct possible_tag *)git_vector_get(&all_matches, cur_match);
				fprintf(stderr, " %-11s %8d %s\n",
					prio_names[t->name->prio],
					t->depth, t->name->path);
			}
			fprintf(stderr, "traversed %lu commits\n", seen_commits);
			if (gave_up_on) {
				git_oid_tostr(oid_str, sizeof(oid_str), &gave_up_on->oid);
				fprintf(stderr,
					"more than %i tags found; listed %i most recent\n"
					"gave up search at %s\n",
					data->opts->max_candidates_tags, data->opts->max_candidates_tags,
					oid_str);
			}
		}
	}
	*/

616
	git_oid_cpy(&data->result->commit_id, &cmit->oid);
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

cleanup:
	{
		size_t i;
		struct possible_tag *match;
		git_vector_foreach(&all_matches, i, match) {
			git__free(match);
		}
	}
	git_vector_free(&all_matches);
	git_pqueue_free(&list);
	git_revwalk_free(walk);
	return error;
}

static int normalize_options(
633 634
	git_describe_options *dst,
	const git_describe_options *src)
635
{
636
	git_describe_options default_options = GIT_DESCRIBE_OPTIONS_INIT;
637 638 639 640 641 642 643 644 645 646
	if (!src) src = &default_options;

	*dst = *src;

	if (dst->max_candidates_tags > GIT_DESCRIBE_DEFAULT_MAX_CANDIDATES_TAGS)
		dst->max_candidates_tags = GIT_DESCRIBE_DEFAULT_MAX_CANDIDATES_TAGS;

	return 0;
}

647
int git_describe_commit(
648
	git_describe_result **result,
649
	git_object *committish,
650
	git_describe_options *opts)
651 652 653 654 655
{
	struct get_name_data data;
	struct commit_name *name;
	git_commit *commit;
	int error = -1;
656
	git_describe_options normalized;
657

658 659
	GIT_ASSERT_ARG(result);
	GIT_ASSERT_ARG(committish);
660 661

	data.result = git__calloc(1, sizeof(git_describe_result));
662
	GIT_ERROR_CHECK_ALLOC(data.result);
663
	data.result->repo = git_object_owner(committish);
664 665 666

	data.repo = git_object_owner(committish);

667
	if ((error = normalize_options(&normalized, opts)) < 0)
668 669
		return error;

670
	GIT_ERROR_CHECK_VERSION(
671
		&normalized,
672
		GIT_DESCRIBE_OPTIONS_VERSION,
673
		"git_describe_options");
674
	data.opts = &normalized;
675

676 677
	if ((error = git_oidmap_new(&data.names)) < 0)
		return error;
678 679 680

	/** TODO: contains to be implemented */

681
	if ((error = git_object_peel((git_object **)(&commit), committish, GIT_OBJECT_COMMIT)) < 0)
682 683
		goto cleanup;

684
	if ((error = git_reference_foreach_name(
685
			git_object_owner(committish),
686
			get_name, &data)) < 0)
687 688
				goto cleanup;

689
	if (git_oidmap_size(data.names) == 0 && !normalized.show_commit_oid_as_fallback) {
690
		git_error_set(GIT_ERROR_DESCRIBE, "cannot describe - "
691
			"no reference found, cannot describe anything.");
692 693 694 695
		error = -1;
		goto cleanup;
	}

696
	if ((error = describe(&data, commit)) < 0)
697 698 699
		goto cleanup;

cleanup:
700 701
	git_commit_free(commit);

702 703 704 705 706 707 708
	git_oidmap_foreach_value(data.names, name, {
		git_tag_free(name->tag);
		git__free(name->path);
		git__free(name);
	});

	git_oidmap_free(data.names);
709 710 711 712 713

	if (error < 0)
		git_describe_result_free(data.result);
	else
		*result = data.result;
714 715 716

	return error;
}
717

718 719 720
int git_describe_workdir(
	git_describe_result **out,
	git_repository *repo,
721
	git_describe_options *opts)
722 723 724 725 726
{
	int error;
	git_oid current_id;
	git_status_list *status = NULL;
	git_status_options status_opts = GIT_STATUS_OPTIONS_INIT;
727
	git_describe_result *result = NULL;
728 729 730 731 732
	git_object *commit;

	if ((error = git_reference_name_to_id(&current_id, repo, GIT_HEAD_FILE)) < 0)
		return error;

733
	if ((error = git_object_lookup(&commit, repo, &current_id, GIT_OBJECT_COMMIT)) < 0)
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
		return error;

	/* The first step is to perform a describe of HEAD, so we can leverage this */
	if ((error = git_describe_commit(&result, commit, opts)) < 0)
		goto out;

	if ((error = git_status_list_new(&status, repo, &status_opts)) < 0)
		goto out;


	if (git_status_list_entrycount(status) > 0)
		result->dirty = 1;

out:
	git_object_free(commit);
	git_status_list_free(status);

	if (error < 0)
		git_describe_result_free(result);
	else
		*out = result;

	return error;
}

759 760 761 762 763
static int normalize_format_options(
	git_describe_format_options *dst,
	const git_describe_format_options *src)
{
	if (!src) {
764
		git_describe_format_options_init(dst, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION);
765 766 767 768 769 770 771 772
		return 0;
	}

	memcpy(dst, src, sizeof(git_describe_format_options));
	return 0;
}

int git_describe_format(git_buf *out, const git_describe_result *result, const git_describe_format_options *given)
773 774 775 776
{
	int error;
	git_repository *repo;
	struct commit_name *name;
777
	git_describe_format_options opts;
778

779 780
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(result);
781

782
	GIT_ERROR_CHECK_VERSION(given, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION, "git_describe_format_options");
783 784
	normalize_format_options(&opts, given);

785 786
	if ((error = git_buf_sanitize(out)) < 0)
		return error;
787 788


789
	if (opts.always_use_long_format && opts.abbreviated_size == 0) {
790
		git_error_set(GIT_ERROR_DESCRIBE, "cannot describe - "
791 792 793 794 795 796 797 798 799 800 801 802 803 804
			"'always_use_long_format' is incompatible with a zero"
			"'abbreviated_size'");
		return -1;
	}


	repo = result->repo;

	/* If we did find an exact match, then it's the easier method */
	if (result->exact_match) {
		name = result->name;
		if ((error = display_name(out, repo, name)) < 0)
			return error;

805
		if (opts.always_use_long_format) {
806
			const git_oid *id = name->tag ? git_tag_target_id(name->tag) : &result->commit_id;
807
			if ((error = show_suffix(out, 0, repo, id, opts.abbreviated_size)) < 0)
808 809 810
				return error;
		}

811 812
		if (result->dirty && opts.dirty_suffix)
			git_buf_puts(out, opts.dirty_suffix);
813 814 815 816 817 818 819

		return git_buf_oom(out) ? -1 : 0;
	}

	/* If we didn't find *any* tags, we fall back to the commit's id */
	if (result->fallback_to_id) {
		char hex_oid[GIT_OID_HEXSZ + 1] = {0};
820 821
		int size = 0;

822
		if ((error = find_unique_abbrev_size(
823
			     &size, repo, &result->commit_id, opts.abbreviated_size)) < 0)
824 825 826 827 828
			return -1;

		git_oid_fmt(hex_oid, &result->commit_id);
		git_buf_put(out, hex_oid, size);

829 830
		if (result->dirty && opts.dirty_suffix)
			git_buf_puts(out, opts.dirty_suffix);
831 832 833 834 835 836 837 838 839 840

		return git_buf_oom(out) ? -1 : 0;
	}

	/* Lastly, if we found a matching tag, we show that */
	name = result->tag->name;

	if ((error = display_name(out, repo, name)) < 0)
		return error;

841
	if (opts.abbreviated_size) {
842
		if ((error = show_suffix(out, result->tag->depth, repo,
843
			&result->commit_id, opts.abbreviated_size)) < 0)
844
			return error;
845 846
	}

847 848
	if (result->dirty && opts.dirty_suffix) {
		git_buf_puts(out, opts.dirty_suffix);
849
	}
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

	return git_buf_oom(out) ? -1 : 0;
}

void git_describe_result_free(git_describe_result *result)
{
	if (result == NULL)
		return;

	if (result->name) {
		git_tag_free(result->name->tag);
		git__free(result->name->path);
		git__free(result->name);
	}

	if (result->tag) {
		git_tag_free(result->tag->name->tag);
		git__free(result->tag->name->path);
		git__free(result->tag->name);
		git__free(result->tag);
	}

	git__free(result);
}
874

875
int git_describe_options_init(git_describe_options *opts, unsigned int version)
876 877 878 879 880 881
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_describe_options, GIT_DESCRIBE_OPTIONS_INIT);
	return 0;
}

882
#ifndef GIT_DEPRECATE_HARD
883 884 885 886
int git_describe_init_options(git_describe_options *opts, unsigned int version)
{
	return git_describe_options_init(opts, version);
}
887
#endif
888 889

int git_describe_format_options_init(git_describe_format_options *opts, unsigned int version)
890 891 892 893 894
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_describe_format_options, GIT_DESCRIBE_FORMAT_OPTIONS_INIT);
	return 0;
}
895

896
#ifndef GIT_DEPRECATE_HARD
897 898 899 900
int git_describe_init_format_options(git_describe_format_options *opts, unsigned int version)
{
	return git_describe_format_options_init(opts, version);
}
901
#endif