f8128037d3fb3eb7eabfab3747150d0fd82332ac
   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 "alias.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 get_revision_ranges(const char *upstream, const char *onto,
  17                               const char **head_hash,
  18                               char **revisions, char **shortrevisions)
  19{
  20        const char *base_rev = upstream ? upstream : onto;
  21        struct object_id orig_head;
  22
  23        if (get_oid("HEAD", &orig_head))
  24                return error(_("no HEAD?"));
  25
  26        *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
  27
  28        if (revisions)
  29                *revisions = xstrfmt("%s...%s", base_rev, *head_hash);
  30        if (shortrevisions) {
  31                const char *shorthead;
  32
  33                shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV);
  34
  35                if (upstream) {
  36                        const char *shortrev;
  37                        struct object_id rev_oid;
  38
  39                        get_oid(base_rev, &rev_oid);
  40                        shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV);
  41
  42                        *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
  43                } else
  44                        *shortrevisions = xstrdup(shorthead);
  45        }
  46
  47        return 0;
  48}
  49
  50static int init_basic_state(struct replay_opts *opts, const char *head_name,
  51                            const char *onto, const char *orig_head)
  52{
  53        FILE *interactive;
  54
  55        if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir()))
  56                return error_errno(_("could not create temporary %s"), path_state_dir());
  57
  58        delete_reflog("REBASE_HEAD");
  59
  60        interactive = fopen(path_interactive(), "w");
  61        if (!interactive)
  62                return error_errno(_("could not mark as interactive"));
  63        fclose(interactive);
  64
  65        return write_basic_state(opts, head_name, onto, orig_head);
  66}
  67
  68static const char * const builtin_rebase_helper_usage[] = {
  69        N_("git rebase--helper [<options>]"),
  70        NULL
  71};
  72
  73int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
  74{
  75        struct replay_opts opts = REPLAY_OPTS_INIT;
  76        unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
  77        int abbreviate_commands = 0, rebase_cousins = -1, ret;
  78        const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL,
  79                *squash_onto = NULL, *upstream = NULL, *head_name = NULL;
  80        char *raw_strategies = NULL;
  81        enum {
  82                CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
  83                CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH,
  84                COMPLETE_ACTION, INIT_BASIC_STATE
  85        } command = 0;
  86        struct option options[] = {
  87                OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
  88                OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")),
  89                OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
  90                        N_("allow commits with empty messages")),
  91                OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
  92                OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
  93                         N_("keep original branch points of cousins")),
  94                OPT_BOOL(0, "autosquash", &autosquash,
  95                         N_("move commits that begin with squash!/fixup!")),
  96                OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
  97                OPT__VERBOSE(&opts.verbose, N_("be verbose")),
  98                OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
  99                                CONTINUE),
 100                OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
 101                                ABORT),
 102                OPT_CMDMODE(0, "make-script", &command,
 103                        N_("make rebase script"), MAKE_SCRIPT),
 104                OPT_CMDMODE(0, "shorten-ids", &command,
 105                        N_("shorten commit ids in the todo list"), SHORTEN_OIDS),
 106                OPT_CMDMODE(0, "expand-ids", &command,
 107                        N_("expand commit ids in the todo list"), EXPAND_OIDS),
 108                OPT_CMDMODE(0, "check-todo-list", &command,
 109                        N_("check the todo list"), CHECK_TODO_LIST),
 110                OPT_CMDMODE(0, "rearrange-squash", &command,
 111                        N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
 112                OPT_CMDMODE(0, "add-exec-commands", &command,
 113                        N_("insert exec commands in todo list"), ADD_EXEC),
 114                OPT_CMDMODE(0, "edit-todo", &command,
 115                            N_("edit the todo list during an interactive rebase"),
 116                            EDIT_TODO),
 117                OPT_CMDMODE(0, "prepare-branch", &command,
 118                            N_("prepare the branch to be rebased"), PREPARE_BRANCH),
 119                OPT_CMDMODE(0, "complete-action", &command,
 120                            N_("complete the action"), COMPLETE_ACTION),
 121                OPT_CMDMODE(0, "init-basic-state", &command,
 122                            N_("initialise the rebase state"), INIT_BASIC_STATE),
 123                OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
 124                OPT_STRING(0, "restrict-revision", &restrict_revision,
 125                           N_("restrict-revision"), N_("restrict revision")),
 126                OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
 127                           N_("squash onto")),
 128                OPT_STRING(0, "upstream", &upstream, N_("upstream"),
 129                           N_("the upstream commit")),
 130                OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")),
 131                OPT_STRING('S', "gpg-sign", &opts.gpg_sign, N_("gpg-sign"),
 132                           N_("GPG-sign commits")),
 133                OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"),
 134                           N_("rebase strategy")),
 135                OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"),
 136                           N_("strategy options")),
 137                OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto),
 138                OPT_END()
 139        };
 140
 141        sequencer_init_config(&opts);
 142        git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
 143
 144        opts.action = REPLAY_INTERACTIVE_REBASE;
 145        opts.allow_ff = 1;
 146        opts.allow_empty = 1;
 147
 148        argc = parse_options(argc, argv, NULL, options,
 149                        builtin_rebase_helper_usage, PARSE_OPT_KEEP_ARGV0);
 150
 151        flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
 152        flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
 153        flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
 154        flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
 155        flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
 156
 157        if (rebase_cousins >= 0 && !rebase_merges)
 158                warning(_("--[no-]rebase-cousins has no effect without "
 159                          "--rebase-merges"));
 160
 161        if (command == CONTINUE && argc == 1)
 162                return !!sequencer_continue(&opts);
 163        if (command == ABORT && argc == 1)
 164                return !!sequencer_remove_state(&opts);
 165        if (command == MAKE_SCRIPT && argc == 1) {
 166                char *revisions = NULL;
 167                struct argv_array make_script_args = ARGV_ARRAY_INIT;
 168
 169                if (!upstream && squash_onto)
 170                        write_file(path_squash_onto(), "%s\n", squash_onto);
 171
 172                ret = get_revision_ranges(upstream, onto, &head_hash, &revisions, NULL);
 173                if (ret)
 174                        return ret;
 175
 176                argv_array_pushl(&make_script_args, "", revisions, NULL);
 177                if (restrict_revision)
 178                        argv_array_push(&make_script_args, restrict_revision);
 179
 180                ret = sequencer_make_script(stdout,
 181                                            make_script_args.argc, make_script_args.argv,
 182                                            flags);
 183
 184                free(revisions);
 185                argv_array_clear(&make_script_args);
 186
 187                return !!ret;
 188        }
 189        if ((command == SHORTEN_OIDS || command == EXPAND_OIDS) && argc == 1)
 190                return !!transform_todos(flags);
 191        if (command == CHECK_TODO_LIST && argc == 1)
 192                return !!check_todo_list();
 193        if (command == REARRANGE_SQUASH && argc == 1)
 194                return !!rearrange_squash();
 195        if (command == ADD_EXEC && argc == 2)
 196                return !!sequencer_add_exec_commands(argv[1]);
 197        if (command == EDIT_TODO && argc == 1)
 198                return !!edit_todo_list(flags);
 199        if (command == PREPARE_BRANCH && argc == 2)
 200                return !!prepare_branch_to_be_rebased(&opts, argv[1]);
 201        if (command == COMPLETE_ACTION && argc == 3) {
 202                char *shortrevisions = NULL;
 203
 204                ret = get_revision_ranges(upstream, onto, &head_hash, NULL, &shortrevisions);
 205                if (ret)
 206                        return ret;
 207
 208                ret = complete_action(&opts, flags, shortrevisions, argv[1], onto,
 209                                      head_hash, argv[2], autosquash);
 210
 211                free(shortrevisions);
 212                return !!ret;
 213        }
 214        if (command == INIT_BASIC_STATE) {
 215                if (raw_strategies)
 216                        parse_strategy_opts(&opts, raw_strategies);
 217
 218                ret = get_revision_ranges(upstream, onto, &head_hash, NULL, NULL);
 219                if (ret)
 220                        return ret;
 221
 222                return !!init_basic_state(&opts, head_name, onto, head_hash);
 223        }
 224
 225        usage_with_options(builtin_rebase_helper_usage, options);
 226}