2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
7 #define USE_THE_REPOSITORY_VARIABLE
8 #define DISABLE_SIGN_COMPARE_WARNINGS
13 #include "environment.h"
16 #include "run-command.h"
21 #include "unpack-trees.h"
23 #include "object-file.h"
24 #include "object-name.h"
25 #include "parse-options.h"
29 #include "wt-status.h"
31 #include "commit-reach.h"
34 #include "sequencer.h"
35 #include "rebase-interactive.h"
40 static char const * const builtin_rebase_usage
[] = {
41 N_("git rebase [-i] [options] [--exec <cmd>] "
42 "[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
43 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
45 "git rebase --continue | --abort | --skip | --edit-todo",
49 static GIT_PATH_FUNC(path_squash_onto
, "rebase-merge/squash-onto")
50 static GIT_PATH_FUNC(path_interactive
, "rebase-merge/interactive")
51 static GIT_PATH_FUNC(apply_dir
, "rebase-apply")
52 static GIT_PATH_FUNC(merge_dir
, "rebase-merge")
55 REBASE_UNSPECIFIED
= -1,
61 EMPTY_UNSPECIFIED
= -1,
74 ACTION_SHOW_CURRENT_PATCH
77 static const char *action_names
[] = {
87 struct rebase_options
{
88 enum rebase_type type
;
89 enum empty_type empty
;
90 char *default_backend
;
91 const char *state_dir
;
92 struct commit
*upstream
;
93 const char *upstream_name
;
94 const char *upstream_arg
;
96 struct commit
*orig_head
;
98 const char *onto_name
;
99 const char *revisions
;
100 const char *switch_to
;
101 int root
, root_with_onto
;
102 struct object_id
*squash_onto
;
103 struct commit
*restrict_revision
;
104 int dont_finish_rebase
;
106 REBASE_NO_QUIET
= 1<<0,
107 REBASE_VERBOSE
= 1<<1,
108 REBASE_DIFFSTAT
= 1<<2,
110 REBASE_INTERACTIVE_EXPLICIT
= 1<<4,
112 struct strvec git_am_opts
;
116 int allow_rerere_autoupdate
;
121 int committer_date_is_author_date
;
123 struct string_list exec
;
124 int allow_empty_message
;
125 int rebase_merges
, rebase_cousins
;
127 struct string_list strategy_opts
;
128 struct strbuf git_format_patch_opt
;
129 int reschedule_failed_exec
;
130 int reapply_cherry_picks
;
133 int config_autosquash
;
134 int config_rebase_merges
;
135 int config_update_refs
;
138 #define REBASE_OPTIONS_INIT { \
139 .type = REBASE_UNSPECIFIED, \
140 .empty = EMPTY_UNSPECIFIED, \
142 .default_backend = xstrdup("merge"), \
143 .flags = REBASE_NO_QUIET, \
144 .git_am_opts = STRVEC_INIT, \
145 .exec = STRING_LIST_INIT_NODUP, \
146 .git_format_patch_opt = STRBUF_INIT, \
148 .reapply_cherry_picks = -1, \
149 .allow_empty_message = 1, \
151 .rebase_merges = -1, \
152 .config_rebase_merges = -1, \
154 .config_update_refs = -1, \
155 .strategy_opts = STRING_LIST_INIT_NODUP,\
158 static void rebase_options_release(struct rebase_options
*opts
)
160 free(opts
->default_backend
);
161 free(opts
->reflog_action
);
162 free(opts
->head_name
);
163 strvec_clear(&opts
->git_am_opts
);
164 free(opts
->gpg_sign_opt
);
165 string_list_clear(&opts
->exec
, 0);
166 free(opts
->strategy
);
167 string_list_clear(&opts
->strategy_opts
, 0);
168 strbuf_release(&opts
->git_format_patch_opt
);
171 static struct replay_opts
get_replay_opts(const struct rebase_options
*opts
)
173 struct replay_opts replay
= REPLAY_OPTS_INIT
;
175 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
176 replay
.strategy
= NULL
;
177 sequencer_init_config(&replay
);
179 replay
.signoff
= opts
->signoff
;
180 replay
.allow_ff
= !(opts
->flags
& REBASE_FORCE
);
181 if (opts
->allow_rerere_autoupdate
)
182 replay
.allow_rerere_auto
= opts
->allow_rerere_autoupdate
;
183 replay
.allow_empty
= 1;
184 replay
.allow_empty_message
= opts
->allow_empty_message
;
185 replay
.drop_redundant_commits
= (opts
->empty
== EMPTY_DROP
);
186 replay
.keep_redundant_commits
= (opts
->empty
== EMPTY_KEEP
);
187 replay
.quiet
= !(opts
->flags
& REBASE_NO_QUIET
);
188 replay
.verbose
= opts
->flags
& REBASE_VERBOSE
;
189 replay
.reschedule_failed_exec
= opts
->reschedule_failed_exec
;
190 replay
.committer_date_is_author_date
=
191 opts
->committer_date_is_author_date
;
192 replay
.ignore_date
= opts
->ignore_date
;
193 free(replay
.gpg_sign
);
194 replay
.gpg_sign
= xstrdup_or_null(opts
->gpg_sign_opt
);
195 replay
.reflog_action
= xstrdup(opts
->reflog_action
);
197 replay
.strategy
= xstrdup_or_null(opts
->strategy
);
198 else if (!replay
.strategy
&& replay
.default_strategy
) {
199 replay
.strategy
= replay
.default_strategy
;
200 replay
.default_strategy
= NULL
;
203 for (size_t i
= 0; i
< opts
->strategy_opts
.nr
; i
++)
204 strvec_push(&replay
.xopts
, opts
->strategy_opts
.items
[i
].string
);
206 if (opts
->squash_onto
) {
207 oidcpy(&replay
.squash_onto
, opts
->squash_onto
);
208 replay
.have_squash_onto
= 1;
214 static int edit_todo_file(unsigned flags
, struct replay_opts
*opts
)
216 const char *todo_file
= rebase_path_todo();
217 struct todo_list todo_list
= TODO_LIST_INIT
,
218 new_todo
= TODO_LIST_INIT
;
221 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
222 return error_errno(_("could not read '%s'."), todo_file
);
224 strbuf_stripspace(&todo_list
.buf
, comment_line_str
);
225 res
= edit_todo_list(the_repository
, opts
, &todo_list
, &new_todo
,
227 if (!res
&& todo_list_write_to_file(the_repository
, &new_todo
, todo_file
,
228 NULL
, NULL
, -1, flags
& ~(TODO_LIST_SHORTEN_IDS
)))
229 res
= error_errno(_("could not write '%s'"), todo_file
);
231 todo_list_release(&todo_list
);
232 todo_list_release(&new_todo
);
237 static int get_revision_ranges(struct commit
*upstream
, struct commit
*onto
,
238 struct object_id
*orig_head
, char **revisions
,
239 char **shortrevisions
)
241 struct commit
*base_rev
= upstream
? upstream
: onto
;
242 const char *shorthead
;
244 *revisions
= xstrfmt("%s...%s", oid_to_hex(&base_rev
->object
.oid
),
245 oid_to_hex(orig_head
));
247 shorthead
= repo_find_unique_abbrev(the_repository
, orig_head
,
251 const char *shortrev
;
253 shortrev
= repo_find_unique_abbrev(the_repository
,
254 &base_rev
->object
.oid
,
257 *shortrevisions
= xstrfmt("%s..%s", shortrev
, shorthead
);
259 *shortrevisions
= xstrdup(shorthead
);
264 static int init_basic_state(struct replay_opts
*opts
, const char *head_name
,
266 const struct object_id
*orig_head
)
270 if (!is_directory(merge_dir()) &&
271 safe_create_dir_in_gitdir(the_repository
, merge_dir()))
272 return error_errno(_("could not create temporary %s"), merge_dir());
274 refs_delete_reflog(get_main_ref_store(the_repository
), "REBASE_HEAD");
276 interactive
= fopen(path_interactive(), "w");
278 return error_errno(_("could not mark as interactive"));
281 return write_basic_state(opts
, head_name
, onto
, orig_head
);
284 static int do_interactive_rebase(struct rebase_options
*opts
, unsigned flags
)
287 char *revisions
= NULL
, *shortrevisions
= NULL
;
288 struct strvec make_script_args
= STRVEC_INIT
;
289 struct todo_list todo_list
= TODO_LIST_INIT
;
290 struct replay_opts replay
= get_replay_opts(opts
);
292 if (get_revision_ranges(opts
->upstream
, opts
->onto
, &opts
->orig_head
->object
.oid
,
293 &revisions
, &shortrevisions
))
296 if (init_basic_state(&replay
,
297 opts
->head_name
? opts
->head_name
: "detached HEAD",
298 opts
->onto
, &opts
->orig_head
->object
.oid
))
301 if (!opts
->upstream
&& opts
->squash_onto
)
302 write_file(path_squash_onto(), "%s\n",
303 oid_to_hex(opts
->squash_onto
));
305 strvec_pushl(&make_script_args
, "", revisions
, NULL
);
306 if (opts
->restrict_revision
)
307 strvec_pushf(&make_script_args
, "^%s",
308 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
310 ret
= sequencer_make_script(the_repository
, &todo_list
.buf
,
311 make_script_args
.nr
, make_script_args
.v
,
315 error(_("could not generate todo list"));
317 discard_index(the_repository
->index
);
318 if (todo_list_parse_insn_buffer(the_repository
, &replay
,
319 todo_list
.buf
.buf
, &todo_list
))
320 BUG("unusable todo list");
322 ret
= complete_action(the_repository
, &replay
, flags
,
323 shortrevisions
, opts
->onto_name
, opts
->onto
,
324 &opts
->orig_head
->object
.oid
, &opts
->exec
,
325 opts
->autosquash
, opts
->update_refs
, &todo_list
);
329 replay_opts_release(&replay
);
331 free(shortrevisions
);
332 todo_list_release(&todo_list
);
333 strvec_clear(&make_script_args
);
338 static int run_sequencer_rebase(struct rebase_options
*opts
)
341 int abbreviate_commands
= 0, ret
= 0;
343 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands
);
345 flags
|= opts
->keep_empty
? TODO_LIST_KEEP_EMPTY
: 0;
346 flags
|= abbreviate_commands
? TODO_LIST_ABBREVIATE_CMDS
: 0;
347 flags
|= opts
->rebase_merges
? TODO_LIST_REBASE_MERGES
: 0;
348 flags
|= opts
->rebase_cousins
> 0 ? TODO_LIST_REBASE_COUSINS
: 0;
349 flags
|= opts
->root_with_onto
? TODO_LIST_ROOT_WITH_ONTO
: 0;
350 flags
|= opts
->reapply_cherry_picks
? TODO_LIST_REAPPLY_CHERRY_PICKS
: 0;
351 flags
|= opts
->flags
& REBASE_NO_QUIET
? TODO_LIST_WARN_SKIPPED_CHERRY_PICKS
: 0;
353 switch (opts
->action
) {
355 if (!opts
->onto
&& !opts
->upstream
)
356 die(_("a base commit must be provided with --upstream or --onto"));
358 ret
= do_interactive_rebase(opts
, flags
);
362 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
364 rerere_clear(the_repository
, &merge_rr
);
367 case ACTION_CONTINUE
: {
368 struct replay_opts replay_opts
= get_replay_opts(opts
);
370 ret
= sequencer_continue(the_repository
, &replay_opts
);
371 replay_opts_release(&replay_opts
);
374 case ACTION_EDIT_TODO
: {
375 struct replay_opts replay_opts
= get_replay_opts(opts
);
377 ret
= edit_todo_file(flags
, &replay_opts
);
378 replay_opts_release(&replay_opts
);
381 case ACTION_SHOW_CURRENT_PATCH
: {
382 struct child_process cmd
= CHILD_PROCESS_INIT
;
385 strvec_pushl(&cmd
.args
, "show", "REBASE_HEAD", "--", NULL
);
386 ret
= run_command(&cmd
);
391 BUG("invalid command '%d'", opts
->action
);
397 static int is_merge(struct rebase_options
*opts
)
399 return opts
->type
== REBASE_MERGE
;
402 static void imply_merge(struct rebase_options
*opts
, const char *option
)
404 switch (opts
->type
) {
406 die(_("%s requires the merge backend"), option
);
411 opts
->type
= REBASE_MERGE
; /* implied */
416 /* Returns the filename prefixed by the state_dir */
417 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
419 static struct strbuf path
= STRBUF_INIT
;
420 static size_t prefix_len
;
423 strbuf_addf(&path
, "%s/", opts
->state_dir
);
424 prefix_len
= path
.len
;
427 strbuf_setlen(&path
, prefix_len
);
428 strbuf_addstr(&path
, filename
);
432 /* Initialize the rebase options from the state directory. */
433 static int read_basic_state(struct rebase_options
*opts
)
435 struct strbuf head_name
= STRBUF_INIT
;
436 struct strbuf buf
= STRBUF_INIT
;
437 struct object_id oid
;
439 if (!read_oneliner(&head_name
, state_dir_path("head-name", opts
),
440 READ_ONELINER_WARN_MISSING
) ||
441 !read_oneliner(&buf
, state_dir_path("onto", opts
),
442 READ_ONELINER_WARN_MISSING
))
444 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
445 xstrdup(head_name
.buf
) : NULL
;
446 strbuf_release(&head_name
);
447 if (get_oid_hex(buf
.buf
, &oid
) ||
448 !(opts
->onto
= lookup_commit_object(the_repository
, &oid
)))
449 return error(_("invalid onto: '%s'"), buf
.buf
);
452 * We always write to orig-head, but interactive rebase used to write to
453 * head. Fall back to reading from head to cover for the case that the
454 * user upgraded git with an ongoing interactive rebase.
457 if (file_exists(state_dir_path("orig-head", opts
))) {
458 if (!read_oneliner(&buf
, state_dir_path("orig-head", opts
),
459 READ_ONELINER_WARN_MISSING
))
461 } else if (!read_oneliner(&buf
, state_dir_path("head", opts
),
462 READ_ONELINER_WARN_MISSING
))
464 if (get_oid_hex(buf
.buf
, &oid
) ||
465 !(opts
->orig_head
= lookup_commit_object(the_repository
, &oid
)))
466 return error(_("invalid orig-head: '%s'"), buf
.buf
);
468 if (file_exists(state_dir_path("quiet", opts
)))
469 opts
->flags
&= ~REBASE_NO_QUIET
;
471 opts
->flags
|= REBASE_NO_QUIET
;
473 if (file_exists(state_dir_path("verbose", opts
)))
474 opts
->flags
|= REBASE_VERBOSE
;
476 if (file_exists(state_dir_path("signoff", opts
))) {
478 opts
->flags
|= REBASE_FORCE
;
481 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts
))) {
483 if (!read_oneliner(&buf
, state_dir_path("allow_rerere_autoupdate", opts
),
484 READ_ONELINER_WARN_MISSING
))
486 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
487 opts
->allow_rerere_autoupdate
= RERERE_AUTOUPDATE
;
488 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
489 opts
->allow_rerere_autoupdate
= RERERE_NOAUTOUPDATE
;
491 warning(_("ignoring invalid allow_rerere_autoupdate: "
495 if (file_exists(state_dir_path("gpg_sign_opt", opts
))) {
497 if (!read_oneliner(&buf
, state_dir_path("gpg_sign_opt", opts
),
498 READ_ONELINER_WARN_MISSING
))
500 free(opts
->gpg_sign_opt
);
501 opts
->gpg_sign_opt
= xstrdup(buf
.buf
);
504 strbuf_release(&buf
);
509 static int rebase_write_basic_state(struct rebase_options
*opts
)
511 write_file(state_dir_path("head-name", opts
), "%s",
512 opts
->head_name
? opts
->head_name
: "detached HEAD");
513 write_file(state_dir_path("onto", opts
), "%s",
514 opts
->onto
? oid_to_hex(&opts
->onto
->object
.oid
) : "");
515 write_file(state_dir_path("orig-head", opts
), "%s",
516 oid_to_hex(&opts
->orig_head
->object
.oid
));
517 if (!(opts
->flags
& REBASE_NO_QUIET
))
518 write_file(state_dir_path("quiet", opts
), "%s", "");
519 if (opts
->flags
& REBASE_VERBOSE
)
520 write_file(state_dir_path("verbose", opts
), "%s", "");
521 if (opts
->allow_rerere_autoupdate
> 0)
522 write_file(state_dir_path("allow_rerere_autoupdate", opts
),
523 "-%s-rerere-autoupdate",
524 opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
?
526 if (opts
->gpg_sign_opt
)
527 write_file(state_dir_path("gpg_sign_opt", opts
), "%s",
530 write_file(state_dir_path("signoff", opts
), "--signoff");
535 static int cleanup_autostash(struct rebase_options
*opts
)
538 struct strbuf dir
= STRBUF_INIT
;
539 const char *path
= state_dir_path("autostash", opts
);
541 if (!file_exists(path
))
543 ret
= apply_autostash(path
);
544 strbuf_addstr(&dir
, opts
->state_dir
);
545 if (remove_dir_recursively(&dir
, 0))
546 ret
= error_errno(_("could not remove '%s'"), opts
->state_dir
);
547 strbuf_release(&dir
);
552 static int finish_rebase(struct rebase_options
*opts
)
554 struct strbuf dir
= STRBUF_INIT
;
557 refs_delete_ref(get_main_ref_store(the_repository
), NULL
,
558 "REBASE_HEAD", NULL
, REF_NO_DEREF
);
559 refs_delete_ref(get_main_ref_store(the_repository
), NULL
,
560 "AUTO_MERGE", NULL
, REF_NO_DEREF
);
561 apply_autostash(state_dir_path("autostash", opts
));
563 * We ignore errors in 'git maintenance run --auto', since the
564 * user should see them.
566 run_auto_maintenance(!(opts
->flags
& (REBASE_NO_QUIET
|REBASE_VERBOSE
)));
567 if (opts
->type
== REBASE_MERGE
) {
568 struct replay_opts replay
= REPLAY_OPTS_INIT
;
570 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
571 ret
= sequencer_remove_state(&replay
);
572 replay_opts_release(&replay
);
574 strbuf_addstr(&dir
, opts
->state_dir
);
575 if (remove_dir_recursively(&dir
, 0))
576 ret
= error(_("could not remove '%s'"),
578 strbuf_release(&dir
);
584 static int move_to_original_branch(struct rebase_options
*opts
)
586 struct strbuf branch_reflog
= STRBUF_INIT
, head_reflog
= STRBUF_INIT
;
587 struct reset_head_opts ropts
= { 0 };
590 if (!opts
->head_name
)
591 return 0; /* nothing to move back to */
594 BUG("move_to_original_branch without onto");
596 strbuf_addf(&branch_reflog
, "%s (finish): %s onto %s",
598 opts
->head_name
, oid_to_hex(&opts
->onto
->object
.oid
));
599 strbuf_addf(&head_reflog
, "%s (finish): returning to %s",
600 opts
->reflog_action
, opts
->head_name
);
601 ropts
.branch
= opts
->head_name
;
602 ropts
.flags
= RESET_HEAD_REFS_ONLY
;
603 ropts
.branch_msg
= branch_reflog
.buf
;
604 ropts
.head_msg
= head_reflog
.buf
;
605 ret
= reset_head(the_repository
, &ropts
);
607 strbuf_release(&branch_reflog
);
608 strbuf_release(&head_reflog
);
612 static int run_am(struct rebase_options
*opts
)
614 struct child_process am
= CHILD_PROCESS_INIT
;
615 struct child_process format_patch
= CHILD_PROCESS_INIT
;
617 char *rebased_patches
;
620 strvec_push(&am
.args
, "am");
621 strvec_pushf(&am
.env
, GIT_REFLOG_ACTION_ENVIRONMENT
"=%s (pick)",
622 opts
->reflog_action
);
623 if (opts
->action
== ACTION_CONTINUE
) {
624 strvec_push(&am
.args
, "--resolved");
625 strvec_pushf(&am
.args
, "--resolvemsg=%s", rebase_resolvemsg
);
626 if (opts
->gpg_sign_opt
)
627 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
628 status
= run_command(&am
);
632 return move_to_original_branch(opts
);
634 if (opts
->action
== ACTION_SKIP
) {
635 strvec_push(&am
.args
, "--skip");
636 strvec_pushf(&am
.args
, "--resolvemsg=%s", rebase_resolvemsg
);
637 status
= run_command(&am
);
641 return move_to_original_branch(opts
);
643 if (opts
->action
== ACTION_SHOW_CURRENT_PATCH
) {
644 strvec_push(&am
.args
, "--show-current-patch");
645 return run_command(&am
);
648 rebased_patches
= repo_git_path(the_repository
, "rebased-patches");
649 format_patch
.out
= open(rebased_patches
,
650 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
651 if (format_patch
.out
< 0) {
652 status
= error_errno(_("could not open '%s' for writing"),
654 free(rebased_patches
);
655 child_process_clear(&am
);
659 format_patch
.git_cmd
= 1;
660 strvec_pushl(&format_patch
.args
, "format-patch", "-k", "--stdout",
661 "--full-index", "--cherry-pick", "--right-only",
662 "--default-prefix", "--no-renames",
663 "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
665 if (opts
->git_format_patch_opt
.len
)
666 strvec_split(&format_patch
.args
,
667 opts
->git_format_patch_opt
.buf
);
668 strvec_pushf(&format_patch
.args
, "%s...%s",
669 oid_to_hex(opts
->root
?
670 /* this is now equivalent to !opts->upstream */
671 &opts
->onto
->object
.oid
:
672 &opts
->upstream
->object
.oid
),
673 oid_to_hex(&opts
->orig_head
->object
.oid
));
674 if (opts
->restrict_revision
)
675 strvec_pushf(&format_patch
.args
, "^%s",
676 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
678 status
= run_command(&format_patch
);
680 struct reset_head_opts ropts
= { 0 };
681 unlink(rebased_patches
);
682 free(rebased_patches
);
683 child_process_clear(&am
);
685 ropts
.oid
= &opts
->orig_head
->object
.oid
;
686 ropts
.branch
= opts
->head_name
;
687 ropts
.default_reflog_action
= opts
->reflog_action
;
688 reset_head(the_repository
, &ropts
);
689 error(_("\ngit encountered an error while preparing the "
690 "patches to replay\n"
693 "As a result, git cannot rebase them."),
699 am
.in
= open(rebased_patches
, O_RDONLY
);
701 status
= error_errno(_("could not open '%s' for reading"),
703 free(rebased_patches
);
704 child_process_clear(&am
);
708 strvec_pushv(&am
.args
, opts
->git_am_opts
.v
);
709 strvec_push(&am
.args
, "--rebasing");
710 strvec_pushf(&am
.args
, "--resolvemsg=%s", rebase_resolvemsg
);
711 strvec_push(&am
.args
, "--patch-format=mboxrd");
712 if (opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
)
713 strvec_push(&am
.args
, "--rerere-autoupdate");
714 else if (opts
->allow_rerere_autoupdate
== RERERE_NOAUTOUPDATE
)
715 strvec_push(&am
.args
, "--no-rerere-autoupdate");
716 if (opts
->gpg_sign_opt
)
717 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
718 status
= run_command(&am
);
719 unlink(rebased_patches
);
720 free(rebased_patches
);
723 return move_to_original_branch(opts
);
726 if (is_directory(opts
->state_dir
))
727 rebase_write_basic_state(opts
);
732 static int run_specific_rebase(struct rebase_options
*opts
)
736 if (opts
->type
== REBASE_MERGE
) {
737 /* Run sequencer-based rebase */
738 if (!(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
))
739 setenv("GIT_SEQUENCE_EDITOR", ":", 1);
740 if (opts
->gpg_sign_opt
) {
741 /* remove the leading "-S" */
742 char *tmp
= xstrdup(opts
->gpg_sign_opt
+ 2);
743 free(opts
->gpg_sign_opt
);
744 opts
->gpg_sign_opt
= tmp
;
747 status
= run_sequencer_rebase(opts
);
748 } else if (opts
->type
== REBASE_APPLY
)
749 status
= run_am(opts
);
751 BUG("Unhandled rebase type %d", opts
->type
);
753 if (opts
->dont_finish_rebase
)
755 else if (opts
->type
== REBASE_MERGE
)
756 ; /* merge backend cleans up after itself */
757 else if (status
== 0) {
758 if (!file_exists(state_dir_path("stopped-sha", opts
)))
760 } else if (status
== 2) {
761 struct strbuf dir
= STRBUF_INIT
;
763 apply_autostash(state_dir_path("autostash", opts
));
764 strbuf_addstr(&dir
, opts
->state_dir
);
765 remove_dir_recursively(&dir
, 0);
766 strbuf_release(&dir
);
767 die("Nothing to do");
770 return status
? -1 : 0;
773 static void parse_rebase_merges_value(struct rebase_options
*options
, const char *value
)
775 if (!strcmp("no-rebase-cousins", value
))
776 options
->rebase_cousins
= 0;
777 else if (!strcmp("rebase-cousins", value
))
778 options
->rebase_cousins
= 1;
780 die(_("Unknown rebase-merges mode: %s"), value
);
783 static int rebase_config(const char *var
, const char *value
,
784 const struct config_context
*ctx
, void *data
)
786 struct rebase_options
*opts
= data
;
788 if (!strcmp(var
, "rebase.stat")) {
789 if (git_config_bool(var
, value
))
790 opts
->flags
|= REBASE_DIFFSTAT
;
792 opts
->flags
&= ~REBASE_DIFFSTAT
;
796 if (!strcmp(var
, "rebase.autosquash")) {
797 opts
->config_autosquash
= git_config_bool(var
, value
);
801 if (!strcmp(var
, "commit.gpgsign")) {
802 free(opts
->gpg_sign_opt
);
803 opts
->gpg_sign_opt
= git_config_bool(var
, value
) ?
804 xstrdup("-S") : NULL
;
808 if (!strcmp(var
, "rebase.autostash")) {
809 opts
->autostash
= git_config_bool(var
, value
);
813 if (!strcmp(var
, "rebase.rebasemerges")) {
814 opts
->config_rebase_merges
= git_parse_maybe_bool(value
);
815 if (opts
->config_rebase_merges
< 0) {
816 opts
->config_rebase_merges
= 1;
817 parse_rebase_merges_value(opts
, value
);
819 opts
->rebase_cousins
= 0;
824 if (!strcmp(var
, "rebase.updaterefs")) {
825 opts
->config_update_refs
= git_config_bool(var
, value
);
829 if (!strcmp(var
, "rebase.reschedulefailedexec")) {
830 opts
->reschedule_failed_exec
= git_config_bool(var
, value
);
834 if (!strcmp(var
, "rebase.forkpoint")) {
835 opts
->fork_point
= git_config_bool(var
, value
) ? -1 : 0;
839 if (!strcmp(var
, "rebase.backend")) {
840 FREE_AND_NULL(opts
->default_backend
);
841 return git_config_string(&opts
->default_backend
, var
, value
);
844 return git_default_config(var
, value
, ctx
, data
);
847 static int checkout_up_to_date(struct rebase_options
*options
)
849 struct strbuf buf
= STRBUF_INIT
;
850 struct reset_head_opts ropts
= { 0 };
853 strbuf_addf(&buf
, "%s: checkout %s",
854 options
->reflog_action
, options
->switch_to
);
855 ropts
.oid
= &options
->orig_head
->object
.oid
;
856 ropts
.branch
= options
->head_name
;
857 ropts
.flags
= RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
859 ropts
.flags
|= RESET_HEAD_DETACH
;
860 ropts
.head_msg
= buf
.buf
;
861 if (reset_head(the_repository
, &ropts
) < 0)
862 ret
= error(_("could not switch to %s"), options
->switch_to
);
863 strbuf_release(&buf
);
869 * Determines whether the commits in from..to are linear, i.e. contain
870 * no merge commits. This function *expects* `from` to be an ancestor of
873 static int is_linear_history(struct commit
*from
, struct commit
*to
)
875 while (to
&& to
!= from
) {
876 repo_parse_commit(the_repository
, to
);
879 if (to
->parents
->next
)
881 to
= to
->parents
->item
;
886 static int can_fast_forward(struct commit
*onto
, struct commit
*upstream
,
887 struct commit
*restrict_revision
,
888 struct commit
*head
, struct object_id
*branch_base
)
890 struct commit_list
*merge_bases
= NULL
;
893 if (is_null_oid(branch_base
))
894 goto done
; /* fill_branch_base() found multiple merge bases */
896 if (!oideq(branch_base
, &onto
->object
.oid
))
899 if (restrict_revision
&& !oideq(&restrict_revision
->object
.oid
, branch_base
))
905 if (repo_get_merge_bases(the_repository
, upstream
, head
, &merge_bases
) < 0)
907 if (!merge_bases
|| merge_bases
->next
)
910 if (!oideq(&onto
->object
.oid
, &merge_bases
->item
->object
.oid
))
916 free_commit_list(merge_bases
);
917 return res
&& is_linear_history(onto
, head
);
920 static void fill_branch_base(struct rebase_options
*options
,
921 struct object_id
*branch_base
)
923 struct commit_list
*merge_bases
= NULL
;
925 if (repo_get_merge_bases(the_repository
, options
->onto
,
926 options
->orig_head
, &merge_bases
) < 0)
928 if (!merge_bases
|| merge_bases
->next
)
929 oidcpy(branch_base
, null_oid(the_hash_algo
));
931 oidcpy(branch_base
, &merge_bases
->item
->object
.oid
);
933 free_commit_list(merge_bases
);
936 static int parse_opt_am(const struct option
*opt
, const char *arg
, int unset
)
938 struct rebase_options
*opts
= opt
->value
;
940 BUG_ON_OPT_NEG(unset
);
943 if (opts
->type
!= REBASE_UNSPECIFIED
&& opts
->type
!= REBASE_APPLY
)
944 die(_("apply options and merge options cannot be used together"));
946 opts
->type
= REBASE_APPLY
;
951 /* -i followed by -m is still -i */
952 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
954 struct rebase_options
*opts
= opt
->value
;
956 BUG_ON_OPT_NEG(unset
);
959 if (opts
->type
!= REBASE_UNSPECIFIED
&& opts
->type
!= REBASE_MERGE
)
960 die(_("apply options and merge options cannot be used together"));
962 opts
->type
= REBASE_MERGE
;
967 /* -i followed by -r is still explicitly interactive, but -r alone is not */
968 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
971 struct rebase_options
*opts
= opt
->value
;
973 BUG_ON_OPT_NEG(unset
);
976 if (opts
->type
!= REBASE_UNSPECIFIED
&& opts
->type
!= REBASE_MERGE
)
977 die(_("apply options and merge options cannot be used together"));
979 opts
->type
= REBASE_MERGE
;
980 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
985 static enum empty_type
parse_empty_value(const char *value
)
987 if (!strcasecmp(value
, "drop"))
989 else if (!strcasecmp(value
, "keep"))
991 else if (!strcasecmp(value
, "stop"))
993 else if (!strcasecmp(value
, "ask")) {
994 warning(_("--empty=ask is deprecated; use '--empty=stop' instead."));
998 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"stop\"."), value
);
1001 static int parse_opt_keep_empty(const struct option
*opt
, const char *arg
,
1004 struct rebase_options
*opts
= opt
->value
;
1006 BUG_ON_OPT_ARG(arg
);
1008 imply_merge(opts
, unset
? "--no-keep-empty" : "--keep-empty");
1009 opts
->keep_empty
= !unset
;
1013 static int parse_opt_empty(const struct option
*opt
, const char *arg
, int unset
)
1015 struct rebase_options
*options
= opt
->value
;
1016 enum empty_type value
= parse_empty_value(arg
);
1018 BUG_ON_OPT_NEG(unset
);
1020 options
->empty
= value
;
1024 static int parse_opt_rebase_merges(const struct option
*opt
, const char *arg
, int unset
)
1026 struct rebase_options
*options
= opt
->value
;
1028 options
->rebase_merges
= !unset
;
1029 options
->rebase_cousins
= 0;
1033 warning(_("--rebase-merges with an empty string "
1034 "argument is deprecated and will stop "
1035 "working in a future version of Git. Use "
1036 "--rebase-merges without an argument "
1037 "instead, which does the same thing."));
1040 parse_rebase_merges_value(options
, arg
);
1046 static void NORETURN
error_on_missing_default_upstream(void)
1048 struct branch
*current_branch
= branch_get(NULL
);
1051 "Please specify which branch you want to rebase against.\n"
1052 "See git-rebase(1) for details.\n"
1054 " git rebase '<branch>'\n"
1056 current_branch
? _("There is no tracking information for "
1057 "the current branch.") :
1058 _("You are not currently on a branch."));
1060 if (current_branch
) {
1061 const char *remote
= current_branch
->remote_name
;
1064 remote
= _("<remote>");
1066 printf(_("If you wish to set tracking information for this "
1067 "branch you can do so with:\n"
1069 " git branch --set-upstream-to=%s/<branch> %s\n"
1071 remote
, current_branch
->name
);
1076 static int check_exec_cmd(const char *cmd
)
1078 if (strchr(cmd
, '\n'))
1079 return error(_("exec commands cannot contain newlines"));
1081 /* Does the command consist purely of whitespace? */
1082 if (!cmd
[strspn(cmd
, " \t\r\f\v")])
1083 return error(_("empty exec command"));
1088 int cmd_rebase(int argc
,
1091 struct repository
*repo UNUSED
)
1093 struct rebase_options options
= REBASE_OPTIONS_INIT
;
1094 const char *branch_name
;
1095 const char *strategy_opt
= NULL
;
1096 int ret
, flags
, total_argc
, in_progress
= 0;
1098 int ok_to_skip_pre_rebase
= 0;
1099 struct strbuf msg
= STRBUF_INIT
;
1100 struct strbuf revisions
= STRBUF_INIT
;
1101 struct strbuf buf
= STRBUF_INIT
;
1102 struct object_id branch_base
;
1103 int ignore_whitespace
= 0;
1104 const char *gpg_sign
= NULL
;
1105 struct object_id squash_onto
;
1106 char *squash_onto_name
= NULL
;
1107 char *keep_base_onto_name
= NULL
;
1108 int reschedule_failed_exec
= -1;
1109 int allow_preemptive_ff
= 1;
1110 int preserve_merges_selected
= 0;
1111 struct reset_head_opts ropts
= { 0 };
1112 struct option builtin_rebase_options
[] = {
1113 OPT_STRING(0, "onto", &options
.onto_name
,
1115 N_("rebase onto given branch instead of upstream")),
1116 OPT_BOOL(0, "keep-base", &keep_base
,
1117 N_("use the merge-base of upstream and branch as the current base")),
1118 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
1119 N_("allow pre-rebase hook to run")),
1120 OPT_NEGBIT('q', "quiet", &options
.flags
,
1121 N_("be quiet. implies --no-stat"),
1122 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1123 OPT_BIT('v', "verbose", &options
.flags
,
1124 N_("display a diffstat of what changed upstream"),
1125 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1127 .type
= OPTION_NEGBIT
,
1129 .long_name
= "no-stat",
1130 .value
= &options
.flags
,
1131 .help
= N_("do not show diffstat of what changed upstream"),
1132 .flags
= PARSE_OPT_NOARG
,
1133 .defval
= REBASE_DIFFSTAT
,
1135 OPT_BOOL(0, "signoff", &options
.signoff
,
1136 N_("add a Signed-off-by trailer to each commit")),
1137 OPT_BOOL(0, "committer-date-is-author-date",
1138 &options
.committer_date_is_author_date
,
1139 N_("make committer date match author date")),
1140 OPT_BOOL(0, "reset-author-date", &options
.ignore_date
,
1141 N_("ignore author date and use current date")),
1142 OPT_HIDDEN_BOOL(0, "ignore-date", &options
.ignore_date
,
1143 N_("synonym of --reset-author-date")),
1144 OPT_PASSTHRU_ARGV('C', NULL
, &options
.git_am_opts
, N_("n"),
1145 N_("passed to 'git apply'"), 0),
1146 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace
,
1147 N_("ignore changes in whitespace")),
1148 OPT_PASSTHRU_ARGV(0, "whitespace", &options
.git_am_opts
,
1149 N_("action"), N_("passed to 'git apply'"), 0),
1150 OPT_BIT('f', "force-rebase", &options
.flags
,
1151 N_("cherry-pick all commits, even if unchanged"),
1153 OPT_BIT(0, "no-ff", &options
.flags
,
1154 N_("cherry-pick all commits, even if unchanged"),
1156 OPT_CMDMODE(0, "continue", &options
.action
, N_("continue"),
1158 OPT_CMDMODE(0, "skip", &options
.action
,
1159 N_("skip current patch and continue"), ACTION_SKIP
),
1160 OPT_CMDMODE(0, "abort", &options
.action
,
1161 N_("abort and check out the original branch"),
1163 OPT_CMDMODE(0, "quit", &options
.action
,
1164 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
1165 OPT_CMDMODE(0, "edit-todo", &options
.action
, N_("edit the todo list "
1166 "during an interactive rebase"), ACTION_EDIT_TODO
),
1167 OPT_CMDMODE(0, "show-current-patch", &options
.action
,
1168 N_("show the patch file being applied or merged"),
1169 ACTION_SHOW_CURRENT_PATCH
),
1170 OPT_CALLBACK_F(0, "apply", &options
, NULL
,
1171 N_("use apply strategies to rebase"),
1172 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1174 OPT_CALLBACK_F('m', "merge", &options
, NULL
,
1175 N_("use merging strategies to rebase"),
1176 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1178 OPT_CALLBACK_F('i', "interactive", &options
, NULL
,
1179 N_("let the user edit the list of commits to rebase"),
1180 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1181 parse_opt_interactive
),
1182 OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected
,
1183 N_("(REMOVED) was: try to recreate merges "
1184 "instead of ignoring them"),
1185 1, PARSE_OPT_HIDDEN
),
1186 OPT_RERERE_AUTOUPDATE(&options
.allow_rerere_autoupdate
),
1187 OPT_CALLBACK_F(0, "empty", &options
, "(drop|keep|stop)",
1188 N_("how to handle commits that become empty"),
1189 PARSE_OPT_NONEG
, parse_opt_empty
),
1190 OPT_CALLBACK_F('k', "keep-empty", &options
, NULL
,
1191 N_("keep commits which start empty"),
1192 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
,
1193 parse_opt_keep_empty
),
1194 OPT_BOOL(0, "autosquash", &options
.autosquash
,
1195 N_("move commits that begin with "
1196 "squash!/fixup! under -i")),
1197 OPT_BOOL(0, "update-refs", &options
.update_refs
,
1198 N_("update branches that point to commits "
1199 "that are being rebased")),
1201 .type
= OPTION_STRING
,
1203 .long_name
= "gpg-sign",
1205 .argh
= N_("key-id"),
1206 .help
= N_("GPG-sign commits"),
1207 .flags
= PARSE_OPT_OPTARG
,
1208 .defval
= (intptr_t) "",
1210 OPT_AUTOSTASH(&options
.autostash
),
1211 OPT_STRING_LIST('x', "exec", &options
.exec
, N_("exec"),
1212 N_("add exec lines after each commit of the "
1214 OPT_BOOL_F(0, "allow-empty-message",
1215 &options
.allow_empty_message
,
1216 N_("allow rebasing commits with empty messages"),
1218 OPT_CALLBACK_F('r', "rebase-merges", &options
, N_("mode"),
1219 N_("try to rebase merges instead of skipping them"),
1220 PARSE_OPT_OPTARG
, parse_opt_rebase_merges
),
1221 OPT_BOOL(0, "fork-point", &options
.fork_point
,
1222 N_("use 'merge-base --fork-point' to refine upstream")),
1223 OPT_STRING('s', "strategy", &strategy_opt
,
1224 N_("strategy"), N_("use the given merge strategy")),
1225 OPT_STRING_LIST('X', "strategy-option", &options
.strategy_opts
,
1227 N_("pass the argument through to the merge "
1229 OPT_BOOL(0, "root", &options
.root
,
1230 N_("rebase all reachable commits up to the root(s)")),
1231 OPT_BOOL(0, "reschedule-failed-exec",
1232 &reschedule_failed_exec
,
1233 N_("automatically re-schedule any `exec` that fails")),
1234 OPT_BOOL(0, "reapply-cherry-picks", &options
.reapply_cherry_picks
,
1235 N_("apply all changes, even those already present upstream")),
1240 show_usage_with_options_if_asked(argc
, argv
,
1241 builtin_rebase_usage
,
1242 builtin_rebase_options
);
1244 prepare_repo_settings(the_repository
);
1245 the_repository
->settings
.command_requires_full_index
= 0;
1247 git_config(rebase_config
, &options
);
1248 /* options.gpg_sign_opt will be either "-S" or NULL */
1249 gpg_sign
= options
.gpg_sign_opt
? "" : NULL
;
1250 FREE_AND_NULL(options
.gpg_sign_opt
);
1253 strbuf_addf(&buf
, "%s/applying", apply_dir());
1254 if(file_exists(buf
.buf
))
1255 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1257 if (is_directory(apply_dir())) {
1258 options
.type
= REBASE_APPLY
;
1259 options
.state_dir
= apply_dir();
1260 } else if (is_directory(merge_dir())) {
1262 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
1263 if (!(options
.action
== ACTION_ABORT
) && is_directory(buf
.buf
)) {
1264 die(_("`rebase --preserve-merges` (-p) is no longer supported.\n"
1265 "Use `git rebase --abort` to terminate current rebase.\n"
1266 "Or downgrade to v2.33, or earlier, to complete the rebase."));
1269 strbuf_addf(&buf
, "%s/interactive", merge_dir());
1270 options
.type
= REBASE_MERGE
;
1271 if (file_exists(buf
.buf
))
1272 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
1274 options
.state_dir
= merge_dir();
1277 if (options
.type
!= REBASE_UNSPECIFIED
)
1281 argc
= parse_options(argc
, argv
, prefix
,
1282 builtin_rebase_options
,
1283 builtin_rebase_usage
, 0);
1285 if (preserve_merges_selected
)
1286 die(_("--preserve-merges was replaced by --rebase-merges\n"
1287 "Note: Your `pull.rebase` configuration may also be set to 'preserve',\n"
1288 "which is no longer supported; use 'merges' instead"));
1290 if (options
.action
!= ACTION_NONE
&& total_argc
!= 2) {
1291 usage_with_options(builtin_rebase_usage
,
1292 builtin_rebase_options
);
1296 usage_with_options(builtin_rebase_usage
,
1297 builtin_rebase_options
);
1300 if (options
.onto_name
)
1301 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--onto");
1303 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--root");
1305 * --keep-base defaults to --no-fork-point to keep the
1308 if (options
.fork_point
< 0)
1309 options
.fork_point
= 0;
1311 if (options
.root
&& options
.fork_point
> 0)
1312 die(_("options '%s' and '%s' cannot be used together"), "--root", "--fork-point");
1314 if (options
.action
!= ACTION_NONE
&& !in_progress
)
1315 die(_("no rebase in progress"));
1317 if (options
.action
== ACTION_EDIT_TODO
&& !is_merge(&options
))
1318 die(_("The --edit-todo action can only be used during "
1319 "interactive rebase."));
1321 if (trace2_is_enabled()) {
1322 if (is_merge(&options
))
1323 trace2_cmd_mode("interactive");
1324 else if (options
.exec
.nr
)
1325 trace2_cmd_mode("interactive-exec");
1327 trace2_cmd_mode(action_names
[options
.action
]);
1330 options
.reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
1331 options
.reflog_action
=
1332 xstrdup(options
.reflog_action
? options
.reflog_action
: "rebase");
1334 switch (options
.action
) {
1335 case ACTION_CONTINUE
: {
1336 struct object_id head
;
1337 struct lock_file lock_file
= LOCK_INIT
;
1341 if (repo_get_oid(the_repository
, "HEAD", &head
))
1342 die(_("Cannot read HEAD"));
1344 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
1345 if (repo_read_index(the_repository
) < 0)
1346 die(_("could not read index"));
1347 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
1350 repo_update_index_if_able(the_repository
, &lock_file
);
1351 rollback_lock_file(&lock_file
);
1353 if (has_unstaged_changes(the_repository
, 1)) {
1354 puts(_("You must edit all merge conflicts and then\n"
1355 "mark them as resolved using git add"));
1358 if (read_basic_state(&options
))
1363 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1365 rerere_clear(the_repository
, &merge_rr
);
1366 string_list_clear(&merge_rr
, 1);
1367 ropts
.flags
= RESET_HEAD_HARD
;
1368 if (reset_head(the_repository
, &ropts
) < 0)
1369 die(_("could not discard worktree changes"));
1370 remove_branch_state(the_repository
, 0);
1371 if (read_basic_state(&options
))
1375 case ACTION_ABORT
: {
1376 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1377 struct strbuf head_msg
= STRBUF_INIT
;
1379 rerere_clear(the_repository
, &merge_rr
);
1380 string_list_clear(&merge_rr
, 1);
1382 if (read_basic_state(&options
))
1385 strbuf_addf(&head_msg
, "%s (abort): returning to %s",
1386 options
.reflog_action
,
1387 options
.head_name
? options
.head_name
1388 : oid_to_hex(&options
.orig_head
->object
.oid
));
1389 ropts
.oid
= &options
.orig_head
->object
.oid
;
1390 ropts
.head_msg
= head_msg
.buf
;
1391 ropts
.branch
= options
.head_name
;
1392 ropts
.flags
= RESET_HEAD_HARD
;
1393 if (reset_head(the_repository
, &ropts
) < 0)
1394 die(_("could not move back to %s"),
1395 oid_to_hex(&options
.orig_head
->object
.oid
));
1396 strbuf_release(&head_msg
);
1397 remove_branch_state(the_repository
, 0);
1398 ret
= finish_rebase(&options
);
1402 save_autostash(state_dir_path("autostash", &options
));
1403 if (options
.type
== REBASE_MERGE
) {
1404 struct replay_opts replay
= REPLAY_OPTS_INIT
;
1406 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
1407 ret
= sequencer_remove_state(&replay
);
1408 replay_opts_release(&replay
);
1411 strbuf_addstr(&buf
, options
.state_dir
);
1412 ret
= remove_dir_recursively(&buf
, 0);
1414 error(_("could not remove '%s'"),
1419 case ACTION_EDIT_TODO
:
1420 options
.dont_finish_rebase
= 1;
1422 case ACTION_SHOW_CURRENT_PATCH
:
1423 options
.dont_finish_rebase
= 1;
1428 BUG("action: %d", options
.action
);
1431 /* Make sure no rebase is in progress */
1433 const char *last_slash
= strrchr(options
.state_dir
, '/');
1434 const char *state_dir_base
=
1435 last_slash
? last_slash
+ 1 : options
.state_dir
;
1436 const char *cmd_live_rebase
=
1437 "git rebase (--continue | --abort | --skip)";
1439 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
1440 die(_("It seems that there is already a %s directory, and\n"
1441 "I wonder if you are in the middle of another rebase. "
1443 "case, please try\n\t%s\n"
1444 "If that is not the case, please\n\t%s\n"
1445 "and run me again. I am stopping in case you still "
1447 "valuable there.\n"),
1448 state_dir_base
, cmd_live_rebase
, buf
.buf
);
1451 if ((options
.flags
& REBASE_INTERACTIVE_EXPLICIT
) ||
1452 (options
.action
!= ACTION_NONE
) ||
1453 (options
.exec
.nr
> 0) ||
1454 options
.autosquash
== 1) {
1455 allow_preemptive_ff
= 0;
1457 if (options
.committer_date_is_author_date
|| options
.ignore_date
)
1458 options
.flags
|= REBASE_FORCE
;
1460 for (i
= 0; i
< options
.git_am_opts
.nr
; i
++) {
1461 const char *option
= options
.git_am_opts
.v
[i
], *p
;
1462 if (!strcmp(option
, "--whitespace=fix") ||
1463 !strcmp(option
, "--whitespace=strip"))
1464 allow_preemptive_ff
= 0;
1465 else if (skip_prefix(option
, "-C", &p
)) {
1467 if (!isdigit(*(p
++)))
1468 die(_("switch `C' expects a "
1469 "numerical value"));
1470 } else if (skip_prefix(option
, "--whitespace=", &p
)) {
1471 if (*p
&& strcmp(p
, "warn") && strcmp(p
, "nowarn") &&
1472 strcmp(p
, "error") && strcmp(p
, "error-all"))
1473 die("Invalid whitespace option: '%s'", p
);
1477 for (i
= 0; i
< options
.exec
.nr
; i
++)
1478 if (check_exec_cmd(options
.exec
.items
[i
].string
))
1481 if (!(options
.flags
& REBASE_NO_QUIET
))
1482 strvec_push(&options
.git_am_opts
, "-q");
1484 if (options
.empty
!= EMPTY_UNSPECIFIED
)
1485 imply_merge(&options
, "--empty");
1487 if (options
.reapply_cherry_picks
< 0)
1489 * We default to --no-reapply-cherry-picks unless
1490 * --keep-base is given; when --keep-base is given, we want
1491 * to default to --reapply-cherry-picks.
1493 options
.reapply_cherry_picks
= keep_base
;
1494 else if (!keep_base
)
1496 * The apply backend always searches for and drops cherry
1497 * picks. This is often not wanted with --keep-base, so
1498 * --keep-base allows --reapply-cherry-picks to be
1499 * simulated by altering the upstream such that
1500 * cherry-picks cannot be detected and thus all commits are
1501 * reapplied. Thus, --[no-]reapply-cherry-picks is
1502 * supported when --keep-base is specified, but not when
1503 * --keep-base is left out.
1505 imply_merge(&options
, options
.reapply_cherry_picks
?
1506 "--reapply-cherry-picks" :
1507 "--no-reapply-cherry-picks");
1510 options
.gpg_sign_opt
= xstrfmt("-S%s", gpg_sign
);
1512 if (options
.exec
.nr
)
1513 imply_merge(&options
, "--exec");
1515 if (options
.type
== REBASE_APPLY
) {
1516 if (ignore_whitespace
)
1517 strvec_push(&options
.git_am_opts
,
1518 "--ignore-whitespace");
1519 if (options
.committer_date_is_author_date
)
1520 strvec_push(&options
.git_am_opts
,
1521 "--committer-date-is-author-date");
1522 if (options
.ignore_date
)
1523 strvec_push(&options
.git_am_opts
, "--ignore-date");
1526 if (ignore_whitespace
) {
1527 string_list_append(&options
.strategy_opts
,
1528 "ignore-space-change");
1533 options
.strategy
= xstrdup(strategy_opt
);
1534 else if (options
.strategy_opts
.nr
&& !options
.strategy
)
1535 options
.strategy
= xstrdup("ort");
1536 if (options
.strategy
)
1537 imply_merge(&options
, "--strategy");
1539 if (options
.root
&& !options
.onto_name
)
1540 imply_merge(&options
, "--root without --onto");
1542 if (isatty(2) && options
.flags
& REBASE_NO_QUIET
)
1543 strbuf_addstr(&options
.git_format_patch_opt
, " --progress");
1545 if (options
.git_am_opts
.nr
|| options
.type
== REBASE_APPLY
) {
1546 /* all am options except -q are compatible only with --apply */
1547 for (i
= options
.git_am_opts
.nr
- 1; i
>= 0; i
--)
1548 if (strcmp(options
.git_am_opts
.v
[i
], "-q"))
1551 if (i
>= 0 || options
.type
== REBASE_APPLY
) {
1552 if (is_merge(&options
))
1553 die(_("apply options and merge options "
1554 "cannot be used together"));
1555 else if (options
.rebase_merges
== -1 && options
.config_rebase_merges
== 1)
1556 die(_("apply options are incompatible with rebase.rebaseMerges. Consider adding --no-rebase-merges"));
1557 else if (options
.update_refs
== -1 && options
.config_update_refs
== 1)
1558 die(_("apply options are incompatible with rebase.updateRefs. Consider adding --no-update-refs"));
1560 options
.type
= REBASE_APPLY
;
1564 if (options
.update_refs
== 1)
1565 imply_merge(&options
, "--update-refs");
1566 options
.update_refs
= (options
.update_refs
>= 0) ? options
.update_refs
:
1567 ((options
.config_update_refs
>= 0) ? options
.config_update_refs
: 0);
1569 if (options
.rebase_merges
== 1)
1570 imply_merge(&options
, "--rebase-merges");
1571 options
.rebase_merges
= (options
.rebase_merges
>= 0) ? options
.rebase_merges
:
1572 ((options
.config_rebase_merges
>= 0) ? options
.config_rebase_merges
: 0);
1574 if (options
.autosquash
== 1) {
1575 imply_merge(&options
, "--autosquash");
1576 } else if (options
.autosquash
== -1) {
1577 options
.autosquash
=
1578 options
.config_autosquash
&&
1579 (options
.flags
& REBASE_INTERACTIVE_EXPLICIT
);
1582 if (options
.type
== REBASE_UNSPECIFIED
) {
1583 if (!strcmp(options
.default_backend
, "merge"))
1584 options
.type
= REBASE_MERGE
;
1585 else if (!strcmp(options
.default_backend
, "apply"))
1586 options
.type
= REBASE_APPLY
;
1588 die(_("Unknown rebase backend: %s"),
1589 options
.default_backend
);
1592 switch (options
.type
) {
1594 options
.state_dir
= merge_dir();
1597 options
.state_dir
= apply_dir();
1600 BUG("options.type was just set above; should be unreachable.");
1603 if (options
.empty
== EMPTY_UNSPECIFIED
) {
1604 if (options
.flags
& REBASE_INTERACTIVE_EXPLICIT
)
1605 options
.empty
= EMPTY_STOP
;
1606 else if (options
.exec
.nr
> 0)
1607 options
.empty
= EMPTY_KEEP
;
1609 options
.empty
= EMPTY_DROP
;
1611 if (reschedule_failed_exec
> 0 && !is_merge(&options
))
1612 die(_("--reschedule-failed-exec requires "
1613 "--exec or --interactive"));
1614 if (reschedule_failed_exec
>= 0)
1615 options
.reschedule_failed_exec
= reschedule_failed_exec
;
1617 if (options
.signoff
) {
1618 strvec_push(&options
.git_am_opts
, "--signoff");
1619 options
.flags
|= REBASE_FORCE
;
1622 if (!options
.root
) {
1624 struct branch
*branch
;
1626 branch
= branch_get(NULL
);
1627 options
.upstream_name
= branch_get_upstream(branch
,
1629 if (!options
.upstream_name
)
1630 error_on_missing_default_upstream();
1631 if (options
.fork_point
< 0)
1632 options
.fork_point
= 1;
1634 options
.upstream_name
= argv
[0];
1637 if (!strcmp(options
.upstream_name
, "-"))
1638 options
.upstream_name
= "@{-1}";
1641 lookup_commit_reference_by_name(options
.upstream_name
);
1642 if (!options
.upstream
)
1643 die(_("invalid upstream '%s'"), options
.upstream_name
);
1644 options
.upstream_arg
= options
.upstream_name
;
1646 if (!options
.onto_name
) {
1647 if (commit_tree("", 0, the_hash_algo
->empty_tree
, NULL
,
1648 &squash_onto
, NULL
, NULL
) < 0)
1649 die(_("Could not create new root commit"));
1650 options
.squash_onto
= &squash_onto
;
1651 options
.onto_name
= squash_onto_name
=
1652 xstrdup(oid_to_hex(&squash_onto
));
1654 options
.root_with_onto
= 1;
1656 options
.upstream_name
= NULL
;
1657 options
.upstream
= NULL
;
1659 usage_with_options(builtin_rebase_usage
,
1660 builtin_rebase_options
);
1661 options
.upstream_arg
= "--root";
1665 * If the branch to rebase is given, that is the branch we will rebase
1666 * branch_name -- branch/commit being rebased, or
1667 * HEAD (already detached)
1668 * orig_head -- commit object name of tip of the branch before rebasing
1669 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1672 /* Is it "rebase other branchname" or "rebase other commit"? */
1673 struct object_id branch_oid
;
1674 branch_name
= argv
[0];
1675 options
.switch_to
= argv
[0];
1677 /* Is it a local branch? */
1679 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
1680 if (!refs_read_ref(get_main_ref_store(the_repository
), buf
.buf
, &branch_oid
)) {
1681 die_if_checked_out(buf
.buf
, 1);
1682 options
.head_name
= xstrdup(buf
.buf
);
1684 lookup_commit_object(the_repository
,
1686 /* If not is it a valid ref (branch or commit)? */
1689 lookup_commit_reference_by_name(branch_name
);
1690 options
.head_name
= NULL
;
1692 if (!options
.orig_head
)
1693 die(_("no such branch/commit '%s'"), branch_name
);
1694 } else if (argc
== 0) {
1695 /* Do not need to switch branches, we are already on it. */
1697 xstrdup_or_null(refs_resolve_ref_unsafe(get_main_ref_store(the_repository
), "HEAD", 0, NULL
,
1699 if (!options
.head_name
)
1700 die(_("No such ref: %s"), "HEAD");
1701 if (flags
& REF_ISSYMREF
) {
1702 if (!skip_prefix(options
.head_name
,
1703 "refs/heads/", &branch_name
))
1704 branch_name
= options
.head_name
;
1707 FREE_AND_NULL(options
.head_name
);
1708 branch_name
= "HEAD";
1710 options
.orig_head
= lookup_commit_reference_by_name("HEAD");
1711 if (!options
.orig_head
)
1712 die(_("Could not resolve HEAD to a commit"));
1714 BUG("unexpected number of arguments left to parse");
1716 /* Make sure the branch to rebase onto is valid. */
1719 strbuf_addstr(&buf
, options
.upstream_name
);
1720 strbuf_addstr(&buf
, "...");
1721 strbuf_addstr(&buf
, branch_name
);
1722 options
.onto_name
= keep_base_onto_name
= xstrdup(buf
.buf
);
1723 } else if (!options
.onto_name
)
1724 options
.onto_name
= options
.upstream_name
;
1725 if (strstr(options
.onto_name
, "...")) {
1726 if (repo_get_oid_mb(the_repository
, options
.onto_name
, &branch_base
) < 0) {
1728 die(_("'%s': need exactly one merge base with branch"),
1729 options
.upstream_name
);
1731 die(_("'%s': need exactly one merge base"),
1734 options
.onto
= lookup_commit_or_die(&branch_base
,
1738 lookup_commit_reference_by_name(options
.onto_name
);
1740 die(_("Does not point to a valid commit '%s'"),
1742 fill_branch_base(&options
, &branch_base
);
1745 if (keep_base
&& options
.reapply_cherry_picks
)
1746 options
.upstream
= options
.onto
;
1748 if (options
.fork_point
> 0)
1749 options
.restrict_revision
=
1750 get_fork_point(options
.upstream_name
, options
.orig_head
);
1752 if (repo_read_index(the_repository
) < 0)
1753 die(_("could not read index"));
1755 if (options
.autostash
)
1756 create_autostash(the_repository
,
1757 state_dir_path("autostash", &options
));
1760 if (require_clean_work_tree(the_repository
, "rebase",
1761 _("Please commit or stash them."), 1, 1)) {
1763 goto cleanup_autostash
;
1767 * Now we are rebasing commits upstream..orig_head (or with --root,
1768 * everything leading up to orig_head) on top of onto.
1772 * Check if we are already based on onto with linear history,
1773 * in which case we could fast-forward without replacing the commits
1774 * with new commits recreated by replaying their changes.
1776 if (allow_preemptive_ff
&&
1777 can_fast_forward(options
.onto
, options
.upstream
, options
.restrict_revision
,
1778 options
.orig_head
, &branch_base
)) {
1781 if (!(options
.flags
& REBASE_FORCE
)) {
1782 /* Lazily switch to the target branch if needed... */
1783 if (options
.switch_to
) {
1784 ret
= checkout_up_to_date(&options
);
1786 goto cleanup_autostash
;
1789 if (!(options
.flags
& REBASE_NO_QUIET
))
1791 else if (!strcmp(branch_name
, "HEAD") &&
1792 refs_resolve_ref_unsafe(get_main_ref_store(the_repository
), "HEAD", 0, NULL
, &flag
))
1793 puts(_("HEAD is up to date."));
1795 printf(_("Current branch %s is up to date.\n"),
1797 ret
= finish_rebase(&options
);
1799 } else if (!(options
.flags
& REBASE_NO_QUIET
))
1801 else if (!strcmp(branch_name
, "HEAD") &&
1802 refs_resolve_ref_unsafe(get_main_ref_store(the_repository
), "HEAD", 0, NULL
, &flag
))
1803 puts(_("HEAD is up to date, rebase forced."));
1805 printf(_("Current branch %s is up to date, rebase "
1806 "forced.\n"), branch_name
);
1809 /* If a hook exists, give it a chance to interrupt*/
1810 if (!ok_to_skip_pre_rebase
&&
1811 run_hooks_l(the_repository
, "pre-rebase", options
.upstream_arg
,
1812 argc
? argv
[0] : NULL
, NULL
)) {
1813 ret
= error(_("The pre-rebase hook refused to rebase."));
1814 goto cleanup_autostash
;
1817 if (options
.flags
& REBASE_DIFFSTAT
) {
1818 struct diff_options opts
;
1820 if (options
.flags
& REBASE_VERBOSE
) {
1821 if (is_null_oid(&branch_base
))
1822 printf(_("Changes to %s:\n"),
1823 oid_to_hex(&options
.onto
->object
.oid
));
1825 printf(_("Changes from %s to %s:\n"),
1826 oid_to_hex(&branch_base
),
1827 oid_to_hex(&options
.onto
->object
.oid
));
1830 /* We want color (if set), but no pager */
1831 repo_diff_setup(the_repository
, &opts
);
1832 init_diffstat_widths(&opts
);
1833 opts
.output_format
|=
1834 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1835 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1836 diff_setup_done(&opts
);
1837 diff_tree_oid(is_null_oid(&branch_base
) ?
1838 the_hash_algo
->empty_tree
: &branch_base
,
1839 &options
.onto
->object
.oid
, "", &opts
);
1840 diffcore_std(&opts
);
1844 if (is_merge(&options
))
1847 /* Detach HEAD and reset the tree */
1848 if (options
.flags
& REBASE_NO_QUIET
)
1849 printf(_("First, rewinding head to replay your work on top of "
1852 strbuf_addf(&msg
, "%s (start): checkout %s",
1853 options
.reflog_action
, options
.onto_name
);
1854 ropts
.oid
= &options
.onto
->object
.oid
;
1855 ropts
.orig_head
= &options
.orig_head
->object
.oid
;
1856 ropts
.flags
= RESET_HEAD_DETACH
| RESET_ORIG_HEAD
|
1857 RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
1858 ropts
.head_msg
= msg
.buf
;
1859 ropts
.default_reflog_action
= options
.reflog_action
;
1860 if (reset_head(the_repository
, &ropts
)) {
1861 ret
= error(_("Could not detach HEAD"));
1862 goto cleanup_autostash
;
1866 * If the onto is a proper descendant of the tip of the branch, then
1867 * we just fast-forwarded.
1869 if (oideq(&branch_base
, &options
.orig_head
->object
.oid
)) {
1870 printf(_("Fast-forwarded %s to %s.\n"),
1871 branch_name
, options
.onto_name
);
1872 move_to_original_branch(&options
);
1873 ret
= finish_rebase(&options
);
1877 strbuf_addf(&revisions
, "%s..%s",
1878 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
1879 (options
.restrict_revision
?
1880 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
1881 oid_to_hex(&options
.upstream
->object
.oid
)),
1882 oid_to_hex(&options
.orig_head
->object
.oid
));
1884 options
.revisions
= revisions
.buf
;
1887 ret
= run_specific_rebase(&options
);
1890 strbuf_release(&buf
);
1891 strbuf_release(&msg
);
1892 strbuf_release(&revisions
);
1893 rebase_options_release(&options
);
1894 free(squash_onto_name
);
1895 free(keep_base_onto_name
);
1899 ret
|= !!cleanup_autostash(&options
);