1 /*
2  * ggit-diff-delta.c
3  * This file is part of libgit2-glib
4  *
5  * Copyright (C) 2012 - Garrett Regier
6  *
7  * libgit2-glib is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * libgit2-glib is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with libgit2-glib. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <glib-object.h>
22 
23 #include "ggit-diff-delta.h"
24 #include "ggit-diff-file.h"
25 
26 G_BEGIN_DECLS
27 
28 struct _GgitDiffDelta {
29 	gint ref_count;
30 
31 	GgitDiffFile *old_file;
32 	GgitDiffFile *new_file;
33 	GgitDeltaType status;
34 	guint32 similarity;
35 	guint32 flags;
36 };
37 
G_DEFINE_BOXED_TYPE(GgitDiffDelta,ggit_diff_delta,ggit_diff_delta_ref,ggit_diff_delta_unref)38 G_DEFINE_BOXED_TYPE (GgitDiffDelta, ggit_diff_delta,
39                      ggit_diff_delta_ref, ggit_diff_delta_unref)
40 
41 GgitDiffDelta *
42 _ggit_diff_delta_wrap (const git_diff_delta *delta)
43 {
44 	GgitDiffDelta *gdelta;
45 
46 	g_return_val_if_fail (delta != NULL, NULL);
47 
48 	gdelta = g_slice_new (GgitDiffDelta);
49 	gdelta->ref_count = 1;
50 	gdelta->old_file = _ggit_diff_file_wrap (&delta->old_file);
51 	gdelta->new_file = _ggit_diff_file_wrap (&delta->new_file);
52 	gdelta->status = (GgitDeltaType)delta->status;
53 	gdelta->similarity = delta->similarity;
54 	gdelta->flags = delta->flags;
55 
56 	return gdelta;
57 }
58 
59 /**
60  * ggit_diff_delta_ref:
61  * @delta: a #GgitDiffDelta.
62  *
63  * Atomically increments the reference count of @delta by one.
64  * This function is MT-safe and may be called from any thread.
65  *
66  * Returns: (transfer none) (nullable): a #GgitDiffDelta or %NULL.
67  **/
68 GgitDiffDelta *
ggit_diff_delta_ref(GgitDiffDelta * delta)69 ggit_diff_delta_ref (GgitDiffDelta *delta)
70 {
71 	g_return_val_if_fail (delta != NULL, NULL);
72 
73 	g_atomic_int_inc (&delta->ref_count);
74 
75 	return delta;
76 }
77 
78 /**
79  * ggit_diff_delta_unref:
80  * @delta: a #GgitDiffDelta.
81  *
82  * Atomically decrements the reference count of @delta by one.
83  * If the reference count drops to 0, @delta is freed.
84  **/
85 void
ggit_diff_delta_unref(GgitDiffDelta * delta)86 ggit_diff_delta_unref (GgitDiffDelta *delta)
87 {
88 	g_return_if_fail (delta != NULL);
89 
90 	if (g_atomic_int_dec_and_test (&delta->ref_count))
91 	{
92 		ggit_diff_file_unref (delta->old_file);
93 		ggit_diff_file_unref (delta->new_file);
94 		g_slice_free (GgitDiffDelta, delta);
95 	}
96 }
97 
98 /**
99  * ggit_diff_delta_get_old_file:
100  * @delta: a #GgitDiffDelta.
101  *
102  * Gets the old file for @delta.
103  *
104  * Returns: (transfer none) (nullable): the delta's old file or %NULL.
105  */
106 GgitDiffFile *
ggit_diff_delta_get_old_file(GgitDiffDelta * delta)107 ggit_diff_delta_get_old_file (GgitDiffDelta *delta)
108 {
109 	g_return_val_if_fail (delta != NULL, NULL);
110 
111 	return delta->old_file;
112 }
113 
114 /**
115  * ggit_diff_delta_get_new_file:
116  * @delta: a #GgitDiffDelta.
117  *
118  * Gets the new file for @delta.
119  *
120  * Returns: (transfer none) (nullable): the delta's new file or %NULL.
121  */
122 GgitDiffFile *
ggit_diff_delta_get_new_file(GgitDiffDelta * delta)123 ggit_diff_delta_get_new_file (GgitDiffDelta *delta)
124 {
125 	g_return_val_if_fail (delta != NULL, NULL);
126 
127 	return delta->new_file;
128 }
129 
130 /**
131  * ggit_diff_delta_get_status:
132  * @delta: a #GgitDiffDelta.
133  *
134  * Gets the #GgitDeltaType for @delta.
135  *
136  * Returns: the delta's status.
137  */
138 GgitDeltaType
ggit_diff_delta_get_status(GgitDiffDelta * delta)139 ggit_diff_delta_get_status (GgitDiffDelta *delta)
140 {
141 	g_return_val_if_fail (delta != NULL, 0);
142 
143 	return delta->status;
144 }
145 
146 /**
147  * ggit_diff_delta_get_similarity:
148  * @delta: a #GgitDiffDelta.
149  *
150  * Gets the similarity between @delta files.
151  *
152  * Returns: the delta's similarity.
153  */
154 guint
ggit_diff_delta_get_similarity(GgitDiffDelta * delta)155 ggit_diff_delta_get_similarity (GgitDiffDelta *delta)
156 {
157 	g_return_val_if_fail (delta != NULL, 0);
158 
159 	return delta->similarity;
160 }
161 
162 /**
163  * ggit_diff_delta_get_flags:
164  * @delta: a #GgitDiffDelta.
165  *
166  * Gets flags for @delta.
167  *
168  * Returns: the delta flags
169  */
170 GgitDiffFlag
ggit_diff_delta_get_flags(GgitDiffDelta * delta)171 ggit_diff_delta_get_flags (GgitDiffDelta *delta)
172 {
173 	g_return_val_if_fail (delta != NULL, 0);
174 
175 	return delta->flags;
176 }
177 
178 /* ex:set ts=8 noet: */
179