index.c 86.4 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7
 */

8 9
#include "index.h"

10 11
#include <stddef.h>

12
#include "repository.h"
13
#include "tree.h"
14
#include "tree-cache.h"
15
#include "hash.h"
16 17
#include "iterator.h"
#include "pathspec.h"
18
#include "ignore.h"
19
#include "blob.h"
20
#include "idxmap.h"
21
#include "diff.h"
David Turner committed
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#define INSERT_IN_MAP_EX(idx, map, e, err) do {				\
		if ((idx)->ignore_case)					\
			git_idxmap_icase_insert((khash_t(idxicase) *) (map), (e), (e), (err)); \
		else							\
			git_idxmap_insert((map), (e), (e), (err));	\
	} while (0)

#define INSERT_IN_MAP(idx, e, err) INSERT_IN_MAP_EX(idx, (idx)->entries_map, e, err)

#define LOOKUP_IN_MAP(p, idx, k) do {					\
		if ((idx)->ignore_case)					\
			(p) = git_idxmap_icase_lookup_index((khash_t(idxicase) *) index->entries_map, (k)); \
		else							\
			(p) = git_idxmap_lookup_index(index->entries_map, (k)); \
	} while (0)

#define DELETE_IN_MAP(idx, e) do {					\
		if ((idx)->ignore_case)					\
			git_idxmap_icase_delete((khash_t(idxicase) *) (idx)->entries_map, (e)); \
		else							\
			git_idxmap_delete((idx)->entries_map, (e));	\
	} while (0)

53 54 55 56
static int 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);

57 58 59 60 61
#define minimal_entry_size (offsetof(struct entry_short, path))

static const size_t INDEX_FOOTER_SIZE = GIT_OID_RAWSZ;
static const size_t INDEX_HEADER_SIZE = 12;

David Turner committed
62 63
static const unsigned int INDEX_VERSION_NUMBER_DEFAULT = 2;
static const unsigned int INDEX_VERSION_NUMBER_LB = 2;
64
static const unsigned int INDEX_VERSION_NUMBER_EXT = 3;
David Turner committed
65 66
static const unsigned int INDEX_VERSION_NUMBER_COMP = 4;
static const unsigned int INDEX_VERSION_NUMBER_UB = 4;
67 68 69

static const unsigned int INDEX_HEADER_SIG = 0x44495243;
static const char INDEX_EXT_TREECACHE_SIG[] = {'T', 'R', 'E', 'E'};
70
static const char INDEX_EXT_UNMERGED_SIG[] = {'R', 'E', 'U', 'C'};
Edward Thomson committed
71
static const char INDEX_EXT_CONFLICT_NAME_SIG[] = {'N', 'A', 'M', 'E'};
72

73 74
#define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx)))

75 76 77 78 79 80 81 82 83 84 85
struct index_header {
	uint32_t signature;
	uint32_t version;
	uint32_t entry_count;
};

struct index_extension {
	char signature[4];
	uint32_t extension_size;
};

86 87 88 89 90
struct entry_time {
	uint32_t seconds;
	uint32_t nanoseconds;
};

91
struct entry_short {
92 93
	struct entry_time ctime;
	struct entry_time mtime;
94 95 96 97 98 99 100 101
	uint32_t dev;
	uint32_t ino;
	uint32_t mode;
	uint32_t uid;
	uint32_t gid;
	uint32_t file_size;
	git_oid oid;
	uint16_t flags;
schu committed
102
	char path[1]; /* arbitrary length */
103 104 105
};

struct entry_long {
106 107
	struct entry_time ctime;
	struct entry_time mtime;
108 109 110 111 112 113 114 115 116
	uint32_t dev;
	uint32_t ino;
	uint32_t mode;
	uint32_t uid;
	uint32_t gid;
	uint32_t file_size;
	git_oid oid;
	uint16_t flags;
	uint16_t flags_extended;
schu committed
117
	char path[1]; /* arbitrary length */
118 119
};

Edward Thomson committed
120 121
struct entry_srch_key {
	const char *path;
122
	size_t pathlen;
Edward Thomson committed
123 124 125
	int stage;
};

126 127 128 129 130 131 132 133 134 135 136 137
struct entry_internal {
	git_index_entry entry;
	size_t pathlen;
	char path[GIT_FLEX_ARRAY];
};

struct reuc_entry_internal {
	git_index_reuc_entry entry;
	size_t pathlen;
	char path[GIT_FLEX_ARRAY];
};

138 139 140 141
/* local declarations */
static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size);
static int read_header(struct index_header *dest, const void *buffer);

142
static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
143
static bool is_index_extended(git_index *index);
144
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file);
145

146
static void index_entry_free(git_index_entry *entry);
Edward Thomson committed
147 148
static void index_entry_reuc_free(git_index_reuc_entry *reuc);

149
int git_index_entry_srch(const void *key, const void *array_member)
150
{
Edward Thomson committed
151
	const struct entry_srch_key *srch_key = key;
152
	const struct entry_internal *entry = array_member;
153 154
	int cmp;
	size_t len1, len2, len;
155

156 157
	len1 = srch_key->pathlen;
	len2 = entry->pathlen;
158
	len = len1 < len2 ? len1 : len2;
Edward Thomson committed
159

160 161 162 163 164 165 166
	cmp = memcmp(srch_key->path, entry->path, len);
	if (cmp)
		return cmp;
	if (len1 < len2)
		return -1;
	if (len1 > len2)
		return 1;
Edward Thomson committed
167

168
	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
169
		return srch_key->stage - GIT_IDXENTRY_STAGE(&entry->entry);
170 171

	return 0;
172 173
}

174
int git_index_entry_isrch(const void *key, const void *array_member)
175
{
Edward Thomson committed
176
	const struct entry_srch_key *srch_key = key;
177
	const struct entry_internal *entry = array_member;
178 179
	int cmp;
	size_t len1, len2, len;
Edward Thomson committed
180

181 182
	len1 = srch_key->pathlen;
	len2 = entry->pathlen;
183
	len = len1 < len2 ? len1 : len2;
Edward Thomson committed
184

185
	cmp = strncasecmp(srch_key->path, entry->path, len);
Edward Thomson committed
186

187 188 189 190 191 192 193 194
	if (cmp)
		return cmp;
	if (len1 < len2)
		return -1;
	if (len1 > len2)
		return 1;

	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
195
		return srch_key->stage - GIT_IDXENTRY_STAGE(&entry->entry);
196 197

	return 0;
Edward Thomson committed
198 199
}

200
static int index_entry_srch_path(const void *path, const void *array_member)
Edward Thomson committed
201
{
202 203
	const git_index_entry *entry = array_member;

Edward Thomson committed
204 205 206
	return strcmp((const char *)path, entry->path);
}

207
static int index_entry_isrch_path(const void *path, const void *array_member)
Edward Thomson committed
208 209 210 211
{
	const git_index_entry *entry = array_member;

	return strcasecmp((const char *)path, entry->path);
212 213
}

214
int git_index_entry_cmp(const void *a, const void *b)
215
{
Edward Thomson committed
216
	int diff;
217 218
	const git_index_entry *entry_a = a;
	const git_index_entry *entry_b = b;
219

Edward Thomson committed
220 221 222
	diff = strcmp(entry_a->path, entry_b->path);

	if (diff == 0)
223
		diff = (GIT_IDXENTRY_STAGE(entry_a) - GIT_IDXENTRY_STAGE(entry_b));
Edward Thomson committed
224 225

	return diff;
226 227
}

228
int git_index_entry_icmp(const void *a, const void *b)
229
{
Edward Thomson committed
230
	int diff;
231 232 233
	const git_index_entry *entry_a = a;
	const git_index_entry *entry_b = b;

Edward Thomson committed
234 235 236
	diff = strcasecmp(entry_a->path, entry_b->path);

	if (diff == 0)
237
		diff = (GIT_IDXENTRY_STAGE(entry_a) - GIT_IDXENTRY_STAGE(entry_b));
Edward Thomson committed
238 239 240 241

	return diff;
}

Edward Thomson committed
242 243 244 245
static int conflict_name_cmp(const void *a, const void *b)
{
	const git_index_name_entry *name_a = a;
	const git_index_name_entry *name_b = b;
246

Edward Thomson committed
247 248
	if (name_a->ancestor && !name_b->ancestor)
		return 1;
249

Edward Thomson committed
250 251
	if (!name_a->ancestor && name_b->ancestor)
		return -1;
252

Edward Thomson committed
253 254
	if (name_a->ancestor)
		return strcmp(name_a->ancestor, name_b->ancestor);
255

Edward Thomson committed
256 257
	if (!name_a->ours || !name_b->ours)
		return 0;
258

Edward Thomson committed
259 260 261
	return strcmp(name_a->ours, name_b->ours);
}

Vicent Marti committed
262 263 264
/**
 * TODO: enable this when resolving case insensitive conflicts
 */
265
#if 0
Edward Thomson committed
266 267 268 269
static int conflict_name_icmp(const void *a, const void *b)
{
	const git_index_name_entry *name_a = a;
	const git_index_name_entry *name_b = b;
270

Edward Thomson committed
271 272
	if (name_a->ancestor && !name_b->ancestor)
		return 1;
273

Edward Thomson committed
274 275
	if (!name_a->ancestor && name_b->ancestor)
		return -1;
276

Edward Thomson committed
277 278
	if (name_a->ancestor)
		return strcasecmp(name_a->ancestor, name_b->ancestor);
279

Edward Thomson committed
280 281
	if (!name_a->ours || !name_b->ours)
		return 0;
282

Edward Thomson committed
283 284
	return strcasecmp(name_a->ours, name_b->ours);
}
285
#endif
Edward Thomson committed
286

Edward Thomson committed
287 288 289 290 291
static int reuc_srch(const void *key, const void *array_member)
{
	const git_index_reuc_entry *reuc = array_member;

	return strcmp(key, reuc->path);
292 293
}

Edward Thomson committed
294
static int reuc_isrch(const void *key, const void *array_member)
295
{
Edward Thomson committed
296
	const git_index_reuc_entry *reuc = array_member;
297

Edward Thomson committed
298
	return strcasecmp(key, reuc->path);
299 300
}

Edward Thomson committed
301
static int reuc_cmp(const void *a, const void *b)
302
{
Edward Thomson committed
303 304
	const git_index_reuc_entry *info_a = a;
	const git_index_reuc_entry *info_b = b;
305 306 307

	return strcmp(info_a->path, info_b->path);
}
308

Edward Thomson committed
309 310 311 312 313 314 315 316
static int reuc_icmp(const void *a, const void *b)
{
	const git_index_reuc_entry *info_a = a;
	const git_index_reuc_entry *info_b = b;

	return strcasecmp(info_a->path, info_b->path);
}

317 318 319 320 321 322 323
static void index_entry_reuc_free(git_index_reuc_entry *reuc)
{
	git__free(reuc);
}

static void index_entry_free(git_index_entry *entry)
{
324 325 326
	if (!entry)
		return;

327
	memset(&entry->id, 0, sizeof(entry->id));
328 329 330
	git__free(entry);
}

331
unsigned int git_index__create_mode(unsigned int mode)
332 333 334
{
	if (S_ISLNK(mode))
		return S_IFLNK;
335

336 337
	if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR))
		return (S_IFLNK | S_IFDIR);
338

339
	return S_IFREG | GIT_PERMS_CANONICAL(mode);
340 341
}

342 343 344 345 346 347 348 349 350
static unsigned int index_merge_mode(
	git_index *index, git_index_entry *existing, unsigned int mode)
{
	if (index->no_symlinks && S_ISREG(mode) &&
		existing && S_ISLNK(existing->mode))
		return existing->mode;

	if (index->distrust_filemode && S_ISREG(mode))
		return (existing && S_ISREG(existing->mode)) ?
351
			existing->mode : git_index__create_mode(0666);
352

353
	return git_index__create_mode(mode);
354 355
}

356 357 358 359 360 361 362 363 364 365 366 367 368
GIT_INLINE(int) index_find_in_entries(
	size_t *out, git_vector *entries, git_vector_cmp entry_srch,
	const char *path, size_t path_len, int stage)
{
	struct entry_srch_key srch_key;
	srch_key.path = path;
	srch_key.pathlen = !path_len ? strlen(path) : path_len;
	srch_key.stage = stage;
	return git_vector_bsearch2(out, entries, entry_srch, &srch_key);
}

GIT_INLINE(int) index_find(
	size_t *out, git_index *index,
369
	const char *path, size_t path_len, int stage)
370
{
371
	git_vector_sort(&index->entries);
372 373 374 375 376

	return index_find_in_entries(
		out, &index->entries, index->entries_search, path, path_len, stage);
}

377
void git_index__set_ignore_case(git_index *index, bool ignore_case)
378
{
379 380
	index->ignore_case = ignore_case;

381 382 383 384 385 386 387 388 389 390 391
	if (ignore_case) {
		index->entries_cmp_path    = git__strcasecmp_cb;
		index->entries_search      = git_index_entry_isrch;
		index->entries_search_path = index_entry_isrch_path;
		index->reuc_search         = reuc_isrch;
	} else {
		index->entries_cmp_path    = git__strcmp_cb;
		index->entries_search      = git_index_entry_srch;
		index->entries_search_path = index_entry_srch_path;
		index->reuc_search         = reuc_srch;
	}
392

393 394
	git_vector_set_cmp(&index->entries,
		ignore_case ? git_index_entry_icmp : git_index_entry_cmp);
395
	git_vector_sort(&index->entries);
Edward Thomson committed
396

397
	git_vector_set_cmp(&index->reuc, ignore_case ? reuc_icmp : reuc_cmp);
Edward Thomson committed
398
	git_vector_sort(&index->reuc);
399 400
}

401
int git_index_open(git_index **index_out, const char *index_path)
402 403
{
	git_index *index;
404
	int error = -1;
405

406
	assert(index_out);
407

408 409
	index = git__calloc(1, sizeof(git_index));
	GITERR_CHECK_ALLOC(index);
410

411
	git_pool_init(&index->tree_pool, 1);
412

413 414
	if (index_path != NULL) {
		index->index_file_path = git__strdup(index_path);
415 416
		if (!index->index_file_path)
			goto fail;
417 418 419 420 421

		/* Check if index file is stored on disk already */
		if (git_path_exists(index->index_file_path) == true)
			index->on_disk = 1;
	}
422

423
	if (git_vector_init(&index->entries, 32, git_index_entry_cmp) < 0 ||
424
		git_idxmap_alloc(&index->entries_map) < 0 ||
425 426 427
		git_vector_init(&index->names, 8, conflict_name_cmp) < 0 ||
		git_vector_init(&index->reuc, 8, reuc_cmp) < 0 ||
		git_vector_init(&index->deleted, 8, git_index_entry_cmp) < 0)
428
		goto fail;
429

430 431 432
	index->entries_cmp_path = git__strcmp_cb;
	index->entries_search = git_index_entry_srch;
	index->entries_search_path = index_entry_srch_path;
Edward Thomson committed
433
	index->reuc_search = reuc_srch;
David Turner committed
434
	index->version = INDEX_VERSION_NUMBER_DEFAULT;
435

436 437
	if (index_path != NULL && (error = git_index_read(index, true)) < 0)
		goto fail;
438

Vicent Marti committed
439
	*index_out = index;
440
	GIT_REFCOUNT_INC(index);
441

442
	return 0;
443 444

fail:
445
	git_pool_clear(&index->tree_pool);
446 447
	git_index_free(index);
	return error;
448 449
}

