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

#include <stddef.h>

#include "common.h"
11
#include "repository.h"
12
#include "index.h"
13
#include "tree.h"
14
#include "tree-cache.h"
15
#include "hash.h"
16 17
#include "iterator.h"
#include "pathspec.h"
18
#include "ignore.h"
19
#include "blob.h"
20
#include "idxmap.h"
21
#include "diff.h"
David Turner committed
22
#include "varint.h"
23

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

30 31 32
GIT__USE_IDXMAP
GIT__USE_IDXMAP_ICASE

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#define INSERT_IN_MAP_EX(idx, map, e, err) do {				\
		if ((idx)->ignore_case)					\
			git_idxmap_icase_insert((khash_t(idxicase) *) (map), (e), (e), (err)); \
		else							\
			git_idxmap_insert((map), (e), (e), (err));	\
	} while (0)

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

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

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

56 57 58 59
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);

60 61 62 63 64 65 66 67 68
#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7)
#define short_entry_size(len) entry_size(struct entry_short, len)
#define long_entry_size(len) entry_size(struct entry_long, len)

#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
69 70
static const unsigned int INDEX_VERSION_NUMBER_DEFAULT = 2;
static const unsigned int INDEX_VERSION_NUMBER_LB = 2;
71
static const unsigned int INDEX_VERSION_NUMBER_EXT = 3;
David Turner committed
72 73
static const unsigned int INDEX_VERSION_NUMBER_COMP = 4;
static const unsigned int INDEX_VERSION_NUMBER_UB = 4;
74 75 76

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

80 81
#define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx)))

82 83 84 85 86 87 88 89 90 91 92
struct index_header {
	uint32_t signature;
	uint32_t version;
	uint32_t entry_count;
};

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

93 94 95 96 97
struct entry_time {
	uint32_t seconds;
	uint32_t nanoseconds;
};

98
struct entry_short {
99 100
	struct entry_time ctime;
	struct entry_time mtime;
101 102 103 104 105 106 107 108
	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
109
	char path[1]; /* arbitrary length */
110 111 112
};

struct entry_long {
113 114
	struct entry_time ctime;
	struct entry_time mtime;
115 116 117 118 119 120 121 122 123
	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
124
	char path[1]; /* arbitrary length */
125 126
};

Edward Thomson committed
127 128
struct entry_srch_key {
	const char *path;
129
	size_t pathlen;
Edward Thomson committed
130 131 132
	int stage;
};

133 134 135 136 137 138 139 140 141 142 143 144
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];
};

145 146 147 148
/* local declarations */
static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size);
static int read_header(struct index_header *dest, const void *buffer);

149
static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
150
static bool is_index_extended(git_index *index);
151
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file);
152

153
static void index_entry_free(git_index_entry *entry);
Edward Thomson committed
154 155
static void index_entry_reuc_free(git_index_reuc_entry *reuc);

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

163 164
	len1 = srch_key->pathlen;
	len2 = entry->pathlen;
165
	len = len1 < len2 ? len1 : len2;
Edward Thomson committed
166

167 168 169 170 171 172 173
	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
174

175
	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
176
		return srch_key->stage - GIT_IDXENTRY_STAGE(&entry->entry);
177 178

	return 0;
179 180
}

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

188 189
	len1 = srch_key->pathlen;
	len2 = entry->pathlen;
190
	len = len1 < len2 ? len1 : len2;
Edward Thomson committed
191

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

194 195 196 197 198 199 200 201
	if (cmp)
		return cmp;
	if (len1 < len2)
		return -1;
	if (len1 > len2)
		return 1;

	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
202
		return srch_key->stage - GIT_IDXENTRY_STAGE(&entry->entry);
203 204

	return 0;
Edward Thomson committed
205 206
}

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

Edward Thomson committed
211 212 213
	return strcmp((const char *)path, entry->path);
}

214
static int index_entry_isrch_path(const void *path, const void *array_member)
Edward Thomson committed
215 216 217 218
{
	const git_index_entry *entry = array_member;

	return strcasecmp((const char *)path, entry->path);
219 220
}

221
int git_index_entry_cmp(const void *a, const void *b)
222
{
Edward Thomson committed
223
	int diff;
224 225
	const git_index_entry *entry_a = a;
	const git_index_entry *entry_b = b;
226

Edward Thomson committed
227 228 229
	diff = strcmp(entry_a->path, entry_b->path);

	if (diff == 0)
230
		diff = (GIT_IDXENTRY_STAGE(entry_a) - GIT_IDXENTRY_STAGE(entry_b));
Edward Thomson committed
231 232

	return diff;
233 234
}

235
int git_index_entry_icmp(const void *a, const void *b)
236
{
Edward Thomson committed
237
	int diff;
238 239 240
	const git_index_entry *entry_a = a;
	const git_index_entry *entry_b = b;

Edward Thomson committed
241 242 243
	diff = strcasecmp(entry_a->path, entry_b->path);

	if (diff == 0)
244
		diff = (GIT_IDXENTRY_STAGE(entry_a) - GIT_IDXENTRY_STAGE(entry_b));
Edward Thomson committed
245 246 247 248

	return diff;
}

Edward Thomson committed
249 250 251 252
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;
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 && name_b->ancestor)
		return -1;
259

Edward Thomson committed
260 261
	if (name_a->ancestor)
		return strcmp(name_a->ancestor, name_b->ancestor);
262

Edward Thomson committed
263 264
	if (!name_a->ours || !name_b->ours)
		return 0;
265

Edward Thomson committed
266 267 268
	return strcmp(name_a->ours, name_b->ours);
}

Vicent Marti committed
269 270 271
/**
 * TODO: enable this when resolving case insensitive conflicts
 */
272
#if 0
Edward Thomson committed
273 274 275 276
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;
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 && name_b->ancestor)
		return -1;
283

Edward Thomson committed
284 285
	if (name_a->ancestor)
		return strcasecmp(name_a->ancestor, name_b->ancestor);
286

Edward Thomson committed
287 288
	if (!name_a->ours || !name_b->ours)
		return 0;
289

Edward Thomson committed
290 291
	return strcasecmp(name_a->ours, name_b->ours);
}
292
#endif
Edward Thomson committed
293

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

	return strcmp(key, reuc->path);
299 300
}

Edward Thomson committed
301
static int reuc_isrch(const void *key, const void *array_member)
302
{
Edward Thomson committed
303
	const git_index_reuc_entry *reuc = array_member;
304

Edward Thomson committed
305
	return strcasecmp(key, reuc->path);
306 307
}

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

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

Edward Thomson committed
316 317 318 319 320 321 322 323
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);
}

324 325 326 327 328 329 330
static void index_entry_reuc_free(git_index_reuc_entry *reuc)
{
	git__free(reuc);
}

static void index_entry_free(git_index_entry *entry)
{
331 332 333
	if (!entry)
		return;

334
	memset(&entry->id, 0, sizeof(entry->id));
335 336 337
	git__free(entry);
}

338
unsigned int git_index__create_mode(unsigned int mode)
339 340 341
{
	if (S_ISLNK(mode))
		return S_IFLNK;
342

343 344
	if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR))
		return (S_IFLNK | S_IFDIR);
345

346
	return S_IFREG | GIT_PERMS_CANONICAL(mode);
347 348
}

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

360
	return git_index__create_mode(mode);
361 362
}

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

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

384
void git_index__set_ignore_case(git_index *index, bool ignore_case)
385
{
386 387
	index->ignore_case = ignore_case;

388 389 390 391 392 393 394 395 396 397 398
	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;
	}
399

400 401
	git_vector_set_cmp(&index->entries,
		ignore_case ? git_index_entry_icmp : git_index_entry_cmp);
402
	git_vector_sort(&index->entries);
Edward Thomson committed
403

404
	git_vector_set_cmp(&index->reuc, ignore_case ? reuc_icmp : reuc_cmp);
Edward Thomson committed
405
	git_vector_sort(&index->reuc);
406 407
}

408
int git_index_open(git_index **index_out, const char *index_path)
409 410
{
	git_index *index;
411
	int error = -1;
412

413
	assert(index_out);
414

415 416
	index = git__calloc(1, sizeof(git_index));
	GITERR_CHECK_ALLOC(index);
417

418
	git_pool_init(&index->tree_pool, 1);
419

420 421
	if (index_path != NULL) {
		index->index_file_path = git__strdup(index_path);
422 423
		if (!index->index_file_path)
			goto fail;
424 425 426 427 428

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

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

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

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

Vicent Marti committed
446
	*index_out = index;
447
	GIT_REFCOUNT_INC(index);
448

449
	return 0;
450 451

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

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

462
static void index_free(git_index *index)
463
{
464 465 466 467 468
	/* index iterators increment the refcount of the index, so if we
	 * get here then there should be no outstanding iterators.
	 */
	assert(!git_atomic_get(&index->readers));

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

476
	git__free(index->index_file_path);
477

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

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

487
	GIT_REFCOUNT_DEC(index, index_free);
488 489
}

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

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

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

	git_vector_clear(&index->deleted);
}

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

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

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

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

	return error;
529
}
530

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

535 536
	assert(index);

537
	index->tree = NULL;
538
	git_pool_clear(&index->tree_pool);
539

540
	git_idxmap_clear(index->entries_map);
541
	while (!error && index->entries.length > 0)
542
		error = index_remove_entry(index, index->entries.length - 1);
543 544
	index_free_deleted(index);

545
	git_index_reuc_clear(index);
Edward Thomson committed
546
	git_index_name_clear(index);
nulltoken committed
547

548
	git_futils_filestamp_set(&index->stamp, NULL);
549

550
	return error;
551 552
}

553 554 555 556 557 558
static int create_index_error(int error, const char *msg)
{
	giterr_set(GITERR_INDEX, msg);
	return error;
}

Russell Belfer committed
559
int git_index_set_caps(git_index *index, int caps)
560
{
Linquize committed
561
	unsigned int old_ignore_case;
562

563 564
	assert(index);

565 566
	old_ignore_case = index->ignore_case;

567
	if (caps == GIT_INDEXCAP_FROM_OWNER) {
568
		git_repository *repo = INDEX_OWNER(index);
569 570
		int val;

571 572 573
		if (!repo)
			return create_index_error(
				-1, "Cannot access repository to set index caps");
574

575
		if (!git_repository__cvar(&val, repo, GIT_CVAR_IGNORECASE))
576
			index->ignore_case = (val != 0);
577
		if (!git_repository__cvar(&val, repo, GIT_CVAR_FILEMODE))
578
			index->distrust_filemode = (val == 0);
579
		if (!git_repository__cvar(&val, repo, GIT_CVAR_SYMLINKS))
580
			index->no_symlinks = (val == 0);
581 582 583 584 585 586 587
	}
	else {
		index->ignore_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0);
		index->distrust_filemode = ((caps & GIT_INDEXCAP_NO_FILEMODE) != 0);
		index->no_symlinks = ((caps & GIT_INDEXCAP_NO_SYMLINKS) != 0);
	}

