afc75fe7314e6d23f4c9c36138eede77064d804c
   1/*
   2 * "git rebase" builtin command
   3 *
   4 * Copyright (c) 2018 Pratik Karki
   5 */
   6
   7#include "builtin.h"
   8#include "run-command.h"
   9#include "exec-cmd.h"
  10#include "argv-array.h"
  11#include "dir.h"
  12#include "packfile.h"
  13#include "refs.h"
  14#include "quote.h"
  15#include "config.h"
  16#include "cache-tree.h"
  17#include "unpack-trees.h"
  18#include "lockfile.h"
  19#include "parse-options.h"
  20#include "commit.h"
  21#include "diff.h"
  22#include "wt-status.h"
  23#include "revision.h"
  24
  25static char const * const builtin_rebase_usage[] = {
  26        N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
  27                "[<upstream>] [<branch>]"),
  28        N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
  29                "--root [<branch>]"),
  30        N_("git rebase --continue | --abort | --skip | --edit-todo"),
  31        NULL
  32};
  33
  34static GIT_PATH_FUNC(apply_dir, "rebase-apply")
  35static GIT_PATH_FUNC(merge_dir, "rebase-merge")
  36
  37enum rebase_type {
  38        REBASE_UNSPECIFIED = -1,
  39        REBASE_AM,
  40        REBASE_MERGE,
  41        REBASE_INTERACTIVE,
  42        REBASE_PRESERVE_MERGES
  43};
  44
  45static int use_builtin_rebase(void)
  46{
  47        struct child_process cp = CHILD_PROCESS_INIT;
  48        struct strbuf out = STRBUF_INIT;
  49        int ret;
  50
  51        argv_array_pushl(&cp.args,
  52                         "config", "--bool", "rebase.usebuiltin", NULL);
  53        cp.git_cmd = 1;
  54        if (capture_command(&cp, &out, 6)) {
  55                strbuf_release(&out);
  56                return 0;
  57        }
  58
  59        strbuf_trim(&out);
  60        ret = !strcmp("true", out.buf);
  61        strbuf_release(&out);
  62        return ret;
  63}
  64
  65static int apply_autostash(void)
  66{
  67        warning("TODO");
  68        return 0;
  69}
  70
  71struct rebase_options {
  72        enum rebase_type type;
  73        const char *state_dir;
  74        struct commit *upstream;
  75        const char *upstream_name;
  76        const char *upstream_arg;
  77        char *head_name;
  78        struct object_id orig_head;
  79        struct commit *onto;
  80        const char *onto_name;
  81        const char *revisions;
  82        int root;
  83        struct commit *restrict_revision;
  84        int dont_finish_rebase;
  85        enum {
  86                REBASE_NO_QUIET = 1<<0,
  87                REBASE_VERBOSE = 1<<1,
  88                REBASE_DIFFSTAT = 1<<2,
  89                REBASE_FORCE = 1<<3,
  90                REBASE_INTERACTIVE_EXPLICIT = 1<<4,
  91        } flags;
  92        struct strbuf git_am_opt;
  93};
  94
  95static int is_interactive(struct rebase_options *opts)
  96{
  97        return opts->type == REBASE_INTERACTIVE ||
  98                opts->type == REBASE_PRESERVE_MERGES;
  99}
 100
 101/* Returns the filename prefixed by the state_dir */
 102static const char *state_dir_path(const char *filename, struct rebase_options *opts)
 103{
 104        static struct strbuf path = STRBUF_INIT;
 105        static size_t prefix_len;
 106
 107        if (!prefix_len) {
 108                strbuf_addf(&path, "%s/", opts->state_dir);
 109                prefix_len = path.len;
 110        }
 111
 112        strbuf_setlen(&path, prefix_len);
 113        strbuf_addstr(&path, filename);
 114        return path.buf;
 115}
 116
 117static int finish_rebase(struct rebase_options *opts)
 118{
 119        struct strbuf dir = STRBUF_INIT;
 120        const char *argv_gc_auto[] = { "gc", "--auto", NULL };
 121
 122        delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
 123        apply_autostash();
 124        close_all_packs(the_repository->objects);
 125        /*
 126         * We ignore errors in 'gc --auto', since the
 127         * user should see them.
 128         */
 129        run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
 130        strbuf_addstr(&dir, opts->state_dir);
 131        remove_dir_recursively(&dir, 0);
 132        strbuf_release(&dir);
 133
 134        return 0;
 135}
 136
 137static struct commit *peel_committish(const char *name)
 138{
 139        struct object *obj;
 140        struct object_id oid;
 141
 142        if (get_oid(name, &oid))
 143                return NULL;
 144        obj = parse_object(the_repository, &oid);
 145        return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
 146}
 147
 148static void add_var(struct strbuf *buf, const char *name, const char *value)
 149{
 150        if (!value)
 151                strbuf_addf(buf, "unset %s; ", name);
 152        else {
 153                strbuf_addf(buf, "%s=", name);
 154                sq_quote_buf(buf, value);
 155                strbuf_addstr(buf, "; ");
 156        }
 157}
 158
 159static int run_specific_rebase(struct rebase_options *opts)
 160{
 161        const char *argv[] = { NULL, NULL };
 162        struct strbuf script_snippet = STRBUF_INIT;
 163        int status;
 164        const char *backend, *backend_func;
 165
 166        add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
 167        add_var(&script_snippet, "state_dir", opts->state_dir);
 168
 169        add_var(&script_snippet, "upstream_name", opts->upstream_name);
 170        add_var(&script_snippet, "upstream",
 171                                 oid_to_hex(&opts->upstream->object.oid));
 172        add_var(&script_snippet, "head_name",
 173                opts->head_name ? opts->head_name : "detached HEAD");
 174        add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
 175        add_var(&script_snippet, "onto", oid_to_hex(&opts->onto->object.oid));
 176        add_var(&script_snippet, "onto_name", opts->onto_name);
 177        add_var(&script_snippet, "revisions", opts->revisions);
 178        add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
 179                oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
 180        add_var(&script_snippet, "GIT_QUIET",
 181                opts->flags & REBASE_NO_QUIET ? "" : "t");
 182        add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
 183        add_var(&script_snippet, "verbose",
 184                opts->flags & REBASE_VERBOSE ? "t" : "");
 185        add_var(&script_snippet, "diffstat",
 186                opts->flags & REBASE_DIFFSTAT ? "t" : "");
 187        add_var(&script_snippet, "force_rebase",
 188                opts->flags & REBASE_FORCE ? "t" : "");
 189
 190        switch (opts->type) {
 191        case REBASE_AM:
 192                backend = "git-rebase--am";
 193                backend_func = "git_rebase__am";
 194                break;
 195        case REBASE_INTERACTIVE:
 196                backend = "git-rebase--interactive";
 197                backend_func = "git_rebase__interactive";
 198                break;
 199        case REBASE_MERGE:
 200                backend = "git-rebase--merge";
 201                backend_func = "git_rebase__merge";
 202                break;
 203        case REBASE_PRESERVE_MERGES:
 204                backend = "git-rebase--preserve-merges";
 205                backend_func = "git_rebase__preserve_merges";
 206                break;
 207        default:
 208                BUG("Unhandled rebase type %d", opts->type);
 209                break;
 210        }
 211
 212        strbuf_addf(&script_snippet,
 213                    ". git-sh-setup && . git-rebase--common &&"
 214                    " . %s && %s", backend, backend_func);
 215        argv[0] = script_snippet.buf;
 216
 217        status = run_command_v_opt(argv, RUN_USING_SHELL);
 218        if (opts->dont_finish_rebase)
 219                ; /* do nothing */
 220        else if (status == 0) {
 221                if (!file_exists(state_dir_path("stopped-sha", opts)))
 222                        finish_rebase(opts);
 223        } else if (status == 2) {
 224                struct strbuf dir = STRBUF_INIT;
 225
 226                apply_autostash();
 227                strbuf_addstr(&dir, opts->state_dir);
 228                remove_dir_recursively(&dir, 0);
 229                strbuf_release(&dir);
 230                die("Nothing to do");
 231        }
 232
 233        strbuf_release(&script_snippet);
 234
 235        return status ? -1 : 0;
 236}
 237
 238#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
 239
 240static int reset_head(struct object_id *oid, const char *action,
 241                      const char *switch_to_branch, int detach_head)
 242{
 243        struct object_id head_oid;
 244        struct tree_desc desc;
 245        struct lock_file lock = LOCK_INIT;
 246        struct unpack_trees_options unpack_tree_opts;
 247        struct tree *tree;
 248        const char *reflog_action;
 249        struct strbuf msg = STRBUF_INIT;
 250        size_t prefix_len;
 251        struct object_id *orig = NULL, oid_orig,
 252                *old_orig = NULL, oid_old_orig;
 253        int ret = 0;
 254
 255        if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
 256                BUG("Not a fully qualified branch: '%s'", switch_to_branch);
 257
 258        if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
 259                return -1;
 260
 261        if (!oid) {
 262                if (get_oid("HEAD", &head_oid)) {
 263                        rollback_lock_file(&lock);
 264                        return error(_("could not determine HEAD revision"));
 265                }
 266                oid = &head_oid;
 267        }
 268
 269        memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
 270        setup_unpack_trees_porcelain(&unpack_tree_opts, action);
 271        unpack_tree_opts.head_idx = 1;
 272        unpack_tree_opts.src_index = the_repository->index;
 273        unpack_tree_opts.dst_index = the_repository->index;
 274        unpack_tree_opts.fn = oneway_merge;
 275        unpack_tree_opts.update = 1;
 276        unpack_tree_opts.merge = 1;
 277        if (!detach_head)
 278                unpack_tree_opts.reset = 1;
 279
 280        if (read_index_unmerged(the_repository->index) < 0) {
 281                rollback_lock_file(&lock);
 282                return error(_("could not read index"));
 283        }
 284
 285        if (!fill_tree_descriptor(&desc, oid)) {
 286                error(_("failed to find tree of %s"), oid_to_hex(oid));
 287                rollback_lock_file(&lock);
 288                free((void *)desc.buffer);
 289                return -1;
 290        }
 291
 292        if (unpack_trees(1, &desc, &unpack_tree_opts)) {
 293                rollback_lock_file(&lock);
 294                free((void *)desc.buffer);
 295                return -1;
 296        }
 297
 298        tree = parse_tree_indirect(oid);
 299        prime_cache_tree(the_repository->index, tree);
 300
 301        if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
 302                ret = error(_("could not write index"));
 303        free((void *)desc.buffer);
 304
 305        if (ret)
 306                return ret;
 307
 308        reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
 309        strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
 310        prefix_len = msg.len;
 311
 312        if (!get_oid("ORIG_HEAD", &oid_old_orig))
 313                old_orig = &oid_old_orig;
 314        if (!get_oid("HEAD", &oid_orig)) {
 315                orig = &oid_orig;
 316                strbuf_addstr(&msg, "updating ORIG_HEAD");
 317                update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
 318                           UPDATE_REFS_MSG_ON_ERR);
 319        } else if (old_orig)
 320                delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
 321        strbuf_setlen(&msg, prefix_len);
 322        strbuf_addstr(&msg, "updating HEAD");
 323        if (!switch_to_branch)
 324                ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
 325                                 UPDATE_REFS_MSG_ON_ERR);
 326        else {
 327                ret = create_symref("HEAD", switch_to_branch, msg.buf);
 328                if (!ret)
 329                        ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
 330                                         UPDATE_REFS_MSG_ON_ERR);
 331        }
 332
 333        strbuf_release(&msg);
 334        return ret;
 335}
 336
 337static int rebase_config(const char *var, const char *value, void *data)
 338{
 339        struct rebase_options *opts = data;
 340
 341        if (!strcmp(var, "rebase.stat")) {
 342                if (git_config_bool(var, value))
 343                        opts->flags |= REBASE_DIFFSTAT;
 344                else
 345                        opts->flags &= !REBASE_DIFFSTAT;
 346                return 0;
 347        }
 348
 349        return git_default_config(var, value, data);
 350}
 351
 352/*
 353 * Determines whether the commits in from..to are linear, i.e. contain
 354 * no merge commits. This function *expects* `from` to be an ancestor of
 355 * `to`.
 356 */
 357static int is_linear_history(struct commit *from, struct commit *to)
 358{
 359        while (to && to != from) {
 360                parse_commit(to);
 361                if (!to->parents)
 362                        return 1;
 363                if (to->parents->next)
 364                        return 0;
 365                to = to->parents->item;
 366        }
 367        return 1;
 368}
 369
 370static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
 371                            struct object_id *merge_base)
 372{
 373        struct commit *head = lookup_commit(the_repository, head_oid);
 374        struct commit_list *merge_bases;
 375        int res;
 376
 377        if (!head)
 378                return 0;
 379
 380        merge_bases = get_merge_bases(onto, head);
 381        if (merge_bases && !merge_bases->next) {
 382                oidcpy(merge_base, &merge_bases->item->object.oid);
 383                res = !oidcmp(merge_base, &onto->object.oid);
 384        } else {
 385                oidcpy(merge_base, &null_oid);
 386                res = 0;
 387        }
 388        free_commit_list(merge_bases);
 389        return res && is_linear_history(onto, head);
 390}
 391
 392int cmd_rebase(int argc, const char **argv, const char *prefix)
 393{
 394        struct rebase_options options = {
 395                .type = REBASE_UNSPECIFIED,
 396                .flags = REBASE_NO_QUIET,
 397                .git_am_opt = STRBUF_INIT,
 398        };
 399        const char *branch_name;
 400        int ret, flags, in_progress = 0;
 401        int ok_to_skip_pre_rebase = 0;
 402        struct strbuf msg = STRBUF_INIT;
 403        struct strbuf revisions = STRBUF_INIT;
 404        struct strbuf buf = STRBUF_INIT;
 405        struct object_id merge_base;
 406        struct option builtin_rebase_options[] = {
 407                OPT_STRING(0, "onto", &options.onto_name,
 408                           N_("revision"),
 409                           N_("rebase onto given branch instead of upstream")),
 410                OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
 411                         N_("allow pre-rebase hook to run")),
 412                OPT_NEGBIT('q', "quiet", &options.flags,
 413                           N_("be quiet. implies --no-stat"),
 414                           REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
 415                OPT_BIT('v', "verbose", &options.flags,
 416                        N_("display a diffstat of what changed upstream"),
 417                        REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
 418                {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
 419                        N_("do not show diffstat of what changed upstream"),
 420                        PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
 421                OPT_BIT('f', "force-rebase", &options.flags,
 422                        N_("cherry-pick all commits, even if unchanged"),
 423                        REBASE_FORCE),
 424                OPT_BIT(0, "no-ff", &options.flags,
 425                        N_("cherry-pick all commits, even if unchanged"),
 426                        REBASE_FORCE),
 427                OPT_END(),
 428        };
 429
 430        /*
 431         * NEEDSWORK: Once the builtin rebase has been tested enough
 432         * and git-legacy-rebase.sh is retired to contrib/, this preamble
 433         * can be removed.
 434         */
 435
 436        if (!use_builtin_rebase()) {
 437                const char *path = mkpath("%s/git-legacy-rebase",
 438                                          git_exec_path());
 439
 440                if (sane_execvp(path, (char **)argv) < 0)
 441                        die_errno(_("could not exec %s"), path);
 442                else
 443                        BUG("sane_execvp() returned???");
 444        }
 445
 446        if (argc == 2 && !strcmp(argv[1], "-h"))
 447                usage_with_options(builtin_rebase_usage,
 448                                   builtin_rebase_options);
 449
 450        prefix = setup_git_directory();
 451        trace_repo_setup(prefix);
 452        setup_work_tree();
 453
 454        git_config(rebase_config, &options);
 455
 456        if (is_directory(apply_dir())) {
 457                options.type = REBASE_AM;
 458                options.state_dir = apply_dir();
 459        } else if (is_directory(merge_dir())) {
 460                strbuf_reset(&buf);
 461                strbuf_addf(&buf, "%s/rewritten", merge_dir());
 462                if (is_directory(buf.buf)) {
 463                        options.type = REBASE_PRESERVE_MERGES;
 464                        options.flags |= REBASE_INTERACTIVE_EXPLICIT;
 465                } else {
 466                        strbuf_reset(&buf);
 467                        strbuf_addf(&buf, "%s/interactive", merge_dir());
 468                        if(file_exists(buf.buf)) {
 469                                options.type = REBASE_INTERACTIVE;
 470                                options.flags |= REBASE_INTERACTIVE_EXPLICIT;
 471                        } else
 472                                options.type = REBASE_MERGE;
 473                }
 474                options.state_dir = merge_dir();
 475        }
 476
 477        if (options.type != REBASE_UNSPECIFIED)
 478                in_progress = 1;
 479
 480        argc = parse_options(argc, argv, prefix,
 481                             builtin_rebase_options,
 482                             builtin_rebase_usage, 0);
 483
 484        if (argc > 2)
 485                usage_with_options(builtin_rebase_usage,
 486                                   builtin_rebase_options);
 487
 488        /* Make sure no rebase is in progress */
 489        if (in_progress) {
 490                const char *last_slash = strrchr(options.state_dir, '/');
 491                const char *state_dir_base =
 492                        last_slash ? last_slash + 1 : options.state_dir;
 493                const char *cmd_live_rebase =
 494                        "git rebase (--continue | --abort | --skip)";
 495                strbuf_reset(&buf);
 496                strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
 497                die(_("It seems that there is already a %s directory, and\n"
 498                      "I wonder if you are in the middle of another rebase.  "
 499                      "If that is the\n"
 500                      "case, please try\n\t%s\n"
 501                      "If that is not the case, please\n\t%s\n"
 502                      "and run me again.  I am stopping in case you still "
 503                      "have something\n"
 504                      "valuable there.\n"),
 505                    state_dir_base, cmd_live_rebase, buf.buf);
 506        }
 507
 508        if (!(options.flags & REBASE_NO_QUIET))
 509                strbuf_addstr(&options.git_am_opt, " -q");
 510
 511        switch (options.type) {
 512        case REBASE_MERGE:
 513        case REBASE_INTERACTIVE:
 514        case REBASE_PRESERVE_MERGES:
 515                options.state_dir = merge_dir();
 516                break;
 517        case REBASE_AM:
 518                options.state_dir = apply_dir();
 519                break;
 520        default:
 521                /* the default rebase backend is `--am` */
 522                options.type = REBASE_AM;
 523                options.state_dir = apply_dir();
 524                break;
 525        }
 526
 527        if (!options.root) {
 528                if (argc < 1)
 529                        die("TODO: handle @{upstream}");
 530                else {
 531                        options.upstream_name = argv[0];
 532                        argc--;
 533                        argv++;
 534                        if (!strcmp(options.upstream_name, "-"))
 535                                options.upstream_name = "@{-1}";
 536                }
 537                options.upstream = peel_committish(options.upstream_name);
 538                if (!options.upstream)
 539                        die(_("invalid upstream '%s'"), options.upstream_name);
 540                options.upstream_arg = options.upstream_name;
 541        } else
 542                die("TODO: upstream for --root");
 543
 544        /* Make sure the branch to rebase onto is valid. */
 545        if (!options.onto_name)
 546                options.onto_name = options.upstream_name;
 547        if (strstr(options.onto_name, "...")) {
 548                if (get_oid_mb(options.onto_name, &merge_base) < 0)
 549                        die(_("'%s': need exactly one merge base"),
 550                            options.onto_name);
 551                options.onto = lookup_commit_or_die(&merge_base,
 552                                                    options.onto_name);
 553        } else {
 554                options.onto = peel_committish(options.onto_name);
 555                if (!options.onto)
 556                        die(_("Does not point to a valid commit '%s'"),
 557                                options.onto_name);
 558        }
 559
 560        /*
 561         * If the branch to rebase is given, that is the branch we will rebase
 562         * branch_name -- branch/commit being rebased, or
 563         *                HEAD (already detached)
 564         * orig_head -- commit object name of tip of the branch before rebasing
 565         * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
 566         */
 567        if (argc > 0)
 568                 die("TODO: handle switch_to");
 569        else {
 570                /* Do not need to switch branches, we are already on it. */
 571                options.head_name =
 572                        xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
 573                                         &flags));
 574                if (!options.head_name)
 575                        die(_("No such ref: %s"), "HEAD");
 576                if (flags & REF_ISSYMREF) {
 577                        if (!skip_prefix(options.head_name,
 578                                         "refs/heads/", &branch_name))
 579                                branch_name = options.head_name;
 580
 581                } else {
 582                        free(options.head_name);
 583                        options.head_name = NULL;
 584                        branch_name = "HEAD";
 585                }
 586                if (get_oid("HEAD", &options.orig_head))
 587                        die(_("Could not resolve HEAD to a revision"));
 588        }
 589
 590        if (read_index(the_repository->index) < 0)
 591                die(_("could not read index"));
 592
 593        if (require_clean_work_tree("rebase",
 594                                    _("Please commit or stash them."), 1, 1)) {
 595                ret = 1;
 596                goto cleanup;
 597        }
 598
 599        /*
 600         * Now we are rebasing commits upstream..orig_head (or with --root,
 601         * everything leading up to orig_head) on top of onto.
 602         */
 603
 604        /*
 605         * Check if we are already based on onto with linear history,
 606         * but this should be done only when upstream and onto are the same
 607         * and if this is not an interactive rebase.
 608         */
 609        if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
 610            !is_interactive(&options) && !options.restrict_revision &&
 611            !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
 612                int flag;
 613
 614                if (!(options.flags & REBASE_FORCE)) {
 615                        if (!(options.flags & REBASE_NO_QUIET))
 616                                ; /* be quiet */
 617                        else if (!strcmp(branch_name, "HEAD") &&
 618                                 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
 619                                puts(_("HEAD is up to date."));
 620                        else
 621                                printf(_("Current branch %s is up to date.\n"),
 622                                       branch_name);
 623                        ret = !!finish_rebase(&options);
 624                        goto cleanup;
 625                } else if (!(options.flags & REBASE_NO_QUIET))
 626                        ; /* be quiet */
 627                else if (!strcmp(branch_name, "HEAD") &&
 628                         resolve_ref_unsafe("HEAD", 0, NULL, &flag))
 629                        puts(_("HEAD is up to date, rebase forced."));
 630                else
 631                        printf(_("Current branch %s is up to date, rebase "
 632                                 "forced.\n"), branch_name);
 633        }
 634
 635        /* If a hook exists, give it a chance to interrupt*/
 636        if (!ok_to_skip_pre_rebase &&
 637            run_hook_le(NULL, "pre-rebase", options.upstream_arg,
 638                        argc ? argv[0] : NULL, NULL))
 639                die(_("The pre-rebase hook refused to rebase."));
 640
 641        if (options.flags & REBASE_DIFFSTAT) {
 642                struct diff_options opts;
 643
 644                if (options.flags & REBASE_VERBOSE)
 645                        printf(_("Changes from %s to %s:\n"),
 646                                oid_to_hex(&merge_base),
 647                                oid_to_hex(&options.onto->object.oid));
 648
 649                /* We want color (if set), but no pager */
 650                diff_setup(&opts);
 651                opts.stat_width = -1; /* use full terminal width */
 652                opts.stat_graph_width = -1; /* respect statGraphWidth config */
 653                opts.output_format |=
 654                        DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
 655                opts.detect_rename = DIFF_DETECT_RENAME;
 656                diff_setup_done(&opts);
 657                diff_tree_oid(&merge_base, &options.onto->object.oid,
 658                              "", &opts);
 659                diffcore_std(&opts);
 660                diff_flush(&opts);
 661        }
 662
 663        /* Detach HEAD and reset the tree */
 664        if (options.flags & REBASE_NO_QUIET)
 665                printf(_("First, rewinding head to replay your work on top of "
 666                         "it...\n"));
 667
 668        strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
 669        if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
 670                die(_("Could not detach HEAD"));
 671        strbuf_release(&msg);
 672
 673        strbuf_addf(&revisions, "%s..%s",
 674                    options.root ? oid_to_hex(&options.onto->object.oid) :
 675                    (options.restrict_revision ?
 676                     oid_to_hex(&options.restrict_revision->object.oid) :
 677                     oid_to_hex(&options.upstream->object.oid)),
 678                    oid_to_hex(&options.orig_head));
 679
 680        options.revisions = revisions.buf;
 681
 682        ret = !!run_specific_rebase(&options);
 683
 684cleanup:
 685        strbuf_release(&revisions);
 686        free(options.head_name);
 687        return ret;
 688}