deps/libgit2/src/libgit2/patch.c

filter: Statementstmt 60.7% 65 / 107 Branchbran 41.5% 44 / 106 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 51.1% 109 / 213 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/patch_parse.c ↑ Summary deps/libgit2/deps/zlib/adler32.c »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7  
8 #include "patch.h"
9  
10 #include "git2/patch.h"
11 #include "diff.h"
12  
13 42 int git_patch__invoke_callbacks(
14 git_patch *patch,
15 git_diff_file_cb file_cb,
16 git_diff_binary_cb binary_cb,
17 git_diff_hunk_cb hunk_cb,
18 git_diff_line_cb line_cb,
19 void *payload)
20 {
21 42 int error = 0;
22 uint32_t i, j;
23  
24 42 ▶ if (file_cb)
Branch: 1 branch
BranchTrueFalseTotal
if (file_cb) 42 0 42
25 42 error = file_cb(patch->delta, 0, payload);
26  
27 42 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 1 41 42
28 1 return error;
29  
30 41 ▶ if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0) { 1 40 41
31 1 ▶ if (binary_cb)
Branch: 1 branch
BranchTrueFalseTotal
if (binary_cb) 1 0 1
32 1 error = binary_cb(patch->delta, &patch->binary, payload);
33  
34 1 return error;
35 }
36  
37 40 ▶ if (!hunk_cb && !line_cb)
Branch: 2 branches
BranchTrueFalseTotal
if (!hunk_cb && !line_cb) 22 18 40
if (!hunk_cb && !line_cb) 20 2 22
38 20 return error;
39  
40 39 ▶ for (i = 0; !error && i < git_array_size(patch->hunks); ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; !error && i < git_array_size(patch->hunks); ++i) { 39 0 39
for (i = 0; !error && i < git_array_size(patch->hunks); ++i) { 19 20 39
41 19 ▶ git_patch_hunk *h = git_array_get(patch->hunks, i);
Branch: 1 branch
BranchTrueFalseTotal
git_patch_hunk *h = git_array_get(patch->hunks, i); 19 0 19
42  
43 19 ▶ if (hunk_cb)
Branch: 1 branch
BranchTrueFalseTotal
if (hunk_cb) 17 2 19
44 17 error = hunk_cb(patch->delta, &h->hunk, payload);
45  
46 19 ▶ if (!line_cb)
Branch: 1 branch
BranchTrueFalseTotal
if (!line_cb) 0 19 19
47 0 continue;
48  
49 57 ▶ for (j = 0; !error && j < h->line_count; ++j) {
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; !error && j < h->line_count; ++j) { 57 0 57
for (j = 0; !error && j < h->line_count; ++j) { 38 19 57
50 38 git_diff_line *l =
51 38 ▶ git_array_get(patch->lines, h->line_start + j);
Branch: 1 branch
BranchTrueFalseTotal
git_array_get(patch->lines, h->line_start + j); 38 0 38
52  
53 38 error = line_cb(patch->delta, &h->hunk, l, payload);
54 }
55 }
56  
57 20 return error;
58 }
59  
60 0 size_t git_patch_size(
61 git_patch *patch,
62 int include_context,
63 int include_hunk_headers,
64 int include_file_headers)
65 {
66 size_t out;
67  
68 0 ▶ GIT_ASSERT_ARG(patch);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(patch); 0 0 0
69  
70 0 out = patch->content_size;
71  
72 0 ▶ if (!include_context)
Branch: 1 branch
BranchTrueFalseTotal
if (!include_context) 0 0 0
73 0 out -= patch->context_size;
74  
75 0 ▶ if (include_hunk_headers)
Branch: 1 branch
BranchTrueFalseTotal
if (include_hunk_headers) 0 0 0
76 0 out += patch->header_size;
77  
78 0 ▶ if (include_file_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (include_file_headers) { 0 0 0
79 0 git_str file_header = GIT_STR_INIT;
80  
81 0 ▶ if (git_diff_delta__format_file_header(
Branch: 1 branch
BranchTrueFalseTotal
if (git_diff_delta__format_file_header( 0 0 0
82 0 &file_header, patch->delta, NULL, NULL, 0, true) < 0)
83 0 git_error_clear();
84 else
85 0 out += git_str_len(&file_header);
86  
87 0 git_str_dispose(&file_header);
88 }
89  
90 0 return out;
91 }
92  
93 8 int git_patch_line_stats(
94 size_t *total_ctxt,
95 size_t *total_adds,
96 size_t *total_dels,
97 const git_patch *patch)
98 {
99 size_t totals[3], idx;
100  
101 8 memset(totals, 0, sizeof(totals));
102  
103 18 ▶ for (idx = 0; idx < git_array_size(patch->lines); ++idx) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx = 0; idx < git_array_size(patch->lines); ++idx) { 10 8 18
104 10 ▶ git_diff_line *line = git_array_get(patch->lines, idx);
Branch: 1 branch
BranchTrueFalseTotal
git_diff_line *line = git_array_get(patch->lines, idx); 10 0 10
105 10 ▶ if (!line)
Branch: 1 branch
BranchTrueFalseTotal
if (!line) 0 10 10
106 0 continue;
107  
108 10 switch (line->origin) {
109 0 case GIT_DIFF_LINE_CONTEXT: totals[0]++; break;
110 6 case GIT_DIFF_LINE_ADDITION: totals[1]++; break;
111 1 case GIT_DIFF_LINE_DELETION: totals[2]++; break;
112 3 default:
113 /* diff --stat and --numstat don't count EOFNL marks because
114 * they will always be paired with a ADDITION or DELETION line.
115 */
116 3 break;
117 }
118 }
119  
120 8 ▶ if (total_ctxt)
Branch: 1 branch
BranchTrueFalseTotal
if (total_ctxt) 2 6 8
121 2 *total_ctxt = totals[0];
122 8 ▶ if (total_adds)
Branch: 1 branch
BranchTrueFalseTotal
if (total_adds) 8 0 8
123 8 *total_adds = totals[1];
124 8 ▶ if (total_dels)
Branch: 1 branch
BranchTrueFalseTotal
if (total_dels) 8 0 8
125 8 *total_dels = totals[2];
126  
127 8 return 0;
128 }
129  
130 3 const git_diff_delta *git_patch_get_delta(const git_patch *patch)
131 {
132 3 ▶ GIT_ASSERT_ARG_WITH_RETVAL(patch, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(patch, NULL); 0 3 3
133 3 return patch->delta;
134 }
135  
136 10 size_t git_patch_num_hunks(const git_patch *patch)
137 {
138 10 ▶ GIT_ASSERT_ARG(patch);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(patch); 0 10 10
139 10 return git_array_size(patch->hunks);
140 }
141  
142 0 static int patch_error_outofrange(const char *thing)
143 {
144 0 git_error_set(GIT_ERROR_INVALID, "patch %s index out of range", thing);
145 0 return GIT_ENOTFOUND;
146 }
147  
148 4 int git_patch_get_hunk(
149 const git_diff_hunk **out,
150 size_t *lines_in_hunk,
151 git_patch *patch,
152 size_t hunk_idx)
153 {
154 git_patch_hunk *hunk;
155 4 ▶ GIT_ASSERT_ARG(patch);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(patch); 0 4 4
156  
157 4 ▶ hunk = git_array_get(patch->hunks, hunk_idx);
Branch: 1 branch
BranchTrueFalseTotal
hunk = git_array_get(patch->hunks, hunk_idx); 4 0 4
158  
159 4 ▶ if (!hunk) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hunk) { 0 4 4
160 0 ▶ if (out) *out = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (out) *out = NULL; 0 0 0
161 0 ▶ if (lines_in_hunk) *lines_in_hunk = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (lines_in_hunk) *lines_in_hunk = 0; 0 0 0
162 0 return patch_error_outofrange("hunk");
163 }
164  
165 4 ▶ if (out) *out = &hunk->hunk;
Branch: 1 branch
BranchTrueFalseTotal
if (out) *out = &hunk->hunk; 4 0 4
166 4 ▶ if (lines_in_hunk) *lines_in_hunk = hunk->line_count;
Branch: 1 branch
BranchTrueFalseTotal
if (lines_in_hunk) *lines_in_hunk = hunk->line_count; 0 4 4
167 4 return 0;
168 }
169  
170 0 int git_patch_num_lines_in_hunk(const git_patch *patch, size_t hunk_idx)
171 {
172 git_patch_hunk *hunk;
173 0 ▶ GIT_ASSERT_ARG(patch);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(patch); 0 0 0
174  
175 0 ▶ if (!(hunk = git_array_get(patch->hunks, hunk_idx)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(hunk = git_array_get(patch->hunks, hunk_idx))) 0 0 0
if (!(hunk = git_array_get(patch->hunks, hunk_idx))) 0 0 0
176 0 return patch_error_outofrange("hunk");
177 0 return (int)hunk->line_count;
178 }
179  
180 0 int git_patch_get_line_in_hunk(
181 const git_diff_line **out,
182 git_patch *patch,
183 size_t hunk_idx,
184 size_t line_of_hunk)
185 {
186 git_patch_hunk *hunk;
187 git_diff_line *line;
188  
189 0 ▶ GIT_ASSERT_ARG(patch);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(patch); 0 0 0
190  
191 0 ▶ if (!(hunk = git_array_get(patch->hunks, hunk_idx))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(hunk = git_array_get(patch->hunks, hunk_idx))) { 0 0 0
if (!(hunk = git_array_get(patch->hunks, hunk_idx))) { 0 0 0
192 0 ▶ if (out) *out = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (out) *out = NULL; 0 0 0
193 0 return patch_error_outofrange("hunk");
194 }
195  
196 0 ▶ if (line_of_hunk >= hunk->line_count ||
Branch: 1 branch
BranchTrueFalseTotal
if (line_of_hunk >= hunk->line_count || 0 0 0
197 0 ▶ !(line = git_array_get(
Branch: 2 branches
BranchTrueFalseTotal
!(line = git_array_get( 0 0 0
!(line = git_array_get( 0 0 0
198 patch->lines, hunk->line_start + line_of_hunk))) {
199 0 ▶ if (out) *out = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (out) *out = NULL; 0 0 0
200 0 return patch_error_outofrange("line");
201 }
202  
203 0 ▶ if (out) *out = line;
Branch: 1 branch
BranchTrueFalseTotal
if (out) *out = line; 0 0 0
204 0 return 0;
205 }
206  
207 0 git_repository *git_patch_owner(const git_patch *patch)
208 {
209 0 return patch->repo;
210 }
211  
212 51 int git_patch_from_diff(git_patch **out, git_diff *diff, size_t idx)
213 {
214 51 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 51 51
215 51 ▶ GIT_ASSERT_ARG(diff);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(diff); 0 51 51
216 51 ▶ GIT_ASSERT_ARG(diff->patch_fn);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(diff->patch_fn); 0 51 51
217 51 return diff->patch_fn(out, diff, idx);
218 }
219  
220 52 static void git_patch__free(git_patch *patch)
221 {
222 52 ▶ if (patch->free_fn)
Branch: 1 branch
BranchTrueFalseTotal
if (patch->free_fn) 52 0 52
223 52 patch->free_fn(patch);
224 52 }
225  
226 54 void git_patch_free(git_patch *patch)
227 {
228 54 ▶ if (patch)
Branch: 1 branch
BranchTrueFalseTotal
if (patch) 54 0 54
229 54 ▶ GIT_REFCOUNT_DEC(patch, git_patch__free);
Branch: 2 branches
BranchTrueFalseTotal
GIT_REFCOUNT_DEC(patch, git_patch__free); 52 2 54
GIT_REFCOUNT_DEC(patch, git_patch__free); 52 0 52
230 54 }
« deps/libgit2/src/libgit2/patch_parse.c ↑ Summary deps/libgit2/deps/zlib/adler32.c »