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