450 451 452 453 454
int git_index_new(git_index **out)
{
	return git_index_open(out, NULL);
}

455
static void index_free(git_index *index)
456
{
457 458 459 460 461
	/* index iterators increment the refcount of the index, so if we
	 * get here then there should be no outstanding iterators.
	 */
	assert(!git_atomic_get(&index->readers));

462
	git_index_clear(index);
463
	git_idxmap_free(index->entries_map);
464
	git_vector_free(&index->entries);
Edward Thomson committed
465
	git_vector_free(&index->names);
Edward Thomson committed
466
	git_vector_free(&index->reuc);
467
	git_vector_free(&index->deleted);
468

469
	git__free(index->index_file_path);
470

471
	git__memzero(index, sizeof(*index));
472
	git__free(index);
473 474
}

475 476
void git_index_free(git_index *index)
{
477
	if (index == NULL)
478 479
		return;

480
	GIT_REFCOUNT_DEC(index, index_free);
481 482
}

483 484
/* call with locked index */
static void index_free_deleted(git_index *index)
485
{
486
	int readers = (int)git_atomic_get(&index->readers);
487
	size_t i;
488

489
	if (readers > 0 || !index->deleted.length)
490 491
		return;

492 493 494 495
	for (i = 0; i < index->deleted.length; ++i) {
		git_index_entry *ie = git__swap(index->deleted.contents[i], NULL);
		index_entry_free(ie);
	}
496 497 498 499

	git_vector_clear(&index->deleted);
}

500 501
/* call with locked index */
static int index_remove_entry(git_index *index, size_t pos)
502 503 504 505
{
	int error = 0;
	git_index_entry *entry = git_vector_get(&index->entries, pos);

506
	if (entry != NULL) {
507
		git_tree_cache_invalidate_path(index->tree, entry->path);
508 509
		DELETE_IN_MAP(index, entry);
	}
510 511 512 513

	error = git_vector_remove(&index->entries, pos);

	if (!error) {
514
		if (git_atomic_get(&index->readers) > 0) {
515
			error = git_vector_insert(&index->deleted, entry);
516
		} else {
517
			index_entry_free(entry);
518
		}
519 520 521
	}

	return error;
522
}
523

524
int git_index_clear(git_index *index)
525
{
526 527
	int error = 0;

528 529
	assert(index);

530
	index->tree = NULL;
531
	git_pool_clear(&index->tree_pool);
532

533
	git_idxmap_clear(index->entries_map);
534
	while (!error && index->entries.length > 0)
535
		error = index_remove_entry(index, index->entries.length - 1);
536 537
	index_free_deleted(index);

538
	git_index_reuc_clear(index);
Edward Thomson committed
539
	git_index_name_clear(index);
nulltoken committed
540

541
	git_futils_filestamp_set(&index->stamp, NULL);
542

543
	return error;
544 545
}

546 547
static int create_index_error(int error, const char *msg)
{
548
	giterr_set_str(GITERR_INDEX, msg);
549 550 551
	return error;
}

Russell Belfer committed
552
int git_index_set_caps(git_index *index, int caps)
553
{
Linquize committed
554
	unsigned int old_ignore_case;
555

556 557
	assert(index);

558 559
	old_ignore_case = index->ignore_case;

560
	if (caps == GIT_INDEXCAP_FROM_OWNER) {
561
		git_repository *repo = INDEX_OWNER(index);
562 563
		int val;

564 565
		if (!repo)
			return create_index_error(
566
				-1, "cannot access repository to set index caps");
567

568
		if (!git_repository__cvar(&val, repo, GIT_CVAR_IGNORECASE))
569
			index->ignore_case = (val != 0);
570
		if (!git_repository__cvar(&val, repo, GIT_CVAR_FILEMODE))
571
			index->distrust_filemode = (val == 0);
572
		if (!git_repository__cvar(&val, repo, GIT_CVAR_SYMLINKS))
573
			index->no_symlinks = (val == 0);
574 575 576 577 578 579 580
	}
	else {
		index->ignore_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0);
		index->distrust_filemode = ((caps & GIT_INDEXCAP_NO_FILEMODE) != 0);
		index->no_symlinks = ((caps & GIT_INDEXCAP_NO_SYMLINKS) != 0);
	}

581
	if (old_ignore_case != index->ignore_case) {
Linquize committed
582
		git_index__set_ignore_case(index, (bool)index->ignore_case);
583 584
	}

585 586 587
	return 0;
}

Russell Belfer committed
588
int git_index_caps(const git_index *index)
589 590 591 592 593 594
{
	return ((index->ignore_case ? GIT_INDEXCAP_IGNORE_CASE : 0) |
			(index->distrust_filemode ? GIT_INDEXCAP_NO_FILEMODE : 0) |
			(index->no_symlinks ? GIT_INDEXCAP_NO_SYMLINKS : 0));
}

595 596 597 598 599 600 601 602 603 604
const git_oid *git_index_checksum(git_index *index)
{
	return &index->checksum;
}

/**
 * Returns 1 for changed, 0 for not changed and <0 for errors
 */
static int compare_checksum(git_index *index)
{
605
	int fd;
606 607 608 609 610 611
	ssize_t bytes_read;
	git_oid checksum = {{ 0 }};

	if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0)
		return fd;

612
	if (p_lseek(fd, -20, SEEK_END) < 0) {
613 614 615 616 617 618 619 620 621 622 623 624 625 626
		p_close(fd);
		giterr_set(GITERR_OS, "failed to seek to end of file");
		return -1;
	}

	bytes_read = p_read(fd, &checksum, GIT_OID_RAWSZ);
	p_close(fd);

	if (bytes_read < 0)
		return -1;

	return !!git_oid_cmp(&checksum, &index->checksum);
}

627
int git_index_read(git_index *index, int force)
628
{
629
	int error = 0, updated;
630
	git_buf buffer = GIT_BUF_INIT;
631
	git_futils_filestamp stamp = index->stamp;
632

633 634
	if (!index->index_file_path)
		return create_index_error(-1,
635
			"failed to read index: The index is in-memory only");
636

637 638 639
	index->on_disk = git_path_exists(index->index_file_path);

	if (!index->on_disk) {
640
		if (force)
641
			return git_index_clear(index);
642
		return 0;
643 644
	}

645 646
	if ((updated = git_futils_filestamp_check(&stamp, index->index_file_path) < 0) ||
	    ((updated = compare_checksum(index)) < 0)) {
647 648
		giterr_set(
			GITERR_INDEX,
649
			"failed to read index: '%s' no longer exists",
650
			index->index_file_path);
651
		return updated;
652 653 654
	}
	if (!updated && !force)
		return 0;
655 656

	error = git_futils_readbuffer(&buffer, index->index_file_path);
657 658
	if (error < 0)
		return error;
659

660 661 662
	index->tree = NULL;
	git_pool_clear(&index->tree_pool);

663 664 665 666
	error = git_index_clear(index);

	if (!error)
		error = parse_index(index, buffer.ptr, buffer.size);
667

668 669
	if (!error)
		git_futils_filestamp_set(&index->stamp, &stamp);
670

671
	git_buf_free(&buffer);
672 673 674
	return error;
}

675
int git_index__changed_relative_to(
676
	git_index *index, const git_oid *checksum)
677 678 679 680 681
{
	/* attempt to update index (ignoring errors) */
	if (git_index_read(index, false) < 0)
		giterr_clear();

682
	return !!git_oid_cmp(&index->checksum, checksum);
683 684
}

685
static bool is_racy_entry(git_index *index, const git_index_entry *entry)
686 687 688 689 690
{
	/* Git special-cases submodules in the check */
	if (S_ISGITLINK(entry->mode))
		return false;

691
	return git_index_entry_newer_than_index(entry, index);
692 693
}

694 695 696 697
/*
 * Force the next diff to take a look at those entries which have the
 * same timestamp as the current index.
 */
698
static int truncate_racily_clean(git_index *index)
699 700
{
	size_t i;
701
	int error;
702
	git_index_entry *entry;
703
	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
704 705 706
	git_diff *diff = NULL;
	git_vector paths = GIT_VECTOR_INIT;
	git_diff_delta *delta;
707 708 709 710

	/* Nothing to do if there's no repo to talk about */
	if (!INDEX_OWNER(index))
		return 0;
711

712 713 714 715 716
	/* If there's no workdir, we can't know where to even check */
	if (!git_repository_workdir(INDEX_OWNER(index)))
		return 0;

	diff_opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_IGNORE_SUBMODULES | GIT_DIFF_DISABLE_PATHSPEC_MATCH;
717
	git_vector_foreach(&index->entries, i, entry) {
718
		if ((entry->flags_extended & GIT_IDXENTRY_UPTODATE) == 0 &&
719
			is_racy_entry(index, entry))
720 721
			git_vector_insert(&paths, (char *)entry->path);
	}
722

723 724
	if (paths.length == 0)
		goto done;
725

726 727
	diff_opts.pathspec.count = paths.length;
	diff_opts.pathspec.strings = (char **)paths.contents;
728

729 730
	if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0)
		return error;
731

732 733 734 735 736 737 738 739
	git_vector_foreach(&diff->deltas, i, delta) {
		entry = (git_index_entry *)git_index_get_bypath(index, delta->old_file.path, 0);

		/* Ensure that we have a stage 0 for this file (ie, it's not a
		 * conflict), otherwise smudging it is quite pointless.
		 */
		if (entry)
			entry->file_size = 0;
740
	}
741

742 743 744
done:
	git_diff_free(diff);
	git_vector_free(&paths);
745
	return 0;
746 747
}

David Turner committed
748 749 750 751 752 753 754 755 756 757 758 759 760
unsigned git_index_version(git_index *index)
{
	assert(index);

	return index->version;
}

int git_index_set_version(git_index *index, unsigned int version)
{
	assert(index);

	if (version < INDEX_VERSION_NUMBER_LB ||
	    version > INDEX_VERSION_NUMBER_UB) {
761
		giterr_set(GITERR_INDEX, "invalid version number");
David Turner committed
762 763 764 765 766 767 768 769
		return -1;
	}

	index->version = version;

	return 0;
}

770 771
int git_index_write(git_index *index)
{
772
	git_indexwriter writer = GIT_INDEXWRITER_INIT;
773
	int error;
774

775 776
	truncate_racily_clean(index);

777 778
	if ((error = git_indexwriter_init(&writer, index)) == 0)
		error = git_indexwriter_commit(&writer);
779

780
	git_indexwriter_cleanup(&writer);
781

782
	return error;
783
}
784

Jacques Germishuys committed
785
const char * git_index_path(const git_index *index)
786 787 788 789
{
	assert(index);
	return index->index_file_path;
}
790

791 792 793 794 795 796
int git_index_write_tree(git_oid *oid, git_index *index)
{
	git_repository *repo;

	assert(oid && index);

797
	repo = INDEX_OWNER(index);
798

799 800
	if (repo == NULL)
		return create_index_error(-1, "Failed to write tree. "
801
		  "the index file is not backed up by an existing repository");
802 803 804 805

	return git_tree__write_index(oid, index, repo);
}

806 807
int git_index_write_tree_to(
	git_oid *oid, git_index *index, git_repository *repo)
808 809 810 811 812
{
	assert(oid && index && repo);
	return git_tree__write_index(oid, index, repo);
}

813
size_t git_index_entrycount(const git_index *index)
814 815
{
	assert(index);
816
	return index->entries.length;
817 818
}

819 820
const git_index_entry *git_index_get_byindex(
	git_index *index, size_t n)
821 822
{
	assert(index);
823
	git_vector_sort(&index->entries);
Edward Thomson committed
824
	return git_vector_get(&index->entries, n);
825 826
}

827 828
const git_index_entry *git_index_get_bypath(
	git_index *index, const char *path, int stage)
829
{
830 831
	khiter_t pos;
	git_index_entry key = {{ 0 }};
Edward Thomson committed
832 833 834

	assert(index);

835 836 837
	key.path = path;
	GIT_IDXENTRY_STAGE_SET(&key, stage);

838
	LOOKUP_IN_MAP(pos, index, &key);
Edward Thomson committed
839

840 841 842
	if (git_idxmap_valid_index(index->entries_map, pos))
		return git_idxmap_value_at(index->entries_map, pos);

843
	giterr_set(GITERR_INDEX, "index does not contain '%s'", path);
844
	return NULL;
845 846
}

847 848
void git_index_entry__init_from_stat(
	git_index_entry *entry, struct stat *st, bool trust_mode)
849
{
850 851
	entry->ctime.seconds = (int32_t)st->st_ctime;
	entry->mtime.seconds = (int32_t)st->st_mtime;
852
#if defined(GIT_USE_NSEC)
853 854
	entry->mtime.nanoseconds = st->st_mtime_nsec;
	entry->ctime.nanoseconds = st->st_ctime_nsec;
855
#endif
856 857
	entry->dev  = st->st_rdev;
	entry->ino  = st->st_ino;
858
	entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ?
859
		git_index__create_mode(0666) : git_index__create_mode(st->st_mode);
860 861
	entry->uid  = st->st_uid;
	entry->gid  = st->st_gid;
862
	entry->file_size = (uint32_t)st->st_size;
863 864
}

865 866 867 868 869 870 871 872 873 874 875 876
static void index_entry_adjust_namemask(
		git_index_entry *entry,
		size_t path_length)
{
	entry->flags &= ~GIT_IDXENTRY_NAMEMASK;

	if (path_length < GIT_IDXENTRY_NAMEMASK)
		entry->flags |= path_length & GIT_IDXENTRY_NAMEMASK;
	else
		entry->flags |= GIT_IDXENTRY_NAMEMASK;
}

877 878 879 880 881 882
/* When `from_workdir` is true, we will validate the paths to avoid placing
 * paths that are invalid for the working directory on the current filesystem
 * (eg, on Windows, we will disallow `GIT~1`, `AUX`, `COM1`, etc).  This
 * function will *always* prevent `.git` and directory traversal `../` from
 * being added to the index.
 */
883 884 885
static int index_entry_create(
	git_index_entry **out,
	git_repository *repo,
886
	const char *path,
887
	struct stat *st,
888
	bool from_workdir)
889
{
890
	size_t pathlen = strlen(path), alloclen;
891
	struct entry_internal *entry;
892
	unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS;
893
	uint16_t mode = 0;
894

895 896 897 898 899 900
	/* always reject placing `.git` in the index and directory traversal.
	 * when requested, disallow platform-specific filenames and upgrade to
	 * the platform-specific `.git` tests (eg, `git~1`, etc).
	 */
	if (from_workdir)
		path_valid_flags |= GIT_PATH_REJECT_WORKDIR_DEFAULTS;
901 902
	if (st)
		mode = st->st_mode;
903

904
	if (!git_path_isvalid(repo, path, mode, path_valid_flags)) {
905
		giterr_set(GITERR_INDEX, "invalid path: '%s'", path);
906
		return -1;
907
	}
908

909 910 911
	GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(struct entry_internal), pathlen);
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
	entry = git__calloc(1, alloclen);
