Blame src/diff_generate.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
#include "common.h"
Packit Service 20376f
#include "diff.h"
Packit Service 20376f
#include "diff_generate.h"
Packit Service 20376f
#include "patch_generate.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "config.h"
Packit Service 20376f
#include "attr_file.h"
Packit Service 20376f
#include "filter.h"
Packit Service 20376f
#include "pathspec.h"
Packit Service 20376f
#include "index.h"
Packit Service 20376f
#include "odb.h"
Packit Service 20376f
#include "submodule.h"
Packit Service 20376f
Packit Service 20376f
#define DIFF_FLAG_IS_SET(DIFF,FLAG) \
Packit Service 20376f
	(((DIFF)->base.opts.flags & (FLAG)) != 0)
Packit Service 20376f
#define DIFF_FLAG_ISNT_SET(DIFF,FLAG) \
Packit Service 20376f
	(((DIFF)->base.opts.flags & (FLAG)) == 0)
Packit Service 20376f
#define DIFF_FLAG_SET(DIFF,FLAG,VAL) (DIFF)->base.opts.flags = \
Packit Service 20376f
	(VAL) ? ((DIFF)->base.opts.flags | (FLAG)) : \
Packit Service 20376f
	((DIFF)->base.opts.flags & ~(FLAG))
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	struct git_diff base;
Packit Service 20376f
Packit Service 20376f
	git_vector pathspec;
Packit Service 20376f
Packit Service 20376f
	uint32_t diffcaps;
Packit Service 20376f
	bool index_updated;
