index.c 90 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
#include "path.h"
24

25
#include "git2/odb.h"
26
#include "git2/oid.h"
27
#include "git2/blob.h"
28
#include "git2/config.h"
29
#include "git2/sys/index.h"
30

31 32 33 34
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);

35 36 37 38 39
#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
40 41
static const unsigned int INDEX_VERSION_NUMBER_DEFAULT = 2;
static const unsigned int INDEX_VERSION_NUMBER_LB = 2;
42
static const unsigned int INDEX_VERSION_NUMBER_EXT = 3;
David Turner committed
43 44
static const unsigned int INDEX_VERSION_NUMBER_COMP = 4;
static const unsigned int INDEX_VERSION_NUMBER_UB = 4;
45 46 47

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

51 52
#define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx)))

53 54 55 56 57 58 59 60 61 62 63
struct index_header {
	uint32_t signature;
	uint32_t version;
	uint32_t entry_count;
};

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

64 65 66 67 68
struct entry_time {
	uint32_t seconds;
	uint32_t nanoseconds;
};

69
struct entry_short {
70 71
	struct entry_time ctime;
	struct entry_time mtime;
72 73 74 75 76 77 78 79
	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
80
	char path[1]; /* arbitrary length */
81 82 83
};

struct entry_long {
84 85
	struct entry_time ctime;
	struct entry_time mtime;
86 87 88 89 90 91 92 93 94
	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
95
	char path[1]; /* arbitrary length */
96 97
};

Edward Thomson committed
98 99
struct entry_srch_key {
	const char *path;
100
	size_t pathlen;
Edward Thomson committed
101 102 103
	int stage;
};

104 105 106 107 108 109 110 111 112 113 114 115
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];
};

116 117
bool git_index__enforce_unsaved_safety = false;

118
/* local declarations */
119
static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size);
120 121
static int read_header(struct index_header *dest, const void *buffer);

122
static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
123
static bool is_index_extended(git_index *index);
124
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file);
125

126
static void index_entry_free(git_index_entry *entry);
Edward Thomson committed
127 128
static void index_entry_reuc_free(git_index_reuc_entry *reuc);

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
GIT_INLINE(int) index_map_set(git_idxmap *map, git_index_entry *e, bool ignore_case)
{
	if (ignore_case)
		return git_idxmap_icase_set((git_idxmap_icase *) map, e, e);
	else
		return git_idxmap_set(map, e, e);
}

GIT_INLINE(int) index_map_delete(git_idxmap *map, git_index_entry *e, bool ignore_case)
{
	if (ignore_case)
		return git_idxmap_icase_delete((git_idxmap_icase *) map, e);
	else
		return git_idxmap_delete(map, e);
}

145 146 147 148 149 150 151 152
GIT_INLINE(int) index_map_resize(git_idxmap *map, size_t count, bool ignore_case)
{
	if (ignore_case)
		return git_idxmap_icase_resize((git_idxmap_icase *) map, count);
	else
		return git_idxmap_resize(map, count);
}

153
int git_index_entry_srch(const void *key, const void *array_member)
154
{
Edward Thomson committed
155
	const struct entry_srch_key *srch_key = key;
156
	const struct entry_internal *entry = array_member;
157 158
	int cmp;
	size_t len1, len2, len;
159

160 161
	len1 = srch_key->pathlen;
	len2 = entry->pathlen;
162
	len = len1 < len2 ? len1 : len2;
Edward Thomson committed
163

164 165 166 167 168 169 170
	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
171

172
	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
173
		return srch_key->stage - GIT_INDEX_ENTRY_STAGE(&entry->entry);
174 175

	return 0;
176 177
}

178
int git_index_entry_isrch(const void *key, const void *array_member)
179
{
Edward Thomson committed
180
	const struct entry_srch_key *srch_key = key;
181
	const struct entry_internal *entry = array_member;
182 183
	int cmp;
	size_t len1, len2, len;
Edward Thomson committed
184

185 186
	len1 = srch_key->pathlen;
	len2 = entry->pathlen;
187
	len = len1 < len2 ? len1 : len2;
Edward Thomson committed
188

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

191 192 193 194 195 196 197 198
	if (cmp)
		return cmp;
	if (len1 < len2)
		return -1;
	if (len1 > len2)
		return 1;

	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
199
		return srch_key->stage - GIT_INDEX_ENTRY_STAGE(&entry->entry);
200 201

	return 0;
Edward Thomson committed
202 203
}

204
static int index_entry_srch_path(const void *path, const void *array_member)
Edward Thomson committed
205
{
206 207
	const git_index_entry *entry = array_member;

Edward Thomson committed
208 209 210
	return strcmp((const char *)path, entry->path);
}

211
static int index_entry_isrch_path(const void *path, const void *array_member)
Edward Thomson committed
212 213 214 215
{
	const git_index_entry *entry = array_member;

	return strcasecmp((const char *)path, entry->path);
216 217
}

218
int git_index_entry_cmp(const void *a, const void *b)
219
{
Edward Thomson committed
220
	int diff;
221 222
	const git_index_entry *entry_a = a;
	const git_index_entry *entry_b = b;
223

Edward Thomson committed
224 225 226
	diff = strcmp(entry_a->path, entry_b->path);

	if (diff == 0)
227
		diff = (GIT_INDEX_ENTRY_STAGE(entry_a) - GIT_INDEX_ENTRY_STAGE(entry_b));
Edward Thomson committed
228 229

	return diff;
230 231
}

232
int git_index_entry_icmp(const void *a, const void *b)
233
{
Edward Thomson committed
234
	int diff;
235 236 237
	const git_index_entry *entry_a = a;
	const git_index_entry *entry_b = b;

Edward Thomson committed
238 239 240
	diff = strcasecmp(entry_a->path, entry_b->path);

	if (diff == 0)
241
		diff = (GIT_INDEX_ENTRY_STAGE(entry_a) - GIT_INDEX_ENTRY_STAGE(entry_b));
Edward Thomson committed
242 243 244 245

	return diff;
}

Edward Thomson committed
246 247 248 249
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;
250

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

Edward Thomson committed
254 255
	if (!name_a->ancestor && name_b->ancestor)
		return -1;
256

Edward Thomson committed
257 258
	if (name_a->ancestor)
		return strcmp(name_a->ancestor, name_b->ancestor);
259

Edward Thomson committed
260 261
	if (!name_a->ours || !name_b->ours)
		return 0;
262

Edward Thomson committed
263 264 265
	return strcmp(name_a->ours, name_b->ours);
}

Vicent Marti committed
266 267 268
/**
 * TODO: enable this when resolving case insensitive conflicts
 */
269
#if 0
Edward Thomson committed
270 271 272 273
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;
274

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

Edward Thomson committed
278 279
	if (!name_a->ancestor && name_b->ancestor)
		return -1;
280

Edward Thomson committed
281 282
	if (name_a->ancestor)
		return strcasecmp(name_a->ancestor, name_b->ancestor);
283

Edward Thomson committed
284 285
	if (!name_a->ours || !name_b->ours)
		return 0;
286

Edward Thomson committed
287 288
	return strcasecmp(name_a->ours, name_b->ours);
}
289
#endif
Edward Thomson committed
290

Edward Thomson committed
291 292 293 294 295
static int reuc_srch(const void *key, const void *array_member)
{
	const git_index_reuc_entry *reuc = array_member;

	return strcmp(key, reuc->path);
296 297
}

Edward Thomson committed
298
static int reuc_isrch(const void *key, const void *array_member)
299
{
Edward Thomson committed
300
	const git_index_reuc_entry *reuc = array_member;
301

Edward Thomson committed
302
	return strcasecmp(key, reuc->path);
303 304
}

Edward Thomson committed
305
static int reuc_cmp(const void *a, const void *b)
306
{
Edward Thomson committed
307 308
	const git_index_reuc_entry *info_a = a;
	const git_index_reuc_entry *info_b = b;
309 310 311

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

Edward Thomson committed
313 314 315 316 317 318 319 320
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);
}

321 322 323 324 325 326 327
static void index_entry_reuc_free(git_index_reuc_entry *reuc)
{
	git__free(reuc);
}

static void index_entry_free(git_index_entry *entry)
{
328 329 330
	if (!entry)
		return;

331
	memset(&entry->id, 0, sizeof(entry->id));
332 333 334
	git__free(entry);
}

335
unsigned int git_index__create_mode(unsigned int mode)
336 337 338
{
	if (S_ISLNK(mode))
		return S_IFLNK;
339

340 341
	if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR))
		return (S_IFLNK | S_IFDIR);
342

343
	return S_IFREG | GIT_PERMS_CANONICAL(mode);
344 345
}

346 347 348 349 350 351 352 353 354
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)) ?
355
			existing->mode : git_index__create_mode(0666);
356

357
	return git_index__create_mode(mode);
358 359
}

360 361 362 363 364 365 366 367 368 369 370 371 372
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,
373
	const char *path, size_t path_len, int stage)
374
{
375
	git_vector_sort(&index->entries);
376 377 378 379 380

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

381
void git_index__set_ignore_case(git_index *index, bool ignore_case)
382
{
383 384
	index->ignore_case = ignore_case;

385 386 387 388 389 390 391 392 393 394 395
	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;
	}
396

397 398
	git_vector_set_cmp(&index->entries,
		ignore_case ? git_index_entry_icmp : git_index_entry_cmp);
399
	git_vector_sort(&index->entries);
Edward Thomson committed
400

401
	git_vector_set_cmp(&index->reuc, ignore_case ? reuc_icmp : reuc_cmp);
Edward Thomson committed
402
	git_vector_sort(&index->reuc);
403 404
}

405
int git_index_open(git_index **index_out, const char *index_path)
406 407
{
	git_index *index;
408
	int error = -1;
409

Edward Thomson committed
410
	GIT_ASSERT_ARG(index_out);
411

412
	index = git__calloc(1, sizeof(git_index));
413
	GIT_ERROR_CHECK_ALLOC(index);
414

415 416
	if (git_pool_init(&index->tree_pool, 1) < 0)
		goto fail;
417

418 419
	if (index_path != NULL) {
		index->index_file_path = git__strdup(index_path);
420 421
		if (!index->index_file_path)
			goto fail;
422 423

		/* Check if index file is stored on disk already */
424
		if (git_fs_path_exists(index->index_file_path) == true)
425 426
			index->on_disk = 1;
	}
427

428
	if (git_vector_init(&index->entries, 32, git_index_entry_cmp) < 0 ||
429 430 431 432
	    git_idxmap_new(&index->entries_map) < 0 ||
	    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)
433
		goto fail;
434

435 436 437
	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
438
	index->reuc_search = reuc_srch;
David Turner committed
439
	index->version = INDEX_VERSION_NUMBER_DEFAULT;
440

441 442
	if (index_path != NULL && (error = git_index_read(index, true)) < 0)
		goto fail;
443

Vicent Marti committed
444
	*index_out = index;
445
	GIT_REFCOUNT_INC(index);
446

447
	return 0;
448 449

fail:
450
	git_pool_clear(&index->tree_pool);
451 452
	git_index_free(index);
	return error;
453 454
}

455 456 457 458 459
int git_index_new(git_index **out)
{
	return git_index_open(out, NULL);
}

460
static void index_free(git_index *index)
461
{
462 463 464
	/* index iterators increment the refcount of the index, so if we
	 * get here then there should be no outstanding iterators.
	 */
465
	if (git_atomic32_get(&index->readers))
Edward Thomson committed
466
		return;
467

468
	git_index_clear(index);
469
	git_idxmap_free(index->entries_map);
470
	git_vector_free(&index->entries);
Edward Thomson committed
471
	git_vector_free(&index->names);
Edward Thomson committed
472
	git_vector_free(&index->reuc);
473
	git_vector_free(&index->deleted);
474

475
	git__free(index->index_file_path);
476

477
	git__memzero(index, sizeof(*index));
478
	git__free(index);
479 480
}

481 482
void git_index_free(git_index *index)
{
483
	if (index == NULL)
484 485
		return;

486
	GIT_REFCOUNT_DEC(index, index_free);
487 488
}

489 490
/* call with locked index */
static void index_free_deleted(git_index *index)
491
{
492
	int readers = (int)git_atomic32_get(&index->readers);
493
	size_t i;
494

495
	if (readers > 0 || !index->deleted.length)
496 497
		return;

498
	for (i = 0; i < index->deleted.length; ++i) {
499
		git_index_entry *ie = git_atomic_swap(index->deleted.contents[i], NULL);
500 501
		index_entry_free(ie);
	}
502 503 504 505

	git_vector_clear(&index->deleted);
}

