Blame src/rebase.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "filebuf.h"
Packit Service 20376f
#include "merge.h"
Packit Service 20376f
#include "array.h"
Packit Service 20376f
#include "config.h"
Packit Service 20376f
#include "annotated_commit.h"
Packit Service 20376f
#include "index.h"
Packit Service 20376f
Packit Service 20376f
#include <git2/types.h>
Packit Service 20376f
#include <git2/annotated_commit.h>
Packit Service 20376f
#include <git2/rebase.h>
Packit Service 20376f
#include <git2/commit.h>
Packit Service 20376f
#include <git2/reset.h>
Packit Service 20376f
#include <git2/revwalk.h>
Packit Service 20376f
#include <git2/notes.h>
Packit Service 20376f
Packit Service 20376f
#define REBASE_APPLY_DIR    "rebase-apply"
Packit Service 20376f
#define REBASE_MERGE_DIR    "rebase-merge"
Packit Service 20376f
Packit Service 20376f
#define HEAD_NAME_FILE      "head-name"
Packit Service 20376f
#define ORIG_HEAD_FILE      "orig-head"
Packit Service 20376f
#define HEAD_FILE           "head"
Packit Service 20376f
#define ONTO_FILE           "onto"
Packit Service 20376f
#define ONTO_NAME_FILE      "onto_name"
Packit Service 20376f
#define QUIET_FILE          "quiet"
Packit Service 20376f
Packit Service 20376f
#define MSGNUM_FILE         "msgnum"
Packit Service 20376f
#define END_FILE            "end"
Packit Service 20376f
#define CMT_FILE_FMT        "cmt.%" PRIuZ
Packit Service 20376f
#define CURRENT_FILE        "current"
Packit Service 20376f
#define REWRITTEN_FILE      "rewritten"
Packit Service 20376f
Packit Service 20376f
#define ORIG_DETACHED_HEAD  "detached HEAD"
Packit Service 20376f
Packit Service 20376f
#define NOTES_DEFAULT_REF   NULL
Packit Service 20376f
Packit Service 20376f
#define REBASE_DIR_MODE     0777
Packit Service 20376f
#define REBASE_FILE_MODE    0666
Packit Service 20376f
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_REBASE_TYPE_NONE = 0,
Packit Service 20376f
	GIT_REBASE_TYPE_APPLY = 1,
Packit Service 20376f
	GIT_REBASE_TYPE_MERGE = 2,
Packit Service 20376f
	GIT_REBASE_TYPE_INTERACTIVE = 3,
