refs.c 26.4 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 11
 */

#include "refs.h"
#include "hash.h"
#include "repository.h"
#include "fileops.h"
12
#include "filebuf.h"
13
#include "pack.h"
14
#include "reflog.h"
15
#include "refdb.h"
16

Vicent Marti committed
17 18
#include <git2/tag.h>
#include <git2/object.h>
19
#include <git2/oid.h>
20
#include <git2/branch.h>
21 22
#include <git2/refs.h>
#include <git2/refdb.h>
23
#include <git2/sys/refs.h>
24
#include <git2/signature.h>
Vicent Marti committed
25

26
GIT__USE_STRMAP;
27

28 29
#define DEFAULT_NESTING_LEVEL	5
#define MAX_NESTING_LEVEL		10
30

31 32 33 34
enum {
	GIT_PACKREF_HAS_PEEL = 1,
	GIT_PACKREF_WAS_LOOSE = 2
};
35

36
static git_reference *alloc_ref(const char *name)
37 38 39
{
	git_reference *ref;
	size_t namelen = strlen(name);
40

41 42 43 44 45 46 47 48 49
	if ((ref = git__calloc(1, sizeof(git_reference) + namelen + 1)) == NULL)
		return NULL;

	memcpy(ref->name, name, namelen + 1);

	return ref;
}

git_reference *git_reference__alloc_symbolic(
50
	const char *name, const char *target)
51
{
52
	git_reference *ref;
53

54
	assert(name && target);
55

56
	ref = alloc_ref(name);
57
	if (!ref)
58
		return NULL;
Vicent Marti committed
59

60
	ref->type = GIT_REF_SYMBOLIC;
Vicent Marti committed
61

62 63 64
	if ((ref->target.symbolic = git__strdup(target)) == NULL) {
		git__free(ref);
		return NULL;
65
	}
66

67 68 69 70 71 72 73 74 75 76
	return ref;
}

git_reference *git_reference__alloc(
	const char *name,
	const git_oid *oid,
	const git_oid *peel)
{
	git_reference *ref;

77
	assert(name && oid);
78

79
	ref = alloc_ref(name);
80 81 82 83
	if (!ref)
		return NULL;

	ref->type = GIT_REF_OID;
84
	git_oid_cpy(&ref->target.oid, oid);
85 86

	if (peel != NULL)
87
		git_oid_cpy(&ref->peel, peel);
88

89
	return ref;
90
}
91

92 93 94 95 96 97 98 99 100 101 102
git_reference *git_reference__set_name(
	git_reference *ref, const char *name)
{
	size_t namelen = strlen(name);
	git_reference *rewrite =
		git__realloc(ref, sizeof(git_reference) + namelen + 1);
	if (rewrite != NULL)
		memcpy(rewrite->name, name, namelen + 1);
	return rewrite;
}

103
void git_reference_free(git_reference *reference)
104
{
105 106
	if (reference == NULL)
		return;
107

108
	if (reference->type == GIT_REF_SYMBOLIC)
109
		git__free(reference->target.symbolic);
110

Vicent Marti committed
111 112
	if (reference->db)
		GIT_REFCOUNT_DEC(reference->db, git_refdb__free);
113 114

	git__free(reference);
115
}
116

117
int git_reference_delete(git_reference *ref)
118
{
119 120 121 122 123 124 125 126 127
	const git_oid *old_id = NULL;
	const char *old_target = NULL;

	if (ref->type == GIT_REF_OID)
		old_id = &ref->target.oid;
	else
		old_target = ref->target.symbolic;

	return git_refdb_delete(ref->db, ref->name, old_id, old_target);
128 129
}

130 131 132 133 134 135 136 137 138 139 140
int git_reference_remove(git_repository *repo, const char *name)
{
	git_refdb *db;
	int error;

	if ((error = git_repository_refdb__weakptr(&db, repo)) < 0)
		return error;

	return git_refdb_delete(db, name, NULL, NULL);
}

141
int git_reference_lookup(git_reference **ref_out,
142
	git_repository *repo, const char *name)
143
{
144 145 146
	return git_reference_lookup_resolved(ref_out, repo, name, 0);
}

147
int git_reference_name_to_id(
148 149 150 151 152 153 154 155
	git_oid *out, git_repository *repo, const char *name)
{
	int error;
	git_reference *ref;

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

156
	git_oid_cpy(out, git_reference_target(ref));
157 158 159 160
	git_reference_free(ref);
	return 0;
}

161
static int reference_normalize_for_repo(
162
	git_refname_t out,
163 164 165 166 167 168 169 170 171 172
	git_repository *repo,
	const char *name)
{
	int precompose;
	unsigned int flags = GIT_REF_FORMAT_ALLOW_ONELEVEL;

	if (!git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) &&
		precompose)
		flags |= GIT_REF_FORMAT__PRECOMPOSE_UNICODE;

