deps/libgit2/src/libgit2/index.c

filter: Statementstmt 67.9% 1247 / 1835 Branchbran 47.0% 550 / 1170 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 59.8% 1797 / 3005 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/email.c ↑ Summary deps/libgit2/src/libgit2/rebase.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 "index.h"
9  
10 #include <stddef.h>
11  
12 #include "repository.h"
13 #include "tree.h"
14 #include "tree-cache.h"
15 #include "hash.h"
16 #include "iterator.h"
17 #include "pathspec.h"
18 #include "ignore.h"
19 #include "blob.h"
20 #include "idxmap.h"
21 #include "diff.h"
22 #include "varint.h"
23 #include "path.h"
24  
25 #include "git2/odb.h"
26 #include "git2/oid.h"
27 #include "git2/blob.h"
28 #include "git2/config.h"
29 #include "git2/sys/index.h"
30  
31 static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
32 unsigned int flags,
33 git_index_matched_path_cb cb, void *payload);
34  
35 #define minimal_entry_size (offsetof(struct entry_short, path))
36  
37 static const size_t INDEX_HEADER_SIZE = 12;
38  
39 static const unsigned int INDEX_VERSION_NUMBER_DEFAULT = 2;
40 static const unsigned int INDEX_VERSION_NUMBER_LB = 2;
41 static const unsigned int INDEX_VERSION_NUMBER_EXT = 3;
42 static const unsigned int INDEX_VERSION_NUMBER_COMP = 4;
43 static const unsigned int INDEX_VERSION_NUMBER_UB = 4;
44  
45 static const unsigned int INDEX_HEADER_SIG = 0x44495243;
46 static const char INDEX_EXT_TREECACHE_SIG[] = {'T', 'R', 'E', 'E'};
47 static const char INDEX_EXT_UNMERGED_SIG[] = {'R', 'E', 'U', 'C'};
48 static const char INDEX_EXT_CONFLICT_NAME_SIG[] = {'N', 'A', 'M', 'E'};
49  
50 #define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx)))
51  
52 struct index_header {
53 uint32_t signature;
54 uint32_t version;
55 uint32_t entry_count;
56 };
57  
58 struct index_extension {
59 char signature[4];
60 uint32_t extension_size;
61 };
62  
63 struct entry_time {
64 uint32_t seconds;
65 uint32_t nanoseconds;
66 };
67  
68 struct entry_short {
69 struct entry_time ctime;
70 struct entry_time mtime;
71 uint32_t dev;
72 uint32_t ino;
73 uint32_t mode;
74 uint32_t uid;
75 uint32_t gid;
76 uint32_t file_size;
77 unsigned char oid[GIT_OID_RAWSZ];
78 uint16_t flags;
79 char path[1]; /* arbitrary length */
80 };
81  
82 struct entry_long {
83 struct entry_time ctime;
84 struct entry_time mtime;
85 uint32_t dev;
86 uint32_t ino;
87 uint32_t mode;
88 uint32_t uid;
89 uint32_t gid;
90 uint32_t file_size;
91 unsigned char oid[GIT_OID_RAWSZ];
92 uint16_t flags;
93 uint16_t flags_extended;
94 char path[1]; /* arbitrary length */
95 };
96  
97 struct entry_srch_key {
98 const char *path;
99 size_t pathlen;
100 int stage;
101 };
102  
103 struct entry_internal {
104 git_index_entry entry;
105 size_t pathlen;
106 char path[GIT_FLEX_ARRAY];
107 };
108  
109 struct reuc_entry_internal {
110 git_index_reuc_entry entry;
111 size_t pathlen;
112 char path[GIT_FLEX_ARRAY];
113 };
114  
115 bool git_index__enforce_unsaved_safety = false;
116  
117 /* local declarations */
118 static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size);
119 static int read_header(struct index_header *dest, const void *buffer);
120  
121 static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
122 static bool is_index_extended(git_index *index);
123 static int write_index(unsigned char checksum[GIT_HASH_SHA1_SIZE], size_t *checksum_size, git_index *index, git_filebuf *file);
124  
125 static void index_entry_free(git_index_entry *entry);
126 static void index_entry_reuc_free(git_index_reuc_entry *reuc);
127  
128 173 GIT_INLINE(int) index_map_set(git_idxmap *map, git_index_entry *e, bool ignore_case)
129 {
130 173 ▶ if (ignore_case)
Branch: 1 branch
BranchTrueFalseTotal
if (ignore_case) 0 173 173
131 0 return git_idxmap_icase_set((git_idxmap_icase *) map, e, e);
132 else
133 173 return git_idxmap_set(map, e, e);
134 }
135  
136 217 GIT_INLINE(int) index_map_delete(git_idxmap *map, git_index_entry *e, bool ignore_case)
137 {
138 217 ▶ if (ignore_case)
Branch: 1 branch
BranchTrueFalseTotal
if (ignore_case) 0 217 217
139 0 return git_idxmap_icase_delete((git_idxmap_icase *) map, e);
140 else
141 217 return git_idxmap_delete(map, e);
142 }
143  
144 43 GIT_INLINE(int) index_map_resize(git_idxmap *map, size_t count, bool ignore_case)
145 {
146 43 ▶ if (ignore_case)
Branch: 1 branch
BranchTrueFalseTotal
if (ignore_case) 0 43 43
147 0 return git_idxmap_icase_resize((git_idxmap_icase *) map, count);
148 else
149 43 return git_idxmap_resize(map, count);
150 }
151  
152 3229 int git_index_entry_srch(const void *key, const void *array_member)
153 {
154 3229 const struct entry_srch_key *srch_key = key;
155 3229 const struct entry_internal *entry = array_member;
156 int cmp;
157 size_t len1, len2, len;
158  
159 3229 len1 = srch_key->pathlen;
160 3229 len2 = entry->pathlen;
161 3229 len = len1 < len2 ? len1 : len2;
162  
163 3229 cmp = memcmp(srch_key->path, entry->path, len);
164 3229 ▶ if (cmp)
Branch: 1 branch
BranchTrueFalseTotal
if (cmp) 2898 331 3229
165 2898 return cmp;
166 331 ▶ if (len1 < len2)
Branch: 1 branch
BranchTrueFalseTotal
if (len1 < len2) 176 155 331
167 176 return -1;
168 155 ▶ if (len1 > len2)
Branch: 1 branch
BranchTrueFalseTotal
if (len1 > len2) 39 116 155
169 39 return 1;
170  
171 116 ▶ if (srch_key->stage != GIT_INDEX_STAGE_ANY)
Branch: 1 branch
BranchTrueFalseTotal
if (srch_key->stage != GIT_INDEX_STAGE_ANY) 112 4 116
172 112 return srch_key->stage - GIT_INDEX_ENTRY_STAGE(&entry->entry);
173  
174 4 return 0;
175 }
176  
177 0 int git_index_entry_isrch(const void *key, const void *array_member)
178 {
179 0 const struct entry_srch_key *srch_key = key;
180 0 const struct entry_internal *entry = array_member;
181 int cmp;
182 size_t len1, len2, len;
183  
184 0 len1 = srch_key->pathlen;
185 0 len2 = entry->pathlen;
186 0 len = len1 < len2 ? len1 : len2;
187  
188 0 cmp = strncasecmp(srch_key->path, entry->path, len);
189  
190 0 ▶ if (cmp)
Branch: 1 branch
BranchTrueFalseTotal
if (cmp) 0 0 0
191 0 return cmp;
192 0 ▶ if (len1 < len2)
Branch: 1 branch
BranchTrueFalseTotal
if (len1 < len2) 0 0 0
193 0 return -1;
194 0 ▶ if (len1 > len2)
Branch: 1 branch
BranchTrueFalseTotal
if (len1 > len2) 0 0 0
195 0 return 1;
196  
197 0 ▶ if (srch_key->stage != GIT_INDEX_STAGE_ANY)
Branch: 1 branch
BranchTrueFalseTotal
if (srch_key->stage != GIT_INDEX_STAGE_ANY) 0 0 0
198 0 return srch_key->stage - GIT_INDEX_ENTRY_STAGE(&entry->entry);
199  
200 0 return 0;
201 }
202  
203 116 static int index_entry_srch_path(const void *path, const void *array_member)
204 {
205 116 const git_index_entry *entry = array_member;
206  
207 116 return strcmp((const char *)path, entry->path);
208 }
209  
210 0 static int index_entry_isrch_path(const void *path, const void *array_member)
211 {
212 0 const git_index_entry *entry = array_member;
213  
214 0 return strcasecmp((const char *)path, entry->path);
215 }
216  
217 239 int git_index_entry_cmp(const void *a, const void *b)
218 {
219 int diff;
220 239 const git_index_entry *entry_a = a;
221 239 const git_index_entry *entry_b = b;
222  
223 239 diff = strcmp(entry_a->path, entry_b->path);
224  
225 239 ▶ if (diff == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (diff == 0) 118 121 239
226 118 diff = (GIT_INDEX_ENTRY_STAGE(entry_a) - GIT_INDEX_ENTRY_STAGE(entry_b));
227  
228 239 return diff;
229 }
230  
231 0 int git_index_entry_icmp(const void *a, const void *b)
232 {
233 int diff;
234 0 const git_index_entry *entry_a = a;
235 0 const git_index_entry *entry_b = b;
236  
237 0 diff = strcasecmp(entry_a->path, entry_b->path);
238  
239 0 ▶ if (diff == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (diff == 0) 0 0 0
240 0 diff = (GIT_INDEX_ENTRY_STAGE(entry_a) - GIT_INDEX_ENTRY_STAGE(entry_b));
241  
242 0 return diff;
243 }
244  
245 0 static int conflict_name_cmp(const void *a, const void *b)
246 {
247 0 const git_index_name_entry *name_a = a;
248 0 const git_index_name_entry *name_b = b;
249  
250 0 ▶ if (name_a->ancestor && !name_b->ancestor)
Branch: 2 branches
BranchTrueFalseTotal
if (name_a->ancestor && !name_b->ancestor) 0 0 0
if (name_a->ancestor && !name_b->ancestor) 0 0 0
251 0 return 1;
252  
253 0 ▶ if (!name_a->ancestor && name_b->ancestor)
Branch: 2 branches
BranchTrueFalseTotal
if (!name_a->ancestor && name_b->ancestor) 0 0 0
if (!name_a->ancestor && name_b->ancestor) 0 0 0
254 0 return -1;
255  
256 0 ▶ if (name_a->ancestor)
Branch: 1 branch
BranchTrueFalseTotal
if (name_a->ancestor) 0 0 0
257 0 return strcmp(name_a->ancestor, name_b->ancestor);
258  
259 0 ▶ if (!name_a->ours || !name_b->ours)
Branch: 2 branches
BranchTrueFalseTotal
if (!name_a->ours || !name_b->ours) 0 0 0
if (!name_a->ours || !name_b->ours) 0 0 0
260 0 return 0;
261  
262 0 return strcmp(name_a->ours, name_b->ours);
263 }
264  
265 /**
266 * TODO: enable this when resolving case insensitive conflicts
267 */
268 #if 0
269 static int conflict_name_icmp(const void *a, const void *b)
270 {
271 const git_index_name_entry *name_a = a;
272 const git_index_name_entry *name_b = b;
273  
274 if (name_a->ancestor && !name_b->ancestor)
275 return 1;
276  
277 if (!name_a->ancestor && name_b->ancestor)
278 return -1;
279  
280 if (name_a->ancestor)
281 return strcasecmp(name_a->ancestor, name_b->ancestor);
282  
283 if (!name_a->ours || !name_b->ours)
284 return 0;
285  
286 return strcasecmp(name_a->ours, name_b->ours);
287 }
288 #endif
289  
290 4 static int reuc_srch(const void *key, const void *array_member)
291 {
292 4 const git_index_reuc_entry *reuc = array_member;
293  
294 4 return strcmp(key, reuc->path);
295 }
296  
297 0 static int reuc_isrch(const void *key, const void *array_member)
298 {
299 0 const git_index_reuc_entry *reuc = array_member;
300  
301 0 return strcasecmp(key, reuc->path);
302 }
303  
304 4 static int reuc_cmp(const void *a, const void *b)
305 {
306 4 const git_index_reuc_entry *info_a = a;
307 4 const git_index_reuc_entry *info_b = b;
308  
309 4 return strcmp(info_a->path, info_b->path);
310 }
311  
312 0 static int reuc_icmp(const void *a, const void *b)
313 {
314 0 const git_index_reuc_entry *info_a = a;
315 0 const git_index_reuc_entry *info_b = b;
316  
317 0 return strcasecmp(info_a->path, info_b->path);
318 }
319  
320 10 static void index_entry_reuc_free(git_index_reuc_entry *reuc)
321 {
322 10 git__free(reuc);
323 10 }
324  
325 228 static void index_entry_free(git_index_entry *entry)
326 {
327 228 ▶ if (!entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) 0 228 228
328 0 return;
329  
330 228 memset(&entry->id, 0, sizeof(entry->id));
331 228 git__free(entry);
332 }
333  
334 280 unsigned int git_index__create_mode(unsigned int mode)
335 {
336 280 ▶ if (S_ISLNK(mode))
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISLNK(mode)) 1 279 280
337 1 return S_IFLNK;
338  
339 279 ▶ if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR))
Branch: 2 branches
BranchTrueFalseTotal
if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR)) 279 0 279
if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR)) 0 279 279
340 0 return (S_IFLNK | S_IFDIR);
341  
342 279 ▶ return S_IFREG | GIT_PERMS_CANONICAL(mode);
Branch: 1 branch
BranchTrueFalseTotal
return S_IFREG | GIT_PERMS_CANONICAL(mode); 1 278 279
343 }
344  
345 50 static unsigned int index_merge_mode(
346 git_index *index, git_index_entry *existing, unsigned int mode)
347 {
348 50 ▶ if (index->no_symlinks && S_ISREG(mode) &&
Branch: 3 branches
BranchTrueFalseTotal
if (index->no_symlinks && S_ISREG(mode) && 0 50 50
if (index->no_symlinks && S_ISREG(mode) && 0 0 0
if (index->no_symlinks && S_ISREG(mode) && 0 0 0
349 0 ▶ existing && S_ISLNK(existing->mode))
Branch: 1 branch
BranchTrueFalseTotal
existing && S_ISLNK(existing->mode)) 0 0 0
350 0 return existing->mode;
351  
352 50 ▶ if (index->distrust_filemode && S_ISREG(mode))
Branch: 2 branches
BranchTrueFalseTotal
if (index->distrust_filemode && S_ISREG(mode)) 0 50 50
if (index->distrust_filemode && S_ISREG(mode)) 0 0 0
353 0 ▶ return (existing && S_ISREG(existing->mode)) ?
Branch: 1 branch
BranchTrueFalseTotal
return (existing && S_ISREG(existing->mode)) ? 0 0 0
354 0 ▶ existing->mode : git_index__create_mode(0666);
Branch: 1 branch
BranchTrueFalseTotal
existing->mode : git_index__create_mode(0666); 0 0 0
355  
356 50 return git_index__create_mode(mode);
357 }
358  
359 1773 GIT_INLINE(int) index_find_in_entries(
360 size_t *out, git_vector *entries, git_vector_cmp entry_srch,
361 const char *path, size_t path_len, int stage)
362 {
363 struct entry_srch_key srch_key;
364 1773 srch_key.path = path;
365 1773 ▶ srch_key.pathlen = !path_len ? strlen(path) : path_len;
Branch: 1 branch
BranchTrueFalseTotal
srch_key.pathlen = !path_len ? strlen(path) : path_len; 1553 220 1773
366 1773 srch_key.stage = stage;
367 1773 return git_vector_bsearch2(out, entries, entry_srch, &srch_key);
368 }
369  
370 1558 GIT_INLINE(int) index_find(
371 size_t *out, git_index *index,
372 const char *path, size_t path_len, int stage)
373 {
374 1558 git_vector_sort(&index->entries);
375  
376 1558 return index_find_in_entries(
377 out, &index->entries, index->entries_search, path, path_len, stage);
378 }
379  
380 6 void git_index__set_ignore_case(git_index *index, bool ignore_case)
381 {
382 6 index->ignore_case = ignore_case;
383  
384 6 ▶ if (ignore_case) {
Branch: 1 branch
BranchTrueFalseTotal
if (ignore_case) { 0 6 6
385 0 index->entries_cmp_path = git__strcasecmp_cb;
386 0 index->entries_search = git_index_entry_isrch;
387 0 index->entries_search_path = index_entry_isrch_path;
388 0 index->reuc_search = reuc_isrch;
389 } else {
390 6 index->entries_cmp_path = git__strcmp_cb;
391 6 index->entries_search = git_index_entry_srch;
392 6 index->entries_search_path = index_entry_srch_path;
393 6 index->reuc_search = reuc_srch;
394 }
395  
396 6 ▶ git_vector_set_cmp(&index->entries,
Branch: 1 branch
BranchTrueFalseTotal
git_vector_set_cmp(&index->entries, 0 6 6
397 ignore_case ? git_index_entry_icmp : git_index_entry_cmp);
398 6 git_vector_sort(&index->entries);
399  
400 6 ▶ git_vector_set_cmp(&index->reuc, ignore_case ? reuc_icmp : reuc_cmp);
Branch: 1 branch
BranchTrueFalseTotal
git_vector_set_cmp(&index->reuc, ignore_case ? reuc_icmp : reuc_cmp); 0 6 6
401 6 git_vector_sort(&index->reuc);
402 6 }
403  
404 47 int git_index_open(git_index **index_out, const char *index_path)
405 {
406 git_index *index;
407 47 int error = -1;
408  
409 47 ▶ GIT_ASSERT_ARG(index_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index_out); 0 47 47
410  
411 47 index = git__calloc(1, sizeof(git_index));
412 47 ▶ GIT_ERROR_CHECK_ALLOC(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(index); 0 47 47
413  
414 47 ▶ if (git_pool_init(&index->tree_pool, 1) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_pool_init(&index->tree_pool, 1) < 0) 0 47 47
415 0 goto fail;
416  
417 47 ▶ if (index_path != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (index_path != NULL) { 16 31 47
418 16 index->index_file_path = git__strdup(index_path);
419 16 ▶ if (!index->index_file_path)
Branch: 1 branch
BranchTrueFalseTotal
if (!index->index_file_path) 0 16 16
420 0 goto fail;
421  
422 /* Check if index file is stored on disk already */
423 16 ▶ if (git_fs_path_exists(index->index_file_path) == true)
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_exists(index->index_file_path) == true) 10 6 16
424 10 index->on_disk = 1;
425 }
426  
427 94 if (git_vector_init(&index->entries, 32, git_index_entry_cmp) < 0 ||
428 94 ▶ git_idxmap_new(&index->entries_map) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_idxmap_new(&index->entries_map) < 0 || 47 0 47
429 94 ▶ git_vector_init(&index->names, 8, conflict_name_cmp) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_vector_init(&index->names, 8, conflict_name_cmp) < 0 || 47 0 47
430 94 ▶ git_vector_init(&index->reuc, 8, reuc_cmp) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_vector_init(&index->reuc, 8, reuc_cmp) < 0 || 0 47 47
431 47 git_vector_init(&index->deleted, 8, git_index_entry_cmp) < 0)
432 0 goto fail;
433  
434 47 index->entries_cmp_path = git__strcmp_cb;
435 47 index->entries_search = git_index_entry_srch;
436 47 index->entries_search_path = index_entry_srch_path;
437 47 index->reuc_search = reuc_srch;
438 47 index->version = INDEX_VERSION_NUMBER_DEFAULT;
439  
440 47 ▶ if (index_path != NULL && (error = git_index_read(index, true)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (index_path != NULL && (error = git_index_read(index, true)) < 0) 16 31 47
if (index_path != NULL && (error = git_index_read(index, true)) < 0) 0 16 16
441 0 goto fail;
442  
443 47 *index_out = index;
444 47 GIT_REFCOUNT_INC(index);
445  
446 47 return 0;
447  
448 0 fail:
449 0 git_pool_clear(&index->tree_pool);
450 0 git_index_free(index);
451 0 return error;
452 }
453  
454 31 int git_index_new(git_index **out)
455 {
456 31 return git_index_open(out, NULL);
457 }
458  
459 47 static void index_free(git_index *index)
460 {
461 /* index iterators increment the refcount of the index, so if we
462 * get here then there should be no outstanding iterators.
463 */
464 47 ▶ if (git_atomic32_get(&index->readers))
Branch: 1 branch
BranchTrueFalseTotal
if (git_atomic32_get(&index->readers)) 0 47 47
465 0 return;
466  
467 47 git_index_clear(index);
468 47 git_idxmap_free(index->entries_map);
469 47 git_vector_free(&index->entries);
470 47 git_vector_free(&index->names);
471 47 git_vector_free(&index->reuc);
472 47 git_vector_free(&index->deleted);
473  
474 47 git__free(index->index_file_path);
475  
476 47 git__memzero(index, sizeof(*index));
477 47 git__free(index);
478 }
479  
480 923 void git_index_free(git_index *index)
481 {
482 923 ▶ if (index == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (index == NULL) 109 814 923
483 109 return;
484  
485 814 ▶ GIT_REFCOUNT_DEC(index, index_free);
Branch: 2 branches
BranchTrueFalseTotal
GIT_REFCOUNT_DEC(index, index_free); 47 767 814
GIT_REFCOUNT_DEC(index, index_free); 47 0 47
486 }
487  
488 /* call with locked index */
489 71 static void index_free_deleted(git_index *index)
490 {
491 71 int readers = (int)git_atomic32_get(&index->readers);
492 size_t i;
493  
494 71 ▶ if (readers > 0 || !index->deleted.length)
Branch: 2 branches
BranchTrueFalseTotal
if (readers > 0 || !index->deleted.length) 71 0 71
if (readers > 0 || !index->deleted.length) 65 6 71
495 65 return;
496  
497 12 ▶ for (i = 0; i < index->deleted.length; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < index->deleted.length; ++i) { 6 6 12
498 6 git_index_entry *ie = git_atomic_swap(index->deleted.contents[i], NULL);
499 6 index_entry_free(ie);
500 }
501  
502 6 git_vector_clear(&index->deleted);
503 }
504  
505 /* call with locked index */
506 167 static int index_remove_entry(git_index *index, size_t pos)
507 {
508 167 int error = 0;
509 167 git_index_entry *entry = git_vector_get(&index->entries, pos);
510  
511 167 ▶ if (entry != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry != NULL) { 167 0 167
512 167 git_tree_cache_invalidate_path(index->tree, entry->path);
513 167 index_map_delete(index->entries_map, entry, index->ignore_case);
514 }
515  
516 167 error = git_vector_remove(&index->entries, pos);
517  
518 167 ▶ if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error) { 167 0 167
519 167 ▶ if (git_atomic32_get(&index->readers) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_atomic32_get(&index->readers) > 0) { 6 161 167
520 6 error = git_vector_insert(&index->deleted, entry);
521 } else {
522 161 index_entry_free(entry);
523 }
524  
525 167 index->dirty = 1;
526 }
527  
528 167 return error;
529 }
530  
531 71 int git_index_clear(git_index *index)
532 {
533 71 int error = 0;
534  
535 71 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 71 71
536  
537 71 index->dirty = 1;
538 71 index->tree = NULL;
539 71 git_pool_clear(&index->tree_pool);
540  
541 71 git_idxmap_clear(index->entries_map);
542 211 ▶ while (!error && index->entries.length > 0)
Branch: 2 branches
BranchTrueFalseTotal
while (!error && index->entries.length > 0) 211 0 211
while (!error && index->entries.length > 0) 140 71 211
543 140 error = index_remove_entry(index, index->entries.length - 1);
544  
545 71 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 0 71 71
546 0 goto done;
547  
548 71 index_free_deleted(index);
549  
550 142 if ((error = git_index_name_clear(index)) < 0 ||
551 71 (error = git_index_reuc_clear(index)) < 0)
552 0 goto done;
553  
554 71 git_futils_filestamp_set(&index->stamp, NULL);
555  
556 71 done:
557 71 return error;
558 }
559  
560 0 static int create_index_error(int error, const char *msg)
561 {
562 0 git_error_set_str(GIT_ERROR_INDEX, msg);
563 0 return error;
564 }
565  
566 16 int git_index_set_caps(git_index *index, int caps)
567 {
568 unsigned int old_ignore_case;
569  
570 16 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 16 16
571  
572 16 old_ignore_case = index->ignore_case;
573  
574 16 ▶ if (caps == GIT_INDEX_CAPABILITY_FROM_OWNER) {
Branch: 1 branch
BranchTrueFalseTotal
if (caps == GIT_INDEX_CAPABILITY_FROM_OWNER) { 16 0 16
575 16 git_repository *repo = INDEX_OWNER(index);
576 int val;
577  
578 16 ▶ if (!repo)
Branch: 1 branch
BranchTrueFalseTotal
if (!repo) 0 16 16
579 0 return create_index_error(
580 -1, "cannot access repository to set index caps");
581  
582 16 ▶ if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_IGNORECASE))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_IGNORECASE)) 16 0 16
583 16 index->ignore_case = (val != 0);
584 16 ▶ if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_FILEMODE))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_FILEMODE)) 16 0 16
585 16 index->distrust_filemode = (val == 0);
586 16 ▶ if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_SYMLINKS))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_SYMLINKS)) 16 0 16
587 16 index->no_symlinks = (val == 0);
588 }
589 else {
590 0 index->ignore_case = ((caps & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0);
591 0 index->distrust_filemode = ((caps & GIT_INDEX_CAPABILITY_NO_FILEMODE) != 0);
592 0 index->no_symlinks = ((caps & GIT_INDEX_CAPABILITY_NO_SYMLINKS) != 0);
593 }
594  
595 16 ▶ if (old_ignore_case != index->ignore_case) {
Branch: 1 branch
BranchTrueFalseTotal
if (old_ignore_case != index->ignore_case) { 0 16 16
596 0 git_index__set_ignore_case(index, (bool)index->ignore_case);
597 }
598  
599 16 return 0;
600 }
601  
602 1 int git_index_caps(const git_index *index)
603 {
604 1 return ((index->ignore_case ? GIT_INDEX_CAPABILITY_IGNORE_CASE : 0) |
605 1 (index->distrust_filemode ? GIT_INDEX_CAPABILITY_NO_FILEMODE : 0) |
606 (index->no_symlinks ? GIT_INDEX_CAPABILITY_NO_SYMLINKS : 0));
607 }
608  
609 #ifndef GIT_DEPRECATE_HARD
610 1 const git_oid *git_index_checksum(git_index *index)
611 {
612 1 return (git_oid *)index->checksum;
613 }
614 #endif
615  
616 /**
617 * Returns 1 for changed, 0 for not changed and <0 for errors
618 */
619 135 static int compare_checksum(git_index *index)
620 {
621 int fd;
622 ssize_t bytes_read;
623 unsigned char checksum[GIT_HASH_SHA1_SIZE];
624 135 size_t checksum_size = GIT_HASH_SHA1_SIZE;
625  
626 135 ▶ if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0) 0 135 135
627 0 return fd;
628  
629 135 ▶ if (p_lseek(fd, (0 - (ssize_t)checksum_size), SEEK_END) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_lseek(fd, (0 - (ssize_t)checksum_size), SEEK_END) < 0) { 0 135 135
630 0 p_close(fd);
631 0 git_error_set(GIT_ERROR_OS, "failed to seek to end of file");
632 0 return -1;
633 }
634  
635 135 bytes_read = p_read(fd, checksum, checksum_size);
636 135 p_close(fd);
637  
638 135 ▶ if (bytes_read < (ssize_t)checksum_size)
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_read < (ssize_t)checksum_size) 0 135 135
639 0 return -1;
640  
641 135 return !!memcmp(checksum, index->checksum, checksum_size);
642 }
643  
644 152 int git_index_read(git_index *index, int force)
645 {
646 152 int error = 0, updated;
647 152 git_str buffer = GIT_STR_INIT;
648 152 git_futils_filestamp stamp = index->stamp;
649  
650 152 ▶ if (!index->index_file_path)
Branch: 1 branch
BranchTrueFalseTotal
if (!index->index_file_path) 0 152 152
651 0 return create_index_error(-1,
652 "failed to read index: The index is in-memory only");
653  
654 152 index->on_disk = git_fs_path_exists(index->index_file_path);
655  
656 152 ▶ if (!index->on_disk) {
Branch: 1 branch
BranchTrueFalseTotal
if (!index->on_disk) { 17 135 152
657 17 ▶ if (force && (error = git_index_clear(index)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (force && (error = git_index_clear(index)) < 0) 6 11 17
if (force && (error = git_index_clear(index)) < 0) 0 6 6
658 0 return error;
659  
660 17 index->dirty = 0;
661 17 return 0;
662 }
663  
664 270 if ((updated = git_futils_filestamp_check(&stamp, index->index_file_path) < 0) ||
665 135 ((updated = compare_checksum(index)) < 0)) {
666 0 git_error_set(
667 GIT_ERROR_INDEX,
668 "failed to read index: '%s' no longer exists",
669 index->index_file_path);
670 0 return updated;
671 }
672  
673 135 ▶ if (!updated && !force)
Branch: 2 branches
BranchTrueFalseTotal
if (!updated && !force) 125 10 135
if (!updated && !force) 124 1 125
674 124 return 0;
675  
676 11 error = git_futils_readbuffer(&buffer, index->index_file_path);
677 11 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 11 11
678 0 return error;
679  
680 11 index->tree = NULL;
681 11 git_pool_clear(&index->tree_pool);
682  
683 11 error = git_index_clear(index);
684  
685 11 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 11 0 11
686 11 error = parse_index(index, buffer.ptr, buffer.size);
687  
688 11 ▶ if (!error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error) { 11 0 11
689 11 git_futils_filestamp_set(&index->stamp, &stamp);
690 11 index->dirty = 0;
691 }
692  
693 11 git_str_dispose(&buffer);
694 11 return error;
695 }
696  
697 91 int git_index_read_safely(git_index *index)
698 {
699 91 ▶ if (git_index__enforce_unsaved_safety && index->dirty) {
Branch: 2 branches
BranchTrueFalseTotal
if (git_index__enforce_unsaved_safety && index->dirty) { 0 91 91
if (git_index__enforce_unsaved_safety && index->dirty) { 0 0 0
700 0 git_error_set(GIT_ERROR_INDEX,
701 "the index has unsaved changes that would be overwritten by this operation");
702 0 return GIT_EINDEXDIRTY;
703 }
704  
705 91 return git_index_read(index, false);
706 }
707  
708 111 static bool is_racy_entry(git_index *index, const git_index_entry *entry)
709 {
710 /* Git special-cases submodules in the check */
711 111 ▶ if (S_ISGITLINK(entry->mode))
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISGITLINK(entry->mode)) 0 111 111
712 0 return false;
713  
714 111 return git_index_entry_newer_than_index(entry, index);
715 }
716  
717 /*
718 * Force the next diff to take a look at those entries which have the
719 * same timestamp as the current index.
720 */
721 83 static int truncate_racily_clean(git_index *index)
722 {
723 size_t i;
724 int error;
725 git_index_entry *entry;
726 83 git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
727 83 git_diff *diff = NULL;
728 83 git_vector paths = GIT_VECTOR_INIT;
729 git_diff_delta *delta;
730  
731 /* Nothing to do if there's no repo to talk about */
732 83 ▶ if (!INDEX_OWNER(index))
Branch: 1 branch
BranchTrueFalseTotal
if (!INDEX_OWNER(index)) 0 83 83
733 0 return 0;
734  
735 /* If there's no workdir, we can't know where to even check */
736 83 ▶ if (!git_repository_workdir(INDEX_OWNER(index)))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_repository_workdir(INDEX_OWNER(index))) 0 83 83
737 0 return 0;
738  
739 83 diff_opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_IGNORE_SUBMODULES | GIT_DIFF_DISABLE_PATHSPEC_MATCH;
740 254 ▶ git_vector_foreach(&index->entries, i, entry) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&index->entries, i, entry) { 171 83 254
741 282 if ((entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE) == 0 &&
742 111 is_racy_entry(index, entry))
743 27 git_vector_insert(&paths, (char *)entry->path);
744 }
745  
746 83 ▶ if (paths.length == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (paths.length == 0) 61 22 83
747 61 goto done;
748  
749 22 diff_opts.pathspec.count = paths.length;
750 22 diff_opts.pathspec.strings = (char **)paths.contents;
751  
752 22 ▶ if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0) 0 22 22
753 0 return error;
754  
755 22 ▶ git_vector_foreach(&diff->deltas, i, delta) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&diff->deltas, i, delta) { 0 22 22
756 0 entry = (git_index_entry *)git_index_get_bypath(index, delta->old_file.path, 0);
757  
758 /* Ensure that we have a stage 0 for this file (ie, it's not a
759 * conflict), otherwise smudging it is quite pointless.
760 */
761 0 ▶ if (entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry) { 0 0 0
762 0 entry->file_size = 0;
763 0 index->dirty = 1;
764 }
765 }
766  
767 22 done:
768 83 git_diff_free(diff);
769 83 git_vector_free(&paths);
770 83 return 0;
771 }
772  
773 3 unsigned git_index_version(git_index *index)
774 {
775 3 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 3 3
776  
777 3 return index->version;
778 }
779  
780 1 int git_index_set_version(git_index *index, unsigned int version)
781 {
782 1 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 1 1
783  
784 1 ▶ if (version < INDEX_VERSION_NUMBER_LB ||
Branch: 1 branch
BranchTrueFalseTotal
if (version < INDEX_VERSION_NUMBER_LB || 1 0 1
785 1 ▶ version > INDEX_VERSION_NUMBER_UB) {
Branch: 1 branch
BranchTrueFalseTotal
version > INDEX_VERSION_NUMBER_UB) { 0 1 1
786 0 git_error_set(GIT_ERROR_INDEX, "invalid version number");
787 0 return -1;
788 }
789  
790 1 index->version = version;
791  
792 1 return 0;
793 }
794  
795 83 int git_index_write(git_index *index)
796 {
797 83 git_indexwriter writer = GIT_INDEXWRITER_INIT;
798 int error;
799  
800 83 truncate_racily_clean(index);
801  
802 166 if ((error = git_indexwriter_init(&writer, index)) == 0 &&
803 83 (error = git_indexwriter_commit(&writer)) == 0)
804 83 index->dirty = 0;
805  
806 83 git_indexwriter_cleanup(&writer);
807  
808 83 return error;
809 }
810  
811 3 const char *git_index_path(const git_index *index)
812 {
813 3 ▶ GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); 0 3 3
814 3 return index->index_file_path;
815 }
816  
817 38 int git_index_write_tree(git_oid *oid, git_index *index)
818 {
819 git_repository *repo;
820  
821 38 ▶ GIT_ASSERT_ARG(oid);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(oid); 0 38 38
822 38 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 38 38
823  
824 38 repo = INDEX_OWNER(index);
825  
826 38 ▶ if (repo == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (repo == NULL) 0 38 38
827 0 return create_index_error(-1, "Failed to write tree. "
828 "the index file is not backed up by an existing repository");
829  
830 38 return git_tree__write_index(oid, index, repo);
831 }
832  
833 18 int git_index_write_tree_to(
834 git_oid *oid, git_index *index, git_repository *repo)
835 {
836 18 ▶ GIT_ASSERT_ARG(oid);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(oid); 0 18 18
837 18 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 18 18
838 18 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 18 18
839  
840 18 return git_tree__write_index(oid, index, repo);
841 }
842  
843 270 size_t git_index_entrycount(const git_index *index)
844 {
845 270 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 270 270
846  
847 270 return index->entries.length;
848 }
849  
850 419 const git_index_entry *git_index_get_byindex(
851 git_index *index, size_t n)
852 {
853 419 ▶ GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); 0 419 419
854  
855 419 git_vector_sort(&index->entries);
856 419 return git_vector_get(&index->entries, n);
857 }
858  
859 182 const git_index_entry *git_index_get_bypath(
860 git_index *index, const char *path, int stage)
861 {
862 182 git_index_entry key = {{ 0 }};
863 git_index_entry *value;
864  
865 182 ▶ GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); 0 182 182
866  
867 182 key.path = path;
868 182 GIT_INDEX_ENTRY_STAGE_SET(&key, stage);
869  
870 182 ▶ if (index->ignore_case)
Branch: 1 branch
BranchTrueFalseTotal
if (index->ignore_case) 0 182 182
871 0 value = git_idxmap_icase_get((git_idxmap_icase *) index->entries_map, &key);
872 else
873 182 value = git_idxmap_get(index->entries_map, &key);
874  
875 182 ▶ if (!value) {
Branch: 1 branch
BranchTrueFalseTotal
if (!value) { 23 159 182
876 23 git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path);
877 23 return NULL;
878 }
879  
880 159 return value;
881 }
882  
883 91 void git_index_entry__init_from_stat(
884 git_index_entry *entry, struct stat *st, bool trust_mode)
885 {
886 91 entry->ctime.seconds = (int32_t)st->st_ctime;
887 91 entry->mtime.seconds = (int32_t)st->st_mtime;
888 #if defined(GIT_USE_NSEC)
889 entry->mtime.nanoseconds = st->st_mtime_nsec;
890 entry->ctime.nanoseconds = st->st_ctime_nsec;
891 #endif
892 91 entry->dev = st->st_rdev;
893 91 entry->ino = st->st_ino;
894 0 ▶ entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ?
Branch: 1 branch
BranchTrueFalseTotal
entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ? 0 0 0
895 91 ▶ git_index__create_mode(0666) : git_index__create_mode(st->st_mode);
Branch: 1 branch
BranchTrueFalseTotal
git_index__create_mode(0666) : git_index__create_mode(st->st_mode); 0 91 91
896 91 entry->uid = st->st_uid;
897 91 entry->gid = st->st_gid;
898 91 entry->file_size = (uint32_t)st->st_size;
899 91 }
900  
901 200 static void index_entry_adjust_namemask(
902 git_index_entry *entry,
903 size_t path_length)
904 {
905 200 entry->flags &= ~GIT_INDEX_ENTRY_NAMEMASK;
906  
907 200 ▶ if (path_length < GIT_INDEX_ENTRY_NAMEMASK)
Branch: 1 branch
BranchTrueFalseTotal
if (path_length < GIT_INDEX_ENTRY_NAMEMASK) 200 0 200
908 200 entry->flags |= path_length & GIT_INDEX_ENTRY_NAMEMASK;
909 else
910 0 entry->flags |= GIT_INDEX_ENTRY_NAMEMASK;
911 200 }
912  
913 /* When `from_workdir` is true, we will validate the paths to avoid placing
914 * paths that are invalid for the working directory on the current filesystem
915 * (eg, on Windows, we will disallow `GIT~1`, `AUX`, `COM1`, etc). This
916 * function will *always* prevent `.git` and directory traversal `../` from
917 * being added to the index.
918 */
919 228 static int index_entry_create(
920 git_index_entry **out,
921 git_repository *repo,
922 const char *path,
923 struct stat *st,
924 bool from_workdir)
925 {
926 228 size_t pathlen = strlen(path), alloclen;
927 struct entry_internal *entry;
928 228 unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS;
929 228 uint16_t mode = 0;
930  
931 /* always reject placing `.git` in the index and directory traversal.
932 * when requested, disallow platform-specific filenames and upgrade to
933 * the platform-specific `.git` tests (eg, `git~1`, etc).
934 */
935 228 ▶ if (from_workdir)
Branch: 1 branch
BranchTrueFalseTotal
if (from_workdir) 52 176 228
936 52 path_valid_flags |= GIT_PATH_REJECT_WORKDIR_DEFAULTS;
937 228 ▶ if (st)
Branch: 1 branch
BranchTrueFalseTotal
if (st) 52 176 228
938 52 mode = st->st_mode;
939  
940 228 ▶ if (!git_path_is_valid(repo, path, mode, path_valid_flags)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_path_is_valid(repo, path, mode, path_valid_flags)) { 0 228 228
941 0 git_error_set(GIT_ERROR_INDEX, "invalid path: '%s'", path);
942 0 return -1;
943 }
944  
945 228 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(struct entry_internal), pathlen);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(struct entry_internal), pathlen); 0 228 228
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(struct entry_internal), pathlen); 0 228 228
946 228 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); 0 228 228
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); 0 228 228
947 228 entry = git__calloc(1, alloclen);
948 228 ▶ GIT_ERROR_CHECK_ALLOC(entry);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(entry); 0 228 228
949  
950 228 entry->pathlen = pathlen;
951 228 memcpy(entry->path, path, pathlen);
952 228 entry->entry.path = entry->path;
953  
954 228 *out = (git_index_entry *)entry;
955 228 return 0;
956 }
957  
958 52 static int index_entry_init(
959 git_index_entry **entry_out,
960 git_index *index,
961 const char *rel_path)
962 {
963 52 int error = 0;
964 52 git_index_entry *entry = NULL;
965 52 git_str path = GIT_STR_INIT;
966 struct stat st;
967 git_oid oid;
968 git_repository *repo;
969  
970 52 ▶ if (INDEX_OWNER(index) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (INDEX_OWNER(index) == NULL) 0 52 52
971 0 return create_index_error(-1,
972 "could not initialize index entry. "
973 "Index is not backed up by an existing repository.");
974  
975 /*
976 * FIXME: this is duplicated with the work in
977 * git_blob__create_from_paths. It should accept an optional stat
978 * structure so we can pass in the one we have to do here.
979 */
980 52 repo = INDEX_OWNER(index);
981 52 ▶ if (git_repository__ensure_not_bare(repo, "create blob from file") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository__ensure_not_bare(repo, "create blob from file") < 0) 0 52 52
982 0 return GIT_EBAREREPO;
983  
984 52 ▶ if (git_repository_workdir_path(&path, repo, rel_path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository_workdir_path(&path, repo, rel_path) < 0) 0 52 52
985 0 return -1;
986  
987 52 error = git_fs_path_lstat(path.ptr, &st);
988 52 git_str_dispose(&path);
989  
990 52 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 52 52
991 0 return error;
992  
993 52 ▶ if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, &st, true) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, &st, true) < 0) 0 52 52
994 0 return -1;
995  
996 /* write the blob to disk and get the oid and stat info */
997 52 error = git_blob__create_from_paths(
998 52 &oid, &st, INDEX_OWNER(index), NULL, rel_path, 0, true);
999  
1000 52 ▶ if (error < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) { 2 50 52
1001 2 index_entry_free(entry);
1002 2 return error;
1003 }
1004  
1005 50 entry->id = oid;
1006 50 git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
1007  
1008 50 *entry_out = (git_index_entry *)entry;
1009 50 return 0;
1010 }
1011  
1012 10 static git_index_reuc_entry *reuc_entry_alloc(const char *path)
1013 {
1014 10 size_t pathlen = strlen(path),
1015 10 structlen = sizeof(struct reuc_entry_internal),
1016 alloclen;
1017 struct reuc_entry_internal *entry;
1018  
1019 10 ▶ if (GIT_ADD_SIZET_OVERFLOW(&alloclen, structlen, pathlen) ||
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, structlen, pathlen) || 0 10 10
1020 10 ▶ GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1))
Branch: 1 branch
BranchTrueFalseTotal
GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1)) 0 10 10
1021 0 return NULL;
1022  
1023 10 entry = git__calloc(1, alloclen);
1024 10 ▶ if (!entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) 0 10 10
1025 0 return NULL;
1026  
1027 10 entry->pathlen = pathlen;
1028 10 memcpy(entry->path, path, pathlen);
1029 10 entry->entry.path = entry->path;
1030  
1031 10 return (git_index_reuc_entry *)entry;
1032 }
1033  
1034 10 static int index_entry_reuc_init(git_index_reuc_entry **reuc_out,
1035 const char *path,
1036 int ancestor_mode, const git_oid *ancestor_oid,
1037 int our_mode, const git_oid *our_oid,
1038 int their_mode, const git_oid *their_oid)
1039 {
1040 10 git_index_reuc_entry *reuc = NULL;
1041  
1042 10 ▶ GIT_ASSERT_ARG(reuc_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(reuc_out); 0 10 10
1043 10 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 10 10
1044  
1045 10 *reuc_out = reuc = reuc_entry_alloc(path);
1046 10 ▶ GIT_ERROR_CHECK_ALLOC(reuc);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(reuc); 0 10 10
1047  
1048 10 ▶ if ((reuc->mode[0] = ancestor_mode) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((reuc->mode[0] = ancestor_mode) > 0) { 10 0 10
1049 10 ▶ GIT_ASSERT(ancestor_oid);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(ancestor_oid); 0 10 10
1050 10 git_oid_cpy(&reuc->oid[0], ancestor_oid);
1051 }
1052  
1053 10 ▶ if ((reuc->mode[1] = our_mode) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((reuc->mode[1] = our_mode) > 0) { 8 2 10
1054 8 ▶ GIT_ASSERT(our_oid);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(our_oid); 0 8 8
1055 8 git_oid_cpy(&reuc->oid[1], our_oid);
1056 }
1057  
1058 10 ▶ if ((reuc->mode[2] = their_mode) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((reuc->mode[2] = their_mode) > 0) { 6 4 10
1059 6 ▶ GIT_ASSERT(their_oid);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(their_oid); 0 6 6
1060 6 git_oid_cpy(&reuc->oid[2], their_oid);
1061 }
1062  
1063 10 return 0;
1064 }
1065  
1066 231 static void index_entry_cpy(
1067 git_index_entry *tgt,
1068 const git_index_entry *src)
1069 {
1070 231 const char *tgt_path = tgt->path;
1071 231 memcpy(tgt, src, sizeof(*tgt));
1072 231 tgt->path = tgt_path;
1073 231 }
1074  
1075 165 static int index_entry_dup(
1076 git_index_entry **out,
1077 git_index *index,
1078 const git_index_entry *src)
1079 {
1080 165 ▶ if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0) 0 165 165
1081 0 return -1;
1082  
1083 165 index_entry_cpy(*out, src);
1084 165 return 0;
1085 }
1086  
1087 2 static void index_entry_cpy_nocache(
1088 git_index_entry *tgt,
1089 const git_index_entry *src)
1090 {
1091 2 git_oid_cpy(&tgt->id, &src->id);
1092 2 tgt->mode = src->mode;
1093 2 tgt->flags = src->flags;
1094 2 tgt->flags_extended = (src->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS);
1095 2 }
1096  
1097 2 static int index_entry_dup_nocache(
1098 git_index_entry **out,
1099 git_index *index,
1100 const git_index_entry *src)
1101 {
1102 2 ▶ if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0) 0 2 2
1103 0 return -1;
1104  
1105 2 index_entry_cpy_nocache(*out, src);
1106 2 return 0;
1107 }
1108  
1109 126 static int has_file_name(git_index *index,
1110 const git_index_entry *entry, size_t pos, int ok_to_replace)
1111 {
1112 126 size_t len = strlen(entry->path);
1113 126 int stage = GIT_INDEX_ENTRY_STAGE(entry);
1114 126 const char *name = entry->path;
1115  
1116 127 ▶ while (pos < index->entries.length) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < index->entries.length) { 109 18 127
1117 109 struct entry_internal *p = index->entries.contents[pos++];
1118  
1119 109 ▶ if (len >= p->pathlen)
Branch: 1 branch
BranchTrueFalseTotal
if (len >= p->pathlen) 105 4 109
1120 105 break;
1121 4 ▶ if (memcmp(name, p->path, len))
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(name, p->path, len)) 3 1 4
1122 3 break;
1123 1 ▶ if (GIT_INDEX_ENTRY_STAGE(&p->entry) != stage)
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_INDEX_ENTRY_STAGE(&p->entry) != stage) 0 1 1
1124 0 continue;
1125 1 ▶ if (p->path[len] != '/')
Branch: 1 branch
BranchTrueFalseTotal
if (p->path[len] != '/') 1 0 1
1126 1 continue;
1127 0 ▶ if (!ok_to_replace)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok_to_replace) 0 0 0
1128 0 return -1;
1129  
1130 0 ▶ if (index_remove_entry(index, --pos) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index_remove_entry(index, --pos) < 0) 0 0 0
1131 0 break;
1132 }
1133 126 return 0;
1134 }
1135  
1136 /*
1137 * Do we have another file with a pathname that is a proper
1138 * subset of the name we're trying to add?
1139 */
1140 126 static int has_dir_name(git_index *index,
1141 const git_index_entry *entry, int ok_to_replace)
1142 {
1143 126 int stage = GIT_INDEX_ENTRY_STAGE(entry);
1144 126 const char *name = entry->path;
1145 126 const char *slash = name + strlen(name);
1146  
1147 11 for (;;) {
1148 size_t len, pos;
1149  
1150 for (;;) {
1151 847 ▶ if (*--slash == '/')
Branch: 1 branch
BranchTrueFalseTotal
if (*--slash == '/') 13 834 847
1152 13 break;
1153 834 ▶ if (slash <= entry->path)
Branch: 1 branch
BranchTrueFalseTotal
if (slash <= entry->path) 124 710 834
1154 126 return 0;
1155 }
1156 13 len = slash - name;
1157  
1158 13 ▶ if (!index_find(&pos, index, name, len, stage)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!index_find(&pos, index, name, len, stage)) { 0 13 13
1159 0 ▶ if (!ok_to_replace)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok_to_replace) 0 0 0
1160 0 return -1;
1161  
1162 0 ▶ if (index_remove_entry(index, pos) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index_remove_entry(index, pos) < 0) 0 0 0
1163 0 break;
1164 0 continue;
1165 }
1166  
1167 /*
1168 * Trivial optimization: if we find an entry that
1169 * already matches the sub-directory, then we know
1170 * we're ok, and we can exit.
1171 */
1172 13 ▶ for (; pos < index->entries.length; ++pos) {
Branch: 1 branch
BranchTrueFalseTotal
for (; pos < index->entries.length; ++pos) { 7 6 13
1173 7 struct entry_internal *p = index->entries.contents[pos];
1174  
1175 7 ▶ if (p->pathlen <= len ||
Branch: 1 branch
BranchTrueFalseTotal
if (p->pathlen <= len || 7 0 7
1176 7 ▶ p->path[len] != '/' ||
Branch: 1 branch
BranchTrueFalseTotal
p->path[len] != '/' || 2 5 7
1177 2 ▶ memcmp(p->path, name, len))
Branch: 1 branch
BranchTrueFalseTotal
memcmp(p->path, name, len)) 2 0 2
1178 break; /* not our subdirectory */
1179  
1180 2 ▶ if (GIT_INDEX_ENTRY_STAGE(&p->entry) == stage)
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_INDEX_ENTRY_STAGE(&p->entry) == stage) 2 0 2
1181 2 return 0;
1182 }
1183 }
1184  
1185 0 return 0;
1186 }
1187  
1188 126 static int check_file_directory_collision(git_index *index,
1189 git_index_entry *entry, size_t pos, int ok_to_replace)
1190 {
1191 252 if (has_file_name(index, entry, pos, ok_to_replace) < 0 ||
1192 126 has_dir_name(index, entry, ok_to_replace) < 0) {
1193 0 git_error_set(GIT_ERROR_INDEX,
1194 "'%s' appears as both a file and a directory", entry->path);
1195 0 return -1;
1196 }
1197  
1198 126 return 0;
1199 }
1200  
1201 50 static int canonicalize_directory_path(
1202 git_index *index,
1203 git_index_entry *entry,
1204 git_index_entry *existing)
1205 {
1206 50 const git_index_entry *match, *best = NULL;
1207 char *search, *sep;
1208 size_t pos, search_len, best_len;
1209  
1210 50 ▶ if (!index->ignore_case)
Branch: 1 branch
BranchTrueFalseTotal
if (!index->ignore_case) 50 0 50
1211 50 return 0;
1212  
1213 /* item already exists in the index, simply re-use the existing case */
1214 0 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 0 0 0
1215 0 memcpy((char *)entry->path, existing->path, strlen(existing->path));
1216 0 return 0;
1217 }
1218  
1219 /* nothing to do */
1220 0 ▶ if (strchr(entry->path, '/') == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(entry->path, '/') == NULL) 0 0 0
1221 0 return 0;
1222  
1223 0 ▶ if ((search = git__strdup(entry->path)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((search = git__strdup(entry->path)) == NULL) 0 0 0
1224 0 return -1;
1225  
1226 /* starting at the parent directory and descending to the root, find the
1227 * common parent directory.
1228 */
1229 0 ▶ while (!best && (sep = strrchr(search, '/'))) {
Branch: 2 branches
BranchTrueFalseTotal
while (!best && (sep = strrchr(search, '/'))) { 0 0 0
while (!best && (sep = strrchr(search, '/'))) { 0 0 0
1230 0 sep[1] = '\0';
1231  
1232 0 search_len = strlen(search);
1233  
1234 0 git_vector_bsearch2(
1235 &pos, &index->entries, index->entries_search_path, search);
1236  
1237 0 ▶ while ((match = git_vector_get(&index->entries, pos))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((match = git_vector_get(&index->entries, pos))) { 0 0 0
1238 0 ▶ if (GIT_INDEX_ENTRY_STAGE(match) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_INDEX_ENTRY_STAGE(match) != 0) { 0 0 0
1239 /* conflicts do not contribute to canonical paths */
1240 0 ▶ } else if (strncmp(search, match->path, search_len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(search, match->path, search_len) == 0) { 0 0 0
1241 /* prefer an exact match to the input filename */
1242 0 best = match;
1243 0 best_len = search_len;
1244 0 break;
1245 0 ▶ } else if (strncasecmp(search, match->path, search_len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncasecmp(search, match->path, search_len) == 0) { 0 0 0
1246 /* continue walking, there may be a path with an exact
1247 * (case sensitive) match later in the index, but use this
1248 * as the best match until that happens.
1249 */
1250 0 ▶ if (!best) {
Branch: 1 branch
BranchTrueFalseTotal
if (!best) { 0 0 0
1251 0 best = match;
1252 0 best_len = search_len;
1253 }
1254 } else {
1255 0 break;
1256 }
1257  
1258 0 pos++;
1259 }
1260  
1261 0 sep[0] = '\0';
1262 }
1263  
1264 0 ▶ if (best)
Branch: 1 branch
BranchTrueFalseTotal
if (best) 0 0 0
1265 0 memcpy((char *)entry->path, best->path, best_len);
1266  
1267 0 git__free(search);
1268 0 return 0;
1269 }
1270  
1271 0 static int index_no_dups(void **old, void *new)
1272 {
1273 0 const git_index_entry *entry = new;
1274 0 GIT_UNUSED(old);
1275 0 git_error_set(GIT_ERROR_INDEX, "'%s' appears multiple times at stage %d",
1276 0 entry->path, GIT_INDEX_ENTRY_STAGE(entry));
1277 0 return GIT_EEXISTS;
1278 }
1279  
1280 126 static void index_existing_and_best(
1281 git_index_entry **existing,
1282 size_t *existing_position,
1283 git_index_entry **best,
1284 git_index *index,
1285 const git_index_entry *entry)
1286 {
1287 git_index_entry *e;
1288 size_t pos;
1289 int error;
1290  
1291 126 error = index_find(&pos,
1292 126 index, entry->path, 0, GIT_INDEX_ENTRY_STAGE(entry));
1293  
1294 126 ▶ if (error == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == 0) { 57 69 126
1295 57 *existing = index->entries.contents[pos];
1296 57 *existing_position = pos;
1297 57 *best = index->entries.contents[pos];
1298 57 return;
1299 }
1300  
1301 69 *existing = NULL;
1302 69 *existing_position = 0;
1303 69 *best = NULL;
1304  
1305 69 ▶ if (GIT_INDEX_ENTRY_STAGE(entry) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_INDEX_ENTRY_STAGE(entry) == 0) { 43 26 69
1306 45 ▶ for (; pos < index->entries.length; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; pos < index->entries.length; pos++) { 19 26 45
1307 19 int (*strcomp)(const char *a, const char *b) =
1308 19 ▶ index->ignore_case ? git__strcasecmp : git__strcmp;
Branch: 1 branch
BranchTrueFalseTotal
index->ignore_case ? git__strcasecmp : git__strcmp; 0 19 19
1309  
1310 19 e = index->entries.contents[pos];
1311  
1312 19 ▶ if (strcomp(entry->path, e->path) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcomp(entry->path, e->path) != 0) 15 4 19
1313 15 break;
1314  
1315 4 ▶ if (GIT_INDEX_ENTRY_STAGE(e) == GIT_INDEX_STAGE_ANCESTOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_INDEX_ENTRY_STAGE(e) == GIT_INDEX_STAGE_ANCESTOR) { 2 2 4
1316 2 *best = e;
1317 2 continue;
1318 } else {
1319 2 *best = e;
1320 2 break;
1321 }
1322 }
1323 }
1324 }
1325  
1326 /* index_insert takes ownership of the new entry - if it can't insert
1327 * it, then it will return an error **and also free the entry**. When
1328 * it replaces an existing entry, it will update the entry_ptr with the
1329 * actual entry in the index (and free the passed in one).
1330 *
1331 * trust_path is whether we use the given path, or whether (on case
1332 * insensitive systems only) we try to canonicalize the given path to
1333 * be within an existing directory.
1334 *
1335 * trust_mode is whether we trust the mode in entry_ptr.
1336 *
1337 * trust_id is whether we trust the id or it should be validated.
1338 */
1339 126 static int index_insert(
1340 git_index *index,
1341 git_index_entry **entry_ptr,
1342 int replace,
1343 bool trust_path,
1344 bool trust_mode,
1345 bool trust_id)
1346 {
1347 git_index_entry *existing, *best, *entry;
1348 size_t path_length, position;
1349 int error;
1350  
1351 126 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 126 126
1352 126 ▶ GIT_ASSERT_ARG(entry_ptr);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(entry_ptr); 0 126 126
1353  
1354 126 entry = *entry_ptr;
1355  
1356 /* Make sure that the path length flag is correct */
1357 126 path_length = ((struct entry_internal *)entry)->pathlen;
1358 126 index_entry_adjust_namemask(entry, path_length);
1359  
1360 /* This entry is now up-to-date and should not be checked for raciness */
1361 126 entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE;
1362  
1363 126 git_vector_sort(&index->entries);
1364  
1365 /*
1366 * Look if an entry with this path already exists, either staged, or (if
1367 * this entry is a regular staged item) as the "ours" side of a conflict.
1368 */
1369 126 index_existing_and_best(&existing, &position, &best, index, entry);
1370  
1371 /* Update the file mode */
1372 126 entry->mode = trust_mode ?
1373 126 ▶ git_index__create_mode(entry->mode) :
Branch: 1 branch
BranchTrueFalseTotal
git_index__create_mode(entry->mode) : 76 50 126
1374 50 index_merge_mode(index, best, entry->mode);
1375  
1376 /* Canonicalize the directory name */
1377 126 ▶ if (!trust_path && (error = canonicalize_directory_path(index, entry, best)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!trust_path && (error = canonicalize_directory_path(index, entry, best)) < 0) 50 76 126
if (!trust_path && (error = canonicalize_directory_path(index, entry, best)) < 0) 0 50 50
1378 0 goto out;
1379  
1380 /* Ensure that the given id exists (unless it's a submodule) */
1381 126 ▶ if (!trust_id && INDEX_OWNER(index) &&
Branch: 2 branches
BranchTrueFalseTotal
if (!trust_id && INDEX_OWNER(index) && 69 57 126
if (!trust_id && INDEX_OWNER(index) && 43 26 69
1382 43 ▶ (entry->mode & GIT_FILEMODE_COMMIT) != GIT_FILEMODE_COMMIT) {
Branch: 1 branch
BranchTrueFalseTotal
(entry->mode & GIT_FILEMODE_COMMIT) != GIT_FILEMODE_COMMIT) { 43 0 43
1383  
1384 43 ▶ if (!git_object__is_valid(INDEX_OWNER(index), &entry->id,
Branch: 1 branch
BranchTrueFalseTotal
if (!git_object__is_valid(INDEX_OWNER(index), &entry->id, 0 43 43
1385 43 git_object__type_from_filemode(entry->mode))) {
1386 0 error = -1;
1387 0 goto out;
1388 }
1389 }
1390  
1391 /* Look for tree / blob name collisions, removing conflicts if requested */
1392 126 ▶ if ((error = check_file_directory_collision(index, entry, position, replace)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = check_file_directory_collision(index, entry, position, replace)) < 0) 0 126 126
1393 0 goto out;
1394  
1395 /*
1396 * If we are replacing an existing item, overwrite the existing entry
1397 * and return it in place of the passed in one.
1398 */
1399 126 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 57 69 126
1400 57 ▶ if (replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (replace) { 57 0 57
1401 57 index_entry_cpy(existing, entry);
1402  
1403 57 ▶ if (trust_path)
Branch: 1 branch
BranchTrueFalseTotal
if (trust_path) 30 27 57
1404 30 memcpy((char *)existing->path, entry->path, strlen(entry->path));
1405 }
1406  
1407 57 index_entry_free(entry);
1408 57 *entry_ptr = existing;
1409 } else {
1410 /*
1411 * If replace is not requested or no existing entry exists, insert
1412 * at the sorted position. (Since we re-sort after each insert to
1413 * check for dups, this is actually cheaper in the long run.)
1414 */
1415 138 if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0 ||
1416 69 (error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0)
1417 0 goto out;
1418 }
1419  
1420 126 index->dirty = 1;
1421  
1422 126 out:
1423 126 ▶ if (error < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) { 0 126 126
1424 0 index_entry_free(*entry_ptr);
1425 0 *entry_ptr = NULL;
1426 }
1427  
1428 126 return error;
1429 }
1430  
1431 63 static int index_conflict_to_reuc(git_index *index, const char *path)
1432 {
1433 const git_index_entry *conflict_entries[3];
1434 int ancestor_mode, our_mode, their_mode;
1435 git_oid const *ancestor_oid, *our_oid, *their_oid;
1436 int ret;
1437  
1438 63 ▶ if ((ret = git_index_conflict_get(&conflict_entries[0],
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = git_index_conflict_get(&conflict_entries[0], 61 2 63
1439 &conflict_entries[1], &conflict_entries[2], index, path)) < 0)
1440 61 return ret;
1441  
1442 2 ▶ ancestor_mode = conflict_entries[0] == NULL ? 0 : conflict_entries[0]->mode;
Branch: 1 branch
BranchTrueFalseTotal
ancestor_mode = conflict_entries[0] == NULL ? 0 : conflict_entries[0]->mode; 2 0 2
1443 2 ▶ our_mode = conflict_entries[1] == NULL ? 0 : conflict_entries[1]->mode;
Branch: 1 branch
BranchTrueFalseTotal
our_mode = conflict_entries[1] == NULL ? 0 : conflict_entries[1]->mode; 2 0 2
1444 2 ▶ their_mode = conflict_entries[2] == NULL ? 0 : conflict_entries[2]->mode;
Branch: 1 branch
BranchTrueFalseTotal
their_mode = conflict_entries[2] == NULL ? 0 : conflict_entries[2]->mode; 2 0 2
1445  
1446 2 ▶ ancestor_oid = conflict_entries[0] == NULL ? NULL : &conflict_entries[0]->id;
Branch: 1 branch
BranchTrueFalseTotal
ancestor_oid = conflict_entries[0] == NULL ? NULL : &conflict_entries[0]->id; 2 0 2
1447 2 ▶ our_oid = conflict_entries[1] == NULL ? NULL : &conflict_entries[1]->id;
Branch: 1 branch
BranchTrueFalseTotal
our_oid = conflict_entries[1] == NULL ? NULL : &conflict_entries[1]->id; 2 0 2
1448 2 ▶ their_oid = conflict_entries[2] == NULL ? NULL : &conflict_entries[2]->id;
Branch: 1 branch
BranchTrueFalseTotal
their_oid = conflict_entries[2] == NULL ? NULL : &conflict_entries[2]->id; 2 0 2
1449  
1450 2 ▶ if ((ret = git_index_reuc_add(index, path, ancestor_mode, ancestor_oid,
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = git_index_reuc_add(index, path, ancestor_mode, ancestor_oid, 2 0 2
1451 our_mode, our_oid, their_mode, their_oid)) >= 0)
1452 2 ret = git_index_conflict_remove(index, path);
1453  
1454 2 return ret;
1455 }
1456  
1457 78 GIT_INLINE(bool) is_file_or_link(const int filemode)
1458 {
1459 4 ▶ return (filemode == GIT_FILEMODE_BLOB ||
Branch: 1 branch
BranchTrueFalseTotal
return (filemode == GIT_FILEMODE_BLOB || 3 1 4
1460 82 ▶ filemode == GIT_FILEMODE_BLOB_EXECUTABLE ||
Branch: 2 branches
BranchTrueFalseTotal
filemode == GIT_FILEMODE_BLOB_EXECUTABLE || 4 74 78
filemode == GIT_FILEMODE_BLOB_EXECUTABLE || 1 2 3
1461 filemode == GIT_FILEMODE_LINK);
1462 }
1463  
1464 69 GIT_INLINE(bool) valid_filemode(const int filemode)
1465 {
1466 69 ▶ return (is_file_or_link(filemode) || filemode == GIT_FILEMODE_COMMIT);
Branch: 2 branches
BranchTrueFalseTotal
return (is_file_or_link(filemode) || filemode == GIT_FILEMODE_COMMIT); 0 69 69
return (is_file_or_link(filemode) || filemode == GIT_FILEMODE_COMMIT); 0 0 0
1467 }
1468  
1469 9 int git_index_add_from_buffer(
1470 git_index *index, const git_index_entry *source_entry,
1471 const void *buffer, size_t len)
1472 {
1473 9 git_index_entry *entry = NULL;
1474 9 int error = 0;
1475 git_oid id;
1476  
1477 9 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 9 9
1478 9 ▶ GIT_ASSERT_ARG(source_entry && source_entry->path);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ASSERT_ARG(source_entry && source_entry->path); 9 0 9
GIT_ASSERT_ARG(source_entry && source_entry->path); 0 9 9
1479  
1480 9 ▶ if (INDEX_OWNER(index) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (INDEX_OWNER(index) == NULL) 0 9 9
1481 0 return create_index_error(-1,
1482 "could not initialize index entry. "
1483 "Index is not backed up by an existing repository.");
1484  
1485 9 ▶ if (!is_file_or_link(source_entry->mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_file_or_link(source_entry->mode)) { 2 7 9
1486 2 git_error_set(GIT_ERROR_INDEX, "invalid filemode");
1487 2 return -1;
1488 }
1489  
1490 7 ▶ if (len > UINT32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > UINT32_MAX) { 0 7 7
1491 0 git_error_set(GIT_ERROR_INDEX, "buffer is too large");
1492 0 return -1;
1493 }
1494  
1495 7 ▶ if (index_entry_dup(&entry, index, source_entry) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index_entry_dup(&entry, index, source_entry) < 0) 0 7 7
1496 0 return -1;
1497  
1498 7 error = git_blob_create_from_buffer(&id, INDEX_OWNER(index), buffer, len);
1499 7 ▶ if (error < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) { 0 7 7
1500 0 index_entry_free(entry);
1501 0 return error;
1502 }
1503  
1504 7 git_oid_cpy(&entry->id, &id);
1505 7 entry->file_size = (uint32_t)len;
1506  
1507 7 ▶ if ((error = index_insert(index, &entry, 1, true, true, true)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_insert(index, &entry, 1, true, true, true)) < 0) 0 7 7
1508 0 return error;
1509  
1510 /* Adding implies conflict was resolved, move conflict entries to REUC */
1511 7 ▶ if ((error = index_conflict_to_reuc(index, entry->path)) < 0 && error != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if ((error = index_conflict_to_reuc(index, entry->path)) < 0 && error != GIT_ENOTFOUND) 7 0 7
if ((error = index_conflict_to_reuc(index, entry->path)) < 0 && error != GIT_ENOTFOUND) 0 7 7
1512 0 return error;
1513  
1514 7 git_tree_cache_invalidate_path(index->tree, entry->path);
1515 7 return 0;
1516 }
1517  
1518 0 static int add_repo_as_submodule(git_index_entry **out, git_index *index, const char *path)
1519 {
1520 git_repository *sub;
1521 0 git_str abspath = GIT_STR_INIT;
1522 0 git_repository *repo = INDEX_OWNER(index);
1523 git_reference *head;
1524 git_index_entry *entry;
1525 struct stat st;
1526 int error;
1527  
1528 0 ▶ if ((error = git_repository_workdir_path(&abspath, repo, path)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository_workdir_path(&abspath, repo, path)) < 0) 0 0 0
1529 0 return error;
1530  
1531 0 ▶ if ((error = p_stat(abspath.ptr, &st)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = p_stat(abspath.ptr, &st)) < 0) { 0 0 0
1532 0 git_error_set(GIT_ERROR_OS, "failed to stat repository dir");
1533 0 return -1;
1534 }
1535  
1536 0 ▶ if (index_entry_create(&entry, INDEX_OWNER(index), path, &st, true) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index_entry_create(&entry, INDEX_OWNER(index), path, &st, true) < 0) 0 0 0
1537 0 return -1;
1538  
1539 0 git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
1540  
1541 0 ▶ if ((error = git_repository_open(&sub, abspath.ptr)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository_open(&sub, abspath.ptr)) < 0) 0 0 0
1542 0 return error;
1543  
1544 0 ▶ if ((error = git_repository_head(&head, sub)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository_head(&head, sub)) < 0) 0 0 0
1545 0 return error;
1546  
1547 0 git_oid_cpy(&entry->id, git_reference_target(head));
1548 0 entry->mode = GIT_FILEMODE_COMMIT;
1549  
1550 0 git_reference_free(head);
1551 0 git_repository_free(sub);
1552 0 git_str_dispose(&abspath);
1553  
1554 0 *out = entry;
1555 0 return 0;
1556 }
1557  
1558 52 int git_index_add_bypath(git_index *index, const char *path)
1559 {
1560 52 git_index_entry *entry = NULL;
1561 int ret;
1562  
1563 52 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 52 52
1564 52 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 52 52
1565  
1566 52 ▶ if ((ret = index_entry_init(&entry, index, path)) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = index_entry_init(&entry, index, path)) == 0) 50 2 52
1567 50 ret = index_insert(index, &entry, 1, false, false, true);
1568  
1569 /* If we were given a directory, let's see if it's a submodule */
1570 52 ▶ if (ret < 0 && ret != GIT_EDIRECTORY)
Branch: 2 branches
BranchTrueFalseTotal
if (ret < 0 && ret != GIT_EDIRECTORY) 2 50 52
if (ret < 0 && ret != GIT_EDIRECTORY) 2 0 2
1571 2 return ret;
1572  
1573 50 ▶ if (ret == GIT_EDIRECTORY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == GIT_EDIRECTORY) { 0 50 50
1574 git_submodule *sm;
1575 git_error_state err;
1576  
1577 0 git_error_state_capture(&err, ret);
1578  
1579 0 ret = git_submodule_lookup(&sm, INDEX_OWNER(index), path);
1580 0 ▶ if (ret == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == GIT_ENOTFOUND) 0 0 0
1581 0 return git_error_state_restore(&err);
1582  
1583 0 git_error_state_free(&err);
1584  
1585 /*
1586 * EEXISTS means that there is a repository at that path, but it's not known
1587 * as a submodule. We add its HEAD as an entry and don't register it.
1588 */
1589 0 ▶ if (ret == GIT_EEXISTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == GIT_EEXISTS) { 0 0 0
1590 0 ▶ if ((ret = add_repo_as_submodule(&entry, index, path)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = add_repo_as_submodule(&entry, index, path)) < 0) 0 0 0
1591 0 return ret;
1592  
1593 0 ▶ if ((ret = index_insert(index, &entry, 1, false, false, true)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = index_insert(index, &entry, 1, false, false, true)) < 0) 0 0 0
1594 0 return ret;
1595 0 ▶ } else if (ret < 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ret < 0) { 0 0 0
1596 0 return ret;
1597 } else {
1598 0 ret = git_submodule_add_to_index(sm, false);
1599 0 git_submodule_free(sm);
1600 0 return ret;
1601 }
1602 }
1603  
1604 /* Adding implies conflict was resolved, move conflict entries to REUC */
1605 50 ▶ if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND)
Branch: 2 branches
BranchTrueFalseTotal
if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND) 48 2 50
if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND) 0 48 48
1606 0 return ret;
1607  
1608 50 git_tree_cache_invalidate_path(index->tree, entry->path);
1609 50 return 0;
1610 }
1611  
1612 6 int git_index_remove_bypath(git_index *index, const char *path)
1613 {
1614 int ret;
1615  
1616 6 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 6 6
1617 6 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 6 6
1618  
1619 6 ▶ if (((ret = git_index_remove(index, path, 0)) < 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if (((ret = git_index_remove(index, path, 0)) < 0 && 0 6 6
if (((ret = git_index_remove(index, path, 0)) < 0 && 0 0 0
1620 6 ▶ ret != GIT_ENOTFOUND) ||
Branch: 1 branch
BranchTrueFalseTotal
ret != GIT_ENOTFOUND) || 6 0 6
1621 12 ▶ ((ret = index_conflict_to_reuc(index, path)) < 0 &&
Branch: 1 branch
BranchTrueFalseTotal
((ret = index_conflict_to_reuc(index, path)) < 0 && 0 6 6
1622 ret != GIT_ENOTFOUND))
1623 0 return ret;
1624  
1625 6 ▶ if (ret == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == GIT_ENOTFOUND) 6 0 6
1626 6 git_error_clear();
1627  
1628 6 return 0;
1629 }
1630  
1631 28 int git_index__fill(git_index *index, const git_vector *source_entries)
1632 {
1633 28 const git_index_entry *source_entry = NULL;
1634 28 int error = 0;
1635 size_t i;
1636  
1637 28 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 28 28
1638  
1639 28 ▶ if (!source_entries->length)
Branch: 1 branch
BranchTrueFalseTotal
if (!source_entries->length) 3 25 28
1640 3 return 0;
1641  
1642 50 if (git_vector_size_hint(&index->entries, source_entries->length) < 0 ||
1643 25 index_map_resize(index->entries_map, (size_t)(source_entries->length * 1.3),
1644 25 index->ignore_case) < 0)
1645 0 return -1;
1646  
1647 88 ▶ git_vector_foreach(source_entries, i, source_entry) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(source_entries, i, source_entry) { 63 25 88
1648 63 git_index_entry *entry = NULL;
1649  
1650 63 ▶ if ((error = index_entry_dup(&entry, index, source_entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_entry_dup(&entry, index, source_entry)) < 0) 0 63 63
1651 0 break;
1652  
1653 63 index_entry_adjust_namemask(entry, ((struct entry_internal *)entry)->pathlen);
1654 63 entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE;
1655 63 entry->mode = git_index__create_mode(entry->mode);
1656  
1657 63 ▶ if ((error = git_vector_insert(&index->entries, entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_insert(&index->entries, entry)) < 0) 0 63 63
1658 0 break;
1659  
1660 63 ▶ if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0) 0 63 63
1661 0 break;
1662  
1663 63 index->dirty = 1;
1664 }
1665  
1666 25 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 25 0 25
1667 25 git_vector_sort(&index->entries);
1668  
1669 25 return error;
1670 }
1671  
1672  
1673 52 int git_index_add(git_index *index, const git_index_entry *source_entry)
1674 {
1675 52 git_index_entry *entry = NULL;
1676 int ret;
1677  
1678 52 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 52 52
1679 52 ▶ GIT_ASSERT_ARG(source_entry && source_entry->path);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ASSERT_ARG(source_entry && source_entry->path); 52 0 52
GIT_ASSERT_ARG(source_entry && source_entry->path); 0 52 52
1680  
1681 52 ▶ if (!valid_filemode(source_entry->mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_filemode(source_entry->mode)) { 0 52 52
1682 0 git_error_set(GIT_ERROR_INDEX, "invalid entry mode");
1683 0 return -1;
1684 }
1685  
1686 104 if ((ret = index_entry_dup(&entry, index, source_entry)) < 0 ||
1687 52 (ret = index_insert(index, &entry, 1, true, true, false)) < 0)
1688 0 return ret;
1689  
1690 52 git_tree_cache_invalidate_path(index->tree, entry->path);
1691 52 return 0;
1692 }
1693  
1694 50 int git_index_remove(git_index *index, const char *path, int stage)
1695 {
1696 int error;
1697 size_t position;
1698 50 git_index_entry remove_key = {{ 0 }};
1699  
1700 50 remove_key.path = path;
1701 50 GIT_INDEX_ENTRY_STAGE_SET(&remove_key, stage);
1702  
1703 50 index_map_delete(index->entries_map, &remove_key, index->ignore_case);
1704  
1705 50 ▶ if (index_find(&position, index, path, 0, stage) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index_find(&position, index, path, 0, stage) < 0) { 35 15 50
1706 35 git_error_set(
1707 GIT_ERROR_INDEX, "index does not contain %s at stage %d", path, stage);
1708 35 error = GIT_ENOTFOUND;
1709 } else {
1710 15 error = index_remove_entry(index, position);
1711 }
1712  
1713 50 return error;
1714 }
1715  
1716 1 int git_index_remove_directory(git_index *index, const char *dir, int stage)
1717 {
1718 1 git_str pfx = GIT_STR_INIT;
1719 1 int error = 0;
1720 size_t pos;
1721 git_index_entry *entry;
1722  
1723 2 if (!(error = git_str_sets(&pfx, dir)) &&
1724 1 !(error = git_fs_path_to_dir(&pfx)))
1725 1 index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY);
1726  
1727 1 ▶ while (!error) {
Branch: 1 branch
BranchTrueFalseTotal
while (!error) { 1 0 1
1728 1 entry = git_vector_get(&index->entries, pos);
1729 1 ▶ if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0) 1 0 1
if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0) 0 1 1
1730 break;
1731  
1732 0 ▶ if (GIT_INDEX_ENTRY_STAGE(entry) != stage) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_INDEX_ENTRY_STAGE(entry) != stage) { 0 0 0
1733 0 ++pos;
1734 0 continue;
1735 }
1736  
1737 0 error = index_remove_entry(index, pos);
1738  
1739 /* removed entry at 'pos' so we don't need to increment */
1740 }
1741  
1742 1 git_str_dispose(&pfx);
1743  
1744 1 return error;
1745 }
1746  
1747 0 int git_index_find_prefix(size_t *at_pos, git_index *index, const char *prefix)
1748 {
1749 0 int error = 0;
1750 size_t pos;
1751 const git_index_entry *entry;
1752  
1753 0 index_find(&pos, index, prefix, strlen(prefix), GIT_INDEX_STAGE_ANY);
1754 0 entry = git_vector_get(&index->entries, pos);
1755 0 ▶ if (!entry || git__prefixcmp(entry->path, prefix) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!entry || git__prefixcmp(entry->path, prefix) != 0) 0 0 0
if (!entry || git__prefixcmp(entry->path, prefix) != 0) 0 0 0
1756 0 error = GIT_ENOTFOUND;
1757  
1758 0 ▶ if (!error && at_pos)
Branch: 2 branches
BranchTrueFalseTotal
if (!error && at_pos) 0 0 0
if (!error && at_pos) 0 0 0
1759 0 *at_pos = pos;
1760  
1761 0 return error;
1762 }
1763  
1764 1368 int git_index__find_pos(
1765 size_t *out, git_index *index, const char *path, size_t path_len, int stage)
1766 {
1767 1368 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 1368 1368
1768 1368 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 1368 1368
1769 1368 return index_find(out, index, path, path_len, stage);
1770 }
1771  
1772 71 int git_index_find(size_t *at_pos, git_index *index, const char *path)
1773 {
1774 size_t pos;
1775  
1776 71 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 71 71
1777 71 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 71 71
1778  
1779 71 ▶ if (git_vector_bsearch2(
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_bsearch2( 7 64 71
1780 &pos, &index->entries, index->entries_search_path, path) < 0) {
1781 7 git_error_set(GIT_ERROR_INDEX, "index does not contain %s", path);
1782 7 return GIT_ENOTFOUND;
1783 }
1784  
1785 /* Since our binary search only looked at path, we may be in the
1786 * middle of a list of stages.
1787 */
1788 77 ▶ for (; pos > 0; --pos) {
Branch: 1 branch
BranchTrueFalseTotal
for (; pos > 0; --pos) { 28 49 77
1789 28 const git_index_entry *prev = git_vector_get(&index->entries, pos - 1);
1790  
1791 28 ▶ if (index->entries_cmp_path(prev->path, path) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index->entries_cmp_path(prev->path, path) != 0) 15 13 28
1792 15 break;
1793 }
1794  
1795 64 ▶ if (at_pos)
Branch: 1 branch
BranchTrueFalseTotal
if (at_pos) 64 0 64
1796 64 *at_pos = pos;
1797  
1798 64 return 0;
1799 }
1800  
1801 6 int git_index_conflict_add(git_index *index,
1802 const git_index_entry *ancestor_entry,
1803 const git_index_entry *our_entry,
1804 const git_index_entry *their_entry)
1805 {
1806 6 git_index_entry *entries[3] = { 0 };
1807 unsigned short i;
1808 6 int ret = 0;
1809  
1810 6 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 6 6
1811  
1812 12 if ((ancestor_entry &&
1813 12 ▶ (ret = index_entry_dup(&entries[0], index, ancestor_entry)) < 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(ret = index_entry_dup(&entries[0], index, ancestor_entry)) < 0) || 5 1 6
1814 5 ▶ (our_entry &&
Branch: 1 branch
BranchTrueFalseTotal
(our_entry && 5 0 5
1815 11 ▶ (ret = index_entry_dup(&entries[1], index, our_entry)) < 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(ret = index_entry_dup(&entries[1], index, our_entry)) < 0) || 6 0 6
1816 6 ▶ (their_entry &&
Branch: 1 branch
BranchTrueFalseTotal
(their_entry && 0 6 6
1817 6 (ret = index_entry_dup(&entries[2], index, their_entry)) < 0))
1818 0 goto on_error;
1819  
1820 /* Validate entries */
1821 24 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 18 6 24
1822 18 ▶ if (entries[i] && !valid_filemode(entries[i]->mode)) {
Branch: 2 branches
BranchTrueFalseTotal
if (entries[i] && !valid_filemode(entries[i]->mode)) { 17 1 18
if (entries[i] && !valid_filemode(entries[i]->mode)) { 0 17 17
1823 0 git_error_set(GIT_ERROR_INDEX, "invalid filemode for stage %d entry",
1824 i + 1);
1825 0 ret = -1;
1826 0 goto on_error;
1827 }
1828 }
1829  
1830 /* Remove existing index entries for each path */
1831 24 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 18 6 24
1832 18 ▶ if (entries[i] == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (entries[i] == NULL) 1 17 18
1833 1 continue;
1834  
1835 17 ▶ if ((ret = git_index_remove(index, entries[i]->path, 0)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = git_index_remove(index, entries[i]->path, 0)) != 0) { 17 0 17
1836 17 ▶ if (ret != GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (ret != GIT_ENOTFOUND) 0 17 17
1837 0 goto on_error;
1838  
1839 17 git_error_clear();
1840 17 ret = 0;
1841 }
1842 }
1843  
1844 /* Add the conflict entries */
1845 24 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 18 6 24
1846 18 ▶ if (entries[i] == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (entries[i] == NULL) 1 17 18
1847 1 continue;
1848  
1849 /* Make sure stage is correct */
1850 17 GIT_INDEX_ENTRY_STAGE_SET(entries[i], i + 1);
1851  
1852 17 ▶ if ((ret = index_insert(index, &entries[i], 1, true, true, false)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = index_insert(index, &entries[i], 1, true, true, false)) < 0) 0 17 17
1853 0 goto on_error;
1854  
1855 17 entries[i] = NULL; /* don't free if later entry fails */
1856 }
1857  
1858 6 return 0;
1859  
1860 0 on_error:
1861 0 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 0 0 0
1862 0 ▶ if (entries[i] != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (entries[i] != NULL) 0 0 0
1863 0 index_entry_free(entries[i]);
1864 }
1865  
1866 0 return ret;
1867 }
1868  
1869 62 static int index_conflict__get_byindex(
1870 const git_index_entry **ancestor_out,
1871 const git_index_entry **our_out,
1872 const git_index_entry **their_out,
1873 git_index *index,
1874 size_t n)
1875 {
1876 const git_index_entry *conflict_entry;
1877 62 const char *path = NULL;
1878 size_t count;
1879 62 int stage, len = 0;
1880  
1881 62 ▶ GIT_ASSERT_ARG(ancestor_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(ancestor_out); 0 62 62
1882 62 ▶ GIT_ASSERT_ARG(our_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(our_out); 0 62 62
1883 62 ▶ GIT_ASSERT_ARG(their_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(their_out); 0 62 62
1884 62 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 62 62
1885  
1886 62 *ancestor_out = NULL;
1887 62 *our_out = NULL;
1888 62 *their_out = NULL;
1889  
1890 140 ▶ for (count = git_index_entrycount(index); n < count; ++n) {
Branch: 1 branch
BranchTrueFalseTotal
for (count = git_index_entrycount(index); n < count; ++n) { 108 32 140
1891 108 conflict_entry = git_vector_get(&index->entries, n);
1892  
1893 108 ▶ if (path && index->entries_cmp_path(conflict_entry->path, path) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (path && index->entries_cmp_path(conflict_entry->path, path) != 0) 46 62 108
if (path && index->entries_cmp_path(conflict_entry->path, path) != 0) 30 16 46
1894 30 break;
1895  
1896 78 stage = GIT_INDEX_ENTRY_STAGE(conflict_entry);
1897 78 path = conflict_entry->path;
1898  
1899 78 switch (stage) {
1900 7 case 3:
1901 7 *their_out = conflict_entry;
1902 7 len++;
1903 7 break;
1904 7 case 2:
1905 7 *our_out = conflict_entry;
1906 7 len++;
1907 7 break;
1908 7 case 1:
1909 7 *ancestor_out = conflict_entry;
1910 7 len++;
1911 7 break;
1912 57 default:
1913 57 break;
1914 };
1915 }
1916  
1917 62 return len;
1918 }
1919  
1920 64 int git_index_conflict_get(
1921 const git_index_entry **ancestor_out,
1922 const git_index_entry **our_out,
1923 const git_index_entry **their_out,
1924 git_index *index,
1925 const char *path)
1926 {
1927 size_t pos;
1928 64 int len = 0;
1929  
1930 64 ▶ GIT_ASSERT_ARG(ancestor_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(ancestor_out); 0 64 64
1931 64 ▶ GIT_ASSERT_ARG(our_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(our_out); 0 64 64
1932 64 ▶ GIT_ASSERT_ARG(their_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(their_out); 0 64 64
1933 64 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 64 64
1934 64 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 64 64
1935  
1936 64 *ancestor_out = NULL;
1937 64 *our_out = NULL;
1938 64 *their_out = NULL;
1939  
1940 64 ▶ if (git_index_find(&pos, index, path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_index_find(&pos, index, path) < 0) 6 58 64
1941 6 return GIT_ENOTFOUND;
1942  
1943 58 ▶ if ((len = index_conflict__get_byindex(
Branch: 1 branch
BranchTrueFalseTotal
if ((len = index_conflict__get_byindex( 0 58 58
1944 ancestor_out, our_out, their_out, index, pos)) < 0)
1945 0 return len;
1946 58 ▶ else if (len == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (len == 0) 55 3 58
1947 55 return GIT_ENOTFOUND;
1948  
1949 3 return 0;
1950 }
1951  
1952 6 static int index_conflict_remove(git_index *index, const char *path)
1953 {
1954 6 size_t pos = 0;
1955 git_index_entry *conflict_entry;
1956 6 int error = 0;
1957  
1958 6 ▶ if (path != NULL && git_index_find(&pos, index, path) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (path != NULL && git_index_find(&pos, index, path) < 0) 5 1 6
if (path != NULL && git_index_find(&pos, index, path) < 0) 0 5 5
1959 0 return GIT_ENOTFOUND;
1960  
1961 21 ▶ while ((conflict_entry = git_vector_get(&index->entries, pos)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((conflict_entry = git_vector_get(&index->entries, pos)) != NULL) { 16 5 21
1962  
1963 32 if (path != NULL &&
1964 16 index->entries_cmp_path(conflict_entry->path, path) != 0)
1965 1 break;
1966  
1967 15 ▶ if (GIT_INDEX_ENTRY_STAGE(conflict_entry) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_INDEX_ENTRY_STAGE(conflict_entry) == 0) { 3 12 15
1968 3 pos++;
1969 3 continue;
1970 }
1971  
1972 12 ▶ if ((error = index_remove_entry(index, pos)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_remove_entry(index, pos)) < 0) 0 12 12
1973 0 break;
1974 }
1975  
1976 6 return error;
1977 }
1978  
1979 5 int git_index_conflict_remove(git_index *index, const char *path)
1980 {
1981 5 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 5 5
1982 5 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 5 5
1983 5 return index_conflict_remove(index, path);
1984 }
1985  
1986 1 int git_index_conflict_cleanup(git_index *index)
1987 {
1988 1 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 1 1
1989 1 return index_conflict_remove(index, NULL);
1990 }
1991  
1992 123 int git_index_has_conflicts(const git_index *index)
1993 {
1994 size_t i;
1995 git_index_entry *entry;
1996  
1997 123 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 123 123
1998  
1999 406 ▶ git_vector_foreach(&index->entries, i, entry) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&index->entries, i, entry) { 291 115 406
2000 291 ▶ if (GIT_INDEX_ENTRY_STAGE(entry) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_INDEX_ENTRY_STAGE(entry) > 0) 8 283 291
2001 8 return 1;
2002 }
2003  
2004 115 return 0;
2005 }
2006  
2007 0 int git_index_iterator_new(
2008 git_index_iterator **iterator_out,
2009 git_index *index)
2010 {
2011 git_index_iterator *it;
2012 int error;
2013  
2014 0 ▶ GIT_ASSERT_ARG(iterator_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(iterator_out); 0 0 0
2015 0 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 0 0
2016  
2017 0 it = git__calloc(1, sizeof(git_index_iterator));
2018 0 ▶ GIT_ERROR_CHECK_ALLOC(it);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(it); 0 0 0
2019  
2020 0 ▶ if ((error = git_index_snapshot_new(&it->snap, index)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_index_snapshot_new(&it->snap, index)) < 0) { 0 0 0
2021 0 git__free(it);
2022 0 return error;
2023 }
2024  
2025 0 it->index = index;
2026  
2027 0 *iterator_out = it;
2028 0 return 0;
2029 }
2030  
2031 0 int git_index_iterator_next(
2032 const git_index_entry **out,
2033 git_index_iterator *it)
2034 {
2035 0 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 0 0
2036 0 ▶ GIT_ASSERT_ARG(it);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(it); 0 0 0
2037  
2038 0 ▶ if (it->cur >= git_vector_length(&it->snap))
Branch: 1 branch
BranchTrueFalseTotal
if (it->cur >= git_vector_length(&it->snap)) 0 0 0
2039 0 return GIT_ITEROVER;
2040  
2041 0 *out = (git_index_entry *)git_vector_get(&it->snap, it->cur++);
2042 0 return 0;
2043 }
2044  
2045 0 void git_index_iterator_free(git_index_iterator *it)
2046 {
2047 0 ▶ if (it == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (it == NULL) 0 0 0
2048 0 return;
2049  
2050 0 git_index_snapshot_release(&it->snap, it->index);
2051 0 git__free(it);
2052 }
2053  
2054 69 int git_index_conflict_iterator_new(
2055 git_index_conflict_iterator **iterator_out,
2056 git_index *index)
2057 {
2058 69 git_index_conflict_iterator *it = NULL;
2059  
2060 69 ▶ GIT_ASSERT_ARG(iterator_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(iterator_out); 0 69 69
2061 69 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 69 69
2062  
2063 69 it = git__calloc(1, sizeof(git_index_conflict_iterator));
2064 69 ▶ GIT_ERROR_CHECK_ALLOC(it);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(it); 0 69 69
2065  
2066 69 it->index = index;
2067  
2068 69 *iterator_out = it;
2069 69 return 0;
2070 }
2071  
2072 73 int git_index_conflict_next(
2073 const git_index_entry **ancestor_out,
2074 const git_index_entry **our_out,
2075 const git_index_entry **their_out,
2076 git_index_conflict_iterator *iterator)
2077 {
2078 const git_index_entry *entry;
2079 int len;
2080  
2081 73 ▶ GIT_ASSERT_ARG(ancestor_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(ancestor_out); 0 73 73
2082 73 ▶ GIT_ASSERT_ARG(our_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(our_out); 0 73 73
2083 73 ▶ GIT_ASSERT_ARG(their_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(their_out); 0 73 73
2084 73 ▶ GIT_ASSERT_ARG(iterator);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(iterator); 0 73 73
2085  
2086 73 *ancestor_out = NULL;
2087 73 *our_out = NULL;
2088 73 *their_out = NULL;
2089  
2090 213 ▶ while (iterator->cur < iterator->index->entries.length) {
Branch: 1 branch
BranchTrueFalseTotal
while (iterator->cur < iterator->index->entries.length) { 144 69 213
2091 144 entry = git_index_get_byindex(iterator->index, iterator->cur);
2092  
2093 144 ▶ if (git_index_entry_is_conflict(entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_index_entry_is_conflict(entry)) { 4 140 144
2094 4 ▶ if ((len = index_conflict__get_byindex(
Branch: 1 branch
BranchTrueFalseTotal
if ((len = index_conflict__get_byindex( 0 4 4
2095 ancestor_out,
2096 our_out,
2097 their_out,
2098 iterator->index,
2099 iterator->cur)) < 0)
2100 0 return len;
2101  
2102 4 iterator->cur += len;
2103 4 return 0;
2104 }
2105  
2106 140 iterator->cur++;
2107 }
2108  
2109 69 return GIT_ITEROVER;
2110 }
2111  
2112 69 void git_index_conflict_iterator_free(git_index_conflict_iterator *iterator)
2113 {
2114 69 ▶ if (iterator == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (iterator == NULL) 0 69 69
2115 0 return;
2116  
2117 69 git__free(iterator);
2118 }
2119  
2120 17 size_t git_index_name_entrycount(git_index *index)
2121 {
2122 17 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 17 17
2123 17 return index->names.length;
2124 }
2125  
2126 0 const git_index_name_entry *git_index_name_get_byindex(
2127 git_index *index, size_t n)
2128 {
2129 0 ▶ GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); 0 0 0
2130  
2131 0 git_vector_sort(&index->names);
2132 0 return git_vector_get(&index->names, n);
2133 }
2134  
2135 0 static void index_name_entry_free(git_index_name_entry *ne)
2136 {
2137 0 ▶ if (!ne)
Branch: 1 branch
BranchTrueFalseTotal
if (!ne) 0 0 0
2138 0 return;
2139 0 git__free(ne->ancestor);
2140 0 git__free(ne->ours);
2141 0 git__free(ne->theirs);
2142 0 git__free(ne);
2143 }
2144  
2145 0 int git_index_name_add(git_index *index,
2146 const char *ancestor, const char *ours, const char *theirs)
2147 {
2148 git_index_name_entry *conflict_name;
2149  
2150 0 ▶ GIT_ASSERT_ARG((ancestor && ours) || (ancestor && theirs) || (ours && theirs));
Branch: 6 branches
BranchTrueFalseTotal
GIT_ASSERT_ARG((ancestor && ours) || (ancestor && theirs) || (ours && theirs)); 0 0 0
GIT_ASSERT_ARG((ancestor && ours) || (ancestor && theirs) || (ours && theirs)); 0 0 0
GIT_ASSERT_ARG((ancestor && ours) || (ancestor && theirs) || (ours && theirs)); 0 0 0
GIT_ASSERT_ARG((ancestor && ours) || (ancestor && theirs) || (ours && theirs)); 0 0 0
GIT_ASSERT_ARG((ancestor && ours) || (ancestor && theirs) || (ours && theirs)); 0 0 0
GIT_ASSERT_ARG((ancestor && ours) || (ancestor && theirs) || (ours && theirs)); 0 0 0
2151  
2152 0 conflict_name = git__calloc(1, sizeof(git_index_name_entry));
2153 0 ▶ GIT_ERROR_CHECK_ALLOC(conflict_name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(conflict_name); 0 0 0
2154  
2155 0 ▶ if ((ancestor && !(conflict_name->ancestor = git__strdup(ancestor))) ||
Branch: 3 branches
BranchTrueFalseTotal
if ((ancestor && !(conflict_name->ancestor = git__strdup(ancestor))) || 0 0 0
if ((ancestor && !(conflict_name->ancestor = git__strdup(ancestor))) || 0 0 0
if ((ancestor && !(conflict_name->ancestor = git__strdup(ancestor))) || 0 0 0
2156 0 ▶ (ours && !(conflict_name->ours = git__strdup(ours))) ||
Branch: 2 branches
BranchTrueFalseTotal
(ours && !(conflict_name->ours = git__strdup(ours))) || 0 0 0
(ours && !(conflict_name->ours = git__strdup(ours))) || 0 0 0
2157 0 (theirs && !(conflict_name->theirs = git__strdup(theirs))) ||
2158 0 git_vector_insert(&index->names, conflict_name) < 0)
2159 {
2160 0 index_name_entry_free(conflict_name);
2161 0 return -1;
2162 }
2163  
2164 0 index->dirty = 1;
2165 0 return 0;
2166 }
2167  
2168 123 int git_index_name_clear(git_index *index)
2169 {
2170 size_t i;
2171 git_index_name_entry *conflict_name;
2172  
2173 123 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 123 123
2174  
2175 123 ▶ git_vector_foreach(&index->names, i, conflict_name)
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&index->names, i, conflict_name) 0 123 123
2176 0 index_name_entry_free(conflict_name);
2177  
2178 123 git_vector_clear(&index->names);
2179  
2180 123 index->dirty = 1;
2181  
2182 123 return 0;
2183 }
2184  
2185 0 size_t git_index_reuc_entrycount(git_index *index)
2186 {
2187 0 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 0 0
2188 0 return index->reuc.length;
2189 }
2190  
2191 4 static int index_reuc_on_dup(void **old, void *new)
2192 {
2193 4 index_entry_reuc_free(*old);
2194 4 *old = new;
2195 4 return GIT_EEXISTS;
2196 }
2197  
2198 10 static int index_reuc_insert(
2199 git_index *index,
2200 git_index_reuc_entry *reuc)
2201 {
2202 int res;
2203  
2204 10 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 10 10
2205 10 ▶ GIT_ASSERT_ARG(reuc && reuc->path != NULL);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ASSERT_ARG(reuc && reuc->path != NULL); 10 0 10
GIT_ASSERT_ARG(reuc && reuc->path != NULL); 0 10 10
2206 10 ▶ GIT_ASSERT(git_vector_is_sorted(&index->reuc));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(git_vector_is_sorted(&index->reuc)); 0 10 10
2207  
2208 10 res = git_vector_insert_sorted(&index->reuc, reuc, &index_reuc_on_dup);
2209 10 index->dirty = 1;
2210  
2211 10 ▶ return res == GIT_EEXISTS ? 0 : res;
Branch: 1 branch
BranchTrueFalseTotal
return res == GIT_EEXISTS ? 0 : res; 6 4 10
2212 }
2213  
2214 10 int git_index_reuc_add(git_index *index, const char *path,
2215 int ancestor_mode, const git_oid *ancestor_oid,
2216 int our_mode, const git_oid *our_oid,
2217 int their_mode, const git_oid *their_oid)
2218 {
2219 10 git_index_reuc_entry *reuc = NULL;
2220 10 int error = 0;
2221  
2222 10 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 10 10
2223 10 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 10 10
2224  
2225 10 ▶ if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode, 10 0 10
2226 10 ▶ ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 || 0 10 10
2227 10 (error = index_reuc_insert(index, reuc)) < 0)
2228 0 index_entry_reuc_free(reuc);
2229  
2230 10 return error;
2231 }
2232  
2233 4 int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path)
2234 {
2235 4 return git_vector_bsearch2(at_pos, &index->reuc, index->reuc_search, path);
2236 }
2237  
2238 6 const git_index_reuc_entry *git_index_reuc_get_bypath(
2239 git_index *index, const char *path)
2240 {
2241 size_t pos;
2242  
2243 6 ▶ GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); 0 6 6
2244 6 ▶ GIT_ASSERT_ARG_WITH_RETVAL(path, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(path, NULL); 0 6 6
2245  
2246 6 ▶ if (!index->reuc.length)
Branch: 1 branch
BranchTrueFalseTotal
if (!index->reuc.length) 2 4 6
2247 2 return NULL;
2248  
2249 4 ▶ GIT_ASSERT_WITH_RETVAL(git_vector_is_sorted(&index->reuc), NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_WITH_RETVAL(git_vector_is_sorted(&index->reuc), NULL); 0 4 4
2250  
2251 4 ▶ if (git_index_reuc_find(&pos, index, path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_index_reuc_find(&pos, index, path) < 0) 0 4 4
2252 0 return NULL;
2253  
2254 4 return git_vector_get(&index->reuc, pos);
2255 }
2256  
2257 0 const git_index_reuc_entry *git_index_reuc_get_byindex(
2258 git_index *index, size_t n)
2259 {
2260 0 ▶ GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(index, NULL); 0 0 0
2261 0 ▶ GIT_ASSERT_WITH_RETVAL(git_vector_is_sorted(&index->reuc), NULL);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_WITH_RETVAL(git_vector_is_sorted(&index->reuc), NULL); 0 0 0
2262  
2263 0 return git_vector_get(&index->reuc, n);
2264 }
2265  
2266 0 int git_index_reuc_remove(git_index *index, size_t position)
2267 {
2268 int error;
2269 git_index_reuc_entry *reuc;
2270  
2271 0 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 0 0
2272 0 ▶ GIT_ASSERT(git_vector_is_sorted(&index->reuc));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(git_vector_is_sorted(&index->reuc)); 0 0 0
2273  
2274 0 reuc = git_vector_get(&index->reuc, position);
2275 0 error = git_vector_remove(&index->reuc, position);
2276  
2277 0 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 0 0 0
2278 0 index_entry_reuc_free(reuc);
2279  
2280 0 index->dirty = 1;
2281 0 return error;
2282 }
2283  
2284 123 int git_index_reuc_clear(git_index *index)
2285 {
2286 size_t i;
2287  
2288 123 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 123 123
2289  
2290 129 ▶ for (i = 0; i < index->reuc.length; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < index->reuc.length; ++i) 6 123 129
2291 6 index_entry_reuc_free(git_atomic_swap(index->reuc.contents[i], NULL));
2292  
2293 123 git_vector_clear(&index->reuc);
2294  
2295 123 index->dirty = 1;
2296  
2297 123 return 0;
2298 }
2299  
2300 0 static int index_error_invalid(const char *message)
2301 {
2302 0 git_error_set(GIT_ERROR_INDEX, "invalid data in index - %s", message);
2303 0 return -1;
2304 }
2305  
2306 0 static int read_reuc(git_index *index, const char *buffer, size_t size)
2307 {
2308 const char *endptr;
2309 size_t len;
2310 int i;
2311  
2312 /* If called multiple times, the vector might already be initialized */
2313 0 if (index->reuc._alloc_size == 0 &&
2314 0 git_vector_init(&index->reuc, 16, reuc_cmp) < 0)
2315 0 return -1;
2316  
2317 0 ▶ while (size) {
Branch: 1 branch
BranchTrueFalseTotal
while (size) { 0 0 0
2318 git_index_reuc_entry *lost;
2319  
2320 0 len = p_strnlen(buffer, size) + 1;
2321 0 ▶ if (size <= len)
Branch: 1 branch
BranchTrueFalseTotal
if (size <= len) 0 0 0
2322 0 return index_error_invalid("reading reuc entries");
2323  
2324 0 lost = reuc_entry_alloc(buffer);
2325 0 ▶ GIT_ERROR_CHECK_ALLOC(lost);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(lost); 0 0 0
2326  
2327 0 size -= len;
2328 0 buffer += len;
2329  
2330 /* read 3 ASCII octal numbers for stage entries */
2331 0 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 0 0 0
2332 int64_t tmp;
2333  
2334 0 ▶ if (git__strntol64(&tmp, buffer, size, &endptr, 8) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (git__strntol64(&tmp, buffer, size, &endptr, 8) < 0 || 0 0 0
2335 0 ▶ !endptr || endptr == buffer || *endptr ||
Branch: 3 branches
BranchTrueFalseTotal
!endptr || endptr == buffer || *endptr || 0 0 0
!endptr || endptr == buffer || *endptr || 0 0 0
!endptr || endptr == buffer || *endptr || 0 0 0
2336 0 ▶ tmp < 0 || tmp > UINT32_MAX) {
Branch: 2 branches
BranchTrueFalseTotal
tmp < 0 || tmp > UINT32_MAX) { 0 0 0
tmp < 0 || tmp > UINT32_MAX) { 0 0 0
2337 0 index_entry_reuc_free(lost);
2338 0 return index_error_invalid("reading reuc entry stage");
2339 }
2340  
2341 0 lost->mode[i] = (uint32_t)tmp;
2342  
2343 0 len = (endptr + 1) - buffer;
2344 0 ▶ if (size <= len) {
Branch: 1 branch
BranchTrueFalseTotal
if (size <= len) { 0 0 0
2345 0 index_entry_reuc_free(lost);
2346 0 return index_error_invalid("reading reuc entry stage");
2347 }
2348  
2349 0 size -= len;
2350 0 buffer += len;
2351 }
2352  
2353 /* read up to 3 OIDs for stage entries */
2354 0 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 0 0 0
2355 0 ▶ if (!lost->mode[i])
Branch: 1 branch
BranchTrueFalseTotal
if (!lost->mode[i]) 0 0 0
2356 0 continue;
2357 0 ▶ if (size < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if (size < 20) { 0 0 0
2358 0 index_entry_reuc_free(lost);
2359 0 return index_error_invalid("reading reuc entry oid");
2360 }
2361  
2362 0 git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer);
2363 0 size -= 20;
2364 0 buffer += 20;
2365 }
2366  
2367 /* entry was read successfully - insert into reuc vector */
2368 0 ▶ if (git_vector_insert(&index->reuc, lost) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_insert(&index->reuc, lost) < 0) 0 0 0
2369 0 return -1;
2370 }
2371  
2372 /* entries are guaranteed to be sorted on-disk */
2373 0 git_vector_set_sorted(&index->reuc, true);
2374  
2375 0 return 0;
2376 }
2377  
2378  
2379 0 static int read_conflict_names(git_index *index, const char *buffer, size_t size)
2380 {
2381 size_t len;
2382  
2383 /* This gets called multiple times, the vector might already be initialized */
2384 0 if (index->names._alloc_size == 0 &&
2385 0 git_vector_init(&index->names, 16, conflict_name_cmp) < 0)
2386 0 return -1;
2387  
2388 #define read_conflict_name(ptr) \
2389 len = p_strnlen(buffer, size) + 1; \
2390 if (size < len) { \
2391 index_error_invalid("reading conflict name entries"); \
2392 goto out_err; \
2393 } \
2394 if (len == 1) \
2395 ptr = NULL; \
2396 else { \
2397 ptr = git__malloc(len); \
2398 GIT_ERROR_CHECK_ALLOC(ptr); \
2399 memcpy(ptr, buffer, len); \
2400 } \
2401 \
2402 buffer += len; \
2403 size -= len;
2404  
2405 0 ▶ while (size) {
Branch: 1 branch
BranchTrueFalseTotal
while (size) { 0 0 0
2406 0 git_index_name_entry *conflict_name = git__calloc(1, sizeof(git_index_name_entry));
2407 0 ▶ GIT_ERROR_CHECK_ALLOC(conflict_name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(conflict_name); 0 0 0
2408  
2409 0 ▶ read_conflict_name(conflict_name->ancestor);
Branch: 3 branches
BranchTrueFalseTotal
read_conflict_name(conflict_name->ancestor); 0 0 0
read_conflict_name(conflict_name->ancestor); 0 0 0
read_conflict_name(conflict_name->ancestor); 0 0 0
2410 0 ▶ read_conflict_name(conflict_name->ours);
Branch: 3 branches
BranchTrueFalseTotal
read_conflict_name(conflict_name->ours); 0 0 0
read_conflict_name(conflict_name->ours); 0 0 0
read_conflict_name(conflict_name->ours); 0 0 0
2411 0 ▶ read_conflict_name(conflict_name->theirs);
Branch: 3 branches
BranchTrueFalseTotal
read_conflict_name(conflict_name->theirs); 0 0 0
read_conflict_name(conflict_name->theirs); 0 0 0
read_conflict_name(conflict_name->theirs); 0 0 0
2412  
2413 0 ▶ if (git_vector_insert(&index->names, conflict_name) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_insert(&index->names, conflict_name) < 0) 0 0 0
2414 0 goto out_err;
2415  
2416 0 continue;
2417  
2418 0 out_err:
2419 0 git__free(conflict_name->ancestor);
2420 0 git__free(conflict_name->ours);
2421 0 git__free(conflict_name->theirs);
2422 0 git__free(conflict_name);
2423 0 return -1;
2424 }
2425  
2426 #undef read_conflict_name
2427  
2428 /* entries are guaranteed to be sorted on-disk */
2429 0 git_vector_set_sorted(&index->names, true);
2430  
2431 0 return 0;
2432 }
2433  
2434 232 static size_t index_entry_size(size_t path_len, size_t varint_len, uint32_t flags)
2435 {
2436 232 ▶ if (varint_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (varint_len) { 0 232 232
2437 0 ▶ if (flags & GIT_INDEX_ENTRY_EXTENDED)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_INDEX_ENTRY_EXTENDED) 0 0 0
2438 0 return offsetof(struct entry_long, path) + path_len + 1 + varint_len;
2439 else
2440 0 return offsetof(struct entry_short, path) + path_len + 1 + varint_len;
2441 } else {
2442 #define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7)
2443 232 ▶ if (flags & GIT_INDEX_ENTRY_EXTENDED)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_INDEX_ENTRY_EXTENDED) 0 232 232
2444 0 return entry_size(struct entry_long, path_len);
2445 else
2446 232 return entry_size(struct entry_short, path_len);
2447 #undef entry_size
2448 }
2449 }
2450  
2451 26 static int read_entry(
2452 git_index_entry **out,
2453 size_t *out_size,
2454 git_index *index,
2455 const void *buffer,
2456 size_t buffer_size,
2457 const char *last)
2458 {
2459 size_t path_length, entry_size;
2460 const char *path_ptr;
2461 struct entry_short source;
2462 26 git_index_entry entry = {{0}};
2463 26 bool compressed = index->version >= INDEX_VERSION_NUMBER_COMP;
2464 26 char *tmp_path = NULL;
2465 26 size_t checksum_size = GIT_HASH_SHA1_SIZE;
2466  
2467 26 ▶ if (checksum_size + minimal_entry_size > buffer_size)
Branch: 1 branch
BranchTrueFalseTotal
if (checksum_size + minimal_entry_size > buffer_size) 0 26 26
2468 0 return -1;
2469  
2470 /* buffer is not guaranteed to be aligned */
2471 26 memcpy(&source, buffer, sizeof(struct entry_short));
2472  
2473 26 entry.ctime.seconds = (git_time_t)ntohl(source.ctime.seconds);
2474 26 entry.ctime.nanoseconds = ntohl(source.ctime.nanoseconds);
2475 26 entry.mtime.seconds = (git_time_t)ntohl(source.mtime.seconds);
2476 26 entry.mtime.nanoseconds = ntohl(source.mtime.nanoseconds);
2477 26 entry.dev = ntohl(source.dev);
2478 26 entry.ino = ntohl(source.ino);
2479 26 entry.mode = ntohl(source.mode);
2480 26 entry.uid = ntohl(source.uid);
2481 26 entry.gid = ntohl(source.gid);
2482 26 entry.file_size = ntohl(source.file_size);
2483 26 entry.flags = ntohs(source.flags);
2484  
2485 26 ▶ if (git_oid_fromraw(&entry.id, source.oid) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid_fromraw(&entry.id, source.oid) < 0) 0 26 26
2486 0 return -1;
2487  
2488 26 ▶ if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) { 0 26 26
2489 uint16_t flags_raw;
2490 size_t flags_offset;
2491  
2492 0 flags_offset = offsetof(struct entry_long, flags_extended);
2493 0 memcpy(&flags_raw, (const char *) buffer + flags_offset,
2494 sizeof(flags_raw));
2495 0 flags_raw = ntohs(flags_raw);
2496  
2497 0 memcpy(&entry.flags_extended, &flags_raw, sizeof(flags_raw));
2498 0 path_ptr = (const char *) buffer + offsetof(struct entry_long, path);
2499 } else
2500 26 path_ptr = (const char *) buffer + offsetof(struct entry_short, path);
2501  
2502 26 ▶ if (!compressed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!compressed) { 26 0 26
2503 26 path_length = entry.flags & GIT_INDEX_ENTRY_NAMEMASK;
2504  
2505 /* if this is a very long string, we must find its
2506 * real length without overflowing */
2507 26 ▶ if (path_length == 0xFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (path_length == 0xFFF) { 0 26 26
2508 const char *path_end;
2509  
2510 0 path_end = memchr(path_ptr, '\0', buffer_size);
2511 0 ▶ if (path_end == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (path_end == NULL) 0 0 0
2512 0 return -1;
2513  
2514 0 path_length = path_end - path_ptr;
2515 }
2516  
2517 26 entry_size = index_entry_size(path_length, 0, entry.flags);
2518 26 entry.path = (char *)path_ptr;
2519 } else {
2520 size_t varint_len, last_len, prefix_len, suffix_len, path_len;
2521 uintmax_t strip_len;
2522  
2523 0 strip_len = git_decode_varint((const unsigned char *)path_ptr, &varint_len);
2524 0 last_len = strlen(last);
2525  
2526 0 ▶ if (varint_len == 0 || last_len < strip_len)
Branch: 2 branches
BranchTrueFalseTotal
if (varint_len == 0 || last_len < strip_len) 0 0 0
if (varint_len == 0 || last_len < strip_len) 0 0 0
2527 0 return index_error_invalid("incorrect prefix length");
2528  
2529 0 prefix_len = last_len - (size_t)strip_len;
2530 0 suffix_len = strlen(path_ptr + varint_len);
2531  
2532 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len); 0 0 0
2533 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&path_len, path_len, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&path_len, path_len, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&path_len, path_len, 1); 0 0 0
2534  
2535 0 ▶ if (path_len > GIT_PATH_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (path_len > GIT_PATH_MAX) 0 0 0
2536 0 return index_error_invalid("unreasonable path length");
2537  
2538 0 tmp_path = git__malloc(path_len);
2539 0 ▶ GIT_ERROR_CHECK_ALLOC(tmp_path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(tmp_path); 0 0 0
2540  
2541 0 memcpy(tmp_path, last, prefix_len);
2542 0 memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1);
2543 0 entry_size = index_entry_size(suffix_len, varint_len, entry.flags);
2544 0 entry.path = tmp_path;
2545 }
2546  
2547 26 ▶ if (entry_size == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (entry_size == 0) 0 26 26
2548 0 return -1;
2549  
2550 26 ▶ if (checksum_size + entry_size > buffer_size)
Branch: 1 branch
BranchTrueFalseTotal
if (checksum_size + entry_size > buffer_size) 0 26 26
2551 0 return -1;
2552  
2553 26 ▶ if (index_entry_dup(out, index, &entry) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index_entry_dup(out, index, &entry) < 0) { 0 26 26
2554 0 git__free(tmp_path);
2555 0 return -1;
2556 }
2557  
2558 26 git__free(tmp_path);
2559 26 *out_size = entry_size;
2560 26 return 0;
2561 }
2562  
2563 11 static int read_header(struct index_header *dest, const void *buffer)
2564 {
2565 11 const struct index_header *source = buffer;
2566  
2567 11 dest->signature = ntohl(source->signature);
2568 11 ▶ if (dest->signature != INDEX_HEADER_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (dest->signature != INDEX_HEADER_SIG) 0 11 11
2569 0 return index_error_invalid("incorrect header signature");
2570  
2571 11 dest->version = ntohl(source->version);
2572 11 ▶ if (dest->version < INDEX_VERSION_NUMBER_LB ||
Branch: 1 branch
BranchTrueFalseTotal
if (dest->version < INDEX_VERSION_NUMBER_LB || 11 0 11
2573 11 ▶ dest->version > INDEX_VERSION_NUMBER_UB)
Branch: 1 branch
BranchTrueFalseTotal
dest->version > INDEX_VERSION_NUMBER_UB) 0 11 11
2574 0 return index_error_invalid("incorrect header version");
2575  
2576 11 dest->entry_count = ntohl(source->entry_count);
2577 11 return 0;
2578 }
2579  
2580 11 static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size)
2581 {
2582 struct index_extension dest;
2583 size_t total_size;
2584 11 size_t checksum_size = GIT_HASH_SHA1_SIZE;
2585  
2586 /* buffer is not guaranteed to be aligned */
2587 11 memcpy(&dest, buffer, sizeof(struct index_extension));
2588 11 dest.extension_size = ntohl(dest.extension_size);
2589  
2590 11 total_size = dest.extension_size + sizeof(struct index_extension);
2591  
2592 11 ▶ if (dest.extension_size > total_size ||
Branch: 2 branches
BranchTrueFalseTotal
if (dest.extension_size > total_size || 11 0 11
if (dest.extension_size > total_size || 11 0 11
2593 11 buffer_size < total_size ||
2594 11 ▶ buffer_size - total_size < checksum_size) {
Branch: 1 branch
BranchTrueFalseTotal
buffer_size - total_size < checksum_size) { 0 11 11
2595 0 index_error_invalid("extension is truncated");
2596 0 return -1;
2597 }
2598  
2599 /* optional extension */
2600 11 ▶ if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') { 11 0 11
if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') { 11 0 11
2601 /* tree cache */
2602 22 ▶ if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) { 11 0 11
2603 11 ▶ if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, &index->tree_pool) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, &index->tree_pool) < 0) 0 11 11
2604 0 return -1;
2605 0 ▶ } else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) { 0 0 0
2606 0 ▶ if (read_reuc(index, buffer + 8, dest.extension_size) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (read_reuc(index, buffer + 8, dest.extension_size) < 0) 0 0 0
2607 0 return -1;
2608 0 ▶ } else if (memcmp(dest.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (memcmp(dest.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4) == 0) { 0 0 0
2609 0 ▶ if (read_conflict_names(index, buffer + 8, dest.extension_size) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (read_conflict_names(index, buffer + 8, dest.extension_size) < 0) 0 0 0
2610 0 return -1;
2611 }
2612 /* else, unsupported extension. We cannot parse this, but we can skip
2613 * it by returning `total_size */
2614 } else {
2615 /* we cannot handle non-ignorable extensions;
2616 * in fact they aren't even defined in the standard */
2617 0 git_error_set(GIT_ERROR_INDEX, "unsupported mandatory extension: '%.4s'", dest.signature);
2618 0 return -1;
2619 }
2620  
2621 11 *read_len = total_size;
2622  
2623 11 return 0;
2624 }
2625  
2626 11 static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
2627 {
2628 11 int error = 0;
2629 unsigned int i;
2630 11 struct index_header header = { 0 };
2631 unsigned char checksum[GIT_HASH_SHA1_SIZE];
2632 11 size_t checksum_size = GIT_HASH_SHA1_SIZE;
2633 11 const char *last = NULL;
2634 11 const char *empty = "";
2635  
2636 #define seek_forward(_increase) { \
2637 if (_increase >= buffer_size) { \
2638 error = index_error_invalid("ran out of data while parsing"); \
2639 goto done; } \
2640 buffer += _increase; \
2641 buffer_size -= _increase;\
2642 }
2643  
2644 11 ▶ if (buffer_size < INDEX_HEADER_SIZE + checksum_size)
Branch: 1 branch
BranchTrueFalseTotal
if (buffer_size < INDEX_HEADER_SIZE + checksum_size) 0 11 11
2645 0 return index_error_invalid("insufficient buffer space");
2646  
2647 /* Precalculate the SHA1 of the files's contents -- we'll match it to
2648 * the provided SHA1 in the footer */
2649 11 git_hash_buf(checksum, buffer, buffer_size - checksum_size, GIT_HASH_ALGORITHM_SHA1);
2650  
2651 /* Parse header */
2652 11 ▶ if ((error = read_header(&header, buffer)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = read_header(&header, buffer)) < 0) 0 11 11
2653 0 return error;
2654  
2655 11 index->version = header.version;
2656 11 ▶ if (index->version >= INDEX_VERSION_NUMBER_COMP)
Branch: 1 branch
BranchTrueFalseTotal
if (index->version >= INDEX_VERSION_NUMBER_COMP) 0 11 11
2657 0 last = empty;
2658  
2659 11 ▶ seek_forward(INDEX_HEADER_SIZE);
Branch: 1 branch
BranchTrueFalseTotal
seek_forward(INDEX_HEADER_SIZE); 0 11 11
2660  
2661 11 ▶ GIT_ASSERT(!index->entries.length);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(!index->entries.length); 0 11 11
2662  
2663 11 ▶ if ((error = index_map_resize(index->entries_map, header.entry_count, index->ignore_case)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_map_resize(index->entries_map, header.entry_count, index->ignore_case)) < 0) 0 11 11
2664 0 return error;
2665  
2666 /* Parse all the entries */
2667 37 ▶ for (i = 0; i < header.entry_count && buffer_size > checksum_size; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < header.entry_count && buffer_size > checksum_size; ++i) { 26 11 37
for (i = 0; i < header.entry_count && buffer_size > checksum_size; ++i) { 26 0 26
2668 26 git_index_entry *entry = NULL;
2669 size_t entry_size;
2670  
2671 26 ▶ if ((error = read_entry(&entry, &entry_size, index, buffer, buffer_size, last)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = read_entry(&entry, &entry_size, index, buffer, buffer_size, last)) < 0) { 0 26 26
2672 0 error = index_error_invalid("invalid entry");
2673 0 goto done;
2674 }
2675  
2676 26 ▶ if ((error = git_vector_insert(&index->entries, entry)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_insert(&index->entries, entry)) < 0) { 0 26 26
2677 0 index_entry_free(entry);
2678 0 goto done;
2679 }
2680  
2681 26 ▶ if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0) { 0 26 26
2682 0 index_entry_free(entry);
2683 0 goto done;
2684 }
2685 26 error = 0;
2686  
2687 26 ▶ if (index->version >= INDEX_VERSION_NUMBER_COMP)
Branch: 1 branch
BranchTrueFalseTotal
if (index->version >= INDEX_VERSION_NUMBER_COMP) 0 26 26
2688 0 last = entry->path;
2689  
2690 26 ▶ seek_forward(entry_size);
Branch: 1 branch
BranchTrueFalseTotal
seek_forward(entry_size); 0 26 26
2691 }
2692  
2693 11 ▶ if (i != header.entry_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (i != header.entry_count) { 0 11 11
2694 0 error = index_error_invalid("header entries changed while parsing");
2695 0 goto done;
2696 }
2697  
2698 /* There's still space for some extensions! */
2699 22 ▶ while (buffer_size > checksum_size) {
Branch: 1 branch
BranchTrueFalseTotal
while (buffer_size > checksum_size) { 11 11 22
2700 size_t extension_size;
2701  
2702 11 ▶ if ((error = read_extension(&extension_size, index, buffer, buffer_size)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = read_extension(&extension_size, index, buffer, buffer_size)) < 0) { 0 11 11
2703 0 goto done;
2704 }
2705  
2706 11 ▶ seek_forward(extension_size);
Branch: 1 branch
BranchTrueFalseTotal
seek_forward(extension_size); 0 11 11
2707 }
2708  
2709 11 ▶ if (buffer_size != checksum_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (buffer_size != checksum_size) { 0 11 11
2710 0 error = index_error_invalid(
2711 "buffer size does not match index footer size");
2712 0 goto done;
2713 }
2714  
2715 /* 160-bit SHA-1 over the content of the index file before this checksum. */
2716 11 ▶ if (memcmp(checksum, buffer, checksum_size) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(checksum, buffer, checksum_size) != 0) { 0 11 11
2717 0 error = index_error_invalid(
2718 "calculated checksum does not match expected");
2719 0 goto done;
2720 }
2721  
2722 11 memcpy(index->checksum, checksum, checksum_size);
2723  
2724 #undef seek_forward
2725  
2726 /* Entries are stored case-sensitively on disk, so re-sort now if
2727 * in-memory index is supposed to be case-insensitive
2728 */
2729 11 ▶ git_vector_set_sorted(&index->entries, !index->ignore_case);
Branch: 1 branch
BranchTrueFalseTotal
git_vector_set_sorted(&index->entries, !index->ignore_case); 11 0 11
2730 11 git_vector_sort(&index->entries);
2731  
2732 11 index->dirty = 0;
2733 11 done:
2734 11 return error;
2735 }
2736  
2737 97 static bool is_index_extended(git_index *index)
2738 {
2739 size_t i, extended;
2740 git_index_entry *entry;
2741  
2742 97 extended = 0;
2743  
2744 303 ▶ git_vector_foreach(&index->entries, i, entry) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&index->entries, i, entry) { 206 97 303
2745 206 entry->flags &= ~GIT_INDEX_ENTRY_EXTENDED;
2746 206 ▶ if (entry->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS) { 0 206 206
2747 0 extended++;
2748 0 entry->flags |= GIT_INDEX_ENTRY_EXTENDED;
2749 }
2750 }
2751  
2752 97 return (extended > 0);
2753 }
2754  
2755 206 static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char *last)
2756 {
2757 206 void *mem = NULL;
2758 struct entry_short ondisk;
2759 size_t path_len, disk_size;
2760 206 int varint_len = 0;
2761 char *path;
2762 206 const char *path_start = entry->path;
2763 206 size_t same_len = 0;
2764  
2765 206 path_len = ((struct entry_internal *)entry)->pathlen;
2766  
2767 206 ▶ if (last) {
Branch: 1 branch
BranchTrueFalseTotal
if (last) { 0 206 206
2768 0 const char *last_c = last;
2769  
2770 0 ▶ while (*path_start == *last_c) {
Branch: 1 branch
BranchTrueFalseTotal
while (*path_start == *last_c) { 0 0 0
2771 0 ▶ if (!*path_start || !*last_c)
Branch: 2 branches
BranchTrueFalseTotal
if (!*path_start || !*last_c) 0 0 0
if (!*path_start || !*last_c) 0 0 0
2772 break;
2773 0 ++path_start;
2774 0 ++last_c;
2775 0 ++same_len;
2776 }
2777 0 path_len -= same_len;
2778 0 varint_len = git_encode_varint(NULL, 0, strlen(last) - same_len);
2779 }
2780  
2781 206 disk_size = index_entry_size(path_len, varint_len, entry->flags);
2782  
2783 206 ▶ if (git_filebuf_reserve(file, &mem, disk_size) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_filebuf_reserve(file, &mem, disk_size) < 0) 0 206 206
2784 0 return -1;
2785  
2786 206 memset(mem, 0x0, disk_size);
2787  
2788 /**
2789 * Yes, we have to truncate.
2790 *
2791 * The on-disk format for Index entries clearly defines
2792 * the time and size fields to be 4 bytes each -- so even if
2793 * we store these values with 8 bytes on-memory, they must
2794 * be truncated to 4 bytes before writing to disk.
2795 *
2796 * In 2038 I will be either too dead or too rich to care about this
2797 */
2798 206 ondisk.ctime.seconds = htonl((uint32_t)entry->ctime.seconds);
2799 206 ondisk.mtime.seconds = htonl((uint32_t)entry->mtime.seconds);
2800 206 ondisk.ctime.nanoseconds = htonl(entry->ctime.nanoseconds);
2801 206 ondisk.mtime.nanoseconds = htonl(entry->mtime.nanoseconds);
2802 206 ondisk.dev = htonl(entry->dev);
2803 206 ondisk.ino = htonl(entry->ino);
2804 206 ondisk.mode = htonl(entry->mode);
2805 206 ondisk.uid = htonl(entry->uid);
2806 206 ondisk.gid = htonl(entry->gid);
2807 206 ondisk.file_size = htonl((uint32_t)entry->file_size);
2808 206 git_oid_raw_cpy(ondisk.oid, entry->id.id);
2809 206 ondisk.flags = htons(entry->flags);
2810  
2811 206 ▶ if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) { 0 206 206
2812 0 const size_t path_offset = offsetof(struct entry_long, path);
2813 struct entry_long ondisk_ext;
2814 0 memcpy(&ondisk_ext, &ondisk, sizeof(struct entry_short));
2815 0 ondisk_ext.flags_extended = htons(entry->flags_extended &
2816 GIT_INDEX_ENTRY_EXTENDED_FLAGS);
2817 0 memcpy(mem, &ondisk_ext, path_offset);
2818 0 path = (char *)mem + path_offset;
2819 0 disk_size -= path_offset;
2820 } else {
2821 206 const size_t path_offset = offsetof(struct entry_short, path);
2822 206 memcpy(mem, &ondisk, path_offset);
2823 206 path = (char *)mem + path_offset;
2824 206 disk_size -= path_offset;
2825 }
2826  
2827 206 ▶ if (last) {
Branch: 1 branch
BranchTrueFalseTotal
if (last) { 0 206 206
2828 0 varint_len = git_encode_varint((unsigned char *) path,
2829 0 disk_size, strlen(last) - same_len);
2830 0 ▶ GIT_ASSERT(varint_len > 0);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(varint_len > 0); 0 0 0
2831  
2832 0 path += varint_len;
2833 0 disk_size -= varint_len;
2834  
2835 /*
2836 * If using path compression, we are not allowed
2837 * to have additional trailing NULs.
2838 */
2839 0 ▶ GIT_ASSERT(disk_size == path_len + 1);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(disk_size == path_len + 1); 0 0 0
2840 } else {
2841 /*
2842 * If no path compression is used, we do have
2843 * NULs as padding. As such, simply assert that
2844 * we have enough space left to write the path.
2845 */
2846 206 ▶ GIT_ASSERT(disk_size > path_len);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(disk_size > path_len); 0 206 206
2847 }
2848  
2849 206 memcpy(path, path_start, path_len + 1);
2850  
2851 206 return 0;
2852 }
2853  
2854 97 static int write_entries(git_index *index, git_filebuf *file)
2855 {
2856 97 int error = 0;
2857 size_t i;
2858 97 git_vector case_sorted = GIT_VECTOR_INIT, *entries = NULL;
2859 git_index_entry *entry;
2860 97 const char *last = NULL;
2861  
2862 /* If index->entries is sorted case-insensitively, then we need
2863 * to re-sort it case-sensitively before writing */
2864 97 ▶ if (index->ignore_case) {
Branch: 1 branch
BranchTrueFalseTotal
if (index->ignore_case) { 0 97 97
2865 0 ▶ if ((error = git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp)) < 0) 0 0 0
2866 0 goto done;
2867  
2868 0 git_vector_sort(&case_sorted);
2869 0 entries = &case_sorted;
2870 } else {
2871 97 entries = &index->entries;
2872 }
2873  
2874 97 ▶ if (index->version >= INDEX_VERSION_NUMBER_COMP)
Branch: 1 branch
BranchTrueFalseTotal
if (index->version >= INDEX_VERSION_NUMBER_COMP) 0 97 97
2875 0 last = "";
2876  
2877 303 ▶ git_vector_foreach(entries, i, entry) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(entries, i, entry) { 206 97 303
2878 206 ▶ if ((error = write_disk_entry(file, entry, last)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = write_disk_entry(file, entry, last)) < 0) 0 206 206
2879 0 break;
2880 206 ▶ if (index->version >= INDEX_VERSION_NUMBER_COMP)
Branch: 1 branch
BranchTrueFalseTotal
if (index->version >= INDEX_VERSION_NUMBER_COMP) 0 206 206
2881 0 last = entry->path;
2882 }
2883  
2884 97 done:
2885 97 git_vector_free(&case_sorted);
2886 97 return error;
2887 }
2888  
2889 97 static int write_extension(git_filebuf *file, struct index_extension *header, git_str *data)
2890 {
2891 struct index_extension ondisk;
2892  
2893 97 memset(&ondisk, 0x0, sizeof(struct index_extension));
2894 97 memcpy(&ondisk, header, 4);
2895 97 ondisk.extension_size = htonl(header->extension_size);
2896  
2897 97 git_filebuf_write(file, &ondisk, sizeof(struct index_extension));
2898 97 return git_filebuf_write(file, data->ptr, data->size);
2899 }
2900  
2901 0 static int create_name_extension_data(git_str *name_buf, git_index_name_entry *conflict_name)
2902 {
2903 0 int error = 0;
2904  
2905 0 ▶ if (conflict_name->ancestor == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (conflict_name->ancestor == NULL) 0 0 0
2906 0 error = git_str_put(name_buf, "\0", 1);
2907 else
2908 0 error = git_str_put(name_buf, conflict_name->ancestor, strlen(conflict_name->ancestor) + 1);
2909  
2910 0 ▶ if (error != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error != 0) 0 0 0
2911 0 goto on_error;
2912  
2913 0 ▶ if (conflict_name->ours == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (conflict_name->ours == NULL) 0 0 0
2914 0 error = git_str_put(name_buf, "\0", 1);
2915 else
2916 0 error = git_str_put(name_buf, conflict_name->ours, strlen(conflict_name->ours) + 1);
2917  
2918 0 ▶ if (error != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error != 0) 0 0 0
2919 0 goto on_error;
2920  
2921 0 ▶ if (conflict_name->theirs == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (conflict_name->theirs == NULL) 0 0 0
2922 0 error = git_str_put(name_buf, "\0", 1);
2923 else
2924 0 error = git_str_put(name_buf, conflict_name->theirs, strlen(conflict_name->theirs) + 1);
2925  
2926 0 on_error:
2927 0 return error;
2928 }
2929  
2930 0 static int write_name_extension(git_index *index, git_filebuf *file)
2931 {
2932 0 git_str name_buf = GIT_STR_INIT;
2933 0 git_vector *out = &index->names;
2934 git_index_name_entry *conflict_name;
2935 struct index_extension extension;
2936 size_t i;
2937 0 int error = 0;
2938  
2939 0 ▶ git_vector_foreach(out, i, conflict_name) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(out, i, conflict_name) { 0 0 0
2940 0 ▶ if ((error = create_name_extension_data(&name_buf, conflict_name)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = create_name_extension_data(&name_buf, conflict_name)) < 0) 0 0 0
2941 0 goto done;
2942 }
2943  
2944 0 memset(&extension, 0x0, sizeof(struct index_extension));
2945 0 memcpy(&extension.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4);
2946 0 extension.extension_size = (uint32_t)name_buf.size;
2947  
2948 0 error = write_extension(file, &extension, &name_buf);
2949  
2950 0 git_str_dispose(&name_buf);
2951  
2952 0 done:
2953 0 return error;
2954 }
2955  
2956 5 static int create_reuc_extension_data(git_str *reuc_buf, git_index_reuc_entry *reuc)
2957 {
2958 int i;
2959 5 int error = 0;
2960  
2961 5 ▶ if ((error = git_str_put(reuc_buf, reuc->path, strlen(reuc->path) + 1)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_put(reuc_buf, reuc->path, strlen(reuc->path) + 1)) < 0) 0 5 5
2962 0 return error;
2963  
2964 20 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 15 5 20
2965 30 if ((error = git_str_printf(reuc_buf, "%o", reuc->mode[i])) < 0 ||
2966 15 (error = git_str_put(reuc_buf, "\0", 1)) < 0)
2967 0 return error;
2968 }
2969  
2970 20 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 15 5 20
2971 15 ▶ if (reuc->mode[i] && (error = git_str_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_RAWSZ)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (reuc->mode[i] && (error = git_str_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_RAWSZ)) < 0) 15 0 15
if (reuc->mode[i] && (error = git_str_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_RAWSZ)) < 0) 0 15 15
2972 0 return error;
2973 }
2974  
2975 5 return 0;
2976 }
2977  
2978 5 static int write_reuc_extension(git_index *index, git_filebuf *file)
2979 {
2980 5 git_str reuc_buf = GIT_STR_INIT;
2981 5 git_vector *out = &index->reuc;
2982 git_index_reuc_entry *reuc;
2983 struct index_extension extension;
2984 size_t i;
2985 5 int error = 0;
2986  
2987 10 ▶ git_vector_foreach(out, i, reuc) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(out, i, reuc) { 5 5 10
2988 5 ▶ if ((error = create_reuc_extension_data(&reuc_buf, reuc)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = create_reuc_extension_data(&reuc_buf, reuc)) < 0) 0 5 5
2989 0 goto done;
2990 }
2991  
2992 5 memset(&extension, 0x0, sizeof(struct index_extension));
2993 5 memcpy(&extension.signature, INDEX_EXT_UNMERGED_SIG, 4);
2994 5 extension.extension_size = (uint32_t)reuc_buf.size;
2995  
2996 5 error = write_extension(file, &extension, &reuc_buf);
2997  
2998 5 git_str_dispose(&reuc_buf);
2999  
3000 5 done:
3001 5 return error;
3002 }
3003  
3004 92 static int write_tree_extension(git_index *index, git_filebuf *file)
3005 {
3006 struct index_extension extension;
3007 92 git_str buf = GIT_STR_INIT;
3008 int error;
3009  
3010 92 ▶ if (index->tree == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (index->tree == NULL) 0 92 92
3011 0 return 0;
3012  
3013 92 ▶ if ((error = git_tree_cache_write(&buf, index->tree)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_tree_cache_write(&buf, index->tree)) < 0) 0 92 92
3014 0 return error;
3015  
3016 92 memset(&extension, 0x0, sizeof(struct index_extension));
3017 92 memcpy(&extension.signature, INDEX_EXT_TREECACHE_SIG, 4);
3018 92 extension.extension_size = (uint32_t)buf.size;
3019  
3020 92 error = write_extension(file, &extension, &buf);
3021  
3022 92 git_str_dispose(&buf);
3023  
3024 92 return error;
3025 }
3026  
3027 99 static void clear_uptodate(git_index *index)
3028 {
3029 git_index_entry *entry;
3030 size_t i;
3031  
3032 311 ▶ git_vector_foreach(&index->entries, i, entry)
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&index->entries, i, entry) 212 99 311
3033 212 entry->flags_extended &= ~GIT_INDEX_ENTRY_UPTODATE;
3034 99 }
3035  
3036 97 static int write_index(
3037 unsigned char checksum[GIT_HASH_SHA1_SIZE],
3038 size_t *checksum_size,
3039 git_index *index,
3040 git_filebuf *file)
3041 {
3042 struct index_header header;
3043 bool is_extended;
3044 uint32_t index_version_number;
3045  
3046 97 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 97 97
3047 97 ▶ GIT_ASSERT_ARG(file);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(file); 0 97 97
3048  
3049 97 *checksum_size = GIT_HASH_SHA1_SIZE;
3050  
3051 97 ▶ if (index->version <= INDEX_VERSION_NUMBER_EXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (index->version <= INDEX_VERSION_NUMBER_EXT) { 97 0 97
3052 97 is_extended = is_index_extended(index);
3053 97 ▶ index_version_number = is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER_LB;
Branch: 1 branch
BranchTrueFalseTotal
index_version_number = is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER_LB; 0 97 97
3054 } else {
3055 0 index_version_number = index->version;
3056 }
3057  
3058 97 header.signature = htonl(INDEX_HEADER_SIG);
3059 97 header.version = htonl(index_version_number);
3060 97 header.entry_count = htonl((uint32_t)index->entries.length);
3061  
3062 97 ▶ if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0) 0 97 97
3063 0 return -1;
3064  
3065 97 ▶ if (write_entries(index, file) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (write_entries(index, file) < 0) 0 97 97
3066 0 return -1;
3067  
3068 /* write the tree cache extension */
3069 97 ▶ if (index->tree != NULL && write_tree_extension(index, file) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (index->tree != NULL && write_tree_extension(index, file) < 0) 92 5 97
if (index->tree != NULL && write_tree_extension(index, file) < 0) 0 92 92
3070 0 return -1;
3071  
3072 /* write the rename conflict extension */
3073 97 ▶ if (index->names.length > 0 && write_name_extension(index, file) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (index->names.length > 0 && write_name_extension(index, file) < 0) 0 97 97
if (index->names.length > 0 && write_name_extension(index, file) < 0) 0 0 0
3074 0 return -1;
3075  
3076 /* write the reuc extension */
3077 97 ▶ if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0) 5 92 97
if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0) 0 5 5
3078 0 return -1;
3079  
3080 /* get out the hash for all the contents we've appended to the file */
3081 97 git_filebuf_hash(checksum, file);
3082  
3083 /* write it at the end of the file */
3084 97 ▶ if (git_filebuf_write(file, checksum, *checksum_size) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_filebuf_write(file, checksum, *checksum_size) < 0) 0 97 97
3085 0 return -1;
3086  
3087 /* file entries are no longer up to date */
3088 97 clear_uptodate(index);
3089  
3090 97 return 0;
3091 }
3092  
3093 2 int git_index_entry_stage(const git_index_entry *entry)
3094 {
3095 2 return GIT_INDEX_ENTRY_STAGE(entry);
3096 }
3097  
3098 2802 int git_index_entry_is_conflict(const git_index_entry *entry)
3099 {
3100 2802 return (GIT_INDEX_ENTRY_STAGE(entry) > 0);
3101 }
3102  
3103 typedef struct read_tree_data {
3104 git_index *index;
3105 git_vector *old_entries;
3106 git_vector *new_entries;
3107 git_vector_cmp entry_cmp;
3108 git_tree_cache *tree;
3109 } read_tree_data;
3110  
3111 13 static int read_tree_cb(
3112 const char *root, const git_tree_entry *tentry, void *payload)
3113 {
3114 13 read_tree_data *data = payload;
3115 13 git_index_entry *entry = NULL, *old_entry;
3116 13 git_str path = GIT_STR_INIT;
3117 size_t pos;
3118  
3119 13 ▶ if (git_tree_entry__is_tree(tentry))
Branch: 1 branch
BranchTrueFalseTotal
if (git_tree_entry__is_tree(tentry)) 4 9 13
3120 4 return 0;
3121  
3122 9 ▶ if (git_str_joinpath(&path, root, tentry->filename) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_joinpath(&path, root, tentry->filename) < 0) 0 9 9
3123 0 return -1;
3124  
3125 9 ▶ if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, NULL, false) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, NULL, false) < 0) 0 9 9
3126 0 return -1;
3127  
3128 9 entry->mode = tentry->attr;
3129 9 git_oid_cpy(&entry->id, git_tree_entry_id(tentry));
3130  
3131 /* look for corresponding old entry and copy data to new entry */
3132 18 if (data->old_entries != NULL &&
3133 9 !index_find_in_entries(
3134 18 ▶ &pos, data->old_entries, data->entry_cmp, path.ptr, 0, 0) &&
Branch: 1 branch
BranchTrueFalseTotal
&pos, data->old_entries, data->entry_cmp, path.ptr, 0, 0) && 9 0 9
3135 9 (old_entry = git_vector_get(data->old_entries, pos)) != NULL &&
3136 18 entry->mode == old_entry->mode &&
3137 9 git_oid_equal(&entry->id, &old_entry->id))
3138 {
3139 9 index_entry_cpy(entry, old_entry);
3140 9 entry->flags_extended = 0;
3141 }
3142  
3143 9 index_entry_adjust_namemask(entry, path.size);
3144 9 git_str_dispose(&path);
3145  
3146 9 ▶ if (git_vector_insert(data->new_entries, entry) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_vector_insert(data->new_entries, entry) < 0) { 0 9 9
3147 0 index_entry_free(entry);
3148 0 return -1;
3149 }
3150  
3151 9 return 0;
3152 }
3153  
3154 5 int git_index_read_tree(git_index *index, const git_tree *tree)
3155 {
3156 5 int error = 0;
3157 5 git_vector entries = GIT_VECTOR_INIT;
3158 git_idxmap *entries_map;
3159 read_tree_data data;
3160 size_t i;
3161 git_index_entry *e;
3162  
3163 5 ▶ if (git_idxmap_new(&entries_map) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_idxmap_new(&entries_map) < 0) 0 5 5
3164 0 return -1;
3165  
3166 5 git_vector_set_cmp(&entries, index->entries._cmp); /* match sort */
3167  
3168 5 data.index = index;
3169 5 data.old_entries = &index->entries;
3170 5 data.new_entries = &entries;
3171 5 data.entry_cmp = index->entries_search;
3172  
3173 5 index->tree = NULL;
3174 5 git_pool_clear(&index->tree_pool);
3175  
3176 5 git_vector_sort(&index->entries);
3177  
3178 5 ▶ if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0) 0 5 5
3179 0 goto cleanup;
3180  
3181 5 ▶ if ((error = index_map_resize(entries_map, entries.length, index->ignore_case)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_map_resize(entries_map, entries.length, index->ignore_case)) < 0) 0 5 5
3182 0 goto cleanup;
3183  
3184 14 ▶ git_vector_foreach(&entries, i, e) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&entries, i, e) { 9 5 14
3185 9 ▶ if ((error = index_map_set(entries_map, e, index->ignore_case)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_map_set(entries_map, e, index->ignore_case)) < 0) { 0 9 9
3186 0 git_error_set(GIT_ERROR_INDEX, "failed to insert entry into map");
3187 0 return error;
3188 }
3189 }
3190  
3191 5 error = 0;
3192  
3193 5 git_vector_sort(&entries);
3194  
3195 5 ▶ if ((error = git_index_clear(index)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_index_clear(index)) < 0) { 5 0 5
3196 /* well, this isn't good */;
3197 } else {
3198 5 git_vector_swap(&entries, &index->entries);
3199 5 entries_map = git_atomic_swap(index->entries_map, entries_map);
3200 }
3201  
3202 5 index->dirty = 1;
3203  
3204 5 cleanup:
3205 5 git_vector_free(&entries);
3206 5 git_idxmap_free(entries_map);
3207 5 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 5 5
3208 0 return error;
3209  
3210 5 error = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool);
3211  
3212 5 return error;
3213 }
3214  
3215 2 static int git_index_read_iterator(
3216 git_index *index,
3217 git_iterator *new_iterator,
3218 size_t new_length_hint)
3219 {
3220 2 git_vector new_entries = GIT_VECTOR_INIT,
3221 2 remove_entries = GIT_VECTOR_INIT;
3222 2 git_idxmap *new_entries_map = NULL;
3223 2 git_iterator *index_iterator = NULL;
3224 2 git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
3225 const git_index_entry *old_entry, *new_entry;
3226 git_index_entry *entry;
3227 size_t i;
3228 int error;
3229  
3230 2 ▶ GIT_ASSERT((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE)); 0 2 2
3231  
3232 4 if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 ||
3233 4 ▶ (error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 || 0 2 2
3234 2 (error = git_idxmap_new(&new_entries_map)) < 0)
3235 0 goto done;
3236  
3237 4 if (new_length_hint && (error = index_map_resize(new_entries_map, new_length_hint,
3238 2 index->ignore_case)) < 0)
3239 0 goto done;
3240  
3241 2 opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
3242 GIT_ITERATOR_INCLUDE_CONFLICTS;
3243  
3244 2 ▶ if ((error = git_iterator_for_index(&index_iterator,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_for_index(&index_iterator, 2 0 2
3245 2 ▶ git_index_owner(index), index, &opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_index_owner(index), index, &opts)) < 0 || 0 2 2
3246 2 ▶ ((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
Branch: 1 branch
BranchTrueFalseTotal
((error = git_iterator_current(&old_entry, index_iterator)) < 0 && 0 0 0
3247 2 ▶ error != GIT_ITEROVER) ||
Branch: 1 branch
BranchTrueFalseTotal
error != GIT_ITEROVER) || 2 0 2
3248 2 ▶ ((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
Branch: 1 branch
BranchTrueFalseTotal
((error = git_iterator_current(&new_entry, new_iterator)) < 0 && 0 0 0
3249 error != GIT_ITEROVER))
3250 0 goto done;
3251  
3252 6 while (true) {
3253 git_index_entry
3254 8 *dup_entry = NULL,
3255 8 *add_entry = NULL,
3256 8 *remove_entry = NULL;
3257 int diff;
3258  
3259 8 error = 0;
3260  
3261 8 ▶ if (old_entry && new_entry)
Branch: 2 branches
BranchTrueFalseTotal
if (old_entry && new_entry) 6 2 8
if (old_entry && new_entry) 6 0 6
3262 6 diff = git_index_entry_cmp(old_entry, new_entry);
3263 2 ▶ else if (!old_entry && new_entry)
Branch: 2 branches
BranchTrueFalseTotal
else if (!old_entry && new_entry) 2 0 2
else if (!old_entry && new_entry) 0 2 2
3264 0 diff = 1;
3265 2 ▶ else if (old_entry && !new_entry)
Branch: 2 branches
BranchTrueFalseTotal
else if (old_entry && !new_entry) 0 2 2
else if (old_entry && !new_entry) 0 0 0
3266 0 diff = -1;
3267 else
3268 break;
3269  
3270 6 ▶ if (diff < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff < 0) { 0 6 6
3271 0 remove_entry = (git_index_entry *)old_entry;
3272 6 ▶ } else if (diff > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (diff > 0) { 0 6 6
3273 0 dup_entry = (git_index_entry *)new_entry;
3274 } else {
3275 /* Path and stage are equal, if the OID is equal, keep it to
3276 * keep the stat cache data.
3277 */
3278 6 ▶ if (git_oid_equal(&old_entry->id, &new_entry->id) &&
Branch: 1 branch
BranchTrueFalseTotal
if (git_oid_equal(&old_entry->id, &new_entry->id) && 4 2 6
3279 4 ▶ old_entry->mode == new_entry->mode) {
Branch: 1 branch
BranchTrueFalseTotal
old_entry->mode == new_entry->mode) { 4 0 4
3280 4 add_entry = (git_index_entry *)old_entry;
3281 } else {
3282 2 dup_entry = (git_index_entry *)new_entry;
3283 2 remove_entry = (git_index_entry *)old_entry;
3284 }
3285 }
3286  
3287 6 ▶ if (dup_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (dup_entry) { 2 4 6
3288 2 ▶ if ((error = index_entry_dup_nocache(&add_entry, index, dup_entry)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = index_entry_dup_nocache(&add_entry, index, dup_entry)) < 0) 0 2 2
3289 0 goto done;
3290  
3291 2 index_entry_adjust_namemask(add_entry,
3292 2 ((struct entry_internal *)add_entry)->pathlen);
3293 }
3294  
3295 /* invalidate this path in the tree cache if this is new (to
3296 * invalidate the parent trees)
3297 */
3298 6 ▶ if (dup_entry && !remove_entry && index->tree)
Branch: 3 branches
BranchTrueFalseTotal
if (dup_entry && !remove_entry && index->tree) 2 4 6
if (dup_entry && !remove_entry && index->tree) 0 2 2
if (dup_entry && !remove_entry && index->tree) 0 0 0
3299 0 git_tree_cache_invalidate_path(index->tree, dup_entry->path);
3300  
3301 6 ▶ if (add_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (add_entry) { 6 0 6
3302 6 ▶ if ((error = git_vector_insert(&new_entries, add_entry)) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_insert(&new_entries, add_entry)) == 0) 6 0 6
3303 6 error = index_map_set(new_entries_map, add_entry,
3304 6 index->ignore_case);
3305 }
3306  
3307 6 ▶ if (remove_entry && error >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (remove_entry && error >= 0) 2 4 6
if (remove_entry && error >= 0) 2 0 2
3308 2 error = git_vector_insert(&remove_entries, remove_entry);
3309  
3310 6 ▶ if (error < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) { 0 6 6
3311 0 git_error_set(GIT_ERROR_INDEX, "failed to insert entry");
3312 0 goto done;
3313 }
3314  
3315 6 ▶ if (diff <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff <= 0) { 6 0 6
3316 6 ▶ if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 && 2 4 6
if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 && 0 2 2
3317 error != GIT_ITEROVER)
3318 0 goto done;
3319 }
3320  
3321 6 ▶ if (diff >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff >= 0) { 6 0 6
3322 6 ▶ if ((error = git_iterator_advance(&new_entry, new_iterator)) < 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if ((error = git_iterator_advance(&new_entry, new_iterator)) < 0 && 2 4 6
if ((error = git_iterator_advance(&new_entry, new_iterator)) < 0 && 0 2 2
3323 error != GIT_ITEROVER)
3324 0 goto done;
3325 }
3326 }
3327  
3328 4 if ((error = git_index_name_clear(index)) < 0 ||
3329 2 (error = git_index_reuc_clear(index)) < 0)
3330 0 goto done;
3331  
3332 2 git_vector_swap(&new_entries, &index->entries);
3333 2 new_entries_map = git_atomic_swap(index->entries_map, new_entries_map);
3334  
3335 4 ▶ git_vector_foreach(&remove_entries, i, entry) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&remove_entries, i, entry) { 2 2 4
3336 2 ▶ if (index->tree)
Branch: 1 branch
BranchTrueFalseTotal
if (index->tree) 2 0 2
3337 2 git_tree_cache_invalidate_path(index->tree, entry->path);
3338  
3339 2 index_entry_free(entry);
3340 }
3341  
3342 2 clear_uptodate(index);
3343  
3344 2 index->dirty = 1;
3345 2 error = 0;
3346  
3347 2 done:
3348 2 git_idxmap_free(new_entries_map);
3349 2 git_vector_free(&new_entries);
3350 2 git_vector_free(&remove_entries);
3351 2 git_iterator_free(index_iterator);
3352 2 return error;
3353 }
3354  
3355 2 int git_index_read_index(
3356 git_index *index,
3357 const git_index *new_index)
3358 {
3359 2 git_iterator *new_iterator = NULL;
3360 2 git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
3361 int error;
3362  
3363 2 opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
3364 GIT_ITERATOR_INCLUDE_CONFLICTS;
3365  
3366 2 ▶ if ((error = git_iterator_for_index(&new_iterator,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_iterator_for_index(&new_iterator, 2 0 2
3367 2 ▶ git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 || 2 0 2
3368 2 (error = git_index_read_iterator(index, new_iterator,
3369 2 new_index->entries.length)) < 0)
3370 0 goto done;
3371  
3372 2 done:
3373 2 git_iterator_free(new_iterator);
3374 2 return error;
3375 }
3376  
3377 54 git_repository *git_index_owner(const git_index *index)
3378 {
3379 54 return INDEX_OWNER(index);
3380 }
3381  
3382 enum {
3383 INDEX_ACTION_NONE = 0,
3384 INDEX_ACTION_UPDATE = 1,
3385 INDEX_ACTION_REMOVE = 2,
3386 INDEX_ACTION_ADDALL = 3
3387 };
3388  
3389 4 int git_index_add_all(
3390 git_index *index,
3391 const git_strarray *paths,
3392 unsigned int flags,
3393 git_index_matched_path_cb cb,
3394 void *payload)
3395 {
3396 int error;
3397 git_repository *repo;
3398 4 git_iterator *wditer = NULL;
3399 git_pathspec ps;
3400 4 bool no_fnmatch = (flags & GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH) != 0;
3401  
3402 4 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 4 4
3403  
3404 4 repo = INDEX_OWNER(index);
3405 4 ▶ if ((error = git_repository__ensure_not_bare(repo, "index add all")) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository__ensure_not_bare(repo, "index add all")) < 0) 0 4 4
3406 0 return error;
3407  
3408 4 ▶ if ((error = git_pathspec__init(&ps, paths)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_pathspec__init(&ps, paths)) < 0) 0 4 4
3409 0 return error;
3410  
3411 /* optionally check that pathspec doesn't mention any ignored files */
3412 4 ▶ if ((flags & GIT_INDEX_ADD_CHECK_PATHSPEC) != 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & GIT_INDEX_ADD_CHECK_PATHSPEC) != 0 && 0 4 4
3413 0 (flags & GIT_INDEX_ADD_FORCE) == 0 &&
3414 0 (error = git_ignore__check_pathspec_for_exact_ignores(
3415 repo, &ps.pathspec, no_fnmatch)) < 0)
3416 0 goto cleanup;
3417  
3418 4 error = index_apply_to_wd_diff(index, INDEX_ACTION_ADDALL, paths, flags, cb, payload);
3419  
3420 4 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 4 0 4
3421 0 git_error_set_after_callback(error);
3422  
3423 4 cleanup:
3424 4 git_iterator_free(wditer);
3425 4 git_pathspec__clear(&ps);
3426  
3427 4 return error;
3428 }
3429  
3430 struct foreach_diff_data {
3431 git_index *index;
3432 const git_pathspec *pathspec;
3433 unsigned int flags;
3434 git_index_matched_path_cb cb;
3435 void *payload;
3436 };
3437  
3438 12 static int apply_each_file(const git_diff_delta *delta, float progress, void *payload)
3439 {
3440 12 struct foreach_diff_data *data = payload;
3441 const char *match, *path;
3442 12 int error = 0;
3443  
3444 12 GIT_UNUSED(progress);
3445  
3446 12 path = delta->old_file.path;
3447  
3448 /* We only want those which match the pathspecs */
3449 12 ▶ if (!git_pathspec__match(
Branch: 1 branch
BranchTrueFalseTotal
if (!git_pathspec__match( 6 6 12
3450 12 &data->pathspec->pathspec, path, false, (bool)data->index->ignore_case,
3451 &match, NULL))
3452 6 return 0;
3453  
3454 6 ▶ if (data->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (data->cb) 6 0 6
3455 6 error = data->cb(path, match, data->payload);
3456  
3457 6 ▶ if (error > 0) /* skip this entry */
Branch: 1 branch
BranchTrueFalseTotal
if (error > 0) /* skip this entry */ 1 5 6
3458 1 return 0;
3459 5 ▶ if (error < 0) /* actual error */
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) /* actual error */ 1 4 5
3460 1 return error;
3461  
3462 /* If the workdir item does not exist, remove it from the index. */
3463 4 ▶ if ((delta->new_file.flags & GIT_DIFF_FLAG_EXISTS) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((delta->new_file.flags & GIT_DIFF_FLAG_EXISTS) == 0) 0 4 4
3464 0 error = git_index_remove_bypath(data->index, path);
3465 else
3466 4 error = git_index_add_bypath(data->index, delta->new_file.path);
3467  
3468 4 return error;
3469 }
3470  
3471 8 static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
3472 unsigned int flags,
3473 git_index_matched_path_cb cb, void *payload)
3474 {
3475 int error;
3476 git_diff *diff;
3477 git_pathspec ps;
3478 git_repository *repo;
3479 8 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
3480 8 struct foreach_diff_data data = {
3481 index,
3482 NULL,
3483 flags,
3484 cb,
3485 payload,
3486 };
3487  
3488 8 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 8 8
3489 8 ▶ GIT_ASSERT_ARG(action == INDEX_ACTION_UPDATE || action == INDEX_ACTION_ADDALL);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ASSERT_ARG(action == INDEX_ACTION_UPDATE || action == INDEX_ACTION_ADDALL); 4 4 8
GIT_ASSERT_ARG(action == INDEX_ACTION_UPDATE || action == INDEX_ACTION_ADDALL); 0 4 4
3490  
3491 8 repo = INDEX_OWNER(index);
3492  
3493 8 ▶ if (!repo) {
Branch: 1 branch
BranchTrueFalseTotal
if (!repo) { 0 8 8
3494 0 return create_index_error(-1,
3495 "cannot run update; the index is not backed up by a repository.");
3496 }
3497  
3498 /*
3499 * We do the matching ourselves instead of passing the list to
3500 * diff because we want to tell the callback which one
3501 * matched, which we do not know if we ask diff to filter for us.
3502 */
3503 8 ▶ if ((error = git_pathspec__init(&ps, paths)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_pathspec__init(&ps, paths)) < 0) 0 8 8
3504 0 return error;
3505  
3506 8 opts.flags = GIT_DIFF_INCLUDE_TYPECHANGE;
3507 8 ▶ if (action == INDEX_ACTION_ADDALL) {
Branch: 1 branch
BranchTrueFalseTotal
if (action == INDEX_ACTION_ADDALL) { 4 4 8
3508 4 opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
3509 GIT_DIFF_RECURSE_UNTRACKED_DIRS;
3510  
3511 4 ▶ if (flags == GIT_INDEX_ADD_FORCE)
Branch: 1 branch
BranchTrueFalseTotal
if (flags == GIT_INDEX_ADD_FORCE) 1 3 4
3512 1 opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
3513 }
3514  
3515 8 ▶ if ((error = git_diff_index_to_workdir(&diff, repo, index, &opts)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_diff_index_to_workdir(&diff, repo, index, &opts)) < 0) 0 8 8
3516 0 goto cleanup;
3517  
3518 8 data.pathspec = &ps;
3519 8 error = git_diff_foreach(diff, apply_each_file, NULL, NULL, NULL, &data);
3520 8 git_diff_free(diff);
3521  
3522 8 ▶ if (error) /* make sure error is set if callback stopped iteration */
Branch: 1 branch
BranchTrueFalseTotal
if (error) /* make sure error is set if callback stopped iteration */ 7 1 8
3523 1 git_error_set_after_callback(error);
3524  
3525 7 cleanup:
3526 8 git_pathspec__clear(&ps);
3527 8 return error;
3528 }
3529  
3530 1 static int index_apply_to_all(
3531 git_index *index,
3532 int action,
3533 const git_strarray *paths,
3534 git_index_matched_path_cb cb,
3535 void *payload)
3536 {
3537 1 int error = 0;
3538 size_t i;
3539 git_pathspec ps;
3540 const char *match;
3541 1 git_str path = GIT_STR_INIT;
3542  
3543 1 ▶ GIT_ASSERT_ARG(index);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(index); 0 1 1
3544  
3545 1 ▶ if ((error = git_pathspec__init(&ps, paths)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_pathspec__init(&ps, paths)) < 0) 0 1 1
3546 0 return error;
3547  
3548 1 git_vector_sort(&index->entries);
3549  
3550 2 ▶ for (i = 0; !error && i < index->entries.length; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; !error && i < index->entries.length; ++i) { 2 0 2
for (i = 0; !error && i < index->entries.length; ++i) { 1 1 2
3551 1 git_index_entry *entry = git_vector_get(&index->entries, i);
3552  
3553 /* check if path actually matches */
3554 1 ▶ if (!git_pathspec__match(
Branch: 1 branch
BranchTrueFalseTotal
if (!git_pathspec__match( 0 1 1
3555 1 &ps.pathspec, entry->path, false, (bool)index->ignore_case,
3556 &match, NULL))
3557 0 continue;
3558  
3559 /* issue notification callback if requested */
3560 1 ▶ if (cb && (error = cb(entry->path, match, payload)) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (cb && (error = cb(entry->path, match, payload)) != 0) { 1 0 1
if (cb && (error = cb(entry->path, match, payload)) != 0) { 0 1 1
3561 0 ▶ if (error > 0) { /* return > 0 means skip this one */
Branch: 1 branch
BranchTrueFalseTotal
if (error > 0) { /* return > 0 means skip this one */ 0 0 0
3562 0 error = 0;
3563 0 continue;
3564 }
3565 0 ▶ if (error < 0) /* return < 0 means abort */
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) /* return < 0 means abort */ 0 0 0
3566 0 break;
3567 }
3568  
3569 /* index manipulation may alter entry, so don't depend on it */
3570 1 ▶ if ((error = git_str_sets(&path, entry->path)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_sets(&path, entry->path)) < 0) 0 1 1
3571 0 break;
3572  
3573 1 switch (action) {
3574 0 case INDEX_ACTION_NONE:
3575 0 break;
3576 0 case INDEX_ACTION_UPDATE:
3577 0 error = git_index_add_bypath(index, path.ptr);
3578  
3579 0 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 0 0 0
3580 0 git_error_clear();
3581  
3582 0 error = git_index_remove_bypath(index, path.ptr);
3583  
3584 0 ▶ if (!error) /* back up foreach if we removed this */
Branch: 1 branch
BranchTrueFalseTotal
if (!error) /* back up foreach if we removed this */ 0 0 0
3585 0 i--;
3586 }
3587 0 break;
3588 1 case INDEX_ACTION_REMOVE:
3589 1 ▶ if (!(error = git_index_remove_bypath(index, path.ptr)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = git_index_remove_bypath(index, path.ptr))) 1 0 1
3590 1 i--; /* back up foreach if we removed this */
3591 1 break;
3592 0 default:
3593 0 git_error_set(GIT_ERROR_INVALID, "unknown index action %d", action);
3594 0 error = -1;
3595 0 break;
3596 }
3597 }
3598  
3599 1 git_str_dispose(&path);
3600 1 git_pathspec__clear(&ps);
3601  
3602 1 return error;
3603 }
3604  
3605 1 int git_index_remove_all(
3606 git_index *index,
3607 const git_strarray *pathspec,
3608 git_index_matched_path_cb cb,
3609 void *payload)
3610 {
3611 1 int error = index_apply_to_all(
3612 index, INDEX_ACTION_REMOVE, pathspec, cb, payload);
3613  
3614 1 ▶ if (error) /* make sure error is set if callback stopped iteration */
Branch: 1 branch
BranchTrueFalseTotal
if (error) /* make sure error is set if callback stopped iteration */ 0 1 1
3615 0 git_error_set_after_callback(error);
3616  
3617 1 return error;
3618 }
3619  
3620 4 int git_index_update_all(
3621 git_index *index,
3622 const git_strarray *pathspec,
3623 git_index_matched_path_cb cb,
3624 void *payload)
3625 {
3626 4 int error = index_apply_to_wd_diff(index, INDEX_ACTION_UPDATE, pathspec, 0, cb, payload);
3627 4 ▶ if (error) /* make sure error is set if callback stopped iteration */
Branch: 1 branch
BranchTrueFalseTotal
if (error) /* make sure error is set if callback stopped iteration */ 1 3 4
3628 1 git_error_set_after_callback(error);
3629  
3630 4 return error;
3631 }
3632  
3633 431 int git_index_snapshot_new(git_vector *snap, git_index *index)
3634 {
3635 int error;
3636  
3637 431 GIT_REFCOUNT_INC(index);
3638  
3639 431 git_atomic32_inc(&index->readers);
3640 431 git_vector_sort(&index->entries);
3641  
3642 431 error = git_vector_dup(snap, &index->entries, index->entries._cmp);
3643  
3644 431 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 431 431
3645 0 git_index_snapshot_release(snap, index);
3646  
3647 431 return error;
3648 }
3649  
3650 431 void git_index_snapshot_release(git_vector *snap, git_index *index)
3651 {
3652 431 git_vector_free(snap);
3653  
3654 431 git_atomic32_dec(&index->readers);
3655  
3656 431 git_index_free(index);
3657 431 }
3658  
3659 206 int git_index_snapshot_find(
3660 size_t *out, git_vector *entries, git_vector_cmp entry_srch,
3661 const char *path, size_t path_len, int stage)
3662 {
3663 206 return index_find_in_entries(out, entries, entry_srch, path, path_len, stage);
3664 }
3665  
3666 98 int git_indexwriter_init(
3667 git_indexwriter *writer,
3668 git_index *index)
3669 {
3670 int error;
3671  
3672 98 GIT_REFCOUNT_INC(index);
3673  
3674 98 writer->index = index;
3675  
3676 98 ▶ if (!index->index_file_path)
Branch: 1 branch
BranchTrueFalseTotal
if (!index->index_file_path) 0 98 98
3677 0 return create_index_error(-1,
3678 "failed to write index: The index is in-memory only");
3679  
3680 98 ▶ if ((error = git_filebuf_open(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_filebuf_open( 0 98 98
3681 98 &writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
3682  
3683 0 ▶ if (error == GIT_ELOCKED)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ELOCKED) 0 0 0
3684 0 git_error_set(GIT_ERROR_INDEX, "the index is locked; this might be due to a concurrent or crashed process");
3685  
3686 0 return error;
3687 }
3688  
3689 98 writer->should_write = 1;
3690  
3691 98 return 0;
3692 }
3693  
3694 15 int git_indexwriter_init_for_operation(
3695 git_indexwriter *writer,
3696 git_repository *repo,
3697 unsigned int *checkout_strategy)
3698 {
3699 git_index *index;
3700 int error;
3701  
3702 30 if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
3703 15 (error = git_indexwriter_init(writer, index)) < 0)
3704 0 return error;
3705  
3706 15 writer->should_write = (*checkout_strategy & GIT_CHECKOUT_DONT_WRITE_INDEX) == 0;
3707 15 *checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX;
3708  
3709 15 return 0;
3710 }
3711  
3712 97 int git_indexwriter_commit(git_indexwriter *writer)
3713 {
3714 unsigned char checksum[GIT_HASH_SHA1_SIZE];
3715 size_t checksum_size;
3716 int error;
3717  
3718 97 ▶ if (!writer->should_write)
Branch: 1 branch
BranchTrueFalseTotal
if (!writer->should_write) 0 97 97
3719 0 return 0;
3720  
3721 97 git_vector_sort(&writer->index->entries);
3722 97 git_vector_sort(&writer->index->reuc);
3723  
3724 97 ▶ if ((error = write_index(checksum, &checksum_size, writer->index, &writer->file)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = write_index(checksum, &checksum_size, writer->index, &writer->file)) < 0) { 0 97 97
3725 0 git_indexwriter_cleanup(writer);
3726 0 return error;
3727 }
3728  
3729 97 ▶ if ((error = git_filebuf_commit(&writer->file)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_filebuf_commit(&writer->file)) < 0) 0 97 97
3730 0 return error;
3731  
3732 97 ▶ if ((error = git_futils_filestamp_check(
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_futils_filestamp_check( 0 97 97
3733 97 &writer->index->stamp, writer->index->index_file_path)) < 0) {
3734 0 git_error_set(GIT_ERROR_OS, "could not read index timestamp");
3735 0 return -1;
3736 }
3737  
3738 97 writer->index->dirty = 0;
3739 97 writer->index->on_disk = 1;
3740 97 memcpy(writer->index->checksum, checksum, checksum_size);
3741  
3742 97 git_index_free(writer->index);
3743 97 writer->index = NULL;
3744  
3745 97 return 0;
3746 }
3747  
3748 98 void git_indexwriter_cleanup(git_indexwriter *writer)
3749 {
3750 98 git_filebuf_cleanup(&writer->file);
3751  
3752 98 git_index_free(writer->index);
3753 98 writer->index = NULL;
3754 98 }
3755  
3756 /* Deprecated functions */
3757  
3758 #ifndef GIT_DEPRECATE_HARD
3759 9 int git_index_add_frombuffer(
3760 git_index *index, const git_index_entry *source_entry,
3761 const void *buffer, size_t len)
3762 {
3763 9 return git_index_add_from_buffer(index, source_entry, buffer, len);
3764 }
3765 #endif
« deps/libgit2/src/libgit2/email.c ↑ Summary deps/libgit2/src/libgit2/rebase.c »