912
	GITERR_CHECK_ALLOC(entry);
913 914 915 916 917

	entry->pathlen = pathlen;
	memcpy(entry->path, path, pathlen);
	entry->entry.path = entry->path;

918 919
	*out = (git_index_entry *)entry;
	return 0;
920 921
}

922
static int index_entry_init(
923 924 925
	git_index_entry **entry_out,
	git_index *index,
	const char *rel_path)
926
{
927
	int error = 0;
928
	git_index_entry *entry = NULL;
929
	git_buf path = GIT_BUF_INIT;
930 931
	struct stat st;
	git_oid oid;
932
	git_repository *repo;
933

934 935
	if (INDEX_OWNER(index) == NULL)
		return create_index_error(-1,
936
			"could not initialize index entry. "
937 938
			"Index is not backed up by an existing repository.");

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
	/*
	 * FIXME: this is duplicated with the work in
	 * git_blob__create_from_paths. It should accept an optional stat
	 * structure so we can pass in the one we have to do here.
	 */
	repo = INDEX_OWNER(index);
	if (git_repository__ensure_not_bare(repo, "create blob from file") < 0)
		return GIT_EBAREREPO;

	if (git_buf_joinpath(&path, git_repository_workdir(repo), rel_path) < 0)
		return -1;

	error = git_path_lstat(path.ptr, &st);
	git_buf_free(&path);

	if (error < 0)
		return error;

	if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, &st, true) < 0)
958 959
		return -1;

960 961 962
	/* write the blob to disk and get the oid and stat info */
	error = git_blob__create_from_paths(
		&oid, &st, INDEX_OWNER(index), NULL, rel_path, 0, true);
963

964 965 966 967
	if (error < 0) {
		index_entry_free(entry);
		return error;
	}
968

969
	entry->id = oid;
970
	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
971

972
	*entry_out = (git_index_entry *)entry;
973
	return 0;
974 975
}

976 977
static git_index_reuc_entry *reuc_entry_alloc(const char *path)
{
978
	size_t pathlen = strlen(path),
979 980
		structlen = sizeof(struct reuc_entry_internal),
		alloclen;
981 982
	struct reuc_entry_internal *entry;

983 984
	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, structlen, pathlen) ||
		GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1))
985 986
		return NULL;

987
	entry = git__calloc(1, alloclen);
988 989 990 991 992 993 994 995 996 997
	if (!entry)
		return NULL;

	entry->pathlen = pathlen;
	memcpy(entry->path, path, pathlen);
	entry->entry.path = entry->path;

	return (git_index_reuc_entry *)entry;
}

Edward Thomson committed
998 999
static int index_entry_reuc_init(git_index_reuc_entry **reuc_out,
	const char *path,
Edward Thomson committed
1000 1001 1002
	int ancestor_mode, const git_oid *ancestor_oid,
	int our_mode, const git_oid *our_oid,
	int their_mode, const git_oid *their_oid)
Edward Thomson committed
1003 1004 1005 1006 1007
{
	git_index_reuc_entry *reuc = NULL;

	assert(reuc_out && path);

1008
	*reuc_out = reuc = reuc_entry_alloc(path);
Edward Thomson committed
1009 1010
	GITERR_CHECK_ALLOC(reuc);

1011 1012
	if ((reuc->mode[0] = ancestor_mode) > 0) {
		assert(ancestor_oid);
1013
		git_oid_cpy(&reuc->oid[0], ancestor_oid);
1014
	}
Edward Thomson committed
1015

1016 1017
	if ((reuc->mode[1] = our_mode) > 0) {
		assert(our_oid);
1018
		git_oid_cpy(&reuc->oid[1], our_oid);
1019
	}
Edward Thomson committed
1020

1021 1022
	if ((reuc->mode[2] = their_mode) > 0) {
		assert(their_oid);
1023
		git_oid_cpy(&reuc->oid[2], their_oid);
1024
	}
Edward Thomson committed
1025 1026 1027 1028

	return 0;
}

1029 1030
static void index_entry_cpy(
	git_index_entry *tgt,
1031
	const git_index_entry *src)
1032
{
1033
	const char *tgt_path = tgt->path;
1034
	memcpy(tgt, src, sizeof(*tgt));
1035
	tgt->path = tgt_path;
1036 1037
}

1038 1039
static int index_entry_dup(
	git_index_entry **out,
1040
	git_index *index,
1041
	const git_index_entry *src)
1042
{
1043
	if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0)
1044
		return -1;
1045

1046 1047 1048
	index_entry_cpy(*out, src);
	return 0;
}
1049

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
static void index_entry_cpy_nocache(
	git_index_entry *tgt,
	const git_index_entry *src)
{
	git_oid_cpy(&tgt->id, &src->id);
	tgt->mode = src->mode;
	tgt->flags = src->flags;
	tgt->flags_extended = (src->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS);
}

static int index_entry_dup_nocache(
	git_index_entry **out,
	git_index *index,
	const git_index_entry *src)
{
1065
	if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0)
1066
		return -1;
1067

1068
	index_entry_cpy_nocache(*out, src);
1069
	return 0;
1070 1071
}

1072
static int has_file_name(git_index *index,
1073
	 const git_index_entry *entry, size_t pos, int ok_to_replace)
1074 1075
{
	int retval = 0;
1076 1077 1078
	size_t len = strlen(entry->path);
	int stage = GIT_IDXENTRY_STAGE(entry);
	const char *name = entry->path;
1079

1080
	while (pos < index->entries.length) {
1081
		struct entry_internal *p = index->entries.contents[pos++];
1082

1083
		if (len >= p->pathlen)
1084
			break;
1085
		if (memcmp(name, p->path, len))
1086
			break;
1087
		if (GIT_IDXENTRY_STAGE(&p->entry) != stage)
1088
			continue;
1089
		if (p->path[len] != '/')
1090 1091 1092 1093
			continue;
		retval = -1;
		if (!ok_to_replace)
			break;
1094

1095
		if (index_remove_entry(index, --pos) < 0)
1096
			break;
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	}
	return retval;
}

/*
 * Do we have another file with a pathname that is a proper
 * subset of the name we're trying to add?
 */
static int has_dir_name(git_index *index,
		const git_index_entry *entry, int ok_to_replace)
{
	int retval = 0;
1109 1110 1111
	int stage = GIT_IDXENTRY_STAGE(entry);
	const char *name = entry->path;
	const char *slash = name + strlen(name);
1112 1113

	for (;;) {
1114
		size_t len, pos;
1115 1116 1117 1118

		for (;;) {
			if (*--slash == '/')
				break;
1119
			if (slash <= entry->path)
1120 1121 1122 1123
				return retval;
		}
		len = slash - name;

1124
		if (!index_find(&pos, index, name, len, stage)) {
1125 1126 1127 1128
			retval = -1;
			if (!ok_to_replace)
				break;

1129
			if (index_remove_entry(index, pos) < 0)
1130
				break;
1131
			continue;
1132 1133 1134 1135 1136 1137 1138
		}

		/*
		 * Trivial optimization: if we find an entry that
		 * already matches the sub-directory, then we know
		 * we're ok, and we can exit.
		 */
1139 1140
		for (; pos < index->entries.length; ++pos) {
			struct entry_internal *p = index->entries.contents[pos];
1141

1142 1143
			if (p->pathlen <= len ||
			    p->path[len] != '/' ||
1144
			    memcmp(p->path, name, len))
1145
				break; /* not our subdirectory */
1146

1147
			if (GIT_IDXENTRY_STAGE(&p->entry) == stage)
1148 1149 1150
				return retval;
		}
	}
1151

1152 1153
	return retval;
}
1154

1155
static int check_file_directory_collision(git_index *index,
1156 1157 1158 1159 1160 1161
		git_index_entry *entry, size_t pos, int ok_to_replace)
{
	int retval = has_file_name(index, entry, pos, ok_to_replace);
	retval = retval + has_dir_name(index, entry, ok_to_replace);

	if (retval) {
1162 1163
		giterr_set(GITERR_INDEX,
			"'%s' appears as both a file and a directory", entry->path);
1164 1165 1166 1167 1168
		return -1;
	}

	return 0;
}
1169

1170
static int canonicalize_directory_path(
1171 1172 1173
	git_index *index,
	git_index_entry *entry,
	git_index_entry *existing)
1174 1175 1176 1177 1178 1179 1180 1181 1182
{
	const git_index_entry *match, *best = NULL;
	char *search, *sep;
	size_t pos, search_len, best_len;

	if (!index->ignore_case)
		return 0;

	/* item already exists in the index, simply re-use the existing case */
1183 1184
	if (existing) {
		memcpy((char *)entry->path, existing->path, strlen(existing->path));
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
		return 0;
	}

	/* nothing to do */
	if (strchr(entry->path, '/') == NULL)
		return 0;

	if ((search = git__strdup(entry->path)) == NULL)
		return -1;

	/* starting at the parent directory and descending to the root, find the
	 * common parent directory.
	 */
	while (!best && (sep = strrchr(search, '/'))) {
		sep[1] = '\0';

		search_len = strlen(search);

		git_vector_bsearch2(
			&pos, &index->entries, index->entries_search_path, search);

		while ((match = git_vector_get(&index->entries, pos))) {
			if (GIT_IDXENTRY_STAGE(match) != 0) {
				/* conflicts do not contribute to canonical paths */
1209
			} else if (strncmp(search, match->path, search_len) == 0) {
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
				/* prefer an exact match to the input filename */
				best = match;
				best_len = search_len;
				break;
			} else if (strncasecmp(search, match->path, search_len) == 0) {
				/* continue walking, there may be a path with an exact
				 * (case sensitive) match later in the index, but use this
				 * as the best match until that happens.
				 */
				if (!best) {
					best = match;
					best_len = search_len;
				}
			} else {
				break;
			}

			pos++;
		}

		sep[0] = '\0';
	}

	if (best)
		memcpy((char *)entry->path, best->path, best_len);

	git__free(search);
	return 0;
}

1240 1241 1242 1243 1244 1245 1246 1247 1248
static int index_no_dups(void **old, void *new)
{
	const git_index_entry *entry = new;
	GIT_UNUSED(old);
	giterr_set(GITERR_INDEX, "'%s' appears multiple times at stage %d",
		entry->path, GIT_IDXENTRY_STAGE(entry));
	return GIT_EEXISTS;
}

1249
static void index_existing_and_best(
1250
	git_index_entry **existing,
1251
	size_t *existing_position,
1252
	git_index_entry **best,
1253 1254 1255
	git_index *index,
	const git_index_entry *entry)
{
1256
	git_index_entry *e;
1257 1258 1259 1260
	size_t pos;
	int error;

	error = index_find(&pos,
1261
		index, entry->path, 0, GIT_IDXENTRY_STAGE(entry));
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294

	if (error == 0) {
		*existing = index->entries.contents[pos];
		*existing_position = pos;
		*best = index->entries.contents[pos];
		return;
	}

	*existing = NULL;
	*existing_position = 0;
	*best = NULL;

	if (GIT_IDXENTRY_STAGE(entry) == 0) {
		for (; pos < index->entries.length; pos++) {
			int (*strcomp)(const char *a, const char *b) =
				index->ignore_case ? git__strcasecmp : git__strcmp;

			e = index->entries.contents[pos];

			if (strcomp(entry->path, e->path) != 0)
				break;

			if (GIT_IDXENTRY_STAGE(e) == GIT_INDEX_STAGE_ANCESTOR) {
				*best = e;
				continue;
			} else {
				*best = e;
				break;
			}
		}
	}
}

1295 1296 1297 1298
/* index_insert takes ownership of the new entry - if it can't insert
 * it, then it will return an error **and also free the entry**.  When
 * it replaces an existing entry, it will update the entry_ptr with the
 * actual entry in the index (and free the passed in one).
1299
 *
1300 1301 1302
 * trust_path is whether we use the given path, or whether (on case
 * insensitive systems only) we try to canonicalize the given path to
 * be within an existing directory.
1303
 *
1304
 * trust_mode is whether we trust the mode in entry_ptr.
1305 1306
 *
 * trust_id is whether we trust the id or it should be validated.
1307 1308
 */
static int index_insert(
1309 1310 1311 1312
	git_index *index,
	git_index_entry **entry_ptr,
	int replace,
	bool trust_path,
1313 1314
	bool trust_mode,
	bool trust_id)
1315
{
1316
	int error = 0;
1317
	size_t path_length, position;
1318
	git_index_entry *existing, *best, *entry;
1319

1320 1321 1322
	assert(index && entry_ptr);

	entry = *entry_ptr;
1323

1324
	/* make sure that the path length flag is correct */
1325
	path_length = ((struct entry_internal *)entry)->pathlen;
1326
	index_entry_adjust_namemask(entry, path_length);
1327

1328 1329 1330
	/* this entry is now up-to-date and should not be checked for raciness */
	entry->flags_extended |= GIT_IDXENTRY_UPTODATE;

1331 1332
	git_vector_sort(&index->entries);

1333 1334 1335 1336 1337 1338 1339 1340 1341
	/* look if an entry with this path already exists, either staged, or (if
	 * this entry is a regular staged item) as the "ours" side of a conflict.
	 */
	index_existing_and_best(&existing, &position, &best, index, entry);

	/* update the file mode */
	entry->mode = trust_mode ?
		git_index__create_mode(entry->mode) :
		index_merge_mode(index, best, entry->mode);
1342

1343 1344
	/* canonicalize the directory name */
	if (!trust_path)
1345
		error = canonicalize_directory_path(index, entry, best);
1346

1347 1348 1349 1350 1351 1352 1353 1354 1355
	/* ensure that the given id exists (unless it's a submodule) */
	if (!error && !trust_id && INDEX_OWNER(index) &&
		(entry->mode & GIT_FILEMODE_COMMIT) != GIT_FILEMODE_COMMIT) {

		if (!git_object__is_valid(INDEX_OWNER(index), &entry->id,
			git_object__type_from_filemode(entry->mode)))
			error = -1;
	}

1356
	/* look for tree / blob name collisions, removing conflicts if requested */
1357 1358 1359
	if (!error)
		error = check_file_directory_collision(index, entry, position, replace);

1360
	if (error < 0)
1361
		/* skip changes */;
1362 1363 1364 1365

	/* if we are replacing an existing item, overwrite the existing entry
	 * and return it in place of the passed in one.
	 */
1366
	else if (existing) {
1367 1368 1369 1370 1371 1372 1373
		if (replace) {
			index_entry_cpy(existing, entry);

			if (trust_path)
				memcpy((char *)existing->path, entry->path, strlen(entry->path));
		}

1374
		index_entry_free(entry);
1375
		*entry_ptr = entry = existing;
1376
	}
1377
	else {
1378 1379 1380
		/* if replace is not requested or no existing entry exists, insert
		 * at the sorted position.  (Since we re-sort after each insert to
		 * check for dups, this is actually cheaper in the long run.)
1381
		 */
1382
		error = git_vector_insert_sorted(&index->entries, entry, index_no_dups);
1383 1384

		if (error == 0) {
1385
			INSERT_IN_MAP(index, entry, &error);
1386
		}
1387
	}
1388

1389 1390 1391 1392
	if (error < 0) {
		index_entry_free(*entry_ptr);
		*entry_ptr = NULL;
	}
1393

1394
	return error;
1395 1396
}

