line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
1
|
|
|
|
|
|
static int write_revert_head( |
24
|
|
|
|
|
|
|
git_repository *repo, |
25
|
|
|
|
|
|
|
const char *commit_oidstr) |
26
|
|
|
|
|
|
|
{ |
27
|
1
|
|
|
|
|
|
git_filebuf file = GIT_FILEBUF_INIT; |
28
|
1
|
|
|
|
|
|
git_str file_path = GIT_STR_INIT; |
29
|
1
|
|
|
|
|
|
int error = 0; |
30
|
|
|
|
|
|
|
|
31
|
1
|
50
|
|
|
|
|
if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_REVERT_HEAD_FILE)) >= 0 && |
|
|
50
|
|
|
|
|
|
32
|
1
|
50
|
|
|
|
|
(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
|
1
|
|
|
|
|
|
error = git_filebuf_commit(&file); |
35
|
|
|
|
|
|
|
|
36
|
1
|
50
|
|
|
|
|
if (error < 0) |
37
|
0
|
|
|
|
|
|
git_filebuf_cleanup(&file); |
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
|
|
|
git_str_dispose(&file_path); |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
|
return error; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
1
|
|
|
|
|
|
static int write_merge_msg( |
45
|
|
|
|
|
|
|
git_repository *repo, |
46
|
|
|
|
|
|
|
const char *commit_oidstr, |
47
|
|
|
|
|
|
|
const char *commit_msgline) |
48
|
|
|
|
|
|
|
{ |
49
|
1
|
|
|
|
|
|
git_filebuf file = GIT_FILEBUF_INIT; |
50
|
1
|
|
|
|
|
|
git_str file_path = GIT_STR_INIT; |
51
|
1
|
|
|
|
|
|
int error = 0; |
52
|
|
|
|
|
|
|
|
53
|
1
|
50
|
|
|
|
|
if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 || |
|
|
50
|
|
|
|
|
|
54
|
1
|
50
|
|
|
|
|
(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
|
1
|
|
|
|
|
|
error = git_filebuf_commit(&file); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
cleanup: |
62
|
1
|
50
|
|
|
|
|
if (error < 0) |
63
|
0
|
|
|
|
|
|
git_filebuf_cleanup(&file); |
64
|
|
|
|
|
|
|
|
65
|
1
|
|
|
|
|
|
git_str_dispose(&file_path); |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
|
return error; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
|
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
|
1
|
|
|
|
|
|
int error = 0; |
77
|
1
|
|
|
|
|
|
unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE | |
78
|
|
|
|
|
|
|
GIT_CHECKOUT_ALLOW_CONFLICTS; |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
|
GIT_UNUSED(repo); |
81
|
|
|
|
|
|
|
|
82
|
1
|
50
|
|
|
|
|
if (given != NULL) |
83
|
1
|
|
|
|
|
|
memcpy(opts, given, sizeof(git_revert_options)); |
84
|
|
|
|
|
|
|
else { |
85
|
0
|
|
|
|
|
|
git_revert_options default_opts = GIT_REVERT_OPTIONS_INIT; |
86
|
0
|
|
|
|
|
|
memcpy(opts, &default_opts, sizeof(git_revert_options)); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
1
|
50
|
|
|
|
|
if (!opts->checkout_opts.checkout_strategy) |
90
|
0
|
|
|
|
|
|
opts->checkout_opts.checkout_strategy = default_checkout_strategy; |
91
|
|
|
|
|
|
|
|
92
|
1
|
50
|
|
|
|
|
if (!opts->checkout_opts.our_label) |
93
|
1
|
|
|
|
|
|
opts->checkout_opts.our_label = "HEAD"; |
94
|
|
|
|
|
|
|
|
95
|
1
|
50
|
|
|
|
|
if (!opts->checkout_opts.their_label) |
96
|
1
|
|
|
|
|
|
opts->checkout_opts.their_label = their_label; |
97
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
|
return error; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
static int revert_state_cleanup(git_repository *repo) |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
|
|
|
const char *state_files[] = { GIT_REVERT_HEAD_FILE, GIT_MERGE_MSG_FILE }; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
static int revert_seterr(git_commit *commit, const char *fmt) |
109
|
|
|
|
|
|
|
{ |
110
|
|
|
|
|
|
|
char commit_oidstr[GIT_OID_HEXSZ + 1]; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
git_oid_fmt(commit_oidstr, git_commit_id(commit)); |
113
|
0
|
|
|
|
|
|
commit_oidstr[GIT_OID_HEXSZ] = '\0'; |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_REVERT, fmt, commit_oidstr); |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
return -1; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
1
|
|
|
|
|
|
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
|
1
|
|
|
|
|
|
git_commit *parent_commit = NULL; |
129
|
1
|
|
|
|
|
|
git_tree *parent_tree = NULL, *our_tree = NULL, *revert_tree = NULL; |
130
|
1
|
|
|
|
|
|
int parent = 0, error = 0; |
131
|
|
|
|
|
|
|
|
132
|
1
|
50
|
|
|
|
|
GIT_ASSERT_ARG(out); |
133
|
1
|
50
|
|
|
|
|
GIT_ASSERT_ARG(repo); |
134
|
1
|
50
|
|
|
|
|
GIT_ASSERT_ARG(revert_commit); |
135
|
1
|
50
|
|
|
|
|
GIT_ASSERT_ARG(our_commit); |
136
|
|
|
|
|
|
|
|
137
|
1
|
50
|
|
|
|
|
if (git_commit_parentcount(revert_commit) > 1) { |
138
|
0
|
0
|
|
|
|
|
if (!mainline) |
139
|
0
|
|
|
|
|
|
return revert_seterr(revert_commit, |
140
|
|
|
|
|
|
|
"mainline branch is not specified but %s is a merge commit"); |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
parent = mainline; |
143
|
|
|
|
|
|
|
} else { |
144
|
1
|
50
|
|
|
|
|
if (mainline) |
145
|
0
|
|
|
|
|
|
return revert_seterr(revert_commit, |
146
|
|
|
|
|
|
|
"mainline branch specified but %s is not a merge commit"); |
147
|
|
|
|
|
|
|
|
148
|
1
|
|
|
|
|
|
parent = git_commit_parentcount(revert_commit); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
1
|
50
|
|
|
|
|
if (parent && |
|
|
50
|
|
|
|
|
|
152
|
1
|
50
|
|
|
|
|
((error = git_commit_parent(&parent_commit, revert_commit, (parent - 1))) < 0 || |
153
|
1
|
|
|
|
|
|
(error = git_commit_tree(&parent_tree, parent_commit)) < 0)) |
154
|
|
|
|
|
|
|
goto done; |
155
|
|
|
|
|
|
|
|
156
|
1
|
50
|
|
|
|
|
if ((error = git_commit_tree(&revert_tree, revert_commit)) < 0 || |
|
|
50
|
|
|
|
|
|
157
|
|
|
|
|
|
|
(error = git_commit_tree(&our_tree, our_commit)) < 0) |
158
|
|
|
|
|
|
|
goto done; |
159
|
|
|
|
|
|
|
|
160
|
1
|
|
|
|
|
|
error = git_merge_trees(out, repo, revert_tree, our_tree, parent_tree, merge_opts); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
done: |
163
|
1
|
|
|
|
|
|
git_tree_free(parent_tree); |
164
|
1
|
|
|
|
|
|
git_tree_free(our_tree); |
165
|
1
|
|
|
|
|
|
git_tree_free(revert_tree); |
166
|
1
|
|
|
|
|
|
git_commit_free(parent_commit); |
167
|
|
|
|
|
|
|
|
168
|
1
|
|
|
|
|
|
return error; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
1
|
|
|
|
|
|
int git_revert( |
172
|
|
|
|
|
|
|
git_repository *repo, |
173
|
|
|
|
|
|
|
git_commit *commit, |
174
|
|
|
|
|
|
|
const git_revert_options *given_opts) |
175
|
|
|
|
|
|
|
{ |
176
|
|
|
|
|
|
|
git_revert_options opts; |
177
|
1
|
|
|
|
|
|
git_reference *our_ref = NULL; |
178
|
1
|
|
|
|
|
|
git_commit *our_commit = NULL; |
179
|
|
|
|
|
|
|
char commit_oidstr[GIT_OID_HEXSZ + 1]; |
180
|
|
|
|
|
|
|
const char *commit_msg; |
181
|
1
|
|
|
|
|
|
git_str their_label = GIT_STR_INIT; |
182
|
1
|
|
|
|
|
|
git_index *index = NULL; |
183
|
1
|
|
|
|
|
|
git_indexwriter indexwriter = GIT_INDEXWRITER_INIT; |
184
|
|
|
|
|
|
|
int error; |
185
|
|
|
|
|
|
|
|
186
|
1
|
50
|
|
|
|
|
GIT_ASSERT_ARG(repo); |
187
|
1
|
50
|
|
|
|
|
GIT_ASSERT_ARG(commit); |
188
|
|
|
|
|
|
|
|
189
|
1
|
50
|
|
|
|
|
GIT_ERROR_CHECK_VERSION(given_opts, GIT_REVERT_OPTIONS_VERSION, "git_revert_options"); |
190
|
|
|
|
|
|
|
|
191
|
1
|
50
|
|
|
|
|
if ((error = git_repository__ensure_not_bare(repo, "revert")) < 0) |
192
|
0
|
|
|
|
|
|
return error; |
193
|
|
|
|
|
|
|
|
194
|
1
|
|
|
|
|
|
git_oid_fmt(commit_oidstr, git_commit_id(commit)); |
195
|
1
|
|
|
|
|
|
commit_oidstr[GIT_OID_HEXSZ] = '\0'; |
196
|
|
|
|
|
|
|
|
197
|
1
|
50
|
|
|
|
|
if ((commit_msg = git_commit_summary(commit)) == NULL) { |
198
|
0
|
|
|
|
|
|
error = -1; |
199
|
0
|
|
|
|
|
|
goto on_error; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
2
|
50
|
|
|
|
|
if ((error = git_str_printf(&their_label, "parent of %.7s... %s", commit_oidstr, commit_msg)) < 0 || |
|
|
50
|
|
|
|
|
|
203
|
2
|
50
|
|
|
|
|
(error = revert_normalize_opts(repo, &opts, given_opts, git_str_cstr(&their_label))) < 0 || |
204
|
1
|
50
|
|
|
|
|
(error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 || |
205
|
1
|
50
|
|
|
|
|
(error = write_revert_head(repo, commit_oidstr)) < 0 || |
206
|
1
|
50
|
|
|
|
|
(error = write_merge_msg(repo, commit_oidstr, commit_msg)) < 0 || |
207
|
1
|
50
|
|
|
|
|
(error = git_repository_head(&our_ref, repo)) < 0 || |
208
|
1
|
50
|
|
|
|
|
(error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJECT_COMMIT)) < 0 || |
209
|
1
|
50
|
|
|
|
|
(error = git_revert_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 || |
210
|
1
|
50
|
|
|
|
|
(error = git_merge__check_result(repo, index)) < 0 || |
211
|
1
|
50
|
|
|
|
|
(error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 || |
212
|
1
|
50
|
|
|
|
|
(error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 || |
213
|
|
|
|
|
|
|
(error = git_indexwriter_commit(&indexwriter)) < 0) |
214
|
|
|
|
|
|
|
goto on_error; |
215
|
|
|
|
|
|
|
|
216
|
1
|
|
|
|
|
|
goto done; |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
on_error: |
219
|
0
|
|
|
|
|
|
revert_state_cleanup(repo); |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
done: |
222
|
1
|
|
|
|
|
|
git_indexwriter_cleanup(&indexwriter); |
223
|
1
|
|
|
|
|
|
git_index_free(index); |
224
|
1
|
|
|
|
|
|
git_commit_free(our_commit); |
225
|
1
|
|
|
|
|
|
git_reference_free(our_ref); |
226
|
1
|
|
|
|
|
|
git_str_dispose(&their_label); |
227
|
|
|
|
|
|
|
|
228
|
1
|
|
|
|
|
|
return error; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
int git_revert_options_init(git_revert_options *opts, unsigned int version) |
232
|
|
|
|
|
|
|
{ |
233
|
0
|
0
|
|
|
|
|
GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
234
|
|
|
|
|
|
|
opts, version, git_revert_options, GIT_REVERT_OPTIONS_INIT); |
235
|
0
|
|
|
|
|
|
return 0; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
#ifndef GIT_DEPRECATE_HARD |
239
|
0
|
|
|
|
|
|
int git_revert_init_options(git_revert_options *opts, unsigned int version) |
240
|
|
|
|
|
|
|
{ |
241
|
0
|
|
|
|
|
|
return git_revert_options_init(opts, version); |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
#endif |