rebase.c 29.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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"
#include "buffer.h"
#include "repository.h"
#include "posix.h"
#include "filebuf.h"
#include "merge.h"
#include "array.h"
15
#include "config.h"
16
#include "annotated_commit.h"
17
#include "index.h"
18 19

#include <git2/types.h>
20
#include <git2/annotated_commit.h>
21 22 23 24
#include <git2/rebase.h>
#include <git2/commit.h>
#include <git2/reset.h>
#include <git2/revwalk.h>
25
#include <git2/notes.h>
26

27 28
#define REBASE_APPLY_DIR    "rebase-apply"
#define REBASE_MERGE_DIR    "rebase-merge"
29

30 31 32 33 34 35
#define HEAD_NAME_FILE      "head-name"
#define ORIG_HEAD_FILE      "orig-head"
#define HEAD_FILE           "head"
#define ONTO_FILE           "onto"
#define ONTO_NAME_FILE      "onto_name"
#define QUIET_FILE          "quiet"
36

37 38 39 40 41
#define MSGNUM_FILE         "msgnum"
#define END_FILE            "end"
#define CMT_FILE_FMT        "cmt.%" PRIuZ
#define CURRENT_FILE        "current"
#define REWRITTEN_FILE      "rewritten"
42

43
#define ORIG_DETACHED_HEAD  "detached HEAD"
44

45
#define NOTES_DEFAULT_REF   NULL
46

47 48
#define REBASE_DIR_MODE     0777
#define REBASE_FILE_MODE    0666
49 50 51 52 53

typedef enum {
	GIT_REBASE_TYPE_NONE = 0,
	GIT_REBASE_TYPE_APPLY = 1,
	GIT_REBASE_TYPE_MERGE = 2,
54
	GIT_REBASE_TYPE_INTERACTIVE = 3,
55 56
} git_rebase_type_t;

57 58 59
struct git_rebase {
	git_repository *repo;

60 61
	git_rebase_options options;

62 63 64
	git_rebase_type_t type;
	char *state_path;

65
	int head_detached : 1,
66 67
		quiet : 1,
		started : 1;
68 69 70

	char *orig_head_name;
	git_oid orig_head_id;
71

72
	git_oid onto_id;
73
	char *onto_name;
74

75
	git_array_t(git_rebase_operation) operations;
76
	size_t current;
77
};
78 79 80

#define GIT_REBASE_STATE_INIT {0}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static int rebase_state_type(
	git_rebase_type_t *type_out,
	char **path_out,
	git_repository *repo)
{
	git_buf path = GIT_BUF_INIT;
	git_rebase_type_t type = GIT_REBASE_TYPE_NONE;

	if (git_buf_joinpath(&path, repo->path_repository, REBASE_APPLY_DIR) < 0)
		return -1;

	if (git_path_isdir(git_buf_cstr(&path))) {
		type = GIT_REBASE_TYPE_APPLY;
		goto done;
	}

	git_buf_clear(&path);
	if (git_buf_joinpath(&path, repo->path_repository, REBASE_MERGE_DIR) < 0)
		return -1;

	if (git_path_isdir(git_buf_cstr(&path))) {
		type = GIT_REBASE_TYPE_MERGE;
		goto done;
	}

done:
	*type_out = type;

	if (type != GIT_REBASE_TYPE_NONE && path_out)
		*path_out = git_buf_detach(&path);

	git_buf_free(&path);

	return 0;
}

117 118 119 120
GIT_INLINE(int) rebase_readfile(
	git_buf *out,
	git_buf *state_path,
	const char *filename)
121
{
122 123
	size_t state_path_len = state_path->size;
	int error;
124

125 126 127 128
	git_buf_clear(out);

	if ((error = git_buf_joinpath(state_path, state_path->ptr, filename)) < 0 ||
		(error = git_futils_readbuffer(out, state_path->ptr)) < 0)
129 130
		goto done;

131
	git_buf_rtrim(out);
132

133 134 135 136
done:
	git_buf_truncate(state_path, state_path_len);
	return error;
}
137

138 139 140 141
GIT_INLINE(int) rebase_readint(
	size_t *out, git_buf *asc_out, git_buf *state_path, const char *filename)
{
	int32_t num;
142
	const char *eol;
143
	int error = 0;
144

145 146
	if ((error = rebase_readfile(asc_out, state_path, filename)) < 0)
		return error;
147

148 149 150 151
	if (git__strtol32(&num, asc_out->ptr, &eol, 10) < 0 || num < 0 || *eol) {
		giterr_set(GITERR_REBASE, "The file '%s' contains an invalid numeric value", filename);
		return -1;
	}
152

153
	*out = (size_t) num;
154

155 156
	return 0;
}
157

