deps/libgit2/src/libgit2/cherrypick.c

filter: Statementstmt 80.9% 98 / 121 Branchbran 48.3% 30 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.9% 128 / 183 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« const-c-object.inc ↑ Summary deps/libgit2/src/util/pool.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  
10 #include "repository.h"
11 #include "filebuf.h"
12 #include "merge.h"
13 #include "vector.h"
14 #include "index.h"
15  
16 #include "git2/types.h"
17 #include "git2/merge.h"
18 #include "git2/cherrypick.h"
19 #include "git2/commit.h"
20 #include "git2/sys/commit.h"
21  
22 #define GIT_CHERRYPICK_FILE_MODE 0666
23  
24 6 static int write_cherrypick_head(
25 git_repository *repo,
26 const char *commit_oidstr)
27 {
28 6 git_filebuf file = GIT_FILEBUF_INIT;
29 6 git_str file_path = GIT_STR_INIT;
30 6 int error = 0;
31  
32 12 if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_CHERRYPICK_HEAD_FILE)) >= 0 &&
33 12 ▶ (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_CHERRYPICK_FILE_MODE)) >= 0 &&
Branch: 1 branch
BranchTrueFalseTotal
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_CHERRYPICK_FILE_MODE)) >= 0 && 6 0 6
34 6 (error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
35 6 error = git_filebuf_commit(&file);
36  
37 6 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 6 6
38 0 git_filebuf_cleanup(&file);
39  
40 6 git_str_dispose(&file_path);
41  
42 6 return error;
43 }
44  
45 6 static int write_merge_msg(
46 git_repository *repo,
47 const char *commit_msg)
48 {
49 6 git_filebuf file = GIT_FILEBUF_INIT;
50 6 git_str file_path = GIT_STR_INIT;
51 6 int error = 0;
52  
53 12 if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
54 12 ▶ (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_CHERRYPICK_FILE_MODE)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_CHERRYPICK_FILE_MODE)) < 0 || 0 6 6
55 6 (error = git_filebuf_printf(&file, "%s", commit_msg)) < 0)
56 0 goto cleanup;
57  
58 6 error = git_filebuf_commit(&file);
59  
60 6 cleanup:
61 6 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 6 6
62 0 git_filebuf_cleanup(&file);
63  
64 6 git_str_dispose(&file_path);
65  
66 6 return error;
67 }
68  
69 6 static int cherrypick_normalize_opts(
70 git_repository *repo,
71 git_cherrypick_options *opts,
72 const git_cherrypick_options *given,
73 const char *their_label)
74 {
75 6 int error = 0;
76 6 unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE |
77 GIT_CHECKOUT_ALLOW_CONFLICTS;
78  
79 6 GIT_UNUSED(repo);
80  
81 6 ▶ if (given != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (given != NULL) 6 0 6
82 6 memcpy(opts, given, sizeof(git_cherrypick_options));
83 else {
84 0 git_cherrypick_options default_opts = GIT_CHERRYPICK_OPTIONS_INIT;
85 0 memcpy(opts, &default_opts, sizeof(git_cherrypick_options));
86 }
87  
88 6 ▶ if (!opts->checkout_opts.checkout_strategy)
Branch: 1 branch
BranchTrueFalseTotal
if (!opts->checkout_opts.checkout_strategy) 0 6 6
89 0 opts->checkout_opts.checkout_strategy = default_checkout_strategy;
90  
91 6 ▶ if (!opts->checkout_opts.our_label)
Branch: 1 branch
BranchTrueFalseTotal
if (!opts->checkout_opts.our_label) 6 0 6
92 6 opts->checkout_opts.our_label = "HEAD";
93  
94 6 ▶ if (!opts->checkout_opts.their_label)
Branch: 1 branch
BranchTrueFalseTotal
if (!opts->checkout_opts.their_label) 6 0 6
95 6 opts->checkout_opts.their_label = their_label;
96  
97 6 return error;
98 }
99  
100 1 static int cherrypick_state_cleanup(git_repository *repo)
101 {
102 1 const char *state_files[] = { GIT_CHERRYPICK_HEAD_FILE, GIT_MERGE_MSG_FILE };
103  
104 1 return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
105 }
106  
107 0 static int cherrypick_seterr(git_commit *commit, const char *fmt)
108 {
109 char commit_oidstr[GIT_OID_HEXSZ + 1];
110  
111 0 git_error_set(GIT_ERROR_CHERRYPICK, fmt,
112 git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));
113  
114 0 return -1;
115 }
116  
117 6 int git_cherrypick_commit(
118 git_index **out,
119 git_repository *repo,
120 git_commit *cherrypick_commit,
121 git_commit *our_commit,
122 unsigned int mainline,
123 const git_merge_options *merge_opts)
124 {
125 6 git_commit *parent_commit = NULL;
126 6 git_tree *parent_tree = NULL, *our_tree = NULL, *cherrypick_tree = NULL;
127 6 int parent = 0, error = 0;
128  
129 6 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 6 6
130 6 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 6 6
131 6 ▶ GIT_ASSERT_ARG(cherrypick_commit);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cherrypick_commit); 0 6 6
132 6 ▶ GIT_ASSERT_ARG(our_commit);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(our_commit); 0 6 6
133  
134 6 ▶ if (git_commit_parentcount(cherrypick_commit) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_commit_parentcount(cherrypick_commit) > 1) { 0 6 6
135 0 ▶ if (!mainline)
Branch: 1 branch
BranchTrueFalseTotal
if (!mainline) 0 0 0
136 0 return cherrypick_seterr(cherrypick_commit,
137 "mainline branch is not specified but %s is a merge commit");
138  
139 0 parent = mainline;
140 } else {
141 6 ▶ if (mainline)
Branch: 1 branch
BranchTrueFalseTotal
if (mainline) 0 6 6
142 0 return cherrypick_seterr(cherrypick_commit,
143 "mainline branch specified but %s is not a merge commit");
144  
145 6 parent = git_commit_parentcount(cherrypick_commit);
146 }
147  
148 12 if (parent &&
149 12 ▶ ((error = git_commit_parent(&parent_commit, cherrypick_commit, (parent - 1))) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
((error = git_commit_parent(&parent_commit, cherrypick_commit, (parent - 1))) < 0 || 0 6 6
150 6 (error = git_commit_tree(&parent_tree, parent_commit)) < 0))
151 0 goto done;
152  
153 12 if ((error = git_commit_tree(&cherrypick_tree, cherrypick_commit)) < 0 ||
154 6 (error = git_commit_tree(&our_tree, our_commit)) < 0)
155 0 goto done;
156  
157 6 error = git_merge_trees(out, repo, parent_tree, our_tree, cherrypick_tree, merge_opts);
158  
159 6 done:
160 6 git_tree_free(parent_tree);
161 6 git_tree_free(our_tree);
162 6 git_tree_free(cherrypick_tree);
163 6 git_commit_free(parent_commit);
164  
165 6 return error;
166 }
167  
168 6 int git_cherrypick(
169 git_repository *repo,
170 git_commit *commit,
171 const git_cherrypick_options *given_opts)
172 {
173 git_cherrypick_options opts;
174 6 git_reference *our_ref = NULL;
175 6 git_commit *our_commit = NULL;
176 char commit_oidstr[GIT_OID_HEXSZ + 1];
177 const char *commit_msg, *commit_summary;
178 6 git_str their_label = GIT_STR_INIT;
179 6 git_index *index = NULL;
180 6 git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
181 6 int error = 0;
182  
183 6 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 6 6
184 6 ▶ GIT_ASSERT_ARG(commit);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(commit); 0 6 6
185  
186 6 ▶ GIT_ERROR_CHECK_VERSION(given_opts, GIT_CHERRYPICK_OPTIONS_VERSION, "git_cherrypick_options");
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_VERSION(given_opts, GIT_CHERRYPICK_OPTIONS_VERSION, "git_cherrypick_options"); 0 6 6
187  
188 6 ▶ if ((error = git_repository__ensure_not_bare(repo, "cherry-pick")) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository__ensure_not_bare(repo, "cherry-pick")) < 0) 0 6 6
189 0 return error;
190  
191 12 if ((commit_msg = git_commit_message(commit)) == NULL ||
192 6 (commit_summary = git_commit_summary(commit)) == NULL) {
193 0 error = -1;
194 0 goto on_error;
195 }
196  
197 6 git_oid_nfmt(commit_oidstr, sizeof(commit_oidstr), git_commit_id(commit));
198  
199 12 if ((error = write_merge_msg(repo, commit_msg)) < 0 ||
200 12 ▶ (error = git_str_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_str_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 || 6 0 6
201 12 ▶ (error = cherrypick_normalize_opts(repo, &opts, given_opts, git_str_cstr(&their_label))) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = cherrypick_normalize_opts(repo, &opts, given_opts, git_str_cstr(&their_label))) < 0 || 6 0 6
202 12 ▶ (error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 || 6 0 6
203 12 ▶ (error = write_cherrypick_head(repo, commit_oidstr)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = write_cherrypick_head(repo, commit_oidstr)) < 0 || 6 0 6
204 12 ▶ (error = git_repository_head(&our_ref, repo)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_repository_head(&our_ref, repo)) < 0 || 6 0 6
205 12 ▶ (error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJECT_COMMIT)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJECT_COMMIT)) < 0 || 6 0 6
206 12 ▶ (error = git_cherrypick_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_cherrypick_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 || 5 1 6
207 11 ▶ (error = git_merge__check_result(repo, index)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_merge__check_result(repo, index)) < 0 || 5 0 5
208 10 ▶ (error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 || 5 0 5
209 10 ▶ (error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
(error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 || 0 5 5
210 5 (error = git_indexwriter_commit(&indexwriter)) < 0)
211 1 goto on_error;
212  
213 5 goto done;
214  
215 1 on_error:
216 1 cherrypick_state_cleanup(repo);
217  
218 6 done:
219 6 git_indexwriter_cleanup(&indexwriter);
220 6 git_index_free(index);
221 6 git_commit_free(our_commit);
222 6 git_reference_free(our_ref);
223 6 git_str_dispose(&their_label);
224  
225 6 return error;
226 }
227  
228 0 int git_cherrypick_options_init(
229 git_cherrypick_options *opts, unsigned int version)
230 {
231 0 ▶ GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Branch: 1 branch
BranchTrueFalseTotal
GIT_INIT_STRUCTURE_FROM_TEMPLATE( 0 0 0
232 opts, version, git_cherrypick_options, GIT_CHERRYPICK_OPTIONS_INIT);
233 0 return 0;
234 }
235  
236 #ifndef GIT_DEPRECATE_HARD
237 0 int git_cherrypick_init_options(
238 git_cherrypick_options *opts, unsigned int version)
239 {
240 0 return git_cherrypick_options_init(opts, version);
241 }
242 #endif
« const-c-object.inc ↑ Summary deps/libgit2/src/util/pool.c »