clone.c 8.35 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * 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 <assert.h>
9

10 11
#include "git2/clone.h"
#include "git2/remote.h"
12
#include "git2/revparse.h"
13 14
#include "git2/branch.h"
#include "git2/config.h"
15
#include "git2/checkout.h"
16 17
#include "git2/commit.h"
#include "git2/tree.h"
18 19 20 21

#include "common.h"
#include "remote.h"
#include "fileops.h"
22
#include "refs.h"
23
#include "path.h"
24

25
static int create_branch(
26 27 28 29
	git_reference **branch,
	git_repository *repo,
	const git_oid *target,
	const char *name)
30
{
Vicent Marti committed
31
	git_commit *head_obj = NULL;
32
	git_reference *branch_ref;
33
	int error;
Ben Straub committed
34 35

	/* Find the target commit */
Vicent Marti committed
36
	if ((error = git_commit_lookup(&head_obj, repo, target)) < 0)
37
		return error;
Ben Straub committed
38 39

	/* Create the new branch */
40
	error = git_branch_create(&branch_ref, repo, name, head_obj, 0);
Ben Straub committed
41

Vicent Marti committed
42
	git_commit_free(head_obj);
43

44
	if (!error)
45 46 47 48
		*branch = branch_ref;
	else
		git_reference_free(branch_ref);

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
	return error;
}

static int setup_tracking_config(
	git_repository *repo,
	const char *branch_name,
	const char *remote_name,
	const char *merge_target)
{
	git_config *cfg;
	git_buf remote_key = GIT_BUF_INIT, merge_key = GIT_BUF_INIT;
	int error = -1;

	if (git_repository_config__weakptr(&cfg, repo) < 0)
		return -1;

	if (git_buf_printf(&remote_key, "branch.%s.remote", branch_name) < 0)
		goto cleanup;

	if (git_buf_printf(&merge_key, "branch.%s.merge", branch_name) < 0)
		goto cleanup;

	if (git_config_set_string(cfg, git_buf_cstr(&remote_key), remote_name) < 0)
		goto cleanup;

	if (git_config_set_string(cfg, git_buf_cstr(&merge_key), merge_target) < 0)
		goto cleanup;

	error = 0;

cleanup:
	git_buf_free(&remote_key);
	git_buf_free(&merge_key);
	return error;
}

static int create_tracking_branch(
	git_reference **branch,
	git_repository *repo,
	const git_oid *target,
	const char *branch_name)
{
	int error;

	if ((error = create_branch(branch, repo, target, branch_name)) < 0)
		return error;

	return setup_tracking_config(
		repo,
		branch_name,
		GIT_REMOTE_ORIGIN,
		git_reference_name(*branch));
101 102
}

103 104 105 106
struct head_info {
	git_repository *repo;
	git_oid remote_head_oid;
	git_buf branchname;
107
	const git_refspec *refspec;
108 109
};

110 111 112
static int reference_matches_remote_head(
	const char *reference_name,
	void *payload)
113
{
114
	struct head_info *head_info = (struct head_info *)payload;
Ben Straub committed
115 116
	git_oid oid;

117 118 119 120
	/* TODO: Should we guard against references
	 * which name doesn't start with refs/heads/ ?
	 */

Ben Straub committed
121
	/* Stop looking if we've already found a match */
122 123 124
	if (git_buf_len(&head_info->branchname) > 0)
		return 0;

125
	if (git_reference_name_to_id(
126 127 128 129 130 131 132
		&oid,
		head_info->repo,
		reference_name) < 0) {
			/* TODO: How to handle not found references?
			 */
			return -1;
	}
Ben Straub committed
133

134 135 136 137 138 139 140 141 142 143 144 145
	if (git_oid_cmp(&head_info->remote_head_oid, &oid) == 0) {
		/* Determine the local reference name from the remote tracking one */
		if (git_refspec_transform_l(
			&head_info->branchname, 
			head_info->refspec,
			reference_name) < 0)
				return -1;
		
		if (git_buf_sets(
			&head_info->branchname,
			git_buf_cstr(&head_info->branchname) + strlen(GIT_REFS_HEADS_DIR)) < 0)
				return -1;
Ben Straub committed
146
	}
147

Ben Straub committed
148
	return 0;
149 150
}

