]> git.proxmox.com Git - libgit2.git/blob - src/revert.c
Merge remote-tracking branch 'upstream/master' into cmn/describe
[libgit2.git] / src / revert.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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 "repository.h"
10 #include "filebuf.h"
11 #include "merge.h"
12
13 #include "git2/types.h"
14 #include "git2/merge.h"
15 #include "git2/revert.h"
16 #include "git2/commit.h"
17 #include "git2/sys/commit.h"
18
19 #define GIT_REVERT_FILE_MODE 0666
20
21 static int write_revert_head(
22 git_repository *repo,
23 const char *commit_oidstr)
24 {
25 git_filebuf file = GIT_FILEBUF_INIT;
26 git_buf file_path = GIT_BUF_INIT;
27 int error = 0;
28
29 if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_REVERT_HEAD_FILE)) >= 0 &&
30 (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) >= 0 &&
31 (error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
32 error = git_filebuf_commit(&file);
33
34 if (error < 0)
35 git_filebuf_cleanup(&file);
36
37 git_buf_free(&file_path);
38
39 return error;
40 }
41
42 static int write_merge_msg(
43 git_repository *repo,
44 const char *commit_oidstr,
45 const char *commit_msgline)
46 {
47 git_filebuf file = GIT_FILEBUF_INIT;
48 git_buf file_path = GIT_BUF_INIT;
49 int error = 0;
50
51 if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
52 (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) < 0 ||
53 (error = git_filebuf_printf(&file, "Revert \"%s\"\n\nThis reverts commit %s.\n",
54 commit_msgline, commit_oidstr)) < 0)
55 goto cleanup;
56
57 error = git_filebuf_commit(&file);
58
59 cleanup:
60 if (error < 0)
61 git_filebuf_cleanup(&file);
62
63 git_buf_free(&file_path);
64
65 return error;
66 }
67
68 static int revert_normalize_opts(
69 git_repository *repo,
70 git_revert_options *opts,
71 const git_revert_options *given,
72 const char *their_label)
73 {
74 int error = 0;
75 unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE_CREATE |
76 GIT_CHECKOUT_ALLOW_CONFLICTS;
77
78 GIT_UNUSED(repo);
79
80 if (given != NULL)
81 memcpy(opts, given, sizeof(git_revert_options));
82 else {
83 git_revert_options default_opts = GIT_REVERT_OPTIONS_INIT;
84 memcpy(opts, &default_opts, sizeof(git_revert_options));
85 }
86
87 if (!opts->checkout_opts.checkout_strategy)
88 opts->checkout_opts.checkout_strategy = default_checkout_strategy;
89
90 if (!opts->checkout_opts.our_label)
91 opts->checkout_opts.our_label = "HEAD";
92
93 if (!opts->checkout_opts.their_label)
94 opts->checkout_opts.their_label = their_label;
95
96 return error;
97 }
98
99 static int revert_state_cleanup(git_repository *repo)
100 {
101 const char *state_files[] = { GIT_REVERT_HEAD_FILE, GIT_MERGE_MSG_FILE };
102
103 return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
104 }
105
106 static int revert_seterr(git_commit *commit, const char *fmt)
107 {
108 char commit_oidstr[GIT_OID_HEXSZ + 1];
109
110 git_oid_fmt(commit_oidstr, git_commit_id(commit));
111 commit_oidstr[GIT_OID_HEXSZ] = '\0';
112
113 giterr_set(GITERR_REVERT, fmt, commit_oidstr);
114
115 return -1;
116 }
117
118 int git_revert_commit(
119 git_index **out,
120 git_repository *repo,
121 git_commit *revert_commit,
122 git_commit *our_commit,
123 unsigned int mainline,
124 const git_merge_options *merge_opts)
125 {
126 git_commit *parent_commit = NULL;
127 git_tree *parent_tree = NULL, *our_tree = NULL, *revert_tree = NULL;
128 int parent = 0, error = 0;
129
130 assert(out && repo && revert_commit && our_commit);
131
132 if (git_commit_parentcount(revert_commit) > 1) {
133 if (!mainline)
134 return revert_seterr(revert_commit,
135 "Mainline branch is not specified but %s is a merge commit");
136
137 parent = mainline;
138 } else {
139 if (mainline)
140 return revert_seterr(revert_commit,
141 "Mainline branch specified but %s is not a merge commit");
142
143 parent = git_commit_parentcount(revert_commit);
144 }
145
146 if (parent &&
147 ((error = git_commit_parent(&parent_commit, revert_commit, (parent - 1))) < 0 ||
148 (error = git_commit_tree(&parent_tree, parent_commit)) < 0))
149 goto done;
150
151 if ((error = git_commit_tree(&revert_tree, revert_commit)) < 0 ||
152 (error = git_commit_tree(&our_tree, our_commit)) < 0)
153 goto done;
154
155 error = git_merge_trees(out, repo, revert_tree, our_tree, parent_tree, merge_opts);
156
157 done:
158 git_tree_free(parent_tree);
159 git_tree_free(our_tree);
160 git_tree_free(revert_tree);
161 git_commit_free(parent_commit);
162
163 return error;
164 }
165
166 int git_revert(
167 git_repository *repo,
168 git_commit *commit,
169 const git_revert_options *given_opts)
170 {
171 git_revert_options opts;
172 git_reference *our_ref = NULL;
173 git_commit *our_commit = NULL;
174 char commit_oidstr[GIT_OID_HEXSZ + 1];
175 const char *commit_msg;
176 git_buf their_label = GIT_BUF_INIT;
177 git_index *index_new = NULL;
178 int error;
179
180 assert(repo && commit);
181
182 GITERR_CHECK_VERSION(given_opts, GIT_REVERT_OPTIONS_VERSION, "git_revert_options");
183
184 if ((error = git_repository__ensure_not_bare(repo, "revert")) < 0)
185 return error;
186
187 git_oid_fmt(commit_oidstr, git_commit_id(commit));
188 commit_oidstr[GIT_OID_HEXSZ] = '\0';
189
190 if ((commit_msg = git_commit_summary(commit)) == NULL) {
191 error = -1;
192 goto on_error;
193 }
194
195 if ((error = git_buf_printf(&their_label, "parent of %.7s... %s", commit_oidstr, commit_msg)) < 0 ||
196 (error = revert_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
197 (error = write_revert_head(repo, commit_oidstr)) < 0 ||
198 (error = write_merge_msg(repo, commit_oidstr, commit_msg)) < 0 ||
199 (error = git_repository_head(&our_ref, repo)) < 0 ||
200 (error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJ_COMMIT)) < 0 ||
201 (error = git_revert_commit(&index_new, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
202 (error = git_merge__check_result(repo, index_new)) < 0 ||
203 (error = git_merge__append_conflicts_to_merge_msg(repo, index_new)) < 0 ||
204 (error = git_checkout_index(repo, index_new, &opts.checkout_opts)) < 0)
205 goto on_error;
206
207 goto done;
208
209 on_error:
210 revert_state_cleanup(repo);
211
212 done:
213 git_index_free(index_new);
214 git_commit_free(our_commit);
215 git_reference_free(our_ref);
216 git_buf_free(&their_label);
217
218 return error;
219 }
220
221 int git_revert_init_options(git_revert_options *opts, unsigned int version)
222 {
223 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
224 opts, version, git_revert_options, GIT_REVERT_OPTIONS_INIT);
225 return 0;
226 }