588
	if (old_ignore_case != index->ignore_case) {
Linquize committed
589
		git_index__set_ignore_case(index, (bool)index->ignore_case);
590 591
	}

592 593 594
	return 0;
}

Russell Belfer committed
595
int git_index_caps(const git_index *index)
596 597 598 599 600 601
{
	return ((index->ignore_case ? GIT_INDEXCAP_IGNORE_CASE : 0) |
			(index->distrust_filemode ? GIT_INDEXCAP_NO_FILEMODE : 0) |
			(index->no_symlinks ? GIT_INDEXCAP_NO_SYMLINKS : 0));
}

602 603 604 605 606 607 608 609 610 611
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)
{
612
	int fd;
613 614 615 616 617 618
	ssize_t bytes_read;
	git_oid checksum = {{ 0 }};

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

619
	if (p_lseek(fd, -20, SEEK_END) < 0) {
620 621 622 623 624 625 626 627 628 629 630 631 632 633
		p_close(fd);
		giterr_set(GITERR_OS, "failed to seek to end of file");
		return -1;
	}

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

	if (bytes_read < 0)
		return -1;

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

634
int git_index_read(git_index *index, int force)
635
{
636
	int error = 0, updated;
637
	git_buf buffer = GIT_BUF_INIT;
638
	git_futils_filestamp stamp = index->stamp;
639

640 641
	if (!index->index_file_path)
		return create_index_error(-1,
642
			"Failed to read index: The index is in-memory only");
643

644 645 646
	index->on_disk = git_path_exists(index->index_file_path);

	if (!index->on_disk) {
647
		if (force)
648
			return git_index_clear(index);
649
		return 0;
650 651
	}

652 653
	if ((updated = git_futils_filestamp_check(&stamp, index->index_file_path) < 0) ||
	    ((updated = compare_checksum(index)) < 0)) {
654 655 656 657
		giterr_set(
			GITERR_INDEX,
			"Failed to read index: '%s' no longer exists",
			index->index_file_path);
658
		return updated;
659 660 661
	}
	if (!updated && !force)
		return 0;
662 663

	error = git_futils_readbuffer(&buffer, index->index_file_path);
664 665
	if (error < 0)
		return error;
666

667 668 669
	index->tree = NULL;
	git_pool_clear(&index->tree_pool);

670 671 672 673
	error = git_index_clear(index);

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

675 676
	if (!error)
		git_futils_filestamp_set(&index->stamp, &stamp);
677

678
	git_buf_free(&buffer);
679 680 681
	return error;
}

682
int git_index__changed_relative_to(
683
	git_index *index, const git_oid *checksum)
684 685 686 687 688
{
	/* attempt to update index (ignoring errors) */
	if (git_index_read(index, false) < 0)
		giterr_clear();

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

692
static bool is_racy_entry(git_index *index, const git_index_entry *entry)
693 694 695 696 697
{
	/* Git special-cases submodules in the check */
	if (S_ISGITLINK(entry->mode))
		return false;

698
	return git_index_entry_newer_than_index(entry, index);
699 700
}

701 702 703 704
/*
 * Force the next diff to take a look at those entries which have the
 * same timestamp as the current index.
 */
705
static int truncate_racily_clean(git_index *index)
706 707
{
	size_t i;
708
	int error;
709
	git_index_entry *entry;
710
	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
711 712 713
	git_diff *diff = NULL;
	git_vector paths = GIT_VECTOR_INIT;
	git_diff_delta *delta;
714 715 716 717

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

719 720 721 722 723
	/* 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;
724
	git_vector_foreach(&index->entries, i, entry) {
725
		if ((entry->flags_extended & GIT_IDXENTRY_UPTODATE) == 0 &&
726
			is_racy_entry(index, entry))
727 728
			git_vector_insert(&paths, (char *)entry->path);
	}
729

730 731
	if (paths.length == 0)
		goto done;
732

733 734
	diff_opts.pathspec.count = paths.length;
	diff_opts.pathspec.strings = (char **)paths.contents;
735

736 737
	if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0)
		return error;
738

739 740 741 742 743 744 745 746
	git_vector_foreach(&diff->deltas, i, delta) {
		entry = (git_index_entry *)git_index_get_bypath(index, delta->old_file.path, 0);

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

749 750 751
done:
	git_diff_free(diff);
	git_vector_free(&paths);
752
	return 0;
753 754
}

David Turner committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
unsigned git_index_version(git_index *index)
{
	assert(index);

	return index->version;
}

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

	if (version < INDEX_VERSION_NUMBER_LB ||
	    version > INDEX_VERSION_NUMBER_UB) {
		giterr_set(GITERR_INDEX, "Invalid version number");
		return -1;
	}

	index->version = version;

	return 0;
}

777 778
int git_index_write(git_index *index)
{
779
	git_indexwriter writer = GIT_INDEXWRITER_INIT;
780
	int error;
781

782 783
	truncate_racily_clean(index);

784 785
	if ((error = git_indexwriter_init(&writer, index)) == 0)
		error = git_indexwriter_commit(&writer);
786

787
	git_indexwriter_cleanup(&writer);
788

789
	return error;
790
}
791

Jacques Germishuys committed
792
const char * git_index_path(const git_index *index)
793 794 795 796
{
	assert(index);
	return index->index_file_path;
}
797

798 799 800 801 802 803
int git_index_write_tree(git_oid *oid, git_index *index)
{
	git_repository *repo;

	assert(oid && index);

804
	repo = INDEX_OWNER(index);
805

806 807
	if (repo == NULL)
		return create_index_error(-1, "Failed to write tree. "
808 809 810 811 812
		  "The index file is not backed up by an existing repository");

	return git_tree__write_index(oid, index, repo);
}

813 814
int git_index_write_tree_to(
	git_oid *oid, git_index *index, git_repository *repo)
815 816 817 818 819
{
	assert(oid && index && repo);
	return git_tree__write_index(oid, index, repo);
}

820
size_t git_index_entrycount(const git_index *index)
821 822
{
	assert(index);
823
	return index->entries.length;
824 825
}

826 827
const git_index_entry *git_index_get_byindex(
	git_index *index, size_t n)
828 829
{
	assert(index);
830
	git_vector_sort(&index->entries);
Edward Thomson committed
831
	return git_vector_get(&index->entries, n);
832 833
}

834 835
const git_index_entry *git_index_get_bypath(
	git_index *index, const char *path, int stage)
836
{
837 838
	khiter_t pos;
	git_index_entry key = {{ 0 }};
Edward Thomson committed
839 840 841

	assert(index);

842 843 844
	key.path = path;
	GIT_IDXENTRY_STAGE_SET(&key, stage);

845
	LOOKUP_IN_MAP(pos, index, &key);
Edward Thomson committed
846

847 848 849 850 851
	if (git_idxmap_valid_index(index->entries_map, pos))
		return git_idxmap_value_at(index->entries_map, pos);

	giterr_set(GITERR_INDEX, "Index does not contain %s", path);
	return NULL;
852 853
}

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

872 873 874 875 876 877 878 879 880 881 882 883
static void index_entry_adjust_namemask(
		git_index_entry *entry,
		size_t path_length)
{
	entry->flags &= ~GIT_IDXENTRY_NAMEMASK;

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

884 885 886 887 888 889
/* 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.
 */
890 891 892
static int index_entry_create(
	git_index_entry **out,
	git_repository *repo,
893 894
	const char *path,
	bool from_workdir)
895
{
896
	size_t pathlen = strlen(path), alloclen;
897
	struct entry_internal *entry;
898 899 900 901 902 903 904 905
	unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS;

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

907 908
	if (!git_path_isvalid(repo, path, path_valid_flags)) {
		giterr_set(GITERR_INDEX, "invalid path: '%s'", path);
909
		return -1;
910
	}
911

912 913 914
	GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(struct entry_internal), pathlen);
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
	entry = git__calloc(1, alloclen);
915
	GITERR_CHECK_ALLOC(entry);
916 917 918 919 920

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

921 922
	*out = (git_index_entry *)entry;
	return 0;
923 924
}

925
static int index_entry_init(
926 927 928
	git_index_entry **entry_out,
	git_index *index,
	const char *rel_path)
929
{
930
	int error = 0;
931
	git_index_entry *entry = NULL;
932 933
	struct stat st;
	git_oid oid;
934

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

940
	if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, true) < 0)
941 942
		return -1;

943 944 945
	/* 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);
946

947 948 949 950
	if (error < 0) {
		index_entry_free(entry);
		return error;
	}
951

952
	entry->id = oid;
953
	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
954

955
	*entry_out = (git_index_entry *)entry;
956
	return 0;
957 958
}

959 960
static git_index_reuc_entry *reuc_entry_alloc(const char *path)
{
961
	size_t pathlen = strlen(path),
962 963
		structlen = sizeof(struct reuc_entry_internal),
		alloclen;
964 965
	struct reuc_entry_internal *entry;

966 967
	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, structlen, pathlen) ||
		GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1))
968 969
		return NULL;

970
	entry = git__calloc(1, alloclen);
971 972 973 974 975 976 977 978 979 980
	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
981 982
static int index_entry_reuc_init(git_index_reuc_entry **reuc_out,
	const char *path,
Edward Thomson committed
983 984 985
	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
986 987 988 989 990
{
	git_index_reuc_entry *reuc = NULL;

	assert(reuc_out && path);

991
	*reuc_out = reuc = reuc_entry_alloc(path);
Edward Thomson committed
992 993
	GITERR_CHECK_ALLOC(reuc);

994 995
	if ((reuc->mode[0] = ancestor_mode) > 0) {
		assert(ancestor_oid);
996
		git_oid_cpy(&reuc->oid[0], ancestor_oid);
997
	}
Edward Thomson committed
998

999 1000
	if ((reuc->mode[1] = our_mode) > 0) {
		assert(our_oid);
1001
		git_oid_cpy(&reuc->oid[1], our_oid);
1002
	}
Edward Thomson committed
1003

1004 1005
	if ((reuc->mode[2] = their_mode) > 0) {
		assert(their_oid);
1006
		git_oid_cpy(&reuc->oid[2], their_oid);
1007
	}
Edward Thomson committed
1008 1009 1010 1011

	return 0;
}

1012 1013
static void index_entry_cpy(
	git_index_entry *tgt,
1014
	const git_index_entry *src)
1015
{
1016
	const char *tgt_path = tgt->path;
1017
	memcpy(tgt, src, sizeof(*tgt));
1018
	tgt->path = tgt_path;
1019 1020
}

1021 1022
static int index_entry_dup(
	git_index_entry **out,
1023
	git_index *index,
1024
	const git_index_entry *src)
1025
{
1026
	if (index_entry_create(out, INDEX_OWNER(index), src->path, false) < 0)
1027
		return -1;
1028

1029 1030 1031
	index_entry_cpy(*out, src);
	return 0;
}
1032

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
static void index_entry_cpy_nocache(
	git_index_entry *tgt,
	const git_index_entry *src)
{
	git_oid_cpy(&tgt->id, &src->id);
	tgt->mode = src->mode;
	tgt->flags = src->flags;
	tgt->flags_extended = (src->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS);
}

static int index_entry_dup_nocache(
	git_index_entry **out,
	git_index *index,
	const git_index_entry *src)
{
1048
	if (index_entry_create(out, INDEX_OWNER(index), src->path, false) < 0)
1049
		return -1;
1050

1051
	index_entry_cpy_nocache(*out, src);
1052
	return 0;
1053 1054
}

1055
static int has_file_name(git_index *index,
1056
	 const git_index_entry *entry, size_t pos, int ok_to_replace)
1057 1058
{
	int retval = 0;
1059 1060 1061
	size_t len = strlen(entry->path);
	int stage = GIT_IDXENTRY_STAGE(entry);
	const char *name = entry->path;
1062

1063
	while (pos < index->entries.length) {
1064
		struct entry_internal *p = index->entries.contents[pos++];
1065

1066
		if (len >= p->pathlen)
1067
			break;
1068
		if (memcmp(name, p->path, len))
1069
			break;
1070
		if (GIT_IDXENTRY_STAGE(&p->entry) != stage)
1071
			continue;
1072
		if (p->path[len] != '/')
1073 1074 1075 1076
			continue;
		retval = -1;
		if (!ok_to_replace)
			break;
1077

1078
		if (index_remove_entry(index, --pos) < 0)
1079
			break;
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	}
	return retval;
}

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

	for (;;) {
1097
		size_t len, pos;
1098 1099 1100 1101

		for (;;) {
			if (*--slash == '/')
				break;
1102
			if (slash <= entry->path)
1103 1104 1105 1106
				return retval;
		}
		len = slash - name;

1107
		if (!index_find(&pos, index, name, len, stage)) {
1108 1109 1110 1111
			retval = -1;
			if (!ok_to_replace)
				break;

1112
			if (index_remove_entry(index, pos) < 0)
1113
				break;
1114
			continue;
1115 1116 1117 1118 1119 1120 1121
		}

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

1125 1126
			if (p->pathlen <= len ||
			    p->path[len] != '/' ||
1127
			    memcmp(p->path, name, len))
1128
				break; /* not our subdirectory */
1129

1130
			if (GIT_IDXENTRY_STAGE(&p->entry) == stage)
1131 1132 1133
				return retval;
		}
	}