173
	return git_reference_normalize_name(out, GIT_REFNAME_MAX, name, flags);
174 175
}

176 177 178 179 180 181
int git_reference_lookup_resolved(
	git_reference **ref_out,
	git_repository *repo,
	const char *name,
	int max_nesting)
{
182
	git_refname_t scan_name;
183 184 185 186
	git_ref_t scan_type;
	int error = 0, nesting;
	git_reference *ref = NULL;
	git_refdb *refdb;
187 188

	assert(ref_out && repo && name);
189

190
	*ref_out = NULL;
191

192 193 194 195
	if (max_nesting > MAX_NESTING_LEVEL)
		max_nesting = MAX_NESTING_LEVEL;
	else if (max_nesting < 0)
		max_nesting = DEFAULT_NESTING_LEVEL;
196

197
	scan_type = GIT_REF_SYMBOLIC;
198

199
	if ((error = reference_normalize_for_repo(scan_name, repo, name)) < 0)
200
		return error;
201

202
	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
203
		return error;
204 205

	for (nesting = max_nesting;
206
		 nesting >= 0 && scan_type == GIT_REF_SYMBOLIC;
207 208
		 nesting--)
	{
209
		if (nesting != max_nesting) {
210
			strncpy(scan_name, ref->target.symbolic, sizeof(scan_name));
211 212
			git_reference_free(ref);
		}
213

214 215
		if ((error = git_refdb_lookup(&ref, refdb, scan_name)) < 0)
			return error;
216

217
		scan_type = ref->type;
218 219
	}

220
	if (scan_type != GIT_REF_OID && max_nesting != 0) {
221 222
		giterr_set(GITERR_REFERENCE,
			"Cannot resolve reference (>%u levels deep)", max_nesting);
223
		git_reference_free(ref);
224 225 226
		return -1;
	}

227
	*ref_out = ref;
228
	return 0;
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
int git_reference_dwim(git_reference **out, git_repository *repo, const char *refname)
{
	int error = 0, i;
	bool fallbackmode = true, foundvalid = false;
	git_reference *ref;
	git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT;

	static const char* formatters[] = {
		"%s",
		GIT_REFS_DIR "%s",
		GIT_REFS_TAGS_DIR "%s",
		GIT_REFS_HEADS_DIR "%s",
		GIT_REFS_REMOTES_DIR "%s",
		GIT_REFS_REMOTES_DIR "%s/" GIT_HEAD_FILE,
		NULL
	};

	if (*refname)
		git_buf_puts(&name, refname);
	else {
		git_buf_puts(&name, GIT_HEAD_FILE);
		fallbackmode = false;
	}

	for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) {

		git_buf_clear(&refnamebuf);

		if ((error = git_buf_printf(&refnamebuf, formatters[i], git_buf_cstr(&name))) < 0)
			goto cleanup;

		if (!git_reference_is_valid_name(git_buf_cstr(&refnamebuf))) {
			error = GIT_EINVALIDSPEC;
			continue;
		}
		foundvalid = true;

		error = git_reference_lookup_resolved(&ref, repo, git_buf_cstr(&refnamebuf), -1);

		if (!error) {
			*out = ref;
			error = 0;
			goto cleanup;
		}

		if (error != GIT_ENOTFOUND)
			goto cleanup;
	}

cleanup:
	if (error && !foundvalid) {
		/* never found a valid reference name */
		giterr_set(GITERR_REFERENCE,
			"Could not use '%s' as valid reference name", git_buf_cstr(&name));
	}

	git_buf_free(&name);
	git_buf_free(&refnamebuf);
	return error;
}

292 293 294
/**
 * Getters
 */
295
git_ref_t git_reference_type(const git_reference *ref)
Vicent Marti committed
296 297
{
	assert(ref);
298
	return ref->type;
Vicent Marti committed
299 300
}

301
const char *git_reference_name(const git_reference *ref)
Vicent Marti committed
302 303
{
	assert(ref);
304
	return ref->name;
Vicent Marti committed
305 306
}

307
git_repository *git_reference_owner(const git_reference *ref)
308
{
309
	assert(ref);
310
	return ref->db->repo;
311 312
}

313
const git_oid *git_reference_target(const git_reference *ref)
Vicent Marti committed
314 315 316
{
	assert(ref);

317
	if (ref->type != GIT_REF_OID)
Vicent Marti committed
318 319
		return NULL;

320
	return &ref->target.oid;
321 322 323 324 325 326
}

const git_oid *git_reference_target_peel(const git_reference *ref)
{
	assert(ref);

327
	if (ref->type != GIT_REF_OID || git_oid_iszero(&ref->peel))
328 329
		return NULL;

330
	return &ref->peel;
Vicent Marti committed
331 332
}