Edward Thomson committed
1397
static int index_conflict_to_reuc(git_index *index, const char *path)
1398
{
1399
	const git_index_entry *conflict_entries[3];
Edward Thomson committed
1400
	int ancestor_mode, our_mode, their_mode;
Edward Thomson committed
1401
	git_oid const *ancestor_oid, *our_oid, *their_oid;
1402
	int ret;
1403

Edward Thomson committed
1404 1405
	if ((ret = git_index_conflict_get(&conflict_entries[0],
		&conflict_entries[1], &conflict_entries[2], index, path)) < 0)
1406
		return ret;
1407

Edward Thomson committed
1408 1409 1410
	ancestor_mode = conflict_entries[0] == NULL ? 0 : conflict_entries[0]->mode;
	our_mode = conflict_entries[1] == NULL ? 0 : conflict_entries[1]->mode;
	their_mode = conflict_entries[2] == NULL ? 0 : conflict_entries[2]->mode;
1411

1412 1413 1414
	ancestor_oid = conflict_entries[0] == NULL ? NULL : &conflict_entries[0]->id;
	our_oid = conflict_entries[1] == NULL ? NULL : &conflict_entries[1]->id;
	their_oid = conflict_entries[2] == NULL ? NULL : &conflict_entries[2]->id;
Edward Thomson committed
1415 1416 1417 1418 1419 1420

	if ((ret = git_index_reuc_add(index, path, ancestor_mode, ancestor_oid,
		our_mode, our_oid, their_mode, their_oid)) >= 0)
		ret = git_index_conflict_remove(index, path);

	return ret;
1421 1422
}

1423
GIT_INLINE(bool) is_file_or_link(const int filemode)
1424 1425 1426
{
	return (filemode == GIT_FILEMODE_BLOB ||
		filemode == GIT_FILEMODE_BLOB_EXECUTABLE ||
1427 1428 1429 1430 1431 1432
		filemode == GIT_FILEMODE_LINK);
}

GIT_INLINE(bool) valid_filemode(const int filemode)
{
	return (is_file_or_link(filemode) || filemode == GIT_FILEMODE_COMMIT);
1433 1434 1435
}

int git_index_add_frombuffer(
1436
    git_index *index, const git_index_entry *source_entry,
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
    const void *buffer, size_t len)
{
	git_index_entry *entry = NULL;
	int error = 0;
	git_oid id;

	assert(index && source_entry->path);

	if (INDEX_OWNER(index) == NULL)
		return create_index_error(-1,
1447
			"could not initialize index entry. "
1448 1449
			"Index is not backed up by an existing repository.");

1450
	if (!is_file_or_link(source_entry->mode)) {
1451 1452 1453 1454
		giterr_set(GITERR_INDEX, "invalid filemode");
		return -1;
	}

1455
	if (index_entry_dup(&entry, index, source_entry) < 0)
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
		return -1;

	error = git_blob_create_frombuffer(&id, INDEX_OWNER(index), buffer, len);
	if (error < 0) {
		index_entry_free(entry);
		return error;
	}

	git_oid_cpy(&entry->id, &id);
	entry->file_size = len;

1467
	if ((error = index_insert(index, &entry, 1, true, true, true)) < 0)
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
		return error;

	/* Adding implies conflict was resolved, move conflict entries to REUC */
	if ((error = index_conflict_to_reuc(index, entry->path)) < 0 && error != GIT_ENOTFOUND)
		return error;

	git_tree_cache_invalidate_path(index->tree, entry->path);
	return 0;
}

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
static int add_repo_as_submodule(git_index_entry **out, git_index *index, const char *path)
{
	git_repository *sub;
	git_buf abspath = GIT_BUF_INIT;
	git_repository *repo = INDEX_OWNER(index);
	git_reference *head;
	git_index_entry *entry;
	struct stat st;
	int error;

	if ((error = git_buf_joinpath(&abspath, git_repository_workdir(repo), path)) < 0)
		return error;

	if ((error = p_stat(abspath.ptr, &st)) < 0) {
		giterr_set(GITERR_OS, "failed to stat repository dir");
		return -1;
	}

1496 1497 1498
	if (index_entry_create(&entry, INDEX_OWNER(index), path, &st, true) < 0)
		return -1;

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);

	if ((error = git_repository_open(&sub, abspath.ptr)) < 0)
		return error;

	if ((error = git_repository_head(&head, sub)) < 0)
		return error;

	git_oid_cpy(&entry->id, git_reference_target(head));
	entry->mode = GIT_FILEMODE_COMMIT;

	git_reference_free(head);
	git_repository_free(sub);
	git_buf_free(&abspath);

	*out = entry;
	return 0;
}
1517

1518
int git_index_add_bypath(git_index *index, const char *path)
1519
{
Edward Thomson committed
1520 1521 1522 1523 1524
	git_index_entry *entry = NULL;
	int ret;

	assert(index && path);

1525
	if ((ret = index_entry_init(&entry, index, path)) == 0)
1526
		ret = index_insert(index, &entry, 1, false, false, true);
1527 1528 1529 1530 1531 1532 1533 1534 1535

	/* If we were given a directory, let's see if it's a submodule */
	if (ret < 0 && ret != GIT_EDIRECTORY)
		return ret;

	if (ret == GIT_EDIRECTORY) {
		git_submodule *sm;
		git_error_state err;

1536
		giterr_state_capture(&err, ret);
1537 1538 1539

		ret = git_submodule_lookup(&sm, INDEX_OWNER(index), path);
		if (ret == GIT_ENOTFOUND)
1540
			return giterr_state_restore(&err);
1541

1542
		giterr_state_free(&err);
1543 1544 1545 1546 1547 1548 1549 1550 1551

		/*
		 * EEXISTS means that there is a repository at that path, but it's not known
		 * as a submodule. We add its HEAD as an entry and don't register it.
		 */
		if (ret == GIT_EEXISTS) {
			if ((ret = add_repo_as_submodule(&entry, index, path)) < 0)
				return ret;

1552
			if ((ret = index_insert(index, &entry, 1, false, false, true)) < 0)
1553 1554 1555 1556 1557 1558 1559 1560
				return ret;
		} else if (ret < 0) {
			return ret;
		} else {
			ret = git_submodule_add_to_index(sm, false);
			git_submodule_free(sm);
			return ret;
		}
1561
	}
Edward Thomson committed
1562 1563 1564

	/* Adding implies conflict was resolved, move conflict entries to REUC */
	if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND)
1565
		return ret;
Edward Thomson committed
1566 1567 1568

	git_tree_cache_invalidate_path(index->tree, entry->path);
	return 0;
1569 1570
}

1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
int git_index_remove_bypath(git_index *index, const char *path)
{
	int ret;

	assert(index && path);

	if (((ret = git_index_remove(index, path, 0)) < 0 &&
		ret != GIT_ENOTFOUND) ||
		((ret = index_conflict_to_reuc(index, path)) < 0 &&
		ret != GIT_ENOTFOUND))
		return ret;

1583 1584 1585
	if (ret == GIT_ENOTFOUND)
		giterr_clear();

1586 1587 1588
	return 0;
}

1589 1590 1591 1592 1593 1594 1595 1596
int git_index__fill(git_index *index, const git_vector *source_entries)
{
	const git_index_entry *source_entry = NULL;
	size_t i;
	int ret = 0;

	assert(index);

1597 1598 1599
	if (!source_entries->length)
		return 0;

1600
	git_vector_size_hint(&index->entries, source_entries->length);
1601
	git_idxmap_resize(index->entries_map, (khint_t)(source_entries->length * 1.3));
1602

1603 1604 1605 1606 1607 1608
	git_vector_foreach(source_entries, i, source_entry) {
		git_index_entry *entry = NULL;

		if ((ret = index_entry_dup(&entry, index, source_entry)) < 0)
			break;

1609
		index_entry_adjust_namemask(entry, ((struct entry_internal *)entry)->pathlen);
1610
		entry->flags_extended |= GIT_IDXENTRY_UPTODATE;
1611
		entry->mode = git_index__create_mode(entry->mode);
1612

1613
		if ((ret = git_vector_insert(&index->entries, entry)) < 0)
1614 1615
			break;

1616
		INSERT_IN_MAP(index, entry, &ret);
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
		if (ret < 0)
			break;
	}

	if (!ret)
		git_vector_sort(&index->entries);

	return ret;
}

1627

Edward Thomson committed
1628
int git_index_add(git_index *index, const git_index_entry *source_entry)
1629 1630 1631 1632
{
	git_index_entry *entry = NULL;
	int ret;

1633
	assert(index && source_entry && source_entry->path);
1634

1635
	if (!valid_filemode(source_entry->mode)) {
1636
		giterr_set(GITERR_INDEX, "invalid entry mode");
1637 1638 1639
		return -1;
	}

1640
	if ((ret = index_entry_dup(&entry, index, source_entry)) < 0 ||
1641
		(ret = index_insert(index, &entry, 1, true, true, false)) < 0)
1642
		return ret;
1643

1644
	git_tree_cache_invalidate_path(index->tree, entry->path);
1645
	return 0;
1646 1647
}

Edward Thomson committed
1648
int git_index_remove(git_index *index, const char *path, int stage)
1649
{
1650
	int error;
1651
	size_t position;
1652
	git_index_entry remove_key = {{ 0 }};
1653

1654 1655
	remove_key.path = path;
	GIT_IDXENTRY_STAGE_SET(&remove_key, stage);
1656 1657

	DELETE_IN_MAP(index, &remove_key);
1658

1659
	if (index_find(&position, index, path, 0, stage) < 0) {
1660
		giterr_set(
1661
			GITERR_INDEX, "index does not contain %s at stage %d", path, stage);
1662 1663 1664
		error = GIT_ENOTFOUND;
	} else {
		error = index_remove_entry(index, position);
1665
	}
Edward Thomson committed
1666

1667
	return error;
1668
}
1669

1670 1671 1672 1673 1674 1675 1676
int git_index_remove_directory(git_index *index, const char *dir, int stage)
{
	git_buf pfx = GIT_BUF_INIT;
	int error = 0;
	size_t pos;
	git_index_entry *entry;

1677 1678
	if (!(error = git_buf_sets(&pfx, dir)) &&
		!(error = git_path_to_dir(&pfx)))
1679
		index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY);
1680

1681
	while (!error) {
1682 1683 1684 1685
		entry = git_vector_get(&index->entries, pos);
		if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0)
			break;

1686
		if (GIT_IDXENTRY_STAGE(entry) != stage) {
1687 1688 1689 1690
			++pos;
			continue;
		}

1691
		error = index_remove_entry(index, pos);
1692

1693
		/* removed entry at 'pos' so we don't need to increment */
1694 1695 1696 1697 1698 1699 1700
	}

	git_buf_free(&pfx);

	return error;
}

1701 1702 1703 1704 1705 1706
int git_index_find_prefix(size_t *at_pos, git_index *index, const char *prefix)
{
	int error = 0;
	size_t pos;
	const git_index_entry *entry;

1707
	index_find(&pos, index, prefix, strlen(prefix), GIT_INDEX_STAGE_ANY);
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
	entry = git_vector_get(&index->entries, pos);
	if (!entry || git__prefixcmp(entry->path, prefix) != 0)
		error = GIT_ENOTFOUND;

	if (!error && at_pos)
		*at_pos = pos;

	return error;
}

1718
int git_index__find_pos(
1719 1720 1721
	size_t *out, git_index *index, const char *path, size_t path_len, int stage)
{
	assert(index && path);
1722
	return index_find(out, index, path, path_len, stage);
Edward Thomson committed
1723 1724
}

1725
int git_index_find(size_t *at_pos, git_index *index, const char *path)
1726
{
1727
	size_t pos;
Edward Thomson committed
1728 1729 1730

	assert(index && path);

1731 1732
	if (git_vector_bsearch2(
			&pos, &index->entries, index->entries_search_path, path) < 0) {
1733
		giterr_set(GITERR_INDEX, "index does not contain %s", path);
1734
		return GIT_ENOTFOUND;
1735
	}
Edward Thomson committed
1736 1737

	/* Since our binary search only looked at path, we may be in the
1738 1739
	 * middle of a list of stages.
	 */
1740 1741
	for (; pos > 0; --pos) {
		const git_index_entry *prev = git_vector_get(&index->entries, pos - 1);
Edward Thomson committed
1742 1743 1744 1745 1746

		if (index->entries_cmp_path(prev->path, path) != 0)
			break;
	}

1747 1748 1749 1750
	if (at_pos)
		*at_pos = pos;

	return 0;
1751 1752
}

Edward Thomson committed
1753 1754 1755 1756 1757 1758
int 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)
{
	git_index_entry *entries[3] = { 0 };
1759
	unsigned short i;
Edward Thomson committed
1760 1761 1762 1763
	int ret = 0;

	assert (index);

1764 1765 1766 1767 1768 1769
	if ((ancestor_entry &&
			(ret = index_entry_dup(&entries[0], index, ancestor_entry)) < 0) ||
		(our_entry &&
			(ret = index_entry_dup(&entries[1], index, our_entry)) < 0) ||
		(their_entry &&
			(ret = index_entry_dup(&entries[2], index, their_entry)) < 0))
1770
		goto on_error;
Edward Thomson committed
1771

1772 1773 1774 1775
	/* Validate entries */
	for (i = 0; i < 3; i++) {
		if (entries[i] && !valid_filemode(entries[i]->mode)) {
			giterr_set(GITERR_INDEX, "invalid filemode for stage %d entry",
1776
				i + 1);
1777 1778 1779 1780
			return -1;
		}
	}

1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
	/* Remove existing index entries for each path */
	for (i = 0; i < 3; i++) {
		if (entries[i] == NULL)
			continue;

		if ((ret = git_index_remove(index, entries[i]->path, 0)) != 0) {
			if (ret != GIT_ENOTFOUND)
				goto on_error;

			giterr_clear();
			ret = 0;
		}
	}

	/* Add the conflict entries */
Edward Thomson committed
1796 1797 1798 1799 1800
	for (i = 0; i < 3; i++) {
		if (entries[i] == NULL)
			continue;

		/* Make sure stage is correct */
1801
		GIT_IDXENTRY_STAGE_SET(entries[i], i + 1);
Edward Thomson committed
1802

1803
		if ((ret = index_insert(index, &entries[i], 1, true, true, false)) < 0)
Edward Thomson committed
1804
			goto on_error;
1805 1806

		entries[i] = NULL; /* don't free if later entry fails */
Edward Thomson committed
1807 1808
	}

nulltoken committed
1809
	return 0;
Edward Thomson committed
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819

on_error:
	for (i = 0; i < 3; i++) {
		if (entries[i] != NULL)
			index_entry_free(entries[i]);
	}

	return ret;
}

