<commit_before>/*
 * ggit-clone-options.c
 * This file is part of libgit2-glib
 *
 * Copyright (C) 2013 - <NAME>
 *
 * libgit2-glib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libgit2-glib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libgit2-glib. If not, see <http://www.gnu.org/licenses/>.
 */

#include <git2.h>
#include <gio/gio.h>

#include "ggit-clone-options.h"
#include "ggit-transfer-progress.h"
#include "ggit-native.h"
#include "ggit-remote.h"
#include "ggit-repository.h"

typedef struct _GgitCloneOptionsPrivate
{
	git_clone_options native;
	GgitFetchOptions *fetch_options;
} GgitCloneOptionsPrivate;

/**
 * GgitCloneOptionsClass::create_repository:
 * @options: a #GgitCloneOptions.
 * @path: the repository path.
 * @is_bare: whether a bare repository should be created.
 * @error: a #GError for error reporting.
 *
 * Returns: (transfer full) (nullable): a #GgitRepository or %NULL in case of an error.
 */

/**
 * GgitCloneOptionsClass::create_remote:
 * @options: a #GgitCloneOptions.
 * @repository: the repository.
 * @name: the remote name.
 * @url: the remote url.
 * @error: a #GError for error reporting.
 *
 * Returns: (transfer full) (nullable): a #GgitRemote or %NULL in case of an error.
 */

G_DEFINE_TYPE_WITH_PRIVATE (GgitCloneOptions, ggit_clone_options, G_TYPE_OBJECT)

const git_clone_options *
_ggit_clone_options_get_native (GgitCloneOptions *options)
{
	GgitCloneOptionsPrivate *priv;

	/* NULL is common for clone_options as it specifies to use the default
	 * so handle a NULL clone_options here instead of in every caller.
	 */
	if (options == NULL)
	{
		return NULL;
	}

	g_return_val_if_fail (GGIT_IS_CLONE_OPTIONS (options), NULL);

	priv = ggit_clone_options_get_instance_private (options);

	return (const git_clone_options *)&priv->native;
}

static void
ggit_clone_options_finalize (GObject *object)
{
	GgitCloneOptions *options = GGIT_CLONE_OPTIONS (object);
	GgitCloneOptionsPrivate *priv;

	priv = ggit_clone_options_get_instance_private (options);

	g_free ((gchar *)priv->native.checkout_branch);

	ggit_fetch_options_free (priv->fetch_options);

	priv->native.repository_cb = NULL;
	priv->native.repository_cb_payload = NULL;

	priv->native.remote_cb = NULL;
	priv->native.remote_cb_payload = NULL;

	G_OBJECT_CLASS (ggit_clone_options_parent_class)->finalize (object);
}

static GgitRepository *
create_repository_default (GgitCloneOptions  *options,
                           const gchar       *path,
                           gboolean           is_bare,
                           GError           **error)
{
	GFile *location;
	GgitRepository *ret;

	location = g_file_new_for_path (path);
	ret = ggit_repository_init_repository (location, is_bare, error);
	g_object_unref (location);

	return ret;
}

static GgitRemote *
create_remote_default (GgitCloneOptions  *options,
                       GgitRepository    *repository,
                       const gchar       *name,
                       const gchar       *url,
                       GError           **error)
{
	return ggit_remote_new (repository, name, url, error);
}

static void
ggit_clone_options_class_init (GgitCloneOptionsClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = ggit_clone_options_finalize;

	klass->create_repository = create_repository_default;
	klass->create_remote = create_remote_default;
}