333
const char *git_reference_symbolic_target(const git_reference *ref)
334
{
335
	assert(ref);
336

337
	if (ref->type != GIT_REF_SYMBOLIC)
338 339
		return NULL;

340
	return ref->target.symbolic;
341 342
}

343
static int reference__create(
344 345 346
	git_reference **ref_out,
	git_repository *repo,
	const char *name,
347 348
	const git_oid *oid,
	const char *symbolic,
349 350
	int force,
	const git_signature *signature,
351
	const char *log_message,
352 353
	const git_oid *old_id,
	const char *old_target)
354
{
355
	git_refname_t normalized;
356
	git_refdb *refdb;
357
	git_reference *ref = NULL;
358
	int error = 0;
359

360
	assert(repo && name);
361
	assert(symbolic || signature);
362

363 364 365
	if (ref_out)
		*ref_out = NULL;

366
	error = reference_normalize_for_repo(normalized, repo, name);
Vicent Marti committed
367 368 369 370 371
	if (error < 0)
		return error;

	error = git_repository_refdb__weakptr(&refdb, repo);
	if (error < 0)
372
		return error;
373 374

	if (oid != NULL) {
375 376
		git_odb *odb;

377
		assert(symbolic == NULL);
378 379 380 381 382 383 384 385 386 387 388

		/* Sanity check the reference being created - target must exist. */
		if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
			return error;

		if (!git_odb_exists(odb, oid)) {
			giterr_set(GITERR_REFERENCE,
				"Target OID for the reference doesn't exist on the repository");
			return -1;
		}

389
		ref = git_reference__alloc(normalized, oid, NULL);
390
	} else {
391
		git_refname_t normalized_target;
392

393
		if ((error = reference_normalize_for_repo(normalized_target, repo, symbolic)) < 0)
394 395
			return error;

396
		ref = git_reference__alloc_symbolic(normalized, normalized_target);
397
	}
398

399
	GITERR_CHECK_ALLOC(ref);
400

401
	if ((error = git_refdb_write(refdb, ref, force, signature, log_message, old_id, old_target)) < 0) {
402
		git_reference_free(ref);
403
		return error;
404
	}
405

406
	if (ref_out == NULL)
407
		git_reference_free(ref);
408
	else
409
		*ref_out = ref;
410

411
	return 0;
412 413
}

414
static int log_signature(git_signature **out, git_repository *repo)
415
{
416
	int error;
417
	git_signature *who;
418

419 420
	if(((error = git_signature_default(&who, repo)) < 0) &&
	   ((error = git_signature_now(&who, "unknown", "unknown")) < 0))
421
		return error;
422

423 424
	*out = who;
	return 0;
425 426
}

427
int git_reference_create_matching(
428 429 430 431 432
	git_reference **ref_out,
	git_repository *repo,
	const char *name,
	const git_oid *id,
	int force,
433
	const git_oid *old_id,
434
	const git_signature *signature,
435
	const char *log_message)
436

437
{
438 439 440
	int error;
	git_signature *who = NULL;
	
441
	assert(id);
442

443 444 445 446 447 448 449 450
	if (!signature) {
		if ((error = log_signature(&who, repo)) < 0)
			return error;
		else
			signature = who;
	}

	error = reference__create(
451
		ref_out, repo, name, id, NULL, force, signature, log_message, old_id, NULL);
Vicent Marti committed
452

453 454
	git_signature_free(who);
	return error;
455
}
456

457 458 459 460 461 462 463 464 465
int git_reference_create(
	git_reference **ref_out,
	git_repository *repo,
	const char *name,
	const git_oid *id,
	int force,
	const git_signature *signature,
	const char *log_message)
{
466
        return git_reference_create_matching(ref_out, repo, name, id, force, NULL, signature, log_message);
467 468
}

469
int git_reference_symbolic_create_matching(
470 471 472 473 474
	git_reference **ref_out,
	git_repository *repo,
	const char *name,
	const char *target,
	int force,
475
	const char *old_target,
476
	const git_signature *signature,
477
	const char *log_message)
478
{
479 480 481 482 483 484 485 486 487 488 489
	int error;
	git_signature *who = NULL;

	assert(target);

	if (!signature) {
		if ((error = log_signature(&who, repo)) < 0)
			return error;
		else
			signature = who;
	}
490

491
	error = reference__create(
492
		ref_out, repo, name, NULL, target, force, signature, log_message, NULL, old_target);
493 494 495

	git_signature_free(who);
	return error;
496 497
}

498 499 500 501 502 503 504 505 506
int git_reference_symbolic_create(
	git_reference **ref_out,
	git_repository *repo,
	const char *name,
	const char *target,
	int force,
	const git_signature *signature,
	const char *log_message)
{
507
	return git_reference_symbolic_create_matching(ref_out, repo, name, target, force, NULL, signature, log_message);
508 509
}

