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

#include "common.h"
#include "commit.h"
#include "tag.h"
11 12
#include "config.h"
#include "refspec.h"
13
#include "refs.h"
14
#include "remote.h"
15

16 17
#include "git2/branch.h"

18 19 20 21 22 23
static int retrieve_branch_reference(
	git_reference **branch_reference_out,
	git_repository *repo,
	const char *branch_name,
	int is_remote)
{
Russell Belfer committed
24 25
	git_reference *branch = NULL;
	int error = 0;
26 27 28 29 30
	char *prefix;
	git_buf ref_name = GIT_BUF_INIT;

	prefix = is_remote ? GIT_REFS_REMOTES_DIR : GIT_REFS_HEADS_DIR;

Russell Belfer committed
31 32 33 34 35 36
	if ((error = git_buf_joinpath(&ref_name, prefix, branch_name)) < 0)
		/* OOM */;
	else if ((error = git_reference_lookup(&branch, repo, ref_name.ptr)) < 0)
		giterr_set(
			GITERR_REFERENCE, "Cannot locate %s branch '%s'",
			is_remote ? "remote-tracking" : "local", branch_name);
37

Russell Belfer committed
38
	*branch_reference_out = branch; /* will be NULL on error */
39 40 41 42 43

	git_buf_free(&ref_name);
	return error;
}

44
static int not_a_local_branch(const char *reference_name)
45
{
46 47 48
	giterr_set(
		GITERR_INVALID,
		"Reference '%s' is not a local branch.", reference_name);
49 50 51
	return -1;
}

52
int git_branch_create(
53 54 55 56
	git_reference **ref_out,
	git_repository *repository,
	const char *branch_name,
	const git_commit *commit,
57 58 59
	int force,
	const git_signature *signature,
	const char *log_message)
60 61
{
	git_reference *branch = NULL;
62 63 64
	git_buf canonical_branch_name = GIT_BUF_INIT,
			  log_message_buf = GIT_BUF_INIT;
	int error = -1;
65

Vicent Marti committed
66 67
	assert(branch_name && commit && ref_out);
	assert(git_object_owner((const git_object *)commit) == repository);
68

69 70
	if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0)
		goto cleanup;
71

72 73 74 75 76 77 78 79
	if (git_buf_sets(&log_message_buf, log_message ? log_message : "Branch: created") < 0)
		goto cleanup;

	error = git_reference_create(&branch, repository,
		git_buf_cstr(&canonical_branch_name), git_commit_id(commit), force, signature,
		git_buf_cstr(&log_message_buf));

	if (!error)
80
		*ref_out = branch;
81

82
cleanup:
83
	git_buf_free(&canonical_branch_name);
84
	git_buf_free(&log_message_buf);
85 86 87
	return error;
}

88
int git_branch_delete(git_reference *branch)
89
{
90
	int is_head;
91 92
	git_buf config_section = GIT_BUF_INIT;
	int error = -1;
93

94
	assert(branch);
95

96 97 98 99
	if (!git_reference_is_branch(branch) && !git_reference_is_remote(branch)) {
		giterr_set(GITERR_INVALID, "Reference '%s' is not a valid branch.",
			git_reference_name(branch));
		return GIT_ENOTFOUND;
100
	}
101

102 103
	if ((is_head = git_branch_is_head(branch)) < 0)
		return is_head;
104

105
	if (is_head) {
106 107
		giterr_set(GITERR_REFERENCE, "Cannot delete branch '%s' as it is "
			"the current HEAD of the repository.", git_reference_name(branch));
108
		return -1;
109 110
	}

111 112
	if (git_buf_join(&config_section, '.', "branch",
			git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
113 114 115
		goto on_error;

	if (git_config_rename_section(
116 117
		git_reference_owner(branch), git_buf_cstr(&config_section), NULL) < 0)
		goto on_error;
118

119 120 121
	if (git_reference_delete(branch) < 0)
		goto on_error;

122 123 124
	if (git_reflog_delete(git_reference_owner(branch), git_reference_name(branch)) < 0)
		goto on_error;

125 126 127 128 129
	error = 0;

on_error:
	git_buf_free(&config_section);
	return error;
130 131
}

132
typedef struct {
133
	git_reference_iterator *iter;
134 135 136
	unsigned int flags;
} branch_iter;

137
int git_branch_next(git_reference **out, git_branch_t *out_type, git_branch_iterator *_iter)
138 139
{
	branch_iter *iter = (branch_iter *) _iter;
Vicent Marti committed
140
	git_reference *ref;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	int error;

	while ((error = git_reference_next(&ref, iter->iter)) == 0) {
		if ((iter->flags & GIT_BRANCH_LOCAL) &&
		    !git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR)) {
			*out = ref;
			*out_type = GIT_BRANCH_LOCAL;

			return 0;
		} else  if ((iter->flags & GIT_BRANCH_REMOTE) &&
			    !git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR)) {
			*out = ref;
			*out_type = GIT_BRANCH_REMOTE;

			return 0;
		} else {
			git_reference_free(ref);
		}
	}
160

161 162
	return error;
}
163