static gint
create_repository_wrapper (git_repository **out,
                           const char      *path,
                           gint             is_bare,
                           void            *payload)
{
	GgitCloneOptions *options = GGIT_CLONE_OPTIONS (payload);
	GgitCloneOptionsClass *cls = GGIT_CLONE_OPTIONS_GET_CLASS (options);
	GError *error = NULL;
	GgitRepository *repository;

	repository = cls->create_repository(options, path, is_bare ? TRUE : FALSE, &error);

	if (error != NULL)
	{
#if LIBGIT2_VER_MAJOR > 0 || (LIBGIT2_VER_MAJOR == 0 && LIBGIT_VER_MINOR >= 28)
		git_error_set_str (GIT_ERROR, error->message);
#else
		giterr_set_str (GIT_ERROR, error->message);
#endif
		g_error_free (error);

		if (repository != NULL)
		{
			g_object_unref (repository);
		}

		return GIT_ERROR;
	}

	if (repository != NULL)
	{
		*out = _ggit_native_release (repository);
		g_object_unref (repository);
	}

	return GIT_OK;
}

static gint
create_remote_wrapper (git_remote     **out,
                       git_repository  *repo,
                       const char      *name,
                       const char      *url,
                       void            *payload)
{
	GgitCloneOptions *options = GGIT_CLONE_OPTIONS (payload);
	GgitCloneOptionsClass *cls = GGIT_CLONE_OPTIONS_GET_CLASS (options);
	GgitRepository *repository = _ggit_repository_wrap (repo, FALSE);
	GError *error = NULL;
	GgitRemote *remote;

	remote = cls->create_remote(options, repository, name, url, &error);
	g_object_unref (repository);

	if (error)
	{
#if LIBGIT2_VER_MAJOR > 0 || (LIBGIT2_VER_MAJOR == 0 && LIBGIT_VER_MINOR >= 28)
		git_error_set_str (GIT_ERROR, error->message);
#else
		giterr_set_str (GIT_ERROR, error->message);
#endif
		g_error_free (error);

		if (remote != NULL)
		{
			g_object_unref (remote);
		}

		return GIT_ERROR;
	}

	if (remote != NULL)
	{
		*out = _ggit_native_release (GGIT_NATIVE (remote));
		g_object_unref (remote);
	}

	return GIT_OK;
}

static void<commit_msg>detect libgit2 version<commit_after>/*
 * ggit-clone-options.c
 * This file is part of libgit2-glib
 *
 * Copyright (C) 2013 - <NAME>
 *
 * libgit2-glib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libgit2-glib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libgit2-glib. If not, see <http://www.gnu.org/licenses/>.
 */

#include <git2.h>
#include <gio/gio.h>

#include "ggit-clone-options.h"
#include "ggit-transfer-progress.h"
#include "ggit-native.h"
#include "ggit-remote.h"
#include "ggit-repository.h"

typedef struct _GgitCloneOptionsPrivate
{
	git_clone_options native;
	GgitFetchOptions *fetch_options;
} GgitCloneOptionsPrivate;

/**
 * GgitCloneOptionsClass::create_repository:
 * @options: a #GgitCloneOptions.
 * @path: the repository path.
 * @is_bare: whether a bare repository should be created.
 * @error: a #GError for error reporting.
 *
 * Returns: (transfer full) (nullable): a #GgitRepository or %NULL in case of an error.
 */

/**
 * GgitCloneOptionsClass::create_remote:
 * @options: a #GgitCloneOptions.
 * @repository: the repository.
 * @name: the remote name.
 * @url: the remote url.
 * @error: a #GError for error reporting.
 *
 * Returns: (transfer full) (nullable): a #GgitRemote or %NULL in case of an error.
 */

G_DEFINE_TYPE_WITH_PRIVATE (GgitCloneOptions, ggit_clone_options, G_TYPE_OBJECT)

const git_clone_options *
_ggit_clone_options_get_native (GgitCloneOptions *options)
{
	GgitCloneOptionsPrivate *priv;

	/* NULL is common for clone_options as it specifies to use the default
	 * so handle a NULL clone_options here instead of in every caller.
	 */
	if (options == NULL)
	{
		return NULL;
	}

	g_return_val_if_fail (GGIT_IS_CLONE_OPTIONS (options), NULL);

	priv = ggit_clone_options_get_instance_private (options);

	return (const git_clone_options *)&priv->native;
}