506 507
/* call with locked index */
static int index_remove_entry(git_index *index, size_t pos)
508 509 510 511
{
	int error = 0;
	git_index_entry *entry = git_vector_get(&index->entries, pos);

512
	if (entry != NULL) {
513
		git_tree_cache_invalidate_path(index->tree, entry->path);
514
		index_map_delete(index->entries_map, entry, index->ignore_case);
515
	}
516 517 518 519

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

	if (!error) {
520
		if (git_atomic32_get(&index->readers) > 0) {
521
			error = git_vector_insert(&index->deleted, entry);
522
		} else {
523
			index_entry_free(entry);
524
		}
525 526

		index->dirty = 1;
527 528 529
	}

	return error;
530
}
531

532
int git_index_clear(git_index *index)
533
{
534 535
	int error = 0;

Edward Thomson committed
536
	GIT_ASSERT_ARG(index);
537

538
	index->dirty = 1;
539
	index->tree = NULL;
540
	git_pool_clear(&index->tree_pool);
541

542
	git_idxmap_clear(index->entries_map);
543
	while (!error && index->entries.length > 0)
544
		error = index_remove_entry(index, index->entries.length - 1);
545 546 547 548

	if (error)
		goto done;

549 550
	index_free_deleted(index);

551 552 553
	if ((error = git_index_name_clear(index)) < 0 ||
		(error = git_index_reuc_clear(index)) < 0)
	    goto done;
nulltoken committed
554

555
	git_futils_filestamp_set(&index->stamp, NULL);
556

557
done:
558
	return error;
559 560
}

561 562
static int create_index_error(int error, const char *msg)
{
563
	git_error_set_str(GIT_ERROR_INDEX, msg);
564 565 566
	return error;
}

Russell Belfer committed
567
int git_index_set_caps(git_index *index, int caps)
568
{
Linquize committed
569
	unsigned int old_ignore_case;
570

Edward Thomson committed
571
	GIT_ASSERT_ARG(index);
572

573 574
	old_ignore_case = index->ignore_case;

575
	if (caps == GIT_INDEX_CAPABILITY_FROM_OWNER) {
576
		git_repository *repo = INDEX_OWNER(index);
577 578
		int val;

579 580
		if (!repo)
			return create_index_error(
581
				-1, "cannot access repository to set index caps");
582

583
		if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_IGNORECASE))
584
			index->ignore_case = (val != 0);
585
		if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_FILEMODE))
586
			index->distrust_filemode = (val == 0);
587
		if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_SYMLINKS))
588
			index->no_symlinks = (val == 0);
589 590
	}
	else {
591 592 593
		index->ignore_case = ((caps & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0);
		index->distrust_filemode = ((caps & GIT_INDEX_CAPABILITY_NO_FILEMODE) != 0);
		index->no_symlinks = ((caps & GIT_INDEX_CAPABILITY_NO_SYMLINKS) != 0);
594 595
	}

596
	if (old_ignore_case != index->ignore_case) {
Linquize committed
597
		git_index__set_ignore_case(index, (bool)index->ignore_case);
598 599
	}

600 601 602
	return 0;
}

Russell Belfer committed
603
int git_index_caps(const git_index *index)
604
{
605 606 607
	return ((index->ignore_case ? GIT_INDEX_CAPABILITY_IGNORE_CASE : 0) |
			(index->distrust_filemode ? GIT_INDEX_CAPABILITY_NO_FILEMODE : 0) |
			(index->no_symlinks ? GIT_INDEX_CAPABILITY_NO_SYMLINKS : 0));
608 609
}

610 611 612 613 614 615 616 617 618 619
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)
{
620
	int fd;
621 622 623 624 625 626
	ssize_t bytes_read;
	git_oid checksum = {{ 0 }};

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

627
	if (p_lseek(fd, -20, SEEK_END) < 0) {
628
		p_close(fd);
629
		git_error_set(GIT_ERROR_OS, "failed to seek to end of file");
630 631 632 633 634 635 636 637 638 639 640 641
		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);
}

642
int git_index_read(git_index *index, int force)
643
{
644
	int error = 0, updated;
645
	git_str buffer = GIT_STR_INIT;
646
	git_futils_filestamp stamp = index->stamp;
647

648 649
	if (!index->index_file_path)
		return create_index_error(-1,
650
			"failed to read index: The index is in-memory only");
651

652
	index->on_disk = git_fs_path_exists(index->index_file_path);
653 654

	if (!index->on_disk) {
655 656 657 658
		if (force && (error = git_index_clear(index)) < 0)
			return error;

		index->dirty = 0;
659
		return 0;
660 661
	}

662 663
	if ((updated = git_futils_filestamp_check(&stamp, index->index_file_path) < 0) ||
	    ((updated = compare_checksum(index)) < 0)) {
664 665
		git_error_set(
			GIT_ERROR_INDEX,
666
			"failed to read index: '%s' no longer exists",
667
			index->index_file_path);
668
		return updated;
669
	}
670

671 672
	if (!updated && !force)
		return 0;
673 674

	error = git_futils_readbuffer(&buffer, index->index_file_path);
675 676
	if (error < 0)
		return error;
677

678 679 680
	index->tree = NULL;
	git_pool_clear(&index->tree_pool);

681 682 683 684
	error = git_index_clear(index);

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

686
	if (!error) {
687
		git_futils_filestamp_set(&index->stamp, &stamp);
688 689
		index->dirty = 0;
	}
690

691
	git_str_dispose(&buffer);
692 693 694
	return error;
}

695 696
int git_index_read_safely(git_index *index)
{
697
	if (git_index__enforce_unsaved_safety && index->dirty) {
698
		git_error_set(GIT_ERROR_INDEX,
699
			"the index has unsaved changes that would be overwritten by this operation");
700
		return GIT_EINDEXDIRTY;
701 702 703 704 705
	}

	return git_index_read(index, false);
}

706
int git_index__changed_relative_to(
707
	git_index *index, const git_oid *checksum)
708 709 710
{
	/* attempt to update index (ignoring errors) */
	if (git_index_read(index, false) < 0)
711
		git_error_clear();
712

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

716
static bool is_racy_entry(git_index *index, const git_index_entry *entry)
717 718 719 720 721
{
	/* Git special-cases submodules in the check */
	if (S_ISGITLINK(entry->mode))
		return false;

722
	return git_index_entry_newer_than_index(entry, index);
723 724
}

725 726 727 728
/*
 * Force the next diff to take a look at those entries which have the
 * same timestamp as the current index.
 */
729
static int truncate_racily_clean(git_index *index)
730 731
{
	size_t i;
732
	int error;
733
	git_index_entry *entry;
734
	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
735 736 737
	git_diff *diff = NULL;
	git_vector paths = GIT_VECTOR_INIT;
	git_diff_delta *delta;
738 739 740 741

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

743 744 745 746 747
	/* 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;
748
	git_vector_foreach(&index->entries, i, entry) {
749
		if ((entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE) == 0 &&
750
			is_racy_entry(index, entry))
751 752
			git_vector_insert(&paths, (char *)entry->path);
	}
753

754 755
	if (paths.length == 0)
		goto done;
756

757 758
	diff_opts.pathspec.count = paths.length;
	diff_opts.pathspec.strings = (char **)paths.contents;
759

760 761
	if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0)
		return error;
762

763 764 765 766 767 768
	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.
		 */
769
		if (entry) {
770
			entry->file_size = 0;
771 772
			index->dirty = 1;
		}
773
	}
774

775 776 777
done:
	git_diff_free(diff);
	git_vector_free(&paths);
778
	return 0;
779 780
}

David Turner committed
781 782
unsigned git_index_version(git_index *index)
{
Edward Thomson committed
783
	GIT_ASSERT_ARG(index);
David Turner committed
784 785 786 787 788 789

	return index->version;
}

int git_index_set_version(git_index *index, unsigned int version)
{
Edward Thomson committed
790
	GIT_ASSERT_ARG(index);
David Turner committed
791 792 793

	if (version < INDEX_VERSION_NUMBER_LB ||
	    version > INDEX_VERSION_NUMBER_UB) {
794
		git_error_set(GIT_ERROR_INDEX, "invalid version number");
David Turner committed
795 796 797 798 799 800 801 802
		return -1;
	}

	index->version = version;

	return 0;
}

803 804
int git_index_write(git_index *index)
{
805
	git_indexwriter writer = GIT_INDEXWRITER_INIT;
806
	int error;
807

808 809
	truncate_racily_clean(index);

810 811 812
	if ((error = git_indexwriter_init(&writer, index)) == 0 &&
		(error = git_indexwriter_commit(&writer)) == 0)
		index->dirty = 0;
813

814
	git_indexwriter_cleanup(&writer);
815

816
	return error;
817
}
818

Edward Thomson committed
819
const char *git_index_path(const git_index *index)
820
{
Edward Thomson committed
821
	GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
822 823
	return index->index_file_path;
}
824

825 826 827 828
int git_index_write_tree(git_oid *oid, git_index *index)
{
	git_repository *repo;

Edward Thomson committed
829 830
	GIT_ASSERT_ARG(oid);
	GIT_ASSERT_ARG(index);
831

832
	repo = INDEX_OWNER(index);
833

834 835
	if (repo == NULL)
		return create_index_error(-1, "Failed to write tree. "
836
		  "the index file is not backed up by an existing repository");
837 838 839 840

	return git_tree__write_index(oid, index, repo);
}

841 842
int git_index_write_tree_to(
	git_oid *oid, git_index *index, git_repository *repo)
843
{
Edward Thomson committed
844 845 846 847
	GIT_ASSERT_ARG(oid);
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(repo);

848 849 850
	return git_tree__write_index(oid, index, repo);
}

851
size_t git_index_entrycount(const git_index *index)
852
{
Edward Thomson committed
853 854
	GIT_ASSERT_ARG(index);

855
	return index->entries.length;
856 857
}

858 859
const git_index_entry *git_index_get_byindex(
	git_index *index, size_t n)
860
{
Edward Thomson committed
861 862
	GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);

863
	git_vector_sort(&index->entries);
Edward Thomson committed
864
	return git_vector_get(&index->entries, n);
865 866
}

867 868
const git_index_entry *git_index_get_bypath(
	git_index *index, const char *path, int stage)
869
{
870
	git_index_entry key = {{ 0 }};
871
	git_index_entry *value;
Edward Thomson committed
872

Edward Thomson committed
873
	GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
Edward Thomson committed
874

875
	key.path = path;
876
	GIT_INDEX_ENTRY_STAGE_SET(&key, stage);
877

878 879 880 881
	if (index->ignore_case)
		value = git_idxmap_icase_get((git_idxmap_icase *) index->entries_map, &key);
	else
		value = git_idxmap_get(index->entries_map, &key);
Edward Thomson committed
882

883 884 885 886
	if (!value) {
	    git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path);
	    return NULL;
	}
887

888
	return value;
889 890
}

891 892
void git_index_entry__init_from_stat(
	git_index_entry *entry, struct stat *st, bool trust_mode)
893
{
894 895
	entry->ctime.seconds = (int32_t)st->st_ctime;
	entry->mtime.seconds = (int32_t)st->st_mtime;
896
#if defined(GIT_USE_NSEC)
897 898
	entry->mtime.nanoseconds = st->st_mtime_nsec;
	entry->ctime.nanoseconds = st->st_ctime_nsec;
899
#endif
900 901
	entry->dev  = st->st_rdev;
	entry->ino  = st->st_ino;
902
	entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ?
903
		git_index__create_mode(0666) : git_index__create_mode(st->st_mode);
904 905
	entry->uid  = st->st_uid;
	entry->gid  = st->st_gid;
906
	entry->file_size = (uint32_t)st->st_size;
907 908
}

909 910 911 912
static void index_entry_adjust_namemask(
		git_index_entry *entry,
		size_t path_length)
{
913
	entry->flags &= ~GIT_INDEX_ENTRY_NAMEMASK;
914

915 916
	if (path_length < GIT_INDEX_ENTRY_NAMEMASK)
		entry->flags |= path_length & GIT_INDEX_ENTRY_NAMEMASK;
917
	else
918
		entry->flags |= GIT_INDEX_ENTRY_NAMEMASK;
919 920
}

921 922 923 924 925 926
/* 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.
 */
927 928 929
static int index_entry_create(
	git_index_entry **out,
	git_repository *repo,
930
	const char *path,
931
	struct stat *st,
932
	bool from_workdir)