1820 1821 1822 1823 1824 1825
static int 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)
Edward Thomson committed
1826
{
1827 1828 1829 1830
	const git_index_entry *conflict_entry;
	const char *path = NULL;
	size_t count;
	int stage, len = 0;
Edward Thomson committed
1831

1832
	assert(ancestor_out && our_out && their_out && index);
1833

Edward Thomson committed
1834 1835 1836 1837
	*ancestor_out = NULL;
	*our_out = NULL;
	*their_out = NULL;

1838 1839
	for (count = git_index_entrycount(index); n < count; ++n) {
		conflict_entry = git_vector_get(&index->entries, n);
Edward Thomson committed
1840

1841
		if (path && index->entries_cmp_path(conflict_entry->path, path) != 0)
Edward Thomson committed
1842 1843
			break;

1844
		stage = GIT_IDXENTRY_STAGE(conflict_entry);
1845
		path = conflict_entry->path;
1846

Edward Thomson committed
1847 1848 1849
		switch (stage) {
		case 3:
			*their_out = conflict_entry;
1850
			len++;
Edward Thomson committed
1851 1852 1853
			break;
		case 2:
			*our_out = conflict_entry;
1854
			len++;
Edward Thomson committed
1855 1856 1857
			break;
		case 1:
			*ancestor_out = conflict_entry;
1858
			len++;
Edward Thomson committed
1859 1860 1861 1862 1863 1864
			break;
		default:
			break;
		};
	}

1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
	return len;
}

int 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)
{
	size_t pos;
	int len = 0;

	assert(ancestor_out && our_out && their_out && index && path);

	*ancestor_out = NULL;
	*our_out = NULL;
	*their_out = NULL;

	if (git_index_find(&pos, index, path) < 0)
		return GIT_ENOTFOUND;

	if ((len = index_conflict__get_byindex(
		ancestor_out, our_out, their_out, index, pos)) < 0)
		return len;
	else if (len == 0)
		return GIT_ENOTFOUND;

	return 0;
Edward Thomson committed
1894 1895
}

1896
static int index_conflict_remove(git_index *index, const char *path)
Edward Thomson committed
1897
{
1898
	size_t pos = 0;
Edward Thomson committed
1899
	git_index_entry *conflict_entry;
1900
	int error = 0;
Edward Thomson committed
1901

1902
	if (path != NULL && git_index_find(&pos, index, path) < 0)
1903
		return GIT_ENOTFOUND;
Edward Thomson committed
1904

1905
	while ((conflict_entry = git_vector_get(&index->entries, pos)) != NULL) {
Edward Thomson committed
1906

1907 1908
		if (path != NULL &&
			index->entries_cmp_path(conflict_entry->path, path) != 0)
Edward Thomson committed
1909 1910
			break;

1911
		if (GIT_IDXENTRY_STAGE(conflict_entry) == 0) {
Edward Thomson committed
1912 1913 1914 1915
			pos++;
			continue;
		}

1916
		if ((error = index_remove_entry(index, pos)) < 0)
1917
			break;
Edward Thomson committed
1918 1919
	}

1920
	return error;
Edward Thomson committed
1921 1922
}

1923
int git_index_conflict_remove(git_index *index, const char *path)
Edward Thomson committed
1924
{
1925 1926
	assert(index && path);
	return index_conflict_remove(index, path);
Edward Thomson committed
1927 1928
}

1929
int git_index_conflict_cleanup(git_index *index)
Edward Thomson committed
1930 1931
{
	assert(index);
1932
	return index_conflict_remove(index, NULL);
Edward Thomson committed
1933 1934
}

1935
int git_index_has_conflicts(const git_index *index)
1936
{
1937
	size_t i;
1938 1939 1940 1941 1942
	git_index_entry *entry;

	assert(index);

	git_vector_foreach(&index->entries, i, entry) {
1943
		if (GIT_IDXENTRY_STAGE(entry) > 0)
1944 1945 1946 1947 1948 1949
			return 1;
	}

	return 0;
}

1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
int git_index_conflict_iterator_new(
	git_index_conflict_iterator **iterator_out,
	git_index *index)
{
	git_index_conflict_iterator *it = NULL;

	assert(iterator_out && index);

	it = git__calloc(1, sizeof(git_index_conflict_iterator));
	GITERR_CHECK_ALLOC(it);

	it->index = index;

	*iterator_out = it;
	return 0;
}

int 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)
{
	const git_index_entry *entry;
	int len;

	assert(ancestor_out && our_out && their_out && iterator);

	*ancestor_out = NULL;
	*our_out = NULL;
	*their_out = NULL;

	while (iterator->cur < iterator->index->entries.length) {
		entry = git_index_get_byindex(iterator->index, iterator->cur);

1985
		if (git_index_entry_is_conflict(entry)) {
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
			if ((len = index_conflict__get_byindex(
				ancestor_out,
				our_out,
				their_out,
				iterator->index,
				iterator->cur)) < 0)
				return len;

			iterator->cur += len;
			return 0;
		}

		iterator->cur++;
	}

	return GIT_ITEROVER;
}

void git_index_conflict_iterator_free(git_index_conflict_iterator *iterator)
{
	if (iterator == NULL)
		return;

	git__free(iterator);
}

2012
size_t git_index_name_entrycount(git_index *index)
Edward Thomson committed
2013 2014
{
	assert(index);
2015
	return index->names.length;
Edward Thomson committed
2016 2017 2018 2019 2020 2021
}

const git_index_name_entry *git_index_name_get_byindex(
	git_index *index, size_t n)
{
	assert(index);
nulltoken committed
2022

Edward Thomson committed
2023 2024 2025 2026
	git_vector_sort(&index->names);
	return git_vector_get(&index->names, n);
}

2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
static void index_name_entry_free(git_index_name_entry *ne)
{
	if (!ne)
		return;
	git__free(ne->ancestor);
	git__free(ne->ours);
	git__free(ne->theirs);
	git__free(ne);
}

Edward Thomson committed
2037 2038 2039 2040 2041
int git_index_name_add(git_index *index,
	const char *ancestor, const char *ours, const char *theirs)
{
	git_index_name_entry *conflict_name;

2042
	assert((ancestor && ours) || (ancestor && theirs) || (ours && theirs));
Edward Thomson committed
2043 2044 2045

	conflict_name = git__calloc(1, sizeof(git_index_name_entry));
	GITERR_CHECK_ALLOC(conflict_name);
nulltoken committed
2046

2047 2048 2049 2050 2051 2052 2053
	if ((ancestor && !(conflict_name->ancestor = git__strdup(ancestor))) ||
		(ours     && !(conflict_name->ours     = git__strdup(ours))) ||
		(theirs   && !(conflict_name->theirs   = git__strdup(theirs))) ||
		git_vector_insert(&index->names, conflict_name) < 0)
	{
		index_name_entry_free(conflict_name);
		return -1;
Edward Thomson committed
2054
	}
nulltoken committed
2055

2056
	return 0;
Edward Thomson committed
2057 2058 2059 2060 2061 2062 2063 2064
}

void git_index_name_clear(git_index *index)
{
	size_t i;
	git_index_name_entry *conflict_name;

	assert(index);
nulltoken committed
2065

2066 2067
	git_vector_foreach(&index->names, i, conflict_name)
		index_name_entry_free(conflict_name);
nulltoken committed
2068

Edward Thomson committed
2069 2070 2071
	git_vector_clear(&index->names);
}

2072
size_t git_index_reuc_entrycount(git_index *index)
Edward Thomson committed
2073 2074
{
	assert(index);
2075
	return index->reuc.length;
Edward Thomson committed
2076 2077
}

2078 2079 2080 2081 2082 2083 2084
static int index_reuc_on_dup(void **old, void *new)
{
	index_entry_reuc_free(*old);
	*old = new;
	return GIT_EEXISTS;
}

Edward Thomson committed
2085 2086
static int index_reuc_insert(
	git_index *index,
2087
	git_index_reuc_entry *reuc)
Edward Thomson committed
2088
{
2089
	int res;
Edward Thomson committed
2090 2091

	assert(index && reuc && reuc->path != NULL);
2092
	assert(git_vector_is_sorted(&index->reuc));
Edward Thomson committed
2093

2094 2095
	res = git_vector_insert_sorted(&index->reuc, reuc, &index_reuc_on_dup);
	return res == GIT_EEXISTS ? 0 : res;
Edward Thomson committed
2096 2097 2098
}

int git_index_reuc_add(git_index *index, const char *path,
Edward Thomson committed
2099 2100 2101
	int ancestor_mode, const git_oid *ancestor_oid,
	int our_mode, const git_oid *our_oid,
	int their_mode, const git_oid *their_oid)
Edward Thomson committed
2102 2103 2104 2105 2106 2107
{
	git_index_reuc_entry *reuc = NULL;
	int error = 0;

	assert(index && path);

2108 2109
	if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode,
			ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 ||
2110
		(error = index_reuc_insert(index, reuc)) < 0)
Edward Thomson committed
2111 2112 2113
		index_entry_reuc_free(reuc);

	return error;
2114
}
Edward Thomson committed
2115

2116
int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path)
2117
{
2118
	return git_vector_bsearch2(at_pos, &index->reuc, index->reuc_search, path);
2119 2120
}

Edward Thomson committed
2121
const git_index_reuc_entry *git_index_reuc_get_bypath(
2122
	git_index *index, const char *path)
2123
{
2124
	size_t pos;
2125
	assert(index && path);
2126

Edward Thomson committed
2127
	if (!index->reuc.length)
2128
		return NULL;
2129

2130
	assert(git_vector_is_sorted(&index->reuc));
Edward Thomson committed
2131

2132
	if (git_index_reuc_find(&pos, index, path) < 0)
2133
		return NULL;
2134

Edward Thomson committed
2135
	return git_vector_get(&index->reuc, pos);
2136 2137
}

Edward Thomson committed
2138
const git_index_reuc_entry *git_index_reuc_get_byindex(
2139
	git_index *index, size_t n)
2140 2141
{
	assert(index);
2142
	assert(git_vector_is_sorted(&index->reuc));
Edward Thomson committed
2143 2144 2145 2146

	return git_vector_get(&index->reuc, n);
}

Ben Straub committed
2147
int git_index_reuc_remove(git_index *index, size_t position)
Edward Thomson committed
2148 2149 2150 2151
{
	int error;
	git_index_reuc_entry *reuc;

2152
	assert(git_vector_is_sorted(&index->reuc));
Edward Thomson committed
2153 2154

	reuc = git_vector_get(&index->reuc, position);
2155
	error = git_vector_remove(&index->reuc, position);
Edward Thomson committed
2156 2157 2158 2159 2160

	if (!error)
		index_entry_reuc_free(reuc);

	return error;
2161 2162
}

Edward Thomson committed
2163 2164 2165 2166 2167 2168
void git_index_reuc_clear(git_index *index)
{
	size_t i;

	assert(index);

2169 2170
	for (i = 0; i < index->reuc.length; ++i)
		index_entry_reuc_free(git__swap(index->reuc.contents[i], NULL));
Edward Thomson committed
2171 2172 2173 2174

	git_vector_clear(&index->reuc);
}

2175 2176
static int index_error_invalid(const char *message)
{
2177
	giterr_set(GITERR_INDEX, "invalid data in index - %s", message);
2178 2179 2180
	return -1;
}

Edward Thomson committed
2181
static int read_reuc(git_index *index, const char *buffer, size_t size)
2182
{
2183 2184
	const char *endptr;
	size_t len;
2185 2186
	int i;

2187 2188 2189
	/* If called multiple times, the vector might already be initialized */
	if (index->reuc._alloc_size == 0 &&
		git_vector_init(&index->reuc, 16, reuc_cmp) < 0)
2190
		return -1;
2191 2192

	while (size) {
Edward Thomson committed
2193
		git_index_reuc_entry *lost;
2194

2195
		len = p_strnlen(buffer, size) + 1;
2196
		if (size <= len)
Edward Thomson committed
2197
			return index_error_invalid("reading reuc entries");
2198

2199
		lost = reuc_entry_alloc(buffer);
2200
		GITERR_CHECK_ALLOC(lost);
2201 2202 2203 2204

		size -= len;
		buffer += len;

2205
		/* read 3 ASCII octal numbers for stage entries */
2206
		for (i = 0; i < 3; i++) {
2207
			int64_t tmp;
2208

2209
			if (git__strtol64(&tmp, buffer, &endptr, 8) < 0 ||
2210
				!endptr || endptr == buffer || *endptr ||
2211
				tmp < 0 || tmp > UINT32_MAX) {
2212
				index_entry_reuc_free(lost);
Edward Thomson committed
2213
				return index_error_invalid("reading reuc entry stage");
2214
			}
2215

2216
			lost->mode[i] = (uint32_t)tmp;
2217

2218
			len = (endptr + 1) - buffer;
2219 2220
			if (size <= len) {
				index_entry_reuc_free(lost);
Edward Thomson committed
2221
				return index_error_invalid("reading reuc entry stage");
2222
			}
2223

2224 2225 2226 2227
			size -= len;
			buffer += len;
		}

2228
		/* read up to 3 OIDs for stage entries */
2229 2230 2231
		for (i = 0; i < 3; i++) {
			if (!lost->mode[i])
				continue;
2232 2233
			if (size < 20) {
				index_entry_reuc_free(lost);
Edward Thomson committed
2234
				return index_error_invalid("reading reuc entry oid");
2235
			}
2236

2237
			git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer);
2238 2239 2240
			size -= 20;
			buffer += 20;
		}
2241 2242 2243 2244

		/* entry was read successfully - insert into reuc vector */
		if (git_vector_insert(&index->reuc, lost) < 0)
			return -1;
2245 2246
	}

Edward Thomson committed
2247
	/* entries are guaranteed to be sorted on-disk */
2248
	git_vector_set_sorted(&index->reuc, true);
Edward Thomson committed
2249

2250
	return 0;
2251 2252
}

Edward Thomson committed
2253 2254 2255 2256

static int read_conflict_names(git_index *index, const char *buffer, size_t size)
{
	size_t len;
nulltoken committed
2257

Edward Thomson committed
2258 2259 2260 2261 2262 2263
	/* This gets called multiple times, the vector might already be initialized */
	if (index->names._alloc_size == 0 &&
		git_vector_init(&index->names, 16, conflict_name_cmp) < 0)
		return -1;

#define read_conflict_name(ptr) \
2264
	len = p_strnlen(buffer, size) + 1; \
2265 2266 2267 2268
	if (size < len) { \
		index_error_invalid("reading conflict name entries"); \
		goto out_err; \
	} \
Edward Thomson committed
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
	if (len == 1) \
		ptr = NULL; \
	else { \
		ptr = git__malloc(len); \
		GITERR_CHECK_ALLOC(ptr); \
		memcpy(ptr, buffer, len); \
	} \
	\
	buffer += len; \
	size -= len;
nulltoken committed
2279

Edward Thomson committed
2280 2281 2282 2283 2284 2285 2286
	while (size) {
		git_index_name_entry *conflict_name = git__calloc(1, sizeof(git_index_name_entry));
		GITERR_CHECK_ALLOC(conflict_name);

		read_conflict_name(conflict_name->ancestor);
		read_conflict_name(conflict_name->ours);
		read_conflict_name(conflict_name->theirs);
nulltoken committed
2287

Edward Thomson committed
2288
		if (git_vector_insert(&index->names, conflict_name) < 0)
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
			goto out_err;

		continue;

out_err:
		git__free(conflict_name->ancestor);
		git__free(conflict_name->ours);
		git__free(conflict_name->theirs);
		git__free(conflict_name);
		return -1;
Edward Thomson committed
2299 2300 2301
	}

#undef read_conflict_name
nulltoken committed
2302

Edward Thomson committed
2303
	/* entries are guaranteed to be sorted on-disk */
2304
	git_vector_set_sorted(&index->names, true);
nulltoken committed
2305 2306

	return 0;
Edward Thomson committed
2307 2308
}