510 511 512 513
static int ensure_is_an_updatable_direct_reference(git_reference *ref)
{
	if (ref->type == GIT_REF_OID)
		return 0;
514

515 516
	giterr_set(GITERR_REFERENCE, "Cannot set OID on symbolic reference");
	return -1;
517
}
518

519
int git_reference_set_target(
520 521
	git_reference **out,
	git_reference *ref,
522 523
	const git_oid *id,
	const git_signature *signature,
524
	const char *log_message)
525 526
{
	int error;
527
	git_repository *repo;
528 529 530

	assert(out && ref && id);

531 532
	repo = ref->db->repo;

533 534 535
	if ((error = ensure_is_an_updatable_direct_reference(ref)) < 0)
		return error;

536
	return git_reference_create_matching(out, repo, ref->name, id, 1, &ref->target.oid, signature, log_message);
537 538
}

539 540 541 542 543 544 545 546 547
static int ensure_is_an_updatable_symbolic_reference(git_reference *ref)
{
	if (ref->type == GIT_REF_SYMBOLIC)
		return 0;

	giterr_set(GITERR_REFERENCE, "Cannot set symbolic target on a direct reference");
	return -1;
}

548 549 550
int git_reference_symbolic_set_target(
	git_reference **out,
	git_reference *ref,
551 552 553
	const char *target,
	const git_signature *signature,
	const char *log_message)
Vicent Marti committed
554
{
555 556
	int error;

557
	assert(out && ref && target);
558

559 560
	if ((error = ensure_is_an_updatable_symbolic_reference(ref)) < 0)
		return error;
561

562
	return git_reference_symbolic_create_matching(
563
		out, ref->db->repo, ref->name, target, 1, ref->target.symbolic, signature, log_message);
Vicent Marti committed
564 565
}

566 567
static int reference__rename(git_reference **out, git_reference *ref, const char *new_name, int force,
				 const git_signature *signature, const char *message)
568
{
569
	git_refname_t normalized;
570
	bool should_head_be_updated = false;
571
	int error = 0;
572 573

	assert(ref && new_name && signature);
574

575
	if ((error = reference_normalize_for_repo(
576
			normalized, git_reference_owner(ref), new_name)) < 0)
577
		return error;
578

579

580
	/* Check if we have to update HEAD. */
581
	if ((error = git_branch_is_head(ref)) < 0)
Vicent Marti committed
582
		return error;
583

584 585
	should_head_be_updated = (error > 0);

586
	if ((error = git_refdb_rename(out, ref->db, ref->name, normalized, force, signature, message)) < 0)
Vicent Marti committed
587
		return error;
588

589
	/* Update HEAD it was pointing to the reference being renamed */
Vicent Marti committed
590
	if (should_head_be_updated &&
591
		(error = git_repository_set_head(ref->db->repo, normalized, signature, message)) < 0) {
592
		giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference");
Vicent Marti committed
593
		return error;
594 595
	}

596 597 598
	return 0;
}

599

600 601 602 603
int git_reference_rename(
	git_reference **out,
	git_reference *ref,
	const char *new_name,
604 605 606
	int force,
	const git_signature *signature,
	const char *log_message)
607
{
608
	git_signature *who = (git_signature*)signature;
609
	int error;
610

611
	/* Should we return an error if there is no default? */
612 613
	if (!who &&
	    ((error = git_signature_default(&who, ref->db->repo)) < 0) &&
614
	    ((error = git_signature_now(&who, "unknown", "unknown")) < 0)) {
Vicent Marti committed
615
		return error;
616
	}
617

618
	error = reference__rename(out, ref, new_name, force, who, log_message);
619

620 621
	if (!signature)
		git_signature_free(who);
622 623 624 625

	return error;
}

626
int git_reference_resolve(git_reference **ref_out, const git_reference *ref)
Vicent Marti committed
627
{
628 629
	switch (git_reference_type(ref)) {
	case GIT_REF_OID:
630
		return git_reference_lookup(ref_out, ref->db->repo, ref->name);
631

632 633 634 635 636 637 638
	case GIT_REF_SYMBOLIC:
		return git_reference_lookup_resolved(ref_out, ref->db->repo, ref->target.symbolic, -1);

	default:
		giterr_set(GITERR_REFERENCE, "Invalid reference");
		return -1;
	}
Vicent Marti committed
639 640
}

641 642
int git_reference_foreach(
	git_repository *repo,
643
	git_reference_foreach_cb callback,
644
	void *payload)
