[libgit2-glib] Add GgitDiffHunk



commit d7b50cdf58ad1e25471923ccd79ac53a39c9bccb
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Thu Nov 21 17:37:30 2013 +0100

    Add GgitDiffHunk

 libgit2-glib/Makefile.am      |    2 +
 libgit2-glib/ggit-diff-hunk.c |  193 +++++++++++++++++++++++++++++++++++++++++
 libgit2-glib/ggit-diff-hunk.h |   49 +++++++++++
 libgit2-glib/ggit-diff.c      |    1 +
 4 files changed, 245 insertions(+), 0 deletions(-)
---
diff --git a/libgit2-glib/Makefile.am b/libgit2-glib/Makefile.am
index fef6467..8de0bd9 100644
--- a/libgit2-glib/Makefile.am
+++ b/libgit2-glib/Makefile.am
@@ -30,6 +30,7 @@ INST_H_FILES =                                \
        ggit-diff.h                     \
        ggit-diff-delta.h               \
        ggit-diff-file.h                \
+       ggit-diff-hunk.h                \
        ggit-diff-line.h                \
        ggit-diff-options.h             \
        ggit-diff-similarity-metric.h   \
@@ -84,6 +85,7 @@ C_FILES =                             \
        ggit-diff.c                     \
        ggit-diff-delta.c               \
        ggit-diff-file.c                \
+       ggit-diff-hunk.c                \
        ggit-diff-line.c                \
        ggit-diff-options.c             \
        ggit-diff-similarity-metric.c   \
