cherrypick.c 6.37 KB
Newer Older
1 2 3 4 5 6 7 8
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/

#include "common.h"
9

10 11 12 13
#include "repository.h"
#include "filebuf.h"
#include "merge.h"
#include "vector.h"
14
#include "index.h"
15 16 17 18 19 20 21

#include "git2/types.h"
#include "git2/merge.h"
#include "git2/cherrypick.h"
#include "git2/commit.h"
#include "git2/sys/commit.h"

22
#define GIT_CHERRYPICK_FILE_MODE		0666
23

24
static int write_cherrypick_head(
25 26 27 28 29 30 31
	git_repository *repo,
	const char *commit_oidstr)
{
	git_filebuf file = GIT_FILEBUF_INIT;
	git_buf file_path = GIT_BUF_INIT;
	int error = 0;

32
	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_CHERRYPICK_HEAD_FILE)) >= 0 &&
33
		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRYPICK_FILE_MODE)) >= 0 &&
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
		(error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
		error = git_filebuf_commit(&file);

	if (error < 0)
		git_filebuf_cleanup(&file);

	git_buf_free(&file_path);

	return error;
}

static int write_merge_msg(
	git_repository *repo,
	const char *commit_msg)
{
	git_filebuf file = GIT_FILEBUF_INIT;
	git_buf file_path = GIT_BUF_INIT;
	int error = 0;

53
	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
54
		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRYPICK_FILE_MODE)) < 0 ||
55 56 57 58 59 60 61 62 63 64 65 66 67 68
		(error = git_filebuf_printf(&file, "%s", commit_msg)) < 0)
		goto cleanup;

	error = git_filebuf_commit(&file);

cleanup:
	if (error < 0)
		git_filebuf_cleanup(&file);

	git_buf_free(&file_path);

	return error;
}

69
static int cherrypick_normalize_opts(
70
	git_repository *repo,
71 72
	git_cherrypick_options *opts,
	const git_cherrypick_options *given,
73 74 75
	const char *their_label)
{
	int error = 0;
76
	unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE |
77 78 79 80 81
		GIT_CHECKOUT_ALLOW_CONFLICTS;

	GIT_UNUSED(repo);

	if (given != NULL)
82
		memcpy(opts, given, sizeof(git_cherrypick_options));
83
	else {
84 85
		git_cherrypick_options default_opts = GIT_CHERRYPICK_OPTIONS_INIT;
		memcpy(opts, &default_opts, sizeof(git_cherrypick_options));
86 87 88 89 90 91 92 93 94 95 96 97 98 99
	}

	if (!opts->checkout_opts.checkout_strategy)
		opts->checkout_opts.checkout_strategy = default_checkout_strategy;

	if (!opts->checkout_opts.our_label)
		opts->checkout_opts.our_label = "HEAD";

	if (!opts->checkout_opts.their_label)
		opts->checkout_opts.their_label = their_label;

	return error;
}

100
static int cherrypick_state_cleanup(git_repository *repo)
101
{
102
	const char *state_files[] = { GIT_CHERRYPICK_HEAD_FILE, GIT_MERGE_MSG_FILE };
103 104 105 106

	return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
}

107
static int cherrypick_seterr(git_commit *commit, const char *fmt)
108 109 110 111 112 113 114 115 116
{
	char commit_oidstr[GIT_OID_HEXSZ + 1];

	giterr_set(GITERR_CHERRYPICK, fmt,
		git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));

	return -1;
}

