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,
62 struct rebase_options
{
63 enum rebase_type type
;
64 enum empty_type empty
;
65 const char *default_backend
;
66 const char *state_dir
;
67 struct commit
*upstream
;
68 const char *upstream_name
;
69 const char *upstream_arg
;
71 struct object_id orig_head
;
73 const char *onto_name
;
74 const char *revisions
;
75 const char *switch_to
;
76 int root
, root_with_onto
;
77 struct object_id
*squash_onto
;
78 struct commit
*restrict_revision
;
79 int dont_finish_rebase
;
81 REBASE_NO_QUIET
= 1<<0,
82 REBASE_VERBOSE
= 1<<1,
83 REBASE_DIFFSTAT
= 1<<2,
85 REBASE_INTERACTIVE_EXPLICIT
= 1<<4,
87 struct strvec git_am_opts
;
90 int allow_rerere_autoupdate
;
95 int committer_date_is_author_date
;
98 int allow_empty_message
;
99 int rebase_merges
, rebase_cousins
;
100 char *strategy
, *strategy_opts
;
101 struct strbuf git_format_patch_opt
;
102 int reschedule_failed_exec
;
103 int reapply_cherry_picks
;
107 #define REBASE_OPTIONS_INIT { \
108 .type = REBASE_UNSPECIFIED, \
109 .empty = EMPTY_UNSPECIFIED, \
111 .default_backend = "merge", \
112 .flags = REBASE_NO_QUIET, \
113 .git_am_opts = STRVEC_INIT, \
114 .git_format_patch_opt = STRBUF_INIT, \
118 static struct replay_opts
get_replay_opts(const struct rebase_options
*opts
)
120 struct replay_opts replay
= REPLAY_OPTS_INIT
;
122 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
123 replay
.strategy
= NULL
;
124 sequencer_init_config(&replay
);
126 replay
.signoff
= opts
->signoff
;
127 replay
.allow_ff
= !(opts
->flags
& REBASE_FORCE
);
128 if (opts
->allow_rerere_autoupdate
)
129 replay
.allow_rerere_auto
= opts
->allow_rerere_autoupdate
;
130 replay
.allow_empty
= 1;
131 replay
.allow_empty_message
= opts
->allow_empty_message
;
132 replay
.drop_redundant_commits
= (opts
->empty
== EMPTY_DROP
);
133 replay
.keep_redundant_commits
= (opts
->empty
== EMPTY_KEEP
);
134 replay
.quiet
= !(opts
->flags
& REBASE_NO_QUIET
);
135 replay
.verbose
= opts
->flags
& REBASE_VERBOSE
;
136 replay
.reschedule_failed_exec
= opts
->reschedule_failed_exec
;
137 replay
.committer_date_is_author_date
=
138 opts
->committer_date_is_author_date
;
139 replay
.ignore_date
= opts
->ignore_date
;
140 replay
.gpg_sign
= xstrdup_or_null(opts
->gpg_sign_opt
);
142 replay
.strategy
= xstrdup_or_null(opts
->strategy
);
143 else if (!replay
.strategy
&& replay
.default_strategy
) {
144 replay
.strategy
= replay
.default_strategy
;
145 replay
.default_strategy
= NULL
;
148 if (opts
->strategy_opts
)
149 parse_strategy_opts(&replay
, opts
->strategy_opts
);
151 if (opts
->squash_onto
) {
152 oidcpy(&replay
.squash_onto
, opts
->squash_onto
);
153 replay
.have_squash_onto
= 1;
166 ACTION_SHOW_CURRENT_PATCH
169 static const char *action_names
[] = { "undefined",
175 "show_current_patch" };
177 static int edit_todo_file(unsigned flags
)
179 const char *todo_file
= rebase_path_todo();
180 struct todo_list todo_list
= TODO_LIST_INIT
,
181 new_todo
= TODO_LIST_INIT
;
184 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
185 return error_errno(_("could not read '%s'."), todo_file
);
187 strbuf_stripspace(&todo_list
.buf
, 1);
188 res
= edit_todo_list(the_repository
, &todo_list
, &new_todo
, NULL
, NULL
, flags
);
189 if (!res
&& todo_list_write_to_file(the_repository
, &new_todo
, todo_file
,
190 NULL
, NULL
, -1, flags
& ~(TODO_LIST_SHORTEN_IDS
)))
191 res
= error_errno(_("could not write '%s'"), todo_file
);
193 todo_list_release(&todo_list
);
194 todo_list_release(&new_todo
);
199 static int get_revision_ranges(struct commit
*upstream
, struct commit
*onto
,
200 struct object_id
*orig_head
, char **revisions
,
201 char **shortrevisions
)
203 struct commit
*base_rev
= upstream
? upstream
: onto
;
204 const char *shorthead
;
206 *revisions
= xstrfmt("%s...%s", oid_to_hex(&base_rev
->object
.oid
),
207 oid_to_hex(orig_head
));
209 shorthead
= find_unique_abbrev(orig_head
, DEFAULT_ABBREV
);
212 const char *shortrev
;
214 shortrev
= find_unique_abbrev(&base_rev
->object
.oid
,
217 *shortrevisions
= xstrfmt("%s..%s", shortrev
, shorthead
);
219 *shortrevisions
= xstrdup(shorthead
);
224 static int init_basic_state(struct replay_opts
*opts
, const char *head_name
,
226 const struct object_id
*orig_head
)
230 if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir()))
231 return error_errno(_("could not create temporary %s"), merge_dir());
233 delete_reflog("REBASE_HEAD");
235 interactive
= fopen(path_interactive(), "w");
237 return error_errno(_("could not mark as interactive"));
240 return write_basic_state(opts
, head_name
, onto
, orig_head
);
243 static void split_exec_commands(const char *cmd
, struct string_list
*commands
)
246 string_list_split(commands
, cmd
, '\n', -1);
248 /* rebase.c adds a new line to cmd after every command,
249 * so here the last command is always empty */
250 string_list_remove_empty_items(commands
, 0);
254 static int do_interactive_rebase(struct rebase_options
*opts
, unsigned flags
)
257 char *revisions
= NULL
, *shortrevisions
= NULL
;
258 struct strvec make_script_args
= STRVEC_INIT
;
259 struct todo_list todo_list
= TODO_LIST_INIT
;
260 struct replay_opts replay
= get_replay_opts(opts
);
261 struct string_list commands
= STRING_LIST_INIT_DUP
;
263 if (get_revision_ranges(opts
->upstream
, opts
->onto
, &opts
->orig_head
,
264 &revisions
, &shortrevisions
))
267 if (init_basic_state(&replay
,
268 opts
->head_name
? opts
->head_name
: "detached HEAD",
269 opts
->onto
, &opts
->orig_head
)) {
271 free(shortrevisions
);
276 if (!opts
->upstream
&& opts
->squash_onto
)
277 write_file(path_squash_onto(), "%s\n",
278 oid_to_hex(opts
->squash_onto
));
280 strvec_pushl(&make_script_args
, "", revisions
, NULL
);
281 if (opts
->restrict_revision
)
282 strvec_pushf(&make_script_args
, "^%s",
283 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
285 ret
= sequencer_make_script(the_repository
, &todo_list
.buf
,
286 make_script_args
.nr
, make_script_args
.v
,
290 error(_("could not generate todo list"));
293 if (todo_list_parse_insn_buffer(the_repository
, todo_list
.buf
.buf
,
295 BUG("unusable todo list");
297 split_exec_commands(opts
->cmd
, &commands
);
298 ret
= complete_action(the_repository
, &replay
, flags
,
299 shortrevisions
, opts
->onto_name
, opts
->onto
,
300 &opts
->orig_head
, &commands
, opts
->autosquash
,
304 string_list_clear(&commands
, 0);
306 free(shortrevisions
);
307 todo_list_release(&todo_list
);
308 strvec_clear(&make_script_args
);
313 static int run_sequencer_rebase(struct rebase_options
*opts
,
317 int abbreviate_commands
= 0, ret
= 0;
319 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands
);
321 flags
|= opts
->keep_empty
? TODO_LIST_KEEP_EMPTY
: 0;
322 flags
|= abbreviate_commands
? TODO_LIST_ABBREVIATE_CMDS
: 0;
323 flags
|= opts
->rebase_merges
? TODO_LIST_REBASE_MERGES
: 0;
324 flags
|= opts
->rebase_cousins
> 0 ? TODO_LIST_REBASE_COUSINS
: 0;
325 flags
|= opts
->root_with_onto
? TODO_LIST_ROOT_WITH_ONTO
: 0;
326 flags
|= opts
->reapply_cherry_picks
? TODO_LIST_REAPPLY_CHERRY_PICKS
: 0;
327 flags
|= opts
->flags
& REBASE_NO_QUIET
? TODO_LIST_WARN_SKIPPED_CHERRY_PICKS
: 0;
331 if (!opts
->onto
&& !opts
->upstream
)
332 die(_("a base commit must be provided with --upstream or --onto"));
334 ret
= do_interactive_rebase(opts
, flags
);
338 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
340 rerere_clear(the_repository
, &merge_rr
);
343 case ACTION_CONTINUE
: {
344 struct replay_opts replay_opts
= get_replay_opts(opts
);
346 ret
= sequencer_continue(the_repository
, &replay_opts
);
349 case ACTION_EDIT_TODO
:
350 ret
= edit_todo_file(flags
);
352 case ACTION_SHOW_CURRENT_PATCH
: {
353 struct child_process cmd
= CHILD_PROCESS_INIT
;
356 strvec_pushl(&cmd
.args
, "show", "REBASE_HEAD", "--", NULL
);
357 ret
= run_command(&cmd
);
362 BUG("invalid command '%d'", command
);
368 static void imply_merge(struct rebase_options
*opts
, const char *option
);
369 static int parse_opt_keep_empty(const struct option
*opt
, const char *arg
,
372 struct rebase_options
*opts
= opt
->value
;
376 imply_merge(opts
, unset
? "--no-keep-empty" : "--keep-empty");
377 opts
->keep_empty
= !unset
;
378 opts
->type
= REBASE_MERGE
;
382 static int is_merge(struct rebase_options
*opts
)
384 return opts
->type
== REBASE_MERGE
;
387 static void imply_merge(struct rebase_options
*opts
, const char *option
)
389 switch (opts
->type
) {
391 die(_("%s requires the merge backend"), option
);
396 opts
->type
= REBASE_MERGE
; /* implied */
401 /* Returns the filename prefixed by the state_dir */
402 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
404 static struct strbuf path
= STRBUF_INIT
;
405 static size_t prefix_len
;
408 strbuf_addf(&path
, "%s/", opts
->state_dir
);
409 prefix_len
= path
.len
;
412 strbuf_setlen(&path
, prefix_len
);
413 strbuf_addstr(&path
, filename
);
417 /* Initialize the rebase options from the state directory. */
418 static int read_basic_state(struct rebase_options
*opts
)
420 struct strbuf head_name
= STRBUF_INIT
;
421 struct strbuf buf
= STRBUF_INIT
;
422 struct object_id oid
;
424 if (!read_oneliner(&head_name
, state_dir_path("head-name", opts
),
425 READ_ONELINER_WARN_MISSING
) ||
426 !read_oneliner(&buf
, state_dir_path("onto", opts
),
427 READ_ONELINER_WARN_MISSING
))
429 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
430 xstrdup(head_name
.buf
) : NULL
;
431 strbuf_release(&head_name
);
432 if (get_oid(buf
.buf
, &oid
))
433 return error(_("could not get 'onto': '%s'"), buf
.buf
);
434 opts
->onto
= lookup_commit_or_die(&oid
, buf
.buf
);
437 * We always write to orig-head, but interactive rebase used to write to
438 * head. Fall back to reading from head to cover for the case that the
439 * user upgraded git with an ongoing interactive rebase.
442 if (file_exists(state_dir_path("orig-head", opts
))) {
443 if (!read_oneliner(&buf
, state_dir_path("orig-head", opts
),
444 READ_ONELINER_WARN_MISSING
))
446 } else if (!read_oneliner(&buf
, state_dir_path("head", opts
),
447 READ_ONELINER_WARN_MISSING
))
449 if (get_oid(buf
.buf
, &opts
->orig_head
))
450 return error(_("invalid orig-head: '%s'"), buf
.buf
);
452 if (file_exists(state_dir_path("quiet", opts
)))
453 opts
->flags
&= ~REBASE_NO_QUIET
;
455 opts
->flags
|= REBASE_NO_QUIET
;
457 if (file_exists(state_dir_path("verbose", opts
)))
458 opts
->flags
|= REBASE_VERBOSE
;
460 if (file_exists(state_dir_path("signoff", opts
))) {
462 opts
->flags
|= REBASE_FORCE
;
465 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts
))) {
467 if (!read_oneliner(&buf
, state_dir_path("allow_rerere_autoupdate", opts
),
468 READ_ONELINER_WARN_MISSING
))
470 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
471 opts
->allow_rerere_autoupdate
= RERERE_AUTOUPDATE
;
472 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
473 opts
->allow_rerere_autoupdate
= RERERE_NOAUTOUPDATE
;
475 warning(_("ignoring invalid allow_rerere_autoupdate: "
479 if (file_exists(state_dir_path("gpg_sign_opt", opts
))) {
481 if (!read_oneliner(&buf
, state_dir_path("gpg_sign_opt", opts
),
482 READ_ONELINER_WARN_MISSING
))
484 free(opts
->gpg_sign_opt
);
485 opts
->gpg_sign_opt
= xstrdup(buf
.buf
);
488 if (file_exists(state_dir_path("strategy", opts
))) {
490 if (!read_oneliner(&buf
, state_dir_path("strategy", opts
),
491 READ_ONELINER_WARN_MISSING
))
493 free(opts
->strategy
);
494 opts
->strategy
= xstrdup(buf
.buf
);
497 if (file_exists(state_dir_path("strategy_opts", opts
))) {
499 if (!read_oneliner(&buf
, state_dir_path("strategy_opts", opts
),
500 READ_ONELINER_WARN_MISSING
))
502 free(opts
->strategy_opts
);
503 opts
->strategy_opts
= xstrdup(buf
.buf
);
506 strbuf_release(&buf
);
511 static int rebase_write_basic_state(struct rebase_options
*opts
)
513 write_file(state_dir_path("head-name", opts
), "%s",
514 opts
->head_name
? opts
->head_name
: "detached HEAD");
515 write_file(state_dir_path("onto", opts
), "%s",
516 opts
->onto
? oid_to_hex(&opts
->onto
->object
.oid
) : "");
517 write_file(state_dir_path("orig-head", opts
), "%s",
518 oid_to_hex(&opts
->orig_head
));
519 if (!(opts
->flags
& REBASE_NO_QUIET
))
520 write_file(state_dir_path("quiet", opts
), "%s", "");
521 if (opts
->flags
& REBASE_VERBOSE
)
522 write_file(state_dir_path("verbose", opts
), "%s", "");
524 write_file(state_dir_path("strategy", opts
), "%s",
526 if (opts
->strategy_opts
)
527 write_file(state_dir_path("strategy_opts", opts
), "%s",
528 opts
->strategy_opts
);
529 if (opts
->allow_rerere_autoupdate
> 0)
530 write_file(state_dir_path("allow_rerere_autoupdate", opts
),
531 "-%s-rerere-autoupdate",
532 opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
?
534 if (opts
->gpg_sign_opt
)
535 write_file(state_dir_path("gpg_sign_opt", opts
), "%s",
538 write_file(state_dir_path("signoff", opts
), "--signoff");
543 static int finish_rebase(struct rebase_options
*opts
)
545 struct strbuf dir
= STRBUF_INIT
;
548 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
549 unlink(git_path_auto_merge(the_repository
));
550 apply_autostash(state_dir_path("autostash", opts
));
552 * We ignore errors in 'git maintenance run --auto', since the
553 * user should see them.
555 run_auto_maintenance(!(opts
->flags
& (REBASE_NO_QUIET
|REBASE_VERBOSE
)));
556 if (opts
->type
== REBASE_MERGE
) {
557 struct replay_opts replay
= REPLAY_OPTS_INIT
;
559 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
560 ret
= sequencer_remove_state(&replay
);
562 strbuf_addstr(&dir
, opts
->state_dir
);
563 if (remove_dir_recursively(&dir
, 0))
564 ret
= error(_("could not remove '%s'"),
566 strbuf_release(&dir
);
572 static int move_to_original_branch(struct rebase_options
*opts
)
574 struct strbuf branch_reflog
= STRBUF_INIT
, head_reflog
= STRBUF_INIT
;
575 struct reset_head_opts ropts
= { 0 };
578 if (!opts
->head_name
)
579 return 0; /* nothing to move back to */
582 BUG("move_to_original_branch without onto");
584 strbuf_addf(&branch_reflog
, "rebase finished: %s onto %s",
585 opts
->head_name
, oid_to_hex(&opts
->onto
->object
.oid
));
586 strbuf_addf(&head_reflog
, "rebase finished: returning to %s",
588 ropts
.branch
= opts
->head_name
;
589 ropts
.flags
= RESET_HEAD_REFS_ONLY
;
590 ropts
.branch_msg
= branch_reflog
.buf
;
591 ropts
.head_msg
= head_reflog
.buf
;
592 ret
= reset_head(the_repository
, &ropts
);
594 strbuf_release(&branch_reflog
);
595 strbuf_release(&head_reflog
);
599 static const char *resolvemsg
=
600 N_("Resolve all conflicts manually, mark them as resolved with\n"
601 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
602 "You can instead skip this commit: run \"git rebase --skip\".\n"
603 "To abort and get back to the state before \"git rebase\", run "
604 "\"git rebase --abort\".");
606 static int run_am(struct rebase_options
*opts
)
608 struct child_process am
= CHILD_PROCESS_INIT
;
609 struct child_process format_patch
= CHILD_PROCESS_INIT
;
610 struct strbuf revisions
= STRBUF_INIT
;
612 char *rebased_patches
;
615 strvec_push(&am
.args
, "am");
617 if (opts
->action
&& !strcmp("continue", opts
->action
)) {
618 strvec_push(&am
.args
, "--resolved");
619 strvec_pushf(&am
.args
, "--resolvemsg=%s", resolvemsg
);
620 if (opts
->gpg_sign_opt
)
621 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
622 status
= run_command(&am
);
626 return move_to_original_branch(opts
);
628 if (opts
->action
&& !strcmp("skip", opts
->action
)) {
629 strvec_push(&am
.args
, "--skip");
630 strvec_pushf(&am
.args
, "--resolvemsg=%s", resolvemsg
);
631 status
= run_command(&am
);
635 return move_to_original_branch(opts
);
637 if (opts
->action
&& !strcmp("show-current-patch", opts
->action
)) {
638 strvec_push(&am
.args
, "--show-current-patch");
639 return run_command(&am
);
642 strbuf_addf(&revisions
, "%s...%s",
643 oid_to_hex(opts
->root
?
644 /* this is now equivalent to !opts->upstream */
645 &opts
->onto
->object
.oid
:
646 &opts
->upstream
->object
.oid
),
647 oid_to_hex(&opts
->orig_head
));
649 rebased_patches
= xstrdup(git_path("rebased-patches"));
650 format_patch
.out
= open(rebased_patches
,
651 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
652 if (format_patch
.out
< 0) {
653 status
= error_errno(_("could not open '%s' for writing"),
655 free(rebased_patches
);
656 strvec_clear(&am
.args
);
660 format_patch
.git_cmd
= 1;
661 strvec_pushl(&format_patch
.args
, "format-patch", "-k", "--stdout",
662 "--full-index", "--cherry-pick", "--right-only",
663 "--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
664 "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
666 if (opts
->git_format_patch_opt
.len
)
667 strvec_split(&format_patch
.args
,
668 opts
->git_format_patch_opt
.buf
);
669 strvec_push(&format_patch
.args
, revisions
.buf
);
670 if (opts
->restrict_revision
)
671 strvec_pushf(&format_patch
.args
, "^%s",
672 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
674 status
= run_command(&format_patch
);
676 struct reset_head_opts ropts
= { 0 };
677 unlink(rebased_patches
);
678 free(rebased_patches
);
679 strvec_clear(&am
.args
);
681 ropts
.oid
= &opts
->orig_head
;
682 ropts
.branch
= opts
->head_name
;
683 ropts
.default_reflog_action
= DEFAULT_REFLOG_ACTION
;
684 reset_head(the_repository
, &ropts
);
685 error(_("\ngit encountered an error while preparing the "
686 "patches to replay\n"
689 "As a result, git cannot rebase them."),
692 strbuf_release(&revisions
);
695 strbuf_release(&revisions
);
697 am
.in
= open(rebased_patches
, O_RDONLY
);
699 status
= error_errno(_("could not open '%s' for reading"),
701 free(rebased_patches
);
702 strvec_clear(&am
.args
);
706 strvec_pushv(&am
.args
, opts
->git_am_opts
.v
);
707 strvec_push(&am
.args
, "--rebasing");
708 strvec_pushf(&am
.args
, "--resolvemsg=%s", resolvemsg
);
709 strvec_push(&am
.args
, "--patch-format=mboxrd");
710 if (opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
)
711 strvec_push(&am
.args
, "--rerere-autoupdate");
712 else if (opts
->allow_rerere_autoupdate
== RERERE_NOAUTOUPDATE
)
713 strvec_push(&am
.args
, "--no-rerere-autoupdate");
714 if (opts
->gpg_sign_opt
)
715 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
716 status
= run_command(&am
);
717 unlink(rebased_patches
);
718 free(rebased_patches
);
721 return move_to_original_branch(opts
);
724 if (is_directory(opts
->state_dir
))
725 rebase_write_basic_state(opts
);
730 static int run_specific_rebase(struct rebase_options
*opts
, enum action action
)
734 if (opts
->type
== REBASE_MERGE
) {
735 /* Run sequencer-based rebase */
736 setenv("GIT_CHERRY_PICK_HELP", resolvemsg
, 1);
737 if (!(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
)) {
738 setenv("GIT_SEQUENCE_EDITOR", ":", 1);
739 opts
->autosquash
= 0;
741 if (opts
->gpg_sign_opt
) {
742 /* remove the leading "-S" */
743 char *tmp
= xstrdup(opts
->gpg_sign_opt
+ 2);
744 free(opts
->gpg_sign_opt
);
745 opts
->gpg_sign_opt
= tmp
;
748 status
= run_sequencer_rebase(opts
, action
);
749 } else if (opts
->type
== REBASE_APPLY
)
750 status
= run_am(opts
);
752 BUG("Unhandled rebase type %d", opts
->type
);
754 if (opts
->dont_finish_rebase
)
756 else if (opts
->type
== REBASE_MERGE
)
757 ; /* merge backend cleans up after itself */
758 else if (status
== 0) {
759 if (!file_exists(state_dir_path("stopped-sha", opts
)))
761 } else if (status
== 2) {
762 struct strbuf dir
= STRBUF_INIT
;
764 apply_autostash(state_dir_path("autostash", opts
));
765 strbuf_addstr(&dir
, opts
->state_dir
);
766 remove_dir_recursively(&dir
, 0);
767 strbuf_release(&dir
);
768 die("Nothing to do");
771 return status
? -1 : 0;
774 static int rebase_config(const char *var
, const char *value
, void *data
)
776 struct rebase_options
*opts
= data
;
778 if (!strcmp(var
, "rebase.stat")) {
779 if (git_config_bool(var
, value
))
780 opts
->flags
|= REBASE_DIFFSTAT
;
782 opts
->flags
&= ~REBASE_DIFFSTAT
;
786 if (!strcmp(var
, "rebase.autosquash")) {
787 opts
->autosquash
= git_config_bool(var
, value
);
791 if (!strcmp(var
, "commit.gpgsign")) {
792 free(opts
->gpg_sign_opt
);
793 opts
->gpg_sign_opt
= git_config_bool(var
, value
) ?
794 xstrdup("-S") : NULL
;
798 if (!strcmp(var
, "rebase.autostash")) {
799 opts
->autostash
= git_config_bool(var
, value
);
803 if (!strcmp(var
, "rebase.reschedulefailedexec")) {
804 opts
->reschedule_failed_exec
= git_config_bool(var
, value
);
808 if (!strcmp(var
, "rebase.forkpoint")) {
809 opts
->fork_point
= git_config_bool(var
, value
) ? -1 : 0;
813 if (!strcmp(var
, "rebase.backend")) {
814 return git_config_string(&opts
->default_backend
, var
, value
);
817 return git_default_config(var
, value
, data
);
820 static int checkout_up_to_date(struct rebase_options
*options
)
822 struct strbuf buf
= STRBUF_INIT
;
823 struct reset_head_opts ropts
= { 0 };
826 strbuf_addf(&buf
, "%s: checkout %s",
827 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
),
829 ropts
.oid
= &options
->orig_head
;
830 ropts
.branch
= options
->head_name
;
831 ropts
.flags
= RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
833 ropts
.flags
|= RESET_HEAD_DETACH
;
834 ropts
.head_msg
= buf
.buf
;
835 if (reset_head(the_repository
, &ropts
) < 0)
836 ret
= error(_("could not switch to %s"), options
->switch_to
);
837 strbuf_release(&buf
);
843 * Determines whether the commits in from..to are linear, i.e. contain
844 * no merge commits. This function *expects* `from` to be an ancestor of
847 static int is_linear_history(struct commit
*from
, struct commit
*to
)
849 while (to
&& to
!= from
) {
853 if (to
->parents
->next
)
855 to
= to
->parents
->item
;
860 static int can_fast_forward(struct commit
*onto
, struct commit
*upstream
,
861 struct commit
*restrict_revision
,
862 struct object_id
*head_oid
, struct object_id
*merge_base
)
864 struct commit
*head
= lookup_commit(the_repository
, head_oid
);
865 struct commit_list
*merge_bases
= NULL
;
871 merge_bases
= get_merge_bases(onto
, head
);
872 if (!merge_bases
|| merge_bases
->next
) {
873 oidcpy(merge_base
, null_oid());
877 oidcpy(merge_base
, &merge_bases
->item
->object
.oid
);
878 if (!oideq(merge_base
, &onto
->object
.oid
))
881 if (restrict_revision
&& !oideq(&restrict_revision
->object
.oid
, merge_base
))
887 free_commit_list(merge_bases
);
888 merge_bases
= get_merge_bases(upstream
, head
);
889 if (!merge_bases
|| merge_bases
->next
)
892 if (!oideq(&onto
->object
.oid
, &merge_bases
->item
->object
.oid
))
898 free_commit_list(merge_bases
);
899 return res
&& is_linear_history(onto
, head
);
902 static int parse_opt_am(const struct option
*opt
, const char *arg
, int unset
)
904 struct rebase_options
*opts
= opt
->value
;
906 BUG_ON_OPT_NEG(unset
);
909 opts
->type
= REBASE_APPLY
;
914 /* -i followed by -m is still -i */
915 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
917 struct rebase_options
*opts
= opt
->value
;
919 BUG_ON_OPT_NEG(unset
);
923 opts
->type
= REBASE_MERGE
;
928 /* -i followed by -r is still explicitly interactive, but -r alone is not */
929 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
932 struct rebase_options
*opts
= opt
->value
;
934 BUG_ON_OPT_NEG(unset
);
937 opts
->type
= REBASE_MERGE
;
938 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
943 static enum empty_type
parse_empty_value(const char *value
)
945 if (!strcasecmp(value
, "drop"))
947 else if (!strcasecmp(value
, "keep"))
949 else if (!strcasecmp(value
, "ask"))
952 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"ask\"."), value
);
955 static int parse_opt_empty(const struct option
*opt
, const char *arg
, int unset
)
957 struct rebase_options
*options
= opt
->value
;
958 enum empty_type value
= parse_empty_value(arg
);
960 BUG_ON_OPT_NEG(unset
);
962 options
->empty
= value
;
966 static void NORETURN
error_on_missing_default_upstream(void)
968 struct branch
*current_branch
= branch_get(NULL
);
971 "Please specify which branch you want to rebase against.\n"
972 "See git-rebase(1) for details.\n"
974 " git rebase '<branch>'\n"
976 current_branch
? _("There is no tracking information for "
977 "the current branch.") :
978 _("You are not currently on a branch."));
980 if (current_branch
) {
981 const char *remote
= current_branch
->remote_name
;
984 remote
= _("<remote>");
986 printf(_("If you wish to set tracking information for this "
987 "branch you can do so with:\n"
989 " git branch --set-upstream-to=%s/<branch> %s\n"
991 remote
, current_branch
->name
);
996 static void set_reflog_action(struct rebase_options
*options
)
999 struct strbuf buf
= STRBUF_INIT
;
1001 if (!is_merge(options
))
1004 env
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
1005 if (env
&& strcmp("rebase", env
))
1006 return; /* only override it if it is "rebase" */
1008 strbuf_addf(&buf
, "rebase (%s)", options
->action
);
1009 setenv(GIT_REFLOG_ACTION_ENVIRONMENT
, buf
.buf
, 1);
1010 strbuf_release(&buf
);
1013 static int check_exec_cmd(const char *cmd
)
1015 if (strchr(cmd
, '\n'))
1016 return error(_("exec commands cannot contain newlines"));
1018 /* Does the command consist purely of whitespace? */
1019 if (!cmd
[strspn(cmd
, " \t\r\f\v")])
1020 return error(_("empty exec command"));
1025 int cmd_rebase(int argc
, const char **argv
, const char *prefix
)
1027 struct rebase_options options
= REBASE_OPTIONS_INIT
;
1028 const char *branch_name
;
1029 int ret
, flags
, total_argc
, in_progress
= 0;
1031 int ok_to_skip_pre_rebase
= 0;
1032 struct strbuf msg
= STRBUF_INIT
;
1033 struct strbuf revisions
= STRBUF_INIT
;
1034 struct strbuf buf
= STRBUF_INIT
;
1035 struct object_id merge_base
;
1036 int ignore_whitespace
= 0;
1037 enum action action
= ACTION_NONE
;
1038 const char *gpg_sign
= NULL
;
1039 struct string_list exec
= STRING_LIST_INIT_NODUP
;
1040 const char *rebase_merges
= NULL
;
1041 struct string_list strategy_options
= STRING_LIST_INIT_NODUP
;
1042 struct object_id squash_onto
;
1043 char *squash_onto_name
= NULL
;
1044 int reschedule_failed_exec
= -1;
1045 int allow_preemptive_ff
= 1;
1046 int preserve_merges_selected
= 0;
1047 struct reset_head_opts ropts
= { 0 };
1048 struct option builtin_rebase_options
[] = {
1049 OPT_STRING(0, "onto", &options
.onto_name
,
1051 N_("rebase onto given branch instead of upstream")),
1052 OPT_BOOL(0, "keep-base", &keep_base
,
1053 N_("use the merge-base of upstream and branch as the current base")),
1054 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
1055 N_("allow pre-rebase hook to run")),
1056 OPT_NEGBIT('q', "quiet", &options
.flags
,
1057 N_("be quiet. implies --no-stat"),
1058 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1059 OPT_BIT('v', "verbose", &options
.flags
,
1060 N_("display a diffstat of what changed upstream"),
1061 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1062 {OPTION_NEGBIT
, 'n', "no-stat", &options
.flags
, NULL
,
1063 N_("do not show diffstat of what changed upstream"),
1064 PARSE_OPT_NOARG
, NULL
, REBASE_DIFFSTAT
},
1065 OPT_BOOL(0, "signoff", &options
.signoff
,
1066 N_("add a Signed-off-by trailer to each commit")),
1067 OPT_BOOL(0, "committer-date-is-author-date",
1068 &options
.committer_date_is_author_date
,
1069 N_("make committer date match author date")),
1070 OPT_BOOL(0, "reset-author-date", &options
.ignore_date
,
1071 N_("ignore author date and use current date")),
1072 OPT_HIDDEN_BOOL(0, "ignore-date", &options
.ignore_date
,
1073 N_("synonym of --reset-author-date")),
1074 OPT_PASSTHRU_ARGV('C', NULL
, &options
.git_am_opts
, N_("n"),
1075 N_("passed to 'git apply'"), 0),
1076 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace
,
1077 N_("ignore changes in whitespace")),
1078 OPT_PASSTHRU_ARGV(0, "whitespace", &options
.git_am_opts
,
1079 N_("action"), N_("passed to 'git apply'"), 0),
1080 OPT_BIT('f', "force-rebase", &options
.flags
,
1081 N_("cherry-pick all commits, even if unchanged"),
1083 OPT_BIT(0, "no-ff", &options
.flags
,
1084 N_("cherry-pick all commits, even if unchanged"),
1086 OPT_CMDMODE(0, "continue", &action
, N_("continue"),
1088 OPT_CMDMODE(0, "skip", &action
,
1089 N_("skip current patch and continue"), ACTION_SKIP
),
1090 OPT_CMDMODE(0, "abort", &action
,
1091 N_("abort and check out the original branch"),
1093 OPT_CMDMODE(0, "quit", &action
,
1094 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
1095 OPT_CMDMODE(0, "edit-todo", &action
, N_("edit the todo list "
1096 "during an interactive rebase"), ACTION_EDIT_TODO
),
1097 OPT_CMDMODE(0, "show-current-patch", &action
,
1098 N_("show the patch file being applied or merged"),
1099 ACTION_SHOW_CURRENT_PATCH
),
1100 OPT_CALLBACK_F(0, "apply", &options
, NULL
,
1101 N_("use apply strategies to rebase"),
1102 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1104 OPT_CALLBACK_F('m', "merge", &options
, NULL
,
1105 N_("use merging strategies to rebase"),
1106 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1108 OPT_CALLBACK_F('i', "interactive", &options
, NULL
,
1109 N_("let the user edit the list of commits to rebase"),
1110 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1111 parse_opt_interactive
),
1112 OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected
,
1113 N_("(DEPRECATED) try to recreate merges instead of "
1115 1, PARSE_OPT_HIDDEN
),
1116 OPT_RERERE_AUTOUPDATE(&options
.allow_rerere_autoupdate
),
1117 OPT_CALLBACK_F(0, "empty", &options
, "{drop,keep,ask}",
1118 N_("how to handle commits that become empty"),
1119 PARSE_OPT_NONEG
, parse_opt_empty
),
1120 OPT_CALLBACK_F('k', "keep-empty", &options
, NULL
,
1121 N_("keep commits which start empty"),
1122 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
,
1123 parse_opt_keep_empty
),
1124 OPT_BOOL(0, "autosquash", &options
.autosquash
,
1125 N_("move commits that begin with "
1126 "squash!/fixup! under -i")),
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
.allow_empty_message
= 1;
1169 git_config(rebase_config
, &options
);
1170 /* options.gpg_sign_opt will be either "-S" or NULL */
1171 gpg_sign
= options
.gpg_sign_opt
? "" : NULL
;
1172 FREE_AND_NULL(options
.gpg_sign_opt
);
1175 strbuf_addf(&buf
, "%s/applying", apply_dir());
1176 if(file_exists(buf
.buf
))
1177 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1179 if (is_directory(apply_dir())) {
1180 options
.type
= REBASE_APPLY
;
1181 options
.state_dir
= apply_dir();
1182 } else if (is_directory(merge_dir())) {
1184 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
1185 if (is_directory(buf
.buf
)) {
1186 die("`rebase -p` is no longer supported");
1189 strbuf_addf(&buf
, "%s/interactive", merge_dir());
1190 if(file_exists(buf
.buf
)) {
1191 options
.type
= REBASE_MERGE
;
1192 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
1194 options
.type
= REBASE_MERGE
;
1196 options
.state_dir
= merge_dir();
1199 if (options
.type
!= REBASE_UNSPECIFIED
)
1203 argc
= parse_options(argc
, argv
, prefix
,
1204 builtin_rebase_options
,
1205 builtin_rebase_usage
, 0);
1207 if (preserve_merges_selected
)
1208 die(_("--preserve-merges was replaced by --rebase-merges"));
1210 if (action
!= ACTION_NONE
&& total_argc
!= 2) {
1211 usage_with_options(builtin_rebase_usage
,
1212 builtin_rebase_options
);
1216 usage_with_options(builtin_rebase_usage
,
1217 builtin_rebase_options
);
1220 if (options
.onto_name
)
1221 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--onto");
1223 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--root");
1226 if (options
.root
&& options
.fork_point
> 0)
1227 die(_("options '%s' and '%s' cannot be used together"), "--root", "--fork-point");
1229 if (action
!= ACTION_NONE
&& !in_progress
)
1230 die(_("No rebase in progress?"));
1231 setenv(GIT_REFLOG_ACTION_ENVIRONMENT
, "rebase", 0);
1233 if (action
== ACTION_EDIT_TODO
&& !is_merge(&options
))
1234 die(_("The --edit-todo action can only be used during "
1235 "interactive rebase."));
1237 if (trace2_is_enabled()) {
1238 if (is_merge(&options
))
1239 trace2_cmd_mode("interactive");
1241 trace2_cmd_mode("interactive-exec");
1243 trace2_cmd_mode(action_names
[action
]);
1247 case ACTION_CONTINUE
: {
1248 struct object_id head
;
1249 struct lock_file lock_file
= LOCK_INIT
;
1252 options
.action
= "continue";
1253 set_reflog_action(&options
);
1256 if (get_oid("HEAD", &head
))
1257 die(_("Cannot read HEAD"));
1259 fd
= hold_locked_index(&lock_file
, 0);
1260 if (repo_read_index(the_repository
) < 0)
1261 die(_("could not read index"));
1262 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
1265 repo_update_index_if_able(the_repository
, &lock_file
);
1266 rollback_lock_file(&lock_file
);
1268 if (has_unstaged_changes(the_repository
, 1)) {
1269 puts(_("You must edit all merge conflicts and then\n"
1270 "mark them as resolved using git add"));
1273 if (read_basic_state(&options
))
1278 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1280 options
.action
= "skip";
1281 set_reflog_action(&options
);
1283 rerere_clear(the_repository
, &merge_rr
);
1284 string_list_clear(&merge_rr
, 1);
1285 ropts
.flags
= RESET_HEAD_HARD
;
1286 if (reset_head(the_repository
, &ropts
) < 0)
1287 die(_("could not discard worktree changes"));
1288 remove_branch_state(the_repository
, 0);
1289 if (read_basic_state(&options
))
1293 case ACTION_ABORT
: {
1294 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1295 options
.action
= "abort";
1296 set_reflog_action(&options
);
1298 rerere_clear(the_repository
, &merge_rr
);
1299 string_list_clear(&merge_rr
, 1);
1301 if (read_basic_state(&options
))
1303 ropts
.oid
= &options
.orig_head
;
1304 ropts
.branch
= options
.head_name
;
1305 ropts
.flags
= RESET_HEAD_HARD
;
1306 ropts
.default_reflog_action
= DEFAULT_REFLOG_ACTION
;
1307 if (reset_head(the_repository
, &ropts
) < 0)
1308 die(_("could not move back to %s"),
1309 oid_to_hex(&options
.orig_head
));
1310 remove_branch_state(the_repository
, 0);
1311 ret
= finish_rebase(&options
);
1315 save_autostash(state_dir_path("autostash", &options
));
1316 if (options
.type
== REBASE_MERGE
) {
1317 struct replay_opts replay
= REPLAY_OPTS_INIT
;
1319 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
1320 ret
= sequencer_remove_state(&replay
);
1323 strbuf_addstr(&buf
, options
.state_dir
);
1324 ret
= remove_dir_recursively(&buf
, 0);
1326 error(_("could not remove '%s'"),
1331 case ACTION_EDIT_TODO
:
1332 options
.action
= "edit-todo";
1333 options
.dont_finish_rebase
= 1;
1335 case ACTION_SHOW_CURRENT_PATCH
:
1336 options
.action
= "show-current-patch";
1337 options
.dont_finish_rebase
= 1;
1342 BUG("action: %d", action
);
1345 /* Make sure no rebase is in progress */
1347 const char *last_slash
= strrchr(options
.state_dir
, '/');
1348 const char *state_dir_base
=
1349 last_slash
? last_slash
+ 1 : options
.state_dir
;
1350 const char *cmd_live_rebase
=
1351 "git rebase (--continue | --abort | --skip)";
1353 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
1354 die(_("It seems that there is already a %s directory, and\n"
1355 "I wonder if you are in the middle of another rebase. "
1357 "case, please try\n\t%s\n"
1358 "If that is not the case, please\n\t%s\n"
1359 "and run me again. I am stopping in case you still "
1361 "valuable there.\n"),
1362 state_dir_base
, cmd_live_rebase
, buf
.buf
);
1365 if ((options
.flags
& REBASE_INTERACTIVE_EXPLICIT
) ||
1366 (action
!= ACTION_NONE
) ||
1368 options
.autosquash
) {
1369 allow_preemptive_ff
= 0;
1371 if (options
.committer_date_is_author_date
|| options
.ignore_date
)
1372 options
.flags
|= REBASE_FORCE
;
1374 for (i
= 0; i
< options
.git_am_opts
.nr
; i
++) {
1375 const char *option
= options
.git_am_opts
.v
[i
], *p
;
1376 if (!strcmp(option
, "--whitespace=fix") ||
1377 !strcmp(option
, "--whitespace=strip"))
1378 allow_preemptive_ff
= 0;
1379 else if (skip_prefix(option
, "-C", &p
)) {
1381 if (!isdigit(*(p
++)))
1382 die(_("switch `C' expects a "
1383 "numerical value"));
1384 } else if (skip_prefix(option
, "--whitespace=", &p
)) {
1385 if (*p
&& strcmp(p
, "warn") && strcmp(p
, "nowarn") &&
1386 strcmp(p
, "error") && strcmp(p
, "error-all"))
1387 die("Invalid whitespace option: '%s'", p
);
1391 for (i
= 0; i
< exec
.nr
; i
++)
1392 if (check_exec_cmd(exec
.items
[i
].string
))
1395 if (!(options
.flags
& REBASE_NO_QUIET
))
1396 strvec_push(&options
.git_am_opts
, "-q");
1398 if (options
.empty
!= EMPTY_UNSPECIFIED
)
1399 imply_merge(&options
, "--empty");
1401 if (options
.reapply_cherry_picks
)
1402 imply_merge(&options
, "--reapply-cherry-picks");
1405 options
.gpg_sign_opt
= xstrfmt("-S%s", gpg_sign
);
1410 imply_merge(&options
, "--exec");
1413 for (i
= 0; i
< exec
.nr
; i
++)
1414 strbuf_addf(&buf
, "exec %s\n", exec
.items
[i
].string
);
1415 options
.cmd
= xstrdup(buf
.buf
);
1418 if (rebase_merges
) {
1419 if (!*rebase_merges
)
1420 ; /* default mode; do nothing */
1421 else if (!strcmp("rebase-cousins", rebase_merges
))
1422 options
.rebase_cousins
= 1;
1423 else if (strcmp("no-rebase-cousins", rebase_merges
))
1424 die(_("Unknown mode: %s"), rebase_merges
);
1425 options
.rebase_merges
= 1;
1426 imply_merge(&options
, "--rebase-merges");
1429 if (options
.type
== REBASE_APPLY
) {
1430 if (ignore_whitespace
)
1431 strvec_push(&options
.git_am_opts
,
1432 "--ignore-whitespace");
1433 if (options
.committer_date_is_author_date
)
1434 strvec_push(&options
.git_am_opts
,
1435 "--committer-date-is-author-date");
1436 if (options
.ignore_date
)
1437 strvec_push(&options
.git_am_opts
, "--ignore-date");
1440 if (ignore_whitespace
) {
1441 string_list_append(&strategy_options
,
1442 "ignore-space-change");
1446 if (strategy_options
.nr
) {
1449 if (!options
.strategy
)
1450 options
.strategy
= "ort";
1453 for (i
= 0; i
< strategy_options
.nr
; i
++)
1454 strbuf_addf(&buf
, " --%s",
1455 strategy_options
.items
[i
].string
);
1456 options
.strategy_opts
= xstrdup(buf
.buf
);
1459 if (options
.strategy
) {
1460 options
.strategy
= xstrdup(options
.strategy
);
1461 switch (options
.type
) {
1463 die(_("--strategy requires --merge or --interactive"));
1467 case REBASE_UNSPECIFIED
:
1468 options
.type
= REBASE_MERGE
;
1471 BUG("unhandled rebase type (%d)", options
.type
);
1475 if (options
.type
== REBASE_MERGE
)
1476 imply_merge(&options
, "--merge");
1478 if (options
.root
&& !options
.onto_name
)
1479 imply_merge(&options
, "--root without --onto");
1481 if (isatty(2) && options
.flags
& REBASE_NO_QUIET
)
1482 strbuf_addstr(&options
.git_format_patch_opt
, " --progress");
1484 if (options
.git_am_opts
.nr
|| options
.type
== REBASE_APPLY
) {
1485 /* all am options except -q are compatible only with --apply */
1486 for (i
= options
.git_am_opts
.nr
- 1; i
>= 0; i
--)
1487 if (strcmp(options
.git_am_opts
.v
[i
], "-q"))
1491 if (is_merge(&options
))
1492 die(_("apply options and merge options "
1493 "cannot be used together"));
1495 options
.type
= REBASE_APPLY
;
1499 if (options
.type
== REBASE_UNSPECIFIED
) {
1500 if (!strcmp(options
.default_backend
, "merge"))
1501 imply_merge(&options
, "--merge");
1502 else if (!strcmp(options
.default_backend
, "apply"))
1503 options
.type
= REBASE_APPLY
;
1505 die(_("Unknown rebase backend: %s"),
1506 options
.default_backend
);
1509 if (options
.type
== REBASE_MERGE
&&
1510 !options
.strategy
&&
1511 getenv("GIT_TEST_MERGE_ALGORITHM"))
1512 options
.strategy
= xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
1514 switch (options
.type
) {
1516 options
.state_dir
= merge_dir();
1519 options
.state_dir
= apply_dir();
1522 BUG("options.type was just set above; should be unreachable.");
1525 if (options
.empty
== EMPTY_UNSPECIFIED
) {
1526 if (options
.flags
& REBASE_INTERACTIVE_EXPLICIT
)
1527 options
.empty
= EMPTY_ASK
;
1528 else if (exec
.nr
> 0)
1529 options
.empty
= EMPTY_KEEP
;
1531 options
.empty
= EMPTY_DROP
;
1533 if (reschedule_failed_exec
> 0 && !is_merge(&options
))
1534 die(_("--reschedule-failed-exec requires "
1535 "--exec or --interactive"));
1536 if (reschedule_failed_exec
>= 0)
1537 options
.reschedule_failed_exec
= reschedule_failed_exec
;
1539 if (options
.signoff
) {
1540 strvec_push(&options
.git_am_opts
, "--signoff");
1541 options
.flags
|= REBASE_FORCE
;
1544 if (!options
.root
) {
1546 struct branch
*branch
;
1548 branch
= branch_get(NULL
);
1549 options
.upstream_name
= branch_get_upstream(branch
,
1551 if (!options
.upstream_name
)
1552 error_on_missing_default_upstream();
1553 if (options
.fork_point
< 0)
1554 options
.fork_point
= 1;
1556 options
.upstream_name
= argv
[0];
1559 if (!strcmp(options
.upstream_name
, "-"))
1560 options
.upstream_name
= "@{-1}";
1563 lookup_commit_reference_by_name(options
.upstream_name
);
1564 if (!options
.upstream
)
1565 die(_("invalid upstream '%s'"), options
.upstream_name
);
1566 options
.upstream_arg
= options
.upstream_name
;
1568 if (!options
.onto_name
) {
1569 if (commit_tree("", 0, the_hash_algo
->empty_tree
, NULL
,
1570 &squash_onto
, NULL
, NULL
) < 0)
1571 die(_("Could not create new root commit"));
1572 options
.squash_onto
= &squash_onto
;
1573 options
.onto_name
= squash_onto_name
=
1574 xstrdup(oid_to_hex(&squash_onto
));
1576 options
.root_with_onto
= 1;
1578 options
.upstream_name
= NULL
;
1579 options
.upstream
= NULL
;
1581 usage_with_options(builtin_rebase_usage
,
1582 builtin_rebase_options
);
1583 options
.upstream_arg
= "--root";
1586 /* Make sure the branch to rebase onto is valid. */
1589 strbuf_addstr(&buf
, options
.upstream_name
);
1590 strbuf_addstr(&buf
, "...");
1591 options
.onto_name
= xstrdup(buf
.buf
);
1592 } else if (!options
.onto_name
)
1593 options
.onto_name
= options
.upstream_name
;
1594 if (strstr(options
.onto_name
, "...")) {
1595 if (get_oid_mb(options
.onto_name
, &merge_base
) < 0) {
1597 die(_("'%s': need exactly one merge base with branch"),
1598 options
.upstream_name
);
1600 die(_("'%s': need exactly one merge base"),
1603 options
.onto
= lookup_commit_or_die(&merge_base
,
1607 lookup_commit_reference_by_name(options
.onto_name
);
1609 die(_("Does not point to a valid commit '%s'"),
1614 * If the branch to rebase is given, that is the branch we will rebase
1615 * branch_name -- branch/commit being rebased, or
1616 * HEAD (already detached)
1617 * orig_head -- commit object name of tip of the branch before rebasing
1618 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1621 /* Is it "rebase other branchname" or "rebase other commit"? */
1622 branch_name
= argv
[0];
1623 options
.switch_to
= argv
[0];
1625 /* Is it a local branch? */
1627 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
1628 if (!read_ref(buf
.buf
, &options
.orig_head
)) {
1629 die_if_checked_out(buf
.buf
, 1);
1630 options
.head_name
= xstrdup(buf
.buf
);
1631 /* If not is it a valid ref (branch or commit)? */
1633 struct commit
*commit
=
1634 lookup_commit_reference_by_name(branch_name
);
1636 die(_("no such branch/commit '%s'"),
1638 oidcpy(&options
.orig_head
, &commit
->object
.oid
);
1639 options
.head_name
= NULL
;
1641 } else if (argc
== 0) {
1642 /* Do not need to switch branches, we are already on it. */
1644 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL
,
1646 if (!options
.head_name
)
1647 die(_("No such ref: %s"), "HEAD");
1648 if (flags
& REF_ISSYMREF
) {
1649 if (!skip_prefix(options
.head_name
,
1650 "refs/heads/", &branch_name
))
1651 branch_name
= options
.head_name
;
1654 FREE_AND_NULL(options
.head_name
);
1655 branch_name
= "HEAD";
1657 if (get_oid("HEAD", &options
.orig_head
))
1658 die(_("Could not resolve HEAD to a revision"));
1660 BUG("unexpected number of arguments left to parse");
1662 if (options
.fork_point
> 0) {
1663 struct commit
*head
=
1664 lookup_commit_reference(the_repository
,
1665 &options
.orig_head
);
1666 options
.restrict_revision
=
1667 get_fork_point(options
.upstream_name
, head
);
1670 if (repo_read_index(the_repository
) < 0)
1671 die(_("could not read index"));
1673 if (options
.autostash
)
1674 create_autostash(the_repository
,
1675 state_dir_path("autostash", &options
));
1678 if (require_clean_work_tree(the_repository
, "rebase",
1679 _("Please commit or stash them."), 1, 1)) {
1685 * Now we are rebasing commits upstream..orig_head (or with --root,
1686 * everything leading up to orig_head) on top of onto.
1690 * Check if we are already based on onto with linear history,
1691 * in which case we could fast-forward without replacing the commits
1692 * with new commits recreated by replaying their changes.
1694 * Note that can_fast_forward() initializes merge_base, so we have to
1695 * call it before checking allow_preemptive_ff.
1697 if (can_fast_forward(options
.onto
, options
.upstream
, options
.restrict_revision
,
1698 &options
.orig_head
, &merge_base
) &&
1699 allow_preemptive_ff
) {
1702 if (!(options
.flags
& REBASE_FORCE
)) {
1703 /* Lazily switch to the target branch if needed... */
1704 if (options
.switch_to
) {
1705 ret
= checkout_up_to_date(&options
);
1710 if (!(options
.flags
& REBASE_NO_QUIET
))
1712 else if (!strcmp(branch_name
, "HEAD") &&
1713 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1714 puts(_("HEAD is up to date."));
1716 printf(_("Current branch %s is up to date.\n"),
1718 ret
= finish_rebase(&options
);
1720 } else if (!(options
.flags
& REBASE_NO_QUIET
))
1722 else if (!strcmp(branch_name
, "HEAD") &&
1723 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1724 puts(_("HEAD is up to date, rebase forced."));
1726 printf(_("Current branch %s is up to date, rebase "
1727 "forced.\n"), branch_name
);
1730 /* If a hook exists, give it a chance to interrupt*/
1731 if (!ok_to_skip_pre_rebase
&&
1732 run_hooks_l("pre-rebase", options
.upstream_arg
,
1733 argc
? argv
[0] : NULL
, NULL
))
1734 die(_("The pre-rebase hook refused to rebase."));
1736 if (options
.flags
& REBASE_DIFFSTAT
) {
1737 struct diff_options opts
;
1739 if (options
.flags
& REBASE_VERBOSE
) {
1740 if (is_null_oid(&merge_base
))
1741 printf(_("Changes to %s:\n"),
1742 oid_to_hex(&options
.onto
->object
.oid
));
1744 printf(_("Changes from %s to %s:\n"),
1745 oid_to_hex(&merge_base
),
1746 oid_to_hex(&options
.onto
->object
.oid
));
1749 /* We want color (if set), but no pager */
1751 opts
.stat_width
= -1; /* use full terminal width */
1752 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
1753 opts
.output_format
|=
1754 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1755 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1756 diff_setup_done(&opts
);
1757 diff_tree_oid(is_null_oid(&merge_base
) ?
1758 the_hash_algo
->empty_tree
: &merge_base
,
1759 &options
.onto
->object
.oid
, "", &opts
);
1760 diffcore_std(&opts
);
1764 if (is_merge(&options
))
1767 /* Detach HEAD and reset the tree */
1768 if (options
.flags
& REBASE_NO_QUIET
)
1769 printf(_("First, rewinding head to replay your work on top of "
1772 strbuf_addf(&msg
, "%s: checkout %s",
1773 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
), options
.onto_name
);
1774 ropts
.oid
= &options
.onto
->object
.oid
;
1775 ropts
.orig_head
= &options
.orig_head
,
1776 ropts
.flags
= RESET_HEAD_DETACH
| RESET_ORIG_HEAD
|
1777 RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
1778 ropts
.head_msg
= msg
.buf
;
1779 ropts
.default_reflog_action
= DEFAULT_REFLOG_ACTION
;
1780 if (reset_head(the_repository
, &ropts
))
1781 die(_("Could not detach HEAD"));
1782 strbuf_release(&msg
);
1785 * If the onto is a proper descendant of the tip of the branch, then
1786 * we just fast-forwarded.
1789 if (oideq(&merge_base
, &options
.orig_head
)) {
1790 printf(_("Fast-forwarded %s to %s.\n"),
1791 branch_name
, options
.onto_name
);
1792 strbuf_addf(&msg
, "rebase finished: %s onto %s",
1793 options
.head_name
? options
.head_name
: "detached HEAD",
1794 oid_to_hex(&options
.onto
->object
.oid
));
1795 memset(&ropts
, 0, sizeof(ropts
));
1796 ropts
.branch
= options
.head_name
;
1797 ropts
.flags
= RESET_HEAD_REFS_ONLY
;
1798 ropts
.head_msg
= msg
.buf
;
1799 reset_head(the_repository
, &ropts
);
1800 strbuf_release(&msg
);
1801 ret
= finish_rebase(&options
);
1805 strbuf_addf(&revisions
, "%s..%s",
1806 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
1807 (options
.restrict_revision
?
1808 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
1809 oid_to_hex(&options
.upstream
->object
.oid
)),
1810 oid_to_hex(&options
.orig_head
));
1812 options
.revisions
= revisions
.buf
;
1815 ret
= run_specific_rebase(&options
, action
);
1818 strbuf_release(&buf
);
1819 strbuf_release(&revisions
);
1820 free(options
.head_name
);
1821 free(options
.gpg_sign_opt
);
1823 free(options
.strategy
);
1824 strbuf_release(&options
.git_format_patch_opt
);
1825 free(squash_onto_name
);