Hash :
a35a9890
Author :
Date :
2014-07-17T18:25:03
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 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 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
/*
* 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"
#include <git2/types.h>
#include <git2/rebase.h>
#include <git2/commit.h>
#include <git2/reset.h>
#include <git2/revwalk.h>
#define REBASE_APPLY_DIR "rebase-apply"
#define REBASE_MERGE_DIR "rebase-merge"
#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"
#define MSGNUM_FILE "msgnum"
#define END_FILE "end"
#define CMT_FILE_FMT "cmt.%d"
#define CURRENT_FILE "current"
#define REWRITTEN_FILE "rewritten"
#define ORIG_DETACHED_HEAD "detached HEAD"
#define REBASE_DIR_MODE 0777
#define REBASE_FILE_MODE 0666
typedef enum {
GIT_REBASE_TYPE_NONE = 0,
GIT_REBASE_TYPE_APPLY = 1,
GIT_REBASE_TYPE_MERGE = 2,
GIT_REBASE_TYPE_INTERACTIVE = 3,
} git_rebase_type_t;
struct git_rebase_state_merge {
int32_t msgnum;
int32_t end;
char *onto_name;
git_commit *current;
};
typedef struct {
git_rebase_type_t type;
char *state_path;
int head_detached : 1;
char *orig_head_name;
git_oid orig_head_id;
union {
struct git_rebase_state_merge merge;
};
} git_rebase_state;
#define GIT_REBASE_STATE_INIT {0}
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;
}
static int rebase_state_merge(git_rebase_state *state, git_repository *repo)
{
git_buf path = GIT_BUF_INIT, msgnum = GIT_BUF_INIT, end = GIT_BUF_INIT,
onto_name = GIT_BUF_INIT, current = GIT_BUF_INIT;
git_oid current_id;
int state_path_len, error;
GIT_UNUSED(repo);
if ((error = git_buf_puts(&path, state->state_path)) < 0)
goto done;
state_path_len = git_buf_len(&path);
/* Read 'end' */
if ((error = git_buf_joinpath(&path, path.ptr, END_FILE)) < 0 ||
(error = git_futils_readbuffer(&end, path.ptr)) < 0)
goto done;
git_buf_rtrim(&end);
if ((error = git__strtol32(&state->merge.end, end.ptr, NULL, 10)) < 0)
goto done;
/* Read 'onto_name' */
git_buf_truncate(&path, state_path_len);
if ((error = git_buf_joinpath(&path, path.ptr, ONTO_NAME_FILE)) < 0 ||
(error = git_futils_readbuffer(&onto_name, path.ptr)) < 0)
goto done;
git_buf_rtrim(&onto_name);
state->merge.onto_name = git_buf_detach(&onto_name);
/* Read 'msgnum' if it exists, otherwise let msgnum = 0 */
git_buf_truncate(&path, state_path_len);
if ((error = git_buf_joinpath(&path, path.ptr, MSGNUM_FILE)) < 0)
goto done;
if (git_path_exists(path.ptr)) {
if ((error = git_futils_readbuffer(&msgnum, path.ptr)) < 0)
goto done;
git_buf_rtrim(&msgnum);
if ((error = git__strtol32(&state->merge.msgnum, msgnum.ptr, NULL, 10)) < 0)
goto done;
}
/* Read 'current' if it exists, otherwise let current = null */
git_buf_truncate(&path, state_path_len);
if ((error = git_buf_joinpath(&path, path.ptr, CURRENT_FILE)) < 0)
goto done;
if (git_path_exists(path.ptr)) {
if ((error = git_futils_readbuffer(¤t, path.ptr)) < 0)
goto done;
git_buf_rtrim(¤t);
if ((error = git_oid_fromstr(¤t_id, current.ptr)) < 0 ||
(error = git_commit_lookup(&state->merge.current, repo, ¤t_id)) < 0)
goto done;
}
done:
git_buf_free(&path);
git_buf_free(&msgnum);
git_buf_free(&end);
git_buf_free(&onto_name);
git_buf_free(¤t);
return error;
}
static int rebase_state(git_rebase_state *state, git_repository *repo)
{
git_buf path = GIT_BUF_INIT, orig_head_name = GIT_BUF_INIT,
orig_head_id = GIT_BUF_INIT;
int state_path_len, error;
memset(state, 0x0, sizeof(git_rebase_state));
if ((error = rebase_state_type(&state->type, &state->state_path, repo)) < 0)
goto done;
if (state->type == GIT_REBASE_TYPE_NONE) {
giterr_set(GITERR_REBASE, "There is no rebase in progress");
return GIT_ENOTFOUND;
}
if ((error = git_buf_puts(&path, state->state_path)) < 0)
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)
state->head_detached = 1;
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);
if ((error = git_oid_fromstr(&state->orig_head_id, orig_head_id.ptr)) < 0)
goto done;
if (!state->head_detached)
state->orig_head_name = git_buf_detach(&orig_head_name);
switch (state->type) {
case GIT_REBASE_TYPE_INTERACTIVE:
giterr_set(GITERR_REBASE, "Interactive rebase is not supported");
error = -1;
break;
case GIT_REBASE_TYPE_MERGE:
error = rebase_state_merge(state, repo);
break;
case GIT_REBASE_TYPE_APPLY:
giterr_set(GITERR_REBASE, "Patch application rebase is not supported");
error = -1;
break;
default:
abort();
}
done:
git_buf_free(&path);
git_buf_free(&orig_head_name);
git_buf_free(&orig_head_id);
git_buf_free(&onto_id);
return error;
}
static void rebase_state_free(git_rebase_state *state)
{
if (state == NULL)
return;
if (state->type == GIT_REBASE_TYPE_MERGE) {
git__free(state->merge.onto_name);
git_commit_free(state->merge.current);
}
git__free(state->orig_head_name);
git__free(state->state_path);
}
static int rebase_finish(git_rebase_state *state)
{
return git_path_isdir(state->state_path) ?
git_futils_rmdir_r(state->state_path, NULL, GIT_RMDIR_REMOVE_FILES) :
0;
}
static int rebase_setupfile(git_repository *repo, const char *filename, int flags, const char *fmt, ...)
{
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);
if ((error = git_buf_joinpath(&path, repo->path_repository, REBASE_MERGE_DIR)) == 0 &&
(error = git_buf_joinpath(&path, path.ptr, filename)) == 0)
error = git_futils_writebuffer(&contents, path.ptr, flags, REBASE_FILE_MODE);
git_buf_free(&path);
git_buf_free(&contents);
return error;
}
/* TODO: git.git actually uses the literal argv here, this is an attempt
* to emulate that.
*/
static const char *rebase_onto_name(const git_merge_head *onto)
{
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
return onto->oid_str;
}
static int rebase_setup_merge(
git_repository *repo,
const git_merge_head *branch,
const git_merge_head *upstream,
const git_merge_head *onto,
const git_rebase_options *opts)
{
git_revwalk *revwalk = NULL;
git_commit *commit;
git_buf commit_filename = GIT_BUF_INIT;
git_oid id;
char id_str[GIT_OID_HEXSZ];
bool merge;
int commit_cnt = 0, error;
GIT_UNUSED(opts);
if (!upstream)
upstream = onto;
if ((error = git_revwalk_new(&revwalk, repo)) < 0 ||
(error = git_revwalk_push(revwalk, &branch->oid)) < 0 ||
(error = git_revwalk_hide(revwalk, &upstream->oid)) < 0)
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;
commit_cnt++;
git_buf_clear(&commit_filename);
git_buf_printf(&commit_filename, CMT_FILE_FMT, commit_cnt);
git_oid_fmt(id_str, &id);
if ((error = rebase_setupfile(repo, commit_filename.ptr, -1,
"%.*s\n", GIT_OID_HEXSZ, id_str)) < 0)
goto done;
}
if (error != GIT_ITEROVER ||
(error = rebase_setupfile(repo, END_FILE, -1, "%d\n", commit_cnt)) < 0)
goto done;
error = rebase_setupfile(repo, ONTO_NAME_FILE, -1, "%s\n",
rebase_onto_name(onto));
done:
git_revwalk_free(revwalk);
git_buf_free(&commit_filename);
return error;
}
static int rebase_setup(
git_repository *repo,
const git_merge_head *branch,
const git_merge_head *upstream,
const git_merge_head *onto,
const git_rebase_options *opts)
{
git_buf state_path = GIT_BUF_INIT;
const char *orig_head_name;
int error;
if (git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR) < 0)
return -1;
if ((error = p_mkdir(state_path.ptr, REBASE_DIR_MODE)) < 0) {
giterr_set(GITERR_OS, "Failed to create rebase directory '%s'",
state_path.ptr);
goto done;
}
if ((error = git_repository__set_orig_head(repo, &branch->oid)) < 0)
goto done;
orig_head_name = branch->ref_name ? branch->ref_name : ORIG_DETACHED_HEAD;
if ((error = rebase_setupfile(repo, HEAD_NAME_FILE, -1, "%s\n", orig_head_name)) < 0 ||
(error = rebase_setupfile(repo, ONTO_FILE, -1, "%s\n", onto->oid_str)) < 0 ||
(error = rebase_setupfile(repo, ORIG_HEAD_FILE, -1, "%s\n", branch->oid_str)) < 0 ||
(error = rebase_setupfile(repo, QUIET_FILE, -1, opts->quiet ? "t\n" : "\n")) < 0)
goto done;
error = rebase_setup_merge(repo, branch, upstream, onto, opts);
done:
if (error < 0)
git_repository__cleanup_files(repo, (const char **)&state_path.ptr, 1);
git_buf_free(&state_path);
return error;
}
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 void rebase_normalize_options(
git_rebase_options *opts,
const git_rebase_options *given_opts)
{
if (given_opts)
memcpy(&opts, given_opts, sizeof(git_rebase_options));
}
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;
}
static int rebase_ensure_not_dirty(git_repository *repo)
{
git_tree *head = NULL;
git_index *index = NULL;
git_diff *diff = NULL;
int error;
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;
if (git_diff_num_deltas(diff) > 0) {
giterr_set(GITERR_REBASE, "Uncommitted changes exist in index");
error = -1;
goto done;
}
git_diff_free(diff);
diff = NULL;
if ((error = git_diff_index_to_workdir(&diff, repo, index, NULL)) < 0)
goto done;
if (git_diff_num_deltas(diff) > 0) {
giterr_set(GITERR_REBASE, "Unstaged changes exist in workdir");
error = -1;
}
done:
git_diff_free(diff);
git_index_free(index);
git_tree_free(head);
return error;
}
int git_rebase(
git_repository *repo,
const git_merge_head *branch,
const git_merge_head *upstream,
const git_merge_head *onto,
const git_signature *signature,
const git_rebase_options *given_opts)
{
git_rebase_options opts = GIT_REBASE_OPTIONS_INIT;
git_reference *head_ref = NULL;
git_buf reflog = GIT_BUF_INIT;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
int error;
assert(repo && branch && (upstream || onto));
GITERR_CHECK_VERSION(given_opts, GIT_MERGE_OPTIONS_VERSION, "git_merge_options");
rebase_normalize_options(&opts, given_opts);
if ((error = git_repository__ensure_not_bare(repo, "rebase")) < 0 ||
(error = rebase_ensure_not_in_progress(repo)) < 0 ||
(error = rebase_ensure_not_dirty(repo)) < 0)
goto done;
if (!onto)
onto = upstream;
if ((error = rebase_setup(repo, branch, upstream, onto, &opts)) < 0)
goto done;
if ((error = git_buf_printf(&reflog,
"rebase: checkout %s", rebase_onto_name(onto))) < 0 ||
(error = git_reference_create(&head_ref, repo, GIT_HEAD_FILE,
&onto->oid, 1, signature, reflog.ptr)) < 0)
goto done;
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
error = git_checkout_head(repo, &checkout_opts);
done:
git_reference_free(head_ref);
git_buf_free(&reflog);
return error;
}
static int normalize_checkout_opts(
git_repository *repo,
git_checkout_options *checkout_opts,
const git_checkout_options *given_checkout_opts,
const git_rebase_state *state)
{
int error = 0;
GIT_UNUSED(repo);
if (given_checkout_opts != NULL)
memcpy(checkout_opts, given_checkout_opts, sizeof(git_checkout_options));
else {
git_checkout_options default_checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
default_checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
memcpy(checkout_opts, &default_checkout_opts, sizeof(git_checkout_options));
}
if (!checkout_opts->ancestor_label)
checkout_opts->ancestor_label = "ancestor";
if (state->type == GIT_REBASE_TYPE_MERGE) {
if (!checkout_opts->our_label)
checkout_opts->our_label = state->merge.onto_name;
if (!checkout_opts->their_label)
checkout_opts->their_label = git_commit_summary(state->merge.current);
} else {
abort();
}
return error;
}
static int rebase_next_merge(
git_repository *repo,
git_rebase_state *state,
git_checkout_options *given_checkout_opts)
{
git_buf path = GIT_BUF_INIT, current = GIT_BUF_INIT;
git_checkout_options checkout_opts = {0};
git_oid current_id;
git_commit *parent_commit = NULL;
git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL;
git_index *index = NULL;
unsigned int parent_count;
int error;
if (state->merge.msgnum == state->merge.end)
return GIT_ITEROVER;
state->merge.msgnum++;
if ((error = git_buf_joinpath(&path, state->state_path, "cmt.")) < 0 ||
(error = git_buf_printf(&path, "%d", state->merge.msgnum)) < 0 ||
(error = git_futils_readbuffer(¤t, path.ptr)) < 0)
goto done;
git_buf_rtrim(¤t);
if (state->merge.current)
git_commit_free(state->merge.current);
if ((error = git_oid_fromstr(¤t_id, current.ptr)) < 0 ||
(error = git_commit_lookup(&state->merge.current, repo, ¤t_id)) < 0 ||
(error = git_commit_tree(¤t_tree, state->merge.current)) < 0 ||
(error = git_repository_head_tree(&head_tree, repo)) < 0)
goto done;
if ((parent_count = git_commit_parentcount(state->merge.current)) > 1) {
giterr_set(GITERR_REBASE, "Cannot rebase a merge commit");
error = -1;
goto done;
} else if (parent_count) {
if ((error = git_commit_parent(&parent_commit, state->merge.current, 0)) < 0 ||
(error = git_commit_tree(&parent_tree, parent_commit)) < 0)
goto done;
}
if ((error = rebase_setupfile(repo, MSGNUM_FILE, -1, "%d\n", state->merge.msgnum)) < 0 ||
(error = rebase_setupfile(repo, CURRENT_FILE, -1, "%s\n", current.ptr)) < 0)
goto done;
if ((error = normalize_checkout_opts(repo, &checkout_opts, given_checkout_opts, state)) < 0 ||
(error = git_merge_trees(&index, repo, parent_tree, head_tree, current_tree, NULL)) < 0 ||
(error = git_merge__check_result(repo, index)) < 0 ||
(error = git_checkout_index(repo, index, &checkout_opts)) < 0)
goto done;
done:
git_index_free(index);
git_tree_free(current_tree);
git_tree_free(head_tree);
git_tree_free(parent_tree);
git_commit_free(parent_commit);
git_buf_free(&path);
git_buf_free(¤t);
return error;
}
int git_rebase_next(
git_repository *repo,
git_checkout_options *checkout_opts)
{
git_rebase_state state = GIT_REBASE_STATE_INIT;
int error;
assert(repo);
if ((error = rebase_state(&state, repo)) < 0)
return -1;
switch (state.type) {
case GIT_REBASE_TYPE_MERGE:
error = rebase_next_merge(repo, &state, checkout_opts);
break;
default:
abort();
}
rebase_state_free(&state);
return error;
}
static int rebase_commit_merge(
git_oid *commit_id,
git_repository *repo,
git_rebase_state *state,
const git_signature *author,
const git_signature *committer,
const char *message_encoding,
const char *message)
{
git_index *index = NULL;
git_reference *head = NULL;
git_commit *head_commit = NULL;
git_tree *tree = NULL;
git_oid tree_id;
char old_idstr[GIT_OID_HEXSZ], new_idstr[GIT_OID_HEXSZ];
int error;
if (!state->merge.msgnum || !state->merge.current) {
giterr_set(GITERR_REBASE, "No rebase-merge state files exist");
error = -1;
goto done;
}
if ((error = git_repository_index(&index, repo)) < 0)
goto done;
if (git_index_has_conflicts(index)) {
giterr_set(GITERR_REBASE, "Conflicts have not been resolved");
error = GIT_EMERGECONFLICT;
goto done;
}
/* TODO: if there are no changes, error with a useful code */
if ((error = git_repository_head(&head, repo)) < 0 ||
(error = git_reference_peel((git_object **)&head_commit, head, GIT_OBJ_COMMIT)) < 0 ||
(error = git_index_write_tree(&tree_id, index)) < 0 ||
(error = git_tree_lookup(&tree, repo, &tree_id)) < 0)
goto done;
if (!author)
author = git_commit_author(state->merge.current);
if (!message) {
message_encoding = git_commit_message_encoding(state->merge.current);
message = git_commit_message(state->merge.current);
}
if ((error = git_commit_create(commit_id, repo, "HEAD", author,
committer, message_encoding, message, tree, 1,
(const git_commit **)&head_commit)) < 0)
goto done;
git_oid_fmt(old_idstr, git_commit_id(state->merge.current));
git_oid_fmt(new_idstr, commit_id);
error = rebase_setupfile(repo, REWRITTEN_FILE, O_CREAT|O_WRONLY|O_APPEND,
"%.*s %.*s\n", GIT_OID_HEXSZ, old_idstr, GIT_OID_HEXSZ, new_idstr);
done:
git_tree_free(tree);
git_commit_free(head_commit);
git_reference_free(head);
git_index_free(index);
return error;
}
int git_rebase_commit(
git_oid *id,
git_repository *repo,
const git_signature *author,
const git_signature *committer,
const char *message_encoding,
const char *message)
{
git_rebase_state state = GIT_REBASE_STATE_INIT;
int error;
assert(repo && committer);
if ((error = rebase_state(&state, repo)) < 0)
goto done;
switch (state.type) {
case GIT_REBASE_TYPE_MERGE:
error = rebase_commit_merge(
id, repo, &state, author, committer, message_encoding, message);
break;
default:
abort();
}
done:
rebase_state_free(&state);
return error;
}
int git_rebase_abort(git_repository *repo, const git_signature *signature)
{
git_rebase_state state = GIT_REBASE_STATE_INIT;
git_reference *orig_head_ref = NULL;
git_commit *orig_head_commit = NULL;
int error;
assert(repo && signature);
if ((error = rebase_state(&state, repo)) < 0)
goto done;
error = state.head_detached ?
git_reference_create(&orig_head_ref, repo, GIT_HEAD_FILE,
&state.orig_head_id, 1, signature, "rebase: aborting") :
git_reference_symbolic_create(
&orig_head_ref, repo, GIT_HEAD_FILE, state.orig_head_name, 1,
signature, "rebase: aborting");
if (error < 0)
goto done;
if ((error = git_commit_lookup(
&orig_head_commit, repo, &state.orig_head_id)) < 0 ||
(error = git_reset(repo, (git_object *)orig_head_commit,
GIT_RESET_HARD, NULL, signature, NULL)) < 0)
goto done;
error = rebase_finish(&state);
done:
git_commit_free(orig_head_commit);
git_reference_free(orig_head_ref);
rebase_state_free(&state);
return error;
}