reset.c 4.5 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"
nulltoken committed
13
#include "git2/reset.h"
14
#include "git2/checkout.h"
Edward Thomson committed
15
#include "git2/merge.h"
16
#include "git2/refs.h"
nulltoken committed
17 18 19

#define ERROR_MSG "Cannot perform reset"

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

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

	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
60 61 62
	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);

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

67 68 69 70 71 72 73 74
		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;
		}

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

	return error;
}

nulltoken committed
99 100
int git_reset(
	git_repository *repo,
101
	git_object *target,
102 103 104
	git_reset_t reset_type,
	git_signature *signature,
	const char *log_message)
nulltoken committed
105 106 107 108
{
	git_object *commit = NULL;
	git_index *index = NULL;
	git_tree *tree = NULL;
109
	int error = 0;
110
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
111
	git_buf log_message_buf = GIT_BUF_INIT;
nulltoken committed
112 113 114

	assert(repo && target);

115 116 117 118 119
	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
120

121 122
	if (reset_type != GIT_RESET_SOFT &&
		(error = git_repository__ensure_not_bare(repo,
123
			reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard")) < 0)
124
		return error;
nulltoken committed
125

126 127 128
	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
129 130
		goto cleanup;

131
	if (reset_type == GIT_RESET_SOFT &&
132
		(git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE ||
133 134 135 136
		 git_index_has_conflicts(index)))
	{
		giterr_set(GITERR_OBJECT, "%s (soft) in the middle of a merge.", ERROR_MSG);
		error = GIT_EUNMERGED;
nulltoken committed
137 138 139
		goto cleanup;
	}

140 141 142 143 144
	if (log_message)
		git_buf_sets(&log_message_buf, log_message);
	else
		git_buf_sets(&log_message_buf, "reset: moving");

145
	/* move HEAD to the new target */
146
	if ((error = git_reference__update_terminal(repo, GIT_HEAD_FILE,
147
		git_object_id(commit), signature, git_buf_cstr(&log_message_buf))) < 0)
nulltoken committed
148 149
		goto cleanup;

150 151
	if (reset_type == GIT_RESET_HARD) {
		/* overwrite working directory with HEAD */
152
		opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_SKIP_UNMERGED;
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
	if (reset_type > GIT_RESET_SOFT) {
		/* reset index to the target content */
160

nulltoken committed
161
		if ((error = git_index_read_tree(index, tree)) < 0 ||
162 163
			(error = git_index_write(index)) < 0)
			goto cleanup;
164

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

cleanup:
172
	git_object_free(commit);
nulltoken committed
173 174
	git_index_free(index);
	git_tree_free(tree);
175
	git_buf_free(&log_message_buf);
nulltoken committed
176 177 178

	return error;
}