933
{
934
	size_t pathlen = strlen(path), alloclen;
935
	struct entry_internal *entry;
936
	unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS;
937
	uint16_t mode = 0;
938

939 940 941 942 943 944
	/* 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;
945 946
	if (st)
		mode = st->st_mode;
947

948
	if (!git_path_is_valid(repo, path, mode, path_valid_flags)) {
949
		git_error_set(GIT_ERROR_INDEX, "invalid path: '%s'", path);
950
		return -1;
951
	}
952

953 954
	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(struct entry_internal), pathlen);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
955
	entry = git__calloc(1, alloclen);
956
	GIT_ERROR_CHECK_ALLOC(entry);
957 958 959 960 961

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

962 963
	*out = (git_index_entry *)entry;
	return 0;
964 965
}

966
static int index_entry_init(
967 968 969
	git_index_entry **entry_out,
	git_index *index,
	const char *rel_path)
970
{
971
	int error = 0;
972
	git_index_entry *entry = NULL;
973
	git_str path = GIT_STR_INIT;
974 975
	struct stat st;
	git_oid oid;
976
	git_repository *repo;
977

978 979
	if (INDEX_OWNER(index) == NULL)
		return create_index_error(-1,
980
			"could not initialize index entry. "
981 982
			"Index is not backed up by an existing repository.");

983 984 985 986 987 988 989 990 991
	/*
	 * 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;

992
	if (git_repository_workdir_path(&path, repo, rel_path) < 0)
993 994
		return -1;

995
	error = git_fs_path_lstat(path.ptr, &st);
996
	git_str_dispose(&path);
997 998 999 1000 1001

	if (error < 0)
		return error;

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

1004 1005 1006
	/* 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);
1007

1008 1009 1010 1011
	if (error < 0) {
		index_entry_free(entry);
		return error;
	}
1012

1013
	entry->id = oid;
1014
	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
1015

1016
	*entry_out = (git_index_entry *)entry;
1017
	return 0;
1018 1019
}

1020 1021
static git_index_reuc_entry *reuc_entry_alloc(const char *path)
{
1022
	size_t pathlen = strlen(path),
1023 1024
		structlen = sizeof(struct reuc_entry_internal),
		alloclen;
1025 1026
	struct reuc_entry_internal *entry;

1027 1028
	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, structlen, pathlen) ||
		GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1))
1029 1030
		return NULL;

1031
	entry = git__calloc(1, alloclen);
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	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
1042 1043
static int index_entry_reuc_init(git_index_reuc_entry **reuc_out,
	const char *path,
Edward Thomson committed
1044 1045 1046
	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
1047 1048 1049
{
	git_index_reuc_entry *reuc = NULL;

Edward Thomson committed
1050 1051
	GIT_ASSERT_ARG(reuc_out);
	GIT_ASSERT_ARG(path);
Edward Thomson committed
1052

1053
	*reuc_out = reuc = reuc_entry_alloc(path);
1054
	GIT_ERROR_CHECK_ALLOC(reuc);
Edward Thomson committed
1055

1056
	if ((reuc->mode[0] = ancestor_mode) > 0) {
Edward Thomson committed
1057
		GIT_ASSERT(ancestor_oid);
1058
		git_oid_cpy(&reuc->oid[0], ancestor_oid);
1059
	}
Edward Thomson committed
1060

1061
	if ((reuc->mode[1] = our_mode) > 0) {
Edward Thomson committed
1062
		GIT_ASSERT(our_oid);
1063
		git_oid_cpy(&reuc->oid[1], our_oid);
1064
	}
Edward Thomson committed
1065

1066
	if ((reuc->mode[2] = their_mode) > 0) {
Edward Thomson committed
1067
		GIT_ASSERT(their_oid);
1068
		git_oid_cpy(&reuc->oid[2], their_oid);
1069
	}
Edward Thomson committed
1070 1071 1072 1073

	return 0;
}

1074 1075
static void index_entry_cpy(
	git_index_entry *tgt,
1076
	const git_index_entry *src)
1077
{
1078
	const char *tgt_path = tgt->path;
1079
	memcpy(tgt, src, sizeof(*tgt));
1080
	tgt->path = tgt_path;
1081 1082
}

1083 1084
static int index_entry_dup(
	git_index_entry **out,
1085
	git_index *index,
1086
	const git_index_entry *src)
1087
{
1088
	if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0)
1089
		return -1;
1090

1091 1092 1093
	index_entry_cpy(*out, src);
	return 0;
}
1094

1095 1096 1097 1098 1099 1100 1101
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;
1102
	tgt->flags_extended = (src->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS);
1103 1104 1105 1106 1107 1108 1109
}

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

1113
	index_entry_cpy_nocache(*out, src);
1114
	return 0;
1115 1116
}

1117
static int has_file_name(git_index *index,
1118
	 const git_index_entry *entry, size_t pos, int ok_to_replace)
1119
{
1120
	size_t len = strlen(entry->path);
1121
	int stage = GIT_INDEX_ENTRY_STAGE(entry);
1122
	const char *name = entry->path;
1123

1124
	while (pos < index->entries.length) {
1125
		struct entry_internal *p = index->entries.contents[pos++];
1126

1127
		if (len >= p->pathlen)
1128
			break;
1129
		if (memcmp(name, p->path, len))
1130
			break;
1131
		if (GIT_INDEX_ENTRY_STAGE(&p->entry) != stage)
1132
			continue;
1133
		if (p->path[len] != '/')
1134 1135
			continue;
		if (!ok_to_replace)
1136
			return -1;
1137

1138
		if (index_remove_entry(index, --pos) < 0)
1139
			break;
1140
	}
1141
	return 0;
1142 1143 1144 1145 1146 1147 1148 1149 1150
}

/*
 * 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)
{
1151
	int stage = GIT_INDEX_ENTRY_STAGE(entry);
1152 1153
	const char *name = entry->path;
	const char *slash = name + strlen(name);
1154 1155

	for (;;) {
1156
		size_t len, pos;
1157 1158 1159 1160

		for (;;) {
			if (*--slash == '/')
				break;
1161
			if (slash <= entry->path)
1162
				return 0;
1163 1164 1165
		}
		len = slash - name;

1166
		if (!index_find(&pos, index, name, len, stage)) {
1167
			if (!ok_to_replace)
1168
				return -1;
1169

1170
			if (index_remove_entry(index, pos) < 0)
1171
				break;
1172
			continue;
1173 1174 1175 1176 1177 1178 1179
		}

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

1183 1184
			if (p->pathlen <= len ||
			    p->path[len] != '/' ||
1185
			    memcmp(p->path, name, len))
1186
				break; /* not our subdirectory */
1187

1188
			if (GIT_INDEX_ENTRY_STAGE(&p->entry) == stage)
1189
				return 0;
1190 1191
		}
	}
1192

1193
	return 0;
1194
}
1195

1196
static int check_file_directory_collision(git_index *index,
1197 1198
		git_index_entry *entry, size_t pos, int ok_to_replace)
{
1199 1200
	if (has_file_name(index, entry, pos, ok_to_replace) < 0 ||
	    has_dir_name(index, entry, ok_to_replace) < 0) {
1201
		git_error_set(GIT_ERROR_INDEX,
1202
			"'%s' appears as both a file and a directory", entry->path);
1203 1204 1205 1206 1207
		return -1;
	}

	return 0;
}
1208

1209
static int canonicalize_directory_path(
1210 1211 1212
	git_index *index,
	git_index_entry *entry,
	git_index_entry *existing)
