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

#include "common.h"
#include "commit.h"
#include "tag.h"
Edward Thomson committed
11
#include "merge.h"
12
#include "diff.h"
13
#include "annotated_commit.h"
nulltoken committed
14
#include "git2/reset.h"
15
#include "git2/checkout.h"
Edward Thomson committed
16
#include "git2/merge.h"
17
#include "git2/refs.h"
nulltoken committed
18 19 20

#define ERROR_MSG "Cannot perform reset"

21 22 23 24 25 26 27
int git_reset_default(
	git_repository *repo,
	git_object *target,
	git_strarray* pathspecs)
{
	git_object *commit = NULL;
	git_tree *tree = NULL;
28
	git_diff *diff = NULL;
29
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
Russell Belfer committed
30
	size_t i, max_i;
31 32 33 34
	git_index_entry entry;
	int error;
	git_index *index = NULL;

nulltoken committed
35
	assert(pathspecs != NULL && pathspecs->count > 0);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

	memset(&entry, 0, sizeof(git_index_entry));

	if ((error = git_repository_index(&index, repo)) < 0)
		goto cleanup;

	if (target) {
		if (git_object_owner(target) != repo) {
			giterr_set(GITERR_OBJECT,
				"%s_default - The given target does not belong to this repository.", ERROR_MSG);
			return -1;
		}

		if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0 ||
			(error = git_commit_tree(&tree, (git_commit *)commit)) < 0)
			goto cleanup;
	}

	opts.pathspec = *pathspecs;
	opts.flags = GIT_DIFF_REVERSE;

	if ((error = git_diff_tree_to_index(
		&diff, repo, tree, index, &opts)) < 0)
			goto cleanup;

Russell Belfer committed
61 62 63
	for (i = 0, max_i = git_diff_num_deltas(diff); i < max_i; ++i) {
		const git_diff_delta *delta = git_diff_get_delta(diff, i);

64 65
		assert(delta->status == GIT_DELTA_ADDED ||
			delta->status == GIT_DELTA_MODIFIED ||
66
			delta->status == GIT_DELTA_CONFLICTED ||
67 68
			delta->status == GIT_DELTA_DELETED);

69 70 71 72 73 74 75 76
		error = git_index_conflict_remove(index, delta->old_file.path);
		if (error < 0) {
			if (delta->status == GIT_DELTA_ADDED && error == GIT_ENOTFOUND)
				giterr_clear();
			else
				goto cleanup;
		}

77 78 79 80 81
		if (delta->status == GIT_DELTA_DELETED) {
			if ((error = git_index_remove(index, delta->old_file.path, 0)) < 0)
				goto cleanup;
		} else {
			entry.mode = delta->new_file.mode;
82
			git_oid_cpy(&entry.id, &delta->new_file.id);
83 84 85 86 87 88 89 90 91 92 93 94 95
			entry.path = (char *)delta->new_file.path;

			if ((error = git_index_add(index, &entry)) < 0)
				goto cleanup;
		}
	}

	error = git_index_write(index);

cleanup:
	git_object_free(commit);
	git_tree_free(tree);
	git_index_free(index);
96
	git_diff_free(diff);
97 98 99 100

	return error;
}

101
static int reset(
nulltoken committed
102
	git_repository *repo,
103
	git_object *target,
104
	const char *to,
105
	git_reset_t reset_type,
106
	const git_checkout_options *checkout_opts)
nulltoken committed
107 108 109 110
{
	git_object *commit = NULL;
	git_index *index = NULL;
	git_tree *tree = NULL;
111
	int error = 0;
112
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
113
	git_buf log_message = GIT_BUF_INIT;
nulltoken committed
114 115 116

	assert(repo && target);

117 118 119
	if (checkout_opts)
		opts = *checkout_opts;

120 121 122 123 124
	if (git_object_owner(target) != repo) {
		giterr_set(GITERR_OBJECT,
			"%s - The given target does not belong to this repository.", ERROR_MSG);
		return -1;
	}
nulltoken committed
125

126 127
	if (reset_type != GIT_RESET_SOFT &&
		(error = git_repository__ensure_not_bare(repo,
128
			reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard")) < 0)
129
		return error;
nulltoken committed
130

131 132 133
	if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0 ||
		(error = git_repository_index(&index, repo)) < 0 ||
		(error = git_commit_tree(&tree, (git_commit *)commit)) < 0)
Edward Thomson committed
134 135
		goto cleanup;

136
	if (reset_type == GIT_RESET_SOFT &&
137
		(git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE ||
138 139
		 git_index_has_conflicts(index)))
	{
140
		giterr_set(GITERR_OBJECT, "%s (soft) in the middle of a merge", ERROR_MSG);
141
		error = GIT_EUNMERGED;
nulltoken committed
142 143 144
		goto cleanup;
	}

145
	if ((error = git_buf_printf(&log_message, "reset: moving to %s", to)) < 0)
146
		return error;
147

148
	if (reset_type == GIT_RESET_HARD) {
149
		/* overwrite working directory with the new tree */
150
		opts.checkout_strategy = GIT_CHECKOUT_FORCE;
nulltoken committed
151

152 153
		if ((error = git_checkout_tree(repo, (git_object *)tree, &opts)) < 0)
			goto cleanup;
Edward Thomson committed
154 155
	}

156 157 158 159 160
	/* move HEAD to the new target */
	if ((error = git_reference__update_terminal(repo, GIT_HEAD_FILE,
		git_object_id(commit), NULL, git_buf_cstr(&log_message))) < 0)
		goto cleanup;

161 162
	if (reset_type > GIT_RESET_SOFT) {
		/* reset index to the target content */
163

nulltoken committed
164
		if ((error = git_index_read_tree(index, tree)) < 0 ||
165 166
			(error = git_index_write(index)) < 0)
			goto cleanup;
167

168
		if ((error = git_repository_state_cleanup(repo)) < 0) {
169 170 171 172
			giterr_set(GITERR_INDEX, "%s - failed to clean up merge data", ERROR_MSG);
			goto cleanup;
		}
	}
nulltoken committed
173 174

cleanup:
175
	git_object_free(commit);
nulltoken committed
176 177
	git_index_free(index);
	git_tree_free(tree);
178
	git_buf_free(&log_message);
nulltoken committed
179 180 181

	return error;
}
182 183 184 185 186

int git_reset(
	git_repository *repo,
	git_object *target,
	git_reset_t reset_type,
187
	const git_checkout_options *checkout_opts)
188 189 190 191 192 193 194 195
{
	return reset(repo, target, git_oid_tostr_s(git_object_id(target)), reset_type, checkout_opts);
}

int git_reset_from_annotated(
	git_repository *repo,
	git_annotated_commit *commit,
	git_reset_t reset_type,
196
	const git_checkout_options *checkout_opts)
197
{
198
	return reset(repo, (git_object *) commit->commit, commit->description, reset_type, checkout_opts);
199
}