]>
Commit | Line | Data |
---|---|---|
4d7b9939 JG |
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" | |
eae0bfdc | 9 | |
4d7b9939 JG |
10 | #include "repository.h" |
11 | #include "filebuf.h" | |
12 | #include "merge.h" | |
13 | #include "vector.h" | |
be8404a7 | 14 | #include "index.h" |
4d7b9939 JG |
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 | ||
0ba4dca5 | 22 | #define GIT_CHERRYPICK_FILE_MODE 0666 |
4d7b9939 | 23 | |
0ba4dca5 | 24 | static int write_cherrypick_head( |
4d7b9939 JG |
25 | git_repository *repo, |
26 | const char *commit_oidstr) | |
27 | { | |
28 | git_filebuf file = GIT_FILEBUF_INIT; | |
29 | git_buf file_path = GIT_BUF_INIT; | |
30 | int error = 0; | |
31 | ||
84f56cb0 | 32 | if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_CHERRYPICK_HEAD_FILE)) >= 0 && |
0ba4dca5 | 33 | (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRYPICK_FILE_MODE)) >= 0 && |
4d7b9939 JG |
34 | (error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0) |
35 | error = git_filebuf_commit(&file); | |
36 | ||
37 | if (error < 0) | |
38 | git_filebuf_cleanup(&file); | |
39 | ||
ac3d33df | 40 | git_buf_dispose(&file_path); |
4d7b9939 JG |
41 | |
42 | return error; | |
43 | } | |
44 | ||
45 | static int write_merge_msg( | |
46 | git_repository *repo, | |
47 | const char *commit_msg) | |
48 | { | |
49 | git_filebuf file = GIT_FILEBUF_INIT; | |
50 | git_buf file_path = GIT_BUF_INIT; | |
51 | int error = 0; | |
52 | ||
84f56cb0 | 53 | if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 || |
0ba4dca5 | 54 | (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRYPICK_FILE_MODE)) < 0 || |
4d7b9939 JG |
55 | (error = git_filebuf_printf(&file, "%s", commit_msg)) < 0) |
56 | goto cleanup; | |
57 | ||
58 | error = git_filebuf_commit(&file); | |
59 | ||
60 | cleanup: | |
61 | if (error < 0) | |
62 | git_filebuf_cleanup(&file); | |
63 | ||
ac3d33df | 64 | git_buf_dispose(&file_path); |
4d7b9939 JG |
65 | |
66 | return error; | |
67 | } | |
68 | ||
0ba4dca5 | 69 | static int cherrypick_normalize_opts( |
4d7b9939 | 70 | git_repository *repo, |
0ba4dca5 ET |
71 | git_cherrypick_options *opts, |
72 | const git_cherrypick_options *given, | |
4d7b9939 JG |
73 | const char *their_label) |
74 | { | |
75 | int error = 0; | |
094cfc29 | 76 | unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE | |
4d7b9939 JG |
77 | GIT_CHECKOUT_ALLOW_CONFLICTS; |
78 | ||
79 | GIT_UNUSED(repo); | |
80 | ||
81 | if (given != NULL) | |
0ba4dca5 | 82 | memcpy(opts, given, sizeof(git_cherrypick_options)); |
4d7b9939 | 83 | else { |
0ba4dca5 ET |
84 | git_cherrypick_options default_opts = GIT_CHERRYPICK_OPTIONS_INIT; |
85 | memcpy(opts, &default_opts, sizeof(git_cherrypick_options)); | |
4d7b9939 JG |
86 | } |
87 | ||
88 | if (!opts->checkout_opts.checkout_strategy) | |
89 | opts->checkout_opts.checkout_strategy = default_checkout_strategy; | |
90 | ||
91 | if (!opts->checkout_opts.our_label) | |
92 | opts->checkout_opts.our_label = "HEAD"; | |
93 | ||
94 | if (!opts->checkout_opts.their_label) | |
95 | opts->checkout_opts.their_label = their_label; | |
96 | ||
97 | return error; | |
98 | } | |
99 | ||
0ba4dca5 | 100 | static int cherrypick_state_cleanup(git_repository *repo) |
4d7b9939 | 101 | { |
0ba4dca5 | 102 | const char *state_files[] = { GIT_CHERRYPICK_HEAD_FILE, GIT_MERGE_MSG_FILE }; |
4d7b9939 JG |
103 | |
104 | return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); | |
105 | } | |
106 | ||
0ba4dca5 | 107 | static int cherrypick_seterr(git_commit *commit, const char *fmt) |
4d7b9939 JG |
108 | { |
109 | char commit_oidstr[GIT_OID_HEXSZ + 1]; | |
110 | ||
ac3d33df | 111 | git_error_set(GIT_ERROR_CHERRYPICK, fmt, |
4d7b9939 JG |
112 | git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit))); |
113 | ||
114 | return -1; | |
115 | } | |
116 | ||
0ba4dca5 | 117 | int git_cherrypick_commit( |
4d7b9939 JG |
118 | git_index **out, |
119 | git_repository *repo, | |
0ba4dca5 | 120 | git_commit *cherrypick_commit, |
4d7b9939 JG |
121 | git_commit *our_commit, |
122 | unsigned int mainline, | |
123 | const git_merge_options *merge_opts) | |
124 | { | |
125 | git_commit *parent_commit = NULL; | |
0ba4dca5 | 126 | git_tree *parent_tree = NULL, *our_tree = NULL, *cherrypick_tree = NULL; |
4d7b9939 JG |
127 | int parent = 0, error = 0; |
128 | ||
0ba4dca5 | 129 | assert(out && repo && cherrypick_commit && our_commit); |
4d7b9939 | 130 | |
0ba4dca5 | 131 | if (git_commit_parentcount(cherrypick_commit) > 1) { |
4d7b9939 | 132 | if (!mainline) |
0ba4dca5 | 133 | return cherrypick_seterr(cherrypick_commit, |
909d5494 | 134 | "mainline branch is not specified but %s is a merge commit"); |
4d7b9939 JG |
135 | |
136 | parent = mainline; | |
137 | } else { | |
138 | if (mainline) | |
0ba4dca5 | 139 | return cherrypick_seterr(cherrypick_commit, |
909d5494 | 140 | "mainline branch specified but %s is not a merge commit"); |
4d7b9939 | 141 | |
0ba4dca5 | 142 | parent = git_commit_parentcount(cherrypick_commit); |
4d7b9939 JG |
143 | } |
144 | ||
145 | if (parent && | |
0ba4dca5 | 146 | ((error = git_commit_parent(&parent_commit, cherrypick_commit, (parent - 1))) < 0 || |
4d7b9939 JG |
147 | (error = git_commit_tree(&parent_tree, parent_commit)) < 0)) |
148 | goto done; | |
149 | ||
0ba4dca5 | 150 | if ((error = git_commit_tree(&cherrypick_tree, cherrypick_commit)) < 0 || |
4d7b9939 JG |
151 | (error = git_commit_tree(&our_tree, our_commit)) < 0) |
152 | goto done; | |
153 | ||
0ba4dca5 | 154 | error = git_merge_trees(out, repo, parent_tree, our_tree, cherrypick_tree, merge_opts); |
4d7b9939 JG |
155 | |
156 | done: | |
157 | git_tree_free(parent_tree); | |
158 | git_tree_free(our_tree); | |
0ba4dca5 | 159 | git_tree_free(cherrypick_tree); |
4d7b9939 JG |
160 | git_commit_free(parent_commit); |
161 | ||
162 | return error; | |
163 | } | |
164 | ||
0ba4dca5 | 165 | int git_cherrypick( |
4d7b9939 JG |
166 | git_repository *repo, |
167 | git_commit *commit, | |
0ba4dca5 | 168 | const git_cherrypick_options *given_opts) |
4d7b9939 | 169 | { |
0ba4dca5 | 170 | git_cherrypick_options opts; |
4d7b9939 JG |
171 | git_reference *our_ref = NULL; |
172 | git_commit *our_commit = NULL; | |
173 | char commit_oidstr[GIT_OID_HEXSZ + 1]; | |
174 | const char *commit_msg, *commit_summary; | |
175 | git_buf their_label = GIT_BUF_INIT; | |
41fae48d | 176 | git_index *index = NULL; |
be8404a7 | 177 | git_indexwriter indexwriter = GIT_INDEXWRITER_INIT; |
4d7b9939 JG |
178 | int error = 0; |
179 | ||
180 | assert(repo && commit); | |
181 | ||
ac3d33df | 182 | GIT_ERROR_CHECK_VERSION(given_opts, GIT_CHERRYPICK_OPTIONS_VERSION, "git_cherrypick_options"); |
4d7b9939 JG |
183 | |
184 | if ((error = git_repository__ensure_not_bare(repo, "cherry-pick")) < 0) | |
185 | return error; | |
186 | ||
187 | if ((commit_msg = git_commit_message(commit)) == NULL || | |
188 | (commit_summary = git_commit_summary(commit)) == NULL) { | |
189 | error = -1; | |
190 | goto on_error; | |
191 | } | |
192 | ||
2bed3553 | 193 | git_oid_nfmt(commit_oidstr, sizeof(commit_oidstr), git_commit_id(commit)); |
4d7b9939 JG |
194 | |
195 | if ((error = write_merge_msg(repo, commit_msg)) < 0 || | |
196 | (error = git_buf_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 || | |
41fae48d ET |
197 | (error = cherrypick_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 || |
198 | (error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 || | |
199 | (error = write_cherrypick_head(repo, commit_oidstr)) < 0 || | |
4d7b9939 | 200 | (error = git_repository_head(&our_ref, repo)) < 0 || |
ac3d33df | 201 | (error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJECT_COMMIT)) < 0 || |
41fae48d ET |
202 | (error = git_cherrypick_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 || |
203 | (error = git_merge__check_result(repo, index)) < 0 || | |
204 | (error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 || | |
205 | (error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 || | |
206 | (error = git_indexwriter_commit(&indexwriter)) < 0) | |
4d7b9939 | 207 | goto on_error; |
be8404a7 | 208 | |
4d7b9939 JG |
209 | goto done; |
210 | ||
211 | on_error: | |
0ba4dca5 | 212 | cherrypick_state_cleanup(repo); |
4d7b9939 JG |
213 | |
214 | done: | |
be8404a7 ET |
215 | git_indexwriter_cleanup(&indexwriter); |
216 | git_index_free(index); | |
4d7b9939 JG |
217 | git_commit_free(our_commit); |
218 | git_reference_free(our_ref); | |
ac3d33df | 219 | git_buf_dispose(&their_label); |
4d7b9939 JG |
220 | |
221 | return error; | |
222 | } | |
223 | ||
0ba4dca5 ET |
224 | int git_cherrypick_init_options( |
225 | git_cherrypick_options *opts, unsigned int version) | |
4d7b9939 | 226 | { |
bc91347b | 227 | GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
0ba4dca5 | 228 | opts, version, git_cherrypick_options, GIT_CHERRYPICK_OPTIONS_INIT); |
bc91347b | 229 | return 0; |
4d7b9939 | 230 | } |