Blame src/checkout.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 <assert.h>
Packit Service 20376f
Packit Service 20376f
#include "checkout.h"
Packit Service 20376f
Packit Service 20376f
#include "git2/repository.h"
Packit Service 20376f
#include "git2/refs.h"
Packit Service 20376f
#include "git2/tree.h"
Packit Service 20376f
#include "git2/blob.h"
Packit Service 20376f
#include "git2/config.h"
Packit Service 20376f
#include "git2/diff.h"
Packit Service 20376f
#include "git2/submodule.h"
Packit Service 20376f
#include "git2/sys/index.h"
Packit Service 20376f
#include "git2/sys/filter.h"
Packit Service 20376f
#include "git2/merge.h"
Packit Service 20376f
Packit Service 20376f
#include "refs.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "index.h"
Packit Service 20376f
#include "filter.h"
Packit Service 20376f
#include "blob.h"
Packit Service 20376f
#include "diff.h"
Packit Service 20376f
#include "diff_generate.h"
Packit Service 20376f
#include "pathspec.h"
Packit Service 20376f
#include "buf_text.h"
Packit Service 20376f
#include "diff_xdiff.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
#include "attr.h"
Packit Service 20376f
#include "pool.h"
Packit Service 20376f
#include "strmap.h"
Packit Service 20376f
Packit Service 20376f
/* See docs/checkout-internals.md for more information */
Packit Service 20376f
Packit Service 20376f
enum {
Packit Service 20376f
	CHECKOUT_ACTION__NONE = 0,
Packit Service 20376f
	CHECKOUT_ACTION__REMOVE = 1,
Packit Service 20376f
	CHECKOUT_ACTION__UPDATE_BLOB = 2,
Packit Service 20376f
	CHECKOUT_ACTION__UPDATE_SUBMODULE = 4,
Packit Service 20376f
	CHECKOUT_ACTION__CONFLICT = 8,
Packit Service 20376f
	CHECKOUT_ACTION__REMOVE_CONFLICT = 16,
Packit Service 20376f
	CHECKOUT_ACTION__UPDATE_CONFLICT = 32,
Packit Service 20376f
	CHECKOUT_ACTION__MAX = 32,
Packit Service 20376f
	CHECKOUT_ACTION__DEFER_REMOVE = 64,
Packit Service 20376f
	CHECKOUT_ACTION__REMOVE_AND_UPDATE =
Packit Service 20376f
		(CHECKOUT_ACTION__UPDATE_BLOB | CHECKOUT_ACTION__REMOVE),
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_iterator *target;
Packit Service 20376f
	git_diff *diff;
Packit Service 20376f
	git_checkout_options opts;
Packit Service 20376f
	bool opts_free_baseline;
Packit Service 20376f
	char *pfx;
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_pool pool;
Packit Service 20376f
	git_vector removes;
Packit Service 20376f
	git_vector remove_conflicts;
Packit Service 20376f
	git_vector update_conflicts;
Packit Service 20376f
	git_vector *update_reuc;
Packit Service 20376f
	git_vector *update_names;
Packit Service 20376f
	git_buf target_path;
Packit Service 20376f
	size_t target_len;
Packit Service 20376f
	git_buf tmp;
Packit Service 20376f
	unsigned int strategy;
Packit Service 20376f
	int can_symlink;
Packit Service 20376f
	bool reload_submodules;
Packit Service 20376f
	size_t total_steps;
Packit Service 20376f
	size_t completed_steps;
Packit Service 20376f
	git_checkout_perfdata perfdata;
Packit Service 20376f
	git_strmap *mkdir_map;
Packit Service 20376f
	git_attr_session attr_session;
Packit Service 20376f
} checkout_data;
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	const git_index_entry *ancestor;
Packit Service 20376f
	const git_index_entry *ours;
Packit Service 20376f
	const git_index_entry *theirs;
Packit Service 20376f
Packit Service 20376f
	int name_collision:1,
Packit Service 20376f
		directoryfile:1,
Packit Service 20376f
		one_to_two:1,
Packit Service 20376f
		binary:1,
Packit Service 20376f
		submodule:1;
Packit Service 20376f
} checkout_conflictdata;
Packit Service 20376f
Packit Service 20376f
static int checkout_notify(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_checkout_notify_t why,
Packit Service 20376f
	const git_diff_delta *delta,
Packit Service 20376f
	const git_index_entry *wditem)
