[libgit2-glib] Made config API a little bit more like the original libgit2 API



commit 52a7a65e6d82a648b8ef23f7758318d22a09f608
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date:   Tue Oct 30 10:35:09 2012 +0100

    Made config API a little bit more like the original libgit2 API
    
    There are now 3 constructors:
    ggit_config_new
    ggit_config_new_default
    ggit_config_new_from_file
    
    ggit_config_new_default replaces ggit_config_new

 libgit2-glib/ggit-config.c |   79 ++++++++++++++++++++++++++++++++++---------
 libgit2-glib/ggit-config.h |    6 +++-
 2 files changed, 67 insertions(+), 18 deletions(-)
---
diff --git a/libgit2-glib/ggit-config.c b/libgit2-glib/ggit-config.c
index 2716f13..08854f8 100644
--- a/libgit2-glib/ggit-config.c
+++ b/libgit2-glib/ggit-config.c
@@ -72,8 +72,10 @@ ggit_config_init (GgitConfig *config)
 /**
  * ggit_config_new:
  *
- * Create a new config.See also ggit_config_get_default() to get
- * a config representing the global, XDG and system configuration files.
+ * Create a new config. See also ggit_config_get_default() to get
+ * a #GgitConfig representing the global, XDG and system configuration files.
+ * To get a #GgitConfig for a repository use #ggit_repository_get_config
+ * instead.
  *
  * Returns: (transfer full): a #GgitConfig.
  *
@@ -89,34 +91,77 @@ ggit_config_new (void)
 }
 
 /**
- * ggit_config_get_default:
+ * ggit_config_new_from_file:
+ * @file: the file to load
+ * @error: a #GError for error reporting, or %NULL.
  *
- * Get the global, XDG and system configuration files.
+ * Create a new config from a single on disk file. This is a convenience
+ * API and is exactly the same as creating an empty #GgitConfig using
+ * #ggit_config_new and adding the file with #ggit_config_add_file. The
+ * level will be set to #GGIT_CONFIG_LEVEL_LOCAL. If the config could not be
+ * loaded this function returns %NULL and @error will be set accordingly.
  *
- * Returns: (transfer none): A #GgitConfig
+ * Returns: (transfer full): a #GgitConfig.
  *
  **/
 GgitConfig *
-ggit_config_get_default (void)
+ggit_config_new_from_file (GFile   *file,
+                           GError **error)
 {
-	static GgitConfig *default_config = NULL;
+	git_config *config;
+	gchar *path;
+	gint ret;
+
+	g_return_val_if_fail (G_IS_FILE (file), NULL);
+
+	path = g_file_get_path (file);
+
+	g_return_val_if_fail (path != NULL, NULL);
 
-	if (!default_config)
+	ret = git_config_open_ondisk (&config, path);
+	g_free (path);
+
+	if (ret != GIT_OK)
+	{
+		_ggit_error_set (error, ret);
+		return NULL;
+	}
+	else
 	{
-		git_config *config;
+		return _ggit_config_wrap (config);
+	}
+}
 
-		if (git_config_open_default (&config) != GIT_OK)
-		{
-			git_config_new (&config);
-		}
+/**
+ * ggit_config_new_default:
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Get the global, XDG and system configuration files merged into one
+ * #GgitConfig with their appropriate priority levels. If an error occured
+ * trying to load the various configuration files, this function will return
+ * %NULL and @error will be set accordingly.
+ *
+ * Returns: (transfer none): A #GgitConfig
+ *
+ **/
+GgitConfig *
+ggit_config_new_default (GError **error)
+{
+	git_config *config;
+	gint ret;
 
-		default_config = _ggit_config_wrap (config);
+	ret = git_config_open_default (&config);
 
-		g_object_add_weak_pointer (G_OBJECT (default_config),
-		                           (gpointer *)&default_config);
+	if (ret != GIT_OK)
+	{
+		_ggit_error_set (error, ret);
+		return NULL;
+	}
+	else
+	{
+		return _ggit_config_wrap (config);
 	}
 
-	return default_config;
 }
 
 /**
diff --git a/libgit2-glib/ggit-config.h b/libgit2-glib/ggit-config.h
index 35f5b88..fa9d93b 100644
--- a/libgit2-glib/ggit-config.h
+++ b/libgit2-glib/ggit-config.h
@@ -61,7 +61,11 @@ struct _GgitConfigClass
 GType        ggit_config_get_type      (void) G_GNUC_CONST;
 
 GgitConfig  *ggit_config_new           (void);
-GgitConfig  *ggit_config_get_default   (void);
+
+GgitConfig  *ggit_config_new_default   (GError                 **error);
+
+GgitConfig  *ggit_config_new_from_file (GFile                   *file,
+                                        GError                 **error);
 
 void         ggit_config_add_file      (GgitConfig               *config,
                                         GFile                    *file,



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