158 159 160 161
GIT_INLINE(int) rebase_readoid(
	git_oid *out, git_buf *str_out, git_buf *state_path, const char *filename)
{
	int error;
162

163 164
	if ((error = rebase_readfile(str_out, state_path, filename)) < 0)
		return error;
165

166 167 168 169
	if (str_out->size != GIT_OID_HEXSZ || git_oid_fromstr(out, str_out->ptr) < 0) {
		giterr_set(GITERR_REBASE, "The file '%s' contains an invalid object ID", filename);
		return -1;
	}
170

171 172
	return 0;
}
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
static git_rebase_operation *rebase_operation_alloc(
	git_rebase *rebase,
	git_rebase_operation_t type,
	git_oid *id,
	const char *exec)
{
	git_rebase_operation *operation;

	assert((type == GIT_REBASE_OPERATION_EXEC) == !id);
	assert((type == GIT_REBASE_OPERATION_EXEC) == !!exec);

	if ((operation = git_array_alloc(rebase->operations)) == NULL)
		return NULL;

	operation->type = type;
	git_oid_cpy((git_oid *)&operation->id, id);
	operation->exec = exec;

	return operation;
}

195 196 197
static int rebase_open_merge(git_rebase *rebase)
{
	git_buf state_path = GIT_BUF_INIT, buf = GIT_BUF_INIT, cmt = GIT_BUF_INIT;
198
	git_oid id;
199 200 201
	git_rebase_operation *operation;
	size_t i, msgnum = 0, end;
	int error;
202

203 204
	if ((error = git_buf_puts(&state_path, rebase->state_path)) < 0)
		goto done;
205

206 207 208 209
	/* Read 'msgnum' if it exists (otherwise, let msgnum = 0) */
	if ((error = rebase_readint(&msgnum, &buf, &state_path, MSGNUM_FILE)) < 0 &&
		error != GIT_ENOTFOUND)
		goto done;
210

211 212 213 214
	if (msgnum) {
		rebase->started = 1;
		rebase->current = msgnum - 1;
	}
215

216 217
	/* Read 'end' */
	if ((error = rebase_readint(&end, &buf, &state_path, END_FILE)) < 0)
218 219
		goto done;

220
	/* Read 'current' if it exists */
221
	if ((error = rebase_readoid(&id, &buf, &state_path, CURRENT_FILE)) < 0 &&
222 223 224 225 226 227
		error != GIT_ENOTFOUND)
		goto done;

	/* Read cmt.* */
	git_array_init_to_size(rebase->operations, end);
	GITERR_CHECK_ARRAY(rebase->operations);
228

229 230 231 232
	for (i = 0; i < end; i++) {
		git_buf_clear(&cmt);

		if ((error = git_buf_printf(&cmt, "cmt.%" PRIuZ, (i+1))) < 0 ||
233
			(error = rebase_readoid(&id, &buf, &state_path, cmt.ptr)) < 0)
234
			goto done;
235 236 237

		operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL);
		GITERR_CHECK_ALLOC(operation);
238
	}
239

240 241 242 243 244 245
	/* Read 'onto_name' */
	if ((error = rebase_readfile(&buf, &state_path, ONTO_NAME_FILE)) < 0)
		goto done;

	rebase->onto_name = git_buf_detach(&buf);

246
done:
247 248 249
	git_buf_free(&cmt);
	git_buf_free(&state_path);
	git_buf_free(&buf);
250 251 252 253

	return error;
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
static git_rebase *rebase_alloc(const git_rebase_options *rebase_opts)
{
	git_rebase *rebase = git__calloc(1, sizeof(git_rebase));

	if (!rebase)
		return NULL;

	if (rebase_opts)
		memcpy(&rebase->options, rebase_opts, sizeof(git_rebase_options));
	else
		git_rebase_init_options(&rebase->options, GIT_REBASE_OPTIONS_VERSION);

	if (rebase_opts && rebase_opts->rewrite_notes_ref) {
		if ((rebase->options.rewrite_notes_ref = git__strdup(rebase_opts->rewrite_notes_ref)) == NULL)
			return NULL;
	}

271 272
	if ((rebase->options.checkout_options.checkout_strategy & (GIT_CHECKOUT_SAFE | GIT_CHECKOUT_FORCE)) == 0)
		rebase->options.checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
273 274 275 276 277 278 279 280 281

	return rebase;
}

static int rebase_check_versions(const git_rebase_options *given_opts)
{
	GITERR_CHECK_VERSION(given_opts, GIT_REBASE_OPTIONS_VERSION, "git_rebase_options");

	if (given_opts)
282
		GITERR_CHECK_VERSION(&given_opts->checkout_options, GIT_CHECKOUT_OPTIONS_VERSION, "git_checkout_options");
283 284 285 286 287 288 289 290

	return 0;
}

int git_rebase_open(
	git_rebase **out,
	git_repository *repo,
	const git_rebase_options *given_opts)