645
{
646
	git_reference_iterator *iter;
647
	git_reference *ref;
648 649
	int error;

650 651
	if ((error = git_reference_iterator_new(&iter, repo)) < 0)
		return error;
652

653 654
	while (!(error = git_reference_next(&ref, iter))) {
		if ((error = callback(ref, payload)) != 0) {
655
			giterr_set_after_callback(error);
656 657 658
			break;
		}
	}
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

	if (error == GIT_ITEROVER)
		error = 0;

	git_reference_iterator_free(iter);
	return error;
}

int git_reference_foreach_name(
	git_repository *repo,
	git_reference_foreach_name_cb callback,
	void *payload)
{
	git_reference_iterator *iter;
	const char *refname;
674 675
	int error;

676 677
	if ((error = git_reference_iterator_new(&iter, repo)) < 0)
		return error;
678

679 680
	while (!(error = git_reference_next_name(&refname, iter))) {
		if ((error = callback(refname, payload)) != 0) {
681
			giterr_set_after_callback(error);
682 683 684
			break;
		}
	}
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

	if (error == GIT_ITEROVER)
		error = 0;

	git_reference_iterator_free(iter);
	return error;
}

int git_reference_foreach_glob(
	git_repository *repo,
	const char *glob,
	git_reference_foreach_name_cb callback,
	void *payload)
{
	git_reference_iterator *iter;
	const char *refname;
	int error;

703 704
	if ((error = git_reference_iterator_glob_new(&iter, repo, glob)) < 0)
		return error;
705

706 707
	while (!(error = git_reference_next_name(&refname, iter))) {
		if ((error = callback(refname, payload)) != 0) {
708
			giterr_set_after_callback(error);
709 710 711
			break;
		}
	}
712 713 714 715 716 717

	if (error == GIT_ITEROVER)
		error = 0;

	git_reference_iterator_free(iter);
	return error;
718 719
}

720 721 722 723 724 725 726
int git_reference_iterator_new(git_reference_iterator **out, git_repository *repo)
{
	git_refdb *refdb;

	if (git_repository_refdb__weakptr(&refdb, repo) < 0)
		return -1;

727
	return git_refdb_iterator(out, refdb, NULL);
728 729
}

730 731
int git_reference_iterator_glob_new(
	git_reference_iterator **out, git_repository *repo, const char *glob)
732
{
733
	git_refdb *refdb;
734

735 736 737
	if (git_repository_refdb__weakptr(&refdb, repo) < 0)
		return -1;

738
	return git_refdb_iterator(out, refdb, glob);
739 740
}

Vicent Marti committed
741
int git_reference_next(git_reference **out, git_reference_iterator *iter)
742
{
743 744 745 746 747 748
	return git_refdb_iterator_next(out, iter);
}

int git_reference_next_name(const char **out, git_reference_iterator *iter)
{
	return git_refdb_iterator_next_name(out, iter);
749 750 751 752
}

void git_reference_iterator_free(git_reference_iterator *iter)
{
753 754 755
	if (iter == NULL)
		return;

756
	git_refdb_iterator_free(iter);
757 758
}

759
static int cb__reflist_add(const char *ref, void *data)
760
{
761 762 763
	char *name = git__strdup(ref);
	GITERR_CHECK_ALLOC(name);
	return git_vector_insert((git_vector *)data, name);
764 765
}

766
int git_reference_list(
767
	git_strarray *array,
768
	git_repository *repo)
769 770 771 772 773 774 775 776
{
	git_vector ref_list;

	assert(array && repo);

	array->strings = NULL;
	array->count = 0;

777
	if (git_vector_init(&ref_list, 8, NULL) < 0)
778
		return -1;
779

780
	if (git_reference_foreach_name(
781
			repo, &cb__reflist_add, (void *)&ref_list) < 0) {
782
		git_vector_free(&ref_list);
783
		return -1;
784 785
	}

786 787
	array->strings = (char **)git_vector_detach(&array->count, NULL, &ref_list);

788
	return 0;
789
}
Vicent Marti committed
790

791
static int is_valid_ref_char(char ch)
792
{
793
	if ((unsigned) ch <= ' ')
794
		return 0;
795 796 797 798 799 800 801 802

	switch (ch) {
	case '~':
	case '^':
	case ':':
	case '\\':
	case '?':
	case '[':
803
	case '*':
804
		return 0;
805
	default:
806
		return 1;
807 808 809
	}
}