1134

1135 1136
	return retval;
}
1137

1138
static int check_file_directory_collision(git_index *index,
1139 1140 1141 1142 1143 1144
		git_index_entry *entry, size_t pos, int ok_to_replace)
{
	int retval = has_file_name(index, entry, pos, ok_to_replace);
	retval = retval + has_dir_name(index, entry, ok_to_replace);

	if (retval) {
1145 1146
		giterr_set(GITERR_INDEX,
			"'%s' appears as both a file and a directory", entry->path);
1147 1148 1149 1150 1151
		return -1;
	}

	return 0;
}
1152

1153
static int canonicalize_directory_path(
1154 1155 1156
	git_index *index,
	git_index_entry *entry,
	git_index_entry *existing)
1157 1158 1159 1160 1161 1162 1163 1164 1165
{
	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 */
1166 1167
	if (existing) {
		memcpy((char *)entry->path, existing->path, strlen(existing->path));
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
		return 0;
	}

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

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

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

		search_len = strlen(search);

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

		while ((match = git_vector_get(&index->entries, pos))) {
			if (GIT_IDXENTRY_STAGE(match) != 0) {
				/* conflicts do not contribute to canonical paths */
1192
			} else if (strncmp(search, match->path, search_len) == 0) {
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
				/* 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;
}

1223 1224 1225 1226 1227 1228 1229 1230 1231
static int index_no_dups(void **old, void *new)
{
	const git_index_entry *entry = new;
	GIT_UNUSED(old);
	giterr_set(GITERR_INDEX, "'%s' appears multiple times at stage %d",
		entry->path, GIT_IDXENTRY_STAGE(entry));
	return GIT_EEXISTS;
}

1232
static void index_existing_and_best(
1233
	git_index_entry **existing,
1234
	size_t *existing_position,
1235
	git_index_entry **best,
1236 1237 1238
	git_index *index,
	const git_index_entry *entry)
{
1239
	git_index_entry *e;
1240 1241 1242 1243
	size_t pos;
	int error;

	error = index_find(&pos,
1244
		index, entry->path, 0, GIT_IDXENTRY_STAGE(entry));
1245 1246 1247 1248 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

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

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

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

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

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

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

1278 1279 1280 1281
/* 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).
1282
 *
1283 1284 1285
 * 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.
1286
 *
1287
 * trust_mode is whether we trust the mode in entry_ptr.
1288 1289
 *
 * trust_id is whether we trust the id or it should be validated.
1290 1291
 */
static int index_insert(
1292 1293 1294 1295
	git_index *index,
	git_index_entry **entry_ptr,
	int replace,
	bool trust_path,
1296 1297
	bool trust_mode,
	bool trust_id)
1298
{
1299
	int error = 0;
1300
	size_t path_length, position;
1301
	git_index_entry *existing, *best, *entry;
1302

1303 1304 1305
	assert(index && entry_ptr);

	entry = *entry_ptr;
1306

1307
	/* make sure that the path length flag is correct */
1308
	path_length = ((struct entry_internal *)entry)->pathlen;
1309
	index_entry_adjust_namemask(entry, path_length);
1310

1311 1312 1313
	/* this entry is now up-to-date and should not be checked for raciness */
	entry->flags_extended |= GIT_IDXENTRY_UPTODATE;

1314 1315
	git_vector_sort(&index->entries);

1316 1317 1318 1319 1320 1321 1322 1323 1324
	/* look if an entry with this path already exists, either staged, or (if
	 * this entry is a regular staged item) as the "ours" side of a conflict.
	 */
	index_existing_and_best(&existing, &position, &best, index, entry);

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

1326 1327
	/* canonicalize the directory name */
	if (!trust_path)
1328
		error = canonicalize_directory_path(index, entry, best);
1329

1330 1331 1332 1333 1334 1335 1336 1337 1338
	/* ensure that the given id exists (unless it's a submodule) */
	if (!error && !trust_id && INDEX_OWNER(index) &&
		(entry->mode & GIT_FILEMODE_COMMIT) != GIT_FILEMODE_COMMIT) {

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

1339
	/* look for tree / blob name collisions, removing conflicts if requested */
1340 1341 1342
	if (!error)
		error = check_file_directory_collision(index, entry, position, replace);

1343
	if (error < 0)
1344
		/* skip changes */;
1345 1346 1347 1348

	/* if we are replacing an existing item, overwrite the existing entry
	 * and return it in place of the passed in one.
	 */
1349
	else if (existing) {
1350 1351 1352 1353 1354 1355 1356
		if (replace) {
			index_entry_cpy(existing, entry);

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

1357
		index_entry_free(entry);
1358
		*entry_ptr = entry = existing;
1359
	}
1360
	else {
1361 1362 1363
		/* if replace is not requested or no existing entry exists, insert
		 * at the sorted position.  (Since we re-sort after each insert to
		 * check for dups, this is actually cheaper in the long run.)
1364
		 */
1365
		error = git_vector_insert_sorted(&index->entries, entry, index_no_dups);
1366 1367

		if (error == 0) {
1368
			INSERT_IN_MAP(index, entry, error);
1369
		}
1370
	}
1371

1372 1373 1374 1375
	if (error < 0) {
		index_entry_free(*entry_ptr);
		*entry_ptr = NULL;
	}
1376

1377
	return error;
1378 1379
}

Edward Thomson committed
1380
static int index_conflict_to_reuc(git_index *index, const char *path)
1381
{
1382
	const git_index_entry *conflict_entries[3];
Edward Thomson committed
1383
	int ancestor_mode, our_mode, their_mode;
Edward Thomson committed
1384
	git_oid const *ancestor_oid, *our_oid, *their_oid;
1385
	int ret;
1386

Edward Thomson committed
1387 1388
	if ((ret = git_index_conflict_get(&conflict_entries[0],
		&conflict_entries[1], &conflict_entries[2], index, path)) < 0)
1389
		return ret;
1390

Edward Thomson committed
1391 1392 1393
	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;
1394

1395 1396 1397
	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
1398 1399 1400 1401 1402 1403

	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;
1404 1405
}

1406 1407 1408 1409 1410 1411 1412 1413 1414
static bool valid_filemode(const int filemode)
{
	return (filemode == GIT_FILEMODE_BLOB ||
		filemode == GIT_FILEMODE_BLOB_EXECUTABLE ||
		filemode == GIT_FILEMODE_LINK ||
		filemode == GIT_FILEMODE_COMMIT);
}

int git_index_add_frombuffer(
1415
    git_index *index, const git_index_entry *source_entry,
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
    const void *buffer, size_t len)
{
	git_index_entry *entry = NULL;
	int error = 0;
	git_oid id;

	assert(index && source_entry->path);

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

	if (!valid_filemode(source_entry->mode)) {
		giterr_set(GITERR_INDEX, "invalid filemode");
		return -1;
	}

1434
	if (index_entry_dup(&entry, index, source_entry) < 0)
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
		return -1;

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

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

1446
	if ((error = index_insert(index, &entry, 1, true, true, true)) < 0)
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
		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;
}

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
static int add_repo_as_submodule(git_index_entry **out, git_index *index, const char *path)
{
	git_repository *sub;
	git_buf abspath = GIT_BUF_INIT;
	git_repository *repo = INDEX_OWNER(index);
	git_reference *head;
	git_index_entry *entry;
	struct stat st;
	int error;

1467
	if (index_entry_create(&entry, INDEX_OWNER(index), path, true) < 0)
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
		return -1;

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

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

	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);

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

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

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

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

	*out = entry;
	return 0;
}
1496

1497
int git_index_add_bypath(git_index *index, const char *path)
1498
{
Edward Thomson committed
1499 1500 1501 1502 1503
	git_index_entry *entry = NULL;
	int ret;

	assert(index && path);

1504
	if ((ret = index_entry_init(&entry, index, path)) == 0)
1505
		ret = index_insert(index, &entry, 1, false, false, true);
1506 1507 1508 1509 1510 1511 1512 1513 1514

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

1515
		giterr_state_capture(&err, ret);
1516 1517 1518

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

1521
		giterr_state_free(&err);
1522 1523 1524 1525 1526 1527 1528 1529 1530

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

1531
			if ((ret = index_insert(index, &entry, 1, false, false, true)) < 0)
1532 1533 1534 1535 1536 1537 1538 1539
				return ret;
		} else if (ret < 0) {
			return ret;
		} else {
			ret = git_submodule_add_to_index(sm, false);
			git_submodule_free(sm);
			return ret;
		}
1540
	}
Edward Thomson committed
1541 1542 1543

	/* Adding implies conflict was resolved, move conflict entries to REUC */
	if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND)
1544
		return ret;
Edward Thomson committed
1545 1546 1547

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

1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
int git_index_remove_bypath(git_index *index, const char *path)
{
	int ret;

	assert(index && path);

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

1562 1563 1564
	if (ret == GIT_ENOTFOUND)
		giterr_clear();

1565 1566 1567
	return 0;
}

1568 1569 1570 1571 1572 1573 1574 1575
int git_index__fill(git_index *index, const git_vector *source_entries)
{
	const git_index_entry *source_entry = NULL;
	size_t i;
	int ret = 0;

	assert(index);

1576 1577 1578
	if (!source_entries->length)
		return 0;

1579
	git_vector_size_hint(&index->entries, source_entries->length);
1580
	git_idxmap_resize(index->entries_map, (khint_t)(source_entries->length * 1.3));
1581

1582 1583 1584 1585 1586 1587
	git_vector_foreach(source_entries, i, source_entry) {
		git_index_entry *entry = NULL;

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

1588
		index_entry_adjust_namemask(entry, ((struct entry_internal *)entry)->pathlen);
1589
		entry->flags_extended |= GIT_IDXENTRY_UPTODATE;
1590
		entry->mode = git_index__create_mode(entry->mode);
1591

1592
		if ((ret = git_vector_insert(&index->entries, entry)) < 0)
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
			break;

		INSERT_IN_MAP(index, entry, ret);
		if (ret < 0)
			break;
	}

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

	return ret;
}

1606

Edward Thomson committed
1607
int git_index_add(git_index *index, const git_index_entry *source_entry)
1608 1609 1610 1611
{
	git_index_entry *entry = NULL;
	int ret;

1612
	assert(index && source_entry && source_entry->path);
1613

1614 1615 1616 1617 1618
	if (!valid_filemode(source_entry->mode)) {
		giterr_set(GITERR_INDEX, "invalid filemode");
		return -1;
	}

1619
	if ((ret = index_entry_dup(&entry, index, source_entry)) < 0 ||
1620
		(ret = index_insert(index, &entry, 1, true, true, false)) < 0)
1621
		return ret;
1622

1623
	git_tree_cache_invalidate_path(index->tree, entry->path);
1624
	return 0;
1625 1626
}

Edward Thomson committed
1627
int git_index_remove(git_index *index, const char *path, int stage)
1628
{
1629
	int error;
1630
	size_t position;
1631
	git_index_entry remove_key = {{ 0 }};
1632

1633 1634
	remove_key.path = path;
	GIT_IDXENTRY_STAGE_SET(&remove_key, stage);
1635 1636

	DELETE_IN_MAP(index, &remove_key);
1637

1638
	if (index_find(&position, index, path, 0, stage) < 0) {
1639 1640 1641 1642 1643
		giterr_set(
			GITERR_INDEX, "Index does not contain %s at stage %d", path, stage);
		error = GIT_ENOTFOUND;
	} else {
		error = index_remove_entry(index, position);
1644
	}
Edward Thomson committed
1645

1646
	return error;
1647
}
1648

1649 1650 1651 1652 1653 1654 1655
int git_index_remove_directory(git_index *index, const char *dir, int stage)
{
	git_buf pfx = GIT_BUF_INIT;
	int error = 0;
	size_t pos;
	git_index_entry *entry;

1656 1657
	if (!(error = git_buf_sets(&pfx, dir)) &&
		!(error = git_path_to_dir(&pfx)))
1658
		index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY);
1659

1660
	while (!error) {
1661 1662 1663 1664
		entry = git_vector_get(&index->entries, pos);
		if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0)
			break;

1665
		if (GIT_IDXENTRY_STAGE(entry) != stage) {
1666 1667 1668 1669
			++pos;
			continue;
		}

1670
		error = index_remove_entry(index, pos);
1671

1672
		/* removed entry at 'pos' so we don't need to increment */
1673 1674 1675 1676 1677 1678 1679
	}

	git_buf_free(&pfx);

	return error;
}