2309 2310 2311 2312 2313 2314 2315 2316
static size_t index_entry_size(size_t path_len, size_t varint_len, uint32_t flags)
{
	if (varint_len) {
		if (flags & GIT_IDXENTRY_EXTENDED)
			return offsetof(struct entry_long, path) + path_len + 1 + varint_len;
		else
			return offsetof(struct entry_short, path) + path_len + 1 + varint_len;
	} else {
2317
#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7)
2318
		if (flags & GIT_IDXENTRY_EXTENDED)
2319
			return entry_size(struct entry_long, path_len);
2320
		else
2321 2322
			return entry_size(struct entry_short, path_len);
#undef entry_size
2323 2324 2325
	}
}

2326
static int read_entry(
2327
	git_index_entry **out,
2328
	size_t *out_size,
2329 2330
	git_index *index,
	const void *buffer,
David Turner committed
2331
	size_t buffer_size,
2332
	const char *last)
2333 2334 2335
{
	size_t path_length, entry_size;
	const char *path_ptr;
2336
	struct entry_short source;
2337
	git_index_entry entry = {{0}};
David Turner committed
2338 2339
	bool compressed = index->version >= INDEX_VERSION_NUMBER_COMP;
	char *tmp_path = NULL;
2340 2341

	if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size)
2342
		return -1;
2343

2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
	/* buffer is not guaranteed to be aligned */
	memcpy(&source, buffer, sizeof(struct entry_short));

	entry.ctime.seconds = (git_time_t)ntohl(source.ctime.seconds);
	entry.ctime.nanoseconds = ntohl(source.ctime.nanoseconds);
	entry.mtime.seconds = (git_time_t)ntohl(source.mtime.seconds);
	entry.mtime.nanoseconds = ntohl(source.mtime.nanoseconds);
	entry.dev = ntohl(source.dev);
	entry.ino = ntohl(source.ino);
	entry.mode = ntohl(source.mode);
	entry.uid = ntohl(source.uid);
	entry.gid = ntohl(source.gid);
	entry.file_size = ntohl(source.file_size);
	git_oid_cpy(&entry.id, &source.oid);
	entry.flags = ntohs(source.flags);
2359 2360

	if (entry.flags & GIT_IDXENTRY_EXTENDED) {
2361 2362
		uint16_t flags_raw;
		size_t flags_offset;
2363

2364 2365 2366 2367 2368 2369 2370
		flags_offset = offsetof(struct entry_long, flags_extended);
		memcpy(&flags_raw, (const char *) buffer + flags_offset,
			sizeof(flags_raw));
		flags_raw = ntohs(flags_raw);

		memcpy(&entry.flags_extended, &flags_raw, sizeof(flags_raw));
		path_ptr = (const char *) buffer + offsetof(struct entry_long, path);
2371
	} else
2372
		path_ptr = (const char *) buffer + offsetof(struct entry_short, path);
2373

David Turner committed
2374 2375
	if (!compressed) {
		path_length = entry.flags & GIT_IDXENTRY_NAMEMASK;
2376

David Turner committed
2377 2378 2379 2380
		/* if this is a very long string, we must find its
		 * real length without overflowing */
		if (path_length == 0xFFF) {
			const char *path_end;
2381

David Turner committed
2382 2383
			path_end = memchr(path_ptr, '\0', buffer_size);
			if (path_end == NULL)
2384
				return -1;
2385

David Turner committed
2386 2387
			path_length = path_end - path_ptr;
		}
2388

2389
		entry_size = index_entry_size(path_length, 0, entry.flags);
David Turner committed
2390 2391
		entry.path = (char *)path_ptr;
	} else {
2392 2393 2394 2395 2396 2397 2398
		size_t varint_len, last_len, prefix_len, suffix_len, path_len;
		uintmax_t strip_len;

		strip_len = git_decode_varint((const unsigned char *)path_ptr, &varint_len);
		last_len = strlen(last);

		if (varint_len == 0 || last_len < strip_len)
David Turner committed
2399 2400
			return index_error_invalid("incorrect prefix length");

2401 2402 2403
		prefix_len = last_len - strip_len;
		suffix_len = strlen(path_ptr + varint_len);

2404 2405
		GITERR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len);
		GITERR_CHECK_ALLOC_ADD(&path_len, path_len, 1);
2406 2407 2408 2409

		if (path_len > GIT_PATH_MAX)
			return index_error_invalid("unreasonable path length");

2410
		tmp_path = git__malloc(path_len);
David Turner committed
2411
		GITERR_CHECK_ALLOC(tmp_path);
2412 2413 2414

		memcpy(tmp_path, last, prefix_len);
		memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1);
2415
		entry_size = index_entry_size(suffix_len, varint_len, entry.flags);
David Turner committed
2416 2417 2418
		entry.path = tmp_path;
	}

2419 2420 2421
	if (entry_size == 0)
		return -1;

2422
	if (INDEX_FOOTER_SIZE + entry_size > buffer_size)
2423
		return -1;
2424

David Turner committed
2425 2426
	if (index_entry_dup(out, index, &entry) < 0) {
		git__free(tmp_path);
2427
		return -1;
David Turner committed
2428
	}
2429

David Turner committed
2430
	git__free(tmp_path);
2431 2432
	*out_size = entry_size;
	return 0;
2433 2434 2435 2436
}

static int read_header(struct index_header *dest, const void *buffer)
{
2437
	const struct index_header *source = buffer;
2438

2439 2440
	dest->signature = ntohl(source->signature);
	if (dest->signature != INDEX_HEADER_SIG)
2441
		return index_error_invalid("incorrect header signature");
2442 2443

	dest->version = ntohl(source->version);
David Turner committed
2444 2445
	if (dest->version < INDEX_VERSION_NUMBER_LB ||
		dest->version > INDEX_VERSION_NUMBER_UB)
2446
		return index_error_invalid("incorrect header version");
2447 2448

	dest->entry_count = ntohl(source->entry_count);
2449
	return 0;
2450 2451 2452 2453 2454 2455 2456
}

static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size)
{
	struct index_extension dest;
	size_t total_size;

2457 2458 2459
	/* buffer is not guaranteed to be aligned */
	memcpy(&dest, buffer, sizeof(struct index_extension));
	dest.extension_size = ntohl(dest.extension_size);
2460 2461 2462

	total_size = dest.extension_size + sizeof(struct index_extension);

2463 2464
	if (dest.extension_size > total_size ||
		buffer_size < total_size ||
2465
		buffer_size - total_size < INDEX_FOOTER_SIZE)
2466 2467 2468 2469 2470 2471
		return 0;

	/* optional extension */
	if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
		/* tree cache */
		if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {
2472
			if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, &index->tree_pool) < 0)
2473
				return 0;
2474
		} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
Edward Thomson committed
2475
			if (read_reuc(index, buffer + 8, dest.extension_size) < 0)
2476
				return 0;
Edward Thomson committed
2477 2478 2479
		} else if (memcmp(dest.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4) == 0) {
			if (read_conflict_names(index, buffer + 8, dest.extension_size) < 0)
				return 0;
2480
		}
2481 2482
		/* else, unsupported extension. We cannot parse this, but we can skip
		 * it by returning `total_size */
2483 2484 2485 2486 2487 2488 2489 2490 2491
	} else {
		/* we cannot handle non-ignorable extensions;
		 * in fact they aren't even defined in the standard */
		return 0;
	}

	return total_size;
}

2492
static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
2493
{
2494
	int error = 0;
2495
	unsigned int i;
2496
	struct index_header header = { 0 };
2497
	git_oid checksum_calculated, checksum_expected;
2498
	const char *last = NULL;
David Turner committed
2499
	const char *empty = "";
2500 2501

#define seek_forward(_increase) { \
2502 2503 2504
	if (_increase >= buffer_size) { \
		error = index_error_invalid("ran out of data while parsing"); \
		goto done; } \
2505 2506 2507 2508 2509
	buffer += _increase; \
	buffer_size -= _increase;\
}

	if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
2510
		return index_error_invalid("insufficient buffer space");
2511 2512 2513

	/* Precalculate the SHA1 of the files's contents -- we'll match it to
	 * the provided SHA1 in the footer */
2514
	git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE);
2515 2516

	/* Parse header */
2517 2518
	if ((error = read_header(&header, buffer)) < 0)
		return error;
2519

David Turner committed
2520 2521
	index->version = header.version;
	if (index->version >= INDEX_VERSION_NUMBER_COMP)
2522
		last = empty;
David Turner committed
2523

2524 2525
	seek_forward(INDEX_HEADER_SIZE);

2526
	assert(!index->entries.length);
2527

2528
	if (index->ignore_case)
2529
		git_idxmap_icase_resize((khash_t(idxicase) *) index->entries_map, header.entry_count);
2530
	else
2531
		git_idxmap_resize(index->entries_map, header.entry_count);
2532

2533
	/* Parse all the entries */
2534
	for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
2535
		git_index_entry *entry = NULL;
2536
		size_t entry_size;
2537

2538
		if ((error = read_entry(&entry, &entry_size, index, buffer, buffer_size, last)) < 0) {
2539 2540 2541
			error = index_error_invalid("invalid entry");
			goto done;
		}
2542

2543 2544
		if ((error = git_vector_insert(&index->entries, entry)) < 0) {
			index_entry_free(entry);
2545
			goto done;
2546
		}
2547

2548
		INSERT_IN_MAP(index, entry, &error);
2549 2550 2551 2552 2553

		if (error < 0) {
			index_entry_free(entry);
			goto done;
		}
2554
		error = 0;
2555

2556 2557 2558
		if (index->version >= INDEX_VERSION_NUMBER_COMP)
			last = entry->path;

2559 2560 2561
		seek_forward(entry_size);
	}

2562 2563 2564 2565
	if (i != header.entry_count) {
		error = index_error_invalid("header entries changed while parsing");
		goto done;
	}
2566

2567 2568 2569 2570 2571 2572 2573
	/* There's still space for some extensions! */
	while (buffer_size > INDEX_FOOTER_SIZE) {
		size_t extension_size;

		extension_size = read_extension(index, buffer, buffer_size);

		/* see if we have read any bytes from the extension */
2574 2575 2576 2577
		if (extension_size == 0) {
			error = index_error_invalid("extension is truncated");
			goto done;
		}
2578 2579 2580 2581

		seek_forward(extension_size);
	}

2582 2583 2584 2585 2586
	if (buffer_size != INDEX_FOOTER_SIZE) {
		error = index_error_invalid(
			"buffer size does not match index footer size");
		goto done;
	}
2587 2588

	/* 160-bit SHA-1 over the content of the index file before this checksum. */
Vicent Marti committed
2589
	git_oid_fromraw(&checksum_expected, (const unsigned char *)buffer);
2590

2591 2592 2593 2594 2595
	if (git_oid__cmp(&checksum_calculated, &checksum_expected) != 0) {
		error = index_error_invalid(
			"calculated checksum does not match expected");
		goto done;
	}
2596

2597 2598
	git_oid_cpy(&index->checksum, &checksum_calculated);

2599 2600
#undef seek_forward

2601 2602 2603 2604
	/* Entries are stored case-sensitively on disk, so re-sort now if
	 * in-memory index is supposed to be case-insensitive
	 */
	git_vector_set_sorted(&index->entries, !index->ignore_case);
2605
	git_vector_sort(&index->entries);
2606

2607 2608
done:
	return error;
2609 2610
}

2611
static bool is_index_extended(git_index *index)
Vicent Marti committed
2612
{
2613
	size_t i, extended;
2614
	git_index_entry *entry;
Vicent Marti committed
2615 2616 2617

	extended = 0;

2618
	git_vector_foreach(&index->entries, i, entry) {
Vicent Marti committed
2619 2620 2621 2622 2623 2624
		entry->flags &= ~GIT_IDXENTRY_EXTENDED;
		if (entry->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS) {
			extended++;
			entry->flags |= GIT_IDXENTRY_EXTENDED;
		}
	}
2625

2626
	return (extended > 0);
Vicent Marti committed
2627 2628
}

2629
static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char *last)
2630
{
2631
	void *mem = NULL;
2632
	struct entry_short ondisk;
2633
	size_t path_len, disk_size;
2634
	int varint_len = 0;
2635
	char *path;
David Turner committed
2636 2637
	const char *path_start = entry->path;
	size_t same_len = 0;
2638

2639
	path_len = ((struct entry_internal *)entry)->pathlen;
2640

David Turner committed
2641
	if (last) {
2642
		const char *last_c = last;
David Turner committed
2643 2644 2645 2646 2647 2648 2649 2650 2651

		while (*path_start == *last_c) {
			if (!*path_start || !*last_c)
				break;
			++path_start;
			++last_c;
			++same_len;
		}
		path_len -= same_len;
2652
		varint_len = git_encode_varint(NULL, 0, same_len);
David Turner committed
2653 2654
	}

2655
	disk_size = index_entry_size(path_len, varint_len, entry->flags);
2656

2657 2658
	if (git_filebuf_reserve(file, &mem, disk_size) < 0)
		return -1;
2659

2660
	memset(mem, 0x0, disk_size);
2661

2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
	/**
	 * Yes, we have to truncate.
	 *
	 * The on-disk format for Index entries clearly defines
	 * the time and size fields to be 4 bytes each -- so even if
	 * we store these values with 8 bytes on-memory, they must
	 * be truncated to 4 bytes before writing to disk.
	 *
	 * In 2038 I will be either too dead or too rich to care about this
	 */
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
	ondisk.ctime.seconds = htonl((uint32_t)entry->ctime.seconds);
	ondisk.mtime.seconds = htonl((uint32_t)entry->mtime.seconds);
	ondisk.ctime.nanoseconds = htonl(entry->ctime.nanoseconds);
	ondisk.mtime.nanoseconds = htonl(entry->mtime.nanoseconds);
	ondisk.dev = htonl(entry->dev);
	ondisk.ino = htonl(entry->ino);
	ondisk.mode = htonl(entry->mode);
	ondisk.uid = htonl(entry->uid);
	ondisk.gid = htonl(entry->gid);
	ondisk.file_size = htonl((uint32_t)entry->file_size);
2682

2683
	git_oid_cpy(&ondisk.oid, &entry->id);
2684

2685
	ondisk.flags = htons(entry->flags);
2686 2687

	if (entry->flags & GIT_IDXENTRY_EXTENDED) {
2688 2689 2690
		struct entry_long ondisk_ext;
		memcpy(&ondisk_ext, &ondisk, sizeof(struct entry_short));
		ondisk_ext.flags_extended = htons(entry->flags_extended &
2691
			GIT_IDXENTRY_EXTENDED_FLAGS);
2692 2693
		memcpy(mem, &ondisk_ext, offsetof(struct entry_long, path));
		path = ((struct entry_long*)mem)->path;
2694 2695
		disk_size -= offsetof(struct entry_long, path);
	} else {
2696 2697
		memcpy(mem, &ondisk, offsetof(struct entry_short, path));
		path = ((struct entry_short*)mem)->path;
2698 2699
		disk_size -= offsetof(struct entry_short, path);
	}
2700

David Turner committed
2701
	if (last) {
2702
		varint_len = git_encode_varint((unsigned char *) path,
2703
					  disk_size, same_len);
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
		assert(varint_len > 0);
		path += varint_len;
		disk_size -= varint_len;

		/*
		 * If using path compression, we are not allowed
		 * to have additional trailing NULs.
		 */
		assert(disk_size == path_len + 1);
	} else {
		/*
		 * If no path compression is used, we do have
		 * NULs as padding. As such, simply assert that
		 * we have enough space left to write the path.
		 */
		assert(disk_size > path_len);
David Turner committed
2720
	}
2721 2722

	memcpy(path, path_start, path_len + 1);
2723

2724
	return 0;
2725 2726
}