291
{
292
	git_rebase *rebase;
293
	git_buf path = GIT_BUF_INIT, orig_head_name = GIT_BUF_INIT,
294
		orig_head_id = GIT_BUF_INIT, onto_id = GIT_BUF_INIT;
295 296
	int state_path_len, error;

297 298
	assert(repo);

299 300 301 302
	if ((error = rebase_check_versions(given_opts)) < 0)
		return error;

	rebase = rebase_alloc(given_opts);
303
	GITERR_CHECK_ALLOC(rebase);
304

305 306 307
	rebase->repo = repo;

	if ((error = rebase_state_type(&rebase->type, &rebase->state_path, repo)) < 0)
308 309
		goto done;

310
	if (rebase->type == GIT_REBASE_TYPE_NONE) {
311
		giterr_set(GITERR_REBASE, "There is no rebase in progress");
Jacques Germishuys committed
312 313
		error = GIT_ENOTFOUND;
		goto done;
314 315
	}

316
	if ((error = git_buf_puts(&path, rebase->state_path)) < 0)
317 318 319 320 321 322 323 324 325 326 327
		goto done;

	state_path_len = git_buf_len(&path);

	if ((error = git_buf_joinpath(&path, path.ptr, HEAD_NAME_FILE)) < 0 ||
		(error = git_futils_readbuffer(&orig_head_name, path.ptr)) < 0)
		goto done;

	git_buf_rtrim(&orig_head_name);

	if (strcmp(ORIG_DETACHED_HEAD, orig_head_name.ptr) == 0)
328
		rebase->head_detached = 1;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

	git_buf_truncate(&path, state_path_len);

	if ((error = git_buf_joinpath(&path, path.ptr, ORIG_HEAD_FILE)) < 0)
		goto done;

	if (!git_path_isfile(path.ptr)) {
		/* Previous versions of git.git used 'head' here; support that. */
		git_buf_truncate(&path, state_path_len);

		if ((error = git_buf_joinpath(&path, path.ptr, HEAD_FILE)) < 0)
			goto done;
	}

	if ((error = git_futils_readbuffer(&orig_head_id, path.ptr)) < 0)
		goto done;

	git_buf_rtrim(&orig_head_id);

348
	if ((error = git_oid_fromstr(&rebase->orig_head_id, orig_head_id.ptr)) < 0)
349 350
		goto done;

351 352 353 354 355 356 357 358
	git_buf_truncate(&path, state_path_len);

	if ((error = git_buf_joinpath(&path, path.ptr, ONTO_FILE)) < 0 ||
		(error = git_futils_readbuffer(&onto_id, path.ptr)) < 0)
		goto done;

	git_buf_rtrim(&onto_id);

359
	if ((error = git_oid_fromstr(&rebase->onto_id, onto_id.ptr)) < 0)
360 361
		goto done;

362 363
	if (!rebase->head_detached)
		rebase->orig_head_name = git_buf_detach(&orig_head_name);
364

365
	switch (rebase->type) {
366 367 368 369 370
	case GIT_REBASE_TYPE_INTERACTIVE:
		giterr_set(GITERR_REBASE, "Interactive rebase is not supported");
		error = -1;
		break;
	case GIT_REBASE_TYPE_MERGE:
371
		error = rebase_open_merge(rebase);
372 373 374 375 376 377 378 379 380
		break;
	case GIT_REBASE_TYPE_APPLY:
		giterr_set(GITERR_REBASE, "Patch application rebase is not supported");
		error = -1;
		break;
	default:
		abort();
	}

381
done:
382 383 384 385 386
	if (error == 0)
		*out = rebase;
	else
		git_rebase_free(rebase);

387 388 389 390 391 392 393
	git_buf_free(&path);
	git_buf_free(&orig_head_name);
	git_buf_free(&orig_head_id);
	git_buf_free(&onto_id);
	return error;
}

394
static int rebase_cleanup(git_rebase *rebase)
395
{
396 397
	return git_path_isdir(rebase->state_path) ?
		git_futils_rmdir_r(rebase->state_path, NULL, GIT_RMDIR_REMOVE_FILES) :
398 399 400
		0;
}

401
static int rebase_setupfile(git_rebase *rebase, const char *filename, int flags, const char *fmt, ...)
402 403 404 405 406 407 408 409 410 411
{
	git_buf path = GIT_BUF_INIT,
		contents = GIT_BUF_INIT;
	va_list ap;
	int error;

	va_start(ap, fmt);
	git_buf_vprintf(&contents, fmt, ap);
	va_end(ap);

412
	if ((error = git_buf_joinpath(&path, rebase->state_path, filename)) == 0)
413
		error = git_futils_writebuffer(&contents, path.ptr, flags, REBASE_FILE_MODE);
414 415 416 417 418 419 420

	git_buf_free(&path);
	git_buf_free(&contents);

	return error;
}

421
static const char *rebase_onto_name(const git_annotated_commit *onto)
422 423 424 425 426 427
{
	if (onto->ref_name && git__strncmp(onto->ref_name, "refs/heads/", 11) == 0)
		return onto->ref_name + 11;
	else if (onto->ref_name)
		return onto->ref_name;
	else
428
		return onto->id_str;
429 430
}

431
static int rebase_setupfiles_merge(git_rebase *rebase)
432 433 434
{
	git_buf commit_filename = GIT_BUF_INIT;
	char id_str[GIT_OID_HEXSZ];
435 436 437
	git_rebase_operation *operation;
	size_t i;
	int error = 0;
438

439
	if ((error = rebase_setupfile(rebase, END_FILE, -1, "%d\n", git_array_size(rebase->operations))) < 0 ||
440
		(error = rebase_setupfile(rebase, ONTO_NAME_FILE, -1, "%s\n", rebase->onto_name)) < 0)
441 442
		goto done;

443 444
	for (i = 0; i < git_array_size(rebase->operations); i++) {
		operation = git_array_get(rebase->operations, i);
445 446

		git_buf_clear(&commit_filename);
447 448 449
		git_buf_printf(&commit_filename, CMT_FILE_FMT, i+1);

		git_oid_fmt(id_str, &operation->id);
450

451
		if ((error = rebase_setupfile(rebase, commit_filename.ptr, -1,
452 453 454 455 456 457 458 459 460
				"%.*s\n", GIT_OID_HEXSZ, id_str)) < 0)
			goto done;
	}

done:
	git_buf_free(&commit_filename);
	return error;
}