1680 1681 1682 1683 1684 1685
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;

1686
	index_find(&pos, index, prefix, strlen(prefix), GIT_INDEX_STAGE_ANY);
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
	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;
}

1697
int git_index__find_pos(
1698 1699 1700
	size_t *out, git_index *index, const char *path, size_t path_len, int stage)
{
	assert(index && path);
1701
	return index_find(out, index, path, path_len, stage);
Edward Thomson committed
1702 1703
}

1704
int git_index_find(size_t *at_pos, git_index *index, const char *path)
1705
{
1706
	size_t pos;
Edward Thomson committed
1707 1708 1709

	assert(index && path);

1710 1711
	if (git_vector_bsearch2(
			&pos, &index->entries, index->entries_search_path, path) < 0) {
1712
		giterr_set(GITERR_INDEX, "Index does not contain %s", path);
1713
		return GIT_ENOTFOUND;
1714
	}
Edward Thomson committed
1715 1716

	/* Since our binary search only looked at path, we may be in the
1717 1718
	 * middle of a list of stages.
	 */
1719 1720
	for (; pos > 0; --pos) {
		const git_index_entry *prev = git_vector_get(&index->entries, pos - 1);
Edward Thomson committed
1721 1722 1723 1724 1725

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

1726 1727 1728 1729
	if (at_pos)
		*at_pos = pos;

	return 0;
1730 1731
}

Edward Thomson committed
1732 1733 1734 1735 1736 1737
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 };
1738
	unsigned short i;
Edward Thomson committed
1739 1740 1741 1742
	int ret = 0;

	assert (index);

1743 1744 1745 1746 1747 1748
	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))
1749
		goto on_error;
Edward Thomson committed
1750

1751 1752 1753 1754
	/* Validate entries */
	for (i = 0; i < 3; i++) {
		if (entries[i] && !valid_filemode(entries[i]->mode)) {
			giterr_set(GITERR_INDEX, "invalid filemode for stage %d entry",
1755
				i + 1);
1756 1757 1758 1759
			return -1;
		}
	}

1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
	/* Remove existing index entries for each path */
	for (i = 0; i < 3; i++) {
		if (entries[i] == NULL)
			continue;

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

			giterr_clear();
			ret = 0;
		}
	}

	/* Add the conflict entries */
Edward Thomson committed
1775 1776 1777 1778 1779
	for (i = 0; i < 3; i++) {
		if (entries[i] == NULL)
			continue;

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

1782
		if ((ret = index_insert(index, &entries[i], 1, true, true, false)) < 0)
Edward Thomson committed
1783
			goto on_error;
1784 1785

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

nulltoken committed
1788
	return 0;
Edward Thomson committed
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798

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

	return ret;
}

1799 1800 1801 1802 1803 1804
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
1805
{
1806 1807 1808 1809
	const git_index_entry *conflict_entry;
	const char *path = NULL;
	size_t count;
	int stage, len = 0;
Edward Thomson committed
1810

1811
	assert(ancestor_out && our_out && their_out && index);
1812

Edward Thomson committed
1813 1814 1815 1816
	*ancestor_out = NULL;
	*our_out = NULL;
	*their_out = NULL;

1817 1818
	for (count = git_index_entrycount(index); n < count; ++n) {
		conflict_entry = git_vector_get(&index->entries, n);
Edward Thomson committed
1819

1820
		if (path && index->entries_cmp_path(conflict_entry->path, path) != 0)
Edward Thomson committed
1821 1822
			break;

1823
		stage = GIT_IDXENTRY_STAGE(conflict_entry);
1824
		path = conflict_entry->path;
1825

Edward Thomson committed
1826 1827 1828
		switch (stage) {
		case 3:
			*their_out = conflict_entry;
1829
			len++;
Edward Thomson committed
1830 1831 1832
			break;
		case 2:
			*our_out = conflict_entry;
1833
			len++;
Edward Thomson committed
1834 1835 1836
			break;
		case 1:
			*ancestor_out = conflict_entry;
1837
			len++;
Edward Thomson committed
1838 1839 1840 1841 1842 1843
			break;
		default:
			break;
		};
	}

1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
	return len;
}

int git_index_conflict_get(
	const git_index_entry **ancestor_out,
	const git_index_entry **our_out,
	const git_index_entry **their_out,
	git_index *index,
	const char *path)
{
	size_t pos;
	int len = 0;

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

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

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

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

	return 0;
Edward Thomson committed
1873 1874
}

1875
static int index_conflict_remove(git_index *index, const char *path)
Edward Thomson committed
1876
{
1877
	size_t pos = 0;
Edward Thomson committed
1878
	git_index_entry *conflict_entry;
1879
	int error = 0;
Edward Thomson committed
1880

1881
	if (path != NULL && git_index_find(&pos, index, path) < 0)
1882
		return GIT_ENOTFOUND;
Edward Thomson committed
1883

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

1886 1887
		if (path != NULL &&
			index->entries_cmp_path(conflict_entry->path, path) != 0)
Edward Thomson committed
1888 1889
			break;

1890
		if (GIT_IDXENTRY_STAGE(conflict_entry) == 0) {
Edward Thomson committed
1891 1892 1893 1894
			pos++;
			continue;
		}

1895
		if ((error = index_remove_entry(index, pos)) < 0)
1896
			break;
Edward Thomson committed
1897 1898
	}

1899
	return error;
Edward Thomson committed
1900 1901
}

1902
int git_index_conflict_remove(git_index *index, const char *path)
Edward Thomson committed
1903
{
1904 1905
	assert(index && path);
	return index_conflict_remove(index, path);
Edward Thomson committed
1906 1907
}

