builtin / rebase.con commit rebase: remove the rebase.useBuiltin setting (d03ebd4)
   1/*
   2 * "git rebase" builtin command
   3 *
   4 * Copyright (c) 2018 Pratik Karki
   5 */
   6
   7#define USE_THE_INDEX_COMPATIBILITY_MACROS
   8#include "builtin.h"
   9#include "run-command.h"
  10#include "exec-cmd.h"
  11#include "argv-array.h"
  12#include "dir.h"
  13#include "packfile.h"
  14#include "refs.h"
  15#include "quote.h"
  16#include "config.h"
  17#include "cache-tree.h"
  18#include "unpack-trees.h"
  19#include "lockfile.h"
  20#include "parse-options.h"
  21#include "commit.h"
  22#include "diff.h"
  23#include "wt-status.h"
  24#include "revision.h"
  25#include "commit-reach.h"
  26#include "rerere.h"
  27#include "branch.h"
  28
  29static char const * const builtin_rebase_usage[] = {
  30        N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
  31                "[<upstream>] [<branch>]"),
  32        N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
  33                "--root [<branch>]"),
  34        N_("git rebase --continue | --abort | --skip | --edit-todo"),
  35        NULL
  36};
  37
  38static GIT_PATH_FUNC(apply_dir, "rebase-apply")
  39static GIT_PATH_FUNC(merge_dir, "rebase-merge")
  40
  41enum rebase_type {
  42        REBASE_UNSPECIFIED = -1,
  43        REBASE_AM,
  44        REBASE_MERGE,
  45        REBASE_INTERACTIVE,
  46        REBASE_PRESERVE_MERGES
  47};
  48
  49struct rebase_options {
  50        enum rebase_type type;
  51        const char *state_dir;
  52        struct commit *upstream;
  53        const char *upstream_name;
  54        const char *upstream_arg;
  55        char *head_name;
  56        struct object_id orig_head;
  57        struct commit *onto;
  58        const char *onto_name;
  59        const char *revisions;
  60        const char *switch_to;
  61        int root;
  62        struct object_id *squash_onto;
  63        struct commit *restrict_revision;
  64        int dont_finish_rebase;
  65        enum {
  66                REBASE_NO_QUIET = 1<<0,
  67                REBASE_VERBOSE = 1<<1,
  68                REBASE_DIFFSTAT = 1<<2,
  69                REBASE_FORCE = 1<<3,
  70                REBASE_INTERACTIVE_EXPLICIT = 1<<4,
  71        } flags;
  72        struct argv_array git_am_opts;
  73        const char *action;
  74        int signoff;
  75        int allow_rerere_autoupdate;
  76        int keep_empty;
  77        int autosquash;
  78        char *gpg_sign_opt;
  79        int autostash;
  80        char *cmd;
  81        int allow_empty_message;
  82        int rebase_merges, rebase_cousins;
  83        char *strategy, *strategy_opts;
  84        struct strbuf git_format_patch_opt;
  85        int reschedule_failed_exec;
  86        int use_legacy_rebase;
  87};
  88
  89static int is_interactive(struct rebase_options *opts)
  90{
  91        return opts->type == REBASE_INTERACTIVE ||
  92                opts->type == REBASE_PRESERVE_MERGES;
  93}
  94
  95static void imply_interactive(struct rebase_options *opts, const char *option)
  96{
  97        switch (opts->type) {
  98        case REBASE_AM:
  99                die(_("%s requires an interactive rebase"), option);
 100                break;
 101        case REBASE_INTERACTIVE:
 102        case REBASE_PRESERVE_MERGES:
 103                break;
 104        case REBASE_MERGE:
 105                /* we now implement --merge via --interactive */
 106        default:
 107                opts->type = REBASE_INTERACTIVE; /* implied */
 108                break;
 109        }
 110}
 111
 112/* Returns the filename prefixed by the state_dir */
 113static const char *state_dir_path(const char *filename, struct rebase_options *opts)
 114{
 115        static struct strbuf path = STRBUF_INIT;
 116        static size_t prefix_len;
 117
 118        if (!prefix_len) {
 119                strbuf_addf(&path, "%s/", opts->state_dir);
 120                prefix_len = path.len;
 121        }
 122
 123        strbuf_setlen(&path, prefix_len);
 124        strbuf_addstr(&path, filename);
 125        return path.buf;
 126}
 127
 128/* Read one file, then strip line endings */
 129static int read_one(const char *path, struct strbuf *buf)
 130{
 131        if (strbuf_read_file(buf, path, 0) < 0)
 132                return error_errno(_("could not read '%s'"), path);
 133        strbuf_trim_trailing_newline(buf);
 134        return 0;
 135}
 136
 137/* Initialize the rebase options from the state directory. */
 138static int read_basic_state(struct rebase_options *opts)
 139{
 140        struct strbuf head_name = STRBUF_INIT;
 141        struct strbuf buf = STRBUF_INIT;
 142        struct object_id oid;
 143
 144        if (read_one(state_dir_path("head-name", opts), &head_name) ||
 145            read_one(state_dir_path("onto", opts), &buf))
 146                return -1;
 147        opts->head_name = starts_with(head_name.buf, "refs/") ?
 148                xstrdup(head_name.buf) : NULL;
 149        strbuf_release(&head_name);
 150        if (get_oid(buf.buf, &oid))
 151                return error(_("could not get 'onto': '%s'"), buf.buf);
 152        opts->onto = lookup_commit_or_die(&oid, buf.buf);
 153
 154        /*
 155         * We always write to orig-head, but interactive rebase used to write to
 156         * head. Fall back to reading from head to cover for the case that the
 157         * user upgraded git with an ongoing interactive rebase.
 158         */
 159        strbuf_reset(&buf);
 160        if (file_exists(state_dir_path("orig-head", opts))) {
 161                if (read_one(state_dir_path("orig-head", opts), &buf))
 162                        return -1;
 163        } else if (read_one(state_dir_path("head", opts), &buf))
 164                return -1;
 165        if (get_oid(buf.buf, &opts->orig_head))
 166                return error(_("invalid orig-head: '%s'"), buf.buf);
 167
 168        if (file_exists(state_dir_path("quiet", opts)))
 169                opts->flags &= ~REBASE_NO_QUIET;
 170        else
 171                opts->flags |= REBASE_NO_QUIET;
 172
 173        if (file_exists(state_dir_path("verbose", opts)))
 174                opts->flags |= REBASE_VERBOSE;
 175
 176        if (file_exists(state_dir_path("signoff", opts))) {
 177                opts->signoff = 1;
 178                opts->flags |= REBASE_FORCE;
 179        }
 180
 181        if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
 182                strbuf_reset(&buf);
 183                if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
 184                            &buf))
 185                        return -1;
 186                if (!strcmp(buf.buf, "--rerere-autoupdate"))
 187                        opts->allow_rerere_autoupdate = 1;
 188                else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
 189                        opts->allow_rerere_autoupdate = 0;
 190                else
 191                        warning(_("ignoring invalid allow_rerere_autoupdate: "
 192                                  "'%s'"), buf.buf);
 193        } else
 194                opts->allow_rerere_autoupdate = -1;
 195
 196        if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
 197                strbuf_reset(&buf);
 198                if (read_one(state_dir_path("gpg_sign_opt", opts),
 199                            &buf))
 200                        return -1;
 201                free(opts->gpg_sign_opt);
 202                opts->gpg_sign_opt = xstrdup(buf.buf);
 203        }
 204
 205        if (file_exists(state_dir_path("strategy", opts))) {
 206                strbuf_reset(&buf);
 207                if (read_one(state_dir_path("strategy", opts), &buf))
 208                        return -1;
 209                free(opts->strategy);
 210                opts->strategy = xstrdup(buf.buf);
 211        }
 212
 213        if (file_exists(state_dir_path("strategy_opts", opts))) {
 214                strbuf_reset(&buf);
 215                if (read_one(state_dir_path("strategy_opts", opts), &buf))
 216                        return -1;
 217                free(opts->strategy_opts);
 218                opts->strategy_opts = xstrdup(buf.buf);
 219        }
 220
 221        strbuf_release(&buf);
 222
 223        return 0;
 224}
 225
 226static int write_basic_state(struct rebase_options *opts)
 227{
 228        write_file(state_dir_path("head-name", opts), "%s",
 229                   opts->head_name ? opts->head_name : "detached HEAD");
 230        write_file(state_dir_path("onto", opts), "%s",
 231                   opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
 232        write_file(state_dir_path("orig-head", opts), "%s",
 233                   oid_to_hex(&opts->orig_head));
 234        write_file(state_dir_path("quiet", opts), "%s",
 235                   opts->flags & REBASE_NO_QUIET ? "" : "t");
 236        if (opts->flags & REBASE_VERBOSE)
 237                write_file(state_dir_path("verbose", opts), "%s", "");
 238        if (opts->strategy)
 239                write_file(state_dir_path("strategy", opts), "%s",
 240                           opts->strategy);
 241        if (opts->strategy_opts)
 242                write_file(state_dir_path("strategy_opts", opts), "%s",
 243                           opts->strategy_opts);
 244        if (opts->allow_rerere_autoupdate >= 0)
 245                write_file(state_dir_path("allow_rerere_autoupdate", opts),
 246                           "-%s-rerere-autoupdate",
 247                           opts->allow_rerere_autoupdate ? "" : "-no");
 248        if (opts->gpg_sign_opt)
 249                write_file(state_dir_path("gpg_sign_opt", opts), "%s",
 250                           opts->gpg_sign_opt);
 251        if (opts->signoff)
 252                write_file(state_dir_path("strategy", opts), "--signoff");
 253
 254        return 0;
 255}
 256
 257static int apply_autostash(struct rebase_options *opts)
 258{
 259        const char *path = state_dir_path("autostash", opts);
 260        struct strbuf autostash = STRBUF_INIT;
 261        struct child_process stash_apply = CHILD_PROCESS_INIT;
 262
 263        if (!file_exists(path))
 264                return 0;
 265
 266        if (read_one(path, &autostash))
 267                return error(_("Could not read '%s'"), path);
 268        /* Ensure that the hash is not mistaken for a number */
 269        strbuf_addstr(&autostash, "^0");
 270        argv_array_pushl(&stash_apply.args,
 271                         "stash", "apply", autostash.buf, NULL);
 272        stash_apply.git_cmd = 1;
 273        stash_apply.no_stderr = stash_apply.no_stdout =
 274                stash_apply.no_stdin = 1;
 275        if (!run_command(&stash_apply))
 276                printf(_("Applied autostash.\n"));
 277        else {
 278                struct argv_array args = ARGV_ARRAY_INIT;
 279                int res = 0;
 280
 281                argv_array_pushl(&args,
 282                                 "stash", "store", "-m", "autostash", "-q",
 283                                 autostash.buf, NULL);
 284                if (run_command_v_opt(args.argv, RUN_GIT_CMD))
 285                        res = error(_("Cannot store %s"), autostash.buf);
 286                argv_array_clear(&args);
 287                strbuf_release(&autostash);
 288                if (res)
 289                        return res;
 290
 291                fprintf(stderr,
 292                        _("Applying autostash resulted in conflicts.\n"
 293                          "Your changes are safe in the stash.\n"
 294                          "You can run \"git stash pop\" or \"git stash drop\" "
 295                          "at any time.\n"));
 296        }
 297
 298        strbuf_release(&autostash);
 299        return 0;
 300}
 301
 302static int finish_rebase(struct rebase_options *opts)
 303{
 304        struct strbuf dir = STRBUF_INIT;
 305        const char *argv_gc_auto[] = { "gc", "--auto", NULL };
 306
 307        delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
 308        apply_autostash(opts);
 309        close_all_packs(the_repository->objects);
 310        /*
 311         * We ignore errors in 'gc --auto', since the
 312         * user should see them.
 313         */
 314        run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
 315        strbuf_addstr(&dir, opts->state_dir);
 316        remove_dir_recursively(&dir, 0);
 317        strbuf_release(&dir);
 318
 319        return 0;
 320}
 321
 322static struct commit *peel_committish(const char *name)
 323{
 324        struct object *obj;
 325        struct object_id oid;
 326
 327        if (get_oid(name, &oid))
 328                return NULL;
 329        obj = parse_object(the_repository, &oid);
 330        return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
 331}
 332
 333static void add_var(struct strbuf *buf, const char *name, const char *value)
 334{
 335        if (!value)
 336                strbuf_addf(buf, "unset %s; ", name);
 337        else {
 338                strbuf_addf(buf, "%s=", name);
 339                sq_quote_buf(buf, value);
 340                strbuf_addstr(buf, "; ");
 341        }
 342}
 343
 344#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
 345
 346#define RESET_HEAD_DETACH (1<<0)
 347#define RESET_HEAD_HARD (1<<1)
 348#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
 349#define RESET_HEAD_REFS_ONLY (1<<3)
 350
 351static int reset_head(struct object_id *oid, const char *action,
 352                      const char *switch_to_branch, unsigned flags,
 353                      const char *reflog_orig_head, const char *reflog_head)
 354{
 355        unsigned detach_head = flags & RESET_HEAD_DETACH;
 356        unsigned reset_hard = flags & RESET_HEAD_HARD;
 357        unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
 358        unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
 359        struct object_id head_oid;
 360        struct tree_desc desc[2] = { { NULL }, { NULL } };
 361        struct lock_file lock = LOCK_INIT;
 362        struct unpack_trees_options unpack_tree_opts;
 363        struct tree *tree;
 364        const char *reflog_action;
 365        struct strbuf msg = STRBUF_INIT;
 366        size_t prefix_len;
 367        struct object_id *orig = NULL, oid_orig,
 368                *old_orig = NULL, oid_old_orig;
 369        int ret = 0, nr = 0;
 370
 371        if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
 372                BUG("Not a fully qualified branch: '%s'", switch_to_branch);
 373
 374        if (!refs_only && hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
 375                ret = -1;
 376                goto leave_reset_head;
 377        }
 378
 379        if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
 380                ret = error(_("could not determine HEAD revision"));
 381                goto leave_reset_head;
 382        }
 383
 384        if (!oid)
 385                oid = &head_oid;
 386
 387        if (refs_only)
 388                goto reset_head_refs;
 389
 390        memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
 391        setup_unpack_trees_porcelain(&unpack_tree_opts, action);
 392        unpack_tree_opts.head_idx = 1;
 393        unpack_tree_opts.src_index = the_repository->index;
 394        unpack_tree_opts.dst_index = the_repository->index;
 395        unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
 396        unpack_tree_opts.update = 1;
 397        unpack_tree_opts.merge = 1;
 398        if (!detach_head)
 399                unpack_tree_opts.reset = 1;
 400
 401        if (repo_read_index_unmerged(the_repository) < 0) {
 402                ret = error(_("could not read index"));
 403                goto leave_reset_head;
 404        }
 405
 406        if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
 407                ret = error(_("failed to find tree of %s"),
 408                            oid_to_hex(&head_oid));
 409                goto leave_reset_head;
 410        }
 411
 412        if (!fill_tree_descriptor(&desc[nr++], oid)) {
 413                ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
 414                goto leave_reset_head;
 415        }
 416
 417        if (unpack_trees(nr, desc, &unpack_tree_opts)) {
 418                ret = -1;
 419                goto leave_reset_head;
 420        }
 421
 422        tree = parse_tree_indirect(oid);
 423        prime_cache_tree(the_repository, the_repository->index, tree);
 424
 425        if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
 426                ret = error(_("could not write index"));
 427                goto leave_reset_head;
 428        }
 429
 430reset_head_refs:
 431        reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
 432        strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
 433        prefix_len = msg.len;
 434
 435        if (!get_oid("ORIG_HEAD", &oid_old_orig))
 436                old_orig = &oid_old_orig;
 437        if (!get_oid("HEAD", &oid_orig)) {
 438                orig = &oid_orig;
 439                if (!reflog_orig_head) {
 440                        strbuf_addstr(&msg, "updating ORIG_HEAD");
 441                        reflog_orig_head = msg.buf;
 442                }
 443                update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
 444                           UPDATE_REFS_MSG_ON_ERR);
 445        } else if (old_orig)
 446                delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
 447        if (!reflog_head) {
 448                strbuf_setlen(&msg, prefix_len);
 449                strbuf_addstr(&msg, "updating HEAD");
 450                reflog_head = msg.buf;
 451        }
 452        if (!switch_to_branch)
 453                ret = update_ref(reflog_head, "HEAD", oid, orig,
 454                                 detach_head ? REF_NO_DEREF : 0,
 455                                 UPDATE_REFS_MSG_ON_ERR);
 456        else {
 457                ret = update_ref(reflog_orig_head, switch_to_branch, oid,
 458                                 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
 459                if (!ret)
 460                        ret = create_symref("HEAD", switch_to_branch,
 461                                            reflog_head);
 462        }
 463        if (run_hook)
 464                run_hook_le(NULL, "post-checkout",
 465                            oid_to_hex(orig ? orig : &null_oid),
 466                            oid_to_hex(oid), "1", NULL);
 467
 468leave_reset_head:
 469        strbuf_release(&msg);
 470        rollback_lock_file(&lock);
 471        while (nr)
 472                free((void *)desc[--nr].buffer);
 473        return ret;
 474}
 475
 476static int move_to_original_branch(struct rebase_options *opts)
 477{
 478        struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
 479        int ret;
 480
 481        if (!opts->head_name)
 482                return 0; /* nothing to move back to */
 483
 484        if (!opts->onto)
 485                BUG("move_to_original_branch without onto");
 486
 487        strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s",
 488                    opts->head_name, oid_to_hex(&opts->onto->object.oid));
 489        strbuf_addf(&head_reflog, "rebase finished: returning to %s",
 490                    opts->head_name);
 491        ret = reset_head(NULL, "", opts->head_name, RESET_HEAD_REFS_ONLY,
 492                         orig_head_reflog.buf, head_reflog.buf);
 493
 494        strbuf_release(&orig_head_reflog);
 495        strbuf_release(&head_reflog);
 496        return ret;
 497}
 498
 499static const char *resolvemsg =
 500N_("Resolve all conflicts manually, mark them as resolved with\n"
 501"\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
 502"You can instead skip this commit: run \"git rebase --skip\".\n"
 503"To abort and get back to the state before \"git rebase\", run "
 504"\"git rebase --abort\".");
 505
 506static int run_am(struct rebase_options *opts)
 507{
 508        struct child_process am = CHILD_PROCESS_INIT;
 509        struct child_process format_patch = CHILD_PROCESS_INIT;
 510        struct strbuf revisions = STRBUF_INIT;
 511        int status;
 512        char *rebased_patches;
 513
 514        am.git_cmd = 1;
 515        argv_array_push(&am.args, "am");
 516
 517        if (opts->action && !strcmp("continue", opts->action)) {
 518                argv_array_push(&am.args, "--resolved");
 519                argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
 520                if (opts->gpg_sign_opt)
 521                        argv_array_push(&am.args, opts->gpg_sign_opt);
 522                status = run_command(&am);
 523                if (status)
 524                        return status;
 525
 526                return move_to_original_branch(opts);
 527        }
 528        if (opts->action && !strcmp("skip", opts->action)) {
 529                argv_array_push(&am.args, "--skip");
 530                argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
 531                status = run_command(&am);
 532                if (status)
 533                        return status;
 534
 535                return move_to_original_branch(opts);
 536        }
 537        if (opts->action && !strcmp("show-current-patch", opts->action)) {
 538                argv_array_push(&am.args, "--show-current-patch");
 539                return run_command(&am);
 540        }
 541
 542        strbuf_addf(&revisions, "%s...%s",
 543                    oid_to_hex(opts->root ?
 544                               /* this is now equivalent to !opts->upstream */
 545                               &opts->onto->object.oid :
 546                               &opts->upstream->object.oid),
 547                    oid_to_hex(&opts->orig_head));
 548
 549        rebased_patches = xstrdup(git_path("rebased-patches"));
 550        format_patch.out = open(rebased_patches,
 551                                O_WRONLY | O_CREAT | O_TRUNC, 0666);
 552        if (format_patch.out < 0) {
 553                status = error_errno(_("could not open '%s' for writing"),
 554                                     rebased_patches);
 555                free(rebased_patches);
 556                argv_array_clear(&am.args);
 557                return status;
 558        }
 559
 560        format_patch.git_cmd = 1;
 561        argv_array_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
 562                         "--full-index", "--cherry-pick", "--right-only",
 563                         "--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
 564                         "--no-cover-letter", "--pretty=mboxrd", "--topo-order", NULL);
 565        if (opts->git_format_patch_opt.len)
 566                argv_array_split(&format_patch.args,
 567                                 opts->git_format_patch_opt.buf);
 568        argv_array_push(&format_patch.args, revisions.buf);
 569        if (opts->restrict_revision)
 570                argv_array_pushf(&format_patch.args, "^%s",
 571                                 oid_to_hex(&opts->restrict_revision->object.oid));
 572
 573        status = run_command(&format_patch);
 574        if (status) {
 575                unlink(rebased_patches);
 576                free(rebased_patches);
 577                argv_array_clear(&am.args);
 578
 579                reset_head(&opts->orig_head, "checkout", opts->head_name, 0,
 580                           "HEAD", NULL);
 581                error(_("\ngit encountered an error while preparing the "
 582                        "patches to replay\n"
 583                        "these revisions:\n"
 584                        "\n    %s\n\n"
 585                        "As a result, git cannot rebase them."),
 586                      opts->revisions);
 587
 588                strbuf_release(&revisions);
 589                return status;
 590        }
 591        strbuf_release(&revisions);
 592
 593        am.in = open(rebased_patches, O_RDONLY);
 594        if (am.in < 0) {
 595                status = error_errno(_("could not open '%s' for reading"),
 596                                     rebased_patches);
 597                free(rebased_patches);
 598                argv_array_clear(&am.args);
 599                return status;
 600        }
 601
 602        argv_array_pushv(&am.args, opts->git_am_opts.argv);
 603        argv_array_push(&am.args, "--rebasing");
 604        argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
 605        argv_array_push(&am.args, "--patch-format=mboxrd");
 606        if (opts->allow_rerere_autoupdate > 0)
 607                argv_array_push(&am.args, "--rerere-autoupdate");
 608        else if (opts->allow_rerere_autoupdate == 0)
 609                argv_array_push(&am.args, "--no-rerere-autoupdate");
 610        if (opts->gpg_sign_opt)
 611                argv_array_push(&am.args, opts->gpg_sign_opt);
 612        status = run_command(&am);
 613        unlink(rebased_patches);
 614        free(rebased_patches);
 615
 616        if (!status) {
 617                return move_to_original_branch(opts);
 618        }
 619
 620        if (is_directory(opts->state_dir))
 621                write_basic_state(opts);
 622
 623        return status;
 624}
 625
 626static int run_specific_rebase(struct rebase_options *opts)
 627{
 628        const char *argv[] = { NULL, NULL };
 629        struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT;
 630        int status;
 631        const char *backend, *backend_func;
 632
 633        if (opts->type == REBASE_INTERACTIVE) {
 634                /* Run builtin interactive rebase */
 635                struct child_process child = CHILD_PROCESS_INIT;
 636
 637                argv_array_pushf(&child.env_array, "GIT_CHERRY_PICK_HELP=%s",
 638                                 resolvemsg);
 639                if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
 640                        argv_array_push(&child.env_array,
 641                                        "GIT_SEQUENCE_EDITOR=:");
 642                        opts->autosquash = 0;
 643                }
 644
 645                child.git_cmd = 1;
 646                argv_array_push(&child.args, "rebase--interactive");
 647
 648                if (opts->action)
 649                        argv_array_pushf(&child.args, "--%s", opts->action);
 650                if (opts->keep_empty)
 651                        argv_array_push(&child.args, "--keep-empty");
 652                if (opts->rebase_merges)
 653                        argv_array_push(&child.args, "--rebase-merges");
 654                if (opts->rebase_cousins)
 655                        argv_array_push(&child.args, "--rebase-cousins");
 656                if (opts->autosquash)
 657                        argv_array_push(&child.args, "--autosquash");
 658                if (opts->flags & REBASE_VERBOSE)
 659                        argv_array_push(&child.args, "--verbose");
 660                if (opts->flags & REBASE_FORCE)
 661                        argv_array_push(&child.args, "--no-ff");
 662                if (opts->restrict_revision)
 663                        argv_array_pushf(&child.args,
 664                                         "--restrict-revision=^%s",
 665                                         oid_to_hex(&opts->restrict_revision->object.oid));
 666                if (opts->upstream)
 667                        argv_array_pushf(&child.args, "--upstream=%s",
 668                                         oid_to_hex(&opts->upstream->object.oid));
 669                if (opts->onto)
 670                        argv_array_pushf(&child.args, "--onto=%s",
 671                                         oid_to_hex(&opts->onto->object.oid));
 672                if (opts->squash_onto)
 673                        argv_array_pushf(&child.args, "--squash-onto=%s",
 674                                         oid_to_hex(opts->squash_onto));
 675                if (opts->onto_name)
 676                        argv_array_pushf(&child.args, "--onto-name=%s",
 677                                         opts->onto_name);
 678                argv_array_pushf(&child.args, "--head-name=%s",
 679                                 opts->head_name ?
 680                                 opts->head_name : "detached HEAD");
 681                if (opts->strategy)
 682                        argv_array_pushf(&child.args, "--strategy=%s",
 683                                         opts->strategy);
 684                if (opts->strategy_opts)
 685                        argv_array_pushf(&child.args, "--strategy-opts=%s",
 686                                         opts->strategy_opts);
 687                if (opts->switch_to)
 688                        argv_array_pushf(&child.args, "--switch-to=%s",
 689                                         opts->switch_to);
 690                if (opts->cmd)
 691                        argv_array_pushf(&child.args, "--cmd=%s", opts->cmd);
 692                if (opts->allow_empty_message)
 693                        argv_array_push(&child.args, "--allow-empty-message");
 694                if (opts->allow_rerere_autoupdate > 0)
 695                        argv_array_push(&child.args, "--rerere-autoupdate");
 696                else if (opts->allow_rerere_autoupdate == 0)
 697                        argv_array_push(&child.args, "--no-rerere-autoupdate");
 698                if (opts->gpg_sign_opt)
 699                        argv_array_push(&child.args, opts->gpg_sign_opt);
 700                if (opts->signoff)
 701                        argv_array_push(&child.args, "--signoff");
 702                if (opts->reschedule_failed_exec)
 703                        argv_array_push(&child.args, "--reschedule-failed-exec");
 704
 705                status = run_command(&child);
 706                goto finished_rebase;
 707        }
 708
 709        if (opts->type == REBASE_AM) {
 710                status = run_am(opts);
 711                goto finished_rebase;
 712        }
 713
 714        add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
 715        add_var(&script_snippet, "state_dir", opts->state_dir);
 716
 717        add_var(&script_snippet, "upstream_name", opts->upstream_name);
 718        add_var(&script_snippet, "upstream", opts->upstream ?
 719                oid_to_hex(&opts->upstream->object.oid) : NULL);
 720        add_var(&script_snippet, "head_name",
 721                opts->head_name ? opts->head_name : "detached HEAD");
 722        add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
 723        add_var(&script_snippet, "onto", opts->onto ?
 724                oid_to_hex(&opts->onto->object.oid) : NULL);
 725        add_var(&script_snippet, "onto_name", opts->onto_name);
 726        add_var(&script_snippet, "revisions", opts->revisions);
 727        add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
 728                oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
 729        add_var(&script_snippet, "GIT_QUIET",
 730                opts->flags & REBASE_NO_QUIET ? "" : "t");
 731        sq_quote_argv_pretty(&buf, opts->git_am_opts.argv);
 732        add_var(&script_snippet, "git_am_opt", buf.buf);
 733        strbuf_release(&buf);
 734        add_var(&script_snippet, "verbose",
 735                opts->flags & REBASE_VERBOSE ? "t" : "");
 736        add_var(&script_snippet, "diffstat",
 737                opts->flags & REBASE_DIFFSTAT ? "t" : "");
 738        add_var(&script_snippet, "force_rebase",
 739                opts->flags & REBASE_FORCE ? "t" : "");
 740        if (opts->switch_to)
 741                add_var(&script_snippet, "switch_to", opts->switch_to);
 742        add_var(&script_snippet, "action", opts->action ? opts->action : "");
 743        add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
 744        add_var(&script_snippet, "allow_rerere_autoupdate",
 745                opts->allow_rerere_autoupdate < 0 ? "" :
 746                opts->allow_rerere_autoupdate ?
 747                "--rerere-autoupdate" : "--no-rerere-autoupdate");
 748        add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
 749        add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
 750        add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
 751        add_var(&script_snippet, "cmd", opts->cmd);
 752        add_var(&script_snippet, "allow_empty_message",
 753                opts->allow_empty_message ?  "--allow-empty-message" : "");
 754        add_var(&script_snippet, "rebase_merges",
 755                opts->rebase_merges ? "t" : "");
 756        add_var(&script_snippet, "rebase_cousins",
 757                opts->rebase_cousins ? "t" : "");
 758        add_var(&script_snippet, "strategy", opts->strategy);
 759        add_var(&script_snippet, "strategy_opts", opts->strategy_opts);
 760        add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
 761        add_var(&script_snippet, "squash_onto",
 762                opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
 763        add_var(&script_snippet, "git_format_patch_opt",
 764                opts->git_format_patch_opt.buf);
 765
 766        if (is_interactive(opts) &&
 767            !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
 768                strbuf_addstr(&script_snippet,
 769                              "GIT_SEQUENCE_EDITOR=:; export GIT_SEQUENCE_EDITOR; ");
 770                opts->autosquash = 0;
 771        }
 772
 773        switch (opts->type) {
 774        case REBASE_AM:
 775                backend = "git-rebase--am";
 776                backend_func = "git_rebase__am";
 777                break;
 778        case REBASE_PRESERVE_MERGES:
 779                backend = "git-rebase--preserve-merges";
 780                backend_func = "git_rebase__preserve_merges";
 781                break;
 782        default:
 783                BUG("Unhandled rebase type %d", opts->type);
 784                break;
 785        }
 786
 787        strbuf_addf(&script_snippet,
 788                    ". git-sh-setup && . git-rebase--common &&"
 789                    " . %s && %s", backend, backend_func);
 790        argv[0] = script_snippet.buf;
 791
 792        status = run_command_v_opt(argv, RUN_USING_SHELL);
 793finished_rebase:
 794        if (opts->dont_finish_rebase)
 795                ; /* do nothing */
 796        else if (opts->type == REBASE_INTERACTIVE)
 797                ; /* interactive rebase cleans up after itself */
 798        else if (status == 0) {
 799                if (!file_exists(state_dir_path("stopped-sha", opts)))
 800                        finish_rebase(opts);
 801        } else if (status == 2) {
 802                struct strbuf dir = STRBUF_INIT;
 803
 804                apply_autostash(opts);
 805                strbuf_addstr(&dir, opts->state_dir);
 806                remove_dir_recursively(&dir, 0);
 807                strbuf_release(&dir);
 808                die("Nothing to do");
 809        }
 810
 811        strbuf_release(&script_snippet);
 812
 813        return status ? -1 : 0;
 814}
 815
 816static int rebase_config(const char *var, const char *value, void *data)
 817{
 818        struct rebase_options *opts = data;
 819
 820        if (!strcmp(var, "rebase.stat")) {
 821                if (git_config_bool(var, value))
 822                        opts->flags |= REBASE_DIFFSTAT;
 823                else
 824                        opts->flags &= !REBASE_DIFFSTAT;
 825                return 0;
 826        }
 827
 828        if (!strcmp(var, "rebase.autosquash")) {
 829                opts->autosquash = git_config_bool(var, value);
 830                return 0;
 831        }
 832
 833        if (!strcmp(var, "commit.gpgsign")) {
 834                free(opts->gpg_sign_opt);
 835                opts->gpg_sign_opt = git_config_bool(var, value) ?
 836                        xstrdup("-S") : NULL;
 837                return 0;
 838        }
 839
 840        if (!strcmp(var, "rebase.autostash")) {
 841                opts->autostash = git_config_bool(var, value);
 842                return 0;
 843        }
 844
 845        if (!strcmp(var, "rebase.reschedulefailedexec")) {
 846                opts->reschedule_failed_exec = git_config_bool(var, value);
 847                return 0;
 848        }
 849
 850        if (!strcmp(var, "rebase.usebuiltin")) {
 851                opts->use_legacy_rebase = !git_config_bool(var, value);
 852                return 0;
 853        }
 854
 855        return git_default_config(var, value, data);
 856}
 857
 858/*
 859 * Determines whether the commits in from..to are linear, i.e. contain
 860 * no merge commits. This function *expects* `from` to be an ancestor of
 861 * `to`.
 862 */
 863static int is_linear_history(struct commit *from, struct commit *to)
 864{
 865        while (to && to != from) {
 866                parse_commit(to);
 867                if (!to->parents)
 868                        return 1;
 869                if (to->parents->next)
 870                        return 0;
 871                to = to->parents->item;
 872        }
 873        return 1;
 874}
 875
 876static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
 877                            struct object_id *merge_base)
 878{
 879        struct commit *head = lookup_commit(the_repository, head_oid);
 880        struct commit_list *merge_bases;
 881        int res;
 882
 883        if (!head)
 884                return 0;
 885
 886        merge_bases = get_merge_bases(onto, head);
 887        if (merge_bases && !merge_bases->next) {
 888                oidcpy(merge_base, &merge_bases->item->object.oid);
 889                res = oideq(merge_base, &onto->object.oid);
 890        } else {
 891                oidcpy(merge_base, &null_oid);
 892                res = 0;
 893        }
 894        free_commit_list(merge_bases);
 895        return res && is_linear_history(onto, head);
 896}
 897
 898/* -i followed by -m is still -i */
 899static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
 900{
 901        struct rebase_options *opts = opt->value;
 902
 903        BUG_ON_OPT_NEG(unset);
 904        BUG_ON_OPT_ARG(arg);
 905
 906        if (!is_interactive(opts))
 907                opts->type = REBASE_MERGE;
 908
 909        return 0;
 910}
 911
 912/* -i followed by -p is still explicitly interactive, but -p alone is not */
 913static int parse_opt_interactive(const struct option *opt, const char *arg,
 914                                 int unset)
 915{
 916        struct rebase_options *opts = opt->value;
 917
 918        BUG_ON_OPT_NEG(unset);
 919        BUG_ON_OPT_ARG(arg);
 920
 921        opts->type = REBASE_INTERACTIVE;
 922        opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
 923
 924        return 0;
 925}
 926
 927static void NORETURN error_on_missing_default_upstream(void)
 928{
 929        struct branch *current_branch = branch_get(NULL);
 930
 931        printf(_("%s\n"
 932                 "Please specify which branch you want to rebase against.\n"
 933                 "See git-rebase(1) for details.\n"
 934                 "\n"
 935                 "    git rebase '<branch>'\n"
 936                 "\n"),
 937                current_branch ? _("There is no tracking information for "
 938                        "the current branch.") :
 939                        _("You are not currently on a branch."));
 940
 941        if (current_branch) {
 942                const char *remote = current_branch->remote_name;
 943
 944                if (!remote)
 945                        remote = _("<remote>");
 946
 947                printf(_("If you wish to set tracking information for this "
 948                         "branch you can do so with:\n"
 949                         "\n"
 950                         "    git branch --set-upstream-to=%s/<branch> %s\n"
 951                         "\n"),
 952                       remote, current_branch->name);
 953        }
 954        exit(1);
 955}
 956
 957static void set_reflog_action(struct rebase_options *options)
 958{
 959        const char *env;
 960        struct strbuf buf = STRBUF_INIT;
 961
 962        if (!is_interactive(options))
 963                return;
 964
 965        env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
 966        if (env && strcmp("rebase", env))
 967                return; /* only override it if it is "rebase" */
 968
 969        strbuf_addf(&buf, "rebase -i (%s)", options->action);
 970        setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1);
 971        strbuf_release(&buf);
 972}
 973
 974static int check_exec_cmd(const char *cmd)
 975{
 976        if (strchr(cmd, '\n'))
 977                return error(_("exec commands cannot contain newlines"));
 978
 979        /* Does the command consist purely of whitespace? */
 980        if (!cmd[strspn(cmd, " \t\r\f\v")])
 981                return error(_("empty exec command"));
 982
 983        return 0;
 984}
 985
 986
 987int cmd_rebase(int argc, const char **argv, const char *prefix)
 988{
 989        struct rebase_options options = {
 990                .type = REBASE_UNSPECIFIED,
 991                .flags = REBASE_NO_QUIET,
 992                .git_am_opts = ARGV_ARRAY_INIT,
 993                .allow_rerere_autoupdate  = -1,
 994                .allow_empty_message = 1,
 995                .git_format_patch_opt = STRBUF_INIT,
 996        };
 997        const char *branch_name;
 998        int ret, flags, total_argc, in_progress = 0;
 999        int ok_to_skip_pre_rebase = 0;
1000        struct strbuf msg = STRBUF_INIT;
1001        struct strbuf revisions = STRBUF_INIT;
1002        struct strbuf buf = STRBUF_INIT;
1003        struct object_id merge_base;
1004        enum {
1005                NO_ACTION,
1006                ACTION_CONTINUE,
1007                ACTION_SKIP,
1008                ACTION_ABORT,
1009                ACTION_QUIT,
1010                ACTION_EDIT_TODO,
1011                ACTION_SHOW_CURRENT_PATCH,
1012        } action = NO_ACTION;
1013        static const char *action_names[] = { N_("undefined"),
1014                                              N_("continue"),
1015                                              N_("skip"),
1016                                              N_("abort"),
1017                                              N_("quit"),
1018                                              N_("edit_todo"),
1019                                              N_("show_current_patch"),
1020                                              NULL };
1021        const char *gpg_sign = NULL;
1022        struct string_list exec = STRING_LIST_INIT_NODUP;
1023        const char *rebase_merges = NULL;
1024        int fork_point = -1;
1025        struct string_list strategy_options = STRING_LIST_INIT_NODUP;
1026        struct object_id squash_onto;
1027        char *squash_onto_name = NULL;
1028        struct option builtin_rebase_options[] = {
1029                OPT_STRING(0, "onto", &options.onto_name,
1030                           N_("revision"),
1031                           N_("rebase onto given branch instead of upstream")),
1032                OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
1033                         N_("allow pre-rebase hook to run")),
1034                OPT_NEGBIT('q', "quiet", &options.flags,
1035                           N_("be quiet. implies --no-stat"),
1036                           REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
1037                OPT_BIT('v', "verbose", &options.flags,
1038                        N_("display a diffstat of what changed upstream"),
1039                        REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1040                {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
1041                        N_("do not show diffstat of what changed upstream"),
1042                        PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
1043                OPT_BOOL(0, "signoff", &options.signoff,
1044                         N_("add a Signed-off-by: line to each commit")),
1045                OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options.git_am_opts,
1046                                  NULL, N_("passed to 'git am'"),
1047                                  PARSE_OPT_NOARG),
1048                OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
1049                                  &options.git_am_opts, NULL,
1050                                  N_("passed to 'git am'"), PARSE_OPT_NOARG),
1051                OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL,
1052                                  N_("passed to 'git am'"), PARSE_OPT_NOARG),
1053                OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
1054                                  N_("passed to 'git apply'"), 0),
1055                OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
1056                                  N_("action"), N_("passed to 'git apply'"), 0),
1057                OPT_BIT('f', "force-rebase", &options.flags,
1058                        N_("cherry-pick all commits, even if unchanged"),
1059                        REBASE_FORCE),
1060                OPT_BIT(0, "no-ff", &options.flags,
1061                        N_("cherry-pick all commits, even if unchanged"),
1062                        REBASE_FORCE),
1063                OPT_CMDMODE(0, "continue", &action, N_("continue"),
1064                            ACTION_CONTINUE),
1065                OPT_CMDMODE(0, "skip", &action,
1066                            N_("skip current patch and continue"), ACTION_SKIP),
1067                OPT_CMDMODE(0, "abort", &action,
1068                            N_("abort and check out the original branch"),
1069                            ACTION_ABORT),
1070                OPT_CMDMODE(0, "quit", &action,
1071                            N_("abort but keep HEAD where it is"), ACTION_QUIT),
1072                OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
1073                            "during an interactive rebase"), ACTION_EDIT_TODO),
1074                OPT_CMDMODE(0, "show-current-patch", &action,
1075                            N_("show the patch file being applied or merged"),
1076                            ACTION_SHOW_CURRENT_PATCH),
1077                { OPTION_CALLBACK, 'm', "merge", &options, NULL,
1078                        N_("use merging strategies to rebase"),
1079                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1080                        parse_opt_merge },
1081                { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
1082                        N_("let the user edit the list of commits to rebase"),
1083                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1084                        parse_opt_interactive },
1085                OPT_SET_INT('p', "preserve-merges", &options.type,
1086                            N_("try to recreate merges instead of ignoring "
1087                               "them"), REBASE_PRESERVE_MERGES),
1088                OPT_BOOL(0, "rerere-autoupdate",
1089                         &options.allow_rerere_autoupdate,
1090                         N_("allow rerere to update index with resolved "
1091                            "conflict")),
1092                OPT_BOOL('k', "keep-empty", &options.keep_empty,
1093                         N_("preserve empty commits during rebase")),
1094                OPT_BOOL(0, "autosquash", &options.autosquash,
1095                         N_("move commits that begin with "
1096                            "squash!/fixup! under -i")),
1097                { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
1098                        N_("GPG-sign commits"),
1099                        PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1100                OPT_BOOL(0, "autostash", &options.autostash,
1101                         N_("automatically stash/stash pop before and after")),
1102                OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
1103                                N_("add exec lines after each commit of the "
1104                                   "editable list")),
1105                OPT_BOOL(0, "allow-empty-message",
1106                         &options.allow_empty_message,
1107                         N_("allow rebasing commits with empty messages")),
1108                {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
1109                        N_("mode"),
1110                        N_("try to rebase merges instead of skipping them"),
1111                        PARSE_OPT_OPTARG, NULL, (intptr_t)""},
1112                OPT_BOOL(0, "fork-point", &fork_point,
1113                         N_("use 'merge-base --fork-point' to refine upstream")),
1114                OPT_STRING('s', "strategy", &options.strategy,
1115                           N_("strategy"), N_("use the given merge strategy")),
1116                OPT_STRING_LIST('X', "strategy-option", &strategy_options,
1117                                N_("option"),
1118                                N_("pass the argument through to the merge "
1119                                   "strategy")),
1120                OPT_BOOL(0, "root", &options.root,
1121                         N_("rebase all reachable commits up to the root(s)")),
1122                OPT_BOOL(0, "reschedule-failed-exec",
1123                         &options.reschedule_failed_exec,
1124                         N_("automatically re-schedule any `exec` that fails")),
1125                OPT_END(),
1126        };
1127        int i;
1128
1129        if (argc == 2 && !strcmp(argv[1], "-h"))
1130                usage_with_options(builtin_rebase_usage,
1131                                   builtin_rebase_options);
1132
1133        prefix = setup_git_directory();
1134        trace_repo_setup(prefix);
1135        setup_work_tree();
1136
1137        git_config(rebase_config, &options);
1138
1139        if (options.use_legacy_rebase ||
1140            !git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1))
1141                warning(_("the rebase.useBuiltin support has been removed!\n"
1142                          "See its entry in 'git help config' for details."));
1143
1144        strbuf_reset(&buf);
1145        strbuf_addf(&buf, "%s/applying", apply_dir());
1146        if(file_exists(buf.buf))
1147                die(_("It looks like 'git am' is in progress. Cannot rebase."));
1148
1149        if (is_directory(apply_dir())) {
1150                options.type = REBASE_AM;
1151                options.state_dir = apply_dir();
1152        } else if (is_directory(merge_dir())) {
1153                strbuf_reset(&buf);
1154                strbuf_addf(&buf, "%s/rewritten", merge_dir());
1155                if (is_directory(buf.buf)) {
1156                        options.type = REBASE_PRESERVE_MERGES;
1157                        options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1158                } else {
1159                        strbuf_reset(&buf);
1160                        strbuf_addf(&buf, "%s/interactive", merge_dir());
1161                        if(file_exists(buf.buf)) {
1162                                options.type = REBASE_INTERACTIVE;
1163                                options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1164                        } else
1165                                options.type = REBASE_MERGE;
1166                }
1167                options.state_dir = merge_dir();
1168        }
1169
1170        if (options.type != REBASE_UNSPECIFIED)
1171                in_progress = 1;
1172
1173        total_argc = argc;
1174        argc = parse_options(argc, argv, prefix,
1175                             builtin_rebase_options,
1176                             builtin_rebase_usage, 0);
1177
1178        if (action != NO_ACTION && total_argc != 2) {
1179                usage_with_options(builtin_rebase_usage,
1180                                   builtin_rebase_options);
1181        }
1182
1183        if (argc > 2)
1184                usage_with_options(builtin_rebase_usage,
1185                                   builtin_rebase_options);
1186
1187        if (action != NO_ACTION && !in_progress)
1188                die(_("No rebase in progress?"));
1189        setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0);
1190
1191        if (action == ACTION_EDIT_TODO && !is_interactive(&options))
1192                die(_("The --edit-todo action can only be used during "
1193                      "interactive rebase."));
1194
1195        if (trace2_is_enabled()) {
1196                if (is_interactive(&options))
1197                        trace2_cmd_mode("interactive");
1198                else if (exec.nr)
1199                        trace2_cmd_mode("interactive-exec");
1200                else
1201                        trace2_cmd_mode(action_names[action]);
1202        }
1203
1204        switch (action) {
1205        case ACTION_CONTINUE: {
1206                struct object_id head;
1207                struct lock_file lock_file = LOCK_INIT;
1208                int fd;
1209
1210                options.action = "continue";
1211                set_reflog_action(&options);
1212
1213                /* Sanity check */
1214                if (get_oid("HEAD", &head))
1215                        die(_("Cannot read HEAD"));
1216
1217                fd = hold_locked_index(&lock_file, 0);
1218                if (repo_read_index(the_repository) < 0)
1219                        die(_("could not read index"));
1220                refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1221                              NULL);
1222                if (0 <= fd)
1223                        repo_update_index_if_able(the_repository, &lock_file);
1224                rollback_lock_file(&lock_file);
1225
1226                if (has_unstaged_changes(the_repository, 1)) {
1227                        puts(_("You must edit all merge conflicts and then\n"
1228                               "mark them as resolved using git add"));
1229                        exit(1);
1230                }
1231                if (read_basic_state(&options))
1232                        exit(1);
1233                goto run_rebase;
1234        }
1235        case ACTION_SKIP: {
1236                struct string_list merge_rr = STRING_LIST_INIT_DUP;
1237
1238                options.action = "skip";
1239                set_reflog_action(&options);
1240
1241                rerere_clear(the_repository, &merge_rr);
1242                string_list_clear(&merge_rr, 1);
1243
1244                if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1245                               NULL, NULL) < 0)
1246                        die(_("could not discard worktree changes"));
1247                remove_branch_state(the_repository);
1248                if (read_basic_state(&options))
1249                        exit(1);
1250                goto run_rebase;
1251        }
1252        case ACTION_ABORT: {
1253                struct string_list merge_rr = STRING_LIST_INIT_DUP;
1254                options.action = "abort";
1255                set_reflog_action(&options);
1256
1257                rerere_clear(the_repository, &merge_rr);
1258                string_list_clear(&merge_rr, 1);
1259
1260                if (read_basic_state(&options))
1261                        exit(1);
1262                if (reset_head(&options.orig_head, "reset",
1263                               options.head_name, RESET_HEAD_HARD,
1264                               NULL, NULL) < 0)
1265                        die(_("could not move back to %s"),
1266                            oid_to_hex(&options.orig_head));
1267                remove_branch_state(the_repository);
1268                ret = finish_rebase(&options);
1269                goto cleanup;
1270        }
1271        case ACTION_QUIT: {
1272                strbuf_reset(&buf);
1273                strbuf_addstr(&buf, options.state_dir);
1274                ret = !!remove_dir_recursively(&buf, 0);
1275                if (ret)
1276                        die(_("could not remove '%s'"), options.state_dir);
1277                goto cleanup;
1278        }
1279        case ACTION_EDIT_TODO:
1280                options.action = "edit-todo";
1281                options.dont_finish_rebase = 1;
1282                goto run_rebase;
1283        case ACTION_SHOW_CURRENT_PATCH:
1284                options.action = "show-current-patch";
1285                options.dont_finish_rebase = 1;
1286                goto run_rebase;
1287        case NO_ACTION:
1288                break;
1289        default:
1290                BUG("action: %d", action);
1291        }
1292
1293        /* Make sure no rebase is in progress */
1294        if (in_progress) {
1295                const char *last_slash = strrchr(options.state_dir, '/');
1296                const char *state_dir_base =
1297                        last_slash ? last_slash + 1 : options.state_dir;
1298                const char *cmd_live_rebase =
1299                        "git rebase (--continue | --abort | --skip)";
1300                strbuf_reset(&buf);
1301                strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1302                die(_("It seems that there is already a %s directory, and\n"
1303                      "I wonder if you are in the middle of another rebase.  "
1304                      "If that is the\n"
1305                      "case, please try\n\t%s\n"
1306                      "If that is not the case, please\n\t%s\n"
1307                      "and run me again.  I am stopping in case you still "
1308                      "have something\n"
1309                      "valuable there.\n"),
1310                    state_dir_base, cmd_live_rebase, buf.buf);
1311        }
1312
1313        for (i = 0; i < options.git_am_opts.argc; i++) {
1314                const char *option = options.git_am_opts.argv[i], *p;
1315                if (!strcmp(option, "--committer-date-is-author-date") ||
1316                    !strcmp(option, "--ignore-date") ||
1317                    !strcmp(option, "--whitespace=fix") ||
1318                    !strcmp(option, "--whitespace=strip"))
1319                        options.flags |= REBASE_FORCE;
1320                else if (skip_prefix(option, "-C", &p)) {
1321                        while (*p)
1322                                if (!isdigit(*(p++)))
1323                                        die(_("switch `C' expects a "
1324                                              "numerical value"));
1325                } else if (skip_prefix(option, "--whitespace=", &p)) {
1326                        if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1327                            strcmp(p, "error") && strcmp(p, "error-all"))
1328                                die("Invalid whitespace option: '%s'", p);
1329                }
1330        }
1331
1332        for (i = 0; i < exec.nr; i++)
1333                if (check_exec_cmd(exec.items[i].string))
1334                        exit(1);
1335
1336        if (!(options.flags & REBASE_NO_QUIET))
1337                argv_array_push(&options.git_am_opts, "-q");
1338
1339        if (options.keep_empty)
1340                imply_interactive(&options, "--keep-empty");
1341
1342        if (gpg_sign) {
1343                free(options.gpg_sign_opt);
1344                options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1345        }
1346
1347        if (exec.nr) {
1348                int i;
1349
1350                imply_interactive(&options, "--exec");
1351
1352                strbuf_reset(&buf);
1353                for (i = 0; i < exec.nr; i++)
1354                        strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1355                options.cmd = xstrdup(buf.buf);
1356        }
1357
1358        if (rebase_merges) {
1359                if (!*rebase_merges)
1360                        ; /* default mode; do nothing */
1361                else if (!strcmp("rebase-cousins", rebase_merges))
1362                        options.rebase_cousins = 1;
1363                else if (strcmp("no-rebase-cousins", rebase_merges))
1364                        die(_("Unknown mode: %s"), rebase_merges);
1365                options.rebase_merges = 1;
1366                imply_interactive(&options, "--rebase-merges");
1367        }
1368
1369        if (strategy_options.nr) {
1370                int i;
1371
1372                if (!options.strategy)
1373                        options.strategy = "recursive";
1374
1375                strbuf_reset(&buf);
1376                for (i = 0; i < strategy_options.nr; i++)
1377                        strbuf_addf(&buf, " --%s",
1378                                    strategy_options.items[i].string);
1379                options.strategy_opts = xstrdup(buf.buf);
1380        }
1381
1382        if (options.strategy) {
1383                options.strategy = xstrdup(options.strategy);
1384                switch (options.type) {
1385                case REBASE_AM:
1386                        die(_("--strategy requires --merge or --interactive"));
1387                case REBASE_MERGE:
1388                case REBASE_INTERACTIVE:
1389                case REBASE_PRESERVE_MERGES:
1390                        /* compatible */
1391                        break;
1392                case REBASE_UNSPECIFIED:
1393                        options.type = REBASE_MERGE;
1394                        break;
1395                default:
1396                        BUG("unhandled rebase type (%d)", options.type);
1397                }
1398        }
1399
1400        if (options.type == REBASE_MERGE)
1401                imply_interactive(&options, "--merge");
1402
1403        if (options.root && !options.onto_name)
1404                imply_interactive(&options, "--root without --onto");
1405
1406        if (isatty(2) && options.flags & REBASE_NO_QUIET)
1407                strbuf_addstr(&options.git_format_patch_opt, " --progress");
1408
1409        switch (options.type) {
1410        case REBASE_MERGE:
1411        case REBASE_INTERACTIVE:
1412        case REBASE_PRESERVE_MERGES:
1413                options.state_dir = merge_dir();
1414                break;
1415        case REBASE_AM:
1416                options.state_dir = apply_dir();
1417                break;
1418        default:
1419                /* the default rebase backend is `--am` */
1420                options.type = REBASE_AM;
1421                options.state_dir = apply_dir();
1422                break;
1423        }
1424
1425        if (options.reschedule_failed_exec && !is_interactive(&options))
1426                die(_("%s requires an interactive rebase"), "--reschedule-failed-exec");
1427
1428        if (options.git_am_opts.argc) {
1429                /* all am options except -q are compatible only with --am */
1430                for (i = options.git_am_opts.argc - 1; i >= 0; i--)
1431                        if (strcmp(options.git_am_opts.argv[i], "-q"))
1432                                break;
1433
1434                if (is_interactive(&options) && i >= 0)
1435                        die(_("cannot combine am options with either "
1436                              "interactive or merge options"));
1437        }
1438
1439        if (options.signoff) {
1440                if (options.type == REBASE_PRESERVE_MERGES)
1441                        die("cannot combine '--signoff' with "
1442                            "'--preserve-merges'");
1443                argv_array_push(&options.git_am_opts, "--signoff");
1444                options.flags |= REBASE_FORCE;
1445        }
1446
1447        if (options.type == REBASE_PRESERVE_MERGES) {
1448                /*
1449                 * Note: incompatibility with --signoff handled in signoff block above
1450                 * Note: incompatibility with --interactive is just a strong warning;
1451                 *       git-rebase.txt caveats with "unless you know what you are doing"
1452                 */
1453                if (options.rebase_merges)
1454                        die(_("cannot combine '--preserve-merges' with "
1455                              "'--rebase-merges'"));
1456
1457                if (options.reschedule_failed_exec)
1458                        die(_("error: cannot combine '--preserve-merges' with "
1459                              "'--reschedule-failed-exec'"));
1460        }
1461
1462        if (options.rebase_merges) {
1463                if (strategy_options.nr)
1464                        die(_("cannot combine '--rebase-merges' with "
1465                              "'--strategy-option'"));
1466                if (options.strategy)
1467                        die(_("cannot combine '--rebase-merges' with "
1468                              "'--strategy'"));
1469        }
1470
1471        if (!options.root) {
1472                if (argc < 1) {
1473                        struct branch *branch;
1474
1475                        branch = branch_get(NULL);
1476                        options.upstream_name = branch_get_upstream(branch,
1477                                                                    NULL);
1478                        if (!options.upstream_name)
1479                                error_on_missing_default_upstream();
1480                        if (fork_point < 0)
1481                                fork_point = 1;
1482                } else {
1483                        options.upstream_name = argv[0];
1484                        argc--;
1485                        argv++;
1486                        if (!strcmp(options.upstream_name, "-"))
1487                                options.upstream_name = "@{-1}";
1488                }
1489                options.upstream = peel_committish(options.upstream_name);
1490                if (!options.upstream)
1491                        die(_("invalid upstream '%s'"), options.upstream_name);
1492                options.upstream_arg = options.upstream_name;
1493        } else {
1494                if (!options.onto_name) {
1495                        if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1496                                        &squash_onto, NULL, NULL) < 0)
1497                                die(_("Could not create new root commit"));
1498                        options.squash_onto = &squash_onto;
1499                        options.onto_name = squash_onto_name =
1500                                xstrdup(oid_to_hex(&squash_onto));
1501                }
1502                options.upstream_name = NULL;
1503                options.upstream = NULL;
1504                if (argc > 1)
1505                        usage_with_options(builtin_rebase_usage,
1506                                           builtin_rebase_options);
1507                options.upstream_arg = "--root";
1508        }
1509
1510        /* Make sure the branch to rebase onto is valid. */
1511        if (!options.onto_name)
1512                options.onto_name = options.upstream_name;
1513        if (strstr(options.onto_name, "...")) {
1514                if (get_oid_mb(options.onto_name, &merge_base) < 0)
1515                        die(_("'%s': need exactly one merge base"),
1516                            options.onto_name);
1517                options.onto = lookup_commit_or_die(&merge_base,
1518                                                    options.onto_name);
1519        } else {
1520                options.onto = peel_committish(options.onto_name);
1521                if (!options.onto)
1522                        die(_("Does not point to a valid commit '%s'"),
1523                                options.onto_name);
1524        }
1525
1526        /*
1527         * If the branch to rebase is given, that is the branch we will rebase
1528         * branch_name -- branch/commit being rebased, or
1529         *                HEAD (already detached)
1530         * orig_head -- commit object name of tip of the branch before rebasing
1531         * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1532         */
1533        if (argc == 1) {
1534                /* Is it "rebase other branchname" or "rebase other commit"? */
1535                branch_name = argv[0];
1536                options.switch_to = argv[0];
1537
1538                /* Is it a local branch? */
1539                strbuf_reset(&buf);
1540                strbuf_addf(&buf, "refs/heads/%s", branch_name);
1541                if (!read_ref(buf.buf, &options.orig_head))
1542                        options.head_name = xstrdup(buf.buf);
1543                /* If not is it a valid ref (branch or commit)? */
1544                else if (!get_oid(branch_name, &options.orig_head))
1545                        options.head_name = NULL;
1546                else
1547                        die(_("fatal: no such branch/commit '%s'"),
1548                            branch_name);
1549        } else if (argc == 0) {
1550                /* Do not need to switch branches, we are already on it. */
1551                options.head_name =
1552                        xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1553                                         &flags));
1554                if (!options.head_name)
1555                        die(_("No such ref: %s"), "HEAD");
1556                if (flags & REF_ISSYMREF) {
1557                        if (!skip_prefix(options.head_name,
1558                                         "refs/heads/", &branch_name))
1559                                branch_name = options.head_name;
1560
1561                } else {
1562                        free(options.head_name);
1563                        options.head_name = NULL;
1564                        branch_name = "HEAD";
1565                }
1566                if (get_oid("HEAD", &options.orig_head))
1567                        die(_("Could not resolve HEAD to a revision"));
1568        } else
1569                BUG("unexpected number of arguments left to parse");
1570
1571        if (fork_point > 0) {
1572                struct commit *head =
1573                        lookup_commit_reference(the_repository,
1574                                                &options.orig_head);
1575                options.restrict_revision =
1576                        get_fork_point(options.upstream_name, head);
1577        }
1578
1579        if (repo_read_index(the_repository) < 0)
1580                die(_("could not read index"));
1581
1582        if (options.autostash) {
1583                struct lock_file lock_file = LOCK_INIT;
1584                int fd;
1585
1586                fd = hold_locked_index(&lock_file, 0);
1587                refresh_cache(REFRESH_QUIET);
1588                if (0 <= fd)
1589                        repo_update_index_if_able(the_repository, &lock_file);
1590                rollback_lock_file(&lock_file);
1591
1592                if (has_unstaged_changes(the_repository, 1) ||
1593                    has_uncommitted_changes(the_repository, 1)) {
1594                        const char *autostash =
1595                                state_dir_path("autostash", &options);
1596                        struct child_process stash = CHILD_PROCESS_INIT;
1597                        struct object_id oid;
1598                        struct commit *head =
1599                                lookup_commit_reference(the_repository,
1600                                                        &options.orig_head);
1601
1602                        argv_array_pushl(&stash.args,
1603                                         "stash", "create", "autostash", NULL);
1604                        stash.git_cmd = 1;
1605                        stash.no_stdin = 1;
1606                        strbuf_reset(&buf);
1607                        if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1608                                die(_("Cannot autostash"));
1609                        strbuf_trim_trailing_newline(&buf);
1610                        if (get_oid(buf.buf, &oid))
1611                                die(_("Unexpected stash response: '%s'"),
1612                                    buf.buf);
1613                        strbuf_reset(&buf);
1614                        strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1615
1616                        if (safe_create_leading_directories_const(autostash))
1617                                die(_("Could not create directory for '%s'"),
1618                                    options.state_dir);
1619                        write_file(autostash, "%s", oid_to_hex(&oid));
1620                        printf(_("Created autostash: %s\n"), buf.buf);
1621                        if (reset_head(&head->object.oid, "reset --hard",
1622                                       NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
1623                                die(_("could not reset --hard"));
1624                        printf(_("HEAD is now at %s"),
1625                               find_unique_abbrev(&head->object.oid,
1626                                                  DEFAULT_ABBREV));
1627                        strbuf_reset(&buf);
1628                        pp_commit_easy(CMIT_FMT_ONELINE, head, &buf);
1629                        if (buf.len > 0)
1630                                printf(" %s", buf.buf);
1631                        putchar('\n');
1632
1633                        if (discard_index(the_repository->index) < 0 ||
1634                                repo_read_index(the_repository) < 0)
1635                                die(_("could not read index"));
1636                }
1637        }
1638
1639        if (require_clean_work_tree(the_repository, "rebase",
1640                                    _("Please commit or stash them."), 1, 1)) {
1641                ret = 1;
1642                goto cleanup;
1643        }
1644
1645        /*
1646         * Now we are rebasing commits upstream..orig_head (or with --root,
1647         * everything leading up to orig_head) on top of onto.
1648         */
1649
1650        /*
1651         * Check if we are already based on onto with linear history,
1652         * but this should be done only when upstream and onto are the same
1653         * and if this is not an interactive rebase.
1654         */
1655        if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
1656            !is_interactive(&options) && !options.restrict_revision &&
1657            options.upstream &&
1658            !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1659                int flag;
1660
1661                if (!(options.flags & REBASE_FORCE)) {
1662                        /* Lazily switch to the target branch if needed... */
1663                        if (options.switch_to) {
1664                                struct object_id oid;
1665
1666                                if (get_oid(options.switch_to, &oid) < 0) {
1667                                        ret = !!error(_("could not parse '%s'"),
1668                                                      options.switch_to);
1669                                        goto cleanup;
1670                                }
1671
1672                                strbuf_reset(&buf);
1673                                strbuf_addf(&buf, "%s: checkout %s",
1674                                            getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
1675                                            options.switch_to);
1676                                if (reset_head(&oid, "checkout",
1677                                               options.head_name,
1678                                               RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1679                                               NULL, buf.buf) < 0) {
1680                                        ret = !!error(_("could not switch to "
1681                                                        "%s"),
1682                                                      options.switch_to);
1683                                        goto cleanup;
1684                                }
1685                        }
1686
1687                        if (!(options.flags & REBASE_NO_QUIET))
1688                                ; /* be quiet */
1689                        else if (!strcmp(branch_name, "HEAD") &&
1690                                 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1691                                puts(_("HEAD is up to date."));
1692                        else
1693                                printf(_("Current branch %s is up to date.\n"),
1694                                       branch_name);
1695                        ret = !!finish_rebase(&options);
1696                        goto cleanup;
1697                } else if (!(options.flags & REBASE_NO_QUIET))
1698                        ; /* be quiet */
1699                else if (!strcmp(branch_name, "HEAD") &&
1700                         resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1701                        puts(_("HEAD is up to date, rebase forced."));
1702                else
1703                        printf(_("Current branch %s is up to date, rebase "
1704                                 "forced.\n"), branch_name);
1705        }
1706
1707        /* If a hook exists, give it a chance to interrupt*/
1708        if (!ok_to_skip_pre_rebase &&
1709            run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1710                        argc ? argv[0] : NULL, NULL))
1711                die(_("The pre-rebase hook refused to rebase."));
1712
1713        if (options.flags & REBASE_DIFFSTAT) {
1714                struct diff_options opts;
1715
1716                if (options.flags & REBASE_VERBOSE) {
1717                        if (is_null_oid(&merge_base))
1718                                printf(_("Changes to %s:\n"),
1719                                       oid_to_hex(&options.onto->object.oid));
1720                        else
1721                                printf(_("Changes from %s to %s:\n"),
1722                                       oid_to_hex(&merge_base),
1723                                       oid_to_hex(&options.onto->object.oid));
1724                }
1725
1726                /* We want color (if set), but no pager */
1727                diff_setup(&opts);
1728                opts.stat_width = -1; /* use full terminal width */
1729                opts.stat_graph_width = -1; /* respect statGraphWidth config */
1730                opts.output_format |=
1731                        DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1732                opts.detect_rename = DIFF_DETECT_RENAME;
1733                diff_setup_done(&opts);
1734                diff_tree_oid(is_null_oid(&merge_base) ?
1735                              the_hash_algo->empty_tree : &merge_base,
1736                              &options.onto->object.oid, "", &opts);
1737                diffcore_std(&opts);
1738                diff_flush(&opts);
1739        }
1740
1741        if (is_interactive(&options))
1742                goto run_rebase;
1743
1744        /* Detach HEAD and reset the tree */
1745        if (options.flags & REBASE_NO_QUIET)
1746                printf(_("First, rewinding head to replay your work on top of "
1747                         "it...\n"));
1748
1749        strbuf_addf(&msg, "%s: checkout %s",
1750                    getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
1751        if (reset_head(&options.onto->object.oid, "checkout", NULL,
1752                       RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1753                       NULL, msg.buf))
1754                die(_("Could not detach HEAD"));
1755        strbuf_release(&msg);
1756
1757        /*
1758         * If the onto is a proper descendant of the tip of the branch, then
1759         * we just fast-forwarded.
1760         */
1761        strbuf_reset(&msg);
1762        if (!oidcmp(&merge_base, &options.orig_head)) {
1763                printf(_("Fast-forwarded %s to %s.\n"),
1764                        branch_name, options.onto_name);
1765                strbuf_addf(&msg, "rebase finished: %s onto %s",
1766                        options.head_name ? options.head_name : "detached HEAD",
1767                        oid_to_hex(&options.onto->object.oid));
1768                reset_head(NULL, "Fast-forwarded", options.head_name, 0,
1769                           "HEAD", msg.buf);
1770                strbuf_release(&msg);
1771                ret = !!finish_rebase(&options);
1772                goto cleanup;
1773        }
1774
1775        strbuf_addf(&revisions, "%s..%s",
1776                    options.root ? oid_to_hex(&options.onto->object.oid) :
1777                    (options.restrict_revision ?
1778                     oid_to_hex(&options.restrict_revision->object.oid) :
1779                     oid_to_hex(&options.upstream->object.oid)),
1780                    oid_to_hex(&options.orig_head));
1781
1782        options.revisions = revisions.buf;
1783
1784run_rebase:
1785        ret = !!run_specific_rebase(&options);
1786
1787cleanup:
1788        strbuf_release(&revisions);
1789        free(options.head_name);
1790        free(options.gpg_sign_opt);
1791        free(options.cmd);
1792        free(squash_onto_name);
1793        return ret;
1794}