Packit Service 20376f
{
Packit Service 20376f
	git_diff_file wdfile;
Packit Service 20376f
	const git_diff_file *baseline = NULL, *target = NULL, *workdir = NULL;
Packit Service 20376f
	const char *path = NULL;
Packit Service 20376f
Packit Service 20376f
	if (!data->opts.notify_cb ||
Packit Service 20376f
		(why & data->opts.notify_flags) == 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (wditem) {
Packit Service 20376f
		memset(&wdfile, 0, sizeof(wdfile));
Packit Service 20376f
Packit Service 20376f
		git_oid_cpy(&wdfile.id, &wditem->id);
Packit Service 20376f
		wdfile.path = wditem->path;
Packit Service 20376f
		wdfile.size = wditem->file_size;
Packit Service 20376f
		wdfile.flags = GIT_DIFF_FLAG_VALID_ID;
Packit Service 20376f
		wdfile.mode = wditem->mode;
Packit Service 20376f
Packit Service 20376f
		workdir = &wdfile;
Packit Service 20376f
Packit Service 20376f
		path = wditem->path;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (delta) {
Packit Service 20376f
		switch (delta->status) {
Packit Service 20376f
		case GIT_DELTA_UNMODIFIED:
Packit Service 20376f
		case GIT_DELTA_MODIFIED:
Packit Service 20376f
		case GIT_DELTA_TYPECHANGE:
Packit Service 20376f
		default:
Packit Service 20376f
			baseline = &delta->old_file;
Packit Service 20376f
			target = &delta->new_file;
Packit Service 20376f
			break;
Packit Service 20376f
		case GIT_DELTA_ADDED:
Packit Service 20376f
		case GIT_DELTA_IGNORED:
Packit Service 20376f
		case GIT_DELTA_UNTRACKED:
Packit Service 20376f
		case GIT_DELTA_UNREADABLE:
Packit Service 20376f
			target = &delta->new_file;
Packit Service 20376f
			break;
Packit Service 20376f
		case GIT_DELTA_DELETED:
Packit Service 20376f
			baseline = &delta->old_file;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		path = delta->old_file.path;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	{
Packit Service 20376f
		int error = data->opts.notify_cb(
Packit Service 20376f
			why, path, baseline, target, workdir, data->opts.notify_payload);
Packit Service 20376f
Packit Service 20376f
		return giterr_set_after_callback_function(
Packit Service 20376f
			error, "git_checkout notification");
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) is_workdir_base_or_new(
Packit Service 20376f
	const git_oid *workdir_id,
Packit Service 20376f
	const git_diff_file *baseitem,
Packit Service 20376f
	const git_diff_file *newitem)
Packit Service 20376f
{
Packit Service 20376f
	return (git_oid__cmp(&baseitem->id, workdir_id) == 0 ||
Packit Service 20376f
		git_oid__cmp(&newitem->id, workdir_id) == 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) is_file_mode_changed(git_filemode_t a, git_filemode_t b)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	/*
Packit Service 20376f
	 * On Win32 we do not support the executable bit; the file will
Packit Service 20376f
	 * always be 0100644 on disk, don't bother doing a test.
Packit Service 20376f
	 */
Packit Service 20376f
	return false;
Packit Service 20376f
#else
Packit Service 20376f
	return (S_ISREG(a) && S_ISREG(b) && a != b);
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool checkout_is_workdir_modified(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_file *baseitem,
Packit Service 20376f
	const git_diff_file *newitem,
Packit Service 20376f
	const git_index_entry *wditem)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	const git_index_entry *ie;
Packit Service 20376f
Packit Service 20376f
	/* handle "modified" submodule */
Packit Service 20376f
	if (wditem->mode == GIT_FILEMODE_COMMIT) {
Packit Service 20376f
		git_submodule *sm;
Packit Service 20376f
		unsigned int sm_status = 0;
Packit Service 20376f
		const git_oid *sm_oid = NULL;
Packit Service 20376f
		bool rval = false;
Packit Service 20376f
Packit Service 20376f
		if (git_submodule_lookup(&sm, data->repo, wditem->path) < 0) {
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			return true;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (git_submodule_status(&sm_status, data->repo, wditem->path, GIT_SUBMODULE_IGNORE_UNSPECIFIED) < 0 ||
Packit Service 20376f
			GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
Packit Service 20376f
			rval = true;
Packit Service 20376f
		else if ((sm_oid = git_submodule_wd_id(sm)) == NULL)
Packit Service 20376f
			rval = false;
Packit Service 20376f
		else
Packit Service 20376f
			rval = (git_oid__cmp(&baseitem->id, sm_oid) != 0);
Packit Service 20376f
Packit Service 20376f
		git_submodule_free(sm);
Packit Service 20376f
		return rval;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Look at the cache to decide if the workdir is modified: if the
Packit Service 20376f
	 * cache contents match the workdir contents, then we do not need
Packit Service 20376f
	 * to examine the working directory directly, instead we can
Packit Service 20376f
	 * examine the cache to see if _it_ has been modified.  This allows
Packit Service 20376f
	 * us to avoid touching the disk.
Packit Service 20376f
	 */
Packit Service 20376f
	ie = git_index_get_bypath(data->index, wditem->path, 0);
Packit Service 20376f
Packit Service 20376f
	if (ie != NULL &&
Packit Service 20376f
		git_index_time_eq(&wditem->mtime, &ie->mtime) &&
Packit Service 20376f
		wditem->file_size == ie->file_size &&
Packit Service 20376f
		!is_file_mode_changed(wditem->mode, ie->mode)) {
Packit Service 20376f
Packit Service 20376f
		/* The workdir is modified iff the index entry is modified */
Packit Service 20376f
		return !is_workdir_base_or_new(&ie->id, baseitem, newitem) ||
Packit Service 20376f
			is_file_mode_changed(baseitem->mode, ie->mode);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* depending on where base is coming from, we may or may not know
Packit Service 20376f
	 * the actual size of the data, so we can't rely on this shortcut.
Packit Service 20376f
	 */
Packit Service 20376f
	if (baseitem->size && wditem->file_size != baseitem->size)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	/* if the workdir item is a directory, it cannot be a modified file */
Packit Service 20376f
	if (S_ISDIR(wditem->mode))
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if (is_file_mode_changed(baseitem->mode, wditem->mode))
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (git_diff__oid_for_entry(&oid, data->diff, wditem, wditem->mode, NULL) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	/* Allow the checkout if the workdir is not modified *or* if the checkout
Packit Service 20376f
	 * target's contents are already in the working directory.
Packit Service 20376f
	 */
Packit Service 20376f
	return !is_workdir_base_or_new(&oid, baseitem, newitem);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define CHECKOUT_ACTION_IF(FLAG,YES,NO) \
Packit Service 20376f
	((data->strategy & GIT_CHECKOUT_##FLAG) ? CHECKOUT_ACTION__##YES : CHECKOUT_ACTION__##NO)
Packit Service 20376f
Packit Service 20376f
static int checkout_action_common(
Packit Service 20376f
	int *action,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_delta *delta,
Packit Service 20376f
	const git_index_entry *wd)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
Packit Service 20376f
Packit Service 20376f
	if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
Packit Service 20376f
		*action = (*action & ~CHECKOUT_ACTION__REMOVE);
Packit Service 20376f
Packit Service 20376f
	if ((*action & CHECKOUT_ACTION__UPDATE_BLOB) != 0) {
Packit Service 20376f
		if (S_ISGITLINK(delta->new_file.mode))
Packit Service 20376f
			*action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB) |
Packit Service 20376f
				CHECKOUT_ACTION__UPDATE_SUBMODULE;
Packit Service 20376f
Packit Service 20376f
		/* to "update" a symlink, we must remove the old one first */
Packit Service 20376f
		if (delta->new_file.mode == GIT_FILEMODE_LINK && wd != NULL)
Packit Service 20376f
			*action |= CHECKOUT_ACTION__REMOVE;
Packit Service 20376f
Packit Service 20376f
		/* if the file is on disk and doesn't match our mode, force update */
Packit Service 20376f
		if (wd &&
Packit Service 20376f
			GIT_PERMS_IS_EXEC(wd->mode) !=
Packit Service 20376f
			GIT_PERMS_IS_EXEC(delta->new_file.mode))
Packit Service 20376f
				*action |= CHECKOUT_ACTION__REMOVE;
Packit Service 20376f
Packit Service 20376f
		notify = GIT_CHECKOUT_NOTIFY_UPDATED;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((*action & CHECKOUT_ACTION__CONFLICT) != 0)
Packit Service 20376f
		notify = GIT_CHECKOUT_NOTIFY_CONFLICT;
Packit Service 20376f
Packit Service 20376f
	return checkout_notify(data, notify, delta, wd);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_action_no_wd(
Packit Service 20376f
	int *action,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_delta *delta)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	*action = CHECKOUT_ACTION__NONE;
Packit Service 20376f
Packit Service 20376f
	switch (delta->status) {
Packit Service 20376f
	case GIT_DELTA_UNMODIFIED: /* case 12 */
Packit Service 20376f
		error = checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL);
Packit Service 20376f
		if (error)
Packit Service 20376f
			return error;
Packit Service 20376f
		*action = CHECKOUT_ACTION_IF(RECREATE_MISSING, UPDATE_BLOB, NONE);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_ADDED:    /* case 2 or 28 (and 5 but not really) */
Packit Service 20376f
		*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_MODIFIED: /* case 13 (and 35 but not really) */
Packit Service 20376f
		*action = CHECKOUT_ACTION_IF(RECREATE_MISSING, UPDATE_BLOB, CONFLICT);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_TYPECHANGE: /* case 21 (B->T) and 28 (T->B)*/
Packit Service 20376f
		if (delta->new_file.mode == GIT_FILEMODE_TREE)
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_DELETED: /* case 8 or 25 */
Packit Service 20376f
		*action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
Packit Service 20376f
		break;
Packit Service 20376f
	default: /* impossible */
Packit Service 20376f
		break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return checkout_action_common(action, data, delta, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_target_fullpath(
Packit Service 20376f
	git_buf **out, checkout_data *data, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf_truncate(&data->target_path, data->target_len);
Packit Service 20376f
Packit Service 20376f
	if (path && git_buf_puts(&data->target_path, path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	*out = &data->target_path;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool wd_item_is_removable(
Packit Service 20376f
	checkout_data *data, const git_index_entry *wd)
Packit Service 20376f
{
Packit Service 20376f
	git_buf *full;
Packit Service 20376f
Packit Service 20376f
	if (wd->mode != GIT_FILEMODE_TREE)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (checkout_target_fullpath(&full, data, wd->path) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return !full || !git_path_contains(full, DOT_GIT);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_queue_remove(checkout_data *data, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	char *copy = git_pool_strdup(&data->pool, path);
Packit Service 20376f
	GITERR_CHECK_ALLOC(copy);
Packit Service 20376f
	return git_vector_insert(&data->removes, copy);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* note that this advances the iterator over the wd item */
Packit Service 20376f
static int checkout_action_wd_only(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_iterator *workdir,
Packit Service 20376f
	const git_index_entry **wditem,
Packit Service 20376f
	git_vector *pathspec)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	bool remove = false;
Packit Service 20376f
	git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;
Packit Service 20376f
	const git_index_entry *wd = *wditem;
Packit Service 20376f
Packit Service 20376f
	if (!git_pathspec__match(
Packit Service 20376f
			pathspec, wd->path,
Packit Service 20376f
			(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
Packit Service 20376f
			git_iterator_ignore_case(workdir), NULL, NULL))
Packit Service 20376f
		return git_iterator_advance(wditem, workdir);
Packit Service 20376f
Packit Service 20376f
	/* check if item is tracked in the index but not in the checkout diff */
Packit Service 20376f
	if (data->index != NULL) {
Packit Service 20376f
		size_t pos;
Packit Service 20376f
Packit Service 20376f
		error = git_index__find_pos(
Packit Service 20376f
			&pos, data->index, wd->path, 0, GIT_INDEX_STAGE_ANY);
Packit Service 20376f
Packit Service 20376f
		if (wd->mode != GIT_FILEMODE_TREE) {
Packit Service 20376f
			if (!error) { /* found by git_index__find_pos call */
Packit Service 20376f
				notify = GIT_CHECKOUT_NOTIFY_DIRTY;
Packit Service 20376f
				remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
Packit Service 20376f
			} else if (error != GIT_ENOTFOUND)
Packit Service 20376f
				return error;
Packit Service 20376f
			else
Packit Service 20376f
				error = 0; /* git_index__find_pos does not set error msg */
Packit Service 20376f
		} else {
Packit Service 20376f
			/* for tree entries, we have to see if there are any index
Packit Service 20376f
			 * entries that are contained inside that tree
Packit Service 20376f
			 */
Packit Service 20376f
			const git_index_entry *e = git_index_get_byindex(data->index, pos);
Packit Service 20376f
Packit Service 20376f
			if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0)
Packit Service 20376f
				return git_iterator_advance_into(wditem, workdir);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (notify != GIT_CHECKOUT_NOTIFY_NONE) {
Packit Service 20376f
		/* if we found something in the index, notify and advance */
Packit Service 20376f
		if ((error = checkout_notify(data, notify, NULL, wd)) != 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if (remove && wd_item_is_removable(data, wd))
Packit Service 20376f
			error = checkout_queue_remove(data, wd->path);
Packit Service 20376f
Packit Service 20376f
		if (!error)
Packit Service 20376f
			error = git_iterator_advance(wditem, workdir);
Packit Service 20376f
	} else {
Packit Service 20376f
		/* untracked or ignored - can't know which until we advance through */
Packit Service 20376f
		bool over = false, removable = wd_item_is_removable(data, wd);
Packit Service 20376f
		git_iterator_status_t untracked_state;
Packit Service 20376f
Packit Service 20376f
		/* copy the entry for issuing notification callback later */
Packit Service 20376f
		git_index_entry saved_wd = *wd;
Packit Service 20376f
		git_buf_sets(&data->tmp, wd->path);
Packit Service 20376f
		saved_wd.path = data->tmp.ptr;
Packit Service 20376f
Packit Service 20376f
		error = git_iterator_advance_over(
Packit Service 20376f
			wditem, &untracked_state, workdir);
Packit Service 20376f
		if (error == GIT_ITEROVER)
Packit Service 20376f
			over = true;
Packit Service 20376f
		else if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if (untracked_state == GIT_ITERATOR_STATUS_IGNORED) {
Packit Service 20376f
			notify = GIT_CHECKOUT_NOTIFY_IGNORED;
Packit Service 20376f
			remove = ((data->strategy & GIT_CHECKOUT_REMOVE_IGNORED) != 0);
Packit Service 20376f
		} else {
Packit Service 20376f
			notify = GIT_CHECKOUT_NOTIFY_UNTRACKED;
Packit Service 20376f
			remove = ((data->strategy & GIT_CHECKOUT_REMOVE_UNTRACKED) != 0);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if ((error = checkout_notify(data, notify, NULL, &saved_wd)) != 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if (remove && removable)
Packit Service 20376f
			error = checkout_queue_remove(data, saved_wd.path);
Packit Service 20376f
Packit Service 20376f
		if (!error && over) /* restore ITEROVER if needed */
Packit Service 20376f
			error = GIT_ITEROVER;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool submodule_is_config_only(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_submodule *sm = NULL;
Packit Service 20376f
	unsigned int sm_loc = 0;
Packit Service 20376f
	bool rval = false;
Packit Service 20376f
Packit Service 20376f
	if (git_submodule_lookup(&sm, data->repo, path) < 0)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (git_submodule_location(&sm_loc, sm) < 0 ||
Packit Service 20376f
		sm_loc == GIT_SUBMODULE_STATUS_IN_CONFIG)
Packit Service 20376f
		rval = true;
Packit Service 20376f
Packit Service 20376f
	git_submodule_free(sm);
Packit Service 20376f
Packit Service 20376f
	return rval;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool checkout_is_empty_dir(checkout_data *data, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf *fullpath;
Packit Service 20376f
Packit Service 20376f
	if (checkout_target_fullpath(&fullpath, data, path) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return git_path_is_empty_dir(fullpath->ptr);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_action_with_wd(
Packit Service 20376f
	int *action,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_delta *delta,
Packit Service 20376f
	git_iterator *workdir,
Packit Service 20376f
	const git_index_entry *wd)
Packit Service 20376f
{
Packit Service 20376f
	*action = CHECKOUT_ACTION__NONE;
Packit Service 20376f
Packit Service 20376f
	switch (delta->status) {
Packit Service 20376f
	case GIT_DELTA_UNMODIFIED: /* case 14/15 or 33 */
Packit Service 20376f
		if (checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd)) {
Packit Service 20376f
			GITERR_CHECK_ERROR(
Packit Service 20376f
				checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) );
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE);
Packit Service 20376f
		}
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_ADDED: /* case 3, 4 or 6 */
Packit Service 20376f
		if (git_iterator_current_is_ignored(workdir))
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED, CONFLICT, UPDATE_BLOB);
Packit Service 20376f
		else
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_DELETED: /* case 9 or 10 (or 26 but not really) */
Packit Service 20376f
		if (checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd))
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
Packit Service 20376f
		else
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(SAFE, REMOVE, NONE);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_MODIFIED: /* case 16, 17, 18 (or 36 but not really) */
Packit Service 20376f
		if (wd->mode != GIT_FILEMODE_COMMIT &&
Packit Service 20376f
			checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd))
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, CONFLICT);
Packit Service 20376f
		else
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_TYPECHANGE: /* case 22, 23, 29, 30 */
Packit Service 20376f
		if (delta->old_file.mode == GIT_FILEMODE_TREE) {
Packit Service 20376f
			if (wd->mode == GIT_FILEMODE_TREE)
Packit Service 20376f
				/* either deleting items in old tree will delete the wd dir,
Packit Service 20376f
				 * or we'll get a conflict when we attempt blob update...
Packit Service 20376f
				 */
Packit Service 20376f
				*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
Packit Service 20376f
			else if (wd->mode == GIT_FILEMODE_COMMIT) {
Packit Service 20376f
				/* workdir is possibly a "phantom" submodule - treat as a
Packit Service 20376f
				 * tree if the only submodule info came from the config
Packit Service 20376f
				 */
Packit Service 20376f
				if (submodule_is_config_only(data, wd->path))
Packit Service 20376f
					*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
Packit Service 20376f
				else
Packit Service 20376f
					*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
Packit Service 20376f
			} else
Packit Service 20376f
				*action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
Packit Service 20376f
		}
Packit Service 20376f
		else if (checkout_is_workdir_modified(data, &delta->old_file, &delta->new_file, wd))
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
Packit Service 20376f
		else
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(SAFE, REMOVE_AND_UPDATE, NONE);
Packit Service 20376f
Packit Service 20376f
		/* don't update if the typechange is to a tree */
Packit Service 20376f
		if (delta->new_file.mode == GIT_FILEMODE_TREE)
Packit Service 20376f
			*action = (*action & ~CHECKOUT_ACTION__UPDATE_BLOB);
Packit Service 20376f
		break;
Packit Service 20376f
	default: /* impossible */
Packit Service 20376f
		break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return checkout_action_common(action, data, delta, wd);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_action_with_wd_blocker(
Packit Service 20376f
	int *action,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_delta *delta,
Packit Service 20376f
	const git_index_entry *wd)
Packit Service 20376f
{
Packit Service 20376f
	*action = CHECKOUT_ACTION__NONE;
Packit Service 20376f
Packit Service 20376f
	switch (delta->status) {
Packit Service 20376f
	case GIT_DELTA_UNMODIFIED:
Packit Service 20376f
		/* should show delta as dirty / deleted */
Packit Service 20376f
		GITERR_CHECK_ERROR(
Packit Service 20376f
			checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd) );
Packit Service 20376f
		*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_ADDED:
Packit Service 20376f
	case GIT_DELTA_MODIFIED:
Packit Service 20376f
		*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_DELETED:
Packit Service 20376f
		*action = CHECKOUT_ACTION_IF(FORCE, REMOVE, CONFLICT);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_TYPECHANGE:
Packit Service 20376f
		/* not 100% certain about this... */
Packit Service 20376f
		*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
Packit Service 20376f
		break;
Packit Service 20376f
	default: /* impossible */
Packit Service 20376f
		break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return checkout_action_common(action, data, delta, wd);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_action_with_wd_dir(
Packit Service 20376f
	int *action,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_delta *delta,
Packit Service 20376f
	git_iterator *workdir,
Packit Service 20376f
	const git_index_entry *wd)
Packit Service 20376f
{
Packit Service 20376f
	*action = CHECKOUT_ACTION__NONE;
Packit Service 20376f
Packit Service 20376f
	switch (delta->status) {
Packit Service 20376f
	case GIT_DELTA_UNMODIFIED: /* case 19 or 24 (or 34 but not really) */
Packit Service 20376f
		GITERR_CHECK_ERROR(
Packit Service 20376f
			checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL));
Packit Service 20376f
		GITERR_CHECK_ERROR(
Packit Service 20376f
			checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd));
Packit Service 20376f
		*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_ADDED:/* case 4 (and 7 for dir) */
Packit Service 20376f
	case GIT_DELTA_MODIFIED: /* case 20 (or 37 but not really) */
Packit Service 20376f
		if (delta->old_file.mode == GIT_FILEMODE_COMMIT)
Packit Service 20376f
			/* expected submodule (and maybe found one) */;
Packit Service 20376f
		else if (delta->new_file.mode != GIT_FILEMODE_TREE)
Packit Service 20376f
			*action = git_iterator_current_is_ignored(workdir) ?
Packit Service 20376f
				CHECKOUT_ACTION_IF(DONT_OVERWRITE_IGNORED, CONFLICT, REMOVE_AND_UPDATE) :
Packit Service 20376f
				CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_DELETED: /* case 11 (and 27 for dir) */
Packit Service 20376f
		if (delta->old_file.mode != GIT_FILEMODE_TREE)
Packit Service 20376f
			GITERR_CHECK_ERROR(
Packit Service 20376f
				checkout_notify(data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd));
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_DELTA_TYPECHANGE: /* case 24 or 31 */
Packit Service 20376f
		if (delta->old_file.mode == GIT_FILEMODE_TREE) {
Packit Service 20376f
			/* For typechange from dir, remove dir and add blob, but it is
Packit Service 20376f
			 * not safe to remove dir if it contains modified files.
Packit Service 20376f
			 * However, safely removing child files will remove the parent
Packit Service 20376f
			 * directory if is it left empty, so we can defer removing the
Packit Service 20376f
			 * dir and it will succeed if no children are left.
Packit Service 20376f
			 */
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(SAFE, UPDATE_BLOB, NONE);
Packit Service 20376f
		}
Packit Service 20376f
		else if (delta->new_file.mode != GIT_FILEMODE_TREE)
Packit Service 20376f
			/* For typechange to dir, dir is already created so no action */
Packit Service 20376f
			*action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, CONFLICT);
Packit Service 20376f
		break;
Packit Service 20376f
	default: /* impossible */
Packit Service 20376f
		break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return checkout_action_common(action, data, delta, wd);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_action_with_wd_dir_empty(
Packit Service 20376f
	int *action,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_delta *delta)
Packit Service 20376f
{
Packit Service 20376f
	int error = checkout_action_no_wd(action, data, delta);
Packit Service 20376f
Packit Service 20376f
	/* We can always safely remove an empty directory. */
Packit Service 20376f
	if (error == 0 && *action != CHECKOUT_ACTION__NONE)
Packit Service 20376f
		*action |= CHECKOUT_ACTION__REMOVE;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_action(
Packit Service 20376f
	int *action,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_diff_delta *delta,
Packit Service 20376f
	git_iterator *workdir,
Packit Service 20376f
	const git_index_entry **wditem,
Packit Service 20376f
	git_vector *pathspec)
Packit Service 20376f
{
Packit Service 20376f
	int cmp = -1, error;
Packit Service 20376f
	int (*strcomp)(const char *, const char *) = data->diff->strcomp;
Packit Service 20376f
	int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp;
Packit Service 20376f
	int (*advance)(const git_index_entry **, git_iterator *) = NULL;
Packit Service 20376f
Packit Service 20376f
	/* move workdir iterator to follow along with deltas */
Packit Service 20376f
Packit Service 20376f
	while (1) {
Packit Service 20376f
		const git_index_entry *wd = *wditem;
Packit Service 20376f
Packit Service 20376f
		if (!wd)
Packit Service 20376f
			return checkout_action_no_wd(action, data, delta);
Packit Service 20376f
Packit Service 20376f
		cmp = strcomp(wd->path, delta->old_file.path);
Packit Service 20376f
Packit Service 20376f
		/* 1. wd before delta ("a/a" before "a/b")
Packit Service 20376f
		 * 2. wd prefixes delta & should expand ("a/" before "a/b")
Packit Service 20376f
		 * 3. wd prefixes delta & cannot expand ("a/b" before "a/b/c")
Packit Service 20376f
		 * 4. wd equals delta ("a/b" and "a/b")
Packit Service 20376f
		 * 5. wd after delta & delta prefixes wd ("a/b/c" after "a/b/" or "a/b")
Packit Service 20376f
		 * 6. wd after delta ("a/c" after "a/b")
Packit Service 20376f
		 */
Packit Service 20376f
Packit Service 20376f
		if (cmp < 0) {
Packit Service 20376f
			cmp = pfxcomp(delta->old_file.path, wd->path);
Packit Service 20376f
Packit Service 20376f
			if (cmp == 0) {
Packit Service 20376f
				if (wd->mode == GIT_FILEMODE_TREE) {
Packit Service 20376f
					/* case 2 - entry prefixed by workdir tree */
Packit Service 20376f
					error = git_iterator_advance_into(wditem, workdir);
Packit Service 20376f
					if (error < 0 && error != GIT_ITEROVER)
Packit Service 20376f
						goto done;
Packit Service 20376f
					continue;
Packit Service 20376f
				}
Packit Service 20376f
Packit Service 20376f
				/* case 3 maybe - wd contains non-dir where dir expected */
Packit Service 20376f
				if (delta->old_file.path[strlen(wd->path)] == '/') {
Packit Service 20376f
					error = checkout_action_with_wd_blocker(
Packit Service 20376f
						action, data, delta, wd);
Packit Service 20376f
					advance = git_iterator_advance;
Packit Service 20376f
					goto done;
Packit Service 20376f
				}
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			/* case 1 - handle wd item (if it matches pathspec) */
Packit Service 20376f
			error = checkout_action_wd_only(data, workdir, wditem, pathspec);
Packit Service 20376f
			if (error && error != GIT_ITEROVER)
Packit Service 20376f
				goto done;
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (cmp == 0) {
Packit Service 20376f
			/* case 4 */
Packit Service 20376f
			error = checkout_action_with_wd(action, data, delta, workdir, wd);
Packit Service 20376f
			advance = git_iterator_advance;
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		cmp = pfxcomp(wd->path, delta->old_file.path);
Packit Service 20376f
Packit Service 20376f
		if (cmp == 0) { /* case 5 */
Packit Service 20376f
			if (wd->path[strlen(delta->old_file.path)] != '/')
Packit Service 20376f
				return checkout_action_no_wd(action, data, delta);
Packit Service 20376f
Packit Service 20376f
			if (delta->status == GIT_DELTA_TYPECHANGE) {
Packit Service 20376f
				if (delta->old_file.mode == GIT_FILEMODE_TREE) {
Packit Service 20376f
					error = checkout_action_with_wd(action, data, delta, workdir, wd);
Packit Service 20376f
					advance = git_iterator_advance_into;
Packit Service 20376f
					goto done;
Packit Service 20376f
				}
Packit Service 20376f
Packit Service 20376f
				if (delta->new_file.mode == GIT_FILEMODE_TREE ||
Packit Service 20376f
					delta->new_file.mode == GIT_FILEMODE_COMMIT ||
Packit Service 20376f
					delta->old_file.mode == GIT_FILEMODE_COMMIT)
Packit Service 20376f
				{
Packit Service 20376f
					error = checkout_action_with_wd(action, data, delta, workdir, wd);
Packit Service 20376f
					advance = git_iterator_advance;
Packit Service 20376f
					goto done;
Packit Service 20376f
				}
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			return checkout_is_empty_dir(data, wd->path) ?
Packit Service 20376f
				checkout_action_with_wd_dir_empty(action, data, delta) :
Packit Service 20376f
				checkout_action_with_wd_dir(action, data, delta, workdir, wd);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* case 6 - wd is after delta */
Packit Service 20376f
		return checkout_action_no_wd(action, data, delta);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	if (!error && advance != NULL &&
Packit Service 20376f
		(error = advance(wditem, workdir)) < 0) {
Packit Service 20376f
		*wditem = NULL;
Packit Service 20376f
		if (error == GIT_ITEROVER)
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 checkout_remaining_wd_items(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_iterator *workdir,
Packit Service 20376f
	const git_index_entry *wd,
Packit Service 20376f
	git_vector *spec)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	while (wd && !error)
Packit Service 20376f
		error = checkout_action_wd_only(data, workdir, &wd, spec);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ITEROVER)
Packit Service 20376f
		error = 0;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) checkout_idxentry_cmp(
Packit Service 20376f
	const git_index_entry *a,
Packit Service 20376f
	const git_index_entry *b)
Packit Service 20376f
{
Packit Service 20376f
	if (!a && !b)
Packit Service 20376f
		return 0;
Packit Service 20376f
	else if (!a && b)
Packit Service 20376f
		return -1;
Packit Service 20376f
	else if(a && !b)
Packit Service 20376f
		return 1;
Packit Service 20376f
	else
Packit Service 20376f
		return strcmp(a->path, b->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflictdata_cmp(const void *a, const void *b)
Packit Service 20376f
{
Packit Service 20376f
	const checkout_conflictdata *ca = a;
Packit Service 20376f
	const checkout_conflictdata *cb = b;
Packit Service 20376f
	int diff;
Packit Service 20376f
Packit Service 20376f
	if ((diff = checkout_idxentry_cmp(ca->ancestor, cb->ancestor)) == 0 &&
Packit Service 20376f
		(diff = checkout_idxentry_cmp(ca->ours, cb->theirs)) == 0)
Packit Service 20376f
		diff = checkout_idxentry_cmp(ca->theirs, cb->theirs);
Packit Service 20376f
Packit Service 20376f
	return diff;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int checkout_conflictdata_empty(
Packit Service 20376f
	const git_vector *conflicts, size_t idx, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	checkout_conflictdata *conflict;
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(payload);
Packit Service 20376f
Packit Service 20376f
	if ((conflict = git_vector_get(conflicts, idx)) == NULL)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (conflict->ancestor || conflict->ours || conflict->theirs)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	git__free(conflict);
Packit Service 20376f
	return 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) conflict_pathspec_match(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_iterator *workdir,
Packit Service 20376f
	git_vector *pathspec,
Packit Service 20376f
	const git_index_entry *ancestor,
Packit Service 20376f
	const git_index_entry *ours,
Packit Service 20376f
	const git_index_entry *theirs)
Packit Service 20376f
{
Packit Service 20376f
	/* if the pathspec matches ours *or* theirs, proceed */
Packit Service 20376f
	if (ours && git_pathspec__match(pathspec, ours->path,
Packit Service 20376f
		(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
Packit Service 20376f
		git_iterator_ignore_case(workdir), NULL, NULL))
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (theirs && git_pathspec__match(pathspec, theirs->path,
Packit Service 20376f
		(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
Packit Service 20376f
		git_iterator_ignore_case(workdir), NULL, NULL))
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (ancestor && git_pathspec__match(pathspec, ancestor->path,
Packit Service 20376f
		(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
Packit Service 20376f
		git_iterator_ignore_case(workdir), NULL, NULL))
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	return false;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) checkout_conflict_detect_submodule(checkout_conflictdata *conflict)
Packit Service 20376f
{
Packit Service 20376f
	conflict->submodule = ((conflict->ancestor && S_ISGITLINK(conflict->ancestor->mode)) ||
Packit Service 20376f
		(conflict->ours && S_ISGITLINK(conflict->ours->mode)) ||
Packit Service 20376f
		(conflict->theirs && S_ISGITLINK(conflict->theirs->mode)));
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) checkout_conflict_detect_binary(git_repository *repo, checkout_conflictdata *conflict)
Packit Service 20376f
{
Packit Service 20376f
	git_blob *ancestor_blob = NULL, *our_blob = NULL, *their_blob = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (conflict->submodule)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (conflict->ancestor) {
Packit Service 20376f
		if ((error = git_blob_lookup(&ancestor_blob, repo, &conflict->ancestor->id)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		conflict->binary = git_blob_is_binary(ancestor_blob);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!conflict->binary && conflict->ours) {
Packit Service 20376f
		if ((error = git_blob_lookup(&our_blob, repo, &conflict->ours->id)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		conflict->binary = git_blob_is_binary(our_blob);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!conflict->binary && conflict->theirs) {
Packit Service 20376f
		if ((error = git_blob_lookup(&their_blob, repo, &conflict->theirs->id)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		conflict->binary = git_blob_is_binary(their_blob);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_blob_free(ancestor_blob);
Packit Service 20376f
	git_blob_free(our_blob);
Packit Service 20376f
	git_blob_free(their_blob);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflict_append_update(
Packit Service 20376f
	const git_index_entry *ancestor,
Packit Service 20376f
	const git_index_entry *ours,
Packit Service 20376f
	const git_index_entry *theirs,
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	checkout_data *data = payload;
Packit Service 20376f
	checkout_conflictdata *conflict;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	conflict = git__calloc(1, sizeof(checkout_conflictdata));
Packit Service 20376f
	GITERR_CHECK_ALLOC(conflict);
Packit Service 20376f
Packit Service 20376f
	conflict->ancestor = ancestor;
Packit Service 20376f
	conflict->ours = ours;
Packit Service 20376f
	conflict->theirs = theirs;
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_conflict_detect_submodule(conflict)) < 0 ||
Packit Service 20376f
		(error = checkout_conflict_detect_binary(data->repo, conflict)) < 0)
Packit Service 20376f
	{
Packit Service 20376f
		git__free(conflict);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (git_vector_insert(&data->update_conflicts, conflict))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflicts_foreach(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	git_iterator *workdir,
Packit Service 20376f
	git_vector *pathspec,
Packit Service 20376f
	int (*cb)(const git_index_entry *, const git_index_entry *, const git_index_entry *, void *),
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	git_index_conflict_iterator *iterator = NULL;
Packit Service 20376f
	const git_index_entry *ancestor, *ours, *theirs;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_index_conflict_iterator_new(&iterator, index)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	/* Collect the conflicts */
Packit Service 20376f
	while ((error = git_index_conflict_next(&ancestor, &ours, &theirs, iterator)) == 0) {
Packit Service 20376f
		if (!conflict_pathspec_match(data, workdir, pathspec, ancestor, ours, theirs))
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		if ((error = cb(ancestor, ours, theirs, payload)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ITEROVER)
Packit Service 20376f
		error = 0;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_index_conflict_iterator_free(iterator);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflicts_load(checkout_data *data, git_iterator *workdir, git_vector *pathspec)
Packit Service 20376f
{
Packit Service 20376f
	git_index *index;
Packit Service 20376f
Packit Service 20376f
	/* Only write conficts from sources that have them: indexes. */
Packit Service 20376f
	if ((index = git_iterator_index(data->target)) == NULL)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	data->update_conflicts._cmp = checkout_conflictdata_cmp;
Packit Service 20376f
Packit Service 20376f
	if (checkout_conflicts_foreach(data, index, workdir, pathspec, checkout_conflict_append_update, data) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* Collect the REUC and NAME entries */
Packit Service 20376f
	data->update_reuc = &index->reuc;
Packit Service 20376f
	data->update_names = &index->names;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) checkout_conflicts_cmp_entry(
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const git_index_entry *entry)
Packit Service 20376f
{
Packit Service 20376f
	return strcmp((const char *)path, entry->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflicts_cmp_ancestor(const void *p, const void *c)
Packit Service 20376f
{
Packit Service 20376f
	const char *path = p;
Packit Service 20376f
	const checkout_conflictdata *conflict = c;
Packit Service 20376f
Packit Service 20376f
	if (!conflict->ancestor)
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	return checkout_conflicts_cmp_entry(path, conflict->ancestor);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static checkout_conflictdata *checkout_conflicts_search_ancestor(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const char *path)
Packit Service 20376f
{
Packit Service 20376f
	size_t pos;
Packit Service 20376f
Packit Service 20376f
	if (git_vector_bsearch2(&pos, &data->update_conflicts, checkout_conflicts_cmp_ancestor, path) < 0)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	return git_vector_get(&data->update_conflicts, pos);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static checkout_conflictdata *checkout_conflicts_search_branch(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const char *path)
Packit Service 20376f
{
Packit Service 20376f
	checkout_conflictdata *conflict;
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&data->update_conflicts, i, conflict) {
Packit Service 20376f
		int cmp = -1;
Packit Service 20376f
Packit Service 20376f
		if (conflict->ancestor)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		if (conflict->ours)
Packit Service 20376f
			cmp = checkout_conflicts_cmp_entry(path, conflict->ours);
Packit Service 20376f
		else if (conflict->theirs)
Packit Service 20376f
			cmp = checkout_conflicts_cmp_entry(path, conflict->theirs);
Packit Service 20376f
Packit Service 20376f
		if (cmp == 0)
Packit Service 20376f
			return conflict;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflicts_load_byname_entry(
Packit Service 20376f
	checkout_conflictdata **ancestor_out,
Packit Service 20376f
	checkout_conflictdata **ours_out,
Packit Service 20376f
	checkout_conflictdata **theirs_out,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_index_name_entry *name_entry)
Packit Service 20376f
{
Packit Service 20376f
	checkout_conflictdata *ancestor, *ours = NULL, *theirs = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	*ancestor_out = NULL;
Packit Service 20376f
	*ours_out = NULL;
Packit Service 20376f
	*theirs_out = NULL;
Packit Service 20376f
Packit Service 20376f
	if (!name_entry->ancestor) {
Packit Service 20376f
		giterr_set(GITERR_INDEX, "a NAME entry exists without an ancestor");
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!name_entry->ours && !name_entry->theirs) {
Packit Service 20376f
		giterr_set(GITERR_INDEX, "a NAME entry exists without an ours or theirs");
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((ancestor = checkout_conflicts_search_ancestor(data,
Packit Service 20376f
		name_entry->ancestor)) == NULL) {
Packit Service 20376f
		giterr_set(GITERR_INDEX,
Packit Service 20376f
			"a NAME entry referenced ancestor entry '%s' which does not exist in the main index",
Packit Service 20376f
			name_entry->ancestor);
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (name_entry->ours) {
Packit Service 20376f
		if (strcmp(name_entry->ancestor, name_entry->ours) == 0)
Packit Service 20376f
			ours = ancestor;
Packit Service 20376f
		else if ((ours = checkout_conflicts_search_branch(data, name_entry->ours)) == NULL ||
Packit Service 20376f
			ours->ours == NULL) {
Packit Service 20376f
			giterr_set(GITERR_INDEX,
Packit Service 20376f
				"a NAME entry referenced our entry '%s' which does not exist in the main index",
Packit Service 20376f
				name_entry->ours);
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (name_entry->theirs) {
Packit Service 20376f
		if (strcmp(name_entry->ancestor, name_entry->theirs) == 0)
Packit Service 20376f
			theirs = ancestor;
Packit Service 20376f
		else if (name_entry->ours && strcmp(name_entry->ours, name_entry->theirs) == 0)
Packit Service 20376f
			theirs = ours;
Packit Service 20376f
		else if ((theirs = checkout_conflicts_search_branch(data, name_entry->theirs)) == NULL ||
Packit Service 20376f
			theirs->theirs == NULL) {
Packit Service 20376f
			giterr_set(GITERR_INDEX,
Packit Service 20376f
				"a NAME entry referenced their entry '%s' which does not exist in the main index",
Packit Service 20376f
				name_entry->theirs);
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*ancestor_out = ancestor;
Packit Service 20376f
	*ours_out = ours;
Packit Service 20376f
	*theirs_out = theirs;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflicts_coalesce_renames(
Packit Service 20376f
	checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	const git_index_name_entry *name_entry;
Packit Service 20376f
	checkout_conflictdata *ancestor_conflict, *our_conflict, *their_conflict;
Packit Service 20376f
	size_t i, names;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((index = git_iterator_index(data->target)) == NULL)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* Juggle entries based on renames */
Packit Service 20376f
	names = git_index_name_entrycount(index);
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < names; i++) {
Packit Service 20376f
		name_entry = git_index_name_get_byindex(index, i);
Packit Service 20376f
Packit Service 20376f
		if ((error = checkout_conflicts_load_byname_entry(
Packit Service 20376f
			&ancestor_conflict, &our_conflict, &their_conflict,
Packit Service 20376f
			data, name_entry)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		if (our_conflict && our_conflict != ancestor_conflict) {
Packit Service 20376f
			ancestor_conflict->ours = our_conflict->ours;
Packit Service 20376f
			our_conflict->ours = NULL;
Packit Service 20376f
Packit Service 20376f
			if (our_conflict->theirs)
Packit Service 20376f
				our_conflict->name_collision = 1;
Packit Service 20376f
Packit Service 20376f
			if (our_conflict->name_collision)
Packit Service 20376f
				ancestor_conflict->name_collision = 1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (their_conflict && their_conflict != ancestor_conflict) {
Packit Service 20376f
			ancestor_conflict->theirs = their_conflict->theirs;
Packit Service 20376f
			their_conflict->theirs = NULL;
Packit Service 20376f
Packit Service 20376f
			if (their_conflict->ours)
Packit Service 20376f
				their_conflict->name_collision = 1;
Packit Service 20376f
Packit Service 20376f
			if (their_conflict->name_collision)
Packit Service 20376f
				ancestor_conflict->name_collision = 1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (our_conflict && our_conflict != ancestor_conflict &&
Packit Service 20376f
			their_conflict && their_conflict != ancestor_conflict)
Packit Service 20376f
			ancestor_conflict->one_to_two = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_remove_matching(
Packit Service 20376f
		&data->update_conflicts, checkout_conflictdata_empty, NULL);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflicts_mark_directoryfile(
Packit Service 20376f
	checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	checkout_conflictdata *conflict;
Packit Service 20376f
	const git_index_entry *entry;
Packit Service 20376f
	size_t i, j, len;
Packit Service 20376f
	const char *path;
Packit Service 20376f
	int prefixed, error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((index = git_iterator_index(data->target)) == NULL)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	len = git_index_entrycount(index);
Packit Service 20376f
Packit Service 20376f
	/* Find d/f conflicts */
Packit Service 20376f
	git_vector_foreach(&data->update_conflicts, i, conflict) {
Packit Service 20376f
		if ((conflict->ours && conflict->theirs) ||
Packit Service 20376f
			(!conflict->ours && !conflict->theirs))
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		path = conflict->ours ?
Packit Service 20376f
			conflict->ours->path : conflict->theirs->path;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_index_find(&j, index, path)) < 0) {
Packit Service 20376f
			if (error == GIT_ENOTFOUND)
Packit Service 20376f
				giterr_set(GITERR_INDEX,
Packit Service 20376f
					"index inconsistency, could not find entry for expected conflict '%s'", path);
Packit Service 20376f
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		for (; j < len; j++) {
Packit Service 20376f
			if ((entry = git_index_get_byindex(index, j)) == NULL) {
Packit Service 20376f
				giterr_set(GITERR_INDEX,
Packit Service 20376f
					"index inconsistency, truncated index while loading expected conflict '%s'", path);
Packit Service 20376f
				error = -1;
Packit Service 20376f
				goto done;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			prefixed = git_path_equal_or_prefixed(path, entry->path, NULL);
Packit Service 20376f
Packit Service 20376f
			if (prefixed == GIT_PATH_EQUAL)
Packit Service 20376f
				continue;
Packit Service 20376f
Packit Service 20376f
			if (prefixed == GIT_PATH_PREFIX)
Packit Service 20376f
				conflict->directoryfile = 1;
Packit Service 20376f
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_get_update_conflicts(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_iterator *workdir,
Packit Service 20376f
	git_vector *pathspec)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (data->strategy & GIT_CHECKOUT_SKIP_UNMERGED)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_conflicts_load(data, workdir, pathspec)) < 0 ||
Packit Service 20376f
		(error = checkout_conflicts_coalesce_renames(data)) < 0 ||
Packit Service 20376f
		(error = checkout_conflicts_mark_directoryfile(data)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflict_append_remove(
Packit Service 20376f
	const git_index_entry *ancestor,
Packit Service 20376f
	const git_index_entry *ours,
Packit Service 20376f
	const git_index_entry *theirs,
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	checkout_data *data = payload;
Packit Service 20376f
	const char *name;
Packit Service 20376f
Packit Service 20376f
	assert(ancestor || ours || theirs);
Packit Service 20376f
Packit Service 20376f
	if (ancestor)
Packit Service 20376f
		name = git__strdup(ancestor->path);
Packit Service 20376f
	else if (ours)
Packit Service 20376f
		name = git__strdup(ours->path);
Packit Service 20376f
	else if (theirs)
Packit Service 20376f
		name = git__strdup(theirs->path);
Packit Service 20376f
	else
Packit Service 20376f
		abort();
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC(name);
Packit Service 20376f
Packit Service 20376f
	return git_vector_insert(&data->remove_conflicts, (char *)name);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_get_remove_conflicts(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_iterator *workdir,
Packit Service 20376f
	git_vector *pathspec)
Packit Service 20376f
{
Packit Service 20376f
	if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	return checkout_conflicts_foreach(data, data->index, workdir, pathspec, checkout_conflict_append_remove, data);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_verify_paths(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	int action,
Packit Service 20376f
	git_diff_delta *delta)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int flags = GIT_PATH_REJECT_WORKDIR_DEFAULTS;
Packit Service 20376f
Packit Service 20376f
	if (action & CHECKOUT_ACTION__REMOVE) {
Packit Service 20376f
		if (!git_path_isvalid(repo, delta->old_file.path, delta->old_file.mode, flags)) {
Packit Service 20376f
			giterr_set(GITERR_CHECKOUT, "cannot remove invalid path '%s'", delta->old_file.path);
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (action & ~CHECKOUT_ACTION__REMOVE) {
Packit Service 20376f
		if (!git_path_isvalid(repo, delta->new_file.path, delta->new_file.mode, flags)) {
Packit Service 20376f
			giterr_set(GITERR_CHECKOUT, "cannot checkout to invalid path '%s'", delta->new_file.path);
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_get_actions(
Packit Service 20376f
	uint32_t **actions_ptr,
Packit Service 20376f
	size_t **counts_ptr,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_iterator *workdir)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0, act;
Packit Service 20376f
	const git_index_entry *wditem;
Packit Service 20376f
	git_vector pathspec = GIT_VECTOR_INIT, *deltas;
Packit Service 20376f
	git_pool pathpool;
Packit Service 20376f
	git_diff_delta *delta;
Packit Service 20376f
	size_t i, *counts = NULL;
Packit Service 20376f
	uint32_t *actions = NULL;
Packit Service 20376f
Packit Service 20376f
	git_pool_init(&pathpool, 1);
Packit Service 20376f
Packit Service 20376f
	if (data->opts.paths.count > 0 &&
Packit Service 20376f
		git_pathspec__vinit(&pathspec, &data->opts.paths, &pathpool) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_iterator_current(&wditem, workdir)) < 0 &&
Packit Service 20376f
		error != GIT_ITEROVER)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	deltas = &data->diff->deltas;
Packit Service 20376f
Packit Service 20376f
	*counts_ptr = counts = git__calloc(CHECKOUT_ACTION__MAX+1, sizeof(size_t));
Packit Service 20376f
	*actions_ptr = actions = git__calloc(
Packit Service 20376f
		deltas->length ? deltas->length : 1, sizeof(uint32_t));
Packit Service 20376f
	if (!counts || !actions) {
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto fail;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(deltas, i, delta) {
Packit Service 20376f
		if ((error = checkout_action(&act, data, delta, workdir, &wditem, &pathspec)) == 0)
Packit Service 20376f
			error = checkout_verify_paths(data->repo, act, delta);
Packit Service 20376f
Packit Service 20376f
		if (error != 0)
Packit Service 20376f
			goto fail;
Packit Service 20376f
Packit Service 20376f
		actions[i] = act;
Packit Service 20376f
Packit Service 20376f
		if (act & CHECKOUT_ACTION__REMOVE)
Packit Service 20376f
			counts[CHECKOUT_ACTION__REMOVE]++;
Packit Service 20376f
		if (act & CHECKOUT_ACTION__UPDATE_BLOB)
Packit Service 20376f
			counts[CHECKOUT_ACTION__UPDATE_BLOB]++;
Packit Service 20376f
		if (act & CHECKOUT_ACTION__UPDATE_SUBMODULE)
Packit Service 20376f
			counts[CHECKOUT_ACTION__UPDATE_SUBMODULE]++;
Packit Service 20376f
		if (act & CHECKOUT_ACTION__CONFLICT)
Packit Service 20376f
			counts[CHECKOUT_ACTION__CONFLICT]++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = checkout_remaining_wd_items(data, workdir, wditem, &pathspec);
Packit Service 20376f
	if (error)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	counts[CHECKOUT_ACTION__REMOVE] += data->removes.length;
Packit Service 20376f
Packit Service 20376f
	if (counts[CHECKOUT_ACTION__CONFLICT] > 0 &&
Packit Service 20376f
		(data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) == 0)
Packit Service 20376f
	{
Packit Service 20376f
		giterr_set(GITERR_CHECKOUT, "%"PRIuZ" %s checkout",
Packit Service 20376f
			counts[CHECKOUT_ACTION__CONFLICT],
Packit Service 20376f
			counts[CHECKOUT_ACTION__CONFLICT] == 1 ?
Packit Service 20376f
			"conflict prevents" : "conflicts prevent");
Packit Service 20376f
		error = GIT_ECONFLICT;
Packit Service 20376f
		goto fail;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_get_remove_conflicts(data, workdir, &pathspec)) < 0 ||
Packit Service 20376f
		(error = checkout_get_update_conflicts(data, workdir, &pathspec)) < 0)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	counts[CHECKOUT_ACTION__REMOVE_CONFLICT] = git_vector_length(&data->remove_conflicts);
Packit Service 20376f
	counts[CHECKOUT_ACTION__UPDATE_CONFLICT] = git_vector_length(&data->update_conflicts);
Packit Service 20376f
Packit Service 20376f
	git_pathspec__vfree(&pathspec);
Packit Service 20376f
	git_pool_clear(&pathpool);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
fail:
Packit Service 20376f
	*counts_ptr = NULL;
Packit Service 20376f
	git__free(counts);
Packit Service 20376f
	*actions_ptr = NULL;
Packit Service 20376f
	git__free(actions);
Packit Service 20376f
Packit Service 20376f
	git_pathspec__vfree(&pathspec);
Packit Service 20376f
	git_pool_clear(&pathpool);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool should_remove_existing(checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	int ignorecase;
Packit Service 20376f
Packit Service 20376f
	if (git_repository__cvar(&ignorecase, data->repo, GIT_CVAR_IGNORECASE) < 0) {
Packit Service 20376f
		ignorecase = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return (ignorecase &&
Packit Service 20376f
		(data->strategy & GIT_CHECKOUT_DONT_REMOVE_EXISTING) == 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define MKDIR_NORMAL \
Packit Service 20376f
	GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR
Packit Service 20376f
#define MKDIR_REMOVE_EXISTING \
Packit Service 20376f
	MKDIR_NORMAL | GIT_MKDIR_REMOVE_FILES | GIT_MKDIR_REMOVE_SYMLINKS
Packit Service 20376f
Packit Service 20376f
static int checkout_mkdir(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const char *base,
Packit Service 20376f
	mode_t mode,
Packit Service 20376f
	unsigned int flags)
Packit Service 20376f
{
Packit Service 20376f
	struct git_futils_mkdir_options mkdir_opts = {0};
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	mkdir_opts.dir_map = data->mkdir_map;
Packit Service 20376f
	mkdir_opts.pool = &data->pool;
Packit Service 20376f
Packit Service 20376f
	error = git_futils_mkdir_relative(
Packit Service 20376f
		path, base, mode, flags, &mkdir_opts);
Packit Service 20376f
Packit Service 20376f
	data->perfdata.mkdir_calls += mkdir_opts.perfdata.mkdir_calls;
Packit Service 20376f
	data->perfdata.stat_calls += mkdir_opts.perfdata.stat_calls;
Packit Service 20376f
	data->perfdata.chmod_calls += mkdir_opts.perfdata.chmod_calls;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int mkpath2file(
Packit Service 20376f
	checkout_data *data, const char *path, unsigned int mode)
Packit Service 20376f
{
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	bool remove_existing = should_remove_existing(data);
Packit Service 20376f
	unsigned int flags =
Packit Service 20376f
		(remove_existing ? MKDIR_REMOVE_EXISTING : MKDIR_NORMAL) |
Packit Service 20376f
		GIT_MKDIR_SKIP_LAST;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_mkdir(
Packit Service 20376f
			data, path, data->opts.target_directory, mode, flags)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (remove_existing) {
Packit Service 20376f
		data->perfdata.stat_calls++;
Packit Service 20376f
Packit Service 20376f
		if (p_lstat(path, &st) == 0) {
Packit Service 20376f
Packit Service 20376f
			/* Some file, symlink or folder already exists at this name.
Packit Service 20376f
			 * We would have removed it in remove_the_old unless we're on
Packit Service 20376f
			 * a case inensitive filesystem (or the user has asked us not
Packit Service 20376f
			 * to).  Remove the similarly named file to write the new.
Packit Service 20376f
			 */
Packit Service 20376f
			error = git_futils_rmdir_r(path, NULL, GIT_RMDIR_REMOVE_FILES);
Packit Service 20376f
		} else if (errno != ENOENT) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to stat '%s'", path);
Packit Service 20376f
			return GIT_EEXISTS;
Packit Service 20376f
		} else {
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
struct checkout_stream {
Packit Service 20376f
	git_writestream base;
Packit Service 20376f
	const char *path;
Packit Service 20376f
	int fd;
Packit Service 20376f
	int open;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static int checkout_stream_write(
Packit Service 20376f
	git_writestream *s, const char *buffer, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	struct checkout_stream *stream = (struct checkout_stream *)s;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	if ((ret = p_write(stream->fd, buffer, len)) < 0)
Packit Service 20376f
		giterr_set(GITERR_OS, "could not write to '%s'", stream->path);
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_stream_close(git_writestream *s)
Packit Service 20376f
{
Packit Service 20376f
	struct checkout_stream *stream = (struct checkout_stream *)s;
Packit Service 20376f
	assert(stream && stream->open);
Packit Service 20376f
Packit Service 20376f
	stream->open = 0;
Packit Service 20376f
	return p_close(stream->fd);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void checkout_stream_free(git_writestream *s)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(s);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int blob_content_to_file(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	struct stat *st,
Packit Service 20376f
	git_blob *blob,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const char *hint_path,
Packit Service 20376f
	mode_t entry_filemode)
Packit Service 20376f
{
Packit Service 20376f
	int flags = data->opts.file_open_flags;
Packit Service 20376f
	mode_t file_mode = data->opts.file_mode ?
Packit Service 20376f
		data->opts.file_mode : entry_filemode;
Packit Service 20376f
	git_filter_options filter_opts = GIT_FILTER_OPTIONS_INIT;
Packit Service 20376f
	struct checkout_stream writer;
Packit Service 20376f
	mode_t mode;
Packit Service 20376f
	git_filter_list *fl = NULL;
Packit Service 20376f
	int fd;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (hint_path == NULL)
Packit Service 20376f
		hint_path = path;
Packit Service 20376f
Packit Service 20376f
	if ((error = mkpath2file(data, path, data->opts.dir_mode)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (flags <= 0)
Packit Service 20376f
		flags = O_CREAT | O_TRUNC | O_WRONLY;
Packit Service 20376f
	if (!(mode = file_mode))
Packit Service 20376f
		mode = GIT_FILEMODE_BLOB;
Packit Service 20376f
Packit Service 20376f
	if ((fd = p_open(path, flags, mode)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "could not open '%s' for writing", path);
Packit Service 20376f
		return fd;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	filter_opts.attr_session = &data->attr_session;
Packit Service 20376f
	filter_opts.temp_buf = &data->tmp;
Packit Service 20376f
Packit Service 20376f
	if (!data->opts.disable_filters &&
Packit Service 20376f
		(error = git_filter_list__load_ext(
Packit Service 20376f
			&fl, data->repo, blob, hint_path,
Packit Service 20376f
			GIT_FILTER_TO_WORKTREE, &filter_opts))) {
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* setup the writer */
Packit Service 20376f
	memset(&writer, 0, sizeof(struct checkout_stream));
Packit Service 20376f
	writer.base.write = checkout_stream_write;
Packit Service 20376f
	writer.base.close = checkout_stream_close;
Packit Service 20376f
	writer.base.free = checkout_stream_free;
Packit Service 20376f
	writer.path = path;
Packit Service 20376f
	writer.fd = fd;
Packit Service 20376f
	writer.open = 1;
Packit Service 20376f
Packit Service 20376f
	error = git_filter_list_stream_blob(fl, blob, &writer.base);
Packit Service 20376f
Packit Service 20376f
	assert(writer.open == 0);
Packit Service 20376f
Packit Service 20376f
	git_filter_list_free(fl);
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (st) {
Packit Service 20376f
		data->perfdata.stat_calls++;
Packit Service 20376f
Packit Service 20376f
		if ((error = p_stat(path, st)) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to stat '%s'", path);
Packit Service 20376f
			return error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		st->st_mode = entry_filemode;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int blob_content_to_link(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	struct stat *st,
Packit Service 20376f
	git_blob *blob,
Packit Service 20376f
	const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf linktarget = GIT_BUF_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = mkpath2file(data, path, data->opts.dir_mode)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_blob__getbuf(&linktarget, blob)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (data->can_symlink) {
Packit Service 20376f
		if ((error = p_symlink(git_buf_cstr(&linktarget), path)) < 0)
Packit Service 20376f
			giterr_set(GITERR_OS, "could not create symlink %s", path);
Packit Service 20376f
	} else {
Packit Service 20376f
		error = git_futils_fake_symlink(git_buf_cstr(&linktarget), path);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!error) {
Packit Service 20376f
		data->perfdata.stat_calls++;
Packit Service 20376f
Packit Service 20376f
		if ((error = p_lstat(path, st)) < 0)
Packit Service 20376f
			giterr_set(GITERR_CHECKOUT, "could not stat symlink %s", path);
Packit Service 20376f
Packit Service 20376f
		st->st_mode = GIT_FILEMODE_LINK;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&linktarget);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_update_index(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_file *file,
Packit Service 20376f
	struct stat *st)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry entry;
Packit Service 20376f
Packit Service 20376f
	if (!data->index)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	memset(&entry, 0, sizeof(entry));
Packit Service 20376f
	entry.path = (char *)file->path; /* cast to prevent warning */
Packit Service 20376f
	git_index_entry__init_from_stat(&entry, st, true);
Packit Service 20376f
	git_oid_cpy(&entry.id, &file->id);
Packit Service 20376f
Packit Service 20376f
	return git_index_add(data->index, &entry);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_submodule_update_index(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_file *file)
Packit Service 20376f
{
Packit Service 20376f
	git_buf *fullpath;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	/* update the index unless prevented */
Packit Service 20376f
	if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) != 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (checkout_target_fullpath(&fullpath, data, file->path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	data->perfdata.stat_calls++;
Packit Service 20376f
	if (p_stat(fullpath->ptr, &st) < 0) {
Packit Service 20376f
		giterr_set(
Packit Service 20376f
			GITERR_CHECKOUT, "could not stat submodule %s\n", file->path);
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	st.st_mode = GIT_FILEMODE_COMMIT;
Packit Service 20376f
Packit Service 20376f
	return checkout_update_index(data, file, &st);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_submodule(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_file *file)
Packit Service 20376f
{
Packit Service 20376f
	bool remove_existing = should_remove_existing(data);
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	/* Until submodules are supported, UPDATE_ONLY means do nothing here */
Packit Service 20376f
	if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_mkdir(
Packit Service 20376f
			data,
Packit Service 20376f
			file->path, data->opts.target_directory, data->opts.dir_mode,
Packit Service 20376f
			remove_existing ? MKDIR_REMOVE_EXISTING : MKDIR_NORMAL)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_submodule_lookup(NULL, data->repo, file->path)) < 0) {
Packit Service 20376f
		/* I've observed repos with submodules in the tree that do not
Packit Service 20376f
		 * have a .gitmodules - core Git just makes an empty directory
Packit Service 20376f
		 */
Packit Service 20376f
		if (error == GIT_ENOTFOUND) {
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			return checkout_submodule_update_index(data, file);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* TODO: Support checkout_strategy options.  Two circumstances:
Packit Service 20376f
	 * 1 - submodule already checked out, but we need to move the HEAD
Packit Service 20376f
	 *     to the new OID, or
Packit Service 20376f
	 * 2 - submodule not checked out and we should recursively check it out
Packit Service 20376f
	 *
Packit Service 20376f
	 * Checkout will not execute a pull on the submodule, but a clone
Packit Service 20376f
	 * command should probably be able to.  Do we need a submodule callback?
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
	return checkout_submodule_update_index(data, file);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void report_progress(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const char *path)
Packit Service 20376f
{
Packit Service 20376f
	if (data->opts.progress_cb)
Packit Service 20376f
		data->opts.progress_cb(
Packit Service 20376f
			path, data->completed_steps, data->total_steps,
Packit Service 20376f
			data->opts.progress_payload);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_safe_for_update_only(
Packit Service 20376f
	checkout_data *data, const char *path, mode_t expected_mode)
Packit Service 20376f
{
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	data->perfdata.stat_calls++;
Packit Service 20376f
Packit Service 20376f
	if (p_lstat(path, &st) < 0) {
Packit Service 20376f
		/* if doesn't exist, then no error and no update */
Packit Service 20376f
		if (errno == ENOENT || errno == ENOTDIR)
Packit Service 20376f
			return 0;
Packit Service 20376f
Packit Service 20376f
		/* otherwise, stat error and no update */
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to stat '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* only safe for update if this is the same type of file */
Packit Service 20376f
	if ((st.st_mode & ~0777) == (expected_mode & ~0777))
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_write_content(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_oid *oid,
Packit Service 20376f
	const char *full_path,
Packit Service 20376f
	const char *hint_path,
Packit Service 20376f
	unsigned int mode,
Packit Service 20376f
	struct stat *st)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_blob *blob;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_blob_lookup(&blob, data->repo, oid)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (S_ISLNK(mode))
Packit Service 20376f
		error = blob_content_to_link(data, st, blob, full_path);
Packit Service 20376f
	else
Packit Service 20376f
		error = blob_content_to_file(data, st, blob, full_path, hint_path, mode);
Packit Service 20376f
Packit Service 20376f
	git_blob_free(blob);
Packit Service 20376f
Packit Service 20376f
	/* if we try to create the blob and an existing directory blocks it from
Packit Service 20376f
	 * being written, then there must have been a typechange conflict in a
Packit Service 20376f
	 * parent directory - suppress the error and try to continue.
Packit Service 20376f
	 */
Packit Service 20376f
	if ((data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) != 0 &&
Packit Service 20376f
		(error == GIT_ENOTFOUND || error == GIT_EEXISTS))
Packit Service 20376f
	{
Packit Service 20376f
		giterr_clear();
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 checkout_blob(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_diff_file *file)
Packit Service 20376f
{
Packit Service 20376f
	git_buf *fullpath;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (checkout_target_fullpath(&fullpath, data, file->path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0) {
Packit Service 20376f
		int rval = checkout_safe_for_update_only(
Packit Service 20376f
			data, fullpath->ptr, file->mode);
Packit Service 20376f
Packit Service 20376f
		if (rval <= 0)
Packit Service 20376f
			return rval;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = checkout_write_content(
Packit Service 20376f
		data, &file->id, fullpath->ptr, NULL, file->mode, &st);
Packit Service 20376f
Packit Service 20376f
	/* update the index unless prevented */
Packit Service 20376f
	if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
Packit Service 20376f
		error = checkout_update_index(data, file, &st);
Packit Service 20376f
Packit Service 20376f
	/* update the submodule data if this was a new .gitmodules file */
Packit Service 20376f
	if (!error && strcmp(file->path, ".gitmodules") == 0)
Packit Service 20376f
		data->reload_submodules = true;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_remove_the_old(
Packit Service 20376f
	unsigned int *actions,
Packit Service 20376f
	checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_diff_delta *delta;
Packit Service 20376f
	const char *str;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	git_buf *fullpath;
Packit Service 20376f
	uint32_t flg = GIT_RMDIR_EMPTY_PARENTS |
Packit Service 20376f
		GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS;
Packit Service 20376f
Packit Service 20376f
	if (data->opts.checkout_strategy & GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES)
Packit Service 20376f
		flg |= GIT_RMDIR_SKIP_NONEMPTY;
Packit Service 20376f
Packit Service 20376f
	if (checkout_target_fullpath(&fullpath, data, NULL) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&data->diff->deltas, i, delta) {
Packit Service 20376f
		if (actions[i] & CHECKOUT_ACTION__REMOVE) {
Packit Service 20376f
			error = git_futils_rmdir_r(
Packit Service 20376f
				delta->old_file.path, fullpath->ptr, flg);
Packit Service 20376f
Packit Service 20376f
			if (error < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
Packit Service 20376f
			data->completed_steps++;
Packit Service 20376f
			report_progress(data, delta->old_file.path);
Packit Service 20376f
Packit Service 20376f
			if ((actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) == 0 &&
Packit Service 20376f
				(data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 &&
Packit Service 20376f
				data->index != NULL)
Packit Service 20376f
			{
Packit Service 20376f
				(void)git_index_remove(data->index, delta->old_file.path, 0);
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&data->removes, i, str) {
Packit Service 20376f
		error = git_futils_rmdir_r(str, fullpath->ptr, flg);
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		data->completed_steps++;
Packit Service 20376f
		report_progress(data, str);
Packit Service 20376f
Packit Service 20376f
		if ((data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0 &&
Packit Service 20376f
			data->index != NULL)
Packit Service 20376f
		{
Packit Service 20376f
			if (str[strlen(str) - 1] == '/')
Packit Service 20376f
				(void)git_index_remove_directory(data->index, str, 0);
Packit Service 20376f
			else
Packit Service 20376f
				(void)git_index_remove(data->index, str, 0);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_deferred_remove(git_repository *repo, const char *path)
Packit Service 20376f
{
Packit Service 20376f
#if 0
Packit Service 20376f
	int error = git_futils_rmdir_r(
Packit Service 20376f
		path, data->opts.target_directory, GIT_RMDIR_EMPTY_PARENTS);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ENOTFOUND) {
Packit Service 20376f
		error = 0;
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
#else
Packit Service 20376f
	GIT_UNUSED(repo);
Packit Service 20376f
	GIT_UNUSED(path);
Packit Service 20376f
	assert(false);
Packit Service 20376f
	return 0;
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_create_the_new(
Packit Service 20376f
	unsigned int *actions,
Packit Service 20376f
	checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_diff_delta *delta;
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&data->diff->deltas, i, delta) {
Packit Service 20376f
		if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) {
Packit Service 20376f
			/* this had a blocker directory that should only be removed iff
Packit Service 20376f
			 * all of the contents of the directory were safely removed
Packit Service 20376f
			 */
Packit Service 20376f
			if ((error = checkout_deferred_remove(
Packit Service 20376f
					data->repo, delta->old_file.path)) < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (actions[i] & CHECKOUT_ACTION__UPDATE_BLOB) {
Packit Service 20376f
			error = checkout_blob(data, &delta->new_file);
Packit Service 20376f
			if (error < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
Packit Service 20376f
			data->completed_steps++;
Packit Service 20376f
			report_progress(data, delta->new_file.path);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_create_submodules(
Packit Service 20376f
	unsigned int *actions,
Packit Service 20376f
	checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_diff_delta *delta;
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&data->diff->deltas, i, delta) {
Packit Service 20376f
		if (actions[i] & CHECKOUT_ACTION__DEFER_REMOVE) {
Packit Service 20376f
			/* this has a blocker directory that should only be removed iff
Packit Service 20376f
			 * all of the contents of the directory were safely removed
Packit Service 20376f
			 */
Packit Service 20376f
			if ((error = checkout_deferred_remove(
Packit Service 20376f
					data->repo, delta->old_file.path)) < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (actions[i] & CHECKOUT_ACTION__UPDATE_SUBMODULE) {
Packit Service 20376f
			int error = checkout_submodule(data, &delta->new_file);
Packit Service 20376f
			if (error < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
Packit Service 20376f
			data->completed_steps++;
Packit Service 20376f
			report_progress(data, delta->new_file.path);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_lookup_head_tree(git_tree **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_reference *ref = NULL;
Packit Service 20376f
	git_object *head;
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_repository_head(&ref, repo)) &&
Packit Service 20376f
		!(error = git_reference_peel(&head, ref, GIT_OBJ_TREE)))
Packit Service 20376f
		*out = (git_tree *)head;
Packit Service 20376f
Packit Service 20376f
	git_reference_free(ref);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static int conflict_entry_name(
Packit Service 20376f
	git_buf *out,
Packit Service 20376f
	const char *side_name,
Packit Service 20376f
	const char *filename)
Packit Service 20376f
{
Packit Service 20376f
	if (git_buf_puts(out, side_name) < 0 ||
Packit Service 20376f
		git_buf_putc(out, ':') < 0 ||
Packit Service 20376f
		git_buf_puts(out, filename) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_path_suffixed(git_buf *path, const char *suffix)
Packit Service 20376f
{
Packit Service 20376f
	size_t path_len;
Packit Service 20376f
	int i = 0, error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_putc(path, '~')) < 0 || (error = git_buf_puts(path, suffix)) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	path_len = git_buf_len(path);
Packit Service 20376f
Packit Service 20376f
	while (git_path_exists(git_buf_cstr(path)) && i < INT_MAX) {
Packit Service 20376f
		git_buf_truncate(path, path_len);
Packit Service 20376f
Packit Service 20376f
		if ((error = git_buf_putc(path, '_')) < 0 ||
Packit Service 20376f
			(error = git_buf_printf(path, "%d", i)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		i++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (i == INT_MAX) {
Packit Service 20376f
		git_buf_truncate(path, path_len);
Packit Service 20376f
Packit Service 20376f
		giterr_set(GITERR_CHECKOUT, "could not write '%s': working directory file exists", path->ptr);
Packit Service 20376f
		return GIT_EEXISTS;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_write_entry(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	checkout_conflictdata *conflict,
Packit Service 20376f
	const git_index_entry *side)
Packit Service 20376f
{
Packit Service 20376f
	const char *hint_path = NULL, *suffix;
Packit Service 20376f
	git_buf *fullpath;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert (side == conflict->ours || side == conflict->theirs);
Packit Service 20376f
Packit Service 20376f
	if (checkout_target_fullpath(&fullpath, data, side->path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if ((conflict->name_collision || conflict->directoryfile) &&
Packit Service 20376f
		(data->strategy & GIT_CHECKOUT_USE_OURS) == 0 &&
Packit Service 20376f
		(data->strategy & GIT_CHECKOUT_USE_THEIRS) == 0) {
Packit Service 20376f
Packit Service 20376f
		if (side == conflict->ours)
Packit Service 20376f
			suffix = data->opts.our_label ? data->opts.our_label :
Packit Service 20376f
				"ours";
Packit Service 20376f
		else
Packit Service 20376f
			suffix = data->opts.their_label ? data->opts.their_label :
Packit Service 20376f
				"theirs";
Packit Service 20376f
Packit Service 20376f
		if (checkout_path_suffixed(fullpath, suffix) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		hint_path = side->path;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 &&
Packit Service 20376f
		(error = checkout_safe_for_update_only(data, fullpath->ptr, side->mode)) <= 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	return checkout_write_content(data,
Packit Service 20376f
		&side->id, fullpath->ptr, hint_path, side->mode, &st);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_write_entries(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	checkout_conflictdata *conflict)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_write_entry(data, conflict, conflict->ours)) >= 0)
Packit Service 20376f
		error = checkout_write_entry(data, conflict, conflict->theirs);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_merge_path(
Packit Service 20376f
	git_buf *out,
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	checkout_conflictdata *conflict,
Packit Service 20376f
	git_merge_file_result *result)
Packit Service 20376f
{
Packit Service 20376f
	const char *our_label_raw, *their_label_raw, *suffix;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(out, git_repository_workdir(data->repo), result->path)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* Most conflicts simply use the filename in the index */
Packit Service 20376f
	if (!conflict->name_collision)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* Rename 2->1 conflicts need the branch name appended */
Packit Service 20376f
	our_label_raw = data->opts.our_label ? data->opts.our_label : "ours";
Packit Service 20376f
	their_label_raw = data->opts.their_label ? data->opts.their_label : "theirs";
Packit Service 20376f
	suffix = strcmp(result->path, conflict->ours->path) == 0 ? our_label_raw : their_label_raw;
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_path_suffixed(out, suffix)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_write_merge(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	checkout_conflictdata *conflict)
Packit Service 20376f
{
Packit Service 20376f
	git_buf our_label = GIT_BUF_INIT, their_label = GIT_BUF_INIT,
Packit Service 20376f
		path_suffixed = GIT_BUF_INIT, path_workdir = GIT_BUF_INIT,
Packit Service 20376f
		in_data = GIT_BUF_INIT, out_data = GIT_BUF_INIT;
Packit Service 20376f
	git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
Packit Service 20376f
	git_merge_file_result result = {0};
Packit Service 20376f
	git_filebuf output = GIT_FILEBUF_INIT;
Packit Service 20376f
	git_filter_list *fl = NULL;
Packit Service 20376f
	git_filter_options filter_opts = GIT_FILTER_OPTIONS_INIT;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (data->opts.checkout_strategy & GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)
Packit Service 20376f
		opts.flags |= GIT_MERGE_FILE_STYLE_DIFF3;
Packit Service 20376f
Packit Service 20376f
	opts.ancestor_label = data->opts.ancestor_label ?
Packit Service 20376f
		data->opts.ancestor_label : "ancestor";
Packit Service 20376f
	opts.our_label = data->opts.our_label ?
Packit Service 20376f
		data->opts.our_label : "ours";
Packit Service 20376f
	opts.their_label = data->opts.their_label ?
Packit Service 20376f
		data->opts.their_label : "theirs";
Packit Service 20376f
Packit Service 20376f
	/* If all the paths are identical, decorate the diff3 file with the branch
Packit Service 20376f
	 * names.  Otherwise, append branch_name:path.
Packit Service 20376f
	 */
Packit Service 20376f
	if (conflict->ours && conflict->theirs &&
Packit Service 20376f
		strcmp(conflict->ours->path, conflict->theirs->path) != 0) {
Packit Service 20376f
Packit Service 20376f
		if ((error = conflict_entry_name(
Packit Service 20376f
			&our_label, opts.our_label, conflict->ours->path)) < 0 ||
Packit Service 20376f
			(error = conflict_entry_name(
Packit Service 20376f
			&their_label, opts.their_label, conflict->theirs->path)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		opts.our_label = git_buf_cstr(&our_label);
Packit Service 20376f
		opts.their_label = git_buf_cstr(&their_label);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_merge_file_from_index(&result, data->repo,
Packit Service 20376f
		conflict->ancestor, conflict->ours, conflict->theirs, &opts)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (result.path == NULL || result.mode == 0) {
Packit Service 20376f
		giterr_set(GITERR_CHECKOUT, "could not merge contents of file");
Packit Service 20376f
		error = GIT_ECONFLICT;
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_merge_path(&path_workdir, data, conflict, &result)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 &&
Packit Service 20376f
		(error = checkout_safe_for_update_only(data, git_buf_cstr(&path_workdir), result.mode)) <= 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (!data->opts.disable_filters) {
Packit Service 20376f
		in_data.ptr = (char *)result.ptr;
Packit Service 20376f
		in_data.size = result.len;
Packit Service 20376f
Packit Service 20376f
		filter_opts.attr_session = &data->attr_session;
Packit Service 20376f
		filter_opts.temp_buf = &data->tmp;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_filter_list__load_ext(
Packit Service 20376f
				&fl, data->repo, NULL, git_buf_cstr(&path_workdir),
Packit Service 20376f
				GIT_FILTER_TO_WORKTREE, &filter_opts)) < 0 ||
Packit Service 20376f
			(error = git_filter_list_apply_to_data(&out_data, fl, &in_data)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	} else {
Packit Service 20376f
		out_data.ptr = (char *)result.ptr;
Packit Service 20376f
		out_data.size = result.len;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = mkpath2file(data, path_workdir.ptr, data->opts.dir_mode)) < 0 ||
Packit Service 20376f
		(error = git_filebuf_open(&output, git_buf_cstr(&path_workdir), GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 ||
Packit Service 20376f
		(error = git_filebuf_write(&output, out_data.ptr, out_data.size)) < 0 ||
Packit Service 20376f
		(error = git_filebuf_commit(&output)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_filter_list_free(fl);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&out_data);
Packit Service 20376f
	git_buf_free(&our_label);
Packit Service 20376f
	git_buf_free(&their_label);
Packit Service 20376f
Packit Service 20376f
	git_merge_file_result_free(&result);
Packit Service 20376f
	git_buf_free(&path_workdir);
Packit Service 20376f
	git_buf_free(&path_suffixed);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflict_add(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	const git_index_entry *conflict)
Packit Service 20376f
{
Packit Service 20376f
	int error = git_index_remove(data->index, conflict->path, 0);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	else if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	return git_index_add(data->index, conflict);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_conflict_update_index(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	checkout_conflictdata *conflict)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (conflict->ancestor)
Packit Service 20376f
		error = checkout_conflict_add(data, conflict->ancestor);
Packit Service 20376f
Packit Service 20376f
	if (!error && conflict->ours)
Packit Service 20376f
		error = checkout_conflict_add(data, conflict->ours);
Packit Service 20376f
Packit Service 20376f
	if (!error && conflict->theirs)
Packit Service 20376f
		error = checkout_conflict_add(data, conflict->theirs);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_create_conflicts(checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	checkout_conflictdata *conflict;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&data->update_conflicts, i, conflict) {
Packit Service 20376f
Packit Service 20376f
		/* Both deleted: nothing to do */
Packit Service 20376f
		if (conflict->ours == NULL && conflict->theirs == NULL)
Packit Service 20376f
			error = 0;
Packit Service 20376f
Packit Service 20376f
		else if ((data->strategy & GIT_CHECKOUT_USE_OURS) &&
Packit Service 20376f
			conflict->ours)
Packit Service 20376f
			error = checkout_write_entry(data, conflict, conflict->ours);
Packit Service 20376f
		else if ((data->strategy & GIT_CHECKOUT_USE_THEIRS) &&
Packit Service 20376f
			conflict->theirs)
Packit Service 20376f
			error = checkout_write_entry(data, conflict, conflict->theirs);
Packit Service 20376f
Packit Service 20376f
		/* Ignore the other side of name collisions. */
Packit Service 20376f
		else if ((data->strategy & GIT_CHECKOUT_USE_OURS) &&
Packit Service 20376f
			!conflict->ours && conflict->name_collision)
Packit Service 20376f
			error = 0;
Packit Service 20376f
		else if ((data->strategy & GIT_CHECKOUT_USE_THEIRS) &&
Packit Service 20376f
			!conflict->theirs && conflict->name_collision)
Packit Service 20376f
			error = 0;
Packit Service 20376f
Packit Service 20376f
		/* For modify/delete, name collisions and d/f conflicts, write
Packit Service 20376f
		 * the file (potentially with the name mangled.
Packit Service 20376f
		 */
Packit Service 20376f
		else if (conflict->ours != NULL && conflict->theirs == NULL)
Packit Service 20376f
			error = checkout_write_entry(data, conflict, conflict->ours);
Packit Service 20376f
		else if (conflict->ours == NULL && conflict->theirs != NULL)
Packit Service 20376f
			error = checkout_write_entry(data, conflict, conflict->theirs);
Packit Service 20376f
Packit Service 20376f
		/* Add/add conflicts and rename 1->2 conflicts, write the
Packit Service 20376f
		 * ours/theirs sides (potentially name mangled).
Packit Service 20376f
		 */
Packit Service 20376f
		else if (conflict->one_to_two)
Packit Service 20376f
			error = checkout_write_entries(data, conflict);
Packit Service 20376f
Packit Service 20376f
		/* If all sides are links, write the ours side */
Packit Service 20376f
		else if (S_ISLNK(conflict->ours->mode) &&
Packit Service 20376f
			S_ISLNK(conflict->theirs->mode))
Packit Service 20376f
			error = checkout_write_entry(data, conflict, conflict->ours);
Packit Service 20376f
		/* Link/file conflicts, write the file side */
Packit Service 20376f
		else if (S_ISLNK(conflict->ours->mode))
Packit Service 20376f
			error = checkout_write_entry(data, conflict, conflict->theirs);
Packit Service 20376f
		else if (S_ISLNK(conflict->theirs->mode))
Packit Service 20376f
			error = checkout_write_entry(data, conflict, conflict->ours);
Packit Service 20376f
Packit Service 20376f
		/* If any side is a gitlink, do nothing. */
Packit Service 20376f
		else if (conflict->submodule)
Packit Service 20376f
			error = 0;
Packit Service 20376f
Packit Service 20376f
		/* If any side is binary, write the ours side */
Packit Service 20376f
		else if (conflict->binary)
Packit Service 20376f
			error = checkout_write_entry(data, conflict, conflict->ours);
Packit Service 20376f
Packit Service 20376f
		else if (!error)
Packit Service 20376f
			error = checkout_write_merge(data, conflict);
Packit Service 20376f
Packit Service 20376f
		/* Update the index extensions (REUC and NAME) if we're checking
Packit Service 20376f
		 * out a different index. (Otherwise just leave them there.)
Packit Service 20376f
		 */
Packit Service 20376f
		if (!error && (data->strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
Packit Service 20376f
			error = checkout_conflict_update_index(data, conflict);
Packit Service 20376f
Packit Service 20376f
		if (error)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		data->completed_steps++;
Packit Service 20376f
		report_progress(data,
Packit Service 20376f
			conflict->ours ? conflict->ours->path :
Packit Service 20376f
			(conflict->theirs ? conflict->theirs->path : conflict->ancestor->path));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_remove_conflicts(checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	const char *conflict;
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	git_vector_foreach(&data->remove_conflicts, i, conflict) {
Packit Service 20376f
		if (git_index_conflict_remove(data->index, conflict) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		data->completed_steps++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_extensions_update_index(checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_reuc_entry *reuc_entry;
Packit Service 20376f
	const git_index_name_entry *name_entry;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (data->update_reuc) {
Packit Service 20376f
		git_vector_foreach(data->update_reuc, i, reuc_entry) {
Packit Service 20376f
			if ((error = git_index_reuc_add(data->index, reuc_entry->path,
Packit Service 20376f
				reuc_entry->mode[0], &reuc_entry->oid[0],
Packit Service 20376f
				reuc_entry->mode[1], &reuc_entry->oid[1],
Packit Service 20376f
				reuc_entry->mode[2], &reuc_entry->oid[2])) < 0)
Packit Service 20376f
				goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (data->update_names) {
Packit Service 20376f
		git_vector_foreach(data->update_names, i, name_entry) {
Packit Service 20376f
			if ((error = git_index_name_add(data->index, name_entry->ancestor,
Packit Service 20376f
				name_entry->ours, name_entry->theirs)) < 0)
Packit Service 20376f
				goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void checkout_data_clear(checkout_data *data)
Packit Service 20376f
{
Packit Service 20376f
	if (data->opts_free_baseline) {
Packit Service 20376f
		git_tree_free(data->opts.baseline);
Packit Service 20376f
		data->opts.baseline = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_vector_free(&data->removes);
Packit Service 20376f
	git_pool_clear(&data->pool);
Packit Service 20376f
Packit Service 20376f
	git_vector_free_deep(&data->remove_conflicts);
Packit Service 20376f
	git_vector_free_deep(&data->update_conflicts);
Packit Service 20376f
Packit Service 20376f
	git__free(data->pfx);
Packit Service 20376f
	data->pfx = NULL;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&data->target_path);
Packit Service 20376f
	git_buf_free(&data->tmp);
Packit Service 20376f
Packit Service 20376f
	git_index_free(data->index);
Packit Service 20376f
	data->index = NULL;
Packit Service 20376f
Packit Service 20376f
	git_strmap_free(data->mkdir_map);
Packit Service 20376f
	data->mkdir_map = NULL;
Packit Service 20376f
Packit Service 20376f
	git_attr_session__free(&data->attr_session);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_data_init(
Packit Service 20376f
	checkout_data *data,
Packit Service 20376f
	git_iterator *target,
Packit Service 20376f
	const git_checkout_options *proposed)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_repository *repo = git_iterator_owner(target);
Packit Service 20376f
Packit Service 20376f
	memset(data, 0, sizeof(*data));
Packit Service 20376f
Packit Service 20376f
	if (!repo) {
Packit Service 20376f
		giterr_set(GITERR_CHECKOUT, "cannot checkout nothing");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((!proposed || !proposed->target_directory) &&
Packit Service 20376f
		(error = git_repository__ensure_not_bare(repo, "checkout")) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	data->repo = repo;
Packit Service 20376f
	data->target = target;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_VERSION(
Packit Service 20376f
		proposed, GIT_CHECKOUT_OPTIONS_VERSION, "git_checkout_options");
Packit Service 20376f
Packit Service 20376f
	if (!proposed)
Packit Service 20376f
		GIT_INIT_STRUCTURE(&data->opts, GIT_CHECKOUT_OPTIONS_VERSION);
Packit Service 20376f
	else
Packit Service 20376f
		memmove(&data->opts, proposed, sizeof(git_checkout_options));
Packit Service 20376f
Packit Service 20376f
	if (!data->opts.target_directory)
Packit Service 20376f
		data->opts.target_directory = git_repository_workdir(repo);
Packit Service 20376f
	else if (!git_path_isdir(data->opts.target_directory) &&
Packit Service 20376f
			 (error = checkout_mkdir(data,
Packit Service 20376f
				data->opts.target_directory, NULL,
Packit Service 20376f
				GIT_DIR_MODE, GIT_MKDIR_VERIFY_DIR)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* refresh config and index content unless NO_REFRESH is given */
Packit Service 20376f
	if ((data->opts.checkout_strategy & GIT_CHECKOUT_NO_REFRESH) == 0) {
Packit Service 20376f
		git_config *cfg;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
Packit Service 20376f
		/* Get the repository index and reload it (unless we're checking
Packit Service 20376f
		 * out the index; then it has the changes we're trying to check
Packit Service 20376f
		 * out and those should not be overwritten.)
Packit Service 20376f
		 */
Packit Service 20376f
		if ((error = git_repository_index(&data->index, data->repo)) < 0)
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
Packit Service 20376f
		if (data->index != git_iterator_index(target)) {
Packit Service 20376f
			if ((error = git_index_read(data->index, true)) < 0)
Packit Service 20376f
				goto cleanup;
Packit Service 20376f
Packit Service 20376f
			/* cannot checkout if unresolved conflicts exist */
Packit Service 20376f
			if ((data->opts.checkout_strategy & GIT_CHECKOUT_FORCE) == 0 &&
Packit Service 20376f
				git_index_has_conflicts(data->index)) {
Packit Service 20376f
				error = GIT_ECONFLICT;
Packit Service 20376f
				giterr_set(GITERR_CHECKOUT,
Packit Service 20376f
					"unresolved conflicts exist in the index");
Packit Service 20376f
				goto cleanup;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			/* clean conflict data in the current index */
Packit Service 20376f
			git_index_name_clear(data->index);
Packit Service 20376f
			git_index_reuc_clear(data->index);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* if you are forcing, allow all safe updates, plus recreate missing */
Packit Service 20376f
	if ((data->opts.checkout_strategy & GIT_CHECKOUT_FORCE) != 0)
Packit Service 20376f
		data->opts.checkout_strategy |= GIT_CHECKOUT_SAFE |
Packit Service 20376f
			GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	/* if the repository does not actually have an index file, then this
Packit Service 20376f
	 * is an initial checkout (perhaps from clone), so we allow safe updates
Packit Service 20376f
	 */
Packit Service 20376f
	if (!data->index->on_disk &&
Packit Service 20376f
		(data->opts.checkout_strategy & GIT_CHECKOUT_SAFE) != 0)
Packit Service 20376f
		data->opts.checkout_strategy |= GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	data->strategy = data->opts.checkout_strategy;
Packit Service 20376f
Packit Service 20376f
	/* opts->disable_filters is false by default */
Packit Service 20376f
Packit Service 20376f
	if (!data->opts.dir_mode)
Packit Service 20376f
		data->opts.dir_mode = GIT_DIR_MODE;
Packit Service 20376f
Packit Service 20376f
	if (!data->opts.file_open_flags)
Packit Service 20376f
		data->opts.file_open_flags = O_CREAT | O_TRUNC | O_WRONLY;
Packit Service 20376f
Packit Service 20376f
	data->pfx = git_pathspec_prefix(&data->opts.paths);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository__cvar(
Packit Service 20376f
			 &data->can_symlink, repo, GIT_CVAR_SYMLINKS)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (!data->opts.baseline && !data->opts.baseline_index) {
Packit Service 20376f
		data->opts_free_baseline = true;
Packit Service 20376f
		error = 0;
Packit Service 20376f
Packit Service 20376f
		/* if we don't have an index, this is an initial checkout and
Packit Service 20376f
		 * should be against an empty baseline
Packit Service 20376f
		 */
Packit Service 20376f
		if (data->index->on_disk)
Packit Service 20376f
			error = checkout_lookup_head_tree(&data->opts.baseline, repo);
Packit Service 20376f
Packit Service 20376f
		if (error == GIT_EUNBORNBRANCH) {
Packit Service 20376f
			error = 0;
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((data->opts.checkout_strategy &
Packit Service 20376f
		(GIT_CHECKOUT_CONFLICT_STYLE_MERGE | GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)) == 0) {
Packit Service 20376f
		git_config_entry *conflict_style = NULL;
Packit Service 20376f
		git_config *cfg = NULL;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_repository_config__weakptr(&cfg, repo)) < 0 ||
Packit Service 20376f
			(error = git_config_get_entry(&conflict_style, cfg, "merge.conflictstyle")) < 0 ||
Packit Service 20376f
			error == GIT_ENOTFOUND)
Packit Service 20376f
			;
Packit Service 20376f
		else if (error)
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
		else if (strcmp(conflict_style->value, "merge") == 0)
Packit Service 20376f
			data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_MERGE;
Packit Service 20376f
		else if (strcmp(conflict_style->value, "diff3") == 0)
Packit Service 20376f
			data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3;
Packit Service 20376f
		else {
Packit Service 20376f
			giterr_set(GITERR_CHECKOUT, "unknown style '%s' given for 'merge.conflictstyle'",
Packit Service 20376f
				conflict_style->value);
Packit Service 20376f
			error = -1;
Packit Service 20376f
			git_config_entry_free(conflict_style);
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
		git_config_entry_free(conflict_style);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_pool_init(&data->pool, 1);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
Packit Service 20376f
		(error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 ||
Packit Service 20376f
		(error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 ||
Packit Service 20376f
		(error = git_buf_puts(&data->target_path, data->opts.target_directory)) < 0 ||
Packit Service 20376f
		(error = git_path_to_dir(&data->target_path)) < 0 ||
Packit Service 20376f
		(error = git_strmap_alloc(&data->mkdir_map)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	data->target_len = git_buf_len(&data->target_path);
Packit Service 20376f
Packit Service 20376f
	git_attr_session__init(&data->attr_session, data->repo);
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		checkout_data_clear(data);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define CHECKOUT_INDEX_DONT_WRITE_MASK \
Packit Service 20376f
	(GIT_CHECKOUT_DONT_UPDATE_INDEX | GIT_CHECKOUT_DONT_WRITE_INDEX)
Packit Service 20376f
Packit Service 20376f
int git_checkout_iterator(
Packit Service 20376f
	git_iterator *target,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_checkout_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_iterator *baseline = NULL, *workdir = NULL;
Packit Service 20376f
	git_iterator_options baseline_opts = GIT_ITERATOR_OPTIONS_INIT,
Packit Service 20376f
		workdir_opts = GIT_ITERATOR_OPTIONS_INIT;
Packit Service 20376f
	checkout_data data = {0};
Packit Service 20376f
	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
Packit Service 20376f
	uint32_t *actions = NULL;
Packit Service 20376f
	size_t *counts = NULL;
Packit Service 20376f
Packit Service 20376f
	/* initialize structures and options */
Packit Service 20376f
	error = checkout_data_init(&data, target, opts);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	diff_opts.flags =
Packit Service 20376f
		GIT_DIFF_INCLUDE_UNMODIFIED |
Packit Service 20376f
		GIT_DIFF_INCLUDE_UNREADABLE |
Packit Service 20376f
		GIT_DIFF_INCLUDE_UNTRACKED |
Packit Service 20376f
		GIT_DIFF_RECURSE_UNTRACKED_DIRS | /* needed to match baseline */
Packit Service 20376f
		GIT_DIFF_INCLUDE_IGNORED |
Packit Service 20376f
		GIT_DIFF_INCLUDE_TYPECHANGE |
Packit Service 20376f
		GIT_DIFF_INCLUDE_TYPECHANGE_TREES |
Packit Service 20376f
		GIT_DIFF_SKIP_BINARY_CHECK |
Packit Service 20376f
		GIT_DIFF_INCLUDE_CASECHANGE;
Packit Service 20376f
	if (data.opts.checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH)
Packit Service 20376f
		diff_opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
Packit Service 20376f
	if (data.opts.paths.count > 0)
Packit Service 20376f
		diff_opts.pathspec = data.opts.paths;
Packit Service 20376f
Packit Service 20376f
	/* set up iterators */
Packit Service 20376f
Packit Service 20376f
	workdir_opts.flags = git_iterator_ignore_case(target) ?
Packit Service 20376f
		GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
Packit Service 20376f
	workdir_opts.flags |= GIT_ITERATOR_DONT_AUTOEXPAND;
Packit Service 20376f
	workdir_opts.start = data.pfx;
Packit Service 20376f
	workdir_opts.end = data.pfx;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_iterator_reset_range(target, data.pfx, data.pfx)) < 0 ||
Packit Service 20376f
		(error = git_iterator_for_workdir_ext(
Packit Service 20376f
			&workdir, data.repo, data.opts.target_directory, index, NULL,
Packit Service 20376f
			&workdir_opts)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	baseline_opts.flags = git_iterator_ignore_case(target) ?
Packit Service 20376f
		GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
Packit Service 20376f
	baseline_opts.start = data.pfx;
Packit Service 20376f
	baseline_opts.end = data.pfx;
Packit Service 20376f
	if (opts && (opts->checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH)) {
Packit Service 20376f
		baseline_opts.pathlist.count = opts->paths.count;
Packit Service 20376f
		baseline_opts.pathlist.strings = opts->paths.strings;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (data.opts.baseline_index) {
Packit Service 20376f
		if ((error = git_iterator_for_index(
Packit Service 20376f
				&baseline, git_index_owner(data.opts.baseline_index),
Packit Service 20376f
				data.opts.baseline_index, &baseline_opts)) < 0)
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
	} else {
Packit Service 20376f
		if ((error = git_iterator_for_tree(
Packit Service 20376f
				&baseline, data.opts.baseline, &baseline_opts)) < 0)
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Should not have case insensitivity mismatch */
Packit Service 20376f
	assert(git_iterator_ignore_case(workdir) == git_iterator_ignore_case(baseline));
Packit Service 20376f
Packit Service 20376f
	/* Generate baseline-to-target diff which will include an entry for
Packit Service 20376f
	 * every possible update that might need to be made.
Packit Service 20376f
	 */
Packit Service 20376f
	if ((error = git_diff__from_iterators(
Packit Service 20376f
			&data.diff, data.repo, baseline, target, &diff_opts)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	/* Loop through diff (and working directory iterator) building a list of
Packit Service 20376f
	 * actions to be taken, plus look for conflicts and send notifications,
Packit Service 20376f
	 * then loop through conflicts.
Packit Service 20376f
	 */
Packit Service 20376f
	if ((error = checkout_get_actions(&actions, &counts, &data, workdir)) != 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	data.total_steps = counts[CHECKOUT_ACTION__REMOVE] +
Packit Service 20376f
		counts[CHECKOUT_ACTION__REMOVE_CONFLICT] +
Packit Service 20376f
		counts[CHECKOUT_ACTION__UPDATE_BLOB] +
Packit Service 20376f
		counts[CHECKOUT_ACTION__UPDATE_SUBMODULE] +
Packit Service 20376f
		counts[CHECKOUT_ACTION__UPDATE_CONFLICT];
Packit Service 20376f
Packit Service 20376f
	report_progress(&data, NULL); /* establish 0 baseline */
Packit Service 20376f
Packit Service 20376f
	/* To deal with some order dependencies, perform remaining checkout
Packit Service 20376f
	 * in three passes: removes, then update blobs, then update submodules.
Packit Service 20376f
	 */
Packit Service 20376f
	if (counts[CHECKOUT_ACTION__REMOVE] > 0 &&
Packit Service 20376f
		(error = checkout_remove_the_old(actions, &data)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (counts[CHECKOUT_ACTION__REMOVE_CONFLICT] > 0 &&
Packit Service 20376f
		(error = checkout_remove_conflicts(&data)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (counts[CHECKOUT_ACTION__UPDATE_BLOB] > 0 &&
Packit Service 20376f
		(error = checkout_create_the_new(actions, &data)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (counts[CHECKOUT_ACTION__UPDATE_SUBMODULE] > 0 &&
Packit Service 20376f
		(error = checkout_create_submodules(actions, &data)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (counts[CHECKOUT_ACTION__UPDATE_CONFLICT] > 0 &&
Packit Service 20376f
		(error = checkout_create_conflicts(&data)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (data.index != git_iterator_index(target) &&
Packit Service 20376f
		(error = checkout_extensions_update_index(&data)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	assert(data.completed_steps == data.total_steps);
Packit Service 20376f
Packit Service 20376f
	if (data.opts.perfdata_cb)
Packit Service 20376f
		data.opts.perfdata_cb(&data.perfdata, data.opts.perfdata_payload);
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	if (!error && data.index != NULL &&
Packit Service 20376f
		(data.strategy & CHECKOUT_INDEX_DONT_WRITE_MASK) == 0)
Packit Service 20376f
		error = git_index_write(data.index);
Packit Service 20376f
Packit Service 20376f
	git_diff_free(data.diff);
Packit Service 20376f
	git_iterator_free(workdir);
Packit Service 20376f
	git_iterator_free(baseline);
Packit Service 20376f
	git__free(actions);
Packit Service 20376f
	git__free(counts);
Packit Service 20376f
	checkout_data_clear(&data);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_checkout_index(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	const git_checkout_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	int error, owned = 0;
Packit Service 20376f
	git_iterator *index_i;
Packit Service 20376f
Packit Service 20376f
	if (!index && !repo) {
Packit Service 20376f
		giterr_set(GITERR_CHECKOUT,
Packit Service 20376f
			"must provide either repository or index to checkout");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (index && repo &&
Packit Service 20376f
		git_index_owner(index) &&
Packit Service 20376f
		git_index_owner(index) != repo) {
Packit Service 20376f
		giterr_set(GITERR_CHECKOUT,
Packit Service 20376f
			"index to checkout does not match repository");
Packit Service 20376f
		return -1;
Packit Service 20376f
	} else if(index && repo && !git_index_owner(index)) {
Packit Service 20376f
		GIT_REFCOUNT_OWN(index, repo);
Packit Service 20376f
		owned = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!repo)
Packit Service 20376f
		repo = git_index_owner(index);
Packit Service 20376f
Packit Service 20376f
	if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
	GIT_REFCOUNT_INC(index);
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_iterator_for_index(&index_i, repo, index, NULL)))
Packit Service 20376f
		error = git_checkout_iterator(index_i, index, opts);
Packit Service 20376f
Packit Service 20376f
	if (owned)
Packit Service 20376f
		GIT_REFCOUNT_OWN(index, NULL);
Packit Service 20376f
Packit Service 20376f
	git_iterator_free(index_i);
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_checkout_tree(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_object *treeish,
Packit Service 20376f
	const git_checkout_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_tree *tree = NULL;
Packit Service 20376f
	git_iterator *tree_i = NULL;
Packit Service 20376f
	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	if (!treeish && !repo) {
Packit Service 20376f
		giterr_set(GITERR_CHECKOUT,
Packit Service 20376f
			"must provide either repository or tree to checkout");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
	if (treeish && repo && git_object_owner(treeish) != repo) {
Packit Service 20376f
		giterr_set(GITERR_CHECKOUT,
Packit Service 20376f
			"object to checkout does not match repository");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!repo)
Packit Service 20376f
		repo = git_object_owner(treeish);
Packit Service 20376f
Packit Service 20376f
	if (treeish) {
Packit Service 20376f
		if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) {
Packit Service 20376f
			giterr_set(
Packit Service 20376f
				GITERR_CHECKOUT, "provided object cannot be peeled to a tree");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
	else {
Packit Service 20376f
		if ((error = checkout_lookup_head_tree(&tree, repo)) < 0) {
Packit Service 20376f
			if (error != GIT_EUNBORNBRANCH)
Packit Service 20376f
				giterr_set(
Packit Service 20376f
					GITERR_CHECKOUT,
Packit Service 20376f
					"HEAD could not be peeled to a tree and no treeish given");
Packit Service 20376f
			return error;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_index(&index, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (opts && (opts->checkout_strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH)) {
Packit Service 20376f
		iter_opts.pathlist.count = opts->paths.count;
Packit Service 20376f
		iter_opts.pathlist.strings = opts->paths.strings;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_iterator_for_tree(&tree_i, tree, &iter_opts)))
Packit Service 20376f
		error = git_checkout_iterator(tree_i, index, opts);
Packit Service 20376f
Packit Service 20376f
	git_iterator_free(tree_i);
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
	git_tree_free(tree);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_checkout_head(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_checkout_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo);
Packit Service 20376f
	return git_checkout_tree(repo, NULL, opts);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_checkout_init_options(git_checkout_options *opts, unsigned int version)
Packit Service 20376f
{
Packit Service 20376f
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Packit Service 20376f
		opts, version, git_checkout_options, GIT_CHECKOUT_OPTIONS_INIT);
Packit Service 20376f
	return 0;
Packit Service 20376f
}