1908
int git_index_conflict_cleanup(git_index *index)
Edward Thomson committed
1909 1910
{
	assert(index);
1911
	return index_conflict_remove(index, NULL);
Edward Thomson committed
1912 1913
}

1914
int git_index_has_conflicts(const git_index *index)
1915
{
1916
	size_t i;
1917 1918 1919 1920 1921
	git_index_entry *entry;

	assert(index);

	git_vector_foreach(&index->entries, i, entry) {
1922
		if (GIT_IDXENTRY_STAGE(entry) > 0)
1923 1924 1925 1926 1927 1928
			return 1;
	}

	return 0;
}

1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
int git_index_conflict_iterator_new(
	git_index_conflict_iterator **iterator_out,
	git_index *index)
{
	git_index_conflict_iterator *it = NULL;

	assert(iterator_out && index);

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

	it->index = index;

	*iterator_out = it;
	return 0;
}

int git_index_conflict_next(
	const git_index_entry **ancestor_out,
	const git_index_entry **our_out,
	const git_index_entry **their_out,
	git_index_conflict_iterator *iterator)
{
	const git_index_entry *entry;
	int len;

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

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

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

1964
		if (git_index_entry_is_conflict(entry)) {
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
			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);
}

1991
size_t git_index_name_entrycount(git_index *index)
Edward Thomson committed
1992 1993
{
	assert(index);
1994
	return index->names.length;
Edward Thomson committed
1995 1996 1997 1998 1999 2000
}

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

Edward Thomson committed
2002 2003 2004 2005
	git_vector_sort(&index->names);
	return git_vector_get(&index->names, n);
}

2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
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
2016 2017 2018 2019 2020
int git_index_name_add(git_index *index,
	const char *ancestor, const char *ours, const char *theirs)
{
	git_index_name_entry *conflict_name;

2021
	assert((ancestor && ours) || (ancestor && theirs) || (ours && theirs));
Edward Thomson committed
2022 2023 2024

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

2026 2027 2028 2029 2030 2031 2032
	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
2033
	}
nulltoken committed
2034

2035
	return 0;
Edward Thomson committed
2036 2037 2038 2039 2040 2041 2042 2043
}

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

	assert(index);
nulltoken committed
2044

2045 2046
	git_vector_foreach(&index->names, i, conflict_name)
		index_name_entry_free(conflict_name);
nulltoken committed
2047

Edward Thomson committed
2048 2049 2050
	git_vector_clear(&index->names);
}

2051
size_t git_index_reuc_entrycount(git_index *index)
Edward Thomson committed
2052 2053
{
	assert(index);
2054
	return index->reuc.length;
Edward Thomson committed
2055 2056
}

2057 2058 2059 2060 2061 2062 2063
static int index_reuc_on_dup(void **old, void *new)
{
	index_entry_reuc_free(*old);
	*old = new;
	return GIT_EEXISTS;
}

Edward Thomson committed
2064 2065
static int index_reuc_insert(
	git_index *index,
2066
	git_index_reuc_entry *reuc)
Edward Thomson committed
2067
{
2068
	int res;
Edward Thomson committed
2069 2070

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

2073 2074
	res = git_vector_insert_sorted(&index->reuc, reuc, &index_reuc_on_dup);
	return res == GIT_EEXISTS ? 0 : res;
Edward Thomson committed
2075 2076 2077
}

int git_index_reuc_add(git_index *index, const char *path,
Edward Thomson committed
2078 2079 2080
	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
2081 2082 2083 2084 2085 2086
{
	git_index_reuc_entry *reuc = NULL;
	int error = 0;

	assert(index && path);

2087 2088
	if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode,
			ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 ||
2089
		(error = index_reuc_insert(index, reuc)) < 0)
Edward Thomson committed
2090 2091 2092
		index_entry_reuc_free(reuc);

	return error;
2093
}
Edward Thomson committed
2094

2095
int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path)
2096
{
2097
	return git_vector_bsearch2(at_pos, &index->reuc, index->reuc_search, path);
2098 2099
}

Edward Thomson committed
2100
const git_index_reuc_entry *git_index_reuc_get_bypath(
2101
	git_index *index, const char *path)
2102
{
2103
	size_t pos;
2104
	assert(index && path);
2105

Edward Thomson committed
2106
	if (!index->reuc.length)
2107
		return NULL;
2108

2109
	assert(git_vector_is_sorted(&index->reuc));
Edward Thomson committed
2110

2111
	if (git_index_reuc_find(&pos, index, path) < 0)
2112
		return NULL;
2113

Edward Thomson committed
2114
	return git_vector_get(&index->reuc, pos);
2115 2116
}

Edward Thomson committed
2117
const git_index_reuc_entry *git_index_reuc_get_byindex(
2118
	git_index *index, size_t n)
2119 2120
{
	assert(index);
2121
	assert(git_vector_is_sorted(&index->reuc));
Edward Thomson committed
2122 2123 2124 2125

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

Ben Straub committed
2126
int git_index_reuc_remove(git_index *index, size_t position)
Edward Thomson committed
2127 2128 2129 2130
{
	int error;
	git_index_reuc_entry *reuc;

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

	reuc = git_vector_get(&index->reuc, position);
2134
	error = git_vector_remove(&index->reuc, position);
Edward Thomson committed
2135 2136 2137 2138 2139

	if (!error)
		index_entry_reuc_free(reuc);

	return error;
2140 2141
}

Edward Thomson committed
2142 2143 2144 2145 2146 2147
void git_index_reuc_clear(git_index *index)
{
	size_t i;

	assert(index);

2148 2149
	for (i = 0; i < index->reuc.length; ++i)
		index_entry_reuc_free(git__swap(index->reuc.contents[i], NULL));
Edward Thomson committed
2150 2151 2152 2153

	git_vector_clear(&index->reuc);
}

2154 2155 2156 2157 2158 2159
static int index_error_invalid(const char *message)
{
	giterr_set(GITERR_INDEX, "Invalid data in index - %s", message);
	return -1;
}

Edward Thomson committed
2160
static int read_reuc(git_index *index, const char *buffer, size_t size)
2161
{
2162 2163
	const char *endptr;
	size_t len;
2164 2165
	int i;

2166 2167 2168
	/* 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)
2169
		return -1;
2170 2171

	while (size) {
Edward Thomson committed
2172
		git_index_reuc_entry *lost;
2173

2174
		len = p_strnlen(buffer, size) + 1;
2175
		if (size <= len)
Edward Thomson committed
2176
			return index_error_invalid("reading reuc entries");
2177

2178
		lost = reuc_entry_alloc(buffer);
2179
		GITERR_CHECK_ALLOC(lost);
2180 2181 2182 2183

		size -= len;
		buffer += len;

2184
		/* read 3 ASCII octal numbers for stage entries */
2185
		for (i = 0; i < 3; i++) {
2186
			int64_t tmp;
2187

2188
			if (git__strtol64(&tmp, buffer, &endptr, 8) < 0 ||
2189
				!endptr || endptr == buffer || *endptr ||
2190
				tmp < 0 || tmp > UINT32_MAX) {
2191
				index_entry_reuc_free(lost);
Edward Thomson committed
2192
				return index_error_invalid("reading reuc entry stage");
2193
			}
2194

2195
			lost->mode[i] = (uint32_t)tmp;
2196

2197
			len = (endptr + 1) - buffer;
2198 2199
			if (size <= len) {
				index_entry_reuc_free(lost);
Edward Thomson committed
2200
				return index_error_invalid("reading reuc entry stage");
2201
			}
2202

2203 2204 2205 2206
			size -= len;
			buffer += len;
		}

2207
		/* read up to 3 OIDs for stage entries */
2208 2209 2210
		for (i = 0; i < 3; i++) {
			if (!lost->mode[i])
				continue;
2211 2212
			if (size < 20) {
				index_entry_reuc_free(lost);
Edward Thomson committed
2213
				return index_error_invalid("reading reuc entry oid");
2214
			}
2215

2216
			git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer);
2217 2218 2219
			size -= 20;
			buffer += 20;
		}
2220 2221 2222 2223

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

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

2229
	return 0;
2230 2231
}

Edward Thomson committed
2232 2233 2234 2235

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

Edward Thomson committed
2237 2238 2239 2240 2241 2242
	/* 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) \
2243
	len = p_strnlen(buffer, size) + 1; \
2244 2245 2246 2247
	if (size < len) { \
		index_error_invalid("reading conflict name entries"); \
		goto out_err; \
	} \
Edward Thomson committed
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
	if (len == 1) \
		ptr = NULL; \
	else { \
		ptr = git__malloc(len); \
		GITERR_CHECK_ALLOC(ptr); \
		memcpy(ptr, buffer, len); \
	} \
	\
	buffer += len; \
	size -= len;
nulltoken committed
2258

Edward Thomson committed
2259 2260 2261 2262 2263 2264 2265
	while (size) {
		git_index_name_entry *conflict_name = git__calloc(1, sizeof(git_index_name_entry));
		GITERR_CHECK_ALLOC(conflict_name);

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

Edward Thomson committed
2267
		if (git_vector_insert(&index->names, conflict_name) < 0)
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
			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
2278 2279 2280
	}

#undef read_conflict_name
nulltoken committed
2281

Edward Thomson committed
2282
	/* entries are guaranteed to be sorted on-disk */
2283
	git_vector_set_sorted(&index->names, true);
nulltoken committed
2284 2285

	return 0;
Edward Thomson committed
2286 2287
}

2288
static size_t read_entry(
2289 2290 2291
	git_index_entry **out,
	git_index *index,
	const void *buffer,
David Turner committed
2292 2293
	size_t buffer_size,
	const char **last)
2294 2295 2296
{
	size_t path_length, entry_size;
	const char *path_ptr;
2297
	struct entry_short source;
2298
	git_index_entry entry = {{0}};
David Turner committed
2299 2300
	bool compressed = index->version >= INDEX_VERSION_NUMBER_COMP;
	char *tmp_path = NULL;
2301 2302 2303 2304

	if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size)
		return 0;

2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
	/* 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);
2320 2321

	if (entry.flags & GIT_IDXENTRY_EXTENDED) {
2322 2323
		uint16_t flags_raw;
		size_t flags_offset;
2324

2325 2326 2327 2328 2329 2330 2331
		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);
2332
	} else
2333
		path_ptr = (const char *) buffer + offsetof(struct entry_short, path);
2334

David Turner committed
2335 2336
	if (!compressed) {
		path_length = entry.flags & GIT_IDXENTRY_NAMEMASK;
2337

David Turner committed
2338 2339 2340 2341
		/* if this is a very long string, we must find its
		 * real length without overflowing */
		if (path_length == 0xFFF) {
			const char *path_end;
2342

David Turner committed
2343 2344 2345
			path_end = memchr(path_ptr, '\0', buffer_size);
			if (path_end == NULL)
				return 0;
2346

David Turner committed
2347 2348
			path_length = path_end - path_ptr;
		}
