Blame src/index.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include <stddef.h>
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "index.h"
Packit Service 20376f
#include "tree.h"
Packit Service 20376f
#include "tree-cache.h"
Packit Service 20376f
#include "hash.h"
Packit Service 20376f
#include "iterator.h"
Packit Service 20376f
#include "pathspec.h"
Packit Service 20376f
#include "ignore.h"
Packit Service 20376f
#include "blob.h"
Packit Service 20376f
#include "idxmap.h"
Packit Service 20376f
#include "diff.h"
Packit Service 20376f
#include "varint.h"
Packit Service 20376f
Packit Service 20376f
#include "git2/odb.h"
Packit Service 20376f
#include "git2/oid.h"
Packit Service 20376f
#include "git2/blob.h"
Packit Service 20376f
#include "git2/config.h"
Packit Service 20376f
#include "git2/sys/index.h"
Packit Service 20376f
Packit Service 20376f
#define INSERT_IN_MAP_EX(idx, map, e, err) do {				\
Packit Service 20376f
		if ((idx)->ignore_case)					\
Packit Service 20376f
			git_idxmap_icase_insert((khash_t(idxicase) *) (map), (e), (e), (err)); \
Packit Service 20376f
		else							\
Packit Service 20376f
			git_idxmap_insert((map), (e), (e), (err));	\
Packit Service 20376f
	} while (0)
Packit Service 20376f
Packit Service 20376f
#define INSERT_IN_MAP(idx, e, err) INSERT_IN_MAP_EX(idx, (idx)->entries_map, e, err)
Packit Service 20376f
Packit Service 20376f
#define LOOKUP_IN_MAP(p, idx, k) do {					\
Packit Service 20376f
		if ((idx)->ignore_case)					\
Packit Service 20376f
			(p) = git_idxmap_icase_lookup_index((khash_t(idxicase) *) index->entries_map, (k)); \
Packit Service 20376f
		else							\
Packit Service 20376f
			(p) = git_idxmap_lookup_index(index->entries_map, (k)); \
Packit Service 20376f
	} while (0)
Packit Service 20376f
Packit Service 20376f
#define DELETE_IN_MAP(idx, e) do {					\
Packit Service 20376f
		if ((idx)->ignore_case)					\
Packit Service 20376f
			git_idxmap_icase_delete((khash_t(idxicase) *) (idx)->entries_map, (e)); \
Packit Service 20376f
		else							\
Packit Service 20376f
			git_idxmap_delete((idx)->entries_map, (e));	\
Packit Service 20376f
	} while (0)
Packit Service 20376f
Packit Service 20376f
static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
Packit Service 20376f
				  unsigned int flags,
Packit Service 20376f
				  git_index_matched_path_cb cb, void *payload);