164 165 166
int git_branch_iterator_new(
	git_branch_iterator **out,
	git_repository *repo,
167
	git_branch_t list_flags)
168 169
{
	branch_iter *iter;
170

171 172
	iter = git__calloc(1, sizeof(branch_iter));
	GITERR_CHECK_ALLOC(iter);
Vicent Marti committed
173

174
	iter->flags = list_flags;
175

176 177 178
	if (git_reference_iterator_new(&iter->iter, repo) < 0) {
		git__free(iter);
		return -1;
179 180
	}

181
	*out = (git_branch_iterator *) iter;
182

183 184 185 186 187 188 189
	return 0;
}

void git_branch_iterator_free(git_branch_iterator *_iter)
{
	branch_iter *iter = (branch_iter *) _iter;

190 191 192
	if (iter == NULL)
		return;

193 194
	git_reference_iterator_free(iter->iter);
	git__free(iter);
195 196
}

197
int git_branch_move(
198
	git_reference **out,
199 200
	git_reference *branch,
	const char *new_branch_name,
201 202 203
	int force,
	const git_signature *signature,
	const char *log_message)
204
{
205
	git_buf new_reference_name = GIT_BUF_INIT,
206 207 208
	        old_config_section = GIT_BUF_INIT,
	        new_config_section = GIT_BUF_INIT,
	        log_message_buf = GIT_BUF_INIT;
209
	int error;
210

211 212 213
	assert(branch && new_branch_name);

	if (!git_reference_is_branch(branch))
214
		return not_a_local_branch(git_reference_name(branch));
215

216
	if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0)
217
		goto done;
218

219 220 221 222 223 224 225 226 227
	if (log_message) {
		if ((error = git_buf_sets(&log_message_buf, log_message)) < 0)
			goto done;
	} else {
		if ((error = git_buf_printf(&log_message_buf, "Branch: renamed %s to %s",
						git_reference_name(branch), git_buf_cstr(&new_reference_name))) < 0)
			goto done;
	}

228
	/* first update ref then config so failure won't trash config */
Vicent Marti committed
229

230
	error = git_reference_rename(
231
		out, branch, git_buf_cstr(&new_reference_name), force,
232
		signature, git_buf_cstr(&log_message_buf));
233
	if (error < 0)
234
		goto done;
235

236 237 238 239 240 241 242 243
	git_buf_join(&old_config_section, '.', "branch",
		git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR));
	git_buf_join(&new_config_section, '.', "branch", new_branch_name);

	error = git_config_rename_section(
		git_reference_owner(branch),
		git_buf_cstr(&old_config_section),
		git_buf_cstr(&new_config_section));
244

245
done:
246
	git_buf_free(&new_reference_name);
247 248
	git_buf_free(&old_config_section);
	git_buf_free(&new_config_section);
249
	git_buf_free(&log_message_buf);
250

251
	return error;
252
}
253 254

int git_branch_lookup(
255 256 257 258
	git_reference **ref_out,
	git_repository *repo,
	const char *branch_name,
	git_branch_t branch_type)
259 260 261 262 263
{
	assert(ref_out && repo && branch_name);

	return retrieve_branch_reference(ref_out, repo, branch_name, branch_type == GIT_BRANCH_REMOTE);
}
264

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
int git_branch_name(const char **out, git_reference *ref)
{
	const char *branch_name;

	assert(out && ref);

	branch_name = ref->name;

	if (git_reference_is_branch(ref)) {
		branch_name += strlen(GIT_REFS_HEADS_DIR);
	} else if (git_reference_is_remote(ref)) {
		branch_name += strlen(GIT_REFS_REMOTES_DIR);
	} else {
		giterr_set(GITERR_INVALID,
				"Reference '%s' is neither a local nor a remote branch.", ref->name);
		return -1;
	}
	*out = branch_name;
	return 0;
}

286
static int retrieve_upstream_configuration(
287 288 289 290
	const char **out,
	git_repository *repo,
	const char *canonical_branch_name,
	const char *format)
291 292 293 294 295
{
	git_config *config;
	git_buf buf = GIT_BUF_INIT;
	int error;

296
	if (git_repository_config__weakptr(&config, repo) < 0)
297 298 299
		return -1;

	if (git_buf_printf(&buf, format,
300
		canonical_branch_name + strlen(GIT_REFS_HEADS_DIR)) < 0)
301 302 303 304 305 306 307
			return -1;

	error = git_config_get_string(out, config, git_buf_cstr(&buf));
	git_buf_free(&buf);
	return error;
}

