2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
9 #include "run-command.h"
17 #include "cache-tree.h"
18 #include "unpack-trees.h"
20 #include "parse-options.h"
23 #include "wt-status.h"
25 #include "commit-reach.h"
28 #include "sequencer.h"
29 #include "rebase-interactive.h"
33 #define DEFAULT_REFLOG_ACTION "rebase"
35 static char const * const builtin_rebase_usage
[] = {
36 N_("git rebase [-i] [options] [--exec <cmd>] "
37 "[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
38 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
40 "git rebase --continue | --abort | --skip | --edit-todo",
44 static GIT_PATH_FUNC(path_squash_onto
, "rebase-merge/squash-onto")
45 static GIT_PATH_FUNC(path_interactive
, "rebase-merge/interactive")
46 static GIT_PATH_FUNC(apply_dir
, "rebase-apply")
47 static GIT_PATH_FUNC(merge_dir
, "rebase-merge")
50 REBASE_UNSPECIFIED
= -1,
56 EMPTY_UNSPECIFIED
= -1,
69 ACTION_SHOW_CURRENT_PATCH
72 static const char *action_names
[] = {
82 struct rebase_options
{
83 enum rebase_type type
;
84 enum empty_type empty
;
85 const char *default_backend
;
86 const char *state_dir
;
87 struct commit
*upstream
;
88 const char *upstream_name
;
89 const char *upstream_arg
;
91 struct commit
*orig_head
;
93 const char *onto_name
;
94 const char *revisions
;
95 const char *switch_to
;
96 int root
, root_with_onto
;
97 struct object_id
*squash_onto
;
98 struct commit
*restrict_revision
;
99 int dont_finish_rebase
;
101 REBASE_NO_QUIET
= 1<<0,
102 REBASE_VERBOSE
= 1<<1,
103 REBASE_DIFFSTAT
= 1<<2,
105 REBASE_INTERACTIVE_EXPLICIT
= 1<<4,
107 struct strvec git_am_opts
;
110 int allow_rerere_autoupdate
;
115 int committer_date_is_author_date
;
118 int allow_empty_message
;
119 int rebase_merges
, rebase_cousins
;
120 char *strategy
, *strategy_opts
;
121 struct strbuf git_format_patch_opt
;
122 int reschedule_failed_exec
;
123 int reapply_cherry_picks
;
128 #define REBASE_OPTIONS_INIT { \
129 .type = REBASE_UNSPECIFIED, \
130 .empty = EMPTY_UNSPECIFIED, \
132 .default_backend = "merge", \
133 .flags = REBASE_NO_QUIET, \
134 .git_am_opts = STRVEC_INIT, \
135 .git_format_patch_opt = STRBUF_INIT, \
139 static struct replay_opts
get_replay_opts(const struct rebase_options
*opts
)
141 struct replay_opts replay
= REPLAY_OPTS_INIT
;
143 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
144 replay
.strategy
= NULL
;
145 sequencer_init_config(&replay
);
147 replay
.signoff
= opts
->signoff
;
148 replay
.allow_ff
= !(opts
->flags
& REBASE_FORCE
);
149 if (opts
->allow_rerere_autoupdate
)
150 replay
.allow_rerere_auto
= opts
->allow_rerere_autoupdate
;
151 replay
.allow_empty
= 1;
152 replay
.allow_empty_message
= opts
->allow_empty_message
;
153 replay
.drop_redundant_commits
= (opts
->empty
== EMPTY_DROP
);
154 replay
.keep_redundant_commits
= (opts
->empty
== EMPTY_KEEP
);
155 replay
.quiet
= !(opts
->flags
& REBASE_NO_QUIET
);
156 replay
.verbose
= opts
->flags
& REBASE_VERBOSE
;
157 replay
.reschedule_failed_exec
= opts
->reschedule_failed_exec
;
158 replay
.committer_date_is_author_date
=
159 opts
->committer_date_is_author_date
;
160 replay
.ignore_date
= opts
->ignore_date
;
161 replay
.gpg_sign
= xstrdup_or_null(opts
->gpg_sign_opt
);
163 replay
.strategy
= xstrdup_or_null(opts
->strategy
);
164 else if (!replay
.strategy
&& replay
.default_strategy
) {
165 replay
.strategy
= replay
.default_strategy
;
166 replay
.default_strategy
= NULL
;
169 if (opts
->strategy_opts
)
170 parse_strategy_opts(&replay
, opts
->strategy_opts
);
172 if (opts
->squash_onto
) {
173 oidcpy(&replay
.squash_onto
, opts
->squash_onto
);
174 replay
.have_squash_onto
= 1;
180 static int edit_todo_file(unsigned flags
)
182 const char *todo_file
= rebase_path_todo();
183 struct todo_list todo_list
= TODO_LIST_INIT
,
184 new_todo
= TODO_LIST_INIT
;
187 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
188 return error_errno(_("could not read '%s'."), todo_file
);
190 strbuf_stripspace(&todo_list
.buf
, 1);
191 res
= edit_todo_list(the_repository
, &todo_list
, &new_todo
, NULL
, NULL
, flags
);
192 if (!res
&& todo_list_write_to_file(the_repository
, &new_todo
, todo_file
,
193 NULL
, NULL
, -1, flags
& ~(TODO_LIST_SHORTEN_IDS
)))
194 res
= error_errno(_("could not write '%s'"), todo_file
);
196 todo_list_release(&todo_list
);
197 todo_list_release(&new_todo
);
202 static int get_revision_ranges(struct commit
*upstream
, struct commit
*onto
,
203 struct object_id
*orig_head
, char **revisions
,
204 char **shortrevisions
)
206 struct commit
*base_rev
= upstream
? upstream
: onto
;
207 const char *shorthead
;
209 *revisions
= xstrfmt("%s...%s", oid_to_hex(&base_rev
->object
.oid
),
210 oid_to_hex(orig_head
));
212 shorthead
= find_unique_abbrev(orig_head
, DEFAULT_ABBREV
);
215 const char *shortrev
;
217 shortrev
= find_unique_abbrev(&base_rev
->object
.oid
,
220 *shortrevisions
= xstrfmt("%s..%s", shortrev
, shorthead
);
222 *shortrevisions
= xstrdup(shorthead
);
227 static int init_basic_state(struct replay_opts
*opts
, const char *head_name
,
229 const struct object_id
*orig_head
)
233 if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir()))
234 return error_errno(_("could not create temporary %s"), merge_dir());
236 delete_reflog("REBASE_HEAD");
238 interactive
= fopen(path_interactive(), "w");
240 return error_errno(_("could not mark as interactive"));
243 return write_basic_state(opts
, head_name
, onto
, orig_head
);
246 static void split_exec_commands(const char *cmd
, struct string_list
*commands
)
249 string_list_split(commands
, cmd
, '\n', -1);
251 /* rebase.c adds a new line to cmd after every command,
252 * so here the last command is always empty */
253 string_list_remove_empty_items(commands
, 0);
257 static int do_interactive_rebase(struct rebase_options
*opts
, unsigned flags
)
260 char *revisions
= NULL
, *shortrevisions
= NULL
;
261 struct strvec make_script_args
= STRVEC_INIT
;
262 struct todo_list todo_list
= TODO_LIST_INIT
;
263 struct replay_opts replay
= get_replay_opts(opts
);
264 struct string_list commands
= STRING_LIST_INIT_DUP
;
266 if (get_revision_ranges(opts
->upstream
, opts
->onto
, &opts
->orig_head
->object
.oid
,
267 &revisions
, &shortrevisions
))
270 if (init_basic_state(&replay
,
271 opts
->head_name
? opts
->head_name
: "detached HEAD",
272 opts
->onto
, &opts
->orig_head
->object
.oid
)) {
274 free(shortrevisions
);
279 if (!opts
->upstream
&& opts
->squash_onto
)
280 write_file(path_squash_onto(), "%s\n",
281 oid_to_hex(opts
->squash_onto
));
283 strvec_pushl(&make_script_args
, "", revisions
, NULL
);
284 if (opts
->restrict_revision
)
285 strvec_pushf(&make_script_args
, "^%s",
286 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
288 ret
= sequencer_make_script(the_repository
, &todo_list
.buf
,
289 make_script_args
.nr
, make_script_args
.v
,
293 error(_("could not generate todo list"));
296 if (todo_list_parse_insn_buffer(the_repository
, todo_list
.buf
.buf
,
298 BUG("unusable todo list");
300 split_exec_commands(opts
->cmd
, &commands
);
301 ret
= complete_action(the_repository
, &replay
, flags
,
302 shortrevisions
, opts
->onto_name
, opts
->onto
,
303 &opts
->orig_head
->object
.oid
, &commands
,
304 opts
->autosquash
, opts
->update_refs
, &todo_list
);
307 string_list_clear(&commands
, 0);
309 free(shortrevisions
);
310 todo_list_release(&todo_list
);
311 strvec_clear(&make_script_args
);
316 static int run_sequencer_rebase(struct rebase_options
*opts
)
319 int abbreviate_commands
= 0, ret
= 0;
321 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands
);
323 flags
|= opts
->keep_empty
? TODO_LIST_KEEP_EMPTY
: 0;
324 flags
|= abbreviate_commands
? TODO_LIST_ABBREVIATE_CMDS
: 0;
325 flags
|= opts
->rebase_merges
? TODO_LIST_REBASE_MERGES
: 0;
326 flags
|= opts
->rebase_cousins
> 0 ? TODO_LIST_REBASE_COUSINS
: 0;
327 flags
|= opts
->root_with_onto
? TODO_LIST_ROOT_WITH_ONTO
: 0;
328 flags
|= opts
->reapply_cherry_picks
? TODO_LIST_REAPPLY_CHERRY_PICKS
: 0;
329 flags
|= opts
->flags
& REBASE_NO_QUIET
? TODO_LIST_WARN_SKIPPED_CHERRY_PICKS
: 0;
331 switch (opts
->action
) {
333 if (!opts
->onto
&& !opts
->upstream
)
334 die(_("a base commit must be provided with --upstream or --onto"));
336 ret
= do_interactive_rebase(opts
, flags
);
340 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
342 rerere_clear(the_repository
, &merge_rr
);
345 case ACTION_CONTINUE
: {
346 struct replay_opts replay_opts
= get_replay_opts(opts
);
348 ret
= sequencer_continue(the_repository
, &replay_opts
);
351 case ACTION_EDIT_TODO
:
352 ret
= edit_todo_file(flags
);
354 case ACTION_SHOW_CURRENT_PATCH
: {
355 struct child_process cmd
= CHILD_PROCESS_INIT
;
358 strvec_pushl(&cmd
.args
, "show", "REBASE_HEAD", "--", NULL
);
359 ret
= run_command(&cmd
);
364 BUG("invalid command '%d'", opts
->action
);
370 static void imply_merge(struct rebase_options
*opts
, const char *option
);
371 static int parse_opt_keep_empty(const struct option
*opt
, const char *arg
,
374 struct rebase_options
*opts
= opt
->value
;
378 imply_merge(opts
, unset
? "--no-keep-empty" : "--keep-empty");
379 opts
->keep_empty
= !unset
;
380 opts
->type
= REBASE_MERGE
;
384 static int is_merge(struct rebase_options
*opts
)
386 return opts
->type
== REBASE_MERGE
;
389 static void imply_merge(struct rebase_options
*opts
, const char *option
)
391 switch (opts
->type
) {
393 die(_("%s requires the merge backend"), option
);
398 opts
->type
= REBASE_MERGE
; /* implied */
403 /* Returns the filename prefixed by the state_dir */
404 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
406 static struct strbuf path
= STRBUF_INIT
;
407 static size_t prefix_len
;
410 strbuf_addf(&path
, "%s/", opts
->state_dir
);
411 prefix_len
= path
.len
;
414 strbuf_setlen(&path
, prefix_len
);
415 strbuf_addstr(&path
, filename
);
419 /* Initialize the rebase options from the state directory. */
420 static int read_basic_state(struct rebase_options
*opts
)
422 struct strbuf head_name
= STRBUF_INIT
;
423 struct strbuf buf
= STRBUF_INIT
;
424 struct object_id oid
;
426 if (!read_oneliner(&head_name
, state_dir_path("head-name", opts
),
427 READ_ONELINER_WARN_MISSING
) ||
428 !read_oneliner(&buf
, state_dir_path("onto", opts
),
429 READ_ONELINER_WARN_MISSING
))
431 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
432 xstrdup(head_name
.buf
) : NULL
;
433 strbuf_release(&head_name
);
434 if (get_oid_hex(buf
.buf
, &oid
) ||
435 !(opts
->onto
= lookup_commit_object(the_repository
, &oid
)))
436 return error(_("invalid onto: '%s'"), buf
.buf
);
439 * We always write to orig-head, but interactive rebase used to write to
440 * head. Fall back to reading from head to cover for the case that the
441 * user upgraded git with an ongoing interactive rebase.
444 if (file_exists(state_dir_path("orig-head", opts
))) {
445 if (!read_oneliner(&buf
, state_dir_path("orig-head", opts
),
446 READ_ONELINER_WARN_MISSING
))
448 } else if (!read_oneliner(&buf
, state_dir_path("head", opts
),
449 READ_ONELINER_WARN_MISSING
))
451 if (get_oid_hex(buf
.buf
, &oid
) ||
452 !(opts
->orig_head
= lookup_commit_object(the_repository
, &oid
)))
453 return error(_("invalid orig-head: '%s'"), buf
.buf
);
455 if (file_exists(state_dir_path("quiet", opts
)))
456 opts
->flags
&= ~REBASE_NO_QUIET
;
458 opts
->flags
|= REBASE_NO_QUIET
;
460 if (file_exists(state_dir_path("verbose", opts
)))
461 opts
->flags
|= REBASE_VERBOSE
;
463 if (file_exists(state_dir_path("signoff", opts
))) {
465 opts
->flags
|= REBASE_FORCE
;
468 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts
))) {
470 if (!read_oneliner(&buf
, state_dir_path("allow_rerere_autoupdate", opts
),
471 READ_ONELINER_WARN_MISSING
))
473 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
474 opts
->allow_rerere_autoupdate
= RERERE_AUTOUPDATE
;
475 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
476 opts
->allow_rerere_autoupdate
= RERERE_NOAUTOUPDATE
;
478 warning(_("ignoring invalid allow_rerere_autoupdate: "
482 if (file_exists(state_dir_path("gpg_sign_opt", opts
))) {
484 if (!read_oneliner(&buf
, state_dir_path("gpg_sign_opt", opts
),
485 READ_ONELINER_WARN_MISSING
))
487 free(opts
->gpg_sign_opt
);
488 opts
->gpg_sign_opt
= xstrdup(buf
.buf
);
491 if (file_exists(state_dir_path("strategy", opts
))) {
493 if (!read_oneliner(&buf
, state_dir_path("strategy", opts
),
494 READ_ONELINER_WARN_MISSING
))
496 free(opts
->strategy
);
497 opts
->strategy
= xstrdup(buf
.buf
);
500 if (file_exists(state_dir_path("strategy_opts", opts
))) {
502 if (!read_oneliner(&buf
, state_dir_path("strategy_opts", opts
),
503 READ_ONELINER_WARN_MISSING
))
505 free(opts
->strategy_opts
);
506 opts
->strategy_opts
= xstrdup(buf
.buf
);
509 strbuf_release(&buf
);
514 static int rebase_write_basic_state(struct rebase_options
*opts
)
516 write_file(state_dir_path("head-name", opts
), "%s",
517 opts
->head_name
? opts
->head_name
: "detached HEAD");
518 write_file(state_dir_path("onto", opts
), "%s",
519 opts
->onto
? oid_to_hex(&opts
->onto
->object
.oid
) : "");
520 write_file(state_dir_path("orig-head", opts
), "%s",
521 oid_to_hex(&opts
->orig_head
->object
.oid
));
522 if (!(opts
->flags
& REBASE_NO_QUIET
))
523 write_file(state_dir_path("quiet", opts
), "%s", "");
524 if (opts
->flags
& REBASE_VERBOSE
)
525 write_file(state_dir_path("verbose", opts
), "%s", "");
527 write_file(state_dir_path("strategy", opts
), "%s",
529 if (opts
->strategy_opts
)
530 write_file(state_dir_path("strategy_opts", opts
), "%s",
531 opts
->strategy_opts
);
532 if (opts
->allow_rerere_autoupdate
> 0)
533 write_file(state_dir_path("allow_rerere_autoupdate", opts
),
534 "-%s-rerere-autoupdate",
535 opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
?
537 if (opts
->gpg_sign_opt
)
538 write_file(state_dir_path("gpg_sign_opt", opts
), "%s",
541 write_file(state_dir_path("signoff", opts
), "--signoff");
546 static int finish_rebase(struct rebase_options
*opts
)
548 struct strbuf dir
= STRBUF_INIT
;
551 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
552 unlink(git_path_auto_merge(the_repository
));
553 apply_autostash(state_dir_path("autostash", opts
));
555 * We ignore errors in 'git maintenance run --auto', since the
556 * user should see them.
558 run_auto_maintenance(!(opts
->flags
& (REBASE_NO_QUIET
|REBASE_VERBOSE
)));
559 if (opts
->type
== REBASE_MERGE
) {
560 struct replay_opts replay
= REPLAY_OPTS_INIT
;
562 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
563 ret
= sequencer_remove_state(&replay
);
565 strbuf_addstr(&dir
, opts
->state_dir
);
566 if (remove_dir_recursively(&dir
, 0))
567 ret
= error(_("could not remove '%s'"),
569 strbuf_release(&dir
);
575 static int move_to_original_branch(struct rebase_options
*opts
)
577 struct strbuf branch_reflog
= STRBUF_INIT
, head_reflog
= STRBUF_INIT
;
578 struct reset_head_opts ropts
= { 0 };
581 if (!opts
->head_name
)
582 return 0; /* nothing to move back to */
585 BUG("move_to_original_branch without onto");
587 strbuf_addf(&branch_reflog
, "%s (finish): %s onto %s",
588 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
),
589 opts
->head_name
, oid_to_hex(&opts
->onto
->object
.oid
));
590 strbuf_addf(&head_reflog
, "%s (finish): returning to %s",
591 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
), opts
->head_name
);
592 ropts
.branch
= opts
->head_name
;
593 ropts
.flags
= RESET_HEAD_REFS_ONLY
;
594 ropts
.branch_msg
= branch_reflog
.buf
;
595 ropts
.head_msg
= head_reflog
.buf
;
596 ret
= reset_head(the_repository
, &ropts
);
598 strbuf_release(&branch_reflog
);
599 strbuf_release(&head_reflog
);
603 static const char *resolvemsg
=
604 N_("Resolve all conflicts manually, mark them as resolved with\n"
605 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
606 "You can instead skip this commit: run \"git rebase --skip\".\n"
607 "To abort and get back to the state before \"git rebase\", run "
608 "\"git rebase --abort\".");
610 static int run_am(struct rebase_options
*opts
)
612 struct child_process am
= CHILD_PROCESS_INIT
;
613 struct child_process format_patch
= CHILD_PROCESS_INIT
;
614 struct strbuf revisions
= STRBUF_INIT
;
616 char *rebased_patches
;
619 strvec_push(&am
.args
, "am");
620 strvec_pushf(&am
.env
, GIT_REFLOG_ACTION_ENVIRONMENT
"=%s (pick)",
621 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
));
622 if (opts
->action
== ACTION_CONTINUE
) {
623 strvec_push(&am
.args
, "--resolved");
624 strvec_pushf(&am
.args
, "--resolvemsg=%s", resolvemsg
);
625 if (opts
->gpg_sign_opt
)
626 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
627 status
= run_command(&am
);
631 return move_to_original_branch(opts
);
633 if (opts
->action
== ACTION_SKIP
) {
634 strvec_push(&am
.args
, "--skip");
635 strvec_pushf(&am
.args
, "--resolvemsg=%s", resolvemsg
);
636 status
= run_command(&am
);
640 return move_to_original_branch(opts
);
642 if (opts
->action
== ACTION_SHOW_CURRENT_PATCH
) {
643 strvec_push(&am
.args
, "--show-current-patch");
644 return run_command(&am
);
647 strbuf_addf(&revisions
, "%s...%s",
648 oid_to_hex(opts
->root
?
649 /* this is now equivalent to !opts->upstream */
650 &opts
->onto
->object
.oid
:
651 &opts
->upstream
->object
.oid
),
652 oid_to_hex(&opts
->orig_head
->object
.oid
));
654 rebased_patches
= xstrdup(git_path("rebased-patches"));
655 format_patch
.out
= open(rebased_patches
,
656 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
657 if (format_patch
.out
< 0) {
658 status
= error_errno(_("could not open '%s' for writing"),
660 free(rebased_patches
);
661 strvec_clear(&am
.args
);
665 format_patch
.git_cmd
= 1;
666 strvec_pushl(&format_patch
.args
, "format-patch", "-k", "--stdout",
667 "--full-index", "--cherry-pick", "--right-only",
668 "--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
669 "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
671 if (opts
->git_format_patch_opt
.len
)
672 strvec_split(&format_patch
.args
,
673 opts
->git_format_patch_opt
.buf
);
674 strvec_push(&format_patch
.args
, revisions
.buf
);
675 if (opts
->restrict_revision
)
676 strvec_pushf(&format_patch
.args
, "^%s",
677 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
679 status
= run_command(&format_patch
);
681 struct reset_head_opts ropts
= { 0 };
682 unlink(rebased_patches
);
683 free(rebased_patches
);
684 strvec_clear(&am
.args
);
686 ropts
.oid
= &opts
->orig_head
->object
.oid
;
687 ropts
.branch
= opts
->head_name
;
688 ropts
.default_reflog_action
= DEFAULT_REFLOG_ACTION
;
689 reset_head(the_repository
, &ropts
);
690 error(_("\ngit encountered an error while preparing the "
691 "patches to replay\n"
694 "As a result, git cannot rebase them."),
697 strbuf_release(&revisions
);
700 strbuf_release(&revisions
);
702 am
.in
= open(rebased_patches
, O_RDONLY
);
704 status
= error_errno(_("could not open '%s' for reading"),
706 free(rebased_patches
);
707 strvec_clear(&am
.args
);
711 strvec_pushv(&am
.args
, opts
->git_am_opts
.v
);
712 strvec_push(&am
.args
, "--rebasing");
713 strvec_pushf(&am
.args
, "--resolvemsg=%s", resolvemsg
);
714 strvec_push(&am
.args
, "--patch-format=mboxrd");
715 if (opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
)
716 strvec_push(&am
.args
, "--rerere-autoupdate");
717 else if (opts
->allow_rerere_autoupdate
== RERERE_NOAUTOUPDATE
)
718 strvec_push(&am
.args
, "--no-rerere-autoupdate");
719 if (opts
->gpg_sign_opt
)
720 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
721 status
= run_command(&am
);
722 unlink(rebased_patches
);
723 free(rebased_patches
);
726 return move_to_original_branch(opts
);
729 if (is_directory(opts
->state_dir
))
730 rebase_write_basic_state(opts
);
735 static int run_specific_rebase(struct rebase_options
*opts
)
739 if (opts
->type
== REBASE_MERGE
) {
740 /* Run sequencer-based rebase */
741 setenv("GIT_CHERRY_PICK_HELP", resolvemsg
, 1);
742 if (!(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
)) {
743 setenv("GIT_SEQUENCE_EDITOR", ":", 1);
744 opts
->autosquash
= 0;
746 if (opts
->gpg_sign_opt
) {
747 /* remove the leading "-S" */
748 char *tmp
= xstrdup(opts
->gpg_sign_opt
+ 2);
749 free(opts
->gpg_sign_opt
);
750 opts
->gpg_sign_opt
= tmp
;
753 status
= run_sequencer_rebase(opts
);
754 } else if (opts
->type
== REBASE_APPLY
)
755 status
= run_am(opts
);
757 BUG("Unhandled rebase type %d", opts
->type
);
759 if (opts
->dont_finish_rebase
)
761 else if (opts
->type
== REBASE_MERGE
)
762 ; /* merge backend cleans up after itself */
763 else if (status
== 0) {
764 if (!file_exists(state_dir_path("stopped-sha", opts
)))
766 } else if (status
== 2) {
767 struct strbuf dir
= STRBUF_INIT
;
769 apply_autostash(state_dir_path("autostash", opts
));
770 strbuf_addstr(&dir
, opts
->state_dir
);
771 remove_dir_recursively(&dir
, 0);
772 strbuf_release(&dir
);
773 die("Nothing to do");
776 return status
? -1 : 0;
779 static int rebase_config(const char *var
, const char *value
, void *data
)
781 struct rebase_options
*opts
= data
;
783 if (!strcmp(var
, "rebase.stat")) {
784 if (git_config_bool(var
, value
))
785 opts
->flags
|= REBASE_DIFFSTAT
;
787 opts
->flags
&= ~REBASE_DIFFSTAT
;
791 if (!strcmp(var
, "rebase.autosquash")) {
792 opts
->autosquash
= git_config_bool(var
, value
);
796 if (!strcmp(var
, "commit.gpgsign")) {
797 free(opts
->gpg_sign_opt
);
798 opts
->gpg_sign_opt
= git_config_bool(var
, value
) ?
799 xstrdup("-S") : NULL
;
803 if (!strcmp(var
, "rebase.autostash")) {
804 opts
->autostash
= git_config_bool(var
, value
);
808 if (!strcmp(var
, "rebase.updaterefs")) {
809 opts
->update_refs
= git_config_bool(var
, value
);
813 if (!strcmp(var
, "rebase.reschedulefailedexec")) {
814 opts
->reschedule_failed_exec
= git_config_bool(var
, value
);
818 if (!strcmp(var
, "rebase.forkpoint")) {
819 opts
->fork_point
= git_config_bool(var
, value
) ? -1 : 0;
823 if (!strcmp(var
, "rebase.backend")) {
824 return git_config_string(&opts
->default_backend
, var
, value
);
827 return git_default_config(var
, value
, data
);
830 static int checkout_up_to_date(struct rebase_options
*options
)
832 struct strbuf buf
= STRBUF_INIT
;
833 struct reset_head_opts ropts
= { 0 };
836 strbuf_addf(&buf
, "%s: checkout %s",
837 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
),
839 ropts
.oid
= &options
->orig_head
->object
.oid
;
840 ropts
.branch
= options
->head_name
;
841 ropts
.flags
= RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
843 ropts
.flags
|= RESET_HEAD_DETACH
;
844 ropts
.head_msg
= buf
.buf
;
845 if (reset_head(the_repository
, &ropts
) < 0)
846 ret
= error(_("could not switch to %s"), options
->switch_to
);
847 strbuf_release(&buf
);
853 * Determines whether the commits in from..to are linear, i.e. contain
854 * no merge commits. This function *expects* `from` to be an ancestor of
857 static int is_linear_history(struct commit
*from
, struct commit
*to
)
859 while (to
&& to
!= from
) {
863 if (to
->parents
->next
)
865 to
= to
->parents
->item
;
870 static int can_fast_forward(struct commit
*onto
, struct commit
*upstream
,
871 struct commit
*restrict_revision
,
872 struct commit
*head
, struct object_id
*branch_base
)
874 struct commit_list
*merge_bases
= NULL
;
877 if (is_null_oid(branch_base
))
878 goto done
; /* fill_branch_base() found multiple merge bases */
880 if (!oideq(branch_base
, &onto
->object
.oid
))
883 if (restrict_revision
&& !oideq(&restrict_revision
->object
.oid
, branch_base
))
889 merge_bases
= get_merge_bases(upstream
, head
);
890 if (!merge_bases
|| merge_bases
->next
)
893 if (!oideq(&onto
->object
.oid
, &merge_bases
->item
->object
.oid
))
899 free_commit_list(merge_bases
);
900 return res
&& is_linear_history(onto
, head
);
903 static void fill_branch_base(struct rebase_options
*options
,
904 struct object_id
*branch_base
)
906 struct commit_list
*merge_bases
= NULL
;
908 merge_bases
= get_merge_bases(options
->onto
, options
->orig_head
);
909 if (!merge_bases
|| merge_bases
->next
)
910 oidcpy(branch_base
, null_oid());
912 oidcpy(branch_base
, &merge_bases
->item
->object
.oid
);
914 free_commit_list(merge_bases
);
917 static int parse_opt_am(const struct option
*opt
, const char *arg
, int unset
)
919 struct rebase_options
*opts
= opt
->value
;
921 BUG_ON_OPT_NEG(unset
);
924 opts
->type
= REBASE_APPLY
;
929 /* -i followed by -m is still -i */
930 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
932 struct rebase_options
*opts
= opt
->value
;
934 BUG_ON_OPT_NEG(unset
);
938 opts
->type
= REBASE_MERGE
;
943 /* -i followed by -r is still explicitly interactive, but -r alone is not */
944 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
947 struct rebase_options
*opts
= opt
->value
;
949 BUG_ON_OPT_NEG(unset
);
952 opts
->type
= REBASE_MERGE
;
953 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
958 static enum empty_type
parse_empty_value(const char *value
)
960 if (!strcasecmp(value
, "drop"))
962 else if (!strcasecmp(value
, "keep"))
964 else if (!strcasecmp(value
, "ask"))
967 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"ask\"."), value
);
970 static int parse_opt_empty(const struct option
*opt
, const char *arg
, int unset
)
972 struct rebase_options
*options
= opt
->value
;
973 enum empty_type value
= parse_empty_value(arg
);
975 BUG_ON_OPT_NEG(unset
);
977 options
->empty
= value
;
981 static void NORETURN
error_on_missing_default_upstream(void)
983 struct branch
*current_branch
= branch_get(NULL
);
986 "Please specify which branch you want to rebase against.\n"
987 "See git-rebase(1) for details.\n"
989 " git rebase '<branch>'\n"
991 current_branch
? _("There is no tracking information for "
992 "the current branch.") :
993 _("You are not currently on a branch."));
995 if (current_branch
) {
996 const char *remote
= current_branch
->remote_name
;
999 remote
= _("<remote>");
1001 printf(_("If you wish to set tracking information for this "
1002 "branch you can do so with:\n"
1004 " git branch --set-upstream-to=%s/<branch> %s\n"
1006 remote
, current_branch
->name
);
1011 static int check_exec_cmd(const char *cmd
)
1013 if (strchr(cmd
, '\n'))
1014 return error(_("exec commands cannot contain newlines"));
1016 /* Does the command consist purely of whitespace? */
1017 if (!cmd
[strspn(cmd
, " \t\r\f\v")])
1018 return error(_("empty exec command"));
1023 int cmd_rebase(int argc
, const char **argv
, const char *prefix
)
1025 struct rebase_options options
= REBASE_OPTIONS_INIT
;
1026 const char *branch_name
;
1027 int ret
, flags
, total_argc
, in_progress
= 0;
1029 int ok_to_skip_pre_rebase
= 0;
1030 struct strbuf msg
= STRBUF_INIT
;
1031 struct strbuf revisions
= STRBUF_INIT
;
1032 struct strbuf buf
= STRBUF_INIT
;
1033 struct object_id branch_base
;
1034 int ignore_whitespace
= 0;
1035 const char *gpg_sign
= NULL
;
1036 struct string_list exec
= STRING_LIST_INIT_NODUP
;
1037 const char *rebase_merges
= NULL
;
1038 struct string_list strategy_options
= STRING_LIST_INIT_NODUP
;
1039 struct object_id squash_onto
;
1040 char *squash_onto_name
= NULL
;
1041 int reschedule_failed_exec
= -1;
1042 int allow_preemptive_ff
= 1;
1043 int preserve_merges_selected
= 0;
1044 struct reset_head_opts ropts
= { 0 };
1045 struct option builtin_rebase_options
[] = {
1046 OPT_STRING(0, "onto", &options
.onto_name
,
1048 N_("rebase onto given branch instead of upstream")),
1049 OPT_BOOL(0, "keep-base", &keep_base
,
1050 N_("use the merge-base of upstream and branch as the current base")),
1051 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
1052 N_("allow pre-rebase hook to run")),
1053 OPT_NEGBIT('q', "quiet", &options
.flags
,
1054 N_("be quiet. implies --no-stat"),
1055 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1056 OPT_BIT('v', "verbose", &options
.flags
,
1057 N_("display a diffstat of what changed upstream"),
1058 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1059 {OPTION_NEGBIT
, 'n', "no-stat", &options
.flags
, NULL
,
1060 N_("do not show diffstat of what changed upstream"),
1061 PARSE_OPT_NOARG
, NULL
, REBASE_DIFFSTAT
},
1062 OPT_BOOL(0, "signoff", &options
.signoff
,
1063 N_("add a Signed-off-by trailer to each commit")),
1064 OPT_BOOL(0, "committer-date-is-author-date",
1065 &options
.committer_date_is_author_date
,
1066 N_("make committer date match author date")),
1067 OPT_BOOL(0, "reset-author-date", &options
.ignore_date
,
1068 N_("ignore author date and use current date")),
1069 OPT_HIDDEN_BOOL(0, "ignore-date", &options
.ignore_date
,
1070 N_("synonym of --reset-author-date")),
1071 OPT_PASSTHRU_ARGV('C', NULL
, &options
.git_am_opts
, N_("n"),
1072 N_("passed to 'git apply'"), 0),
1073 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace
,
1074 N_("ignore changes in whitespace")),
1075 OPT_PASSTHRU_ARGV(0, "whitespace", &options
.git_am_opts
,
1076 N_("action"), N_("passed to 'git apply'"), 0),
1077 OPT_BIT('f', "force-rebase", &options
.flags
,
1078 N_("cherry-pick all commits, even if unchanged"),
1080 OPT_BIT(0, "no-ff", &options
.flags
,
1081 N_("cherry-pick all commits, even if unchanged"),
1083 OPT_CMDMODE(0, "continue", &options
.action
, N_("continue"),
1085 OPT_CMDMODE(0, "skip", &options
.action
,
1086 N_("skip current patch and continue"), ACTION_SKIP
),
1087 OPT_CMDMODE(0, "abort", &options
.action
,
1088 N_("abort and check out the original branch"),
1090 OPT_CMDMODE(0, "quit", &options
.action
,
1091 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
1092 OPT_CMDMODE(0, "edit-todo", &options
.action
, N_("edit the todo list "
1093 "during an interactive rebase"), ACTION_EDIT_TODO
),
1094 OPT_CMDMODE(0, "show-current-patch", &options
.action
,
1095 N_("show the patch file being applied or merged"),
1096 ACTION_SHOW_CURRENT_PATCH
),
1097 OPT_CALLBACK_F(0, "apply", &options
, NULL
,
1098 N_("use apply strategies to rebase"),
1099 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1101 OPT_CALLBACK_F('m', "merge", &options
, NULL
,
1102 N_("use merging strategies to rebase"),
1103 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1105 OPT_CALLBACK_F('i', "interactive", &options
, NULL
,
1106 N_("let the user edit the list of commits to rebase"),
1107 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1108 parse_opt_interactive
),
1109 OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected
,
1110 N_("(REMOVED) was: try to recreate merges "
1111 "instead of ignoring them"),
1112 1, PARSE_OPT_HIDDEN
),
1113 OPT_RERERE_AUTOUPDATE(&options
.allow_rerere_autoupdate
),
1114 OPT_CALLBACK_F(0, "empty", &options
, "{drop,keep,ask}",
1115 N_("how to handle commits that become empty"),
1116 PARSE_OPT_NONEG
, parse_opt_empty
),
1117 OPT_CALLBACK_F('k', "keep-empty", &options
, NULL
,
1118 N_("keep commits which start empty"),
1119 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
,
1120 parse_opt_keep_empty
),
1121 OPT_BOOL(0, "autosquash", &options
.autosquash
,
1122 N_("move commits that begin with "
1123 "squash!/fixup! under -i")),
1124 OPT_BOOL(0, "update-refs", &options
.update_refs
,
1125 N_("update branches that point to commits "
1126 "that are being rebased")),
1127 { OPTION_STRING
, 'S', "gpg-sign", &gpg_sign
, N_("key-id"),
1128 N_("GPG-sign commits"),
1129 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1130 OPT_AUTOSTASH(&options
.autostash
),
1131 OPT_STRING_LIST('x', "exec", &exec
, N_("exec"),
1132 N_("add exec lines after each commit of the "
1134 OPT_BOOL_F(0, "allow-empty-message",
1135 &options
.allow_empty_message
,
1136 N_("allow rebasing commits with empty messages"),
1138 {OPTION_STRING
, 'r', "rebase-merges", &rebase_merges
,
1140 N_("try to rebase merges instead of skipping them"),
1141 PARSE_OPT_OPTARG
, NULL
, (intptr_t)""},
1142 OPT_BOOL(0, "fork-point", &options
.fork_point
,
1143 N_("use 'merge-base --fork-point' to refine upstream")),
1144 OPT_STRING('s', "strategy", &options
.strategy
,
1145 N_("strategy"), N_("use the given merge strategy")),
1146 OPT_STRING_LIST('X', "strategy-option", &strategy_options
,
1148 N_("pass the argument through to the merge "
1150 OPT_BOOL(0, "root", &options
.root
,
1151 N_("rebase all reachable commits up to the root(s)")),
1152 OPT_BOOL(0, "reschedule-failed-exec",
1153 &reschedule_failed_exec
,
1154 N_("automatically re-schedule any `exec` that fails")),
1155 OPT_BOOL(0, "reapply-cherry-picks", &options
.reapply_cherry_picks
,
1156 N_("apply all changes, even those already present upstream")),
1161 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1162 usage_with_options(builtin_rebase_usage
,
1163 builtin_rebase_options
);
1165 prepare_repo_settings(the_repository
);
1166 the_repository
->settings
.command_requires_full_index
= 0;
1168 options
.reapply_cherry_picks
= -1;
1169 options
.allow_empty_message
= 1;
1170 git_config(rebase_config
, &options
);
1171 /* options.gpg_sign_opt will be either "-S" or NULL */
1172 gpg_sign
= options
.gpg_sign_opt
? "" : NULL
;
1173 FREE_AND_NULL(options
.gpg_sign_opt
);
1176 strbuf_addf(&buf
, "%s/applying", apply_dir());
1177 if(file_exists(buf
.buf
))
1178 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1180 if (is_directory(apply_dir())) {
1181 options
.type
= REBASE_APPLY
;
1182 options
.state_dir
= apply_dir();
1183 } else if (is_directory(merge_dir())) {
1185 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
1186 if (!(options
.action
== ACTION_ABORT
) && is_directory(buf
.buf
)) {
1187 die(_("`rebase --preserve-merges` (-p) is no longer supported.\n"
1188 "Use `git rebase --abort` to terminate current rebase.\n"
1189 "Or downgrade to v2.33, or earlier, to complete the rebase."));
1192 strbuf_addf(&buf
, "%s/interactive", merge_dir());
1193 options
.type
= REBASE_MERGE
;
1194 if (file_exists(buf
.buf
))
1195 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
1197 options
.state_dir
= merge_dir();
1200 if (options
.type
!= REBASE_UNSPECIFIED
)
1204 argc
= parse_options(argc
, argv
, prefix
,
1205 builtin_rebase_options
,
1206 builtin_rebase_usage
, 0);
1208 if (preserve_merges_selected
)
1209 die(_("--preserve-merges was replaced by --rebase-merges\n"
1210 "Note: Your `pull.rebase` configuration may also be set to 'preserve',\n"
1211 "which is no longer supported; use 'merges' instead"));
1213 if (options
.action
!= ACTION_NONE
&& total_argc
!= 2) {
1214 usage_with_options(builtin_rebase_usage
,
1215 builtin_rebase_options
);
1219 usage_with_options(builtin_rebase_usage
,
1220 builtin_rebase_options
);
1223 if (options
.onto_name
)
1224 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--onto");
1226 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--root");
1228 * --keep-base defaults to --no-fork-point to keep the
1231 if (options
.fork_point
< 0)
1232 options
.fork_point
= 0;
1235 * --keep-base defaults to --reapply-cherry-picks to avoid losing
1236 * commits when using this option.
1238 if (options
.reapply_cherry_picks
< 0)
1239 options
.reapply_cherry_picks
= keep_base
;
1241 if (options
.root
&& options
.fork_point
> 0)
1242 die(_("options '%s' and '%s' cannot be used together"), "--root", "--fork-point");
1244 if (options
.action
!= ACTION_NONE
&& !in_progress
)
1245 die(_("No rebase in progress?"));
1246 setenv(GIT_REFLOG_ACTION_ENVIRONMENT
, "rebase", 0);
1248 if (options
.action
== ACTION_EDIT_TODO
&& !is_merge(&options
))
1249 die(_("The --edit-todo action can only be used during "
1250 "interactive rebase."));
1252 if (trace2_is_enabled()) {
1253 if (is_merge(&options
))
1254 trace2_cmd_mode("interactive");
1256 trace2_cmd_mode("interactive-exec");
1258 trace2_cmd_mode(action_names
[options
.action
]);
1261 switch (options
.action
) {
1262 case ACTION_CONTINUE
: {
1263 struct object_id head
;
1264 struct lock_file lock_file
= LOCK_INIT
;
1268 if (get_oid("HEAD", &head
))
1269 die(_("Cannot read HEAD"));
1271 fd
= hold_locked_index(&lock_file
, 0);
1272 if (repo_read_index(the_repository
) < 0)
1273 die(_("could not read index"));
1274 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
1277 repo_update_index_if_able(the_repository
, &lock_file
);
1278 rollback_lock_file(&lock_file
);
1280 if (has_unstaged_changes(the_repository
, 1)) {
1281 puts(_("You must edit all merge conflicts and then\n"
1282 "mark them as resolved using git add"));
1285 if (read_basic_state(&options
))
1290 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1292 rerere_clear(the_repository
, &merge_rr
);
1293 string_list_clear(&merge_rr
, 1);
1294 ropts
.flags
= RESET_HEAD_HARD
;
1295 if (reset_head(the_repository
, &ropts
) < 0)
1296 die(_("could not discard worktree changes"));
1297 remove_branch_state(the_repository
, 0);
1298 if (read_basic_state(&options
))
1302 case ACTION_ABORT
: {
1303 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1304 struct strbuf head_msg
= STRBUF_INIT
;
1306 rerere_clear(the_repository
, &merge_rr
);
1307 string_list_clear(&merge_rr
, 1);
1309 if (read_basic_state(&options
))
1312 strbuf_addf(&head_msg
, "%s (abort): returning to %s",
1313 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
),
1314 options
.head_name
? options
.head_name
1315 : oid_to_hex(&options
.orig_head
->object
.oid
));
1316 ropts
.oid
= &options
.orig_head
->object
.oid
;
1317 ropts
.head_msg
= head_msg
.buf
;
1318 ropts
.branch
= options
.head_name
;
1319 ropts
.flags
= RESET_HEAD_HARD
;
1320 if (reset_head(the_repository
, &ropts
) < 0)
1321 die(_("could not move back to %s"),
1322 oid_to_hex(&options
.orig_head
->object
.oid
));
1323 remove_branch_state(the_repository
, 0);
1324 ret
= finish_rebase(&options
);
1328 save_autostash(state_dir_path("autostash", &options
));
1329 if (options
.type
== REBASE_MERGE
) {
1330 struct replay_opts replay
= REPLAY_OPTS_INIT
;
1332 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
1333 ret
= sequencer_remove_state(&replay
);
1336 strbuf_addstr(&buf
, options
.state_dir
);
1337 ret
= remove_dir_recursively(&buf
, 0);
1339 error(_("could not remove '%s'"),
1344 case ACTION_EDIT_TODO
:
1345 options
.dont_finish_rebase
= 1;
1347 case ACTION_SHOW_CURRENT_PATCH
:
1348 options
.dont_finish_rebase
= 1;
1353 BUG("action: %d", options
.action
);
1356 /* Make sure no rebase is in progress */
1358 const char *last_slash
= strrchr(options
.state_dir
, '/');
1359 const char *state_dir_base
=
1360 last_slash
? last_slash
+ 1 : options
.state_dir
;
1361 const char *cmd_live_rebase
=
1362 "git rebase (--continue | --abort | --skip)";
1364 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
1365 die(_("It seems that there is already a %s directory, and\n"
1366 "I wonder if you are in the middle of another rebase. "
1368 "case, please try\n\t%s\n"
1369 "If that is not the case, please\n\t%s\n"
1370 "and run me again. I am stopping in case you still "
1372 "valuable there.\n"),
1373 state_dir_base
, cmd_live_rebase
, buf
.buf
);
1376 if ((options
.flags
& REBASE_INTERACTIVE_EXPLICIT
) ||
1377 (options
.action
!= ACTION_NONE
) ||
1379 options
.autosquash
) {
1380 allow_preemptive_ff
= 0;
1382 if (options
.committer_date_is_author_date
|| options
.ignore_date
)
1383 options
.flags
|= REBASE_FORCE
;
1385 for (i
= 0; i
< options
.git_am_opts
.nr
; i
++) {
1386 const char *option
= options
.git_am_opts
.v
[i
], *p
;
1387 if (!strcmp(option
, "--whitespace=fix") ||
1388 !strcmp(option
, "--whitespace=strip"))
1389 allow_preemptive_ff
= 0;
1390 else if (skip_prefix(option
, "-C", &p
)) {
1392 if (!isdigit(*(p
++)))
1393 die(_("switch `C' expects a "
1394 "numerical value"));
1395 } else if (skip_prefix(option
, "--whitespace=", &p
)) {
1396 if (*p
&& strcmp(p
, "warn") && strcmp(p
, "nowarn") &&
1397 strcmp(p
, "error") && strcmp(p
, "error-all"))
1398 die("Invalid whitespace option: '%s'", p
);
1402 for (i
= 0; i
< exec
.nr
; i
++)
1403 if (check_exec_cmd(exec
.items
[i
].string
))
1406 if (!(options
.flags
& REBASE_NO_QUIET
))
1407 strvec_push(&options
.git_am_opts
, "-q");
1409 if (options
.empty
!= EMPTY_UNSPECIFIED
)
1410 imply_merge(&options
, "--empty");
1413 * --keep-base implements --reapply-cherry-picks by altering upstream so
1414 * it works with both backends.
1416 if (options
.reapply_cherry_picks
&& !keep_base
)
1417 imply_merge(&options
, "--reapply-cherry-picks");
1420 options
.gpg_sign_opt
= xstrfmt("-S%s", gpg_sign
);
1425 imply_merge(&options
, "--exec");
1428 for (i
= 0; i
< exec
.nr
; i
++)
1429 strbuf_addf(&buf
, "exec %s\n", exec
.items
[i
].string
);
1430 options
.cmd
= xstrdup(buf
.buf
);
1433 if (rebase_merges
) {
1434 if (!*rebase_merges
)
1435 ; /* default mode; do nothing */
1436 else if (!strcmp("rebase-cousins", rebase_merges
))
1437 options
.rebase_cousins
= 1;
1438 else if (strcmp("no-rebase-cousins", rebase_merges
))
1439 die(_("Unknown mode: %s"), rebase_merges
);
1440 options
.rebase_merges
= 1;
1441 imply_merge(&options
, "--rebase-merges");
1444 if (options
.type
== REBASE_APPLY
) {
1445 if (ignore_whitespace
)
1446 strvec_push(&options
.git_am_opts
,
1447 "--ignore-whitespace");
1448 if (options
.committer_date_is_author_date
)
1449 strvec_push(&options
.git_am_opts
,
1450 "--committer-date-is-author-date");
1451 if (options
.ignore_date
)
1452 strvec_push(&options
.git_am_opts
, "--ignore-date");
1455 if (ignore_whitespace
) {
1456 string_list_append(&strategy_options
,
1457 "ignore-space-change");
1461 if (strategy_options
.nr
) {
1464 if (!options
.strategy
)
1465 options
.strategy
= "ort";
1468 for (i
= 0; i
< strategy_options
.nr
; i
++)
1469 strbuf_addf(&buf
, " --%s",
1470 strategy_options
.items
[i
].string
);
1471 options
.strategy_opts
= xstrdup(buf
.buf
);
1474 if (options
.strategy
) {
1475 options
.strategy
= xstrdup(options
.strategy
);
1476 switch (options
.type
) {
1478 die(_("--strategy requires --merge or --interactive"));
1482 case REBASE_UNSPECIFIED
:
1483 options
.type
= REBASE_MERGE
;
1486 BUG("unhandled rebase type (%d)", options
.type
);
1490 if (options
.type
== REBASE_MERGE
)
1491 imply_merge(&options
, "--merge");
1493 if (options
.root
&& !options
.onto_name
)
1494 imply_merge(&options
, "--root without --onto");
1496 if (isatty(2) && options
.flags
& REBASE_NO_QUIET
)
1497 strbuf_addstr(&options
.git_format_patch_opt
, " --progress");
1499 if (options
.git_am_opts
.nr
|| options
.type
== REBASE_APPLY
) {
1500 /* all am options except -q are compatible only with --apply */
1501 for (i
= options
.git_am_opts
.nr
- 1; i
>= 0; i
--)
1502 if (strcmp(options
.git_am_opts
.v
[i
], "-q"))
1506 if (is_merge(&options
))
1507 die(_("apply options and merge options "
1508 "cannot be used together"));
1510 options
.type
= REBASE_APPLY
;
1514 if (options
.type
== REBASE_UNSPECIFIED
) {
1515 if (!strcmp(options
.default_backend
, "merge"))
1516 imply_merge(&options
, "--merge");
1517 else if (!strcmp(options
.default_backend
, "apply"))
1518 options
.type
= REBASE_APPLY
;
1520 die(_("Unknown rebase backend: %s"),
1521 options
.default_backend
);
1524 if (options
.type
== REBASE_MERGE
&&
1525 !options
.strategy
&&
1526 getenv("GIT_TEST_MERGE_ALGORITHM"))
1527 options
.strategy
= xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
1529 switch (options
.type
) {
1531 options
.state_dir
= merge_dir();
1534 options
.state_dir
= apply_dir();
1537 BUG("options.type was just set above; should be unreachable.");
1540 if (options
.empty
== EMPTY_UNSPECIFIED
) {
1541 if (options
.flags
& REBASE_INTERACTIVE_EXPLICIT
)
1542 options
.empty
= EMPTY_ASK
;
1543 else if (exec
.nr
> 0)
1544 options
.empty
= EMPTY_KEEP
;
1546 options
.empty
= EMPTY_DROP
;
1548 if (reschedule_failed_exec
> 0 && !is_merge(&options
))
1549 die(_("--reschedule-failed-exec requires "
1550 "--exec or --interactive"));
1551 if (reschedule_failed_exec
>= 0)
1552 options
.reschedule_failed_exec
= reschedule_failed_exec
;
1554 if (options
.signoff
) {
1555 strvec_push(&options
.git_am_opts
, "--signoff");
1556 options
.flags
|= REBASE_FORCE
;
1559 if (!options
.root
) {
1561 struct branch
*branch
;
1563 branch
= branch_get(NULL
);
1564 options
.upstream_name
= branch_get_upstream(branch
,
1566 if (!options
.upstream_name
)
1567 error_on_missing_default_upstream();
1568 if (options
.fork_point
< 0)
1569 options
.fork_point
= 1;
1571 options
.upstream_name
= argv
[0];
1574 if (!strcmp(options
.upstream_name
, "-"))
1575 options
.upstream_name
= "@{-1}";
1578 lookup_commit_reference_by_name(options
.upstream_name
);
1579 if (!options
.upstream
)
1580 die(_("invalid upstream '%s'"), options
.upstream_name
);
1581 options
.upstream_arg
= options
.upstream_name
;
1583 if (!options
.onto_name
) {
1584 if (commit_tree("", 0, the_hash_algo
->empty_tree
, NULL
,
1585 &squash_onto
, NULL
, NULL
) < 0)
1586 die(_("Could not create new root commit"));
1587 options
.squash_onto
= &squash_onto
;
1588 options
.onto_name
= squash_onto_name
=
1589 xstrdup(oid_to_hex(&squash_onto
));
1591 options
.root_with_onto
= 1;
1593 options
.upstream_name
= NULL
;
1594 options
.upstream
= NULL
;
1596 usage_with_options(builtin_rebase_usage
,
1597 builtin_rebase_options
);
1598 options
.upstream_arg
= "--root";
1602 * If the branch to rebase is given, that is the branch we will rebase
1603 * branch_name -- branch/commit being rebased, or
1604 * HEAD (already detached)
1605 * orig_head -- commit object name of tip of the branch before rebasing
1606 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1609 /* Is it "rebase other branchname" or "rebase other commit"? */
1610 struct object_id branch_oid
;
1611 branch_name
= argv
[0];
1612 options
.switch_to
= argv
[0];
1614 /* Is it a local branch? */
1616 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
1617 if (!read_ref(buf
.buf
, &branch_oid
)) {
1618 die_if_checked_out(buf
.buf
, 1);
1619 options
.head_name
= xstrdup(buf
.buf
);
1621 lookup_commit_object(the_repository
,
1623 /* If not is it a valid ref (branch or commit)? */
1626 lookup_commit_reference_by_name(branch_name
);
1627 options
.head_name
= NULL
;
1629 if (!options
.orig_head
)
1630 die(_("no such branch/commit '%s'"), branch_name
);
1631 } else if (argc
== 0) {
1632 /* Do not need to switch branches, we are already on it. */
1634 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL
,
1636 if (!options
.head_name
)
1637 die(_("No such ref: %s"), "HEAD");
1638 if (flags
& REF_ISSYMREF
) {
1639 if (!skip_prefix(options
.head_name
,
1640 "refs/heads/", &branch_name
))
1641 branch_name
= options
.head_name
;
1644 FREE_AND_NULL(options
.head_name
);
1645 branch_name
= "HEAD";
1647 options
.orig_head
= lookup_commit_reference_by_name("HEAD");
1648 if (!options
.orig_head
)
1649 die(_("Could not resolve HEAD to a commit"));
1651 BUG("unexpected number of arguments left to parse");
1653 /* Make sure the branch to rebase onto is valid. */
1656 strbuf_addstr(&buf
, options
.upstream_name
);
1657 strbuf_addstr(&buf
, "...");
1658 strbuf_addstr(&buf
, branch_name
);
1659 options
.onto_name
= xstrdup(buf
.buf
);
1660 } else if (!options
.onto_name
)
1661 options
.onto_name
= options
.upstream_name
;
1662 if (strstr(options
.onto_name
, "...")) {
1663 if (get_oid_mb(options
.onto_name
, &branch_base
) < 0) {
1665 die(_("'%s': need exactly one merge base with branch"),
1666 options
.upstream_name
);
1668 die(_("'%s': need exactly one merge base"),
1671 options
.onto
= lookup_commit_or_die(&branch_base
,
1675 lookup_commit_reference_by_name(options
.onto_name
);
1677 die(_("Does not point to a valid commit '%s'"),
1679 fill_branch_base(&options
, &branch_base
);
1682 if (keep_base
&& options
.reapply_cherry_picks
)
1683 options
.upstream
= options
.onto
;
1685 if (options
.fork_point
> 0)
1686 options
.restrict_revision
=
1687 get_fork_point(options
.upstream_name
, options
.orig_head
);
1689 if (repo_read_index(the_repository
) < 0)
1690 die(_("could not read index"));
1692 if (options
.autostash
)
1693 create_autostash(the_repository
,
1694 state_dir_path("autostash", &options
));
1697 if (require_clean_work_tree(the_repository
, "rebase",
1698 _("Please commit or stash them."), 1, 1)) {
1704 * Now we are rebasing commits upstream..orig_head (or with --root,
1705 * everything leading up to orig_head) on top of onto.
1709 * Check if we are already based on onto with linear history,
1710 * in which case we could fast-forward without replacing the commits
1711 * with new commits recreated by replaying their changes.
1713 if (allow_preemptive_ff
&&
1714 can_fast_forward(options
.onto
, options
.upstream
, options
.restrict_revision
,
1715 options
.orig_head
, &branch_base
)) {
1718 if (!(options
.flags
& REBASE_FORCE
)) {
1719 /* Lazily switch to the target branch if needed... */
1720 if (options
.switch_to
) {
1721 ret
= checkout_up_to_date(&options
);
1726 if (!(options
.flags
& REBASE_NO_QUIET
))
1728 else if (!strcmp(branch_name
, "HEAD") &&
1729 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1730 puts(_("HEAD is up to date."));
1732 printf(_("Current branch %s is up to date.\n"),
1734 ret
= finish_rebase(&options
);
1736 } else if (!(options
.flags
& REBASE_NO_QUIET
))
1738 else if (!strcmp(branch_name
, "HEAD") &&
1739 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1740 puts(_("HEAD is up to date, rebase forced."));
1742 printf(_("Current branch %s is up to date, rebase "
1743 "forced.\n"), branch_name
);
1746 /* If a hook exists, give it a chance to interrupt*/
1747 if (!ok_to_skip_pre_rebase
&&
1748 run_hooks_l("pre-rebase", options
.upstream_arg
,
1749 argc
? argv
[0] : NULL
, NULL
))
1750 die(_("The pre-rebase hook refused to rebase."));
1752 if (options
.flags
& REBASE_DIFFSTAT
) {
1753 struct diff_options opts
;
1755 if (options
.flags
& REBASE_VERBOSE
) {
1756 if (is_null_oid(&branch_base
))
1757 printf(_("Changes to %s:\n"),
1758 oid_to_hex(&options
.onto
->object
.oid
));
1760 printf(_("Changes from %s to %s:\n"),
1761 oid_to_hex(&branch_base
),
1762 oid_to_hex(&options
.onto
->object
.oid
));
1765 /* We want color (if set), but no pager */
1767 opts
.stat_width
= -1; /* use full terminal width */
1768 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
1769 opts
.output_format
|=
1770 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1771 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1772 diff_setup_done(&opts
);
1773 diff_tree_oid(is_null_oid(&branch_base
) ?
1774 the_hash_algo
->empty_tree
: &branch_base
,
1775 &options
.onto
->object
.oid
, "", &opts
);
1776 diffcore_std(&opts
);
1780 if (is_merge(&options
))
1783 /* Detach HEAD and reset the tree */
1784 if (options
.flags
& REBASE_NO_QUIET
)
1785 printf(_("First, rewinding head to replay your work on top of "
1788 strbuf_addf(&msg
, "%s (start): checkout %s",
1789 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
), options
.onto_name
);
1790 ropts
.oid
= &options
.onto
->object
.oid
;
1791 ropts
.orig_head
= &options
.orig_head
->object
.oid
,
1792 ropts
.flags
= RESET_HEAD_DETACH
| RESET_ORIG_HEAD
|
1793 RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
1794 ropts
.head_msg
= msg
.buf
;
1795 ropts
.default_reflog_action
= DEFAULT_REFLOG_ACTION
;
1796 if (reset_head(the_repository
, &ropts
))
1797 die(_("Could not detach HEAD"));
1798 strbuf_release(&msg
);
1801 * If the onto is a proper descendant of the tip of the branch, then
1802 * we just fast-forwarded.
1804 if (oideq(&branch_base
, &options
.orig_head
->object
.oid
)) {
1805 printf(_("Fast-forwarded %s to %s.\n"),
1806 branch_name
, options
.onto_name
);
1807 move_to_original_branch(&options
);
1808 ret
= finish_rebase(&options
);
1812 strbuf_addf(&revisions
, "%s..%s",
1813 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
1814 (options
.restrict_revision
?
1815 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
1816 oid_to_hex(&options
.upstream
->object
.oid
)),
1817 oid_to_hex(&options
.orig_head
->object
.oid
));
1819 options
.revisions
= revisions
.buf
;
1822 ret
= run_specific_rebase(&options
);
1825 strbuf_release(&buf
);
1826 strbuf_release(&revisions
);
1827 free(options
.head_name
);
1828 free(options
.gpg_sign_opt
);
1830 free(options
.strategy
);
1831 strbuf_release(&options
.git_format_patch_opt
);
1832 free(squash_onto_name
);