1213 1214 1215 1216 1217 1218 1219 1220 1221
{
	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 */
1222 1223
	if (existing) {
		memcpy((char *)entry->path, existing->path, strlen(existing->path));
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
		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))) {
1246
			if (GIT_INDEX_ENTRY_STAGE(match) != 0) {
1247
				/* conflicts do not contribute to canonical paths */
1248
			} else if (strncmp(search, match->path, search_len) == 0) {
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
				/* 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;
}

1279 1280 1281 1282
static int index_no_dups(void **old, void *new)
{
	const git_index_entry *entry = new;
	GIT_UNUSED(old);
1283
	git_error_set(GIT_ERROR_INDEX, "'%s' appears multiple times at stage %d",
1284
		entry->path, GIT_INDEX_ENTRY_STAGE(entry));
1285 1286 1287
	return GIT_EEXISTS;
}

1288
static void index_existing_and_best(
1289
	git_index_entry **existing,
1290
	size_t *existing_position,
1291
	git_index_entry **best,
1292 1293 1294
	git_index *index,
	const git_index_entry *entry)
{
1295
	git_index_entry *e;
1296 1297 1298 1299
	size_t pos;
	int error;

	error = index_find(&pos,
1300
		index, entry->path, 0, GIT_INDEX_ENTRY_STAGE(entry));
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312

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

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

1313
	if (GIT_INDEX_ENTRY_STAGE(entry) == 0) {
1314 1315 1316 1317 1318 1319 1320 1321 1322
		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;

1323
			if (GIT_INDEX_ENTRY_STAGE(e) == GIT_INDEX_STAGE_ANCESTOR) {
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
				*best = e;
				continue;
			} else {
				*best = e;
				break;
			}
		}
	}
}

1334 1335 1336 1337
/* 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).
1338
 *
1339 1340 1341
 * 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.
1342
 *
1343
 * trust_mode is whether we trust the mode in entry_ptr.
1344 1345
 *
 * trust_id is whether we trust the id or it should be validated.
1346 1347
 */
static int index_insert(
1348 1349 1350 1351
	git_index *index,
	git_index_entry **entry_ptr,
	int replace,
	bool trust_path,
1352 1353
	bool trust_mode,
	bool trust_id)
1354
{
1355
	git_index_entry *existing, *best, *entry;
1356 1357
	size_t path_length, position;
	int error;
1358

Edward Thomson committed
1359 1360
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(entry_ptr);
1361 1362

	entry = *entry_ptr;
1363

1364
	/* Make sure that the path length flag is correct */
1365
	path_length = ((struct entry_internal *)entry)->pathlen;
1366
	index_entry_adjust_namemask(entry, path_length);
1367

1368
	/* This entry is now up-to-date and should not be checked for raciness */
1369
	entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE;
1370

1371 1372
	git_vector_sort(&index->entries);

1373 1374
	/*
	 * Look if an entry with this path already exists, either staged, or (if
1375 1376 1377 1378
	 * this entry is a regular staged item) as the "ours" side of a conflict.
	 */
	index_existing_and_best(&existing, &position, &best, index, entry);

1379
	/* Update the file mode */
1380 1381 1382
	entry->mode = trust_mode ?
		git_index__create_mode(entry->mode) :
		index_merge_mode(index, best, entry->mode);
1383

1384 1385 1386
	/* Canonicalize the directory name */
	if (!trust_path && (error = canonicalize_directory_path(index, entry, best)) < 0)
		goto out;
1387

1388 1389 1390
	/* Ensure that the given id exists (unless it's a submodule) */
	if (!trust_id && INDEX_OWNER(index) &&
	    (entry->mode & GIT_FILEMODE_COMMIT) != GIT_FILEMODE_COMMIT) {
1391 1392

		if (!git_object__is_valid(INDEX_OWNER(index), &entry->id,
1393
					  git_object__type_from_filemode(entry->mode))) {
1394
			error = -1;
1395 1396
			goto out;
		}
1397 1398
	}

1399 1400 1401
	/* Look for tree / blob name collisions, removing conflicts if requested */
	if ((error = check_file_directory_collision(index, entry, position, replace)) < 0)
		goto out;
1402

1403 1404
	/*
	 * If we are replacing an existing item, overwrite the existing entry
1405 1406
	 * and return it in place of the passed in one.
	 */
1407
	if (existing) {
1408 1409 1410 1411 1412 1413 1414
		if (replace) {
			index_entry_cpy(existing, entry);

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

1415
		index_entry_free(entry);
1416 1417 1418 1419
		*entry_ptr = existing;
	} else {
		/*
		 * If replace is not requested or no existing entry exists, insert
1420 1421
		 * at the sorted position.  (Since we re-sort after each insert to
		 * check for dups, this is actually cheaper in the long run.)
1422
		 */
1423 1424
		if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0 ||
		    (error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0)
1425
			goto out;
1426
	}
1427

1428 1429 1430
	index->dirty = 1;

out:
1431 1432 1433 1434
	if (error < 0) {
		index_entry_free(*entry_ptr);
		*entry_ptr = NULL;
	}
1435

1436
	return error;
1437 1438
}

Edward Thomson committed
1439
static int index_conflict_to_reuc(git_index *index, const char *path)
1440
{
1441
	const git_index_entry *conflict_entries[3];
Edward Thomson committed
1442
	int ancestor_mode, our_mode, their_mode;
Edward Thomson committed
1443
	git_oid const *ancestor_oid, *our_oid, *their_oid;
1444
	int ret;
1445

Edward Thomson committed
1446 1447
	if ((ret = git_index_conflict_get(&conflict_entries[0],
		&conflict_entries[1], &conflict_entries[2], index, path)) < 0)
1448
		return ret;
1449

Edward Thomson committed
1450 1451 1452
	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;
1453

1454 1455 1456
	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
1457 1458 1459 1460 1461 1462

	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;
1463 1464
}

1465
GIT_INLINE(bool) is_file_or_link(const int filemode)
1466 1467 1468
{
	return (filemode == GIT_FILEMODE_BLOB ||
		filemode == GIT_FILEMODE_BLOB_EXECUTABLE ||
1469 1470 1471 1472 1473 1474
		filemode == GIT_FILEMODE_LINK);
}

GIT_INLINE(bool) valid_filemode(const int filemode)
{
	return (is_file_or_link(filemode) || filemode == GIT_FILEMODE_COMMIT);
1475 1476
}

1477
int git_index_add_from_buffer(
1478
    git_index *index, const git_index_entry *source_entry,
1479 1480 1481 1482 1483 1484
    const void *buffer, size_t len)
{
	git_index_entry *entry = NULL;
	int error = 0;
	git_oid id;

Edward Thomson committed
1485 1486
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(source_entry && source_entry->path);
1487 1488 1489

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

1493
	if (!is_file_or_link(source_entry->mode)) {
1494
		git_error_set(GIT_ERROR_INDEX, "invalid filemode");
1495 1496 1497
		return -1;
	}

1498 1499 1500 1501 1502
	if (len > UINT32_MAX) {
		git_error_set(GIT_ERROR_INDEX, "buffer is too large");
		return -1;
	}

1503
	if (index_entry_dup(&entry, index, source_entry) < 0)
1504 1505
		return -1;

1506
	error = git_blob_create_from_buffer(&id, INDEX_OWNER(index), buffer, len);
1507 1508 1509 1510 1511 1512
	if (error < 0) {
		index_entry_free(entry);
		return error;
	}

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

1515
	if ((error = index_insert(index, &entry, 1, true, true, true)) < 0)
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
		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;
}

1526 1527 1528
static int add_repo_as_submodule(git_index_entry **out, git_index *index, const char *path)
{
	git_repository *sub;
1529
	git_str abspath = GIT_STR_INIT;
1530 1531 1532 1533 1534 1535
	git_repository *repo = INDEX_OWNER(index);
	git_reference *head;
	git_index_entry *entry;
	struct stat st;
	int error;

1536
	if ((error = git_repository_workdir_path(&abspath, repo, path)) < 0)
1537 1538 1539
		return error;

	if ((error = p_stat(abspath.ptr, &st)) < 0) {
1540
		git_error_set(GIT_ERROR_OS, "failed to stat repository dir");
1541 1542 1543
		return -1;
	}

1544 1545 1546
	if (index_entry_create(&entry, INDEX_OWNER(index), path, &st, true) < 0)
		return -1;

1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
	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);
1560
	git_str_dispose(&abspath);
1561 1562 1563 1564

	*out = entry;
	return 0;
}
1565

1566
int git_index_add_bypath(git_index *index, const char *path)
1567
{
Edward Thomson committed
1568 1569 1570
	git_index_entry *entry = NULL;
	int ret;

Edward Thomson committed
1571 1572
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(path);
Edward Thomson committed
1573

1574
	if ((ret = index_entry_init(&entry, index, path)) == 0)
1575
		ret = index_insert(index, &entry, 1, false, false, true);
1576 1577 1578 1579 1580 1581 1582 1583 1584

	/* 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;

1585
		git_error_state_capture(&err, ret);
1586 1587 1588

		ret = git_submodule_lookup(&sm, INDEX_OWNER(index), path);
		if (ret == GIT_ENOTFOUND)
1589
			return git_error_state_restore(&err);
1590

1591
		git_error_state_free(&err);
1592 1593 1594 1595 1596 1597 1598 1599 1600

		/*
		 * 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;

1601
			if ((ret = index_insert(index, &entry, 1, false, false, true)) < 0)
1602 1603 1604 1605 1606 1607 1608 1609
				return ret;
		} else if (ret < 0) {
			return ret;
		} else {
			ret = git_submodule_add_to_index(sm, false);
			git_submodule_free(sm);
			return ret;
		}
1610
	}
Edward Thomson committed
1611 1612 1613

	/* Adding implies conflict was resolved, move conflict entries to REUC */
	if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND)
1614
		return ret;
Edward Thomson committed
1615 1616 1617

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

1620 1621 1622 1623
int git_index_remove_bypath(git_index *index, const char *path)
{
	int ret;

Edward Thomson committed
1624 1625
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(path);
1626 1627 1628 1629 1630 1631 1632

	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;

1633
	if (ret == GIT_ENOTFOUND)
1634
		git_error_clear();
1635

1636 1637 1638
	return 0;
}

1639 1640 1641
int git_index__fill(git_index *index, const git_vector *source_entries)
{
	const git_index_entry *source_entry = NULL;
1642
	int error = 0;
1643 1644
	size_t i;

Edward Thomson committed
1645
	GIT_ASSERT_ARG(index);
1646

1647 1648 1649
	if (!source_entries->length)
		return 0;

1650
	if (git_vector_size_hint(&index->entries, source_entries->length) < 0 ||
1651 1652
	    index_map_resize(index->entries_map, (size_t)(source_entries->length * 1.3),
			     index->ignore_case) < 0)
1653
		return -1;
1654

1655 1656 1657
	git_vector_foreach(source_entries, i, source_entry) {
		git_index_entry *entry = NULL;

1658
		if ((error = index_entry_dup(&entry, index, source_entry)) < 0)
1659 1660
			break;

1661
		index_entry_adjust_namemask(entry, ((struct entry_internal *)entry)->pathlen);
1662
		entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE;
1663
		entry->mode = git_index__create_mode(entry->mode);
1664

1665
		if ((error = git_vector_insert(&index->entries, entry)) < 0)
1666 1667
			break;

1668
		if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0)
1669
			break;
1670 1671

		index->dirty = 1;
1672 1673
	}

1674
	if (!error)
1675 1676
		git_vector_sort(&index->entries);

1677
	return error;
1678 1679
}

1680

Edward Thomson committed
1681
int git_index_add(git_index *index, const git_index_entry *source_entry)
1682 1683 1684 1685
{
	git_index_entry *entry = NULL;
	int ret;

Edward Thomson committed
1686 1687
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(source_entry && source_entry->path);
1688

1689
	if (!valid_filemode(source_entry->mode)) {
1690
		git_error_set(GIT_ERROR_INDEX, "invalid entry mode");
1691 1692 1693
		return -1;
	}

1694
	if ((ret = index_entry_dup(&entry, index, source_entry)) < 0 ||
1695
		(ret = index_insert(index, &entry, 1, true, true, false)) < 0)
1696
		return ret;
1697

1698
	git_tree_cache_invalidate_path(index->tree, entry->path);
1699
	return 0;
1700 1701
}

Edward Thomson committed
1702
int git_index_remove(git_index *index, const char *path, int stage)
1703
{
1704
	int error;
1705
	size_t position;
1706
	git_index_entry remove_key = {{ 0 }};
1707

1708
	remove_key.path = path;
1709
	GIT_INDEX_ENTRY_STAGE_SET(&remove_key, stage);
1710

1711
	index_map_delete(index->entries_map, &remove_key, index->ignore_case);
1712

1713
	if (index_find(&position, index, path, 0, stage) < 0) {
1714 1715
		git_error_set(
			GIT_ERROR_INDEX, "index does not contain %s at stage %d", path, stage);
1716 1717 1718
		error = GIT_ENOTFOUND;
	} else {
		error = index_remove_entry(index, position);
1719
	}
Edward Thomson committed
1720

1721
	return error;
1722
}
1723

1724 1725
int git_index_remove_directory(git_index *index, const char *dir, int stage)
{
1726
	git_str pfx = GIT_STR_INIT;
1727 1728 1729 1730
	int error = 0;
	size_t pos;
	git_index_entry *entry;

1731
	if (!(error = git_str_sets(&pfx, dir)) &&
1732
		!(error = git_fs_path_to_dir(&pfx)))
1733
		index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY);
1734

1735
	while (!error) {
1736 1737 1738 1739
		entry = git_vector_get(&index->entries, pos);
		if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0)
			break;

1740
		if (GIT_INDEX_ENTRY_STAGE(entry) != stage) {
1741 1742 1743 1744
			++pos;
			continue;
		}

1745
		error = index_remove_entry(index, pos);
1746

1747
		/* removed entry at 'pos' so we don't need to increment */
1748 1749
	}

1750
	git_str_dispose(&pfx);
1751 1752 1753 1754

	return error;
}

1755 1756 1757 1758 1759 1760
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;

1761
	index_find(&pos, index, prefix, strlen(prefix), GIT_INDEX_STAGE_ANY);
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
	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;
}

1772
int git_index__find_pos(
1773 1774
	size_t *out, git_index *index, const char *path, size_t path_len, int stage)
{
Edward Thomson committed
1775 1776
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(path);
1777
	return index_find(out, index, path, path_len, stage);
Edward Thomson committed
1778 1779
}

1780
int git_index_find(size_t *at_pos, git_index *index, const char *path)
1781
{
1782
	size_t pos;
Edward Thomson committed
1783

Edward Thomson committed
1784 1785
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(path);
Edward Thomson committed
1786

1787 1788
	if (git_vector_bsearch2(
			&pos, &index->entries, index->entries_search_path, path) < 0) {
1789
		git_error_set(GIT_ERROR_INDEX, "index does not contain %s", path);
1790
		return GIT_ENOTFOUND;
1791
	}
Edward Thomson committed
1792 1793

	/* Since our binary search only looked at path, we may be in the
1794 1795
	 * middle of a list of stages.
	 */
1796 1797
	for (; pos > 0; --pos) {
		const git_index_entry *prev = git_vector_get(&index->entries, pos - 1);
Edward Thomson committed
1798 1799 1800 1801 1802

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

1803 1804 1805 1806
	if (at_pos)
		*at_pos = pos;

	return 0;
1807 1808
}

Edward Thomson committed
1809 1810 1811 1812 1813 1814
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 };
1815
	unsigned short i;
Edward Thomson committed
1816 1817
	int ret = 0;

Edward Thomson committed
1818
	GIT_ASSERT_ARG(index);
Edward Thomson committed
1819

1820 1821 1822 1823 1824 1825
	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))
1826
		goto on_error;
Edward Thomson committed
1827

1828 1829 1830
	/* Validate entries */
	for (i = 0; i < 3; i++) {
		if (entries[i] && !valid_filemode(entries[i]->mode)) {
1831
			git_error_set(GIT_ERROR_INDEX, "invalid filemode for stage %d entry",
1832
				i + 1);
abyss7 committed
1833 1834
			ret = -1;
			goto on_error;
1835 1836 1837
		}
	}

1838 1839 1840 1841 1842 1843 1844 1845 1846
	/* 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;

1847
			git_error_clear();
1848 1849 1850 1851 1852
			ret = 0;
		}
	}

	/* Add the conflict entries */
Edward Thomson committed
1853 1854 1855 1856 1857
	for (i = 0; i < 3; i++) {
		if (entries[i] == NULL)
			continue;

		/* Make sure stage is correct */
1858
		GIT_INDEX_ENTRY_STAGE_SET(entries[i], i + 1);
Edward Thomson committed
1859

1860
		if ((ret = index_insert(index, &entries[i], 1, true, true, false)) < 0)
Edward Thomson committed
1861
			goto on_error;
1862 1863

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

nulltoken committed
1866
	return 0;
Edward Thomson committed
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876

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

	return ret;
}

1877 1878 1879 1880 1881 1882
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
1883
{
1884 1885 1886 1887
	const git_index_entry *conflict_entry;
	const char *path = NULL;
	size_t count;
	int stage, len = 0;
Edward Thomson committed
1888

Edward Thomson committed
1889 1890 1891 1892
	GIT_ASSERT_ARG(ancestor_out);
	GIT_ASSERT_ARG(our_out);
	GIT_ASSERT_ARG(their_out);
	GIT_ASSERT_ARG(index);
1893

Edward Thomson committed
1894 1895 1896 1897
	*ancestor_out = NULL;
	*our_out = NULL;
	*their_out = NULL;

1898 1899
	for (count = git_index_entrycount(index); n < count; ++n) {
		conflict_entry = git_vector_get(&index->entries, n);
Edward Thomson committed
1900

1901
		if (path && index->entries_cmp_path(conflict_entry->path, path) != 0)
Edward Thomson committed
1902 1903
			break;

1904
		stage = GIT_INDEX_ENTRY_STAGE(conflict_entry);
1905
		path = conflict_entry->path;
1906

Edward Thomson committed
1907 1908 1909
		switch (stage) {
		case 3:
			*their_out = conflict_entry;
1910
			len++;
Edward Thomson committed
1911 1912 1913
			break;
		case 2:
			*our_out = conflict_entry;
1914
			len++;
Edward Thomson committed
1915 1916 1917
			break;
		case 1:
			*ancestor_out = conflict_entry;
1918
			len++;
Edward Thomson committed
1919 1920 1921 1922 1923 1924
			break;
		default:
			break;
		};
	}

1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
	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;

Edward Thomson committed
1938 1939 1940 1941 1942
	GIT_ASSERT_ARG(ancestor_out);
	GIT_ASSERT_ARG(our_out);
	GIT_ASSERT_ARG(their_out);
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(path);
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957

	*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
1958 1959
}