308 309
int git_branch_upstream_name(
	git_buf *out,
310
	git_repository *repo,
311
	const char *refname)
312 313 314 315 316 317 318
{
	const char *remote_name, *merge_name;
	git_buf buf = GIT_BUF_INIT;
	int error = -1;
	git_remote *remote = NULL;
	const git_refspec *refspec;

319 320 321
	assert(out && refname);

	git_buf_sanitize(out);
322

323 324
	if (!git_reference__is_branch(refname))
		return not_a_local_branch(refname);
325

326
	if ((error = retrieve_upstream_configuration(
327
		&remote_name, repo, refname, "branch.%s.remote")) < 0)
328
			goto cleanup;
329

330
	if ((error = retrieve_upstream_configuration(
331
		&merge_name, repo, refname, "branch.%s.merge")) < 0)
332
			goto cleanup;
333

nulltoken committed
334
	if (!*remote_name || !*merge_name) {
335
		giterr_set(GITERR_REFERENCE,
336
			"branch '%s' does not have an upstream", refname);
nulltoken committed
337 338 339
		error = GIT_ENOTFOUND;
		goto cleanup;
	}
340 341

	if (strcmp(".", remote_name) != 0) {
342
		if ((error = git_remote_load(&remote, repo, remote_name)) < 0)
343 344
			goto cleanup;

345 346 347 348
		refspec = git_remote__matching_refspec(remote, merge_name);
		if (!refspec) {
			error = GIT_ENOTFOUND;
			goto cleanup;
349 350
		}

351
		if (git_refspec_transform(&buf, refspec, merge_name) < 0)
352 353 354 355 356
			goto cleanup;
	} else
		if (git_buf_sets(&buf, merge_name) < 0)
			goto cleanup;

357
	error = git_buf_set(out, git_buf_cstr(&buf), git_buf_len(&buf));
358 359 360 361 362 363

cleanup:
	git_remote_free(remote);
	git_buf_free(&buf);
	return error;
}
364

365
int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refname)
366 367
{
	git_strarray remote_list = {0};
368
	size_t i;
369 370 371 372 373
	git_remote *remote;
	const git_refspec *fetchspec;
	int error = 0;
	char *remote_name = NULL;

374 375 376
	assert(buf && repo && refname);

	git_buf_sanitize(buf);
377 378

	/* Verify that this is a remote branch */
379
	if (!git_reference__is_remote(refname)) {
380
		giterr_set(GITERR_INVALID, "Reference '%s' is not a remote branch.",
381
			refname);
382 383 384 385 386 387 388 389 390 391 392
		error = GIT_ERROR;
		goto cleanup;
	}

	/* Get the remotes */
	if ((error = git_remote_list(&remote_list, repo)) < 0)
		goto cleanup;

	/* Find matching remotes */
	for (i = 0; i < remote_list.count; i++) {
		if ((error = git_remote_load(&remote, repo, remote_list.strings[i])) < 0)
393
			continue;
394

395
		fetchspec = git_remote__matching_dst_refspec(remote, refname);
396
		if (fetchspec) {
397 398 399 400 401 402 403 404
			/* If we have not already set out yet, then set
			 * it to the matching remote name. Otherwise
			 * multiple remotes match this reference, and it
			 * is ambiguous. */
			if (!remote_name) {
				remote_name = remote_list.strings[i];
			} else {
				git_remote_free(remote);
405 406

				giterr_set(GITERR_REFERENCE,
407
					"Reference '%s' is ambiguous", refname);
408 409 410 411 412 413 414 415 416
				error = GIT_EAMBIGUOUS;
				goto cleanup;
			}
		}

		git_remote_free(remote);
	}

	if (remote_name) {
417 418
		git_buf_clear(buf);
		error = git_buf_puts(buf, remote_name);
419
	} else {
420
		giterr_set(GITERR_REFERENCE,
421
			"Could not determine remote for '%s'", refname);
422 423 424 425
		error = GIT_ENOTFOUND;
	}

cleanup:
426 427 428
	if (error < 0)
		git_buf_free(buf);

429 430 431 432
	git_strarray_free(&remote_list);
	return error;
}

433
int git_branch_upstream(
434 435 436 437 438 439
		git_reference **tracking_out,
		git_reference *branch)
{
	int error;
	git_buf tracking_name = GIT_BUF_INIT;

440
	if ((error = git_branch_upstream_name(&tracking_name,
441 442 443 444 445 446 447 448 449 450 451 452
		git_reference_owner(branch), git_reference_name(branch))) < 0)
			return error;

	error = git_reference_lookup(
		tracking_out,
		git_reference_owner(branch),
		git_buf_cstr(&tracking_name));

	git_buf_free(&tracking_name);
	return error;
}