810
static int ensure_segment_validity(const char *name)
811
{
812 813
	const char *current = name;
	char prev = '\0';
814 815
	const int lock_len = (int)strlen(GIT_FILELOCK_EXTENSION);
	int segment_len;
816

817 818
	if (*current == '.')
		return -1; /* Refname starts with "." */
819

820 821 822
	for (current = name; ; current++) {
		if (*current == '\0' || *current == '/')
			break;
823

824 825
		if (!is_valid_ref_char(*current))
			return -1; /* Illegal character in refname */
826

827 828
		if (prev == '.' && *current == '.')
			return -1; /* Refname contains ".." */
829

830 831
		if (prev == '@' && *current == '{')
			return -1; /* Refname contains "@{" */
832

833 834
		prev = *current;
	}
835

836 837
	segment_len = (int)(current - name);

838
	/* A refname component can not end with ".lock" */
839
	if (segment_len >= lock_len &&
840
		!memcmp(current - lock_len, GIT_FILELOCK_EXTENSION, lock_len))
841 842
			return -1;

843
	return segment_len;
844
}
845

846
static bool is_all_caps_and_underscore(const char *name, size_t len)
847
{
848
	size_t i;
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
	char c;

	assert(name && len > 0);

	for (i = 0; i < len; i++)
	{
		c = name[i];
		if ((c < 'A' || c > 'Z') && c != '_')
			return false;
	}

	if (*name == '_' || name[len - 1] == '_')
		return false;

	return true;
}

866
/* Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100 */
867 868 869 870 871 872
int git_reference__normalize_name(
	git_buf *buf,
	const char *name,
	unsigned int flags)
{
	char *current;
873
	int segment_len, segments_count = 0, error = GIT_EINVALIDSPEC;
874 875
	unsigned int process_flags;
	bool normalize = (buf != NULL);
876 877

#ifdef GIT_USE_ICONV
878
	git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
879
#endif
880

881
	assert(name);
882

883
	process_flags = flags;
884 885
	current = (char *)name;

886 887 888
	if (*current == '/')
		goto cleanup;

889 890
	if (normalize)
		git_buf_clear(buf);
891

892
#ifdef GIT_USE_ICONV
893 894 895 896 897
	if ((flags & GIT_REF_FORMAT__PRECOMPOSE_UNICODE) != 0) {
		size_t namelen = strlen(current);
		if ((error = git_path_iconv_init_precompose(&ic)) < 0 ||
			(error = git_path_iconv(&ic, &current, &namelen)) < 0)
			goto cleanup;
898
		error = GIT_EINVALIDSPEC;
899
	}
900
#endif
901

902 903 904
	while (true) {
		segment_len = ensure_segment_validity(current);
		if (segment_len < 0) {
905
			if ((process_flags & GIT_REF_FORMAT_REFSPEC_PATTERN) &&
906 907 908
					current[0] == '*' &&
					(current[1] == '\0' || current[1] == '/')) {
				/* Accept one wildcard as a full refname component. */
909
				process_flags &= ~GIT_REF_FORMAT_REFSPEC_PATTERN;
910 911 912 913
				segment_len = 1;
			} else
				goto cleanup;
		}
914

915
		if (segment_len > 0) {
916
			if (normalize) {
917
				size_t cur_len = git_buf_len(buf);
918

919
				git_buf_joinpath(buf, git_buf_cstr(buf), current);
920
				git_buf_truncate(buf,
921
					cur_len + segment_len + (segments_count ? 1 : 0));
922

923 924
				if (git_buf_oom(buf)) {
					error = -1;
925
					goto cleanup;
926
				}
927
			}
928

929
			segments_count++;
930
		}
931

932 933
		/* No empty segment is allowed when not normalizing */
		if (segment_len == 0 && !normalize)
934
			goto cleanup;
935

936 937
		if (current[segment_len] == '\0')
			break;
938

939
		current += segment_len + 1;
940
	}
941

942
	/* A refname can not be empty */
943
	if (segment_len == 0 && segments_count == 0)
944 945 946 947 948 949 950 951 952 953
		goto cleanup;

	/* A refname can not end with "." */
	if (current[segment_len - 1] == '.')
		goto cleanup;

	/* A refname can not end with "/" */
	if (current[segment_len - 1] == '/')
		goto cleanup;

954 955 956 957
	if ((segments_count == 1 ) && !(flags & GIT_REF_FORMAT_ALLOW_ONELEVEL))
		goto cleanup;

	if ((segments_count == 1 ) &&
958
	    !(flags & GIT_REF_FORMAT_REFSPEC_SHORTHAND) &&
959
		!(is_all_caps_and_underscore(name, (size_t)segment_len) ||
960 961 962 963 964 965
			((flags & GIT_REF_FORMAT_REFSPEC_PATTERN) && !strcmp("*", name))))
			goto cleanup;

	if ((segments_count > 1)
		&& (is_all_caps_and_underscore(name, strchr(name, '/') - name)))
			goto cleanup;
966

967
	error = 0;
968

969
cleanup:
970
	if (error == GIT_EINVALIDSPEC)
971 972 973
		giterr_set(
			GITERR_REFERENCE,
			"The given reference name '%s' is not valid", name);
974

975 976 977
	if (error && normalize)
		git_buf_free(buf);

978
#ifdef GIT_USE_ICONV
979
	git_path_iconv_clear(&ic);
980
#endif
981

982 983
	return error;
}
984

