[libgit2-glib] Bind ggit_tree_entry_by_path



commit c0b8654991e3e527b311a5308af7829d9c7031d3
Author: Ignacio Casal Quinteiro <ignacio casal nice-software com>
Date:   Sun Jan 13 23:34:48 2013 +0100

    Bind ggit_tree_entry_by_path

 libgit2-glib/ggit-tree-entry.c |   13 +++++++++-
 libgit2-glib/ggit-tree-entry.h |    3 +-
 libgit2-glib/ggit-tree.c       |   47 ++++++++++++++++++++++++++++++++++-----
 libgit2-glib/ggit-tree.h       |    3 ++
 4 files changed, 57 insertions(+), 9 deletions(-)
---
diff --git a/libgit2-glib/ggit-tree-entry.c b/libgit2-glib/ggit-tree-entry.c
index fdecaa1..5933817 100644
--- a/libgit2-glib/ggit-tree-entry.c
+++ b/libgit2-glib/ggit-tree-entry.c
@@ -21,10 +21,12 @@
 #include "ggit-tree-entry.h"
 #include "ggit-oid.h"
 #include "ggit-utils.h"
+#include <git2/errors.h>
 
 struct _GgitTreeEntry
 {
-	const git_tree_entry *entry;
+	git_tree_entry *entry;
+	gboolean free_entry;
 	gint ref_count;
 };
 
@@ -34,12 +36,14 @@ G_DEFINE_BOXED_TYPE (GgitTreeEntry,
                      ggit_tree_entry_unref)
 
 GgitTreeEntry *
-_ggit_tree_entry_wrap (const git_tree_entry *entry)
+_ggit_tree_entry_wrap (git_tree_entry *entry,
+                       gboolean        free_entry)
 {
 	GgitTreeEntry *ret;
 
 	ret = g_slice_new (GgitTreeEntry);
 	ret->entry = entry;
+	ret->free_entry = free_entry;
 	ret->ref_count = 1;
 
 	return ret;
@@ -78,6 +82,11 @@ ggit_tree_entry_unref (GgitTreeEntry *entry)
 
 	if (g_atomic_int_dec_and_test (&entry->ref_count))
 	{
+		if (entry->free_entry)
+		{
+			git_tree_entry_free (entry->entry);
+		}
+
 		g_slice_free (GgitTreeEntry, entry);
 	}
 }
diff --git a/libgit2-glib/ggit-tree-entry.h b/libgit2-glib/ggit-tree-entry.h
index 5552fa1..053b04b 100644
--- a/libgit2-glib/ggit-tree-entry.h
+++ b/libgit2-glib/ggit-tree-entry.h
@@ -32,7 +32,8 @@ G_BEGIN_DECLS
 
 GType          ggit_tree_entry_get_type        (void) G_GNUC_CONST;
 
-GgitTreeEntry *_ggit_tree_entry_wrap           (const git_tree_entry *entry);
+GgitTreeEntry *_ggit_tree_entry_wrap           (git_tree_entry       *entry,
+                                                gboolean              free_entry);
 GgitTreeEntry *ggit_tree_entry_ref             (GgitTreeEntry        *entry);
 void           ggit_tree_entry_unref           (GgitTreeEntry        *entry);
 
diff --git a/libgit2-glib/ggit-tree.c b/libgit2-glib/ggit-tree.c
index 101ca7b..e187163 100644
--- a/libgit2-glib/ggit-tree.c
+++ b/libgit2-glib/ggit-tree.c
@@ -102,7 +102,7 @@ ggit_tree_get (GgitTree *tree,
 
 	entry = git_tree_entry_byindex (t, i);
 
-	return _ggit_tree_entry_wrap (entry);
+	return _ggit_tree_entry_wrap ((git_tree_entry *)entry, FALSE);
 }
 
 /**
@@ -127,11 +127,11 @@ ggit_tree_size (GgitTree *tree)
 }
 
 /**
- * ggit_tree_get_by_file:
+ * ggit_tree_get_by_name:
  * @tree: a #GgitTree.
- * @file: a #GFile.
+ * @name: a filename.
  *
- * Get a tree entry by file.
+ * Get a tree entry by name.
  *
  * Returns: (transfer full): a #GgitTreeEntry.
  *
@@ -153,7 +153,42 @@ ggit_tree_get_by_name (GgitTree    *tree,
 
 	if (tree_entry != NULL)
 	{
-		entry = _ggit_tree_entry_wrap (tree_entry);
+		entry = _ggit_tree_entry_wrap ((git_tree_entry *)tree_entry, FALSE);
+	}
+
+	return entry;
+}
+
+/**
+ * ggit_tree_get_by_path:
+ * @tree: a #GgitTree.
+ * @path: a path.
+ *
+ * Retrieves a tree entry contained in a tree or in any of its subtrees,
+ * given its relative path.
+ *
+ * Returns: (transfer full): a #GgitTreeEntry.
+ *
+ **/
+GgitTreeEntry *
+ggit_tree_get_by_path (GgitTree    *tree,
+                       const gchar *path)
+{
+	git_tree *t;
+	GgitTreeEntry *entry = NULL;
+	git_tree_entry *tree_entry;
+	gint ret;
+
+	g_return_val_if_fail (GGIT_IS_TREE (tree), NULL);
+	g_return_val_if_fail (path != NULL, NULL);
+
+	t = _ggit_native_get (tree);
+
+	ret = git_tree_entry_bypath (&tree_entry, t, path);
+
+	if (ret == GIT_OK)
+	{
+		entry = _ggit_tree_entry_wrap (tree_entry, TRUE);
 	}
 
 	return entry;
@@ -174,7 +209,7 @@ walk_callback_wrapper (const char           *root,
 	GgitTreeEntry *wentry;
 	WalkInfo *info = (WalkInfo *)payload;
 
-	wentry = _ggit_tree_entry_wrap (entry);
+	wentry = _ggit_tree_entry_wrap ((git_tree_entry *)entry, FALSE);
 
 	ret = info->callback(root, wentry, info->user_data);
 
diff --git a/libgit2-glib/ggit-tree.h b/libgit2-glib/ggit-tree.h
index 327755e..e3e646a 100644
--- a/libgit2-glib/ggit-tree.h
+++ b/libgit2-glib/ggit-tree.h
@@ -72,6 +72,9 @@ guint          ggit_tree_size           (GgitTree *tree);
 GgitTreeEntry *ggit_tree_get_by_name    (GgitTree    *tree,
                                          const gchar *name);
 
+GgitTreeEntry *ggit_tree_get_by_path    (GgitTree    *tree,
+                                         const gchar *path);
+
 void           ggit_tree_walk           (GgitTree              *tree,
                                          GgitTreeWalkMode       mode,
                                          GgitTreeWalkCallback   callback,



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