453 454 455 456 457 458 459 460 461 462 463 464 465 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 498 499 500 501 502 503 504 505 506 507
static int unset_upstream(git_config *config, const char *shortname)
{
	git_buf buf = GIT_BUF_INIT;

	if (git_buf_printf(&buf, "branch.%s.remote", shortname) < 0)
		return -1;

	if (git_config_delete_entry(config, git_buf_cstr(&buf)) < 0)
		goto on_error;

	git_buf_clear(&buf);
	if (git_buf_printf(&buf, "branch.%s.merge", shortname) < 0)
		goto on_error;

	if (git_config_delete_entry(config, git_buf_cstr(&buf)) < 0)
		goto on_error;

	git_buf_free(&buf);
	return 0;

on_error:
	git_buf_free(&buf);
	return -1;
}

int git_branch_set_upstream(git_reference *branch, const char *upstream_name)
{
	git_buf key = GIT_BUF_INIT, value = GIT_BUF_INIT;
	git_reference *upstream;
	git_repository *repo;
	git_remote *remote = NULL;
	git_config *config;
	const char *name, *shortname;
	int local;
	const git_refspec *fetchspec;

	name = git_reference_name(branch);
	if (!git_reference__is_branch(name))
		return not_a_local_branch(name);

	if (git_repository_config__weakptr(&config, git_reference_owner(branch)) < 0)
		return -1;

	shortname = name + strlen(GIT_REFS_HEADS_DIR);

	if (upstream_name == NULL)
		return unset_upstream(config, shortname);

	repo = git_reference_owner(branch);

	/* First we need to figure out whether it's a branch or remote-tracking */
	if (git_branch_lookup(&upstream, repo, upstream_name, GIT_BRANCH_LOCAL) == 0)
		local = 1;
	else if (git_branch_lookup(&upstream, repo, upstream_name, GIT_BRANCH_REMOTE) == 0)
		local = 0;
508 509 510
	else {
		giterr_set(GITERR_REFERENCE,
			"Cannot set upstream for branch '%s'", shortname);
511
		return GIT_ENOTFOUND;
512
	}
513 514 515 516 517 518 519 520 521 522

	/*
	 * If it's local, the remote is "." and the branch name is
	 * simply the refname. Otherwise we need to figure out what
	 * the remote-tracking branch's name on the remote is and use
	 * that.
	 */
	if (local)
		git_buf_puts(&value, ".");
	else
523
		git_branch_remote_name(&value, repo, git_reference_name(upstream));
524 525 526 527 528 529 530 531

	if (git_buf_printf(&key, "branch.%s.remote", shortname) < 0)
		goto on_error;

	if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&value)) < 0)
		goto on_error;

	if (local) {
532 533
		git_buf_clear(&value);
		if (git_buf_puts(&value, git_reference_name(upstream)) < 0)
534 535 536 537 538 539
			goto on_error;
	} else {
		/* Get the remoe-tracking branch's refname in its repo */
		if (git_remote_load(&remote, repo, git_buf_cstr(&value)) < 0)
			goto on_error;

540
		fetchspec = git_remote__matching_dst_refspec(remote, git_reference_name(upstream));
541
		git_buf_clear(&value);
542
		if (!fetchspec || git_refspec_rtransform(&value, fetchspec, git_reference_name(upstream)) < 0)
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
			goto on_error;

		git_remote_free(remote);
		remote = NULL;
	}

	git_buf_clear(&key);
	if (git_buf_printf(&key, "branch.%s.merge", shortname) < 0)
		goto on_error;

	if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&value)) < 0)
		goto on_error;

	git_reference_free(upstream);
	git_buf_free(&key);
	git_buf_free(&value);

	return 0;

on_error:
	git_reference_free(upstream);
	git_buf_free(&key);
	git_buf_free(&value);
	git_remote_free(remote);

	return -1;
}

571 572 573 574 575
int git_branch_is_head(
		git_reference *branch)
{
	git_reference *head;
	bool is_same = false;
576
	int error;
577 578 579 580 581 582

	assert(branch);

	if (!git_reference_is_branch(branch))
		return false;

583 584
	error = git_repository_head(&head, git_reference_owner(branch));

585
	if (error == GIT_EUNBORNBRANCH || error == GIT_ENOTFOUND)
586 587 588
		return false;

	if (error < 0)
589 590 591 592 593 594 595 596 597 598
		return -1;

	is_same = strcmp(
		git_reference_name(branch),
		git_reference_name(head)) == 0;

	git_reference_free(head);

	return is_same;
}