branch.c 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) 2009-2012 the libgit2 contributors
 *
 * 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

14 15
#include "git2/branch.h"

16 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
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;
}

47 48 49 50 51 52
static int not_a_local_branch(git_reference *ref)
{
	giterr_set(GITERR_INVALID, "Reference '%s' is not a local branch.", git_reference_name(ref));
	return -1;
}

53
int git_branch_create(
54
		git_reference **ref_out,
55
		git_repository *repository,
56
		const char *branch_name,
Vicent Marti committed
57
		const git_commit *commit,
58 59 60 61 62 63
		int force)
{
	git_reference *branch = NULL;
	git_buf canonical_branch_name = GIT_BUF_INIT;
	int error = -1;

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

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

70
	error = git_reference_create(&branch, repository,
Vicent Marti committed
71
		git_buf_cstr(&canonical_branch_name), git_commit_id(commit), force);
72

73 74
	if (!error)
		*ref_out = branch;
75 76 77 78 79 80

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

81
int git_branch_delete(git_reference *branch)
82
{
83
	int is_head;
84 85
	git_buf config_section = GIT_BUF_INIT;
	int error = -1;
86

87
	assert(branch);
88

89 90 91 92 93
	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;
	}
94

95 96
	if ((is_head = git_branch_is_head(branch)) < 0)
		return is_head;
97

98 99 100 101
	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;
102 103
	}

104 105 106 107
	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(
108
		git_reference_owner(branch), 
109
		git_buf_cstr(&config_section),
110
		NULL) < 0)
111 112
			goto on_error;

113 114 115 116 117 118 119 120
	if (git_reference_delete(branch) < 0)
		goto on_error;

	error = 0;

on_error:
	git_buf_free(&config_section);
	return error;
121 122 123
}

typedef struct {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	int (*branch_cb)(
			const char *branch_name,
			git_branch_t branch_type,
			void *payload);
	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(
		git_repository *repo,
		unsigned int list_flags,
		int (*branch_cb)(
			const char *branch_name,
			git_branch_t branch_type,
			void *payload),
		void *payload
)
{
	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);
}

166 167 168 169 170
int git_branch_move(
	git_reference *branch,
	const char *new_branch_name,
	int force)
{
171
	git_buf new_reference_name = GIT_BUF_INIT,
172 173
		old_config_section = GIT_BUF_INIT,
		new_config_section = GIT_BUF_INIT;
174
	int error;
175

176 177 178 179
	assert(branch && new_branch_name);

	if (!git_reference_is_branch(branch))
		return not_a_local_branch(branch);
180

181 182
	if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0)
		goto cleanup;
183

184 185 186 187 188
	if (git_buf_printf(
		&old_config_section,
		"branch.%s",
		git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
			goto cleanup;
189 190 191 192

	if ((error = git_reference_rename(branch, git_buf_cstr(&new_reference_name), force)) < 0)
		goto cleanup;

193 194 195 196
	if (git_buf_printf(&new_config_section, "branch.%s", new_branch_name) < 0)
		goto cleanup;

	if ((error = git_config_rename_section(
197
		git_reference_owner(branch), 
198 199
		git_buf_cstr(&old_config_section),
		git_buf_cstr(&new_config_section))) < 0)
200
			goto cleanup;
201

202
cleanup:
203
	git_buf_free(&new_reference_name);
204 205
	git_buf_free(&old_config_section);
	git_buf_free(&new_config_section);
206

207
	return error;
208
}
209 210 211 212 213 214 215 216 217 218 219

int git_branch_lookup(
		git_reference **ref_out,
		git_repository *repo,
		const char *branch_name,
		git_branch_t branch_type)
{
	assert(ref_out && repo && branch_name);

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

221 222
static int retrieve_tracking_configuration(
	const char **out, git_reference *branch, const char *format)
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 255 256 257 258 259
{
	git_config *config;
	git_buf buf = GIT_BUF_INIT;
	int error;

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

	if (git_buf_printf(&buf, format,
		git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
			return -1;

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

int git_branch_tracking(
		git_reference **tracking_out,
		git_reference *branch)
{
	const char *remote_name, *merge_name;
	git_buf buf = GIT_BUF_INIT;
	int error = -1;
	git_remote *remote = NULL;
	const git_refspec *refspec;

	assert(tracking_out && branch);

	if (!git_reference_is_branch(branch))
		return not_a_local_branch(branch);

	if ((error = retrieve_tracking_configuration(&remote_name, branch, "branch.%s.remote")) < 0)
		goto cleanup;

	if ((error = retrieve_tracking_configuration(&merge_name, branch, "branch.%s.merge")) < 0)
		goto cleanup;
260 261

    if (!*remote_name || !*merge_name) {
262 263 264
        error = GIT_ENOTFOUND;
        goto cleanup;
    }
265 266 267 268 269 270

	if (strcmp(".", remote_name) != 0) {
		if ((error = git_remote_load(&remote, git_reference_owner(branch), remote_name)) < 0)
			goto cleanup;

		refspec = git_remote_fetchspec(remote);
271 272 273 274 275
		if (refspec == NULL
			|| refspec->src == NULL
			|| refspec->dst == NULL) {
				error = GIT_ENOTFOUND;
				goto cleanup;
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
		}

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

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

cleanup:
	git_remote_free(remote);
	git_buf_free(&buf);
	return error;
}
294 295 296 297 298 299

int git_branch_is_head(
		git_reference *branch)
{
	git_reference *head;
	bool is_same = false;
300
	int error;
301 302 303 304 305 306

	assert(branch);

	if (!git_reference_is_branch(branch))
		return false;

307 308
	error = git_repository_head(&head, git_reference_owner(branch));

309
	if (error == GIT_EORPHANEDHEAD || error == GIT_ENOTFOUND)
310 311 312
		return false;

	if (error < 0)
313 314 315 316 317 318 319 320 321 322
		return -1;

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

	git_reference_free(head);

	return is_same;
}