b277239f21444f804e17a68845a859d3232ed69f
   1#include "builtin.h"
   2#include "cache.h"
   3#include "config.h"
   4#include "parse-options.h"
   5#include "sequencer.h"
   6#include "rebase-interactive.h"
   7#include "argv-array.h"
   8#include "refs.h"
   9#include "rerere.h"
  10#include "run-command.h"
  11
  12static GIT_PATH_FUNC(path_state_dir, "rebase-merge/")
  13static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
  14static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
  15
  16static int add_exec_commands(struct string_list *commands)
  17{
  18        const char *todo_file = rebase_path_todo();
  19        struct todo_list todo_list = TODO_LIST_INIT;
  20        int res;
  21
  22        if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
  23                return error_errno(_("could not read '%s'."), todo_file);
  24
  25        if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
  26                                        &todo_list)) {
  27                todo_list_release(&todo_list);
  28                return error(_("unusable todo list: '%s'"), todo_file);
  29        }
  30
  31        todo_list_add_exec_commands(&todo_list, commands);
  32        res = todo_list_write_to_file(the_repository, &todo_list,
  33                                      todo_file, NULL, NULL, -1, 0);
  34        todo_list_release(&todo_list);
  35
  36        if (res)
  37                return error_errno(_("could not write '%s'."), todo_file);
  38        return 0;
  39}
  40
  41static int rearrange_squash_in_todo_file(void)
  42{
  43        const char *todo_file = rebase_path_todo();
  44        struct todo_list todo_list = TODO_LIST_INIT;
  45        int res = 0;
  46
  47        if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
  48                return error_errno(_("could not read '%s'."), todo_file);
  49        if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
  50                                        &todo_list)) {
  51                todo_list_release(&todo_list);
  52                return error(_("unusable todo list: '%s'"), todo_file);
  53        }
  54
  55        res = todo_list_rearrange_squash(&todo_list);
  56        if (!res)
  57                res = todo_list_write_to_file(the_repository, &todo_list,
  58                                              todo_file, NULL, NULL, -1, 0);
  59
  60        todo_list_release(&todo_list);
  61
  62        if (res)
  63                return error_errno(_("could not write '%s'."), todo_file);
  64        return 0;
  65}
  66
  67static int edit_todo_file(unsigned flags)
  68{
  69        const char *todo_file = rebase_path_todo();
  70        struct todo_list todo_list = TODO_LIST_INIT,
  71                new_todo = TODO_LIST_INIT;
  72        int res = 0;
  73
  74        if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
  75                return error_errno(_("could not read '%s'."), todo_file);
  76
  77        strbuf_stripspace(&todo_list.buf, 1);
  78        res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
  79        if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
  80                                            NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
  81                res = error_errno(_("could not write '%s'"), todo_file);
  82
  83        todo_list_release(&todo_list);
  84        todo_list_release(&new_todo);
  85
  86        return res;
  87}
  88
  89static int get_revision_ranges(const char *upstream, const char *onto,
  90                               const char **head_hash,
  91                               char **revisions, char **shortrevisions)
  92{
  93        const char *base_rev = upstream ? upstream : onto, *shorthead;
  94        struct object_id orig_head;
  95
  96        if (get_oid("HEAD", &orig_head))
  97                return error(_("no HEAD?"));
  98
  99        *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
 100        *revisions = xstrfmt("%s...%s", base_rev, *head_hash);
 101
 102        shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV);
 103
 104        if (upstream) {
 105                const char *shortrev;
 106                struct object_id rev_oid;
 107
 108                get_oid(base_rev, &rev_oid);
 109                shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV);
 110
 111                *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
 112        } else
 113                *shortrevisions = xstrdup(shorthead);
 114
 115        return 0;
 116}
 117
 118static int init_basic_state(struct replay_opts *opts, const char *head_name,
 119                            const char *onto, const char *orig_head)
 120{
 121        FILE *interactive;
 122
 123        if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir()))
 124                return error_errno(_("could not create temporary %s"), path_state_dir());
 125
 126        delete_reflog("REBASE_HEAD");
 127
 128        interactive = fopen(path_interactive(), "w");
 129        if (!interactive)
 130                return error_errno(_("could not mark as interactive"));
 131        fclose(interactive);
 132
 133        return write_basic_state(opts, head_name, onto, orig_head);
 134}
 135
 136static int do_interactive_rebase(struct replay_opts *opts, unsigned flags,
 137                                 const char *switch_to, const char *upstream,
 138                                 const char *onto, const char *onto_name,
 139                                 const char *squash_onto, const char *head_name,
 140                                 const char *restrict_revision, char *raw_strategies,
 141                                 struct string_list *commands, unsigned autosquash)
 142{
 143        int ret;
 144        const char *head_hash = NULL;
 145        char *revisions = NULL, *shortrevisions = NULL;
 146        struct argv_array make_script_args = ARGV_ARRAY_INIT;
 147        struct todo_list todo_list = TODO_LIST_INIT;
 148
 149        if (prepare_branch_to_be_rebased(opts, switch_to))
 150                return -1;
 151
 152        if (get_revision_ranges(upstream, onto, &head_hash,
 153                                &revisions, &shortrevisions))
 154                return -1;
 155
 156        if (raw_strategies)
 157                parse_strategy_opts(opts, raw_strategies);
 158
 159        if (init_basic_state(opts, head_name, onto, head_hash)) {
 160                free(revisions);
 161                free(shortrevisions);
 162
 163                return -1;
 164        }
 165
 166        if (!upstream && squash_onto)
 167                write_file(path_squash_onto(), "%s\n", squash_onto);
 168
 169        argv_array_pushl(&make_script_args, "", revisions, NULL);
 170        if (restrict_revision)
 171                argv_array_push(&make_script_args, restrict_revision);
 172
 173        ret = sequencer_make_script(the_repository, &todo_list.buf,
 174                                    make_script_args.argc, make_script_args.argv,
 175                                    flags);
 176
 177        if (ret)
 178                error(_("could not generate todo list"));
 179        else {
 180                discard_cache();
 181                if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
 182                                                &todo_list))
 183                        BUG("unusable todo list");
 184
 185                ret = complete_action(the_repository, opts, flags, shortrevisions, onto_name,
 186                                      onto, head_hash, commands, autosquash, &todo_list);
 187        }
 188
 189        free(revisions);
 190        free(shortrevisions);
 191        todo_list_release(&todo_list);
 192        argv_array_clear(&make_script_args);
 193
 194        return ret;
 195}
 196
 197static const char * const builtin_rebase_interactive_usage[] = {
 198        N_("git rebase--interactive [<options>]"),
 199        NULL
 200};
 201
 202int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
 203{
 204        struct replay_opts opts = REPLAY_OPTS_INIT;
 205        unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
 206        int abbreviate_commands = 0, rebase_cousins = -1, ret = 0;
 207        const char *onto = NULL, *onto_name = NULL, *restrict_revision = NULL,
 208                *squash_onto = NULL, *upstream = NULL, *head_name = NULL,
 209                *switch_to = NULL, *cmd = NULL;
 210        struct string_list commands = STRING_LIST_INIT_DUP;
 211        char *raw_strategies = NULL;
 212        enum {
 213                NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH,
 214                SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC
 215        } command = 0;
 216        struct option options[] = {
 217                OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
 218                OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")),
 219                OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
 220                         N_("allow commits with empty messages")),
 221                OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
 222                OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
 223                         N_("keep original branch points of cousins")),
 224                OPT_BOOL(0, "autosquash", &autosquash,
 225                         N_("move commits that begin with squash!/fixup!")),
 226                OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
 227                OPT__VERBOSE(&opts.verbose, N_("be verbose")),
 228                OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
 229                            CONTINUE),
 230                OPT_CMDMODE(0, "skip", &command, N_("skip commit"), SKIP),
 231                OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list"),
 232                            EDIT_TODO),
 233                OPT_CMDMODE(0, "show-current-patch", &command, N_("show the current patch"),
 234                            SHOW_CURRENT_PATCH),
 235                OPT_CMDMODE(0, "shorten-ids", &command,
 236                        N_("shorten commit ids in the todo list"), SHORTEN_OIDS),
 237                OPT_CMDMODE(0, "expand-ids", &command,
 238                        N_("expand commit ids in the todo list"), EXPAND_OIDS),
 239                OPT_CMDMODE(0, "check-todo-list", &command,
 240                        N_("check the todo list"), CHECK_TODO_LIST),
 241                OPT_CMDMODE(0, "rearrange-squash", &command,
 242                        N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
 243                OPT_CMDMODE(0, "add-exec-commands", &command,
 244                        N_("insert exec commands in todo list"), ADD_EXEC),
 245                OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
 246                OPT_STRING(0, "restrict-revision", &restrict_revision,
 247                           N_("restrict-revision"), N_("restrict revision")),
 248                OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
 249                           N_("squash onto")),
 250                OPT_STRING(0, "upstream", &upstream, N_("upstream"),
 251                           N_("the upstream commit")),
 252                OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")),
 253                { OPTION_STRING, 'S', "gpg-sign", &opts.gpg_sign, N_("key-id"),
 254                        N_("GPG-sign commits"),
 255                        PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
 256                OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"),
 257                           N_("rebase strategy")),
 258                OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"),
 259                           N_("strategy options")),
 260                OPT_STRING(0, "switch-to", &switch_to, N_("switch-to"),
 261                           N_("the branch or commit to checkout")),
 262                OPT_STRING(0, "onto-name", &onto_name, N_("onto-name"), N_("onto name")),
 263                OPT_STRING(0, "cmd", &cmd, N_("cmd"), N_("the command to run")),
 264                OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto),
 265                OPT_END()
 266        };
 267
 268        sequencer_init_config(&opts);
 269        git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
 270
 271        opts.action = REPLAY_INTERACTIVE_REBASE;
 272        opts.allow_ff = 1;
 273        opts.allow_empty = 1;
 274
 275        if (argc == 1)
 276                usage_with_options(builtin_rebase_interactive_usage, options);
 277
 278        argc = parse_options(argc, argv, NULL, options,
 279                        builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0);
 280
 281        opts.gpg_sign = xstrdup_or_null(opts.gpg_sign);
 282
 283        flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
 284        flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
 285        flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
 286        flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
 287        flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
 288
 289        if (rebase_cousins >= 0 && !rebase_merges)
 290                warning(_("--[no-]rebase-cousins has no effect without "
 291                          "--rebase-merges"));
 292
 293        if (cmd && *cmd) {
 294                string_list_split(&commands, cmd, '\n', -1);
 295
 296                /* rebase.c adds a new line to cmd after every command,
 297                 * so here the last command is always empty */
 298                string_list_remove_empty_items(&commands, 0);
 299        }
 300
 301        switch (command) {
 302        case NONE:
 303                if (!onto && !upstream)
 304                        die(_("a base commit must be provided with --upstream or --onto"));
 305
 306                ret = do_interactive_rebase(&opts, flags, switch_to, upstream, onto,
 307                                            onto_name, squash_onto, head_name, restrict_revision,
 308                                            raw_strategies, &commands, autosquash);
 309                break;
 310        case SKIP: {
 311                struct string_list merge_rr = STRING_LIST_INIT_DUP;
 312
 313                rerere_clear(the_repository, &merge_rr);
 314                /* fallthrough */
 315        case CONTINUE:
 316                ret = sequencer_continue(the_repository, &opts);
 317                break;
 318        }
 319        case EDIT_TODO:
 320                ret = edit_todo_file(flags);
 321                break;
 322        case SHOW_CURRENT_PATCH: {
 323                struct child_process cmd = CHILD_PROCESS_INIT;
 324
 325                cmd.git_cmd = 1;
 326                argv_array_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL);
 327                ret = run_command(&cmd);
 328
 329                break;
 330        }
 331        case SHORTEN_OIDS:
 332        case EXPAND_OIDS:
 333                ret = transform_todo_file(the_repository, flags);
 334                break;
 335        case CHECK_TODO_LIST:
 336                ret = check_todo_list_from_file(the_repository);
 337                break;
 338        case REARRANGE_SQUASH:
 339                ret = rearrange_squash_in_todo_file();
 340                break;
 341        case ADD_EXEC:
 342                ret = add_exec_commands(&commands);
 343                break;
 344        default:
 345                BUG("invalid command '%d'", command);
 346        }
 347
 348        string_list_clear(&commands, 0);
 349        return !!ret;
 350}