reset.c 5.23 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
 *
 * 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"
9

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

#define ERROR_MSG "Cannot perform reset"

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

Edward Thomson committed
36
	GIT_ASSERT_ARG(pathspecs && pathspecs->count > 0);
37 38 39 40 41 42 43 44

	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) {
45
			git_error_set(GIT_ERROR_OBJECT,
46 47 48 49
				"%s_default - The given target does not belong to this repository.", ERROR_MSG);
			return -1;
		}

50
		if ((error = git_object_peel(&commit, target, GIT_OBJECT_COMMIT)) < 0 ||
51 52 53 54 55 56 57 58 59 60 61
			(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
62 63 64
	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);

Edward Thomson committed
65 66 67 68
		GIT_ASSERT(delta->status == GIT_DELTA_ADDED ||
		           delta->status == GIT_DELTA_MODIFIED ||
		           delta->status == GIT_DELTA_CONFLICTED ||
		           delta->status == GIT_DELTA_DELETED);
69

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

78 79 80 81 82
		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;
83
			git_oid_cpy(&entry.id, &delta->new_file.id);
84 85 86 87 88 89 90 91 92 93 94 95 96
			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);
97
	git_diff_free(diff);
98 99 100 101

	return error;
}

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

Edward Thomson committed
116 117
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(target);
nulltoken committed
118

119 120 121
	if (checkout_opts)
		opts = *checkout_opts;

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

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

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

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

147
	if ((error = git_str_printf(&log_message, "reset: moving to %s", to)) < 0)
148
		return error;
149

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

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

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

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

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

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

cleanup:
177
	git_object_free(commit);
nulltoken committed
178 179
	git_index_free(index);
	git_tree_free(tree);
180
	git_str_dispose(&log_message);
nulltoken committed
181 182 183

	return error;
}
184 185 186

int git_reset(
	git_repository *repo,
187
	const git_object *target,
188
	git_reset_t reset_type,
189
	const git_checkout_options *checkout_opts)
190
{
191
	char to[GIT_OID_MAX_HEXSIZE + 1];
192

193
	git_oid_tostr(to, GIT_OID_MAX_HEXSIZE + 1, git_object_id(target));
194
	return reset(repo, target, to, reset_type, checkout_opts);
195 196 197 198
}

int git_reset_from_annotated(
	git_repository *repo,
199
	const git_annotated_commit *commit,
200
	git_reset_t reset_type,
201
	const git_checkout_options *checkout_opts)
202
{
203
	return reset(repo, (git_object *) commit->commit, commit->description, reset_type, checkout_opts);
204
}