1960
static int index_conflict_remove(git_index *index, const char *path)
Edward Thomson committed
1961
{
1962
	size_t pos = 0;
Edward Thomson committed
1963
	git_index_entry *conflict_entry;
1964
	int error = 0;
Edward Thomson committed
1965

1966
	if (path != NULL && git_index_find(&pos, index, path) < 0)
1967
		return GIT_ENOTFOUND;
Edward Thomson committed
1968

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

1971 1972
		if (path != NULL &&
			index->entries_cmp_path(conflict_entry->path, path) != 0)
Edward Thomson committed
1973 1974
			break;

1975
		if (GIT_INDEX_ENTRY_STAGE(conflict_entry) == 0) {
Edward Thomson committed
1976 1977 1978 1979
			pos++;
			continue;
		}

1980
		if ((error = index_remove_entry(index, pos)) < 0)
1981
			break;
Edward Thomson committed
1982 1983
	}

1984
	return error;
Edward Thomson committed
1985 1986
}

1987
int git_index_conflict_remove(git_index *index, const char *path)
Edward Thomson committed
1988
{
Edward Thomson committed
1989 1990
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(path);
1991
	return index_conflict_remove(index, path);
Edward Thomson committed
1992 1993
}

1994
int git_index_conflict_cleanup(git_index *index)
Edward Thomson committed
1995
{
Edward Thomson committed
1996
	GIT_ASSERT_ARG(index);
1997
	return index_conflict_remove(index, NULL);
Edward Thomson committed
1998 1999
}

2000
int git_index_has_conflicts(const git_index *index)
2001
{
2002
	size_t i;
2003 2004
	git_index_entry *entry;

Edward Thomson committed
2005
	GIT_ASSERT_ARG(index);
2006 2007

	git_vector_foreach(&index->entries, i, entry) {
2008
		if (GIT_INDEX_ENTRY_STAGE(entry) > 0)
2009 2010 2011 2012 2013 2014
			return 1;
	}

	return 0;
}

2015 2016 2017 2018 2019 2020 2021
int git_index_iterator_new(
	git_index_iterator **iterator_out,
	git_index *index)
{
	git_index_iterator *it;
	int error;

Edward Thomson committed
2022 2023
	GIT_ASSERT_ARG(iterator_out);
	GIT_ASSERT_ARG(index);
2024 2025

	it = git__calloc(1, sizeof(git_index_iterator));
2026
	GIT_ERROR_CHECK_ALLOC(it);
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042

	if ((error = git_index_snapshot_new(&it->snap, index)) < 0) {
		git__free(it);
		return error;
	}

	it->index = index;

	*iterator_out = it;
	return 0;
}

int git_index_iterator_next(
	const git_index_entry **out,
	git_index_iterator *it)
{
Edward Thomson committed
2043 2044
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(it);
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061

	if (it->cur >= git_vector_length(&it->snap))
		return GIT_ITEROVER;

	*out = (git_index_entry *)git_vector_get(&it->snap, it->cur++);
	return 0;
}

void git_index_iterator_free(git_index_iterator *it)
{
	if (it == NULL)
		return;

	git_index_snapshot_release(&it->snap, it->index);
	git__free(it);
}

2062 2063 2064 2065 2066 2067
int git_index_conflict_iterator_new(
	git_index_conflict_iterator **iterator_out,
	git_index *index)
{
	git_index_conflict_iterator *it = NULL;

Edward Thomson committed
2068 2069
	GIT_ASSERT_ARG(iterator_out);
	GIT_ASSERT_ARG(index);
2070 2071

	it = git__calloc(1, sizeof(git_index_conflict_iterator));
2072
	GIT_ERROR_CHECK_ALLOC(it);
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088

	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;

Edward Thomson committed
2089 2090 2091 2092
	GIT_ASSERT_ARG(ancestor_out);
	GIT_ASSERT_ARG(our_out);
	GIT_ASSERT_ARG(their_out);
	GIT_ASSERT_ARG(iterator);
2093 2094 2095 2096 2097 2098 2099 2100

	*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);

2101
		if (git_index_entry_is_conflict(entry)) {
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
			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);
}

2128
size_t git_index_name_entrycount(git_index *index)
Edward Thomson committed
2129
{
Edward Thomson committed
2130
	GIT_ASSERT_ARG(index);
2131
	return index->names.length;
Edward Thomson committed
2132 2133 2134 2135 2136
}

const git_index_name_entry *git_index_name_get_byindex(
	git_index *index, size_t n)
{
Edward Thomson committed
2137
	GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
nulltoken committed
2138

Edward Thomson committed
2139 2140 2141 2142
	git_vector_sort(&index->names);
	return git_vector_get(&index->names, n);
}

2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
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
2153 2154 2155 2156 2157
int git_index_name_add(git_index *index,
	const char *ancestor, const char *ours, const char *theirs)
{
	git_index_name_entry *conflict_name;

Edward Thomson committed
2158
	GIT_ASSERT_ARG((ancestor && ours) || (ancestor && theirs) || (ours && theirs));
Edward Thomson committed
2159 2160

	conflict_name = git__calloc(1, sizeof(git_index_name_entry));
2161
	GIT_ERROR_CHECK_ALLOC(conflict_name);
nulltoken committed
2162

2163 2164 2165 2166 2167 2168 2169
	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
2170
	}
nulltoken committed
2171

2172
	index->dirty = 1;
2173
	return 0;
Edward Thomson committed
2174 2175
}

2176
int git_index_name_clear(git_index *index)
Edward Thomson committed
2177 2178 2179 2180
{
	size_t i;
	git_index_name_entry *conflict_name;

Edward Thomson committed
2181
	GIT_ASSERT_ARG(index);
nulltoken committed
2182

2183 2184
	git_vector_foreach(&index->names, i, conflict_name)
		index_name_entry_free(conflict_name);
nulltoken committed
2185

Edward Thomson committed
2186
	git_vector_clear(&index->names);
2187 2188

	index->dirty = 1;
2189 2190

	return 0;
Edward Thomson committed
2191 2192
}

2193
size_t git_index_reuc_entrycount(git_index *index)
Edward Thomson committed
2194
{
Edward Thomson committed
2195
	GIT_ASSERT_ARG(index);
2196
	return index->reuc.length;
Edward Thomson committed
2197 2198
}

2199 2200 2201 2202 2203 2204 2205
static int index_reuc_on_dup(void **old, void *new)
{
	index_entry_reuc_free(*old);
	*old = new;
	return GIT_EEXISTS;
}

Edward Thomson committed
2206 2207
static int index_reuc_insert(
	git_index *index,
2208
	git_index_reuc_entry *reuc)
Edward Thomson committed
2209
{
2210
	int res;
Edward Thomson committed
2211

Edward Thomson committed
2212 2213 2214
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(reuc && reuc->path != NULL);
	GIT_ASSERT(git_vector_is_sorted(&index->reuc));
Edward Thomson committed
2215

2216
	res = git_vector_insert_sorted(&index->reuc, reuc, &index_reuc_on_dup);
2217 2218
	index->dirty = 1;

2219
	return res == GIT_EEXISTS ? 0 : res;
Edward Thomson committed
2220 2221 2222
}

int git_index_reuc_add(git_index *index, const char *path,
Edward Thomson committed
2223 2224 2225
	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
2226 2227 2228 2229
{
	git_index_reuc_entry *reuc = NULL;
	int error = 0;

Edward Thomson committed
2230 2231
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(path);
Edward Thomson committed
2232

2233 2234
	if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode,
			ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 ||
2235
		(error = index_reuc_insert(index, reuc)) < 0)
Edward Thomson committed
2236 2237 2238
		index_entry_reuc_free(reuc);

	return error;
2239
}
Edward Thomson committed
2240

2241
int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path)
2242
{
2243
	return git_vector_bsearch2(at_pos, &index->reuc, index->reuc_search, path);
2244 2245
}

Edward Thomson committed
2246
const git_index_reuc_entry *git_index_reuc_get_bypath(
2247
	git_index *index, const char *path)
2248
{
2249
	size_t pos;
Edward Thomson committed
2250 2251 2252

	GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
	GIT_ASSERT_ARG_WITH_RETVAL(path, NULL);
2253

Edward Thomson committed
2254
	if (!index->reuc.length)
2255
		return NULL;
2256

Edward Thomson committed
2257
	GIT_ASSERT_WITH_RETVAL(git_vector_is_sorted(&index->reuc), NULL);
Edward Thomson committed
2258

2259
	if (git_index_reuc_find(&pos, index, path) < 0)
2260
		return NULL;
2261

Edward Thomson committed
2262
	return git_vector_get(&index->reuc, pos);
2263 2264
}

Edward Thomson committed
2265
const git_index_reuc_entry *git_index_reuc_get_byindex(
2266
	git_index *index, size_t n)
2267
{
Edward Thomson committed
2268 2269
	GIT_ASSERT_ARG_WITH_RETVAL(index, NULL);
	GIT_ASSERT_WITH_RETVAL(git_vector_is_sorted(&index->reuc), NULL);
Edward Thomson committed
2270 2271 2272 2273

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

Ben Straub committed
2274
int git_index_reuc_remove(git_index *index, size_t position)
Edward Thomson committed
2275 2276 2277 2278
{
	int error;
	git_index_reuc_entry *reuc;

Edward Thomson committed
2279 2280
	GIT_ASSERT_ARG(index);
	GIT_ASSERT(git_vector_is_sorted(&index->reuc));
Edward Thomson committed
2281 2282

	reuc = git_vector_get(&index->reuc, position);
2283
	error = git_vector_remove(&index->reuc, position);
Edward Thomson committed
2284 2285 2286 2287

	if (!error)
		index_entry_reuc_free(reuc);

2288
	index->dirty = 1;
Edward Thomson committed
2289
	return error;
2290 2291
}

2292
int git_index_reuc_clear(git_index *index)
Edward Thomson committed
2293 2294 2295
{
	size_t i;

Edward Thomson committed
2296
	GIT_ASSERT_ARG(index);
Edward Thomson committed
2297

2298
	for (i = 0; i < index->reuc.length; ++i)
2299
		index_entry_reuc_free(git_atomic_swap(index->reuc.contents[i], NULL));
Edward Thomson committed
2300 2301

	git_vector_clear(&index->reuc);
2302 2303

	index->dirty = 1;
2304 2305

	return 0;
Edward Thomson committed
2306 2307
}

2308 2309
static int index_error_invalid(const char *message)
{
2310
	git_error_set(GIT_ERROR_INDEX, "invalid data in index - %s", message);
2311 2312 2313
	return -1;
}

Edward Thomson committed
2314
static int read_reuc(git_index *index, const char *buffer, size_t size)
2315
{
2316 2317
	const char *endptr;
	size_t len;
2318 2319
	int i;

2320 2321 2322
	/* 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)
2323
		return -1;
2324 2325

	while (size) {
Edward Thomson committed
2326
		git_index_reuc_entry *lost;
2327

2328
		len = p_strnlen(buffer, size) + 1;
2329
		if (size <= len)
Edward Thomson committed
2330
			return index_error_invalid("reading reuc entries");
2331

2332
		lost = reuc_entry_alloc(buffer);
2333
		GIT_ERROR_CHECK_ALLOC(lost);
2334 2335 2336 2337

		size -= len;
		buffer += len;

2338
		/* read 3 ASCII octal numbers for stage entries */
2339
		for (i = 0; i < 3; i++) {
2340
			int64_t tmp;
2341

2342
			if (git__strntol64(&tmp, buffer, size, &endptr, 8) < 0 ||
2343
				!endptr || endptr == buffer || *endptr ||
2344
				tmp < 0 || tmp > UINT32_MAX) {
2345
				index_entry_reuc_free(lost);
Edward Thomson committed
2346
				return index_error_invalid("reading reuc entry stage");
2347
			}
2348

2349
			lost->mode[i] = (uint32_t)tmp;
2350

2351
			len = (endptr + 1) - buffer;
2352 2353
			if (size <= len) {
				index_entry_reuc_free(lost);
Edward Thomson committed
2354
				return index_error_invalid("reading reuc entry stage");
2355
			}
2356

2357 2358 2359 2360
			size -= len;
			buffer += len;
		}

2361
		/* read up to 3 OIDs for stage entries */
2362 2363 2364
		for (i = 0; i < 3; i++) {
			if (!lost->mode[i])
				continue;
2365 2366
			if (size < 20) {
				index_entry_reuc_free(lost);
Edward Thomson committed
2367
				return index_error_invalid("reading reuc entry oid");
2368
			}
2369

2370
			git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer);
2371 2372 2373
			size -= 20;
			buffer += 20;
		}
2374 2375 2376 2377

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

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

2383
	return 0;
2384 2385
}

Edward Thomson committed
2386 2387 2388 2389

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