Packit Service 20376f
} git_diff_generated;
Packit Service 20376f
Packit Service 20376f
static git_diff_delta *diff_delta__alloc(
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	git_delta_t status,
Packit Service 20376f
	const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_delta *delta = git__calloc(1, sizeof(git_diff_delta));
Packit Service 20376f
	if (!delta)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	delta->old_file.path = git_pool_strdup(&diff->base.pool, path);
Packit Service 20376f
	if (delta->old_file.path == NULL) {
Packit Service 20376f
		git__free(delta);
Packit Service 20376f
		return NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	delta->new_file.path = delta->old_file.path;
Packit Service 20376f
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
Packit Service 20376f
		switch (status) {
Packit Service 20376f
		case GIT_DELTA_ADDED:   status = GIT_DELTA_DELETED; break;
Packit Service 20376f
		case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break;
Packit Service 20376f
		default: break; /* leave other status values alone */
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
	delta->status = status;
Packit Service 20376f
Packit Service 20376f
	return delta;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int diff_insert_delta(
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	git_diff_delta *delta,
Packit Service 20376f
	const char *matched_pathspec)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (diff->base.opts.notify_cb) {
Packit Service 20376f
		error = diff->base.opts.notify_cb(
Packit Service 20376f
			&diff->base, delta, matched_pathspec, diff->base.opts.payload);
Packit Service 20376f
Packit Service 20376f
		if (error) {
Packit Service 20376f
			git__free(delta);
Packit Service 20376f
Packit Service 20376f
			if (error > 0)	/* positive value means to skip this delta */
Packit Service 20376f
				return 0;
Packit Service 20376f
			else			/* negative value means to cancel diff */
Packit Service 20376f
				return giterr_set_after_callback_function(error, "git_diff");
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_vector_insert(&diff->base.deltas, delta)) < 0)
Packit Service 20376f
		git__free(delta);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool diff_pathspec_match(
Packit Service 20376f
	const char **matched_pathspec,
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	const git_index_entry *entry)
Packit Service 20376f
{
Packit Service 20376f
	bool disable_pathspec_match =
Packit Service 20376f
		DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH);
Packit Service 20376f
Packit Service 20376f
	/* If we're disabling fnmatch, then the iterator has already applied
Packit Service 20376f
	 * the filters to the files for us and we don't have to do anything.
Packit Service 20376f
	 * However, this only applies to *files* - the iterator will include
Packit Service 20376f
	 * directories that we need to recurse into when not autoexpanding,
Packit Service 20376f
	 * so we still need to apply the pathspec match to directories.
Packit Service 20376f
	 */
Packit Service 20376f
	if ((S_ISLNK(entry->mode) || S_ISREG(entry->mode)) &&
Packit Service 20376f
		disable_pathspec_match) {
Packit Service 20376f
		*matched_pathspec = entry->path;
Packit Service 20376f
		return true;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return git_pathspec__match(
Packit Service 20376f
		&diff->pathspec, entry->path, disable_pathspec_match,
Packit Service 20376f
		DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE),
Packit Service 20376f
		matched_pathspec, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int diff_delta__from_one(
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	git_delta_t status,
Packit Service 20376f
	const git_index_entry *oitem,
Packit Service 20376f
	const git_index_entry *nitem)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *entry = nitem;
Packit Service 20376f
	bool has_old = false;
Packit Service 20376f
	git_diff_delta *delta;
Packit Service 20376f
	const char *matched_pathspec;
Packit Service 20376f
Packit Service 20376f
	assert((oitem != NULL) ^ (nitem != NULL));
Packit Service 20376f
Packit Service 20376f
	if (oitem) {
Packit Service 20376f
		entry = oitem;
Packit Service 20376f
		has_old = true;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
Packit Service 20376f
		has_old = !has_old;
Packit Service 20376f
Packit Service 20376f
	if ((entry->flags & GIT_IDXENTRY_VALID) != 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (status == GIT_DELTA_IGNORED &&
Packit Service 20376f
		DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (status == GIT_DELTA_UNTRACKED &&
Packit Service 20376f
		DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (status == GIT_DELTA_UNREADABLE &&
Packit Service 20376f
		DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (!diff_pathspec_match(&matched_pathspec, diff, entry))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	delta = diff_delta__alloc(diff, status, entry->path);
Packit Service 20376f
	GITERR_CHECK_ALLOC(delta);
Packit Service 20376f
Packit Service 20376f
	/* This fn is just for single-sided diffs */
Packit Service 20376f
	assert(status != GIT_DELTA_MODIFIED);
Packit Service 20376f
	delta->nfiles = 1;
Packit Service 20376f
Packit Service 20376f
	if (has_old) {
Packit Service 20376f
		delta->old_file.mode = entry->mode;
Packit Service 20376f
		delta->old_file.size = entry->file_size;
Packit Service 20376f
		delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
Packit Service 20376f
		git_oid_cpy(&delta->old_file.id, &entry->id);
Packit Service 20376f
		delta->old_file.id_abbrev = GIT_OID_HEXSZ;
Packit Service 20376f
	} else /* ADDED, IGNORED, UNTRACKED */ {
Packit Service 20376f
		delta->new_file.mode = entry->mode;
Packit Service 20376f
		delta->new_file.size = entry->file_size;
Packit Service 20376f
		delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
Packit Service 20376f
		git_oid_cpy(&delta->new_file.id, &entry->id);
Packit Service 20376f
		delta->new_file.id_abbrev = GIT_OID_HEXSZ;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
Packit Service 20376f
Packit Service 20376f
	if (has_old || !git_oid_iszero(&delta->new_file.id))
Packit Service 20376f
		delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
Packit Service 20376f
Packit Service 20376f
	return diff_insert_delta(diff, delta, matched_pathspec);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int diff_delta__from_two(
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	git_delta_t status,
Packit Service 20376f
	const git_index_entry *old_entry,
Packit Service 20376f
	uint32_t old_mode,
Packit Service 20376f
	const git_index_entry *new_entry,
Packit Service 20376f
	uint32_t new_mode,
Packit Service 20376f
	const git_oid *new_id,
Packit Service 20376f
	const char *matched_pathspec)
Packit Service 20376f
{
Packit Service 20376f
	const git_oid *old_id = &old_entry->id;
Packit Service 20376f
	git_diff_delta *delta;
Packit Service 20376f
	const char *canonical_path = old_entry->path;
Packit Service 20376f
Packit Service 20376f
	if (status == GIT_DELTA_UNMODIFIED &&
Packit Service 20376f
		DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (!new_id)
Packit Service 20376f
		new_id = &new_entry->id;
Packit Service 20376f
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
Packit Service 20376f
		uint32_t temp_mode = old_mode;
Packit Service 20376f
		const git_index_entry *temp_entry = old_entry;
Packit Service 20376f
		const git_oid *temp_id = old_id;
Packit Service 20376f
Packit Service 20376f
		old_entry = new_entry;
Packit Service 20376f
		new_entry = temp_entry;
Packit Service 20376f
		old_mode = new_mode;
Packit Service 20376f
		new_mode = temp_mode;
Packit Service 20376f
		old_id = new_id;
Packit Service 20376f
		new_id = temp_id;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	delta = diff_delta__alloc(diff, status, canonical_path);
Packit Service 20376f
	GITERR_CHECK_ALLOC(delta);
Packit Service 20376f
	delta->nfiles = 2;
Packit Service 20376f
Packit Service 20376f
	if (!git_index_entry_is_conflict(old_entry)) {
Packit Service 20376f
		delta->old_file.size = old_entry->file_size;
Packit Service 20376f
		delta->old_file.mode = old_mode;
Packit Service 20376f
		git_oid_cpy(&delta->old_file.id, old_id);
Packit Service 20376f
		delta->old_file.id_abbrev = GIT_OID_HEXSZ;
Packit Service 20376f
		delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
Packit Service 20376f
			GIT_DIFF_FLAG_EXISTS;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!git_index_entry_is_conflict(new_entry)) {
Packit Service 20376f
		git_oid_cpy(&delta->new_file.id, new_id);
Packit Service 20376f
		delta->new_file.id_abbrev = GIT_OID_HEXSZ;
Packit Service 20376f
		delta->new_file.size = new_entry->file_size;
Packit Service 20376f
		delta->new_file.mode = new_mode;
Packit Service 20376f
		delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
Packit Service 20376f
		delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
Packit Service 20376f
Packit Service 20376f
		if (!git_oid_iszero(&new_entry->id))
Packit Service 20376f
			delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return diff_insert_delta(diff, delta, matched_pathspec);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static git_diff_delta *diff_delta__last_for_item(
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	const git_index_entry *item)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_delta *delta = git_vector_last(&diff->base.deltas);
Packit Service 20376f
	if (!delta)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	switch (delta->status) {
Packit Service 20376f
	case GIT_DELTA_UNMODIFIED:
Packit Service 20376f
	case GIT_DELTA_DELETED:
Packit Service 20376f
		if (git_oid__cmp(&delta->old_file.id, &item->id) == 0)
Packit Service 20376f
			return delta;
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_ADDED:
Packit Service 20376f
		if (git_oid__cmp(&delta->new_file.id, &item->id) == 0)
Packit Service 20376f
			return delta;
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_UNREADABLE:
Packit Service 20376f
	case GIT_DELTA_UNTRACKED:
Packit Service 20376f
		if (diff->base.strcomp(delta->new_file.path, item->path) == 0 &&
Packit Service 20376f
			git_oid__cmp(&delta->new_file.id, &item->id) == 0)
Packit Service 20376f
			return delta;
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_MODIFIED:
Packit Service 20376f
		if (git_oid__cmp(&delta->old_file.id, &item->id) == 0 ||
Packit Service 20376f
			git_oid__cmp(&delta->new_file.id, &item->id) == 0)
Packit Service 20376f
			return delta;
Packit Service 20376f
		break;
Packit Service 20376f
	default:
Packit Service 20376f
		break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static char *diff_strdup_prefix(git_pool *pool, const char *prefix)
Packit Service 20376f
{
Packit Service 20376f
	size_t len = strlen(prefix);
Packit Service 20376f
Packit Service 20376f
	/* append '/' at end if needed */
Packit Service 20376f
	if (len > 0 && prefix[len - 1] != '/')
Packit Service 20376f
		return git_pool_strcat(pool, prefix, "/");
Packit Service 20376f
	else
Packit Service 20376f
		return git_pool_strndup(pool, prefix, len + 1);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(const char *) diff_delta__i2w_path(const git_diff_delta *delta)
Packit Service 20376f
{
Packit Service 20376f
	return delta->old_file.path ?
Packit Service 20376f
		delta->old_file.path : delta->new_file.path;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff_delta__i2w_cmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	const git_diff_delta *da = a, *db = b;
Packit Service 20376f
	int val = strcmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
Packit Service 20376f
	return val ? val : ((int)da->status - (int)db->status);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff_delta__i2w_casecmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	const git_diff_delta *da = a, *db = b;
Packit Service 20376f
	int val = strcasecmp(diff_delta__i2w_path(da), diff_delta__i2w_path(db));
Packit Service 20376f
	return val ? val : ((int)da->status - (int)db->status);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_diff_delta__should_skip(
Packit Service 20376f
	const git_diff_options *opts, const git_diff_delta *delta)
Packit Service 20376f
{
Packit Service 20376f
	uint32_t flags = opts ? opts->flags : 0;
Packit Service 20376f
Packit Service 20376f
	if (delta->status == GIT_DELTA_UNMODIFIED &&
Packit Service 20376f
		(flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (delta->status == GIT_DELTA_IGNORED &&
Packit Service 20376f
		(flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (delta->status == GIT_DELTA_UNTRACKED &&
Packit Service 20376f
		(flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (delta->status == GIT_DELTA_UNREADABLE &&
Packit Service 20376f
		(flags & GIT_DIFF_INCLUDE_UNREADABLE) == 0)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	return false;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static const char *diff_mnemonic_prefix(
Packit Service 20376f
	git_iterator_type_t type, bool left_side)
Packit Service 20376f
{
Packit Service 20376f
	const char *pfx = "";
Packit Service 20376f
Packit Service 20376f
	switch (type) {
Packit Service 20376f
	case GIT_ITERATOR_TYPE_EMPTY:   pfx = "c"; break;
Packit Service 20376f
	case GIT_ITERATOR_TYPE_TREE:    pfx = "c"; break;
Packit Service 20376f
	case GIT_ITERATOR_TYPE_INDEX:   pfx = "i"; break;
Packit Service 20376f
	case GIT_ITERATOR_TYPE_WORKDIR: pfx = "w"; break;
Packit Service 20376f
	case GIT_ITERATOR_TYPE_FS:      pfx = left_side ? "1" : "2"; break;
Packit Service 20376f
	default: break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* note: without a deeper look at pathspecs, there is no easy way
Packit Service 20376f
	 * to get the (o)bject / (w)ork tree mnemonics working...
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
	return pfx;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_diff__set_ignore_case(git_diff *diff, bool ignore_case)
Packit Service 20376f
{
Packit Service 20376f
	if (!ignore_case) {
Packit Service 20376f
		diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE;
Packit Service 20376f
Packit Service 20376f
		diff->strcomp    = git__strcmp;
Packit Service 20376f
		diff->strncomp   = git__strncmp;
Packit Service 20376f
		diff->pfxcomp    = git__prefixcmp;
Packit Service 20376f
		diff->entrycomp  = git_diff__entry_cmp;
Packit Service 20376f
Packit Service 20376f
		git_vector_set_cmp(&diff->deltas, git_diff_delta__cmp);
Packit Service 20376f
	} else {
Packit Service 20376f
		diff->opts.flags |= GIT_DIFF_IGNORE_CASE;
Packit Service 20376f
Packit Service 20376f
		diff->strcomp    = git__strcasecmp;
Packit Service 20376f
		diff->strncomp   = git__strncasecmp;
Packit Service 20376f
		diff->pfxcomp    = git__prefixcmp_icase;
Packit Service 20376f
		diff->entrycomp  = git_diff__entry_icmp;
Packit Service 20376f
Packit Service 20376f
		git_vector_set_cmp(&diff->deltas, git_diff_delta__casecmp);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_sort(&diff->deltas);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void diff_generated_free(git_diff *d)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_generated *diff = (git_diff_generated *)d;
Packit Service 20376f
Packit Service 20376f
	git_vector_free_deep(&diff->base.deltas);
Packit Service 20376f
Packit Service 20376f
	git_pathspec__vfree(&diff->pathspec);
Packit Service 20376f
	git_pool_clear(&diff->base.pool);
Packit Service 20376f
Packit Service 20376f
	git__memzero(diff, sizeof(*diff));
Packit Service 20376f
	git__free(diff);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static git_diff_generated *diff_generated_alloc(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_iterator *old_iter,
Packit Service 20376f
	git_iterator *new_iter)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_generated *diff;
Packit Service 20376f
	git_diff_options dflt = GIT_DIFF_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	assert(repo && old_iter && new_iter);
Packit Service 20376f
Packit Service 20376f
	if ((diff = git__calloc(1, sizeof(git_diff_generated))) == NULL)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(diff);
Packit Service 20376f
	diff->base.type = GIT_DIFF_TYPE_GENERATED;
Packit Service 20376f
	diff->base.repo = repo;
Packit Service 20376f
	diff->base.old_src = old_iter->type;
Packit Service 20376f
	diff->base.new_src = new_iter->type;
Packit Service 20376f
	diff->base.patch_fn = git_patch_generated_from_diff;
Packit Service 20376f
	diff->base.free_fn = diff_generated_free;
Packit Service 20376f
	memcpy(&diff->base.opts, &dflt, sizeof(git_diff_options));
Packit Service 20376f
Packit Service 20376f
	git_pool_init(&diff->base.pool, 1);
Packit Service 20376f
Packit Service 20376f
	if (git_vector_init(&diff->base.deltas, 0, git_diff_delta__cmp) < 0) {
Packit Service 20376f
		git_diff_free(&diff->base);
Packit Service 20376f
		return NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Use case-insensitive compare if either iterator has
Packit Service 20376f
	 * the ignore_case bit set */
Packit Service 20376f
	git_diff__set_ignore_case(
Packit Service 20376f
		&diff->base,
Packit Service 20376f
		git_iterator_ignore_case(old_iter) ||
Packit Service 20376f
		git_iterator_ignore_case(new_iter));
Packit Service 20376f
Packit Service 20376f
	return diff;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int diff_generated_apply_options(
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	const git_diff_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_config *cfg = NULL;
Packit Service 20376f
	git_repository *repo = diff->base.repo;
Packit Service 20376f
	git_pool *pool = &diff->base.pool;
Packit Service 20376f
	int val;
Packit Service 20376f
Packit Service 20376f
	if (opts) {
Packit Service 20376f
		/* copy user options (except case sensitivity info from iterators) */
Packit Service 20376f
		bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE);
Packit Service 20376f
		memcpy(&diff->base.opts, opts, sizeof(diff->base.opts));
Packit Service 20376f
		DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase);
Packit Service 20376f
Packit Service 20376f
		/* initialize pathspec from options */
Packit Service 20376f
		if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES))
Packit Service 20376f
		diff->base.opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
Packit Service 20376f
Packit Service 20376f
	/* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT))
Packit Service 20376f
		diff->base.opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
Packit Service 20376f
Packit Service 20376f
	/* load config values that affect diff behavior */
Packit Service 20376f
	if ((val = git_repository_config_snapshot(&cfg, repo)) < 0)
Packit Service 20376f
		return val;
Packit Service 20376f
Packit Service 20376f
	if (!git_config__cvar(&val, cfg, GIT_CVAR_SYMLINKS) && val)
Packit Service 20376f
		diff->diffcaps |= GIT_DIFFCAPS_HAS_SYMLINKS;
Packit Service 20376f
Packit Service 20376f
	if (!git_config__cvar(&val, cfg, GIT_CVAR_IGNORESTAT) && val)
Packit Service 20376f
		diff->diffcaps |= GIT_DIFFCAPS_IGNORE_STAT;
Packit Service 20376f
Packit Service 20376f
	if ((diff->base.opts.flags & GIT_DIFF_IGNORE_FILEMODE) == 0 &&
Packit Service 20376f
		!git_config__cvar(&val, cfg, GIT_CVAR_FILEMODE) && val)
Packit Service 20376f
		diff->diffcaps |= GIT_DIFFCAPS_TRUST_MODE_BITS;
Packit Service 20376f
Packit Service 20376f
	if (!git_config__cvar(&val, cfg, GIT_CVAR_TRUSTCTIME) && val)
Packit Service 20376f
		diff->diffcaps |= GIT_DIFFCAPS_TRUST_CTIME;
Packit Service 20376f
Packit Service 20376f
	/* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
Packit Service 20376f
Packit Service 20376f
	/* If not given explicit `opts`, check `diff.xyz` configs */
Packit Service 20376f
	if (!opts) {
Packit Service 20376f
		int context = git_config__get_int_force(cfg, "diff.context", 3);
Packit Service 20376f
		diff->base.opts.context_lines = context >= 0 ? (uint32_t)context : 3;
Packit Service 20376f
Packit Service 20376f
		/* add other defaults here */
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Reverse src info if diff is reversed */
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
Packit Service 20376f
		git_iterator_type_t tmp_src = diff->base.old_src;
Packit Service 20376f
		diff->base.old_src = diff->base.new_src;
Packit Service 20376f
		diff->base.new_src = tmp_src;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Unset UPDATE_INDEX unless diffing workdir and index */
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) &&
Packit Service 20376f
		(!(diff->base.old_src == GIT_ITERATOR_TYPE_WORKDIR ||
Packit Service 20376f
		   diff->base.new_src == GIT_ITERATOR_TYPE_WORKDIR) ||
Packit Service 20376f
		 !(diff->base.old_src == GIT_ITERATOR_TYPE_INDEX ||
Packit Service 20376f
		   diff->base.new_src == GIT_ITERATOR_TYPE_INDEX)))
Packit Service 20376f
		diff->base.opts.flags &= ~GIT_DIFF_UPDATE_INDEX;
Packit Service 20376f
Packit Service 20376f
	/* if ignore_submodules not explicitly set, check diff config */
Packit Service 20376f
	if (diff->base.opts.ignore_submodules <= 0) {
Packit Service 20376f
		 git_config_entry *entry;
Packit Service 20376f
		git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true);
Packit Service 20376f
Packit Service 20376f
		if (entry && git_submodule_parse_ignore(
Packit Service 20376f
				&diff->base.opts.ignore_submodules, entry->value) < 0)
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
		git_config_entry_free(entry);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* if either prefix is not set, figure out appropriate value */
Packit Service 20376f
	if (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) {
Packit Service 20376f
		const char *use_old = DIFF_OLD_PREFIX_DEFAULT;
Packit Service 20376f
		const char *use_new = DIFF_NEW_PREFIX_DEFAULT;
Packit Service 20376f
Packit Service 20376f
		if (git_config__get_bool_force(cfg, "diff.noprefix", 0))
Packit Service 20376f
			use_old = use_new = "";
Packit Service 20376f
		else if (git_config__get_bool_force(cfg, "diff.mnemonicprefix", 0)) {
Packit Service 20376f
			use_old = diff_mnemonic_prefix(diff->base.old_src, true);
Packit Service 20376f
			use_new = diff_mnemonic_prefix(diff->base.new_src, false);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (!diff->base.opts.old_prefix)
Packit Service 20376f
			diff->base.opts.old_prefix = use_old;
Packit Service 20376f
		if (!diff->base.opts.new_prefix)
Packit Service 20376f
			diff->base.opts.new_prefix = use_new;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* strdup prefix from pool so we're not dependent on external data */
Packit Service 20376f
	diff->base.opts.old_prefix = diff_strdup_prefix(pool, diff->base.opts.old_prefix);
Packit Service 20376f
	diff->base.opts.new_prefix = diff_strdup_prefix(pool, diff->base.opts.new_prefix);
Packit Service 20376f
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
Packit Service 20376f
		const char *tmp_prefix = diff->base.opts.old_prefix;
Packit Service 20376f
		diff->base.opts.old_prefix  = diff->base.opts.new_prefix;
Packit Service 20376f
		diff->base.opts.new_prefix  = tmp_prefix;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_config_free(cfg);
Packit Service 20376f
Packit Service 20376f
	/* check strdup results for error */
Packit Service 20376f
	return (!diff->base.opts.old_prefix || !diff->base.opts.new_prefix) ? -1 : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff__oid_for_file(
Packit Service 20376f
	git_oid *out,
Packit Service 20376f
	git_diff *diff,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	uint16_t mode,
Packit Service 20376f
	git_off_t size)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry entry;
Packit Service 20376f
Packit Service 20376f
	memset(&entry, 0, sizeof(entry));
Packit Service 20376f
	entry.mode = mode;
Packit Service 20376f
	entry.file_size = size;
Packit Service 20376f
	entry.path = (char *)path;
Packit Service 20376f
Packit Service 20376f
	return git_diff__oid_for_entry(out, diff, &entry, mode, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff__oid_for_entry(
Packit Service 20376f
	git_oid *out,
Packit Service 20376f
	git_diff *d,
Packit Service 20376f
	const git_index_entry *src,
Packit Service 20376f
	uint16_t mode,
Packit Service 20376f
	const git_oid *update_match)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_generated *diff;
Packit Service 20376f
	git_buf full_path = GIT_BUF_INIT;
Packit Service 20376f
	git_index_entry entry = *src;
Packit Service 20376f
	git_filter_list *fl = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(d->type == GIT_DIFF_TYPE_GENERATED);
Packit Service 20376f
	diff = (git_diff_generated *)d;
Packit Service 20376f
Packit Service 20376f
	memset(out, 0, sizeof(*out));
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&full_path,
Packit Service 20376f
		git_repository_workdir(diff->base.repo), entry.path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (!mode) {
Packit Service 20376f
		struct stat st;
Packit Service 20376f
Packit Service 20376f
		diff->base.perf.stat_calls++;
Packit Service 20376f
Packit Service 20376f
		if (p_stat(full_path.ptr, &st) < 0) {
Packit Service 20376f
			error = git_path_set_error(errno, entry.path, "stat");
Packit Service 20376f
			git_buf_free(&full_path);
Packit Service 20376f
			return error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_index_entry__init_from_stat(&entry,
Packit Service 20376f
			&st, (diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) != 0);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* calculate OID for file if possible */
Packit Service 20376f
	if (S_ISGITLINK(mode)) {
Packit Service 20376f
		git_submodule *sm;
Packit Service 20376f
Packit Service 20376f
		if (!git_submodule_lookup(&sm, diff->base.repo, entry.path)) {
Packit Service 20376f
			const git_oid *sm_oid = git_submodule_wd_id(sm);
Packit Service 20376f
			if (sm_oid)
Packit Service 20376f
				git_oid_cpy(out, sm_oid);
Packit Service 20376f
			git_submodule_free(sm);
Packit Service 20376f
		} else {
Packit Service 20376f
			/* if submodule lookup failed probably just in an intermediate
Packit Service 20376f
			 * state where some init hasn't happened, so ignore the error
Packit Service 20376f
			 */
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
		}
Packit Service 20376f
	} else if (S_ISLNK(mode)) {
Packit Service 20376f
		error = git_odb__hashlink(out, full_path.ptr);
Packit Service 20376f
		diff->base.perf.oid_calculations++;
Packit Service 20376f
	} else if (!git__is_sizet(entry.file_size)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "file size overflow (for 32-bits) on '%s'",
Packit Service 20376f
			entry.path);
Packit Service 20376f
		error = -1;
Packit Service 20376f
	} else if (!(error = git_filter_list_load(&fl,
Packit Service 20376f
		diff->base.repo, NULL, entry.path,
Packit Service 20376f
		GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)))
Packit Service 20376f
	{
Packit Service 20376f
		int fd = git_futils_open_ro(full_path.ptr);
Packit Service 20376f
		if (fd < 0)
Packit Service 20376f
			error = fd;
Packit Service 20376f
		else {
Packit Service 20376f
			error = git_odb__hashfd_filtered(
Packit Service 20376f
				out, fd, (size_t)entry.file_size, GIT_OBJ_BLOB, fl);
Packit Service 20376f
			p_close(fd);
Packit Service 20376f
			diff->base.perf.oid_calculations++;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_filter_list_free(fl);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* update index for entry if requested */
Packit Service 20376f
	if (!error && update_match && git_oid_equal(out, update_match)) {
Packit Service 20376f
		git_index *idx;
Packit Service 20376f
		git_index_entry updated_entry;
Packit Service 20376f
Packit Service 20376f
		memcpy(&updated_entry, &entry, sizeof(git_index_entry));
Packit Service 20376f
		updated_entry.mode = mode;
Packit Service 20376f
		git_oid_cpy(&updated_entry.id, out);
Packit Service 20376f
Packit Service 20376f
		if (!(error = git_repository_index__weakptr(&idx,
Packit Service 20376f
			diff->base.repo))) {
Packit Service 20376f
			error = git_index_add(idx, &updated_entry);
Packit Service 20376f
			diff->index_updated = true;
Packit Service 20376f
		}
Packit Service 20376f
 	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&full_path);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_iterator *old_iter;
Packit Service 20376f
	git_iterator *new_iter;
Packit Service 20376f
	const git_index_entry *oitem;
Packit Service 20376f
	const git_index_entry *nitem;
Packit Service 20376f
} diff_in_progress;
Packit Service 20376f
Packit Service 20376f
#define MODE_BITS_MASK 0000777
Packit Service 20376f
Packit Service 20376f
static int maybe_modified_submodule(
Packit Service 20376f
	git_delta_t *status,
Packit Service 20376f
	git_oid *found_oid,
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	diff_in_progress *info)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_submodule *sub;
Packit Service 20376f
	unsigned int sm_status = 0;
Packit Service 20376f
	git_submodule_ignore_t ign = diff->base.opts.ignore_submodules;
Packit Service 20376f
Packit Service 20376f
	*status = GIT_DELTA_UNMODIFIED;
Packit Service 20376f
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) ||
Packit Service 20376f
		ign == GIT_SUBMODULE_IGNORE_ALL)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_submodule_lookup(
Packit Service 20376f
			&sub, diff->base.repo, info->nitem->path)) < 0) {
Packit Service 20376f
Packit Service 20376f
		/* GIT_EEXISTS means dir with .git in it was found - ignore it */
Packit Service 20376f
		if (error == GIT_EEXISTS) {
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			error = 0;
Packit Service 20376f
		}
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL)
Packit Service 20376f
		/* ignore it */;
Packit Service 20376f
	else if ((error = git_submodule__status(
Packit Service 20376f
			&sm_status, NULL, NULL, found_oid, sub, ign)) < 0)
Packit Service 20376f
		/* return error below */;
Packit Service 20376f
Packit Service 20376f
	/* check IS_WD_UNMODIFIED because this case is only used
Packit Service 20376f
	 * when the new side of the diff is the working directory
Packit Service 20376f
	 */
Packit Service 20376f
	else if (!GIT_SUBMODULE_STATUS_IS_WD_UNMODIFIED(sm_status))
Packit Service 20376f
		*status = GIT_DELTA_MODIFIED;
Packit Service 20376f
Packit Service 20376f
	/* now that we have a HEAD OID, check if HEAD moved */
Packit Service 20376f
	else if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
Packit Service 20376f
		!git_oid_equal(&info->oitem->id, found_oid))
Packit Service 20376f
		*status = GIT_DELTA_MODIFIED;
Packit Service 20376f
Packit Service 20376f
	git_submodule_free(sub);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int maybe_modified(
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	diff_in_progress *info)
Packit Service 20376f
{
Packit Service 20376f
	git_oid noid;
Packit Service 20376f
	git_delta_t status = GIT_DELTA_MODIFIED;
Packit Service 20376f
	const git_index_entry *oitem = info->oitem;
Packit Service 20376f
	const git_index_entry *nitem = info->nitem;
Packit Service 20376f
	unsigned int omode = oitem->mode;
Packit Service 20376f
	unsigned int nmode = nitem->mode;
Packit Service 20376f
	bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
Packit Service 20376f
	bool modified_uncertain = false;
Packit Service 20376f
	const char *matched_pathspec;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (!diff_pathspec_match(&matched_pathspec, diff, oitem))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	memset(&noid, 0, sizeof(noid));
Packit Service 20376f
Packit Service 20376f
	/* on platforms with no symlinks, preserve mode of existing symlinks */
Packit Service 20376f
	if (S_ISLNK(omode) && S_ISREG(nmode) && new_is_workdir &&
Packit Service 20376f
		!(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS))
Packit Service 20376f
		nmode = omode;
Packit Service 20376f
Packit Service 20376f
	/* on platforms with no execmode, just preserve old mode */
Packit Service 20376f
	if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) &&
Packit Service 20376f
		(nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) &&
Packit Service 20376f
		new_is_workdir)
Packit Service 20376f
		nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK);
Packit Service 20376f
Packit Service 20376f
	/* if one side is a conflict, mark the whole delta as conflicted */
Packit Service 20376f
	if (git_index_entry_is_conflict(oitem) ||
Packit Service 20376f
			git_index_entry_is_conflict(nitem)) {
Packit Service 20376f
		status = GIT_DELTA_CONFLICTED;
Packit Service 20376f
Packit Service 20376f
	/* support "assume unchanged" (poorly, b/c we still stat everything) */
Packit Service 20376f
	} else if ((oitem->flags & GIT_IDXENTRY_VALID) != 0) {
Packit Service 20376f
		status = GIT_DELTA_UNMODIFIED;
Packit Service 20376f
Packit Service 20376f
	/* support "skip worktree" index bit */
Packit Service 20376f
	} else if ((oitem->flags_extended & GIT_IDXENTRY_SKIP_WORKTREE) != 0) {
Packit Service 20376f
		status = GIT_DELTA_UNMODIFIED;
Packit Service 20376f
Packit Service 20376f
	/* if basic type of file changed, then split into delete and add */
Packit Service 20376f
	} else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
Packit Service 20376f
		if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE)) {
Packit Service 20376f
			status = GIT_DELTA_TYPECHANGE;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		else if (nmode == GIT_FILEMODE_UNREADABLE) {
Packit Service 20376f
			if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
Packit Service 20376f
				error = diff_delta__from_one(diff, GIT_DELTA_UNREADABLE, NULL, nitem);
Packit Service 20376f
			return error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		else {
Packit Service 20376f
			if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
Packit Service 20376f
				error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
Packit Service 20376f
			return error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
	/* if oids and modes match (and are valid), then file is unmodified */
Packit Service 20376f
	} else if (git_oid_equal(&oitem->id, &nitem->id) &&
Packit Service 20376f
			 omode == nmode &&
Packit Service 20376f
			 !git_oid_iszero(&oitem->id)) {
Packit Service 20376f
		status = GIT_DELTA_UNMODIFIED;
Packit Service 20376f
Packit Service 20376f
	/* if we have an unknown OID and a workdir iterator, then check some
Packit Service 20376f
	 * circumstances that can accelerate things or need special handling
Packit Service 20376f
	 */
Packit Service 20376f
	} else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
Packit Service 20376f
		bool use_ctime =
Packit Service 20376f
			((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
Packit Service 20376f
		git_index *index = git_iterator_index(info->new_iter);
Packit Service 20376f
Packit Service 20376f
		status = GIT_DELTA_UNMODIFIED;
Packit Service 20376f
Packit Service 20376f
		if (S_ISGITLINK(nmode)) {
Packit Service 20376f
			if ((error = maybe_modified_submodule(&status, &noid, diff, info)) < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* if the stat data looks different, then mark modified - this just
Packit Service 20376f
		 * means that the OID will be recalculated below to confirm change
Packit Service 20376f
		 */
Packit Service 20376f
		else if (omode != nmode || oitem->file_size != nitem->file_size) {
Packit Service 20376f
			status = GIT_DELTA_MODIFIED;
Packit Service 20376f
			modified_uncertain =
Packit Service 20376f
				(oitem->file_size <= 0 && nitem->file_size > 0);
Packit Service 20376f
		}
Packit Service 20376f
		else if (!git_index_time_eq(&oitem->mtime, &nitem->mtime) ||
Packit Service 20376f
			(use_ctime && !git_index_time_eq(&oitem->ctime, &nitem->ctime)) ||
Packit Service 20376f
			oitem->ino != nitem->ino ||
Packit Service 20376f
			oitem->uid != nitem->uid ||
Packit Service 20376f
			oitem->gid != nitem->gid ||
Packit Service 20376f
			git_index_entry_newer_than_index(nitem, index))
Packit Service 20376f
		{
Packit Service 20376f
			status = GIT_DELTA_MODIFIED;
Packit Service 20376f
			modified_uncertain = true;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
	/* if mode is GITLINK and submodules are ignored, then skip */
Packit Service 20376f
	} else if (S_ISGITLINK(nmode) &&
Packit Service 20376f
			 DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES)) {
Packit Service 20376f
		status = GIT_DELTA_UNMODIFIED;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* if we got here and decided that the files are modified, but we
Packit Service 20376f
	 * haven't calculated the OID of the new item, then calculate it now
Packit Service 20376f
	 */
Packit Service 20376f
	if (modified_uncertain && git_oid_iszero(&nitem->id)) {
Packit Service 20376f
		const git_oid *update_check =
Packit Service 20376f
			DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && omode == nmode ?
Packit Service 20376f
			&oitem->id : NULL;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_diff__oid_for_entry(
Packit Service 20376f
				&noid, &diff->base, nitem, nmode, update_check)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		/* if oid matches, then mark unmodified (except submodules, where
Packit Service 20376f
		 * the filesystem content may be modified even if the oid still
Packit Service 20376f
		 * matches between the index and the workdir HEAD)
Packit Service 20376f
		 */
Packit Service 20376f
		if (omode == nmode && !S_ISGITLINK(omode) &&
Packit Service 20376f
			git_oid_equal(&oitem->id, &noid))
Packit Service 20376f
			status = GIT_DELTA_UNMODIFIED;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* If we want case changes, then break this into a delete of the old
Packit Service 20376f
	 * and an add of the new so that consumers can act accordingly (eg,
Packit Service 20376f
	 * checkout will update the case on disk.)
Packit Service 20376f
	 */
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE) &&
Packit Service 20376f
		DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_CASECHANGE) &&
Packit Service 20376f
		strcmp(oitem->path, nitem->path) != 0) {
Packit Service 20376f
Packit Service 20376f
		if (!(error = diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem, NULL)))
Packit Service 20376f
			error = diff_delta__from_one(diff, GIT_DELTA_ADDED, NULL, nitem);
Packit Service 20376f
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return diff_delta__from_two(
Packit Service 20376f
		diff, status, oitem, omode, nitem, nmode,
Packit Service 20376f
		git_oid_iszero(&noid) ? NULL : &noid, matched_pathspec);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool entry_is_prefixed(
Packit Service 20376f
	git_diff_generated *diff,
Packit Service 20376f
	const git_index_entry *item,
Packit Service 20376f
	const git_index_entry *prefix_item)
Packit Service 20376f
{
Packit Service 20376f
	size_t pathlen;
Packit Service 20376f
Packit Service 20376f
	if (!item || diff->base.pfxcomp(item->path, prefix_item->path) != 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	pathlen = strlen(prefix_item->path);
Packit Service 20376f
Packit Service 20376f
	return (prefix_item->path[pathlen - 1] == '/' ||
Packit Service 20376f
			item->path[pathlen] == '\0' ||
Packit Service 20376f
			item->path[pathlen] == '/');
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int iterator_current(
Packit Service 20376f
	const git_index_entry **entry,
Packit Service 20376f
	git_iterator *iterator)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_iterator_current(entry, iterator)) == GIT_ITEROVER) {
Packit Service 20376f
		*entry = NULL;
Packit Service 20376f
		error = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int iterator_advance(
Packit Service 20376f
	const git_index_entry **entry,
Packit Service 20376f
	git_iterator *iterator)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *prev_entry = *entry;
Packit Service 20376f
	int cmp, error;
Packit Service 20376f
Packit Service 20376f
	/* if we're looking for conflicts, we only want to report
Packit Service 20376f
	 * one conflict for each file, instead of all three sides.
Packit Service 20376f
	 * so if this entry is a conflict for this file, and the
Packit Service 20376f
	 * previous one was a conflict for the same file, skip it.
Packit Service 20376f
	 */
Packit Service 20376f
	while ((error = git_iterator_advance(entry, iterator)) == 0) {
Packit Service 20376f
		if (!(iterator->flags & GIT_ITERATOR_INCLUDE_CONFLICTS) ||
Packit Service 20376f
			!git_index_entry_is_conflict(prev_entry) ||
Packit Service 20376f
			!git_index_entry_is_conflict(*entry))
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		cmp = (iterator->flags & GIT_ITERATOR_IGNORE_CASE) ?
Packit Service 20376f
			strcasecmp(prev_entry->path, (*entry)->path) :
Packit Service 20376f
			strcmp(prev_entry->path, (*entry)->path);
Packit Service 20376f
Packit Service 20376f
		if (cmp)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ITEROVER) {
Packit Service 20376f
		*entry = NULL;
Packit Service 20376f
		error = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int iterator_advance_into(
Packit Service 20376f
	const git_index_entry **entry,
Packit Service 20376f
	git_iterator *iterator)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_iterator_advance_into(entry, iterator)) == GIT_ITEROVER) {
Packit Service 20376f
		*entry = NULL;
Packit Service 20376f
		error = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int iterator_advance_over(
Packit Service 20376f
	const git_index_entry **entry,
Packit Service 20376f
	git_iterator_status_t *status,
Packit Service 20376f
	git_iterator *iterator)
Packit Service 20376f
{
Packit Service 20376f
	int error = git_iterator_advance_over(entry, status, iterator);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ITEROVER) {
Packit Service 20376f
		*entry = NULL;
Packit Service 20376f
		error = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int handle_unmatched_new_item(
Packit Service 20376f
	git_diff_generated *diff, diff_in_progress *info)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	const git_index_entry *nitem = info->nitem;
Packit Service 20376f
	git_delta_t delta_type = GIT_DELTA_UNTRACKED;
Packit Service 20376f
	bool contains_oitem;
Packit Service 20376f
Packit Service 20376f
	/* check if this is a prefix of the other side */
Packit Service 20376f
	contains_oitem = entry_is_prefixed(diff, info->oitem, nitem);
Packit Service 20376f
Packit Service 20376f
	/* update delta_type if this item is conflicted */
Packit Service 20376f
	if (git_index_entry_is_conflict(nitem))
Packit Service 20376f
		delta_type = GIT_DELTA_CONFLICTED;
Packit Service 20376f
Packit Service 20376f
	/* update delta_type if this item is ignored */
Packit Service 20376f
	else if (git_iterator_current_is_ignored(info->new_iter))
Packit Service 20376f
		delta_type = GIT_DELTA_IGNORED;
Packit Service 20376f
Packit Service 20376f
	if (nitem->mode == GIT_FILEMODE_TREE) {
Packit Service 20376f
		bool recurse_into_dir = contains_oitem;
Packit Service 20376f
Packit Service 20376f
		/* check if user requests recursion into this type of dir */
Packit Service 20376f
		recurse_into_dir = contains_oitem ||
Packit Service 20376f
			(delta_type == GIT_DELTA_UNTRACKED &&
Packit Service 20376f
			 DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) ||
Packit Service 20376f
			(delta_type == GIT_DELTA_IGNORED &&
Packit Service 20376f
			 DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS));
Packit Service 20376f
Packit Service 20376f
		/* do not advance into directories that contain a .git file */
Packit Service 20376f
		if (recurse_into_dir && !contains_oitem) {
Packit Service 20376f
			git_buf *full = NULL;
Packit Service 20376f
			if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
Packit Service 20376f
				return -1;
Packit Service 20376f
			if (full && git_path_contains(full, DOT_GIT)) {
Packit Service 20376f
				/* TODO: warning if not a valid git repository */
Packit Service 20376f
				recurse_into_dir = false;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* still have to look into untracked directories to match core git -
Packit Service 20376f
		 * with no untracked files, directory is treated as ignored
Packit Service 20376f
		 */
Packit Service 20376f
		if (!recurse_into_dir &&
Packit Service 20376f
			delta_type == GIT_DELTA_UNTRACKED &&
Packit Service 20376f
			DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS))
Packit Service 20376f
		{
Packit Service 20376f
			git_diff_delta *last;
Packit Service 20376f
			git_iterator_status_t untracked_state;
Packit Service 20376f
Packit Service 20376f
			/* attempt to insert record for this directory */
Packit Service 20376f
			if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
Packit Service 20376f
				return error;
Packit Service 20376f
Packit Service 20376f
			/* if delta wasn't created (because of rules), just skip ahead */
Packit Service 20376f
			last = diff_delta__last_for_item(diff, nitem);
Packit Service 20376f
			if (!last)
Packit Service 20376f
				return iterator_advance(&info->nitem, info->new_iter);
Packit Service 20376f
Packit Service 20376f
			/* iterate into dir looking for an actual untracked file */
Packit Service 20376f
			if ((error = iterator_advance_over(
Packit Service 20376f
					&info->nitem, &untracked_state, info->new_iter)) < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
Packit Service 20376f
			/* if we found nothing that matched our pathlist filter, exclude */
Packit Service 20376f
			if (untracked_state == GIT_ITERATOR_STATUS_FILTERED) {
Packit Service 20376f
				git_vector_pop(&diff->base.deltas);
Packit Service 20376f
				git__free(last);
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			/* if we found nothing or just ignored items, update the record */
Packit Service 20376f
			if (untracked_state == GIT_ITERATOR_STATUS_IGNORED ||
Packit Service 20376f
				untracked_state == GIT_ITERATOR_STATUS_EMPTY) {
Packit Service 20376f
				last->status = GIT_DELTA_IGNORED;
Packit Service 20376f
Packit Service 20376f
				/* remove the record if we don't want ignored records */
Packit Service 20376f
				if (DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED)) {
Packit Service 20376f
					git_vector_pop(&diff->base.deltas);
Packit Service 20376f
					git__free(last);
Packit Service 20376f
				}
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			return 0;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* try to advance into directory if necessary */
Packit Service 20376f
		if (recurse_into_dir) {
Packit Service 20376f
			error = iterator_advance_into(&info->nitem, info->new_iter);
Packit Service 20376f
Packit Service 20376f
			/* if directory is empty, can't advance into it, so skip it */
Packit Service 20376f
			if (error == GIT_ENOTFOUND) {
Packit Service 20376f
				giterr_clear();
Packit Service 20376f
				error = iterator_advance(&info->nitem, info->new_iter);
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			return error;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	else if (delta_type == GIT_DELTA_IGNORED &&
Packit Service 20376f
		DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS) &&
Packit Service 20376f
		git_iterator_current_tree_is_ignored(info->new_iter))
Packit Service 20376f
		/* item contained in ignored directory, so skip over it */
Packit Service 20376f
		return iterator_advance(&info->nitem, info->new_iter);
Packit Service 20376f
Packit Service 20376f
	else if (info->new_iter->type != GIT_ITERATOR_TYPE_WORKDIR) {
Packit Service 20376f
		if (delta_type != GIT_DELTA_CONFLICTED)
Packit Service 20376f
			delta_type = GIT_DELTA_ADDED;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	else if (nitem->mode == GIT_FILEMODE_COMMIT) {
Packit Service 20376f
		/* ignore things that are not actual submodules */
Packit Service 20376f
		if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) {
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			delta_type = GIT_DELTA_IGNORED;
Packit Service 20376f
Packit Service 20376f
			/* if this contains a tracked item, treat as normal TREE */
Packit Service 20376f
			if (contains_oitem) {
Packit Service 20376f
				error = iterator_advance_into(&info->nitem, info->new_iter);
Packit Service 20376f
				if (error != GIT_ENOTFOUND)
Packit Service 20376f
					return error;
Packit Service 20376f
Packit Service 20376f
				giterr_clear();
Packit Service 20376f
				return iterator_advance(&info->nitem, info->new_iter);
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	else if (nitem->mode == GIT_FILEMODE_UNREADABLE) {
Packit Service 20376f
		if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED))
Packit Service 20376f
			delta_type = GIT_DELTA_UNTRACKED;
Packit Service 20376f
		else
Packit Service 20376f
			delta_type = GIT_DELTA_UNREADABLE;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Actually create the record for this item if necessary */
Packit Service 20376f
	if ((error = diff_delta__from_one(diff, delta_type, NULL, nitem)) != 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* If user requested TYPECHANGE records, then check for that instead of
Packit Service 20376f
	 * just generating an ADDED/UNTRACKED record
Packit Service 20376f
	 */
Packit Service 20376f
	if (delta_type != GIT_DELTA_IGNORED &&
Packit Service 20376f
		DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
Packit Service 20376f
		contains_oitem)
Packit Service 20376f
	{
Packit Service 20376f
		/* this entry was prefixed with a tree - make TYPECHANGE */
Packit Service 20376f
		git_diff_delta *last = diff_delta__last_for_item(diff, nitem);
Packit Service 20376f
		if (last) {
Packit Service 20376f
			last->status = GIT_DELTA_TYPECHANGE;
Packit Service 20376f
			last->old_file.mode = GIT_FILEMODE_TREE;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return iterator_advance(&info->nitem, info->new_iter);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int handle_unmatched_old_item(
Packit Service 20376f
	git_diff_generated *diff, diff_in_progress *info)
Packit Service 20376f
{
Packit Service 20376f
	git_delta_t delta_type = GIT_DELTA_DELETED;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	/* update delta_type if this item is conflicted */
Packit Service 20376f
	if (git_index_entry_is_conflict(info->oitem))
Packit Service 20376f
		delta_type = GIT_DELTA_CONFLICTED;
Packit Service 20376f
Packit Service 20376f
	if ((error = diff_delta__from_one(diff, delta_type, info->oitem, NULL)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* if we are generating TYPECHANGE records then check for that
Packit Service 20376f
	 * instead of just generating a DELETE record
Packit Service 20376f
	 */
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
Packit Service 20376f
		entry_is_prefixed(diff, info->nitem, info->oitem))
Packit Service 20376f
	{
Packit Service 20376f
		/* this entry has become a tree! convert to TYPECHANGE */
Packit Service 20376f
		git_diff_delta *last = diff_delta__last_for_item(diff, info->oitem);
Packit Service 20376f
		if (last) {
Packit Service 20376f
			last->status = GIT_DELTA_TYPECHANGE;
Packit Service 20376f
			last->new_file.mode = GIT_FILEMODE_TREE;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* If new_iter is a workdir iterator, then this situation
Packit Service 20376f
		 * will certainly be followed by a series of untracked items.
Packit Service 20376f
		 * Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
Packit Service 20376f
		 */
Packit Service 20376f
		if (S_ISDIR(info->nitem->mode) &&
Packit Service 20376f
			DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS))
Packit Service 20376f
			return iterator_advance(&info->nitem, info->new_iter);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return iterator_advance(&info->oitem, info->old_iter);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int handle_matched_item(
Packit Service 20376f
	git_diff_generated *diff, diff_in_progress *info)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = maybe_modified(diff, info)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (!(error = iterator_advance(&info->oitem, info->old_iter)))
Packit Service 20376f
		error = iterator_advance(&info->nitem, info->new_iter);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff__from_iterators(
Packit Service 20376f
	git_diff **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_iterator *old_iter,
Packit Service 20376f
	git_iterator *new_iter,
Packit Service 20376f
	const git_diff_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_generated *diff;
Packit Service 20376f
	diff_in_progress info;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	diff = diff_generated_alloc(repo, old_iter, new_iter);
Packit Service 20376f
	GITERR_CHECK_ALLOC(diff);
Packit Service 20376f
Packit Service 20376f
	info.repo = repo;
Packit Service 20376f
	info.old_iter = old_iter;
Packit Service 20376f
	info.new_iter = new_iter;
Packit Service 20376f
Packit Service 20376f
	/* make iterators have matching icase behavior */
Packit Service 20376f
	if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) {
Packit Service 20376f
		git_iterator_set_ignore_case(old_iter, true);
Packit Service 20376f
		git_iterator_set_ignore_case(new_iter, true);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* finish initialization */
Packit Service 20376f
	if ((error = diff_generated_apply_options(diff, opts)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if ((error = iterator_current(&info.oitem, old_iter)) < 0 ||
Packit Service 20376f
		(error = iterator_current(&info.nitem, new_iter)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* run iterators building diffs */
Packit Service 20376f
	while (!error && (info.oitem || info.nitem)) {
Packit Service 20376f
		int cmp;
Packit Service 20376f
Packit Service 20376f
		/* report progress */
Packit Service 20376f
		if (opts && opts->progress_cb) {
Packit Service 20376f
			if ((error = opts->progress_cb(&diff->base,
Packit Service 20376f
					info.oitem ? info.oitem->path : NULL,
Packit Service 20376f
					info.nitem ? info.nitem->path : NULL,
Packit Service 20376f
					opts->payload)))
Packit Service 20376f
				break;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		cmp = info.oitem ?
Packit Service 20376f
			(info.nitem ? diff->base.entrycomp(info.oitem, info.nitem) : -1) : 1;
Packit Service 20376f
Packit Service 20376f
		/* create DELETED records for old items not matched in new */
Packit Service 20376f
		if (cmp < 0)
Packit Service 20376f
			error = handle_unmatched_old_item(diff, &info;;
Packit Service 20376f
Packit Service 20376f
		/* create ADDED, TRACKED, or IGNORED records for new items not
Packit Service 20376f
		 * matched in old (and/or descend into directories as needed)
Packit Service 20376f
		 */
Packit Service 20376f
		else if (cmp > 0)
Packit Service 20376f
			error = handle_unmatched_new_item(diff, &info;;
Packit Service 20376f
Packit Service 20376f
		/* otherwise item paths match, so create MODIFIED record
Packit Service 20376f
		 * (or ADDED and DELETED pair if type changed)
Packit Service 20376f
		 */
Packit Service 20376f
		else
Packit Service 20376f
			error = handle_matched_item(diff, &info;;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	diff->base.perf.stat_calls +=
Packit Service 20376f
		old_iter->stat_calls + new_iter->stat_calls;
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	if (!error)
Packit Service 20376f
		*out = &diff->base;
Packit Service 20376f
	else
Packit Service 20376f
		git_diff_free(&diff->base);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define DIFF_FROM_ITERATORS(MAKE_FIRST, FLAGS_FIRST, MAKE_SECOND, FLAGS_SECOND) do { \
Packit Service 20376f
	git_iterator *a = NULL, *b = NULL; \
Packit Service 20376f
	char *pfx = (opts && !(opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) ? \
Packit Service 20376f
		git_pathspec_prefix(&opts->pathspec) : NULL; \
Packit Service 20376f
	git_iterator_options a_opts = GIT_ITERATOR_OPTIONS_INIT, \
Packit Service 20376f
		b_opts = GIT_ITERATOR_OPTIONS_INIT; \
Packit Service 20376f
	a_opts.flags = FLAGS_FIRST; \
Packit Service 20376f
	a_opts.start = pfx; \
Packit Service 20376f
	a_opts.end = pfx; \
Packit Service 20376f
	b_opts.flags = FLAGS_SECOND; \
Packit Service 20376f
	b_opts.start = pfx; \
Packit Service 20376f
	b_opts.end = pfx; \
Packit Service 20376f
	GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \
Packit Service 20376f
	if (opts && (opts->flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH)) { \
Packit Service 20376f
		a_opts.pathlist.strings = opts->pathspec.strings; \
Packit Service 20376f
		a_opts.pathlist.count = opts->pathspec.count; \
Packit Service 20376f
		b_opts.pathlist.strings = opts->pathspec.strings; \
Packit Service 20376f
		b_opts.pathlist.count = opts->pathspec.count; \
Packit Service 20376f
	} \
Packit Service 20376f
	if (!error && !(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \
Packit Service 20376f
		error = git_diff__from_iterators(&diff, repo, a, b, opts); \
Packit Service 20376f
	git__free(pfx); git_iterator_free(a); git_iterator_free(b); \
Packit Service 20376f
} while (0)
Packit Service 20376f
Packit Service 20376f
int git_diff_tree_to_tree(
Packit Service 20376f
	git_diff **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_tree *old_tree,
Packit Service 20376f
	git_tree *new_tree,
Packit Service 20376f
	const git_diff_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_diff *diff = NULL;
Packit Service 20376f
	git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	/* for tree to tree diff, be case sensitive even if the index is
Packit Service 20376f
	 * currently case insensitive, unless the user explicitly asked
Packit Service 20376f
	 * for case insensitivity
Packit Service 20376f
	 */
Packit Service 20376f
	if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0)
Packit Service 20376f
		iflag = GIT_ITERATOR_IGNORE_CASE;
Packit Service 20376f
Packit Service 20376f
	DIFF_FROM_ITERATORS(
Packit Service 20376f
		git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
Packit Service 20376f
		git_iterator_for_tree(&b, new_tree, &b_opts), iflag
Packit Service 20376f
	);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		*out = diff;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int diff_load_index(git_index **index, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error = git_repository_index__weakptr(index, repo);
Packit Service 20376f
Packit Service 20376f
	/* reload the repository index when user did not pass one in */
Packit Service 20376f
	if (!error && git_index_read(*index, false) < 0)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff_tree_to_index(
Packit Service 20376f
	git_diff **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_tree *old_tree,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_diff_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_diff *diff = NULL;
Packit Service 20376f
	git_iterator_flag_t iflag = GIT_ITERATOR_DONT_IGNORE_CASE |
Packit Service 20376f
		GIT_ITERATOR_INCLUDE_CONFLICTS;
Packit Service 20376f
	bool index_ignore_case = false;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if (!index && (error = diff_load_index(&index, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	index_ignore_case = index->ignore_case;
Packit Service 20376f
Packit Service 20376f
	DIFF_FROM_ITERATORS(
Packit Service 20376f
		git_iterator_for_tree(&a, old_tree, &a_opts), iflag,
Packit Service 20376f
		git_iterator_for_index(&b, repo, index, &b_opts), iflag
Packit Service 20376f
	);
Packit Service 20376f
Packit Service 20376f
	/* if index is in case-insensitive order, re-sort deltas to match */
Packit Service 20376f
	if (!error && index_ignore_case)
Packit Service 20376f
		git_diff__set_ignore_case(diff, true);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		*out = diff;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff_index_to_workdir(
Packit Service 20376f
	git_diff **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_diff_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_diff *diff = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if (!index && (error = diff_load_index(&index, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	DIFF_FROM_ITERATORS(
Packit Service 20376f
		git_iterator_for_index(&a, repo, index, &a_opts),
Packit Service 20376f
		GIT_ITERATOR_INCLUDE_CONFLICTS,
Packit Service 20376f
Packit Service 20376f
		git_iterator_for_workdir(&b, repo, index, NULL, &b_opts),
Packit Service 20376f
		GIT_ITERATOR_DONT_AUTOEXPAND
Packit Service 20376f
	);
Packit Service 20376f
Packit Service 20376f
	if (!error && (diff->opts.flags & GIT_DIFF_UPDATE_INDEX) != 0 &&
Packit Service 20376f
		((git_diff_generated *)diff)->index_updated)
Packit Service 20376f
		error = git_index_write(index);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		*out = diff;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff_tree_to_workdir(
Packit Service 20376f
	git_diff **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_tree *old_tree,
Packit Service 20376f
	const git_diff_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_diff *diff = NULL;
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_index__weakptr(&index, repo)))
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	DIFF_FROM_ITERATORS(
Packit Service 20376f
		git_iterator_for_tree(&a, old_tree, &a_opts), 0,
Packit Service 20376f
		git_iterator_for_workdir(&b, repo, index, old_tree, &b_opts), GIT_ITERATOR_DONT_AUTOEXPAND
Packit Service 20376f
	);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		*out = diff;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff_tree_to_workdir_with_index(
Packit Service 20376f
	git_diff **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_tree *tree,
Packit Service 20376f
	const git_diff_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_diff *d1 = NULL, *d2 = NULL;
Packit Service 20376f
	git_index *index = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((error = diff_load_index(&index, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_diff_tree_to_index(&d1, repo, tree, index, opts)) &&
Packit Service 20376f
		!(error = git_diff_index_to_workdir(&d2, repo, index, opts)))
Packit Service 20376f
		error = git_diff_merge(d1, d2);
Packit Service 20376f
Packit Service 20376f
	git_diff_free(d2);
Packit Service 20376f
Packit Service 20376f
	if (error) {
Packit Service 20376f
		git_diff_free(d1);
Packit Service 20376f
		d1 = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = d1;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff_index_to_index(
Packit Service 20376f
	git_diff **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_index *old_index,
Packit Service 20376f
	git_index *new_index,
Packit Service 20376f
	const git_diff_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_diff *diff;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && old_index && new_index);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	DIFF_FROM_ITERATORS(
Packit Service 20376f
		git_iterator_for_index(&a, repo, old_index, &a_opts), GIT_ITERATOR_DONT_IGNORE_CASE,
Packit Service 20376f
		git_iterator_for_index(&b, repo, new_index, &b_opts), GIT_ITERATOR_DONT_IGNORE_CASE
Packit Service 20376f
	);
Packit Service 20376f
Packit Service 20376f
	/* if index is in case-insensitive order, re-sort deltas to match */
Packit Service 20376f
	if (!error && (old_index->ignore_case || new_index->ignore_case))
Packit Service 20376f
		git_diff__set_ignore_case(diff, true);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		*out = diff;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff__paired_foreach(
Packit Service 20376f
	git_diff *head2idx,
Packit Service 20376f
	git_diff *idx2wd,
Packit Service 20376f
	int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload),
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	int cmp, error = 0;
Packit Service 20376f
	git_diff_delta *h2i, *i2w;
Packit Service 20376f
	size_t i, j, i_max, j_max;
Packit Service 20376f
	int (*strcomp)(const char *, const char *) = git__strcmp;
Packit Service 20376f
	bool h2i_icase, i2w_icase, icase_mismatch;
Packit Service 20376f
Packit Service 20376f
	i_max = head2idx ? head2idx->deltas.length : 0;
Packit Service 20376f
	j_max = idx2wd ? idx2wd->deltas.length : 0;
Packit Service 20376f
	if (!i_max && !j_max)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* At some point, tree-to-index diffs will probably never ignore case,
Packit Service 20376f
	 * even if that isn't true now.  Index-to-workdir diffs may or may not
Packit Service 20376f
	 * ignore case, but the index filename for the idx2wd diff should
Packit Service 20376f
	 * still be using the canonical case-preserving name.
Packit Service 20376f
	 *
Packit Service 20376f
	 * Therefore the main thing we need to do here is make sure the diffs
Packit Service 20376f
	 * are traversed in a compatible order.  To do this, we temporarily
Packit Service 20376f
	 * resort a mismatched diff to get the order correct.
Packit Service 20376f
	 *
Packit Service 20376f
	 * In order to traverse renames in the index->workdir, we need to
Packit Service 20376f
	 * ensure that we compare the index name on both sides, so we
Packit Service 20376f
	 * always sort by the old name in the i2w list.
Packit Service 20376f
	 */
Packit Service 20376f
	h2i_icase = head2idx != NULL && git_diff_is_sorted_icase(head2idx);
Packit Service 20376f
	i2w_icase = idx2wd != NULL && git_diff_is_sorted_icase(idx2wd);
Packit Service 20376f
Packit Service 20376f
	icase_mismatch =
Packit Service 20376f
		(head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase);
Packit Service 20376f
Packit Service 20376f
	if (icase_mismatch && h2i_icase) {
Packit Service 20376f
		git_vector_set_cmp(&head2idx->deltas, git_diff_delta__cmp);
Packit Service 20376f
		git_vector_sort(&head2idx->deltas);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (i2w_icase && !icase_mismatch) {
Packit Service 20376f
		strcomp = git__strcasecmp;
Packit Service 20376f
Packit Service 20376f
		git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_casecmp);
Packit Service 20376f
		git_vector_sort(&idx2wd->deltas);
Packit Service 20376f
	} else if (idx2wd != NULL) {
Packit Service 20376f
		git_vector_set_cmp(&idx2wd->deltas, git_diff_delta__i2w_cmp);
Packit Service 20376f
		git_vector_sort(&idx2wd->deltas);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	for (i = 0, j = 0; i < i_max || j < j_max; ) {
Packit Service 20376f
		h2i = head2idx ? GIT_VECTOR_GET(&head2idx->deltas, i) : NULL;
Packit Service 20376f
		i2w = idx2wd ? GIT_VECTOR_GET(&idx2wd->deltas, j) : NULL;
Packit Service 20376f
Packit Service 20376f
		cmp = !i2w ? -1 : !h2i ? 1 :
Packit Service 20376f
			strcomp(h2i->new_file.path, i2w->old_file.path);
Packit Service 20376f
Packit Service 20376f
		if (cmp < 0) {
Packit Service 20376f
			i++; i2w = NULL;
Packit Service 20376f
		} else if (cmp > 0) {
Packit Service 20376f
			j++; h2i = NULL;
Packit Service 20376f
		} else {
Packit Service 20376f
			i++; j++;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if ((error = cb(h2i, i2w, payload)) != 0) {
Packit Service 20376f
			giterr_set_after_callback(error);
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* restore case-insensitive delta sort */
Packit Service 20376f
	if (icase_mismatch && h2i_icase) {
Packit Service 20376f
		git_vector_set_cmp(&head2idx->deltas, git_diff_delta__casecmp);
Packit Service 20376f
		git_vector_sort(&head2idx->deltas);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* restore idx2wd sort by new path */
Packit Service 20376f
	if (idx2wd != NULL) {
Packit Service 20376f
		git_vector_set_cmp(&idx2wd->deltas,
Packit Service 20376f
			i2w_icase ? git_diff_delta__casecmp : git_diff_delta__cmp);
Packit Service 20376f
		git_vector_sort(&idx2wd->deltas);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_diff__commit(
Packit Service 20376f
	git_diff **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_commit *commit,
Packit Service 20376f
	const git_diff_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_commit *parent = NULL;
Packit Service 20376f
	git_diff *commit_diff = NULL;
Packit Service 20376f
	git_tree *old_tree = NULL, *new_tree = NULL;
Packit Service 20376f
	size_t parents;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((parents = git_commit_parentcount(commit)) > 1) {
Packit Service 20376f
		char commit_oidstr[GIT_OID_HEXSZ + 1];
Packit Service 20376f
Packit Service 20376f
		error = -1;
Packit Service 20376f
		giterr_set(GITERR_INVALID, "commit %s is a merge commit",
Packit Service 20376f
			git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (parents > 0)
Packit Service 20376f
		if ((error = git_commit_parent(&parent, commit, 0)) < 0 ||
Packit Service 20376f
			(error = git_commit_tree(&old_tree, parent)) < 0)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_commit_tree(&new_tree, commit)) < 0 ||
Packit Service 20376f
		(error = git_diff_tree_to_tree(&commit_diff, repo, old_tree, new_tree, opts)) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
	*out = commit_diff;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	git_tree_free(new_tree);
Packit Service 20376f
	git_tree_free(old_tree);
Packit Service 20376f
	git_commit_free(parent);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f