revert.c 6.45 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
#include "repository.h"
#include "filebuf.h"
#include "merge.h"
13
#include "index.h"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

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

#define GIT_REVERT_FILE_MODE		0666

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

31
	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_REVERT_HEAD_FILE)) >= 0 &&
32
		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_REVERT_FILE_MODE)) >= 0 &&
33 34 35 36 37 38
		(error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
		error = git_filebuf_commit(&file);

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

39
	git_buf_dispose(&file_path);
40 41 42 43 44 45 46 47 48 49 50 51 52

	return error;
}

static int write_merge_msg(
	git_repository *repo,
	const char *commit_oidstr,
	const char *commit_msgline)
{
	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_CREATE_LEADING_DIRS, GIT_REVERT_FILE_MODE)) < 0 ||
55 56 57 58 59 60 61 62 63 64
		(error = git_filebuf_printf(&file, "Revert \"%s\"\n\nThis reverts commit %s.\n",
		commit_msgline, commit_oidstr)) < 0)
		goto cleanup;

	error = git_filebuf_commit(&file);

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

65
	git_buf_dispose(&file_path);
66 67 68 69 70 71

	return error;
}

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

	GIT_UNUSED(repo);

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

	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;
}

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

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

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

	git_oid_fmt(commit_oidstr, git_commit_id(commit));
	commit_oidstr[GIT_OID_HEXSZ] = '\0';

115
	git_error_set(GIT_ERROR_REVERT, fmt, commit_oidstr);
116 117 118 119 120 121 122 123 124 125

	return -1;
}

int git_revert_commit(
	git_index **out,
	git_repository *repo,
	git_commit *revert_commit,
	git_commit *our_commit,
	unsigned int mainline,
126
	const git_merge_options *merge_opts)
127 128 129 130 131 132 133 134 135 136
{
	git_commit *parent_commit = NULL;
	git_tree *parent_tree = NULL, *our_tree = NULL, *revert_tree = NULL;
	int parent = 0, error = 0;

	assert(out && repo && revert_commit && our_commit);

	if (git_commit_parentcount(revert_commit) > 1) {
		if (!mainline)
			return revert_seterr(revert_commit,
137
				"mainline branch is not specified but %s is a merge commit");
138 139 140 141 142

		parent = mainline;
	} else {
		if (mainline)
			return revert_seterr(revert_commit,
143
				"mainline branch specified but %s is not a merge commit");
144 145 146 147 148 149 150 151 152 153 154 155 156

		parent = git_commit_parentcount(revert_commit);
	}

	if (parent &&
		((error = git_commit_parent(&parent_commit, revert_commit, (parent - 1))) < 0 ||
		(error = git_commit_tree(&parent_tree, parent_commit)) < 0))
		goto done;

	if ((error = git_commit_tree(&revert_tree, revert_commit)) < 0 ||
		(error = git_commit_tree(&our_tree, our_commit)) < 0)
		goto done;

157
	error = git_merge_trees(out, repo, revert_tree, our_tree, parent_tree, merge_opts);
158 159 160 161 162 163 164 165 166 167

done:
	git_tree_free(parent_tree);
	git_tree_free(our_tree);
	git_tree_free(revert_tree);
	git_commit_free(parent_commit);

	return error;
}

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

	assert(repo && commit);

185
	GIT_ERROR_CHECK_VERSION(given_opts, GIT_REVERT_OPTIONS_VERSION, "git_revert_options");
186

187 188 189 190 191 192 193 194 195 196 197 198
	if ((error = git_repository__ensure_not_bare(repo, "revert")) < 0)
		return error;

	git_oid_fmt(commit_oidstr, git_commit_id(commit));
	commit_oidstr[GIT_OID_HEXSZ] = '\0';

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

	if ((error = git_buf_printf(&their_label, "parent of %.7s... %s", commit_oidstr, commit_msg)) < 0 ||
199 200 201
		(error = revert_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_revert_head(repo, commit_oidstr)) < 0 ||
Edward Thomson committed
202
		(error = write_merge_msg(repo, commit_oidstr, commit_msg)) < 0 ||
203
		(error = git_repository_head(&our_ref, repo)) < 0 ||
204
		(error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJECT_COMMIT)) < 0 ||
205 206 207 208 209
		(error = git_revert_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)
210 211 212 213 214
		goto on_error;

	goto done;

on_error:
215
	revert_state_cleanup(repo);
216 217

done:
218 219
	git_indexwriter_cleanup(&indexwriter);
	git_index_free(index);
220 221
	git_commit_free(our_commit);
	git_reference_free(our_ref);
222
	git_buf_dispose(&their_label);
223 224 225

	return error;
}
226

227
int git_revert_options_init(git_revert_options *opts, unsigned int version)
228
{
229 230 231
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_revert_options, GIT_REVERT_OPTIONS_INIT);
	return 0;
232
}
233

234
#ifndef GIT_DEPRECATE_HARD
235 236 237 238
int git_revert_init_options(git_revert_options *opts, unsigned int version)
{
	return git_revert_options_init(opts, version);
}
239
#endif