[libgit2-glib] Fix GgitConfig



commit 2818020a29cfbe5be86c33d9527a1f08344d440f
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Tue Oct 30 09:23:14 2012 +0100

    Fix GgitConfig
    
    Missing the new methods

 libgit2-glib/ggit-config-entry.c |    8 +-
 libgit2-glib/ggit-config-entry.h |    1 +
 libgit2-glib/ggit-config.c       |  254 ++++++++++++++++----------------------
 libgit2-glib/ggit-config.h       |   14 ++-
 libgit2-glib/ggit-types.c        |    1 +
 libgit2-glib/ggit-types.h        |   15 ++-
 6 files changed, 133 insertions(+), 160 deletions(-)
---
diff --git a/libgit2-glib/ggit-config-entry.c b/libgit2-glib/ggit-config-entry.c
index 48bca2a..f49d043 100644
--- a/libgit2-glib/ggit-config-entry.c
+++ b/libgit2-glib/ggit-config-entry.c
@@ -20,7 +20,7 @@
 
 #include "ggit-config-entry.h"
 
-struct _GgitTreeEntry
+struct _GgitConfigEntry
 {
 	const git_config_entry *entry;
 	gint ref_count;
@@ -34,9 +34,9 @@ G_DEFINE_BOXED_TYPE (GgitConfigEntry,
 GgitConfigEntry *
 _ggit_config_entry_wrap (const git_config_entry *entry)
 {
-	GgitTreeEntry *ret;
+	GgitConfigEntry *ret;
 
-	ret = g_slice_new (GgitTreeEntry);
+	ret = g_slice_new (GgitConfigEntry);
 	ret->entry = entry;
 	ret->ref_count = 1;
 
@@ -76,7 +76,7 @@ ggit_config_entry_unref (GgitConfigEntry *entry)
 
 	if (g_atomic_int_dec_and_test (&entry->ref_count))
 	{
-		g_slice_free (GgitTreeEntry, entry);
+		g_slice_free (GgitConfigEntry, entry);
 	}
 }
 
diff --git a/libgit2-glib/ggit-config-entry.h b/libgit2-glib/ggit-config-entry.h
index c65cb2f..a23fcf5 100644
--- a/libgit2-glib/ggit-config-entry.h
+++ b/libgit2-glib/ggit-config-entry.h
@@ -21,6 +21,7 @@
 #ifndef __GGIT_CONFIG_ENTRY_H__
 #define __GGIT_CONFIG_ENTRY_H__
 
+#include <glib-object.h>
 #include <git2/config.h>
 
 #include "ggit-types.h"
diff --git a/libgit2-glib/ggit-config.c b/libgit2-glib/ggit-config.c
index 4d98b2e..32e2734 100644
--- a/libgit2-glib/ggit-config.c
+++ b/libgit2-glib/ggit-config.c
@@ -19,6 +19,7 @@
  */
 
 #include "ggit-config.h"
+#include "ggit-config-entry.h"
 
 #include <git2/errors.h>
 #include <git2/config.h>
@@ -27,100 +28,31 @@
 
 #define GGIT_CONFIG_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GGIT_TYPE_CONFIG, GgitConfigPrivate))
 
-struct _GgitConfigPrivate
-{
-	GFile *file;
-};
-
 G_DEFINE_TYPE (GgitConfig, ggit_config, GGIT_TYPE_NATIVE)
 
-enum
-{
-	PROP_0,
-	PROP_FILE
-};
-
-static void
-ggit_config_finalize (GObject *object)
-{
-	GgitConfig *config = GGIT_CONFIG (object);
-
-	if (config->priv->file)
-	{
-		g_object_unref (config->priv->file);
-	}
-
-	G_OBJECT_CLASS (ggit_config_parent_class)->finalize (object);
-}
-
-static void
-ggit_config_constructed (GObject *object)
+typedef struct
 {
-	GgitConfig *config = GGIT_CONFIG (object);
-
-	if (config->priv->file)
-	{
-		gchar *path;
-
-		path = g_file_get_path (config->priv->file);
-
-		if (path != NULL)
-		{
-			git_config_add_file_ondisk (_ggit_native_get (config),
-			                            path,
-			                            0);
-		}
+	gpointer user_data;
 
-		g_free (path);
-	}
-}
+	GgitConfigCallback callback;
+} CallbackWrapperData;
 
-static void
-ggit_config_set_property (GObject      *object,
-                          guint         prop_id,
-                          const GValue *value,
-                          GParamSpec   *pspec)
+GgitConfig *
+_ggit_config_wrap (git_config *config)
 {
-	GgitConfig *self = GGIT_CONFIG (object);
-
-	switch (prop_id)
-	{
-		case PROP_FILE:
-		{
-			GFile *f;
+	GgitConfig *ret;
 
-			f = g_value_get_object (value);
+	g_return_val_if_fail (config != NULL, NULL);
 
-			if (f)
-			{
-				self->priv->file = g_file_dup (f);
-			}
+	ret = g_object_new (GGIT_TYPE_CONFIG, "native", config, NULL);
 
-			break;
-		}
-		default:
-			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-			break;
-	}
+	return ret;
 }
 
 static void
-ggit_config_get_property (GObject    *object,
-                          guint       prop_id,
-                          GValue     *value,
-                          GParamSpec *pspec)
+ggit_config_finalize (GObject *object)
 {
-	GgitConfig *self = GGIT_CONFIG (object);
-
-	switch (prop_id)
-	{
-		case PROP_FILE:
-			g_value_set_object (value, self->priv->file);
-			break;
-		default:
-			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-			break;
-	}
+	G_OBJECT_CLASS (ggit_config_parent_class)->finalize (object);
 }
 
 static void
@@ -129,90 +61,107 @@ ggit_config_class_init (GgitConfigClass *klass)
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
 	object_class->finalize = ggit_config_finalize;
-
-	object_class->get_property = ggit_config_get_property;
-	object_class->set_property = ggit_config_set_property;
-
-	object_class->constructed = ggit_config_constructed;
-
-	g_type_class_add_private (object_class, sizeof (GgitConfigPrivate));
-
-	g_object_class_install_property (object_class,
-	                                 PROP_FILE,
-	                                 g_param_spec_object ("file",
-	                                                      "File",
-	                                                      "File",
-	                                                      G_TYPE_FILE,
-	                                                      G_PARAM_READWRITE |
-	                                                      G_PARAM_CONSTRUCT_ONLY |
-	                                                      G_PARAM_STATIC_STRINGS));
 }
 
 static void
