]> git.proxmox.com Git - libgit2.git/blob - src/reset.c
Fixed size_t format string warning
[libgit2.git] / src / reset.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "common.h"
9 #include "commit.h"
10 #include "tag.h"
11 #include "merge.h"
12 #include "git2/reset.h"
13 #include "git2/checkout.h"
14 #include "git2/merge.h"
15
16 #define ERROR_MSG "Cannot perform reset"
17
18 static int update_head(git_repository *repo, git_object *commit)
19 {
20 int error;
21 git_reference *head = NULL, *target = NULL;
22
23 error = git_repository_head(&head, repo);
24
25 if (error < 0 && error != GIT_EORPHANEDHEAD)
26 return error;
27
28 if (error == GIT_EORPHANEDHEAD) {
29 giterr_clear();
30
31 /*
32 * TODO: This is a bit weak as this doesn't support chained
33 * symbolic references. yet.
34 */
35 if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
36 goto cleanup;
37
38 if ((error = git_reference_create(
39 &target,
40 repo,
41 git_reference_symbolic_target(head),
42 git_object_id(commit), 0)) < 0)
43 goto cleanup;
44 } else {
45 if ((error = git_reference_set_target(head, git_object_id(commit))) < 0)
46 goto cleanup;
47 }
48
49 error = 0;
50
51 cleanup:
52 git_reference_free(head);
53 git_reference_free(target);
54 return error;
55 }
56
57 int git_reset(
58 git_repository *repo,
59 git_object *target,
60 git_reset_t reset_type)
61 {
62 git_object *commit = NULL;
63 git_index *index = NULL;
64 git_tree *tree = NULL;
65 int error = 0;
66 git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
67
68 assert(repo && target);
69
70 if (git_object_owner(target) != repo) {
71 giterr_set(GITERR_OBJECT,
72 "%s - The given target does not belong to this repository.", ERROR_MSG);
73 return -1;
74 }
75
76 if (reset_type != GIT_RESET_SOFT &&
77 (error = git_repository__ensure_not_bare(repo,
78 reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard")) < 0)
79 return error;
80
81 if ((error = git_object_peel(&commit, target, GIT_OBJ_COMMIT)) < 0 ||
82 (error = git_repository_index(&index, repo)) < 0 ||
83 (error = git_commit_tree(&tree, (git_commit *)commit)) < 0)
84 goto cleanup;
85
86 if (reset_type == GIT_RESET_SOFT &&
87 (git_repository_state(repo) == GIT_REPOSITORY_STATE_MERGE ||
88 git_index_has_conflicts(index)))
89 {
90 giterr_set(GITERR_OBJECT, "%s (soft) in the middle of a merge.", ERROR_MSG);
91 error = GIT_EUNMERGED;
92 goto cleanup;
93 }
94
95 /* move HEAD to the new target */
96 if ((error = update_head(repo, commit)) < 0)
97 goto cleanup;
98
99 if (reset_type == GIT_RESET_HARD) {
100 /* overwrite working directory with HEAD */
101 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
102
103 if ((error = git_checkout_tree(repo, (git_object *)tree, &opts)) < 0)
104 goto cleanup;
105 }
106
107 if (reset_type > GIT_RESET_SOFT) {
108 /* reset index to the target content */
109
110 if ((error = git_repository_index(&index, repo)) < 0 ||
111 (error = git_index_read_tree(index, tree)) < 0 ||
112 (error = git_index_write(index)) < 0)
113 goto cleanup;
114
115 if ((error = git_repository_merge_cleanup(repo)) < 0) {
116 giterr_set(GITERR_INDEX, "%s - failed to clean up merge data", ERROR_MSG);
117 goto cleanup;
118 }
119 }
120
121 cleanup:
122 git_object_free(commit);
123 git_index_free(index);
124 git_tree_free(tree);
125
126 return error;
127 }