branch.c 11 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

15 16
#include "git2/branch.h"

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
static int retrieve_branch_reference(
	git_reference **branch_reference_out,
	git_repository *repo,
	const char *branch_name,
	int is_remote)
{
	git_reference *branch;
	int error = -1;
	char *prefix;
	git_buf ref_name = GIT_BUF_INIT;

	*branch_reference_out = NULL;

	prefix = is_remote ? GIT_REFS_REMOTES_DIR : GIT_REFS_HEADS_DIR;

	if (git_buf_joinpath(&ref_name, prefix, branch_name) < 0)
		goto cleanup;

	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);
		goto cleanup;
	}

	*branch_reference_out = branch;

cleanup:
	git_buf_free(&ref_name);
	return error;
}

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

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

Vicent Marti committed
67 68
	assert(branch_name && commit && ref_out);
	assert(git_object_owner((const git_object *)commit) == repository);
69 70 71 72

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

73
	error = git_reference_create(&branch, repository,
Vicent Marti committed
74
		git_buf_cstr(&canonical_branch_name), git_commit_id(commit), force);
75

76 77
	if (!error)
		*ref_out = branch;
78 79 80 81 82 83

cleanup:
	git_buf_free(&canonical_branch_name);
	return error;
}

84
int git_branch_delete(git_reference *branch)
85
{
86
	int is_head;
87 88
	git_buf config_section = GIT_BUF_INIT;
	int error = -1;
89

90
	assert(branch);
91

92 93 94 95 96
	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 -1;
	}
97

98 99
	if ((is_head = git_branch_is_head(branch)) < 0)
		return is_head;
100

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