Edward Thomson committed
2391 2392 2393 2394 2395 2396
	/* 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) \
2397
	len = p_strnlen(buffer, size) + 1; \
2398 2399 2400 2401
	if (size < len) { \
		index_error_invalid("reading conflict name entries"); \
		goto out_err; \
	} \
Edward Thomson committed
2402 2403 2404 2405
	if (len == 1) \
		ptr = NULL; \
	else { \
		ptr = git__malloc(len); \
2406
		GIT_ERROR_CHECK_ALLOC(ptr); \
Edward Thomson committed
2407 2408 2409 2410 2411
		memcpy(ptr, buffer, len); \
	} \
	\
	buffer += len; \
	size -= len;
nulltoken committed
2412

Edward Thomson committed
2413 2414
	while (size) {
		git_index_name_entry *conflict_name = git__calloc(1, sizeof(git_index_name_entry));
2415
		GIT_ERROR_CHECK_ALLOC(conflict_name);
Edward Thomson committed
2416 2417 2418 2419

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

Edward Thomson committed
2421
		if (git_vector_insert(&index->names, conflict_name) < 0)
2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
			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
2432 2433 2434
	}

#undef read_conflict_name
nulltoken committed
2435

Edward Thomson committed
2436
	/* entries are guaranteed to be sorted on-disk */
2437
	git_vector_set_sorted(&index->names, true);
nulltoken committed
2438 2439

	return 0;
Edward Thomson committed
2440 2441
}

2442 2443 2444
static size_t index_entry_size(size_t path_len, size_t varint_len, uint32_t flags)
{
	if (varint_len) {
2445
		if (flags & GIT_INDEX_ENTRY_EXTENDED)
2446 2447 2448 2449
			return offsetof(struct entry_long, path) + path_len + 1 + varint_len;
		else
			return offsetof(struct entry_short, path) + path_len + 1 + varint_len;
	} else {
2450
#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7)
2451
		if (flags & GIT_INDEX_ENTRY_EXTENDED)
2452
			return entry_size(struct entry_long, path_len);
2453
		else
2454 2455
			return entry_size(struct entry_short, path_len);
#undef entry_size
2456 2457 2458
	}
}

2459
static int read_entry(
2460
	git_index_entry **out,
2461
	size_t *out_size,
2462 2463
	git_index *index,
	const void *buffer,
David Turner committed
2464
	size_t buffer_size,
2465
	const char *last)
2466 2467 2468
{
	size_t path_length, entry_size;
	const char *path_ptr;
2469
	struct entry_short source;
2470
	git_index_entry entry = {{0}};
David Turner committed
2471 2472
	bool compressed = index->version >= INDEX_VERSION_NUMBER_COMP;
	char *tmp_path = NULL;
2473 2474

	if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size)
2475
		return -1;
2476

2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
	/* 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);
2492

2493
	if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) {
2494 2495
		uint16_t flags_raw;
		size_t flags_offset;
2496

2497 2498 2499 2500 2501 2502 2503
		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);
2504
	} else
2505
		path_ptr = (const char *) buffer + offsetof(struct entry_short, path);
2506

David Turner committed
2507
	if (!compressed) {
2508
		path_length = entry.flags & GIT_INDEX_ENTRY_NAMEMASK;
2509

David Turner committed
2510 2511 2512 2513
		/* if this is a very long string, we must find its
		 * real length without overflowing */
		if (path_length == 0xFFF) {
			const char *path_end;
2514

David Turner committed
2515 2516
			path_end = memchr(path_ptr, '\0', buffer_size);
			if (path_end == NULL)
2517
				return -1;
2518

David Turner committed
2519 2520
			path_length = path_end - path_ptr;
		}
2521

2522
		entry_size = index_entry_size(path_length, 0, entry.flags);
David Turner committed
2523 2524
		entry.path = (char *)path_ptr;
	} else {
2525 2526 2527 2528 2529 2530 2531
		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
2532 2533
			return index_error_invalid("incorrect prefix length");

2534
		prefix_len = last_len - (size_t)strip_len;
2535 2536
		suffix_len = strlen(path_ptr + varint_len);

2537 2538
		GIT_ERROR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len);
		GIT_ERROR_CHECK_ALLOC_ADD(&path_len, path_len, 1);
2539 2540 2541 2542

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

2543
		tmp_path = git__malloc(path_len);
2544
		GIT_ERROR_CHECK_ALLOC(tmp_path);
2545 2546 2547

		memcpy(tmp_path, last, prefix_len);
		memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1);
2548
		entry_size = index_entry_size(suffix_len, varint_len, entry.flags);
David Turner committed
2549 2550 2551
		entry.path = tmp_path;
	}

2552 2553 2554
	if (entry_size == 0)
		return -1;

2555
	if (INDEX_FOOTER_SIZE + entry_size > buffer_size)
2556
		return -1;
2557

David Turner committed
2558 2559
	if (index_entry_dup(out, index, &entry) < 0) {
		git__free(tmp_path);
2560
		return -1;
David Turner committed
2561
	}
2562

David Turner committed
2563
	git__free(tmp_path);
2564 2565
	*out_size = entry_size;
	return 0;
2566 2567 2568 2569
}

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

2572 2573
	dest->signature = ntohl(source->signature);
	if (dest->signature != INDEX_HEADER_SIG)
2574
		return index_error_invalid("incorrect header signature");
2575 2576

	dest->version = ntohl(source->version);
David Turner committed
2577 2578
	if (dest->version < INDEX_VERSION_NUMBER_LB ||
		dest->version > INDEX_VERSION_NUMBER_UB)
2579
		return index_error_invalid("incorrect header version");
2580 2581

	dest->entry_count = ntohl(source->entry_count);
2582
	return 0;
2583 2584
}

2585
static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size)
2586 2587 2588 2589
{
	struct index_extension dest;
	size_t total_size;

2590 2591 2592
	/* buffer is not guaranteed to be aligned */
	memcpy(&dest, buffer, sizeof(struct index_extension));
	dest.extension_size = ntohl(dest.extension_size);
2593 2594 2595

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

2596 2597
	if (dest.extension_size > total_size ||
		buffer_size < total_size ||
2598 2599 2600 2601
		buffer_size - total_size < INDEX_FOOTER_SIZE) {
		index_error_invalid("extension is truncated");
		return -1;
	}
2602 2603 2604 2605 2606

	/* optional extension */
	if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
		/* tree cache */
		if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {
2607
			if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, &index->tree_pool) < 0)
2608
				return -1;
2609
		} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
Edward Thomson committed
2610
			if (read_reuc(index, buffer + 8, dest.extension_size) < 0)
2611
				return -1;
Edward Thomson committed
2612 2613
		} else if (memcmp(dest.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4) == 0) {
			if (read_conflict_names(index, buffer + 8, dest.extension_size) < 0)
2614
				return -1;
2615
		}
2616 2617
		/* else, unsupported extension. We cannot parse this, but we can skip
		 * it by returning `total_size */
2618 2619 2620
	} else {
		/* we cannot handle non-ignorable extensions;
		 * in fact they aren't even defined in the standard */
2621 2622
		git_error_set(GIT_ERROR_INDEX, "unsupported mandatory extension: '%.4s'", dest.signature);
		return -1;
2623 2624
	}

2625 2626 2627
	*read_len = total_size;

	return 0;
2628 2629
}

2630
static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
2631
{
2632
	int error = 0;
2633
	unsigned int i;
2634
	struct index_header header = { 0 };
2635
	git_oid checksum_calculated, checksum_expected;
2636
	const char *last = NULL;
David Turner committed
2637
	const char *empty = "";
2638 2639

#define seek_forward(_increase) { \
2640 2641 2642
	if (_increase >= buffer_size) { \
		error = index_error_invalid("ran out of data while parsing"); \
		goto done; } \
2643 2644 2645 2646 2647
	buffer += _increase; \
	buffer_size -= _increase;\
}

	if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
2648
		return index_error_invalid("insufficient buffer space");
2649 2650 2651

	/* Precalculate the SHA1 of the files's contents -- we'll match it to
	 * the provided SHA1 in the footer */
2652
	git_hash_buf(checksum_calculated.id, buffer, buffer_size - INDEX_FOOTER_SIZE, GIT_HASH_ALGORITHM_SHA1);
2653 2654

	/* Parse header */
2655 2656
	if ((error = read_header(&header, buffer)) < 0)
		return error;
2657

David Turner committed
2658 2659
	index->version = header.version;
	if (index->version >= INDEX_VERSION_NUMBER_COMP)
2660
		last = empty;
David Turner committed
2661

2662 2663
	seek_forward(INDEX_HEADER_SIZE);

Edward Thomson committed
2664
	GIT_ASSERT(!index->entries.length);
2665

2666
	if ((error = index_map_resize(index->entries_map, header.entry_count, index->ignore_case)) < 0)
2667
		return error;
2668

2669
	/* Parse all the entries */
2670
	for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
2671
		git_index_entry *entry = NULL;
2672
		size_t entry_size;
2673

2674
		if ((error = read_entry(&entry, &entry_size, index, buffer, buffer_size, last)) < 0) {
2675 2676 2677
			error = index_error_invalid("invalid entry");
			goto done;
		}
2678

2679 2680
		if ((error = git_vector_insert(&index->entries, entry)) < 0) {
			index_entry_free(entry);
2681
			goto done;
2682
		}
2683

2684
		if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0) {
2685 2686 2687
			index_entry_free(entry);
			goto done;
		}
2688
		error = 0;
2689

2690 2691 2692
		if (index->version >= INDEX_VERSION_NUMBER_COMP)
			last = entry->path;

2693 2694 2695
		seek_forward(entry_size);
	}

2696 2697 2698 2699
	if (i != header.entry_count) {
		error = index_error_invalid("header entries changed while parsing");
		goto done;
	}
2700

2701 2702 2703 2704
	/* There's still space for some extensions! */
	while (buffer_size > INDEX_FOOTER_SIZE) {
		size_t extension_size;

2705
		if ((error = read_extension(&extension_size, index, buffer, buffer_size)) < 0) {
2706 2707
			goto done;
		}
2708 2709 2710 2711

		seek_forward(extension_size);
	}

2712 2713 2714 2715 2716
	if (buffer_size != INDEX_FOOTER_SIZE) {
		error = index_error_invalid(
			"buffer size does not match index footer size");
		goto done;
	}
2717 2718

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

2721 2722 2723 2724 2725
	if (git_oid__cmp(&checksum_calculated, &checksum_expected) != 0) {
		error = index_error_invalid(
			"calculated checksum does not match expected");
		goto done;
	}
2726

2727 2728
	git_oid_cpy(&index->checksum, &checksum_calculated);

2729 2730
#undef seek_forward

2731 2732 2733 2734
	/* 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);
2735
	git_vector_sort(&index->entries);
2736

2737
	index->dirty = 0;
2738 2739
done:
	return error;
2740 2741
}

2742
static bool is_index_extended(git_index *index)
Vicent Marti committed
2743
{
2744
	size_t i, extended;
2745
	git_index_entry *entry;
Vicent Marti committed
2746 2747 2748

	extended = 0;

2749
	git_vector_foreach(&index->entries, i, entry) {
2750 2751
		entry->flags &= ~GIT_INDEX_ENTRY_EXTENDED;
		if (entry->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS) {
Vicent Marti committed
2752
			extended++;
2753
			entry->flags |= GIT_INDEX_ENTRY_EXTENDED;
Vicent Marti committed
2754 2755
		}
	}
2756

2757
	return (extended > 0);
Vicent Marti committed
2758 2759
}

2760
static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char *last)
2761
{
2762
	void *mem = NULL;
2763
	struct entry_short ondisk;
2764
	size_t path_len, disk_size;
2765
	int varint_len = 0;
2766
	char *path;
David Turner committed
2767 2768
	const char *path_start = entry->path;
	size_t same_len = 0;
2769

2770
	path_len = ((struct entry_internal *)entry)->pathlen;
2771

David Turner committed
2772
	if (last) {
2773
		const char *last_c = last;
David Turner committed
2774 2775 2776 2777 2778 2779 2780 2781 2782

		while (*path_start == *last_c) {
			if (!*path_start || !*last_c)
				break;
			++path_start;
			++last_c;
			++same_len;
		}
		path_len -= same_len;
2783
		varint_len = git_encode_varint(NULL, 0, strlen(last) - same_len);
David Turner committed
2784 2785
	}

2786
	disk_size = index_entry_size(path_len, varint_len, entry->flags);
2787

2788 2789
	if (git_filebuf_reserve(file, &mem, disk_size) < 0)
		return -1;
2790

2791
	memset(mem, 0x0, disk_size);
2792

2793 2794 2795 2796 2797 2798 2799 2800 2801 2802
	/**
	 * 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
	 */
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
	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);
2813

2814
	git_oid_cpy(&ondisk.oid, &entry->id);
2815

2816
	ondisk.flags = htons(entry->flags);
2817

2818
	if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) {
2819
		const size_t path_offset = offsetof(struct entry_long, path);
2820 2821 2822
		struct entry_long ondisk_ext;
		memcpy(&ondisk_ext, &ondisk, sizeof(struct entry_short));
		ondisk_ext.flags_extended = htons(entry->flags_extended &
2823
			GIT_INDEX_ENTRY_EXTENDED_FLAGS);
2824 2825 2826
		memcpy(mem, &ondisk_ext, path_offset);
		path = (char *)mem + path_offset;
		disk_size -= path_offset;
2827
	} else {
2828 2829 2830 2831
		const size_t path_offset = offsetof(struct entry_short, path);
		memcpy(mem, &ondisk, path_offset);
		path = (char *)mem + path_offset;
		disk_size -= path_offset;
2832
	}