-ggit_config_init (GgitConfig *self)
+ggit_config_init (GgitConfig *config)
 {
-	git_config *config;
-
-	self->priv = GGIT_CONFIG_GET_PRIVATE (self);
-
-	git_config_new (&config);
-	_ggit_native_set (self, config, (GDestroyNotify) git_config_free);
+	_ggit_native_set_destroy_func (config, (GDestroyNotify) git_config_free);
 }
 
 /**
  * ggit_config_new:
- * @file: (allow-none): a #GFile
  *
- * Create a new config for a given file. If %NULL is specified for @file, an
- * an empty configuration is created. See also ggit_config_get_global() to get
- * a config representing the global user configuration.
+ * Create a new config.See also ggit_config_get_default() to get
+ * a config representing the global, XDG and system configuration files.
  *
- * Returns: (transfer full): a #GgitConfig
+ * Returns: (transfer full): a #GgitConfig.
  *
  **/
 GgitConfig *
-ggit_config_new (GFile *file)
+ggit_config_new (void)
 {
-	return g_object_new (GGIT_TYPE_CONFIG,
-	                     "file", file,
-	                     NULL);
+	git_config *config;
+
+	git_config_new (&config);
+
+	return _ggit_config_wrap (config);
 }
 
 /**
- * ggit_config_get_global:
+ * ggit_config_get_default:
  *
- * Get the global config.
+ * Get the global, XDG and system configuration files.
  *
  * Returns: (transfer none): A #GgitConfig
  *
  **/
 GgitConfig *