461
static int rebase_setupfiles(git_rebase *rebase)
462
{
463
	char onto[GIT_OID_HEXSZ], orig_head[GIT_OID_HEXSZ];
464

465 466
	git_oid_fmt(onto, &rebase->onto_id);
	git_oid_fmt(orig_head, &rebase->orig_head_id);
467

468 469 470
	if (p_mkdir(rebase->state_path, REBASE_DIR_MODE) < 0) {
		giterr_set(GITERR_OS, "Failed to create rebase directory '%s'", rebase->state_path);
		return -1;
471 472
	}

473 474 475 476 477 478
	if (git_repository__set_orig_head(rebase->repo, &rebase->orig_head_id) < 0 ||
		rebase_setupfile(rebase, HEAD_NAME_FILE, -1, "%s\n", rebase->orig_head_name) < 0 ||
		rebase_setupfile(rebase, ONTO_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, onto) < 0 ||
		rebase_setupfile(rebase, ORIG_HEAD_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, orig_head) < 0 ||
		rebase_setupfile(rebase, QUIET_FILE, -1, rebase->quiet ? "t\n" : "\n") < 0)
		return -1;
479

480
	return rebase_setupfiles_merge(rebase);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
}

int git_rebase_init_options(git_rebase_options *opts, unsigned int version)
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_rebase_options, GIT_REBASE_OPTIONS_INIT);
	return 0;
}

static int rebase_ensure_not_in_progress(git_repository *repo)
{
	int error;
	git_rebase_type_t type;

	if ((error = rebase_state_type(&type, NULL, repo)) < 0)
		return error;

	if (type != GIT_REBASE_TYPE_NONE) {
		giterr_set(GITERR_REBASE, "There is an existing rebase in progress");
		return -1;
	}

	return 0;
}

506 507 508 509 510
static int rebase_ensure_not_dirty(
	git_repository *repo,
	bool check_index,
	bool check_workdir,
	int fail_with)
511 512 513 514 515 516
{
	git_tree *head = NULL;
	git_index *index = NULL;
	git_diff *diff = NULL;
	int error;

517 518 519 520 521
	if (check_index) {
		if ((error = git_repository_head_tree(&head, repo)) < 0 ||
			(error = git_repository_index(&index, repo)) < 0 ||
			(error = git_diff_tree_to_index(&diff, repo, head, index, NULL)) < 0)
			goto done;
522

523 524 525 526 527
		if (git_diff_num_deltas(diff) > 0) {
			giterr_set(GITERR_REBASE, "Uncommitted changes exist in index");
			error = fail_with;
			goto done;
		}
528

529 530 531
		git_diff_free(diff);
		diff = NULL;
	}
532

533 534 535
	if (check_workdir) {
		if ((error = git_diff_index_to_workdir(&diff, repo, index, NULL)) < 0)
			goto done;
536

537 538 539 540 541
		if (git_diff_num_deltas(diff) > 0) {
			giterr_set(GITERR_REBASE, "Unstaged changes exist in workdir");
			error = fail_with;
			goto done;
		}
542 543 544 545 546 547 548 549 550 551
	}

done:
	git_diff_free(diff);
	git_index_free(index);
	git_tree_free(head);

	return error;
}

552 553 554
static int rebase_init_operations(
	git_rebase *rebase,
	git_repository *repo,
555 556 557
	const git_annotated_commit *branch,
	const git_annotated_commit *upstream,
	const git_annotated_commit *onto)
558 559 560 561 562 563 564 565 566 567 568 569
{
	git_revwalk *revwalk = NULL;
	git_commit *commit;
	git_oid id;
	bool merge;
	git_rebase_operation *operation;
	int error;

	if (!upstream)
		upstream = onto;

	if ((error = git_revwalk_new(&revwalk, rebase->repo)) < 0 ||
570 571
		(error = git_revwalk_push(revwalk, git_annotated_commit_id(branch))) < 0 ||
		(error = git_revwalk_hide(revwalk, git_annotated_commit_id(upstream))) < 0)
572 573 574 575 576 577 578 579 580 581 582 583 584 585
		goto done;

	git_revwalk_sorting(revwalk, GIT_SORT_REVERSE | GIT_SORT_TIME);

	while ((error = git_revwalk_next(&id, revwalk)) == 0) {
		if ((error = git_commit_lookup(&commit, repo, &id)) < 0)
			goto done;

		merge = (git_commit_parentcount(commit) > 1);
		git_commit_free(commit);

		if (merge)
			continue;

586
		operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL);
587
		GITERR_CHECK_ALLOC(operation);
588 589 590 591 592 593 594 595 596 597 598 599
	}

	error = 0;

done:
	git_revwalk_free(revwalk);
	return error;
}

static int rebase_init_merge(
	git_rebase *rebase,
	git_repository *repo,
600 601 602
	const git_annotated_commit *branch,
	const git_annotated_commit *upstream,
	const git_annotated_commit *onto)
