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