985 986 987 988 989 990 991 992 993 994 995 996 997 998
int git_reference_normalize_name(
	char *buffer_out,
	size_t buffer_size,
	const char *name,
	unsigned int flags)
{
	git_buf buf = GIT_BUF_INIT;
	int error;

	if ((error = git_reference__normalize_name(&buf, name, flags)) < 0)
		goto cleanup;

	if (git_buf_len(&buf) > buffer_size - 1) {
		giterr_set(
999
		GITERR_REFERENCE,
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
		"The provided buffer is too short to hold the normalization of '%s'", name);
		error = GIT_EBUFS;
		goto cleanup;
	}

	git_buf_copy_cstr(buffer_out, buffer_size, &buf);

	error = 0;

cleanup:
	git_buf_free(&buf);
	return error;
1012
}
1013

1014 1015
#define GIT_REF_TYPEMASK (GIT_REF_OID | GIT_REF_SYMBOLIC)

Jacques Germishuys committed
1016 1017 1018
int git_reference_cmp(
	const git_reference *ref1,
	const git_reference *ref2)
1019
{
1020
	git_ref_t type1, type2;
1021 1022
	assert(ref1 && ref2);

1023 1024 1025
	type1 = git_reference_type(ref1);
	type2 = git_reference_type(ref2);

1026
	/* let's put symbolic refs before OIDs */
1027 1028
	if (type1 != type2)
		return (type1 == GIT_REF_SYMBOLIC) ? -1 : 1;
1029

1030
	if (type1 == GIT_REF_SYMBOLIC)
1031 1032
		return strcmp(ref1->target.symbolic, ref2->target.symbolic);

1033
	return git_oid__cmp(&ref1->target.oid, &ref2->target.oid);
1034 1035
}

1036 1037 1038 1039
static int reference__update_terminal(
	git_repository *repo,
	const char *ref_name,
	const git_oid *oid,
1040 1041 1042
	int nesting,
	const git_signature *signature,
	const char *log_message)
nulltoken committed
1043 1044
{
	git_reference *ref;
1045
	int error = 0;
nulltoken committed
1046

1047 1048
	if (nesting > MAX_NESTING_LEVEL) {
		giterr_set(GITERR_REFERENCE, "Reference chain too deep (%d)", nesting);
1049
		return GIT_ENOTFOUND;
1050
	}
1051

1052
	error = git_reference_lookup(&ref, repo, ref_name);
nulltoken committed
1053

1054 1055
	/* If we haven't found the reference at all, create a new reference. */
	if (error == GIT_ENOTFOUND) {
nulltoken committed
1056
		giterr_clear();
1057
		return git_reference_create(NULL, repo, ref_name, oid, 0, signature, log_message);
nulltoken committed
1058
	}
1059

1060 1061
	if (error < 0)
		return error;
1062

1063
	/* If the ref is a symbolic reference, follow its target. */
nulltoken committed
1064
	if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
1065
		error = reference__update_terminal(repo, git_reference_symbolic_target(ref), oid,
1066
			nesting+1, signature, log_message);
nulltoken committed
1067
		git_reference_free(ref);
1068
	} else {
Ben Straub committed
1069 1070 1071
		/* If we're not moving the target, don't recreate the ref */
		if (0 != git_oid_cmp(git_reference_target(ref), oid))
			error = git_reference_create(NULL, repo, ref_name, oid, 1, signature, log_message);
1072
		git_reference_free(ref);
nulltoken committed
1073
	}
1074

1075
	return error;
nulltoken committed
1076
}
1077

1078 1079 1080 1081 1082 1083 1084 1085
/*
 * Starting with the reference given by `ref_name`, follows symbolic
 * references until a direct reference is found and updated the OID
 * on that direct reference to `oid`.
 */
int git_reference__update_terminal(
	git_repository *repo,
	const char *ref_name,
1086 1087 1088
	const git_oid *oid,
	const git_signature *signature,
	const char *log_message)
1089
{
1090
	return reference__update_terminal(repo, ref_name, oid, 0, signature, log_message);
1091 1092
}

1093
int git_reference_has_log(git_repository *repo, const char *refname)
1094
{
1095 1096
	int error;
	git_refdb *refdb;
1097

1098
	assert(repo && refname);
1099

1100 1101
	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
		return error;
1102

1103
	return git_refdb_has_log(refdb, refname);
1104
}
1105

1106 1107 1108 1109 1110 1111 1112 1113 1114
int git_reference_ensure_log(git_repository *repo, const char *refname)
{
	int error;
	git_refdb *refdb;

	assert(repo && refname);

	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
		return error;
1115

1116
	return git_refdb_ensure_log(refdb, refname);
1117
}
1118

