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