1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
5 #include "parse-options.h"
7 #include "rebase-interactive.h"
8 #include "argv-array.h"
11 #include "run-command.h"
13 static GIT_PATH_FUNC(path_state_dir, "rebase-merge/")
14 static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
15 static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
17 static int add_exec_commands(struct string_list *commands)
19 const char *todo_file = rebase_path_todo();
20 struct todo_list todo_list = TODO_LIST_INIT;
23 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
24 return error_errno(_("could not read '%s'."), todo_file);
26 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
28 todo_list_release(&todo_list);
29 return error(_("unusable todo list: '%s'"), todo_file);
32 todo_list_add_exec_commands(&todo_list, commands);
33 res = todo_list_write_to_file(the_repository, &todo_list,
34 todo_file, NULL, NULL, -1, 0);
35 todo_list_release(&todo_list);
38 return error_errno(_("could not write '%s'."), todo_file);
42 static int rearrange_squash_in_todo_file(void)
44 const char *todo_file = rebase_path_todo();
45 struct todo_list todo_list = TODO_LIST_INIT;
48 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
49 return error_errno(_("could not read '%s'."), todo_file);
50 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
52 todo_list_release(&todo_list);
53 return error(_("unusable todo list: '%s'"), todo_file);
56 res = todo_list_rearrange_squash(&todo_list);
58 res = todo_list_write_to_file(the_repository, &todo_list,
59 todo_file, NULL, NULL, -1, 0);
61 todo_list_release(&todo_list);
64 return error_errno(_("could not write '%s'."), todo_file);
68 static int transform_todo_file(unsigned flags)
70 const char *todo_file = rebase_path_todo();
71 struct todo_list todo_list = TODO_LIST_INIT;
74 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
75 return error_errno(_("could not read '%s'."), todo_file);
77 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
79 todo_list_release(&todo_list);
80 return error(_("unusable todo list: '%s'"), todo_file);
83 res = todo_list_write_to_file(the_repository, &todo_list, todo_file,
84 NULL, NULL, -1, flags);
85 todo_list_release(&todo_list);
88 return error_errno(_("could not write '%s'."), todo_file);
92 static int edit_todo_file(unsigned flags)
94 const char *todo_file = rebase_path_todo();
95 struct todo_list todo_list = TODO_LIST_INIT,
96 new_todo = TODO_LIST_INIT;
99 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
100 return error_errno(_("could not read '%s'."), todo_file);
102 strbuf_stripspace(&todo_list.buf, 1);
103 res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
104 if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
105 NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
106 res = error_errno(_("could not write '%s'"), todo_file);
108 todo_list_release(&todo_list);
109 todo_list_release(&new_todo);
114 static int get_revision_ranges(const char *upstream, const char *onto,
115 const char **head_hash,
116 char **revisions, char **shortrevisions)
118 const char *base_rev = upstream ? upstream : onto, *shorthead;
119 struct object_id orig_head;
121 if (get_oid("HEAD", &orig_head))
122 return error(_("no HEAD?"));
124 *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
125 *revisions = xstrfmt("%s...%s", base_rev, *head_hash);
127 shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV);
130 const char *shortrev;
131 struct object_id rev_oid;
133 get_oid(base_rev, &rev_oid);
134 shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV);
136 *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
138 *shortrevisions = xstrdup(shorthead);
143 static int init_basic_state(struct replay_opts *opts, const char *head_name,
144 const char *onto, const char *orig_head)
148 if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir()))
149 return error_errno(_("could not create temporary %s"), path_state_dir());
151 delete_reflog("REBASE_HEAD");
153 interactive = fopen(path_interactive(), "w");
155 return error_errno(_("could not mark as interactive"));
158 return write_basic_state(opts, head_name, onto, orig_head);
161 static int do_interactive_rebase(struct replay_opts *opts, unsigned flags,
162 const char *switch_to, const char *upstream,
163 const char *onto, const char *onto_name,
164 const char *squash_onto, const char *head_name,
165 const char *restrict_revision, char *raw_strategies,
166 struct string_list *commands, unsigned autosquash)
169 const char *head_hash = NULL;
170 char *revisions = NULL, *shortrevisions = NULL;
171 struct argv_array make_script_args = ARGV_ARRAY_INIT;
172 struct todo_list todo_list = TODO_LIST_INIT;
174 if (prepare_branch_to_be_rebased(opts, switch_to))
177 if (get_revision_ranges(upstream, onto, &head_hash,
178 &revisions, &shortrevisions))
182 parse_strategy_opts(opts, raw_strategies);
184 if (init_basic_state(opts, head_name, onto, head_hash)) {
186 free(shortrevisions);
191 if (!upstream && squash_onto)
192 write_file(path_squash_onto(), "%s\n", squash_onto);
194 argv_array_pushl(&make_script_args, "", revisions, NULL);
195 if (restrict_revision)
196 argv_array_push(&make_script_args, restrict_revision);
198 ret = sequencer_make_script(the_repository, &todo_list.buf,
199 make_script_args.argc, make_script_args.argv,
203 error(_("could not generate todo list"));
206 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
208 BUG("unusable todo list");
210 ret = complete_action(the_repository, opts, flags, shortrevisions, onto_name,
211 onto, head_hash, commands, autosquash, &todo_list);
215 free(shortrevisions);
216 todo_list_release(&todo_list);
217 argv_array_clear(&make_script_args);
222 static const char * const builtin_rebase_interactive_usage[] = {
223 N_("git rebase--interactive [<options>]"),
227 int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
229 struct replay_opts opts = REPLAY_OPTS_INIT;
230 unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
231 int abbreviate_commands = 0, rebase_cousins = -1, ret = 0;
232 const char *onto = NULL, *onto_name = NULL, *restrict_revision = NULL,
233 *squash_onto = NULL, *upstream = NULL, *head_name = NULL,
234 *switch_to = NULL, *cmd = NULL;
235 struct string_list commands = STRING_LIST_INIT_DUP;
236 char *raw_strategies = NULL;
238 NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH,
239 SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC
241 struct option options[] = {
242 OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
243 OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")),
244 OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
245 N_("allow commits with empty messages")),
246 OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
247 OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
248 N_("keep original branch points of cousins")),
249 OPT_BOOL(0, "autosquash", &autosquash,
250 N_("move commits that begin with squash!/fixup!")),
251 OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
252 OPT__VERBOSE(&opts.verbose, N_("be verbose")),
253 OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
255 OPT_CMDMODE(0, "skip", &command, N_("skip commit"), SKIP),
256 OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list"),
258 OPT_CMDMODE(0, "show-current-patch", &command, N_("show the current patch"),
260 OPT_CMDMODE(0, "shorten-ids", &command,
261 N_("shorten commit ids in the todo list"), SHORTEN_OIDS),
262 OPT_CMDMODE(0, "expand-ids", &command,
263 N_("expand commit ids in the todo list"), EXPAND_OIDS),
264 OPT_CMDMODE(0, "check-todo-list", &command,
265 N_("check the todo list"), CHECK_TODO_LIST),
266 OPT_CMDMODE(0, "rearrange-squash", &command,
267 N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
268 OPT_CMDMODE(0, "add-exec-commands", &command,
269 N_("insert exec commands in todo list"), ADD_EXEC),
270 OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
271 OPT_STRING(0, "restrict-revision", &restrict_revision,
272 N_("restrict-revision"), N_("restrict revision")),
273 OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
275 OPT_STRING(0, "upstream", &upstream, N_("upstream"),
276 N_("the upstream commit")),
277 OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")),
278 { OPTION_STRING, 'S', "gpg-sign", &opts.gpg_sign, N_("key-id"),
279 N_("GPG-sign commits"),
280 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
281 OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"),
282 N_("rebase strategy")),
283 OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"),
284 N_("strategy options")),
285 OPT_STRING(0, "switch-to", &switch_to, N_("switch-to"),
286 N_("the branch or commit to checkout")),
287 OPT_STRING(0, "onto-name", &onto_name, N_("onto-name"), N_("onto name")),
288 OPT_STRING(0, "cmd", &cmd, N_("cmd"), N_("the command to run")),
289 OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto),
290 OPT_BOOL(0, "reschedule-failed-exec", &opts.reschedule_failed_exec,
291 N_("automatically re-schedule any `exec` that fails")),
295 sequencer_init_config(&opts);
296 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
298 opts.action = REPLAY_INTERACTIVE_REBASE;
300 opts.allow_empty = 1;
303 usage_with_options(builtin_rebase_interactive_usage, options);
305 argc = parse_options(argc, argv, NULL, options,
306 builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0);
308 opts.gpg_sign = xstrdup_or_null(opts.gpg_sign);
310 flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
311 flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
312 flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
313 flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
314 flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
316 if (rebase_cousins >= 0 && !rebase_merges)
317 warning(_("--[no-]rebase-cousins has no effect without "
321 string_list_split(&commands, cmd, '\n', -1);
323 /* rebase.c adds a new line to cmd after every command,
324 * so here the last command is always empty */
325 string_list_remove_empty_items(&commands, 0);
330 if (!onto && !upstream)
331 die(_("a base commit must be provided with --upstream or --onto"));
333 ret = do_interactive_rebase(&opts, flags, switch_to, upstream, onto,
334 onto_name, squash_onto, head_name, restrict_revision,
335 raw_strategies, &commands, autosquash);
338 struct string_list merge_rr = STRING_LIST_INIT_DUP;
340 rerere_clear(the_repository, &merge_rr);
343 ret = sequencer_continue(the_repository, &opts);
347 ret = edit_todo_file(flags);
349 case SHOW_CURRENT_PATCH: {
350 struct child_process cmd = CHILD_PROCESS_INIT;
353 argv_array_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL);
354 ret = run_command(&cmd);
360 ret = transform_todo_file(flags);
362 case CHECK_TODO_LIST:
363 ret = check_todo_list_from_file(the_repository);
365 case REARRANGE_SQUASH:
366 ret = rearrange_squash_in_todo_file();
369 ret = add_exec_commands(&commands);
372 BUG("invalid command '%d'", command);
375 string_list_clear(&commands, 0);