117
int git_cherrypick_commit(
118 119
	git_index **out,
	git_repository *repo,
120
	git_commit *cherrypick_commit,
121 122 123 124 125
	git_commit *our_commit,
	unsigned int mainline,
	const git_merge_options *merge_opts)
{
	git_commit *parent_commit = NULL;
126
	git_tree *parent_tree = NULL, *our_tree = NULL, *cherrypick_tree = NULL;
127 128
	int parent = 0, error = 0;

129
	assert(out && repo && cherrypick_commit && our_commit);
130

131
	if (git_commit_parentcount(cherrypick_commit) > 1) {
132
		if (!mainline)
133
			return cherrypick_seterr(cherrypick_commit,
134
				"mainline branch is not specified but %s is a merge commit");
135 136 137 138

		parent = mainline;
	} else {
		if (mainline)
139
			return cherrypick_seterr(cherrypick_commit,
140
				"mainline branch specified but %s is not a merge commit");
141

142
		parent = git_commit_parentcount(cherrypick_commit);
143 144 145
	}

	if (parent &&
146
		((error = git_commit_parent(&parent_commit, cherrypick_commit, (parent - 1))) < 0 ||
147 148 149
		(error = git_commit_tree(&parent_tree, parent_commit)) < 0))
		goto done;

150
	if ((error = git_commit_tree(&cherrypick_tree, cherrypick_commit)) < 0 ||
151 152 153
		(error = git_commit_tree(&our_tree, our_commit)) < 0)
		goto done;

154
	error = git_merge_trees(out, repo, parent_tree, our_tree, cherrypick_tree, merge_opts);
155 156 157 158

done:
	git_tree_free(parent_tree);
	git_tree_free(our_tree);
159
	git_tree_free(cherrypick_tree);
160 161 162 163 164
	git_commit_free(parent_commit);

	return error;
}

165
int git_cherrypick(
166 167
	git_repository *repo,
	git_commit *commit,
168
	const git_cherrypick_options *given_opts)
169
{
170
	git_cherrypick_options opts;
171 172 173 174 175
	git_reference *our_ref = NULL;
	git_commit *our_commit = NULL;
	char commit_oidstr[GIT_OID_HEXSZ + 1];
	const char *commit_msg, *commit_summary;
	git_buf their_label = GIT_BUF_INIT;
176
	git_index *index = NULL;
177
	git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
178 179 180 181
	int error = 0;

	assert(repo && commit);

182
	GITERR_CHECK_VERSION(given_opts, GIT_CHERRYPICK_OPTIONS_VERSION, "git_cherrypick_options");
183 184 185 186 187 188 189 190 191 192

	if ((error = git_repository__ensure_not_bare(repo, "cherry-pick")) < 0)
		return error;

	if ((commit_msg = git_commit_message(commit)) == NULL ||
		(commit_summary = git_commit_summary(commit)) == NULL) {
		error = -1;
		goto on_error;
	}

193
	git_oid_nfmt(commit_oidstr, sizeof(commit_oidstr), git_commit_id(commit));
194 195 196

	if ((error = write_merge_msg(repo, commit_msg)) < 0 ||
		(error = git_buf_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 ||
197 198 199
		(error = cherrypick_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
		(error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 ||
		(error = write_cherrypick_head(repo, commit_oidstr)) < 0 ||
200 201
		(error = git_repository_head(&our_ref, repo)) < 0 ||
		(error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJ_COMMIT)) < 0 ||
202 203 204 205 206
		(error = git_cherrypick_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
		(error = git_merge__check_result(repo, index)) < 0 ||
		(error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 ||
		(error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 ||
		(error = git_indexwriter_commit(&indexwriter)) < 0)
207
		goto on_error;
208

209 210 211
	goto done;

on_error:
212
	cherrypick_state_cleanup(repo);
213 214

done:
215 216
	git_indexwriter_cleanup(&indexwriter);
	git_index_free(index);
217 218 219 220 221 222 223
	git_commit_free(our_commit);
	git_reference_free(our_ref);
	git_buf_free(&their_label);

	return error;
}

224 225
int git_cherrypick_init_options(
	git_cherrypick_options *opts, unsigned int version)
226
{
227
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
228
		opts, version, git_cherrypick_options, GIT_CHERRYPICK_OPTIONS_INIT);
229
	return 0;
230
}