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