2833

David Turner committed
2834
	if (last) {
2835
		varint_len = git_encode_varint((unsigned char *) path,
2836
					  disk_size, strlen(last) - same_len);
Edward Thomson committed
2837 2838
		GIT_ASSERT(varint_len > 0);

2839 2840 2841 2842 2843 2844 2845
		path += varint_len;
		disk_size -= varint_len;

		/*
		 * If using path compression, we are not allowed
		 * to have additional trailing NULs.
		 */
Edward Thomson committed
2846
		GIT_ASSERT(disk_size == path_len + 1);
2847 2848 2849 2850 2851 2852
	} 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.
		 */
Edward Thomson committed
2853
		GIT_ASSERT(disk_size > path_len);
David Turner committed
2854
	}
2855 2856

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

2858
	return 0;
2859 2860
}

2861
static int write_entries(git_index *index, git_filebuf *file)
2862
{
2863
	int error = 0;
2864
	size_t i;
2865
	git_vector case_sorted = GIT_VECTOR_INIT, *entries = NULL;
2866
	git_index_entry *entry;
2867
	const char *last = NULL;
2868 2869 2870 2871

	/* If index->entries is sorted case-insensitively, then we need
	 * to re-sort it case-sensitively before writing */
	if (index->ignore_case) {
2872
		if ((error = git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp)) < 0)
2873
			goto done;
2874

2875
		git_vector_sort(&case_sorted);
2876 2877 2878
		entries = &case_sorted;
	} else {
		entries = &index->entries;
2879 2880
	}

David Turner committed
2881
	if (index->version >= INDEX_VERSION_NUMBER_COMP)
2882
		last = "";
David Turner committed
2883

2884
	git_vector_foreach(entries, i, entry) {
David Turner committed
2885
		if ((error = write_disk_entry(file, entry, last)) < 0)
2886
			break;
2887 2888 2889
		if (index->version >= INDEX_VERSION_NUMBER_COMP)
			last = entry->path;
	}
2890

2891 2892
done:
	git_vector_free(&case_sorted);
2893
	return error;
2894 2895
}

2896
static int write_extension(git_filebuf *file, struct index_extension *header, git_str *data)
Edward Thomson committed
2897 2898 2899 2900 2901 2902 2903
{
	struct index_extension ondisk;

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

2904 2905
	git_filebuf_write(file, &ondisk, sizeof(struct index_extension));
	return git_filebuf_write(file, data->ptr, data->size);
Edward Thomson committed
2906 2907
}

2908
static int create_name_extension_data(git_str *name_buf, git_index_name_entry *conflict_name)
Edward Thomson committed
2909 2910 2911 2912
{
	int error = 0;

	if (conflict_name->ancestor == NULL)
2913
		error = git_str_put(name_buf, "\0", 1);
Edward Thomson committed
2914
	else
2915
		error = git_str_put(name_buf, conflict_name->ancestor, strlen(conflict_name->ancestor) + 1);
nulltoken committed
2916

Edward Thomson committed
2917 2918 2919 2920
	if (error != 0)
		goto on_error;

	if (conflict_name->ours == NULL)
2921
		error = git_str_put(name_buf, "\0", 1);
Edward Thomson committed
2922
	else
2923
		error = git_str_put(name_buf, conflict_name->ours, strlen(conflict_name->ours) + 1);
Edward Thomson committed
2924 2925 2926 2927 2928

	if (error != 0)
		goto on_error;

	if (conflict_name->theirs == NULL)
2929
		error = git_str_put(name_buf, "\0", 1);
Edward Thomson committed
2930
	else
2931
		error = git_str_put(name_buf, conflict_name->theirs, strlen(conflict_name->theirs) + 1);
Edward Thomson committed
2932 2933 2934 2935 2936 2937 2938

on_error:
	return error;
}

static int write_name_extension(git_index *index, git_filebuf *file)
{
2939
	git_str name_buf = GIT_STR_INIT;
Edward Thomson committed
2940 2941 2942 2943 2944
	git_vector *out = &index->names;
	git_index_name_entry *conflict_name;
	struct index_extension extension;
	size_t i;
	int error = 0;
nulltoken committed
2945

Edward Thomson committed
2946 2947 2948 2949
	git_vector_foreach(out, i, conflict_name) {
		if ((error = create_name_extension_data(&name_buf, conflict_name)) < 0)
			goto done;
	}
nulltoken committed
2950

Edward Thomson committed
2951 2952 2953
	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
2954

Edward Thomson committed
2955
	error = write_extension(file, &extension, &name_buf);
nulltoken committed
2956

2957
	git_str_dispose(&name_buf);
nulltoken committed
2958

Edward Thomson committed
2959 2960 2961 2962
done:
	return error;
}

2963
static int create_reuc_extension_data(git_str *reuc_buf, git_index_reuc_entry *reuc)
Edward Thomson committed
2964 2965 2966 2967
{
	int i;
	int error = 0;

2968
	if ((error = git_str_put(reuc_buf, reuc->path, strlen(reuc->path) + 1)) < 0)
Edward Thomson committed
2969 2970 2971
		return error;

	for (i = 0; i < 3; i++) {
2972 2973
		if ((error = git_str_printf(reuc_buf, "%o", reuc->mode[i])) < 0 ||
			(error = git_str_put(reuc_buf, "\0", 1)) < 0)
Edward Thomson committed
2974 2975 2976 2977
			return error;
	}

	for (i = 0; i < 3; i++) {
2978
		if (reuc->mode[i] && (error = git_str_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_RAWSZ)) < 0)
Edward Thomson committed
2979 2980 2981 2982 2983 2984 2985 2986
			return error;
	}

	return 0;
}

static int write_reuc_extension(git_index *index, git_filebuf *file)
{
2987
	git_str reuc_buf = GIT_STR_INIT;
Edward Thomson committed
2988 2989 2990
	git_vector *out = &index->reuc;
	git_index_reuc_entry *reuc;
	struct index_extension extension;
2991
	size_t i;
Edward Thomson committed
2992 2993 2994 2995 2996 2997 2998 2999 3000
	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);
3001
	extension.extension_size = (uint32_t)reuc_buf.size;
Edward Thomson committed
3002 3003 3004

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

3005
	git_str_dispose(&reuc_buf);
Edward Thomson committed
3006 3007 3008 3009 3010

done:
	return error;
}

3011 3012 3013
static int write_tree_extension(git_index *index, git_filebuf *file)
{
	struct index_extension extension;
3014
	git_str buf = GIT_STR_INIT;
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028
	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);

3029
	git_str_dispose(&buf);
3030 3031 3032 3033

	return error;
}

3034 3035 3036 3037 3038 3039
static void clear_uptodate(git_index *index)
{
	git_index_entry *entry;
	size_t i;

	git_vector_foreach(&index->entries, i, entry)
3040
		entry->flags_extended &= ~GIT_INDEX_ENTRY_UPTODATE;
3041 3042
}

3043
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file)
3044 3045 3046
{
	git_oid hash_final;
	struct index_header header;
3047
	bool is_extended;
Ben Straub committed
3048
	uint32_t index_version_number;
3049

Edward Thomson committed
3050 3051
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(file);
3052

David Turner committed
3053 3054 3055 3056 3057 3058
	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
3059

3060
	header.signature = htonl(INDEX_HEADER_SIG);
Ben Straub committed
3061
	header.version = htonl(index_version_number);
3062
	header.entry_count = htonl((uint32_t)index->entries.length);
3063

3064 3065
	if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0)
		return -1;
3066

3067 3068
	if (write_entries(index, file) < 0)
		return -1;
3069

3070 3071 3072
	/* write the tree cache extension */
	if (index->tree != NULL && write_tree_extension(index, file) < 0)
		return -1;
Edward Thomson committed
3073

Edward Thomson committed
3074 3075 3076
	/* write the rename conflict extension */
	if (index->names.length > 0 && write_name_extension(index, file) < 0)
		return -1;
nulltoken committed
3077

Edward Thomson committed
3078 3079 3080
	/* write the reuc extension */
	if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0)
		return -1;
3081

3082
	/* get out the hash for all the contents we've appended to the file */
3083
	git_filebuf_hash(hash_final.id, file);
3084
	git_oid_cpy(checksum, &hash_final);
3085 3086

	/* write it at the end of the file */
3087 3088 3089 3090 3091 3092 3093
	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;
3094
}
3095 3096 3097

int git_index_entry_stage(const git_index_entry *entry)
{
3098
	return GIT_INDEX_ENTRY_STAGE(entry);
3099
}
3100

3101 3102
int git_index_entry_is_conflict(const git_index_entry *entry)
{
3103
	return (GIT_INDEX_ENTRY_STAGE(entry) > 0);
3104 3105
}

3106
typedef struct read_tree_data {
3107
	git_index *index;
3108
	git_vector *old_entries;
3109
	git_vector *new_entries;
3110
	git_vector_cmp entry_cmp;
3111
	git_tree_cache *tree;
3112 3113
} read_tree_data;

3114 3115
static int read_tree_cb(
	const char *root, const git_tree_entry *tentry, void *payload)
3116
{
3117 3118
	read_tree_data *data = payload;
	git_index_entry *entry = NULL, *old_entry;
3119
	git_str path = GIT_STR_INIT;
3120
	size_t pos;
3121

Vicent Martí committed
3122
	if (git_tree_entry__is_tree(tentry))
3123
		return 0;
3124

3125
	if (git_str_joinpath(&path, root, tentry->filename) < 0)
3126
		return -1;
3127

3128
	if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, NULL, false) < 0)
3129
		return -1;
3130 3131

	entry->mode = tentry->attr;
3132
	git_oid_cpy(&entry->id, git_tree_entry_id(tentry));
3133

3134
	/* look for corresponding old entry and copy data to new entry */
3135
	if (data->old_entries != NULL &&
3136
		!index_find_in_entries(
3137 3138 3139 3140 3141
			&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))
	{
3142
		index_entry_cpy(entry, old_entry);
3143
		entry->flags_extended = 0;
3144 3145
	}

3146
	index_entry_adjust_namemask(entry, path.size);
3147
	git_str_dispose(&path);
3148

3149
	if (git_vector_insert(data->new_entries, entry) < 0) {
3150
		index_entry_free(entry);
3151 3152 3153 3154
		return -1;
	}

	return 0;
3155 3156
}

Ben Straub committed
3157
int git_index_read_tree(git_index *index, const git_tree *tree)
3158
{
3159 3160
	int error = 0;
	git_vector entries = GIT_VECTOR_INIT;
3161
	git_idxmap *entries_map;
3162
	read_tree_data data;
3163 3164 3165
	size_t i;
	git_index_entry *e;

3166
	if (git_idxmap_new(&entries_map) < 0)
3167
		return -1;
3168

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

3171
	data.index = index;
3172 3173
	data.old_entries = &index->entries;
	data.new_entries = &entries;
3174
	data.entry_cmp   = index->entries_search;
3175

3176 3177 3178
	index->tree = NULL;
	git_pool_clear(&index->tree_pool);

3179
	git_vector_sort(&index->entries);
3180

3181 3182
	if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0)
		goto cleanup;
3183

3184
	if ((error = index_map_resize(entries_map, entries.length, index->ignore_case)) < 0)
3185
		goto cleanup;
3186

3187
	git_vector_foreach(&entries, i, e) {
3188
		if ((error = index_map_set(entries_map, e, index->ignore_case)) < 0) {
3189
			git_error_set(GIT_ERROR_INDEX, "failed to insert entry into map");
3190
			return error;
3191
		}
3192 3193
	}

3194 3195 3196 3197
	error = 0;

	git_vector_sort(&entries);

3198
	if ((error = git_index_clear(index)) < 0) {
3199 3200 3201
		/* well, this isn't good */;
	} else {
		git_vector_swap(&entries, &index->entries);
3202
		entries_map = git_atomic_swap(index->entries_map, entries_map);
3203 3204
	}

3205 3206
	index->dirty = 1;

3207
cleanup:
3208
	git_vector_free(&entries);
3209
	git_idxmap_free(entries_map);
3210 3211 3212 3213
	if (error < 0)
		return error;

	error = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool);
3214 3215

	return error;
3216
}
3217

3218
static int git_index_read_iterator(
3219
	git_index *index,
3220 3221
	git_iterator *new_iterator,
	size_t new_length_hint)
