Пример #1
0
int git_clone(
	git_repository **out,
	const char *url,
	const char *local_path,
	const git_clone_options *_options)
{
	int retcode = GIT_ERROR;
	git_repository *repo = NULL;
	git_remote *origin;
	git_clone_options options = GIT_CLONE_OPTIONS_INIT;
	int remove_directory_on_failure = 0;

	assert(out && url && local_path);

	if (_options)
		memcpy(&options, _options, sizeof(git_clone_options));

	GITERR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");

	/* Only clone to a new directory or an empty directory */
	if (git_path_exists(local_path) && !git_path_is_empty_dir(local_path)) {
		giterr_set(GITERR_INVALID,
			"'%s' exists and is not an empty directory", local_path);
		return GIT_ERROR;
	}

	/* Only remove the directory on failure if we create it */
	remove_directory_on_failure = !git_path_exists(local_path);

	if ((retcode = git_repository_init(&repo, local_path, options.bare)) < 0)
		return retcode;

	if ((retcode = create_and_configure_origin(&origin, repo, url, &options)) < 0)
		goto cleanup;

	retcode = git_clone_into(repo, origin, &options.checkout_opts, options.checkout_branch);
	git_remote_free(origin);

	if (retcode < 0)
		goto cleanup;

	*out = repo;
	return 0;

cleanup:
	git_repository_free(repo);
	if (remove_directory_on_failure)
		git_futils_rmdir_r(local_path, NULL, GIT_RMDIR_REMOVE_FILES);
	else
		git_futils_cleanupdir_r(local_path);

	return retcode;
}
Пример #2
0
int git_clone(
	git_repository **out,
	const char *url,
	const char *local_path,
	const git_clone_options *options)
{
	int retcode = GIT_ERROR;
	git_repository *repo = NULL;
	git_clone_options normOptions;
	int remove_directory_on_failure = 0;

	assert(out && url && local_path);

	normalize_options(&normOptions, options);
	GITERR_CHECK_VERSION(&normOptions, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");

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

	/* Only remove the directory on failure if we create it */
	remove_directory_on_failure = !git_path_exists(local_path);

	if (!(retcode = git_repository_init(&repo, local_path, normOptions.bare))) {
		if ((retcode = setup_remotes_and_fetch(repo, url, &normOptions)) < 0) {
			/* Failed to fetch; clean up */
			git_repository_free(repo);

			if (remove_directory_on_failure)
				git_futils_rmdir_r(local_path, NULL, GIT_RMDIR_REMOVE_FILES);
			else
				git_futils_cleanupdir_r(local_path);

		} else {
			*out = repo;
			retcode = 0;
		}
	}

	if (!retcode && should_checkout(repo, normOptions.bare, &normOptions.checkout_opts))
		retcode = git_checkout_head(*out, &normOptions.checkout_opts);

	return retcode;
}