deps/libgit2/src/libgit2/diff_print.c

filter: Statementstmt 73.8% 327 / 443 Branchbran 53.2% 132 / 248 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.4% 459 / 691 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/filter.c ↑ Summary xs/Cert/X509.xs »
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 "common.h"
9  
10 #include "buf.h"
11 #include "diff.h"
12 #include "diff_file.h"
13 #include "patch_generate.h"
14 #include "futils.h"
15 #include "zstream.h"
16 #include "blob.h"
17 #include "delta.h"
18 #include "git2/sys/diff.h"
19  
20 typedef struct {
21 git_diff_format_t format;
22 git_diff_line_cb print_cb;
23 void *payload;
24  
25 git_str *buf;
26 git_diff_line line;
27  
28 const char *old_prefix;
29 const char *new_prefix;
30 uint32_t flags;
31 int id_strlen;
32  
33 int (*strcomp)(const char *, const char *);
34 } diff_print_info;
35  
36 24 static int diff_print_info_init__common(
37 diff_print_info *pi,
38 git_str *out,
39 git_repository *repo,
40 git_diff_format_t format,
41 git_diff_line_cb cb,
42 void *payload)
43 {
44 24 pi->format = format;
45 24 pi->print_cb = cb;
46 24 pi->payload = payload;
47 24 pi->buf = out;
48  
49 24 ▶ if (!pi->id_strlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pi->id_strlen) { 24 0 24
50 24 ▶ if (!repo)
Branch: 1 branch
BranchTrueFalseTotal
if (!repo) 1 23 24
51 1 pi->id_strlen = GIT_ABBREV_DEFAULT;
52 23 ▶ else if (git_repository__configmap_lookup(&pi->id_strlen, repo, GIT_CONFIGMAP_ABBREV) < 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (git_repository__configmap_lookup(&pi->id_strlen, repo, GIT_CONFIGMAP_ABBREV) < 0) 0 23 23
53 0 return -1;
54 }
55  
56 24 ▶ if (pi->id_strlen > GIT_OID_HEXSZ)
Branch: 1 branch
BranchTrueFalseTotal
if (pi->id_strlen > GIT_OID_HEXSZ) 0 24 24
57 0 pi->id_strlen = GIT_OID_HEXSZ;
58  
59 24 memset(&pi->line, 0, sizeof(pi->line));
60 24 pi->line.old_lineno = -1;
61 24 pi->line.new_lineno = -1;
62 24 pi->line.num_lines = 1;
63  
64 24 return 0;
65 }
66  
67 14 static int diff_print_info_init_fromdiff(
68 diff_print_info *pi,
69 git_str *out,
70 git_diff *diff,
71 git_diff_format_t format,
72 git_diff_line_cb cb,
73 void *payload)
74 {
75 14 ▶ git_repository *repo = diff ? diff->repo : NULL;
Branch: 1 branch
BranchTrueFalseTotal
git_repository *repo = diff ? diff->repo : NULL; 14 0 14
76  
77 14 memset(pi, 0, sizeof(diff_print_info));
78  
79 14 ▶ if (diff) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff) { 14 0 14
80 14 pi->flags = diff->opts.flags;
81 14 pi->id_strlen = diff->opts.id_abbrev;
82 14 pi->old_prefix = diff->opts.old_prefix;
83 14 pi->new_prefix = diff->opts.new_prefix;
84  
85 14 pi->strcomp = diff->strcomp;
86 }
87  
88 14 return diff_print_info_init__common(pi, out, repo, format, cb, payload);
89 }
90  
91 10 static int diff_print_info_init_frompatch(
92 diff_print_info *pi,
93 git_str *out,
94 git_patch *patch,
95 git_diff_format_t format,
96 git_diff_line_cb cb,
97 void *payload)
98 {
99 10 ▶ GIT_ASSERT_ARG(patch);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(patch); 0 10 10
100  
101 10 memset(pi, 0, sizeof(diff_print_info));
102  
103 10 pi->flags = patch->diff_opts.flags;
104 10 pi->id_strlen = patch->diff_opts.id_abbrev;
105 10 pi->old_prefix = patch->diff_opts.old_prefix;
106 10 pi->new_prefix = patch->diff_opts.new_prefix;
107  
108 10 return diff_print_info_init__common(pi, out, patch->repo, format, cb, payload);
109 }
110  
111 6 static char diff_pick_suffix(int mode)
112 {
113 6 ▶ if (S_ISDIR(mode))
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(mode)) 0 6 6
114 0 return '/';
115 6 ▶ else if (GIT_PERMS_IS_EXEC(mode)) /* -V536 */
Branch: 1 branch
BranchTrueFalseTotal
else if (GIT_PERMS_IS_EXEC(mode)) /* -V536 */ 0 6 6
116 /* in git, modes are very regular, so we must have 0100755 mode */
117 0 return '*';
118 else
119 6 return ' ';
120 }
121  
122 5 char git_diff_status_char(git_delta_t status)
123 {
124 char code;
125  
126 5 switch (status) {
127 5 case GIT_DELTA_ADDED: code = 'A'; break;
128 0 case GIT_DELTA_DELETED: code = 'D'; break;
129 0 case GIT_DELTA_MODIFIED: code = 'M'; break;
130 0 case GIT_DELTA_RENAMED: code = 'R'; break;
131 0 case GIT_DELTA_COPIED: code = 'C'; break;
132 0 case GIT_DELTA_IGNORED: code = 'I'; break;
133 0 case GIT_DELTA_UNTRACKED: code = '?'; break;
134 0 case GIT_DELTA_TYPECHANGE: code = 'T'; break;
135 0 case GIT_DELTA_UNREADABLE: code = 'X'; break;
136 0 default: code = ' '; break;
137 }
138  
139 5 return code;
140 }
141  
142 2 static int diff_print_one_name_only(
143 const git_diff_delta *delta, float progress, void *data)
144 {
145 2 diff_print_info *pi = data;
146 2 git_str *out = pi->buf;
147  
148 2 GIT_UNUSED(progress);
149  
150 2 ▶ if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && 2 0 2
151 2 ▶ delta->status == GIT_DELTA_UNMODIFIED)
Branch: 1 branch
BranchTrueFalseTotal
delta->status == GIT_DELTA_UNMODIFIED) 0 2 2
152 0 return 0;
153  
154 2 git_str_clear(out);
155 2 git_str_puts(out, delta->new_file.path);
156 2 git_str_putc(out, '\n');
157 2 ▶ if (git_str_oom(out))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(out)) 0 2 2
158 0 return -1;
159  
160 2 pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
161 2 pi->line.content = git_str_cstr(out);
162 2 pi->line.content_len = git_str_len(out);
163  
164 2 return pi->print_cb(delta, NULL, &pi->line, pi->payload);
165 }
166  
167 3 static int diff_print_one_name_status(
168 const git_diff_delta *delta, float progress, void *data)
169 {
170 3 diff_print_info *pi = data;
171 3 git_str *out = pi->buf;
172 3 char old_suffix, new_suffix, code = git_diff_status_char(delta->status);
173 6 int(*strcomp)(const char *, const char *) = pi->strcomp ?
174 3 ▶ pi->strcomp : git__strcmp;
Branch: 1 branch
BranchTrueFalseTotal
pi->strcomp : git__strcmp; 3 0 3
175  
176 3 GIT_UNUSED(progress);
177  
178 3 ▶ if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ')
Branch: 2 branches
BranchTrueFalseTotal
if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ') 3 0 3
if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ') 0 3 3
179 0 return 0;
180  
181 3 old_suffix = diff_pick_suffix(delta->old_file.mode);
182 3 new_suffix = diff_pick_suffix(delta->new_file.mode);
183  
184 3 git_str_clear(out);
185  
186 3 if (delta->old_file.path != delta->new_file.path &&
187 0 strcomp(delta->old_file.path,delta->new_file.path) != 0)
188 0 git_str_printf(out, "%c\t%s%c %s%c\n", code,
189 0 delta->old_file.path, old_suffix, delta->new_file.path, new_suffix);
190 3 ▶ else if (delta->old_file.mode != delta->new_file.mode &&
Branch: 1 branch
BranchTrueFalseTotal
else if (delta->old_file.mode != delta->new_file.mode && 3 0 3
191 3 ▶ delta->old_file.mode != 0 && delta->new_file.mode != 0)
Branch: 2 branches
BranchTrueFalseTotal
delta->old_file.mode != 0 && delta->new_file.mode != 0) 0 3 3
delta->old_file.mode != 0 && delta->new_file.mode != 0) 0 0 0
192 0 git_str_printf(out, "%c\t%s%c %s%c\n", code,
193 0 delta->old_file.path, old_suffix, delta->new_file.path, new_suffix);
194 3 ▶ else if (old_suffix != ' ')
Branch: 1 branch
BranchTrueFalseTotal
else if (old_suffix != ' ') 0 3 3
195 0 git_str_printf(out, "%c\t%s%c\n", code, delta->old_file.path, old_suffix);
196 else
197 3 git_str_printf(out, "%c\t%s\n", code, delta->old_file.path);
198 3 ▶ if (git_str_oom(out))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(out)) 0 3 3
199 0 return -1;
200  
201 3 pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
202 3 pi->line.content = git_str_cstr(out);
203 3 pi->line.content_len = git_str_len(out);
204  
205 3 return pi->print_cb(delta, NULL, &pi->line, pi->payload);
206 }
207  
208 2 static int diff_print_one_raw(
209 const git_diff_delta *delta, float progress, void *data)
210 {
211 2 diff_print_info *pi = data;
212 2 git_str *out = pi->buf;
213 int id_abbrev;
214 2 char code = git_diff_status_char(delta->status);
215 char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1];
216  
217 2 GIT_UNUSED(progress);
218  
219 2 ▶ if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ')
Branch: 2 branches
BranchTrueFalseTotal
if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ') 2 0 2
if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ') 0 2 2
220 0 return 0;
221  
222 2 git_str_clear(out);
223  
224 2 ▶ id_abbrev = delta->old_file.mode ? delta->old_file.id_abbrev :
Branch: 1 branch
BranchTrueFalseTotal
id_abbrev = delta->old_file.mode ? delta->old_file.id_abbrev : 0 2 2
225 2 delta->new_file.id_abbrev;
226  
227 2 ▶ if (pi->id_strlen > id_abbrev) {
Branch: 1 branch
BranchTrueFalseTotal
if (pi->id_strlen > id_abbrev) { 0 2 2
228 0 git_error_set(GIT_ERROR_PATCH,
229 "the patch input contains %d id characters (cannot print %d)",
230 id_abbrev, pi->id_strlen);
231 0 return -1;
232 }
233  
234 2 git_oid_tostr(start_oid, pi->id_strlen + 1, &delta->old_file.id);
235 2 git_oid_tostr(end_oid, pi->id_strlen + 1, &delta->new_file.id);
236  
237 2 git_str_printf(
238 2 out, (pi->id_strlen <= GIT_OID_HEXSZ) ?
239 ":%06o %06o %s... %s... %c" : ":%06o %06o %s %s %c",
240 2 ▶ delta->old_file.mode, delta->new_file.mode, start_oid, end_oid, code);
Branch: 1 branch
BranchTrueFalseTotal
delta->old_file.mode, delta->new_file.mode, start_oid, end_oid, code); 2 0 2
241  
242 2 ▶ if (delta->similarity > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (delta->similarity > 0) 0 2 2
243 0 git_str_printf(out, "%03u", delta->similarity);
244  
245 2 ▶ if (delta->old_file.path != delta->new_file.path)
Branch: 1 branch
BranchTrueFalseTotal
if (delta->old_file.path != delta->new_file.path) 0 2 2
246 0 git_str_printf(
247 0 out, "\t%s %s\n", delta->old_file.path, delta->new_file.path);
248 else
249 2 git_str_printf(
250 2 ▶ out, "\t%s\n", delta->old_file.path ?
Branch: 1 branch
BranchTrueFalseTotal
out, "\t%s\n", delta->old_file.path ? 2 0 2
251 delta->old_file.path : delta->new_file.path);
252  
253 2 ▶ if (git_str_oom(out))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(out)) 0 2 2
254 0 return -1;
255  
256 2 pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
257 2 pi->line.content = git_str_cstr(out);
258 2 pi->line.content_len = git_str_len(out);
259  
260 2 return pi->print_cb(delta, NULL, &pi->line, pi->payload);
261 }
262  
263 0 static int diff_print_modes(
264 git_str *out, const git_diff_delta *delta)
265 {
266 0 git_str_printf(out, "old mode %o\n", delta->old_file.mode);
267 0 git_str_printf(out, "new mode %o\n", delta->new_file.mode);
268  
269 0 ▶ return git_str_oom(out) ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return git_str_oom(out) ? -1 : 0; 0 0 0
270 }
271  
272 22 static int diff_print_oid_range(
273 git_str *out, const git_diff_delta *delta, int id_strlen,
274 bool print_index)
275 {
276 char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1];
277  
278 22 ▶ if (delta->old_file.mode &&
Branch: 1 branch
BranchTrueFalseTotal
if (delta->old_file.mode && 7 15 22
279 7 ▶ id_strlen > delta->old_file.id_abbrev) {
Branch: 1 branch
BranchTrueFalseTotal
id_strlen > delta->old_file.id_abbrev) { 0 7 7
280 0 git_error_set(GIT_ERROR_PATCH,
281 "the patch input contains %d id characters (cannot print %d)",
282 0 delta->old_file.id_abbrev, id_strlen);
283 0 return -1;
284 }
285  
286 22 ▶ if ((delta->new_file.mode &&
Branch: 1 branch
BranchTrueFalseTotal
if ((delta->new_file.mode && 19 3 22
287 19 ▶ id_strlen > delta->new_file.id_abbrev)) {
Branch: 1 branch
BranchTrueFalseTotal
id_strlen > delta->new_file.id_abbrev)) { 0 19 19
288 0 git_error_set(GIT_ERROR_PATCH,
289 "the patch input contains %d id characters (cannot print %d)",
290 0 delta->new_file.id_abbrev, id_strlen);
291 0 return -1;
292 }
293  
294 22 git_oid_tostr(start_oid, id_strlen + 1, &delta->old_file.id);
295 22 git_oid_tostr(end_oid, id_strlen + 1, &delta->new_file.id);
296  
297 22 ▶ if (delta->old_file.mode == delta->new_file.mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (delta->old_file.mode == delta->new_file.mode) { 4 18 22
298 4 ▶ if (print_index)
Branch: 1 branch
BranchTrueFalseTotal
if (print_index) 3 1 4
299 3 git_str_printf(out, "index %s..%s %o\n",
300 3 start_oid, end_oid, delta->old_file.mode);
301 } else {
302 18 ▶ if (delta->old_file.mode == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (delta->old_file.mode == 0) 15 3 18
303 15 git_str_printf(out, "new file mode %o\n", delta->new_file.mode);
304 3 ▶ else if (delta->new_file.mode == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (delta->new_file.mode == 0) 3 0 3
305 3 git_str_printf(out, "deleted file mode %o\n", delta->old_file.mode);
306 else
307 0 diff_print_modes(out, delta);
308  
309 18 ▶ if (print_index)
Branch: 1 branch
BranchTrueFalseTotal
if (print_index) 17 1 18
310 17 git_str_printf(out, "index %s..%s\n", start_oid, end_oid);
311 }
312  
313 22 ▶ return git_str_oom(out) ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return git_str_oom(out) ? -1 : 0; 0 22 22
314 }
315  
316 46 static int diff_delta_format_path(
317 git_str *out, const char *prefix, const char *filename)
318 {
319 46 ▶ if (!filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (!filename) { 0 46 46
320 /* don't prefix "/dev/null" */
321 0 return git_str_puts(out, "/dev/null");
322 }
323  
324 46 ▶ if (git_str_joinpath(out, prefix, filename) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_joinpath(out, prefix, filename) < 0) 0 46 46
325 0 return -1;
326  
327 46 return git_str_quote(out);
328 }
329  
330 21 static int diff_delta_format_with_paths(
331 git_str *out,
332 const git_diff_delta *delta,
333 const char *template,
334 const char *oldpath,
335 const char *newpath)
336 {
337 21 ▶ if (git_oid_is_zero(&delta->old_file.id))
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid_is_zero(&delta->old_file.id)) 15 6 21
338 15 oldpath = "/dev/null";
339  
340 21 ▶ if (git_oid_is_zero(&delta->new_file.id))
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid_is_zero(&delta->new_file.id)) 2 19 21
341 2 newpath = "/dev/null";
342  
343 21 return git_str_printf(out, template, oldpath, newpath);
344 }
345  
346 2 static int diff_delta_format_similarity_header(
347 git_str *out,
348 const git_diff_delta *delta)
349 {
350 2 git_str old_path = GIT_STR_INIT, new_path = GIT_STR_INIT;
351 const char *type;
352 2 int error = 0;
353  
354 2 ▶ if (delta->similarity > 100) {
Branch: 1 branch
BranchTrueFalseTotal
if (delta->similarity > 100) { 0 2 2
355 0 git_error_set(GIT_ERROR_PATCH, "invalid similarity %d", delta->similarity);
356 0 error = -1;
357 0 goto done;
358 }
359  
360 2 ▶ GIT_ASSERT(delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ASSERT(delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED); 0 2 2
GIT_ASSERT(delta->status == GIT_DELTA_RENAMED || delta->status == GIT_DELTA_COPIED); 0 0 0
361 2 ▶ if (delta->status == GIT_DELTA_RENAMED)
Branch: 1 branch
BranchTrueFalseTotal
if (delta->status == GIT_DELTA_RENAMED) 2 0 2
362 2 type = "rename";
363 else
364 0 type = "copy";
365  
366 4 if ((error = git_str_puts(&old_path, delta->old_file.path)) < 0 ||
367 4 ▶ (error = git_str_puts(&new_path, delta->new_file.path)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_str_puts(&new_path, delta->new_file.path)) < 0 || 2 0 2
368 4 ▶ (error = git_str_quote(&old_path)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_str_quote(&old_path)) < 0 || 0 2 2
369 2 (error = git_str_quote(&new_path)) < 0)
370 0 goto done;
371  
372 2 git_str_printf(out,
373 "similarity index %d%%\n"
374 "%s from %s\n"
375 "%s to %s\n",
376 2 delta->similarity,
377 type, old_path.ptr,
378 type, new_path.ptr);
379  
380 2 ▶ if (git_str_oom(out))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(out)) 2 0 2
381 0 error = -1;
382  
383 2 done:
384 2 git_str_dispose(&old_path);
385 2 git_str_dispose(&new_path);
386  
387 2 return error;
388 }
389  
390 23 static bool delta_is_unchanged(const git_diff_delta *delta)
391 {
392 38 if (git_oid_is_zero(&delta->old_file.id) &&
393 15 git_oid_is_zero(&delta->new_file.id))
394 0 return true;
395  
396 23 ▶ if (delta->old_file.mode == GIT_FILEMODE_COMMIT ||
Branch: 1 branch
BranchTrueFalseTotal
if (delta->old_file.mode == GIT_FILEMODE_COMMIT || 23 0 23
397 23 ▶ delta->new_file.mode == GIT_FILEMODE_COMMIT)
Branch: 1 branch
BranchTrueFalseTotal
delta->new_file.mode == GIT_FILEMODE_COMMIT) 0 23 23
398 0 return false;
399  
400 23 ▶ if (git_oid_equal(&delta->old_file.id, &delta->new_file.id))
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid_equal(&delta->old_file.id, &delta->new_file.id)) 1 22 23
401 1 return true;
402  
403 22 return false;
404 }
405  
406 23 int git_diff_delta__format_file_header(
407 git_str *out,
408 const git_diff_delta *delta,
409 const char *oldpfx,
410 const char *newpfx,
411 int id_strlen,
412 bool print_index)
413 {
414 23 git_str old_path = GIT_STR_INIT, new_path = GIT_STR_INIT;
415 23 bool unchanged = delta_is_unchanged(delta);
416 23 int error = 0;
417  
418 23 ▶ if (!oldpfx)
Branch: 1 branch
BranchTrueFalseTotal
if (!oldpfx) 0 23 23
419 0 oldpfx = DIFF_OLD_PREFIX_DEFAULT;
420 23 ▶ if (!newpfx)
Branch: 1 branch
BranchTrueFalseTotal
if (!newpfx) 0 23 23
421 0 newpfx = DIFF_NEW_PREFIX_DEFAULT;
422 23 ▶ if (!id_strlen)
Branch: 1 branch
BranchTrueFalseTotal
if (!id_strlen) 0 23 23
423 0 id_strlen = GIT_ABBREV_DEFAULT;
424  
425 23 ▶ if ((error = diff_delta_format_path(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_delta_format_path( 23 0 23
426 46 ▶ &old_path, oldpfx, delta->old_file.path)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
&old_path, oldpfx, delta->old_file.path)) < 0 || 0 23 23
427 23 (error = diff_delta_format_path(
428 23 &new_path, newpfx, delta->new_file.path)) < 0)
429 0 goto done;
430  
431 23 git_str_clear(out);
432  
433 23 git_str_printf(out, "diff --git %s %s\n",
434 old_path.ptr, new_path.ptr);
435  
436 23 ▶ if (unchanged && delta->old_file.mode != delta->new_file.mode)
Branch: 2 branches
BranchTrueFalseTotal
if (unchanged && delta->old_file.mode != delta->new_file.mode) 1 22 23
if (unchanged && delta->old_file.mode != delta->new_file.mode) 0 1 1
437 0 diff_print_modes(out, delta);
438  
439 23 ▶ if (delta->status == GIT_DELTA_RENAMED ||
Branch: 1 branch
BranchTrueFalseTotal
if (delta->status == GIT_DELTA_RENAMED || 21 2 23
440 21 ▶ (delta->status == GIT_DELTA_COPIED && unchanged)) {
Branch: 2 branches
BranchTrueFalseTotal
(delta->status == GIT_DELTA_COPIED && unchanged)) { 0 21 21
(delta->status == GIT_DELTA_COPIED && unchanged)) { 0 0 0
441 2 ▶ if ((error = diff_delta_format_similarity_header(out, delta)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_delta_format_similarity_header(out, delta)) < 0) 0 2 2
442 0 goto done;
443 }
444  
445 23 ▶ if (!unchanged) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unchanged) { 22 1 23
446 22 ▶ if ((error = diff_print_oid_range(out, delta,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_print_oid_range(out, delta, 0 22 22
447 id_strlen, print_index)) < 0)
448 0 goto done;
449  
450 22 ▶ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) 21 1 22
451 21 diff_delta_format_with_paths(out, delta,
452 21 "--- %s\n+++ %s\n", old_path.ptr, new_path.ptr);
453 }
454  
455 23 ▶ if (git_str_oom(out))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(out)) 23 0 23
456 0 error = -1;
457  
458 23 done:
459 23 git_str_dispose(&old_path);
460 23 git_str_dispose(&new_path);
461  
462 23 return error;
463 }
464  
465 2 static int format_binary(
466 diff_print_info *pi,
467 git_diff_binary_t type,
468 const char *data,
469 size_t datalen,
470 size_t inflatedlen)
471 {
472 2 const char *typename = type == GIT_DIFF_BINARY_DELTA ?
473 2 ▶ "delta" : "literal";
Branch: 1 branch
BranchTrueFalseTotal
"delta" : "literal"; 0 2 2
474 const char *scan, *end;
475  
476 2 git_str_printf(pi->buf, "%s %" PRIuZ "\n", typename, inflatedlen);
477 2 pi->line.num_lines++;
478  
479 4 ▶ for (scan = data, end = data + datalen; scan < end; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (scan = data, end = data + datalen; scan < end; ) { 2 2 4
480 2 size_t chunk_len = end - scan;
481 2 ▶ if (chunk_len > 52)
Branch: 1 branch
BranchTrueFalseTotal
if (chunk_len > 52) 0 2 2
482 0 chunk_len = 52;
483  
484 2 ▶ if (chunk_len <= 26)
Branch: 1 branch
BranchTrueFalseTotal
if (chunk_len <= 26) 2 0 2
485 2 git_str_putc(pi->buf, (char)chunk_len + 'A' - 1);
486 else
487 0 git_str_putc(pi->buf, (char)chunk_len - 26 + 'a' - 1);
488  
489 2 git_str_encode_base85(pi->buf, scan, chunk_len);
490 2 git_str_putc(pi->buf, '\n');
491  
492 2 ▶ if (git_str_oom(pi->buf))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(pi->buf)) 0 2 2
493 0 return -1;
494  
495 2 scan += chunk_len;
496 2 pi->line.num_lines++;
497 }
498 2 git_str_putc(pi->buf, '\n');
499  
500 2 ▶ if (git_str_oom(pi->buf))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(pi->buf)) 0 2 2
501 0 return -1;
502  
503 2 return 0;
504 }
505  
506 0 static int diff_print_patch_file_binary_noshow(
507 diff_print_info *pi, git_diff_delta *delta,
508 const char *old_pfx, const char *new_pfx)
509 {
510 0 git_str old_path = GIT_STR_INIT, new_path = GIT_STR_INIT;
511 int error;
512  
513 0 if ((error = diff_delta_format_path(&old_path, old_pfx, delta->old_file.path)) < 0 ||
514 0 ▶ (error = diff_delta_format_path(&new_path, new_pfx, delta->new_file.path)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = diff_delta_format_path(&new_path, new_pfx, delta->new_file.path)) < 0 || 0 0 0
515 0 (error = diff_delta_format_with_paths(pi->buf, delta, "Binary files %s and %s differ\n",
516 0 old_path.ptr, new_path.ptr)) < 0)
517 0 goto done;
518  
519 0 pi->line.num_lines = 1;
520  
521 0 done:
522 0 git_str_dispose(&old_path);
523 0 git_str_dispose(&new_path);
524 0 return error;
525 }
526  
527 1 static int diff_print_patch_file_binary(
528 diff_print_info *pi, git_diff_delta *delta,
529 const char *old_pfx, const char *new_pfx,
530 const git_diff_binary *binary)
531 {
532 size_t pre_binary_size;
533 int error;
534  
535 1 ▶ if (delta->status == GIT_DELTA_UNMODIFIED)
Branch: 1 branch
BranchTrueFalseTotal
if (delta->status == GIT_DELTA_UNMODIFIED) 0 1 1
536 0 return 0;
537  
538 1 ▶ if ((pi->flags & GIT_DIFF_SHOW_BINARY) == 0 || !binary->contains_data)
Branch: 2 branches
BranchTrueFalseTotal
if ((pi->flags & GIT_DIFF_SHOW_BINARY) == 0 || !binary->contains_data) 1 0 1
if ((pi->flags & GIT_DIFF_SHOW_BINARY) == 0 || !binary->contains_data) 0 1 1
539 0 return diff_print_patch_file_binary_noshow(
540 pi, delta, old_pfx, new_pfx);
541  
542 1 pre_binary_size = pi->buf->size;
543 1 git_str_printf(pi->buf, "GIT binary patch\n");
544 1 pi->line.num_lines++;
545  
546 1 ▶ if ((error = format_binary(pi, binary->new_file.type, binary->new_file.data,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = format_binary(pi, binary->new_file.type, binary->new_file.data, 1 0 1
547 2 ▶ binary->new_file.datalen, binary->new_file.inflatedlen)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
binary->new_file.datalen, binary->new_file.inflatedlen)) < 0 || 0 1 1
548 1 (error = format_binary(pi, binary->old_file.type, binary->old_file.data,
549 1 binary->old_file.datalen, binary->old_file.inflatedlen)) < 0) {
550 0 ▶ if (error == GIT_EBUFS) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_EBUFS) { 0 0 0
551 0 git_error_clear();
552 0 git_str_truncate(pi->buf, pre_binary_size);
553  
554 0 return diff_print_patch_file_binary_noshow(
555 pi, delta, old_pfx, new_pfx);
556 }
557 }
558  
559 1 pi->line.num_lines++;
560 1 return error;
561 }
562  
563 23 static int diff_print_patch_file(
564 const git_diff_delta *delta, float progress, void *data)
565 {
566 int error;
567 23 diff_print_info *pi = data;
568 23 const char *oldpfx =
569 23 ▶ pi->old_prefix ? pi->old_prefix : DIFF_OLD_PREFIX_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
pi->old_prefix ? pi->old_prefix : DIFF_OLD_PREFIX_DEFAULT; 21 2 23
570 23 const char *newpfx =
571 23 ▶ pi->new_prefix ? pi->new_prefix : DIFF_NEW_PREFIX_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
pi->new_prefix ? pi->new_prefix : DIFF_NEW_PREFIX_DEFAULT; 21 2 23
572  
573 45 ▶ bool binary = (delta->flags & GIT_DIFF_FLAG_BINARY) ||
Branch: 1 branch
BranchTrueFalseTotal
bool binary = (delta->flags & GIT_DIFF_FLAG_BINARY) || 22 1 23
574 22 ▶ (pi->flags & GIT_DIFF_FORCE_BINARY);
Branch: 1 branch
BranchTrueFalseTotal
(pi->flags & GIT_DIFF_FORCE_BINARY); 0 22 22
575 23 bool show_binary = !!(pi->flags & GIT_DIFF_SHOW_BINARY);
576 23 int id_strlen = pi->id_strlen;
577 23 bool print_index = (pi->format != GIT_DIFF_FORMAT_PATCH_ID);
578  
579 23 ▶ if (binary && show_binary)
Branch: 2 branches
BranchTrueFalseTotal
if (binary && show_binary) 1 22 23
if (binary && show_binary) 1 0 1
580 1 ▶ id_strlen = delta->old_file.id_abbrev ? delta->old_file.id_abbrev :
Branch: 1 branch
BranchTrueFalseTotal
id_strlen = delta->old_file.id_abbrev ? delta->old_file.id_abbrev : 1 0 1
581 0 delta->new_file.id_abbrev;
582  
583 23 GIT_UNUSED(progress);
584  
585 23 ▶ if (S_ISDIR(delta->new_file.mode) ||
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(delta->new_file.mode) || 23 0 23
586 23 ▶ delta->status == GIT_DELTA_UNMODIFIED ||
Branch: 1 branch
BranchTrueFalseTotal
delta->status == GIT_DELTA_UNMODIFIED || 23 0 23
587 23 ▶ delta->status == GIT_DELTA_IGNORED ||
Branch: 1 branch
BranchTrueFalseTotal
delta->status == GIT_DELTA_IGNORED || 23 0 23
588 23 ▶ delta->status == GIT_DELTA_UNREADABLE ||
Branch: 1 branch
BranchTrueFalseTotal
delta->status == GIT_DELTA_UNREADABLE || 23 0 23
589 23 ▶ (delta->status == GIT_DELTA_UNTRACKED &&
Branch: 1 branch
BranchTrueFalseTotal
(delta->status == GIT_DELTA_UNTRACKED && 0 23 23
590 0 ▶ (pi->flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) == 0))
Branch: 1 branch
BranchTrueFalseTotal
(pi->flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) == 0)) 0 0 0
591 0 return 0;
592  
593 23 ▶ if ((error = git_diff_delta__format_file_header(pi->buf, delta, oldpfx, newpfx,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_diff_delta__format_file_header(pi->buf, delta, oldpfx, newpfx, 0 23 23
594 id_strlen, print_index)) < 0)
595 0 return error;
596  
597 23 pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
598 23 pi->line.content = git_str_cstr(pi->buf);
599 23 pi->line.content_len = git_str_len(pi->buf);
600  
601 23 return pi->print_cb(delta, NULL, &pi->line, pi->payload);
602 }
603  
604 1 static int diff_print_patch_binary(
605 const git_diff_delta *delta,
606 const git_diff_binary *binary,
607 void *data)
608 {
609 1 diff_print_info *pi = data;
610 1 const char *old_pfx =
611 1 ▶ pi->old_prefix ? pi->old_prefix : DIFF_OLD_PREFIX_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
pi->old_prefix ? pi->old_prefix : DIFF_OLD_PREFIX_DEFAULT; 1 0 1
612 1 const char *new_pfx =
613 1 ▶ pi->new_prefix ? pi->new_prefix : DIFF_NEW_PREFIX_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
pi->new_prefix ? pi->new_prefix : DIFF_NEW_PREFIX_DEFAULT; 1 0 1
614 int error;
615  
616 1 git_str_clear(pi->buf);
617  
618 1 ▶ if ((error = diff_print_patch_file_binary(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_print_patch_file_binary( 0 1 1
619 pi, (git_diff_delta *)delta, old_pfx, new_pfx, binary)) < 0)
620 0 return error;
621  
622 1 pi->line.origin = GIT_DIFF_LINE_BINARY;
623 1 pi->line.content = git_str_cstr(pi->buf);
624 1 pi->line.content_len = git_str_len(pi->buf);
625  
626 1 return pi->print_cb(delta, NULL, &pi->line, pi->payload);
627 }
628  
629 17 static int diff_print_patch_hunk(
630 const git_diff_delta *d,
631 const git_diff_hunk *h,
632 void *data)
633 {
634 17 diff_print_info *pi = data;
635  
636 17 ▶ if (S_ISDIR(d->new_file.mode))
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(d->new_file.mode)) 0 17 17
637 0 return 0;
638  
639 17 pi->line.origin = GIT_DIFF_LINE_HUNK_HDR;
640 17 pi->line.content = h->header;
641 17 pi->line.content_len = h->header_len;
642  
643 17 return pi->print_cb(d, h, &pi->line, pi->payload);
644 }
645  
646 38 static int diff_print_patch_line(
647 const git_diff_delta *delta,
648 const git_diff_hunk *hunk,
649 const git_diff_line *line,
650 void *data)
651 {
652 38 diff_print_info *pi = data;
653  
654 38 ▶ if (S_ISDIR(delta->new_file.mode))
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(delta->new_file.mode)) 0 38 38
655 0 return 0;
656  
657 38 return pi->print_cb(delta, hunk, line, pi->payload);
658 }
659  
660 /* print a git_diff to an output callback */
661 14 int git_diff_print(
662 git_diff *diff,
663 git_diff_format_t format,
664 git_diff_line_cb print_cb,
665 void *payload)
666 {
667 int error;
668 14 git_str buf = GIT_STR_INIT;
669 diff_print_info pi;
670 14 git_diff_file_cb print_file = NULL;
671 14 git_diff_binary_cb print_binary = NULL;
672 14 git_diff_hunk_cb print_hunk = NULL;
673 14 git_diff_line_cb print_line = NULL;
674  
675 14 switch (format) {
676 8 case GIT_DIFF_FORMAT_PATCH:
677 8 print_file = diff_print_patch_file;
678 8 print_binary = diff_print_patch_binary;
679 8 print_hunk = diff_print_patch_hunk;
680 8 print_line = diff_print_patch_line;
681 8 break;
682 1 case GIT_DIFF_FORMAT_PATCH_ID:
683 1 print_file = diff_print_patch_file;
684 1 print_binary = diff_print_patch_binary;
685 1 print_line = diff_print_patch_line;
686 1 break;
687 1 case GIT_DIFF_FORMAT_PATCH_HEADER:
688 1 print_file = diff_print_patch_file;
689 1 break;
690 1 case GIT_DIFF_FORMAT_RAW:
691 1 print_file = diff_print_one_raw;
692 1 break;
693 1 case GIT_DIFF_FORMAT_NAME_ONLY:
694 1 print_file = diff_print_one_name_only;
695 1 break;
696 2 case GIT_DIFF_FORMAT_NAME_STATUS:
697 2 print_file = diff_print_one_name_status;
698 2 break;
699 0 default:
700 0 git_error_set(GIT_ERROR_INVALID, "unknown diff output format (%d)", format);
701 0 return -1;
702 }
703  
704 14 ▶ if ((error = diff_print_info_init_fromdiff(&pi, &buf, diff, format, print_cb, payload)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_print_info_init_fromdiff(&pi, &buf, diff, format, print_cb, payload)) < 0) 0 14 14
705 0 goto out;
706  
707 14 ▶ if ((error = git_diff_foreach(diff, print_file, print_binary, print_hunk, print_line, &pi)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_diff_foreach(diff, print_file, print_binary, print_hunk, print_line, &pi)) != 0) { 14 0 14
708 0 git_error_set_after_callback_function(error, "git_diff_print");
709 0 goto out;
710 }
711  
712 14 out:
713 14 git_str_dispose(&buf);
714 14 return error;
715 }
716  
717 44 int git_diff_print_callback__to_buf(
718 const git_diff_delta *delta,
719 const git_diff_hunk *hunk,
720 const git_diff_line *line,
721 void *payload)
722 {
723 44 git_str *output = payload;
724 44 GIT_UNUSED(delta); GIT_UNUSED(hunk);
725  
726 44 ▶ if (!output) {
Branch: 1 branch
BranchTrueFalseTotal
if (!output) { 0 44 44
727 0 git_error_set(GIT_ERROR_INVALID, "buffer pointer must be provided");
728 0 return -1;
729 }
730  
731 44 ▶ if (line->origin == GIT_DIFF_LINE_ADDITION ||
Branch: 1 branch
BranchTrueFalseTotal
if (line->origin == GIT_DIFF_LINE_ADDITION || 34 10 44
732 34 ▶ line->origin == GIT_DIFF_LINE_DELETION ||
Branch: 1 branch
BranchTrueFalseTotal
line->origin == GIT_DIFF_LINE_DELETION || 32 2 34
733 32 ▶ line->origin == GIT_DIFF_LINE_CONTEXT)
Branch: 1 branch
BranchTrueFalseTotal
line->origin == GIT_DIFF_LINE_CONTEXT) 5 27 32
734 17 git_str_putc(output, line->origin);
735  
736 44 return git_str_put(output, line->content, line->content_len);
737 }
738  
739 0 int git_diff_print_callback__to_file_handle(
740 const git_diff_delta *delta,
741 const git_diff_hunk *hunk,
742 const git_diff_line *line,
743 void *payload)
744 {
745 0 ▶ FILE *fp = payload ? payload : stdout;
Branch: 1 branch
BranchTrueFalseTotal
FILE *fp = payload ? payload : stdout; 0 0 0
746 int error;
747  
748 0 GIT_UNUSED(delta);
749 0 GIT_UNUSED(hunk);
750  
751 0 ▶ if (line->origin == GIT_DIFF_LINE_CONTEXT ||
Branch: 1 branch
BranchTrueFalseTotal
if (line->origin == GIT_DIFF_LINE_CONTEXT || 0 0 0
752 0 ▶ line->origin == GIT_DIFF_LINE_ADDITION ||
Branch: 1 branch
BranchTrueFalseTotal
line->origin == GIT_DIFF_LINE_ADDITION || 0 0 0
753 0 ▶ line->origin == GIT_DIFF_LINE_DELETION) {
Branch: 1 branch
BranchTrueFalseTotal
line->origin == GIT_DIFF_LINE_DELETION) { 0 0 0
754 0 ▶ while ((error = fputc(line->origin, fp)) == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
while ((error = fputc(line->origin, fp)) == EINTR) 0 0 0
755 0 continue;
756 0 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 0 0
757 0 git_error_set(GIT_ERROR_OS, "could not write status");
758 0 return -1;
759 }
760 }
761  
762 0 ▶ if (fwrite(line->content, line->content_len, 1, fp) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fwrite(line->content, line->content_len, 1, fp) != 1) { 0 0 0
763 0 git_error_set(GIT_ERROR_OS, "could not write line");
764 0 return -1;
765 }
766  
767 0 return 0;
768 }
769  
770 /* print a git_diff to a git_str */
771 1 int git_diff_to_buf(git_buf *out, git_diff *diff, git_diff_format_t format)
772 {
773 1 git_str str = GIT_STR_INIT;
774 int error;
775  
776 1 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 1 1
777 1 ▶ GIT_ASSERT_ARG(diff);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(diff); 0 1 1
778  
779 2 if ((error = git_buf_tostr(&str, out)) < 0 ||
780 1 (error = git_diff_print(diff, format, git_diff_print_callback__to_buf, &str)) < 0)
781 0 goto done;
782  
783 1 error = git_buf_fromstr(out, &str);
784  
785 1 done:
786 1 git_str_dispose(&str);
787 1 return error;
788 }
789  
790 /* print a git_patch to an output callback */
791 10 int git_patch_print(
792 git_patch *patch,
793 git_diff_line_cb print_cb,
794 void *payload)
795 {
796 10 git_str temp = GIT_STR_INIT;
797 diff_print_info pi;
798 int error;
799  
800 10 ▶ GIT_ASSERT_ARG(patch);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(patch); 0 10 10
801 10 ▶ GIT_ASSERT_ARG(print_cb);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(print_cb); 0 10 10
802  
803 10 ▶ if ((error = diff_print_info_init_frompatch(&pi, &temp, patch,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_print_info_init_frompatch(&pi, &temp, patch, 0 10 10
804 GIT_DIFF_FORMAT_PATCH, print_cb, payload)) < 0)
805 0 goto out;
806  
807 10 ▶ if ((error = git_patch__invoke_callbacks(patch, diff_print_patch_file, diff_print_patch_binary,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_patch__invoke_callbacks(patch, diff_print_patch_file, diff_print_patch_binary, 10 0 10
808 diff_print_patch_hunk, diff_print_patch_line, &pi)) < 0) {
809 0 git_error_set_after_callback_function(error, "git_patch_print");
810 0 goto out;
811 }
812  
813 10 out:
814 10 git_str_dispose(&temp);
815 10 return error;
816 }
817  
818 /* print a git_patch to a git_str */
819 7 int git_patch_to_buf(git_buf *out, git_patch *patch)
820 {
821 7 ▶ GIT_BUF_WRAP_PRIVATE(out, git_patch__to_buf, patch);
Branch: 2 branches
BranchTrueFalseTotal
GIT_BUF_WRAP_PRIVATE(out, git_patch__to_buf, patch); 7 0 7
GIT_BUF_WRAP_PRIVATE(out, git_patch__to_buf, patch); 7 0 7
822 }
823  
824 10 int git_patch__to_buf(git_str *out, git_patch *patch)
825 {
826 10 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 10 10
827 10 ▶ GIT_ASSERT_ARG(patch);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(patch); 0 10 10
828  
829 10 return git_patch_print(patch, git_diff_print_callback__to_buf, out);
830 }
« deps/libgit2/src/libgit2/filter.c ↑ Summary xs/Cert/X509.xs »