[libgit2-glib] Handle ownership of refs.



commit f92bca0ee5c062214dfd51548d33c36696b4a8c3
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Wed Jan 21 22:24:48 2015 +0100

    Handle ownership of refs.
    
    Depending on where we are wrapping the reference we might need to free it.

 libgit2-glib/ggit-branch-enumerator.c |    2 +-
 libgit2-glib/ggit-branch.c            |    2 +-
 libgit2-glib/ggit-ref.c               |   25 ++++++++++++++++++-------
 libgit2-glib/ggit-ref.h               |    3 ++-
 libgit2-glib/ggit-repository.c        |    8 ++++----
 5 files changed, 26 insertions(+), 14 deletions(-)
---
diff --git a/libgit2-glib/ggit-branch-enumerator.c b/libgit2-glib/ggit-branch-enumerator.c
index 2ecdd89..0df57dd 100644
--- a/libgit2-glib/ggit-branch-enumerator.c
+++ b/libgit2-glib/ggit-branch-enumerator.c
@@ -98,7 +98,7 @@ ggit_branch_enumerator_next (GgitBranchEnumerator *enumerator)
        }
        else
        {
-               enumerator->ref = _ggit_ref_wrap (ref);
+               enumerator->ref = _ggit_ref_wrap (ref, FALSE);
        }
 
        return TRUE;
diff --git a/libgit2-glib/ggit-branch.c b/libgit2-glib/ggit-branch.c
index 9cee659..f29143f 100644
--- a/libgit2-glib/ggit-branch.c
+++ b/libgit2-glib/ggit-branch.c
@@ -178,7 +178,7 @@ ggit_branch_get_upstream (GgitBranch  *branch,
                return NULL;
        }
 
-       return _ggit_ref_wrap (upstream);
+       return _ggit_ref_wrap (upstream, FALSE);
 }
 
 /**
diff --git a/libgit2-glib/ggit-ref.c b/libgit2-glib/ggit-ref.c
index 8044d9e..76e4788 100644
--- a/libgit2-glib/ggit-ref.c
+++ b/libgit2-glib/ggit-ref.c
@@ -44,18 +44,29 @@ ggit_ref_init (GgitRef *self)
 }
 
 GgitRef *
-_ggit_ref_wrap (git_reference *ref)
+_ggit_ref_wrap (git_reference *ref,
+                gboolean       owned)
 {
+       GgitRef *gref;
+
        if (git_reference_is_branch (ref))
        {
-               return GGIT_REF (_ggit_branch_wrap (ref));
+               gref = GGIT_REF (_ggit_branch_wrap (ref));
        }
        else
        {
-               return g_object_new (GGIT_TYPE_REF,
+               gref = g_object_new (GGIT_TYPE_REF,
                                     "native", ref,
                                     NULL);
        }
+
+       if (owned)
+       {
+               _ggit_native_set_destroy_func (gref,
+                                              (GDestroyNotify)git_reference_free);
+       }
+
+       return gref;
 }
 
 /**
@@ -221,7 +232,7 @@ ggit_ref_resolve (GgitRef  *ref,
 
        if (ret == GIT_OK)
        {
-               rev_ref = _ggit_ref_wrap (reference);
+               rev_ref = _ggit_ref_wrap (reference, FALSE);
        }
        else
        {
@@ -293,7 +304,7 @@ ggit_ref_set_symbolic_target (GgitRef       *ref,
                return NULL;
        }
 
-       return _ggit_ref_wrap (out);
+       return _ggit_ref_wrap (out, FALSE);
 }
 
 /**
@@ -338,7 +349,7 @@ ggit_ref_set_target (GgitRef       *ref,
                return NULL;
        }
 
-       return _ggit_ref_wrap (out);
+       return _ggit_ref_wrap (out, FALSE);
 }
 
 /**
@@ -402,7 +413,7 @@ ggit_ref_rename (GgitRef       *ref,
                return NULL;
        }
 
-       return _ggit_ref_wrap (out);
+       return _ggit_ref_wrap (out, FALSE);
 }
 
 /**
diff --git a/libgit2-glib/ggit-ref.h b/libgit2-glib/ggit-ref.h
index 5696987..57a92bf 100644
--- a/libgit2-glib/ggit-ref.h
+++ b/libgit2-glib/ggit-ref.h
@@ -60,7 +60,8 @@ struct _GgitRefClass
 
 GType           ggit_ref_get_type           (void) G_GNUC_CONST;
 
-GgitRef        *_ggit_ref_wrap              (git_reference  *ref);
+GgitRef        *_ggit_ref_wrap              (git_reference  *ref,
+                                             gboolean        owned);
 
 gboolean        ggit_ref_is_valid_name      (const gchar    *name);
 
diff --git a/libgit2-glib/ggit-repository.c b/libgit2-glib/ggit-repository.c
index 6b889ff..0dbee2c 100644
--- a/libgit2-glib/ggit-repository.c
+++ b/libgit2-glib/ggit-repository.c
@@ -686,7 +686,7 @@ ggit_repository_lookup_reference (GgitRepository  *repository,
 
        if (ret == GIT_OK)
        {
-               ref = _ggit_ref_wrap (reference);
+               ref = _ggit_ref_wrap (reference, FALSE);
        }
        else
        {
@@ -737,7 +737,7 @@ ggit_repository_create_reference (GgitRepository  *repository,
 
        if (ret == GIT_OK)
        {
-               ref = _ggit_ref_wrap (reference);
+               ref = _ggit_ref_wrap (reference, FALSE);
        }
        else
        {
@@ -788,7 +788,7 @@ ggit_repository_create_symbolic_reference (GgitRepository  *repository,
 
        if (ret == GIT_OK)
        {
-               ref = _ggit_ref_wrap (reference);
+               ref = _ggit_ref_wrap (reference, FALSE);
        }
        else
        {
@@ -825,7 +825,7 @@ ggit_repository_get_head (GgitRepository  *repository,
 
        if (ret == GIT_OK)
        {
-               ref = _ggit_ref_wrap (reference);
+               ref = _ggit_ref_wrap (reference, TRUE);
        }
        else
        {


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