Hash :
a147408f
Author :
Date :
2012-09-22T12:47:17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/*
* 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"
#include "git2/reset.h"
#include "git2/checkout.h"
#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;
}
int git_reset(
git_repository *repo,
git_object *target,
git_reset_type reset_type)
{
git_object *commit = NULL;
git_index *index = NULL;
git_tree *tree = NULL;
int error = -1;
git_checkout_opts opts;
git_reference *head = NULL;
assert(repo && target);
assert(reset_type == GIT_RESET_SOFT
|| reset_type == GIT_RESET_MIXED
|| reset_type == GIT_RESET_HARD);
if (git_object_owner(target) != repo)
return reset_error_invalid("The given target does not belong to this repository.");
if (reset_type == GIT_RESET_MIXED
&& git_repository__ensure_not_bare(repo, "reset mixed") < 0)
return GIT_EBAREREPO;
if (git_object_peel(&commit, target, GIT_OBJ_COMMIT) < 0) {
reset_error_invalid("The given target does not resolve to a commit");
goto cleanup;
}
//TODO: Check for unmerged entries
if (git_repository_head(&head, repo) < 0)
goto cleanup;
if (git_reference_set_oid(head, git_object_id(commit)) < 0)
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;
}
if (git_index_read_tree(index, tree, NULL) < 0) {
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;
}
if (reset_type == GIT_RESET_MIXED) {
error = 0;
goto cleanup;
}
memset(&opts, 0, sizeof(opts));
opts.checkout_strategy =
GIT_CHECKOUT_CREATE_MISSING
| GIT_CHECKOUT_OVERWRITE_MODIFIED
| GIT_CHECKOUT_REMOVE_UNTRACKED;
if (git_checkout_index(repo, &opts, NULL) < 0) {
giterr_set(GITERR_INDEX, "%s - Failed to checkout the index.", ERROR_MSG);
goto cleanup;
}
error = 0;
cleanup:
git_reference_free(head);
git_object_free(commit);
git_index_free(index);
git_tree_free(tree);
return error;
}