603 604 605 606
{
	if (rebase_init_operations(rebase, repo, branch, upstream, onto) < 0)
		return -1;

607 608
	rebase->onto_name = git__strdup(rebase_onto_name(onto));
	GITERR_CHECK_ALLOC(rebase->onto_name);
609 610 611 612 613 614 615

	return 0;
}

static int rebase_init(
	git_rebase *rebase,
	git_repository *repo,
616 617
	const git_annotated_commit *branch,
	const git_annotated_commit *upstream,
618
	const git_annotated_commit *onto)
619
{
620 621
	git_reference *head_ref = NULL;
	git_annotated_commit *head_branch = NULL;
622 623 624
	git_buf state_path = GIT_BUF_INIT;
	int error;

625
	if ((error = git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR)) < 0)
626 627 628 629 630 631 632 633 634
		goto done;

	if (!branch) {
		if ((error = git_repository_head(&head_ref, repo)) < 0 ||
			(error = git_annotated_commit_from_ref(&head_branch, repo, head_ref)) < 0)
			goto done;

		branch = head_branch;
	}
635 636 637 638 639

	rebase->repo = repo;
	rebase->type = GIT_REBASE_TYPE_MERGE;
	rebase->state_path = git_buf_detach(&state_path);
	rebase->orig_head_name = git__strdup(branch->ref_name ? branch->ref_name : ORIG_DETACHED_HEAD);
640
	rebase->quiet = rebase->options.quiet;
641

642 643
	git_oid_cpy(&rebase->orig_head_id, git_annotated_commit_id(branch));
	git_oid_cpy(&rebase->onto_id, git_annotated_commit_id(onto));
644 645 646 647 648 649 650 651

	if (!rebase->orig_head_name || !rebase->state_path)
		return -1;

	error = rebase_init_merge(rebase, repo, branch, upstream, onto);

	git_buf_free(&state_path);

652 653 654 655
done:
	git_reference_free(head_ref);
	git_annotated_commit_free(head_branch);

656 657 658 659 660
	return error;
}

int git_rebase_init(
	git_rebase **out,
661
	git_repository *repo,
662 663 664
	const git_annotated_commit *branch,
	const git_annotated_commit *upstream,
	const git_annotated_commit *onto,
665
	const git_rebase_options *given_opts)
666
{
667
	git_rebase *rebase = NULL;
668
	git_buf reflog = GIT_BUF_INIT;
669
	git_commit *onto_commit = NULL;
670
	git_reference *head_ref = NULL;
671 672
	int error;

673
	assert(repo && (upstream || onto));
674

675 676 677 678 679
	*out = NULL;

	if (!onto)
		onto = upstream;

680
	if ((error = rebase_check_versions(given_opts)) < 0 ||
681
		(error = git_repository__ensure_not_bare(repo, "rebase")) < 0 ||
682
		(error = rebase_ensure_not_in_progress(repo)) < 0 ||
683
		(error = rebase_ensure_not_dirty(repo, true, true, GIT_ERROR)) < 0 ||
684 685
		(error = git_commit_lookup(
			&onto_commit, repo, git_annotated_commit_id(onto))) < 0)
686
		return error;
687

688
	rebase = rebase_alloc(given_opts);
689

690
	if ((error = rebase_init(
691
			rebase, repo, branch, upstream, onto)) < 0 ||
692 693
		(error = rebase_setupfiles(rebase)) < 0 ||
		(error = git_buf_printf(&reflog,
694
			"rebase: checkout %s", rebase_onto_name(onto))) < 0 ||
695
		(error = git_checkout_tree(
696
			repo, (git_object *)onto_commit, &rebase->options.checkout_options)) < 0 ||
697 698
		(error = git_reference_create(&head_ref, repo, GIT_HEAD_FILE,
			git_annotated_commit_id(onto), 1, reflog.ptr)) < 0)
699 700
		goto done;

701
	*out = rebase;
702 703

done:
704
	git_reference_free(head_ref);
705 706 707 708 709
	if (error < 0) {
		rebase_cleanup(rebase);
		git_rebase_free(rebase);
	}

710
	git_commit_free(onto_commit);
711
	git_buf_free(&reflog);
712

713 714
	return error;
}
715

716
static void normalize_checkout_options_for_apply(
717
	git_checkout_options *checkout_opts,
718 719
	git_rebase *rebase,
	git_commit *current_commit)
720
{
721
	memcpy(checkout_opts, &rebase->options.checkout_options, sizeof(git_checkout_options));
722 723 724 725

	if (!checkout_opts->ancestor_label)
		checkout_opts->ancestor_label = "ancestor";

726
	if (rebase->type == GIT_REBASE_TYPE_MERGE) {
727
		if (!checkout_opts->our_label)
728
			checkout_opts->our_label = rebase->onto_name;
729

730 731
		if (!checkout_opts->their_label)
			checkout_opts->their_label = git_commit_summary(current_commit);
732 733 734 735 736
	} else {
		abort();
	}
}

737 738 739 740 741 742 743 744 745 746 747 748 749
GIT_INLINE(int) rebase_movenext(git_rebase *rebase)
{
	size_t next = rebase->started ? rebase->current + 1 : 0;

	if (next == git_array_size(rebase->operations))
		return GIT_ITEROVER;

	rebase->started = 1;
	rebase->current = next;

	return 0;
}