107 108 109 110
	if (git_buf_printf(&config_section, "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
		goto on_error;

	if (git_config_rename_section(
111
		git_reference_owner(branch), 
112
		git_buf_cstr(&config_section),
113
		NULL) < 0)
114 115
			goto on_error;

116 117 118 119 120 121 122 123
	if (git_reference_delete(branch) < 0)
		goto on_error;

	error = 0;

on_error:
	git_buf_free(&config_section);
	return error;
124 125 126
}

typedef struct {
127
	git_branch_foreach_cb branch_cb;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	void *callback_payload;
	unsigned int branch_type;
} branch_foreach_filter;

static int branch_foreach_cb(const char *branch_name, void *payload)
{
	branch_foreach_filter *filter = (branch_foreach_filter *)payload;

	if (filter->branch_type & GIT_BRANCH_LOCAL &&
		git__prefixcmp(branch_name, GIT_REFS_HEADS_DIR) == 0)
		return filter->branch_cb(branch_name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, filter->callback_payload);

	if (filter->branch_type & GIT_BRANCH_REMOTE &&
		git__prefixcmp(branch_name, GIT_REFS_REMOTES_DIR) == 0)
		return filter->branch_cb(branch_name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, filter->callback_payload);

	return 0;
}

int git_branch_foreach(
148 149 150 151
	git_repository *repo,
	unsigned int list_flags,
	git_branch_foreach_cb branch_cb,
	void *payload)
152 153 154 155 156 157 158 159 160 161
{
	branch_foreach_filter filter;

	filter.branch_cb = branch_cb;
	filter.branch_type = list_flags;
	filter.callback_payload = payload;

	return git_reference_foreach(repo, GIT_REF_LISTALL, &branch_foreach_cb, (void *)&filter);
}

162
int git_branch_move(
163
	git_reference **out,
164 165 166 167
	git_reference *branch,
	const char *new_branch_name,
	int force)
{
168
	git_buf new_reference_name = GIT_BUF_INIT,
169 170
		old_config_section = GIT_BUF_INIT,
		new_config_section = GIT_BUF_INIT;
171
	int error;
172

173 174 175
	assert(branch && new_branch_name);

	if (!git_reference_is_branch(branch))
176
		return not_a_local_branch(git_reference_name(branch));
177

178
	if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0 ||
179
		(error = git_buf_printf(&old_config_section, "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR))) < 0 ||
180 181
		(error = git_buf_printf(&new_config_section, "branch.%s", new_branch_name)) < 0)
		goto done;
182

183
	if ((error = git_config_rename_section(git_reference_owner(branch),
184 185
		git_buf_cstr(&old_config_section),
		git_buf_cstr(&new_config_section))) < 0)
186 187 188 189
		goto done;
	
	if ((error = git_reference_rename(out, branch, git_buf_cstr(&new_reference_name), force)) < 0)
		goto done;
190

191
done:
192
	git_buf_free(&new_reference_name);
193 194
	git_buf_free(&old_config_section);
	git_buf_free(&new_config_section);
195

196
	return error;
197
}
198 199

int git_branch_lookup(
200 201 202 203
	git_reference **ref_out,
	git_repository *repo,
	const char *branch_name,
	git_branch_t branch_type)
204 205 206 207 208
{
	assert(ref_out && repo && branch_name);

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

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
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;
}

231
static int retrieve_tracking_configuration(
232 233 234 235
	const char **out,
	git_repository *repo,
	const char *canonical_branch_name,
	const char *format)
236 237 238 239 240
{
	git_config *config;
	git_buf buf = GIT_BUF_INIT;
	int error;

241
	if (git_repository_config__weakptr(&config, repo) < 0)
242 243 244
		return -1;

	if (git_buf_printf(&buf, format,
245
		canonical_branch_name + strlen(GIT_REFS_HEADS_DIR)) < 0)
246 247 248 249 250 251 252
			return -1;

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

253 254 255 256
int git_branch_tracking__name(
	git_buf *tracking_name,
	git_repository *repo,
	const char *canonical_branch_name)
257 258 259 260 261 262 263
{
	const char *remote_name, *merge_name;
	git_buf buf = GIT_BUF_INIT;
	int error = -1;
	git_remote *remote = NULL;
	const git_refspec *refspec;

264
	assert(tracking_name && canonical_branch_name);
265

266 267
	if (!git_reference__is_branch(canonical_branch_name))
		return not_a_local_branch(canonical_branch_name);
268

269 270 271
	if ((error = retrieve_tracking_configuration(
		&remote_name, repo, canonical_branch_name, "branch.%s.remote")) < 0)
			goto cleanup;
272

273 274 275
	if ((error = retrieve_tracking_configuration(
		&merge_name, repo, canonical_branch_name, "branch.%s.merge")) < 0)
			goto cleanup;
276

nulltoken committed
277 278 279 280
	if (!*remote_name || !*merge_name) {
		error = GIT_ENOTFOUND;
		goto cleanup;
	}
281 282

	if (strcmp(".", remote_name) != 0) {
283
		if ((error = git_remote_load(&remote, repo, remote_name)) < 0)
284 285 286
			goto cleanup;

		refspec = git_remote_fetchspec(remote);
287 288 289 290 291
		if (refspec == NULL
			|| refspec->src == NULL
			|| refspec->dst == NULL) {
				error = GIT_ENOTFOUND;
				goto cleanup;
292 293 294 295 296 297 298 299
		}

		if (git_refspec_transform_r(&buf, refspec, merge_name) < 0)
			goto cleanup;
	} else
		if (git_buf_sets(&buf, merge_name) < 0)
			goto cleanup;

300
	error = git_buf_set(tracking_name, git_buf_cstr(&buf), git_buf_len(&buf));
301 302 303 304 305 306

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

308 309 310 311
int git_branch_remote_name(
	char *remote_name_out,
	size_t buffer_size,
	git_repository *repo,
312
	const char *canonical_branch_name)
313 314 315 316 317 318 319 320
{
	git_strarray remote_list = {0};
	size_t i, remote_name_size;
	git_remote *remote;
	const git_refspec *fetchspec;
	int error = 0;
	char *remote_name = NULL;

321
	assert(repo && canonical_branch_name);
322 323 324 325 326

	if (remote_name_out && buffer_size)
		*remote_name_out = '\0';

	/* Verify that this is a remote branch */
327 328 329
	if (!git_reference__is_remote(canonical_branch_name)) {
		giterr_set(GITERR_INVALID, "Reference '%s' is not a remote branch.",
			canonical_branch_name);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
		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)
			goto cleanup;

		fetchspec = git_remote_fetchspec(remote);

		/* Defensivly check that we have a fetchspec */
		if (fetchspec &&
347
			git_refspec_dst_matches(fetchspec, canonical_branch_name)) {
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
			/* 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);
				error = GIT_EAMBIGUOUS;
				goto cleanup;
			}
		}

		git_remote_free(remote);
	}

	if (remote_name) {
		remote_name_size = strlen(remote_name) + 1;
		error = (int) remote_name_size;

		if (remote_name_out) {
			if(remote_name_size > buffer_size) {
				giterr_set(
					GITERR_INVALID,
					"Buffer too short to hold the remote name.");
				error = GIT_ERROR;
				goto cleanup;
			}

			memcpy(remote_name_out, remote_name, remote_name_size);
		}
	} else {
		error = GIT_ENOTFOUND;
		goto cleanup;
	}

cleanup:
	git_strarray_free(&remote_list);
	return error;
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
int git_branch_tracking_name(
	char *tracking_branch_name_out,
	size_t buffer_size,
	git_repository *repo,
	const char *canonical_branch_name)
{
	git_buf buf = GIT_BUF_INIT;
	int error;

	assert(canonical_branch_name);

	if (tracking_branch_name_out && buffer_size)
		*tracking_branch_name_out = '\0';

	if ((error = git_branch_tracking__name(
		&buf, repo, canonical_branch_name)) < 0)
			goto cleanup;

	if (tracking_branch_name_out && buf.size + 1 > buffer_size) { /* +1 for NUL byte */
		giterr_set(
			GITERR_INVALID,
			"Buffer too short to hold the tracked reference name.");
		error = -1;
		goto cleanup;
	}

	if (tracking_branch_name_out)
		git_buf_copy_cstr(tracking_branch_name_out, buffer_size, &buf);

418
	error = (int)buf.size + 1;
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

cleanup:
	git_buf_free(&buf);
	return (int)error;
}

int git_branch_tracking(
		git_reference **tracking_out,
		git_reference *branch)
{
	int error;
	git_buf tracking_name = GIT_BUF_INIT;

	if ((error = git_branch_tracking__name(&tracking_name,
		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;
}

445 446 447 448 449
int git_branch_is_head(
		git_reference *branch)
{
	git_reference *head;
	bool is_same = false;
450
	int error;
451 452 453 454 455 456

	assert(branch);

	if (!git_reference_is_branch(branch))
		return false;

457 458
	error = git_repository_head(&head, git_reference_owner(branch));

459
	if (error == GIT_EORPHANEDHEAD || error == GIT_ENOTFOUND)
460 461 462
		return false;

	if (error < 0)
463 464 465 466 467 468 469 470 471 472
		return -1;

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

	git_reference_free(head);

	return is_same;
}