2349

David Turner committed
2350 2351 2352 2353
		if (entry.flags & GIT_IDXENTRY_EXTENDED)
			entry_size = long_entry_size(path_length);
		else
			entry_size = short_entry_size(path_length);
2354

David Turner committed
2355 2356
		if (INDEX_FOOTER_SIZE + entry_size > buffer_size)
			return 0;
2357

David Turner committed
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
		entry.path = (char *)path_ptr;
	} else {
		size_t varint_len;
		size_t shared = git_decode_varint((const unsigned char *)path_ptr, 
						  &varint_len);
		size_t len = strlen(path_ptr + varint_len);
		size_t last_len = strlen(*last);
		size_t tmp_path_len;

		if (varint_len == 0)
			return index_error_invalid("incorrect prefix length");

		GITERR_CHECK_ALLOC_ADD(&tmp_path_len, shared, len + 1);
		tmp_path = git__malloc(tmp_path_len);
		GITERR_CHECK_ALLOC(tmp_path);
		memcpy(tmp_path, last, last_len);
		memcpy(tmp_path + last_len, path_ptr + varint_len, len);
		entry_size = long_entry_size(shared + len);
		entry.path = tmp_path;
	}

	if (index_entry_dup(out, index, &entry) < 0) {
		git__free(tmp_path);
2381
		return 0;
David Turner committed
2382
	}
2383

David Turner committed
2384
	git__free(tmp_path);
2385 2386 2387 2388 2389
	return entry_size;
}

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

2392 2393
	dest->signature = ntohl(source->signature);
	if (dest->signature != INDEX_HEADER_SIG)
2394
		return index_error_invalid("incorrect header signature");
2395 2396

	dest->version = ntohl(source->version);
David Turner committed
2397 2398
	if (dest->version < INDEX_VERSION_NUMBER_LB ||
		dest->version > INDEX_VERSION_NUMBER_UB)
2399
		return index_error_invalid("incorrect header version");
2400 2401

	dest->entry_count = ntohl(source->entry_count);
2402
	return 0;
2403 2404 2405 2406 2407 2408 2409
}

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

2410 2411 2412
	/* buffer is not guaranteed to be aligned */
	memcpy(&dest, buffer, sizeof(struct index_extension));
	dest.extension_size = ntohl(dest.extension_size);
2413 2414 2415

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

2416 2417
	if (dest.extension_size > total_size ||
		buffer_size < total_size ||
2418
		buffer_size - total_size < INDEX_FOOTER_SIZE)
2419 2420 2421 2422 2423 2424
		return 0;

	/* optional extension */
	if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
		/* tree cache */
		if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {
2425
			if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, &index->tree_pool) < 0)
2426
				return 0;
2427
		} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
Edward Thomson committed
2428
			if (read_reuc(index, buffer + 8, dest.extension_size) < 0)
2429
				return 0;
Edward Thomson committed
2430 2431 2432
		} else if (memcmp(dest.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4) == 0) {
			if (read_conflict_names(index, buffer + 8, dest.extension_size) < 0)
				return 0;
2433
		}
2434 2435
		/* else, unsupported extension. We cannot parse this, but we can skip
		 * it by returning `total_size */
2436 2437 2438 2439 2440 2441 2442 2443 2444
	} else {
		/* we cannot handle non-ignorable extensions;
		 * in fact they aren't even defined in the standard */
		return 0;
	}

	return total_size;
}

2445
static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
2446
{
2447
	int error = 0;
2448
	unsigned int i;
2449
	struct index_header header = { 0 };
2450
	git_oid checksum_calculated, checksum_expected;
David Turner committed
2451 2452
	const char **last = NULL;
	const char *empty = "";
2453 2454

#define seek_forward(_increase) { \
2455 2456 2457
	if (_increase >= buffer_size) { \
		error = index_error_invalid("ran out of data while parsing"); \
		goto done; } \
2458 2459 2460 2461 2462
	buffer += _increase; \
	buffer_size -= _increase;\
}

	if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
2463
		return index_error_invalid("insufficient buffer space");
2464 2465 2466

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

	/* Parse header */
2470 2471
	if ((error = read_header(&header, buffer)) < 0)
		return error;
2472

David Turner committed
2473 2474 2475 2476
	index->version = header.version;
	if (index->version >= INDEX_VERSION_NUMBER_COMP)
		last = &empty;

2477 2478
	seek_forward(INDEX_HEADER_SIZE);

2479
	assert(!index->entries.length);
2480

2481 2482 2483 2484 2485
	if (index->ignore_case)
		kh_resize(idxicase, (khash_t(idxicase) *) index->entries_map, header.entry_count);
	else
		kh_resize(idx, index->entries_map, header.entry_count);

2486
	/* Parse all the entries */
2487 2488
	for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
		git_index_entry *entry;
David Turner committed
2489
		size_t entry_size = read_entry(&entry, index, buffer, buffer_size, last);
2490 2491

		/* 0 bytes read means an object corruption */
2492 2493 2494 2495
		if (entry_size == 0) {
			error = index_error_invalid("invalid entry");
			goto done;
		}
2496

2497 2498
		if ((error = git_vector_insert(&index->entries, entry)) < 0) {
			index_entry_free(entry);
2499
			goto done;
2500
		}
2501

2502
		INSERT_IN_MAP(index, entry, error);
2503 2504 2505 2506 2507

		if (error < 0) {
			index_entry_free(entry);
			goto done;
		}
2508
		error = 0;
2509

2510 2511 2512
		seek_forward(entry_size);
	}

2513 2514 2515 2516
	if (i != header.entry_count) {
		error = index_error_invalid("header entries changed while parsing");
		goto done;
	}
2517

2518 2519 2520 2521 2522 2523 2524
	/* There's still space for some extensions! */
	while (buffer_size > INDEX_FOOTER_SIZE) {
		size_t extension_size;

		extension_size = read_extension(index, buffer, buffer_size);

		/* see if we have read any bytes from the extension */
2525 2526 2527 2528
		if (extension_size == 0) {
			error = index_error_invalid("extension is truncated");
			goto done;
		}
2529 2530 2531 2532

		seek_forward(extension_size);
	}

2533 2534 2535 2536 2537
	if (buffer_size != INDEX_FOOTER_SIZE) {
		error = index_error_invalid(
			"buffer size does not match index footer size");
		goto done;
	}
2538 2539

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

2542 2543 2544 2545 2546
	if (git_oid__cmp(&checksum_calculated, &checksum_expected) != 0) {
		error = index_error_invalid(
			"calculated checksum does not match expected");
		goto done;
	}
2547

2548 2549
	git_oid_cpy(&index->checksum, &checksum_calculated);

2550 2551
#undef seek_forward

2552 2553 2554 2555
	/* 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);
2556
	git_vector_sort(&index->entries);
2557

2558 2559
done:
	return error;
2560 2561
}

2562
static bool is_index_extended(git_index *index)
Vicent Marti committed
2563
{
2564
	size_t i, extended;
2565
	git_index_entry *entry;
Vicent Marti committed
2566 2567 2568

	extended = 0;

2569
	git_vector_foreach(&index->entries, i, entry) {
Vicent Marti committed
2570 2571 2572 2573 2574 2575
		entry->flags &= ~GIT_IDXENTRY_EXTENDED;
		if (entry->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS) {
			extended++;
			entry->flags |= GIT_IDXENTRY_EXTENDED;
		}
	}
2576

2577
	return (extended > 0);
Vicent Marti committed
2578 2579
}

David Turner committed
2580
static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char **last)
2581
{
2582
	void *mem = NULL;
2583
	struct entry_short *ondisk;
2584
	size_t path_len, disk_size;
2585
	char *path;
David Turner committed
2586 2587
	const char *path_start = entry->path;
	size_t same_len = 0;
2588

2589
	path_len = ((struct entry_internal *)entry)->pathlen;
2590

David Turner committed
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
	if (last) {
		const char *last_c = *last;

		while (*path_start == *last_c) {
			if (!*path_start || !*last_c)
				break;
			++path_start;
			++last_c;
			++same_len;
		}
		path_len -= same_len;
		*last = entry->path;
	}

2605
	if (entry->flags & GIT_IDXENTRY_EXTENDED)
2606
		disk_size = long_entry_size(path_len);
2607
	else
2608
		disk_size = short_entry_size(path_len);
2609

2610 2611
	if (git_filebuf_reserve(file, &mem, disk_size) < 0)
		return -1;
2612

2613 2614
	ondisk = (struct entry_short *)mem;

2615
	memset(ondisk, 0x0, disk_size);
2616

2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628
	/**
	 * 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
	 */
	ondisk->ctime.seconds = htonl((uint32_t)entry->ctime.seconds);
	ondisk->mtime.seconds = htonl((uint32_t)entry->mtime.seconds);
2629 2630
	ondisk->ctime.nanoseconds = htonl(entry->ctime.nanoseconds);
	ondisk->mtime.nanoseconds = htonl(entry->mtime.nanoseconds);
Vicent Marti committed
2631 2632
	ondisk->dev = htonl(entry->dev);
	ondisk->ino = htonl(entry->ino);
2633
	ondisk->mode = htonl(entry->mode);
Vicent Marti committed
2634 2635
	ondisk->uid = htonl(entry->uid);
	ondisk->gid = htonl(entry->gid);
2636
	ondisk->file_size = htonl((uint32_t)entry->file_size);
2637

2638
	git_oid_cpy(&ondisk->oid, &entry->id);
2639 2640 2641 2642 2643 2644

	ondisk->flags = htons(entry->flags);

	if (entry->flags & GIT_IDXENTRY_EXTENDED) {
		struct entry_long *ondisk_ext;
		ondisk_ext = (struct entry_long *)ondisk;
2645 2646
		ondisk_ext->flags_extended = htons(entry->flags_extended &
			GIT_IDXENTRY_EXTENDED_FLAGS);
2647 2648 2649 2650 2651
		path = ondisk_ext->path;
	}
	else
		path = ondisk->path;

David Turner committed
2652 2653 2654 2655 2656 2657
	if (last) {
		path += git_encode_varint((unsigned char *) path,
					  disk_size,
					  path_len - same_len);
	}
	memcpy(path, path_start, path_len);
2658

2659
	return 0;
2660 2661
}

