2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
8 #include "run-command.h"
10 #include "argv-array.h"
16 #include "cache-tree.h"
17 #include "unpack-trees.h"
19 #include "parse-options.h"
22 #include "wt-status.h"
24 #include "commit-reach.h"
27 static char const * const builtin_rebase_usage
[] = {
28 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
29 "[<upstream>] [<branch>]"),
30 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
32 N_("git rebase --continue | --abort | --skip | --edit-todo"),
36 static GIT_PATH_FUNC(apply_dir
, "rebase-apply")
37 static GIT_PATH_FUNC(merge_dir
, "rebase-merge")
40 REBASE_UNSPECIFIED
= -1,
44 REBASE_PRESERVE_MERGES
47 static int use_builtin_rebase(void)
49 struct child_process cp
= CHILD_PROCESS_INIT
;
50 struct strbuf out
= STRBUF_INIT
;
53 argv_array_pushl(&cp
.args
,
54 "config", "--bool", "rebase.usebuiltin", NULL
);
56 if (capture_command(&cp
, &out
, 6)) {
62 ret
= !strcmp("true", out
.buf
);
67 struct rebase_options
{
68 enum rebase_type type
;
69 const char *state_dir
;
70 struct commit
*upstream
;
71 const char *upstream_name
;
72 const char *upstream_arg
;
74 struct object_id orig_head
;
76 const char *onto_name
;
77 const char *revisions
;
78 const char *switch_to
;
80 struct object_id
*squash_onto
;
81 struct commit
*restrict_revision
;
82 int dont_finish_rebase
;
84 REBASE_NO_QUIET
= 1<<0,
85 REBASE_VERBOSE
= 1<<1,
86 REBASE_DIFFSTAT
= 1<<2,
88 REBASE_INTERACTIVE_EXPLICIT
= 1<<4,
90 struct strbuf git_am_opt
;
93 int allow_rerere_autoupdate
;
99 int allow_empty_message
;
100 int rebase_merges
, rebase_cousins
;
101 char *strategy
, *strategy_opts
;
102 struct strbuf git_format_patch_opt
;
105 static int is_interactive(struct rebase_options
*opts
)
107 return opts
->type
== REBASE_INTERACTIVE
||
108 opts
->type
== REBASE_PRESERVE_MERGES
;
111 static void imply_interactive(struct rebase_options
*opts
, const char *option
)
113 switch (opts
->type
) {
115 die(_("%s requires an interactive rebase"), option
);
117 case REBASE_INTERACTIVE
:
118 case REBASE_PRESERVE_MERGES
:
121 /* we silently *upgrade* --merge to --interactive if needed */
123 opts
->type
= REBASE_INTERACTIVE
; /* implied */
128 /* Returns the filename prefixed by the state_dir */
129 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
131 static struct strbuf path
= STRBUF_INIT
;
132 static size_t prefix_len
;
135 strbuf_addf(&path
, "%s/", opts
->state_dir
);
136 prefix_len
= path
.len
;
139 strbuf_setlen(&path
, prefix_len
);
140 strbuf_addstr(&path
, filename
);
144 /* Read one file, then strip line endings */
145 static int read_one(const char *path
, struct strbuf
*buf
)
147 if (strbuf_read_file(buf
, path
, 0) < 0)
148 return error_errno(_("could not read '%s'"), path
);
149 strbuf_trim_trailing_newline(buf
);
153 /* Initialize the rebase options from the state directory. */
154 static int read_basic_state(struct rebase_options
*opts
)
156 struct strbuf head_name
= STRBUF_INIT
;
157 struct strbuf buf
= STRBUF_INIT
;
158 struct object_id oid
;
160 if (read_one(state_dir_path("head-name", opts
), &head_name
) ||
161 read_one(state_dir_path("onto", opts
), &buf
))
163 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
164 xstrdup(head_name
.buf
) : NULL
;
165 strbuf_release(&head_name
);
166 if (get_oid(buf
.buf
, &oid
))
167 return error(_("could not get 'onto': '%s'"), buf
.buf
);
168 opts
->onto
= lookup_commit_or_die(&oid
, buf
.buf
);
171 * We always write to orig-head, but interactive rebase used to write to
172 * head. Fall back to reading from head to cover for the case that the
173 * user upgraded git with an ongoing interactive rebase.
176 if (file_exists(state_dir_path("orig-head", opts
))) {
177 if (read_one(state_dir_path("orig-head", opts
), &buf
))
179 } else if (read_one(state_dir_path("head", opts
), &buf
))
181 if (get_oid(buf
.buf
, &opts
->orig_head
))
182 return error(_("invalid orig-head: '%s'"), buf
.buf
);
185 if (read_one(state_dir_path("quiet", opts
), &buf
))
188 opts
->flags
&= ~REBASE_NO_QUIET
;
190 opts
->flags
|= REBASE_NO_QUIET
;
192 if (file_exists(state_dir_path("verbose", opts
)))
193 opts
->flags
|= REBASE_VERBOSE
;
195 if (file_exists(state_dir_path("signoff", opts
))) {
197 opts
->flags
|= REBASE_FORCE
;
200 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts
))) {
202 if (read_one(state_dir_path("allow_rerere_autoupdate", opts
),
205 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
206 opts
->allow_rerere_autoupdate
= 1;
207 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
208 opts
->allow_rerere_autoupdate
= 0;
210 warning(_("ignoring invalid allow_rerere_autoupdate: "
213 opts
->allow_rerere_autoupdate
= -1;
215 if (file_exists(state_dir_path("gpg_sign_opt", opts
))) {
217 if (read_one(state_dir_path("gpg_sign_opt", opts
),
220 free(opts
->gpg_sign_opt
);
221 opts
->gpg_sign_opt
= xstrdup(buf
.buf
);
224 if (file_exists(state_dir_path("strategy", opts
))) {
226 if (read_one(state_dir_path("strategy", opts
), &buf
))
228 free(opts
->strategy
);
229 opts
->strategy
= xstrdup(buf
.buf
);
232 if (file_exists(state_dir_path("strategy_opts", opts
))) {
234 if (read_one(state_dir_path("strategy_opts", opts
), &buf
))
236 free(opts
->strategy_opts
);
237 opts
->strategy_opts
= xstrdup(buf
.buf
);
240 strbuf_release(&buf
);
245 static int apply_autostash(struct rebase_options
*opts
)
247 const char *path
= state_dir_path("autostash", opts
);
248 struct strbuf autostash
= STRBUF_INIT
;
249 struct child_process stash_apply
= CHILD_PROCESS_INIT
;
251 if (!file_exists(path
))
254 if (read_one(path
, &autostash
))
255 return error(_("Could not read '%s'"), path
);
256 /* Ensure that the hash is not mistaken for a number */
257 strbuf_addstr(&autostash
, "^0");
258 argv_array_pushl(&stash_apply
.args
,
259 "stash", "apply", autostash
.buf
, NULL
);
260 stash_apply
.git_cmd
= 1;
261 stash_apply
.no_stderr
= stash_apply
.no_stdout
=
262 stash_apply
.no_stdin
= 1;
263 if (!run_command(&stash_apply
))
264 printf(_("Applied autostash.\n"));
266 struct argv_array args
= ARGV_ARRAY_INIT
;
269 argv_array_pushl(&args
,
270 "stash", "store", "-m", "autostash", "-q",
271 autostash
.buf
, NULL
);
272 if (run_command_v_opt(args
.argv
, RUN_GIT_CMD
))
273 res
= error(_("Cannot store %s"), autostash
.buf
);
274 argv_array_clear(&args
);
275 strbuf_release(&autostash
);
280 _("Applying autostash resulted in conflicts.\n"
281 "Your changes are safe in the stash.\n"
282 "You can run \"git stash pop\" or \"git stash drop\" "
286 strbuf_release(&autostash
);
290 static int finish_rebase(struct rebase_options
*opts
)
292 struct strbuf dir
= STRBUF_INIT
;
293 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
295 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
296 apply_autostash(opts
);
297 close_all_packs(the_repository
->objects
);
299 * We ignore errors in 'gc --auto', since the
300 * user should see them.
302 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
303 strbuf_addstr(&dir
, opts
->state_dir
);
304 remove_dir_recursively(&dir
, 0);
305 strbuf_release(&dir
);
310 static struct commit
*peel_committish(const char *name
)
313 struct object_id oid
;
315 if (get_oid(name
, &oid
))
317 obj
= parse_object(the_repository
, &oid
);
318 return (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
321 static void add_var(struct strbuf
*buf
, const char *name
, const char *value
)
324 strbuf_addf(buf
, "unset %s; ", name
);
326 strbuf_addf(buf
, "%s=", name
);
327 sq_quote_buf(buf
, value
);
328 strbuf_addstr(buf
, "; ");
332 static const char *resolvemsg
=
333 N_("Resolve all conflicts manually, mark them as resolved with\n"
334 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
335 "You can instead skip this commit: run \"git rebase --skip\".\n"
336 "To abort and get back to the state before \"git rebase\", run "
337 "\"git rebase --abort\".");
339 static int run_specific_rebase(struct rebase_options
*opts
)
341 const char *argv
[] = { NULL
, NULL
};
342 struct strbuf script_snippet
= STRBUF_INIT
;
344 const char *backend
, *backend_func
;
346 if (opts
->type
== REBASE_INTERACTIVE
) {
347 /* Run builtin interactive rebase */
348 struct child_process child
= CHILD_PROCESS_INIT
;
350 argv_array_pushf(&child
.env_array
, "GIT_CHERRY_PICK_HELP=%s",
352 if (!(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
)) {
353 argv_array_push(&child
.env_array
, "GIT_EDITOR=:");
354 opts
->autosquash
= 0;
358 argv_array_push(&child
.args
, "rebase--interactive");
361 argv_array_pushf(&child
.args
, "--%s", opts
->action
);
362 if (opts
->keep_empty
)
363 argv_array_push(&child
.args
, "--keep-empty");
364 if (opts
->rebase_merges
)
365 argv_array_push(&child
.args
, "--rebase-merges");
366 if (opts
->rebase_cousins
)
367 argv_array_push(&child
.args
, "--rebase-cousins");
368 if (opts
->autosquash
)
369 argv_array_push(&child
.args
, "--autosquash");
370 if (opts
->flags
& REBASE_VERBOSE
)
371 argv_array_push(&child
.args
, "--verbose");
372 if (opts
->flags
& REBASE_FORCE
)
373 argv_array_push(&child
.args
, "--no-ff");
374 if (opts
->restrict_revision
)
375 argv_array_pushf(&child
.args
,
376 "--restrict-revision=^%s",
377 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
379 argv_array_pushf(&child
.args
, "--upstream=%s",
380 oid_to_hex(&opts
->upstream
->object
.oid
));
382 argv_array_pushf(&child
.args
, "--onto=%s",
383 oid_to_hex(&opts
->onto
->object
.oid
));
384 if (opts
->squash_onto
)
385 argv_array_pushf(&child
.args
, "--squash-onto=%s",
386 oid_to_hex(opts
->squash_onto
));
388 argv_array_pushf(&child
.args
, "--onto-name=%s",
390 argv_array_pushf(&child
.args
, "--head-name=%s",
392 opts
->head_name
: "detached HEAD");
394 argv_array_pushf(&child
.args
, "--strategy=%s",
396 if (opts
->strategy_opts
)
397 argv_array_pushf(&child
.args
, "--strategy-opts=%s",
398 opts
->strategy_opts
);
400 argv_array_pushf(&child
.args
, "--switch-to=%s",
403 argv_array_pushf(&child
.args
, "--cmd=%s", opts
->cmd
);
404 if (opts
->allow_empty_message
)
405 argv_array_push(&child
.args
, "--allow-empty-message");
406 if (opts
->allow_rerere_autoupdate
> 0)
407 argv_array_push(&child
.args
, "--rerere-autoupdate");
408 else if (opts
->allow_rerere_autoupdate
== 0)
409 argv_array_push(&child
.args
, "--no-rerere-autoupdate");
410 if (opts
->gpg_sign_opt
)
411 argv_array_push(&child
.args
, opts
->gpg_sign_opt
);
413 argv_array_push(&child
.args
, "--signoff");
415 status
= run_command(&child
);
416 goto finished_rebase
;
419 add_var(&script_snippet
, "GIT_DIR", absolute_path(get_git_dir()));
420 add_var(&script_snippet
, "state_dir", opts
->state_dir
);
422 add_var(&script_snippet
, "upstream_name", opts
->upstream_name
);
423 add_var(&script_snippet
, "upstream", opts
->upstream
?
424 oid_to_hex(&opts
->upstream
->object
.oid
) : NULL
);
425 add_var(&script_snippet
, "head_name",
426 opts
->head_name
? opts
->head_name
: "detached HEAD");
427 add_var(&script_snippet
, "orig_head", oid_to_hex(&opts
->orig_head
));
428 add_var(&script_snippet
, "onto", opts
->onto
?
429 oid_to_hex(&opts
->onto
->object
.oid
) : NULL
);
430 add_var(&script_snippet
, "onto_name", opts
->onto_name
);
431 add_var(&script_snippet
, "revisions", opts
->revisions
);
432 add_var(&script_snippet
, "restrict_revision", opts
->restrict_revision
?
433 oid_to_hex(&opts
->restrict_revision
->object
.oid
) : NULL
);
434 add_var(&script_snippet
, "GIT_QUIET",
435 opts
->flags
& REBASE_NO_QUIET
? "" : "t");
436 add_var(&script_snippet
, "git_am_opt", opts
->git_am_opt
.buf
);
437 add_var(&script_snippet
, "verbose",
438 opts
->flags
& REBASE_VERBOSE
? "t" : "");
439 add_var(&script_snippet
, "diffstat",
440 opts
->flags
& REBASE_DIFFSTAT
? "t" : "");
441 add_var(&script_snippet
, "force_rebase",
442 opts
->flags
& REBASE_FORCE
? "t" : "");
444 add_var(&script_snippet
, "switch_to", opts
->switch_to
);
445 add_var(&script_snippet
, "action", opts
->action
? opts
->action
: "");
446 add_var(&script_snippet
, "signoff", opts
->signoff
? "--signoff" : "");
447 add_var(&script_snippet
, "allow_rerere_autoupdate",
448 opts
->allow_rerere_autoupdate
< 0 ? "" :
449 opts
->allow_rerere_autoupdate
?
450 "--rerere-autoupdate" : "--no-rerere-autoupdate");
451 add_var(&script_snippet
, "keep_empty", opts
->keep_empty
? "yes" : "");
452 add_var(&script_snippet
, "autosquash", opts
->autosquash
? "t" : "");
453 add_var(&script_snippet
, "gpg_sign_opt", opts
->gpg_sign_opt
);
454 add_var(&script_snippet
, "cmd", opts
->cmd
);
455 add_var(&script_snippet
, "allow_empty_message",
456 opts
->allow_empty_message
? "--allow-empty-message" : "");
457 add_var(&script_snippet
, "rebase_merges",
458 opts
->rebase_merges
? "t" : "");
459 add_var(&script_snippet
, "rebase_cousins",
460 opts
->rebase_cousins
? "t" : "");
461 add_var(&script_snippet
, "strategy", opts
->strategy
);
462 add_var(&script_snippet
, "strategy_opts", opts
->strategy_opts
);
463 add_var(&script_snippet
, "rebase_root", opts
->root
? "t" : "");
464 add_var(&script_snippet
, "squash_onto",
465 opts
->squash_onto
? oid_to_hex(opts
->squash_onto
) : "");
466 add_var(&script_snippet
, "git_format_patch_opt",
467 opts
->git_format_patch_opt
.buf
);
469 if (is_interactive(opts
) &&
470 !(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
)) {
471 strbuf_addstr(&script_snippet
,
472 "GIT_EDITOR=:; export GIT_EDITOR; ");
473 opts
->autosquash
= 0;
476 switch (opts
->type
) {
478 backend
= "git-rebase--am";
479 backend_func
= "git_rebase__am";
482 backend
= "git-rebase--merge";
483 backend_func
= "git_rebase__merge";
485 case REBASE_PRESERVE_MERGES
:
486 backend
= "git-rebase--preserve-merges";
487 backend_func
= "git_rebase__preserve_merges";
490 BUG("Unhandled rebase type %d", opts
->type
);
494 strbuf_addf(&script_snippet
,
495 ". git-sh-setup && . git-rebase--common &&"
496 " . %s && %s", backend
, backend_func
);
497 argv
[0] = script_snippet
.buf
;
499 status
= run_command_v_opt(argv
, RUN_USING_SHELL
);
501 if (opts
->dont_finish_rebase
)
503 else if (opts
->type
== REBASE_INTERACTIVE
)
504 ; /* interactive rebase cleans up after itself */
505 else if (status
== 0) {
506 if (!file_exists(state_dir_path("stopped-sha", opts
)))
508 } else if (status
== 2) {
509 struct strbuf dir
= STRBUF_INIT
;
511 apply_autostash(opts
);
512 strbuf_addstr(&dir
, opts
->state_dir
);
513 remove_dir_recursively(&dir
, 0);
514 strbuf_release(&dir
);
515 die("Nothing to do");
518 strbuf_release(&script_snippet
);
520 return status
? -1 : 0;
523 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
525 #define RESET_HEAD_DETACH (1<<0)
526 #define RESET_HEAD_HARD (1<<1)
528 static int reset_head(struct object_id
*oid
, const char *action
,
529 const char *switch_to_branch
, unsigned flags
,
530 const char *reflog_orig_head
, const char *reflog_head
)
532 unsigned detach_head
= flags
& RESET_HEAD_DETACH
;
533 unsigned reset_hard
= flags
& RESET_HEAD_HARD
;
534 struct object_id head_oid
;
535 struct tree_desc desc
[2] = { { NULL
}, { NULL
} };
536 struct lock_file lock
= LOCK_INIT
;
537 struct unpack_trees_options unpack_tree_opts
;
539 const char *reflog_action
;
540 struct strbuf msg
= STRBUF_INIT
;
542 struct object_id
*orig
= NULL
, oid_orig
,
543 *old_orig
= NULL
, oid_old_orig
;
546 if (switch_to_branch
&& !starts_with(switch_to_branch
, "refs/"))
547 BUG("Not a fully qualified branch: '%s'", switch_to_branch
);
549 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0) {
551 goto leave_reset_head
;
554 if ((!oid
|| !reset_hard
) && get_oid("HEAD", &head_oid
)) {
555 ret
= error(_("could not determine HEAD revision"));
556 goto leave_reset_head
;
562 memset(&unpack_tree_opts
, 0, sizeof(unpack_tree_opts
));
563 setup_unpack_trees_porcelain(&unpack_tree_opts
, action
);
564 unpack_tree_opts
.head_idx
= 1;
565 unpack_tree_opts
.src_index
= the_repository
->index
;
566 unpack_tree_opts
.dst_index
= the_repository
->index
;
567 unpack_tree_opts
.fn
= reset_hard
? oneway_merge
: twoway_merge
;
568 unpack_tree_opts
.update
= 1;
569 unpack_tree_opts
.merge
= 1;
571 unpack_tree_opts
.reset
= 1;
573 if (read_index_unmerged(the_repository
->index
) < 0) {
574 ret
= error(_("could not read index"));
575 goto leave_reset_head
;
578 if (!reset_hard
&& !fill_tree_descriptor(&desc
[nr
++], &head_oid
)) {
579 ret
= error(_("failed to find tree of %s"),
580 oid_to_hex(&head_oid
));
581 goto leave_reset_head
;
584 if (!fill_tree_descriptor(&desc
[nr
++], oid
)) {
585 ret
= error(_("failed to find tree of %s"), oid_to_hex(oid
));
586 goto leave_reset_head
;
589 if (unpack_trees(nr
, desc
, &unpack_tree_opts
)) {
591 goto leave_reset_head
;
594 tree
= parse_tree_indirect(oid
);
595 prime_cache_tree(the_repository
->index
, tree
);
597 if (write_locked_index(the_repository
->index
, &lock
, COMMIT_LOCK
) < 0) {
598 ret
= error(_("could not write index"));
599 goto leave_reset_head
;
602 reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
603 strbuf_addf(&msg
, "%s: ", reflog_action
? reflog_action
: "rebase");
604 prefix_len
= msg
.len
;
606 if (!get_oid("ORIG_HEAD", &oid_old_orig
))
607 old_orig
= &oid_old_orig
;
608 if (!get_oid("HEAD", &oid_orig
)) {
610 if (!reflog_orig_head
) {
611 strbuf_addstr(&msg
, "updating ORIG_HEAD");
612 reflog_orig_head
= msg
.buf
;
614 update_ref(reflog_orig_head
, "ORIG_HEAD", orig
, old_orig
, 0,
615 UPDATE_REFS_MSG_ON_ERR
);
617 delete_ref(NULL
, "ORIG_HEAD", old_orig
, 0);
619 strbuf_setlen(&msg
, prefix_len
);
620 strbuf_addstr(&msg
, "updating HEAD");
621 reflog_head
= msg
.buf
;
623 if (!switch_to_branch
)
624 ret
= update_ref(reflog_head
, "HEAD", oid
, orig
, REF_NO_DEREF
,
625 UPDATE_REFS_MSG_ON_ERR
);
627 ret
= create_symref("HEAD", switch_to_branch
, msg
.buf
);
629 ret
= update_ref(reflog_head
, "HEAD", oid
, NULL
, 0,
630 UPDATE_REFS_MSG_ON_ERR
);
634 strbuf_release(&msg
);
635 rollback_lock_file(&lock
);
637 free((void *)desc
[--nr
].buffer
);
641 static int rebase_config(const char *var
, const char *value
, void *data
)
643 struct rebase_options
*opts
= data
;
645 if (!strcmp(var
, "rebase.stat")) {
646 if (git_config_bool(var
, value
))
647 opts
->flags
|= REBASE_DIFFSTAT
;
649 opts
->flags
&= !REBASE_DIFFSTAT
;
653 if (!strcmp(var
, "rebase.autosquash")) {
654 opts
->autosquash
= git_config_bool(var
, value
);
658 if (!strcmp(var
, "commit.gpgsign")) {
659 free(opts
->gpg_sign_opt
);
660 opts
->gpg_sign_opt
= git_config_bool(var
, value
) ?
661 xstrdup("-S") : NULL
;
665 if (!strcmp(var
, "rebase.autostash")) {
666 opts
->autostash
= git_config_bool(var
, value
);
670 return git_default_config(var
, value
, data
);
674 * Determines whether the commits in from..to are linear, i.e. contain
675 * no merge commits. This function *expects* `from` to be an ancestor of
678 static int is_linear_history(struct commit
*from
, struct commit
*to
)
680 while (to
&& to
!= from
) {
684 if (to
->parents
->next
)
686 to
= to
->parents
->item
;
691 static int can_fast_forward(struct commit
*onto
, struct object_id
*head_oid
,
692 struct object_id
*merge_base
)
694 struct commit
*head
= lookup_commit(the_repository
, head_oid
);
695 struct commit_list
*merge_bases
;
701 merge_bases
= get_merge_bases(onto
, head
);
702 if (merge_bases
&& !merge_bases
->next
) {
703 oidcpy(merge_base
, &merge_bases
->item
->object
.oid
);
704 res
= oideq(merge_base
, &onto
->object
.oid
);
706 oidcpy(merge_base
, &null_oid
);
709 free_commit_list(merge_bases
);
710 return res
&& is_linear_history(onto
, head
);
713 /* -i followed by -m is still -i */
714 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
716 struct rebase_options
*opts
= opt
->value
;
718 if (!is_interactive(opts
))
719 opts
->type
= REBASE_MERGE
;
724 /* -i followed by -p is still explicitly interactive, but -p alone is not */
725 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
728 struct rebase_options
*opts
= opt
->value
;
730 opts
->type
= REBASE_INTERACTIVE
;
731 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
736 static void NORETURN
error_on_missing_default_upstream(void)
738 struct branch
*current_branch
= branch_get(NULL
);
741 "Please specify which branch you want to rebase against.\n"
742 "See git-rebase(1) for details.\n"
744 " git rebase '<branch>'\n"
746 current_branch
? _("There is no tracking information for "
747 "the current branch.") :
748 _("You are not currently on a branch."));
750 if (current_branch
) {
751 const char *remote
= current_branch
->remote_name
;
754 remote
= _("<remote>");
756 printf(_("If you wish to set tracking information for this "
757 "branch you can do so with:\n"
759 " git branch --set-upstream-to=%s/<branch> %s\n"
761 remote
, current_branch
->name
);
766 int cmd_rebase(int argc
, const char **argv
, const char *prefix
)
768 struct rebase_options options
= {
769 .type
= REBASE_UNSPECIFIED
,
770 .flags
= REBASE_NO_QUIET
,
771 .git_am_opt
= STRBUF_INIT
,
772 .allow_rerere_autoupdate
= -1,
773 .allow_empty_message
= 1,
774 .git_format_patch_opt
= STRBUF_INIT
,
776 const char *branch_name
;
777 int ret
, flags
, total_argc
, in_progress
= 0;
778 int ok_to_skip_pre_rebase
= 0;
779 struct strbuf msg
= STRBUF_INIT
;
780 struct strbuf revisions
= STRBUF_INIT
;
781 struct strbuf buf
= STRBUF_INIT
;
782 struct object_id merge_base
;
790 ACTION_SHOW_CURRENT_PATCH
,
791 } action
= NO_ACTION
;
792 int committer_date_is_author_date
= 0;
794 int ignore_whitespace
= 0;
795 const char *gpg_sign
= NULL
;
797 struct string_list whitespace
= STRING_LIST_INIT_NODUP
;
798 struct string_list exec
= STRING_LIST_INIT_NODUP
;
799 const char *rebase_merges
= NULL
;
801 struct string_list strategy_options
= STRING_LIST_INIT_NODUP
;
802 struct object_id squash_onto
;
803 char *squash_onto_name
= NULL
;
804 struct option builtin_rebase_options
[] = {
805 OPT_STRING(0, "onto", &options
.onto_name
,
807 N_("rebase onto given branch instead of upstream")),
808 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
809 N_("allow pre-rebase hook to run")),
810 OPT_NEGBIT('q', "quiet", &options
.flags
,
811 N_("be quiet. implies --no-stat"),
812 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
813 OPT_BIT('v', "verbose", &options
.flags
,
814 N_("display a diffstat of what changed upstream"),
815 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
816 {OPTION_NEGBIT
, 'n', "no-stat", &options
.flags
, NULL
,
817 N_("do not show diffstat of what changed upstream"),
818 PARSE_OPT_NOARG
, NULL
, REBASE_DIFFSTAT
},
819 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace
,
820 N_("passed to 'git apply'")),
821 OPT_BOOL(0, "signoff", &options
.signoff
,
822 N_("add a Signed-off-by: line to each commit")),
823 OPT_BOOL(0, "committer-date-is-author-date",
824 &committer_date_is_author_date
,
825 N_("passed to 'git am'")),
826 OPT_BOOL(0, "ignore-date", &ignore_date
,
827 N_("passed to 'git am'")),
828 OPT_BIT('f', "force-rebase", &options
.flags
,
829 N_("cherry-pick all commits, even if unchanged"),
831 OPT_BIT(0, "no-ff", &options
.flags
,
832 N_("cherry-pick all commits, even if unchanged"),
834 OPT_CMDMODE(0, "continue", &action
, N_("continue"),
836 OPT_CMDMODE(0, "skip", &action
,
837 N_("skip current patch and continue"), ACTION_SKIP
),
838 OPT_CMDMODE(0, "abort", &action
,
839 N_("abort and check out the original branch"),
841 OPT_CMDMODE(0, "quit", &action
,
842 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
843 OPT_CMDMODE(0, "edit-todo", &action
, N_("edit the todo list "
844 "during an interactive rebase"), ACTION_EDIT_TODO
),
845 OPT_CMDMODE(0, "show-current-patch", &action
,
846 N_("show the patch file being applied or merged"),
847 ACTION_SHOW_CURRENT_PATCH
),
848 { OPTION_CALLBACK
, 'm', "merge", &options
, NULL
,
849 N_("use merging strategies to rebase"),
850 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
852 { OPTION_CALLBACK
, 'i', "interactive", &options
, NULL
,
853 N_("let the user edit the list of commits to rebase"),
854 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
855 parse_opt_interactive
},
856 OPT_SET_INT('p', "preserve-merges", &options
.type
,
857 N_("try to recreate merges instead of ignoring "
858 "them"), REBASE_PRESERVE_MERGES
),
859 OPT_BOOL(0, "rerere-autoupdate",
860 &options
.allow_rerere_autoupdate
,
861 N_("allow rerere to update index with resolved "
863 OPT_BOOL('k', "keep-empty", &options
.keep_empty
,
864 N_("preserve empty commits during rebase")),
865 OPT_BOOL(0, "autosquash", &options
.autosquash
,
866 N_("move commits that begin with "
867 "squash!/fixup! under -i")),
868 { OPTION_STRING
, 'S', "gpg-sign", &gpg_sign
, N_("key-id"),
869 N_("GPG-sign commits"),
870 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
871 OPT_STRING_LIST(0, "whitespace", &whitespace
,
872 N_("whitespace"), N_("passed to 'git apply'")),
873 OPT_SET_INT('C', NULL
, &opt_c
, N_("passed to 'git apply'"),
875 OPT_BOOL(0, "autostash", &options
.autostash
,
876 N_("automatically stash/stash pop before and after")),
877 OPT_STRING_LIST('x', "exec", &exec
, N_("exec"),
878 N_("add exec lines after each commit of the "
880 OPT_BOOL(0, "allow-empty-message",
881 &options
.allow_empty_message
,
882 N_("allow rebasing commits with empty messages")),
883 {OPTION_STRING
, 'r', "rebase-merges", &rebase_merges
,
885 N_("try to rebase merges instead of skipping them"),
886 PARSE_OPT_OPTARG
, NULL
, (intptr_t)""},
887 OPT_BOOL(0, "fork-point", &fork_point
,
888 N_("use 'merge-base --fork-point' to refine upstream")),
889 OPT_STRING('s', "strategy", &options
.strategy
,
890 N_("strategy"), N_("use the given merge strategy")),
891 OPT_STRING_LIST('X', "strategy-option", &strategy_options
,
893 N_("pass the argument through to the merge "
895 OPT_BOOL(0, "root", &options
.root
,
896 N_("rebase all reachable commits up to the root(s)")),
901 * NEEDSWORK: Once the builtin rebase has been tested enough
902 * and git-legacy-rebase.sh is retired to contrib/, this preamble
906 if (!use_builtin_rebase()) {
907 const char *path
= mkpath("%s/git-legacy-rebase",
910 if (sane_execvp(path
, (char **)argv
) < 0)
911 die_errno(_("could not exec %s"), path
);
913 BUG("sane_execvp() returned???");
916 if (argc
== 2 && !strcmp(argv
[1], "-h"))
917 usage_with_options(builtin_rebase_usage
,
918 builtin_rebase_options
);
920 prefix
= setup_git_directory();
921 trace_repo_setup(prefix
);
924 git_config(rebase_config
, &options
);
927 strbuf_addf(&buf
, "%s/applying", apply_dir());
928 if(file_exists(buf
.buf
))
929 die(_("It looks like 'git am' is in progress. Cannot rebase."));
931 if (is_directory(apply_dir())) {
932 options
.type
= REBASE_AM
;
933 options
.state_dir
= apply_dir();
934 } else if (is_directory(merge_dir())) {
936 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
937 if (is_directory(buf
.buf
)) {
938 options
.type
= REBASE_PRESERVE_MERGES
;
939 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
942 strbuf_addf(&buf
, "%s/interactive", merge_dir());
943 if(file_exists(buf
.buf
)) {
944 options
.type
= REBASE_INTERACTIVE
;
945 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
947 options
.type
= REBASE_MERGE
;
949 options
.state_dir
= merge_dir();
952 if (options
.type
!= REBASE_UNSPECIFIED
)
956 argc
= parse_options(argc
, argv
, prefix
,
957 builtin_rebase_options
,
958 builtin_rebase_usage
, 0);
960 if (action
!= NO_ACTION
&& total_argc
!= 2) {
961 usage_with_options(builtin_rebase_usage
,
962 builtin_rebase_options
);
966 usage_with_options(builtin_rebase_usage
,
967 builtin_rebase_options
);
969 if (action
!= NO_ACTION
&& !in_progress
)
970 die(_("No rebase in progress?"));
972 if (action
== ACTION_EDIT_TODO
&& !is_interactive(&options
))
973 die(_("The --edit-todo action can only be used during "
974 "interactive rebase."));
977 case ACTION_CONTINUE
: {
978 struct object_id head
;
979 struct lock_file lock_file
= LOCK_INIT
;
982 options
.action
= "continue";
985 if (get_oid("HEAD", &head
))
986 die(_("Cannot read HEAD"));
988 fd
= hold_locked_index(&lock_file
, 0);
989 if (read_index(the_repository
->index
) < 0)
990 die(_("could not read index"));
991 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
994 update_index_if_able(the_repository
->index
,
996 rollback_lock_file(&lock_file
);
998 if (has_unstaged_changes(1)) {
999 puts(_("You must edit all merge conflicts and then\n"
1000 "mark them as resolved using git add"));
1003 if (read_basic_state(&options
))
1008 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1010 options
.action
= "skip";
1012 rerere_clear(&merge_rr
);
1013 string_list_clear(&merge_rr
, 1);
1015 if (reset_head(NULL
, "reset", NULL
, RESET_HEAD_HARD
,
1017 die(_("could not discard worktree changes"));
1018 if (read_basic_state(&options
))
1022 case ACTION_ABORT
: {
1023 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1024 options
.action
= "abort";
1026 rerere_clear(&merge_rr
);
1027 string_list_clear(&merge_rr
, 1);
1029 if (read_basic_state(&options
))
1031 if (reset_head(&options
.orig_head
, "reset",
1032 options
.head_name
, RESET_HEAD_HARD
,
1034 die(_("could not move back to %s"),
1035 oid_to_hex(&options
.orig_head
));
1036 ret
= finish_rebase(&options
);
1041 strbuf_addstr(&buf
, options
.state_dir
);
1042 ret
= !!remove_dir_recursively(&buf
, 0);
1044 die(_("could not remove '%s'"), options
.state_dir
);
1047 case ACTION_EDIT_TODO
:
1048 options
.action
= "edit-todo";
1049 options
.dont_finish_rebase
= 1;
1051 case ACTION_SHOW_CURRENT_PATCH
:
1052 options
.action
= "show-current-patch";
1053 options
.dont_finish_rebase
= 1;
1058 BUG("action: %d", action
);
1061 /* Make sure no rebase is in progress */
1063 const char *last_slash
= strrchr(options
.state_dir
, '/');
1064 const char *state_dir_base
=
1065 last_slash
? last_slash
+ 1 : options
.state_dir
;
1066 const char *cmd_live_rebase
=
1067 "git rebase (--continue | --abort | --skip)";
1069 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
1070 die(_("It seems that there is already a %s directory, and\n"
1071 "I wonder if you are in the middle of another rebase. "
1073 "case, please try\n\t%s\n"
1074 "If that is not the case, please\n\t%s\n"
1075 "and run me again. I am stopping in case you still "
1077 "valuable there.\n"),
1078 state_dir_base
, cmd_live_rebase
, buf
.buf
);
1081 if (!(options
.flags
& REBASE_NO_QUIET
))
1082 strbuf_addstr(&options
.git_am_opt
, " -q");
1084 if (committer_date_is_author_date
) {
1085 strbuf_addstr(&options
.git_am_opt
,
1086 " --committer-date-is-author-date");
1087 options
.flags
|= REBASE_FORCE
;
1090 if (ignore_whitespace
)
1091 strbuf_addstr(&options
.git_am_opt
, " --ignore-whitespace");
1094 strbuf_addstr(&options
.git_am_opt
, " --ignore-date");
1095 options
.flags
|= REBASE_FORCE
;
1098 if (options
.keep_empty
)
1099 imply_interactive(&options
, "--keep-empty");
1102 free(options
.gpg_sign_opt
);
1103 options
.gpg_sign_opt
= xstrfmt("-S%s", gpg_sign
);
1107 strbuf_addf(&options
.git_am_opt
, " -C%d", opt_c
);
1109 if (whitespace
.nr
) {
1112 for (i
= 0; i
< whitespace
.nr
; i
++) {
1113 const char *item
= whitespace
.items
[i
].string
;
1115 strbuf_addf(&options
.git_am_opt
, " --whitespace=%s",
1118 if ((!strcmp(item
, "fix")) || (!strcmp(item
, "strip")))
1119 options
.flags
|= REBASE_FORCE
;
1126 imply_interactive(&options
, "--exec");
1129 for (i
= 0; i
< exec
.nr
; i
++)
1130 strbuf_addf(&buf
, "exec %s\n", exec
.items
[i
].string
);
1131 options
.cmd
= xstrdup(buf
.buf
);
1134 if (rebase_merges
) {
1135 if (!*rebase_merges
)
1136 ; /* default mode; do nothing */
1137 else if (!strcmp("rebase-cousins", rebase_merges
))
1138 options
.rebase_cousins
= 1;
1139 else if (strcmp("no-rebase-cousins", rebase_merges
))
1140 die(_("Unknown mode: %s"), rebase_merges
);
1141 options
.rebase_merges
= 1;
1142 imply_interactive(&options
, "--rebase-merges");
1145 if (strategy_options
.nr
) {
1148 if (!options
.strategy
)
1149 options
.strategy
= "recursive";
1152 for (i
= 0; i
< strategy_options
.nr
; i
++)
1153 strbuf_addf(&buf
, " --%s",
1154 strategy_options
.items
[i
].string
);
1155 options
.strategy_opts
= xstrdup(buf
.buf
);
1158 if (options
.strategy
) {
1159 options
.strategy
= xstrdup(options
.strategy
);
1160 switch (options
.type
) {
1162 die(_("--strategy requires --merge or --interactive"));
1164 case REBASE_INTERACTIVE
:
1165 case REBASE_PRESERVE_MERGES
:
1168 case REBASE_UNSPECIFIED
:
1169 options
.type
= REBASE_MERGE
;
1172 BUG("unhandled rebase type (%d)", options
.type
);
1176 if (options
.root
&& !options
.onto_name
)
1177 imply_interactive(&options
, "--root without --onto");
1179 if (isatty(2) && options
.flags
& REBASE_NO_QUIET
)
1180 strbuf_addstr(&options
.git_format_patch_opt
, " --progress");
1182 switch (options
.type
) {
1184 case REBASE_INTERACTIVE
:
1185 case REBASE_PRESERVE_MERGES
:
1186 options
.state_dir
= merge_dir();
1189 options
.state_dir
= apply_dir();
1192 /* the default rebase backend is `--am` */
1193 options
.type
= REBASE_AM
;
1194 options
.state_dir
= apply_dir();
1198 if (options
.git_am_opt
.len
) {
1201 /* all am options except -q are compatible only with --am */
1203 strbuf_addbuf(&buf
, &options
.git_am_opt
);
1204 strbuf_addch(&buf
, ' ');
1205 while ((p
= strstr(buf
.buf
, " -q ")))
1206 strbuf_splice(&buf
, p
- buf
.buf
, 4, " ", 1);
1209 if (is_interactive(&options
) && buf
.len
)
1210 die(_("error: cannot combine interactive options "
1211 "(--interactive, --exec, --rebase-merges, "
1212 "--preserve-merges, --keep-empty, --root + "
1213 "--onto) with am options (%s)"), buf
.buf
);
1214 if (options
.type
== REBASE_MERGE
&& buf
.len
)
1215 die(_("error: cannot combine merge options (--merge, "
1216 "--strategy, --strategy-option) with am options "
1220 if (options
.signoff
) {
1221 if (options
.type
== REBASE_PRESERVE_MERGES
)
1222 die("cannot combine '--signoff' with "
1223 "'--preserve-merges'");
1224 strbuf_addstr(&options
.git_am_opt
, " --signoff");
1225 options
.flags
|= REBASE_FORCE
;
1228 if (options
.type
== REBASE_PRESERVE_MERGES
)
1230 * Note: incompatibility with --signoff handled in signoff block above
1231 * Note: incompatibility with --interactive is just a strong warning;
1232 * git-rebase.txt caveats with "unless you know what you are doing"
1234 if (options
.rebase_merges
)
1235 die(_("error: cannot combine '--preserve-merges' with "
1236 "'--rebase-merges'"));
1238 if (options
.rebase_merges
) {
1239 if (strategy_options
.nr
)
1240 die(_("error: cannot combine '--rebase-merges' with "
1241 "'--strategy-option'"));
1242 if (options
.strategy
)
1243 die(_("error: cannot combine '--rebase-merges' with "
1247 if (!options
.root
) {
1249 struct branch
*branch
;
1251 branch
= branch_get(NULL
);
1252 options
.upstream_name
= branch_get_upstream(branch
,
1254 if (!options
.upstream_name
)
1255 error_on_missing_default_upstream();
1259 options
.upstream_name
= argv
[0];
1262 if (!strcmp(options
.upstream_name
, "-"))
1263 options
.upstream_name
= "@{-1}";
1265 options
.upstream
= peel_committish(options
.upstream_name
);
1266 if (!options
.upstream
)
1267 die(_("invalid upstream '%s'"), options
.upstream_name
);
1268 options
.upstream_arg
= options
.upstream_name
;
1270 if (!options
.onto_name
) {
1271 if (commit_tree("", 0, the_hash_algo
->empty_tree
, NULL
,
1272 &squash_onto
, NULL
, NULL
) < 0)
1273 die(_("Could not create new root commit"));
1274 options
.squash_onto
= &squash_onto
;
1275 options
.onto_name
= squash_onto_name
=
1276 xstrdup(oid_to_hex(&squash_onto
));
1278 options
.upstream_name
= NULL
;
1279 options
.upstream
= NULL
;
1281 usage_with_options(builtin_rebase_usage
,
1282 builtin_rebase_options
);
1283 options
.upstream_arg
= "--root";
1286 /* Make sure the branch to rebase onto is valid. */
1287 if (!options
.onto_name
)
1288 options
.onto_name
= options
.upstream_name
;
1289 if (strstr(options
.onto_name
, "...")) {
1290 if (get_oid_mb(options
.onto_name
, &merge_base
) < 0)
1291 die(_("'%s': need exactly one merge base"),
1293 options
.onto
= lookup_commit_or_die(&merge_base
,
1296 options
.onto
= peel_committish(options
.onto_name
);
1298 die(_("Does not point to a valid commit '%s'"),
1303 * If the branch to rebase is given, that is the branch we will rebase
1304 * branch_name -- branch/commit being rebased, or
1305 * HEAD (already detached)
1306 * orig_head -- commit object name of tip of the branch before rebasing
1307 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1310 /* Is it "rebase other branchname" or "rebase other commit"? */
1311 branch_name
= argv
[0];
1312 options
.switch_to
= argv
[0];
1314 /* Is it a local branch? */
1316 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
1317 if (!read_ref(buf
.buf
, &options
.orig_head
))
1318 options
.head_name
= xstrdup(buf
.buf
);
1319 /* If not is it a valid ref (branch or commit)? */
1320 else if (!get_oid(branch_name
, &options
.orig_head
))
1321 options
.head_name
= NULL
;
1323 die(_("fatal: no such branch/commit '%s'"),
1325 } else if (argc
== 0) {
1326 /* Do not need to switch branches, we are already on it. */
1328 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL
,
1330 if (!options
.head_name
)
1331 die(_("No such ref: %s"), "HEAD");
1332 if (flags
& REF_ISSYMREF
) {
1333 if (!skip_prefix(options
.head_name
,
1334 "refs/heads/", &branch_name
))
1335 branch_name
= options
.head_name
;
1338 free(options
.head_name
);
1339 options
.head_name
= NULL
;
1340 branch_name
= "HEAD";
1342 if (get_oid("HEAD", &options
.orig_head
))
1343 die(_("Could not resolve HEAD to a revision"));
1345 BUG("unexpected number of arguments left to parse");
1347 if (fork_point
> 0) {
1348 struct commit
*head
=
1349 lookup_commit_reference(the_repository
,
1350 &options
.orig_head
);
1351 options
.restrict_revision
=
1352 get_fork_point(options
.upstream_name
, head
);
1355 if (read_index(the_repository
->index
) < 0)
1356 die(_("could not read index"));
1358 if (options
.autostash
) {
1359 struct lock_file lock_file
= LOCK_INIT
;
1362 fd
= hold_locked_index(&lock_file
, 0);
1363 refresh_cache(REFRESH_QUIET
);
1365 update_index_if_able(&the_index
, &lock_file
);
1366 rollback_lock_file(&lock_file
);
1368 if (has_unstaged_changes(1) || has_uncommitted_changes(1)) {
1369 const char *autostash
=
1370 state_dir_path("autostash", &options
);
1371 struct child_process stash
= CHILD_PROCESS_INIT
;
1372 struct object_id oid
;
1373 struct commit
*head
=
1374 lookup_commit_reference(the_repository
,
1375 &options
.orig_head
);
1377 argv_array_pushl(&stash
.args
,
1378 "stash", "create", "autostash", NULL
);
1382 if (capture_command(&stash
, &buf
, GIT_MAX_HEXSZ
))
1383 die(_("Cannot autostash"));
1384 strbuf_trim_trailing_newline(&buf
);
1385 if (get_oid(buf
.buf
, &oid
))
1386 die(_("Unexpected stash response: '%s'"),
1389 strbuf_add_unique_abbrev(&buf
, &oid
, DEFAULT_ABBREV
);
1391 if (safe_create_leading_directories_const(autostash
))
1392 die(_("Could not create directory for '%s'"),
1394 write_file(autostash
, "%s", oid_to_hex(&oid
));
1395 printf(_("Created autostash: %s\n"), buf
.buf
);
1396 if (reset_head(&head
->object
.oid
, "reset --hard",
1397 NULL
, RESET_HEAD_HARD
, NULL
, NULL
) < 0)
1398 die(_("could not reset --hard"));
1399 printf(_("HEAD is now at %s"),
1400 find_unique_abbrev(&head
->object
.oid
,
1403 pp_commit_easy(CMIT_FMT_ONELINE
, head
, &buf
);
1405 printf(" %s", buf
.buf
);
1408 if (discard_index(the_repository
->index
) < 0 ||
1409 read_index(the_repository
->index
) < 0)
1410 die(_("could not read index"));
1414 if (require_clean_work_tree("rebase",
1415 _("Please commit or stash them."), 1, 1)) {
1421 * Now we are rebasing commits upstream..orig_head (or with --root,
1422 * everything leading up to orig_head) on top of onto.
1426 * Check if we are already based on onto with linear history,
1427 * but this should be done only when upstream and onto are the same
1428 * and if this is not an interactive rebase.
1430 if (can_fast_forward(options
.onto
, &options
.orig_head
, &merge_base
) &&
1431 !is_interactive(&options
) && !options
.restrict_revision
&&
1433 !oidcmp(&options
.upstream
->object
.oid
, &options
.onto
->object
.oid
)) {
1436 if (!(options
.flags
& REBASE_FORCE
)) {
1437 /* Lazily switch to the target branch if needed... */
1438 if (options
.switch_to
) {
1439 struct object_id oid
;
1441 if (get_oid(options
.switch_to
, &oid
) < 0) {
1442 ret
= !!error(_("could not parse '%s'"),
1448 strbuf_addf(&buf
, "rebase: checkout %s",
1450 if (reset_head(&oid
, "checkout",
1451 options
.head_name
, 0,
1453 ret
= !!error(_("could not switch to "
1460 if (!(options
.flags
& REBASE_NO_QUIET
))
1462 else if (!strcmp(branch_name
, "HEAD") &&
1463 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1464 puts(_("HEAD is up to date."));
1466 printf(_("Current branch %s is up to date.\n"),
1468 ret
= !!finish_rebase(&options
);
1470 } else if (!(options
.flags
& REBASE_NO_QUIET
))
1472 else if (!strcmp(branch_name
, "HEAD") &&
1473 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1474 puts(_("HEAD is up to date, rebase forced."));
1476 printf(_("Current branch %s is up to date, rebase "
1477 "forced.\n"), branch_name
);
1480 /* If a hook exists, give it a chance to interrupt*/
1481 if (!ok_to_skip_pre_rebase
&&
1482 run_hook_le(NULL
, "pre-rebase", options
.upstream_arg
,
1483 argc
? argv
[0] : NULL
, NULL
))
1484 die(_("The pre-rebase hook refused to rebase."));
1486 if (options
.flags
& REBASE_DIFFSTAT
) {
1487 struct diff_options opts
;
1489 if (options
.flags
& REBASE_VERBOSE
)
1490 printf(_("Changes from %s to %s:\n"),
1491 oid_to_hex(&merge_base
),
1492 oid_to_hex(&options
.onto
->object
.oid
));
1494 /* We want color (if set), but no pager */
1496 opts
.stat_width
= -1; /* use full terminal width */
1497 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
1498 opts
.output_format
|=
1499 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1500 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1501 diff_setup_done(&opts
);
1502 diff_tree_oid(&merge_base
, &options
.onto
->object
.oid
,
1504 diffcore_std(&opts
);
1508 if (is_interactive(&options
))
1511 /* Detach HEAD and reset the tree */
1512 if (options
.flags
& REBASE_NO_QUIET
)
1513 printf(_("First, rewinding head to replay your work on top of "
1516 strbuf_addf(&msg
, "rebase: checkout %s", options
.onto_name
);
1517 if (reset_head(&options
.onto
->object
.oid
, "checkout", NULL
,
1518 RESET_HEAD_DETACH
, NULL
, msg
.buf
))
1519 die(_("Could not detach HEAD"));
1520 strbuf_release(&msg
);
1523 * If the onto is a proper descendant of the tip of the branch, then
1524 * we just fast-forwarded.
1527 if (!oidcmp(&merge_base
, &options
.orig_head
)) {
1528 printf(_("Fast-forwarded %s to %s. \n"),
1529 branch_name
, options
.onto_name
);
1530 strbuf_addf(&msg
, "rebase finished: %s onto %s",
1531 options
.head_name
? options
.head_name
: "detached HEAD",
1532 oid_to_hex(&options
.onto
->object
.oid
));
1533 reset_head(NULL
, "Fast-forwarded", options
.head_name
, 0,
1535 strbuf_release(&msg
);
1536 ret
= !!finish_rebase(&options
);
1540 strbuf_addf(&revisions
, "%s..%s",
1541 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
1542 (options
.restrict_revision
?
1543 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
1544 oid_to_hex(&options
.upstream
->object
.oid
)),
1545 oid_to_hex(&options
.orig_head
));
1547 options
.revisions
= revisions
.buf
;
1550 ret
= !!run_specific_rebase(&options
);
1553 strbuf_release(&revisions
);
1554 free(options
.head_name
);
1555 free(options
.gpg_sign_opt
);
1557 free(squash_onto_name
);