acc71a6f99a067441acf4cdd785fd91a49afda43
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
9static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
10
11static int get_revision_ranges(const char *upstream, const char *onto,
12 const char **head_hash,
13 char **revisions)
14{
15 const char *base_rev = upstream ? upstream : onto;
16 struct object_id orig_head;
17
18 if (get_oid("HEAD", &orig_head))
19 return error(_("no HEAD?"));
20
21 *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
22 *revisions = xstrfmt("%s...%s", base_rev, *head_hash);
23
24 return 0;
25}
26
27static const char * const builtin_rebase_helper_usage[] = {
28 N_("git rebase--helper [<options>]"),
29 NULL
30};
31
32int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
33{
34 struct replay_opts opts = REPLAY_OPTS_INIT;
35 unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
36 int abbreviate_commands = 0, rebase_cousins = -1, ret;
37 const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL,
38 *squash_onto = NULL, *upstream = NULL;
39 enum {
40 CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
41 CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH,
42 COMPLETE_ACTION
43 } command = 0;
44 struct option options[] = {
45 OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
46 OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")),
47 OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
48 N_("allow commits with empty messages")),
49 OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
50 OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
51 N_("keep original branch points of cousins")),
52 OPT_BOOL(0, "autosquash", &autosquash,
53 N_("move commits that begin with squash!/fixup!")),
54 OPT__VERBOSE(&opts.verbose, N_("be verbose")),
55 OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
56 CONTINUE),
57 OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
58 ABORT),
59 OPT_CMDMODE(0, "make-script", &command,
60 N_("make rebase script"), MAKE_SCRIPT),
61 OPT_CMDMODE(0, "shorten-ids", &command,
62 N_("shorten commit ids in the todo list"), SHORTEN_OIDS),
63 OPT_CMDMODE(0, "expand-ids", &command,
64 N_("expand commit ids in the todo list"), EXPAND_OIDS),
65 OPT_CMDMODE(0, "check-todo-list", &command,
66 N_("check the todo list"), CHECK_TODO_LIST),
67 OPT_CMDMODE(0, "rearrange-squash", &command,
68 N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
69 OPT_CMDMODE(0, "add-exec-commands", &command,
70 N_("insert exec commands in todo list"), ADD_EXEC),
71 OPT_CMDMODE(0, "edit-todo", &command,
72 N_("edit the todo list during an interactive rebase"),
73 EDIT_TODO),
74 OPT_CMDMODE(0, "prepare-branch", &command,
75 N_("prepare the branch to be rebased"), PREPARE_BRANCH),
76 OPT_CMDMODE(0, "complete-action", &command,
77 N_("complete the action"), COMPLETE_ACTION),
78 OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
79 OPT_STRING(0, "restrict-revision", &restrict_revision,
80 N_("restrict-revision"), N_("restrict revision")),
81 OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
82 N_("squash onto")),
83 OPT_STRING(0, "upstream", &upstream, N_("upstream"),
84 N_("the upstream commit")),
85 OPT_END()
86 };
87
88 sequencer_init_config(&opts);
89 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
90
91 opts.action = REPLAY_INTERACTIVE_REBASE;
92 opts.allow_ff = 1;
93 opts.allow_empty = 1;
94
95 argc = parse_options(argc, argv, NULL, options,
96 builtin_rebase_helper_usage, PARSE_OPT_KEEP_ARGV0);
97
98 flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
99 flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
100 flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
101 flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
102 flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
103
104 if (rebase_cousins >= 0 && !rebase_merges)
105 warning(_("--[no-]rebase-cousins has no effect without "
106 "--rebase-merges"));
107
108 if (command == CONTINUE && argc == 1)
109 return !!sequencer_continue(&opts);
110 if (command == ABORT && argc == 1)
111 return !!sequencer_remove_state(&opts);
112 if (command == MAKE_SCRIPT && argc == 1) {
113 char *revisions = NULL;
114 struct argv_array make_script_args = ARGV_ARRAY_INIT;
115
116 if (!upstream && squash_onto)
117 write_file(path_squash_onto(), "%s\n", squash_onto);
118
119 ret = get_revision_ranges(upstream, onto, &head_hash, &revisions);
120 if (ret)
121 return ret;
122
123 argv_array_pushl(&make_script_args, "", revisions, NULL);
124 if (restrict_revision)
125 argv_array_push(&make_script_args, restrict_revision);
126
127 ret = sequencer_make_script(stdout,
128 make_script_args.argc, make_script_args.argv,
129 flags);
130
131 free(revisions);
132 argv_array_clear(&make_script_args);
133
134 return !!ret;
135 }
136 if ((command == SHORTEN_OIDS || command == EXPAND_OIDS) && argc == 1)
137 return !!transform_todos(flags);
138 if (command == CHECK_TODO_LIST && argc == 1)
139 return !!check_todo_list();
140 if (command == REARRANGE_SQUASH && argc == 1)
141 return !!rearrange_squash();
142 if (command == ADD_EXEC && argc == 2)
143 return !!sequencer_add_exec_commands(argv[1]);
144 if (command == EDIT_TODO && argc == 1)
145 return !!edit_todo_list(flags);
146 if (command == PREPARE_BRANCH && argc == 2)
147 return !!prepare_branch_to_be_rebased(&opts, argv[1]);
148 if (command == COMPLETE_ACTION && argc == 6)
149 return !!complete_action(&opts, flags, argv[1], argv[2], argv[3],
150 argv[4], argv[5], autosquash);
151
152 usage_with_options(builtin_rebase_helper_usage, options);
153}