2662
static int write_entries(git_index *index, git_filebuf *file)
2663
{
2664
	int error = 0;
2665
	size_t i;
2666
	git_vector case_sorted, *entries;
2667
	git_index_entry *entry;
David Turner committed
2668 2669
	const char **last = NULL;
	const char *empty = "";
2670 2671 2672 2673

	/* If index->entries is sorted case-insensitively, then we need
	 * to re-sort it case-sensitively before writing */
	if (index->ignore_case) {
2674
		git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp);
2675
		git_vector_sort(&case_sorted);
2676 2677 2678
		entries = &case_sorted;
	} else {
		entries = &index->entries;
2679 2680
	}

David Turner committed
2681 2682 2683
	if (index->version >= INDEX_VERSION_NUMBER_COMP)
		last = &empty;

2684
	git_vector_foreach(entries, i, entry)
David Turner committed
2685
		if ((error = write_disk_entry(file, entry, last)) < 0)
2686 2687 2688 2689 2690 2691
			break;

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

	return error;
2692 2693
}

Edward Thomson committed
2694 2695 2696 2697 2698 2699 2700 2701
static int write_extension(git_filebuf *file, struct index_extension *header, git_buf *data)
{
	struct index_extension ondisk;

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

2702 2703
	git_filebuf_write(file, &ondisk, sizeof(struct index_extension));
	return git_filebuf_write(file, data->ptr, data->size);
Edward Thomson committed
2704 2705
}

Edward Thomson committed
2706 2707 2708 2709 2710 2711 2712 2713
static int create_name_extension_data(git_buf *name_buf, git_index_name_entry *conflict_name)
{
	int error = 0;

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

Edward Thomson committed
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
	if (error != 0)
		goto on_error;

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

	if (error != 0)
		goto on_error;

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

on_error:
	return error;
}

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

Edward Thomson committed
2744 2745 2746 2747
	git_vector_foreach(out, i, conflict_name) {
		if ((error = create_name_extension_data(&name_buf, conflict_name)) < 0)
			goto done;
	}
nulltoken committed
2748

Edward Thomson committed
2749 2750 2751
	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
2752

Edward Thomson committed
2753
	error = write_extension(file, &extension, &name_buf);
nulltoken committed
2754

Edward Thomson committed
2755
	git_buf_free(&name_buf);
nulltoken committed
2756

Edward Thomson committed
2757 2758 2759 2760
done:
	return error;
}

Edward Thomson committed
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
static int create_reuc_extension_data(git_buf *reuc_buf, git_index_reuc_entry *reuc)
{
	int i;
	int error = 0;

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

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

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

	return 0;
}

static int write_reuc_extension(git_index *index, git_filebuf *file)
{
	git_buf reuc_buf = GIT_BUF_INIT;
	git_vector *out = &index->reuc;
	git_index_reuc_entry *reuc;
	struct index_extension extension;
2789
	size_t i;
Edward Thomson committed
2790 2791 2792 2793 2794 2795 2796 2797 2798
	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);
2799
	extension.extension_size = (uint32_t)reuc_buf.size;
Edward Thomson committed
2800 2801 2802 2803 2804 2805 2806 2807 2808

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

	git_buf_free(&reuc_buf);

done:
	return error;
}

2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
static int write_tree_extension(git_index *index, git_filebuf *file)
{
	struct index_extension extension;
	git_buf buf = GIT_BUF_INIT;
	int error;

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

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

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

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

	git_buf_free(&buf);

	return error;
}

2832 2833 2834 2835 2836 2837 2838 2839 2840
static void clear_uptodate(git_index *index)
{
	git_index_entry *entry;
	size_t i;

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

2841
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file)
2842 2843 2844
{
	git_oid hash_final;
	struct index_header header;
2845
	bool is_extended;
Ben Straub committed
2846
	uint32_t index_version_number;
2847

2848
	assert(index && file);
2849

David Turner committed
2850 2851 2852 2853 2854 2855
	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
2856

2857
	header.signature = htonl(INDEX_HEADER_SIG);
Ben Straub committed
2858
	header.version = htonl(index_version_number);
2859
	header.entry_count = htonl((uint32_t)index->entries.length);
2860

2861 2862
	if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0)
		return -1;
2863

2864 2865
	if (write_entries(index, file) < 0)
		return -1;
2866

2867 2868 2869
	/* write the tree cache extension */
	if (index->tree != NULL && write_tree_extension(index, file) < 0)
		return -1;
Edward Thomson committed
2870

Edward Thomson committed
2871 2872 2873
	/* write the rename conflict extension */
	if (index->names.length > 0 && write_name_extension(index, file) < 0)
		return -1;
nulltoken committed
2874

Edward Thomson committed
2875 2876 2877
	/* write the reuc extension */
	if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0)
		return -1;
2878

2879 2880
	/* get out the hash for all the contents we've appended to the file */
	git_filebuf_hash(&hash_final, file);
2881
	git_oid_cpy(checksum, &hash_final);
2882 2883

	/* write it at the end of the file */
2884 2885 2886 2887 2888 2889 2890
	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;
2891
}
2892 2893 2894

int git_index_entry_stage(const git_index_entry *entry)
{
2895
	return GIT_IDXENTRY_STAGE(entry);
2896
}
2897

2898 2899 2900 2901 2902
int git_index_entry_is_conflict(const git_index_entry *entry)
{
	return (GIT_IDXENTRY_STAGE(entry) > 0);
}

2903
typedef struct read_tree_data {
2904
	git_index *index;
2905
	git_vector *old_entries;
2906
	git_vector *new_entries;
2907
	git_vector_cmp entry_cmp;
2908
	git_tree_cache *tree;
2909 2910
} read_tree_data;

2911 2912
static int read_tree_cb(
	const char *root, const git_tree_entry *tentry, void *payload)
2913
{
2914 2915
	read_tree_data *data = payload;
	git_index_entry *entry = NULL, *old_entry;
2916
	git_buf path = GIT_BUF_INIT;
2917
	size_t pos;
2918

Vicent Martí committed
2919
	if (git_tree_entry__is_tree(tentry))
2920
		return 0;
2921

2922 2923
	if (git_buf_joinpath(&path, root, tentry->filename) < 0)
		return -1;
2924

2925
	if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, false) < 0)
2926
		return -1;
2927 2928

	entry->mode = tentry->attr;
2929
	git_oid_cpy(&entry->id, git_tree_entry_id(tentry));
2930

2931
	/* look for corresponding old entry and copy data to new entry */
2932
	if (data->old_entries != NULL &&
2933
		!index_find_in_entries(
2934 2935 2936 2937 2938
			&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))
	{
2939
		index_entry_cpy(entry, old_entry);
2940
		entry->flags_extended = 0;
2941 2942
	}

2943
	index_entry_adjust_namemask(entry, path.size);
2944 2945
	git_buf_free(&path);

2946
	if (git_vector_insert(data->new_entries, entry) < 0) {
2947
		index_entry_free(entry);
2948 2949 2950 2951
		return -1;
	}

	return 0;
2952 2953
}

Ben Straub committed
2954
int git_index_read_tree(git_index *index, const git_tree *tree)
2955
{
2956 2957
	int error = 0;
	git_vector entries = GIT_VECTOR_INIT;
2958
	git_idxmap *entries_map;
2959
	read_tree_data data;
2960 2961 2962 2963 2964
	size_t i;
	git_index_entry *e;

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

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

2968
	data.index = index;
2969 2970
	data.old_entries = &index->entries;
	data.new_entries = &entries;
2971
	data.entry_cmp   = index->entries_search;
2972

2973 2974 2975
	index->tree = NULL;
	git_pool_clear(&index->tree_pool);

2976
	git_vector_sort(&index->entries);
2977

2978 2979
	if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0)
		goto cleanup;
2980

2981 2982 2983 2984
	if (index->ignore_case)
		kh_resize(idxicase, (khash_t(idxicase) *) entries_map, entries.length);
	else
		kh_resize(idx, entries_map, entries.length);
2985

2986
	git_vector_foreach(&entries, i, e) {
2987
		INSERT_IN_MAP_EX(index, entries_map, e, error);
2988 2989 2990 2991

		if (error < 0) {
			giterr_set(GITERR_INDEX, "failed to insert entry into map");
			return error;
2992
		}
2993 2994
	}

2995 2996 2997 2998
	error = 0;

	git_vector_sort(&entries);

2999
	if ((error = git_index_clear(index)) < 0) {
3000 3001 3002 3003 3004 3005 3006
		/* well, this isn't good */;
	} else {
		git_vector_swap(&entries, &index->entries);
		entries_map = git__swap(index->entries_map, entries_map);
	}

cleanup:
3007
	git_vector_free(&entries);
3008
	git_idxmap_free(entries_map);
3009 3010 3011 3012
	if (error < 0)
		return error;

	error = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool);
3013 3014

	return error;
3015
}
3016

3017
static int git_index_read_iterator(
3018
	git_index *index,
3019 3020
	git_iterator *new_iterator,
	size_t new_length_hint)
