[libgit2-glib] Bind get_patch and patch_to_string



commit 37d4e5573079dab231e825f5fc98f7c3aa761c53
Author: Sindhu S <sindhus live in>
Date:   Thu Jun 27 15:25:50 2013 +0530

    Bind get_patch and patch_to_string

 libgit2-glib/Makefile.am       |    2 +
 libgit2-glib/ggit-diff-patch.c |  114 ++++++++++++++++++++++++++++++++++++++++
 libgit2-glib/ggit-diff-patch.h |   48 +++++++++++++++++
 libgit2-glib/ggit-diff.c       |   59 +++++++++++++++++++++
 libgit2-glib/ggit-diff.h       |    5 ++
 libgit2-glib/ggit-types.h      |    7 +++
 libgit2-glib/ggit.h            |    1 +
 7 files changed, 236 insertions(+), 0 deletions(-)
---
diff --git a/libgit2-glib/Makefile.am b/libgit2-glib/Makefile.am
index 9e6db90..26b7e4b 100644
--- a/libgit2-glib/Makefile.am
+++ b/libgit2-glib/Makefile.am
@@ -30,6 +30,7 @@ INST_H_FILES =                                \
        ggit-diff-delta.h               \
        ggit-diff-file.h                \
        ggit-diff-options.h             \
+       ggit-diff-patch.h               \
        ggit-diff-range.h               \
        ggit-error.h                    \
        ggit-index.h                    \
@@ -78,6 +79,7 @@ C_FILES =                             \
        ggit-diff-delta.c               \
        ggit-diff-file.c                \
        ggit-diff-options.c             \
+       ggit-diff-patch.c               \
        ggit-diff-range.c               \
        ggit-error.c                    \
        ggit-index.c                    \