-ggit_config_get_global (void)
+ggit_config_get_default (void)
 {
-	static GgitConfig *config = NULL;
+	static GgitConfig *default_config = NULL;
 
-	if (!config)
+	if (!default_config)
 	{
-		gchar global_path[GIT_PATH_MAX];
+		git_config *config;
 
-		if (git_config_find_global (global_path, GIT_PATH_MAX) == GIT_OK)
-		{
-			GFile *f;
-
-			f = g_file_new_for_path (global_path);
-			config = ggit_config_new (f);
-			g_object_unref (f);
-		}
-		else
+		if (git_config_open_default (&config) != GIT_OK)
 		{
-			config = ggit_config_new (NULL);
+			git_config_new (&config);
 		}
 
-		g_object_add_weak_pointer (G_OBJECT (config),
-		                           (gpointer *)&config);
+		default_config = _ggit_config_wrap (config);
+
+		g_object_add_weak_pointer (G_OBJECT (default_config),
+		                           (gpointer *)&default_config);
 	}
 
-	return config;
+	return default_config;
+}
+
+/**
+ * ggit_config_add_file:
+ * @config: a #GgitConfig.
+ * @file: a #GFile.
+ * @level: a #GgitConfigLevel.
+ * @force: if a config file already exists for the given priority level, replace it.
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Add an on-disk config file instance to an existing config
+ *
+ * The on-disk file pointed at by @file will be opened and
+ * parsed; it's expected to be a native Git config file following
+ * the default Git config syntax (see man git-config).
+ *
+ * Further queries on this config object will access each
+ * of the config file instances in order (instances with
+ * a higher priority level will be accessed first).
+ */
+void
+ggit_config_add_file (GgitConfig      *config,
+                      GFile           *file,
+                      GgitConfigLevel  level,
+                      gboolean         force,
+                      GError         **error)
+{
+	gint ret;
+	gchar *path;
+
+	g_return_if_fail (GGIT_IS_CONFIG (config));
+	g_return_if_fail (G_IS_FILE (file));
+	g_return_if_fail (error == NULL || *error == NULL);
+
+	path = g_file_get_path (file);
+	ret = git_config_add_file_ondisk (_ggit_native_get (config),
+	                                  path,
+	                                  level,
+	                                  force);
+	g_free (path);
+
+	if (ret != GIT_OK)
+	{
+		_ggit_error_set (error, ret);
+	}
 }
 
 /**
@@ -533,6 +482,26 @@ ggit_config_delete (GgitConfig   *config,
 	return TRUE;
 }
 
+static gint
+callback_wrapper (const git_config_entry *config_entry,
+                  gpointer                payload)
+{
+	CallbackWrapperData *wrapper_data = (CallbackWrapperData *)payload;
+	GgitConfigEntry *entry;
+	gint ret;
+
+	entry = _ggit_config_entry_wrap (config_entry);
+
+	if (wrapper_data->callback != NULL)
+	{
+		ret = wrapper_data->callback (entry, wrapper_data->user_data);
+	}
+
+	ggit_config_entry_unref (entry);
+
+	return ret;
+}
+
 /**
  * ggit_config_foreach:
  * @config: a #GgitConfig.
@@ -552,12 +521,16 @@ ggit_config_foreach (GgitConfig          *config,
                      GError             **error)
 {
 	gint ret;
+	CallbackWrapperData wrapper_data;
 
 	g_return_val_if_fail (GGIT_IS_CONFIG (config), FALSE);
 	g_return_val_if_fail (callback != NULL, FALSE);
 	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-	ret = git_config_foreach (_ggit_native_get (config), callback, user_data);
+	wrapper_data.user_data = user_data;
+	wrapper_data.callback = callback;
+
+	ret = git_config_foreach (_ggit_native_get (config), callback_wrapper, &wrapper_data);
 
 	if (ret != GIT_OK)
 	{
@@ -694,17 +667,4 @@ ggit_config_match_foreach (GgitConfig               *config,
 	                             error);
 }
 
-GgitConfig *
-_ggit_config_wrap (git_config *config)
-{
-	GgitConfig *ret;
-
-	g_return_val_if_fail (config != NULL, NULL);
-
-	ret = g_object_new (GGIT_TYPE_CONFIG, NULL);
-	_ggit_native_set (ret, config, (GDestroyNotify) git_config_free);
-
-	return ret;
-}
-
 /* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-config.h b/libgit2-glib/ggit-config.h
index 811d01b..35f5b88 100644
--- a/libgit2-glib/ggit-config.h
+++ b/libgit2-glib/ggit-config.h
@@ -38,13 +38,13 @@ G_BEGIN_DECLS
 #define GGIT_CONFIG_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GGIT_TYPE_CONFIG, GgitConfigClass))
 
 typedef struct _GgitConfigClass		GgitConfigClass;
-typedef struct _GgitConfigPrivate	GgitConfigPrivate;
 
 struct _GgitConfig
 {
 	GgitNative parent;
 
-	GgitConfigPrivate *priv;
+	/* priv padding */
+	gpointer priv;
 };
 
 /**
@@ -60,8 +60,14 @@ struct _GgitConfigClass
 
 GType        ggit_config_get_type      (void) G_GNUC_CONST;
 
-GgitConfig  *ggit_config_new           (GFile                    *file);
-GgitConfig  *ggit_config_get_global    (void);
+GgitConfig  *ggit_config_new           (void);
+GgitConfig  *ggit_config_get_default   (void);
+
+void         ggit_config_add_file      (GgitConfig               *config,
+                                        GFile                    *file,
+                                        GgitConfigLevel           level,
+                                        gboolean                  force,
+                                        GError                  **error);
 
 gint32       ggit_config_get_int32     (GgitConfig               *config,
                                         const gchar              *name,
diff --git a/libgit2-glib/ggit-types.c b/libgit2-glib/ggit-types.c
index 479e3b2..55ead72 100644
--- a/libgit2-glib/ggit-types.c
+++ b/libgit2-glib/ggit-types.c
@@ -24,6 +24,7 @@
 #include <git2/status.h>
 #include <git2/submodule.h>
 #include <git2/types.h>
+#include <git2/config.h>
 
 #include "ggit-types.h"
 
diff --git a/libgit2-glib/ggit-types.h b/libgit2-glib/ggit-types.h
index df46aab..237dd27 100644
--- a/libgit2-glib/ggit-types.h
+++ b/libgit2-glib/ggit-types.h
@@ -54,6 +54,13 @@ typedef struct _GgitCommitParents GgitCommitParents;
 typedef struct _GgitConfig GgitConfig;
 
 /**
+ * GgitConfigEntry:
+ *
+ * Represents a git configuration entry.
+ */
+typedef struct _GgitConfigEntry GgitConfigEntry;
+
+/**
  * GgitDiff:
  *
  * Represents a diff.
@@ -601,8 +608,7 @@ typedef gint (* GgitBranchesCallback) (const gchar    *branch_name,
 
 /**
  * GgitConfigCallback:
- * @name: the name of the configuration value
- * @value: the value
+ * @entry: a #GgitConfigEntry.
  * @data: (closure): user-supplied data.
  *
  * The type of the callback functions for retrieving values from a #GgitConfig.
@@ -611,9 +617,8 @@ typedef gint (* GgitBranchesCallback) (const gchar    *branch_name,
  * Returns: 0 to go for the next config value or a #GgitError in case there was
  *          an error.
  */
-typedef gint (* GgitConfigCallback) (const gchar *name,
-                                     const gchar *value,
-                                     gpointer     data);
+typedef gint (* GgitConfigCallback) (GgitConfigEntry *entry,
+                                     gpointer         data);
 
 /**
  * GgitConfigMatchCallback:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]