Blame src/revert.c

Packit ae9e2a
/*
Packit ae9e2a
* Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
*
Packit ae9e2a
* This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
* a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
*/
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "repository.h"
Packit ae9e2a
#include "filebuf.h"
Packit ae9e2a
#include "merge.h"
Packit ae9e2a
#include "index.h"
Packit ae9e2a
Packit ae9e2a
#include "git2/types.h"
Packit ae9e2a
#include "git2/merge.h"
Packit ae9e2a
#include "git2/revert.h"
Packit ae9e2a
#include "git2/commit.h"
Packit ae9e2a
#include "git2/sys/commit.h"
Packit ae9e2a
Packit ae9e2a
#define GIT_REVERT_FILE_MODE		0666
Packit ae9e2a
Packit ae9e2a
static int write_revert_head(
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	const char *commit_oidstr)
Packit ae9e2a
{
Packit ae9e2a
	git_filebuf file = GIT_FILEBUF_INIT;
Packit ae9e2a
	git_buf file_path = GIT_BUF_INIT;
Packit ae9e2a
	int error = 0;
Packit ae9e2a
Packit ae9e2a
	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_REVERT_HEAD_FILE)) >= 0 &&
Packit ae9e2a
		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) >= 0 &&
Packit ae9e2a
		(error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
Packit ae9e2a
		error = git_filebuf_commit(&file;;
Packit ae9e2a
Packit ae9e2a
	if (error < 0)
Packit ae9e2a
		git_filebuf_cleanup(&file;;
Packit ae9e2a
Packit ae9e2a
	git_buf_free(&file_path);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int write_merge_msg(
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	const char *commit_oidstr,
Packit ae9e2a
	const char *commit_msgline)
Packit ae9e2a
{
Packit ae9e2a
	git_filebuf file = GIT_FILEBUF_INIT;
Packit ae9e2a
	git_buf file_path = GIT_BUF_INIT;
Packit ae9e2a
	int error = 0;
Packit ae9e2a
Packit ae9e2a
	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
Packit ae9e2a
		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_REVERT_FILE_MODE)) < 0 ||
Packit ae9e2a
		(error = git_filebuf_printf(&file, "Revert \"%s\"\n\nThis reverts commit %s.\n",
Packit ae9e2a
		commit_msgline, commit_oidstr)) < 0)
Packit ae9e2a
		goto cleanup;
Packit ae9e2a
Packit ae9e2a
	error = git_filebuf_commit(&file;;
Packit ae9e2a
Packit ae9e2a
cleanup:
Packit ae9e2a
	if (error < 0)
Packit ae9e2a
		git_filebuf_cleanup(&file;;
Packit ae9e2a
Packit ae9e2a
	git_buf_free(&file_path);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int revert_normalize_opts(
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_revert_options *opts,
Packit ae9e2a
	const git_revert_options *given,
Packit ae9e2a
	const char *their_label)
Packit ae9e2a
{
Packit ae9e2a
	int error = 0;
Packit ae9e2a
	unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE |
Packit ae9e2a
		GIT_CHECKOUT_ALLOW_CONFLICTS;
Packit ae9e2a
Packit ae9e2a
	GIT_UNUSED(repo);
Packit ae9e2a
Packit ae9e2a
	if (given != NULL)
Packit ae9e2a
		memcpy(opts, given, sizeof(git_revert_options));
Packit ae9e2a
	else {
Packit ae9e2a
		git_revert_options default_opts = GIT_REVERT_OPTIONS_INIT;
Packit ae9e2a
		memcpy(opts, &default_opts, sizeof(git_revert_options));
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!opts->checkout_opts.checkout_strategy)
Packit ae9e2a
		opts->checkout_opts.checkout_strategy = default_checkout_strategy;
Packit ae9e2a
Packit ae9e2a
	if (!opts->checkout_opts.our_label)
Packit ae9e2a
		opts->checkout_opts.our_label = "HEAD";
Packit ae9e2a
Packit ae9e2a
	if (!opts->checkout_opts.their_label)
Packit ae9e2a
		opts->checkout_opts.their_label = their_label;
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int revert_state_cleanup(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	const char *state_files[] = { GIT_REVERT_HEAD_FILE, GIT_MERGE_MSG_FILE };
Packit ae9e2a
Packit ae9e2a
	return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int revert_seterr(git_commit *commit, const char *fmt)
Packit ae9e2a
{
Packit ae9e2a
	char commit_oidstr[GIT_OID_HEXSZ + 1];
Packit ae9e2a
Packit ae9e2a
	git_oid_fmt(commit_oidstr, git_commit_id(commit));
Packit ae9e2a
	commit_oidstr[GIT_OID_HEXSZ] = '\0';
Packit ae9e2a
Packit ae9e2a
	giterr_set(GITERR_REVERT, fmt, commit_oidstr);
Packit ae9e2a
Packit ae9e2a
	return -1;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_revert_commit(
Packit ae9e2a
	git_index **out,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_commit *revert_commit,
Packit ae9e2a
	git_commit *our_commit,
Packit ae9e2a
	unsigned int mainline,
Packit ae9e2a
	const git_merge_options *merge_opts)
Packit ae9e2a
{
Packit ae9e2a
	git_commit *parent_commit = NULL;
Packit ae9e2a
	git_tree *parent_tree = NULL, *our_tree = NULL, *revert_tree = NULL;
Packit ae9e2a
	int parent = 0, error = 0;
Packit ae9e2a
Packit ae9e2a
	assert(out && repo && revert_commit && our_commit);
Packit ae9e2a
Packit ae9e2a
	if (git_commit_parentcount(revert_commit) > 1) {
Packit ae9e2a
		if (!mainline)
Packit ae9e2a
			return revert_seterr(revert_commit,
Packit ae9e2a
				"mainline branch is not specified but %s is a merge commit");
Packit ae9e2a
Packit ae9e2a
		parent = mainline;
Packit ae9e2a
	} else {
Packit ae9e2a
		if (mainline)
Packit ae9e2a
			return revert_seterr(revert_commit,
Packit ae9e2a
				"mainline branch specified but %s is not a merge commit");
Packit ae9e2a
Packit ae9e2a
		parent = git_commit_parentcount(revert_commit);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (parent &&
Packit ae9e2a
		((error = git_commit_parent(&parent_commit, revert_commit, (parent - 1))) < 0 ||
Packit ae9e2a
		(error = git_commit_tree(&parent_tree, parent_commit)) < 0))
Packit ae9e2a
		goto done;
Packit ae9e2a
Packit ae9e2a
	if ((error = git_commit_tree(&revert_tree, revert_commit)) < 0 ||
Packit ae9e2a
		(error = git_commit_tree(&our_tree, our_commit)) < 0)
Packit ae9e2a
		goto done;
Packit ae9e2a
Packit ae9e2a
	error = git_merge_trees(out, repo, revert_tree, our_tree, parent_tree, merge_opts);
Packit ae9e2a
Packit ae9e2a
done:
Packit ae9e2a
	git_tree_free(parent_tree);
Packit ae9e2a
	git_tree_free(our_tree);
Packit ae9e2a
	git_tree_free(revert_tree);
Packit ae9e2a
	git_commit_free(parent_commit);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_revert(
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_commit *commit,
Packit ae9e2a
	const git_revert_options *given_opts)
Packit ae9e2a
{
Packit ae9e2a
	git_revert_options opts;
Packit ae9e2a
	git_reference *our_ref = NULL;
Packit ae9e2a
	git_commit *our_commit = NULL;
Packit ae9e2a
	char commit_oidstr[GIT_OID_HEXSZ + 1];
Packit ae9e2a
	const char *commit_msg;
Packit ae9e2a
	git_buf their_label = GIT_BUF_INIT;
Packit ae9e2a
	git_index *index = NULL;
Packit ae9e2a
	git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
Packit ae9e2a
	int error;
Packit ae9e2a
Packit ae9e2a
	assert(repo && commit);
Packit ae9e2a
Packit ae9e2a
	GITERR_CHECK_VERSION(given_opts, GIT_REVERT_OPTIONS_VERSION, "git_revert_options");
Packit ae9e2a
Packit ae9e2a
	if ((error = git_repository__ensure_not_bare(repo, "revert")) < 0)
Packit ae9e2a
		return error;
Packit ae9e2a
Packit ae9e2a
	git_oid_fmt(commit_oidstr, git_commit_id(commit));
Packit ae9e2a
	commit_oidstr[GIT_OID_HEXSZ] = '\0';
Packit ae9e2a
Packit ae9e2a
	if ((commit_msg = git_commit_summary(commit)) == NULL) {
Packit ae9e2a
		error = -1;
Packit ae9e2a
		goto on_error;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if ((error = git_buf_printf(&their_label, "parent of %.7s... %s", commit_oidstr, commit_msg)) < 0 ||
Packit ae9e2a
		(error = revert_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
Packit ae9e2a
		(error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 ||
Packit ae9e2a
		(error = write_revert_head(repo, commit_oidstr)) < 0 ||
Packit ae9e2a
		(error = write_merge_msg(repo, commit_oidstr, commit_msg)) < 0 ||
Packit ae9e2a
		(error = git_repository_head(&our_ref, repo)) < 0 ||
Packit ae9e2a
		(error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJ_COMMIT)) < 0 ||
Packit ae9e2a
		(error = git_revert_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
Packit ae9e2a
		(error = git_merge__check_result(repo, index)) < 0 ||
Packit ae9e2a
		(error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 ||
Packit ae9e2a
		(error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 ||
Packit ae9e2a
		(error = git_indexwriter_commit(&indexwriter)) < 0)
Packit ae9e2a
		goto on_error;
Packit ae9e2a
Packit ae9e2a
	goto done;
Packit ae9e2a
Packit ae9e2a
on_error:
Packit ae9e2a
	revert_state_cleanup(repo);
Packit ae9e2a
Packit ae9e2a
done:
Packit ae9e2a
	git_indexwriter_cleanup(&indexwriter);
Packit ae9e2a
	git_index_free(index);
Packit ae9e2a
	git_commit_free(our_commit);
Packit ae9e2a
	git_reference_free(our_ref);
Packit ae9e2a
	git_buf_free(&their_label);
Packit ae9e2a
Packit ae9e2a
	return error;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int git_revert_init_options(git_revert_options *opts, unsigned int version)
Packit ae9e2a
{
Packit ae9e2a
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Packit ae9e2a
		opts, version, git_revert_options, GIT_REVERT_OPTIONS_INIT);
Packit ae9e2a
	return 0;
Packit ae9e2a
}