2727
static int write_entries(git_index *index, git_filebuf *file)
2728
{
2729
	int error = 0;
2730
	size_t i;
2731
	git_vector case_sorted, *entries;
2732
	git_index_entry *entry;
2733
	const char *last = NULL;
2734 2735 2736 2737

	/* If index->entries is sorted case-insensitively, then we need
	 * to re-sort it case-sensitively before writing */
	if (index->ignore_case) {
2738
		git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp);
2739
		git_vector_sort(&case_sorted);
2740 2741 2742
		entries = &case_sorted;
	} else {
		entries = &index->entries;
2743 2744
	}

David Turner committed
2745
	if (index->version >= INDEX_VERSION_NUMBER_COMP)
2746
		last = "";
David Turner committed
2747

2748
	git_vector_foreach(entries, i, entry) {
David Turner committed
2749
		if ((error = write_disk_entry(file, entry, last)) < 0)
2750
			break;
2751 2752 2753
		if (index->version >= INDEX_VERSION_NUMBER_COMP)
			last = entry->path;
	}
2754 2755 2756 2757 2758

	if (index->ignore_case)
		git_vector_free(&case_sorted);

	return error;
2759 2760
}

Edward Thomson committed
2761 2762 2763 2764 2765 2766 2767 2768
static int write_extension(git_filebuf *file, struct index_extension *header, git_buf *data)
{
	struct index_extension ondisk;

	memset(&ondisk, 0x0, sizeof(struct index_extension));
	memcpy(&ondisk, header, 4);
	ondisk.extension_size = htonl(header->extension_size);

2769 2770
	git_filebuf_write(file, &ondisk, sizeof(struct index_extension));
	return git_filebuf_write(file, data->ptr, data->size);
Edward Thomson committed
2771 2772
}

Edward Thomson committed
2773 2774 2775 2776 2777 2778 2779 2780
static int create_name_extension_data(git_buf *name_buf, git_index_name_entry *conflict_name)
{
	int error = 0;

	if (conflict_name->ancestor == NULL)
		error = git_buf_put(name_buf, "\0", 1);
	else
		error = git_buf_put(name_buf, conflict_name->ancestor, strlen(conflict_name->ancestor) + 1);
nulltoken committed
2781

Edward Thomson committed
2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
	if (error != 0)
		goto on_error;

	if (conflict_name->ours == NULL)
		error = git_buf_put(name_buf, "\0", 1);
	else
		error = git_buf_put(name_buf, conflict_name->ours, strlen(conflict_name->ours) + 1);

	if (error != 0)
		goto on_error;

	if (conflict_name->theirs == NULL)
		error = git_buf_put(name_buf, "\0", 1);
	else
		error = git_buf_put(name_buf, conflict_name->theirs, strlen(conflict_name->theirs) + 1);

on_error:
	return error;
}

static int write_name_extension(git_index *index, git_filebuf *file)
{
	git_buf name_buf = GIT_BUF_INIT;
	git_vector *out = &index->names;
	git_index_name_entry *conflict_name;
	struct index_extension extension;
	size_t i;
	int error = 0;
nulltoken committed
2810

Edward Thomson committed
2811 2812 2813 2814
	git_vector_foreach(out, i, conflict_name) {
		if ((error = create_name_extension_data(&name_buf, conflict_name)) < 0)
			goto done;
	}
nulltoken committed
2815

Edward Thomson committed
2816 2817 2818
	memset(&extension, 0x0, sizeof(struct index_extension));
	memcpy(&extension.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4);
	extension.extension_size = (uint32_t)name_buf.size;
nulltoken committed
2819

Edward Thomson committed
2820
	error = write_extension(file, &extension, &name_buf);
nulltoken committed
2821

Edward Thomson committed
2822
	git_buf_free(&name_buf);
nulltoken committed
2823

Edward Thomson committed
2824 2825 2826 2827
done:
	return error;
}

Edward Thomson committed
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
static int create_reuc_extension_data(git_buf *reuc_buf, git_index_reuc_entry *reuc)
{
	int i;
	int error = 0;

	if ((error = git_buf_put(reuc_buf, reuc->path, strlen(reuc->path) + 1)) < 0)
		return error;

	for (i = 0; i < 3; i++) {
		if ((error = git_buf_printf(reuc_buf, "%o", reuc->mode[i])) < 0 ||
			(error = git_buf_put(reuc_buf, "\0", 1)) < 0)
			return error;
	}

	for (i = 0; i < 3; i++) {
		if (reuc->mode[i] && (error = git_buf_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_RAWSZ)) < 0)
			return error;
	}

	return 0;
}

static int write_reuc_extension(git_index *index, git_filebuf *file)
{
	git_buf reuc_buf = GIT_BUF_INIT;
	git_vector *out = &index->reuc;
	git_index_reuc_entry *reuc;
	struct index_extension extension;
2856
	size_t i;
Edward Thomson committed
2857 2858 2859 2860 2861 2862 2863 2864 2865
	int error = 0;

	git_vector_foreach(out, i, reuc) {
		if ((error = create_reuc_extension_data(&reuc_buf, reuc)) < 0)
			goto done;
	}

	memset(&extension, 0x0, sizeof(struct index_extension));
	memcpy(&extension.signature, INDEX_EXT_UNMERGED_SIG, 4);
2866
	extension.extension_size = (uint32_t)reuc_buf.size;
Edward Thomson committed
2867 2868 2869 2870 2871 2872 2873 2874 2875

	error = write_extension(file, &extension, &reuc_buf);

	git_buf_free(&reuc_buf);

done:
	return error;
}

2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898
static int write_tree_extension(git_index *index, git_filebuf *file)
{
	struct index_extension extension;
	git_buf buf = GIT_BUF_INIT;
	int error;

	if (index->tree == NULL)
		return 0;

	if ((error = git_tree_cache_write(&buf, index->tree)) < 0)
		return error;

	memset(&extension, 0x0, sizeof(struct index_extension));
	memcpy(&extension.signature, INDEX_EXT_TREECACHE_SIG, 4);
	extension.extension_size = (uint32_t)buf.size;

	error = write_extension(file, &extension, &buf);

	git_buf_free(&buf);

	return error;
}

2899 2900 2901 2902 2903 2904 2905 2906 2907
static void clear_uptodate(git_index *index)
{
	git_index_entry *entry;
	size_t i;

	git_vector_foreach(&index->entries, i, entry)
		entry->flags_extended &= ~GIT_IDXENTRY_UPTODATE;
}

2908
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file)
2909 2910 2911
{
	git_oid hash_final;
	struct index_header header;
2912
	bool is_extended;
Ben Straub committed
2913
	uint32_t index_version_number;
2914

2915
	assert(index && file);
2916

David Turner committed
2917 2918 2919 2920 2921 2922
	if (index->version <= INDEX_VERSION_NUMBER_EXT)  {
		is_extended = is_index_extended(index);
		index_version_number = is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER_LB;
	} else {
		index_version_number = index->version;
	}
Vicent Marti committed
2923

2924
	header.signature = htonl(INDEX_HEADER_SIG);
Ben Straub committed
2925
	header.version = htonl(index_version_number);
2926
	header.entry_count = htonl((uint32_t)index->entries.length);
2927

2928 2929
	if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0)
		return -1;
2930

2931 2932
	if (write_entries(index, file) < 0)
		return -1;
2933

2934 2935 2936
	/* write the tree cache extension */
	if (index->tree != NULL && write_tree_extension(index, file) < 0)
		return -1;
Edward Thomson committed
2937

Edward Thomson committed
2938 2939 2940
	/* write the rename conflict extension */
	if (index->names.length > 0 && write_name_extension(index, file) < 0)
		return -1;
nulltoken committed
2941

Edward Thomson committed
2942 2943 2944
	/* write the reuc extension */
	if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0)
		return -1;
2945

2946 2947
	/* get out the hash for all the contents we've appended to the file */
	git_filebuf_hash(&hash_final, file);
2948
	git_oid_cpy(checksum, &hash_final);
2949 2950

	/* write it at the end of the file */
2951 2952 2953 2954 2955 2956 2957
	if (git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ) < 0)
		return -1;

	/* file entries are no longer up to date */
	clear_uptodate(index);

	return 0;
2958
}
2959 2960 2961

int git_index_entry_stage(const git_index_entry *entry)
{
2962
	return GIT_IDXENTRY_STAGE(entry);
2963
}
2964

2965 2966 2967 2968 2969
int git_index_entry_is_conflict(const git_index_entry *entry)
{
	return (GIT_IDXENTRY_STAGE(entry) > 0);
}

2970
typedef struct read_tree_data {
2971
	git_index *index;
2972
	git_vector *old_entries;
2973
	git_vector *new_entries;
2974
	git_vector_cmp entry_cmp;
2975
	git_tree_cache *tree;
2976 2977
} read_tree_data;

2978 2979
static int read_tree_cb(
	const char *root, const git_tree_entry *tentry, void *payload)
2980
{
2981 2982
	read_tree_data *data = payload;
	git_index_entry *entry = NULL, *old_entry;
2983
	git_buf path = GIT_BUF_INIT;
2984
	size_t pos;
2985

Vicent Martí committed
2986
	if (git_tree_entry__is_tree(tentry))
2987
		return 0;
2988

2989 2990
	if (git_buf_joinpath(&path, root, tentry->filename) < 0)
		return -1;
2991

2992
	if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, NULL, false) < 0)
2993
		return -1;
2994 2995

	entry->mode = tentry->attr;
2996
	git_oid_cpy(&entry->id, git_tree_entry_id(tentry));
2997

2998
	/* look for corresponding old entry and copy data to new entry */
2999
	if (data->old_entries != NULL &&
3000
		!index_find_in_entries(
3001 3002 3003 3004 3005
			&pos, data->old_entries, data->entry_cmp, path.ptr, 0, 0) &&
		(old_entry = git_vector_get(data->old_entries, pos)) != NULL &&
		entry->mode == old_entry->mode &&
		git_oid_equal(&entry->id, &old_entry->id))
	{
3006
		index_entry_cpy(entry, old_entry);
3007
		entry->flags_extended = 0;
3008 3009
	}

3010
	index_entry_adjust_namemask(entry, path.size);
3011 3012
	git_buf_free(&path);

3013
	if (git_vector_insert(data->new_entries, entry) < 0) {
3014
		index_entry_free(entry);
3015 3016 3017 3018
		return -1;
	}

	return 0;
3019 3020
}

Ben Straub committed
3021
int git_index_read_tree(git_index *index, const git_tree *tree)
3022
{
3023 3024
	int error = 0;
	git_vector entries = GIT_VECTOR_INIT;
3025
	git_idxmap *entries_map;
3026
	read_tree_data data;
3027 3028 3029 3030 3031
	size_t i;
	git_index_entry *e;

	if (git_idxmap_alloc(&entries_map) < 0)
		return -1;
3032

3033
	git_vector_set_cmp(&entries, index->entries._cmp); /* match sort */
3034

3035
	data.index = index;
3036 3037
	data.old_entries = &index->entries;
	data.new_entries = &entries;
3038
	data.entry_cmp   = index->entries_search;
3039

3040 3041 3042
	index->tree = NULL;
	git_pool_clear(&index->tree_pool);

3043
	git_vector_sort(&index->entries);
3044

3045 3046
	if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0)
		goto cleanup;
3047

3048
	if (index->ignore_case)
3049
		git_idxmap_icase_resize((khash_t(idxicase) *) entries_map, entries.length);
3050
	else
3051
		git_idxmap_resize(entries_map, entries.length);
3052

3053
	git_vector_foreach(&entries, i, e) {
3054
		INSERT_IN_MAP_EX(index, entries_map, e, &error);
3055 3056 3057 3058

		if (error < 0) {
			giterr_set(GITERR_INDEX, "failed to insert entry into map");
			return error;
3059
		}
3060 3061
	}

3062 3063 3064 3065
	error = 0;

	git_vector_sort(&entries);

3066
	if ((error = git_index_clear(index)) < 0) {
3067 3068 3069 3070 3071 3072 3073
		/* well, this isn't good */;
	} else {
		git_vector_swap(&entries, &index->entries);
		entries_map = git__swap(index->entries_map, entries_map);
	}

cleanup:
3074
	git_vector_free(&entries);
3075
	git_idxmap_free(entries_map);
3076 3077 3078 3079
	if (error < 0)
		return error;

	error = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool);
3080 3081

	return error;
3082
}
3083

3084
static int git_index_read_iterator(
3085
	git_index *index,
3086 3087
	git_iterator *new_iterator,
	size_t new_length_hint)