static void
ggit_clone_options_finalize (GObject *object)
{
	GgitCloneOptions *options = GGIT_CLONE_OPTIONS (object);
	GgitCloneOptionsPrivate *priv;

	priv = ggit_clone_options_get_instance_private (options);

	g_free ((gchar *)priv->native.checkout_branch);

	ggit_fetch_options_free (priv->fetch_options);

	priv->native.repository_cb = NULL;
	priv->native.repository_cb_payload = NULL;

	priv->native.remote_cb = NULL;
	priv->native.remote_cb_payload = NULL;

	G_OBJECT_CLASS (ggit_clone_options_parent_class)->finalize (object);
}

static GgitRepository *
create_repository_default (GgitCloneOptions  *options,
                           const gchar       *path,
                           gboolean           is_bare,
                           GError           **error)
{
	GFile *location;
	GgitRepository *ret;

	location = g_file_new_for_path (path);
	ret = ggit_repository_init_repository (location, is_bare, error);
	g_object_unref (location);

	return ret;
}

static GgitRemote *
create_remote_default (GgitCloneOptions  *options,
                       GgitRepository    *repository,
                       const gchar       *name,
                       const gchar       *url,
                       GError           **error)
{
	return ggit_remote_new (repository, name, url, error);
}

static void
ggit_clone_options_class_init (GgitCloneOptionsClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = ggit_clone_options_finalize;

	klass->create_repository = create_repository_default;
	klass->create_remote = create_remote_default;
}

static gint
create_repository_wrapper (git_repository **out,
                           const char      *path,
                           gint             is_bare,
                           void            *payload)
{
	GgitCloneOptions *options = GGIT_CLONE_OPTIONS (payload);
	GgitCloneOptionsClass *cls = GGIT_CLONE_OPTIONS_GET_CLASS (options);
	GError *error = NULL;
	GgitRepository *repository;

	repository = cls->create_repository(options, path, is_bare ? TRUE : FALSE, &error);

	if (error != NULL)
	{
#if LIBGIT2_VER_MAJOR > 0 || (LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR >= 28)
		git_error_set_str (GIT_ERROR, error->message);
#else
		giterr_set_str (GIT_ERROR, error->message);
#endif
		g_error_free (error);

		if (repository != NULL)
		{
			g_object_unref (repository);
		}

		return GIT_ERROR;
	}

	if (repository != NULL)
	{
		*out = _ggit_native_release (repository);
		g_object_unref (repository);
	}

	return GIT_OK;
}

static gint
create_remote_wrapper (git_remote     **out,
                       git_repository  *repo,
                       const char      *name,
                       const char      *url,
                       void            *payload)
{
	GgitCloneOptions *options = GGIT_CLONE_OPTIONS (payload);
	GgitCloneOptionsClass *cls = GGIT_CLONE_OPTIONS_GET_CLASS (options);
	GgitRepository *repository = _ggit_repository_wrap (repo, FALSE);
	GError *error = NULL;
	GgitRemote *remote;

	remote = cls->create_remote(options, repository, name, url, &error);
	g_object_unref (repository);

	if (error)
	{
#if LIBGIT2_VER_MAJOR > 0 || (LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR >= 28)
		git_error_set_str (GIT_ERROR, error->message);
#else
		giterr_set_str (GIT_ERROR, error->message);
#endif
		g_error_free (error);

		if (remote != NULL)
		{
			g_object_unref (remote);
		}

		return GIT_ERROR;
	}

	if (remote != NULL)
	{
		*out = _ggit_native_release (GGIT_NATIVE (remote));
		g_object_unref (remote);
	}

	return GIT_OK;
}

static void
{'fraction_non_alphanumeric': 0.09557883976488628, 'fraction_numerical': 0.0041741204531902205, 'mean_word_length': 3.4352096713260294, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '343949', 'n_tokens_mistral': 3953, 'n_tokens_neox': 3822, 'n_words': 991}