deps/libgit2/src/libgit2/diff_generate.c

filter: Statementstmt 67.9% 608 / 895 Branchbran 50.9% 322 / 632 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 60.9% 930 / 1527 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/reset.c ↑ Summary deps/libgit2/src/libgit2/pack-objects.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 "diff_generate.h"
9  
10 #include "diff.h"
11 #include "patch_generate.h"
12 #include "futils.h"
13 #include "config.h"
14 #include "attr_file.h"
15 #include "filter.h"
16 #include "pathspec.h"
17 #include "index.h"
18 #include "odb.h"
19 #include "submodule.h"
20  
21 #define DIFF_FLAG_IS_SET(DIFF,FLAG) \
22 (((DIFF)->base.opts.flags & (FLAG)) != 0)
23 #define DIFF_FLAG_ISNT_SET(DIFF,FLAG) \
24 (((DIFF)->base.opts.flags & (FLAG)) == 0)
25 #define DIFF_FLAG_SET(DIFF,FLAG,VAL) (DIFF)->base.opts.flags = \
26 (VAL) ? ((DIFF)->base.opts.flags | (FLAG)) : \
27 ((DIFF)->base.opts.flags & ~(FLAG))
28  
29 typedef struct {
30 struct git_diff base;
31  
32 git_vector pathspec;
33  
34 uint32_t diffcaps;
35 bool index_updated;
36 } git_diff_generated;
37  
38 293 static git_diff_delta *diff_delta__alloc(
39 git_diff_generated *diff,
40 git_delta_t status,
41 const char *path)
42 {
43 293 git_diff_delta *delta = git__calloc(1, sizeof(git_diff_delta));
44 293 ▶ if (!delta)
Branch: 1 branch
BranchTrueFalseTotal
if (!delta) 0 293 293
45 0 return NULL;
46  
47 293 delta->old_file.path = git_pool_strdup(&diff->base.pool, path);
48 293 ▶ if (delta->old_file.path == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (delta->old_file.path == NULL) { 0 293 293
49 0 git__free(delta);
50 0 return NULL;
51 }
52  
53 293 delta->new_file.path = delta->old_file.path;
54  
55 293 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { 2 291 293
56 2 switch (status) {
57 1 case GIT_DELTA_ADDED: status = GIT_DELTA_DELETED; break;
58 0 case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break;
59 1 default: break; /* leave other status values alone */
60 }
61 }
62 293 delta->status = status;
63  
64 293 return delta;
65 }
66  
67 293 static int diff_insert_delta(
68 git_diff_generated *diff,
69 git_diff_delta *delta,
70 const char *matched_pathspec)
71 {
72 293 int error = 0;
73  
74 293 ▶ if (diff->base.opts.notify_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff->base.opts.notify_cb) { 0 293 293
75 0 error = diff->base.opts.notify_cb(
76 0 &diff->base, delta, matched_pathspec, diff->base.opts.payload);
77  
78 0 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 0 0
79 0 git__free(delta);
80  
81 0 ▶ if (error > 0) /* positive value means to skip this delta */
Branch: 1 branch
BranchTrueFalseTotal
if (error > 0) /* positive value means to skip this delta */ 0 0 0
82 0 return 0;
83 else /* negative value means to cancel diff */
84 0 return git_error_set_after_callback_function(error, "git_diff");
85 }
86 }
87  
88 293 ▶ if ((error = git_vector_insert(&diff->base.deltas, delta)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_insert(&diff->base.deltas, delta)) < 0) 0 293 293
89 0 git__free(delta);
90  
91 293 return error;
92 }
93  
94 681 static bool diff_pathspec_match(
95 const char **matched_pathspec,
96 git_diff_generated *diff,
97 const git_index_entry *entry)
98 {
99 681 bool disable_pathspec_match =
100 681 DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH);
101  
102 /* If we're disabling fnmatch, then the iterator has already applied
103 * the filters to the files for us and we don't have to do anything.
104 * However, this only applies to *files* - the iterator will include
105 * directories that we need to recurse into when not autoexpanding,
106 * so we still need to apply the pathspec match to directories.
107 */
108 681 ▶ if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) &&
Branch: 3 branches
BranchTrueFalseTotal
if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) && 681 0 681
if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) && 670 11 681
if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) && 43 627 670
109 disable_pathspec_match) {
110 43 *matched_pathspec = entry->path;
111 43 return true;
112 }
113  
114 638 return git_pathspec__match(
115 638 &diff->pathspec, entry->path, disable_pathspec_match,
116 638 DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE),
117 matched_pathspec, NULL);
118 }
119  
120 586 static void diff_delta__flag_known_size(git_diff_file *file)
121 {
122 /*
123 * If we don't know the ID, that can only come from the workdir
124 * iterator, which means we *do* know the file size. This is a
125 * leaky abstraction, but alas. Otherwise, we test against the
126 * empty blob id.
127 */
128 586 ▶ if (file->size ||
Branch: 1 branch
BranchTrueFalseTotal
if (file->size || 435 151 586
129 869 !(file->flags & GIT_DIFF_FLAG_VALID_ID) ||
130 434 git_oid_equal(&file->id, &git_oid__empty_blob_sha1))
131 164 file->flags |= GIT_DIFF_FLAG_VALID_SIZE;
132 586 }
133  
134 293 static void diff_delta__flag_known_sizes(git_diff_delta *delta)
135 {
136 293 diff_delta__flag_known_size(&delta->old_file);
137 293 diff_delta__flag_known_size(&delta->new_file);
138 293 }
139  
140 281 static int diff_delta__from_one(
141 git_diff_generated *diff,
142 git_delta_t status,
143 const git_index_entry *oitem,
144 const git_index_entry *nitem)
145 {
146 281 const git_index_entry *entry = nitem;
147 281 bool has_old = false;
148 git_diff_delta *delta;
149 const char *matched_pathspec;
150  
151 281 ▶ GIT_ASSERT_ARG((oitem != NULL) ^ (nitem != NULL));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG((oitem != NULL) ^ (nitem != NULL)); 0 281 281
152  
153 281 ▶ if (oitem) {
Branch: 1 branch
BranchTrueFalseTotal
if (oitem) { 21 260 281
154 21 entry = oitem;
155 21 has_old = true;
156 }
157  
158 281 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) 1 280 281
159 1 has_old = !has_old;
160  
161 281 ▶ if ((entry->flags & GIT_INDEX_ENTRY_VALID) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((entry->flags & GIT_INDEX_ENTRY_VALID) != 0) 0 281 281
162 0 return 0;
163  
164 281 ▶ if (status == GIT_DELTA_IGNORED &&
Branch: 1 branch
BranchTrueFalseTotal
if (status == GIT_DELTA_IGNORED && 9 272 281
165 9 ▶ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED))
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) 3 6 9
166 3 return 0;
167  
168 278 ▶ if (status == GIT_DELTA_UNTRACKED &&
Branch: 1 branch
BranchTrueFalseTotal
if (status == GIT_DELTA_UNTRACKED && 202 76 278
169 202 ▶ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED)) 136 66 202
170 136 return 0;
171  
172 142 ▶ if (status == GIT_DELTA_UNREADABLE &&
Branch: 1 branch
BranchTrueFalseTotal
if (status == GIT_DELTA_UNREADABLE && 0 142 142
173 0 ▶ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE))
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE)) 0 0 0
174 0 return 0;
175  
176 142 ▶ if (!diff_pathspec_match(&matched_pathspec, diff, entry))
Branch: 1 branch
BranchTrueFalseTotal
if (!diff_pathspec_match(&matched_pathspec, diff, entry)) 0 142 142
177 0 return 0;
178  
179 142 delta = diff_delta__alloc(diff, status, entry->path);
180 142 ▶ GIT_ERROR_CHECK_ALLOC(delta);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(delta); 0 142 142
181  
182 /* This fn is just for single-sided diffs */
183 142 ▶ GIT_ASSERT(status != GIT_DELTA_MODIFIED);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(status != GIT_DELTA_MODIFIED); 0 142 142
184 142 delta->nfiles = 1;
185  
186 142 ▶ if (has_old) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_old) { 22 120 142
187 22 delta->old_file.mode = entry->mode;
188 22 delta->old_file.size = entry->file_size;
189 22 delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
190 22 git_oid_cpy(&delta->old_file.id, &entry->id);
191 22 delta->old_file.id_abbrev = GIT_OID_HEXSZ;
192 } else /* ADDED, IGNORED, UNTRACKED */ {
193 120 delta->new_file.mode = entry->mode;
194 120 delta->new_file.size = entry->file_size;
195 120 delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
196 120 git_oid_cpy(&delta->new_file.id, &entry->id);
197 120 delta->new_file.id_abbrev = GIT_OID_HEXSZ;
198 }
199  
200 142 delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
201  
202 142 ▶ if (has_old || !git_oid_is_zero(&delta->new_file.id))
Branch: 2 branches
BranchTrueFalseTotal
if (has_old || !git_oid_is_zero(&delta->new_file.id)) 120 22 142
if (has_old || !git_oid_is_zero(&delta->new_file.id)) 48 72 120
203 70 delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
204  
205 142 diff_delta__flag_known_sizes(delta);
206  
207 142 return diff_insert_delta(diff, delta, matched_pathspec);
208 }
209  
210 537 static int diff_delta__from_two(
211 git_diff_generated *diff,
212 git_delta_t status,
213 const git_index_entry *old_entry,
214 uint32_t old_mode,
215 const git_index_entry *new_entry,
216 uint32_t new_mode,
217 const git_oid *new_id,
218 const char *matched_pathspec)
219 {
220 537 const git_oid *old_id = &old_entry->id;
221 git_diff_delta *delta;
222 537 const char *canonical_path = old_entry->path;
223  
224 537 ▶ if (status == GIT_DELTA_UNMODIFIED &&
Branch: 1 branch
BranchTrueFalseTotal
if (status == GIT_DELTA_UNMODIFIED && 474 63 537
225 474 ▶ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED)) 386 88 474
226 386 return 0;
227  
228 151 ▶ if (!new_id)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_id) 139 12 151
229 139 new_id = &new_entry->id;
230  
231 151 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { 1 150 151
232 1 uint32_t temp_mode = old_mode;
233 1 const git_index_entry *temp_entry = old_entry;
234 1 const git_oid *temp_id = old_id;
235  
236 1 old_entry = new_entry;
237 1 new_entry = temp_entry;
238 1 old_mode = new_mode;
239 1 new_mode = temp_mode;
240 1 old_id = new_id;
241 1 new_id = temp_id;
242 }
243  
244 151 delta = diff_delta__alloc(diff, status, canonical_path);
245 151 ▶ GIT_ERROR_CHECK_ALLOC(delta);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(delta); 0 151 151
246 151 delta->nfiles = 2;
247  
248 151 ▶ if (!git_index_entry_is_conflict(old_entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_index_entry_is_conflict(old_entry)) { 151 0 151
249 151 delta->old_file.size = old_entry->file_size;
250 151 delta->old_file.mode = old_mode;
251 151 git_oid_cpy(&delta->old_file.id, old_id);
252 151 delta->old_file.id_abbrev = GIT_OID_HEXSZ;
253 151 delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
254 GIT_DIFF_FLAG_EXISTS;
255 }
256  
257 151 ▶ if (!git_index_entry_is_conflict(new_entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_index_entry_is_conflict(new_entry)) { 151 0 151
258 151 git_oid_cpy(&delta->new_file.id, new_id);
259 151 delta->new_file.id_abbrev = GIT_OID_HEXSZ;
260 151 delta->new_file.size = new_entry->file_size;
261 151 delta->new_file.mode = new_mode;
262 151 delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
263 151 delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
264  
265 151 ▶ if (!git_oid_is_zero(&new_entry->id))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_oid_is_zero(&new_entry->id)) 132 19 151
266 132 delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
267 }
268  
269 151 diff_delta__flag_known_sizes(delta);
270  
271 151 return diff_insert_delta(diff, delta, matched_pathspec);
272 }
273  
274 41 static git_diff_delta *diff_delta__last_for_item(
275 git_diff_generated *diff,
276 const git_index_entry *item)
277 {
278 41 git_diff_delta *delta = git_vector_last(&diff->base.deltas);
279 41 ▶ if (!delta)
Branch: 1 branch
BranchTrueFalseTotal
if (!delta) 29 12 41
280 29 return NULL;
281  
282 12 switch (delta->status) {
283 0 case GIT_DELTA_UNMODIFIED:
284 case GIT_DELTA_DELETED:
285 0 ▶ if (git_oid__cmp(&delta->old_file.id, &item->id) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid__cmp(&delta->old_file.id, &item->id) == 0) 0 0 0
286 0 return delta;
287 0 break;
288 0 case GIT_DELTA_ADDED:
289 0 ▶ if (git_oid__cmp(&delta->new_file.id, &item->id) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid__cmp(&delta->new_file.id, &item->id) == 0) 0 0 0
290 0 return delta;
291 0 break;
292 11 case GIT_DELTA_UNREADABLE:
293 case GIT_DELTA_UNTRACKED:
294 22 if (diff->base.strcomp(delta->new_file.path, item->path) == 0 &&
295 11 git_oid__cmp(&delta->new_file.id, &item->id) == 0)
296 11 return delta;
297 0 break;
298 0 case GIT_DELTA_MODIFIED:
299 0 ▶ if (git_oid__cmp(&delta->old_file.id, &item->id) == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid__cmp(&delta->old_file.id, &item->id) == 0 || 0 0 0
300 0 (delta->new_file.mode == item->mode &&
301 0 git_oid__cmp(&delta->new_file.id, &item->id) == 0))
302 0 return delta;
303 0 break;
304 1 default:
305 1 break;
306 }
307  
308 1 return NULL;
309 }
310  
311 604 static char *diff_strdup_prefix(git_pool *pool, const char *prefix)
312 {
313 604 size_t len = strlen(prefix);
314  
315 /* append '/' at end if needed */
316 604 ▶ if (len > 0 && prefix[len - 1] != '/')
Branch: 2 branches
BranchTrueFalseTotal
if (len > 0 && prefix[len - 1] != '/') 604 0 604
if (len > 0 && prefix[len - 1] != '/') 6 598 604
317 6 return git_pool_strcat(pool, prefix, "/");
318 else
319 598 return git_pool_strndup(pool, prefix, len + 1);
320 }
321  
322 40 GIT_INLINE(const char *) diff_delta__i2w_path(const git_diff_delta *delta)
323 {
324 40 return delta->old_file.path ?
325 40 ▶ delta->old_file.path : delta->new_file.path;
Branch: 1 branch
BranchTrueFalseTotal
delta->old_file.path : delta->new_file.path; 40 0 40
326 }
327  
328 20 static int diff_delta_i2w_cmp(const void *a, const void *b)
329 {
330 20 const git_diff_delta *da = a, *db = b;
331 20 int val = strcmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
332 20 ▶ return val ? val : ((int)da->status - (int)db->status);
Branch: 1 branch
BranchTrueFalseTotal
return val ? val : ((int)da->status - (int)db->status); 0 20 20
333 }
334  
335 0 static int diff_delta_i2w_casecmp(const void *a, const void *b)
336 {
337 0 const git_diff_delta *da = a, *db = b;
338 0 int val = strcasecmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
339 0 ▶ return val ? val : ((int)da->status - (int)db->status);
Branch: 1 branch
BranchTrueFalseTotal
return val ? val : ((int)da->status - (int)db->status); 0 0 0
340 }
341  
342 108 bool git_diff_delta__should_skip(
343 const git_diff_options *opts, const git_diff_delta *delta)
344 {
345 108 ▶ uint32_t flags = opts ? opts->flags : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint32_t flags = opts ? opts->flags : 0; 108 0 108
346  
347 108 ▶ if (delta->status == GIT_DELTA_UNMODIFIED &&
Branch: 1 branch
BranchTrueFalseTotal
if (delta->status == GIT_DELTA_UNMODIFIED && 0 108 108
348 0 ▶ (flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
Branch: 1 branch
BranchTrueFalseTotal
(flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0) 0 0 0
349 0 return true;
350  
351 108 ▶ if (delta->status == GIT_DELTA_IGNORED &&
Branch: 1 branch
BranchTrueFalseTotal
if (delta->status == GIT_DELTA_IGNORED && 2 106 108
352 2 ▶ (flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
Branch: 1 branch
BranchTrueFalseTotal
(flags & GIT_DIFF_INCLUDE_IGNORED) == 0) 0 2 2
353 0 return true;
354  
355 108 ▶ if (delta->status == GIT_DELTA_UNTRACKED &&
Branch: 1 branch
BranchTrueFalseTotal
if (delta->status == GIT_DELTA_UNTRACKED && 38 70 108
356 38 ▶ (flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
Branch: 1 branch
BranchTrueFalseTotal
(flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0) 0 38 38
357 0 return true;
358  
359 108 ▶ if (delta->status == GIT_DELTA_UNREADABLE &&
Branch: 1 branch
BranchTrueFalseTotal
if (delta->status == GIT_DELTA_UNREADABLE && 0 108 108
360 0 ▶ (flags & GIT_DIFF_INCLUDE_UNREADABLE) == 0)
Branch: 1 branch
BranchTrueFalseTotal
(flags & GIT_DIFF_INCLUDE_UNREADABLE) == 0) 0 0 0
361 0 return true;
362  
363 108 return false;
364 }
365  
366  
367 0 static const char *diff_mnemonic_prefix(
368 git_iterator_t type, bool left_side)
369 {
370 0 const char *pfx = "";
371  
372 0 switch (type) {
373 0 case GIT_ITERATOR_EMPTY: pfx = "c"; break;
374 0 case GIT_ITERATOR_TREE: pfx = "c"; break;
375 0 case GIT_ITERATOR_INDEX: pfx = "i"; break;
376 0 case GIT_ITERATOR_WORKDIR: pfx = "w"; break;
377 0 ▶ case GIT_ITERATOR_FS: pfx = left_side ? "1" : "2"; break;
Branch: 1 branch
BranchTrueFalseTotal
case GIT_ITERATOR_FS: pfx = left_side ? "1" : "2"; break; 0 0 0
378 0 default: break;
379 }
380  
381 /* note: without a deeper look at pathspecs, there is no easy way
382 * to get the (o)bject / (w)ork tree mnemonics working...
383 */
384  
385 0 return pfx;
386 }
387  
388 302 static void diff_set_ignore_case(git_diff *diff, bool ignore_case)
389 {
390 302 ▶ if (!ignore_case) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ignore_case) { 302 0 302
391 302 diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE;
392  
393 302 diff->strcomp = git__strcmp;
394 302 diff->strncomp = git__strncmp;
395 302 diff->pfxcomp = git__prefixcmp;
396 302 diff->entrycomp = git_diff__entry_cmp;
397  
398 302 git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
399 } else {
400 0 diff->opts.flags |= GIT_DIFF_IGNORE_CASE;
401  
402 0 diff->strcomp = git__strcasecmp;
403 0 diff->strncomp = git__strncasecmp;
404 0 diff->pfxcomp = git__prefixcmp_icase;
405 0 diff->entrycomp = git_diff__entry_icmp;
406  
407 0 git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
408 }
409  
410 302 git_vector_sort(&diff->deltas);
411 302 }
412  
413 302 static void diff_generated_free(git_diff *d)
414 {
415 302 git_diff_generated *diff = (git_diff_generated *)d;
416  
417 302 git_attr_session__free(&diff->base.attrsession);
418 302 git_vector_free_deep(&diff->base.deltas);
419  
420 302 git_pathspec__vfree(&diff->pathspec);
421 302 git_pool_clear(&diff->base.pool);
422  
423 302 git__memzero(diff, sizeof(*diff));
424 302 git__free(diff);
425 302 }
426  
427 302 static git_diff_generated *diff_generated_alloc(
428 git_repository *repo,
429 git_iterator *old_iter,
430 git_iterator *new_iter)
431 {
432 git_diff_generated *diff;
433 302 git_diff_options dflt = GIT_DIFF_OPTIONS_INIT;
434  
435 302 ▶ GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL); 0 302 302
436 302 ▶ GIT_ASSERT_ARG_WITH_RETVAL(old_iter, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(old_iter, NULL); 0 302 302
437 302 ▶ GIT_ASSERT_ARG_WITH_RETVAL(new_iter, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(new_iter, NULL); 0 302 302
438  
439 302 ▶ if ((diff = git__calloc(1, sizeof(git_diff_generated))) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((diff = git__calloc(1, sizeof(git_diff_generated))) == NULL) 0 302 302
440 0 return NULL;
441  
442 302 GIT_REFCOUNT_INC(&diff->base);
443 302 diff->base.type = GIT_DIFF_TYPE_GENERATED;
444 302 diff->base.repo = repo;
445 302 diff->base.old_src = old_iter->type;
446 302 diff->base.new_src = new_iter->type;
447 302 diff->base.patch_fn = git_patch_generated_from_diff;
448 302 diff->base.free_fn = diff_generated_free;
449 302 git_attr_session__init(&diff->base.attrsession, repo);
450 302 memcpy(&diff->base.opts, &dflt, sizeof(git_diff_options));
451  
452 604 if (git_pool_init(&diff->base.pool, 1) < 0 ||
453 302 git_vector_init(&diff->base.deltas, 0, git_diff_delta__cmp) < 0) {
454 0 git_diff_free(&diff->base);
455 0 return NULL;
456 }
457  
458 /* Use case-insensitive compare if either iterator has
459 * the ignore_case bit set */
460 302 diff_set_ignore_case(
461 302 &diff->base,
462 604 git_iterator_ignore_case(old_iter) ||
463 302 git_iterator_ignore_case(new_iter));
464  
465 302 return diff;
466 }
467  
468 302 static int diff_generated_apply_options(
469 git_diff_generated *diff,
470 const git_diff_options *opts)
471 {
472 302 git_config *cfg = NULL;
473 302 git_repository *repo = diff->base.repo;
474 302 git_pool *pool = &diff->base.pool;
475 int val;
476  
477 302 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 298 4 302
478 /* copy user options (except case sensitivity info from iterators) */
479 298 bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE);
480 298 memcpy(&diff->base.opts, opts, sizeof(diff->base.opts));
481 298 ▶ DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase);
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase); 0 298 298
482  
483 /* initialize pathspec from options */
484 298 ▶ if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0) 0 298 298
485 0 return -1;
486 }
487  
488 /* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */
489 302 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES))
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES)) 53 249 302
490 53 diff->base.opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
491  
492 /* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */
493 302 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT))
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT)) 0 302 302
494 0 diff->base.opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
495  
496 /* load config values that affect diff behavior */
497 302 ▶ if ((val = git_repository_config_snapshot(&cfg, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((val = git_repository_config_snapshot(&cfg, repo)) < 0) 0 302 302
498 0 return val;
499  
500 302 ▶ if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_SYMLINKS) && val)
Branch: 2 branches
BranchTrueFalseTotal
if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_SYMLINKS) && val) 302 0 302
if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_SYMLINKS) && val) 302 0 302
501 302 diff->diffcaps |= GIT_DIFFCAPS_HAS_SYMLINKS;
502  
503 302 ▶ if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_IGNORESTAT) && val)
Branch: 2 branches
BranchTrueFalseTotal
if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_IGNORESTAT) && val) 302 0 302
if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_IGNORESTAT) && val) 0 302 302
504 0 diff->diffcaps |= GIT_DIFFCAPS_IGNORE_STAT;
505  
506 604 if ((diff->base.opts.flags & GIT_DIFF_IGNORE_FILEMODE) == 0 &&
507 604 ▶ !git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_FILEMODE) && val)
Branch: 1 branch
BranchTrueFalseTotal
!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_FILEMODE) && val) 302 0 302
508 302 diff->diffcaps |= GIT_DIFFCAPS_TRUST_MODE_BITS;
509  
510 302 ▶ if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_TRUSTCTIME) && val)
Branch: 2 branches
BranchTrueFalseTotal
if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_TRUSTCTIME) && val) 302 0 302
if (!git_config__configmap_lookup(&val, cfg, GIT_CONFIGMAP_TRUSTCTIME) && val) 302 0 302
511 302 diff->diffcaps |= GIT_DIFFCAPS_TRUST_CTIME;
512  
513 /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
514  
515 /* If not given explicit `opts`, check `diff.xyz` configs */
516 302 ▶ if (!opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (!opts) { 4 298 302
517 4 int context = git_config__get_int_force(cfg, "diff.context", 3);
518 4 ▶ diff->base.opts.context_lines = context >= 0 ? (uint32_t)context : 3;
Branch: 1 branch
BranchTrueFalseTotal
diff->base.opts.context_lines = context >= 0 ? (uint32_t)context : 3; 4 0 4
519  
520 /* add other defaults here */
521 }
522  
523 /* Reverse src info if diff is reversed */
524 302 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { 2 300 302
525 2 git_iterator_t tmp_src = diff->base.old_src;
526 2 diff->base.old_src = diff->base.new_src;
527 2 diff->base.new_src = tmp_src;
528 }
529  
530 /* Unset UPDATE_INDEX unless diffing workdir and index */
531 302 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) &&
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && 0 302 302
532 0 ▶ (!(diff->base.old_src == GIT_ITERATOR_WORKDIR ||
Branch: 1 branch
BranchTrueFalseTotal
(!(diff->base.old_src == GIT_ITERATOR_WORKDIR || 0 0 0
533 0 ▶ diff->base.new_src == GIT_ITERATOR_WORKDIR) ||
Branch: 1 branch
BranchTrueFalseTotal
diff->base.new_src == GIT_ITERATOR_WORKDIR) || 0 0 0
534 0 ▶ !(diff->base.old_src == GIT_ITERATOR_INDEX ||
Branch: 1 branch
BranchTrueFalseTotal
!(diff->base.old_src == GIT_ITERATOR_INDEX || 0 0 0
535 0 ▶ diff->base.new_src == GIT_ITERATOR_INDEX)))
Branch: 1 branch
BranchTrueFalseTotal
diff->base.new_src == GIT_ITERATOR_INDEX))) 0 0 0
536 0 diff->base.opts.flags &= ~GIT_DIFF_UPDATE_INDEX;
537  
538 /* if ignore_submodules not explicitly set, check diff config */
539 302 ▶ if (diff->base.opts.ignore_submodules <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff->base.opts.ignore_submodules <= 0) { 284 18 302
540 git_config_entry *entry;
541 284 git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true);
542  
543 284 if (entry && git_submodule_parse_ignore(
544 0 &diff->base.opts.ignore_submodules, entry->value) < 0)
545 0 git_error_clear();
546 284 git_config_entry_free(entry);
547 }
548  
549 /* if either prefix is not set, figure out appropriate value */
550 302 ▶ if (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) {
Branch: 2 branches
BranchTrueFalseTotal
if (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) { 3 299 302
if (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) { 0 3 3
551 299 const char *use_old = DIFF_OLD_PREFIX_DEFAULT;
552 299 const char *use_new = DIFF_NEW_PREFIX_DEFAULT;
553  
554 299 ▶ if (git_config__get_bool_force(cfg, "diff.noprefix", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (git_config__get_bool_force(cfg, "diff.noprefix", 0)) 0 299 299
555 0 use_old = use_new = "";
556 299 ▶ else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) { 0 299 299
557 0 use_old = diff_mnemonic_prefix(diff->base.old_src, true);
558 0 use_new = diff_mnemonic_prefix(diff->base.new_src, false);
559 }
560  
561 299 ▶ if (!diff->base.opts.old_prefix)
Branch: 1 branch
BranchTrueFalseTotal
if (!diff->base.opts.old_prefix) 299 0 299
562 299 diff->base.opts.old_prefix = use_old;
563 299 ▶ if (!diff->base.opts.new_prefix)
Branch: 1 branch
BranchTrueFalseTotal
if (!diff->base.opts.new_prefix) 299 0 299
564 299 diff->base.opts.new_prefix = use_new;
565 }
566  
567 /* strdup prefix from pool so we're not dependent on external data */
568 302 diff->base.opts.old_prefix = diff_strdup_prefix(pool, diff->base.opts.old_prefix);
569 302 diff->base.opts.new_prefix = diff_strdup_prefix(pool, diff->base.opts.new_prefix);
570  
571 302 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { 2 300 302
572 2 const char *tmp_prefix = diff->base.opts.old_prefix;
573 2 diff->base.opts.old_prefix = diff->base.opts.new_prefix;
574 2 diff->base.opts.new_prefix = tmp_prefix;
575 }
576  
577 302 git_config_free(cfg);
578  
579 /* check strdup results for error */
580 302 ▶ return (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) ? -1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) ? -1 : 0; 302 0 302
return (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) ? -1 : 0; 0 302 302
581 }
582  
583 1 int git_diff__oid_for_file(
584 git_oid *out,
585 git_diff *diff,
586 const char *path,
587 uint16_t mode,
588 git_object_size_t size)
589 {
590 git_index_entry entry;
591  
592 1 ▶ if (size > UINT32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > UINT32_MAX) { 0 1 1
593 0 git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'", path);
594 0 return -1;
595 }
596  
597 1 memset(&entry, 0, sizeof(entry));
598 1 entry.mode = mode;
599 1 entry.file_size = (uint32_t)size;
600 1 entry.path = (char *)path;
601  
602 1 return git_diff__oid_for_entry(out, diff, &entry, mode, NULL);
603 }
604  
605 165 int git_diff__oid_for_entry(
606 git_oid *out,
607 git_diff *d,
608 const git_index_entry *src,
609 uint16_t mode,
610 const git_oid *update_match)
611 {
612 git_diff_generated *diff;
613 165 git_str full_path = GIT_STR_INIT;
614 165 git_index_entry entry = *src;
615 165 git_filter_list *fl = NULL;
616 165 int error = 0;
617  
618 165 ▶ GIT_ASSERT(d->type == GIT_DIFF_TYPE_GENERATED);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(d->type == GIT_DIFF_TYPE_GENERATED); 0 165 165
619 165 diff = (git_diff_generated *)d;
620  
621 165 memset(out, 0, sizeof(*out));
622  
623 165 ▶ if (git_repository_workdir_path(&full_path, diff->base.repo, entry.path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository_workdir_path(&full_path, diff->base.repo, entry.path) < 0) 0 165 165
624 0 return -1;
625  
626 165 ▶ if (!mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mode) { 0 165 165
627 struct stat st;
628  
629 0 diff->base.perf.stat_calls++;
630  
631 0 ▶ if (p_stat(full_path.ptr, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_stat(full_path.ptr, &st) < 0) { 0 0 0
632 0 error = git_fs_path_set_error(errno, entry.path, "stat");
633 0 git_str_dispose(&full_path);
634 0 return error;
635 }
636  
637 0 git_index_entry__init_from_stat(&entry,
638 0 &st, (diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) != 0);
639 }
640  
641 /* calculate OID for file if possible */
642 165 ▶ if (S_ISGITLINK(mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISGITLINK(mode)) { 0 165 165
643 git_submodule *sm;
644  
645 0 ▶ if (!git_submodule_lookup(&sm, diff->base.repo, entry.path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_submodule_lookup(&sm, diff->base.repo, entry.path)) { 0 0 0
646 0 const git_oid *sm_oid = git_submodule_wd_id(sm);
647 0 ▶ if (sm_oid)
Branch: 1 branch
BranchTrueFalseTotal
if (sm_oid) 0 0 0
648 0 git_oid_cpy(out, sm_oid);
649 0 git_submodule_free(sm);
650 } else {
651 /* if submodule lookup failed probably just in an intermediate
652 * state where some init hasn't happened, so ignore the error
653 */
654 0 git_error_clear();
655 }
656 165 ▶ } else if (S_ISLNK(mode)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (S_ISLNK(mode)) { 0 165 165
657 0 error = git_odb__hashlink(out, full_path.ptr);
658 0 diff->base.perf.oid_calculations++;
659 165 ▶ } else if (!git__is_sizet(entry.file_size)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!git__is_sizet(entry.file_size)) { 0 165 165
660 0 git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'",
661 entry.path);
662 0 error = -1;
663 165 ▶ } else if (!(error = git_filter_list_load(&fl,
Branch: 1 branch
BranchTrueFalseTotal
} else if (!(error = git_filter_list_load(&fl, 165 0 165
664 diff->base.repo, NULL, entry.path,
665 GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)))
666 {
667 165 int fd = git_futils_open_ro(full_path.ptr);
668 165 ▶ if (fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) 0 165 165
669 0 error = fd;
670 else {
671 165 error = git_odb__hashfd_filtered(
672 165 out, fd, (size_t)entry.file_size, GIT_OBJECT_BLOB, fl);
673 165 p_close(fd);
674 165 diff->base.perf.oid_calculations++;
675 }
676  
677 165 git_filter_list_free(fl);
678 }
679  
680 /* update index for entry if requested */
681 165 ▶ if (!error && update_match && git_oid_equal(out, update_match)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!error && update_match && git_oid_equal(out, update_match)) { 165 0 165
if (!error && update_match && git_oid_equal(out, update_match)) { 0 165 165
if (!error && update_match && git_oid_equal(out, update_match)) { 0 0 0
682 git_index *idx;
683 git_index_entry updated_entry;
684  
685 0 memcpy(&updated_entry, &entry, sizeof(git_index_entry));
686 0 updated_entry.mode = mode;
687 0 git_oid_cpy(&updated_entry.id, out);
688  
689 0 ▶ if (!(error = git_repository_index__weakptr(&idx,
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = git_repository_index__weakptr(&idx, 0 0 0
690 diff->base.repo))) {
691 0 error = git_index_add(idx, &updated_entry);
692 0 diff->index_updated = true;
693 }
694 }
695  
696 165 git_str_dispose(&full_path);
697 165 return error;
698 }
699  
700 typedef struct {
701 git_repository *repo;
702 git_iterator *old_iter;
703 git_iterator *new_iter;
704 const git_index_entry *oitem;
705 const git_index_entry *nitem;
706 git_strmap *submodule_cache;
707 bool submodule_cache_initialized;
708 } diff_in_progress;
709  
710 #define MODE_BITS_MASK 0000777
711  
712 0 static int maybe_modified_submodule(
713 git_delta_t *status,
714 git_oid *found_oid,
715 git_diff_generated *diff,
716 diff_in_progress *info)
717 {
718 0 int error = 0;
719 git_submodule *sub;
720 0 unsigned int sm_status = 0;
721 0 git_submodule_ignore_t ign = diff->base.opts.ignore_submodules;
722 0 git_strmap *submodule_cache = NULL;
723  
724 0 *status = GIT_DELTA_UNMODIFIED;
725  
726 0 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) ||
Branch: 2 branches
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) || 0 0 0
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) || 0 0 0
727 ign == GIT_SUBMODULE_IGNORE_ALL)
728 0 return 0;
729  
730 0 ▶ if (diff->base.repo->submodule_cache != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff->base.repo->submodule_cache != NULL) { 0 0 0
731 0 submodule_cache = diff->base.repo->submodule_cache;
732 } else {
733 0 ▶ if (!info->submodule_cache_initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (!info->submodule_cache_initialized) { 0 0 0
734 0 info->submodule_cache_initialized = true;
735 /*
736 * Try to cache the submodule information to avoid having to parse it for
737 * every submodule. It is okay if it fails, the cache will still be NULL
738 * and the submodules will be attempted to be looked up individually.
739 */
740 0 git_submodule_cache_init(&info->submodule_cache, diff->base.repo);
741 }
742 0 submodule_cache = info->submodule_cache;
743 }
744  
745 0 ▶ if ((error = git_submodule__lookup_with_cache(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_submodule__lookup_with_cache( 0 0 0
746 0 &sub, diff->base.repo, info->nitem->path, submodule_cache)) < 0) {
747  
748 /* GIT_EEXISTS means dir with .git in it was found - ignore it */
749 0 ▶ if (error == GIT_EEXISTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_EEXISTS) { 0 0 0
750 0 git_error_clear();
751 0 error = 0;
752 }
753 0 return error;
754 }
755  
756 0 ▶ if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL)
Branch: 2 branches
BranchTrueFalseTotal
if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL) 0 0 0
if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL) 0 0 0
757 /* ignore it */;
758 0 ▶ else if ((error = git_submodule__status(
Branch: 1 branch
BranchTrueFalseTotal
else if ((error = git_submodule__status( 0 0 0
759 &sm_status, NULL, NULL, found_oid, sub, ign)) < 0)
760 /* return error below */;
761  
762 /* check IS_WD_UNMODIFIED because this case is only used
763 * when the new side of the diff is the working directory
764 */
765 0 ▶ else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status))
Branch: 1 branch
BranchTrueFalseTotal
else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status)) 0 0 0
766 0 *status = GIT_DELTA_MODIFIED;
767  
768 /* now that we have a HEAD OID, check if HEAD moved */
769 0 else if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
770 0 !git_oid_equal(&info->oitem->id, found_oid))
771 0 *status = GIT_DELTA_MODIFIED;
772  
773 0 git_submodule_free(sub);
774 0 return error;
775 }
776  
777 539 static int maybe_modified(
778 git_diff_generated *diff,
779 diff_in_progress *info)
780 {
781 git_oid noid;
782 539 git_delta_t status = GIT_DELTA_MODIFIED;
783 539 const git_index_entry *oitem = info->oitem;
784 539 const git_index_entry *nitem = info->nitem;
785 539 unsigned int omode = oitem->mode;
786 539 unsigned int nmode = nitem->mode;
787 539 bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_WORKDIR);
788 539 bool modified_uncertain = false;
789 const char *matched_pathspec;
790 539 int error = 0;
791  
792 539 ▶ if (!diff_pathspec_match(&matched_pathspec, diff, oitem))
Branch: 1 branch
BranchTrueFalseTotal
if (!diff_pathspec_match(&matched_pathspec, diff, oitem)) 2 537 539
793 2 return 0;
794  
795 537 memset(&noid, 0, sizeof(noid));
796  
797 /* on platforms with no symlinks, preserve mode of existing symlinks */
798 537 ▶ if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir &&
Branch: 3 branches
BranchTrueFalseTotal
if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir && 0 537 537
if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir && 0 0 0
if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir && 0 0 0
799 0 ▶ !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS))
Branch: 1 branch
BranchTrueFalseTotal
!(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS)) 0 0 0
800 0 nmode = omode;
801  
802 /* on platforms with no execmode, just preserve old mode */
803 537 ▶ if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) && 0 537 537
804 0 ▶ (nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) &&
Branch: 2 branches
BranchTrueFalseTotal
(nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) && 0 0 0
(nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) && 0 0 0
805 new_is_workdir)
806 0 nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
807  
808 /* if one side is a conflict, mark the whole delta as conflicted */
809 1074 if (git_index_entry_is_conflict(oitem) ||
810 537 git_index_entry_is_conflict(nitem)) {
811 0 status = GIT_DELTA_CONFLICTED;
812  
813 /* support "assume unchanged" (poorly, b/c we still stat everything) */
814 537 ▶ } else if ((oitem->flags & GIT_INDEX_ENTRY_VALID) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ((oitem->flags & GIT_INDEX_ENTRY_VALID) != 0) { 0 537 537
815 0 status = GIT_DELTA_UNMODIFIED;
816  
817 /* support "skip worktree" index bit */
818 537 ▶ } else if ((oitem->flags_extended & GIT_INDEX_ENTRY_SKIP_WORKTREE) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ((oitem->flags_extended & GIT_INDEX_ENTRY_SKIP_WORKTREE) != 0) { 0 537 537
819 0 status = GIT_DELTA_UNMODIFIED;
820  
821 /* if basic type of file changed, then split into delete and add */
822 537 ▶ } else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) { 0 537 537
823 0 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) { 0 0 0
824 0 status = GIT_DELTA_TYPECHANGE;
825 }
826  
827 0 ▶ else if (nmode == GIT_FILEMODE_UNREADABLE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (nmode == GIT_FILEMODE_UNREADABLE) { 0 0 0
828 0 ▶ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL))) 0 0 0
829 0 error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, NULL, nitem);
830 0 return error;
831 }
832  
833 else {
834 0 ▶ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL))) 0 0 0
835 0 error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
836 0 return error;
837 }
838  
839 /* if oids and modes match (and are valid), then file is unmodified */
840 537 ▶ } else if (git_oid_equal(&oitem->id, &nitem->id) &&
Branch: 2 branches
BranchTrueFalseTotal
} else if (git_oid_equal(&oitem->id, &nitem->id) && 289 248 537
} else if (git_oid_equal(&oitem->id, &nitem->id) && 289 0 289
841 289 ▶ omode == nmode &&
Branch: 1 branch
BranchTrueFalseTotal
omode == nmode && 289 0 289
842 289 !git_oid_is_zero(&oitem->id)) {
843 289 status = GIT_DELTA_UNMODIFIED;
844  
845 /* if we have an unknown OID and a workdir iterator, then check some
846 * circumstances that can accelerate things or need special handling
847 */
848 452 ▶ } else if (git_oid_is_zero(&nitem->id) && new_is_workdir) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (git_oid_is_zero(&nitem->id) && new_is_workdir) { 204 44 248
} else if (git_oid_is_zero(&nitem->id) && new_is_workdir) { 204 0 204
849 204 bool use_ctime =
850 204 ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
851 204 git_index *index = git_iterator_index(info->new_iter);
852  
853 204 status = GIT_DELTA_UNMODIFIED;
854  
855 204 ▶ if (S_ISGITLINK(nmode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISGITLINK(nmode)) { 0 204 204
856 0 ▶ if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0) 0 0 0
857 0 return error;
858 }
859  
860 /* if the stat data looks different, then mark modified - this just
861 * means that the OID will be recalculated below to confirm change
862 */
863 204 ▶ else if (omode != nmode || oitem->file_size != nitem->file_size) {
Branch: 2 branches
BranchTrueFalseTotal
else if (omode != nmode || oitem->file_size != nitem->file_size) { 204 0 204
else if (omode != nmode || oitem->file_size != nitem->file_size) { 26 178 204
864 26 status = GIT_DELTA_MODIFIED;
865 26 modified_uncertain =
866 26 ▶ (oitem->file_size <= 0 && nitem->file_size > 0);
Branch: 2 branches
BranchTrueFalseTotal
(oitem->file_size <= 0 && nitem->file_size > 0); 19 7 26
(oitem->file_size <= 0 && nitem->file_size > 0); 19 0 19
867 }
868 178 ▶ else if (!git_index_time_eq(&oitem->mtime, &nitem->mtime) ||
Branch: 2 branches
BranchTrueFalseTotal
else if (!git_index_time_eq(&oitem->mtime, &nitem->mtime) || 178 0 178
else if (!git_index_time_eq(&oitem->mtime, &nitem->mtime) || 178 0 178
869 178 ▶ (use_ctime && !git_index_time_eq(&oitem->ctime, &nitem->ctime)) ||
Branch: 1 branch
BranchTrueFalseTotal
(use_ctime && !git_index_time_eq(&oitem->ctime, &nitem->ctime)) || 178 0 178
870 178 ▶ oitem->ino != nitem->ino ||
Branch: 1 branch
BranchTrueFalseTotal
oitem->ino != nitem->ino || 178 0 178
871 178 ▶ oitem->uid != nitem->uid ||
Branch: 1 branch
BranchTrueFalseTotal
oitem->uid != nitem->uid || 178 0 178
872 356 oitem->gid != nitem->gid ||
873 178 git_index_entry_newer_than_index(nitem, index))
874 {
875 97 status = GIT_DELTA_MODIFIED;
876 97 modified_uncertain = true;
877 }
878  
879 /* if mode is GITLINK and submodules are ignored, then skip */
880 44 ▶ } else if (S_ISGITLINK(nmode) &&
Branch: 1 branch
BranchTrueFalseTotal
} else if (S_ISGITLINK(nmode) && 0 44 44
881 0 ▶ DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) {
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) { 0 0 0
882 0 status = GIT_DELTA_UNMODIFIED;
883 }
884  
885 /* if we got here and decided that the files are modified, but we
886 * haven't calculated the OID of the new item, then calculate it now
887 */
888 537 ▶ if (modified_uncertain && git_oid_is_zero(&nitem->id)) {
Branch: 2 branches
BranchTrueFalseTotal
if (modified_uncertain && git_oid_is_zero(&nitem->id)) { 116 421 537
if (modified_uncertain && git_oid_is_zero(&nitem->id)) { 116 0 116
889 116 const git_oid *update_check =
890 0 ▶ DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ?
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ? 0 0 0
891 116 ▶ &oitem->id : NULL;
Branch: 1 branch
BranchTrueFalseTotal
&oitem->id : NULL; 0 116 116
892  
893 116 ▶ if ((error = git_diff__oid_for_entry(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_diff__oid_for_entry( 0 116 116
894 116 &noid, &diff->base, nitem, nmode, update_check)) < 0)
895 0 return error;
896  
897 /* if oid matches, then mark unmodified (except submodules, where
898 * the filesystem content may be modified even if the oid still
899 * matches between the index and the workdir HEAD)
900 */
901 232 ▶ if (omode == nmode && !S_ISGITLINK(omode) &&
Branch: 1 branch
BranchTrueFalseTotal
if (omode == nmode && !S_ISGITLINK(omode) && 116 0 116
902 116 git_oid_equal(&oitem->id, &noid))
903 104 status = GIT_DELTA_UNMODIFIED;
904 }
905  
906 /* If we want case changes, then break this into a delete of the old
907 * and an add of the new so that consumers can act accordingly (eg,
908 * checkout will update the case on disk.)
909 */
910 537 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE) &&
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE) && 0 537 537
911 0 ▶ DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) &&
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) && 0 0 0
912 0 ▶ strcmp(oitem->path, nitem->path) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
strcmp(oitem->path, nitem->path) != 0) { 0 0 0
913  
914 0 ▶ if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL))) 0 0 0
915 0 error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
916  
917 0 return error;
918 }
919  
920 537 ▶ return diff_delta__from_two(
Branch: 1 branch
BranchTrueFalseTotal
return diff_delta__from_two( 116 421 537
921 diff, status, oitem, omode, nitem, nmode,
922 537 git_oid_is_zero(&noid) ? NULL : &noid, matched_pathspec);
923 }
924  
925 382 static bool entry_is_prefixed(
926 git_diff_generated *diff,
927 const git_index_entry *item,
928 const git_index_entry *prefix_item)
929 {
930 size_t pathlen;
931  
932 382 ▶ if (!item || diff->base.pfxcomp(item->path, prefix_item->path) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!item || diff->base.pfxcomp(item->path, prefix_item->path) != 0) 279 103 382
if (!item || diff->base.pfxcomp(item->path, prefix_item->path) != 0) 169 110 279
933 272 return false;
934  
935 110 pathlen = strlen(prefix_item->path);
936  
937 112 return (prefix_item->path[pathlen - 1] == '/' ||
938 112 ▶ item->path[pathlen] == '\0' ||
Branch: 2 branches
BranchTrueFalseTotal
item->path[pathlen] == '\0' || 2 108 110
item->path[pathlen] == '\0' || 2 0 2
939 2 ▶ item->path[pathlen] == '/');
Branch: 1 branch
BranchTrueFalseTotal
item->path[pathlen] == '/'); 0 2 2
940 }
941  
942 604 static int iterator_current(
943 const git_index_entry **entry,
944 git_iterator *iterator)
945 {
946 int error;
947  
948 604 ▶ if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) { 50 554 604
949 50 *entry = NULL;
950 50 error = 0;
951 }
952  
953 604 return error;
954 }
955  
956 1348 static int iterator_advance(
957 const git_index_entry **entry,
958 git_iterator *iterator)
959 {
960 1348 const git_index_entry *prev_entry = *entry;
961 int cmp, error;
962  
963 /* if we're looking for conflicts, we only want to report
964 * one conflict for each file, instead of all three sides.
965 * so if this entry is a conflict for this file, and the
966 * previous one was a conflict for the same file, skip it.
967 */
968 1348 ▶ while ((error = git_iterator_advance(entry, iterator)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((error = git_iterator_advance(entry, iterator)) == 0) { 795 553 1348
969 1107 if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
970 312 ▶ !git_index_entry_is_conflict(prev_entry) ||
Branch: 1 branch
BranchTrueFalseTotal
!git_index_entry_is_conflict(prev_entry) || 0 0 0
971 0 !git_index_entry_is_conflict(*entry))
972 break;
973  
974 0 cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
975 0 ▶ strcasecmp(prev_entry->path, (*entry)->path) :
Branch: 1 branch
BranchTrueFalseTotal
strcasecmp(prev_entry->path, (*entry)->path) : 0 0 0
976 0 strcmp(prev_entry->path, (*entry)->path);
977  
978 0 ▶ if (cmp)
Branch: 1 branch
BranchTrueFalseTotal
if (cmp) 0 0 0
979 0 break;
980 }
981  
982 1348 ▶ if (error == GIT_ITEROVER) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ITEROVER) { 553 795 1348
983 553 *entry = NULL;
984 553 error = 0;
985 }
986  
987 1348 return error;
988 }
989  
990 114 static int iterator_advance_into(
991 const git_index_entry **entry,
992 git_iterator *iterator)
993 {
994 int error;
995  
996 114 ▶ if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) { 0 114 114
997 0 *entry = NULL;
998 0 error = 0;
999 }
1000  
1001 114 return error;
1002 }
1003  
1004 11 static int iterator_advance_over(
1005 const git_index_entry **entry,
1006 git_iterator_status_t *status,
1007 git_iterator *iterator)
1008 {
1009 11 int error = git_iterator_advance_over(entry, status, iterator);
1010  
1011 11 ▶ if (error == GIT_ITEROVER) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ITEROVER) { 1 10 11
1012 1 *entry = NULL;
1013 1 error = 0;
1014 }
1015  
1016 11 return error;
1017 }
1018  
1019 374 static int handle_unmatched_new_item(
1020 git_diff_generated *diff, diff_in_progress *info)
1021 {
1022 374 int error = 0;
1023 374 const git_index_entry *nitem = info->nitem;
1024 374 git_delta_t delta_type = GIT_DELTA_UNTRACKED;
1025 bool contains_oitem;
1026  
1027 /* check if this is a prefix of the other side */
1028 374 contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
1029  
1030 /* update delta_type if this item is conflicted */
1031 374 ▶ if (git_index_entry_is_conflict(nitem))
Branch: 1 branch
BranchTrueFalseTotal
if (git_index_entry_is_conflict(nitem)) 0 374 374
1032 0 delta_type = GIT_DELTA_CONFLICTED;
1033  
1034 /* update delta_type if this item is ignored */
1035 374 ▶ else if (git_iterator_current_is_ignored(info->new_iter))
Branch: 1 branch
BranchTrueFalseTotal
else if (git_iterator_current_is_ignored(info->new_iter)) 9 365 374
1036 9 delta_type = GIT_DELTA_IGNORED;
1037  
1038 374 ▶ if (nitem->mode == GIT_FILEMODE_TREE) {
Branch: 1 branch
BranchTrueFalseTotal
if (nitem->mode == GIT_FILEMODE_TREE) { 155 219 374
1039 155 bool recurse_into_dir = contains_oitem;
1040  
1041 /* check if user requests recursion into this type of dir */
1042 47 ▶ recurse_into_dir = contains_oitem ||
Branch: 1 branch
BranchTrueFalseTotal
recurse_into_dir = contains_oitem || 47 0 47
1043 47 (delta_type == GIT_DELTA_UNTRACKED &&
1044 202 ▶ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) ||
Branch: 3 branches
BranchTrueFalseTotal
DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) || 47 108 155
DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) || 41 6 47
DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) || 0 41 41
1045 0 (delta_type == GIT_DELTA_IGNORED &&
1046 0 ▶ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS));
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS)); 0 0 0
1047  
1048 /* do not advance into directories that contain a .git file */
1049 155 ▶ if (recurse_into_dir && !contains_oitem) {
Branch: 2 branches
BranchTrueFalseTotal
if (recurse_into_dir && !contains_oitem) { 114 41 155
if (recurse_into_dir && !contains_oitem) { 6 108 114
1050 6 git_str *full = NULL;
1051 6 ▶ if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_iterator_current_workdir_path(&full, info->new_iter) < 0) 0 6 6
1052 0 return -1;
1053 6 ▶ if (full && git_fs_path_contains(full, DOT_GIT)) {
Branch: 2 branches
BranchTrueFalseTotal
if (full && git_fs_path_contains(full, DOT_GIT)) { 6 0 6
if (full && git_fs_path_contains(full, DOT_GIT)) { 0 6 6
1054 /* TODO: warning if not a valid git repository */
1055 0 recurse_into_dir = false;
1056 }
1057 }
1058  
1059 /* still have to look into untracked directories to match core git -
1060 * with no untracked files, directory is treated as ignored
1061 */
1062 155 ▶ if (!recurse_into_dir &&
Branch: 2 branches
BranchTrueFalseTotal
if (!recurse_into_dir && 41 114 155
if (!recurse_into_dir && 41 0 41
1063 41 delta_type == GIT_DELTA_UNTRACKED &&
1064 41 ▶ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS))
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS)) 41 0 41
1065 {
1066 git_diff_delta *last;
1067 git_iterator_status_t untracked_state;
1068  
1069 /* attempt to insert record for this directory */
1070 41 ▶ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0) 0 41 41
1071 0 return error;
1072  
1073 /* if delta wasn't created (because of rules), just skip ahead */
1074 41 last = diff_delta__last_for_item(diff, nitem);
1075 41 ▶ if (!last)
Branch: 1 branch
BranchTrueFalseTotal
if (!last) 30 11 41
1076 30 return iterator_advance(&info->nitem, info->new_iter);
1077  
1078 /* iterate into dir looking for an actual untracked file */
1079 11 ▶ if ((error = iterator_advance_over(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = iterator_advance_over( 0 11 11
1080 &info->nitem, &untracked_state, info->new_iter)) < 0)
1081 0 return error;
1082  
1083 /* if we found nothing that matched our pathlist filter, exclude */
1084 11 ▶ if (untracked_state == GIT_ITERATOR_STATUS_FILTERED) {
Branch: 1 branch
BranchTrueFalseTotal
if (untracked_state == GIT_ITERATOR_STATUS_FILTERED) { 0 11 11
1085 0 git_vector_pop(&diff->base.deltas);
1086 0 git__free(last);
1087 }
1088  
1089 /* if we found nothing or just ignored items, update the record */
1090 11 ▶ if (untracked_state == GIT_ITERATOR_STATUS_IGNORED ||
Branch: 1 branch
BranchTrueFalseTotal
if (untracked_state == GIT_ITERATOR_STATUS_IGNORED || 11 0 11
1091 11 ▶ untracked_state == GIT_ITERATOR_STATUS_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
untracked_state == GIT_ITERATOR_STATUS_EMPTY) { 1 10 11
1092 1 last->status = GIT_DELTA_IGNORED;
1093  
1094 /* remove the record if we don't want ignored records */
1095 1 ▶ if (DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) { 0 1 1
1096 0 git_vector_pop(&diff->base.deltas);
1097 0 git__free(last);
1098 }
1099 }
1100  
1101 11 return 0;
1102 }
1103  
1104 /* try to advance into directory if necessary */
1105 114 ▶ if (recurse_into_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (recurse_into_dir) { 114 0 114
1106 114 error = iterator_advance_into(&info->nitem, info->new_iter);
1107  
1108 /* if directory is empty, can't advance into it, so skip it */
1109 114 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 0 114 114
1110 0 git_error_clear();
1111 0 error = iterator_advance(&info->nitem, info->new_iter);
1112 }
1113  
1114 114 return error;
1115 }
1116 }
1117  
1118 219 ▶ else if (delta_type == GIT_DELTA_IGNORED &&
Branch: 1 branch
BranchTrueFalseTotal
else if (delta_type == GIT_DELTA_IGNORED && 9 210 219
1119 16 DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS) &&
1120 7 git_iterator_current_tree_is_ignored(info->new_iter))
1121 /* item contained in ignored directory, so skip over it */
1122 0 return iterator_advance(&info->nitem, info->new_iter);
1123  
1124 219 ▶ else if (info->new_iter->type != GIT_ITERATOR_WORKDIR) {
Branch: 1 branch
BranchTrueFalseTotal
else if (info->new_iter->type != GIT_ITERATOR_WORKDIR) { 49 170 219
1125 49 ▶ if (delta_type != GIT_DELTA_CONFLICTED)
Branch: 1 branch
BranchTrueFalseTotal
if (delta_type != GIT_DELTA_CONFLICTED) 49 0 49
1126 49 delta_type = GIT_DELTA_ADDED;
1127 }
1128  
1129 170 ▶ else if (nitem->mode == GIT_FILEMODE_COMMIT) {
Branch: 1 branch
BranchTrueFalseTotal
else if (nitem->mode == GIT_FILEMODE_COMMIT) { 0 170 170
1130 /* ignore things that are not actual submodules */
1131 0 ▶ if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) { 0 0 0
1132 0 git_error_clear();
1133 0 delta_type = GIT_DELTA_IGNORED;
1134  
1135 /* if this contains a tracked item, treat as normal TREE */
1136 0 ▶ if (contains_oitem) {
Branch: 1 branch
BranchTrueFalseTotal
if (contains_oitem) { 0 0 0
1137 0 error = iterator_advance_into(&info->nitem, info->new_iter);
1138 0 ▶ if (error != GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error != GIT_ENOTFOUND) 0 0 0
1139 0 return error;
1140  
1141 0 git_error_clear();
1142 0 return iterator_advance(&info->nitem, info->new_iter);
1143 }
1144 }
1145 }
1146  
1147 170 ▶ else if (nitem->mode == GIT_FILEMODE_UNREADABLE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (nitem->mode == GIT_FILEMODE_UNREADABLE) { 0 170 170
1148 0 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED))
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED)) 0 0 0
1149 0 delta_type = GIT_DELTA_UNTRACKED;
1150 else
1151 0 delta_type = GIT_DELTA_UNREADABLE;
1152 }
1153  
1154 /* Actually create the record for this item if necessary */
1155 219 ▶ if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0) 0 219 219
1156 0 return error;
1157  
1158 /* If user requested TYPECHANGE records, then check for that instead of
1159 * just generating an ADDED/UNTRACKED record
1160 */
1161 219 ▶ if (delta_type != GIT_DELTA_IGNORED &&
Branch: 1 branch
BranchTrueFalseTotal
if (delta_type != GIT_DELTA_IGNORED && 210 9 219
1162 210 ▶ DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
Branch: 2 branches
BranchTrueFalseTotal
DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) && 7 203 210
DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) && 0 7 7
1163 contains_oitem)
1164 {
1165 /* this entry was prefixed with a tree - make TYPECHANGE */
1166 0 git_diff_delta *last = diff_delta__last_for_item(diff, nitem);
1167 0 ▶ if (last) {
Branch: 1 branch
BranchTrueFalseTotal
if (last) { 0 0 0
1168 0 last->status = GIT_DELTA_TYPECHANGE;
1169 0 last->old_file.mode = GIT_FILEMODE_TREE;
1170 }
1171 }
1172  
1173 219 return iterator_advance(&info->nitem, info->new_iter);
1174 }
1175  
1176 21 static int handle_unmatched_old_item(
1177 git_diff_generated *diff, diff_in_progress *info)
1178 {
1179 21 git_delta_t delta_type = GIT_DELTA_DELETED;
1180 int error;
1181  
1182 /* update delta_type if this item is conflicted */
1183 21 ▶ if (git_index_entry_is_conflict(info->oitem))
Branch: 1 branch
BranchTrueFalseTotal
if (git_index_entry_is_conflict(info->oitem)) 0 21 21
1184 0 delta_type = GIT_DELTA_CONFLICTED;
1185  
1186 21 ▶ if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0) 0 21 21
1187 0 return error;
1188  
1189 /* if we are generating TYPECHANGE records then check for that
1190 * instead of just generating a DELETE record
1191 */
1192 29 if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
1193 8 entry_is_prefixed(diff, info->nitem, info->oitem))
1194 {
1195 /* this entry has become a tree! convert to TYPECHANGE */
1196 0 git_diff_delta *last = diff_delta__last_for_item(diff, info->oitem);
1197 0 ▶ if (last) {
Branch: 1 branch
BranchTrueFalseTotal
if (last) { 0 0 0
1198 0 last->status = GIT_DELTA_TYPECHANGE;
1199 0 last->new_file.mode = GIT_FILEMODE_TREE;
1200 }
1201  
1202 /* If new_iter is a workdir iterator, then this situation
1203 * will certainly be followed by a series of untracked items.
1204 * Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
1205 */
1206 0 ▶ if (S_ISDIR(info->nitem->mode) &&
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR(info->nitem->mode) && 0 0 0
1207 0 ▶ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS))
Branch: 1 branch
BranchTrueFalseTotal
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) 0 0 0
1208 0 return iterator_advance(&info->nitem, info->new_iter);
1209 }
1210  
1211 21 return iterator_advance(&info->oitem, info->old_iter);
1212 }
1213  
1214 539 static int handle_matched_item(
1215 git_diff_generated *diff, diff_in_progress *info)
1216 {
1217 539 int error = 0;
1218  
1219 539 ▶ if ((error = maybe_modified(diff, info)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = maybe_modified(diff, info)) < 0) 0 539 539
1220 0 return error;
1221  
1222 539 ▶ if (!(error = iterator_advance(&info->oitem, info->old_iter)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = iterator_advance(&info->oitem, info->old_iter))) 539 0 539
1223 539 error = iterator_advance(&info->nitem, info->new_iter);
1224  
1225 539 return error;
1226 }
1227  
1228 302 int git_diff__from_iterators(
1229 git_diff **out,
1230 git_repository *repo,
1231 git_iterator *old_iter,
1232 git_iterator *new_iter,
1233 const git_diff_options *opts)
1234 {
1235 git_diff_generated *diff;
1236 302 diff_in_progress info = {0};
1237 302 int error = 0;
1238  
1239 302 *out = NULL;
1240  
1241 302 diff = diff_generated_alloc(repo, old_iter, new_iter);
1242 302 ▶ GIT_ERROR_CHECK_ALLOC(diff);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(diff); 0 302 302
1243  
1244 302 info.repo = repo;
1245 302 info.old_iter = old_iter;
1246 302 info.new_iter = new_iter;
1247  
1248 /* make iterators have matching icase behavior */
1249 302 ▶ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) { 0 302 302
1250 0 if ((error = git_iterator_set_ignore_case(old_iter, true)) < 0 ||
1251 0 (error = git_iterator_set_ignore_case(new_iter, true)) < 0)
1252 0 goto cleanup;
1253 }
1254  
1255 /* finish initialization */
1256 302 ▶ if ((error = diff_generated_apply_options(diff, opts)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_generated_apply_options(diff, opts)) < 0) 0 302 302
1257 0 goto cleanup;
1258  
1259 604 if ((error = iterator_current(&info.oitem, old_iter)) < 0 ||
1260 302 (error = iterator_current(&info.nitem, new_iter)) < 0)
1261 0 goto cleanup;
1262  
1263 /* run iterators building diffs */
1264 1236 ▶ while (!error && (info.oitem || info.nitem)) {
Branch: 3 branches
BranchTrueFalseTotal
while (!error && (info.oitem || info.nitem)) { 1236 0 1236
while (!error && (info.oitem || info.nitem)) { 836 400 1236
while (!error && (info.oitem || info.nitem)) { 98 302 400
1265 int cmp;
1266  
1267 /* report progress */
1268 934 ▶ if (opts && opts->progress_cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (opts && opts->progress_cb) { 925 9 934
if (opts && opts->progress_cb) { 0 925 925
1269 0 ▶ if ((error = opts->progress_cb(&diff->base,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = opts->progress_cb(&diff->base, 0 0 0
1270 0 ▶ info.oitem ? info.oitem->path : NULL,
Branch: 1 branch
BranchTrueFalseTotal
info.oitem ? info.oitem->path : NULL, 0 0 0
1271 0 info.nitem ? info.nitem->path : NULL,
1272 0 ▶ opts->payload)))
Branch: 1 branch
BranchTrueFalseTotal
opts->payload))) 0 0 0
1273 0 break;
1274 }
1275  
1276 1868 cmp = info.oitem ?
1277 934 ▶ (info.nitem ? diff->base.entrycomp(info.oitem, info.nitem) : -1) : 1;
Branch: 2 branches
BranchTrueFalseTotal
(info.nitem ? diff->base.entrycomp(info.oitem, info.nitem) : -1) : 1; 836 98 934
(info.nitem ? diff->base.entrycomp(info.oitem, info.nitem) : -1) : 1; 826 10 836
1278  
1279 /* create DELETED records for old items not matched in new */
1280 934 ▶ if (cmp < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cmp < 0) 21 913 934
1281 21 error = handle_unmatched_old_item(diff, &info);
1282  
1283 /* create ADDED, TRACKED, or IGNORED records for new items not
1284 * matched in old (and/or descend into directories as needed)
1285 */
1286 913 ▶ else if (cmp > 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (cmp > 0) 374 539 913
1287 374 error = handle_unmatched_new_item(diff, &info);
1288  
1289 /* otherwise item paths match, so create MODIFIED record
1290 * (or ADDED and DELETED pair if type changed)
1291 */
1292 else
1293 539 error = handle_matched_item(diff, &info);
1294 }
1295  
1296 302 diff->base.perf.stat_calls +=
1297 302 old_iter->stat_calls + new_iter->stat_calls;
1298  
1299 302 cleanup:
1300 302 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 302 0 302
1301 302 *out = &diff->base;
1302 else
1303 0 git_diff_free(&diff->base);
1304 302 ▶ if (info.submodule_cache)
Branch: 1 branch
BranchTrueFalseTotal
if (info.submodule_cache) 0 302 302
1305 0 git_submodule_cache_free(info.submodule_cache);
1306  
1307 302 return error;
1308 }
1309  
1310 231 static int diff_prepare_iterator_opts(char **prefix, git_iterator_options *a, int aflags,
1311 git_iterator_options *b, int bflags,
1312 const git_diff_options *opts)
1313 {
1314 231 ▶ GIT_ERROR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); 0 231 231
1315  
1316 231 *prefix = NULL;
1317  
1318 231 ▶ if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { 227 4 231
if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { 40 187 227
1319 40 a->pathlist.strings = opts->pathspec.strings;
1320 40 a->pathlist.count = opts->pathspec.count;
1321 40 b->pathlist.strings = opts->pathspec.strings;
1322 40 b->pathlist.count = opts->pathspec.count;
1323 191 ▶ } else if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (opts) { 187 4 191
1324 187 *prefix = git_pathspec_prefix(&opts->pathspec);
1325 187 ▶ GIT_ERROR_CHECK_ALLOC(prefix);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(prefix); 0 187 187
1326 }
1327  
1328 231 a->flags = aflags;
1329 231 b->flags = bflags;
1330 231 a->start = b->start = *prefix;
1331 231 a->end = b->end = *prefix;
1332  
1333 231 return 0;
1334 }
1335  
1336 18 int git_diff_tree_to_tree(
1337 git_diff **out,
1338 git_repository *repo,
1339 git_tree *old_tree,
1340 git_tree *new_tree,
1341 const git_diff_options *opts)
1342 {
1343 18 git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE;
1344 18 git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT,
1345 18 b_opts = GIT_ITERATOR_OPTIONS_INIT;
1346 18 git_iterator *a = NULL, *b = NULL;
1347 18 git_diff *diff = NULL;
1348 18 char *prefix = NULL;
1349 18 int error = 0;
1350  
1351 18 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 18 18
1352 18 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 18 18
1353  
1354 18 *out = NULL;
1355  
1356 /* for tree to tree diff, be case sensitive even if the index is
1357 * currently case insensitive, unless the user explicitly asked
1358 * for case insensitivity
1359 */
1360 18 ▶ if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0) 18 0 18
if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0) 0 18 18
1361 0 iflag = GIT_ITERATOR_IGNORE_CASE;
1362  
1363 36 if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, iflag, &b_opts, iflag, opts)) < 0 ||
1364 36 ▶ (error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 || 18 0 18
1365 36 ▶ (error = git_iterator_for_tree(&b, new_tree, &b_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_tree(&b, new_tree, &b_opts)) < 0 || 0 18 18
1366 18 (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0)
1367 0 goto out;
1368  
1369 18 *out = diff;
1370 18 diff = NULL;
1371 18 out:
1372 18 git_iterator_free(a);
1373 18 git_iterator_free(b);
1374 18 git_diff_free(diff);
1375 18 git__free(prefix);
1376  
1377 18 return error;
1378 }
1379  
1380 16 static int diff_load_index(git_index **index, git_repository *repo)
1381 {
1382 16 int error = git_repository_index__weakptr(index, repo);
1383  
1384 /* reload the repository index when user did not pass one in */
1385 16 ▶ if (!error && git_index_read(*index, false) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!error && git_index_read(*index, false) < 0) 16 0 16
if (!error && git_index_read(*index, false) < 0) 0 16 16
1386 0 git_error_clear();
1387  
1388 16 return error;
1389 }
1390  
1391 95 int git_diff_tree_to_index(
1392 git_diff **out,
1393 git_repository *repo,
1394 git_tree *old_tree,
1395 git_index *index,
1396 const git_diff_options *opts)
1397 {
1398 95 git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
1399 GIT_ITERATOR_INCLUDE_CONFLICTS;
1400 95 git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT,
1401 95 b_opts = GIT_ITERATOR_OPTIONS_INIT;
1402 95 git_iterator *a = NULL, *b = NULL;
1403 95 git_diff *diff = NULL;
1404 95 char *prefix = NULL;
1405 95 bool index_ignore_case = false;
1406 95 int error = 0;
1407  
1408 95 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 95 95
1409 95 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 95 95
1410  
1411 95 *out = NULL;
1412  
1413 95 ▶ if (!index && (error = diff_load_index(&index, repo)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!index && (error = diff_load_index(&index, repo)) < 0) 0 95 95
if (!index && (error = diff_load_index(&index, repo)) < 0) 0 0 0
1414 0 return error;
1415  
1416 95 index_ignore_case = index->ignore_case;
1417  
1418 190 if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, iflag, &b_opts, iflag, opts)) < 0 ||
1419 190 ▶ (error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 || 95 0 95
1420 190 ▶ (error = git_iterator_for_index(&b, repo, index, &b_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_index(&b, repo, index, &b_opts)) < 0 || 0 95 95
1421 95 (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0)
1422 0 goto out;
1423  
1424 /* if index is in case-insensitive order, re-sort deltas to match */
1425 95 ▶ if (index_ignore_case)
Branch: 1 branch
BranchTrueFalseTotal
if (index_ignore_case) 0 95 95
1426 0 diff_set_ignore_case(diff, true);
1427  
1428 95 *out = diff;
1429 95 diff = NULL;
1430 95 out:
1431 95 git_iterator_free(a);
1432 95 git_iterator_free(b);
1433 95 git_diff_free(diff);
1434 95 git__free(prefix);
1435  
1436 95 return error;
1437 }
1438  
1439 116 int git_diff_index_to_workdir(
1440 git_diff **out,
1441 git_repository *repo,
1442 git_index *index,
1443 const git_diff_options *opts)
1444 {
1445 116 git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT,
1446 116 b_opts = GIT_ITERATOR_OPTIONS_INIT;
1447 116 git_iterator *a = NULL, *b = NULL;
1448 116 git_diff *diff = NULL;
1449 116 char *prefix = NULL;
1450 116 int error = 0;
1451  
1452 116 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 116 116
1453 116 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 116 116
1454  
1455 116 *out = NULL;
1456  
1457 116 ▶ if (!index && (error = diff_load_index(&index, repo)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!index && (error = diff_load_index(&index, repo)) < 0) 16 100 116
if (!index && (error = diff_load_index(&index, repo)) < 0) 0 16 16
1458 0 return error;
1459  
1460 116 ▶ if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, GIT_ITERATOR_INCLUDE_CONFLICTS,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, GIT_ITERATOR_INCLUDE_CONFLICTS, 116 0 116
1461 116 ▶ &b_opts, GIT_ITERATOR_DONT_AUTOEXPAND, opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
&b_opts, GIT_ITERATOR_DONT_AUTOEXPAND, opts)) < 0 || 116 0 116
1462 232 ▶ (error = git_iterator_for_index(&a, repo, index, &a_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_index(&a, repo, index, &a_opts)) < 0 || 116 0 116
1463 232 ▶ (error = git_iterator_for_workdir(&b, repo, index, NULL, &b_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_workdir(&b, repo, index, NULL, &b_opts)) < 0 || 0 116 116
1464 116 (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0)
1465 0 goto out;
1466  
1467 116 ▶ if ((diff->opts.flags & GIT_DIFF_UPDATE_INDEX) && ((git_diff_generated *)diff)->index_updated)
Branch: 2 branches
BranchTrueFalseTotal
if ((diff->opts.flags & GIT_DIFF_UPDATE_INDEX) && ((git_diff_generated *)diff)->index_updated) 0 116 116
if ((diff->opts.flags & GIT_DIFF_UPDATE_INDEX) && ((git_diff_generated *)diff)->index_updated) 0 0 0
1468 0 ▶ if ((error = git_index_write(index)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_index_write(index)) < 0) 0 0 0
1469 0 goto out;
1470  
1471 116 *out = diff;
1472 116 diff = NULL;
1473 116 out:
1474 116 git_iterator_free(a);
1475 116 git_iterator_free(b);
1476 116 git_diff_free(diff);
1477 116 git__free(prefix);
1478  
1479 116 return error;
1480 }
1481  
1482 2 int git_diff_tree_to_workdir(
1483 git_diff **out,
1484 git_repository *repo,
1485 git_tree *old_tree,
1486 const git_diff_options *opts)
1487 {
1488 2 git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT,
1489 2 b_opts = GIT_ITERATOR_OPTIONS_INIT;
1490 2 git_iterator *a = NULL, *b = NULL;
1491 2 git_diff *diff = NULL;
1492 2 char *prefix = NULL;
1493 git_index *index;
1494 int error;
1495  
1496 2 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 2 2
1497 2 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 2 2
1498  
1499 2 *out = NULL;
1500  
1501 2 if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, 0,
1502 4 &b_opts, GIT_ITERATOR_DONT_AUTOEXPAND, opts) < 0) ||
1503 4 ▶ (error = git_repository_index__weakptr(&index, repo)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_repository_index__weakptr(&index, repo)) < 0 || 2 0 2
1504 4 ▶ (error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_tree(&a, old_tree, &a_opts)) < 0 || 2 0 2
1505 4 ▶ (error = git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts)) < 0 || 0 2 2
1506 2 (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0)
1507 0 goto out;
1508  
1509 2 *out = diff;
1510 2 diff = NULL;
1511 2 out:
1512 2 git_iterator_free(a);
1513 2 git_iterator_free(b);
1514 2 git_diff_free(diff);
1515 2 git__free(prefix);
1516  
1517 2 return error;
1518 }
1519  
1520 0 int git_diff_tree_to_workdir_with_index(
1521 git_diff **out,
1522 git_repository *repo,
1523 git_tree *tree,
1524 const git_diff_options *opts)
1525 {
1526 0 git_diff *d1 = NULL, *d2 = NULL;
1527 0 git_index *index = NULL;
1528 0 int error = 0;
1529  
1530 0 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 0 0
1531 0 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 0 0
1532  
1533 0 *out = NULL;
1534  
1535 0 ▶ if ((error = diff_load_index(&index, repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = diff_load_index(&index, repo)) < 0) 0 0 0
1536 0 return error;
1537  
1538 0 if (!(error = git_diff_tree_to_index(&d1, repo, tree, index, opts)) &&
1539 0 !(error = git_diff_index_to_workdir(&d2, repo, index, opts)))
1540 0 error = git_diff_merge(d1, d2);
1541  
1542 0 git_diff_free(d2);
1543  
1544 0 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 0 0
1545 0 git_diff_free(d1);
1546 0 d1 = NULL;
1547 }
1548  
1549 0 *out = d1;
1550 0 return error;
1551 }
1552  
1553 0 int git_diff_index_to_index(
1554 git_diff **out,
1555 git_repository *repo,
1556 git_index *old_index,
1557 git_index *new_index,
1558 const git_diff_options *opts)
1559 {
1560 0 git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT,
1561 0 b_opts = GIT_ITERATOR_OPTIONS_INIT;
1562 0 git_iterator *a = NULL, *b = NULL;
1563 0 git_diff *diff = NULL;
1564 0 char *prefix = NULL;
1565 int error;
1566  
1567 0 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 0 0
1568 0 ▶ GIT_ASSERT_ARG(old_index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(old_index); 0 0 0
1569 0 ▶ GIT_ASSERT_ARG(new_index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(new_index); 0 0 0
1570  
1571 0 *out = NULL;
1572  
1573 0 if ((error = diff_prepare_iterator_opts(&prefix, &a_opts, GIT_ITERATOR_DONT_IGNORE_CASE,
1574 0 &b_opts, GIT_ITERATOR_DONT_IGNORE_CASE, opts) < 0) ||
1575 0 ▶ (error = git_iterator_for_index(&a, repo, old_index, &a_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_index(&a, repo, old_index, &a_opts)) < 0 || 0 0 0
1576 0 ▶ (error = git_iterator_for_index(&b, repo, new_index, &b_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_iterator_for_index(&b, repo, new_index, &b_opts)) < 0 || 0 0 0
1577 0 (error = git_diff__from_iterators(&diff, repo, a, b, opts)) < 0)
1578 0 goto out;
1579  
1580 /* if index is in case-insensitive order, re-sort deltas to match */
1581 0 ▶ if (old_index->ignore_case || new_index->ignore_case)
Branch: 2 branches
BranchTrueFalseTotal
if (old_index->ignore_case || new_index->ignore_case) 0 0 0
if (old_index->ignore_case || new_index->ignore_case) 0 0 0
1582 0 diff_set_ignore_case(diff, true);
1583  
1584 0 *out = diff;
1585 0 diff = NULL;
1586 0 out:
1587 0 git_iterator_free(a);
1588 0 git_iterator_free(b);
1589 0 git_diff_free(diff);
1590 0 git__free(prefix);
1591  
1592 0 return error;
1593 }
1594  
1595 63 int git_diff__paired_foreach(
1596 git_diff *head2idx,
1597 git_diff *idx2wd,
1598 int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload),
1599 void *payload)
1600 {
1601 63 int cmp, error = 0;
1602 git_diff_delta *h2i, *i2w;
1603 size_t i, j, i_max, j_max;
1604 63 int (*strcomp)(const char *, const char *) = git__strcmp;
1605 bool h2i_icase, i2w_icase, icase_mismatch;
1606  
1607 63 ▶ i_max = head2idx ? head2idx->deltas.length : 0;
Branch: 1 branch
BranchTrueFalseTotal
i_max = head2idx ? head2idx->deltas.length : 0; 62 1 63
1608 63 ▶ j_max = idx2wd ? idx2wd->deltas.length : 0;
Branch: 1 branch
BranchTrueFalseTotal
j_max = idx2wd ? idx2wd->deltas.length : 0; 62 1 63
1609 63 ▶ if (!i_max && !j_max)
Branch: 2 branches
BranchTrueFalseTotal
if (!i_max && !j_max) 33 30 63
if (!i_max && !j_max) 21 12 33
1610 21 return 0;
1611  
1612 /* At some point, tree-to-index diffs will probably never ignore case,
1613 * even if that isn't true now. Index-to-workdir diffs may or may not
1614 * ignore case, but the index filename for the idx2wd diff should
1615 * still be using the canonical case-preserving name.
1616 *
1617 * Therefore the main thing we need to do here is make sure the diffs
1618 * are traversed in a compatible order. To do this, we temporarily
1619 * resort a mismatched diff to get the order correct.
1620 *
1621 * In order to traverse renames in the index->workdir, we need to
1622 * ensure that we compare the index name on both sides, so we
1623 * always sort by the old name in the i2w list.
1624 */
1625 42 ▶ h2i_icase = head2idx != NULL && git_diff_is_sorted_icase(head2idx);
Branch: 2 branches
BranchTrueFalseTotal
h2i_icase = head2idx != NULL && git_diff_is_sorted_icase(head2idx); 41 1 42
h2i_icase = head2idx != NULL && git_diff_is_sorted_icase(head2idx); 0 41 41
1626 42 ▶ i2w_icase = idx2wd != NULL && git_diff_is_sorted_icase(idx2wd);
Branch: 2 branches
BranchTrueFalseTotal
i2w_icase = idx2wd != NULL && git_diff_is_sorted_icase(idx2wd); 42 0 42
i2w_icase = idx2wd != NULL && git_diff_is_sorted_icase(idx2wd); 0 42 42
1627  
1628 42 icase_mismatch =
1629 42 ▶ (head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase);
Branch: 3 branches
BranchTrueFalseTotal
(head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase); 41 1 42
(head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase); 41 0 41
(head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase); 0 41 41
1630  
1631 42 ▶ if (icase_mismatch && h2i_icase) {
Branch: 2 branches
BranchTrueFalseTotal
if (icase_mismatch && h2i_icase) { 0 42 42
if (icase_mismatch && h2i_icase) { 0 0 0
1632 0 git_vector_set_cmp(&head2idx->deltas, git_diff_delta__cmp);
1633 0 git_vector_sort(&head2idx->deltas);
1634 }
1635  
1636 42 ▶ if (i2w_icase && !icase_mismatch) {
Branch: 2 branches
BranchTrueFalseTotal
if (i2w_icase && !icase_mismatch) { 0 42 42
if (i2w_icase && !icase_mismatch) { 0 0 0
1637 0 strcomp = git__strcasecmp;
1638  
1639 0 git_vector_set_cmp(&idx2wd->deltas, diff_delta_i2w_casecmp);
1640 0 git_vector_sort(&idx2wd->deltas);
1641 42 ▶ } else if (idx2wd != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (idx2wd != NULL) { 42 0 42
1642 42 git_vector_set_cmp(&idx2wd->deltas, diff_delta_i2w_cmp);
1643 42 git_vector_sort(&idx2wd->deltas);
1644 }
1645  
1646 108 ▶ for (i = 0, j = 0; i < i_max || j < j_max; ) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0, j = 0; i < i_max || j < j_max; ) { 38 70 108
for (i = 0, j = 0; i < i_max || j < j_max; ) { 28 42 70
1647 66 ▶ h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL; 65 1 66
h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL; 38 27 65
1648 66 ▶ i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL; 66 0 66
i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL; 49 17 66
1649  
1650 87 ▶ cmp = !i2w ? -1 : !h2i ? 1 :
Branch: 2 branches
BranchTrueFalseTotal
cmp = !i2w ? -1 : !h2i ? 1 : 49 17 66
cmp = !i2w ? -1 : !h2i ? 1 : 21 28 49
1651 21 strcomp(h2i->new_file.path, i2w->old_file.path);
1652  
1653 66 ▶ if (cmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmp < 0) { 20 46 66
1654 20 i++; i2w = NULL;
1655 46 ▶ } else if (cmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cmp > 0) { 36 10 46
1656 36 j++; h2i = NULL;
1657 } else {
1658 10 i++; j++;
1659 }
1660  
1661 66 ▶ if ((error = cb(h2i, i2w, payload)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = cb(h2i, i2w, payload)) != 0) { 0 66 66
1662 0 git_error_set_after_callback(error);
1663 0 break;
1664 }
1665 }
1666  
1667 /* restore case-insensitive delta sort */
1668 42 ▶ if (icase_mismatch && h2i_icase) {
Branch: 2 branches
BranchTrueFalseTotal
if (icase_mismatch && h2i_icase) { 0 42 42
if (icase_mismatch && h2i_icase) { 0 0 0
1669 0 git_vector_set_cmp(&head2idx->deltas, git_diff_delta__casecmp);
1670 0 git_vector_sort(&head2idx->deltas);
1671 }
1672  
1673 /* restore idx2wd sort by new path */
1674 42 ▶ if (idx2wd != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx2wd != NULL) { 42 0 42
1675 42 ▶ git_vector_set_cmp(&idx2wd->deltas,
Branch: 1 branch
BranchTrueFalseTotal
git_vector_set_cmp(&idx2wd->deltas, 0 42 42
1676 i2w_icase ? git_diff_delta__casecmp : git_diff_delta__cmp);
1677 42 git_vector_sort(&idx2wd->deltas);
1678 }
1679  
1680 42 return error;
1681 }
1682  
1683 3 int git_diff__commit(
1684 git_diff **out,
1685 git_repository *repo,
1686 const git_commit *commit,
1687 const git_diff_options *opts)
1688 {
1689 3 git_commit *parent = NULL;
1690 3 git_diff *commit_diff = NULL;
1691 3 git_tree *old_tree = NULL, *new_tree = NULL;
1692 size_t parents;
1693 3 int error = 0;
1694  
1695 3 *out = NULL;
1696  
1697 3 ▶ if ((parents = git_commit_parentcount(commit)) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((parents = git_commit_parentcount(commit)) > 1) { 0 3 3
1698 char commit_oidstr[GIT_OID_HEXSZ + 1];
1699  
1700 0 error = -1;
1701 0 git_error_set(GIT_ERROR_INVALID, "commit %s is a merge commit",
1702 git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));
1703 0 goto on_error;
1704 }
1705  
1706 3 ▶ if (parents > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (parents > 0) 3 0 3
1707 6 if ((error = git_commit_parent(&parent, commit, 0)) < 0 ||
1708 3 (error = git_commit_tree(&old_tree, parent)) < 0)
1709 0 goto on_error;
1710  
1711 6 if ((error = git_commit_tree(&new_tree, commit)) < 0 ||
1712 3 (error = git_diff_tree_to_tree(&commit_diff, repo, old_tree, new_tree, opts)) < 0)
1713 0 goto on_error;
1714  
1715 3 *out = commit_diff;
1716  
1717 3 on_error:
1718 3 git_tree_free(new_tree);
1719 3 git_tree_free(old_tree);
1720 3 git_commit_free(parent);
1721  
1722 3 return error;
1723 }
1724  
« deps/libgit2/src/libgit2/reset.c ↑ Summary deps/libgit2/src/libgit2/pack-objects.c »