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