diff --git a/libgit2-glib/ggit-diff-hunk.c b/libgit2-glib/ggit-diff-hunk.c
new file mode 100644
index 0000000..f88a101
--- /dev/null
+++ b/libgit2-glib/ggit-diff-hunk.c
@@ -0,0 +1,193 @@
+/*
+ * ggit-diff-hunk.c
+ * This file is part of libgit2-glib
+ *
+ * Copyright (C) 2012 - Garrett Regier
+ *
+ * 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 <glib-object.h>
+#include <git2.h>
+
+#include "ggit-diff-hunk.h"
+
+struct _GgitDiffHunk {
+       gint ref_count;
+
+       gint old_start;
+       gint old_lines;
+       gint new_start;
+       gint new_lines;
+       gsize header_len;
+       gchar header[128];
+};
+
+G_DEFINE_BOXED_TYPE (GgitDiffHunk, ggit_diff_hunk,
+                     ggit_diff_hunk_ref, ggit_diff_hunk_unref)
+
+
+GgitDiffHunk *
+_ggit_diff_hunk_wrap (const git_diff_hunk *hunk)
+{
+       GgitDiffHunk *ghunk;
+
+       g_return_val_if_fail (hunk != NULL, NULL);
+
+       ghunk = g_slice_new (GgitDiffHunk);
+       ghunk->ref_count = 1;
+       ghunk->old_start = hunk->old_start;
+       ghunk->old_lines = hunk->old_lines;
+       ghunk->new_start = hunk->new_start;
+       ghunk->new_lines = hunk->new_lines;
+       ghunk->header_len = hunk->header_len;
+       strncpy(ghunk->header, hunk->header, hunk->header_len);
+
+       return ghunk;
+}
+
+/**
+ * ggit_diff_hunk_ref:
+ * @hunk: a #GgitDiffHunk.
+ *
+ * Atomically increments the reference count of @hunk by one.
+ * This function is MT-safe and may be called from any thread.
+ *
+ * Returns: a #GgitDiffHunk.
+ **/
+GgitDiffHunk *
+ggit_diff_hunk_ref (GgitDiffHunk *hunk)
+{
+       g_return_val_if_fail (hunk != NULL, NULL);
+
+       g_atomic_int_inc (&hunk->ref_count);
+
+       return hunk;
+}
+
+/**
+ * ggit_diff_hunk_unref:
+ * @hunk: a #GgitDiffHunk.
+ *
+ * Atomically decrements the reference count of @hunk by one.
+ * If the reference count drops to 0, @hunk is freed.
+ **/
+void
+ggit_diff_hunk_unref (GgitDiffHunk *hunk)
+{
+       g_return_if_fail (hunk != NULL);
+
+       if (g_atomic_int_dec_and_test (&hunk->ref_count))
+       {
+               g_slice_free (GgitDiffHunk, hunk);
+       }
+}
+
+/**
+ * ggit_diff_hunk_get_old_start:
+ * @hunk: a #GgitDiffHunk.
+ *
+ * Gets the starting line number in the old file.
+ *
+ * Returns: the starting line number in the old file.
+ */
+gchar
+ggit_diff_hunk_get_old_start (GgitDiffHunk *hunk)
+{
+       g_return_val_if_fail (hunk != NULL, 0);
+
+       return hunk->old_start;
+}
+
+/**
+ * ggit_diff_hunk_get_old_lines:
+ * @hunk: a #GgitDiffHunk.
+ *
+ * Gets the number of lines in the old file.
+ *
+ * Returns: the number of lines in the old file.
+ */
+gint
+ggit_diff_hunk_get_old_lines (GgitDiffHunk *hunk)
+{
+       g_return_val_if_fail (hunk != NULL, 0);
+
+       return hunk->old_lines;
+}
+
+/**
+ * ggit_diff_hunk_get_new_start:
+ * @hunk: a #GgitDiffHunk.
+ *
+ * Gets the starting line number in the new file.
+ *
+ * Returns: the starting line number in the new file.
+ */
+gint
+ggit_diff_hunk_get_new_start (GgitDiffHunk *hunk)
+{
+       g_return_val_if_fail (hunk != NULL, 0);
+
+       return hunk->new_start;
+}
+
+/**
+ * ggit_diff_hunk_get_new_lines:
+ * @hunk: a #GgitDiffHunk.
+ *
+ * Gets the number of lines in the new file.
+ *
+ * Returns: the number of lines in the new file.
+ */
+gint
+ggit_diff_hunk_get_new_lines (GgitDiffHunk *hunk)
+{
+       g_return_val_if_fail (hunk != NULL, 0);
+
+       return hunk->new_lines;
+}
+
+/**
+ * ggit_diff_hunk_get_header_len:
+ * @hunk: a #GgitDiffHunk.
+ *
+ * Gets the header length.
+ *
+ * Returns: the header length.
+ */
+gsize
+ggit_diff_hunk_get_header_len (GgitDiffHunk *hunk)
+{
+       g_return_val_if_fail (hunk != NULL, 0);
+
+       return hunk->header_len;
+}
+
+/**
+ * ggit_diff_hunk_get_content:
+ * @hunk: a #GgitDiffHunk.
+ *
+ * Gets the header.
+ *
+ * Returns: the header.
+ */
+const gchar *
+ggit_diff_hunk_get_header (GgitDiffHunk *hunk)
+{
+       g_return_val_if_fail (hunk != NULL, 0);
+
+       return hunk->header;
+}
+
+/* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-diff-hunk.h b/libgit2-glib/ggit-diff-hunk.h
new file mode 100644
index 0000000..4d4b8e3
--- /dev/null
+++ b/libgit2-glib/ggit-diff-hunk.h
@@ -0,0 +1,49 @@
+/*
+ * ggit-diff-hunk.h
+ * This file is part of libgit2-glib
+ *
+ * Copyright (C) 2012 - Garrett Regier
+ *
+ * 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_HUNK_H__
+#define __GGIT_DIFF_HUNK_H__
+
+#include "ggit-types.h"
+
+G_BEGIN_DECLS
+
+#define GGIT_TYPE_DIFF_HUNK       (ggit_diff_hunk_get_type ())
+#define GGIT_DIFF_HUNK(obj)       ((GgitDiffHunk *)obj)
+
+GType           ggit_diff_hunk_get_type           (void) G_GNUC_CONST;
+
+GgitDiffHunk  *_ggit_diff_hunk_wrap               (const git_diff_hunk *hunk);
+
+GgitDiffHunk   *ggit_diff_hunk_ref                (GgitDiffHunk        *hunk);
+void            ggit_diff_hunk_unref              (GgitDiffHunk        *hunk);
+
+gchar           ggit_diff_hunk_get_old_start      (GgitDiffHunk        *hunk);
+gint            ggit_diff_hunk_get_old_lines      (GgitDiffHunk        *hunk);
+gint            ggit_diff_hunk_get_new_start      (GgitDiffHunk        *hunk);
+gint            ggit_diff_hunk_get_new_lines      (GgitDiffHunk        *hunk);
+gsize           ggit_diff_hunk_get_header_len     (GgitDiffHunk        *hunk);
+const gchar    *ggit_diff_hunk_get_header         (GgitDiffHunk        *hunk);
+
+G_END_DECLS
+
+#endif /* __GGIT_DIFF_HUNK_H__ */
+
+/* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-diff.c b/libgit2-glib/ggit-diff.c
index afd0ce9..84d87b4 100644
--- a/libgit2-glib/ggit-diff.c
+++ b/libgit2-glib/ggit-diff.c
@@ -24,6 +24,7 @@
 #include "ggit-diff-delta.h"
 #include "ggit-diff-options.h"
 #include "ggit-diff-line.h"
+#include "ggit-diff-hunk.h"
 #include "ggit-patch.h"
 #include "ggit-error.h"
 #include "ggit-repository.h"


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