151 152 153 154
static int update_head_to_new_branch(
	git_repository *repo,
	const git_oid *target,
	const char *name)
155
{
Michael Schubert committed
156
	git_reference *tracking_branch = NULL;
157
	int error;
Ben Straub committed
158

159 160 161 162 163 164
	if ((error = create_tracking_branch(
		&tracking_branch,
		repo,
		target,
		name)) < 0)
			return error;
Ben Straub committed
165

166 167 168 169 170
	error = git_repository_set_head(repo, git_reference_name(tracking_branch));

	git_reference_free(tracking_branch);

	return error;
171 172
}

173 174 175 176 177 178 179 180 181
static int get_head_callback(git_remote_head *head, void *payload)
{
	git_remote_head **destination = (git_remote_head **)payload;

	/* Save the first entry, and terminate the enumeration */
	*destination = head;
	return 1;
}

182 183
static int update_head_to_remote(git_repository *repo, git_remote *remote)
{
184
	int retcode = -1;
Ben Straub committed
185
	git_remote_head *remote_head;
186
	struct head_info head_info;
187
	git_buf remote_master_name = GIT_BUF_INIT;
Ben Straub committed
188

189 190 191 192 193 194 195 196 197
	/* Did we just clone an empty repository? */
	if (remote->refs.length == 0) {
		return setup_tracking_config(
			repo,
			"master",
			GIT_REMOTE_ORIGIN,
			GIT_REFS_HEADS_MASTER_FILE);
	}

Ben Straub committed
198
	/* Get the remote's HEAD. This is always the first ref in remote->refs. */
199 200 201 202 203 204 205
	remote_head = NULL;
	
	if (!remote->transport->ls(remote->transport, get_head_callback, &remote_head))
		return -1;

	assert(remote_head);

Ben Straub committed
206 207 208
	git_oid_cpy(&head_info.remote_head_oid, &remote_head->oid);
	git_buf_init(&head_info.branchname, 16);
	head_info.repo = repo;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	head_info.refspec = git_remote_fetchspec(remote);
	
	/* Determine the remote tracking reference name from the local master */
	if (git_refspec_transform_r(
		&remote_master_name,
		head_info.refspec,
		GIT_REFS_HEADS_MASTER_FILE) < 0)
			return -1;

	/* Check to see if the remote HEAD points to the remote master */
	if (reference_matches_remote_head(git_buf_cstr(&remote_master_name), &head_info) < 0)
		goto cleanup;

	if (git_buf_len(&head_info.branchname) > 0) {
		retcode = update_head_to_new_branch(
			repo,
			&head_info.remote_head_oid,
			git_buf_cstr(&head_info.branchname));

		goto cleanup;
Ben Straub committed
229
	}
230

Ben Straub committed
231
	/* Not master. Check all the other refs. */
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	if (git_reference_foreach(
		repo,
		GIT_REF_LISTALL,
		reference_matches_remote_head,
		&head_info) < 0)
			goto cleanup;

	if (git_buf_len(&head_info.branchname) > 0) {
		retcode = update_head_to_new_branch(
			repo,
			&head_info.remote_head_oid,
			git_buf_cstr(&head_info.branchname));

		goto cleanup;
	} else {
		/* TODO: What should we do if nothing has been found?
		 */
Ben Straub committed
249 250
	}

251 252
cleanup:
	git_buf_free(&remote_master_name);
Ben Straub committed
253 254
	git_buf_free(&head_info.branchname);
	return retcode;
255 256
}

257 258 259 260
/*
 * submodules?
 */

261 262


263 264
static int setup_remotes_and_fetch(
		git_repository *repo,
265
		git_remote *origin,
266
		git_transfer_progress_callback progress_cb,
267
		void *progress_payload)