Packit Service 20376f
} git_rebase_type_t;
Packit Service 20376f
Packit Service 20376f
struct git_rebase {
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
Packit Service 20376f
	git_rebase_options options;
Packit Service 20376f
Packit Service 20376f
	git_rebase_type_t type;
Packit Service 20376f
	char *state_path;
Packit Service 20376f
Packit Service 20376f
	int head_detached : 1,
Packit Service 20376f
		inmemory : 1,
Packit Service 20376f
		quiet : 1,
Packit Service 20376f
		started : 1;
Packit Service 20376f
Packit Service 20376f
	git_array_t(git_rebase_operation) operations;
Packit Service 20376f
	size_t current;
Packit Service 20376f
Packit Service 20376f
	/* Used by in-memory rebase */
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_commit *last_commit;
Packit Service 20376f
Packit Service 20376f
	/* Used by regular (not in-memory) merge-style rebase */
Packit Service 20376f
	git_oid orig_head_id;
Packit Service 20376f
	char *orig_head_name;
Packit Service 20376f
Packit Service 20376f
	git_oid onto_id;
Packit Service 20376f
	char *onto_name;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
#define GIT_REBASE_STATE_INIT {0}
Packit Service 20376f
Packit Service 20376f
static int rebase_state_type(
Packit Service 20376f
	git_rebase_type_t *type_out,
Packit Service 20376f
	char **path_out,
Packit Service 20376f
	git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	git_rebase_type_t type = GIT_REBASE_TYPE_NONE;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&path, repo->gitdir, REBASE_APPLY_DIR) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git_path_isdir(git_buf_cstr(&path))) {
Packit Service 20376f
		type = GIT_REBASE_TYPE_APPLY;
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(&path);
Packit Service 20376f
	if (git_buf_joinpath(&path, repo->gitdir, REBASE_MERGE_DIR) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git_path_isdir(git_buf_cstr(&path))) {
Packit Service 20376f
		type = GIT_REBASE_TYPE_MERGE;
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	*type_out = type;
Packit Service 20376f
Packit Service 20376f
	if (type != GIT_REBASE_TYPE_NONE && path_out)
Packit Service 20376f
		*path_out = git_buf_detach(&path);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) rebase_readfile(
Packit Service 20376f
	git_buf *out,
Packit Service 20376f
	git_buf *state_path,
Packit Service 20376f
	const char *filename)
Packit Service 20376f
{
Packit Service 20376f
	size_t state_path_len = state_path->size;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(out);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(state_path, state_path->ptr, filename)) < 0 ||
Packit Service 20376f
		(error = git_futils_readbuffer(out, state_path->ptr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_buf_rtrim(out);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_buf_truncate(state_path, state_path_len);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) rebase_readint(
Packit Service 20376f
	size_t *out, git_buf *asc_out, git_buf *state_path, const char *filename)
Packit Service 20376f
{
Packit Service 20376f
	int32_t num;
Packit Service 20376f
	const char *eol;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_readfile(asc_out, state_path, filename)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (git__strntol32(&num, asc_out->ptr, asc_out->size, &eol, 10) < 0 || num < 0 || *eol) {
Packit Service 20376f
		giterr_set(GITERR_REBASE, "the file '%s' contains an invalid numeric value", filename);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = (size_t) num;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) rebase_readoid(
Packit Service 20376f
	git_oid *out, git_buf *str_out, git_buf *state_path, const char *filename)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_readfile(str_out, state_path, filename)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (str_out->size != GIT_OID_HEXSZ || git_oid_fromstr(out, str_out->ptr) < 0) {
Packit Service 20376f
		giterr_set(GITERR_REBASE, "the file '%s' contains an invalid object ID", filename);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static git_rebase_operation *rebase_operation_alloc(
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	git_rebase_operation_t type,
Packit Service 20376f
	git_oid *id,
Packit Service 20376f
	const char *exec)
Packit Service 20376f
{
Packit Service 20376f
	git_rebase_operation *operation;
Packit Service 20376f
Packit Service 20376f
	assert((type == GIT_REBASE_OPERATION_EXEC) == !id);
Packit Service 20376f
	assert((type == GIT_REBASE_OPERATION_EXEC) == !!exec);
Packit Service 20376f
Packit Service 20376f
	if ((operation = git_array_alloc(rebase->operations)) == NULL)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	operation->type = type;
Packit Service 20376f
	git_oid_cpy((git_oid *)&operation->id, id);
Packit Service 20376f
	operation->exec = exec;
Packit Service 20376f
Packit Service 20376f
	return operation;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_open_merge(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	git_buf state_path = GIT_BUF_INIT, buf = GIT_BUF_INIT, cmt = GIT_BUF_INIT;
Packit Service 20376f
	git_oid id;
Packit Service 20376f
	git_rebase_operation *operation;
Packit Service 20376f
	size_t i, msgnum = 0, end;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_puts(&state_path, rebase->state_path)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	/* Read 'msgnum' if it exists (otherwise, let msgnum = 0) */
Packit Service 20376f
	if ((error = rebase_readint(&msgnum, &buf, &state_path, MSGNUM_FILE)) < 0 &&
Packit Service 20376f
		error != GIT_ENOTFOUND)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (msgnum) {
Packit Service 20376f
		rebase->started = 1;
Packit Service 20376f
		rebase->current = msgnum - 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Read 'end' */
Packit Service 20376f
	if ((error = rebase_readint(&end, &buf, &state_path, END_FILE)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	/* Read 'current' if it exists */
Packit Service 20376f
	if ((error = rebase_readoid(&id, &buf, &state_path, CURRENT_FILE)) < 0 &&
Packit Service 20376f
		error != GIT_ENOTFOUND)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	/* Read cmt.* */
Packit Service 20376f
	git_array_init_to_size(rebase->operations, end);
Packit Service 20376f
	GITERR_CHECK_ARRAY(rebase->operations);
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < end; i++) {
Packit Service 20376f
		git_buf_clear(&cmt);
Packit Service 20376f
Packit Service 20376f
		if ((error = git_buf_printf(&cmt, "cmt.%" PRIuZ, (i+1))) < 0 ||
Packit Service 20376f
			(error = rebase_readoid(&id, &buf, &state_path, cmt.ptr)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL);
Packit Service 20376f
		GITERR_CHECK_ALLOC(operation);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Read 'onto_name' */
Packit Service 20376f
	if ((error = rebase_readfile(&buf, &state_path, ONTO_NAME_FILE)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	rebase->onto_name = git_buf_detach(&buf;;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_buf_free(&cmt);
Packit Service 20376f
	git_buf_free(&state_path);
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_alloc(git_rebase **out, const git_rebase_options *rebase_opts)
Packit Service 20376f
{
Packit Service 20376f
	git_rebase *rebase = git__calloc(1, sizeof(git_rebase));
Packit Service 20376f
	GITERR_CHECK_ALLOC(rebase);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if (rebase_opts)
Packit Service 20376f
		memcpy(&rebase->options, rebase_opts, sizeof(git_rebase_options));
Packit Service 20376f
	else
Packit Service 20376f
		git_rebase_init_options(&rebase->options, GIT_REBASE_OPTIONS_VERSION);
Packit Service 20376f
Packit Service 20376f
	if (rebase_opts && rebase_opts->rewrite_notes_ref) {
Packit Service 20376f
		rebase->options.rewrite_notes_ref = git__strdup(rebase_opts->rewrite_notes_ref);
Packit Service 20376f
		GITERR_CHECK_ALLOC(rebase->options.rewrite_notes_ref);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((rebase->options.checkout_options.checkout_strategy & (GIT_CHECKOUT_SAFE | GIT_CHECKOUT_FORCE)) == 0)
Packit Service 20376f
		rebase->options.checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
Packit Service 20376f
Packit Service 20376f
	*out = rebase;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_check_versions(const git_rebase_options *given_opts)
Packit Service 20376f
{
Packit Service 20376f
	GITERR_CHECK_VERSION(given_opts, GIT_REBASE_OPTIONS_VERSION, "git_rebase_options");
Packit Service 20376f
Packit Service 20376f
	if (given_opts)
Packit Service 20376f
		GITERR_CHECK_VERSION(&given_opts->checkout_options, GIT_CHECKOUT_OPTIONS_VERSION, "git_checkout_options");
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_rebase_open(
Packit Service 20376f
	git_rebase **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_rebase_options *given_opts)
Packit Service 20376f
{
Packit Service 20376f
	git_rebase *rebase;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT, orig_head_name = GIT_BUF_INIT,
Packit Service 20376f
		orig_head_id = GIT_BUF_INIT, onto_id = GIT_BUF_INIT;
Packit Service 20376f
	int state_path_len, error;
Packit Service 20376f
Packit Service 20376f
	assert(repo);
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_check_versions(given_opts)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (rebase_alloc(&rebase, given_opts) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	rebase->repo = repo;
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_state_type(&rebase->type, &rebase->state_path, repo)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (rebase->type == GIT_REBASE_TYPE_NONE) {
Packit Service 20376f
		giterr_set(GITERR_REBASE, "there is no rebase in progress");
Packit Service 20376f
		error = GIT_ENOTFOUND;
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_puts(&path, rebase->state_path)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	state_path_len = git_buf_len(&path);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&path, path.ptr, HEAD_NAME_FILE)) < 0 ||
Packit Service 20376f
		(error = git_futils_readbuffer(&orig_head_name, path.ptr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_buf_rtrim(&orig_head_name);
Packit Service 20376f
Packit Service 20376f
	if (strcmp(ORIG_DETACHED_HEAD, orig_head_name.ptr) == 0)
Packit Service 20376f
		rebase->head_detached = 1;
Packit Service 20376f
Packit Service 20376f
	git_buf_truncate(&path, state_path_len);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&path, path.ptr, ORIG_HEAD_FILE)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (!git_path_isfile(path.ptr)) {
Packit Service 20376f
		/* Previous versions of git.git used 'head' here; support that. */
Packit Service 20376f
		git_buf_truncate(&path, state_path_len);
Packit Service 20376f
Packit Service 20376f
		if ((error = git_buf_joinpath(&path, path.ptr, HEAD_FILE)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_futils_readbuffer(&orig_head_id, path.ptr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_buf_rtrim(&orig_head_id);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_oid_fromstr(&rebase->orig_head_id, orig_head_id.ptr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_buf_truncate(&path, state_path_len);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&path, path.ptr, ONTO_FILE)) < 0 ||
Packit Service 20376f
		(error = git_futils_readbuffer(&onto_id, path.ptr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_buf_rtrim(&onto_id);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_oid_fromstr(&rebase->onto_id, onto_id.ptr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (!rebase->head_detached)
Packit Service 20376f
		rebase->orig_head_name = git_buf_detach(&orig_head_name);
Packit Service 20376f
Packit Service 20376f
	switch (rebase->type) {
Packit Service 20376f
	case GIT_REBASE_TYPE_INTERACTIVE:
Packit Service 20376f
		giterr_set(GITERR_REBASE, "interactive rebase is not supported");
Packit Service 20376f
		error = -1;
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_REBASE_TYPE_MERGE:
Packit Service 20376f
		error = rebase_open_merge(rebase);
Packit Service 20376f
		break;
Packit Service 20376f
	case GIT_REBASE_TYPE_APPLY:
Packit Service 20376f
		giterr_set(GITERR_REBASE, "patch application rebase is not supported");
Packit Service 20376f
		error = -1;
Packit Service 20376f
		break;
Packit Service 20376f
	default:
Packit Service 20376f
		abort();
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	if (error == 0)
Packit Service 20376f
		*out = rebase;
Packit Service 20376f
	else
Packit Service 20376f
		git_rebase_free(rebase);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&orig_head_name);
Packit Service 20376f
	git_buf_free(&orig_head_id);
Packit Service 20376f
	git_buf_free(&onto_id);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_cleanup(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	if (!rebase || rebase->inmemory)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	return git_path_isdir(rebase->state_path) ?
Packit Service 20376f
		git_futils_rmdir_r(rebase->state_path, NULL, GIT_RMDIR_REMOVE_FILES) :
Packit Service 20376f
		0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_setupfile(git_rebase *rebase, const char *filename, int flags, const char *fmt, ...)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT,
Packit Service 20376f
		contents = GIT_BUF_INIT;
Packit Service 20376f
	va_list ap;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	va_start(ap, fmt);
Packit Service 20376f
	git_buf_vprintf(&contents, fmt, ap);
Packit Service 20376f
	va_end(ap);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&path, rebase->state_path, filename)) == 0)
Packit Service 20376f
		error = git_futils_writebuffer(&contents, path.ptr, flags, REBASE_FILE_MODE);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&contents);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static const char *rebase_onto_name(const git_annotated_commit *onto)
Packit Service 20376f
{
Packit Service 20376f
	if (onto->ref_name && git__strncmp(onto->ref_name, "refs/heads/", 11) == 0)
Packit Service 20376f
		return onto->ref_name + 11;
Packit Service 20376f
	else if (onto->ref_name)
Packit Service 20376f
		return onto->ref_name;
Packit Service 20376f
	else
Packit Service 20376f
		return onto->id_str;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_setupfiles_merge(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	git_buf commit_filename = GIT_BUF_INIT;
Packit Service 20376f
	char id_str[GIT_OID_HEXSZ];
Packit Service 20376f
	git_rebase_operation *operation;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_setupfile(rebase, END_FILE, 0, "%" PRIuZ "\n", git_array_size(rebase->operations))) < 0 ||
Packit Service 20376f
		(error = rebase_setupfile(rebase, ONTO_NAME_FILE, 0, "%s\n", rebase->onto_name)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < git_array_size(rebase->operations); i++) {
Packit Service 20376f
		operation = git_array_get(rebase->operations, i);
Packit Service 20376f
Packit Service 20376f
		git_buf_clear(&commit_filename);
Packit Service 20376f
		git_buf_printf(&commit_filename, CMT_FILE_FMT, i+1);
Packit Service 20376f
Packit Service 20376f
		git_oid_fmt(id_str, &operation->id);
Packit Service 20376f
Packit Service 20376f
		if ((error = rebase_setupfile(rebase, commit_filename.ptr, 0,
Packit Service 20376f
				"%.*s\n", GIT_OID_HEXSZ, id_str)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_buf_free(&commit_filename);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_setupfiles(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	char onto[GIT_OID_HEXSZ], orig_head[GIT_OID_HEXSZ];
Packit Service 20376f
	const char *orig_head_name;
Packit Service 20376f
Packit Service 20376f
	git_oid_fmt(onto, &rebase->onto_id);
Packit Service 20376f
	git_oid_fmt(orig_head, &rebase->orig_head_id);
Packit Service 20376f
Packit Service 20376f
	if (p_mkdir(rebase->state_path, REBASE_DIR_MODE) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to create rebase directory '%s'", rebase->state_path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	orig_head_name = rebase->head_detached ? ORIG_DETACHED_HEAD :
Packit Service 20376f
		rebase->orig_head_name;
Packit Service 20376f
Packit Service 20376f
	if (git_repository__set_orig_head(rebase->repo, &rebase->orig_head_id) < 0 ||
Packit Service 20376f
		rebase_setupfile(rebase, HEAD_NAME_FILE, 0, "%s\n", orig_head_name) < 0 ||
Packit Service 20376f
		rebase_setupfile(rebase, ONTO_FILE, 0, "%.*s\n", GIT_OID_HEXSZ, onto) < 0 ||
Packit Service 20376f
		rebase_setupfile(rebase, ORIG_HEAD_FILE, 0, "%.*s\n", GIT_OID_HEXSZ, orig_head) < 0 ||
Packit Service 20376f
		rebase_setupfile(rebase, QUIET_FILE, 0, rebase->quiet ? "t\n" : "\n") < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return rebase_setupfiles_merge(rebase);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_rebase_init_options(git_rebase_options *opts, unsigned int version)
Packit Service 20376f
{
Packit Service 20376f
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Packit Service 20376f
		opts, version, git_rebase_options, GIT_REBASE_OPTIONS_INIT);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_ensure_not_in_progress(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_rebase_type_t type;
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_state_type(&type, NULL, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (type != GIT_REBASE_TYPE_NONE) {
Packit Service 20376f
		giterr_set(GITERR_REBASE, "there is an existing rebase in progress");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_ensure_not_dirty(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	bool check_index,
Packit Service 20376f
	bool check_workdir,
Packit Service 20376f
	int fail_with)
Packit Service 20376f
{
Packit Service 20376f
	git_tree *head = NULL;
Packit Service 20376f
	git_index *index = NULL;
Packit Service 20376f
	git_diff *diff = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (check_index) {
Packit Service 20376f
		if ((error = git_repository_head_tree(&head, repo)) < 0 ||
Packit Service 20376f
			(error = git_repository_index(&index, repo)) < 0 ||
Packit Service 20376f
			(error = git_diff_tree_to_index(&diff, repo, head, index, NULL)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		if (git_diff_num_deltas(diff) > 0) {
Packit Service 20376f
			giterr_set(GITERR_REBASE, "uncommitted changes exist in index");
Packit Service 20376f
			error = fail_with;
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_diff_free(diff);
Packit Service 20376f
		diff = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (check_workdir) {
Packit Service 20376f
		git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
Packit Service 20376f
		diff_opts.ignore_submodules = GIT_SUBMODULE_IGNORE_UNTRACKED;
Packit Service 20376f
		if ((error = git_diff_index_to_workdir(&diff, repo, index, &diff_opts)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		if (git_diff_num_deltas(diff) > 0) {
Packit Service 20376f
			giterr_set(GITERR_REBASE, "unstaged changes exist in workdir");
Packit Service 20376f
			error = fail_with;
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_diff_free(diff);
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
	git_tree_free(head);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_init_operations(
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_annotated_commit *branch,
Packit Service 20376f
	const git_annotated_commit *upstream,
Packit Service 20376f
	const git_annotated_commit *onto)
Packit Service 20376f
{
Packit Service 20376f
	git_revwalk *revwalk = NULL;
Packit Service 20376f
	git_commit *commit;
Packit Service 20376f
	git_oid id;
Packit Service 20376f
	bool merge;
Packit Service 20376f
	git_rebase_operation *operation;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (!upstream)
Packit Service 20376f
		upstream = onto;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_revwalk_new(&revwalk, rebase->repo)) < 0 ||
Packit Service 20376f
		(error = git_revwalk_push(revwalk, git_annotated_commit_id(branch))) < 0 ||
Packit Service 20376f
		(error = git_revwalk_hide(revwalk, git_annotated_commit_id(upstream))) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_revwalk_sorting(revwalk, GIT_SORT_REVERSE);
Packit Service 20376f
Packit Service 20376f
	while ((error = git_revwalk_next(&id, revwalk)) == 0) {
Packit Service 20376f
		if ((error = git_commit_lookup(&commit, repo, &id)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		merge = (git_commit_parentcount(commit) > 1);
Packit Service 20376f
		git_commit_free(commit);
Packit Service 20376f
Packit Service 20376f
		if (merge)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL);
Packit Service 20376f
		GITERR_CHECK_ALLOC(operation);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = 0;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_revwalk_free(revwalk);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_init_merge(
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_annotated_commit *branch,
Packit Service 20376f
	const git_annotated_commit *upstream,
Packit Service 20376f
	const git_annotated_commit *onto)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *head_ref = NULL;
Packit Service 20376f
	git_commit *onto_commit = NULL;
Packit Service 20376f
	git_buf reflog = GIT_BUF_INIT;
Packit Service 20376f
	git_buf state_path = GIT_BUF_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(upstream);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&state_path, repo->gitdir, REBASE_MERGE_DIR)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	rebase->state_path = git_buf_detach(&state_path);
Packit Service 20376f
	GITERR_CHECK_ALLOC(rebase->state_path);
Packit Service 20376f
Packit Service 20376f
	if (branch->ref_name && strcmp(branch->ref_name, "HEAD")) {
Packit Service 20376f
		rebase->orig_head_name = git__strdup(branch->ref_name);
Packit Service 20376f
		GITERR_CHECK_ALLOC(rebase->orig_head_name);
Packit Service 20376f
	} else {
Packit Service 20376f
		rebase->head_detached = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	rebase->onto_name = git__strdup(rebase_onto_name(onto));
Packit Service 20376f
	GITERR_CHECK_ALLOC(rebase->onto_name);
Packit Service 20376f
Packit Service 20376f
	rebase->quiet = rebase->options.quiet;
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&rebase->orig_head_id, git_annotated_commit_id(branch));
Packit Service 20376f
	git_oid_cpy(&rebase->onto_id, git_annotated_commit_id(onto));
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_setupfiles(rebase)) < 0 ||
Packit Service 20376f
		(error = git_buf_printf(&reflog,
Packit Service 20376f
			"rebase: checkout %s", rebase_onto_name(onto))) < 0 ||
Packit Service 20376f
		(error = git_commit_lookup(
Packit Service 20376f
			&onto_commit, repo, git_annotated_commit_id(onto))) < 0 ||
Packit Service 20376f
		(error = git_checkout_tree(repo,
Packit Service 20376f
			(git_object *)onto_commit, &rebase->options.checkout_options)) < 0 ||
Packit Service 20376f
		(error = git_reference_create(&head_ref, repo, GIT_HEAD_FILE,
Packit Service 20376f
			git_annotated_commit_id(onto), 1, reflog.ptr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_reference_free(head_ref);
Packit Service 20376f
	git_commit_free(onto_commit);
Packit Service 20376f
	git_buf_free(&reflog);
Packit Service 20376f
	git_buf_free(&state_path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_init_inmemory(
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_annotated_commit *branch,
Packit Service 20376f
	const git_annotated_commit *upstream,
Packit Service 20376f
	const git_annotated_commit *onto)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(branch);
Packit Service 20376f
	GIT_UNUSED(upstream);
Packit Service 20376f
Packit Service 20376f
	return git_commit_lookup(
Packit Service 20376f
		&rebase->last_commit, repo, git_annotated_commit_id(onto));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_rebase_init(
Packit Service 20376f
	git_rebase **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_annotated_commit *branch,
Packit Service 20376f
	const git_annotated_commit *upstream,
Packit Service 20376f
	const git_annotated_commit *onto,
Packit Service 20376f
	const git_rebase_options *given_opts)
Packit Service 20376f
{
Packit Service 20376f
	git_rebase *rebase = NULL;
Packit Service 20376f
	git_annotated_commit *head_branch = NULL;
Packit Service 20376f
	git_reference *head_ref = NULL;
Packit Service 20376f
	bool inmemory = (given_opts && given_opts->inmemory);
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(repo && (upstream || onto));
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if (!onto)
Packit Service 20376f
		onto = upstream;
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_check_versions(given_opts)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (!inmemory) {
Packit Service 20376f
		if ((error = git_repository__ensure_not_bare(repo, "rebase")) < 0 ||
Packit Service 20376f
			(error = rebase_ensure_not_in_progress(repo)) < 0 ||
Packit Service 20376f
			(error = rebase_ensure_not_dirty(repo, true, true, GIT_ERROR)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!branch) {
Packit Service 20376f
		if ((error = git_repository_head(&head_ref, repo)) < 0 ||
Packit Service 20376f
			(error = git_annotated_commit_from_ref(&head_branch, repo, head_ref)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		branch = head_branch;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (rebase_alloc(&rebase, given_opts) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	rebase->repo = repo;
Packit Service 20376f
	rebase->inmemory = inmemory;
Packit Service 20376f
	rebase->type = GIT_REBASE_TYPE_MERGE;
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_init_operations(rebase, repo, branch, upstream, onto)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (inmemory)
Packit Service 20376f
		error = rebase_init_inmemory(rebase, repo, branch, upstream, onto);
Packit Service 20376f
	else
Packit Service 20376f
		error = rebase_init_merge(rebase, repo, branch ,upstream, onto);
Packit Service 20376f
Packit Service 20376f
	if (error == 0)
Packit Service 20376f
		*out = rebase;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_reference_free(head_ref);
Packit Service 20376f
	git_annotated_commit_free(head_branch);
Packit Service 20376f
Packit Service 20376f
	if (error < 0) {
Packit Service 20376f
		rebase_cleanup(rebase);
Packit Service 20376f
		git_rebase_free(rebase);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void normalize_checkout_options_for_apply(
Packit Service 20376f
	git_checkout_options *checkout_opts,
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	git_commit *current_commit)
Packit Service 20376f
{
Packit Service 20376f
	memcpy(checkout_opts, &rebase->options.checkout_options, sizeof(git_checkout_options));
Packit Service 20376f
Packit Service 20376f
	if (!checkout_opts->ancestor_label)
Packit Service 20376f
		checkout_opts->ancestor_label = "ancestor";
Packit Service 20376f
Packit Service 20376f
	if (rebase->type == GIT_REBASE_TYPE_MERGE) {
Packit Service 20376f
		if (!checkout_opts->our_label)
Packit Service 20376f
			checkout_opts->our_label = rebase->onto_name;
Packit Service 20376f
Packit Service 20376f
		if (!checkout_opts->their_label)
Packit Service 20376f
			checkout_opts->their_label = git_commit_summary(current_commit);
Packit Service 20376f
	} else {
Packit Service 20376f
		abort();
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) rebase_movenext(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	size_t next = rebase->started ? rebase->current + 1 : 0;
Packit Service 20376f
Packit Service 20376f
	if (next == git_array_size(rebase->operations))
Packit Service 20376f
		return GIT_ITEROVER;
Packit Service 20376f
Packit Service 20376f
	rebase->started = 1;
Packit Service 20376f
	rebase->current = next;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_next_merge(
Packit Service 20376f
	git_rebase_operation **out,
Packit Service 20376f
	git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	git_commit *current_commit = NULL, *parent_commit = NULL;
Packit Service 20376f
	git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL;
Packit Service 20376f
	git_index *index = NULL;
Packit Service 20376f
	git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
Packit Service 20376f
	git_rebase_operation *operation;
Packit Service 20376f
	git_checkout_options checkout_opts;
Packit Service 20376f
	char current_idstr[GIT_OID_HEXSZ];
Packit Service 20376f
	unsigned int parent_count;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	operation = git_array_get(rebase->operations, rebase->current);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_commit_lookup(&current_commit, rebase->repo, &operation->id)) < 0 ||
Packit Service 20376f
		(error = git_commit_tree(&current_tree, current_commit)) < 0 ||
Packit Service 20376f
		(error = git_repository_head_tree(&head_tree, rebase->repo)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if ((parent_count = git_commit_parentcount(current_commit)) > 1) {
Packit Service 20376f
		giterr_set(GITERR_REBASE, "cannot rebase a merge commit");
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto done;
Packit Service 20376f
	} else if (parent_count) {
Packit Service 20376f
		if ((error = git_commit_parent(&parent_commit, current_commit, 0)) < 0 ||
Packit Service 20376f
			(error = git_commit_tree(&parent_tree, parent_commit)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_oid_fmt(current_idstr, &operation->id);
Packit Service 20376f
Packit Service 20376f
	normalize_checkout_options_for_apply(&checkout_opts, rebase, current_commit);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_indexwriter_init_for_operation(&indexwriter, rebase->repo, &checkout_opts.checkout_strategy)) < 0 ||
Packit Service 20376f
		(error = rebase_setupfile(rebase, MSGNUM_FILE, 0, "%" PRIuZ "\n", rebase->current+1)) < 0 ||
Packit Service 20376f
		(error = rebase_setupfile(rebase, CURRENT_FILE, 0, "%.*s\n", GIT_OID_HEXSZ, current_idstr)) < 0 ||
Packit Service 20376f
		(error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, &rebase->options.merge_options)) < 0 ||
Packit Service 20376f
		(error = git_merge__check_result(rebase->repo, index)) < 0 ||
Packit Service 20376f
		(error = git_checkout_index(rebase->repo, index, &checkout_opts)) < 0 ||
Packit Service 20376f
		(error = git_indexwriter_commit(&indexwriter)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	*out = operation;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_indexwriter_cleanup(&indexwriter);
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
	git_tree_free(current_tree);
Packit Service 20376f
	git_tree_free(head_tree);
Packit Service 20376f
	git_tree_free(parent_tree);
Packit Service 20376f
	git_commit_free(parent_commit);
Packit Service 20376f
	git_commit_free(current_commit);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_next_inmemory(
Packit Service 20376f
	git_rebase_operation **out,
Packit Service 20376f
	git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	git_commit *current_commit = NULL, *parent_commit = NULL;
Packit Service 20376f
	git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL;
Packit Service 20376f
	git_rebase_operation *operation;
Packit Service 20376f
	git_index *index = NULL;
Packit Service 20376f
	unsigned int parent_count;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	operation = git_array_get(rebase->operations, rebase->current);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_commit_lookup(&current_commit, rebase->repo, &operation->id)) < 0 ||
Packit Service 20376f
		(error = git_commit_tree(&current_tree, current_commit)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if ((parent_count = git_commit_parentcount(current_commit)) > 1) {
Packit Service 20376f
		giterr_set(GITERR_REBASE, "cannot rebase a merge commit");
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto done;
Packit Service 20376f
	} else if (parent_count) {
Packit Service 20376f
		if ((error = git_commit_parent(&parent_commit, current_commit, 0)) < 0 ||
Packit Service 20376f
			(error = git_commit_tree(&parent_tree, parent_commit)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_commit_tree(&head_tree, rebase->last_commit)) < 0 ||
Packit Service 20376f
		(error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, &rebase->options.merge_options)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (!rebase->index) {
Packit Service 20376f
		rebase->index = index;
Packit Service 20376f
		index = NULL;
Packit Service 20376f
	} else {
Packit Service 20376f
		if ((error = git_index_read_index(rebase->index, index)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = operation;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_commit_free(current_commit);
Packit Service 20376f
	git_commit_free(parent_commit);
Packit Service 20376f
	git_tree_free(current_tree);
Packit Service 20376f
	git_tree_free(head_tree);
Packit Service 20376f
	git_tree_free(parent_tree);
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_rebase_next(
Packit Service 20376f
	git_rebase_operation **out,
Packit Service 20376f
	git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(out && rebase);
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_movenext(rebase)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (rebase->inmemory)
Packit Service 20376f
		error = rebase_next_inmemory(out, rebase);
Packit Service 20376f
	else if (rebase->type == GIT_REBASE_TYPE_MERGE)
Packit Service 20376f
		error = rebase_next_merge(out, rebase);
Packit Service 20376f
	else
Packit Service 20376f
		abort();
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_rebase_inmemory_index(
Packit Service 20376f
	git_index **out,
Packit Service 20376f
	git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	assert(out && rebase && rebase->index);
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(rebase->index);
Packit Service 20376f
	*out = rebase->index;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_commit__create(
Packit Service 20376f
	git_commit **out,
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	git_index *index,
Packit Service 20376f
	git_commit *parent_commit,
Packit Service 20376f
	const git_signature *author,
Packit Service 20376f
	const git_signature *committer,
Packit Service 20376f
	const char *message_encoding,
Packit Service 20376f
	const char *message)
Packit Service 20376f
{
Packit Service 20376f
	git_rebase_operation *operation;
Packit Service 20376f
	git_commit *current_commit = NULL, *commit = NULL;
Packit Service 20376f
	git_tree *parent_tree = NULL, *tree = NULL;
Packit Service 20376f
	git_oid tree_id, commit_id;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	operation = git_array_get(rebase->operations, rebase->current);
Packit Service 20376f
Packit Service 20376f
	if (git_index_has_conflicts(index)) {
Packit Service 20376f
		giterr_set(GITERR_REBASE, "conflicts have not been resolved");
Packit Service 20376f
		error = GIT_EUNMERGED;
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_commit_lookup(&current_commit, rebase->repo, &operation->id)) < 0 ||
Packit Service 20376f
		(error = git_commit_tree(&parent_tree, parent_commit)) < 0 ||
Packit Service 20376f
		(error = git_index_write_tree_to(&tree_id, index, rebase->repo)) < 0 ||
Packit Service 20376f
		(error = git_tree_lookup(&tree, rebase->repo, &tree_id)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (git_oid_equal(&tree_id, git_tree_id(parent_tree))) {
Packit Service 20376f
		giterr_set(GITERR_REBASE, "this patch has already been applied");
Packit Service 20376f
		error = GIT_EAPPLIED;
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!author)
Packit Service 20376f
		author = git_commit_author(current_commit);
Packit Service 20376f
Packit Service 20376f
	if (!message) {
Packit Service 20376f
		message_encoding = git_commit_message_encoding(current_commit);
Packit Service 20376f
		message = git_commit_message(current_commit);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_commit_create(&commit_id, rebase->repo, NULL, author,
Packit Service 20376f
		committer, message_encoding, message, tree, 1,
Packit Service 20376f
		(const git_commit **)&parent_commit)) < 0 ||
Packit Service 20376f
		(error = git_commit_lookup(&commit, rebase->repo, &commit_id)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	*out = commit;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		git_commit_free(commit);
Packit Service 20376f
Packit Service 20376f
	git_commit_free(current_commit);
Packit Service 20376f
	git_tree_free(parent_tree);
Packit Service 20376f
	git_tree_free(tree);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_commit_merge(
Packit Service 20376f
	git_oid *commit_id,
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	const git_signature *author,
Packit Service 20376f
	const git_signature *committer,
Packit Service 20376f
	const char *message_encoding,
Packit Service 20376f
	const char *message)
Packit Service 20376f
{
Packit Service 20376f
	git_rebase_operation *operation;
Packit Service 20376f
	git_reference *head = NULL;
Packit Service 20376f
	git_commit *head_commit = NULL, *commit = NULL;
Packit Service 20376f
	git_index *index = NULL;
Packit Service 20376f
	char old_idstr[GIT_OID_HEXSZ], new_idstr[GIT_OID_HEXSZ];
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	operation = git_array_get(rebase->operations, rebase->current);
Packit Service 20376f
	assert(operation);
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_ensure_not_dirty(rebase->repo, false, true, GIT_EUNMERGED)) < 0 ||
Packit Service 20376f
		(error = git_repository_head(&head, rebase->repo)) < 0 ||
Packit Service 20376f
		(error = git_reference_peel((git_object **)&head_commit, head, GIT_OBJ_COMMIT)) < 0 ||
Packit Service 20376f
		(error = git_repository_index(&index, rebase->repo)) < 0 ||
Packit Service 20376f
		(error = rebase_commit__create(&commit, rebase, index, head_commit,
Packit Service 20376f
			author, committer, message_encoding, message)) < 0 ||
Packit Service 20376f
		(error = git_reference__update_for_commit(
Packit Service 20376f
			rebase->repo, NULL, "HEAD", git_commit_id(commit), "rebase")) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_oid_fmt(old_idstr, &operation->id);
Packit Service 20376f
	git_oid_fmt(new_idstr, git_commit_id(commit));
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_setupfile(rebase, REWRITTEN_FILE, O_CREAT|O_WRONLY|O_APPEND,
Packit Service 20376f
		"%.*s %.*s\n", GIT_OID_HEXSZ, old_idstr, GIT_OID_HEXSZ, new_idstr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(commit_id, git_commit_id(commit));
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
	git_reference_free(head);
Packit Service 20376f
	git_commit_free(head_commit);
Packit Service 20376f
	git_commit_free(commit);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_commit_inmemory(
Packit Service 20376f
	git_oid *commit_id,
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	const git_signature *author,
Packit Service 20376f
	const git_signature *committer,
Packit Service 20376f
	const char *message_encoding,
Packit Service 20376f
	const char *message)
Packit Service 20376f
{
Packit Service 20376f
	git_commit *commit = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(rebase->index);
Packit Service 20376f
	assert(rebase->last_commit);
Packit Service 20376f
	assert(rebase->current < rebase->operations.size);
Packit Service 20376f
Packit Service 20376f
	if ((error = rebase_commit__create(&commit, rebase, rebase->index,
Packit Service 20376f
		rebase->last_commit, author, committer, message_encoding, message)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_commit_free(rebase->last_commit);
Packit Service 20376f
	rebase->last_commit = commit;
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(commit_id, git_commit_id(commit));
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		git_commit_free(commit);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_rebase_commit(
Packit Service 20376f
	git_oid *id,
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	const git_signature *author,
Packit Service 20376f
	const git_signature *committer,
Packit Service 20376f
	const char *message_encoding,
Packit Service 20376f
	const char *message)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(rebase && committer);
Packit Service 20376f
Packit Service 20376f
	if (rebase->inmemory)
Packit Service 20376f
		error = rebase_commit_inmemory(
Packit Service 20376f
			id, rebase, author, committer, message_encoding, message);
Packit Service 20376f
	else if (rebase->type == GIT_REBASE_TYPE_MERGE)
Packit Service 20376f
		error = rebase_commit_merge(
Packit Service 20376f
			id, rebase, author, committer, message_encoding, message);
Packit Service 20376f
	else
Packit Service 20376f
		abort();
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_rebase_abort(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *orig_head_ref = NULL;
Packit Service 20376f
	git_commit *orig_head_commit = NULL;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(rebase);
Packit Service 20376f
Packit Service 20376f
	if (rebase->inmemory)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	error = rebase->head_detached ?
Packit Service 20376f
		git_reference_create(&orig_head_ref, rebase->repo, GIT_HEAD_FILE,
Packit Service 20376f
			 &rebase->orig_head_id, 1, "rebase: aborting") :
Packit Service 20376f
		git_reference_symbolic_create(
Packit Service 20376f
			&orig_head_ref, rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1,
Packit Service 20376f
			"rebase: aborting");
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_commit_lookup(
Packit Service 20376f
			&orig_head_commit, rebase->repo, &rebase->orig_head_id)) < 0 ||
Packit Service 20376f
		(error = git_reset(rebase->repo, (git_object *)orig_head_commit,
Packit Service 20376f
			GIT_RESET_HARD, &rebase->options.checkout_options)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	error = rebase_cleanup(rebase);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_commit_free(orig_head_commit);
Packit Service 20376f
	git_reference_free(orig_head_ref);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int notes_ref_lookup(git_buf *out, git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	git_config *config = NULL;
Packit Service 20376f
	int do_rewrite, error;
Packit Service 20376f
Packit Service 20376f
	if (rebase->options.rewrite_notes_ref) {
Packit Service 20376f
		git_buf_attach_notowned(out,
Packit Service 20376f
			rebase->options.rewrite_notes_ref,
Packit Service 20376f
			strlen(rebase->options.rewrite_notes_ref));
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_config(&config, rebase->repo)) < 0 ||
Packit Service 20376f
		(error = git_config_get_bool(&do_rewrite, config, "notes.rewrite.rebase")) < 0) {
Packit Service 20376f
Packit Service 20376f
		if (error != GIT_ENOTFOUND)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		do_rewrite = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = do_rewrite ?
Packit Service 20376f
		git_config_get_string_buf(out, config, "notes.rewriteref") :
Packit Service 20376f
		GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_config_free(config);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_copy_note(
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	const char *notes_ref,
Packit Service 20376f
	git_oid *from,
Packit Service 20376f
	git_oid *to,
Packit Service 20376f
	const git_signature *committer)
Packit Service 20376f
{
Packit Service 20376f
	git_note *note = NULL;
Packit Service 20376f
	git_oid note_id;
Packit Service 20376f
	git_signature *who = NULL;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_note_read(&note, rebase->repo, notes_ref, from)) < 0) {
Packit Service 20376f
		if (error == GIT_ENOTFOUND) {
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			error = 0;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!committer) {
Packit Service 20376f
		if((error = git_signature_default(&who, rebase->repo)) < 0) {
Packit Service 20376f
			if (error != GIT_ENOTFOUND ||
Packit Service 20376f
				(error = git_signature_now(&who, "unknown", "unknown")) < 0)
Packit Service 20376f
				goto done;
Packit Service 20376f
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		committer = who;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git_note_create(&note_id, rebase->repo, notes_ref,
Packit Service 20376f
		git_note_author(note), committer, to, git_note_message(note), 0);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_note_free(note);
Packit Service 20376f
	git_signature_free(who);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int rebase_copy_notes(
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	const git_signature *committer)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT, rewritten = GIT_BUF_INIT, notes_ref = GIT_BUF_INIT;
Packit Service 20376f
	char *pair_list, *fromstr, *tostr, *end;
Packit Service 20376f
	git_oid from, to;
Packit Service 20376f
	unsigned int linenum = 1;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = notes_ref_lookup(&notes_ref, rebase)) < 0) {
Packit Service 20376f
		if (error == GIT_ENOTFOUND) {
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			error = 0;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&path, rebase->state_path, REWRITTEN_FILE)) < 0 ||
Packit Service 20376f
		(error = git_futils_readbuffer(&rewritten, path.ptr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	pair_list = rewritten.ptr;
Packit Service 20376f
Packit Service 20376f
	while (*pair_list) {
Packit Service 20376f
		fromstr = pair_list;
Packit Service 20376f
Packit Service 20376f
		if ((end = strchr(fromstr, '\n')) == NULL)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		pair_list = end+1;
Packit Service 20376f
		*end = '\0';
Packit Service 20376f
Packit Service 20376f
		if ((end = strchr(fromstr, ' ')) == NULL)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		tostr = end+1;
Packit Service 20376f
		*end = '\0';
Packit Service 20376f
Packit Service 20376f
		if (strlen(fromstr) != GIT_OID_HEXSZ ||
Packit Service 20376f
			strlen(tostr) != GIT_OID_HEXSZ ||
Packit Service 20376f
			git_oid_fromstr(&from, fromstr) < 0 ||
Packit Service 20376f
			git_oid_fromstr(&to, tostr) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		if ((error = rebase_copy_note(rebase, notes_ref.ptr, &from, &to, committer)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		linenum++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	goto done;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	giterr_set(GITERR_REBASE, "invalid rewritten file at line %d", linenum);
Packit Service 20376f
	error = -1;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_buf_free(&rewritten);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&notes_ref);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int return_to_orig_head(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *terminal_ref = NULL, *branch_ref = NULL, *head_ref = NULL;
Packit Service 20376f
	git_commit *terminal_commit = NULL;
Packit Service 20376f
	git_buf branch_msg = GIT_BUF_INIT, head_msg = GIT_BUF_INIT;
Packit Service 20376f
	char onto[GIT_OID_HEXSZ];
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	git_oid_fmt(onto, &rebase->onto_id);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_printf(&branch_msg,
Packit Service 20376f
			"rebase finished: %s onto %.*s",
Packit Service 20376f
			rebase->orig_head_name, GIT_OID_HEXSZ, onto)) == 0 &&
Packit Service 20376f
		(error = git_buf_printf(&head_msg,
Packit Service 20376f
			"rebase finished: returning to %s",
Packit Service 20376f
			rebase->orig_head_name)) == 0 &&
Packit Service 20376f
		(error = git_repository_head(&terminal_ref, rebase->repo)) == 0 &&
Packit Service 20376f
		(error = git_reference_peel((git_object **)&terminal_commit,
Packit Service 20376f
			terminal_ref, GIT_OBJ_COMMIT)) == 0 &&
Packit Service 20376f
		(error = git_reference_create_matching(&branch_ref,
Packit Service 20376f
			rebase->repo, rebase->orig_head_name,
Packit Service 20376f
			git_commit_id(terminal_commit), 1,
Packit Service 20376f
			&rebase->orig_head_id, branch_msg.ptr)) == 0)
Packit Service 20376f
		error = git_reference_symbolic_create(&head_ref,
Packit Service 20376f
			rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1,
Packit Service 20376f
			head_msg.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&head_msg);
Packit Service 20376f
	git_buf_free(&branch_msg);
Packit Service 20376f
	git_commit_free(terminal_commit);
Packit Service 20376f
	git_reference_free(head_ref);
Packit Service 20376f
	git_reference_free(branch_ref);
Packit Service 20376f
	git_reference_free(terminal_ref);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_rebase_finish(
Packit Service 20376f
	git_rebase *rebase,
Packit Service 20376f
	const git_signature *signature)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(rebase);
Packit Service 20376f
Packit Service 20376f
	if (rebase->inmemory)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (!rebase->head_detached)
Packit Service 20376f
		error = return_to_orig_head(rebase);
Packit Service 20376f
Packit Service 20376f
	if (error == 0 && (error = rebase_copy_notes(rebase, signature)) == 0)
Packit Service 20376f
		error = rebase_cleanup(rebase);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_rebase_operation_entrycount(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	assert(rebase);
Packit Service 20376f
Packit Service 20376f
	return git_array_size(rebase->operations);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_rebase_operation_current(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	assert(rebase);
Packit Service 20376f
Packit Service 20376f
	return rebase->started ? rebase->current : GIT_REBASE_NO_OPERATION;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_rebase_operation *git_rebase_operation_byindex(git_rebase *rebase, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	assert(rebase);
Packit Service 20376f
Packit Service 20376f
	return git_array_get(rebase->operations, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_rebase_free(git_rebase *rebase)
Packit Service 20376f
{
Packit Service 20376f
	if (rebase == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git_index_free(rebase->index);
Packit Service 20376f
	git_commit_free(rebase->last_commit);
Packit Service 20376f
	git__free(rebase->onto_name);
Packit Service 20376f
	git__free(rebase->orig_head_name);
Packit Service 20376f
	git__free(rebase->state_path);
Packit Service 20376f
	git_array_clear(rebase->operations);
Packit Service 20376f
	git__free((char *)rebase->options.rewrite_notes_ref);
Packit Service 20376f
	git__free(rebase);
Packit Service 20376f
}