reset.c 5.1 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 25
	const git_object *target,
	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;

nulltoken committed
36
	assert(pathspecs != NULL && 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);

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

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_buf log_message = GIT_BUF_INIT;
nulltoken committed
115 116 117

	assert(repo && target);

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

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

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

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

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

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

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

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

157 158 159 160 161
	/* 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;

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

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

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

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

	return error;
}
183 184 185

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

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