3088 3089 3090
{
	git_vector new_entries = GIT_VECTOR_INIT,
		remove_entries = GIT_VECTOR_INIT;
3091
	git_idxmap *new_entries_map = NULL;
3092
	git_iterator *index_iterator = NULL;
3093
	git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
3094 3095 3096 3097 3098
	const git_index_entry *old_entry, *new_entry;
	git_index_entry *entry;
	size_t i;
	int error;

3099 3100 3101
	assert((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE));

	if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 ||
3102 3103
		(error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 ||
		(error = git_idxmap_alloc(&new_entries_map)) < 0)
3104 3105
		goto done;

3106
	if (index->ignore_case && new_length_hint)
3107
		git_idxmap_icase_resize((khash_t(idxicase) *) new_entries_map, new_length_hint);
3108
	else if (new_length_hint)
3109
		git_idxmap_resize(new_entries_map, new_length_hint);
3110

3111 3112
	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
		GIT_ITERATOR_INCLUDE_CONFLICTS;
3113

3114 3115 3116
	if ((error = git_iterator_for_index(&index_iterator,
			git_index_owner(index), index, &opts)) < 0 ||
		((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
3117
			error != GIT_ITEROVER) ||
3118
		((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
3119 3120 3121 3122
			error != GIT_ITEROVER))
		goto done;

	while (true) {
3123 3124 3125 3126
		git_index_entry
			*dup_entry = NULL,
			*add_entry = NULL,
			*remove_entry = NULL;
3127 3128
		int diff;

3129 3130
		error = 0;

3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
		if (old_entry && new_entry)
			diff = git_index_entry_cmp(old_entry, new_entry);
		else if (!old_entry && new_entry)
			diff = 1;
		else if (old_entry && !new_entry)
			diff = -1;
		else
			break;

		if (diff < 0) {
3141
			remove_entry = (git_index_entry *)old_entry;
3142
		} else if (diff > 0) {
3143
			dup_entry = (git_index_entry *)new_entry;
3144 3145 3146 3147
		} else {
			/* Path and stage are equal, if the OID is equal, keep it to
			 * keep the stat cache data.
			 */
3148 3149
			if (git_oid_equal(&old_entry->id, &new_entry->id) &&
				old_entry->mode == new_entry->mode) {
3150
				add_entry = (git_index_entry *)old_entry;
3151
			} else {
3152
				dup_entry = (git_index_entry *)new_entry;
3153
				remove_entry = (git_index_entry *)old_entry;
3154 3155 3156
			}
		}

3157 3158 3159
		if (dup_entry) {
			if ((error = index_entry_dup_nocache(&add_entry, index, dup_entry)) < 0)
				goto done;
3160 3161 3162

			index_entry_adjust_namemask(add_entry,
				((struct entry_internal *)add_entry)->pathlen);
3163 3164
		}

3165 3166 3167 3168 3169 3170
		/* invalidate this path in the tree cache if this is new (to
		 * invalidate the parent trees)
		 */
		if (dup_entry && !remove_entry && index->tree)
			git_tree_cache_invalidate_path(index->tree, dup_entry->path);

3171 3172
		if (add_entry) {
			if ((error = git_vector_insert(&new_entries, add_entry)) == 0)
3173
				INSERT_IN_MAP_EX(index, new_entries_map, add_entry, &error);
3174 3175
		}

3176
		if (remove_entry && error >= 0)
3177 3178 3179 3180
			error = git_vector_insert(&remove_entries, remove_entry);

		if (error < 0) {
			giterr_set(GITERR_INDEX, "failed to insert entry");
3181
			goto done;
3182 3183
		}

3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200
		if (diff <= 0) {
			if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 &&
				error != GIT_ITEROVER)
				goto done;
		}

		if (diff >= 0) {
			if ((error = git_iterator_advance(&new_entry, new_iterator)) < 0 &&
				error != GIT_ITEROVER)
				goto done;
		}
	}

	git_index_name_clear(index);
	git_index_reuc_clear(index);

	git_vector_swap(&new_entries, &index->entries);
3201
	new_entries_map = git__swap(index->entries_map, new_entries_map);
3202 3203 3204 3205 3206 3207 3208 3209

	git_vector_foreach(&remove_entries, i, entry) {
		if (index->tree)
			git_tree_cache_invalidate_path(index->tree, entry->path);

		index_entry_free(entry);
	}

3210 3211
	clear_uptodate(index);

3212 3213 3214
	error = 0;

done:
3215
	git_idxmap_free(new_entries_map);
3216 3217 3218
	git_vector_free(&new_entries);
	git_vector_free(&remove_entries);
	git_iterator_free(index_iterator);
3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229
	return error;
}

int git_index_read_index(
	git_index *index,
	const git_index *new_index)
{
	git_iterator *new_iterator = NULL;
	git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
	int error;

3230 3231
	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
		GIT_ITERATOR_INCLUDE_CONFLICTS;
3232 3233

	if ((error = git_iterator_for_index(&new_iterator,
3234 3235 3236
		git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 ||
		(error = git_index_read_iterator(index, new_iterator,
		new_index->entries.length)) < 0)
3237 3238 3239
		goto done;

done:
3240 3241 3242 3243
	git_iterator_free(new_iterator);
	return error;
}

3244 3245 3246 3247
git_repository *git_index_owner(const git_index *index)
{
	return INDEX_OWNER(index);
}
3248

3249 3250 3251 3252 3253 3254 3255
enum {
	INDEX_ACTION_NONE = 0,
	INDEX_ACTION_UPDATE = 1,
	INDEX_ACTION_REMOVE = 2,
	INDEX_ACTION_ADDALL = 3,
};

3256 3257 3258 3259 3260 3261 3262 3263 3264 3265
int git_index_add_all(
	git_index *index,
	const git_strarray *paths,
	unsigned int flags,
	git_index_matched_path_cb cb,
	void *payload)
{
	int error;
	git_repository *repo;
	git_iterator *wditer = NULL;
3266
	git_pathspec ps;
3267 3268 3269 3270 3271 3272 3273 3274
	bool no_fnmatch = (flags & GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH) != 0;

	assert(index);

	repo = INDEX_OWNER(index);
	if ((error = git_repository__ensure_not_bare(repo, "index add all")) < 0)
		return error;

3275
	if ((error = git_pathspec__init(&ps, paths)) < 0)
3276 3277 3278 3279 3280 3281 3282 3283 3284
		return error;

	/* optionally check that pathspec doesn't mention any ignored files */
	if ((flags & GIT_INDEX_ADD_CHECK_PATHSPEC) != 0 &&
		(flags & GIT_INDEX_ADD_FORCE) == 0 &&
		(error = git_ignore__check_pathspec_for_exact_ignores(
			repo, &ps.pathspec, no_fnmatch)) < 0)
		goto cleanup;

3285
	error = index_apply_to_wd_diff(index, INDEX_ACTION_ADDALL, paths, flags, cb, payload);
3286

3287 3288
	if (error)
		giterr_set_after_callback(error);
3289 3290 3291

cleanup:
	git_iterator_free(wditer);
3292
	git_pathspec__clear(&ps);
3293 3294 3295 3296

	return error;
}

3297 3298 3299
struct foreach_diff_data {
	git_index *index;
	const git_pathspec *pathspec;
3300
	unsigned int flags;
3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328
	git_index_matched_path_cb cb;
	void *payload;
};

static int apply_each_file(const git_diff_delta *delta, float progress, void *payload)
{
	struct foreach_diff_data *data = payload;
	const char *match, *path;
	int error = 0;

	GIT_UNUSED(progress);

	path = delta->old_file.path;

	/* We only want those which match the pathspecs */
	if (!git_pathspec__match(
		    &data->pathspec->pathspec, path, false, (bool)data->index->ignore_case,
		    &match, NULL))
		return 0;

	if (data->cb)
		error = data->cb(path, match, data->payload);

	if (error > 0) /* skip this entry */
		return 0;
	if (error < 0) /* actual error */
		return error;

3329 3330
	/* If the workdir item does not exist, remove it from the index. */
	if ((delta->new_file.flags & GIT_DIFF_FLAG_EXISTS) == 0)
3331 3332
		error = git_index_remove_bypath(data->index, path);
	else
3333
		error = git_index_add_bypath(data->index, delta->new_file.path);
3334 3335 3336 3337 3338

	return error;
}

static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
3339
				  unsigned int flags,
3340 3341 3342 3343 3344 3345
				  git_index_matched_path_cb cb, void *payload)
{
	int error;
	git_diff *diff;
	git_pathspec ps;
	git_repository *repo;
3346
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
3347 3348 3349
	struct foreach_diff_data data = {
		index,
		NULL,
3350
		flags,
3351 3352 3353 3354 3355
		cb,
		payload,
	};

	assert(index);
3356
	assert(action == INDEX_ACTION_UPDATE || action == INDEX_ACTION_ADDALL);
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372

	repo = INDEX_OWNER(index);

	if (!repo) {
		return create_index_error(-1,
			"cannot run update; the index is not backed up by a repository.");
	}

	/*
	 * We do the matching ourselves intead of passing the list to
	 * diff because we want to tell the callback which one
	 * matched, which we do not know if we ask diff to filter for us.
	 */
	if ((error = git_pathspec__init(&ps, paths)) < 0)
		return error;

3373
	opts.flags = GIT_DIFF_INCLUDE_TYPECHANGE;
3374
	if (action == INDEX_ACTION_ADDALL) {
3375
		opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
3376
			GIT_DIFF_RECURSE_UNTRACKED_DIRS;
3377

3378 3379 3380 3381 3382
		if (flags == GIT_INDEX_ADD_FORCE)
			opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
	}

	if ((error = git_diff_index_to_workdir(&diff, repo, index, &opts)) < 0)
3383 3384 3385
		goto cleanup;

	data.pathspec = &ps;
3386
	error = git_diff_foreach(diff, apply_each_file, NULL, NULL, NULL, &data);
3387 3388 3389 3390 3391 3392 3393 3394 3395 3396
	git_diff_free(diff);

	if (error) /* make sure error is set if callback stopped iteration */
		giterr_set_after_callback(error);

cleanup:
	git_pathspec__clear(&ps);
	return error;
}

3397 3398 3399 3400 3401 3402 3403 3404 3405
static int index_apply_to_all(
	git_index *index,
	int action,
	const git_strarray *paths,
	git_index_matched_path_cb cb,
	void *payload)
{
	int error = 0;
	size_t i;
3406
	git_pathspec ps;
3407
	const char *match;
3408
	git_buf path = GIT_BUF_INIT;
3409 3410 3411

	assert(index);

3412
	if ((error = git_pathspec__init(&ps, paths)) < 0)
3413 3414 3415 3416 3417 3418 3419 3420
		return error;

	git_vector_sort(&index->entries);

	for (i = 0; !error && i < index->entries.length; ++i) {
		git_index_entry *entry = git_vector_get(&index->entries, i);

		/* check if path actually matches */
3421
		if (!git_pathspec__match(
Linquize committed
3422
				&ps.pathspec, entry->path, false, (bool)index->ignore_case,
3423
				&match, NULL))
3424 3425 3426 3427 3428 3429 3430 3431
			continue;

		/* issue notification callback if requested */
		if (cb && (error = cb(entry->path, match, payload)) != 0) {
			if (error > 0) { /* return > 0 means skip this one */
				error = 0;
				continue;
			}
3432
			if (error < 0)   /* return < 0 means abort */
3433 3434 3435
				break;
		}

3436 3437 3438 3439
		/* index manipulation may alter entry, so don't depend on it */
		if ((error = git_buf_sets(&path, entry->path)) < 0)
			break;

3440 3441 3442 3443
		switch (action) {
		case INDEX_ACTION_NONE:
			break;
		case INDEX_ACTION_UPDATE:
3444
			error = git_index_add_bypath(index, path.ptr);
3445 3446 3447 3448

			if (error == GIT_ENOTFOUND) {
				giterr_clear();

3449
				error = git_index_remove_bypath(index, path.ptr);
3450 3451 3452 3453 3454 3455

				if (!error) /* back up foreach if we removed this */
					i--;
			}
			break;
		case INDEX_ACTION_REMOVE:
3456
			if (!(error = git_index_remove_bypath(index, path.ptr)))
3457 3458 3459
				i--; /* back up foreach if we removed this */
			break;
		default:
3460
			giterr_set(GITERR_INVALID, "unknown index action %d", action);
3461 3462 3463 3464 3465
			error = -1;
			break;
		}
	}

3466
	git_buf_free(&path);
3467
	git_pathspec__clear(&ps);
3468 3469 3470 3471 3472 3473 3474 3475 3476 3477

	return error;
}

int git_index_remove_all(
	git_index *index,
	const git_strarray *pathspec,
	git_index_matched_path_cb cb,
	void *payload)
{
3478
	int error = index_apply_to_all(
3479
		index, INDEX_ACTION_REMOVE, pathspec, cb, payload);
3480 3481 3482 3483 3484

	if (error) /* make sure error is set if callback stopped iteration */
		giterr_set_after_callback(error);

	return error;
3485 3486 3487 3488 3489 3490 3491 3492
}

int git_index_update_all(
	git_index *index,
	const git_strarray *pathspec,
	git_index_matched_path_cb cb,
	void *payload)
{
3493
	int error = index_apply_to_wd_diff(index, INDEX_ACTION_UPDATE, pathspec, 0, cb, payload);
3494 3495 3496 3497
	if (error) /* make sure error is set if callback stopped iteration */
		giterr_set_after_callback(error);

	return error;
3498
}
3499

3500
int git_index_snapshot_new(git_vector *snap, git_index *index)
3501 3502 3503 3504
{
	int error;

	GIT_REFCOUNT_INC(index);
3505 3506

	git_atomic_inc(&index->readers);
3507
	git_vector_sort(&index->entries);
3508

3509
	error = git_vector_dup(snap, &index->entries, index->entries._cmp);
3510 3511 3512 3513 3514 3515 3516

	if (error < 0)
		git_index_free(index);

	return error;
}

3517
void git_index_snapshot_release(git_vector *snap, git_index *index)
3518
{
3519 3520
	git_vector_free(snap);

3521 3522
	git_atomic_dec(&index->readers);

3523
	git_index_free(index);
3524
}
3525 3526 3527 3528 3529 3530 3531

int git_index_snapshot_find(
	size_t *out, git_vector *entries, git_vector_cmp entry_srch,
	const char *path, size_t path_len, int stage)
{
	return index_find_in_entries(out, entries, entry_srch, path, path_len, stage);
}
3532 3533 3534 3535 3536 3537 3538

int git_indexwriter_init(
	git_indexwriter *writer,
	git_index *index)
{
	int error;

3539 3540
	GIT_REFCOUNT_INC(index);

3541 3542 3543 3544
	writer->index = index;

	if (!index->index_file_path)
		return create_index_error(-1,
3545
			"failed to write index: The index is in-memory only");
3546 3547 3548

	if ((error = git_filebuf_open(
		&writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
3549

3550
		if (error == GIT_ELOCKED)
3551
			giterr_set(GITERR_INDEX, "the index is locked; this might be due to a concurrent or crashed process");
3552 3553 3554 3555

		return error;
	}

3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
	writer->should_write = 1;

	return 0;
}

int git_indexwriter_init_for_operation(
	git_indexwriter *writer,
	git_repository *repo,
	unsigned int *checkout_strategy)
{
	git_index *index;
	int error;

	if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
		(error = git_indexwriter_init(writer, index)) < 0)
		return error;

	writer->should_write = (*checkout_strategy & GIT_CHECKOUT_DONT_WRITE_INDEX) == 0;
	*checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX;

3576 3577 3578 3579 3580 3581
	return 0;
}

int git_indexwriter_commit(git_indexwriter *writer)
{
	int error;
3582
	git_oid checksum = {{ 0 }};
3583

3584 3585 3586
	if (!writer->should_write)
		return 0;

3587
	git_vector_sort(&writer->index->entries);
3588 3589
	git_vector_sort(&writer->index->reuc);

3590
	if ((error = write_index(&checksum, writer->index, &writer->file)) < 0) {
3591 3592 3593 3594 3595 3596 3597 3598 3599
		git_indexwriter_cleanup(writer);
		return error;
	}

	if ((error = git_filebuf_commit(&writer->file)) < 0)
		return error;

	if ((error = git_futils_filestamp_check(
		&writer->index->stamp, writer->index->index_file_path)) < 0) {
3600
		giterr_set(GITERR_OS, "could not read index timestamp");
3601 3602 3603 3604
		return -1;
	}

	writer->index->on_disk = 1;
3605
	git_oid_cpy(&writer->index->checksum, &checksum);
3606

3607 3608 3609
	git_index_free(writer->index);
	writer->index = NULL;

3610 3611 3612 3613 3614 3615
	return 0;
}

void git_indexwriter_cleanup(git_indexwriter *writer)
{
	git_filebuf_cleanup(&writer->file);
3616 3617 3618

	git_index_free(writer->index);
	writer->index = NULL;
3619
}