File: ggit-diff-hunk.c

package info (click to toggle)
libgit2-glib 0.99.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,692 kB
  • sloc: ansic: 17,155; python: 1,989; makefile: 21; sh: 7
file content (178 lines) | stat: -rw-r--r-- 3,848 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * 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 <string.h>

#include "ggit-diff-hunk.h"
#include "ggit-convert.h"

struct _GgitDiffHunk {
	gint ref_count;

	gint old_start;
	gint old_lines;
	gint new_start;
	gint new_lines;
	gchar *header;
};

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 = ggit_convert_utf8 (hunk->header, hunk->header_len, NULL);
	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: (transfer none) (nullable): a #GgitDiffHunk or %NULL.
 **/
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_free (hunk->header);
		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.
 */
gint
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_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: */