reset.c 3.57 KB
Newer Older
nulltoken committed
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) 2009-2012 the libgit2 contributors
 *
 * 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"
nulltoken committed
12
#include "git2/reset.h"
13
#include "git2/checkout.h"
Edward Thomson committed
14
#include "git2/merge.h"
nulltoken committed
15 16 17 18 19 20 21 22 23

#define ERROR_MSG "Cannot perform reset"

static int reset_error_invalid(const char *msg)
{
	giterr_set(GITERR_INVALID, "%s - %s", ERROR_MSG, msg);
	return -1;
}

24 25 26 27 28 29 30 31 32 33 34 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 60 61 62
static int update_head(git_repository *repo, git_object *commit)
{
	int error;
	git_reference *head = NULL, *target = NULL;

	error = git_repository_head(&head, repo);

	if (error < 0 && error != GIT_EORPHANEDHEAD)
		return error;

	if (error == GIT_EORPHANEDHEAD) {
		giterr_clear();

		/*
		 * TODO: This is a bit weak as this doesn't support chained
		 * symbolic references. yet.
		 */
		if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
			goto cleanup;

		if ((error = git_reference_create_oid(
			&target,
			repo,
			git_reference_target(head),
			git_object_id(commit), 0)) < 0)
				goto cleanup;
	} else {
		if ((error = git_reference_set_oid(head, git_object_id(commit))) < 0)
			goto cleanup;
	}

	error = 0;

cleanup:
	git_reference_free(head);
	git_reference_free(target);
	return error;
}

nulltoken committed
63 64
int git_reset(
	git_repository *repo,
65
	git_object *target,
nulltoken committed
66 67 68 69 70 71
	git_reset_type reset_type)
{
	git_object *commit = NULL;
	git_index *index = NULL;
	git_tree *tree = NULL;
	int error = -1;
72
	git_checkout_opts opts;
nulltoken committed
73 74

	assert(repo && target);
75 76 77
	assert(reset_type == GIT_RESET_SOFT
		|| reset_type == GIT_RESET_MIXED
		|| reset_type == GIT_RESET_HARD);
nulltoken committed
78 79 80 81

	if (git_object_owner(target) != repo)
		return reset_error_invalid("The given target does not belong to this repository.");

82 83 84 85 86
	if (reset_type != GIT_RESET_SOFT
		&& git_repository__ensure_not_bare(
			repo,
			reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard") < 0)
				return GIT_EBAREREPO;
nulltoken committed
87

88 89 90
	if (git_object_peel(&commit, target, GIT_OBJ_COMMIT) < 0) {
		reset_error_invalid("The given target does not resolve to a commit");
		goto cleanup;
nulltoken committed
91 92
	}

Edward Thomson committed
93 94
	if (reset_type == GIT_RESET_SOFT && (git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE)) {
		giterr_set(GITERR_OBJECT, "%s (soft) while in the middle of a merge.", ERROR_MSG);
Edward Thomson committed
95
		error = GIT_EUNMERGED;
Edward Thomson committed
96 97 98
		goto cleanup;
	}

nulltoken committed
99 100
	//TODO: Check for unmerged entries

101
	if (update_head(repo, commit) < 0)
nulltoken committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
		goto cleanup;

	if (reset_type == GIT_RESET_SOFT) {
		error = 0;
		goto cleanup;
	}

	if (git_commit_tree(&tree, (git_commit *)commit) < 0) {
		giterr_set(GITERR_OBJECT, "%s - Failed to retrieve the commit tree.", ERROR_MSG);
		goto cleanup;
	}

	if (git_repository_index(&index, repo) < 0) {
		giterr_set(GITERR_OBJECT, "%s - Failed to retrieve the index.", ERROR_MSG);
		goto cleanup;
	}

119
	if (git_index_read_tree(index, tree) < 0) {
nulltoken committed
120 121 122 123 124 125 126 127 128
		giterr_set(GITERR_INDEX, "%s - Failed to update the index.", ERROR_MSG);
		goto cleanup;
	}

	if (git_index_write(index) < 0) {
		giterr_set(GITERR_INDEX, "%s - Failed to write the index.", ERROR_MSG);
		goto cleanup;
	}

Edward Thomson committed
129 130 131 132 133
	if ((error = git_merge__cleanup(repo)) < 0) {
		giterr_set(GITERR_INDEX, "%s - Failed to clean up merge data.", ERROR_MSG);
		goto cleanup;
	}

134 135 136 137 138
	if (reset_type == GIT_RESET_MIXED) {
		error = 0;
		goto cleanup;
	}

139
	memset(&opts, 0, sizeof(opts));
140
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
141

142
	if (git_checkout_index(repo, &opts) < 0) {
143 144 145 146
		giterr_set(GITERR_INDEX, "%s - Failed to checkout the index.", ERROR_MSG);
		goto cleanup;
	}

nulltoken committed
147 148 149
	error = 0;

cleanup:
150
	git_object_free(commit);
nulltoken committed
151 152 153 154 155
	git_index_free(index);
	git_tree_free(tree);

	return error;
}