2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
9 #include "environment.h"
12 #include "run-command.h"
17 #include "unpack-trees.h"
19 #include "object-file.h"
20 #include "object-name.h"
21 #include "parse-options.h"
25 #include "wt-status.h"
27 #include "commit-reach.h"
30 #include "sequencer.h"
31 #include "rebase-interactive.h"
36 static char const * const builtin_rebase_usage
[] = {
37 N_("git rebase [-i] [options] [--exec <cmd>] "
38 "[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
39 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
41 "git rebase --continue | --abort | --skip | --edit-todo",
45 static GIT_PATH_FUNC(path_squash_onto
, "rebase-merge/squash-onto")
46 static GIT_PATH_FUNC(path_interactive
, "rebase-merge/interactive")
47 static GIT_PATH_FUNC(apply_dir
, "rebase-apply")
48 static GIT_PATH_FUNC(merge_dir
, "rebase-merge")
51 REBASE_UNSPECIFIED
= -1,
57 EMPTY_UNSPECIFIED
= -1,
70 ACTION_SHOW_CURRENT_PATCH
73 static const char *action_names
[] = {
83 struct rebase_options
{
84 enum rebase_type type
;
85 enum empty_type empty
;
86 const char *default_backend
;
87 const char *state_dir
;
88 struct commit
*upstream
;
89 const char *upstream_name
;
90 const char *upstream_arg
;
92 struct commit
*orig_head
;
94 const char *onto_name
;
95 const char *revisions
;
96 const char *switch_to
;
97 int root
, root_with_onto
;
98 struct object_id
*squash_onto
;
99 struct commit
*restrict_revision
;
100 int dont_finish_rebase
;
102 REBASE_NO_QUIET
= 1<<0,
103 REBASE_VERBOSE
= 1<<1,
104 REBASE_DIFFSTAT
= 1<<2,
106 REBASE_INTERACTIVE_EXPLICIT
= 1<<4,
108 struct strvec git_am_opts
;
112 int allow_rerere_autoupdate
;
117 int committer_date_is_author_date
;
119 struct string_list exec
;
120 int allow_empty_message
;
121 int rebase_merges
, rebase_cousins
;
123 struct string_list strategy_opts
;
124 struct strbuf git_format_patch_opt
;
125 int reschedule_failed_exec
;
126 int reapply_cherry_picks
;
129 int config_autosquash
;
130 int config_rebase_merges
;
131 int config_update_refs
;
134 #define REBASE_OPTIONS_INIT { \
135 .type = REBASE_UNSPECIFIED, \
136 .empty = EMPTY_UNSPECIFIED, \
138 .default_backend = "merge", \
139 .flags = REBASE_NO_QUIET, \
140 .git_am_opts = STRVEC_INIT, \
141 .exec = STRING_LIST_INIT_NODUP, \
142 .git_format_patch_opt = STRBUF_INIT, \
144 .reapply_cherry_picks = -1, \
145 .allow_empty_message = 1, \
147 .rebase_merges = -1, \
148 .config_rebase_merges = -1, \
150 .config_update_refs = -1, \
151 .strategy_opts = STRING_LIST_INIT_NODUP,\
154 static struct replay_opts
get_replay_opts(const struct rebase_options
*opts
)
156 struct replay_opts replay
= REPLAY_OPTS_INIT
;
158 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
159 replay
.strategy
= NULL
;
160 sequencer_init_config(&replay
);
162 replay
.signoff
= opts
->signoff
;
163 replay
.allow_ff
= !(opts
->flags
& REBASE_FORCE
);
164 if (opts
->allow_rerere_autoupdate
)
165 replay
.allow_rerere_auto
= opts
->allow_rerere_autoupdate
;
166 replay
.allow_empty
= 1;
167 replay
.allow_empty_message
= opts
->allow_empty_message
;
168 replay
.drop_redundant_commits
= (opts
->empty
== EMPTY_DROP
);
169 replay
.keep_redundant_commits
= (opts
->empty
== EMPTY_KEEP
);
170 replay
.quiet
= !(opts
->flags
& REBASE_NO_QUIET
);
171 replay
.verbose
= opts
->flags
& REBASE_VERBOSE
;
172 replay
.reschedule_failed_exec
= opts
->reschedule_failed_exec
;
173 replay
.committer_date_is_author_date
=
174 opts
->committer_date_is_author_date
;
175 replay
.ignore_date
= opts
->ignore_date
;
176 replay
.gpg_sign
= xstrdup_or_null(opts
->gpg_sign_opt
);
177 replay
.reflog_action
= xstrdup(opts
->reflog_action
);
179 replay
.strategy
= xstrdup_or_null(opts
->strategy
);
180 else if (!replay
.strategy
&& replay
.default_strategy
) {
181 replay
.strategy
= replay
.default_strategy
;
182 replay
.default_strategy
= NULL
;
185 for (size_t i
= 0; i
< opts
->strategy_opts
.nr
; i
++)
186 strvec_push(&replay
.xopts
, opts
->strategy_opts
.items
[i
].string
);
188 if (opts
->squash_onto
) {
189 oidcpy(&replay
.squash_onto
, opts
->squash_onto
);
190 replay
.have_squash_onto
= 1;
196 static int edit_todo_file(unsigned flags
)
198 const char *todo_file
= rebase_path_todo();
199 struct todo_list todo_list
= TODO_LIST_INIT
,
200 new_todo
= TODO_LIST_INIT
;
203 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
204 return error_errno(_("could not read '%s'."), todo_file
);
206 strbuf_stripspace(&todo_list
.buf
, comment_line_str
);
207 res
= edit_todo_list(the_repository
, &todo_list
, &new_todo
, NULL
, NULL
, flags
);
208 if (!res
&& todo_list_write_to_file(the_repository
, &new_todo
, todo_file
,
209 NULL
, NULL
, -1, flags
& ~(TODO_LIST_SHORTEN_IDS
)))
210 res
= error_errno(_("could not write '%s'"), todo_file
);
212 todo_list_release(&todo_list
);
213 todo_list_release(&new_todo
);
218 static int get_revision_ranges(struct commit
*upstream
, struct commit
*onto
,
219 struct object_id
*orig_head
, char **revisions
,
220 char **shortrevisions
)
222 struct commit
*base_rev
= upstream
? upstream
: onto
;
223 const char *shorthead
;
225 *revisions
= xstrfmt("%s...%s", oid_to_hex(&base_rev
->object
.oid
),
226 oid_to_hex(orig_head
));
228 shorthead
= repo_find_unique_abbrev(the_repository
, orig_head
,
232 const char *shortrev
;
234 shortrev
= repo_find_unique_abbrev(the_repository
,
235 &base_rev
->object
.oid
,
238 *shortrevisions
= xstrfmt("%s..%s", shortrev
, shorthead
);
240 *shortrevisions
= xstrdup(shorthead
);
245 static int init_basic_state(struct replay_opts
*opts
, const char *head_name
,
247 const struct object_id
*orig_head
)
251 if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir()))
252 return error_errno(_("could not create temporary %s"), merge_dir());
254 delete_reflog("REBASE_HEAD");
256 interactive
= fopen(path_interactive(), "w");
258 return error_errno(_("could not mark as interactive"));
261 return write_basic_state(opts
, head_name
, onto
, orig_head
);
264 static int do_interactive_rebase(struct rebase_options
*opts
, unsigned flags
)
267 char *revisions
= NULL
, *shortrevisions
= NULL
;
268 struct strvec make_script_args
= STRVEC_INIT
;
269 struct todo_list todo_list
= TODO_LIST_INIT
;
270 struct replay_opts replay
= get_replay_opts(opts
);
272 if (get_revision_ranges(opts
->upstream
, opts
->onto
, &opts
->orig_head
->object
.oid
,
273 &revisions
, &shortrevisions
))
276 if (init_basic_state(&replay
,
277 opts
->head_name
? opts
->head_name
: "detached HEAD",
278 opts
->onto
, &opts
->orig_head
->object
.oid
))
281 if (!opts
->upstream
&& opts
->squash_onto
)
282 write_file(path_squash_onto(), "%s\n",
283 oid_to_hex(opts
->squash_onto
));
285 strvec_pushl(&make_script_args
, "", revisions
, NULL
);
286 if (opts
->restrict_revision
)
287 strvec_pushf(&make_script_args
, "^%s",
288 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
290 ret
= sequencer_make_script(the_repository
, &todo_list
.buf
,
291 make_script_args
.nr
, make_script_args
.v
,
295 error(_("could not generate todo list"));
297 discard_index(the_repository
->index
);
298 if (todo_list_parse_insn_buffer(the_repository
, todo_list
.buf
.buf
,
300 BUG("unusable todo list");
302 ret
= complete_action(the_repository
, &replay
, flags
,
303 shortrevisions
, opts
->onto_name
, opts
->onto
,
304 &opts
->orig_head
->object
.oid
, &opts
->exec
,
305 opts
->autosquash
, opts
->update_refs
, &todo_list
);
309 replay_opts_release(&replay
);
311 free(shortrevisions
);
312 todo_list_release(&todo_list
);
313 strvec_clear(&make_script_args
);
318 static int run_sequencer_rebase(struct rebase_options
*opts
)
321 int abbreviate_commands
= 0, ret
= 0;
323 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands
);
325 flags
|= opts
->keep_empty
? TODO_LIST_KEEP_EMPTY
: 0;
326 flags
|= abbreviate_commands
? TODO_LIST_ABBREVIATE_CMDS
: 0;
327 flags
|= opts
->rebase_merges
? TODO_LIST_REBASE_MERGES
: 0;
328 flags
|= opts
->rebase_cousins
> 0 ? TODO_LIST_REBASE_COUSINS
: 0;
329 flags
|= opts
->root_with_onto
? TODO_LIST_ROOT_WITH_ONTO
: 0;
330 flags
|= opts
->reapply_cherry_picks
? TODO_LIST_REAPPLY_CHERRY_PICKS
: 0;
331 flags
|= opts
->flags
& REBASE_NO_QUIET
? TODO_LIST_WARN_SKIPPED_CHERRY_PICKS
: 0;
333 switch (opts
->action
) {
335 if (!opts
->onto
&& !opts
->upstream
)
336 die(_("a base commit must be provided with --upstream or --onto"));
338 ret
= do_interactive_rebase(opts
, flags
);
342 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
344 rerere_clear(the_repository
, &merge_rr
);
347 case ACTION_CONTINUE
: {
348 struct replay_opts replay_opts
= get_replay_opts(opts
);
350 ret
= sequencer_continue(the_repository
, &replay_opts
);
351 replay_opts_release(&replay_opts
);
354 case ACTION_EDIT_TODO
:
355 ret
= edit_todo_file(flags
);
357 case ACTION_SHOW_CURRENT_PATCH
: {
358 struct child_process cmd
= CHILD_PROCESS_INIT
;
361 strvec_pushl(&cmd
.args
, "show", "REBASE_HEAD", "--", NULL
);
362 ret
= run_command(&cmd
);
367 BUG("invalid command '%d'", opts
->action
);
373 static int is_merge(struct rebase_options
*opts
)
375 return opts
->type
== REBASE_MERGE
;
378 static void imply_merge(struct rebase_options
*opts
, const char *option
)
380 switch (opts
->type
) {
382 die(_("%s requires the merge backend"), option
);
387 opts
->type
= REBASE_MERGE
; /* implied */
392 /* Returns the filename prefixed by the state_dir */
393 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
395 static struct strbuf path
= STRBUF_INIT
;
396 static size_t prefix_len
;
399 strbuf_addf(&path
, "%s/", opts
->state_dir
);
400 prefix_len
= path
.len
;
403 strbuf_setlen(&path
, prefix_len
);
404 strbuf_addstr(&path
, filename
);
408 /* Initialize the rebase options from the state directory. */
409 static int read_basic_state(struct rebase_options
*opts
)
411 struct strbuf head_name
= STRBUF_INIT
;
412 struct strbuf buf
= STRBUF_INIT
;
413 struct object_id oid
;
415 if (!read_oneliner(&head_name
, state_dir_path("head-name", opts
),
416 READ_ONELINER_WARN_MISSING
) ||
417 !read_oneliner(&buf
, state_dir_path("onto", opts
),
418 READ_ONELINER_WARN_MISSING
))
420 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
421 xstrdup(head_name
.buf
) : NULL
;
422 strbuf_release(&head_name
);
423 if (get_oid_hex(buf
.buf
, &oid
) ||
424 !(opts
->onto
= lookup_commit_object(the_repository
, &oid
)))
425 return error(_("invalid onto: '%s'"), buf
.buf
);
428 * We always write to orig-head, but interactive rebase used to write to
429 * head. Fall back to reading from head to cover for the case that the
430 * user upgraded git with an ongoing interactive rebase.
433 if (file_exists(state_dir_path("orig-head", opts
))) {
434 if (!read_oneliner(&buf
, state_dir_path("orig-head", opts
),
435 READ_ONELINER_WARN_MISSING
))
437 } else if (!read_oneliner(&buf
, state_dir_path("head", opts
),
438 READ_ONELINER_WARN_MISSING
))
440 if (get_oid_hex(buf
.buf
, &oid
) ||
441 !(opts
->orig_head
= lookup_commit_object(the_repository
, &oid
)))
442 return error(_("invalid orig-head: '%s'"), buf
.buf
);
444 if (file_exists(state_dir_path("quiet", opts
)))
445 opts
->flags
&= ~REBASE_NO_QUIET
;
447 opts
->flags
|= REBASE_NO_QUIET
;
449 if (file_exists(state_dir_path("verbose", opts
)))
450 opts
->flags
|= REBASE_VERBOSE
;
452 if (file_exists(state_dir_path("signoff", opts
))) {
454 opts
->flags
|= REBASE_FORCE
;
457 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts
))) {
459 if (!read_oneliner(&buf
, state_dir_path("allow_rerere_autoupdate", opts
),
460 READ_ONELINER_WARN_MISSING
))
462 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
463 opts
->allow_rerere_autoupdate
= RERERE_AUTOUPDATE
;
464 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
465 opts
->allow_rerere_autoupdate
= RERERE_NOAUTOUPDATE
;
467 warning(_("ignoring invalid allow_rerere_autoupdate: "
471 if (file_exists(state_dir_path("gpg_sign_opt", opts
))) {
473 if (!read_oneliner(&buf
, state_dir_path("gpg_sign_opt", opts
),
474 READ_ONELINER_WARN_MISSING
))
476 free(opts
->gpg_sign_opt
);
477 opts
->gpg_sign_opt
= xstrdup(buf
.buf
);
480 strbuf_release(&buf
);
485 static int rebase_write_basic_state(struct rebase_options
*opts
)
487 write_file(state_dir_path("head-name", opts
), "%s",
488 opts
->head_name
? opts
->head_name
: "detached HEAD");
489 write_file(state_dir_path("onto", opts
), "%s",
490 opts
->onto
? oid_to_hex(&opts
->onto
->object
.oid
) : "");
491 write_file(state_dir_path("orig-head", opts
), "%s",
492 oid_to_hex(&opts
->orig_head
->object
.oid
));
493 if (!(opts
->flags
& REBASE_NO_QUIET
))
494 write_file(state_dir_path("quiet", opts
), "%s", "");
495 if (opts
->flags
& REBASE_VERBOSE
)
496 write_file(state_dir_path("verbose", opts
), "%s", "");
497 if (opts
->allow_rerere_autoupdate
> 0)
498 write_file(state_dir_path("allow_rerere_autoupdate", opts
),
499 "-%s-rerere-autoupdate",
500 opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
?
502 if (opts
->gpg_sign_opt
)
503 write_file(state_dir_path("gpg_sign_opt", opts
), "%s",
506 write_file(state_dir_path("signoff", opts
), "--signoff");
511 static int finish_rebase(struct rebase_options
*opts
)
513 struct strbuf dir
= STRBUF_INIT
;
516 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
517 delete_ref(NULL
, "AUTO_MERGE", NULL
, REF_NO_DEREF
);
518 apply_autostash(state_dir_path("autostash", opts
));
520 * We ignore errors in 'git maintenance run --auto', since the
521 * user should see them.
523 run_auto_maintenance(!(opts
->flags
& (REBASE_NO_QUIET
|REBASE_VERBOSE
)));
524 if (opts
->type
== REBASE_MERGE
) {
525 struct replay_opts replay
= REPLAY_OPTS_INIT
;
527 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
528 ret
= sequencer_remove_state(&replay
);
529 replay_opts_release(&replay
);
531 strbuf_addstr(&dir
, opts
->state_dir
);
532 if (remove_dir_recursively(&dir
, 0))
533 ret
= error(_("could not remove '%s'"),
535 strbuf_release(&dir
);
541 static int move_to_original_branch(struct rebase_options
*opts
)
543 struct strbuf branch_reflog
= STRBUF_INIT
, head_reflog
= STRBUF_INIT
;
544 struct reset_head_opts ropts
= { 0 };
547 if (!opts
->head_name
)
548 return 0; /* nothing to move back to */
551 BUG("move_to_original_branch without onto");
553 strbuf_addf(&branch_reflog
, "%s (finish): %s onto %s",
555 opts
->head_name
, oid_to_hex(&opts
->onto
->object
.oid
));
556 strbuf_addf(&head_reflog
, "%s (finish): returning to %s",
557 opts
->reflog_action
, opts
->head_name
);
558 ropts
.branch
= opts
->head_name
;
559 ropts
.flags
= RESET_HEAD_REFS_ONLY
;
560 ropts
.branch_msg
= branch_reflog
.buf
;
561 ropts
.head_msg
= head_reflog
.buf
;
562 ret
= reset_head(the_repository
, &ropts
);
564 strbuf_release(&branch_reflog
);
565 strbuf_release(&head_reflog
);
569 static int run_am(struct rebase_options
*opts
)
571 struct child_process am
= CHILD_PROCESS_INIT
;
572 struct child_process format_patch
= CHILD_PROCESS_INIT
;
574 char *rebased_patches
;
577 strvec_push(&am
.args
, "am");
578 strvec_pushf(&am
.env
, GIT_REFLOG_ACTION_ENVIRONMENT
"=%s (pick)",
579 opts
->reflog_action
);
580 if (opts
->action
== ACTION_CONTINUE
) {
581 strvec_push(&am
.args
, "--resolved");
582 strvec_pushf(&am
.args
, "--resolvemsg=%s", rebase_resolvemsg
);
583 if (opts
->gpg_sign_opt
)
584 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
585 status
= run_command(&am
);
589 return move_to_original_branch(opts
);
591 if (opts
->action
== ACTION_SKIP
) {
592 strvec_push(&am
.args
, "--skip");
593 strvec_pushf(&am
.args
, "--resolvemsg=%s", rebase_resolvemsg
);
594 status
= run_command(&am
);
598 return move_to_original_branch(opts
);
600 if (opts
->action
== ACTION_SHOW_CURRENT_PATCH
) {
601 strvec_push(&am
.args
, "--show-current-patch");
602 return run_command(&am
);
605 rebased_patches
= xstrdup(git_path("rebased-patches"));
606 format_patch
.out
= open(rebased_patches
,
607 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
608 if (format_patch
.out
< 0) {
609 status
= error_errno(_("could not open '%s' for writing"),
611 free(rebased_patches
);
612 child_process_clear(&am
);
616 format_patch
.git_cmd
= 1;
617 strvec_pushl(&format_patch
.args
, "format-patch", "-k", "--stdout",
618 "--full-index", "--cherry-pick", "--right-only",
619 "--default-prefix", "--no-renames",
620 "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
622 if (opts
->git_format_patch_opt
.len
)
623 strvec_split(&format_patch
.args
,
624 opts
->git_format_patch_opt
.buf
);
625 strvec_pushf(&format_patch
.args
, "%s...%s",
626 oid_to_hex(opts
->root
?
627 /* this is now equivalent to !opts->upstream */
628 &opts
->onto
->object
.oid
:
629 &opts
->upstream
->object
.oid
),
630 oid_to_hex(&opts
->orig_head
->object
.oid
));
631 if (opts
->restrict_revision
)
632 strvec_pushf(&format_patch
.args
, "^%s",
633 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
635 status
= run_command(&format_patch
);
637 struct reset_head_opts ropts
= { 0 };
638 unlink(rebased_patches
);
639 free(rebased_patches
);
640 child_process_clear(&am
);
642 ropts
.oid
= &opts
->orig_head
->object
.oid
;
643 ropts
.branch
= opts
->head_name
;
644 ropts
.default_reflog_action
= opts
->reflog_action
;
645 reset_head(the_repository
, &ropts
);
646 error(_("\ngit encountered an error while preparing the "
647 "patches to replay\n"
650 "As a result, git cannot rebase them."),
656 am
.in
= open(rebased_patches
, O_RDONLY
);
658 status
= error_errno(_("could not open '%s' for reading"),
660 free(rebased_patches
);
661 child_process_clear(&am
);
665 strvec_pushv(&am
.args
, opts
->git_am_opts
.v
);
666 strvec_push(&am
.args
, "--rebasing");
667 strvec_pushf(&am
.args
, "--resolvemsg=%s", rebase_resolvemsg
);
668 strvec_push(&am
.args
, "--patch-format=mboxrd");
669 if (opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
)
670 strvec_push(&am
.args
, "--rerere-autoupdate");
671 else if (opts
->allow_rerere_autoupdate
== RERERE_NOAUTOUPDATE
)
672 strvec_push(&am
.args
, "--no-rerere-autoupdate");
673 if (opts
->gpg_sign_opt
)
674 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
675 status
= run_command(&am
);
676 unlink(rebased_patches
);
677 free(rebased_patches
);
680 return move_to_original_branch(opts
);
683 if (is_directory(opts
->state_dir
))
684 rebase_write_basic_state(opts
);
689 static int run_specific_rebase(struct rebase_options
*opts
)
693 if (opts
->type
== REBASE_MERGE
) {
694 /* Run sequencer-based rebase */
695 if (!(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
))
696 setenv("GIT_SEQUENCE_EDITOR", ":", 1);
697 if (opts
->gpg_sign_opt
) {
698 /* remove the leading "-S" */
699 char *tmp
= xstrdup(opts
->gpg_sign_opt
+ 2);
700 free(opts
->gpg_sign_opt
);
701 opts
->gpg_sign_opt
= tmp
;
704 status
= run_sequencer_rebase(opts
);
705 } else if (opts
->type
== REBASE_APPLY
)
706 status
= run_am(opts
);
708 BUG("Unhandled rebase type %d", opts
->type
);
710 if (opts
->dont_finish_rebase
)
712 else if (opts
->type
== REBASE_MERGE
)
713 ; /* merge backend cleans up after itself */
714 else if (status
== 0) {
715 if (!file_exists(state_dir_path("stopped-sha", opts
)))
717 } else if (status
== 2) {
718 struct strbuf dir
= STRBUF_INIT
;
720 apply_autostash(state_dir_path("autostash", opts
));
721 strbuf_addstr(&dir
, opts
->state_dir
);
722 remove_dir_recursively(&dir
, 0);
723 strbuf_release(&dir
);
724 die("Nothing to do");
727 return status
? -1 : 0;
730 static void parse_rebase_merges_value(struct rebase_options
*options
, const char *value
)
732 if (!strcmp("no-rebase-cousins", value
))
733 options
->rebase_cousins
= 0;
734 else if (!strcmp("rebase-cousins", value
))
735 options
->rebase_cousins
= 1;
737 die(_("Unknown rebase-merges mode: %s"), value
);
740 static int rebase_config(const char *var
, const char *value
,
741 const struct config_context
*ctx
, void *data
)
743 struct rebase_options
*opts
= data
;
745 if (!strcmp(var
, "rebase.stat")) {
746 if (git_config_bool(var
, value
))
747 opts
->flags
|= REBASE_DIFFSTAT
;
749 opts
->flags
&= ~REBASE_DIFFSTAT
;
753 if (!strcmp(var
, "rebase.autosquash")) {
754 opts
->config_autosquash
= git_config_bool(var
, value
);
758 if (!strcmp(var
, "commit.gpgsign")) {
759 free(opts
->gpg_sign_opt
);
760 opts
->gpg_sign_opt
= git_config_bool(var
, value
) ?
761 xstrdup("-S") : NULL
;
765 if (!strcmp(var
, "rebase.autostash")) {
766 opts
->autostash
= git_config_bool(var
, value
);
770 if (!strcmp(var
, "rebase.rebasemerges")) {
771 opts
->config_rebase_merges
= git_parse_maybe_bool(value
);
772 if (opts
->config_rebase_merges
< 0) {
773 opts
->config_rebase_merges
= 1;
774 parse_rebase_merges_value(opts
, value
);
776 opts
->rebase_cousins
= 0;
781 if (!strcmp(var
, "rebase.updaterefs")) {
782 opts
->config_update_refs
= git_config_bool(var
, value
);
786 if (!strcmp(var
, "rebase.reschedulefailedexec")) {
787 opts
->reschedule_failed_exec
= git_config_bool(var
, value
);
791 if (!strcmp(var
, "rebase.forkpoint")) {
792 opts
->fork_point
= git_config_bool(var
, value
) ? -1 : 0;
796 if (!strcmp(var
, "rebase.backend")) {
797 return git_config_string(&opts
->default_backend
, var
, value
);
800 return git_default_config(var
, value
, ctx
, data
);
803 static int checkout_up_to_date(struct rebase_options
*options
)
805 struct strbuf buf
= STRBUF_INIT
;
806 struct reset_head_opts ropts
= { 0 };
809 strbuf_addf(&buf
, "%s: checkout %s",
810 options
->reflog_action
, options
->switch_to
);
811 ropts
.oid
= &options
->orig_head
->object
.oid
;
812 ropts
.branch
= options
->head_name
;
813 ropts
.flags
= RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
815 ropts
.flags
|= RESET_HEAD_DETACH
;
816 ropts
.head_msg
= buf
.buf
;
817 if (reset_head(the_repository
, &ropts
) < 0)
818 ret
= error(_("could not switch to %s"), options
->switch_to
);
819 strbuf_release(&buf
);
825 * Determines whether the commits in from..to are linear, i.e. contain
826 * no merge commits. This function *expects* `from` to be an ancestor of
829 static int is_linear_history(struct commit
*from
, struct commit
*to
)
831 while (to
&& to
!= from
) {
832 repo_parse_commit(the_repository
, to
);
835 if (to
->parents
->next
)
837 to
= to
->parents
->item
;
842 static int can_fast_forward(struct commit
*onto
, struct commit
*upstream
,
843 struct commit
*restrict_revision
,
844 struct commit
*head
, struct object_id
*branch_base
)
846 struct commit_list
*merge_bases
= NULL
;
849 if (is_null_oid(branch_base
))
850 goto done
; /* fill_branch_base() found multiple merge bases */
852 if (!oideq(branch_base
, &onto
->object
.oid
))
855 if (restrict_revision
&& !oideq(&restrict_revision
->object
.oid
, branch_base
))
861 if (repo_get_merge_bases(the_repository
, upstream
, head
, &merge_bases
) < 0)
863 if (!merge_bases
|| merge_bases
->next
)
866 if (!oideq(&onto
->object
.oid
, &merge_bases
->item
->object
.oid
))
872 free_commit_list(merge_bases
);
873 return res
&& is_linear_history(onto
, head
);
876 static void fill_branch_base(struct rebase_options
*options
,
877 struct object_id
*branch_base
)
879 struct commit_list
*merge_bases
= NULL
;
881 if (repo_get_merge_bases(the_repository
, options
->onto
,
882 options
->orig_head
, &merge_bases
) < 0)
884 if (!merge_bases
|| merge_bases
->next
)
885 oidcpy(branch_base
, null_oid());
887 oidcpy(branch_base
, &merge_bases
->item
->object
.oid
);
889 free_commit_list(merge_bases
);
892 static int parse_opt_am(const struct option
*opt
, const char *arg
, int unset
)
894 struct rebase_options
*opts
= opt
->value
;
896 BUG_ON_OPT_NEG(unset
);
899 if (opts
->type
!= REBASE_UNSPECIFIED
&& opts
->type
!= REBASE_APPLY
)
900 die(_("apply options and merge options cannot be used together"));
902 opts
->type
= REBASE_APPLY
;
907 /* -i followed by -m is still -i */
908 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
910 struct rebase_options
*opts
= opt
->value
;
912 BUG_ON_OPT_NEG(unset
);
915 if (opts
->type
!= REBASE_UNSPECIFIED
&& opts
->type
!= REBASE_MERGE
)
916 die(_("apply options and merge options cannot be used together"));
918 opts
->type
= REBASE_MERGE
;
923 /* -i followed by -r is still explicitly interactive, but -r alone is not */
924 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
927 struct rebase_options
*opts
= opt
->value
;
929 BUG_ON_OPT_NEG(unset
);
932 if (opts
->type
!= REBASE_UNSPECIFIED
&& opts
->type
!= REBASE_MERGE
)
933 die(_("apply options and merge options cannot be used together"));
935 opts
->type
= REBASE_MERGE
;
936 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
941 static enum empty_type
parse_empty_value(const char *value
)
943 if (!strcasecmp(value
, "drop"))
945 else if (!strcasecmp(value
, "keep"))
947 else if (!strcasecmp(value
, "stop"))
949 else if (!strcasecmp(value
, "ask")) {
950 warning(_("--empty=ask is deprecated; use '--empty=stop' instead."));
954 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"stop\"."), value
);
957 static int parse_opt_keep_empty(const struct option
*opt
, const char *arg
,
960 struct rebase_options
*opts
= opt
->value
;
964 imply_merge(opts
, unset
? "--no-keep-empty" : "--keep-empty");
965 opts
->keep_empty
= !unset
;
969 static int parse_opt_empty(const struct option
*opt
, const char *arg
, int unset
)
971 struct rebase_options
*options
= opt
->value
;
972 enum empty_type value
= parse_empty_value(arg
);
974 BUG_ON_OPT_NEG(unset
);
976 options
->empty
= value
;
980 static int parse_opt_rebase_merges(const struct option
*opt
, const char *arg
, int unset
)
982 struct rebase_options
*options
= opt
->value
;
984 options
->rebase_merges
= !unset
;
985 options
->rebase_cousins
= 0;
989 warning(_("--rebase-merges with an empty string "
990 "argument is deprecated and will stop "
991 "working in a future version of Git. Use "
992 "--rebase-merges without an argument "
993 "instead, which does the same thing."));
996 parse_rebase_merges_value(options
, arg
);
1002 static void NORETURN
error_on_missing_default_upstream(void)
1004 struct branch
*current_branch
= branch_get(NULL
);
1007 "Please specify which branch you want to rebase against.\n"
1008 "See git-rebase(1) for details.\n"
1010 " git rebase '<branch>'\n"
1012 current_branch
? _("There is no tracking information for "
1013 "the current branch.") :
1014 _("You are not currently on a branch."));
1016 if (current_branch
) {
1017 const char *remote
= current_branch
->remote_name
;
1020 remote
= _("<remote>");
1022 printf(_("If you wish to set tracking information for this "
1023 "branch you can do so with:\n"
1025 " git branch --set-upstream-to=%s/<branch> %s\n"
1027 remote
, current_branch
->name
);
1032 static int check_exec_cmd(const char *cmd
)
1034 if (strchr(cmd
, '\n'))
1035 return error(_("exec commands cannot contain newlines"));
1037 /* Does the command consist purely of whitespace? */
1038 if (!cmd
[strspn(cmd
, " \t\r\f\v")])
1039 return error(_("empty exec command"));
1044 int cmd_rebase(int argc
, const char **argv
, const char *prefix
)
1046 struct rebase_options options
= REBASE_OPTIONS_INIT
;
1047 const char *branch_name
;
1048 int ret
, flags
, total_argc
, in_progress
= 0;
1050 int ok_to_skip_pre_rebase
= 0;
1051 struct strbuf msg
= STRBUF_INIT
;
1052 struct strbuf revisions
= STRBUF_INIT
;
1053 struct strbuf buf
= STRBUF_INIT
;
1054 struct object_id branch_base
;
1055 int ignore_whitespace
= 0;
1056 const char *gpg_sign
= NULL
;
1057 struct object_id squash_onto
;
1058 char *squash_onto_name
= NULL
;
1059 char *keep_base_onto_name
= NULL
;
1060 int reschedule_failed_exec
= -1;
1061 int allow_preemptive_ff
= 1;
1062 int preserve_merges_selected
= 0;
1063 struct reset_head_opts ropts
= { 0 };
1064 struct option builtin_rebase_options
[] = {
1065 OPT_STRING(0, "onto", &options
.onto_name
,
1067 N_("rebase onto given branch instead of upstream")),
1068 OPT_BOOL(0, "keep-base", &keep_base
,
1069 N_("use the merge-base of upstream and branch as the current base")),
1070 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
1071 N_("allow pre-rebase hook to run")),
1072 OPT_NEGBIT('q', "quiet", &options
.flags
,
1073 N_("be quiet. implies --no-stat"),
1074 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1075 OPT_BIT('v', "verbose", &options
.flags
,
1076 N_("display a diffstat of what changed upstream"),
1077 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1078 {OPTION_NEGBIT
, 'n', "no-stat", &options
.flags
, NULL
,
1079 N_("do not show diffstat of what changed upstream"),
1080 PARSE_OPT_NOARG
, NULL
, REBASE_DIFFSTAT
},
1081 OPT_BOOL(0, "signoff", &options
.signoff
,
1082 N_("add a Signed-off-by trailer to each commit")),
1083 OPT_BOOL(0, "committer-date-is-author-date",
1084 &options
.committer_date_is_author_date
,
1085 N_("make committer date match author date")),
1086 OPT_BOOL(0, "reset-author-date", &options
.ignore_date
,
1087 N_("ignore author date and use current date")),
1088 OPT_HIDDEN_BOOL(0, "ignore-date", &options
.ignore_date
,
1089 N_("synonym of --reset-author-date")),
1090 OPT_PASSTHRU_ARGV('C', NULL
, &options
.git_am_opts
, N_("n"),
1091 N_("passed to 'git apply'"), 0),
1092 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace
,
1093 N_("ignore changes in whitespace")),
1094 OPT_PASSTHRU_ARGV(0, "whitespace", &options
.git_am_opts
,
1095 N_("action"), N_("passed to 'git apply'"), 0),
1096 OPT_BIT('f', "force-rebase", &options
.flags
,
1097 N_("cherry-pick all commits, even if unchanged"),
1099 OPT_BIT(0, "no-ff", &options
.flags
,
1100 N_("cherry-pick all commits, even if unchanged"),
1102 OPT_CMDMODE(0, "continue", &options
.action
, N_("continue"),
1104 OPT_CMDMODE(0, "skip", &options
.action
,
1105 N_("skip current patch and continue"), ACTION_SKIP
),
1106 OPT_CMDMODE(0, "abort", &options
.action
,
1107 N_("abort and check out the original branch"),
1109 OPT_CMDMODE(0, "quit", &options
.action
,
1110 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
1111 OPT_CMDMODE(0, "edit-todo", &options
.action
, N_("edit the todo list "
1112 "during an interactive rebase"), ACTION_EDIT_TODO
),
1113 OPT_CMDMODE(0, "show-current-patch", &options
.action
,
1114 N_("show the patch file being applied or merged"),
1115 ACTION_SHOW_CURRENT_PATCH
),
1116 OPT_CALLBACK_F(0, "apply", &options
, NULL
,
1117 N_("use apply strategies to rebase"),
1118 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1120 OPT_CALLBACK_F('m', "merge", &options
, NULL
,
1121 N_("use merging strategies to rebase"),
1122 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1124 OPT_CALLBACK_F('i', "interactive", &options
, NULL
,
1125 N_("let the user edit the list of commits to rebase"),
1126 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1127 parse_opt_interactive
),
1128 OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected
,
1129 N_("(REMOVED) was: try to recreate merges "
1130 "instead of ignoring them"),
1131 1, PARSE_OPT_HIDDEN
),
1132 OPT_RERERE_AUTOUPDATE(&options
.allow_rerere_autoupdate
),
1133 OPT_CALLBACK_F(0, "empty", &options
, "(drop|keep|stop)",
1134 N_("how to handle commits that become empty"),
1135 PARSE_OPT_NONEG
, parse_opt_empty
),
1136 OPT_CALLBACK_F('k', "keep-empty", &options
, NULL
,
1137 N_("keep commits which start empty"),
1138 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
,
1139 parse_opt_keep_empty
),
1140 OPT_BOOL(0, "autosquash", &options
.autosquash
,
1141 N_("move commits that begin with "
1142 "squash!/fixup! under -i")),
1143 OPT_BOOL(0, "update-refs", &options
.update_refs
,
1144 N_("update branches that point to commits "
1145 "that are being rebased")),
1146 { OPTION_STRING
, 'S', "gpg-sign", &gpg_sign
, N_("key-id"),
1147 N_("GPG-sign commits"),
1148 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1149 OPT_AUTOSTASH(&options
.autostash
),
1150 OPT_STRING_LIST('x', "exec", &options
.exec
, N_("exec"),
1151 N_("add exec lines after each commit of the "
1153 OPT_BOOL_F(0, "allow-empty-message",
1154 &options
.allow_empty_message
,
1155 N_("allow rebasing commits with empty messages"),
1157 OPT_CALLBACK_F('r', "rebase-merges", &options
, N_("mode"),
1158 N_("try to rebase merges instead of skipping them"),
1159 PARSE_OPT_OPTARG
, parse_opt_rebase_merges
),
1160 OPT_BOOL(0, "fork-point", &options
.fork_point
,
1161 N_("use 'merge-base --fork-point' to refine upstream")),
1162 OPT_STRING('s', "strategy", &options
.strategy
,
1163 N_("strategy"), N_("use the given merge strategy")),
1164 OPT_STRING_LIST('X', "strategy-option", &options
.strategy_opts
,
1166 N_("pass the argument through to the merge "
1168 OPT_BOOL(0, "root", &options
.root
,
1169 N_("rebase all reachable commits up to the root(s)")),
1170 OPT_BOOL(0, "reschedule-failed-exec",
1171 &reschedule_failed_exec
,
1172 N_("automatically re-schedule any `exec` that fails")),
1173 OPT_BOOL(0, "reapply-cherry-picks", &options
.reapply_cherry_picks
,
1174 N_("apply all changes, even those already present upstream")),
1179 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1180 usage_with_options(builtin_rebase_usage
,
1181 builtin_rebase_options
);
1183 prepare_repo_settings(the_repository
);
1184 the_repository
->settings
.command_requires_full_index
= 0;
1186 git_config(rebase_config
, &options
);
1187 /* options.gpg_sign_opt will be either "-S" or NULL */
1188 gpg_sign
= options
.gpg_sign_opt
? "" : NULL
;
1189 FREE_AND_NULL(options
.gpg_sign_opt
);
1192 strbuf_addf(&buf
, "%s/applying", apply_dir());
1193 if(file_exists(buf
.buf
))
1194 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1196 if (is_directory(apply_dir())) {
1197 options
.type
= REBASE_APPLY
;
1198 options
.state_dir
= apply_dir();
1199 } else if (is_directory(merge_dir())) {
1201 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
1202 if (!(options
.action
== ACTION_ABORT
) && is_directory(buf
.buf
)) {
1203 die(_("`rebase --preserve-merges` (-p) is no longer supported.\n"
1204 "Use `git rebase --abort` to terminate current rebase.\n"
1205 "Or downgrade to v2.33, or earlier, to complete the rebase."));
1208 strbuf_addf(&buf
, "%s/interactive", merge_dir());
1209 options
.type
= REBASE_MERGE
;
1210 if (file_exists(buf
.buf
))
1211 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
1213 options
.state_dir
= merge_dir();
1216 if (options
.type
!= REBASE_UNSPECIFIED
)
1220 argc
= parse_options(argc
, argv
, prefix
,
1221 builtin_rebase_options
,
1222 builtin_rebase_usage
, 0);
1224 if (preserve_merges_selected
)
1225 die(_("--preserve-merges was replaced by --rebase-merges\n"
1226 "Note: Your `pull.rebase` configuration may also be set to 'preserve',\n"
1227 "which is no longer supported; use 'merges' instead"));
1229 if (options
.action
!= ACTION_NONE
&& total_argc
!= 2) {
1230 usage_with_options(builtin_rebase_usage
,
1231 builtin_rebase_options
);
1235 usage_with_options(builtin_rebase_usage
,
1236 builtin_rebase_options
);
1239 if (options
.onto_name
)
1240 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--onto");
1242 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--root");
1244 * --keep-base defaults to --no-fork-point to keep the
1247 if (options
.fork_point
< 0)
1248 options
.fork_point
= 0;
1250 if (options
.root
&& options
.fork_point
> 0)
1251 die(_("options '%s' and '%s' cannot be used together"), "--root", "--fork-point");
1253 if (options
.action
!= ACTION_NONE
&& !in_progress
)
1254 die(_("no rebase in progress"));
1256 if (options
.action
== ACTION_EDIT_TODO
&& !is_merge(&options
))
1257 die(_("The --edit-todo action can only be used during "
1258 "interactive rebase."));
1260 if (trace2_is_enabled()) {
1261 if (is_merge(&options
))
1262 trace2_cmd_mode("interactive");
1263 else if (options
.exec
.nr
)
1264 trace2_cmd_mode("interactive-exec");
1266 trace2_cmd_mode(action_names
[options
.action
]);
1269 options
.reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
1270 options
.reflog_action
=
1271 xstrdup(options
.reflog_action
? options
.reflog_action
: "rebase");
1273 switch (options
.action
) {
1274 case ACTION_CONTINUE
: {
1275 struct object_id head
;
1276 struct lock_file lock_file
= LOCK_INIT
;
1280 if (repo_get_oid(the_repository
, "HEAD", &head
))
1281 die(_("Cannot read HEAD"));
1283 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
1284 if (repo_read_index(the_repository
) < 0)
1285 die(_("could not read index"));
1286 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
1289 repo_update_index_if_able(the_repository
, &lock_file
);
1290 rollback_lock_file(&lock_file
);
1292 if (has_unstaged_changes(the_repository
, 1)) {
1293 puts(_("You must edit all merge conflicts and then\n"
1294 "mark them as resolved using git add"));
1297 if (read_basic_state(&options
))
1302 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1304 rerere_clear(the_repository
, &merge_rr
);
1305 string_list_clear(&merge_rr
, 1);
1306 ropts
.flags
= RESET_HEAD_HARD
;
1307 if (reset_head(the_repository
, &ropts
) < 0)
1308 die(_("could not discard worktree changes"));
1309 remove_branch_state(the_repository
, 0);
1310 if (read_basic_state(&options
))
1314 case ACTION_ABORT
: {
1315 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1316 struct strbuf head_msg
= STRBUF_INIT
;
1318 rerere_clear(the_repository
, &merge_rr
);
1319 string_list_clear(&merge_rr
, 1);
1321 if (read_basic_state(&options
))
1324 strbuf_addf(&head_msg
, "%s (abort): returning to %s",
1325 options
.reflog_action
,
1326 options
.head_name
? options
.head_name
1327 : oid_to_hex(&options
.orig_head
->object
.oid
));
1328 ropts
.oid
= &options
.orig_head
->object
.oid
;
1329 ropts
.head_msg
= head_msg
.buf
;
1330 ropts
.branch
= options
.head_name
;
1331 ropts
.flags
= RESET_HEAD_HARD
;
1332 if (reset_head(the_repository
, &ropts
) < 0)
1333 die(_("could not move back to %s"),
1334 oid_to_hex(&options
.orig_head
->object
.oid
));
1335 strbuf_release(&head_msg
);
1336 remove_branch_state(the_repository
, 0);
1337 ret
= finish_rebase(&options
);
1341 save_autostash(state_dir_path("autostash", &options
));
1342 if (options
.type
== REBASE_MERGE
) {
1343 struct replay_opts replay
= REPLAY_OPTS_INIT
;
1345 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
1346 ret
= sequencer_remove_state(&replay
);
1347 replay_opts_release(&replay
);
1350 strbuf_addstr(&buf
, options
.state_dir
);
1351 ret
= remove_dir_recursively(&buf
, 0);
1353 error(_("could not remove '%s'"),
1358 case ACTION_EDIT_TODO
:
1359 options
.dont_finish_rebase
= 1;
1361 case ACTION_SHOW_CURRENT_PATCH
:
1362 options
.dont_finish_rebase
= 1;
1367 BUG("action: %d", options
.action
);
1370 /* Make sure no rebase is in progress */
1372 const char *last_slash
= strrchr(options
.state_dir
, '/');
1373 const char *state_dir_base
=
1374 last_slash
? last_slash
+ 1 : options
.state_dir
;
1375 const char *cmd_live_rebase
=
1376 "git rebase (--continue | --abort | --skip)";
1378 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
1379 die(_("It seems that there is already a %s directory, and\n"
1380 "I wonder if you are in the middle of another rebase. "
1382 "case, please try\n\t%s\n"
1383 "If that is not the case, please\n\t%s\n"
1384 "and run me again. I am stopping in case you still "
1386 "valuable there.\n"),
1387 state_dir_base
, cmd_live_rebase
, buf
.buf
);
1390 if ((options
.flags
& REBASE_INTERACTIVE_EXPLICIT
) ||
1391 (options
.action
!= ACTION_NONE
) ||
1392 (options
.exec
.nr
> 0) ||
1393 options
.autosquash
== 1) {
1394 allow_preemptive_ff
= 0;
1396 if (options
.committer_date_is_author_date
|| options
.ignore_date
)
1397 options
.flags
|= REBASE_FORCE
;
1399 for (i
= 0; i
< options
.git_am_opts
.nr
; i
++) {
1400 const char *option
= options
.git_am_opts
.v
[i
], *p
;
1401 if (!strcmp(option
, "--whitespace=fix") ||
1402 !strcmp(option
, "--whitespace=strip"))
1403 allow_preemptive_ff
= 0;
1404 else if (skip_prefix(option
, "-C", &p
)) {
1406 if (!isdigit(*(p
++)))
1407 die(_("switch `C' expects a "
1408 "numerical value"));
1409 } else if (skip_prefix(option
, "--whitespace=", &p
)) {
1410 if (*p
&& strcmp(p
, "warn") && strcmp(p
, "nowarn") &&
1411 strcmp(p
, "error") && strcmp(p
, "error-all"))
1412 die("Invalid whitespace option: '%s'", p
);
1416 for (i
= 0; i
< options
.exec
.nr
; i
++)
1417 if (check_exec_cmd(options
.exec
.items
[i
].string
))
1420 if (!(options
.flags
& REBASE_NO_QUIET
))
1421 strvec_push(&options
.git_am_opts
, "-q");
1423 if (options
.empty
!= EMPTY_UNSPECIFIED
)
1424 imply_merge(&options
, "--empty");
1426 if (options
.reapply_cherry_picks
< 0)
1428 * We default to --no-reapply-cherry-picks unless
1429 * --keep-base is given; when --keep-base is given, we want
1430 * to default to --reapply-cherry-picks.
1432 options
.reapply_cherry_picks
= keep_base
;
1433 else if (!keep_base
)
1435 * The apply backend always searches for and drops cherry
1436 * picks. This is often not wanted with --keep-base, so
1437 * --keep-base allows --reapply-cherry-picks to be
1438 * simulated by altering the upstream such that
1439 * cherry-picks cannot be detected and thus all commits are
1440 * reapplied. Thus, --[no-]reapply-cherry-picks is
1441 * supported when --keep-base is specified, but not when
1442 * --keep-base is left out.
1444 imply_merge(&options
, options
.reapply_cherry_picks
?
1445 "--reapply-cherry-picks" :
1446 "--no-reapply-cherry-picks");
1449 options
.gpg_sign_opt
= xstrfmt("-S%s", gpg_sign
);
1451 if (options
.exec
.nr
)
1452 imply_merge(&options
, "--exec");
1454 if (options
.type
== REBASE_APPLY
) {
1455 if (ignore_whitespace
)
1456 strvec_push(&options
.git_am_opts
,
1457 "--ignore-whitespace");
1458 if (options
.committer_date_is_author_date
)
1459 strvec_push(&options
.git_am_opts
,
1460 "--committer-date-is-author-date");
1461 if (options
.ignore_date
)
1462 strvec_push(&options
.git_am_opts
, "--ignore-date");
1465 if (ignore_whitespace
) {
1466 string_list_append(&options
.strategy_opts
,
1467 "ignore-space-change");
1471 if (options
.strategy_opts
.nr
&& !options
.strategy
)
1472 options
.strategy
= "ort";
1474 if (options
.strategy
) {
1475 options
.strategy
= xstrdup(options
.strategy
);
1476 imply_merge(&options
, "--strategy");
1479 if (options
.root
&& !options
.onto_name
)
1480 imply_merge(&options
, "--root without --onto");
1482 if (isatty(2) && options
.flags
& REBASE_NO_QUIET
)
1483 strbuf_addstr(&options
.git_format_patch_opt
, " --progress");
1485 if (options
.git_am_opts
.nr
|| options
.type
== REBASE_APPLY
) {
1486 /* all am options except -q are compatible only with --apply */
1487 for (i
= options
.git_am_opts
.nr
- 1; i
>= 0; i
--)
1488 if (strcmp(options
.git_am_opts
.v
[i
], "-q"))
1491 if (i
>= 0 || options
.type
== REBASE_APPLY
) {
1492 if (is_merge(&options
))
1493 die(_("apply options and merge options "
1494 "cannot be used together"));
1495 else if (options
.rebase_merges
== -1 && options
.config_rebase_merges
== 1)
1496 die(_("apply options are incompatible with rebase.rebaseMerges. Consider adding --no-rebase-merges"));
1497 else if (options
.update_refs
== -1 && options
.config_update_refs
== 1)
1498 die(_("apply options are incompatible with rebase.updateRefs. Consider adding --no-update-refs"));
1500 options
.type
= REBASE_APPLY
;
1504 if (options
.update_refs
== 1)
1505 imply_merge(&options
, "--update-refs");
1506 options
.update_refs
= (options
.update_refs
>= 0) ? options
.update_refs
:
1507 ((options
.config_update_refs
>= 0) ? options
.config_update_refs
: 0);
1509 if (options
.rebase_merges
== 1)
1510 imply_merge(&options
, "--rebase-merges");
1511 options
.rebase_merges
= (options
.rebase_merges
>= 0) ? options
.rebase_merges
:
1512 ((options
.config_rebase_merges
>= 0) ? options
.config_rebase_merges
: 0);
1514 if (options
.autosquash
== 1) {
1515 imply_merge(&options
, "--autosquash");
1516 } else if (options
.autosquash
== -1) {
1517 options
.autosquash
=
1518 options
.config_autosquash
&&
1519 (options
.flags
& REBASE_INTERACTIVE_EXPLICIT
);
1522 if (options
.type
== REBASE_UNSPECIFIED
) {
1523 if (!strcmp(options
.default_backend
, "merge"))
1524 options
.type
= REBASE_MERGE
;
1525 else if (!strcmp(options
.default_backend
, "apply"))
1526 options
.type
= REBASE_APPLY
;
1528 die(_("Unknown rebase backend: %s"),
1529 options
.default_backend
);
1532 if (options
.type
== REBASE_MERGE
&&
1533 !options
.strategy
&&
1534 getenv("GIT_TEST_MERGE_ALGORITHM"))
1535 options
.strategy
= xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
1537 switch (options
.type
) {
1539 options
.state_dir
= merge_dir();
1542 options
.state_dir
= apply_dir();
1545 BUG("options.type was just set above; should be unreachable.");
1548 if (options
.empty
== EMPTY_UNSPECIFIED
) {
1549 if (options
.flags
& REBASE_INTERACTIVE_EXPLICIT
)
1550 options
.empty
= EMPTY_STOP
;
1551 else if (options
.exec
.nr
> 0)
1552 options
.empty
= EMPTY_KEEP
;
1554 options
.empty
= EMPTY_DROP
;
1556 if (reschedule_failed_exec
> 0 && !is_merge(&options
))
1557 die(_("--reschedule-failed-exec requires "
1558 "--exec or --interactive"));
1559 if (reschedule_failed_exec
>= 0)
1560 options
.reschedule_failed_exec
= reschedule_failed_exec
;
1562 if (options
.signoff
) {
1563 strvec_push(&options
.git_am_opts
, "--signoff");
1564 options
.flags
|= REBASE_FORCE
;
1567 if (!options
.root
) {
1569 struct branch
*branch
;
1571 branch
= branch_get(NULL
);
1572 options
.upstream_name
= branch_get_upstream(branch
,
1574 if (!options
.upstream_name
)
1575 error_on_missing_default_upstream();
1576 if (options
.fork_point
< 0)
1577 options
.fork_point
= 1;
1579 options
.upstream_name
= argv
[0];
1582 if (!strcmp(options
.upstream_name
, "-"))
1583 options
.upstream_name
= "@{-1}";
1586 lookup_commit_reference_by_name(options
.upstream_name
);
1587 if (!options
.upstream
)
1588 die(_("invalid upstream '%s'"), options
.upstream_name
);
1589 options
.upstream_arg
= options
.upstream_name
;
1591 if (!options
.onto_name
) {
1592 if (commit_tree("", 0, the_hash_algo
->empty_tree
, NULL
,
1593 &squash_onto
, NULL
, NULL
) < 0)
1594 die(_("Could not create new root commit"));
1595 options
.squash_onto
= &squash_onto
;
1596 options
.onto_name
= squash_onto_name
=
1597 xstrdup(oid_to_hex(&squash_onto
));
1599 options
.root_with_onto
= 1;
1601 options
.upstream_name
= NULL
;
1602 options
.upstream
= NULL
;
1604 usage_with_options(builtin_rebase_usage
,
1605 builtin_rebase_options
);
1606 options
.upstream_arg
= "--root";
1610 * If the branch to rebase is given, that is the branch we will rebase
1611 * branch_name -- branch/commit being rebased, or
1612 * HEAD (already detached)
1613 * orig_head -- commit object name of tip of the branch before rebasing
1614 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1617 /* Is it "rebase other branchname" or "rebase other commit"? */
1618 struct object_id branch_oid
;
1619 branch_name
= argv
[0];
1620 options
.switch_to
= argv
[0];
1622 /* Is it a local branch? */
1624 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
1625 if (!read_ref(buf
.buf
, &branch_oid
)) {
1626 die_if_checked_out(buf
.buf
, 1);
1627 options
.head_name
= xstrdup(buf
.buf
);
1629 lookup_commit_object(the_repository
,
1631 /* If not is it a valid ref (branch or commit)? */
1634 lookup_commit_reference_by_name(branch_name
);
1635 options
.head_name
= NULL
;
1637 if (!options
.orig_head
)
1638 die(_("no such branch/commit '%s'"), branch_name
);
1639 } else if (argc
== 0) {
1640 /* Do not need to switch branches, we are already on it. */
1642 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL
,
1644 if (!options
.head_name
)
1645 die(_("No such ref: %s"), "HEAD");
1646 if (flags
& REF_ISSYMREF
) {
1647 if (!skip_prefix(options
.head_name
,
1648 "refs/heads/", &branch_name
))
1649 branch_name
= options
.head_name
;
1652 FREE_AND_NULL(options
.head_name
);
1653 branch_name
= "HEAD";
1655 options
.orig_head
= lookup_commit_reference_by_name("HEAD");
1656 if (!options
.orig_head
)
1657 die(_("Could not resolve HEAD to a commit"));
1659 BUG("unexpected number of arguments left to parse");
1661 /* Make sure the branch to rebase onto is valid. */
1664 strbuf_addstr(&buf
, options
.upstream_name
);
1665 strbuf_addstr(&buf
, "...");
1666 strbuf_addstr(&buf
, branch_name
);
1667 options
.onto_name
= keep_base_onto_name
= xstrdup(buf
.buf
);
1668 } else if (!options
.onto_name
)
1669 options
.onto_name
= options
.upstream_name
;
1670 if (strstr(options
.onto_name
, "...")) {
1671 if (repo_get_oid_mb(the_repository
, options
.onto_name
, &branch_base
) < 0) {
1673 die(_("'%s': need exactly one merge base with branch"),
1674 options
.upstream_name
);
1676 die(_("'%s': need exactly one merge base"),
1679 options
.onto
= lookup_commit_or_die(&branch_base
,
1683 lookup_commit_reference_by_name(options
.onto_name
);
1685 die(_("Does not point to a valid commit '%s'"),
1687 fill_branch_base(&options
, &branch_base
);
1690 if (keep_base
&& options
.reapply_cherry_picks
)
1691 options
.upstream
= options
.onto
;
1693 if (options
.fork_point
> 0)
1694 options
.restrict_revision
=
1695 get_fork_point(options
.upstream_name
, options
.orig_head
);
1697 if (repo_read_index(the_repository
) < 0)
1698 die(_("could not read index"));
1700 if (options
.autostash
)
1701 create_autostash(the_repository
,
1702 state_dir_path("autostash", &options
));
1705 if (require_clean_work_tree(the_repository
, "rebase",
1706 _("Please commit or stash them."), 1, 1)) {
1712 * Now we are rebasing commits upstream..orig_head (or with --root,
1713 * everything leading up to orig_head) on top of onto.
1717 * Check if we are already based on onto with linear history,
1718 * in which case we could fast-forward without replacing the commits
1719 * with new commits recreated by replaying their changes.
1721 if (allow_preemptive_ff
&&
1722 can_fast_forward(options
.onto
, options
.upstream
, options
.restrict_revision
,
1723 options
.orig_head
, &branch_base
)) {
1726 if (!(options
.flags
& REBASE_FORCE
)) {
1727 /* Lazily switch to the target branch if needed... */
1728 if (options
.switch_to
) {
1729 ret
= checkout_up_to_date(&options
);
1734 if (!(options
.flags
& REBASE_NO_QUIET
))
1736 else if (!strcmp(branch_name
, "HEAD") &&
1737 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1738 puts(_("HEAD is up to date."));
1740 printf(_("Current branch %s is up to date.\n"),
1742 ret
= finish_rebase(&options
);
1744 } else if (!(options
.flags
& REBASE_NO_QUIET
))
1746 else if (!strcmp(branch_name
, "HEAD") &&
1747 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1748 puts(_("HEAD is up to date, rebase forced."));
1750 printf(_("Current branch %s is up to date, rebase "
1751 "forced.\n"), branch_name
);
1754 /* If a hook exists, give it a chance to interrupt*/
1755 if (!ok_to_skip_pre_rebase
&&
1756 run_hooks_l("pre-rebase", options
.upstream_arg
,
1757 argc
? argv
[0] : NULL
, NULL
))
1758 die(_("The pre-rebase hook refused to rebase."));
1760 if (options
.flags
& REBASE_DIFFSTAT
) {
1761 struct diff_options opts
;
1763 if (options
.flags
& REBASE_VERBOSE
) {
1764 if (is_null_oid(&branch_base
))
1765 printf(_("Changes to %s:\n"),
1766 oid_to_hex(&options
.onto
->object
.oid
));
1768 printf(_("Changes from %s to %s:\n"),
1769 oid_to_hex(&branch_base
),
1770 oid_to_hex(&options
.onto
->object
.oid
));
1773 /* We want color (if set), but no pager */
1774 repo_diff_setup(the_repository
, &opts
);
1775 init_diffstat_widths(&opts
);
1776 opts
.output_format
|=
1777 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1778 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1779 diff_setup_done(&opts
);
1780 diff_tree_oid(is_null_oid(&branch_base
) ?
1781 the_hash_algo
->empty_tree
: &branch_base
,
1782 &options
.onto
->object
.oid
, "", &opts
);
1783 diffcore_std(&opts
);
1787 if (is_merge(&options
))
1790 /* Detach HEAD and reset the tree */
1791 if (options
.flags
& REBASE_NO_QUIET
)
1792 printf(_("First, rewinding head to replay your work on top of "
1795 strbuf_addf(&msg
, "%s (start): checkout %s",
1796 options
.reflog_action
, options
.onto_name
);
1797 ropts
.oid
= &options
.onto
->object
.oid
;
1798 ropts
.orig_head
= &options
.orig_head
->object
.oid
,
1799 ropts
.flags
= RESET_HEAD_DETACH
| RESET_ORIG_HEAD
|
1800 RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
1801 ropts
.head_msg
= msg
.buf
;
1802 ropts
.default_reflog_action
= options
.reflog_action
;
1803 if (reset_head(the_repository
, &ropts
))
1804 die(_("Could not detach HEAD"));
1805 strbuf_release(&msg
);
1808 * If the onto is a proper descendant of the tip of the branch, then
1809 * we just fast-forwarded.
1811 if (oideq(&branch_base
, &options
.orig_head
->object
.oid
)) {
1812 printf(_("Fast-forwarded %s to %s.\n"),
1813 branch_name
, options
.onto_name
);
1814 move_to_original_branch(&options
);
1815 ret
= finish_rebase(&options
);
1819 strbuf_addf(&revisions
, "%s..%s",
1820 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
1821 (options
.restrict_revision
?
1822 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
1823 oid_to_hex(&options
.upstream
->object
.oid
)),
1824 oid_to_hex(&options
.orig_head
->object
.oid
));
1826 options
.revisions
= revisions
.buf
;
1829 ret
= run_specific_rebase(&options
);
1832 strbuf_release(&buf
);
1833 strbuf_release(&revisions
);
1834 free(options
.reflog_action
);
1835 free(options
.head_name
);
1836 strvec_clear(&options
.git_am_opts
);
1837 free(options
.gpg_sign_opt
);
1838 string_list_clear(&options
.exec
, 0);
1839 free(options
.strategy
);
1840 string_list_clear(&options
.strategy_opts
, 0);
1841 strbuf_release(&options
.git_format_patch_opt
);
1842 free(squash_onto_name
);
1843 free(keep_base_onto_name
);