Packit Service 20376f
Packit Service 20376f
#define minimal_entry_size (offsetof(struct entry_short, path))
Packit Service 20376f
Packit Service 20376f
static const size_t INDEX_FOOTER_SIZE = GIT_OID_RAWSZ;
Packit Service 20376f
static const size_t INDEX_HEADER_SIZE = 12;
Packit Service 20376f
Packit Service 20376f
static const unsigned int INDEX_VERSION_NUMBER_DEFAULT = 2;
Packit Service 20376f
static const unsigned int INDEX_VERSION_NUMBER_LB = 2;
Packit Service 20376f
static const unsigned int INDEX_VERSION_NUMBER_EXT = 3;
Packit Service 20376f
static const unsigned int INDEX_VERSION_NUMBER_COMP = 4;
Packit Service 20376f
static const unsigned int INDEX_VERSION_NUMBER_UB = 4;
Packit Service 20376f
Packit Service 20376f
static const unsigned int INDEX_HEADER_SIG = 0x44495243;
Packit Service 20376f
static const char INDEX_EXT_TREECACHE_SIG[] = {'T', 'R', 'E', 'E'};
Packit Service 20376f
static const char INDEX_EXT_UNMERGED_SIG[] = {'R', 'E', 'U', 'C'};
Packit Service 20376f
static const char INDEX_EXT_CONFLICT_NAME_SIG[] = {'N', 'A', 'M', 'E'};
Packit Service 20376f
Packit Service 20376f
#define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx)))
Packit Service 20376f
Packit Service 20376f
struct index_header {
Packit Service 20376f
	uint32_t signature;
Packit Service 20376f
	uint32_t version;
Packit Service 20376f
	uint32_t entry_count;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct index_extension {
Packit Service 20376f
	char signature[4];
Packit Service 20376f
	uint32_t extension_size;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct entry_time {
Packit Service 20376f
	uint32_t seconds;
Packit Service 20376f
	uint32_t nanoseconds;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct entry_short {
Packit Service 20376f
	struct entry_time ctime;
Packit Service 20376f
	struct entry_time mtime;
Packit Service 20376f
	uint32_t dev;
Packit Service 20376f
	uint32_t ino;
Packit Service 20376f
	uint32_t mode;
Packit Service 20376f
	uint32_t uid;
Packit Service 20376f
	uint32_t gid;
Packit Service 20376f
	uint32_t file_size;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	uint16_t flags;
Packit Service 20376f
	char path[1]; /* arbitrary length */
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct entry_long {
Packit Service 20376f
	struct entry_time ctime;
Packit Service 20376f
	struct entry_time mtime;
Packit Service 20376f
	uint32_t dev;
Packit Service 20376f
	uint32_t ino;
Packit Service 20376f
	uint32_t mode;
Packit Service 20376f
	uint32_t uid;
Packit Service 20376f
	uint32_t gid;
Packit Service 20376f
	uint32_t file_size;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	uint16_t flags;
Packit Service 20376f
	uint16_t flags_extended;
Packit Service 20376f
	char path[1]; /* arbitrary length */
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct entry_srch_key {
Packit Service 20376f
	const char *path;
Packit Service 20376f
	size_t pathlen;
Packit Service 20376f
	int stage;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct entry_internal {
Packit Service 20376f
	git_index_entry entry;
Packit Service 20376f
	size_t pathlen;
Packit Service 20376f
	char path[GIT_FLEX_ARRAY];
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct reuc_entry_internal {
Packit Service 20376f
	git_index_reuc_entry entry;
Packit Service 20376f
	size_t pathlen;
Packit Service 20376f
	char path[GIT_FLEX_ARRAY];
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/* local declarations */
Packit Service 20376f
static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size);
Packit Service 20376f
static int read_header(struct index_header *dest, const void *buffer);
Packit Service 20376f
Packit Service 20376f
static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
Packit Service 20376f
static bool is_index_extended(git_index *index);
Packit Service 20376f
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file);
Packit Service 20376f
Packit Service 20376f
static void index_entry_free(git_index_entry *entry);
Packit Service 20376f
static void index_entry_reuc_free(git_index_reuc_entry *reuc);
Packit Service 20376f
Packit Service 20376f
int git_index_entry_srch(const void *key, const void *array_member)
Packit Service 20376f
{
Packit Service 20376f
	const struct entry_srch_key *srch_key = key;
Packit Service 20376f
	const struct entry_internal *entry = array_member;
Packit Service 20376f
	int cmp;
Packit Service 20376f
	size_t len1, len2, len;
Packit Service 20376f
Packit Service 20376f
	len1 = srch_key->pathlen;
Packit Service 20376f
	len2 = entry->pathlen;
Packit Service 20376f
	len = len1 < len2 ? len1 : len2;
Packit Service 20376f
Packit Service 20376f
	cmp = memcmp(srch_key->path, entry->path, len);
Packit Service 20376f
	if (cmp)
Packit Service 20376f
		return cmp;
Packit Service 20376f
	if (len1 < len2)
Packit Service 20376f
		return -1;
Packit Service 20376f
	if (len1 > len2)
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
Packit Service 20376f
		return srch_key->stage - GIT_IDXENTRY_STAGE(&entry->entry);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_entry_isrch(const void *key, const void *array_member)
Packit Service 20376f
{
Packit Service 20376f
	const struct entry_srch_key *srch_key = key;
Packit Service 20376f
	const struct entry_internal *entry = array_member;
Packit Service 20376f
	int cmp;
Packit Service 20376f
	size_t len1, len2, len;
Packit Service 20376f
Packit Service 20376f
	len1 = srch_key->pathlen;
Packit Service 20376f
	len2 = entry->pathlen;
Packit Service 20376f
	len = len1 < len2 ? len1 : len2;
Packit Service 20376f
Packit Service 20376f
	cmp = strncasecmp(srch_key->path, entry->path, len);
Packit Service 20376f
Packit Service 20376f
	if (cmp)
Packit Service 20376f
		return cmp;
Packit Service 20376f
	if (len1 < len2)
Packit Service 20376f
		return -1;
Packit Service 20376f
	if (len1 > len2)
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	if (srch_key->stage != GIT_INDEX_STAGE_ANY)
Packit Service 20376f
		return srch_key->stage - GIT_IDXENTRY_STAGE(&entry->entry);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_entry_srch_path(const void *path, const void *array_member)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *entry = array_member;
Packit Service 20376f
Packit Service 20376f
	return strcmp((const char *)path, entry->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_entry_isrch_path(const void *path, const void *array_member)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *entry = array_member;
Packit Service 20376f
Packit Service 20376f
	return strcasecmp((const char *)path, entry->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_entry_cmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	int diff;
Packit Service 20376f
	const git_index_entry *entry_a = a;
Packit Service 20376f
	const git_index_entry *entry_b = b;
Packit Service 20376f
Packit Service 20376f
	diff = strcmp(entry_a->path, entry_b->path);
Packit Service 20376f
Packit Service 20376f
	if (diff == 0)
Packit Service 20376f
		diff = (GIT_IDXENTRY_STAGE(entry_a) - GIT_IDXENTRY_STAGE(entry_b));
Packit Service 20376f
Packit Service 20376f
	return diff;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_entry_icmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	int diff;
Packit Service 20376f
	const git_index_entry *entry_a = a;
Packit Service 20376f
	const git_index_entry *entry_b = b;
Packit Service 20376f
Packit Service 20376f
	diff = strcasecmp(entry_a->path, entry_b->path);
Packit Service 20376f
Packit Service 20376f
	if (diff == 0)
Packit Service 20376f
		diff = (GIT_IDXENTRY_STAGE(entry_a) - GIT_IDXENTRY_STAGE(entry_b));
Packit Service 20376f
Packit Service 20376f
	return diff;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int conflict_name_cmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_name_entry *name_a = a;
Packit Service 20376f
	const git_index_name_entry *name_b = b;
Packit Service 20376f
Packit Service 20376f
	if (name_a->ancestor && !name_b->ancestor)
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	if (!name_a->ancestor && name_b->ancestor)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (name_a->ancestor)
Packit Service 20376f
		return strcmp(name_a->ancestor, name_b->ancestor);
Packit Service 20376f
Packit Service 20376f
	if (!name_a->ours || !name_b->ours)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	return strcmp(name_a->ours, name_b->ours);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * TODO: enable this when resolving case insensitive conflicts
Packit Service 20376f
 */
Packit Service 20376f
#if 0
Packit Service 20376f
static int conflict_name_icmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_name_entry *name_a = a;
Packit Service 20376f
	const git_index_name_entry *name_b = b;
Packit Service 20376f
Packit Service 20376f
	if (name_a->ancestor && !name_b->ancestor)
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	if (!name_a->ancestor && name_b->ancestor)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (name_a->ancestor)
Packit Service 20376f
		return strcasecmp(name_a->ancestor, name_b->ancestor);
Packit Service 20376f
Packit Service 20376f
	if (!name_a->ours || !name_b->ours)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	return strcasecmp(name_a->ours, name_b->ours);
Packit Service 20376f
}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
static int reuc_srch(const void *key, const void *array_member)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_reuc_entry *reuc = array_member;
Packit Service 20376f
Packit Service 20376f
	return strcmp(key, reuc->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int reuc_isrch(const void *key, const void *array_member)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_reuc_entry *reuc = array_member;
Packit Service 20376f
Packit Service 20376f
	return strcasecmp(key, reuc->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int reuc_cmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_reuc_entry *info_a = a;
Packit Service 20376f
	const git_index_reuc_entry *info_b = b;
Packit Service 20376f
Packit Service 20376f
	return strcmp(info_a->path, info_b->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int reuc_icmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_reuc_entry *info_a = a;
Packit Service 20376f
	const git_index_reuc_entry *info_b = b;
Packit Service 20376f
Packit Service 20376f
	return strcasecmp(info_a->path, info_b->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void index_entry_reuc_free(git_index_reuc_entry *reuc)
Packit Service 20376f
{
Packit Service 20376f
	git__free(reuc);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void index_entry_free(git_index_entry *entry)
Packit Service 20376f
{
Packit Service 20376f
	if (!entry)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	memset(&entry->id, 0, sizeof(entry->id));
Packit Service 20376f
	git__free(entry);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
unsigned int git_index__create_mode(unsigned int mode)
Packit Service 20376f
{
Packit Service 20376f
	if (S_ISLNK(mode))
Packit Service 20376f
		return S_IFLNK;
Packit Service 20376f
Packit Service 20376f
	if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR))
Packit Service 20376f
		return (S_IFLNK | S_IFDIR);
Packit Service 20376f
Packit Service 20376f
	return S_IFREG | GIT_PERMS_CANONICAL(mode);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static unsigned int index_merge_mode(
Packit Service 20376f
	git_index *index, git_index_entry *existing, unsigned int mode)
Packit Service 20376f
{
Packit Service 20376f
	if (index->no_symlinks && S_ISREG(mode) &&
Packit Service 20376f
		existing && S_ISLNK(existing->mode))
Packit Service 20376f
		return existing->mode;
Packit Service 20376f
Packit Service 20376f
	if (index->distrust_filemode && S_ISREG(mode))
Packit Service 20376f
		return (existing && S_ISREG(existing->mode)) ?
Packit Service 20376f
			existing->mode : git_index__create_mode(0666);
Packit Service 20376f
Packit Service 20376f
	return git_index__create_mode(mode);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) index_find_in_entries(
Packit Service 20376f
	size_t *out, git_vector *entries, git_vector_cmp entry_srch,
Packit Service 20376f
	const char *path, size_t path_len, int stage)
Packit Service 20376f
{
Packit Service 20376f
	struct entry_srch_key srch_key;
Packit Service 20376f
	srch_key.path = path;
Packit Service 20376f
	srch_key.pathlen = !path_len ? strlen(path) : path_len;
Packit Service 20376f
	srch_key.stage = stage;
Packit Service 20376f
	return git_vector_bsearch2(out, entries, entry_srch, &srch_key);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) index_find(
Packit Service 20376f
	size_t *out, git_index *index,
Packit Service 20376f
	const char *path, size_t path_len, int stage)
Packit Service 20376f
{
Packit Service 20376f
	git_vector_sort(&index->entries);
Packit Service 20376f
Packit Service 20376f
	return index_find_in_entries(
Packit Service 20376f
		out, &index->entries, index->entries_search, path, path_len, stage);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_index__set_ignore_case(git_index *index, bool ignore_case)
Packit Service 20376f
{
Packit Service 20376f
	index->ignore_case = ignore_case;
Packit Service 20376f
Packit Service 20376f
	if (ignore_case) {
Packit Service 20376f
		index->entries_cmp_path    = git__strcasecmp_cb;
Packit Service 20376f
		index->entries_search      = git_index_entry_isrch;
Packit Service 20376f
		index->entries_search_path = index_entry_isrch_path;
Packit Service 20376f
		index->reuc_search         = reuc_isrch;
Packit Service 20376f
	} else {
Packit Service 20376f
		index->entries_cmp_path    = git__strcmp_cb;
Packit Service 20376f
		index->entries_search      = git_index_entry_srch;
Packit Service 20376f
		index->entries_search_path = index_entry_srch_path;
Packit Service 20376f
		index->reuc_search         = reuc_srch;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_set_cmp(&index->entries,
Packit Service 20376f
		ignore_case ? git_index_entry_icmp : git_index_entry_cmp);
Packit Service 20376f
	git_vector_sort(&index->entries);
Packit Service 20376f
Packit Service 20376f
	git_vector_set_cmp(&index->reuc, ignore_case ? reuc_icmp : reuc_cmp);
Packit Service 20376f
	git_vector_sort(&index->reuc);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_open(git_index **index_out, const char *index_path)
Packit Service 20376f
{
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	int error = -1;
Packit Service 20376f
Packit Service 20376f
	assert(index_out);
Packit Service 20376f
Packit Service 20376f
	index = git__calloc(1, sizeof(git_index));
Packit Service 20376f
	GITERR_CHECK_ALLOC(index);
Packit Service 20376f
Packit Service 20376f
	git_pool_init(&index->tree_pool, 1);
Packit Service 20376f
Packit Service 20376f
	if (index_path != NULL) {
Packit Service 20376f
		index->index_file_path = git__strdup(index_path);
Packit Service 20376f
		if (!index->index_file_path)
Packit Service 20376f
			goto fail;
Packit Service 20376f
Packit Service 20376f
		/* Check if index file is stored on disk already */
Packit Service 20376f
		if (git_path_exists(index->index_file_path) == true)
Packit Service 20376f
			index->on_disk = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (git_vector_init(&index->entries, 32, git_index_entry_cmp) < 0 ||
Packit Service 20376f
		git_idxmap_alloc(&index->entries_map) < 0 ||
Packit Service 20376f
		git_vector_init(&index->names, 8, conflict_name_cmp) < 0 ||
Packit Service 20376f
		git_vector_init(&index->reuc, 8, reuc_cmp) < 0 ||
Packit Service 20376f
		git_vector_init(&index->deleted, 8, git_index_entry_cmp) < 0)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	index->entries_cmp_path = git__strcmp_cb;
Packit Service 20376f
	index->entries_search = git_index_entry_srch;
Packit Service 20376f
	index->entries_search_path = index_entry_srch_path;
Packit Service 20376f
	index->reuc_search = reuc_srch;
Packit Service 20376f
	index->version = INDEX_VERSION_NUMBER_DEFAULT;
Packit Service 20376f
Packit Service 20376f
	if (index_path != NULL && (error = git_index_read(index, true)) < 0)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	*index_out = index;
Packit Service 20376f
	GIT_REFCOUNT_INC(index);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
fail:
Packit Service 20376f
	git_pool_clear(&index->tree_pool);
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_new(git_index **out)
Packit Service 20376f
{
Packit Service 20376f
	return git_index_open(out, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void index_free(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	/* index iterators increment the refcount of the index, so if we
Packit Service 20376f
	 * get here then there should be no outstanding iterators.
Packit Service 20376f
	 */
Packit Service 20376f
	assert(!git_atomic_get(&index->readers));
Packit Service 20376f
Packit Service 20376f
	git_index_clear(index);
Packit Service 20376f
	git_idxmap_free(index->entries_map);
Packit Service 20376f
	git_vector_free(&index->entries);
Packit Service 20376f
	git_vector_free(&index->names);
Packit Service 20376f
	git_vector_free(&index->reuc);
Packit Service 20376f
	git_vector_free(&index->deleted);
Packit Service 20376f
Packit Service 20376f
	git__free(index->index_file_path);
Packit Service 20376f
Packit Service 20376f
	git__memzero(index, sizeof(*index));
Packit Service 20376f
	git__free(index);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_index_free(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	if (index == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_DEC(index, index_free);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* call with locked index */
Packit Service 20376f
static void index_free_deleted(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	int readers = (int)git_atomic_get(&index->readers);
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	if (readers > 0 || !index->deleted.length)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < index->deleted.length; ++i) {
Packit Service 20376f
		git_index_entry *ie = git__swap(index->deleted.contents[i], NULL);
Packit Service 20376f
		index_entry_free(ie);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_clear(&index->deleted);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* call with locked index */
Packit Service 20376f
static int index_remove_entry(git_index *index, size_t pos)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_index_entry *entry = git_vector_get(&index->entries, pos);
Packit Service 20376f
Packit Service 20376f
	if (entry != NULL) {
Packit Service 20376f
		git_tree_cache_invalidate_path(index->tree, entry->path);
Packit Service 20376f
		DELETE_IN_MAP(index, entry);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git_vector_remove(&index->entries, pos);
Packit Service 20376f
Packit Service 20376f
	if (!error) {
Packit Service 20376f
		if (git_atomic_get(&index->readers) > 0) {
Packit Service 20376f
			error = git_vector_insert(&index->deleted, entry);
Packit Service 20376f
		} else {
Packit Service 20376f
			index_entry_free(entry);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_clear(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	index->tree = NULL;
Packit Service 20376f
	git_pool_clear(&index->tree_pool);
Packit Service 20376f
Packit Service 20376f
	git_idxmap_clear(index->entries_map);
Packit Service 20376f
	while (!error && index->entries.length > 0)
Packit Service 20376f
		error = index_remove_entry(index, index->entries.length - 1);
Packit Service 20376f
	index_free_deleted(index);
Packit Service 20376f
Packit Service 20376f
	git_index_reuc_clear(index);
Packit Service 20376f
	git_index_name_clear(index);
Packit Service 20376f
Packit Service 20376f
	git_futils_filestamp_set(&index->stamp, NULL);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int create_index_error(int error, const char *msg)
Packit Service 20376f
{
Packit Service 20376f
	giterr_set_str(GITERR_INDEX, msg);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_set_caps(git_index *index, int caps)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int old_ignore_case;
Packit Service 20376f
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	old_ignore_case = index->ignore_case;
Packit Service 20376f
Packit Service 20376f
	if (caps == GIT_INDEXCAP_FROM_OWNER) {
Packit Service 20376f
		git_repository *repo = INDEX_OWNER(index);
Packit Service 20376f
		int val;
Packit Service 20376f
Packit Service 20376f
		if (!repo)
Packit Service 20376f
			return create_index_error(
Packit Service 20376f
				-1, "cannot access repository to set index caps");
Packit Service 20376f
Packit Service 20376f
		if (!git_repository__cvar(&val, repo, GIT_CVAR_IGNORECASE))
Packit Service 20376f
			index->ignore_case = (val != 0);
Packit Service 20376f
		if (!git_repository__cvar(&val, repo, GIT_CVAR_FILEMODE))
Packit Service 20376f
			index->distrust_filemode = (val == 0);
Packit Service 20376f
		if (!git_repository__cvar(&val, repo, GIT_CVAR_SYMLINKS))
Packit Service 20376f
			index->no_symlinks = (val == 0);
Packit Service 20376f
	}
Packit Service 20376f
	else {
Packit Service 20376f
		index->ignore_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0);
Packit Service 20376f
		index->distrust_filemode = ((caps & GIT_INDEXCAP_NO_FILEMODE) != 0);
Packit Service 20376f
		index->no_symlinks = ((caps & GIT_INDEXCAP_NO_SYMLINKS) != 0);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (old_ignore_case != index->ignore_case) {
Packit Service 20376f
		git_index__set_ignore_case(index, (bool)index->ignore_case);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_caps(const git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	return ((index->ignore_case ? GIT_INDEXCAP_IGNORE_CASE : 0) |
Packit Service 20376f
			(index->distrust_filemode ? GIT_INDEXCAP_NO_FILEMODE : 0) |
Packit Service 20376f
			(index->no_symlinks ? GIT_INDEXCAP_NO_SYMLINKS : 0));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const git_oid *git_index_checksum(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	return &index->checksum;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Returns 1 for changed, 0 for not changed and <0 for errors
Packit Service 20376f
 */
Packit Service 20376f
static int compare_checksum(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	int fd;
Packit Service 20376f
	ssize_t bytes_read;
Packit Service 20376f
	git_oid checksum = {{ 0 }};
Packit Service 20376f
Packit Service 20376f
	if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0)
Packit Service 20376f
		return fd;
Packit Service 20376f
Packit Service 20376f
	if (p_lseek(fd, -20, SEEK_END) < 0) {
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to seek to end of file");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	bytes_read = p_read(fd, &checksum, GIT_OID_RAWSZ);
Packit Service 20376f
	p_close(fd);
Packit Service 20376f
Packit Service 20376f
	if (bytes_read < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return !!git_oid_cmp(&checksum, &index->checksum);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_read(git_index *index, int force)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0, updated;
Packit Service 20376f
	git_buf buffer = GIT_BUF_INIT;
Packit Service 20376f
	git_futils_filestamp stamp = index->stamp;
Packit Service 20376f
Packit Service 20376f
	if (!index->index_file_path)
Packit Service 20376f
		return create_index_error(-1,
Packit Service 20376f
			"failed to read index: The index is in-memory only");
Packit Service 20376f
Packit Service 20376f
	index->on_disk = git_path_exists(index->index_file_path);
Packit Service 20376f
Packit Service 20376f
	if (!index->on_disk) {
Packit Service 20376f
		if (force)
Packit Service 20376f
			return git_index_clear(index);
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((updated = git_futils_filestamp_check(&stamp, index->index_file_path) < 0) ||
Packit Service 20376f
	    ((updated = compare_checksum(index)) < 0)) {
Packit Service 20376f
		giterr_set(
Packit Service 20376f
			GITERR_INDEX,
Packit Service 20376f
			"failed to read index: '%s' no longer exists",
Packit Service 20376f
			index->index_file_path);
Packit Service 20376f
		return updated;
Packit Service 20376f
	}
Packit Service 20376f
	if (!updated && !force)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	error = git_futils_readbuffer(&buffer, index->index_file_path);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	index->tree = NULL;
Packit Service 20376f
	git_pool_clear(&index->tree_pool);
Packit Service 20376f
Packit Service 20376f
	error = git_index_clear(index);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		error = parse_index(index, buffer.ptr, buffer.size);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		git_futils_filestamp_set(&index->stamp, &stamp);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buffer);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index__changed_relative_to(
Packit Service 20376f
	git_index *index, const git_oid *checksum)
Packit Service 20376f
{
Packit Service 20376f
	/* attempt to update index (ignoring errors) */
Packit Service 20376f
	if (git_index_read(index, false) < 0)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
Packit Service 20376f
	return !!git_oid_cmp(&index->checksum, checksum);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool is_racy_entry(git_index *index, const git_index_entry *entry)
Packit Service 20376f
{
Packit Service 20376f
	/* Git special-cases submodules in the check */
Packit Service 20376f
	if (S_ISGITLINK(entry->mode))
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return git_index_entry_newer_than_index(entry, index);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Force the next diff to take a look at those entries which have the
Packit Service 20376f
 * same timestamp as the current index.
Packit Service 20376f
 */
Packit Service 20376f
static int truncate_racily_clean(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int error;
Packit Service 20376f
	git_index_entry *entry;
Packit Service 20376f
	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
Packit Service 20376f
	git_diff *diff = NULL;
Packit Service 20376f
	git_vector paths = GIT_VECTOR_INIT;
Packit Service 20376f
	git_diff_delta *delta;
Packit Service 20376f
Packit Service 20376f
	/* Nothing to do if there's no repo to talk about */
Packit Service 20376f
	if (!INDEX_OWNER(index))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* If there's no workdir, we can't know where to even check */
Packit Service 20376f
	if (!git_repository_workdir(INDEX_OWNER(index)))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	diff_opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_IGNORE_SUBMODULES | GIT_DIFF_DISABLE_PATHSPEC_MATCH;
Packit Service 20376f
	git_vector_foreach(&index->entries, i, entry) {
Packit Service 20376f
		if ((entry->flags_extended & GIT_IDXENTRY_UPTODATE) == 0 &&
Packit Service 20376f
			is_racy_entry(index, entry))
Packit Service 20376f
			git_vector_insert(&paths, (char *)entry->path);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (paths.length == 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	diff_opts.pathspec.count = paths.length;
Packit Service 20376f
	diff_opts.pathspec.strings = (char **)paths.contents;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&diff->deltas, i, delta) {
Packit Service 20376f
		entry = (git_index_entry *)git_index_get_bypath(index, delta->old_file.path, 0);
Packit Service 20376f
Packit Service 20376f
		/* Ensure that we have a stage 0 for this file (ie, it's not a
Packit Service 20376f
		 * conflict), otherwise smudging it is quite pointless.
Packit Service 20376f
		 */
Packit Service 20376f
		if (entry)
Packit Service 20376f
			entry->file_size = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_diff_free(diff);
Packit Service 20376f
	git_vector_free(&paths);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
unsigned git_index_version(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	return index->version;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_set_version(git_index *index, unsigned int version)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	if (version < INDEX_VERSION_NUMBER_LB ||
Packit Service 20376f
	    version > INDEX_VERSION_NUMBER_UB) {
Packit Service 20376f
		giterr_set(GITERR_INDEX, "invalid version number");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	index->version = version;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_write(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	git_indexwriter writer = GIT_INDEXWRITER_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	truncate_racily_clean(index);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_indexwriter_init(&writer, index)) == 0)
Packit Service 20376f
		error = git_indexwriter_commit(&writer);
Packit Service 20376f
Packit Service 20376f
	git_indexwriter_cleanup(&writer);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char * git_index_path(const git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
	return index->index_file_path;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_write_tree(git_oid *oid, git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
Packit Service 20376f
	assert(oid && index);
Packit Service 20376f
Packit Service 20376f
	repo = INDEX_OWNER(index);
Packit Service 20376f
Packit Service 20376f
	if (repo == NULL)
Packit Service 20376f
		return create_index_error(-1, "Failed to write tree. "
Packit Service 20376f
		  "the index file is not backed up by an existing repository");
Packit Service 20376f
Packit Service 20376f
	return git_tree__write_index(oid, index, repo);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_write_tree_to(
Packit Service 20376f
	git_oid *oid, git_index *index, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	assert(oid && index && repo);
Packit Service 20376f
	return git_tree__write_index(oid, index, repo);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_index_entrycount(const git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
	return index->entries.length;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const git_index_entry *git_index_get_byindex(
Packit Service 20376f
	git_index *index, size_t n)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
	git_vector_sort(&index->entries);
Packit Service 20376f
	return git_vector_get(&index->entries, n);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const git_index_entry *git_index_get_bypath(
Packit Service 20376f
	git_index *index, const char *path, int stage)
Packit Service 20376f
{
Packit Service 20376f
	khiter_t pos;
Packit Service 20376f
	git_index_entry key = {{ 0 }};
Packit Service 20376f
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	key.path = path;
Packit Service 20376f
	GIT_IDXENTRY_STAGE_SET(&key, stage);
Packit Service 20376f
Packit Service 20376f
	LOOKUP_IN_MAP(pos, index, &key);
Packit Service 20376f
Packit Service 20376f
	if (git_idxmap_valid_index(index->entries_map, pos))
Packit Service 20376f
		return git_idxmap_value_at(index->entries_map, pos);
Packit Service 20376f
Packit Service 20376f
	giterr_set(GITERR_INDEX, "index does not contain '%s'", path);
Packit Service 20376f
	return NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_index_entry__init_from_stat(
Packit Service 20376f
	git_index_entry *entry, struct stat *st, bool trust_mode)
Packit Service 20376f
{
Packit Service 20376f
	entry->ctime.seconds = (int32_t)st->st_ctime;
Packit Service 20376f
	entry->mtime.seconds = (int32_t)st->st_mtime;
Packit Service 20376f
#if defined(GIT_USE_NSEC)
Packit Service 20376f
	entry->mtime.nanoseconds = st->st_mtime_nsec;
Packit Service 20376f
	entry->ctime.nanoseconds = st->st_ctime_nsec;
Packit Service 20376f
#endif
Packit Service 20376f
	entry->dev  = st->st_rdev;
Packit Service 20376f
	entry->ino  = st->st_ino;
Packit Service 20376f
	entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ?
Packit Service 20376f
		git_index__create_mode(0666) : git_index__create_mode(st->st_mode);
Packit Service 20376f
	entry->uid  = st->st_uid;
Packit Service 20376f
	entry->gid  = st->st_gid;
Packit Service 20376f
	entry->file_size = (uint32_t)st->st_size;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void index_entry_adjust_namemask(
Packit Service 20376f
		git_index_entry *entry,
Packit Service 20376f
		size_t path_length)
Packit Service 20376f
{
Packit Service 20376f
	entry->flags &= ~GIT_IDXENTRY_NAMEMASK;
Packit Service 20376f
Packit Service 20376f
	if (path_length < GIT_IDXENTRY_NAMEMASK)
Packit Service 20376f
		entry->flags |= path_length & GIT_IDXENTRY_NAMEMASK;
Packit Service 20376f
	else
Packit Service 20376f
		entry->flags |= GIT_IDXENTRY_NAMEMASK;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* When `from_workdir` is true, we will validate the paths to avoid placing
Packit Service 20376f
 * paths that are invalid for the working directory on the current filesystem
Packit Service 20376f
 * (eg, on Windows, we will disallow `GIT~1`, `AUX`, `COM1`, etc).  This
Packit Service 20376f
 * function will *always* prevent `.git` and directory traversal `../` from
Packit Service 20376f
 * being added to the index.
Packit Service 20376f
 */
Packit Service 20376f
static int index_entry_create(
Packit Service 20376f
	git_index_entry **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	struct stat *st,
Packit Service 20376f
	bool from_workdir)
Packit Service 20376f
{
Packit Service 20376f
	size_t pathlen = strlen(path), alloclen;
Packit Service 20376f
	struct entry_internal *entry;
Packit Service 20376f
	unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS;
Packit Service 20376f
	uint16_t mode = 0;
Packit Service 20376f
Packit Service 20376f
	/* always reject placing `.git` in the index and directory traversal.
Packit Service 20376f
	 * when requested, disallow platform-specific filenames and upgrade to
Packit Service 20376f
	 * the platform-specific `.git` tests (eg, `git~1`, etc).
Packit Service 20376f
	 */
Packit Service 20376f
	if (from_workdir)
Packit Service 20376f
		path_valid_flags |= GIT_PATH_REJECT_WORKDIR_DEFAULTS;
Packit Service 20376f
	if (st)
Packit Service 20376f
		mode = st->st_mode;
Packit Service 20376f
Packit Service 20376f
	if (!git_path_isvalid(repo, path, mode, path_valid_flags)) {
Packit Service 20376f
		giterr_set(GITERR_INDEX, "invalid path: '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(struct entry_internal), pathlen);
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
Packit Service 20376f
	entry = git__calloc(1, alloclen);
Packit Service 20376f
	GITERR_CHECK_ALLOC(entry);
Packit Service 20376f
Packit Service 20376f
	entry->pathlen = pathlen;
Packit Service 20376f
	memcpy(entry->path, path, pathlen);
Packit Service 20376f
	entry->entry.path = entry->path;
Packit Service 20376f
Packit Service 20376f
	*out = (git_index_entry *)entry;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_entry_init(
Packit Service 20376f
	git_index_entry **entry_out,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const char *rel_path)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_index_entry *entry = NULL;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
Packit Service 20376f
	if (INDEX_OWNER(index) == NULL)
Packit Service 20376f
		return create_index_error(-1,
Packit Service 20376f
			"could not initialize index entry. "
Packit Service 20376f
			"Index is not backed up by an existing repository.");
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * FIXME: this is duplicated with the work in
Packit Service 20376f
	 * git_blob__create_from_paths. It should accept an optional stat
Packit Service 20376f
	 * structure so we can pass in the one we have to do here.
Packit Service 20376f
	 */
Packit Service 20376f
	repo = INDEX_OWNER(index);
Packit Service 20376f
	if (git_repository__ensure_not_bare(repo, "create blob from file") < 0)
Packit Service 20376f
		return GIT_EBAREREPO;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&path, git_repository_workdir(repo), rel_path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	error = git_path_lstat(path.ptr, &st);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, &st, true) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* write the blob to disk and get the oid and stat info */
Packit Service 20376f
	error = git_blob__create_from_paths(
Packit Service 20376f
		&oid, &st, INDEX_OWNER(index), NULL, rel_path, 0, true);
Packit Service 20376f
Packit Service 20376f
	if (error < 0) {
Packit Service 20376f
		index_entry_free(entry);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	entry->id = oid;
Packit Service 20376f
	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
Packit Service 20376f
Packit Service 20376f
	*entry_out = (git_index_entry *)entry;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static git_index_reuc_entry *reuc_entry_alloc(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	size_t pathlen = strlen(path),
Packit Service 20376f
		structlen = sizeof(struct reuc_entry_internal),
Packit Service 20376f
		alloclen;
Packit Service 20376f
	struct reuc_entry_internal *entry;
Packit Service 20376f
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, structlen, pathlen) ||
Packit Service 20376f
		GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1))
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	entry = git__calloc(1, alloclen);
Packit Service 20376f
	if (!entry)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	entry->pathlen = pathlen;
Packit Service 20376f
	memcpy(entry->path, path, pathlen);
Packit Service 20376f
	entry->entry.path = entry->path;
Packit Service 20376f
Packit Service 20376f
	return (git_index_reuc_entry *)entry;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_entry_reuc_init(git_index_reuc_entry **reuc_out,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	int ancestor_mode, const git_oid *ancestor_oid,
Packit Service 20376f
	int our_mode, const git_oid *our_oid,
Packit Service 20376f
	int their_mode, const git_oid *their_oid)
Packit Service 20376f
{
Packit Service 20376f
	git_index_reuc_entry *reuc = NULL;
Packit Service 20376f
Packit Service 20376f
	assert(reuc_out && path);
Packit Service 20376f
Packit Service 20376f
	*reuc_out = reuc = reuc_entry_alloc(path);
Packit Service 20376f
	GITERR_CHECK_ALLOC(reuc);
Packit Service 20376f
Packit Service 20376f
	if ((reuc->mode[0] = ancestor_mode) > 0) {
Packit Service 20376f
		assert(ancestor_oid);
Packit Service 20376f
		git_oid_cpy(&reuc->oid[0], ancestor_oid);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((reuc->mode[1] = our_mode) > 0) {
Packit Service 20376f
		assert(our_oid);
Packit Service 20376f
		git_oid_cpy(&reuc->oid[1], our_oid);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((reuc->mode[2] = their_mode) > 0) {
Packit Service 20376f
		assert(their_oid);
Packit Service 20376f
		git_oid_cpy(&reuc->oid[2], their_oid);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void index_entry_cpy(
Packit Service 20376f
	git_index_entry *tgt,
Packit Service 20376f
	const git_index_entry *src)
Packit Service 20376f
{
Packit Service 20376f
	const char *tgt_path = tgt->path;
Packit Service 20376f
	memcpy(tgt, src, sizeof(*tgt));
Packit Service 20376f
	tgt->path = tgt_path;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_entry_dup(
Packit Service 20376f
	git_index_entry **out,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_index_entry *src)
Packit Service 20376f
{
Packit Service 20376f
	if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	index_entry_cpy(*out, src);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void index_entry_cpy_nocache(
Packit Service 20376f
	git_index_entry *tgt,
Packit Service 20376f
	const git_index_entry *src)
Packit Service 20376f
{
Packit Service 20376f
	git_oid_cpy(&tgt->id, &src->id);
Packit Service 20376f
	tgt->mode = src->mode;
Packit Service 20376f
	tgt->flags = src->flags;
Packit Service 20376f
	tgt->flags_extended = (src->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_entry_dup_nocache(
Packit Service 20376f
	git_index_entry **out,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_index_entry *src)
Packit Service 20376f
{
Packit Service 20376f
	if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	index_entry_cpy_nocache(*out, src);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int has_file_name(git_index *index,
Packit Service 20376f
	 const git_index_entry *entry, size_t pos, int ok_to_replace)
Packit Service 20376f
{
Packit Service 20376f
	int retval = 0;
Packit Service 20376f
	size_t len = strlen(entry->path);
Packit Service 20376f
	int stage = GIT_IDXENTRY_STAGE(entry);
Packit Service 20376f
	const char *name = entry->path;
Packit Service 20376f
Packit Service 20376f
	while (pos < index->entries.length) {
Packit Service 20376f
		struct entry_internal *p = index->entries.contents[pos++];
Packit Service 20376f
Packit Service 20376f
		if (len >= p->pathlen)
Packit Service 20376f
			break;
Packit Service 20376f
		if (memcmp(name, p->path, len))
Packit Service 20376f
			break;
Packit Service 20376f
		if (GIT_IDXENTRY_STAGE(&p->entry) != stage)
Packit Service 20376f
			continue;
Packit Service 20376f
		if (p->path[len] != '/')
Packit Service 20376f
			continue;
Packit Service 20376f
		retval = -1;
Packit Service 20376f
		if (!ok_to_replace)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		if (index_remove_entry(index, --pos) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
	return retval;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Do we have another file with a pathname that is a proper
Packit Service 20376f
 * subset of the name we're trying to add?
Packit Service 20376f
 */
Packit Service 20376f
static int has_dir_name(git_index *index,
Packit Service 20376f
		const git_index_entry *entry, int ok_to_replace)
Packit Service 20376f
{
Packit Service 20376f
	int retval = 0;
Packit Service 20376f
	int stage = GIT_IDXENTRY_STAGE(entry);
Packit Service 20376f
	const char *name = entry->path;
Packit Service 20376f
	const char *slash = name + strlen(name);
Packit Service 20376f
Packit Service 20376f
	for (;;) {
Packit Service 20376f
		size_t len, pos;
Packit Service 20376f
Packit Service 20376f
		for (;;) {
Packit Service 20376f
			if (*--slash == '/')
Packit Service 20376f
				break;
Packit Service 20376f
			if (slash <= entry->path)
Packit Service 20376f
				return retval;
Packit Service 20376f
		}
Packit Service 20376f
		len = slash - name;
Packit Service 20376f
Packit Service 20376f
		if (!index_find(&pos, index, name, len, stage)) {
Packit Service 20376f
			retval = -1;
Packit Service 20376f
			if (!ok_to_replace)
Packit Service 20376f
				break;
Packit Service 20376f
Packit Service 20376f
			if (index_remove_entry(index, pos) < 0)
Packit Service 20376f
				break;
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/*
Packit Service 20376f
		 * Trivial optimization: if we find an entry that
Packit Service 20376f
		 * already matches the sub-directory, then we know
Packit Service 20376f
		 * we're ok, and we can exit.
Packit Service 20376f
		 */
Packit Service 20376f
		for (; pos < index->entries.length; ++pos) {
Packit Service 20376f
			struct entry_internal *p = index->entries.contents[pos];
Packit Service 20376f
Packit Service 20376f
			if (p->pathlen <= len ||
Packit Service 20376f
			    p->path[len] != '/' ||
Packit Service 20376f
			    memcmp(p->path, name, len))
Packit Service 20376f
				break; /* not our subdirectory */
Packit Service 20376f
Packit Service 20376f
			if (GIT_IDXENTRY_STAGE(&p->entry) == stage)
Packit Service 20376f
				return retval;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return retval;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int check_file_directory_collision(git_index *index,
Packit Service 20376f
		git_index_entry *entry, size_t pos, int ok_to_replace)
Packit Service 20376f
{
Packit Service 20376f
	int retval = has_file_name(index, entry, pos, ok_to_replace);
Packit Service 20376f
	retval = retval + has_dir_name(index, entry, ok_to_replace);
Packit Service 20376f
Packit Service 20376f
	if (retval) {
Packit Service 20376f
		giterr_set(GITERR_INDEX,
Packit Service 20376f
			"'%s' appears as both a file and a directory", entry->path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int canonicalize_directory_path(
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	git_index_entry *entry,
Packit Service 20376f
	git_index_entry *existing)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *match, *best = NULL;
Packit Service 20376f
	char *search, *sep;
Packit Service 20376f
	size_t pos, search_len, best_len;
Packit Service 20376f
Packit Service 20376f
	if (!index->ignore_case)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* item already exists in the index, simply re-use the existing case */
Packit Service 20376f
	if (existing) {
Packit Service 20376f
		memcpy((char *)entry->path, existing->path, strlen(existing->path));
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* nothing to do */
Packit Service 20376f
	if (strchr(entry->path, '/') == NULL)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if ((search = git__strdup(entry->path)) == NULL)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* starting at the parent directory and descending to the root, find the
Packit Service 20376f
	 * common parent directory.
Packit Service 20376f
	 */
Packit Service 20376f
	while (!best && (sep = strrchr(search, '/'))) {
Packit Service 20376f
		sep[1] = '\0';
Packit Service 20376f
Packit Service 20376f
		search_len = strlen(search);
Packit Service 20376f
Packit Service 20376f
		git_vector_bsearch2(
Packit Service 20376f
			&pos, &index->entries, index->entries_search_path, search);
Packit Service 20376f
Packit Service 20376f
		while ((match = git_vector_get(&index->entries, pos))) {
Packit Service 20376f
			if (GIT_IDXENTRY_STAGE(match) != 0) {
Packit Service 20376f
				/* conflicts do not contribute to canonical paths */
Packit Service 20376f
			} else if (strncmp(search, match->path, search_len) == 0) {
Packit Service 20376f
				/* prefer an exact match to the input filename */
Packit Service 20376f
				best = match;
Packit Service 20376f
				best_len = search_len;
Packit Service 20376f
				break;
Packit Service 20376f
			} else if (strncasecmp(search, match->path, search_len) == 0) {
Packit Service 20376f
				/* continue walking, there may be a path with an exact
Packit Service 20376f
				 * (case sensitive) match later in the index, but use this
Packit Service 20376f
				 * as the best match until that happens.
Packit Service 20376f
				 */
Packit Service 20376f
				if (!best) {
Packit Service 20376f
					best = match;
Packit Service 20376f
					best_len = search_len;
Packit Service 20376f
				}
Packit Service 20376f
			} else {
Packit Service 20376f
				break;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			pos++;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		sep[0] = '\0';
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (best)
Packit Service 20376f
		memcpy((char *)entry->path, best->path, best_len);
Packit Service 20376f
Packit Service 20376f
	git__free(search);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_no_dups(void **old, void *new)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *entry = new;
Packit Service 20376f
	GIT_UNUSED(old);
Packit Service 20376f
	giterr_set(GITERR_INDEX, "'%s' appears multiple times at stage %d",
Packit Service 20376f
		entry->path, GIT_IDXENTRY_STAGE(entry));
Packit Service 20376f
	return GIT_EEXISTS;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void index_existing_and_best(
Packit Service 20376f
	git_index_entry **existing,
Packit Service 20376f
	size_t *existing_position,
Packit Service 20376f
	git_index_entry **best,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_index_entry *entry)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry *e;
Packit Service 20376f
	size_t pos;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	error = index_find(&pos,
Packit Service 20376f
		index, entry->path, 0, GIT_IDXENTRY_STAGE(entry));
Packit Service 20376f
Packit Service 20376f
	if (error == 0) {
Packit Service 20376f
		*existing = index->entries.contents[pos];
Packit Service 20376f
		*existing_position = pos;
Packit Service 20376f
		*best = index->entries.contents[pos];
Packit Service 20376f
		return;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*existing = NULL;
Packit Service 20376f
	*existing_position = 0;
Packit Service 20376f
	*best = NULL;
Packit Service 20376f
Packit Service 20376f
	if (GIT_IDXENTRY_STAGE(entry) == 0) {
Packit Service 20376f
		for (; pos < index->entries.length; pos++) {
Packit Service 20376f
			int (*strcomp)(const char *a, const char *b) =
Packit Service 20376f
				index->ignore_case ? git__strcasecmp : git__strcmp;
Packit Service 20376f
Packit Service 20376f
			e = index->entries.contents[pos];
Packit Service 20376f
Packit Service 20376f
			if (strcomp(entry->path, e->path) != 0)
Packit Service 20376f
				break;
Packit Service 20376f
Packit Service 20376f
			if (GIT_IDXENTRY_STAGE(e) == GIT_INDEX_STAGE_ANCESTOR) {
Packit Service 20376f
				*best = e;
Packit Service 20376f
				continue;
Packit Service 20376f
			} else {
Packit Service 20376f
				*best = e;
Packit Service 20376f
				break;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* index_insert takes ownership of the new entry - if it can't insert
Packit Service 20376f
 * it, then it will return an error **and also free the entry**.  When
Packit Service 20376f
 * it replaces an existing entry, it will update the entry_ptr with the
Packit Service 20376f
 * actual entry in the index (and free the passed in one).
Packit Service 20376f
 *
Packit Service 20376f
 * trust_path is whether we use the given path, or whether (on case
Packit Service 20376f
 * insensitive systems only) we try to canonicalize the given path to
Packit Service 20376f
 * be within an existing directory.
Packit Service 20376f
 *
Packit Service 20376f
 * trust_mode is whether we trust the mode in entry_ptr.
Packit Service 20376f
 *
Packit Service 20376f
 * trust_id is whether we trust the id or it should be validated.
Packit Service 20376f
 */
Packit Service 20376f
static int index_insert(
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	git_index_entry **entry_ptr,
Packit Service 20376f
	int replace,
Packit Service 20376f
	bool trust_path,
Packit Service 20376f
	bool trust_mode,
Packit Service 20376f
	bool trust_id)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	size_t path_length, position;
Packit Service 20376f
	git_index_entry *existing, *best, *entry;
Packit Service 20376f
Packit Service 20376f
	assert(index && entry_ptr);
Packit Service 20376f
Packit Service 20376f
	entry = *entry_ptr;
Packit Service 20376f
Packit Service 20376f
	/* make sure that the path length flag is correct */
Packit Service 20376f
	path_length = ((struct entry_internal *)entry)->pathlen;
Packit Service 20376f
	index_entry_adjust_namemask(entry, path_length);
Packit Service 20376f
Packit Service 20376f
	/* this entry is now up-to-date and should not be checked for raciness */
Packit Service 20376f
	entry->flags_extended |= GIT_IDXENTRY_UPTODATE;
Packit Service 20376f
Packit Service 20376f
	git_vector_sort(&index->entries);
Packit Service 20376f
Packit Service 20376f
	/* look if an entry with this path already exists, either staged, or (if
Packit Service 20376f
	 * this entry is a regular staged item) as the "ours" side of a conflict.
Packit Service 20376f
	 */
Packit Service 20376f
	index_existing_and_best(&existing, &position, &best, index, entry);
Packit Service 20376f
Packit Service 20376f
	/* update the file mode */
Packit Service 20376f
	entry->mode = trust_mode ?
Packit Service 20376f
		git_index__create_mode(entry->mode) :
Packit Service 20376f
		index_merge_mode(index, best, entry->mode);
Packit Service 20376f
Packit Service 20376f
	/* canonicalize the directory name */
Packit Service 20376f
	if (!trust_path)
Packit Service 20376f
		error = canonicalize_directory_path(index, entry, best);
Packit Service 20376f
Packit Service 20376f
	/* ensure that the given id exists (unless it's a submodule) */
Packit Service 20376f
	if (!error && !trust_id && INDEX_OWNER(index) &&
Packit Service 20376f
		(entry->mode & GIT_FILEMODE_COMMIT) != GIT_FILEMODE_COMMIT) {
Packit Service 20376f
Packit Service 20376f
		if (!git_object__is_valid(INDEX_OWNER(index), &entry->id,
Packit Service 20376f
			git_object__type_from_filemode(entry->mode)))
Packit Service 20376f
			error = -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* look for tree / blob name collisions, removing conflicts if requested */
Packit Service 20376f
	if (!error)
Packit Service 20376f
		error = check_file_directory_collision(index, entry, position, replace);
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		/* skip changes */;
Packit Service 20376f
Packit Service 20376f
	/* if we are replacing an existing item, overwrite the existing entry
Packit Service 20376f
	 * and return it in place of the passed in one.
Packit Service 20376f
	 */
Packit Service 20376f
	else if (existing) {
Packit Service 20376f
		if (replace) {
Packit Service 20376f
			index_entry_cpy(existing, entry);
Packit Service 20376f
Packit Service 20376f
			if (trust_path)
Packit Service 20376f
				memcpy((char *)existing->path, entry->path, strlen(entry->path));
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		index_entry_free(entry);
Packit Service 20376f
		*entry_ptr = entry = existing;
Packit Service 20376f
	}
Packit Service 20376f
	else {
Packit Service 20376f
		/* if replace is not requested or no existing entry exists, insert
Packit Service 20376f
		 * at the sorted position.  (Since we re-sort after each insert to
Packit Service 20376f
		 * check for dups, this is actually cheaper in the long run.)
Packit Service 20376f
		 */
Packit Service 20376f
		error = git_vector_insert_sorted(&index->entries, entry, index_no_dups);
Packit Service 20376f
Packit Service 20376f
		if (error == 0) {
Packit Service 20376f
			INSERT_IN_MAP(index, entry, &error);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error < 0) {
Packit Service 20376f
		index_entry_free(*entry_ptr);
Packit Service 20376f
		*entry_ptr = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_conflict_to_reuc(git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *conflict_entries[3];
Packit Service 20376f
	int ancestor_mode, our_mode, their_mode;
Packit Service 20376f
	git_oid const *ancestor_oid, *our_oid, *their_oid;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	if ((ret = git_index_conflict_get(&conflict_entries[0],
Packit Service 20376f
		&conflict_entries[1], &conflict_entries[2], index, path)) < 0)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	ancestor_mode = conflict_entries[0] == NULL ? 0 : conflict_entries[0]->mode;
Packit Service 20376f
	our_mode = conflict_entries[1] == NULL ? 0 : conflict_entries[1]->mode;
Packit Service 20376f
	their_mode = conflict_entries[2] == NULL ? 0 : conflict_entries[2]->mode;
Packit Service 20376f
Packit Service 20376f
	ancestor_oid = conflict_entries[0] == NULL ? NULL : &conflict_entries[0]->id;
Packit Service 20376f
	our_oid = conflict_entries[1] == NULL ? NULL : &conflict_entries[1]->id;
Packit Service 20376f
	their_oid = conflict_entries[2] == NULL ? NULL : &conflict_entries[2]->id;
Packit Service 20376f
Packit Service 20376f
	if ((ret = git_index_reuc_add(index, path, ancestor_mode, ancestor_oid,
Packit Service 20376f
		our_mode, our_oid, their_mode, their_oid)) >= 0)
Packit Service 20376f
		ret = git_index_conflict_remove(index, path);
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool valid_filemode(const int filemode)
Packit Service 20376f
{
Packit Service 20376f
	return (filemode == GIT_FILEMODE_BLOB ||
Packit Service 20376f
		filemode == GIT_FILEMODE_BLOB_EXECUTABLE ||
Packit Service 20376f
		filemode == GIT_FILEMODE_LINK ||
Packit Service 20376f
		filemode == GIT_FILEMODE_COMMIT);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_add_frombuffer(
Packit Service 20376f
    git_index *index, const git_index_entry *source_entry,
Packit Service 20376f
    const void *buffer, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry *entry = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_oid id;
Packit Service 20376f
Packit Service 20376f
	assert(index && source_entry->path);
Packit Service 20376f
Packit Service 20376f
	if (INDEX_OWNER(index) == NULL)
Packit Service 20376f
		return create_index_error(-1,
Packit Service 20376f
			"could not initialize index entry. "
Packit Service 20376f
			"Index is not backed up by an existing repository.");
Packit Service 20376f
Packit Service 20376f
	if (!valid_filemode(source_entry->mode)) {
Packit Service 20376f
		giterr_set(GITERR_INDEX, "invalid filemode");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (index_entry_dup(&entry, index, source_entry) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	error = git_blob_create_frombuffer(&id, INDEX_OWNER(index), buffer, len);
Packit Service 20376f
	if (error < 0) {
Packit Service 20376f
		index_entry_free(entry);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&entry->id, &id;;
Packit Service 20376f
	entry->file_size = len;
Packit Service 20376f
Packit Service 20376f
	if ((error = index_insert(index, &entry, 1, true, true, true)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* Adding implies conflict was resolved, move conflict entries to REUC */
Packit Service 20376f
	if ((error = index_conflict_to_reuc(index, entry->path)) < 0 && error != GIT_ENOTFOUND)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	git_tree_cache_invalidate_path(index->tree, entry->path);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int add_repo_as_submodule(git_index_entry **out, git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *sub;
Packit Service 20376f
	git_buf abspath = GIT_BUF_INIT;
Packit Service 20376f
	git_repository *repo = INDEX_OWNER(index);
Packit Service 20376f
	git_reference *head;
Packit Service 20376f
	git_index_entry *entry;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&abspath, git_repository_workdir(repo), path)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = p_stat(abspath.ptr, &st)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to stat repository dir");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (index_entry_create(&entry, INDEX_OWNER(index), path, &st, true) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_open(&sub, abspath.ptr)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_head(&head, sub)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&entry->id, git_reference_target(head));
Packit Service 20376f
	entry->mode = GIT_FILEMODE_COMMIT;
Packit Service 20376f
Packit Service 20376f
	git_reference_free(head);
Packit Service 20376f
	git_repository_free(sub);
Packit Service 20376f
	git_buf_free(&abspath);
Packit Service 20376f
Packit Service 20376f
	*out = entry;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_add_bypath(git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry *entry = NULL;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	assert(index && path);
Packit Service 20376f
Packit Service 20376f
	if ((ret = index_entry_init(&entry, index, path)) == 0)
Packit Service 20376f
		ret = index_insert(index, &entry, 1, false, false, true);
Packit Service 20376f
Packit Service 20376f
	/* If we were given a directory, let's see if it's a submodule */
Packit Service 20376f
	if (ret < 0 && ret != GIT_EDIRECTORY)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	if (ret == GIT_EDIRECTORY) {
Packit Service 20376f
		git_submodule *sm;
Packit Service 20376f
		git_error_state err;
Packit Service 20376f
Packit Service 20376f
		giterr_state_capture(&err, ret);
Packit Service 20376f
Packit Service 20376f
		ret = git_submodule_lookup(&sm, INDEX_OWNER(index), path);
Packit Service 20376f
		if (ret == GIT_ENOTFOUND)
Packit Service 20376f
			return giterr_state_restore(&err;;
Packit Service 20376f
Packit Service 20376f
		giterr_state_free(&err;;
Packit Service 20376f
Packit Service 20376f
		/*
Packit Service 20376f
		 * EEXISTS means that there is a repository at that path, but it's not known
Packit Service 20376f
		 * as a submodule. We add its HEAD as an entry and don't register it.
Packit Service 20376f
		 */
Packit Service 20376f
		if (ret == GIT_EEXISTS) {
Packit Service 20376f
			if ((ret = add_repo_as_submodule(&entry, index, path)) < 0)
Packit Service 20376f
				return ret;
Packit Service 20376f
Packit Service 20376f
			if ((ret = index_insert(index, &entry, 1, false, false, true)) < 0)
Packit Service 20376f
				return ret;
Packit Service 20376f
		} else if (ret < 0) {
Packit Service 20376f
			return ret;
Packit Service 20376f
		} else {
Packit Service 20376f
			ret = git_submodule_add_to_index(sm, false);
Packit Service 20376f
			git_submodule_free(sm);
Packit Service 20376f
			return ret;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Adding implies conflict was resolved, move conflict entries to REUC */
Packit Service 20376f
	if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	git_tree_cache_invalidate_path(index->tree, entry->path);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_remove_bypath(git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	assert(index && path);
Packit Service 20376f
Packit Service 20376f
	if (((ret = git_index_remove(index, path, 0)) < 0 &&
Packit Service 20376f
		ret != GIT_ENOTFOUND) ||
Packit Service 20376f
		((ret = index_conflict_to_reuc(index, path)) < 0 &&
Packit Service 20376f
		ret != GIT_ENOTFOUND))
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	if (ret == GIT_ENOTFOUND)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index__fill(git_index *index, const git_vector *source_entries)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *source_entry = NULL;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int ret = 0;
Packit Service 20376f
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	if (!source_entries->length)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	git_vector_size_hint(&index->entries, source_entries->length);
Packit Service 20376f
	git_idxmap_resize(index->entries_map, (khint_t)(source_entries->length * 1.3));
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(source_entries, i, source_entry) {
Packit Service 20376f
		git_index_entry *entry = NULL;
Packit Service 20376f
Packit Service 20376f
		if ((ret = index_entry_dup(&entry, index, source_entry)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		index_entry_adjust_namemask(entry, ((struct entry_internal *)entry)->pathlen);
Packit Service 20376f
		entry->flags_extended |= GIT_IDXENTRY_UPTODATE;
Packit Service 20376f
		entry->mode = git_index__create_mode(entry->mode);
Packit Service 20376f
Packit Service 20376f
		if ((ret = git_vector_insert(&index->entries, entry)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		INSERT_IN_MAP(index, entry, &ret;;
Packit Service 20376f
		if (ret < 0)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!ret)
Packit Service 20376f
		git_vector_sort(&index->entries);
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
int git_index_add(git_index *index, const git_index_entry *source_entry)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry *entry = NULL;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	assert(index && source_entry && source_entry->path);
Packit Service 20376f
Packit Service 20376f
	if (!valid_filemode(source_entry->mode)) {
Packit Service 20376f
		giterr_set(GITERR_INDEX, "invalid filemode");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((ret = index_entry_dup(&entry, index, source_entry)) < 0 ||
Packit Service 20376f
		(ret = index_insert(index, &entry, 1, true, true, false)) < 0)
Packit Service 20376f
		return ret;
Packit Service 20376f
Packit Service 20376f
	git_tree_cache_invalidate_path(index->tree, entry->path);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_remove(git_index *index, const char *path, int stage)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	size_t position;
Packit Service 20376f
	git_index_entry remove_key = {{ 0 }};
Packit Service 20376f
Packit Service 20376f
	remove_key.path = path;
Packit Service 20376f
	GIT_IDXENTRY_STAGE_SET(&remove_key, stage);
Packit Service 20376f
Packit Service 20376f
	DELETE_IN_MAP(index, &remove_key);
Packit Service 20376f
Packit Service 20376f
	if (index_find(&position, index, path, 0, stage) < 0) {
Packit Service 20376f
		giterr_set(
Packit Service 20376f
			GITERR_INDEX, "index does not contain %s at stage %d", path, stage);
Packit Service 20376f
		error = GIT_ENOTFOUND;
Packit Service 20376f
	} else {
Packit Service 20376f
		error = index_remove_entry(index, position);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_remove_directory(git_index *index, const char *dir, int stage)
Packit Service 20376f
{
Packit Service 20376f
	git_buf pfx = GIT_BUF_INIT;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	size_t pos;
Packit Service 20376f
	git_index_entry *entry;
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_buf_sets(&pfx, dir)) &&
Packit Service 20376f
		!(error = git_path_to_dir(&pfx)))
Packit Service 20376f
		index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY);
Packit Service 20376f
Packit Service 20376f
	while (!error) {
Packit Service 20376f
		entry = git_vector_get(&index->entries, pos);
Packit Service 20376f
		if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		if (GIT_IDXENTRY_STAGE(entry) != stage) {
Packit Service 20376f
			++pos;
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		error = index_remove_entry(index, pos);
Packit Service 20376f
Packit Service 20376f
		/* removed entry at 'pos' so we don't need to increment */
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&pfx;;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_find_prefix(size_t *at_pos, git_index *index, const char *prefix)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	size_t pos;
Packit Service 20376f
	const git_index_entry *entry;
Packit Service 20376f
Packit Service 20376f
	index_find(&pos, index, prefix, strlen(prefix), GIT_INDEX_STAGE_ANY);
Packit Service 20376f
	entry = git_vector_get(&index->entries, pos);
Packit Service 20376f
	if (!entry || git__prefixcmp(entry->path, prefix) != 0)
Packit Service 20376f
		error = GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
	if (!error && at_pos)
Packit Service 20376f
		*at_pos = pos;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index__find_pos(
Packit Service 20376f
	size_t *out, git_index *index, const char *path, size_t path_len, int stage)
Packit Service 20376f
{
Packit Service 20376f
	assert(index && path);
Packit Service 20376f
	return index_find(out, index, path, path_len, stage);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_find(size_t *at_pos, git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	size_t pos;
Packit Service 20376f
Packit Service 20376f
	assert(index && path);
Packit Service 20376f
Packit Service 20376f
	if (git_vector_bsearch2(
Packit Service 20376f
			&pos, &index->entries, index->entries_search_path, path) < 0) {
Packit Service 20376f
		giterr_set(GITERR_INDEX, "index does not contain %s", path);
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Since our binary search only looked at path, we may be in the
Packit Service 20376f
	 * middle of a list of stages.
Packit Service 20376f
	 */
Packit Service 20376f
	for (; pos > 0; --pos) {
Packit Service 20376f
		const git_index_entry *prev = git_vector_get(&index->entries, pos - 1);
Packit Service 20376f
Packit Service 20376f
		if (index->entries_cmp_path(prev->path, path) != 0)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (at_pos)
Packit Service 20376f
		*at_pos = pos;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_conflict_add(git_index *index,
Packit Service 20376f
	const git_index_entry *ancestor_entry,
Packit Service 20376f
	const git_index_entry *our_entry,
Packit Service 20376f
	const git_index_entry *their_entry)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry *entries[3] = { 0 };
Packit Service 20376f
	unsigned short i;
Packit Service 20376f
	int ret = 0;
Packit Service 20376f
Packit Service 20376f
	assert (index);
Packit Service 20376f
Packit Service 20376f
	if ((ancestor_entry &&
Packit Service 20376f
			(ret = index_entry_dup(&entries[0], index, ancestor_entry)) < 0) ||
Packit Service 20376f
		(our_entry &&
Packit Service 20376f
			(ret = index_entry_dup(&entries[1], index, our_entry)) < 0) ||
Packit Service 20376f
		(their_entry &&
Packit Service 20376f
			(ret = index_entry_dup(&entries[2], index, their_entry)) < 0))
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* Validate entries */
Packit Service 20376f
	for (i = 0; i < 3; i++) {
Packit Service 20376f
		if (entries[i] && !valid_filemode(entries[i]->mode)) {
Packit Service 20376f
			giterr_set(GITERR_INDEX, "invalid filemode for stage %d entry",
Packit Service 20376f
				i + 1);
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Remove existing index entries for each path */
Packit Service 20376f
	for (i = 0; i < 3; i++) {
Packit Service 20376f
		if (entries[i] == NULL)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		if ((ret = git_index_remove(index, entries[i]->path, 0)) != 0) {
Packit Service 20376f
			if (ret != GIT_ENOTFOUND)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			ret = 0;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Add the conflict entries */
Packit Service 20376f
	for (i = 0; i < 3; i++) {
Packit Service 20376f
		if (entries[i] == NULL)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		/* Make sure stage is correct */
Packit Service 20376f
		GIT_IDXENTRY_STAGE_SET(entries[i], i + 1);
Packit Service 20376f
Packit Service 20376f
		if ((ret = index_insert(index, &entries[i], 1, true, true, false)) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		entries[i] = NULL; /* don't free if later entry fails */
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	for (i = 0; i < 3; i++) {
Packit Service 20376f
		if (entries[i] != NULL)
Packit Service 20376f
			index_entry_free(entries[i]);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_conflict__get_byindex(
Packit Service 20376f
	const git_index_entry **ancestor_out,
Packit Service 20376f
	const git_index_entry **our_out,
Packit Service 20376f
	const git_index_entry **their_out,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	size_t n)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *conflict_entry;
Packit Service 20376f
	const char *path = NULL;
Packit Service 20376f
	size_t count;
Packit Service 20376f
	int stage, len = 0;
Packit Service 20376f
Packit Service 20376f
	assert(ancestor_out && our_out && their_out && index);
Packit Service 20376f
Packit Service 20376f
	*ancestor_out = NULL;
Packit Service 20376f
	*our_out = NULL;
Packit Service 20376f
	*their_out = NULL;
Packit Service 20376f
Packit Service 20376f
	for (count = git_index_entrycount(index); n < count; ++n) {
Packit Service 20376f
		conflict_entry = git_vector_get(&index->entries, n);
Packit Service 20376f
Packit Service 20376f
		if (path && index->entries_cmp_path(conflict_entry->path, path) != 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		stage = GIT_IDXENTRY_STAGE(conflict_entry);
Packit Service 20376f
		path = conflict_entry->path;
Packit Service 20376f
Packit Service 20376f
		switch (stage) {
Packit Service 20376f
		case 3:
Packit Service 20376f
			*their_out = conflict_entry;
Packit Service 20376f
			len++;
Packit Service 20376f
			break;
Packit Service 20376f
		case 2:
Packit Service 20376f
			*our_out = conflict_entry;
Packit Service 20376f
			len++;
Packit Service 20376f
			break;
Packit Service 20376f
		case 1:
Packit Service 20376f
			*ancestor_out = conflict_entry;
Packit Service 20376f
			len++;
Packit Service 20376f
			break;
Packit Service 20376f
		default:
Packit Service 20376f
			break;
Packit Service 20376f
		};
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return len;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_conflict_get(
Packit Service 20376f
	const git_index_entry **ancestor_out,
Packit Service 20376f
	const git_index_entry **our_out,
Packit Service 20376f
	const git_index_entry **their_out,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const char *path)
Packit Service 20376f
{
Packit Service 20376f
	size_t pos;
Packit Service 20376f
	int len = 0;
Packit Service 20376f
Packit Service 20376f
	assert(ancestor_out && our_out && their_out && index && path);
Packit Service 20376f
Packit Service 20376f
	*ancestor_out = NULL;
Packit Service 20376f
	*our_out = NULL;
Packit Service 20376f
	*their_out = NULL;
Packit Service 20376f
Packit Service 20376f
	if (git_index_find(&pos, index, path) < 0)
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
	if ((len = index_conflict__get_byindex(
Packit Service 20376f
		ancestor_out, our_out, their_out, index, pos)) < 0)
Packit Service 20376f
		return len;
Packit Service 20376f
	else if (len == 0)
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_conflict_remove(git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	size_t pos = 0;
Packit Service 20376f
	git_index_entry *conflict_entry;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (path != NULL && git_index_find(&pos, index, path) < 0)
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
	while ((conflict_entry = git_vector_get(&index->entries, pos)) != NULL) {
Packit Service 20376f
Packit Service 20376f
		if (path != NULL &&
Packit Service 20376f
			index->entries_cmp_path(conflict_entry->path, path) != 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		if (GIT_IDXENTRY_STAGE(conflict_entry) == 0) {
Packit Service 20376f
			pos++;
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if ((error = index_remove_entry(index, pos)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_conflict_remove(git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	assert(index && path);
Packit Service 20376f
	return index_conflict_remove(index, path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_conflict_cleanup(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
	return index_conflict_remove(index, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_has_conflicts(const git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
	git_index_entry *entry;
Packit Service 20376f
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&index->entries, i, entry) {
Packit Service 20376f
		if (GIT_IDXENTRY_STAGE(entry) > 0)
Packit Service 20376f
			return 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_conflict_iterator_new(
Packit Service 20376f
	git_index_conflict_iterator **iterator_out,
Packit Service 20376f
	git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	git_index_conflict_iterator *it = NULL;
Packit Service 20376f
Packit Service 20376f
	assert(iterator_out && index);
Packit Service 20376f
Packit Service 20376f
	it = git__calloc(1, sizeof(git_index_conflict_iterator));
Packit Service 20376f
	GITERR_CHECK_ALLOC(it);
Packit Service 20376f
Packit Service 20376f
	it->index = index;
Packit Service 20376f
Packit Service 20376f
	*iterator_out = it;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_conflict_next(
Packit Service 20376f
	const git_index_entry **ancestor_out,
Packit Service 20376f
	const git_index_entry **our_out,
Packit Service 20376f
	const git_index_entry **their_out,
Packit Service 20376f
	git_index_conflict_iterator *iterator)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *entry;
Packit Service 20376f
	int len;
Packit Service 20376f
Packit Service 20376f
	assert(ancestor_out && our_out && their_out && iterator);
Packit Service 20376f
Packit Service 20376f
	*ancestor_out = NULL;
Packit Service 20376f
	*our_out = NULL;
Packit Service 20376f
	*their_out = NULL;
Packit Service 20376f
Packit Service 20376f
	while (iterator->cur < iterator->index->entries.length) {
Packit Service 20376f
		entry = git_index_get_byindex(iterator->index, iterator->cur);
Packit Service 20376f
Packit Service 20376f
		if (git_index_entry_is_conflict(entry)) {
Packit Service 20376f
			if ((len = index_conflict__get_byindex(
Packit Service 20376f
				ancestor_out,
Packit Service 20376f
				our_out,
Packit Service 20376f
				their_out,
Packit Service 20376f
				iterator->index,
Packit Service 20376f
				iterator->cur)) < 0)
Packit Service 20376f
				return len;
Packit Service 20376f
Packit Service 20376f
			iterator->cur += len;
Packit Service 20376f
			return 0;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		iterator->cur++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return GIT_ITEROVER;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_index_conflict_iterator_free(git_index_conflict_iterator *iterator)
Packit Service 20376f
{
Packit Service 20376f
	if (iterator == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git__free(iterator);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_index_name_entrycount(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
	return index->names.length;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const git_index_name_entry *git_index_name_get_byindex(
Packit Service 20376f
	git_index *index, size_t n)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	git_vector_sort(&index->names);
Packit Service 20376f
	return git_vector_get(&index->names, n);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void index_name_entry_free(git_index_name_entry *ne)
Packit Service 20376f
{
Packit Service 20376f
	if (!ne)
Packit Service 20376f
		return;
Packit Service 20376f
	git__free(ne->ancestor);
Packit Service 20376f
	git__free(ne->ours);
Packit Service 20376f
	git__free(ne->theirs);
Packit Service 20376f
	git__free(ne);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_name_add(git_index *index,
Packit Service 20376f
	const char *ancestor, const char *ours, const char *theirs)
Packit Service 20376f
{
Packit Service 20376f
	git_index_name_entry *conflict_name;
Packit Service 20376f
Packit Service 20376f
	assert((ancestor && ours) || (ancestor && theirs) || (ours && theirs));
Packit Service 20376f
Packit Service 20376f
	conflict_name = git__calloc(1, sizeof(git_index_name_entry));
Packit Service 20376f
	GITERR_CHECK_ALLOC(conflict_name);
Packit Service 20376f
Packit Service 20376f
	if ((ancestor && !(conflict_name->ancestor = git__strdup(ancestor))) ||
Packit Service 20376f
		(ours     && !(conflict_name->ours     = git__strdup(ours))) ||
Packit Service 20376f
		(theirs   && !(conflict_name->theirs   = git__strdup(theirs))) ||
Packit Service 20376f
		git_vector_insert(&index->names, conflict_name) < 0)
Packit Service 20376f
	{
Packit Service 20376f
		index_name_entry_free(conflict_name);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_index_name_clear(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
	git_index_name_entry *conflict_name;
Packit Service 20376f
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&index->names, i, conflict_name)
Packit Service 20376f
		index_name_entry_free(conflict_name);
Packit Service 20376f
Packit Service 20376f
	git_vector_clear(&index->names);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_index_reuc_entrycount(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
	return index->reuc.length;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_reuc_on_dup(void **old, void *new)
Packit Service 20376f
{
Packit Service 20376f
	index_entry_reuc_free(*old);
Packit Service 20376f
	*old = new;
Packit Service 20376f
	return GIT_EEXISTS;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_reuc_insert(
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	git_index_reuc_entry *reuc)
Packit Service 20376f
{
Packit Service 20376f
	int res;
Packit Service 20376f
Packit Service 20376f
	assert(index && reuc && reuc->path != NULL);
Packit Service 20376f
	assert(git_vector_is_sorted(&index->reuc));
Packit Service 20376f
Packit Service 20376f
	res = git_vector_insert_sorted(&index->reuc, reuc, &index_reuc_on_dup);
Packit Service 20376f
	return res == GIT_EEXISTS ? 0 : res;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_reuc_add(git_index *index, const char *path,
Packit Service 20376f
	int ancestor_mode, const git_oid *ancestor_oid,
Packit Service 20376f
	int our_mode, const git_oid *our_oid,
Packit Service 20376f
	int their_mode, const git_oid *their_oid)
Packit Service 20376f
{
Packit Service 20376f
	git_index_reuc_entry *reuc = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(index && path);
Packit Service 20376f
Packit Service 20376f
	if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode,
Packit Service 20376f
			ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 ||
Packit Service 20376f
		(error = index_reuc_insert(index, reuc)) < 0)
Packit Service 20376f
		index_entry_reuc_free(reuc);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	return git_vector_bsearch2(at_pos, &index->reuc, index->reuc_search, path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const git_index_reuc_entry *git_index_reuc_get_bypath(
Packit Service 20376f
	git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	size_t pos;
Packit Service 20376f
	assert(index && path);
Packit Service 20376f
Packit Service 20376f
	if (!index->reuc.length)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	assert(git_vector_is_sorted(&index->reuc));
Packit Service 20376f
Packit Service 20376f
	if (git_index_reuc_find(&pos, index, path) < 0)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	return git_vector_get(&index->reuc, pos);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const git_index_reuc_entry *git_index_reuc_get_byindex(
Packit Service 20376f
	git_index *index, size_t n)
Packit Service 20376f
{
Packit Service 20376f
	assert(index);
Packit Service 20376f
	assert(git_vector_is_sorted(&index->reuc));
Packit Service 20376f
Packit Service 20376f
	return git_vector_get(&index->reuc, n);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_reuc_remove(git_index *index, size_t position)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_index_reuc_entry *reuc;
Packit Service 20376f
Packit Service 20376f
	assert(git_vector_is_sorted(&index->reuc));
Packit Service 20376f
Packit Service 20376f
	reuc = git_vector_get(&index->reuc, position);
Packit Service 20376f
	error = git_vector_remove(&index->reuc, position);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		index_entry_reuc_free(reuc);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_index_reuc_clear(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < index->reuc.length; ++i)
Packit Service 20376f
		index_entry_reuc_free(git__swap(index->reuc.contents[i], NULL));
Packit Service 20376f
Packit Service 20376f
	git_vector_clear(&index->reuc);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int index_error_invalid(const char *message)
Packit Service 20376f
{
Packit Service 20376f
	giterr_set(GITERR_INDEX, "invalid data in index - %s", message);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int read_reuc(git_index *index, const char *buffer, size_t size)
Packit Service 20376f
{
Packit Service 20376f
	const char *endptr;
Packit Service 20376f
	size_t len;
Packit Service 20376f
	int i;
Packit Service 20376f
Packit Service 20376f
	/* If called multiple times, the vector might already be initialized */
Packit Service 20376f
	if (index->reuc._alloc_size == 0 &&
Packit Service 20376f
		git_vector_init(&index->reuc, 16, reuc_cmp) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	while (size) {
Packit Service 20376f
		git_index_reuc_entry *lost;
Packit Service 20376f
Packit Service 20376f
		len = p_strnlen(buffer, size) + 1;
Packit Service 20376f
		if (size <= len)
Packit Service 20376f
			return index_error_invalid("reading reuc entries");
Packit Service 20376f
Packit Service 20376f
		lost = reuc_entry_alloc(buffer);
Packit Service 20376f
		GITERR_CHECK_ALLOC(lost);
Packit Service 20376f
Packit Service 20376f
		size -= len;
Packit Service 20376f
		buffer += len;
Packit Service 20376f
Packit Service 20376f
		/* read 3 ASCII octal numbers for stage entries */
Packit Service 20376f
		for (i = 0; i < 3; i++) {
Packit Service 20376f
			int64_t tmp;
Packit Service 20376f
Packit Service 20376f
			if (git__strntol64(&tmp, buffer, size, &endptr, 8) < 0 ||
Packit Service 20376f
				!endptr || endptr == buffer || *endptr ||
Packit Service 20376f
				tmp < 0 || tmp > UINT32_MAX) {
Packit Service 20376f
				index_entry_reuc_free(lost);
Packit Service 20376f
				return index_error_invalid("reading reuc entry stage");
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			lost->mode[i] = (uint32_t)tmp;
Packit Service 20376f
Packit Service 20376f
			len = (endptr + 1) - buffer;
Packit Service 20376f
			if (size <= len) {
Packit Service 20376f
				index_entry_reuc_free(lost);
Packit Service 20376f
				return index_error_invalid("reading reuc entry stage");
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			size -= len;
Packit Service 20376f
			buffer += len;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* read up to 3 OIDs for stage entries */
Packit Service 20376f
		for (i = 0; i < 3; i++) {
Packit Service 20376f
			if (!lost->mode[i])
Packit Service 20376f
				continue;
Packit Service 20376f
			if (size < 20) {
Packit Service 20376f
				index_entry_reuc_free(lost);
Packit Service 20376f
				return index_error_invalid("reading reuc entry oid");
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer);
Packit Service 20376f
			size -= 20;
Packit Service 20376f
			buffer += 20;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* entry was read successfully - insert into reuc vector */
Packit Service 20376f
		if (git_vector_insert(&index->reuc, lost) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* entries are guaranteed to be sorted on-disk */
Packit Service 20376f
	git_vector_set_sorted(&index->reuc, true);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static int read_conflict_names(git_index *index, const char *buffer, size_t size)
Packit Service 20376f
{
Packit Service 20376f
	size_t len;
Packit Service 20376f
Packit Service 20376f
	/* This gets called multiple times, the vector might already be initialized */
Packit Service 20376f
	if (index->names._alloc_size == 0 &&
Packit Service 20376f
		git_vector_init(&index->names, 16, conflict_name_cmp) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
#define read_conflict_name(ptr) \
Packit Service 20376f
	len = p_strnlen(buffer, size) + 1; \
Packit Service 20376f
	if (size < len) { \
Packit Service 20376f
		index_error_invalid("reading conflict name entries"); \
Packit Service 20376f
		goto out_err; \
Packit Service 20376f
	} \
Packit Service 20376f
	if (len == 1) \
Packit Service 20376f
		ptr = NULL; \
Packit Service 20376f
	else { \
Packit Service 20376f
		ptr = git__malloc(len); \
Packit Service 20376f
		GITERR_CHECK_ALLOC(ptr); \
Packit Service 20376f
		memcpy(ptr, buffer, len); \
Packit Service 20376f
	} \
Packit Service 20376f
	\
Packit Service 20376f
	buffer += len; \
Packit Service 20376f
	size -= len;
Packit Service 20376f
Packit Service 20376f
	while (size) {
Packit Service 20376f
		git_index_name_entry *conflict_name = git__calloc(1, sizeof(git_index_name_entry));
Packit Service 20376f
		GITERR_CHECK_ALLOC(conflict_name);
Packit Service 20376f
Packit Service 20376f
		read_conflict_name(conflict_name->ancestor);
Packit Service 20376f
		read_conflict_name(conflict_name->ours);
Packit Service 20376f
		read_conflict_name(conflict_name->theirs);
Packit Service 20376f
Packit Service 20376f
		if (git_vector_insert(&index->names, conflict_name) < 0)
Packit Service 20376f
			goto out_err;
Packit Service 20376f
Packit Service 20376f
		continue;
Packit Service 20376f
Packit Service 20376f
out_err:
Packit Service 20376f
		git__free(conflict_name->ancestor);
Packit Service 20376f
		git__free(conflict_name->ours);
Packit Service 20376f
		git__free(conflict_name->theirs);
Packit Service 20376f
		git__free(conflict_name);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
#undef read_conflict_name
Packit Service 20376f
Packit Service 20376f
	/* entries are guaranteed to be sorted on-disk */
Packit Service 20376f
	git_vector_set_sorted(&index->names, true);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static size_t index_entry_size(size_t path_len, size_t varint_len, uint32_t flags)
Packit Service 20376f
{
Packit Service 20376f
	if (varint_len) {
Packit Service 20376f
		if (flags & GIT_IDXENTRY_EXTENDED)
Packit Service 20376f
			return offsetof(struct entry_long, path) + path_len + 1 + varint_len;
Packit Service 20376f
		else
Packit Service 20376f
			return offsetof(struct entry_short, path) + path_len + 1 + varint_len;
Packit Service 20376f
	} else {
Packit Service 20376f
#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7)
Packit Service 20376f
		if (flags & GIT_IDXENTRY_EXTENDED)
Packit Service 20376f
			return entry_size(struct entry_long, path_len);
Packit Service 20376f
		else
Packit Service 20376f
			return entry_size(struct entry_short, path_len);
Packit Service 20376f
#undef entry_size
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int read_entry(
Packit Service 20376f
	git_index_entry **out,
Packit Service 20376f
	size_t *out_size,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const void *buffer,
Packit Service 20376f
	size_t buffer_size,
Packit Service 20376f
	const char *last)
Packit Service 20376f
{
Packit Service 20376f
	size_t path_length, entry_size;
Packit Service 20376f
	const char *path_ptr;
Packit Service 20376f
	struct entry_short source;
Packit Service 20376f
	git_index_entry entry = {{0}};
Packit Service 20376f
	bool compressed = index->version >= INDEX_VERSION_NUMBER_COMP;
Packit Service 20376f
	char *tmp_path = NULL;
Packit Service 20376f
Packit Service 20376f
	if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* buffer is not guaranteed to be aligned */
Packit Service 20376f
	memcpy(&source, buffer, sizeof(struct entry_short));
Packit Service 20376f
Packit Service 20376f
	entry.ctime.seconds = (git_time_t)ntohl(source.ctime.seconds);
Packit Service 20376f
	entry.ctime.nanoseconds = ntohl(source.ctime.nanoseconds);
Packit Service 20376f
	entry.mtime.seconds = (git_time_t)ntohl(source.mtime.seconds);
Packit Service 20376f
	entry.mtime.nanoseconds = ntohl(source.mtime.nanoseconds);
Packit Service 20376f
	entry.dev = ntohl(source.dev);
Packit Service 20376f
	entry.ino = ntohl(source.ino);
Packit Service 20376f
	entry.mode = ntohl(source.mode);
Packit Service 20376f
	entry.uid = ntohl(source.uid);
Packit Service 20376f
	entry.gid = ntohl(source.gid);
Packit Service 20376f
	entry.file_size = ntohl(source.file_size);
Packit Service 20376f
	git_oid_cpy(&entry.id, &source.oid);
Packit Service 20376f
	entry.flags = ntohs(source.flags);
Packit Service 20376f
Packit Service 20376f
	if (entry.flags & GIT_IDXENTRY_EXTENDED) {
Packit Service 20376f
		uint16_t flags_raw;
Packit Service 20376f
		size_t flags_offset;
Packit Service 20376f
Packit Service 20376f
		flags_offset = offsetof(struct entry_long, flags_extended);
Packit Service 20376f
		memcpy(&flags_raw, (const char *) buffer + flags_offset,
Packit Service 20376f
			sizeof(flags_raw));
Packit Service 20376f
		flags_raw = ntohs(flags_raw);
Packit Service 20376f
Packit Service 20376f
		memcpy(&entry.flags_extended, &flags_raw, sizeof(flags_raw));
Packit Service 20376f
		path_ptr = (const char *) buffer + offsetof(struct entry_long, path);
Packit Service 20376f
	} else
Packit Service 20376f
		path_ptr = (const char *) buffer + offsetof(struct entry_short, path);
Packit Service 20376f
Packit Service 20376f
	if (!compressed) {
Packit Service 20376f
		path_length = entry.flags & GIT_IDXENTRY_NAMEMASK;
Packit Service 20376f
Packit Service 20376f
		/* if this is a very long string, we must find its
Packit Service 20376f
		 * real length without overflowing */
Packit Service 20376f
		if (path_length == 0xFFF) {
Packit Service 20376f
			const char *path_end;
Packit Service 20376f
Packit Service 20376f
			path_end = memchr(path_ptr, '\0', buffer_size);
Packit Service 20376f
			if (path_end == NULL)
Packit Service 20376f
				return -1;
Packit Service 20376f
Packit Service 20376f
			path_length = path_end - path_ptr;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		entry_size = index_entry_size(path_length, 0, entry.flags);
Packit Service 20376f
		entry.path = (char *)path_ptr;
Packit Service 20376f
	} else {
Packit Service 20376f
		size_t varint_len, last_len, prefix_len, suffix_len, path_len;
Packit Service 20376f
		uintmax_t strip_len;
Packit Service 20376f
Packit Service 20376f
		strip_len = git_decode_varint((const unsigned char *)path_ptr, &varint_len);
Packit Service 20376f
		last_len = strlen(last);
Packit Service 20376f
Packit Service 20376f
		if (varint_len == 0 || last_len < strip_len)
Packit Service 20376f
			return index_error_invalid("incorrect prefix length");
Packit Service 20376f
Packit Service 20376f
		prefix_len = last_len - strip_len;
Packit Service 20376f
		suffix_len = strlen(path_ptr + varint_len);
Packit Service 20376f
Packit Service 20376f
		GITERR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len);
Packit Service 20376f
		GITERR_CHECK_ALLOC_ADD(&path_len, path_len, 1);
Packit Service 20376f
Packit Service 20376f
		if (path_len > GIT_PATH_MAX)
Packit Service 20376f
			return index_error_invalid("unreasonable path length");
Packit Service 20376f
Packit Service 20376f
		tmp_path = git__malloc(path_len);
Packit Service 20376f
		GITERR_CHECK_ALLOC(tmp_path);
Packit Service 20376f
Packit Service 20376f
		memcpy(tmp_path, last, prefix_len);
Packit Service 20376f
		memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1);
Packit Service 20376f
		entry_size = index_entry_size(suffix_len, varint_len, entry.flags);
Packit Service 20376f
		entry.path = tmp_path;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (entry_size == 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (INDEX_FOOTER_SIZE + entry_size > buffer_size)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (index_entry_dup(out, index, &entry) < 0) {
Packit Service 20376f
		git__free(tmp_path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git__free(tmp_path);
Packit Service 20376f
	*out_size = entry_size;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int read_header(struct index_header *dest, const void *buffer)
Packit Service 20376f
{
Packit Service 20376f
	const struct index_header *source = buffer;
Packit Service 20376f
Packit Service 20376f
	dest->signature = ntohl(source->signature);
Packit Service 20376f
	if (dest->signature != INDEX_HEADER_SIG)
Packit Service 20376f
		return index_error_invalid("incorrect header signature");
Packit Service 20376f
Packit Service 20376f
	dest->version = ntohl(source->version);
Packit Service 20376f
	if (dest->version < INDEX_VERSION_NUMBER_LB ||
Packit Service 20376f
		dest->version > INDEX_VERSION_NUMBER_UB)
Packit Service 20376f
		return index_error_invalid("incorrect header version");
Packit Service 20376f
Packit Service 20376f
	dest->entry_count = ntohl(source->entry_count);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size)
Packit Service 20376f
{
Packit Service 20376f
	struct index_extension dest;
Packit Service 20376f
	size_t total_size;
Packit Service 20376f
Packit Service 20376f
	/* buffer is not guaranteed to be aligned */
Packit Service 20376f
	memcpy(&dest, buffer, sizeof(struct index_extension));
Packit Service 20376f
	dest.extension_size = ntohl(dest.extension_size);
Packit Service 20376f
Packit Service 20376f
	total_size = dest.extension_size + sizeof(struct index_extension);
Packit Service 20376f
Packit Service 20376f
	if (dest.extension_size > total_size ||
Packit Service 20376f
		buffer_size < total_size ||
Packit Service 20376f
		buffer_size - total_size < INDEX_FOOTER_SIZE)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* optional extension */
Packit Service 20376f
	if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
Packit Service 20376f
		/* tree cache */
Packit Service 20376f
		if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {
Packit Service 20376f
			if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, &index->tree_pool) < 0)
Packit Service 20376f
				return 0;
Packit Service 20376f
		} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
Packit Service 20376f
			if (read_reuc(index, buffer + 8, dest.extension_size) < 0)
Packit Service 20376f
				return 0;
Packit Service 20376f
		} else if (memcmp(dest.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4) == 0) {
Packit Service 20376f
			if (read_conflict_names(index, buffer + 8, dest.extension_size) < 0)
Packit Service 20376f
				return 0;
Packit Service 20376f
		}
Packit Service 20376f
		/* else, unsupported extension. We cannot parse this, but we can skip
Packit Service 20376f
		 * it by returning `total_size */
Packit Service 20376f
	} else {
Packit Service 20376f
		/* we cannot handle non-ignorable extensions;
Packit Service 20376f
		 * in fact they aren't even defined in the standard */
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return total_size;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
	struct index_header header = { 0 };
Packit Service 20376f
	git_oid checksum_calculated, checksum_expected;
Packit Service 20376f
	const char *last = NULL;
Packit Service 20376f
	const char *empty = "";
Packit Service 20376f
Packit Service 20376f
#define seek_forward(_increase) { \
Packit Service 20376f
	if (_increase >= buffer_size) { \
Packit Service 20376f
		error = index_error_invalid("ran out of data while parsing"); \
Packit Service 20376f
		goto done; } \
Packit Service 20376f
	buffer += _increase; \
Packit Service 20376f
	buffer_size -= _increase;\
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
	if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
Packit Service 20376f
		return index_error_invalid("insufficient buffer space");
Packit Service 20376f
Packit Service 20376f
	/* Precalculate the SHA1 of the files's contents -- we'll match it to
Packit Service 20376f
	 * the provided SHA1 in the footer */
Packit Service 20376f
	git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE);
Packit Service 20376f
Packit Service 20376f
	/* Parse header */
Packit Service 20376f
	if ((error = read_header(&header, buffer)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	index->version = header.version;
Packit Service 20376f
	if (index->version >= INDEX_VERSION_NUMBER_COMP)
Packit Service 20376f
		last = empty;
Packit Service 20376f
Packit Service 20376f
	seek_forward(INDEX_HEADER_SIZE);
Packit Service 20376f
Packit Service 20376f
	assert(!index->entries.length);
Packit Service 20376f
Packit Service 20376f
	if (index->ignore_case)
Packit Service 20376f
		git_idxmap_icase_resize((khash_t(idxicase) *) index->entries_map, header.entry_count);
Packit Service 20376f
	else
Packit Service 20376f
		git_idxmap_resize(index->entries_map, header.entry_count);
Packit Service 20376f
Packit Service 20376f
	/* Parse all the entries */
Packit Service 20376f
	for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
Packit Service 20376f
		git_index_entry *entry;
Packit Service 20376f
		size_t entry_size;
Packit Service 20376f
Packit Service 20376f
		if ((error = read_entry(&entry, &entry_size, index, buffer, buffer_size, last)) < 0) {
Packit Service 20376f
			error = index_error_invalid("invalid entry");
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if ((error = git_vector_insert(&index->entries, entry)) < 0) {
Packit Service 20376f
			index_entry_free(entry);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		INSERT_IN_MAP(index, entry, &error);
Packit Service 20376f
Packit Service 20376f
		if (error < 0) {
Packit Service 20376f
			index_entry_free(entry);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
		error = 0;
Packit Service 20376f
Packit Service 20376f
		if (index->version >= INDEX_VERSION_NUMBER_COMP)
Packit Service 20376f
			last = entry->path;
Packit Service 20376f
Packit Service 20376f
		seek_forward(entry_size);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (i != header.entry_count) {
Packit Service 20376f
		error = index_error_invalid("header entries changed while parsing");
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* There's still space for some extensions! */
Packit Service 20376f
	while (buffer_size > INDEX_FOOTER_SIZE) {
Packit Service 20376f
		size_t extension_size;
Packit Service 20376f
Packit Service 20376f
		extension_size = read_extension(index, buffer, buffer_size);
Packit Service 20376f
Packit Service 20376f
		/* see if we have read any bytes from the extension */
Packit Service 20376f
		if (extension_size == 0) {
Packit Service 20376f
			error = index_error_invalid("extension is truncated");
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		seek_forward(extension_size);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (buffer_size != INDEX_FOOTER_SIZE) {
Packit Service 20376f
		error = index_error_invalid(
Packit Service 20376f
			"buffer size does not match index footer size");
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* 160-bit SHA-1 over the content of the index file before this checksum. */
Packit Service 20376f
	git_oid_fromraw(&checksum_expected, (const unsigned char *)buffer);
Packit Service 20376f
Packit Service 20376f
	if (git_oid__cmp(&checksum_calculated, &checksum_expected) != 0) {
Packit Service 20376f
		error = index_error_invalid(
Packit Service 20376f
			"calculated checksum does not match expected");
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&index->checksum, &checksum_calculated);
Packit Service 20376f
Packit Service 20376f
#undef seek_forward
Packit Service 20376f
Packit Service 20376f
	/* Entries are stored case-sensitively on disk, so re-sort now if
Packit Service 20376f
	 * in-memory index is supposed to be case-insensitive
Packit Service 20376f
	 */
Packit Service 20376f
	git_vector_set_sorted(&index->entries, !index->ignore_case);
Packit Service 20376f
	git_vector_sort(&index->entries);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool is_index_extended(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	size_t i, extended;
Packit Service 20376f
	git_index_entry *entry;
Packit Service 20376f
Packit Service 20376f
	extended = 0;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&index->entries, i, entry) {
Packit Service 20376f
		entry->flags &= ~GIT_IDXENTRY_EXTENDED;
Packit Service 20376f
		if (entry->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS) {
Packit Service 20376f
			extended++;
Packit Service 20376f
			entry->flags |= GIT_IDXENTRY_EXTENDED;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return (extended > 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char *last)
Packit Service 20376f
{
Packit Service 20376f
	void *mem = NULL;
Packit Service 20376f
	struct entry_short *ondisk;
Packit Service 20376f
	size_t path_len, disk_size;
Packit Service 20376f
	int varint_len = 0;
Packit Service 20376f
	char *path;
Packit Service 20376f
	const char *path_start = entry->path;
Packit Service 20376f
	size_t same_len = 0;
Packit Service 20376f
Packit Service 20376f
	path_len = ((struct entry_internal *)entry)->pathlen;
Packit Service 20376f
Packit Service 20376f
	if (last) {
Packit Service 20376f
		const char *last_c = last;
Packit Service 20376f
Packit Service 20376f
		while (*path_start == *last_c) {
Packit Service 20376f
			if (!*path_start || !*last_c)
Packit Service 20376f
				break;
Packit Service 20376f
			++path_start;
Packit Service 20376f
			++last_c;
Packit Service 20376f
			++same_len;
Packit Service 20376f
		}
Packit Service 20376f
		path_len -= same_len;
Packit Service 20376f
		varint_len = git_encode_varint(NULL, 0, same_len);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	disk_size = index_entry_size(path_len, varint_len, entry->flags);
Packit Service 20376f
Packit Service 20376f
	if (git_filebuf_reserve(file, &mem, disk_size) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	ondisk = (struct entry_short *)mem;
Packit Service 20376f
Packit Service 20376f
	memset(ondisk, 0x0, disk_size);
Packit Service 20376f
Packit Service 20376f
	/**
Packit Service 20376f
	 * Yes, we have to truncate.
Packit Service 20376f
	 *
Packit Service 20376f
	 * The on-disk format for Index entries clearly defines
Packit Service 20376f
	 * the time and size fields to be 4 bytes each -- so even if
Packit Service 20376f
	 * we store these values with 8 bytes on-memory, they must
Packit Service 20376f
	 * be truncated to 4 bytes before writing to disk.
Packit Service 20376f
	 *
Packit Service 20376f
	 * In 2038 I will be either too dead or too rich to care about this
Packit Service 20376f
	 */
Packit Service 20376f
	ondisk->ctime.seconds = htonl((uint32_t)entry->ctime.seconds);
Packit Service 20376f
	ondisk->mtime.seconds = htonl((uint32_t)entry->mtime.seconds);
Packit Service 20376f
	ondisk->ctime.nanoseconds = htonl(entry->ctime.nanoseconds);
Packit Service 20376f
	ondisk->mtime.nanoseconds = htonl(entry->mtime.nanoseconds);
Packit Service 20376f
	ondisk->dev = htonl(entry->dev);
Packit Service 20376f
	ondisk->ino = htonl(entry->ino);
Packit Service 20376f
	ondisk->mode = htonl(entry->mode);
Packit Service 20376f
	ondisk->uid = htonl(entry->uid);
Packit Service 20376f
	ondisk->gid = htonl(entry->gid);
Packit Service 20376f
	ondisk->file_size = htonl((uint32_t)entry->file_size);
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&ondisk->oid, &entry->id);
Packit Service 20376f
Packit Service 20376f
	ondisk->flags = htons(entry->flags);
Packit Service 20376f
Packit Service 20376f
	if (entry->flags & GIT_IDXENTRY_EXTENDED) {
Packit Service 20376f
		struct entry_long *ondisk_ext;
Packit Service 20376f
		ondisk_ext = (struct entry_long *)ondisk;
Packit Service 20376f
		ondisk_ext->flags_extended = htons(entry->flags_extended &
Packit Service 20376f
			GIT_IDXENTRY_EXTENDED_FLAGS);
Packit Service 20376f
		path = ondisk_ext->path;
Packit Service 20376f
		disk_size -= offsetof(struct entry_long, path);
Packit Service 20376f
	} else {
Packit Service 20376f
		path = ondisk->path;
Packit Service 20376f
		disk_size -= offsetof(struct entry_short, path);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (last) {
Packit Service 20376f
		varint_len = git_encode_varint((unsigned char *) path,
Packit Service 20376f
					  disk_size, same_len);
Packit Service 20376f
		assert(varint_len > 0);
Packit Service 20376f
		path += varint_len;
Packit Service 20376f
		disk_size -= varint_len;
Packit Service 20376f
Packit Service 20376f
		/*
Packit Service 20376f
		 * If using path compression, we are not allowed
Packit Service 20376f
		 * to have additional trailing NULs.
Packit Service 20376f
		 */
Packit Service 20376f
		assert(disk_size == path_len + 1);
Packit Service 20376f
	} else {
Packit Service 20376f
		/*
Packit Service 20376f
		 * If no path compression is used, we do have
Packit Service 20376f
		 * NULs as padding. As such, simply assert that
Packit Service 20376f
		 * we have enough space left to write the path.
Packit Service 20376f
		 */
Packit Service 20376f
		assert(disk_size > path_len);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	memcpy(path, path_start, path_len + 1);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_entries(git_index *index, git_filebuf *file)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	git_vector case_sorted, *entries;
Packit Service 20376f
	git_index_entry *entry;
Packit Service 20376f
	const char *last = NULL;
Packit Service 20376f
Packit Service 20376f
	/* If index->entries is sorted case-insensitively, then we need
Packit Service 20376f
	 * to re-sort it case-sensitively before writing */
Packit Service 20376f
	if (index->ignore_case) {
Packit Service 20376f
		git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp);
Packit Service 20376f
		git_vector_sort(&case_sorted);
Packit Service 20376f
		entries = &case_sorted;
Packit Service 20376f
	} else {
Packit Service 20376f
		entries = &index->entries;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (index->version >= INDEX_VERSION_NUMBER_COMP)
Packit Service 20376f
		last = "";
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(entries, i, entry) {
Packit Service 20376f
		if ((error = write_disk_entry(file, entry, last)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
		if (index->version >= INDEX_VERSION_NUMBER_COMP)
Packit Service 20376f
			last = entry->path;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (index->ignore_case)
Packit Service 20376f
		git_vector_free(&case_sorted);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_extension(git_filebuf *file, struct index_extension *header, git_buf *data)
Packit Service 20376f
{
Packit Service 20376f
	struct index_extension ondisk;
Packit Service 20376f
Packit Service 20376f
	memset(&ondisk, 0x0, sizeof(struct index_extension));
Packit Service 20376f
	memcpy(&ondisk, header, 4);
Packit Service 20376f
	ondisk.extension_size = htonl(header->extension_size);
Packit Service 20376f
Packit Service 20376f
	git_filebuf_write(file, &ondisk, sizeof(struct index_extension));
Packit Service 20376f
	return git_filebuf_write(file, data->ptr, data->size);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int create_name_extension_data(git_buf *name_buf, git_index_name_entry *conflict_name)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (conflict_name->ancestor == NULL)
Packit Service 20376f
		error = git_buf_put(name_buf, "\0", 1);
Packit Service 20376f
	else
Packit Service 20376f
		error = git_buf_put(name_buf, conflict_name->ancestor, strlen(conflict_name->ancestor) + 1);
Packit Service 20376f
Packit Service 20376f
	if (error != 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	if (conflict_name->ours == NULL)
Packit Service 20376f
		error = git_buf_put(name_buf, "\0", 1);
Packit Service 20376f
	else
Packit Service 20376f
		error = git_buf_put(name_buf, conflict_name->ours, strlen(conflict_name->ours) + 1);
Packit Service 20376f
Packit Service 20376f
	if (error != 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	if (conflict_name->theirs == NULL)
Packit Service 20376f
		error = git_buf_put(name_buf, "\0", 1);
Packit Service 20376f
	else
Packit Service 20376f
		error = git_buf_put(name_buf, conflict_name->theirs, strlen(conflict_name->theirs) + 1);
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_name_extension(git_index *index, git_filebuf *file)
Packit Service 20376f
{
Packit Service 20376f
	git_buf name_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_vector *out = &index->names;
Packit Service 20376f
	git_index_name_entry *conflict_name;
Packit Service 20376f
	struct index_extension extension;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(out, i, conflict_name) {
Packit Service 20376f
		if ((error = create_name_extension_data(&name_buf, conflict_name)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	memset(&extension, 0x0, sizeof(struct index_extension));
Packit Service 20376f
	memcpy(&extension.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4);
Packit Service 20376f
	extension.extension_size = (uint32_t)name_buf.size;
Packit Service 20376f
Packit Service 20376f
	error = write_extension(file, &extension, &name_buf);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&name_buf);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int create_reuc_extension_data(git_buf *reuc_buf, git_index_reuc_entry *reuc)
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_put(reuc_buf, reuc->path, strlen(reuc->path) + 1)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < 3; i++) {
Packit Service 20376f
		if ((error = git_buf_printf(reuc_buf, "%o", reuc->mode[i])) < 0 ||
Packit Service 20376f
			(error = git_buf_put(reuc_buf, "\0", 1)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < 3; i++) {
Packit Service 20376f
		if (reuc->mode[i] && (error = git_buf_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_RAWSZ)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_reuc_extension(git_index *index, git_filebuf *file)
Packit Service 20376f
{
Packit Service 20376f
	git_buf reuc_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_vector *out = &index->reuc;
Packit Service 20376f
	git_index_reuc_entry *reuc;
Packit Service 20376f
	struct index_extension extension;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(out, i, reuc) {
Packit Service 20376f
		if ((error = create_reuc_extension_data(&reuc_buf, reuc)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	memset(&extension, 0x0, sizeof(struct index_extension));
Packit Service 20376f
	memcpy(&extension.signature, INDEX_EXT_UNMERGED_SIG, 4);
Packit Service 20376f
	extension.extension_size = (uint32_t)reuc_buf.size;
Packit Service 20376f
Packit Service 20376f
	error = write_extension(file, &extension, &reuc_buf);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&reuc_buf);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_tree_extension(git_index *index, git_filebuf *file)
Packit Service 20376f
{
Packit Service 20376f
	struct index_extension extension;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (index->tree == NULL)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_tree_cache_write(&buf, index->tree)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	memset(&extension, 0x0, sizeof(struct index_extension));
Packit Service 20376f
	memcpy(&extension.signature, INDEX_EXT_TREECACHE_SIG, 4);
Packit Service 20376f
	extension.extension_size = (uint32_t)buf.size;
Packit Service 20376f
Packit Service 20376f
	error = write_extension(file, &extension, &buf;;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void clear_uptodate(git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry *entry;
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&index->entries, i, entry)
Packit Service 20376f
		entry->flags_extended &= ~GIT_IDXENTRY_UPTODATE;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file)
Packit Service 20376f
{
Packit Service 20376f
	git_oid hash_final;
Packit Service 20376f
	struct index_header header;
Packit Service 20376f
	bool is_extended;
Packit Service 20376f
	uint32_t index_version_number;
Packit Service 20376f
Packit Service 20376f
	assert(index && file);
Packit Service 20376f
Packit Service 20376f
	if (index->version <= INDEX_VERSION_NUMBER_EXT)  {
Packit Service 20376f
		is_extended = is_index_extended(index);
Packit Service 20376f
		index_version_number = is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER_LB;
Packit Service 20376f
	} else {
Packit Service 20376f
		index_version_number = index->version;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	header.signature = htonl(INDEX_HEADER_SIG);
Packit Service 20376f
	header.version = htonl(index_version_number);
Packit Service 20376f
	header.entry_count = htonl((uint32_t)index->entries.length);
Packit Service 20376f
Packit Service 20376f
	if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (write_entries(index, file) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* write the tree cache extension */
Packit Service 20376f
	if (index->tree != NULL && write_tree_extension(index, file) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* write the rename conflict extension */
Packit Service 20376f
	if (index->names.length > 0 && write_name_extension(index, file) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* write the reuc extension */
Packit Service 20376f
	if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* get out the hash for all the contents we've appended to the file */
Packit Service 20376f
	git_filebuf_hash(&hash_final, file);
Packit Service 20376f
	git_oid_cpy(checksum, &hash_final);
Packit Service 20376f
Packit Service 20376f
	/* write it at the end of the file */
Packit Service 20376f
	if (git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* file entries are no longer up to date */
Packit Service 20376f
	clear_uptodate(index);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_entry_stage(const git_index_entry *entry)
Packit Service 20376f
{
Packit Service 20376f
	return GIT_IDXENTRY_STAGE(entry);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_entry_is_conflict(const git_index_entry *entry)
Packit Service 20376f
{
Packit Service 20376f
	return (GIT_IDXENTRY_STAGE(entry) > 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
typedef struct read_tree_data {
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_vector *old_entries;
Packit Service 20376f
	git_vector *new_entries;
Packit Service 20376f
	git_vector_cmp entry_cmp;
Packit Service 20376f
	git_tree_cache *tree;
Packit Service 20376f
} read_tree_data;
Packit Service 20376f
Packit Service 20376f
static int read_tree_cb(
Packit Service 20376f
	const char *root, const git_tree_entry *tentry, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	read_tree_data *data = payload;
Packit Service 20376f
	git_index_entry *entry = NULL, *old_entry;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	size_t pos;
Packit Service 20376f
Packit Service 20376f
	if (git_tree_entry__is_tree(tentry))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&path, root, tentry->filename) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, NULL, false) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	entry->mode = tentry->attr;
Packit Service 20376f
	git_oid_cpy(&entry->id, git_tree_entry_id(tentry));
Packit Service 20376f
Packit Service 20376f
	/* look for corresponding old entry and copy data to new entry */
Packit Service 20376f
	if (data->old_entries != NULL &&
Packit Service 20376f
		!index_find_in_entries(
Packit Service 20376f
			&pos, data->old_entries, data->entry_cmp, path.ptr, 0, 0) &&
Packit Service 20376f
		(old_entry = git_vector_get(data->old_entries, pos)) != NULL &&
Packit Service 20376f
		entry->mode == old_entry->mode &&
Packit Service 20376f
		git_oid_equal(&entry->id, &old_entry->id))
Packit Service 20376f
	{
Packit Service 20376f
		index_entry_cpy(entry, old_entry);
Packit Service 20376f
		entry->flags_extended = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	index_entry_adjust_namemask(entry, path.size);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	if (git_vector_insert(data->new_entries, entry) < 0) {
Packit Service 20376f
		index_entry_free(entry);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_read_tree(git_index *index, const git_tree *tree)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_vector entries = GIT_VECTOR_INIT;
Packit Service 20376f
	git_idxmap *entries_map;
Packit Service 20376f
	read_tree_data data;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	git_index_entry *e;
Packit Service 20376f
Packit Service 20376f
	if (git_idxmap_alloc(&entries_map) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	git_vector_set_cmp(&entries, index->entries._cmp); /* match sort */
Packit Service 20376f
Packit Service 20376f
	data.index = index;
Packit Service 20376f
	data.old_entries = &index->entries;
Packit Service 20376f
	data.new_entries = &entries;
Packit Service 20376f
	data.entry_cmp   = index->entries_search;
Packit Service 20376f
Packit Service 20376f
	index->tree = NULL;
Packit Service 20376f
	git_pool_clear(&index->tree_pool);
Packit Service 20376f
Packit Service 20376f
	git_vector_sort(&index->entries);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (index->ignore_case)
Packit Service 20376f
		git_idxmap_icase_resize((khash_t(idxicase) *) entries_map, entries.length);
Packit Service 20376f
	else
Packit Service 20376f
		git_idxmap_resize(entries_map, entries.length);
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&entries, i, e) {
Packit Service 20376f
		INSERT_IN_MAP_EX(index, entries_map, e, &error);
Packit Service 20376f
Packit Service 20376f
		if (error < 0) {
Packit Service 20376f
			giterr_set(GITERR_INDEX, "failed to insert entry into map");
Packit Service 20376f
			return error;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = 0;
Packit Service 20376f
Packit Service 20376f
	git_vector_sort(&entries);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_index_clear(index)) < 0) {
Packit Service 20376f
		/* well, this isn't good */;
Packit Service 20376f
	} else {
Packit Service 20376f
		git_vector_swap(&entries, &index->entries);
Packit Service 20376f
		entries_map = git__swap(index->entries_map, entries_map);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_vector_free(&entries);
Packit Service 20376f
	git_idxmap_free(entries_map);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	error = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int git_index_read_iterator(
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	git_iterator *new_iterator,
Packit Service 20376f
	size_t new_length_hint)
Packit Service 20376f
{
Packit Service 20376f
	git_vector new_entries = GIT_VECTOR_INIT,
Packit Service 20376f
		remove_entries = GIT_VECTOR_INIT;
Packit Service 20376f
	git_idxmap *new_entries_map = NULL;
Packit Service 20376f
	git_iterator *index_iterator = NULL;
Packit Service 20376f
	git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
Packit Service 20376f
	const git_index_entry *old_entry, *new_entry;
Packit Service 20376f
	git_index_entry *entry;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE));
Packit Service 20376f
Packit Service 20376f
	if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 ||
Packit Service 20376f
		(error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 ||
Packit Service 20376f
		(error = git_idxmap_alloc(&new_entries_map)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (index->ignore_case && new_length_hint)
Packit Service 20376f
		git_idxmap_icase_resize((khash_t(idxicase) *) new_entries_map, new_length_hint);
Packit Service 20376f
	else if (new_length_hint)
Packit Service 20376f
		git_idxmap_resize(new_entries_map, new_length_hint);
Packit Service 20376f
Packit Service 20376f
	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
Packit Service 20376f
		GIT_ITERATOR_INCLUDE_CONFLICTS;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_iterator_for_index(&index_iterator,
Packit Service 20376f
			git_index_owner(index), index, &opts)) < 0 ||
Packit Service 20376f
		((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
Packit Service 20376f
			error != GIT_ITEROVER) ||
Packit Service 20376f
		((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
Packit Service 20376f
			error != GIT_ITEROVER))
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	while (true) {
Packit Service 20376f
		git_index_entry
Packit Service 20376f
			*dup_entry = NULL,
Packit Service 20376f
			*add_entry = NULL,
Packit Service 20376f
			*remove_entry = NULL;
Packit Service 20376f
		int diff;
Packit Service 20376f
Packit Service 20376f
		error = 0;
Packit Service 20376f
Packit Service 20376f
		if (old_entry && new_entry)
Packit Service 20376f
			diff = git_index_entry_cmp(old_entry, new_entry);
Packit Service 20376f
		else if (!old_entry && new_entry)
Packit Service 20376f
			diff = 1;
Packit Service 20376f
		else if (old_entry && !new_entry)
Packit Service 20376f
			diff = -1;
Packit Service 20376f
		else
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		if (diff < 0) {
Packit Service 20376f
			remove_entry = (git_index_entry *)old_entry;
Packit Service 20376f
		} else if (diff > 0) {
Packit Service 20376f
			dup_entry = (git_index_entry *)new_entry;
Packit Service 20376f
		} else {
Packit Service 20376f
			/* Path and stage are equal, if the OID is equal, keep it to
Packit Service 20376f
			 * keep the stat cache data.
Packit Service 20376f
			 */
Packit Service 20376f
			if (git_oid_equal(&old_entry->id, &new_entry->id) &&
Packit Service 20376f
				old_entry->mode == new_entry->mode) {
Packit Service 20376f
				add_entry = (git_index_entry *)old_entry;
Packit Service 20376f
			} else {
Packit Service 20376f
				dup_entry = (git_index_entry *)new_entry;
Packit Service 20376f
				remove_entry = (git_index_entry *)old_entry;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (dup_entry) {
Packit Service 20376f
			if ((error = index_entry_dup_nocache(&add_entry, index, dup_entry)) < 0)
Packit Service 20376f
				goto done;
Packit Service 20376f
Packit Service 20376f
			index_entry_adjust_namemask(add_entry,
Packit Service 20376f
				((struct entry_internal *)add_entry)->pathlen);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* invalidate this path in the tree cache if this is new (to
Packit Service 20376f
		 * invalidate the parent trees)
Packit Service 20376f
		 */
Packit Service 20376f
		if (dup_entry && !remove_entry && index->tree)
Packit Service 20376f
			git_tree_cache_invalidate_path(index->tree, dup_entry->path);
Packit Service 20376f
Packit Service 20376f
		if (add_entry) {
Packit Service 20376f
			if ((error = git_vector_insert(&new_entries, add_entry)) == 0)
Packit Service 20376f
				INSERT_IN_MAP_EX(index, new_entries_map, add_entry, &error);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (remove_entry && error >= 0)
Packit Service 20376f
			error = git_vector_insert(&remove_entries, remove_entry);
Packit Service 20376f
Packit Service 20376f
		if (error < 0) {
Packit Service 20376f
			giterr_set(GITERR_INDEX, "failed to insert entry");
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (diff <= 0) {
Packit Service 20376f
			if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 &&
Packit Service 20376f
				error != GIT_ITEROVER)
Packit Service 20376f
				goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (diff >= 0) {
Packit Service 20376f
			if ((error = git_iterator_advance(&new_entry, new_iterator)) < 0 &&
Packit Service 20376f
				error != GIT_ITEROVER)
Packit Service 20376f
				goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_index_name_clear(index);
Packit Service 20376f
	git_index_reuc_clear(index);
Packit Service 20376f
Packit Service 20376f
	git_vector_swap(&new_entries, &index->entries);
Packit Service 20376f
	new_entries_map = git__swap(index->entries_map, new_entries_map);
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&remove_entries, i, entry) {
Packit Service 20376f
		if (index->tree)
Packit Service 20376f
			git_tree_cache_invalidate_path(index->tree, entry->path);
Packit Service 20376f
Packit Service 20376f
		index_entry_free(entry);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	clear_uptodate(index);
Packit Service 20376f
Packit Service 20376f
	error = 0;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_idxmap_free(new_entries_map);
Packit Service 20376f
	git_vector_free(&new_entries);
Packit Service 20376f
	git_vector_free(&remove_entries);
Packit Service 20376f
	git_iterator_free(index_iterator);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_index_read_index(
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_index *new_index)
Packit Service 20376f
{
Packit Service 20376f
	git_iterator *new_iterator = NULL;
Packit Service 20376f
	git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
Packit Service 20376f
		GIT_ITERATOR_INCLUDE_CONFLICTS;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_iterator_for_index(&new_iterator,
Packit Service 20376f
		git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 ||
Packit Service 20376f
		(error = git_index_read_iterator(index, new_iterator,
Packit Service 20376f
		new_index->entries.length)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_iterator_free(new_iterator);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_repository *git_index_owner(const git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	return INDEX_OWNER(index);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
enum {
Packit Service 20376f
	INDEX_ACTION_NONE = 0,
Packit Service 20376f
	INDEX_ACTION_UPDATE = 1,
Packit Service 20376f
	INDEX_ACTION_REMOVE = 2,
Packit Service 20376f
	INDEX_ACTION_ADDALL = 3,
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
int git_index_add_all(
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_strarray *paths,
Packit Service 20376f
	unsigned int flags,
Packit Service 20376f
	git_index_matched_path_cb cb,
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_iterator *wditer = NULL;
Packit Service 20376f
	git_pathspec ps;
Packit Service 20376f
	bool no_fnmatch = (flags & GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH) != 0;
Packit Service 20376f
Packit Service 20376f
	assert(index);
Packit Service 20376f
Packit Service 20376f
	repo = INDEX_OWNER(index);
Packit Service 20376f
	if ((error = git_repository__ensure_not_bare(repo, "index add all")) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_pathspec__init(&ps, paths)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* optionally check that pathspec doesn't mention any ignored files */
Packit Service 20376f
	if ((flags & GIT_INDEX_ADD_CHECK_PATHSPEC) != 0 &&
Packit Service 20376f
		(flags & GIT_INDEX_ADD_FORCE) == 0 &&
Packit Service 20376f
		(error = git_ignore__check_pathspec_for_exact_ignores(
Packit Service 20376f
			repo, &ps.pathspec, no_fnmatch)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	error = index_apply_to_wd_diff(index, INDEX_ACTION_ADDALL, paths, flags, cb, payload);
Packit Service 20376f
Packit Service 20376f
	if (error)
Packit Service 20376f
		giterr_set_after_callback(error);
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_iterator_free(wditer);
Packit Service 20376f
	git_pathspec__clear(&ps);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
struct foreach_diff_data {
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	const git_pathspec *pathspec;
Packit Service 20376f
	unsigned int flags;
Packit Service 20376f
	git_index_matched_path_cb cb;
Packit Service 20376f
	void *payload;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static int apply_each_file(const git_diff_delta *delta, float progress, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	struct foreach_diff_data *data = payload;
Packit Service 20376f
	const char *match, *path;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(progress);
Packit Service 20376f
Packit Service 20376f
	path = delta->old_file.path;
Packit Service 20376f
Packit Service 20376f
	/* W