diff --git a/libgit2-glib/ggit-diff-patch.c b/libgit2-glib/ggit-diff-patch.c
new file mode 100644
index 0000000..52411f6
--- /dev/null
+++ b/libgit2-glib/ggit-diff-patch.c
@@ -0,0 +1,114 @@
+/*
+ * ggit-diff-patch.c
+ * This file is part of libgit2-glib
+ *
+ * Copyright (C) 2013 - Sindhu S
+ *
+ * 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 "ggit-diff-patch.h"
+
+struct _GgitDiffPatch
+{
+       git_diff_patch *diff_patch;
+       gint ref_count;
+};
+
+G_DEFINE_BOXED_TYPE (GgitDiffPatch, ggit_diff_patch,
+                     ggit_diff_patch_ref, ggit_diff_patch_unref)
+
+GgitDiffPatch *
+_ggit_diff_patch_wrap (git_diff_patch *diff_patch)
+{
+       GgitDiffPatch *gdiff_patch;
+
+       gdiff_patch = g_slice_new (GgitDiffPatch);
+       gdiff_patch->diff_patch = diff_patch;
+       gdiff_patch->ref_count = 1;
+
+       return gdiff_patch;
+}
+
+/**
+ * ggit_diff_patch_ref:
+ * @diff_patch: a #GgitDiffPatch.
+ *
+ * Atomically increments the reference count of @entry by one.
+ * This function is MT-safe and may be called from any thread.
+ *
+ * Returns: (transfer none): a #GgitDiffPatch.
+ */
+GgitDiffPatch *
+ggit_diff_patch_ref (GgitDiffPatch *diff_patch)
+{
+       g_return_val_if_fail (diff_patch != NULL, NULL);
+
+       g_atomic_int_inc (&diff_patch->ref_count);
+
+       return diff_patch;
+}
+
+/**
+ * ggit_diff_patch_unref:
+ * @diff_patch: a #GgitDiffPatch.
+ *
+ * Atomically decrements the reference count of @entry by one.
+ * If the reference count drops to 0, @entry is freed.
+ */
+void
+ggit_diff_patch_unref (GgitDiffPatch *diff_patch)
+{
+       g_return_if_fail (diff_patch != NULL);
+
+       if (g_atomic_int_dec_and_test (&diff_patch->ref_count))
+       {
+               git_diff_patch_free (diff_patch->diff_patch);
+               g_slice_free (GgitDiffPatch, diff_patch);
+       }
+}
+
+/**
+ * ggit_diff_patch_to_string:
+ * @diff_patch: a #GgitDiffPatch.
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Gets the content of a patch as a single diff text.
+ *
+ * Returns: the content of a patch as a single diff text.
+ */
+gchar *
+ggit_diff_patch_to_string (GgitDiffPatch  *diff_patch,
+                           GError        **error)
+{
+       gchar *str;
+       gint ret;
+       gchar *gstr;
+
+       g_return_val_if_fail (diff_patch != NULL, NULL);
+
+       ret = git_diff_patch_to_str (&str, diff_patch->diff_patch);
+
+       if (ret != GIT_OK)
+       {
+               return NULL;
+       }
+
+       gstr = g_strdup (str);
+       free (str);
+
+       return gstr;
+}
+
+/* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-diff-patch.h b/libgit2-glib/ggit-diff-patch.h
new file mode 100644
index 0000000..2a6dde7
--- /dev/null
+++ b/libgit2-glib/ggit-diff-patch.h
@@ -0,0 +1,48 @@
+/*
+ * ggit-diff-patch.h
+ * This file is part of libgit2-glib
+ *
+ * Copyright (C) 2013 - Sindhu S
+ *
+ * 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/>.
+ */
+
+#ifndef __GGIT_DIFF_PATCH_H__
+#define __GGIT_DIFF_PATCH_H__
+
+#include <glib-object.h>
+#include <git2.h>
+#include "ggit-types.h"
+
+G_BEGIN_DECLS
+
+#define GGIT_TYPE_DIFF_PATCH       (ggit_diff_patch_get_type ())
+#define GGIT_DIFF_PATCH(obj)       ((GgitDiffPatch *)obj)
+
+GType           ggit_diff_patch_get_type        (void) G_GNUC_CONST;
+
+GgitDiffPatch   *_ggit_diff_patch_wrap          (git_diff_patch *diff_patch);
+
+GgitDiffPatch   *ggit_diff_patch_ref            (GgitDiffPatch    *diff_patch);
+
+void             ggit_diff_patch_unref          (GgitDiffPatch    *diff_patch);
+
+gchar           *ggit_diff_patch_to_string      (GgitDiffPatch    *diff_patch,
+                                                 GError          **error);
+
+G_END_DECLS
+
+#endif /* __GGIT_DIFF_PATCH_H__ */
+
+/* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-diff.c b/libgit2-glib/ggit-diff.c
index 0aafce1..c695b74 100644
--- a/libgit2-glib/ggit-diff.c
+++ b/libgit2-glib/ggit-diff.c
@@ -21,6 +21,7 @@
 #include <git2.h>
 
 #include "ggit-diff.h"
+#include "ggit-diff-patch.h"
 #include "ggit-diff-delta.h"
 #include "ggit-diff-options.h"
 #include "ggit-diff-range.h"
@@ -472,6 +473,64 @@ ggit_diff_print_patch (GgitDiff              *diff,
 }
 
 /**
+ * ggit_diff_get_patch:
+ * @diff: a #GgitDiff.
+ * @idx: index into @diff.
+ * @patch: (allow-none): a #GgitDiffPatch or %NULL.
+ * @delta: (allow-none): a #GgitDiffDelta or %NULL.
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Gets the diff delta and patch for an entry in @diff.
+ *
+ * The #GgitDiffPatch is a newly created object contains the text diffs
+ * for the delta.  You have to call git_diff_patch_unref() when you are
+ * done with it.  You can use the patch object to loop over all the hunks
+ * and lines in the diff of the one delta.
+ *
+ * For an unchanged file or a binary file, no #GgitDiffPatch will be
+ * created, the output will be set to %NULL, and the `binary` flag will be
+ * set true in @delta.
+ *
+ * It is okay to pass %NULL for either of the output parameters; if you pass
+ * %NULL for @patch, then the text diff will not be calculated.
+ */
+void
+ggit_diff_get_patch (GgitDiff       *diff,
+                     gsize           idx,
+                     GgitDiffPatch **patch,
+                     GgitDiffDelta **delta,
+                     GError        **error)
+{
+       gint ret;
+       const git_diff_delta *delta_out;
+       git_diff_patch *patch_out = NULL;
+
+       g_return_if_fail (GGIT_IS_DIFF (diff));
+       g_return_if_fail (error == NULL || *error == NULL);
+
+       ret = git_diff_get_patch (patch ? &patch_out : NULL,
+                                 delta ? &delta_out : NULL,
+                                 _ggit_native_get (diff),
+                                 idx);
+
+       if (ret != GIT_OK)
+       {
+               _ggit_error_set (error, ret);
+               return;
+       }
+
+       if (patch && patch_out)
+       {
+               *patch = _ggit_diff_patch_wrap (patch_out);
+       }
+
+       if (delta)
+       {
+               *delta = _ggit_diff_delta_wrap (delta_out);
+       }
+}
+
+/**
  * ggit_diff_blobs:
  * @old_blob: (allow-none): a #GgitBlob to diff from.
  * @old_as_path: (allow-none): treat @old_blob as if it had this filename, or %NULL,
diff --git a/libgit2-glib/ggit-diff.h b/libgit2-glib/ggit-diff.h
index 91b52a4..aeeafe3 100644
--- a/libgit2-glib/ggit-diff.h
+++ b/libgit2-glib/ggit-diff.h
@@ -99,6 +99,11 @@ void       ggit_diff_print_patch          (GgitDiff              *diff,
                                            GgitDiffLineCallback   print_cb,
                                            gpointer              *user_data,
                                            GError               **error);
+void       ggit_diff_get_patch            (GgitDiff              *diff,
+                                           gsize                  idx,
+                                           GgitDiffPatch        **patch,
+                                           GgitDiffDelta        **delta,
+                                           GError               **error);
 
 void       ggit_diff_blobs                (GgitBlob              *old_blob,
                                            const gchar           *old_as_path,
diff --git a/libgit2-glib/ggit-types.h b/libgit2-glib/ggit-types.h
index 9cdce96..1af2df4 100644
--- a/libgit2-glib/ggit-types.h
+++ b/libgit2-glib/ggit-types.h
@@ -103,6 +103,13 @@ typedef struct _GgitDiff GgitDiff;
 typedef struct _GgitDiffDelta GgitDiffDelta;
 
 /**
+ * GgitDiffPatch:
+ *
+ * Represents a patch object.
+ */
+typedef struct _GgitDiffPatch GgitDiffPatch;
+
+/**
  * GgitDiffFile:
  *
  * Represents a file in a #GgitDiff.
diff --git a/libgit2-glib/ggit.h b/libgit2-glib/ggit.h
index d308c60..8f6fd52 100644
--- a/libgit2-glib/ggit.h
+++ b/libgit2-glib/ggit.h
@@ -33,6 +33,7 @@
 #include <libgit2-glib/ggit-diff-delta.h>
 #include <libgit2-glib/ggit-diff-file.h>
 #include <libgit2-glib/ggit-diff-options.h>
+#include <libgit2-glib/ggit-diff-patch.h>
 #include <libgit2-glib/ggit-diff-range.h>
 #include <libgit2-glib/ggit-enum-types.h>
 #include <libgit2-glib/ggit-error.h>


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