2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
7 #define USE_THE_INDEX_VARIABLE
10 #include "environment.h"
13 #include "run-command.h"
18 #include "unpack-trees.h"
20 #include "object-file.h"
21 #include "object-name.h"
22 #include "parse-options.h"
26 #include "wt-status.h"
28 #include "commit-reach.h"
31 #include "sequencer.h"
32 #include "rebase-interactive.h"
37 static char const * const builtin_rebase_usage
[] = {
38 N_("git rebase [-i] [options] [--exec <cmd>] "
39 "[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
40 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
42 "git rebase --continue | --abort | --skip | --edit-todo",
46 static GIT_PATH_FUNC(path_squash_onto
, "rebase-merge/squash-onto")
47 static GIT_PATH_FUNC(path_interactive
, "rebase-merge/interactive")
48 static GIT_PATH_FUNC(apply_dir
, "rebase-apply")
49 static GIT_PATH_FUNC(merge_dir
, "rebase-merge")
52 REBASE_UNSPECIFIED
= -1,
58 EMPTY_UNSPECIFIED
= -1,
71 ACTION_SHOW_CURRENT_PATCH
74 static const char *action_names
[] = {
84 struct rebase_options
{
85 enum rebase_type type
;
86 enum empty_type empty
;
87 const char *default_backend
;
88 const char *state_dir
;
89 struct commit
*upstream
;
90 const char *upstream_name
;
91 const char *upstream_arg
;
93 struct commit
*orig_head
;
95 const char *onto_name
;
96 const char *revisions
;
97 const char *switch_to
;
98 int root
, root_with_onto
;
99 struct object_id
*squash_onto
;
100 struct commit
*restrict_revision
;
101 int dont_finish_rebase
;
103 REBASE_NO_QUIET
= 1<<0,
104 REBASE_VERBOSE
= 1<<1,
105 REBASE_DIFFSTAT
= 1<<2,
107 REBASE_INTERACTIVE_EXPLICIT
= 1<<4,
109 struct strvec git_am_opts
;
113 int allow_rerere_autoupdate
;
118 int committer_date_is_author_date
;
120 struct string_list exec
;
121 int allow_empty_message
;
122 int rebase_merges
, rebase_cousins
;
124 struct string_list strategy_opts
;
125 struct strbuf git_format_patch_opt
;
126 int reschedule_failed_exec
;
127 int reapply_cherry_picks
;
130 int config_autosquash
;
131 int config_rebase_merges
;
132 int config_update_refs
;
135 #define REBASE_OPTIONS_INIT { \
136 .type = REBASE_UNSPECIFIED, \
137 .empty = EMPTY_UNSPECIFIED, \
139 .default_backend = "merge", \
140 .flags = REBASE_NO_QUIET, \
141 .git_am_opts = STRVEC_INIT, \
142 .exec = STRING_LIST_INIT_NODUP, \
143 .git_format_patch_opt = STRBUF_INIT, \
145 .reapply_cherry_picks = -1, \
146 .allow_empty_message = 1, \
148 .rebase_merges = -1, \
149 .config_rebase_merges = -1, \
151 .config_update_refs = -1, \
152 .strategy_opts = STRING_LIST_INIT_NODUP,\
155 static struct replay_opts
get_replay_opts(const struct rebase_options
*opts
)
157 struct replay_opts replay
= REPLAY_OPTS_INIT
;
159 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
160 replay
.strategy
= NULL
;
161 sequencer_init_config(&replay
);
163 replay
.signoff
= opts
->signoff
;
164 replay
.allow_ff
= !(opts
->flags
& REBASE_FORCE
);
165 if (opts
->allow_rerere_autoupdate
)
166 replay
.allow_rerere_auto
= opts
->allow_rerere_autoupdate
;
167 replay
.allow_empty
= 1;
168 replay
.allow_empty_message
= opts
->allow_empty_message
;
169 replay
.drop_redundant_commits
= (opts
->empty
== EMPTY_DROP
);
170 replay
.keep_redundant_commits
= (opts
->empty
== EMPTY_KEEP
);
171 replay
.quiet
= !(opts
->flags
& REBASE_NO_QUIET
);
172 replay
.verbose
= opts
->flags
& REBASE_VERBOSE
;
173 replay
.reschedule_failed_exec
= opts
->reschedule_failed_exec
;
174 replay
.committer_date_is_author_date
=
175 opts
->committer_date_is_author_date
;
176 replay
.ignore_date
= opts
->ignore_date
;
177 replay
.gpg_sign
= xstrdup_or_null(opts
->gpg_sign_opt
);
178 replay
.reflog_action
= xstrdup(opts
->reflog_action
);
180 replay
.strategy
= xstrdup_or_null(opts
->strategy
);
181 else if (!replay
.strategy
&& replay
.default_strategy
) {
182 replay
.strategy
= replay
.default_strategy
;
183 replay
.default_strategy
= NULL
;
186 for (size_t i
= 0; i
< opts
->strategy_opts
.nr
; i
++)
187 strvec_push(&replay
.xopts
, opts
->strategy_opts
.items
[i
].string
);
189 if (opts
->squash_onto
) {
190 oidcpy(&replay
.squash_onto
, opts
->squash_onto
);
191 replay
.have_squash_onto
= 1;
197 static int edit_todo_file(unsigned flags
)
199 const char *todo_file
= rebase_path_todo();
200 struct todo_list todo_list
= TODO_LIST_INIT
,
201 new_todo
= TODO_LIST_INIT
;
204 if (strbuf_read_file(&todo_list
.buf
, todo_file
, 0) < 0)
205 return error_errno(_("could not read '%s'."), todo_file
);
207 strbuf_stripspace(&todo_list
.buf
, comment_line_char
);
208 res
= edit_todo_list(the_repository
, &todo_list
, &new_todo
, NULL
, NULL
, flags
);
209 if (!res
&& todo_list_write_to_file(the_repository
, &new_todo
, todo_file
,
210 NULL
, NULL
, -1, flags
& ~(TODO_LIST_SHORTEN_IDS
)))
211 res
= error_errno(_("could not write '%s'"), todo_file
);
213 todo_list_release(&todo_list
);
214 todo_list_release(&new_todo
);
219 static int get_revision_ranges(struct commit
*upstream
, struct commit
*onto
,
220 struct object_id
*orig_head
, char **revisions
,
221 char **shortrevisions
)
223 struct commit
*base_rev
= upstream
? upstream
: onto
;
224 const char *shorthead
;
226 *revisions
= xstrfmt("%s...%s", oid_to_hex(&base_rev
->object
.oid
),
227 oid_to_hex(orig_head
));
229 shorthead
= repo_find_unique_abbrev(the_repository
, orig_head
,
233 const char *shortrev
;
235 shortrev
= repo_find_unique_abbrev(the_repository
,
236 &base_rev
->object
.oid
,
239 *shortrevisions
= xstrfmt("%s..%s", shortrev
, shorthead
);
241 *shortrevisions
= xstrdup(shorthead
);
246 static int init_basic_state(struct replay_opts
*opts
, const char *head_name
,
248 const struct object_id
*orig_head
)
252 if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir()))
253 return error_errno(_("could not create temporary %s"), merge_dir());
255 delete_reflog("REBASE_HEAD");
257 interactive
= fopen(path_interactive(), "w");
259 return error_errno(_("could not mark as interactive"));
262 return write_basic_state(opts
, head_name
, onto
, orig_head
);
265 static int do_interactive_rebase(struct rebase_options
*opts
, unsigned flags
)
268 char *revisions
= NULL
, *shortrevisions
= NULL
;
269 struct strvec make_script_args
= STRVEC_INIT
;
270 struct todo_list todo_list
= TODO_LIST_INIT
;
271 struct replay_opts replay
= get_replay_opts(opts
);
273 if (get_revision_ranges(opts
->upstream
, opts
->onto
, &opts
->orig_head
->object
.oid
,
274 &revisions
, &shortrevisions
))
277 if (init_basic_state(&replay
,
278 opts
->head_name
? opts
->head_name
: "detached HEAD",
279 opts
->onto
, &opts
->orig_head
->object
.oid
))
282 if (!opts
->upstream
&& opts
->squash_onto
)
283 write_file(path_squash_onto(), "%s\n",
284 oid_to_hex(opts
->squash_onto
));
286 strvec_pushl(&make_script_args
, "", revisions
, NULL
);
287 if (opts
->restrict_revision
)
288 strvec_pushf(&make_script_args
, "^%s",
289 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
291 ret
= sequencer_make_script(the_repository
, &todo_list
.buf
,
292 make_script_args
.nr
, make_script_args
.v
,
296 error(_("could not generate todo list"));
298 discard_index(&the_index
);
299 if (todo_list_parse_insn_buffer(the_repository
, todo_list
.buf
.buf
,
301 BUG("unusable todo list");
303 ret
= complete_action(the_repository
, &replay
, flags
,
304 shortrevisions
, opts
->onto_name
, opts
->onto
,
305 &opts
->orig_head
->object
.oid
, &opts
->exec
,
306 opts
->autosquash
, opts
->update_refs
, &todo_list
);
310 replay_opts_release(&replay
);
312 free(shortrevisions
);
313 todo_list_release(&todo_list
);
314 strvec_clear(&make_script_args
);
319 static int run_sequencer_rebase(struct rebase_options
*opts
)
322 int abbreviate_commands
= 0, ret
= 0;
324 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands
);
326 flags
|= opts
->keep_empty
? TODO_LIST_KEEP_EMPTY
: 0;
327 flags
|= abbreviate_commands
? TODO_LIST_ABBREVIATE_CMDS
: 0;
328 flags
|= opts
->rebase_merges
? TODO_LIST_REBASE_MERGES
: 0;
329 flags
|= opts
->rebase_cousins
> 0 ? TODO_LIST_REBASE_COUSINS
: 0;
330 flags
|= opts
->root_with_onto
? TODO_LIST_ROOT_WITH_ONTO
: 0;
331 flags
|= opts
->reapply_cherry_picks
? TODO_LIST_REAPPLY_CHERRY_PICKS
: 0;
332 flags
|= opts
->flags
& REBASE_NO_QUIET
? TODO_LIST_WARN_SKIPPED_CHERRY_PICKS
: 0;
334 switch (opts
->action
) {
336 if (!opts
->onto
&& !opts
->upstream
)
337 die(_("a base commit must be provided with --upstream or --onto"));
339 ret
= do_interactive_rebase(opts
, flags
);
343 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
345 rerere_clear(the_repository
, &merge_rr
);
348 case ACTION_CONTINUE
: {
349 struct replay_opts replay_opts
= get_replay_opts(opts
);
351 ret
= sequencer_continue(the_repository
, &replay_opts
);
352 replay_opts_release(&replay_opts
);
355 case ACTION_EDIT_TODO
:
356 ret
= edit_todo_file(flags
);
358 case ACTION_SHOW_CURRENT_PATCH
: {
359 struct child_process cmd
= CHILD_PROCESS_INIT
;
362 strvec_pushl(&cmd
.args
, "show", "REBASE_HEAD", "--", NULL
);
363 ret
= run_command(&cmd
);
368 BUG("invalid command '%d'", opts
->action
);
374 static int is_merge(struct rebase_options
*opts
)
376 return opts
->type
== REBASE_MERGE
;
379 static void imply_merge(struct rebase_options
*opts
, const char *option
)
381 switch (opts
->type
) {
383 die(_("%s requires the merge backend"), option
);
388 opts
->type
= REBASE_MERGE
; /* implied */
393 /* Returns the filename prefixed by the state_dir */
394 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
396 static struct strbuf path
= STRBUF_INIT
;
397 static size_t prefix_len
;
400 strbuf_addf(&path
, "%s/", opts
->state_dir
);
401 prefix_len
= path
.len
;
404 strbuf_setlen(&path
, prefix_len
);
405 strbuf_addstr(&path
, filename
);
409 /* Initialize the rebase options from the state directory. */
410 static int read_basic_state(struct rebase_options
*opts
)
412 struct strbuf head_name
= STRBUF_INIT
;
413 struct strbuf buf
= STRBUF_INIT
;
414 struct object_id oid
;
416 if (!read_oneliner(&head_name
, state_dir_path("head-name", opts
),
417 READ_ONELINER_WARN_MISSING
) ||
418 !read_oneliner(&buf
, state_dir_path("onto", opts
),
419 READ_ONELINER_WARN_MISSING
))
421 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
422 xstrdup(head_name
.buf
) : NULL
;
423 strbuf_release(&head_name
);
424 if (get_oid_hex(buf
.buf
, &oid
) ||
425 !(opts
->onto
= lookup_commit_object(the_repository
, &oid
)))
426 return error(_("invalid onto: '%s'"), buf
.buf
);
429 * We always write to orig-head, but interactive rebase used to write to
430 * head. Fall back to reading from head to cover for the case that the
431 * user upgraded git with an ongoing interactive rebase.
434 if (file_exists(state_dir_path("orig-head", opts
))) {
435 if (!read_oneliner(&buf
, state_dir_path("orig-head", opts
),
436 READ_ONELINER_WARN_MISSING
))
438 } else if (!read_oneliner(&buf
, state_dir_path("head", opts
),
439 READ_ONELINER_WARN_MISSING
))
441 if (get_oid_hex(buf
.buf
, &oid
) ||
442 !(opts
->orig_head
= lookup_commit_object(the_repository
, &oid
)))
443 return error(_("invalid orig-head: '%s'"), buf
.buf
);
445 if (file_exists(state_dir_path("quiet", opts
)))
446 opts
->flags
&= ~REBASE_NO_QUIET
;
448 opts
->flags
|= REBASE_NO_QUIET
;
450 if (file_exists(state_dir_path("verbose", opts
)))
451 opts
->flags
|= REBASE_VERBOSE
;
453 if (file_exists(state_dir_path("signoff", opts
))) {
455 opts
->flags
|= REBASE_FORCE
;
458 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts
))) {
460 if (!read_oneliner(&buf
, state_dir_path("allow_rerere_autoupdate", opts
),
461 READ_ONELINER_WARN_MISSING
))
463 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
464 opts
->allow_rerere_autoupdate
= RERERE_AUTOUPDATE
;
465 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
466 opts
->allow_rerere_autoupdate
= RERERE_NOAUTOUPDATE
;
468 warning(_("ignoring invalid allow_rerere_autoupdate: "
472 if (file_exists(state_dir_path("gpg_sign_opt", opts
))) {
474 if (!read_oneliner(&buf
, state_dir_path("gpg_sign_opt", opts
),
475 READ_ONELINER_WARN_MISSING
))
477 free(opts
->gpg_sign_opt
);
478 opts
->gpg_sign_opt
= xstrdup(buf
.buf
);
481 strbuf_release(&buf
);
486 static int rebase_write_basic_state(struct rebase_options
*opts
)
488 write_file(state_dir_path("head-name", opts
), "%s",
489 opts
->head_name
? opts
->head_name
: "detached HEAD");
490 write_file(state_dir_path("onto", opts
), "%s",
491 opts
->onto
? oid_to_hex(&opts
->onto
->object
.oid
) : "");
492 write_file(state_dir_path("orig-head", opts
), "%s",
493 oid_to_hex(&opts
->orig_head
->object
.oid
));
494 if (!(opts
->flags
& REBASE_NO_QUIET
))
495 write_file(state_dir_path("quiet", opts
), "%s", "");
496 if (opts
->flags
& REBASE_VERBOSE
)
497 write_file(state_dir_path("verbose", opts
), "%s", "");
498 if (opts
->allow_rerere_autoupdate
> 0)
499 write_file(state_dir_path("allow_rerere_autoupdate", opts
),
500 "-%s-rerere-autoupdate",
501 opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
?
503 if (opts
->gpg_sign_opt
)
504 write_file(state_dir_path("gpg_sign_opt", opts
), "%s",
507 write_file(state_dir_path("signoff", opts
), "--signoff");
512 static int finish_rebase(struct rebase_options
*opts
)
514 struct strbuf dir
= STRBUF_INIT
;
517 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
518 delete_ref(NULL
, "AUTO_MERGE", NULL
, REF_NO_DEREF
);
519 apply_autostash(state_dir_path("autostash", opts
));
521 * We ignore errors in 'git maintenance run --auto', since the
522 * user should see them.
524 run_auto_maintenance(!(opts
->flags
& (REBASE_NO_QUIET
|REBASE_VERBOSE
)));
525 if (opts
->type
== REBASE_MERGE
) {
526 struct replay_opts replay
= REPLAY_OPTS_INIT
;
528 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
529 ret
= sequencer_remove_state(&replay
);
530 replay_opts_release(&replay
);
532 strbuf_addstr(&dir
, opts
->state_dir
);
533 if (remove_dir_recursively(&dir
, 0))
534 ret
= error(_("could not remove '%s'"),
536 strbuf_release(&dir
);
542 static int move_to_original_branch(struct rebase_options
*opts
)
544 struct strbuf branch_reflog
= STRBUF_INIT
, head_reflog
= STRBUF_INIT
;
545 struct reset_head_opts ropts
= { 0 };
548 if (!opts
->head_name
)
549 return 0; /* nothing to move back to */
552 BUG("move_to_original_branch without onto");
554 strbuf_addf(&branch_reflog
, "%s (finish): %s onto %s",
556 opts
->head_name
, oid_to_hex(&opts
->onto
->object
.oid
));
557 strbuf_addf(&head_reflog
, "%s (finish): returning to %s",
558 opts
->reflog_action
, opts
->head_name
);
559 ropts
.branch
= opts
->head_name
;
560 ropts
.flags
= RESET_HEAD_REFS_ONLY
;
561 ropts
.branch_msg
= branch_reflog
.buf
;
562 ropts
.head_msg
= head_reflog
.buf
;
563 ret
= reset_head(the_repository
, &ropts
);
565 strbuf_release(&branch_reflog
);
566 strbuf_release(&head_reflog
);
570 static int run_am(struct rebase_options
*opts
)
572 struct child_process am
= CHILD_PROCESS_INIT
;
573 struct child_process format_patch
= CHILD_PROCESS_INIT
;
575 char *rebased_patches
;
578 strvec_push(&am
.args
, "am");
579 strvec_pushf(&am
.env
, GIT_REFLOG_ACTION_ENVIRONMENT
"=%s (pick)",
580 opts
->reflog_action
);
581 if (opts
->action
== ACTION_CONTINUE
) {
582 strvec_push(&am
.args
, "--resolved");
583 strvec_pushf(&am
.args
, "--resolvemsg=%s", rebase_resolvemsg
);
584 if (opts
->gpg_sign_opt
)
585 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
586 status
= run_command(&am
);
590 return move_to_original_branch(opts
);
592 if (opts
->action
== ACTION_SKIP
) {
593 strvec_push(&am
.args
, "--skip");
594 strvec_pushf(&am
.args
, "--resolvemsg=%s", rebase_resolvemsg
);
595 status
= run_command(&am
);
599 return move_to_original_branch(opts
);
601 if (opts
->action
== ACTION_SHOW_CURRENT_PATCH
) {
602 strvec_push(&am
.args
, "--show-current-patch");
603 return run_command(&am
);
606 rebased_patches
= xstrdup(git_path("rebased-patches"));
607 format_patch
.out
= open(rebased_patches
,
608 O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
609 if (format_patch
.out
< 0) {
610 status
= error_errno(_("could not open '%s' for writing"),
612 free(rebased_patches
);
613 strvec_clear(&am
.args
);
617 format_patch
.git_cmd
= 1;
618 strvec_pushl(&format_patch
.args
, "format-patch", "-k", "--stdout",
619 "--full-index", "--cherry-pick", "--right-only",
620 "--default-prefix", "--no-renames",
621 "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
623 if (opts
->git_format_patch_opt
.len
)
624 strvec_split(&format_patch
.args
,
625 opts
->git_format_patch_opt
.buf
);
626 strvec_pushf(&format_patch
.args
, "%s...%s",
627 oid_to_hex(opts
->root
?
628 /* this is now equivalent to !opts->upstream */
629 &opts
->onto
->object
.oid
:
630 &opts
->upstream
->object
.oid
),
631 oid_to_hex(&opts
->orig_head
->object
.oid
));
632 if (opts
->restrict_revision
)
633 strvec_pushf(&format_patch
.args
, "^%s",
634 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
636 status
= run_command(&format_patch
);
638 struct reset_head_opts ropts
= { 0 };
639 unlink(rebased_patches
);
640 free(rebased_patches
);
641 strvec_clear(&am
.args
);
643 ropts
.oid
= &opts
->orig_head
->object
.oid
;
644 ropts
.branch
= opts
->head_name
;
645 ropts
.default_reflog_action
= opts
->reflog_action
;
646 reset_head(the_repository
, &ropts
);
647 error(_("\ngit encountered an error while preparing the "
648 "patches to replay\n"
651 "As a result, git cannot rebase them."),
657 am
.in
= open(rebased_patches
, O_RDONLY
);
659 status
= error_errno(_("could not open '%s' for reading"),
661 free(rebased_patches
);
662 strvec_clear(&am
.args
);
666 strvec_pushv(&am
.args
, opts
->git_am_opts
.v
);
667 strvec_push(&am
.args
, "--rebasing");
668 strvec_pushf(&am
.args
, "--resolvemsg=%s", rebase_resolvemsg
);
669 strvec_push(&am
.args
, "--patch-format=mboxrd");
670 if (opts
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
)
671 strvec_push(&am
.args
, "--rerere-autoupdate");
672 else if (opts
->allow_rerere_autoupdate
== RERERE_NOAUTOUPDATE
)
673 strvec_push(&am
.args
, "--no-rerere-autoupdate");
674 if (opts
->gpg_sign_opt
)
675 strvec_push(&am
.args
, opts
->gpg_sign_opt
);
676 status
= run_command(&am
);
677 unlink(rebased_patches
);
678 free(rebased_patches
);
681 return move_to_original_branch(opts
);
684 if (is_directory(opts
->state_dir
))
685 rebase_write_basic_state(opts
);
690 static int run_specific_rebase(struct rebase_options
*opts
)
694 if (opts
->type
== REBASE_MERGE
) {
695 /* Run sequencer-based rebase */
696 if (!(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
))
697 setenv("GIT_SEQUENCE_EDITOR", ":", 1);
698 if (opts
->gpg_sign_opt
) {
699 /* remove the leading "-S" */
700 char *tmp
= xstrdup(opts
->gpg_sign_opt
+ 2);
701 free(opts
->gpg_sign_opt
);
702 opts
->gpg_sign_opt
= tmp
;
705 status
= run_sequencer_rebase(opts
);
706 } else if (opts
->type
== REBASE_APPLY
)
707 status
= run_am(opts
);
709 BUG("Unhandled rebase type %d", opts
->type
);
711 if (opts
->dont_finish_rebase
)
713 else if (opts
->type
== REBASE_MERGE
)
714 ; /* merge backend cleans up after itself */
715 else if (status
== 0) {
716 if (!file_exists(state_dir_path("stopped-sha", opts
)))
718 } else if (status
== 2) {
719 struct strbuf dir
= STRBUF_INIT
;
721 apply_autostash(state_dir_path("autostash", opts
));
722 strbuf_addstr(&dir
, opts
->state_dir
);
723 remove_dir_recursively(&dir
, 0);
724 strbuf_release(&dir
);
725 die("Nothing to do");
728 return status
? -1 : 0;
731 static void parse_rebase_merges_value(struct rebase_options
*options
, const char *value
)
733 if (!strcmp("no-rebase-cousins", value
))
734 options
->rebase_cousins
= 0;
735 else if (!strcmp("rebase-cousins", value
))
736 options
->rebase_cousins
= 1;
738 die(_("Unknown rebase-merges mode: %s"), value
);
741 static int rebase_config(const char *var
, const char *value
,
742 const struct config_context
*ctx
, void *data
)
744 struct rebase_options
*opts
= data
;
746 if (!strcmp(var
, "rebase.stat")) {
747 if (git_config_bool(var
, value
))
748 opts
->flags
|= REBASE_DIFFSTAT
;
750 opts
->flags
&= ~REBASE_DIFFSTAT
;
754 if (!strcmp(var
, "rebase.autosquash")) {
755 opts
->config_autosquash
= git_config_bool(var
, value
);
759 if (!strcmp(var
, "commit.gpgsign")) {
760 free(opts
->gpg_sign_opt
);
761 opts
->gpg_sign_opt
= git_config_bool(var
, value
) ?
762 xstrdup("-S") : NULL
;
766 if (!strcmp(var
, "rebase.autostash")) {
767 opts
->autostash
= git_config_bool(var
, value
);
771 if (!strcmp(var
, "rebase.rebasemerges")) {
772 opts
->config_rebase_merges
= git_parse_maybe_bool(value
);
773 if (opts
->config_rebase_merges
< 0) {
774 opts
->config_rebase_merges
= 1;
775 parse_rebase_merges_value(opts
, value
);
777 opts
->rebase_cousins
= 0;
782 if (!strcmp(var
, "rebase.updaterefs")) {
783 opts
->config_update_refs
= git_config_bool(var
, value
);
787 if (!strcmp(var
, "rebase.reschedulefailedexec")) {
788 opts
->reschedule_failed_exec
= git_config_bool(var
, value
);
792 if (!strcmp(var
, "rebase.forkpoint")) {
793 opts
->fork_point
= git_config_bool(var
, value
) ? -1 : 0;
797 if (!strcmp(var
, "rebase.backend")) {
798 return git_config_string(&opts
->default_backend
, var
, value
);
801 return git_default_config(var
, value
, ctx
, data
);
804 static int checkout_up_to_date(struct rebase_options
*options
)
806 struct strbuf buf
= STRBUF_INIT
;
807 struct reset_head_opts ropts
= { 0 };
810 strbuf_addf(&buf
, "%s: checkout %s",
811 options
->reflog_action
, options
->switch_to
);
812 ropts
.oid
= &options
->orig_head
->object
.oid
;
813 ropts
.branch
= options
->head_name
;
814 ropts
.flags
= RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
816 ropts
.flags
|= RESET_HEAD_DETACH
;
817 ropts
.head_msg
= buf
.buf
;
818 if (reset_head(the_repository
, &ropts
) < 0)
819 ret
= error(_("could not switch to %s"), options
->switch_to
);
820 strbuf_release(&buf
);
826 * Determines whether the commits in from..to are linear, i.e. contain
827 * no merge commits. This function *expects* `from` to be an ancestor of
830 static int is_linear_history(struct commit
*from
, struct commit
*to
)
832 while (to
&& to
!= from
) {
833 repo_parse_commit(the_repository
, to
);
836 if (to
->parents
->next
)
838 to
= to
->parents
->item
;
843 static int can_fast_forward(struct commit
*onto
, struct commit
*upstream
,
844 struct commit
*restrict_revision
,
845 struct commit
*head
, struct object_id
*branch_base
)
847 struct commit_list
*merge_bases
= NULL
;
850 if (is_null_oid(branch_base
))
851 goto done
; /* fill_branch_base() found multiple merge bases */
853 if (!oideq(branch_base
, &onto
->object
.oid
))
856 if (restrict_revision
&& !oideq(&restrict_revision
->object
.oid
, branch_base
))
862 if (repo_get_merge_bases(the_repository
, upstream
, head
, &merge_bases
) < 0)
864 if (!merge_bases
|| merge_bases
->next
)
867 if (!oideq(&onto
->object
.oid
, &merge_bases
->item
->object
.oid
))
873 free_commit_list(merge_bases
);
874 return res
&& is_linear_history(onto
, head
);
877 static void fill_branch_base(struct rebase_options
*options
,
878 struct object_id
*branch_base
)
880 struct commit_list
*merge_bases
= NULL
;
882 if (repo_get_merge_bases(the_repository
, options
->onto
,
883 options
->orig_head
, &merge_bases
) < 0)
885 if (!merge_bases
|| merge_bases
->next
)
886 oidcpy(branch_base
, null_oid());
888 oidcpy(branch_base
, &merge_bases
->item
->object
.oid
);
890 free_commit_list(merge_bases
);
893 static int parse_opt_am(const struct option
*opt
, const char *arg
, int unset
)
895 struct rebase_options
*opts
= opt
->value
;
897 BUG_ON_OPT_NEG(unset
);
900 if (opts
->type
!= REBASE_UNSPECIFIED
&& opts
->type
!= REBASE_APPLY
)
901 die(_("apply options and merge options cannot be used together"));
903 opts
->type
= REBASE_APPLY
;
908 /* -i followed by -m is still -i */
909 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
911 struct rebase_options
*opts
= opt
->value
;
913 BUG_ON_OPT_NEG(unset
);
916 if (opts
->type
!= REBASE_UNSPECIFIED
&& opts
->type
!= REBASE_MERGE
)
917 die(_("apply options and merge options cannot be used together"));
919 opts
->type
= REBASE_MERGE
;
924 /* -i followed by -r is still explicitly interactive, but -r alone is not */
925 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
928 struct rebase_options
*opts
= opt
->value
;
930 BUG_ON_OPT_NEG(unset
);
933 if (opts
->type
!= REBASE_UNSPECIFIED
&& opts
->type
!= REBASE_MERGE
)
934 die(_("apply options and merge options cannot be used together"));
936 opts
->type
= REBASE_MERGE
;
937 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
942 static enum empty_type
parse_empty_value(const char *value
)
944 if (!strcasecmp(value
, "drop"))
946 else if (!strcasecmp(value
, "keep"))
948 else if (!strcasecmp(value
, "ask"))
951 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"ask\"."), value
);
954 static int parse_opt_keep_empty(const struct option
*opt
, const char *arg
,
957 struct rebase_options
*opts
= opt
->value
;
961 imply_merge(opts
, unset
? "--no-keep-empty" : "--keep-empty");
962 opts
->keep_empty
= !unset
;
966 static int parse_opt_empty(const struct option
*opt
, const char *arg
, int unset
)
968 struct rebase_options
*options
= opt
->value
;
969 enum empty_type value
= parse_empty_value(arg
);
971 BUG_ON_OPT_NEG(unset
);
973 options
->empty
= value
;
977 static int parse_opt_rebase_merges(const struct option
*opt
, const char *arg
, int unset
)
979 struct rebase_options
*options
= opt
->value
;
981 options
->rebase_merges
= !unset
;
982 options
->rebase_cousins
= 0;
986 warning(_("--rebase-merges with an empty string "
987 "argument is deprecated and will stop "
988 "working in a future version of Git. Use "
989 "--rebase-merges without an argument "
990 "instead, which does the same thing."));
993 parse_rebase_merges_value(options
, arg
);
999 static void NORETURN
error_on_missing_default_upstream(void)
1001 struct branch
*current_branch
= branch_get(NULL
);
1004 "Please specify which branch you want to rebase against.\n"
1005 "See git-rebase(1) for details.\n"
1007 " git rebase '<branch>'\n"
1009 current_branch
? _("There is no tracking information for "
1010 "the current branch.") :
1011 _("You are not currently on a branch."));
1013 if (current_branch
) {
1014 const char *remote
= current_branch
->remote_name
;
1017 remote
= _("<remote>");
1019 printf(_("If you wish to set tracking information for this "
1020 "branch you can do so with:\n"
1022 " git branch --set-upstream-to=%s/<branch> %s\n"
1024 remote
, current_branch
->name
);
1029 static int check_exec_cmd(const char *cmd
)
1031 if (strchr(cmd
, '\n'))
1032 return error(_("exec commands cannot contain newlines"));
1034 /* Does the command consist purely of whitespace? */
1035 if (!cmd
[strspn(cmd
, " \t\r\f\v")])
1036 return error(_("empty exec command"));
1041 int cmd_rebase(int argc
, const char **argv
, const char *prefix
)
1043 struct rebase_options options
= REBASE_OPTIONS_INIT
;
1044 const char *branch_name
;
1045 int ret
, flags
, total_argc
, in_progress
= 0;
1047 int ok_to_skip_pre_rebase
= 0;
1048 struct strbuf msg
= STRBUF_INIT
;
1049 struct strbuf revisions
= STRBUF_INIT
;
1050 struct strbuf buf
= STRBUF_INIT
;
1051 struct object_id branch_base
;
1052 int ignore_whitespace
= 0;
1053 const char *gpg_sign
= NULL
;
1054 struct object_id squash_onto
;
1055 char *squash_onto_name
= NULL
;
1056 char *keep_base_onto_name
= NULL
;
1057 int reschedule_failed_exec
= -1;
1058 int allow_preemptive_ff
= 1;
1059 int preserve_merges_selected
= 0;
1060 struct reset_head_opts ropts
= { 0 };
1061 struct option builtin_rebase_options
[] = {
1062 OPT_STRING(0, "onto", &options
.onto_name
,
1064 N_("rebase onto given branch instead of upstream")),
1065 OPT_BOOL(0, "keep-base", &keep_base
,
1066 N_("use the merge-base of upstream and branch as the current base")),
1067 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
1068 N_("allow pre-rebase hook to run")),
1069 OPT_NEGBIT('q', "quiet", &options
.flags
,
1070 N_("be quiet. implies --no-stat"),
1071 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1072 OPT_BIT('v', "verbose", &options
.flags
,
1073 N_("display a diffstat of what changed upstream"),
1074 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
1075 {OPTION_NEGBIT
, 'n', "no-stat", &options
.flags
, NULL
,
1076 N_("do not show diffstat of what changed upstream"),
1077 PARSE_OPT_NOARG
, NULL
, REBASE_DIFFSTAT
},
1078 OPT_BOOL(0, "signoff", &options
.signoff
,
1079 N_("add a Signed-off-by trailer to each commit")),
1080 OPT_BOOL(0, "committer-date-is-author-date",
1081 &options
.committer_date_is_author_date
,
1082 N_("make committer date match author date")),
1083 OPT_BOOL(0, "reset-author-date", &options
.ignore_date
,
1084 N_("ignore author date and use current date")),
1085 OPT_HIDDEN_BOOL(0, "ignore-date", &options
.ignore_date
,
1086 N_("synonym of --reset-author-date")),
1087 OPT_PASSTHRU_ARGV('C', NULL
, &options
.git_am_opts
, N_("n"),
1088 N_("passed to 'git apply'"), 0),
1089 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace
,
1090 N_("ignore changes in whitespace")),
1091 OPT_PASSTHRU_ARGV(0, "whitespace", &options
.git_am_opts
,
1092 N_("action"), N_("passed to 'git apply'"), 0),
1093 OPT_BIT('f', "force-rebase", &options
.flags
,
1094 N_("cherry-pick all commits, even if unchanged"),
1096 OPT_BIT(0, "no-ff", &options
.flags
,
1097 N_("cherry-pick all commits, even if unchanged"),
1099 OPT_CMDMODE(0, "continue", &options
.action
, N_("continue"),
1101 OPT_CMDMODE(0, "skip", &options
.action
,
1102 N_("skip current patch and continue"), ACTION_SKIP
),
1103 OPT_CMDMODE(0, "abort", &options
.action
,
1104 N_("abort and check out the original branch"),
1106 OPT_CMDMODE(0, "quit", &options
.action
,
1107 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
1108 OPT_CMDMODE(0, "edit-todo", &options
.action
, N_("edit the todo list "
1109 "during an interactive rebase"), ACTION_EDIT_TODO
),
1110 OPT_CMDMODE(0, "show-current-patch", &options
.action
,
1111 N_("show the patch file being applied or merged"),
1112 ACTION_SHOW_CURRENT_PATCH
),
1113 OPT_CALLBACK_F(0, "apply", &options
, NULL
,
1114 N_("use apply strategies to rebase"),
1115 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1117 OPT_CALLBACK_F('m', "merge", &options
, NULL
,
1118 N_("use merging strategies to rebase"),
1119 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1121 OPT_CALLBACK_F('i', "interactive", &options
, NULL
,
1122 N_("let the user edit the list of commits to rebase"),
1123 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
1124 parse_opt_interactive
),
1125 OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected
,
1126 N_("(REMOVED) was: try to recreate merges "
1127 "instead of ignoring them"),
1128 1, PARSE_OPT_HIDDEN
),
1129 OPT_RERERE_AUTOUPDATE(&options
.allow_rerere_autoupdate
),
1130 OPT_CALLBACK_F(0, "empty", &options
, "(drop|keep|ask)",
1131 N_("how to handle commits that become empty"),
1132 PARSE_OPT_NONEG
, parse_opt_empty
),
1133 OPT_CALLBACK_F('k', "keep-empty", &options
, NULL
,
1134 N_("keep commits which start empty"),
1135 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
,
1136 parse_opt_keep_empty
),
1137 OPT_BOOL(0, "autosquash", &options
.autosquash
,
1138 N_("move commits that begin with "
1139 "squash!/fixup! under -i")),
1140 OPT_BOOL(0, "update-refs", &options
.update_refs
,
1141 N_("update branches that point to commits "
1142 "that are being rebased")),
1143 { OPTION_STRING
, 'S', "gpg-sign", &gpg_sign
, N_("key-id"),
1144 N_("GPG-sign commits"),
1145 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1146 OPT_AUTOSTASH(&options
.autostash
),
1147 OPT_STRING_LIST('x', "exec", &options
.exec
, N_("exec"),
1148 N_("add exec lines after each commit of the "
1150 OPT_BOOL_F(0, "allow-empty-message",
1151 &options
.allow_empty_message
,
1152 N_("allow rebasing commits with empty messages"),
1154 OPT_CALLBACK_F('r', "rebase-merges", &options
, N_("mode"),
1155 N_("try to rebase merges instead of skipping them"),
1156 PARSE_OPT_OPTARG
, parse_opt_rebase_merges
),
1157 OPT_BOOL(0, "fork-point", &options
.fork_point
,
1158 N_("use 'merge-base --fork-point' to refine upstream")),
1159 OPT_STRING('s', "strategy", &options
.strategy
,
1160 N_("strategy"), N_("use the given merge strategy")),
1161 OPT_STRING_LIST('X', "strategy-option", &options
.strategy_opts
,
1163 N_("pass the argument through to the merge "
1165 OPT_BOOL(0, "root", &options
.root
,
1166 N_("rebase all reachable commits up to the root(s)")),
1167 OPT_BOOL(0, "reschedule-failed-exec",
1168 &reschedule_failed_exec
,
1169 N_("automatically re-schedule any `exec` that fails")),
1170 OPT_BOOL(0, "reapply-cherry-picks", &options
.reapply_cherry_picks
,
1171 N_("apply all changes, even those already present upstream")),
1176 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1177 usage_with_options(builtin_rebase_usage
,
1178 builtin_rebase_options
);
1180 prepare_repo_settings(the_repository
);
1181 the_repository
->settings
.command_requires_full_index
= 0;
1183 git_config(rebase_config
, &options
);
1184 /* options.gpg_sign_opt will be either "-S" or NULL */
1185 gpg_sign
= options
.gpg_sign_opt
? "" : NULL
;
1186 FREE_AND_NULL(options
.gpg_sign_opt
);
1189 strbuf_addf(&buf
, "%s/applying", apply_dir());
1190 if(file_exists(buf
.buf
))
1191 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1193 if (is_directory(apply_dir())) {
1194 options
.type
= REBASE_APPLY
;
1195 options
.state_dir
= apply_dir();
1196 } else if (is_directory(merge_dir())) {
1198 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
1199 if (!(options
.action
== ACTION_ABORT
) && is_directory(buf
.buf
)) {
1200 die(_("`rebase --preserve-merges` (-p) is no longer supported.\n"
1201 "Use `git rebase --abort` to terminate current rebase.\n"
1202 "Or downgrade to v2.33, or earlier, to complete the rebase."));
1205 strbuf_addf(&buf
, "%s/interactive", merge_dir());
1206 options
.type
= REBASE_MERGE
;
1207 if (file_exists(buf
.buf
))
1208 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
1210 options
.state_dir
= merge_dir();
1213 if (options
.type
!= REBASE_UNSPECIFIED
)
1217 argc
= parse_options(argc
, argv
, prefix
,
1218 builtin_rebase_options
,
1219 builtin_rebase_usage
, 0);
1221 if (preserve_merges_selected
)
1222 die(_("--preserve-merges was replaced by --rebase-merges\n"
1223 "Note: Your `pull.rebase` configuration may also be set to 'preserve',\n"
1224 "which is no longer supported; use 'merges' instead"));
1226 if (options
.action
!= ACTION_NONE
&& total_argc
!= 2) {
1227 usage_with_options(builtin_rebase_usage
,
1228 builtin_rebase_options
);
1232 usage_with_options(builtin_rebase_usage
,
1233 builtin_rebase_options
);
1236 if (options
.onto_name
)
1237 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--onto");
1239 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--root");
1241 * --keep-base defaults to --no-fork-point to keep the
1244 if (options
.fork_point
< 0)
1245 options
.fork_point
= 0;
1247 if (options
.root
&& options
.fork_point
> 0)
1248 die(_("options '%s' and '%s' cannot be used together"), "--root", "--fork-point");
1250 if (options
.action
!= ACTION_NONE
&& !in_progress
)
1251 die(_("no rebase in progress"));
1253 if (options
.action
== ACTION_EDIT_TODO
&& !is_merge(&options
))
1254 die(_("The --edit-todo action can only be used during "
1255 "interactive rebase."));
1257 if (trace2_is_enabled()) {
1258 if (is_merge(&options
))
1259 trace2_cmd_mode("interactive");
1260 else if (options
.exec
.nr
)
1261 trace2_cmd_mode("interactive-exec");
1263 trace2_cmd_mode(action_names
[options
.action
]);
1266 options
.reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
1267 options
.reflog_action
=
1268 xstrdup(options
.reflog_action
? options
.reflog_action
: "rebase");
1270 switch (options
.action
) {
1271 case ACTION_CONTINUE
: {
1272 struct object_id head
;
1273 struct lock_file lock_file
= LOCK_INIT
;
1277 if (repo_get_oid(the_repository
, "HEAD", &head
))
1278 die(_("Cannot read HEAD"));
1280 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
1281 if (repo_read_index(the_repository
) < 0)
1282 die(_("could not read index"));
1283 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
1286 repo_update_index_if_able(the_repository
, &lock_file
);
1287 rollback_lock_file(&lock_file
);
1289 if (has_unstaged_changes(the_repository
, 1)) {
1290 puts(_("You must edit all merge conflicts and then\n"
1291 "mark them as resolved using git add"));
1294 if (read_basic_state(&options
))
1299 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1301 rerere_clear(the_repository
, &merge_rr
);
1302 string_list_clear(&merge_rr
, 1);
1303 ropts
.flags
= RESET_HEAD_HARD
;
1304 if (reset_head(the_repository
, &ropts
) < 0)
1305 die(_("could not discard worktree changes"));
1306 remove_branch_state(the_repository
, 0);
1307 if (read_basic_state(&options
))
1311 case ACTION_ABORT
: {
1312 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1313 struct strbuf head_msg
= STRBUF_INIT
;
1315 rerere_clear(the_repository
, &merge_rr
);
1316 string_list_clear(&merge_rr
, 1);
1318 if (read_basic_state(&options
))
1321 strbuf_addf(&head_msg
, "%s (abort): returning to %s",
1322 options
.reflog_action
,
1323 options
.head_name
? options
.head_name
1324 : oid_to_hex(&options
.orig_head
->object
.oid
));
1325 ropts
.oid
= &options
.orig_head
->object
.oid
;
1326 ropts
.head_msg
= head_msg
.buf
;
1327 ropts
.branch
= options
.head_name
;
1328 ropts
.flags
= RESET_HEAD_HARD
;
1329 if (reset_head(the_repository
, &ropts
) < 0)
1330 die(_("could not move back to %s"),
1331 oid_to_hex(&options
.orig_head
->object
.oid
));
1332 strbuf_release(&head_msg
);
1333 remove_branch_state(the_repository
, 0);
1334 ret
= finish_rebase(&options
);
1338 save_autostash(state_dir_path("autostash", &options
));
1339 if (options
.type
== REBASE_MERGE
) {
1340 struct replay_opts replay
= REPLAY_OPTS_INIT
;
1342 replay
.action
= REPLAY_INTERACTIVE_REBASE
;
1343 ret
= sequencer_remove_state(&replay
);
1344 replay_opts_release(&replay
);
1347 strbuf_addstr(&buf
, options
.state_dir
);
1348 ret
= remove_dir_recursively(&buf
, 0);
1350 error(_("could not remove '%s'"),
1355 case ACTION_EDIT_TODO
:
1356 options
.dont_finish_rebase
= 1;
1358 case ACTION_SHOW_CURRENT_PATCH
:
1359 options
.dont_finish_rebase
= 1;
1364 BUG("action: %d", options
.action
);
1367 /* Make sure no rebase is in progress */
1369 const char *last_slash
= strrchr(options
.state_dir
, '/');
1370 const char *state_dir_base
=
1371 last_slash
? last_slash
+ 1 : options
.state_dir
;
1372 const char *cmd_live_rebase
=
1373 "git rebase (--continue | --abort | --skip)";
1375 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
1376 die(_("It seems that there is already a %s directory, and\n"
1377 "I wonder if you are in the middle of another rebase. "
1379 "case, please try\n\t%s\n"
1380 "If that is not the case, please\n\t%s\n"
1381 "and run me again. I am stopping in case you still "
1383 "valuable there.\n"),
1384 state_dir_base
, cmd_live_rebase
, buf
.buf
);
1387 if ((options
.flags
& REBASE_INTERACTIVE_EXPLICIT
) ||
1388 (options
.action
!= ACTION_NONE
) ||
1389 (options
.exec
.nr
> 0) ||
1390 options
.autosquash
== 1) {
1391 allow_preemptive_ff
= 0;
1393 if (options
.committer_date_is_author_date
|| options
.ignore_date
)
1394 options
.flags
|= REBASE_FORCE
;
1396 for (i
= 0; i
< options
.git_am_opts
.nr
; i
++) {
1397 const char *option
= options
.git_am_opts
.v
[i
], *p
;
1398 if (!strcmp(option
, "--whitespace=fix") ||
1399 !strcmp(option
, "--whitespace=strip"))
1400 allow_preemptive_ff
= 0;
1401 else if (skip_prefix(option
, "-C", &p
)) {
1403 if (!isdigit(*(p
++)))
1404 die(_("switch `C' expects a "
1405 "numerical value"));
1406 } else if (skip_prefix(option
, "--whitespace=", &p
)) {
1407 if (*p
&& strcmp(p
, "warn") && strcmp(p
, "nowarn") &&
1408 strcmp(p
, "error") && strcmp(p
, "error-all"))
1409 die("Invalid whitespace option: '%s'", p
);
1413 for (i
= 0; i
< options
.exec
.nr
; i
++)
1414 if (check_exec_cmd(options
.exec
.items
[i
].string
))
1417 if (!(options
.flags
& REBASE_NO_QUIET
))
1418 strvec_push(&options
.git_am_opts
, "-q");
1420 if (options
.empty
!= EMPTY_UNSPECIFIED
)
1421 imply_merge(&options
, "--empty");
1423 if (options
.reapply_cherry_picks
< 0)
1425 * We default to --no-reapply-cherry-picks unless
1426 * --keep-base is given; when --keep-base is given, we want
1427 * to default to --reapply-cherry-picks.
1429 options
.reapply_cherry_picks
= keep_base
;
1430 else if (!keep_base
)
1432 * The apply backend always searches for and drops cherry
1433 * picks. This is often not wanted with --keep-base, so
1434 * --keep-base allows --reapply-cherry-picks to be
1435 * simulated by altering the upstream such that
1436 * cherry-picks cannot be detected and thus all commits are
1437 * reapplied. Thus, --[no-]reapply-cherry-picks is
1438 * supported when --keep-base is specified, but not when
1439 * --keep-base is left out.
1441 imply_merge(&options
, options
.reapply_cherry_picks
?
1442 "--reapply-cherry-picks" :
1443 "--no-reapply-cherry-picks");
1446 options
.gpg_sign_opt
= xstrfmt("-S%s", gpg_sign
);
1448 if (options
.exec
.nr
)
1449 imply_merge(&options
, "--exec");
1451 if (options
.type
== REBASE_APPLY
) {
1452 if (ignore_whitespace
)
1453 strvec_push(&options
.git_am_opts
,
1454 "--ignore-whitespace");
1455 if (options
.committer_date_is_author_date
)
1456 strvec_push(&options
.git_am_opts
,
1457 "--committer-date-is-author-date");
1458 if (options
.ignore_date
)
1459 strvec_push(&options
.git_am_opts
, "--ignore-date");
1462 if (ignore_whitespace
) {
1463 string_list_append(&options
.strategy_opts
,
1464 "ignore-space-change");
1468 if (options
.strategy_opts
.nr
&& !options
.strategy
)
1469 options
.strategy
= "ort";
1471 if (options
.strategy
) {
1472 options
.strategy
= xstrdup(options
.strategy
);
1473 imply_merge(&options
, "--strategy");
1476 if (options
.root
&& !options
.onto_name
)
1477 imply_merge(&options
, "--root without --onto");
1479 if (isatty(2) && options
.flags
& REBASE_NO_QUIET
)
1480 strbuf_addstr(&options
.git_format_patch_opt
, " --progress");
1482 if (options
.git_am_opts
.nr
|| options
.type
== REBASE_APPLY
) {
1483 /* all am options except -q are compatible only with --apply */
1484 for (i
= options
.git_am_opts
.nr
- 1; i
>= 0; i
--)
1485 if (strcmp(options
.git_am_opts
.v
[i
], "-q"))
1488 if (i
>= 0 || options
.type
== REBASE_APPLY
) {
1489 if (is_merge(&options
))
1490 die(_("apply options and merge options "
1491 "cannot be used together"));
1492 else if (options
.rebase_merges
== -1 && options
.config_rebase_merges
== 1)
1493 die(_("apply options are incompatible with rebase.rebaseMerges. Consider adding --no-rebase-merges"));
1494 else if (options
.update_refs
== -1 && options
.config_update_refs
== 1)
1495 die(_("apply options are incompatible with rebase.updateRefs. Consider adding --no-update-refs"));
1497 options
.type
= REBASE_APPLY
;
1501 if (options
.update_refs
== 1)
1502 imply_merge(&options
, "--update-refs");
1503 options
.update_refs
= (options
.update_refs
>= 0) ? options
.update_refs
:
1504 ((options
.config_update_refs
>= 0) ? options
.config_update_refs
: 0);
1506 if (options
.rebase_merges
== 1)
1507 imply_merge(&options
, "--rebase-merges");
1508 options
.rebase_merges
= (options
.rebase_merges
>= 0) ? options
.rebase_merges
:
1509 ((options
.config_rebase_merges
>= 0) ? options
.config_rebase_merges
: 0);
1511 if (options
.autosquash
== 1) {
1512 imply_merge(&options
, "--autosquash");
1513 } else if (options
.autosquash
== -1) {
1514 options
.autosquash
=
1515 options
.config_autosquash
&&
1516 (options
.flags
& REBASE_INTERACTIVE_EXPLICIT
);
1519 if (options
.type
== REBASE_UNSPECIFIED
) {
1520 if (!strcmp(options
.default_backend
, "merge"))
1521 options
.type
= REBASE_MERGE
;
1522 else if (!strcmp(options
.default_backend
, "apply"))
1523 options
.type
= REBASE_APPLY
;
1525 die(_("Unknown rebase backend: %s"),
1526 options
.default_backend
);
1529 if (options
.type
== REBASE_MERGE
&&
1530 !options
.strategy
&&
1531 getenv("GIT_TEST_MERGE_ALGORITHM"))
1532 options
.strategy
= xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
1534 switch (options
.type
) {
1536 options
.state_dir
= merge_dir();
1539 options
.state_dir
= apply_dir();
1542 BUG("options.type was just set above; should be unreachable.");
1545 if (options
.empty
== EMPTY_UNSPECIFIED
) {
1546 if (options
.flags
& REBASE_INTERACTIVE_EXPLICIT
)
1547 options
.empty
= EMPTY_ASK
;
1548 else if (options
.exec
.nr
> 0)
1549 options
.empty
= EMPTY_KEEP
;
1551 options
.empty
= EMPTY_DROP
;
1553 if (reschedule_failed_exec
> 0 && !is_merge(&options
))
1554 die(_("--reschedule-failed-exec requires "
1555 "--exec or --interactive"));
1556 if (reschedule_failed_exec
>= 0)
1557 options
.reschedule_failed_exec
= reschedule_failed_exec
;
1559 if (options
.signoff
) {
1560 strvec_push(&options
.git_am_opts
, "--signoff");
1561 options
.flags
|= REBASE_FORCE
;
1564 if (!options
.root
) {
1566 struct branch
*branch
;
1568 branch
= branch_get(NULL
);
1569 options
.upstream_name
= branch_get_upstream(branch
,
1571 if (!options
.upstream_name
)
1572 error_on_missing_default_upstream();
1573 if (options
.fork_point
< 0)
1574 options
.fork_point
= 1;
1576 options
.upstream_name
= argv
[0];
1579 if (!strcmp(options
.upstream_name
, "-"))
1580 options
.upstream_name
= "@{-1}";
1583 lookup_commit_reference_by_name(options
.upstream_name
);
1584 if (!options
.upstream
)
1585 die(_("invalid upstream '%s'"), options
.upstream_name
);
1586 options
.upstream_arg
= options
.upstream_name
;
1588 if (!options
.onto_name
) {
1589 if (commit_tree("", 0, the_hash_algo
->empty_tree
, NULL
,
1590 &squash_onto
, NULL
, NULL
) < 0)
1591 die(_("Could not create new root commit"));
1592 options
.squash_onto
= &squash_onto
;
1593 options
.onto_name
= squash_onto_name
=
1594 xstrdup(oid_to_hex(&squash_onto
));
1596 options
.root_with_onto
= 1;
1598 options
.upstream_name
= NULL
;
1599 options
.upstream
= NULL
;
1601 usage_with_options(builtin_rebase_usage
,
1602 builtin_rebase_options
);
1603 options
.upstream_arg
= "--root";
1607 * If the branch to rebase is given, that is the branch we will rebase
1608 * branch_name -- branch/commit being rebased, or
1609 * HEAD (already detached)
1610 * orig_head -- commit object name of tip of the branch before rebasing
1611 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1614 /* Is it "rebase other branchname" or "rebase other commit"? */
1615 struct object_id branch_oid
;
1616 branch_name
= argv
[0];
1617 options
.switch_to
= argv
[0];
1619 /* Is it a local branch? */
1621 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
1622 if (!read_ref(buf
.buf
, &branch_oid
)) {
1623 die_if_checked_out(buf
.buf
, 1);
1624 options
.head_name
= xstrdup(buf
.buf
);
1626 lookup_commit_object(the_repository
,
1628 /* If not is it a valid ref (branch or commit)? */
1631 lookup_commit_reference_by_name(branch_name
);
1632 options
.head_name
= NULL
;
1634 if (!options
.orig_head
)
1635 die(_("no such branch/commit '%s'"), branch_name
);
1636 } else if (argc
== 0) {
1637 /* Do not need to switch branches, we are already on it. */
1639 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL
,
1641 if (!options
.head_name
)
1642 die(_("No such ref: %s"), "HEAD");
1643 if (flags
& REF_ISSYMREF
) {
1644 if (!skip_prefix(options
.head_name
,
1645 "refs/heads/", &branch_name
))
1646 branch_name
= options
.head_name
;
1649 FREE_AND_NULL(options
.head_name
);
1650 branch_name
= "HEAD";
1652 options
.orig_head
= lookup_commit_reference_by_name("HEAD");
1653 if (!options
.orig_head
)
1654 die(_("Could not resolve HEAD to a commit"));
1656 BUG("unexpected number of arguments left to parse");
1658 /* Make sure the branch to rebase onto is valid. */
1661 strbuf_addstr(&buf
, options
.upstream_name
);
1662 strbuf_addstr(&buf
, "...");
1663 strbuf_addstr(&buf
, branch_name
);
1664 options
.onto_name
= keep_base_onto_name
= xstrdup(buf
.buf
);
1665 } else if (!options
.onto_name
)
1666 options
.onto_name
= options
.upstream_name
;
1667 if (strstr(options
.onto_name
, "...")) {
1668 if (repo_get_oid_mb(the_repository
, options
.onto_name
, &branch_base
) < 0) {
1670 die(_("'%s': need exactly one merge base with branch"),
1671 options
.upstream_name
);
1673 die(_("'%s': need exactly one merge base"),
1676 options
.onto
= lookup_commit_or_die(&branch_base
,
1680 lookup_commit_reference_by_name(options
.onto_name
);
1682 die(_("Does not point to a valid commit '%s'"),
1684 fill_branch_base(&options
, &branch_base
);
1687 if (keep_base
&& options
.reapply_cherry_picks
)
1688 options
.upstream
= options
.onto
;
1690 if (options
.fork_point
> 0)
1691 options
.restrict_revision
=
1692 get_fork_point(options
.upstream_name
, options
.orig_head
);
1694 if (repo_read_index(the_repository
) < 0)
1695 die(_("could not read index"));
1697 if (options
.autostash
)
1698 create_autostash(the_repository
,
1699 state_dir_path("autostash", &options
));
1702 if (require_clean_work_tree(the_repository
, "rebase",
1703 _("Please commit or stash them."), 1, 1)) {
1709 * Now we are rebasing commits upstream..orig_head (or with --root,
1710 * everything leading up to orig_head) on top of onto.
1714 * Check if we are already based on onto with linear history,
1715 * in which case we could fast-forward without replacing the commits
1716 * with new commits recreated by replaying their changes.
1718 if (allow_preemptive_ff
&&
1719 can_fast_forward(options
.onto
, options
.upstream
, options
.restrict_revision
,
1720 options
.orig_head
, &branch_base
)) {
1723 if (!(options
.flags
& REBASE_FORCE
)) {
1724 /* Lazily switch to the target branch if needed... */
1725 if (options
.switch_to
) {
1726 ret
= checkout_up_to_date(&options
);
1731 if (!(options
.flags
& REBASE_NO_QUIET
))
1733 else if (!strcmp(branch_name
, "HEAD") &&
1734 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1735 puts(_("HEAD is up to date."));
1737 printf(_("Current branch %s is up to date.\n"),
1739 ret
= finish_rebase(&options
);
1741 } else if (!(options
.flags
& REBASE_NO_QUIET
))
1743 else if (!strcmp(branch_name
, "HEAD") &&
1744 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1745 puts(_("HEAD is up to date, rebase forced."));
1747 printf(_("Current branch %s is up to date, rebase "
1748 "forced.\n"), branch_name
);
1751 /* If a hook exists, give it a chance to interrupt*/
1752 if (!ok_to_skip_pre_rebase
&&
1753 run_hooks_l("pre-rebase", options
.upstream_arg
,
1754 argc
? argv
[0] : NULL
, NULL
))
1755 die(_("The pre-rebase hook refused to rebase."));
1757 if (options
.flags
& REBASE_DIFFSTAT
) {
1758 struct diff_options opts
;
1760 if (options
.flags
& REBASE_VERBOSE
) {
1761 if (is_null_oid(&branch_base
))
1762 printf(_("Changes to %s:\n"),
1763 oid_to_hex(&options
.onto
->object
.oid
));
1765 printf(_("Changes from %s to %s:\n"),
1766 oid_to_hex(&branch_base
),
1767 oid_to_hex(&options
.onto
->object
.oid
));
1770 /* We want color (if set), but no pager */
1771 repo_diff_setup(the_repository
, &opts
);
1772 init_diffstat_widths(&opts
);
1773 opts
.output_format
|=
1774 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1775 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1776 diff_setup_done(&opts
);
1777 diff_tree_oid(is_null_oid(&branch_base
) ?
1778 the_hash_algo
->empty_tree
: &branch_base
,
1779 &options
.onto
->object
.oid
, "", &opts
);
1780 diffcore_std(&opts
);
1784 if (is_merge(&options
))
1787 /* Detach HEAD and reset the tree */
1788 if (options
.flags
& REBASE_NO_QUIET
)
1789 printf(_("First, rewinding head to replay your work on top of "
1792 strbuf_addf(&msg
, "%s (start): checkout %s",
1793 options
.reflog_action
, options
.onto_name
);
1794 ropts
.oid
= &options
.onto
->object
.oid
;
1795 ropts
.orig_head
= &options
.orig_head
->object
.oid
,
1796 ropts
.flags
= RESET_HEAD_DETACH
| RESET_ORIG_HEAD
|
1797 RESET_HEAD_RUN_POST_CHECKOUT_HOOK
;
1798 ropts
.head_msg
= msg
.buf
;
1799 ropts
.default_reflog_action
= options
.reflog_action
;
1800 if (reset_head(the_repository
, &ropts
))
1801 die(_("Could not detach HEAD"));
1802 strbuf_release(&msg
);
1805 * If the onto is a proper descendant of the tip of the branch, then
1806 * we just fast-forwarded.
1808 if (oideq(&branch_base
, &options
.orig_head
->object
.oid
)) {
1809 printf(_("Fast-forwarded %s to %s.\n"),
1810 branch_name
, options
.onto_name
);
1811 move_to_original_branch(&options
);
1812 ret
= finish_rebase(&options
);
1816 strbuf_addf(&revisions
, "%s..%s",
1817 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
1818 (options
.restrict_revision
?
1819 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
1820 oid_to_hex(&options
.upstream
->object
.oid
)),
1821 oid_to_hex(&options
.orig_head
->object
.oid
));
1823 options
.revisions
= revisions
.buf
;
1826 ret
= run_specific_rebase(&options
);
1829 strbuf_release(&buf
);
1830 strbuf_release(&revisions
);
1831 free(options
.reflog_action
);
1832 free(options
.head_name
);
1833 strvec_clear(&options
.git_am_opts
);
1834 free(options
.gpg_sign_opt
);
1835 string_list_clear(&options
.exec
, 0);
1836 free(options
.strategy
);
1837 string_list_clear(&options
.strategy_opts
, 0);
1838 strbuf_release(&options
.git_format_patch_opt
);
1839 free(squash_onto_name
);
1840 free(keep_base_onto_name
);