1119 1120 1121 1122 1123
int git_reference__is_branch(const char *ref_name)
{
	return git__prefixcmp(ref_name, GIT_REFS_HEADS_DIR) == 0;
}

1124
int git_reference_is_branch(const git_reference *ref)
1125 1126
{
	assert(ref);
1127
	return git_reference__is_branch(ref->name);
1128
}
1129

1130 1131 1132 1133 1134
int git_reference__is_remote(const char *ref_name)
{
	return git__prefixcmp(ref_name, GIT_REFS_REMOTES_DIR) == 0;
}

Jacques Germishuys committed
1135
int git_reference_is_remote(const git_reference *ref)
1136 1137
{
	assert(ref);
1138
	return git_reference__is_remote(ref->name);
1139
}
1140

1141 1142 1143 1144 1145
int git_reference__is_tag(const char *ref_name)
{
	return git__prefixcmp(ref_name, GIT_REFS_TAGS_DIR) == 0;
}

Jacques Germishuys committed
1146
int git_reference_is_tag(const git_reference *ref)
1147 1148 1149 1150 1151
{
	assert(ref);
	return git_reference__is_tag(ref->name);
}

1152 1153 1154 1155 1156
int git_reference__is_note(const char *ref_name)
{
	return git__prefixcmp(ref_name, GIT_REFS_NOTES_DIR) == 0;
}

Jacques Germishuys committed
1157
int git_reference_is_note(const git_reference *ref)
1158 1159 1160 1161 1162
{
	assert(ref);
	return git_reference__is_note(ref->name);
}

1163 1164 1165 1166 1167 1168 1169 1170 1171
static int peel_error(int error, git_reference *ref, const char* msg)
{
	giterr_set(
		GITERR_INVALID,
		"The reference '%s' cannot be peeled - %s", git_reference_name(ref), msg);
	return error;
}

int git_reference_peel(
1172 1173 1174
	git_object **peeled,
	git_reference *ref,
	git_otype target_type)
1175 1176 1177 1178 1179 1180 1181
{
	git_reference *resolved = NULL;
	git_object *target = NULL;
	int error;

	assert(ref);

1182 1183 1184 1185 1186 1187 1188
	if (ref->type == GIT_REF_OID) {
		resolved = ref;
	} else {
		if ((error = git_reference_resolve(&resolved, ref)) < 0)
			return peel_error(error, ref, "Cannot resolve reference");
	}

1189
	if (!git_oid_iszero(&resolved->peel)) {
1190
		error = git_object_lookup(&target,
1191
			git_reference_owner(ref), &resolved->peel, GIT_OBJ_ANY);
1192 1193
	} else {
		error = git_object_lookup(&target,
1194
			git_reference_owner(ref), &resolved->target.oid, GIT_OBJ_ANY);
1195
	}
1196

1197
	if (error < 0) {
1198 1199 1200
		peel_error(error, ref, "Cannot retrieve reference target");
		goto cleanup;
	}
1201

1202
	if (target_type == GIT_OBJ_ANY && git_object_type(target) != GIT_OBJ_TAG)
1203
		error = git_object_dup(peeled, target);
1204
	else
1205 1206 1207 1208
		error = git_object_peel(peeled, target, target_type);

cleanup:
	git_object_free(target);
1209 1210 1211 1212

	if (resolved != ref)
		git_reference_free(resolved);

1213 1214
	return error;
}
1215

1216
int git_reference__is_valid_name(const char *refname, unsigned int flags)
1217
{
1218 1219 1220 1221
	if (git_reference__normalize_name(NULL, refname, flags) < 0) {
		giterr_clear();
		return false;
	}
nulltoken committed
1222

1223
	return true;
1224 1225
}

1226
int git_reference_is_valid_name(const char *refname)
1227
{
1228
	return git_reference__is_valid_name(refname, GIT_REF_FORMAT_ALLOW_ONELEVEL);
1229
}
1230

Jacques Germishuys committed
1231
const char *git_reference_shorthand(const git_reference *ref)
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
{
	const char *name = ref->name;

	if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR))
		return name + strlen(GIT_REFS_HEADS_DIR);
	else if (!git__prefixcmp(name, GIT_REFS_TAGS_DIR))
		return name + strlen(GIT_REFS_TAGS_DIR);
	else if (!git__prefixcmp(name, GIT_REFS_REMOTES_DIR))
		return name + strlen(GIT_REFS_REMOTES_DIR);
	else if (!git__prefixcmp(name, GIT_REFS_DIR))
		return name + strlen(GIT_REFS_DIR);

	/* No shorthands are avaiable, so just return the name */
	return name;
}