268
{
Ben Straub committed
269 270
	int retcode = GIT_ERROR;

271 272
	/* Add the origin remote */
	if (!git_remote_set_repository(origin, repo) && !git_remote_save(origin)) {
273 274 275 276 277 278
		/*
		 * Don't write FETCH_HEAD, we'll check out the remote tracking
		 * branch ourselves based on the server's default.
		 */
		git_remote_set_update_fetchhead(origin, 0);

Ben Straub committed
279
		/* Connect and download everything */
Ben Straub committed
280
		if (!git_remote_connect(origin, GIT_DIRECTION_FETCH)) {
281
			if (!git_remote_download(origin, progress_cb, progress_payload)) {
Ben Straub committed
282
				/* Create "origin/foo" branches for all remote branches */
283
				if (!git_remote_update_tips(origin)) {
Ben Straub committed
284 285 286 287 288 289 290 291 292 293 294
					/* Point HEAD to the same ref as the remote's head */
					if (!update_head_to_remote(repo, origin)) {
						retcode = 0;
					}
				}
			}
			git_remote_disconnect(origin);
		}
	}

	return retcode;
295 296
}

Ben Straub committed
297 298 299

static bool path_is_okay(const char *path)
{
Ben Straub committed
300 301
	/* The path must either not exist, or be an empty directory */
	if (!git_path_exists(path)) return true;
Ben Straub committed
302
	if (!git_path_is_empty_dir(path)) {
Ben Straub committed
303 304 305 306
		giterr_set(GITERR_INVALID,
					  "'%s' exists and is not an empty directory", path);
		return false;
	}
Ben Straub committed
307
	return true;
Ben Straub committed
308 309
}

310 311 312 313 314 315 316 317 318 319 320 321 322
static bool should_checkout(
	git_repository *repo,
	bool is_bare,
	git_checkout_opts *opts)
{
	if (is_bare)
		return false;

	if (!opts)
		return false;

	return !git_repository_head_orphan(repo);
}
Ben Straub committed
323

324 325
static int clone_internal(
	git_repository **out,
326
	git_remote *origin_remote,
327
	const char *path,
328
	git_transfer_progress_callback fetch_progress_cb,
329
	void *fetch_progress_payload,
330
	git_checkout_opts *checkout_opts,
331
	bool is_bare)
332
{
Ben Straub committed
333 334 335 336 337 338 339 340
	int retcode = GIT_ERROR;
	git_repository *repo = NULL;

	if (!path_is_okay(path)) {
		return GIT_ERROR;
	}

	if (!(retcode = git_repository_init(&repo, path, is_bare))) {
341
		if ((retcode = setup_remotes_and_fetch(repo, origin_remote,
342
						fetch_progress_cb, fetch_progress_payload)) < 0) {
Ben Straub committed
343 344
			/* Failed to fetch; clean up */
			git_repository_free(repo);
345
			git_futils_rmdir_r(path, NULL, GIT_RMDIR_REMOVE_FILES);
Ben Straub committed
346 347 348 349 350 351
		} else {
			*out = repo;
			retcode = 0;
		}
	}

352
	if (!retcode && should_checkout(repo, is_bare, checkout_opts))
353
		retcode = git_checkout_head(*out, checkout_opts);
354

Ben Straub committed
355
	return retcode;
356 357
}

358 359 360 361 362
int git_clone(
	git_repository **out,
	git_remote *origin,
	const char *local_path,
	const git_clone_options *options)
363
{
364 365 366 367
	git_clone_options dummy_options = GIT_CLONE_OPTIONS_INIT;

	assert(out && origin && local_path);
	if (!options) options = &dummy_options;
368

369
	GITERR_CHECK_VERSION(options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
370

371
	return clone_internal(
372 373 374
		out,
		origin,
		local_path,
375 376 377 378
		options->fetch_progress_cb,
		options->fetch_progress_payload,
		options->checkout_opts,
		options->bare ? 1 : 0);
379
}