1 /*
2  * "git rebase" builtin command
3  *
4  * Copyright (c) 2018 Pratik Karki
5  */
6 
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
8 #include "builtin.h"
9 #include "run-command.h"
10 #include "exec-cmd.h"
11 #include "strvec.h"
12 #include "dir.h"
13 #include "packfile.h"
14 #include "refs.h"
15 #include "quote.h"
16 #include "config.h"
17 #include "cache-tree.h"
18 #include "unpack-trees.h"
19 #include "lockfile.h"
20 #include "parse-options.h"
21 #include "commit.h"
22 #include "diff.h"
23 #include "wt-status.h"
24 #include "revision.h"
25 #include "commit-reach.h"
26 #include "rerere.h"
27 #include "branch.h"
28 #include "sequencer.h"
29 #include "rebase-interactive.h"
30 #include "reset.h"
31 
32 #define DEFAULT_REFLOG_ACTION "rebase"
33 
34 static char const * const builtin_rebase_usage[] = {
35 	N_("git rebase [-i] [options] [--exec <cmd>] "
36 		"[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
37 	N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
38 		"--root [<branch>]"),
39 	N_("git rebase --continue | --abort | --skip | --edit-todo"),
40 	NULL
41 };
42 
43 static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
44 static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
45 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
46 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
47 
48 enum rebase_type {
49 	REBASE_UNSPECIFIED = -1,
50 	REBASE_APPLY,
51 	REBASE_MERGE
52 };
53 
54 enum empty_type {
55 	EMPTY_UNSPECIFIED = -1,
56 	EMPTY_DROP,
57 	EMPTY_KEEP,
58 	EMPTY_ASK
59 };
60 
61 struct rebase_options {
62 	enum rebase_type type;
63 	enum empty_type empty;
64 	const char *default_backend;
65 	const char *state_dir;
66 	struct commit *upstream;
67 	const char *upstream_name;
68 	const char *upstream_arg;
69 	char *head_name;
70 	struct object_id orig_head;
71 	struct commit *onto;
72 	const char *onto_name;
73 	const char *revisions;
74 	const char *switch_to;
75 	int root, root_with_onto;
76 	struct object_id *squash_onto;
77 	struct commit *restrict_revision;
78 	int dont_finish_rebase;
79 	enum {
80 		REBASE_NO_QUIET = 1<<0,
81 		REBASE_VERBOSE = 1<<1,
82 		REBASE_DIFFSTAT = 1<<2,
83 		REBASE_FORCE = 1<<3,
84 		REBASE_INTERACTIVE_EXPLICIT = 1<<4,
85 	} flags;
86 	struct strvec git_am_opts;
87 	const char *action;
88 	int signoff;
89 	int allow_rerere_autoupdate;
90 	int keep_empty;
91 	int autosquash;
92 	char *gpg_sign_opt;
93 	int autostash;
94 	int committer_date_is_author_date;
95 	int ignore_date;
96 	char *cmd;
97 	int allow_empty_message;
98 	int rebase_merges, rebase_cousins;
99 	char *strategy, *strategy_opts;
100 	struct strbuf git_format_patch_opt;
101 	int reschedule_failed_exec;
102 	int reapply_cherry_picks;
103 	int fork_point;
104 };
105 
106 #define REBASE_OPTIONS_INIT {			  	\
107 		.type = REBASE_UNSPECIFIED,	  	\
108 		.empty = EMPTY_UNSPECIFIED,	  	\
109 		.keep_empty = 1,			\
110 		.default_backend = "merge",	  	\
111 		.flags = REBASE_NO_QUIET, 		\
112 		.git_am_opts = STRVEC_INIT,		\
113 		.git_format_patch_opt = STRBUF_INIT,	\
114 		.fork_point = -1,			\
115 	}
116 
get_replay_opts(const struct rebase_options * opts)117 static struct replay_opts get_replay_opts(const struct rebase_options *opts)
118 {
119 	struct replay_opts replay = REPLAY_OPTS_INIT;
120 
121 	replay.action = REPLAY_INTERACTIVE_REBASE;
122 	replay.strategy = NULL;
123 	sequencer_init_config(&replay);
124 
125 	replay.signoff = opts->signoff;
126 	replay.allow_ff = !(opts->flags & REBASE_FORCE);
127 	if (opts->allow_rerere_autoupdate)
128 		replay.allow_rerere_auto = opts->allow_rerere_autoupdate;
129 	replay.allow_empty = 1;
130 	replay.allow_empty_message = opts->allow_empty_message;
131 	replay.drop_redundant_commits = (opts->empty == EMPTY_DROP);
132 	replay.keep_redundant_commits = (opts->empty == EMPTY_KEEP);
133 	replay.quiet = !(opts->flags & REBASE_NO_QUIET);
134 	replay.verbose = opts->flags & REBASE_VERBOSE;
135 	replay.reschedule_failed_exec = opts->reschedule_failed_exec;
136 	replay.committer_date_is_author_date =
137 					opts->committer_date_is_author_date;
138 	replay.ignore_date = opts->ignore_date;
139 	replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
140 	if (opts->strategy)
141 		replay.strategy = xstrdup_or_null(opts->strategy);
142 	else if (!replay.strategy && replay.default_strategy) {
143 		replay.strategy = replay.default_strategy;
144 		replay.default_strategy = NULL;
145 	}
146 
147 	if (opts->strategy_opts)
148 		parse_strategy_opts(&replay, opts->strategy_opts);
149 
150 	if (opts->squash_onto) {
151 		oidcpy(&replay.squash_onto, opts->squash_onto);
152 		replay.have_squash_onto = 1;
153 	}
154 
155 	return replay;
156 }
157 
158 enum action {
159 	ACTION_NONE = 0,
160 	ACTION_CONTINUE,
161 	ACTION_SKIP,
162 	ACTION_ABORT,
163 	ACTION_QUIT,
164 	ACTION_EDIT_TODO,
165 	ACTION_SHOW_CURRENT_PATCH
166 };
167 
168 static const char *action_names[] = { "undefined",
169 				      "continue",
170 				      "skip",
171 				      "abort",
172 				      "quit",
173 				      "edit_todo",
174 				      "show_current_patch" };
175 
edit_todo_file(unsigned flags)176 static int edit_todo_file(unsigned flags)
177 {
178 	const char *todo_file = rebase_path_todo();
179 	struct todo_list todo_list = TODO_LIST_INIT,
180 		new_todo = TODO_LIST_INIT;
181 	int res = 0;
182 
183 	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
184 		return error_errno(_("could not read '%s'."), todo_file);
185 
186 	strbuf_stripspace(&todo_list.buf, 1);
187 	res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
188 	if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
189 					    NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
190 		res = error_errno(_("could not write '%s'"), todo_file);
191 
192 	todo_list_release(&todo_list);
193 	todo_list_release(&new_todo);
194 
195 	return res;
196 }
197 
get_revision_ranges(struct commit * upstream,struct commit * onto,struct object_id * orig_head,char ** revisions,char ** shortrevisions)198 static int get_revision_ranges(struct commit *upstream, struct commit *onto,
199 			       struct object_id *orig_head, char **revisions,
200 			       char **shortrevisions)
201 {
202 	struct commit *base_rev = upstream ? upstream : onto;
203 	const char *shorthead;
204 
205 	*revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid),
206 			     oid_to_hex(orig_head));
207 
208 	shorthead = find_unique_abbrev(orig_head, DEFAULT_ABBREV);
209 
210 	if (upstream) {
211 		const char *shortrev;
212 
213 		shortrev = find_unique_abbrev(&base_rev->object.oid,
214 					      DEFAULT_ABBREV);
215 
216 		*shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
217 	} else
218 		*shortrevisions = xstrdup(shorthead);
219 
220 	return 0;
221 }
222 
init_basic_state(struct replay_opts * opts,const char * head_name,struct commit * onto,const struct object_id * orig_head)223 static int init_basic_state(struct replay_opts *opts, const char *head_name,
224 			    struct commit *onto,
225 			    const struct object_id *orig_head)
226 {
227 	FILE *interactive;
228 
229 	if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir()))
230 		return error_errno(_("could not create temporary %s"), merge_dir());
231 
232 	delete_reflog("REBASE_HEAD");
233 
234 	interactive = fopen(path_interactive(), "w");
235 	if (!interactive)
236 		return error_errno(_("could not mark as interactive"));
237 	fclose(interactive);
238 
239 	return write_basic_state(opts, head_name, onto, orig_head);
240 }
241 
split_exec_commands(const char * cmd,struct string_list * commands)242 static void split_exec_commands(const char *cmd, struct string_list *commands)
243 {
244 	if (cmd && *cmd) {
245 		string_list_split(commands, cmd, '\n', -1);
246 
247 		/* rebase.c adds a new line to cmd after every command,
248 		 * so here the last command is always empty */
249 		string_list_remove_empty_items(commands, 0);
250 	}
251 }
252 
do_interactive_rebase(struct rebase_options * opts,unsigned flags)253 static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
254 {
255 	int ret;
256 	char *revisions = NULL, *shortrevisions = NULL;
257 	struct strvec make_script_args = STRVEC_INIT;
258 	struct todo_list todo_list = TODO_LIST_INIT;
259 	struct replay_opts replay = get_replay_opts(opts);
260 	struct string_list commands = STRING_LIST_INIT_DUP;
261 
262 	if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head,
263 				&revisions, &shortrevisions))
264 		return -1;
265 
266 	if (init_basic_state(&replay,
267 			     opts->head_name ? opts->head_name : "detached HEAD",
268 			     opts->onto, &opts->orig_head)) {
269 		free(revisions);
270 		free(shortrevisions);
271 
272 		return -1;
273 	}
274 
275 	if (!opts->upstream && opts->squash_onto)
276 		write_file(path_squash_onto(), "%s\n",
277 			   oid_to_hex(opts->squash_onto));
278 
279 	strvec_pushl(&make_script_args, "", revisions, NULL);
280 	if (opts->restrict_revision)
281 		strvec_pushf(&make_script_args, "^%s",
282 			     oid_to_hex(&opts->restrict_revision->object.oid));
283 
284 	ret = sequencer_make_script(the_repository, &todo_list.buf,
285 				    make_script_args.nr, make_script_args.v,
286 				    flags);
287 
288 	if (ret)
289 		error(_("could not generate todo list"));
290 	else {
291 		discard_cache();
292 		if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
293 						&todo_list))
294 			BUG("unusable todo list");
295 
296 		split_exec_commands(opts->cmd, &commands);
297 		ret = complete_action(the_repository, &replay, flags,
298 			shortrevisions, opts->onto_name, opts->onto,
299 			&opts->orig_head, &commands, opts->autosquash,
300 			&todo_list);
301 	}
302 
303 	string_list_clear(&commands, 0);
304 	free(revisions);
305 	free(shortrevisions);
306 	todo_list_release(&todo_list);
307 	strvec_clear(&make_script_args);
308 
309 	return ret;
310 }
311 
run_sequencer_rebase(struct rebase_options * opts,enum action command)312 static int run_sequencer_rebase(struct rebase_options *opts,
313 				  enum action command)
314 {
315 	unsigned flags = 0;
316 	int abbreviate_commands = 0, ret = 0;
317 
318 	git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
319 
320 	flags |= opts->keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
321 	flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
322 	flags |= opts->rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
323 	flags |= opts->rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
324 	flags |= opts->root_with_onto ? TODO_LIST_ROOT_WITH_ONTO : 0;
325 	flags |= opts->reapply_cherry_picks ? TODO_LIST_REAPPLY_CHERRY_PICKS : 0;
326 	flags |= opts->flags & REBASE_NO_QUIET ? TODO_LIST_WARN_SKIPPED_CHERRY_PICKS : 0;
327 
328 	switch (command) {
329 	case ACTION_NONE: {
330 		if (!opts->onto && !opts->upstream)
331 			die(_("a base commit must be provided with --upstream or --onto"));
332 
333 		ret = do_interactive_rebase(opts, flags);
334 		break;
335 	}
336 	case ACTION_SKIP: {
337 		struct string_list merge_rr = STRING_LIST_INIT_DUP;
338 
339 		rerere_clear(the_repository, &merge_rr);
340 	}
341 		/* fallthrough */
342 	case ACTION_CONTINUE: {
343 		struct replay_opts replay_opts = get_replay_opts(opts);
344 
345 		ret = sequencer_continue(the_repository, &replay_opts);
346 		break;
347 	}
348 	case ACTION_EDIT_TODO:
349 		ret = edit_todo_file(flags);
350 		break;
351 	case ACTION_SHOW_CURRENT_PATCH: {
352 		struct child_process cmd = CHILD_PROCESS_INIT;
353 
354 		cmd.git_cmd = 1;
355 		strvec_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL);
356 		ret = run_command(&cmd);
357 
358 		break;
359 	}
360 	default:
361 		BUG("invalid command '%d'", command);
362 	}
363 
364 	return ret;
365 }
366 
367 static void imply_merge(struct rebase_options *opts, const char *option);
parse_opt_keep_empty(const struct option * opt,const char * arg,int unset)368 static int parse_opt_keep_empty(const struct option *opt, const char *arg,
369 				int unset)
370 {
371 	struct rebase_options *opts = opt->value;
372 
373 	BUG_ON_OPT_ARG(arg);
374 
375 	imply_merge(opts, unset ? "--no-keep-empty" : "--keep-empty");
376 	opts->keep_empty = !unset;
377 	opts->type = REBASE_MERGE;
378 	return 0;
379 }
380 
is_merge(struct rebase_options * opts)381 static int is_merge(struct rebase_options *opts)
382 {
383 	return opts->type == REBASE_MERGE;
384 }
385 
imply_merge(struct rebase_options * opts,const char * option)386 static void imply_merge(struct rebase_options *opts, const char *option)
387 {
388 	switch (opts->type) {
389 	case REBASE_APPLY:
390 		die(_("%s requires the merge backend"), option);
391 		break;
392 	case REBASE_MERGE:
393 		break;
394 	default:
395 		opts->type = REBASE_MERGE; /* implied */
396 		break;
397 	}
398 }
399 
400 /* Returns the filename prefixed by the state_dir */
state_dir_path(const char * filename,struct rebase_options * opts)401 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
402 {
403 	static struct strbuf path = STRBUF_INIT;
404 	static size_t prefix_len;
405 
406 	if (!prefix_len) {
407 		strbuf_addf(&path, "%s/", opts->state_dir);
408 		prefix_len = path.len;
409 	}
410 
411 	strbuf_setlen(&path, prefix_len);
412 	strbuf_addstr(&path, filename);
413 	return path.buf;
414 }
415 
416 /* Initialize the rebase options from the state directory. */
read_basic_state(struct rebase_options * opts)417 static int read_basic_state(struct rebase_options *opts)
418 {
419 	struct strbuf head_name = STRBUF_INIT;
420 	struct strbuf buf = STRBUF_INIT;
421 	struct object_id oid;
422 
423 	if (!read_oneliner(&head_name, state_dir_path("head-name", opts),
424 			   READ_ONELINER_WARN_MISSING) ||
425 	    !read_oneliner(&buf, state_dir_path("onto", opts),
426 			   READ_ONELINER_WARN_MISSING))
427 		return -1;
428 	opts->head_name = starts_with(head_name.buf, "refs/") ?
429 		xstrdup(head_name.buf) : NULL;
430 	strbuf_release(&head_name);
431 	if (get_oid(buf.buf, &oid))
432 		return error(_("could not get 'onto': '%s'"), buf.buf);
433 	opts->onto = lookup_commit_or_die(&oid, buf.buf);
434 
435 	/*
436 	 * We always write to orig-head, but interactive rebase used to write to
437 	 * head. Fall back to reading from head to cover for the case that the
438 	 * user upgraded git with an ongoing interactive rebase.
439 	 */
440 	strbuf_reset(&buf);
441 	if (file_exists(state_dir_path("orig-head", opts))) {
442 		if (!read_oneliner(&buf, state_dir_path("orig-head", opts),
443 				   READ_ONELINER_WARN_MISSING))
444 			return -1;
445 	} else if (!read_oneliner(&buf, state_dir_path("head", opts),
446 				  READ_ONELINER_WARN_MISSING))
447 		return -1;
448 	if (get_oid(buf.buf, &opts->orig_head))
449 		return error(_("invalid orig-head: '%s'"), buf.buf);
450 
451 	if (file_exists(state_dir_path("quiet", opts)))
452 		opts->flags &= ~REBASE_NO_QUIET;
453 	else
454 		opts->flags |= REBASE_NO_QUIET;
455 
456 	if (file_exists(state_dir_path("verbose", opts)))
457 		opts->flags |= REBASE_VERBOSE;
458 
459 	if (file_exists(state_dir_path("signoff", opts))) {
460 		opts->signoff = 1;
461 		opts->flags |= REBASE_FORCE;
462 	}
463 
464 	if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
465 		strbuf_reset(&buf);
466 		if (!read_oneliner(&buf, state_dir_path("allow_rerere_autoupdate", opts),
467 				   READ_ONELINER_WARN_MISSING))
468 			return -1;
469 		if (!strcmp(buf.buf, "--rerere-autoupdate"))
470 			opts->allow_rerere_autoupdate = RERERE_AUTOUPDATE;
471 		else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
472 			opts->allow_rerere_autoupdate = RERERE_NOAUTOUPDATE;
473 		else
474 			warning(_("ignoring invalid allow_rerere_autoupdate: "
475 				  "'%s'"), buf.buf);
476 	}
477 
478 	if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
479 		strbuf_reset(&buf);
480 		if (!read_oneliner(&buf, state_dir_path("gpg_sign_opt", opts),
481 				   READ_ONELINER_WARN_MISSING))
482 			return -1;
483 		free(opts->gpg_sign_opt);
484 		opts->gpg_sign_opt = xstrdup(buf.buf);
485 	}
486 
487 	if (file_exists(state_dir_path("strategy", opts))) {
488 		strbuf_reset(&buf);
489 		if (!read_oneliner(&buf, state_dir_path("strategy", opts),
490 				   READ_ONELINER_WARN_MISSING))
491 			return -1;
492 		free(opts->strategy);
493 		opts->strategy = xstrdup(buf.buf);
494 	}
495 
496 	if (file_exists(state_dir_path("strategy_opts", opts))) {
497 		strbuf_reset(&buf);
498 		if (!read_oneliner(&buf, state_dir_path("strategy_opts", opts),
499 				   READ_ONELINER_WARN_MISSING))
500 			return -1;
501 		free(opts->strategy_opts);
502 		opts->strategy_opts = xstrdup(buf.buf);
503 	}
504 
505 	strbuf_release(&buf);
506 
507 	return 0;
508 }
509 
rebase_write_basic_state(struct rebase_options * opts)510 static int rebase_write_basic_state(struct rebase_options *opts)
511 {
512 	write_file(state_dir_path("head-name", opts), "%s",
513 		   opts->head_name ? opts->head_name : "detached HEAD");
514 	write_file(state_dir_path("onto", opts), "%s",
515 		   opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
516 	write_file(state_dir_path("orig-head", opts), "%s",
517 		   oid_to_hex(&opts->orig_head));
518 	if (!(opts->flags & REBASE_NO_QUIET))
519 		write_file(state_dir_path("quiet", opts), "%s", "");
520 	if (opts->flags & REBASE_VERBOSE)
521 		write_file(state_dir_path("verbose", opts), "%s", "");
522 	if (opts->strategy)
523 		write_file(state_dir_path("strategy", opts), "%s",
524 			   opts->strategy);
525 	if (opts->strategy_opts)
526 		write_file(state_dir_path("strategy_opts", opts), "%s",
527 			   opts->strategy_opts);
528 	if (opts->allow_rerere_autoupdate > 0)
529 		write_file(state_dir_path("allow_rerere_autoupdate", opts),
530 			   "-%s-rerere-autoupdate",
531 			   opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ?
532 				"" : "-no");
533 	if (opts->gpg_sign_opt)
534 		write_file(state_dir_path("gpg_sign_opt", opts), "%s",
535 			   opts->gpg_sign_opt);
536 	if (opts->signoff)
537 		write_file(state_dir_path("signoff", opts), "--signoff");
538 
539 	return 0;
540 }
541 
finish_rebase(struct rebase_options * opts)542 static int finish_rebase(struct rebase_options *opts)
543 {
544 	struct strbuf dir = STRBUF_INIT;
545 	int ret = 0;
546 
547 	delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
548 	unlink(git_path_auto_merge(the_repository));
549 	apply_autostash(state_dir_path("autostash", opts));
550 	/*
551 	 * We ignore errors in 'git maintenance run --auto', since the
552 	 * user should see them.
553 	 */
554 	run_auto_maintenance(!(opts->flags & (REBASE_NO_QUIET|REBASE_VERBOSE)));
555 	if (opts->type == REBASE_MERGE) {
556 		struct replay_opts replay = REPLAY_OPTS_INIT;
557 
558 		replay.action = REPLAY_INTERACTIVE_REBASE;
559 		ret = sequencer_remove_state(&replay);
560 	} else {
561 		strbuf_addstr(&dir, opts->state_dir);
562 		if (remove_dir_recursively(&dir, 0))
563 			ret = error(_("could not remove '%s'"),
564 				    opts->state_dir);
565 		strbuf_release(&dir);
566 	}
567 
568 	return ret;
569 }
570 
move_to_original_branch(struct rebase_options * opts)571 static int move_to_original_branch(struct rebase_options *opts)
572 {
573 	struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
574 	int ret;
575 
576 	if (!opts->head_name)
577 		return 0; /* nothing to move back to */
578 
579 	if (!opts->onto)
580 		BUG("move_to_original_branch without onto");
581 
582 	strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s",
583 		    opts->head_name, oid_to_hex(&opts->onto->object.oid));
584 	strbuf_addf(&head_reflog, "rebase finished: returning to %s",
585 		    opts->head_name);
586 	ret = reset_head(the_repository, NULL, "", opts->head_name,
587 			 RESET_HEAD_REFS_ONLY,
588 			 orig_head_reflog.buf, head_reflog.buf,
589 			 DEFAULT_REFLOG_ACTION);
590 
591 	strbuf_release(&orig_head_reflog);
592 	strbuf_release(&head_reflog);
593 	return ret;
594 }
595 
596 static const char *resolvemsg =
597 N_("Resolve all conflicts manually, mark them as resolved with\n"
598 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
599 "You can instead skip this commit: run \"git rebase --skip\".\n"
600 "To abort and get back to the state before \"git rebase\", run "
601 "\"git rebase --abort\".");
602 
run_am(struct rebase_options * opts)603 static int run_am(struct rebase_options *opts)
604 {
605 	struct child_process am = CHILD_PROCESS_INIT;
606 	struct child_process format_patch = CHILD_PROCESS_INIT;
607 	struct strbuf revisions = STRBUF_INIT;
608 	int status;
609 	char *rebased_patches;
610 
611 	am.git_cmd = 1;
612 	strvec_push(&am.args, "am");
613 
614 	if (opts->action && !strcmp("continue", opts->action)) {
615 		strvec_push(&am.args, "--resolved");
616 		strvec_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
617 		if (opts->gpg_sign_opt)
618 			strvec_push(&am.args, opts->gpg_sign_opt);
619 		status = run_command(&am);
620 		if (status)
621 			return status;
622 
623 		return move_to_original_branch(opts);
624 	}
625 	if (opts->action && !strcmp("skip", opts->action)) {
626 		strvec_push(&am.args, "--skip");
627 		strvec_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
628 		status = run_command(&am);
629 		if (status)
630 			return status;
631 
632 		return move_to_original_branch(opts);
633 	}
634 	if (opts->action && !strcmp("show-current-patch", opts->action)) {
635 		strvec_push(&am.args, "--show-current-patch");
636 		return run_command(&am);
637 	}
638 
639 	strbuf_addf(&revisions, "%s...%s",
640 		    oid_to_hex(opts->root ?
641 			       /* this is now equivalent to !opts->upstream */
642 			       &opts->onto->object.oid :
643 			       &opts->upstream->object.oid),
644 		    oid_to_hex(&opts->orig_head));
645 
646 	rebased_patches = xstrdup(git_path("rebased-patches"));
647 	format_patch.out = open(rebased_patches,
648 				O_WRONLY | O_CREAT | O_TRUNC, 0666);
649 	if (format_patch.out < 0) {
650 		status = error_errno(_("could not open '%s' for writing"),
651 				     rebased_patches);
652 		free(rebased_patches);
653 		strvec_clear(&am.args);
654 		return status;
655 	}
656 
657 	format_patch.git_cmd = 1;
658 	strvec_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
659 		     "--full-index", "--cherry-pick", "--right-only",
660 		     "--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
661 		     "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
662 		     "--no-base", NULL);
663 	if (opts->git_format_patch_opt.len)
664 		strvec_split(&format_patch.args,
665 			     opts->git_format_patch_opt.buf);
666 	strvec_push(&format_patch.args, revisions.buf);
667 	if (opts->restrict_revision)
668 		strvec_pushf(&format_patch.args, "^%s",
669 			     oid_to_hex(&opts->restrict_revision->object.oid));
670 
671 	status = run_command(&format_patch);
672 	if (status) {
673 		unlink(rebased_patches);
674 		free(rebased_patches);
675 		strvec_clear(&am.args);
676 
677 		reset_head(the_repository, &opts->orig_head, "checkout",
678 			   opts->head_name, 0,
679 			   "HEAD", NULL, DEFAULT_REFLOG_ACTION);
680 		error(_("\ngit encountered an error while preparing the "
681 			"patches to replay\n"
682 			"these revisions:\n"
683 			"\n    %s\n\n"
684 			"As a result, git cannot rebase them."),
685 		      opts->revisions);
686 
687 		strbuf_release(&revisions);
688 		return status;
689 	}
690 	strbuf_release(&revisions);
691 
692 	am.in = open(rebased_patches, O_RDONLY);
693 	if (am.in < 0) {
694 		status = error_errno(_("could not open '%s' for reading"),
695 				     rebased_patches);
696 		free(rebased_patches);
697 		strvec_clear(&am.args);
698 		return status;
699 	}
700 
701 	strvec_pushv(&am.args, opts->git_am_opts.v);
702 	strvec_push(&am.args, "--rebasing");
703 	strvec_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
704 	strvec_push(&am.args, "--patch-format=mboxrd");
705 	if (opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE)
706 		strvec_push(&am.args, "--rerere-autoupdate");
707 	else if (opts->allow_rerere_autoupdate == RERERE_NOAUTOUPDATE)
708 		strvec_push(&am.args, "--no-rerere-autoupdate");
709 	if (opts->gpg_sign_opt)
710 		strvec_push(&am.args, opts->gpg_sign_opt);
711 	status = run_command(&am);
712 	unlink(rebased_patches);
713 	free(rebased_patches);
714 
715 	if (!status) {
716 		return move_to_original_branch(opts);
717 	}
718 
719 	if (is_directory(opts->state_dir))
720 		rebase_write_basic_state(opts);
721 
722 	return status;
723 }
724 
run_specific_rebase(struct rebase_options * opts,enum action action)725 static int run_specific_rebase(struct rebase_options *opts, enum action action)
726 {
727 	int status;
728 
729 	if (opts->type == REBASE_MERGE) {
730 		/* Run sequencer-based rebase */
731 		setenv("GIT_CHERRY_PICK_HELP", resolvemsg, 1);
732 		if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
733 			setenv("GIT_SEQUENCE_EDITOR", ":", 1);
734 			opts->autosquash = 0;
735 		}
736 		if (opts->gpg_sign_opt) {
737 			/* remove the leading "-S" */
738 			char *tmp = xstrdup(opts->gpg_sign_opt + 2);
739 			free(opts->gpg_sign_opt);
740 			opts->gpg_sign_opt = tmp;
741 		}
742 
743 		status = run_sequencer_rebase(opts, action);
744 	} else if (opts->type == REBASE_APPLY)
745 		status = run_am(opts);
746 	else
747 		BUG("Unhandled rebase type %d", opts->type);
748 
749 	if (opts->dont_finish_rebase)
750 		; /* do nothing */
751 	else if (opts->type == REBASE_MERGE)
752 		; /* merge backend cleans up after itself */
753 	else if (status == 0) {
754 		if (!file_exists(state_dir_path("stopped-sha", opts)))
755 			finish_rebase(opts);
756 	} else if (status == 2) {
757 		struct strbuf dir = STRBUF_INIT;
758 
759 		apply_autostash(state_dir_path("autostash", opts));
760 		strbuf_addstr(&dir, opts->state_dir);
761 		remove_dir_recursively(&dir, 0);
762 		strbuf_release(&dir);
763 		die("Nothing to do");
764 	}
765 
766 	return status ? -1 : 0;
767 }
768 
rebase_config(const char * var,const char * value,void * data)769 static int rebase_config(const char *var, const char *value, void *data)
770 {
771 	struct rebase_options *opts = data;
772 
773 	if (!strcmp(var, "rebase.stat")) {
774 		if (git_config_bool(var, value))
775 			opts->flags |= REBASE_DIFFSTAT;
776 		else
777 			opts->flags &= ~REBASE_DIFFSTAT;
778 		return 0;
779 	}
780 
781 	if (!strcmp(var, "rebase.autosquash")) {
782 		opts->autosquash = git_config_bool(var, value);
783 		return 0;
784 	}
785 
786 	if (!strcmp(var, "commit.gpgsign")) {
787 		free(opts->gpg_sign_opt);
788 		opts->gpg_sign_opt = git_config_bool(var, value) ?
789 			xstrdup("-S") : NULL;
790 		return 0;
791 	}
792 
793 	if (!strcmp(var, "rebase.autostash")) {
794 		opts->autostash = git_config_bool(var, value);
795 		return 0;
796 	}
797 
798 	if (!strcmp(var, "rebase.reschedulefailedexec")) {
799 		opts->reschedule_failed_exec = git_config_bool(var, value);
800 		return 0;
801 	}
802 
803 	if (!strcmp(var, "rebase.forkpoint")) {
804 		opts->fork_point = git_config_bool(var, value) ? -1 : 0;
805 		return 0;
806 	}
807 
808 	if (!strcmp(var, "rebase.backend")) {
809 		return git_config_string(&opts->default_backend, var, value);
810 	}
811 
812 	return git_default_config(var, value, data);
813 }
814 
815 /*
816  * Determines whether the commits in from..to are linear, i.e. contain
817  * no merge commits. This function *expects* `from` to be an ancestor of
818  * `to`.
819  */
is_linear_history(struct commit * from,struct commit * to)820 static int is_linear_history(struct commit *from, struct commit *to)
821 {
822 	while (to && to != from) {
823 		parse_commit(to);
824 		if (!to->parents)
825 			return 1;
826 		if (to->parents->next)
827 			return 0;
828 		to = to->parents->item;
829 	}
830 	return 1;
831 }
832 
can_fast_forward(struct commit * onto,struct commit * upstream,struct commit * restrict_revision,struct object_id * head_oid,struct object_id * merge_base)833 static int can_fast_forward(struct commit *onto, struct commit *upstream,
834 			    struct commit *restrict_revision,
835 			    struct object_id *head_oid, struct object_id *merge_base)
836 {
837 	struct commit *head = lookup_commit(the_repository, head_oid);
838 	struct commit_list *merge_bases = NULL;
839 	int res = 0;
840 
841 	if (!head)
842 		goto done;
843 
844 	merge_bases = get_merge_bases(onto, head);
845 	if (!merge_bases || merge_bases->next) {
846 		oidcpy(merge_base, null_oid());
847 		goto done;
848 	}
849 
850 	oidcpy(merge_base, &merge_bases->item->object.oid);
851 	if (!oideq(merge_base, &onto->object.oid))
852 		goto done;
853 
854 	if (restrict_revision && !oideq(&restrict_revision->object.oid, merge_base))
855 		goto done;
856 
857 	if (!upstream)
858 		goto done;
859 
860 	free_commit_list(merge_bases);
861 	merge_bases = get_merge_bases(upstream, head);
862 	if (!merge_bases || merge_bases->next)
863 		goto done;
864 
865 	if (!oideq(&onto->object.oid, &merge_bases->item->object.oid))
866 		goto done;
867 
868 	res = 1;
869 
870 done:
871 	free_commit_list(merge_bases);
872 	return res && is_linear_history(onto, head);
873 }
874 
parse_opt_am(const struct option * opt,const char * arg,int unset)875 static int parse_opt_am(const struct option *opt, const char *arg, int unset)
876 {
877 	struct rebase_options *opts = opt->value;
878 
879 	BUG_ON_OPT_NEG(unset);
880 	BUG_ON_OPT_ARG(arg);
881 
882 	opts->type = REBASE_APPLY;
883 
884 	return 0;
885 }
886 
887 /* -i followed by -m is still -i */
parse_opt_merge(const struct option * opt,const char * arg,int unset)888 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
889 {
890 	struct rebase_options *opts = opt->value;
891 
892 	BUG_ON_OPT_NEG(unset);
893 	BUG_ON_OPT_ARG(arg);
894 
895 	if (!is_merge(opts))
896 		opts->type = REBASE_MERGE;
897 
898 	return 0;
899 }
900 
901 /* -i followed by -r is still explicitly interactive, but -r alone is not */
parse_opt_interactive(const struct option * opt,const char * arg,int unset)902 static int parse_opt_interactive(const struct option *opt, const char *arg,
903 				 int unset)
904 {
905 	struct rebase_options *opts = opt->value;
906 
907 	BUG_ON_OPT_NEG(unset);
908 	BUG_ON_OPT_ARG(arg);
909 
910 	opts->type = REBASE_MERGE;
911 	opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
912 
913 	return 0;
914 }
915 
parse_empty_value(const char * value)916 static enum empty_type parse_empty_value(const char *value)
917 {
918 	if (!strcasecmp(value, "drop"))
919 		return EMPTY_DROP;
920 	else if (!strcasecmp(value, "keep"))
921 		return EMPTY_KEEP;
922 	else if (!strcasecmp(value, "ask"))
923 		return EMPTY_ASK;
924 
925 	die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"ask\"."), value);
926 }
927 
parse_opt_empty(const struct option * opt,const char * arg,int unset)928 static int parse_opt_empty(const struct option *opt, const char *arg, int unset)
929 {
930 	struct rebase_options *options = opt->value;
931 	enum empty_type value = parse_empty_value(arg);
932 
933 	BUG_ON_OPT_NEG(unset);
934 
935 	options->empty = value;
936 	return 0;
937 }
938 
error_on_missing_default_upstream(void)939 static void NORETURN error_on_missing_default_upstream(void)
940 {
941 	struct branch *current_branch = branch_get(NULL);
942 
943 	printf(_("%s\n"
944 		 "Please specify which branch you want to rebase against.\n"
945 		 "See git-rebase(1) for details.\n"
946 		 "\n"
947 		 "    git rebase '<branch>'\n"
948 		 "\n"),
949 		current_branch ? _("There is no tracking information for "
950 			"the current branch.") :
951 			_("You are not currently on a branch."));
952 
953 	if (current_branch) {
954 		const char *remote = current_branch->remote_name;
955 
956 		if (!remote)
957 			remote = _("<remote>");
958 
959 		printf(_("If you wish to set tracking information for this "
960 			 "branch you can do so with:\n"
961 			 "\n"
962 			 "    git branch --set-upstream-to=%s/<branch> %s\n"
963 			 "\n"),
964 		       remote, current_branch->name);
965 	}
966 	exit(1);
967 }
968 
set_reflog_action(struct rebase_options * options)969 static void set_reflog_action(struct rebase_options *options)
970 {
971 	const char *env;
972 	struct strbuf buf = STRBUF_INIT;
973 
974 	if (!is_merge(options))
975 		return;
976 
977 	env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
978 	if (env && strcmp("rebase", env))
979 		return; /* only override it if it is "rebase" */
980 
981 	strbuf_addf(&buf, "rebase (%s)", options->action);
982 	setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1);
983 	strbuf_release(&buf);
984 }
985 
check_exec_cmd(const char * cmd)986 static int check_exec_cmd(const char *cmd)
987 {
988 	if (strchr(cmd, '\n'))
989 		return error(_("exec commands cannot contain newlines"));
990 
991 	/* Does the command consist purely of whitespace? */
992 	if (!cmd[strspn(cmd, " \t\r\f\v")])
993 		return error(_("empty exec command"));
994 
995 	return 0;
996 }
997 
cmd_rebase(int argc,const char ** argv,const char * prefix)998 int cmd_rebase(int argc, const char **argv, const char *prefix)
999 {
1000 	struct rebase_options options = REBASE_OPTIONS_INIT;
1001 	const char *branch_name;
1002 	int ret, flags, total_argc, in_progress = 0;
1003 	int keep_base = 0;
1004 	int ok_to_skip_pre_rebase = 0;
1005 	struct strbuf msg = STRBUF_INIT;
1006 	struct strbuf revisions = STRBUF_INIT;
1007 	struct strbuf buf = STRBUF_INIT;
1008 	struct object_id merge_base;
1009 	int ignore_whitespace = 0;
1010 	enum action action = ACTION_NONE;
1011 	const char *gpg_sign = NULL;
1012 	struct string_list exec = STRING_LIST_INIT_NODUP;
1013 	const char *rebase_merges = NULL;
1014 	struct string_list strategy_options = STRING_LIST_INIT_NODUP;
1015 	struct object_id squash_onto;
1016 	char *squash_onto_name = NULL;
1017 	int reschedule_failed_exec = -1;
1018 	int allow_preemptive_ff = 1;
1019 	int preserve_merges_selected = 0;
1020 	struct option builtin_rebase_options[] = {
1021 		OPT_STRING(0, "onto", &options.onto_name,
1022 			   N_("revision"),
1023 			   N_("rebase onto given branch instead of upstream")),
1024 		OPT_BOOL(0, "keep-base", &keep_base,
1025 			 N_("use the merge-base of upstream and branch as the current base")),
1026 		OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
1027 			 N_("allow pre-rebase hook to run")),
1028 		OPT_NEGBIT('q', "quiet", &options.flags,
1029 			   N_("be quiet. implies --no-stat"),
1030 			   REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1031 		OPT_BIT('v', "verbose", &options.flags,
1032 			N_("display a diffstat of what changed upstream"),
1033 			REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1034 		{OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
1035 			N_("do not show diffstat of what changed upstream"),
1036 			PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
1037 		OPT_BOOL(0, "signoff", &options.signoff,
1038 			 N_("add a Signed-off-by trailer to each commit")),
1039 		OPT_BOOL(0, "committer-date-is-author-date",
1040 			 &options.committer_date_is_author_date,
1041 			 N_("make committer date match author date")),
1042 		OPT_BOOL(0, "reset-author-date", &options.ignore_date,
1043 			 N_("ignore author date and use current date")),
1044 		OPT_HIDDEN_BOOL(0, "ignore-date", &options.ignore_date,
1045 				N_("synonym of --reset-author-date")),
1046 		OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
1047 				  N_("passed to 'git apply'"), 0),
1048 		OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
1049 			 N_("ignore changes in whitespace")),
1050 		OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
1051 				  N_("action"), N_("passed to 'git apply'"), 0),
1052 		OPT_BIT('f', "force-rebase", &options.flags,
1053 			N_("cherry-pick all commits, even if unchanged"),
1054 			REBASE_FORCE),
1055 		OPT_BIT(0, "no-ff", &options.flags,
1056 			N_("cherry-pick all commits, even if unchanged"),
1057 			REBASE_FORCE),
1058 		OPT_CMDMODE(0, "continue", &action, N_("continue"),
1059 			    ACTION_CONTINUE),
1060 		OPT_CMDMODE(0, "skip", &action,
1061 			    N_("skip current patch and continue"), ACTION_SKIP),
1062 		OPT_CMDMODE(0, "abort", &action,
1063 			    N_("abort and check out the original branch"),
1064 			    ACTION_ABORT),
1065 		OPT_CMDMODE(0, "quit", &action,
1066 			    N_("abort but keep HEAD where it is"), ACTION_QUIT),
1067 		OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
1068 			    "during an interactive rebase"), ACTION_EDIT_TODO),
1069 		OPT_CMDMODE(0, "show-current-patch", &action,
1070 			    N_("show the patch file being applied or merged"),
1071 			    ACTION_SHOW_CURRENT_PATCH),
1072 		OPT_CALLBACK_F(0, "apply", &options, NULL,
1073 			N_("use apply strategies to rebase"),
1074 			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1075 			parse_opt_am),
1076 		OPT_CALLBACK_F('m', "merge", &options, NULL,
1077 			N_("use merging strategies to rebase"),
1078 			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1079 			parse_opt_merge),
1080 		OPT_CALLBACK_F('i', "interactive", &options, NULL,
1081 			N_("let the user edit the list of commits to rebase"),
1082 			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1083 			parse_opt_interactive),
1084 		OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected,
1085 			      N_("(DEPRECATED) try to recreate merges instead of "
1086 				 "ignoring them"),
1087 			      1, PARSE_OPT_HIDDEN),
1088 		OPT_RERERE_AUTOUPDATE(&options.allow_rerere_autoupdate),
1089 		OPT_CALLBACK_F(0, "empty", &options, "{drop,keep,ask}",
1090 			       N_("how to handle commits that become empty"),
1091 			       PARSE_OPT_NONEG, parse_opt_empty),
1092 		OPT_CALLBACK_F('k', "keep-empty", &options, NULL,
1093 			N_("keep commits which start empty"),
1094 			PARSE_OPT_NOARG | PARSE_OPT_HIDDEN,
1095 			parse_opt_keep_empty),
1096 		OPT_BOOL(0, "autosquash", &options.autosquash,
1097 			 N_("move commits that begin with "
1098 			    "squash!/fixup! under -i")),
1099 		{ OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
1100 			N_("GPG-sign commits"),
1101 			PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1102 		OPT_AUTOSTASH(&options.autostash),
1103 		OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
1104 				N_("add exec lines after each commit of the "
1105 				   "editable list")),
1106 		OPT_BOOL_F(0, "allow-empty-message",
1107 			   &options.allow_empty_message,
1108 			   N_("allow rebasing commits with empty messages"),
1109 			   PARSE_OPT_HIDDEN),
1110 		{OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
1111 			N_("mode"),
1112 			N_("try to rebase merges instead of skipping them"),
1113 			PARSE_OPT_OPTARG, NULL, (intptr_t)""},
1114 		OPT_BOOL(0, "fork-point", &options.fork_point,
1115 			 N_("use 'merge-base --fork-point' to refine upstream")),
1116 		OPT_STRING('s', "strategy", &options.strategy,
1117 			   N_("strategy"), N_("use the given merge strategy")),
1118 		OPT_STRING_LIST('X', "strategy-option", &strategy_options,
1119 				N_("option"),
1120 				N_("pass the argument through to the merge "
1121 				   "strategy")),
1122 		OPT_BOOL(0, "root", &options.root,
1123 			 N_("rebase all reachable commits up to the root(s)")),
1124 		OPT_BOOL(0, "reschedule-failed-exec",
1125 			 &reschedule_failed_exec,
1126 			 N_("automatically re-schedule any `exec` that fails")),
1127 		OPT_BOOL(0, "reapply-cherry-picks", &options.reapply_cherry_picks,
1128 			 N_("apply all changes, even those already present upstream")),
1129 		OPT_END(),
1130 	};
1131 	int i;
1132 
1133 	if (argc == 2 && !strcmp(argv[1], "-h"))
1134 		usage_with_options(builtin_rebase_usage,
1135 				   builtin_rebase_options);
1136 
1137 	prepare_repo_settings(the_repository);
1138 	the_repository->settings.command_requires_full_index = 0;
1139 
1140 	options.allow_empty_message = 1;
1141 	git_config(rebase_config, &options);
1142 	/* options.gpg_sign_opt will be either "-S" or NULL */
1143 	gpg_sign = options.gpg_sign_opt ? "" : NULL;
1144 	FREE_AND_NULL(options.gpg_sign_opt);
1145 
1146 	strbuf_reset(&buf);
1147 	strbuf_addf(&buf, "%s/applying", apply_dir());
1148 	if(file_exists(buf.buf))
1149 		die(_("It looks like 'git am' is in progress. Cannot rebase."));
1150 
1151 	if (is_directory(apply_dir())) {
1152 		options.type = REBASE_APPLY;
1153 		options.state_dir = apply_dir();
1154 	} else if (is_directory(merge_dir())) {
1155 		strbuf_reset(&buf);
1156 		strbuf_addf(&buf, "%s/rewritten", merge_dir());
1157 		if (is_directory(buf.buf)) {
1158 			die("`rebase -p` is no longer supported");
1159 		} else {
1160 			strbuf_reset(&buf);
1161 			strbuf_addf(&buf, "%s/interactive", merge_dir());
1162 			if(file_exists(buf.buf)) {
1163 				options.type = REBASE_MERGE;
1164 				options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1165 			} else
1166 				options.type = REBASE_MERGE;
1167 		}
1168 		options.state_dir = merge_dir();
1169 	}
1170 
1171 	if (options.type != REBASE_UNSPECIFIED)
1172 		in_progress = 1;
1173 
1174 	total_argc = argc;
1175 	argc = parse_options(argc, argv, prefix,
1176 			     builtin_rebase_options,
1177 			     builtin_rebase_usage, 0);
1178 
1179 	if (preserve_merges_selected)
1180 		die(_("--preserve-merges was replaced by --rebase-merges"));
1181 
1182 	if (action != ACTION_NONE && total_argc != 2) {
1183 		usage_with_options(builtin_rebase_usage,
1184 				   builtin_rebase_options);
1185 	}
1186 
1187 	if (argc > 2)
1188 		usage_with_options(builtin_rebase_usage,
1189 				   builtin_rebase_options);
1190 
1191 	if (keep_base) {
1192 		if (options.onto_name)
1193 			die(_("cannot combine '--keep-base' with '--onto'"));
1194 		if (options.root)
1195 			die(_("cannot combine '--keep-base' with '--root'"));
1196 	}
1197 
1198 	if (options.root && options.fork_point > 0)
1199 		die(_("cannot combine '--root' with '--fork-point'"));
1200 
1201 	if (action != ACTION_NONE && !in_progress)
1202 		die(_("No rebase in progress?"));
1203 	setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0);
1204 
1205 	if (action == ACTION_EDIT_TODO && !is_merge(&options))
1206 		die(_("The --edit-todo action can only be used during "
1207 		      "interactive rebase."));
1208 
1209 	if (trace2_is_enabled()) {
1210 		if (is_merge(&options))
1211 			trace2_cmd_mode("interactive");
1212 		else if (exec.nr)
1213 			trace2_cmd_mode("interactive-exec");
1214 		else
1215 			trace2_cmd_mode(action_names[action]);
1216 	}
1217 
1218 	switch (action) {
1219 	case ACTION_CONTINUE: {
1220 		struct object_id head;
1221 		struct lock_file lock_file = LOCK_INIT;
1222 		int fd;
1223 
1224 		options.action = "continue";
1225 		set_reflog_action(&options);
1226 
1227 		/* Sanity check */
1228 		if (get_oid("HEAD", &head))
1229 			die(_("Cannot read HEAD"));
1230 
1231 		fd = hold_locked_index(&lock_file, 0);
1232 		if (repo_read_index(the_repository) < 0)
1233 			die(_("could not read index"));
1234 		refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1235 			      NULL);
1236 		if (0 <= fd)
1237 			repo_update_index_if_able(the_repository, &lock_file);
1238 		rollback_lock_file(&lock_file);
1239 
1240 		if (has_unstaged_changes(the_repository, 1)) {
1241 			puts(_("You must edit all merge conflicts and then\n"
1242 			       "mark them as resolved using git add"));
1243 			exit(1);
1244 		}
1245 		if (read_basic_state(&options))
1246 			exit(1);
1247 		goto run_rebase;
1248 	}
1249 	case ACTION_SKIP: {
1250 		struct string_list merge_rr = STRING_LIST_INIT_DUP;
1251 
1252 		options.action = "skip";
1253 		set_reflog_action(&options);
1254 
1255 		rerere_clear(the_repository, &merge_rr);
1256 		string_list_clear(&merge_rr, 1);
1257 
1258 		if (reset_head(the_repository, NULL, "reset", NULL, RESET_HEAD_HARD,
1259 			       NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1260 			die(_("could not discard worktree changes"));
1261 		remove_branch_state(the_repository, 0);
1262 		if (read_basic_state(&options))
1263 			exit(1);
1264 		goto run_rebase;
1265 	}
1266 	case ACTION_ABORT: {
1267 		struct string_list merge_rr = STRING_LIST_INIT_DUP;
1268 		options.action = "abort";
1269 		set_reflog_action(&options);
1270 
1271 		rerere_clear(the_repository, &merge_rr);
1272 		string_list_clear(&merge_rr, 1);
1273 
1274 		if (read_basic_state(&options))
1275 			exit(1);
1276 		if (reset_head(the_repository, &options.orig_head, "reset",
1277 			       options.head_name, RESET_HEAD_HARD,
1278 			       NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1279 			die(_("could not move back to %s"),
1280 			    oid_to_hex(&options.orig_head));
1281 		remove_branch_state(the_repository, 0);
1282 		ret = finish_rebase(&options);
1283 		goto cleanup;
1284 	}
1285 	case ACTION_QUIT: {
1286 		save_autostash(state_dir_path("autostash", &options));
1287 		if (options.type == REBASE_MERGE) {
1288 			struct replay_opts replay = REPLAY_OPTS_INIT;
1289 
1290 			replay.action = REPLAY_INTERACTIVE_REBASE;
1291 			ret = sequencer_remove_state(&replay);
1292 		} else {
1293 			strbuf_reset(&buf);
1294 			strbuf_addstr(&buf, options.state_dir);
1295 			ret = remove_dir_recursively(&buf, 0);
1296 			if (ret)
1297 				error(_("could not remove '%s'"),
1298 				       options.state_dir);
1299 		}
1300 		goto cleanup;
1301 	}
1302 	case ACTION_EDIT_TODO:
1303 		options.action = "edit-todo";
1304 		options.dont_finish_rebase = 1;
1305 		goto run_rebase;
1306 	case ACTION_SHOW_CURRENT_PATCH:
1307 		options.action = "show-current-patch";
1308 		options.dont_finish_rebase = 1;
1309 		goto run_rebase;
1310 	case ACTION_NONE:
1311 		break;
1312 	default:
1313 		BUG("action: %d", action);
1314 	}
1315 
1316 	/* Make sure no rebase is in progress */
1317 	if (in_progress) {
1318 		const char *last_slash = strrchr(options.state_dir, '/');
1319 		const char *state_dir_base =
1320 			last_slash ? last_slash + 1 : options.state_dir;
1321 		const char *cmd_live_rebase =
1322 			"git rebase (--continue | --abort | --skip)";
1323 		strbuf_reset(&buf);
1324 		strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1325 		die(_("It seems that there is already a %s directory, and\n"
1326 		      "I wonder if you are in the middle of another rebase.  "
1327 		      "If that is the\n"
1328 		      "case, please try\n\t%s\n"
1329 		      "If that is not the case, please\n\t%s\n"
1330 		      "and run me again.  I am stopping in case you still "
1331 		      "have something\n"
1332 		      "valuable there.\n"),
1333 		    state_dir_base, cmd_live_rebase, buf.buf);
1334 	}
1335 
1336 	if ((options.flags & REBASE_INTERACTIVE_EXPLICIT) ||
1337 	    (action != ACTION_NONE) ||
1338 	    (exec.nr > 0) ||
1339 	    options.autosquash) {
1340 		allow_preemptive_ff = 0;
1341 	}
1342 	if (options.committer_date_is_author_date || options.ignore_date)
1343 		options.flags |= REBASE_FORCE;
1344 
1345 	for (i = 0; i < options.git_am_opts.nr; i++) {
1346 		const char *option = options.git_am_opts.v[i], *p;
1347 		if (!strcmp(option, "--whitespace=fix") ||
1348 		    !strcmp(option, "--whitespace=strip"))
1349 			allow_preemptive_ff = 0;
1350 		else if (skip_prefix(option, "-C", &p)) {
1351 			while (*p)
1352 				if (!isdigit(*(p++)))
1353 					die(_("switch `C' expects a "
1354 					      "numerical value"));
1355 		} else if (skip_prefix(option, "--whitespace=", &p)) {
1356 			if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1357 			    strcmp(p, "error") && strcmp(p, "error-all"))
1358 				die("Invalid whitespace option: '%s'", p);
1359 		}
1360 	}
1361 
1362 	for (i = 0; i < exec.nr; i++)
1363 		if (check_exec_cmd(exec.items[i].string))
1364 			exit(1);
1365 
1366 	if (!(options.flags & REBASE_NO_QUIET))
1367 		strvec_push(&options.git_am_opts, "-q");
1368 
1369 	if (options.empty != EMPTY_UNSPECIFIED)
1370 		imply_merge(&options, "--empty");
1371 
1372 	if (options.reapply_cherry_picks)
1373 		imply_merge(&options, "--reapply-cherry-picks");
1374 
1375 	if (gpg_sign)
1376 		options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1377 
1378 	if (exec.nr) {
1379 		int i;
1380 
1381 		imply_merge(&options, "--exec");
1382 
1383 		strbuf_reset(&buf);
1384 		for (i = 0; i < exec.nr; i++)
1385 			strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1386 		options.cmd = xstrdup(buf.buf);
1387 	}
1388 
1389 	if (rebase_merges) {
1390 		if (!*rebase_merges)
1391 			; /* default mode; do nothing */
1392 		else if (!strcmp("rebase-cousins", rebase_merges))
1393 			options.rebase_cousins = 1;
1394 		else if (strcmp("no-rebase-cousins", rebase_merges))
1395 			die(_("Unknown mode: %s"), rebase_merges);
1396 		options.rebase_merges = 1;
1397 		imply_merge(&options, "--rebase-merges");
1398 	}
1399 
1400 	if (options.type == REBASE_APPLY) {
1401 		if (ignore_whitespace)
1402 			strvec_push(&options.git_am_opts,
1403 				    "--ignore-whitespace");
1404 		if (options.committer_date_is_author_date)
1405 			strvec_push(&options.git_am_opts,
1406 				    "--committer-date-is-author-date");
1407 		if (options.ignore_date)
1408 			strvec_push(&options.git_am_opts, "--ignore-date");
1409 	} else {
1410 		/* REBASE_MERGE */
1411 		if (ignore_whitespace) {
1412 			string_list_append(&strategy_options,
1413 					   "ignore-space-change");
1414 		}
1415 	}
1416 
1417 	if (strategy_options.nr) {
1418 		int i;
1419 
1420 		if (!options.strategy)
1421 			options.strategy = "ort";
1422 
1423 		strbuf_reset(&buf);
1424 		for (i = 0; i < strategy_options.nr; i++)
1425 			strbuf_addf(&buf, " --%s",
1426 				    strategy_options.items[i].string);
1427 		options.strategy_opts = xstrdup(buf.buf);
1428 	}
1429 
1430 	if (options.strategy) {
1431 		options.strategy = xstrdup(options.strategy);
1432 		switch (options.type) {
1433 		case REBASE_APPLY:
1434 			die(_("--strategy requires --merge or --interactive"));
1435 		case REBASE_MERGE:
1436 			/* compatible */
1437 			break;
1438 		case REBASE_UNSPECIFIED:
1439 			options.type = REBASE_MERGE;
1440 			break;
1441 		default:
1442 			BUG("unhandled rebase type (%d)", options.type);
1443 		}
1444 	}
1445 
1446 	if (options.type == REBASE_MERGE)
1447 		imply_merge(&options, "--merge");
1448 
1449 	if (options.root && !options.onto_name)
1450 		imply_merge(&options, "--root without --onto");
1451 
1452 	if (isatty(2) && options.flags & REBASE_NO_QUIET)
1453 		strbuf_addstr(&options.git_format_patch_opt, " --progress");
1454 
1455 	if (options.git_am_opts.nr || options.type == REBASE_APPLY) {
1456 		/* all am options except -q are compatible only with --apply */
1457 		for (i = options.git_am_opts.nr - 1; i >= 0; i--)
1458 			if (strcmp(options.git_am_opts.v[i], "-q"))
1459 				break;
1460 
1461 		if (i >= 0) {
1462 			if (is_merge(&options))
1463 				die(_("cannot combine apply options with "
1464 				      "merge options"));
1465 			else
1466 				options.type = REBASE_APPLY;
1467 		}
1468 	}
1469 
1470 	if (options.type == REBASE_UNSPECIFIED) {
1471 		if (!strcmp(options.default_backend, "merge"))
1472 			imply_merge(&options, "--merge");
1473 		else if (!strcmp(options.default_backend, "apply"))
1474 			options.type = REBASE_APPLY;
1475 		else
1476 			die(_("Unknown rebase backend: %s"),
1477 			    options.default_backend);
1478 	}
1479 
1480 	if (options.type == REBASE_MERGE &&
1481 	    !options.strategy &&
1482 	    getenv("GIT_TEST_MERGE_ALGORITHM"))
1483 		options.strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
1484 
1485 	switch (options.type) {
1486 	case REBASE_MERGE:
1487 		options.state_dir = merge_dir();
1488 		break;
1489 	case REBASE_APPLY:
1490 		options.state_dir = apply_dir();
1491 		break;
1492 	default:
1493 		BUG("options.type was just set above; should be unreachable.");
1494 	}
1495 
1496 	if (options.empty == EMPTY_UNSPECIFIED) {
1497 		if (options.flags & REBASE_INTERACTIVE_EXPLICIT)
1498 			options.empty = EMPTY_ASK;
1499 		else if (exec.nr > 0)
1500 			options.empty = EMPTY_KEEP;
1501 		else
1502 			options.empty = EMPTY_DROP;
1503 	}
1504 	if (reschedule_failed_exec > 0 && !is_merge(&options))
1505 		die(_("--reschedule-failed-exec requires "
1506 		      "--exec or --interactive"));
1507 	if (reschedule_failed_exec >= 0)
1508 		options.reschedule_failed_exec = reschedule_failed_exec;
1509 
1510 	if (options.signoff) {
1511 		strvec_push(&options.git_am_opts, "--signoff");
1512 		options.flags |= REBASE_FORCE;
1513 	}
1514 
1515 	if (!options.root) {
1516 		if (argc < 1) {
1517 			struct branch *branch;
1518 
1519 			branch = branch_get(NULL);
1520 			options.upstream_name = branch_get_upstream(branch,
1521 								    NULL);
1522 			if (!options.upstream_name)
1523 				error_on_missing_default_upstream();
1524 			if (options.fork_point < 0)
1525 				options.fork_point = 1;
1526 		} else {
1527 			options.upstream_name = argv[0];
1528 			argc--;
1529 			argv++;
1530 			if (!strcmp(options.upstream_name, "-"))
1531 				options.upstream_name = "@{-1}";
1532 		}
1533 		options.upstream =
1534 			lookup_commit_reference_by_name(options.upstream_name);
1535 		if (!options.upstream)
1536 			die(_("invalid upstream '%s'"), options.upstream_name);
1537 		options.upstream_arg = options.upstream_name;
1538 	} else {
1539 		if (!options.onto_name) {
1540 			if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1541 					&squash_onto, NULL, NULL) < 0)
1542 				die(_("Could not create new root commit"));
1543 			options.squash_onto = &squash_onto;
1544 			options.onto_name = squash_onto_name =
1545 				xstrdup(oid_to_hex(&squash_onto));
1546 		} else
1547 			options.root_with_onto = 1;
1548 
1549 		options.upstream_name = NULL;
1550 		options.upstream = NULL;
1551 		if (argc > 1)
1552 			usage_with_options(builtin_rebase_usage,
1553 					   builtin_rebase_options);
1554 		options.upstream_arg = "--root";
1555 	}
1556 
1557 	/* Make sure the branch to rebase onto is valid. */
1558 	if (keep_base) {
1559 		strbuf_reset(&buf);
1560 		strbuf_addstr(&buf, options.upstream_name);
1561 		strbuf_addstr(&buf, "...");
1562 		options.onto_name = xstrdup(buf.buf);
1563 	} else if (!options.onto_name)
1564 		options.onto_name = options.upstream_name;
1565 	if (strstr(options.onto_name, "...")) {
1566 		if (get_oid_mb(options.onto_name, &merge_base) < 0) {
1567 			if (keep_base)
1568 				die(_("'%s': need exactly one merge base with branch"),
1569 				    options.upstream_name);
1570 			else
1571 				die(_("'%s': need exactly one merge base"),
1572 				    options.onto_name);
1573 		}
1574 		options.onto = lookup_commit_or_die(&merge_base,
1575 						    options.onto_name);
1576 	} else {
1577 		options.onto =
1578 			lookup_commit_reference_by_name(options.onto_name);
1579 		if (!options.onto)
1580 			die(_("Does not point to a valid commit '%s'"),
1581 				options.onto_name);
1582 	}
1583 
1584 	/*
1585 	 * If the branch to rebase is given, that is the branch we will rebase
1586 	 * branch_name -- branch/commit being rebased, or
1587 	 * 		  HEAD (already detached)
1588 	 * orig_head -- commit object name of tip of the branch before rebasing
1589 	 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1590 	 */
1591 	if (argc == 1) {
1592 		/* Is it "rebase other branchname" or "rebase other commit"? */
1593 		branch_name = argv[0];
1594 		options.switch_to = argv[0];
1595 
1596 		/* Is it a local branch? */
1597 		strbuf_reset(&buf);
1598 		strbuf_addf(&buf, "refs/heads/%s", branch_name);
1599 		if (!read_ref(buf.buf, &options.orig_head)) {
1600 			die_if_checked_out(buf.buf, 1);
1601 			options.head_name = xstrdup(buf.buf);
1602 		/* If not is it a valid ref (branch or commit)? */
1603 		} else {
1604 			struct commit *commit =
1605 				lookup_commit_reference_by_name(branch_name);
1606 			if (!commit)
1607 				die(_("no such branch/commit '%s'"),
1608 				    branch_name);
1609 			oidcpy(&options.orig_head, &commit->object.oid);
1610 			options.head_name = NULL;
1611 		}
1612 	} else if (argc == 0) {
1613 		/* Do not need to switch branches, we are already on it. */
1614 		options.head_name =
1615 			xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1616 					 &flags));
1617 		if (!options.head_name)
1618 			die(_("No such ref: %s"), "HEAD");
1619 		if (flags & REF_ISSYMREF) {
1620 			if (!skip_prefix(options.head_name,
1621 					 "refs/heads/", &branch_name))
1622 				branch_name = options.head_name;
1623 
1624 		} else {
1625 			FREE_AND_NULL(options.head_name);
1626 			branch_name = "HEAD";
1627 		}
1628 		if (get_oid("HEAD", &options.orig_head))
1629 			die(_("Could not resolve HEAD to a revision"));
1630 	} else
1631 		BUG("unexpected number of arguments left to parse");
1632 
1633 	if (options.fork_point > 0) {
1634 		struct commit *head =
1635 			lookup_commit_reference(the_repository,
1636 						&options.orig_head);
1637 		options.restrict_revision =
1638 			get_fork_point(options.upstream_name, head);
1639 	}
1640 
1641 	if (repo_read_index(the_repository) < 0)
1642 		die(_("could not read index"));
1643 
1644 	if (options.autostash) {
1645 		create_autostash(the_repository, state_dir_path("autostash", &options),
1646 				 DEFAULT_REFLOG_ACTION);
1647 	}
1648 
1649 	if (require_clean_work_tree(the_repository, "rebase",
1650 				    _("Please commit or stash them."), 1, 1)) {
1651 		ret = -1;
1652 		goto cleanup;
1653 	}
1654 
1655 	/*
1656 	 * Now we are rebasing commits upstream..orig_head (or with --root,
1657 	 * everything leading up to orig_head) on top of onto.
1658 	 */
1659 
1660 	/*
1661 	 * Check if we are already based on onto with linear history,
1662 	 * in which case we could fast-forward without replacing the commits
1663 	 * with new commits recreated by replaying their changes.
1664 	 *
1665 	 * Note that can_fast_forward() initializes merge_base, so we have to
1666 	 * call it before checking allow_preemptive_ff.
1667 	 */
1668 	if (can_fast_forward(options.onto, options.upstream, options.restrict_revision,
1669 		    &options.orig_head, &merge_base) &&
1670 	    allow_preemptive_ff) {
1671 		int flag;
1672 
1673 		if (!(options.flags & REBASE_FORCE)) {
1674 			/* Lazily switch to the target branch if needed... */
1675 			if (options.switch_to) {
1676 				strbuf_reset(&buf);
1677 				strbuf_addf(&buf, "%s: checkout %s",
1678 					    getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
1679 					    options.switch_to);
1680 				if (reset_head(the_repository,
1681 					       &options.orig_head, "checkout",
1682 					       options.head_name,
1683 					       RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1684 					       NULL, buf.buf,
1685 					       DEFAULT_REFLOG_ACTION) < 0) {
1686 					ret = error(_("could not switch to "
1687 							"%s"),
1688 						      options.switch_to);
1689 					goto cleanup;
1690 				}
1691 			}
1692 
1693 			if (!(options.flags & REBASE_NO_QUIET))
1694 				; /* be quiet */
1695 			else if (!strcmp(branch_name, "HEAD") &&
1696 				 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1697 				puts(_("HEAD is up to date."));
1698 			else
1699 				printf(_("Current branch %s is up to date.\n"),
1700 				       branch_name);
1701 			ret = finish_rebase(&options);
1702 			goto cleanup;
1703 		} else if (!(options.flags & REBASE_NO_QUIET))
1704 			; /* be quiet */
1705 		else if (!strcmp(branch_name, "HEAD") &&
1706 			 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1707 			puts(_("HEAD is up to date, rebase forced."));
1708 		else
1709 			printf(_("Current branch %s is up to date, rebase "
1710 				 "forced.\n"), branch_name);
1711 	}
1712 
1713 	/* If a hook exists, give it a chance to interrupt*/
1714 	if (!ok_to_skip_pre_rebase &&
1715 	    run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1716 			argc ? argv[0] : NULL, NULL))
1717 		die(_("The pre-rebase hook refused to rebase."));
1718 
1719 	if (options.flags & REBASE_DIFFSTAT) {
1720 		struct diff_options opts;
1721 
1722 		if (options.flags & REBASE_VERBOSE) {
1723 			if (is_null_oid(&merge_base))
1724 				printf(_("Changes to %s:\n"),
1725 				       oid_to_hex(&options.onto->object.oid));
1726 			else
1727 				printf(_("Changes from %s to %s:\n"),
1728 				       oid_to_hex(&merge_base),
1729 				       oid_to_hex(&options.onto->object.oid));
1730 		}
1731 
1732 		/* We want color (if set), but no pager */
1733 		diff_setup(&opts);
1734 		opts.stat_width = -1; /* use full terminal width */
1735 		opts.stat_graph_width = -1; /* respect statGraphWidth config */
1736 		opts.output_format |=
1737 			DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1738 		opts.detect_rename = DIFF_DETECT_RENAME;
1739 		diff_setup_done(&opts);
1740 		diff_tree_oid(is_null_oid(&merge_base) ?
1741 			      the_hash_algo->empty_tree : &merge_base,
1742 			      &options.onto->object.oid, "", &opts);
1743 		diffcore_std(&opts);
1744 		diff_flush(&opts);
1745 	}
1746 
1747 	if (is_merge(&options))
1748 		goto run_rebase;
1749 
1750 	/* Detach HEAD and reset the tree */
1751 	if (options.flags & REBASE_NO_QUIET)
1752 		printf(_("First, rewinding head to replay your work on top of "
1753 			 "it...\n"));
1754 
1755 	strbuf_addf(&msg, "%s: checkout %s",
1756 		    getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
1757 	if (reset_head(the_repository, &options.onto->object.oid, "checkout", NULL,
1758 		       RESET_HEAD_DETACH | RESET_ORIG_HEAD |
1759 		       RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1760 		       NULL, msg.buf, DEFAULT_REFLOG_ACTION))
1761 		die(_("Could not detach HEAD"));
1762 	strbuf_release(&msg);
1763 
1764 	/*
1765 	 * If the onto is a proper descendant of the tip of the branch, then
1766 	 * we just fast-forwarded.
1767 	 */
1768 	strbuf_reset(&msg);
1769 	if (oideq(&merge_base, &options.orig_head)) {
1770 		printf(_("Fast-forwarded %s to %s.\n"),
1771 			branch_name, options.onto_name);
1772 		strbuf_addf(&msg, "rebase finished: %s onto %s",
1773 			options.head_name ? options.head_name : "detached HEAD",
1774 			oid_to_hex(&options.onto->object.oid));
1775 		reset_head(the_repository, NULL, "Fast-forwarded", options.head_name,
1776 			   RESET_HEAD_REFS_ONLY, "HEAD", msg.buf,
1777 			   DEFAULT_REFLOG_ACTION);
1778 		strbuf_release(&msg);
1779 		ret = finish_rebase(&options);
1780 		goto cleanup;
1781 	}
1782 
1783 	strbuf_addf(&revisions, "%s..%s",
1784 		    options.root ? oid_to_hex(&options.onto->object.oid) :
1785 		    (options.restrict_revision ?
1786 		     oid_to_hex(&options.restrict_revision->object.oid) :
1787 		     oid_to_hex(&options.upstream->object.oid)),
1788 		    oid_to_hex(&options.orig_head));
1789 
1790 	options.revisions = revisions.buf;
1791 
1792 run_rebase:
1793 	ret = run_specific_rebase(&options, action);
1794 
1795 cleanup:
1796 	strbuf_release(&buf);
1797 	strbuf_release(&revisions);
1798 	free(options.head_name);
1799 	free(options.gpg_sign_opt);
1800 	free(options.cmd);
1801 	free(options.strategy);
1802 	strbuf_release(&options.git_format_patch_opt);
1803 	free(squash_onto_name);
1804 	return !!ret;
1805 }
1806