3222 3223 3224
{
	git_vector new_entries = GIT_VECTOR_INIT,
		remove_entries = GIT_VECTOR_INIT;
3225
	git_idxmap *new_entries_map = NULL;
3226
	git_iterator *index_iterator = NULL;
3227
	git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
3228 3229 3230 3231 3232
	const git_index_entry *old_entry, *new_entry;
	git_index_entry *entry;
	size_t i;
	int error;

Edward Thomson committed
3233
	GIT_ASSERT((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE));
3234 3235

	if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 ||
3236 3237
	    (error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 ||
	    (error = git_idxmap_new(&new_entries_map)) < 0)
3238 3239
		goto done;

3240 3241
	if (new_length_hint && (error = index_map_resize(new_entries_map, new_length_hint,
							 index->ignore_case)) < 0)
3242
		goto done;
3243

3244 3245
	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
		GIT_ITERATOR_INCLUDE_CONFLICTS;
3246

3247 3248 3249
	if ((error = git_iterator_for_index(&index_iterator,
			git_index_owner(index), index, &opts)) < 0 ||
		((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
3250
			error != GIT_ITEROVER) ||
3251
		((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
3252 3253 3254 3255
			error != GIT_ITEROVER))
		goto done;

	while (true) {
3256 3257 3258 3259
		git_index_entry
			*dup_entry = NULL,
			*add_entry = NULL,
			*remove_entry = NULL;
3260 3261
		int diff;

3262 3263
		error = 0;

3264 3265 3266 3267 3268 3269 3270 3271 3272 3273
		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) {
3274
			remove_entry = (git_index_entry *)old_entry;
3275
		} else if (diff > 0) {
3276
			dup_entry = (git_index_entry *)new_entry;
3277 3278 3279 3280
		} else {
			/* Path and stage are equal, if the OID is equal, keep it to
			 * keep the stat cache data.
			 */
3281 3282
			if (git_oid_equal(&old_entry->id, &new_entry->id) &&
				old_entry->mode == new_entry->mode) {
3283
				add_entry = (git_index_entry *)old_entry;
3284
			} else {
3285
				dup_entry = (git_index_entry *)new_entry;
3286
				remove_entry = (git_index_entry *)old_entry;
3287 3288 3289
			}
		}

3290 3291 3292
		if (dup_entry) {
			if ((error = index_entry_dup_nocache(&add_entry, index, dup_entry)) < 0)
				goto done;
3293 3294 3295

			index_entry_adjust_namemask(add_entry,
				((struct entry_internal *)add_entry)->pathlen);
3296 3297
		}

3298 3299 3300 3301 3302 3303
		/* 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);

3304 3305
		if (add_entry) {
			if ((error = git_vector_insert(&new_entries, add_entry)) == 0)
3306 3307
				error = index_map_set(new_entries_map, add_entry,
						      index->ignore_case);
3308 3309
		}

3310
		if (remove_entry && error >= 0)
3311 3312 3313
			error = git_vector_insert(&remove_entries, remove_entry);

		if (error < 0) {
3314
			git_error_set(GIT_ERROR_INDEX, "failed to insert entry");
3315
			goto done;
3316 3317
		}

3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330
		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;
		}
	}

3331 3332 3333
	if ((error = git_index_name_clear(index)) < 0 ||
		(error = git_index_reuc_clear(index)) < 0)
	    goto done;
3334 3335

	git_vector_swap(&new_entries, &index->entries);
3336
	new_entries_map = git_atomic_swap(index->entries_map, new_entries_map);
3337 3338 3339 3340 3341 3342 3343 3344

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

		index_entry_free(entry);
	}

3345 3346
	clear_uptodate(index);

3347
	index->dirty = 1;
3348 3349 3350
	error = 0;

done:
3351
	git_idxmap_free(new_entries_map);
3352 3353 3354
	git_vector_free(&new_entries);
	git_vector_free(&remove_entries);
	git_iterator_free(index_iterator);
3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365
	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;

3366 3367
	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
		GIT_ITERATOR_INCLUDE_CONFLICTS;
3368 3369

	if ((error = git_iterator_for_index(&new_iterator,
3370 3371 3372
		git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 ||
		(error = git_index_read_iterator(index, new_iterator,
		new_index->entries.length)) < 0)
3373 3374 3375
		goto done;

done:
3376 3377 3378 3379
	git_iterator_free(new_iterator);
	return error;
}

3380 3381 3382 3383
git_repository *git_index_owner(const git_index *index)
{
	return INDEX_OWNER(index);
}
3384

3385 3386 3387 3388
enum {
	INDEX_ACTION_NONE = 0,
	INDEX_ACTION_UPDATE = 1,
	INDEX_ACTION_REMOVE = 2,
3389
	INDEX_ACTION_ADDALL = 3
3390 3391
};

3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
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;
3402
	git_pathspec ps;
3403 3404
	bool no_fnmatch = (flags & GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH) != 0;

Edward Thomson committed
3405
	GIT_ASSERT_ARG(index);
3406 3407 3408 3409 3410

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

3411
	if ((error = git_pathspec__init(&ps, paths)) < 0)
3412 3413 3414 3415 3416 3417 3418 3419 3420
		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;

3421
	error = index_apply_to_wd_diff(index, INDEX_ACTION_ADDALL, paths, flags, cb, payload);
3422

3423
	if (error)
3424
		git_error_set_after_callback(error);
3425 3426 3427

cleanup:
	git_iterator_free(wditer);
3428
	git_pathspec__clear(&ps);
3429 3430 3431 3432

	return error;
}

3433 3434 3435
struct foreach_diff_data {
	git_index *index;
	const git_pathspec *pathspec;
3436
	unsigned int flags;
3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
	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;

3465 3466
	/* If the workdir item does not exist, remove it from the index. */
	if ((delta->new_file.flags & GIT_DIFF_FLAG_EXISTS) == 0)
3467 3468
		error = git_index_remove_bypath(data->index, path);
	else
3469
		error = git_index_add_bypath(data->index, delta->new_file.path);
3470 3471 3472 3473 3474

	return error;
}

static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
3475
				  unsigned int flags,
3476 3477 3478 3479 3480 3481
				  git_index_matched_path_cb cb, void *payload)
{
	int error;
	git_diff *diff;
	git_pathspec ps;
	git_repository *repo;
3482
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
3483 3484 3485
	struct foreach_diff_data data = {
		index,
		NULL,
3486
		flags,
3487 3488 3489 3490
		cb,
		payload,
	};

Edward Thomson committed
3491 3492
	GIT_ASSERT_ARG(index);
	GIT_ASSERT_ARG(action == INDEX_ACTION_UPDATE || action == INDEX_ACTION_ADDALL);
3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508

	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;

3509
	opts.flags = GIT_DIFF_INCLUDE_TYPECHANGE;
3510
	if (action == INDEX_ACTION_ADDALL) {
3511
		opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
3512
			GIT_DIFF_RECURSE_UNTRACKED_DIRS;
3513

3514 3515 3516 3517 3518
		if (flags == GIT_INDEX_ADD_FORCE)
			opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
	}

	if ((error = git_diff_index_to_workdir(&diff, repo, index, &opts)) < 0)
3519 3520 3521
		goto cleanup;

	data.pathspec = &ps;
3522
	error = git_diff_foreach(diff, apply_each_file, NULL, NULL, NULL, &data);
3523 3524 3525
	git_diff_free(diff);

	if (error) /* make sure error is set if callback stopped iteration */
3526
		git_error_set_after_callback(error);
3527 3528 3529 3530 3531 3532

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

3533 3534 3535 3536 3537 3538 3539 3540 3541
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;
3542
	git_pathspec ps;
3543
	const char *match;
3544
	git_str path = GIT_STR_INIT;
3545

Edward Thomson committed
3546
	GIT_ASSERT_ARG(index);
3547

3548
	if ((error = git_pathspec__init(&ps, paths)) < 0)
3549 3550 3551 3552 3553 3554 3555 3556
		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 */
3557
		if (!git_pathspec__match(
Linquize committed
3558
				&ps.pathspec, entry->path, false, (bool)index->ignore_case,
3559
				&match, NULL))
3560 3561 3562 3563 3564 3565 3566 3567
			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;
			}
3568
			if (error < 0)   /* return < 0 means abort */
3569 3570 3571
				break;
		}

3572
		/* index manipulation may alter entry, so don't depend on it */
3573
		if ((error = git_str_sets(&path, entry->path)) < 0)
3574 3575
			break;

3576 3577 3578 3579
		switch (action) {
		case INDEX_ACTION_NONE:
			break;
		case INDEX_ACTION_UPDATE:
3580
			error = git_index_add_bypath(index, path.ptr);
3581 3582

			if (error == GIT_ENOTFOUND) {
3583
				git_error_clear();
3584

3585
				error = git_index_remove_bypath(index, path.ptr);
3586 3587 3588 3589 3590 3591

				if (!error) /* back up foreach if we removed this */
					i--;
			}
			break;
		case INDEX_ACTION_REMOVE:
3592
			if (!(error = git_index_remove_bypath(index, path.ptr)))
3593 3594 3595
				i--; /* back up foreach if we removed this */
			break;
		default:
3596
			git_error_set(GIT_ERROR_INVALID, "unknown index action %d", action);
3597 3598 3599 3600 3601
			error = -1;
			break;
		}
	}

3602
	git_str_dispose(&path);
3603
	git_pathspec__clear(&ps);
3604 3605 3606 3607 3608 3609 3610 3611 3612 3613

	return error;
}

int git_index_remove_all(
	git_index *index,
	const git_strarray *pathspec,
	git_index_matched_path_cb cb,
	void *payload)
{
3614
	int error = index_apply_to_all(
3615
		index, INDEX_ACTION_REMOVE, pathspec, cb, payload);
3616 3617

	if (error) /* make sure error is set if callback stopped iteration */
3618
		git_error_set_after_callback(error);
3619 3620

	return error;
3621 3622 3623 3624 3625 3626 3627 3628
}

int git_index_update_all(
	git_index *index,
	const git_strarray *pathspec,
	git_index_matched_path_cb cb,
	void *payload)
{
3629
	int error = index_apply_to_wd_diff(index, INDEX_ACTION_UPDATE, pathspec, 0, cb, payload);
3630
	if (error) /* make sure error is set if callback stopped iteration */
3631
		git_error_set_after_callback(error);
3632 3633

	return error;
3634
}
3635

3636
int git_index_snapshot_new(git_vector *snap, git_index *index)
3637 3638 3639 3640
{
	int error;

	GIT_REFCOUNT_INC(index);
3641

3642
	git_atomic32_inc(&index->readers);
3643
	git_vector_sort(&index->entries);
3644

3645
	error = git_vector_dup(snap, &index->entries, index->entries._cmp);
3646 3647

	if (error < 0)
3648
		git_index_snapshot_release(snap, index);
3649 3650 3651 3652

	return error;
}

3653
void git_index_snapshot_release(git_vector *snap, git_index *index)
3654
{
3655 3656
	git_vector_free(snap);

3657
	git_atomic32_dec(&index->readers);
3658

3659
	git_index_free(index);
3660
}
3661 3662 3663 3664 3665 3666 3667

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);
}
3668 3669 3670 3671 3672 3673 3674

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

3675 3676
	GIT_REFCOUNT_INC(index);

3677 3678 3679 3680
	writer->index = index;

	if (!index->index_file_path)
		return create_index_error(-1,
3681
			"failed to write index: The index is in-memory only");
3682 3683 3684

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

3686
		if (error == GIT_ELOCKED)
3687
			git_error_set(GIT_ERROR_INDEX, "the index is locked; this might be due to a concurrent or crashed process");
3688 3689 3690 3691

		return error;
	}

3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711
	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;

3712 3713 3714 3715 3716 3717
	return 0;
}

int git_indexwriter_commit(git_indexwriter *writer)
{
	int error;
3718
	git_oid checksum = {{ 0 }};
3719

3720 3721 3722
	if (!writer->should_write)
		return 0;

3723
	git_vector_sort(&writer->index->entries);
3724 3725
	git_vector_sort(&writer->index->reuc);

3726
	if ((error = write_index(&checksum, writer->index, &writer->file)) < 0) {
3727 3728 3729 3730 3731 3732 3733 3734 3735
		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) {
3736
		git_error_set(GIT_ERROR_OS, "could not read index timestamp");
3737 3738 3739
		return -1;
	}

3740
	writer->index->dirty = 0;
3741
	writer->index->on_disk = 1;
3742
	git_oid_cpy(&writer->index->checksum, &checksum);
3743

3744 3745 3746
	git_index_free(writer->index);
	writer->index = NULL;

3747 3748 3749 3750 3751 3752
	return 0;
}

void git_indexwriter_cleanup(git_indexwriter *writer)
{
	git_filebuf_cleanup(&writer->file);
3753 3754 3755

	git_index_free(writer->index);
	writer->index = NULL;
3756
}
3757 3758 3759

/* Deprecated functions */

3760
#ifndef GIT_DEPRECATE_HARD
3761 3762 3763 3764 3765 3766
int git_index_add_frombuffer(
    git_index *index, const git_index_entry *source_entry,
    const void *buffer, size_t len)
{
	return git_index_add_from_buffer(index, source_entry, buffer, len);
}
3767
#endif