750
static int rebase_next_merge(
751
	git_rebase_operation **out,
752
	git_rebase *rebase)
753
{
754 755
	git_buf path = GIT_BUF_INIT;
	git_commit *current_commit = NULL, *parent_commit = NULL;
756 757
	git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL;
	git_index *index = NULL;
758
	git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
759
	git_rebase_operation *operation;
760
	git_checkout_options checkout_opts;
761
	char current_idstr[GIT_OID_HEXSZ];
762 763 764
	unsigned int parent_count;
	int error;

765
	*out = NULL;
766

767
	if ((error = rebase_movenext(rebase)) < 0)
768 769
		goto done;

770
	operation = git_array_get(rebase->operations, rebase->current);
771

772 773
	if ((error = git_commit_lookup(&current_commit, rebase->repo, &operation->id)) < 0 ||
		(error = git_commit_tree(&current_tree, current_commit)) < 0 ||
774
		(error = git_repository_head_tree(&head_tree, rebase->repo)) < 0)
775 776
		goto done;

777
	if ((parent_count = git_commit_parentcount(current_commit)) > 1) {
778 779 780 781
		giterr_set(GITERR_REBASE, "Cannot rebase a merge commit");
		error = -1;
		goto done;
	} else if (parent_count) {
782
		if ((error = git_commit_parent(&parent_commit, current_commit, 0)) < 0 ||
783 784 785 786
			(error = git_commit_tree(&parent_tree, parent_commit)) < 0)
			goto done;
	}

787 788
	git_oid_fmt(current_idstr, &operation->id);

789
	normalize_checkout_options_for_apply(&checkout_opts, rebase, current_commit);
790

791 792 793 794
	if ((error = git_indexwriter_init_for_operation(&indexwriter, rebase->repo, &checkout_opts.checkout_strategy)) < 0 ||
		(error = rebase_setupfile(rebase, MSGNUM_FILE, -1, "%d\n", rebase->current+1)) < 0 ||
		(error = rebase_setupfile(rebase, CURRENT_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, current_idstr)) < 0 ||
		(error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, NULL)) < 0 ||
795
		(error = git_merge__check_result(rebase->repo, index)) < 0 ||
796 797
		(error = git_checkout_index(rebase->repo, index, &checkout_opts)) < 0 ||
		(error = git_indexwriter_commit(&indexwriter)) < 0)
798 799
		goto done;

800
	*out = operation;
801

802
done:
803
	git_indexwriter_cleanup(&indexwriter);
804 805 806 807 808
	git_index_free(index);
	git_tree_free(current_tree);
	git_tree_free(head_tree);
	git_tree_free(parent_tree);
	git_commit_free(parent_commit);
809
	git_commit_free(current_commit);
810 811 812 813 814
	git_buf_free(&path);

	return error;
}

815
int git_rebase_next(
816
	git_rebase_operation **out,
817
	git_rebase *rebase)
818 819 820
{
	int error;

821
	assert(out && rebase);
822

823
	switch (rebase->type) {
824
	case GIT_REBASE_TYPE_MERGE:
825
		error = rebase_next_merge(out, rebase);
826 827 828 829 830
		break;
	default:
		abort();
	}

831 832 833 834 835
	return error;
}

static int rebase_commit_merge(
	git_oid *commit_id,
836
	git_rebase *rebase,
837 838 839 840 841 842 843
	const git_signature *author,
	const git_signature *committer,
	const char *message_encoding,
	const char *message)
{
	git_index *index = NULL;
	git_reference *head = NULL;
844 845
	git_commit *current_commit = NULL, *head_commit = NULL, *commit = NULL;
	git_rebase_operation *operation;
846 847
	git_tree *head_tree = NULL, *tree = NULL;
	git_diff *diff = NULL;
848
	git_oid tree_id;
849
	git_buf reflog_msg = GIT_BUF_INIT;
850 851 852
	char old_idstr[GIT_OID_HEXSZ], new_idstr[GIT_OID_HEXSZ];
	int error;

853 854
	operation = git_array_get(rebase->operations, rebase->current);
	assert(operation);
855

856
	if ((error = git_repository_index(&index, rebase->repo)) < 0)
857 858 859 860
		goto done;

	if (git_index_has_conflicts(index)) {
		giterr_set(GITERR_REBASE, "Conflicts have not been resolved");
861
		error = GIT_EUNMERGED;
862 863 864
		goto done;
	}

865 866
	if ((error = rebase_ensure_not_dirty(rebase->repo, false, true, GIT_EUNMERGED)) < 0 ||
		(error = git_commit_lookup(&current_commit, rebase->repo, &operation->id)) < 0 ||
867
		(error = git_repository_head(&head, rebase->repo)) < 0 ||
868
		(error = git_reference_peel((git_object **)&head_commit, head, GIT_OBJ_COMMIT)) < 0 ||
869
		(error = git_commit_tree(&head_tree, head_commit)) < 0 ||
870
		(error = git_diff_tree_to_index(&diff, rebase->repo, head_tree, index, NULL)) < 0)
871 872 873 874 875 876 877 878 879
		goto done;

	if (git_diff_num_deltas(diff) == 0) {
		giterr_set(GITERR_REBASE, "This patch has already been applied");
		error = GIT_EAPPLIED;
		goto done;
	}

	if ((error = git_index_write_tree(&tree_id, index)) < 0 ||
880
		(error = git_tree_lookup(&tree, rebase->repo, &tree_id)) < 0)
881 882 883
		goto done;

	if (!author)
884
		author = git_commit_author(current_commit);
885 886

	if (!message) {
887 888
		message_encoding = git_commit_message_encoding(current_commit);
		message = git_commit_message(current_commit);
889 890
	}

891
	if ((error = git_commit_create(commit_id, rebase->repo, NULL, author,
892
			committer, message_encoding, message, tree, 1,
893
			(const git_commit **)&head_commit)) < 0 ||
894
		(error = git_commit_lookup(&commit, rebase->repo, commit_id)) < 0 ||
895
		(error = git_reference__update_for_commit(
896
			rebase->repo, NULL, "HEAD", commit_id, "rebase")) < 0)
897 898
		goto done;

899
	git_oid_fmt(old_idstr, git_commit_id(current_commit));
900 901
	git_oid_fmt(new_idstr, commit_id);

902
	error = rebase_setupfile(rebase, REWRITTEN_FILE, O_CREAT|O_WRONLY|O_APPEND,
903 904 905
		"%.*s %.*s\n", GIT_OID_HEXSZ, old_idstr, GIT_OID_HEXSZ, new_idstr);

done:
906 907
	git_buf_free(&reflog_msg);
	git_commit_free(commit);
908
	git_diff_free(diff);
909
	git_tree_free(tree);
910
	git_tree_free(head_tree);
911
	git_commit_free(head_commit);
912
	git_commit_free(current_commit);
913 914 915 916 917 918 919 920
	git_reference_free(head);
	git_index_free(index);

	return error;
}