3021 3022 3023
{
	git_vector new_entries = GIT_VECTOR_INIT,
		remove_entries = GIT_VECTOR_INIT;
3024
	git_idxmap *new_entries_map = NULL;
3025
	git_iterator *index_iterator = NULL;
3026
	git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
3027 3028 3029 3030 3031
	const git_index_entry *old_entry, *new_entry;
	git_index_entry *entry;
	size_t i;
	int error;

3032 3033 3034
	assert((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE));

	if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 ||
3035 3036
		(error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 ||
		(error = git_idxmap_alloc(&new_entries_map)) < 0)
3037 3038
		goto done;

3039 3040 3041 3042
	if (index->ignore_case && new_length_hint)
		kh_resize(idxicase, (khash_t(idxicase) *) new_entries_map, new_length_hint);
	else if (new_length_hint)
		kh_resize(idx, new_entries_map, new_length_hint);
3043

3044 3045
	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
		GIT_ITERATOR_INCLUDE_CONFLICTS;
3046

3047 3048 3049
	if ((error = git_iterator_for_index(&index_iterator,
			git_index_owner(index), index, &opts)) < 0 ||
		((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
3050
			error != GIT_ITEROVER) ||
3051
		((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
3052 3053 3054 3055
			error != GIT_ITEROVER))
		goto done;

	while (true) {
3056 3057 3058 3059
		git_index_entry
			*dup_entry = NULL,
			*add_entry = NULL,
			*remove_entry = NULL;
3060 3061
		int diff;

3062 3063
		error = 0;

3064 3065 3066 3067 3068 3069 3070 3071 3072 3073
		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) {
3074
			remove_entry = (git_index_entry *)old_entry;
3075
		} else if (diff > 0) {
3076
			dup_entry = (git_index_entry *)new_entry;
3077 3078 3079 3080
		} else {
			/* Path and stage are equal, if the OID is equal, keep it to
			 * keep the stat cache data.
			 */
3081 3082
			if (git_oid_equal(&old_entry->id, &new_entry->id) &&
				old_entry->mode == new_entry->mode) {
3083
				add_entry = (git_index_entry *)old_entry;
3084
			} else {
3085
				dup_entry = (git_index_entry *)new_entry;
3086
				remove_entry = (git_index_entry *)old_entry;
3087 3088 3089
			}
		}

3090 3091 3092
		if (dup_entry) {
			if ((error = index_entry_dup_nocache(&add_entry, index, dup_entry)) < 0)
				goto done;
3093 3094 3095

			index_entry_adjust_namemask(add_entry,
				((struct entry_internal *)add_entry)->pathlen);
3096 3097
		}

3098 3099 3100 3101 3102 3103
		/* 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);

3104 3105 3106 3107 3108
		if (add_entry) {
			if ((error = git_vector_insert(&new_entries, add_entry)) == 0)
				INSERT_IN_MAP_EX(index, new_entries_map, add_entry, error);
		}

3109
		if (remove_entry && error >= 0)
3110 3111 3112 3113
			error = git_vector_insert(&remove_entries, remove_entry);

		if (error < 0) {
			giterr_set(GITERR_INDEX, "failed to insert entry");
3114
			goto done;
3115 3116
		}

3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133
		if (diff <= 0) {
			if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 &&
				error != GIT_ITEROVER)
				goto done;
		}

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

	git_index_name_clear(index);
	git_index_reuc_clear(index);

	git_vector_swap(&new_entries, &index->entries);
3134
	new_entries_map = git__swap(index->entries_map, new_entries_map);
3135 3136 3137 3138 3139 3140 3141 3142

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

		index_entry_free(entry);
	}

3143 3144
	clear_uptodate(index);

3145 3146 3147
	error = 0;

done:
3148
	git_idxmap_free(new_entries_map);
3149 3150 3151
	git_vector_free(&new_entries);
	git_vector_free(&remove_entries);
	git_iterator_free(index_iterator);
3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162
	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;

3163 3164
	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
		GIT_ITERATOR_INCLUDE_CONFLICTS;
3165 3166

	if ((error = git_iterator_for_index(&new_iterator,
3167 3168 3169
		git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 ||
		(error = git_index_read_iterator(index, new_iterator,
		new_index->entries.length)) < 0)
3170 3171 3172
		goto done;

done:
3173 3174 3175 3176
	git_iterator_free(new_iterator);
	return error;
}

3177 3178 3179 3180
git_repository *git_index_owner(const git_index *index)
{
	return INDEX_OWNER(index);
}
3181

3182 3183 3184 3185 3186 3187 3188
enum {
	INDEX_ACTION_NONE = 0,
	INDEX_ACTION_UPDATE = 1,
	INDEX_ACTION_REMOVE = 2,
	INDEX_ACTION_ADDALL = 3,
};

3189 3190 3191 3192 3193 3194 3195 3196 3197 3198
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;
3199
	git_pathspec ps;
3200 3201 3202 3203 3204 3205 3206 3207
	bool no_fnmatch = (flags & GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH) != 0;

	assert(index);

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

3208
	if ((error = git_pathspec__init(&ps, paths)) < 0)
3209 3210 3211 3212 3213 3214 3215 3216 3217
		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;

3218
	error = index_apply_to_wd_diff(index, INDEX_ACTION_ADDALL, paths, flags, cb, payload);
3219

3220 3221
	if (error)
		giterr_set_after_callback(error);
3222 3223 3224

cleanup:
	git_iterator_free(wditer);
3225
	git_pathspec__clear(&ps);
3226 3227 3228 3229

	return error;
}

3230 3231 3232
struct foreach_diff_data {
	git_index *index;
	const git_pathspec *pathspec;
3233
	unsigned int flags;
3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261
	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;

3262 3263
	/* If the workdir item does not exist, remove it from the index. */
	if ((delta->new_file.flags & GIT_DIFF_FLAG_EXISTS) == 0)
3264 3265
		error = git_index_remove_bypath(data->index, path);
	else
3266
		error = git_index_add_bypath(data->index, delta->new_file.path);
3267 3268 3269 3270 3271

	return error;
}

static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
3272
				  unsigned int flags,
3273 3274 3275 3276 3277 3278
				  git_index_matched_path_cb cb, void *payload)
{
	int error;
	git_diff *diff;
	git_pathspec ps;
	git_repository *repo;
3279
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
3280 3281 3282
	struct foreach_diff_data data = {
		index,
		NULL,
3283
		flags,
3284 3285 3286 3287 3288
		cb,
		payload,
	};

	assert(index);
3289
	assert(action == INDEX_ACTION_UPDATE || action == INDEX_ACTION_ADDALL);
3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305

	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;

3306
	opts.flags = GIT_DIFF_INCLUDE_TYPECHANGE;
3307
	if (action == INDEX_ACTION_ADDALL) {
3308
		opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
3309
			GIT_DIFF_RECURSE_UNTRACKED_DIRS;
3310

3311 3312 3313 3314 3315
		if (flags == GIT_INDEX_ADD_FORCE)
			opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
	}

	if ((error = git_diff_index_to_workdir(&diff, repo, index, &opts)) < 0)
3316 3317 3318
		goto cleanup;

	data.pathspec = &ps;
3319
	error = git_diff_foreach(diff, apply_each_file, NULL, NULL, NULL, &data);
3320 3321 3322 3323 3324 3325 3326 3327 3328 3329
	git_diff_free(diff);

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

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

3330 3331 3332 3333 3334 3335 3336 3337 3338
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;
3339
	git_pathspec ps;
3340
	const char *match;
3341
	git_buf path = GIT_BUF_INIT;
3342 3343 3344

	assert(index);

3345
	if ((error = git_pathspec__init(&ps, paths)) < 0)
3346 3347 3348 3349 3350 3351 3352 3353
		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 */
3354
		if (!git_pathspec__match(
Linquize committed
3355
				&ps.pathspec, entry->path, false, (bool)index->ignore_case,
3356
				&match, NULL))
3357 3358 3359 3360 3361 3362 3363 3364
			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;
			}
3365
			if (error < 0)   /* return < 0 means abort */
3366 3367 3368
				break;
		}

3369 3370 3371 3372
		/* index manipulation may alter entry, so don't depend on it */
		if ((error = git_buf_sets(&path, entry->path)) < 0)
			break;

3373 3374 3375 3376
		switch (action) {
		case INDEX_ACTION_NONE:
			break;
		case INDEX_ACTION_UPDATE:
3377
			error = git_index_add_bypath(index, path.ptr);
3378 3379 3380 3381

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

3382
				error = git_index_remove_bypath(index, path.ptr);
3383 3384 3385 3386 3387 3388

				if (!error) /* back up foreach if we removed this */
					i--;
			}
			break;
		case INDEX_ACTION_REMOVE:
3389
			if (!(error = git_index_remove_bypath(index, path.ptr)))
3390 3391 3392 3393 3394 3395 3396 3397 3398
				i--; /* back up foreach if we removed this */
			break;
		default:
			giterr_set(GITERR_INVALID, "Unknown index action %d", action);
			error = -1;
			break;
		}
	}

3399
	git_buf_free(&path);
3400
	git_pathspec__clear(&ps);
3401 3402 3403 3404 3405 3406 3407 3408 3409 3410

	return error;
}

int git_index_remove_all(
	git_index *index,
	const git_strarray *pathspec,
	git_index_matched_path_cb cb,
	void *payload)
{
3411
	int error = index_apply_to_all(
3412
		index, INDEX_ACTION_REMOVE, pathspec, cb, payload);
3413 3414 3415 3416 3417

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

	return error;
3418 3419 3420 3421 3422 3423 3424 3425
}

int git_index_update_all(
	git_index *index,
	const git_strarray *pathspec,
	git_index_matched_path_cb cb,
	void *payload)
{
3426
	int error = index_apply_to_wd_diff(index, INDEX_ACTION_UPDATE, pathspec, 0, cb, payload);
3427 3428 3429 3430
	if (error) /* make sure error is set if callback stopped iteration */
		giterr_set_after_callback(error);

	return error;
3431
}
3432

3433
int git_index_snapshot_new(git_vector *snap, git_index *index)
3434 3435 3436 3437
{
	int error;

	GIT_REFCOUNT_INC(index);
3438 3439

	git_atomic_inc(&index->readers);
3440
	git_vector_sort(&index->entries);
3441

3442
	error = git_vector_dup(snap, &index->entries, index->entries._cmp);
3443 3444 3445 3446 3447 3448 3449

	if (error < 0)
		git_index_free(index);

	return error;
}

3450
void git_index_snapshot_release(git_vector *snap, git_index *index)
3451
{
3452 3453
	git_vector_free(snap);

3454 3455
	git_atomic_dec(&index->readers);

3456
	git_index_free(index);
3457
}
3458 3459 3460 3461 3462 3463 3464

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);
}
3465 3466 3467 3468 3469 3470 3471

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

3472 3473
	GIT_REFCOUNT_INC(index);

3474 3475 3476 3477 3478 3479 3480 3481
	writer->index = index;

	if (!index->index_file_path)
		return create_index_error(-1,
			"Failed to write index: The index is in-memory only");

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

3483 3484 3485 3486 3487 3488
		if (error == GIT_ELOCKED)
			giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrent or crashed process");

		return error;
	}

3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508
	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;

3509 3510 3511 3512 3513 3514
	return 0;
}

int git_indexwriter_commit(git_indexwriter *writer)
{
	int error;
3515
	git_oid checksum = {{ 0 }};
3516

3517 3518 3519
	if (!writer->should_write)
		return 0;

3520
	git_vector_sort(&writer->index->entries);
3521 3522
	git_vector_sort(&writer->index->reuc);

3523
	if ((error = write_index(&checksum, writer->index, &writer->file)) < 0) {
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537
		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) {
		giterr_set(GITERR_OS, "Could not read index timestamp");
		return -1;
	}

	writer->index->on_disk = 1;
3538
	git_oid_cpy(&writer->index->checksum, &checksum);
3539

3540 3541 3542
	git_index_free(writer->index);
	writer->index = NULL;

3543 3544 3545 3546 3547 3548
	return 0;
}

void git_indexwriter_cleanup(git_indexwriter *writer)
{
	git_filebuf_cleanup(&writer->file);
3549 3550 3551

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