int git_rebase_commit(
	git_oid *id,
921
	git_rebase *rebase,
922 923 924 925 926 927 928
	const git_signature *author,
	const git_signature *committer,
	const char *message_encoding,
	const char *message)
{
	int error;

929
	assert(rebase && committer);
930

931
	switch (rebase->type) {
932 933
	case GIT_REBASE_TYPE_MERGE:
		error = rebase_commit_merge(
934
			id, rebase, author, committer, message_encoding, message);
935 936 937 938 939
		break;
	default:
		abort();
	}

940 941 942
	return error;
}

943
int git_rebase_abort(git_rebase *rebase)
944 945 946 947 948
{
	git_reference *orig_head_ref = NULL;
	git_commit *orig_head_commit = NULL;
	int error;

949
	assert(rebase);
950

951 952
	error = rebase->head_detached ?
		git_reference_create(&orig_head_ref, rebase->repo, GIT_HEAD_FILE,
953
			 &rebase->orig_head_id, 1, "rebase: aborting") :
954
		git_reference_symbolic_create(
955
			&orig_head_ref, rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1,
956
			"rebase: aborting");
957 958 959 960 961

	if (error < 0)
		goto done;

	if ((error = git_commit_lookup(
962 963
			&orig_head_commit, rebase->repo, &rebase->orig_head_id)) < 0 ||
		(error = git_reset(rebase->repo, (git_object *)orig_head_commit,
964
			GIT_RESET_HARD, &rebase->options.checkout_options)) < 0)
965 966
		goto done;

967
	error = rebase_cleanup(rebase);
968 969 970 971 972 973 974

done:
	git_commit_free(orig_head_commit);
	git_reference_free(orig_head_ref);

	return error;
}
975

976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
static int notes_ref_lookup(git_buf *out, git_rebase *rebase)
{
	git_config *config = NULL;
	int do_rewrite, error;

	if (rebase->options.rewrite_notes_ref) {
		git_buf_attach_notowned(out,
			rebase->options.rewrite_notes_ref,
			strlen(rebase->options.rewrite_notes_ref));
		return 0;
	}

	if ((error = git_repository_config(&config, rebase->repo)) < 0 ||
		(error = git_config_get_bool(&do_rewrite, config, "notes.rewrite.rebase")) < 0) {

		if (error != GIT_ENOTFOUND)
			goto done;

		giterr_clear();
		do_rewrite = 1;
	}

	error = do_rewrite ?
		git_config_get_string_buf(out, config, "notes.rewriteref") :
		GIT_ENOTFOUND;

done:
	git_config_free(config);
	return error;
}

1007
static int rebase_copy_note(
1008
	git_rebase *rebase,
1009
	const char *notes_ref,
1010 1011
	git_oid *from,
	git_oid *to,
1012
	const git_signature *committer)
1013
{
1014 1015
	git_note *note = NULL;
	git_oid note_id;
1016
	git_signature *who = NULL;
1017 1018
	int error;

1019
	if ((error = git_note_read(&note, rebase->repo, notes_ref, from)) < 0) {
1020 1021 1022 1023 1024 1025 1026 1027
		if (error == GIT_ENOTFOUND) {
			giterr_clear();
			error = 0;
		}

		goto done;
	}

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	if (!committer) {
		if((error = git_signature_default(&who, rebase->repo)) < 0) {
			if (error != GIT_ENOTFOUND ||
				(error = git_signature_now(&who, "unknown", "unknown")) < 0)
				goto done;

			giterr_clear();
		}

		committer = who;
	}

1040
	error = git_note_create(&note_id, rebase->repo, notes_ref,
1041
		git_note_author(note), committer, to, git_note_message(note), 0);
1042 1043 1044

done:
	git_note_free(note);
1045
	git_signature_free(who);
1046 1047 1048 1049 1050

	return error;
}

static int rebase_copy_notes(
1051
	git_rebase *rebase,
1052
	const git_signature *committer)
1053
{
1054
	git_buf path = GIT_BUF_INIT, rewritten = GIT_BUF_INIT, notes_ref = GIT_BUF_INIT;
1055 1056 1057 1058 1059
	char *pair_list, *fromstr, *tostr, *end;
	git_oid from, to;
	unsigned int linenum = 1;
	int error = 0;

1060 1061 1062 1063 1064 1065
	if ((error = notes_ref_lookup(&notes_ref, rebase)) < 0) {
		if (error == GIT_ENOTFOUND) {
			giterr_clear();
			error = 0;
		}

1066
		goto done;
1067
	}
1068

1069
	if ((error = git_buf_joinpath(&path, rebase->state_path, REWRITTEN_FILE)) < 0 ||
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
		(error = git_futils_readbuffer(&rewritten, path.ptr)) < 0)
		goto done;

	pair_list = rewritten.ptr;

	while (*pair_list) {
		fromstr = pair_list;

		if ((end = strchr(fromstr, '\n')) == NULL)
			goto on_error;

		pair_list = end+1;
		*end = '\0';

		if ((end = strchr(fromstr, ' ')) == NULL)
			goto on_error;

		tostr = end+1;
		*end = '\0';

		if (strlen(fromstr) != GIT_OID_HEXSZ ||
			strlen(tostr) != GIT_OID_HEXSZ ||
			git_oid_fromstr(&from, fromstr) < 0 ||
			git_oid_fromstr(&to, tostr) < 0)
			goto on_error;

1096
		if ((error = rebase_copy_note(rebase, notes_ref.ptr, &from, &to, committer)) < 0)
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
			goto done;

		linenum++;
	}

	goto done;

on_error:
	giterr_set(GITERR_REBASE, "Invalid rewritten file at line %d", linenum);
	error = -1;

done:
	git_buf_free(&rewritten);
	git_buf_free(&path);
1111
	git_buf_free(&notes_ref);
1112 1113 1114 1115 1116

	return error;
}

int git_rebase_finish(
1117
	git_rebase *rebase,
1118
	const git_signature *signature)
1119
{
1120 1121 1122 1123 1124 1125
	git_reference *terminal_ref = NULL, *branch_ref = NULL, *head_ref = NULL;
	git_commit *terminal_commit = NULL;
	git_buf branch_msg = GIT_BUF_INIT, head_msg = GIT_BUF_INIT;
	char onto[GIT_OID_HEXSZ];
	int error;

1126
	assert(rebase);
1127

1128
	git_oid_fmt(onto, &rebase->onto_id);
1129 1130

	if ((error = git_buf_printf(&branch_msg, "rebase finished: %s onto %.*s",
1131
			rebase->orig_head_name, GIT_OID_HEXSZ, onto)) < 0 ||
1132
		(error = git_buf_printf(&head_msg, "rebase finished: returning to %s",
1133 1134
			rebase->orig_head_name)) < 0 ||
		(error = git_repository_head(&terminal_ref, rebase->repo)) < 0 ||
1135
		(error = git_reference_peel((git_object **)&terminal_commit,
1136 1137
			terminal_ref, GIT_OBJ_COMMIT)) < 0 ||
		(error = git_reference_create_matching(&branch_ref,
1138
			rebase->repo, rebase->orig_head_name, git_commit_id(terminal_commit), 1,
1139
			&rebase->orig_head_id, branch_msg.ptr)) < 0 ||
1140
		(error = git_reference_symbolic_create(&head_ref,
1141
			rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1,
1142
			head_msg.ptr)) < 0 ||
1143
		(error = rebase_copy_notes(rebase, signature)) < 0)
1144
		goto done;
1145

1146
	error = rebase_cleanup(rebase);
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158

done:
	git_buf_free(&head_msg);
	git_buf_free(&branch_msg);
	git_commit_free(terminal_commit);
	git_reference_free(head_ref);
	git_reference_free(branch_ref);
	git_reference_free(terminal_ref);

	return error;
}

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
size_t git_rebase_operation_entrycount(git_rebase *rebase)
{
	assert(rebase);

	return git_array_size(rebase->operations);
}

size_t git_rebase_operation_current(git_rebase *rebase)
{
	assert(rebase);

1170
	return rebase->started ? rebase->current : GIT_REBASE_NO_OPERATION;
1171 1172 1173 1174 1175 1176 1177 1178 1179
}

git_rebase_operation *git_rebase_operation_byindex(git_rebase *rebase, size_t idx)
{
	assert(rebase);

	return git_array_get(rebase->operations, idx);
}

1180 1181 1182 1183 1184
void git_rebase_free(git_rebase *rebase)
{
	if (rebase == NULL)
		return;

1185
	git__free(rebase->onto_name);
1186 1187
	git__free(rebase->orig_head_name);
	git__free(rebase->state_path);
1188
	git_array_clear(rebase->operations);
1189
	git__free((char *)rebase->options.rewrite_notes_ref);
1190
	git__free(rebase);
1191
}