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
;
51 int ret
, env
= git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1);
56 argv_array_pushl(&cp
.args
,
57 "config", "--bool", "rebase.usebuiltin", NULL
);
59 if (capture_command(&cp
, &out
, 6)) {
65 ret
= !strcmp("true", out
.buf
);
70 struct rebase_options
{
71 enum rebase_type type
;
72 const char *state_dir
;
73 struct commit
*upstream
;
74 const char *upstream_name
;
75 const char *upstream_arg
;
77 struct object_id orig_head
;
79 const char *onto_name
;
80 const char *revisions
;
81 const char *switch_to
;
83 struct object_id
*squash_onto
;
84 struct commit
*restrict_revision
;
85 int dont_finish_rebase
;
87 REBASE_NO_QUIET
= 1<<0,
88 REBASE_VERBOSE
= 1<<1,
89 REBASE_DIFFSTAT
= 1<<2,
91 REBASE_INTERACTIVE_EXPLICIT
= 1<<4,
93 struct strbuf git_am_opt
;
96 int allow_rerere_autoupdate
;
102 int allow_empty_message
;
103 int rebase_merges
, rebase_cousins
;
104 char *strategy
, *strategy_opts
;
105 struct strbuf git_format_patch_opt
;
108 static int is_interactive(struct rebase_options
*opts
)
110 return opts
->type
== REBASE_INTERACTIVE
||
111 opts
->type
== REBASE_PRESERVE_MERGES
;
114 static void imply_interactive(struct rebase_options
*opts
, const char *option
)
116 switch (opts
->type
) {
118 die(_("%s requires an interactive rebase"), option
);
120 case REBASE_INTERACTIVE
:
121 case REBASE_PRESERVE_MERGES
:
124 /* we silently *upgrade* --merge to --interactive if needed */
126 opts
->type
= REBASE_INTERACTIVE
; /* implied */
131 /* Returns the filename prefixed by the state_dir */
132 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
134 static struct strbuf path
= STRBUF_INIT
;
135 static size_t prefix_len
;
138 strbuf_addf(&path
, "%s/", opts
->state_dir
);
139 prefix_len
= path
.len
;
142 strbuf_setlen(&path
, prefix_len
);
143 strbuf_addstr(&path
, filename
);
147 /* Read one file, then strip line endings */
148 static int read_one(const char *path
, struct strbuf
*buf
)
150 if (strbuf_read_file(buf
, path
, 0) < 0)
151 return error_errno(_("could not read '%s'"), path
);
152 strbuf_trim_trailing_newline(buf
);
156 /* Initialize the rebase options from the state directory. */
157 static int read_basic_state(struct rebase_options
*opts
)
159 struct strbuf head_name
= STRBUF_INIT
;
160 struct strbuf buf
= STRBUF_INIT
;
161 struct object_id oid
;
163 if (read_one(state_dir_path("head-name", opts
), &head_name
) ||
164 read_one(state_dir_path("onto", opts
), &buf
))
166 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
167 xstrdup(head_name
.buf
) : NULL
;
168 strbuf_release(&head_name
);
169 if (get_oid(buf
.buf
, &oid
))
170 return error(_("could not get 'onto': '%s'"), buf
.buf
);
171 opts
->onto
= lookup_commit_or_die(&oid
, buf
.buf
);
174 * We always write to orig-head, but interactive rebase used to write to
175 * head. Fall back to reading from head to cover for the case that the
176 * user upgraded git with an ongoing interactive rebase.
179 if (file_exists(state_dir_path("orig-head", opts
))) {
180 if (read_one(state_dir_path("orig-head", opts
), &buf
))
182 } else if (read_one(state_dir_path("head", opts
), &buf
))
184 if (get_oid(buf
.buf
, &opts
->orig_head
))
185 return error(_("invalid orig-head: '%s'"), buf
.buf
);
188 if (read_one(state_dir_path("quiet", opts
), &buf
))
191 opts
->flags
&= ~REBASE_NO_QUIET
;
193 opts
->flags
|= REBASE_NO_QUIET
;
195 if (file_exists(state_dir_path("verbose", opts
)))
196 opts
->flags
|= REBASE_VERBOSE
;
198 if (file_exists(state_dir_path("signoff", opts
))) {
200 opts
->flags
|= REBASE_FORCE
;
203 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts
))) {
205 if (read_one(state_dir_path("allow_rerere_autoupdate", opts
),
208 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
209 opts
->allow_rerere_autoupdate
= 1;
210 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
211 opts
->allow_rerere_autoupdate
= 0;
213 warning(_("ignoring invalid allow_rerere_autoupdate: "
216 opts
->allow_rerere_autoupdate
= -1;
218 if (file_exists(state_dir_path("gpg_sign_opt", opts
))) {
220 if (read_one(state_dir_path("gpg_sign_opt", opts
),
223 free(opts
->gpg_sign_opt
);
224 opts
->gpg_sign_opt
= xstrdup(buf
.buf
);
227 if (file_exists(state_dir_path("strategy", opts
))) {
229 if (read_one(state_dir_path("strategy", opts
), &buf
))
231 free(opts
->strategy
);
232 opts
->strategy
= xstrdup(buf
.buf
);
235 if (file_exists(state_dir_path("strategy_opts", opts
))) {
237 if (read_one(state_dir_path("strategy_opts", opts
), &buf
))
239 free(opts
->strategy_opts
);
240 opts
->strategy_opts
= xstrdup(buf
.buf
);
243 strbuf_release(&buf
);
248 static int apply_autostash(struct rebase_options
*opts
)
250 const char *path
= state_dir_path("autostash", opts
);
251 struct strbuf autostash
= STRBUF_INIT
;
252 struct child_process stash_apply
= CHILD_PROCESS_INIT
;
254 if (!file_exists(path
))
257 if (read_one(path
, &autostash
))
258 return error(_("Could not read '%s'"), path
);
259 /* Ensure that the hash is not mistaken for a number */
260 strbuf_addstr(&autostash
, "^0");
261 argv_array_pushl(&stash_apply
.args
,
262 "stash", "apply", autostash
.buf
, NULL
);
263 stash_apply
.git_cmd
= 1;
264 stash_apply
.no_stderr
= stash_apply
.no_stdout
=
265 stash_apply
.no_stdin
= 1;
266 if (!run_command(&stash_apply
))
267 printf(_("Applied autostash.\n"));
269 struct argv_array args
= ARGV_ARRAY_INIT
;
272 argv_array_pushl(&args
,
273 "stash", "store", "-m", "autostash", "-q",
274 autostash
.buf
, NULL
);
275 if (run_command_v_opt(args
.argv
, RUN_GIT_CMD
))
276 res
= error(_("Cannot store %s"), autostash
.buf
);
277 argv_array_clear(&args
);
278 strbuf_release(&autostash
);
283 _("Applying autostash resulted in conflicts.\n"
284 "Your changes are safe in the stash.\n"
285 "You can run \"git stash pop\" or \"git stash drop\" "
289 strbuf_release(&autostash
);
293 static int finish_rebase(struct rebase_options
*opts
)
295 struct strbuf dir
= STRBUF_INIT
;
296 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
298 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
299 apply_autostash(opts
);
300 close_all_packs(the_repository
->objects
);
302 * We ignore errors in 'gc --auto', since the
303 * user should see them.
305 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
306 strbuf_addstr(&dir
, opts
->state_dir
);
307 remove_dir_recursively(&dir
, 0);
308 strbuf_release(&dir
);
313 static struct commit
*peel_committish(const char *name
)
316 struct object_id oid
;
318 if (get_oid(name
, &oid
))
320 obj
= parse_object(the_repository
, &oid
);
321 return (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
324 static void add_var(struct strbuf
*buf
, const char *name
, const char *value
)
327 strbuf_addf(buf
, "unset %s; ", name
);
329 strbuf_addf(buf
, "%s=", name
);
330 sq_quote_buf(buf
, value
);
331 strbuf_addstr(buf
, "; ");
335 static const char *resolvemsg
=
336 N_("Resolve all conflicts manually, mark them as resolved with\n"
337 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
338 "You can instead skip this commit: run \"git rebase --skip\".\n"
339 "To abort and get back to the state before \"git rebase\", run "
340 "\"git rebase --abort\".");
342 static int run_specific_rebase(struct rebase_options
*opts
)
344 const char *argv
[] = { NULL
, NULL
};
345 struct strbuf script_snippet
= STRBUF_INIT
;
347 const char *backend
, *backend_func
;
349 if (opts
->type
== REBASE_INTERACTIVE
) {
350 /* Run builtin interactive rebase */
351 struct child_process child
= CHILD_PROCESS_INIT
;
353 argv_array_pushf(&child
.env_array
, "GIT_CHERRY_PICK_HELP=%s",
355 if (!(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
)) {
356 argv_array_push(&child
.env_array
, "GIT_EDITOR=:");
357 opts
->autosquash
= 0;
361 argv_array_push(&child
.args
, "rebase--interactive");
364 argv_array_pushf(&child
.args
, "--%s", opts
->action
);
365 if (opts
->keep_empty
)
366 argv_array_push(&child
.args
, "--keep-empty");
367 if (opts
->rebase_merges
)
368 argv_array_push(&child
.args
, "--rebase-merges");
369 if (opts
->rebase_cousins
)
370 argv_array_push(&child
.args
, "--rebase-cousins");
371 if (opts
->autosquash
)
372 argv_array_push(&child
.args
, "--autosquash");
373 if (opts
->flags
& REBASE_VERBOSE
)
374 argv_array_push(&child
.args
, "--verbose");
375 if (opts
->flags
& REBASE_FORCE
)
376 argv_array_push(&child
.args
, "--no-ff");
377 if (opts
->restrict_revision
)
378 argv_array_pushf(&child
.args
,
379 "--restrict-revision=^%s",
380 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
382 argv_array_pushf(&child
.args
, "--upstream=%s",
383 oid_to_hex(&opts
->upstream
->object
.oid
));
385 argv_array_pushf(&child
.args
, "--onto=%s",
386 oid_to_hex(&opts
->onto
->object
.oid
));
387 if (opts
->squash_onto
)
388 argv_array_pushf(&child
.args
, "--squash-onto=%s",
389 oid_to_hex(opts
->squash_onto
));
391 argv_array_pushf(&child
.args
, "--onto-name=%s",
393 argv_array_pushf(&child
.args
, "--head-name=%s",
395 opts
->head_name
: "detached HEAD");
397 argv_array_pushf(&child
.args
, "--strategy=%s",
399 if (opts
->strategy_opts
)
400 argv_array_pushf(&child
.args
, "--strategy-opts=%s",
401 opts
->strategy_opts
);
403 argv_array_pushf(&child
.args
, "--switch-to=%s",
406 argv_array_pushf(&child
.args
, "--cmd=%s", opts
->cmd
);
407 if (opts
->allow_empty_message
)
408 argv_array_push(&child
.args
, "--allow-empty-message");
409 if (opts
->allow_rerere_autoupdate
> 0)
410 argv_array_push(&child
.args
, "--rerere-autoupdate");
411 else if (opts
->allow_rerere_autoupdate
== 0)
412 argv_array_push(&child
.args
, "--no-rerere-autoupdate");
413 if (opts
->gpg_sign_opt
)
414 argv_array_push(&child
.args
, opts
->gpg_sign_opt
);
416 argv_array_push(&child
.args
, "--signoff");
418 status
= run_command(&child
);
419 goto finished_rebase
;
422 add_var(&script_snippet
, "GIT_DIR", absolute_path(get_git_dir()));
423 add_var(&script_snippet
, "state_dir", opts
->state_dir
);
425 add_var(&script_snippet
, "upstream_name", opts
->upstream_name
);
426 add_var(&script_snippet
, "upstream", opts
->upstream
?
427 oid_to_hex(&opts
->upstream
->object
.oid
) : NULL
);
428 add_var(&script_snippet
, "head_name",
429 opts
->head_name
? opts
->head_name
: "detached HEAD");
430 add_var(&script_snippet
, "orig_head", oid_to_hex(&opts
->orig_head
));
431 add_var(&script_snippet
, "onto", opts
->onto
?
432 oid_to_hex(&opts
->onto
->object
.oid
) : NULL
);
433 add_var(&script_snippet
, "onto_name", opts
->onto_name
);
434 add_var(&script_snippet
, "revisions", opts
->revisions
);
435 add_var(&script_snippet
, "restrict_revision", opts
->restrict_revision
?
436 oid_to_hex(&opts
->restrict_revision
->object
.oid
) : NULL
);
437 add_var(&script_snippet
, "GIT_QUIET",
438 opts
->flags
& REBASE_NO_QUIET
? "" : "t");
439 add_var(&script_snippet
, "git_am_opt", opts
->git_am_opt
.buf
);
440 add_var(&script_snippet
, "verbose",
441 opts
->flags
& REBASE_VERBOSE
? "t" : "");
442 add_var(&script_snippet
, "diffstat",
443 opts
->flags
& REBASE_DIFFSTAT
? "t" : "");
444 add_var(&script_snippet
, "force_rebase",
445 opts
->flags
& REBASE_FORCE
? "t" : "");
447 add_var(&script_snippet
, "switch_to", opts
->switch_to
);
448 add_var(&script_snippet
, "action", opts
->action
? opts
->action
: "");
449 add_var(&script_snippet
, "signoff", opts
->signoff
? "--signoff" : "");
450 add_var(&script_snippet
, "allow_rerere_autoupdate",
451 opts
->allow_rerere_autoupdate
< 0 ? "" :
452 opts
->allow_rerere_autoupdate
?
453 "--rerere-autoupdate" : "--no-rerere-autoupdate");
454 add_var(&script_snippet
, "keep_empty", opts
->keep_empty
? "yes" : "");
455 add_var(&script_snippet
, "autosquash", opts
->autosquash
? "t" : "");
456 add_var(&script_snippet
, "gpg_sign_opt", opts
->gpg_sign_opt
);
457 add_var(&script_snippet
, "cmd", opts
->cmd
);
458 add_var(&script_snippet
, "allow_empty_message",
459 opts
->allow_empty_message
? "--allow-empty-message" : "");
460 add_var(&script_snippet
, "rebase_merges",
461 opts
->rebase_merges
? "t" : "");
462 add_var(&script_snippet
, "rebase_cousins",
463 opts
->rebase_cousins
? "t" : "");
464 add_var(&script_snippet
, "strategy", opts
->strategy
);
465 add_var(&script_snippet
, "strategy_opts", opts
->strategy_opts
);
466 add_var(&script_snippet
, "rebase_root", opts
->root
? "t" : "");
467 add_var(&script_snippet
, "squash_onto",
468 opts
->squash_onto
? oid_to_hex(opts
->squash_onto
) : "");
469 add_var(&script_snippet
, "git_format_patch_opt",
470 opts
->git_format_patch_opt
.buf
);
472 if (is_interactive(opts
) &&
473 !(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
)) {
474 strbuf_addstr(&script_snippet
,
475 "GIT_EDITOR=:; export GIT_EDITOR; ");
476 opts
->autosquash
= 0;
479 switch (opts
->type
) {
481 backend
= "git-rebase--am";
482 backend_func
= "git_rebase__am";
485 backend
= "git-rebase--merge";
486 backend_func
= "git_rebase__merge";
488 case REBASE_PRESERVE_MERGES
:
489 backend
= "git-rebase--preserve-merges";
490 backend_func
= "git_rebase__preserve_merges";
493 BUG("Unhandled rebase type %d", opts
->type
);
497 strbuf_addf(&script_snippet
,
498 ". git-sh-setup && . git-rebase--common &&"
499 " . %s && %s", backend
, backend_func
);
500 argv
[0] = script_snippet
.buf
;
502 status
= run_command_v_opt(argv
, RUN_USING_SHELL
);
504 if (opts
->dont_finish_rebase
)
506 else if (opts
->type
== REBASE_INTERACTIVE
)
507 ; /* interactive rebase cleans up after itself */
508 else if (status
== 0) {
509 if (!file_exists(state_dir_path("stopped-sha", opts
)))
511 } else if (status
== 2) {
512 struct strbuf dir
= STRBUF_INIT
;
514 apply_autostash(opts
);
515 strbuf_addstr(&dir
, opts
->state_dir
);
516 remove_dir_recursively(&dir
, 0);
517 strbuf_release(&dir
);
518 die("Nothing to do");
521 strbuf_release(&script_snippet
);
523 return status
? -1 : 0;
526 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
528 static int reset_head(struct object_id
*oid
, const char *action
,
529 const char *switch_to_branch
, int detach_head
,
530 const char *reflog_orig_head
, const char *reflog_head
)
532 struct object_id head_oid
;
533 struct tree_desc desc
;
534 struct lock_file lock
= LOCK_INIT
;
535 struct unpack_trees_options unpack_tree_opts
;
537 const char *reflog_action
;
538 struct strbuf msg
= STRBUF_INIT
;
540 struct object_id
*orig
= NULL
, oid_orig
,
541 *old_orig
= NULL
, oid_old_orig
;
544 if (switch_to_branch
&& !starts_with(switch_to_branch
, "refs/"))
545 BUG("Not a fully qualified branch: '%s'", switch_to_branch
);
547 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0)
551 if (get_oid("HEAD", &head_oid
)) {
552 rollback_lock_file(&lock
);
553 return error(_("could not determine HEAD revision"));
558 memset(&unpack_tree_opts
, 0, sizeof(unpack_tree_opts
));
559 setup_unpack_trees_porcelain(&unpack_tree_opts
, action
);
560 unpack_tree_opts
.head_idx
= 1;
561 unpack_tree_opts
.src_index
= the_repository
->index
;
562 unpack_tree_opts
.dst_index
= the_repository
->index
;
563 unpack_tree_opts
.fn
= oneway_merge
;
564 unpack_tree_opts
.update
= 1;
565 unpack_tree_opts
.merge
= 1;
567 unpack_tree_opts
.reset
= 1;
569 if (read_index_unmerged(the_repository
->index
) < 0) {
570 rollback_lock_file(&lock
);
571 return error(_("could not read index"));
574 if (!fill_tree_descriptor(&desc
, oid
)) {
575 error(_("failed to find tree of %s"), oid_to_hex(oid
));
576 rollback_lock_file(&lock
);
577 free((void *)desc
.buffer
);
581 if (unpack_trees(1, &desc
, &unpack_tree_opts
)) {
582 rollback_lock_file(&lock
);
583 free((void *)desc
.buffer
);
587 tree
= parse_tree_indirect(oid
);
588 prime_cache_tree(the_repository
->index
, tree
);
590 if (write_locked_index(the_repository
->index
, &lock
, COMMIT_LOCK
) < 0)
591 ret
= error(_("could not write index"));
592 free((void *)desc
.buffer
);
597 reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
598 strbuf_addf(&msg
, "%s: ", reflog_action
? reflog_action
: "rebase");
599 prefix_len
= msg
.len
;
601 if (!get_oid("ORIG_HEAD", &oid_old_orig
))
602 old_orig
= &oid_old_orig
;
603 if (!get_oid("HEAD", &oid_orig
)) {
605 if (!reflog_orig_head
) {
606 strbuf_addstr(&msg
, "updating ORIG_HEAD");
607 reflog_orig_head
= msg
.buf
;
609 update_ref(reflog_orig_head
, "ORIG_HEAD", orig
, old_orig
, 0,
610 UPDATE_REFS_MSG_ON_ERR
);
612 delete_ref(NULL
, "ORIG_HEAD", old_orig
, 0);
614 strbuf_setlen(&msg
, prefix_len
);
615 strbuf_addstr(&msg
, "updating HEAD");
616 reflog_head
= msg
.buf
;
618 if (!switch_to_branch
)
619 ret
= update_ref(reflog_head
, "HEAD", oid
, orig
, REF_NO_DEREF
,
620 UPDATE_REFS_MSG_ON_ERR
);
622 ret
= create_symref("HEAD", switch_to_branch
, msg
.buf
);
624 ret
= update_ref(reflog_head
, "HEAD", oid
, NULL
, 0,
625 UPDATE_REFS_MSG_ON_ERR
);
628 strbuf_release(&msg
);
632 static int rebase_config(const char *var
, const char *value
, void *data
)
634 struct rebase_options
*opts
= data
;
636 if (!strcmp(var
, "rebase.stat")) {
637 if (git_config_bool(var
, value
))
638 opts
->flags
|= REBASE_DIFFSTAT
;
640 opts
->flags
&= !REBASE_DIFFSTAT
;
644 if (!strcmp(var
, "rebase.autosquash")) {
645 opts
->autosquash
= git_config_bool(var
, value
);
649 if (!strcmp(var
, "commit.gpgsign")) {
650 free(opts
->gpg_sign_opt
);
651 opts
->gpg_sign_opt
= git_config_bool(var
, value
) ?
652 xstrdup("-S") : NULL
;
656 if (!strcmp(var
, "rebase.autostash")) {
657 opts
->autostash
= git_config_bool(var
, value
);
661 return git_default_config(var
, value
, data
);
665 * Determines whether the commits in from..to are linear, i.e. contain
666 * no merge commits. This function *expects* `from` to be an ancestor of
669 static int is_linear_history(struct commit
*from
, struct commit
*to
)
671 while (to
&& to
!= from
) {
675 if (to
->parents
->next
)
677 to
= to
->parents
->item
;
682 static int can_fast_forward(struct commit
*onto
, struct object_id
*head_oid
,
683 struct object_id
*merge_base
)
685 struct commit
*head
= lookup_commit(the_repository
, head_oid
);
686 struct commit_list
*merge_bases
;
692 merge_bases
= get_merge_bases(onto
, head
);
693 if (merge_bases
&& !merge_bases
->next
) {
694 oidcpy(merge_base
, &merge_bases
->item
->object
.oid
);
695 res
= oideq(merge_base
, &onto
->object
.oid
);
697 oidcpy(merge_base
, &null_oid
);
700 free_commit_list(merge_bases
);
701 return res
&& is_linear_history(onto
, head
);
704 /* -i followed by -m is still -i */
705 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
707 struct rebase_options
*opts
= opt
->value
;
709 if (!is_interactive(opts
))
710 opts
->type
= REBASE_MERGE
;
715 /* -i followed by -p is still explicitly interactive, but -p alone is not */
716 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
719 struct rebase_options
*opts
= opt
->value
;
721 opts
->type
= REBASE_INTERACTIVE
;
722 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
727 static void NORETURN
error_on_missing_default_upstream(void)
729 struct branch
*current_branch
= branch_get(NULL
);
732 "Please specify which branch you want to rebase against.\n"
733 "See git-rebase(1) for details.\n"
735 " git rebase '<branch>'\n"
737 current_branch
? _("There is no tracking information for "
738 "the current branch.") :
739 _("You are not currently on a branch."));
741 if (current_branch
) {
742 const char *remote
= current_branch
->remote_name
;
745 remote
= _("<remote>");
747 printf(_("If you wish to set tracking information for this "
748 "branch you can do so with:\n"
750 " git branch --set-upstream-to=%s/<branch> %s\n"
752 remote
, current_branch
->name
);
757 int cmd_rebase(int argc
, const char **argv
, const char *prefix
)
759 struct rebase_options options
= {
760 .type
= REBASE_UNSPECIFIED
,
761 .flags
= REBASE_NO_QUIET
,
762 .git_am_opt
= STRBUF_INIT
,
763 .allow_rerere_autoupdate
= -1,
764 .allow_empty_message
= 1,
765 .git_format_patch_opt
= STRBUF_INIT
,
767 const char *branch_name
;
768 int ret
, flags
, total_argc
, in_progress
= 0;
769 int ok_to_skip_pre_rebase
= 0;
770 struct strbuf msg
= STRBUF_INIT
;
771 struct strbuf revisions
= STRBUF_INIT
;
772 struct strbuf buf
= STRBUF_INIT
;
773 struct object_id merge_base
;
781 ACTION_SHOW_CURRENT_PATCH
,
782 } action
= NO_ACTION
;
783 int committer_date_is_author_date
= 0;
785 int ignore_whitespace
= 0;
786 const char *gpg_sign
= NULL
;
788 struct string_list whitespace
= STRING_LIST_INIT_NODUP
;
789 struct string_list exec
= STRING_LIST_INIT_NODUP
;
790 const char *rebase_merges
= NULL
;
792 struct string_list strategy_options
= STRING_LIST_INIT_NODUP
;
793 struct object_id squash_onto
;
794 char *squash_onto_name
= NULL
;
795 struct option builtin_rebase_options
[] = {
796 OPT_STRING(0, "onto", &options
.onto_name
,
798 N_("rebase onto given branch instead of upstream")),
799 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
800 N_("allow pre-rebase hook to run")),
801 OPT_NEGBIT('q', "quiet", &options
.flags
,
802 N_("be quiet. implies --no-stat"),
803 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
804 OPT_BIT('v', "verbose", &options
.flags
,
805 N_("display a diffstat of what changed upstream"),
806 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
807 {OPTION_NEGBIT
, 'n', "no-stat", &options
.flags
, NULL
,
808 N_("do not show diffstat of what changed upstream"),
809 PARSE_OPT_NOARG
, NULL
, REBASE_DIFFSTAT
},
810 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace
,
811 N_("passed to 'git apply'")),
812 OPT_BOOL(0, "signoff", &options
.signoff
,
813 N_("add a Signed-off-by: line to each commit")),
814 OPT_BOOL(0, "committer-date-is-author-date",
815 &committer_date_is_author_date
,
816 N_("passed to 'git am'")),
817 OPT_BOOL(0, "ignore-date", &ignore_date
,
818 N_("passed to 'git am'")),
819 OPT_BIT('f', "force-rebase", &options
.flags
,
820 N_("cherry-pick all commits, even if unchanged"),
822 OPT_BIT(0, "no-ff", &options
.flags
,
823 N_("cherry-pick all commits, even if unchanged"),
825 OPT_CMDMODE(0, "continue", &action
, N_("continue"),
827 OPT_CMDMODE(0, "skip", &action
,
828 N_("skip current patch and continue"), ACTION_SKIP
),
829 OPT_CMDMODE(0, "abort", &action
,
830 N_("abort and check out the original branch"),
832 OPT_CMDMODE(0, "quit", &action
,
833 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
834 OPT_CMDMODE(0, "edit-todo", &action
, N_("edit the todo list "
835 "during an interactive rebase"), ACTION_EDIT_TODO
),
836 OPT_CMDMODE(0, "show-current-patch", &action
,
837 N_("show the patch file being applied or merged"),
838 ACTION_SHOW_CURRENT_PATCH
),
839 { OPTION_CALLBACK
, 'm', "merge", &options
, NULL
,
840 N_("use merging strategies to rebase"),
841 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
843 { OPTION_CALLBACK
, 'i', "interactive", &options
, NULL
,
844 N_("let the user edit the list of commits to rebase"),
845 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
846 parse_opt_interactive
},
847 OPT_SET_INT('p', "preserve-merges", &options
.type
,
848 N_("try to recreate merges instead of ignoring "
849 "them"), REBASE_PRESERVE_MERGES
),
850 OPT_BOOL(0, "rerere-autoupdate",
851 &options
.allow_rerere_autoupdate
,
852 N_("allow rerere to update index with resolved "
854 OPT_BOOL('k', "keep-empty", &options
.keep_empty
,
855 N_("preserve empty commits during rebase")),
856 OPT_BOOL(0, "autosquash", &options
.autosquash
,
857 N_("move commits that begin with "
858 "squash!/fixup! under -i")),
859 { OPTION_STRING
, 'S', "gpg-sign", &gpg_sign
, N_("key-id"),
860 N_("GPG-sign commits"),
861 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
862 OPT_STRING_LIST(0, "whitespace", &whitespace
,
863 N_("whitespace"), N_("passed to 'git apply'")),
864 OPT_SET_INT('C', NULL
, &opt_c
, N_("passed to 'git apply'"),
866 OPT_BOOL(0, "autostash", &options
.autostash
,
867 N_("automatically stash/stash pop before and after")),
868 OPT_STRING_LIST('x', "exec", &exec
, N_("exec"),
869 N_("add exec lines after each commit of the "
871 OPT_BOOL(0, "allow-empty-message",
872 &options
.allow_empty_message
,
873 N_("allow rebasing commits with empty messages")),
874 {OPTION_STRING
, 'r', "rebase-merges", &rebase_merges
,
876 N_("try to rebase merges instead of skipping them"),
877 PARSE_OPT_OPTARG
, NULL
, (intptr_t)""},
878 OPT_BOOL(0, "fork-point", &fork_point
,
879 N_("use 'merge-base --fork-point' to refine upstream")),
880 OPT_STRING('s', "strategy", &options
.strategy
,
881 N_("strategy"), N_("use the given merge strategy")),
882 OPT_STRING_LIST('X', "strategy-option", &strategy_options
,
884 N_("pass the argument through to the merge "
886 OPT_BOOL(0, "root", &options
.root
,
887 N_("rebase all reachable commits up to the root(s)")),
892 * NEEDSWORK: Once the builtin rebase has been tested enough
893 * and git-legacy-rebase.sh is retired to contrib/, this preamble
897 if (!use_builtin_rebase()) {
898 const char *path
= mkpath("%s/git-legacy-rebase",
901 if (sane_execvp(path
, (char **)argv
) < 0)
902 die_errno(_("could not exec %s"), path
);
904 BUG("sane_execvp() returned???");
907 if (argc
== 2 && !strcmp(argv
[1], "-h"))
908 usage_with_options(builtin_rebase_usage
,
909 builtin_rebase_options
);
911 prefix
= setup_git_directory();
912 trace_repo_setup(prefix
);
915 git_config(rebase_config
, &options
);
918 strbuf_addf(&buf
, "%s/applying", apply_dir());
919 if(file_exists(buf
.buf
))
920 die(_("It looks like 'git am' is in progress. Cannot rebase."));
922 if (is_directory(apply_dir())) {
923 options
.type
= REBASE_AM
;
924 options
.state_dir
= apply_dir();
925 } else if (is_directory(merge_dir())) {
927 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
928 if (is_directory(buf
.buf
)) {
929 options
.type
= REBASE_PRESERVE_MERGES
;
930 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
933 strbuf_addf(&buf
, "%s/interactive", merge_dir());
934 if(file_exists(buf
.buf
)) {
935 options
.type
= REBASE_INTERACTIVE
;
936 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
938 options
.type
= REBASE_MERGE
;
940 options
.state_dir
= merge_dir();
943 if (options
.type
!= REBASE_UNSPECIFIED
)
947 argc
= parse_options(argc
, argv
, prefix
,
948 builtin_rebase_options
,
949 builtin_rebase_usage
, 0);
951 if (action
!= NO_ACTION
&& total_argc
!= 2) {
952 usage_with_options(builtin_rebase_usage
,
953 builtin_rebase_options
);
957 usage_with_options(builtin_rebase_usage
,
958 builtin_rebase_options
);
960 if (action
!= NO_ACTION
&& !in_progress
)
961 die(_("No rebase in progress?"));
963 if (action
== ACTION_EDIT_TODO
&& !is_interactive(&options
))
964 die(_("The --edit-todo action can only be used during "
965 "interactive rebase."));
968 case ACTION_CONTINUE
: {
969 struct object_id head
;
970 struct lock_file lock_file
= LOCK_INIT
;
973 options
.action
= "continue";
976 if (get_oid("HEAD", &head
))
977 die(_("Cannot read HEAD"));
979 fd
= hold_locked_index(&lock_file
, 0);
980 if (read_index(the_repository
->index
) < 0)
981 die(_("could not read index"));
982 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
985 update_index_if_able(the_repository
->index
,
987 rollback_lock_file(&lock_file
);
989 if (has_unstaged_changes(1)) {
990 puts(_("You must edit all merge conflicts and then\n"
991 "mark them as resolved using git add"));
994 if (read_basic_state(&options
))
999 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1001 options
.action
= "skip";
1003 rerere_clear(&merge_rr
);
1004 string_list_clear(&merge_rr
, 1);
1006 if (reset_head(NULL
, "reset", NULL
, 0, NULL
, NULL
) < 0)
1007 die(_("could not discard worktree changes"));
1008 if (read_basic_state(&options
))
1012 case ACTION_ABORT
: {
1013 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1014 options
.action
= "abort";
1016 rerere_clear(&merge_rr
);
1017 string_list_clear(&merge_rr
, 1);
1019 if (read_basic_state(&options
))
1021 if (reset_head(&options
.orig_head
, "reset",
1022 options
.head_name
, 0, NULL
, NULL
) < 0)
1023 die(_("could not move back to %s"),
1024 oid_to_hex(&options
.orig_head
));
1025 ret
= finish_rebase(&options
);
1030 strbuf_addstr(&buf
, options
.state_dir
);
1031 ret
= !!remove_dir_recursively(&buf
, 0);
1033 die(_("could not remove '%s'"), options
.state_dir
);
1036 case ACTION_EDIT_TODO
:
1037 options
.action
= "edit-todo";
1038 options
.dont_finish_rebase
= 1;
1040 case ACTION_SHOW_CURRENT_PATCH
:
1041 options
.action
= "show-current-patch";
1042 options
.dont_finish_rebase
= 1;
1047 BUG("action: %d", action
);
1050 /* Make sure no rebase is in progress */
1052 const char *last_slash
= strrchr(options
.state_dir
, '/');
1053 const char *state_dir_base
=
1054 last_slash
? last_slash
+ 1 : options
.state_dir
;
1055 const char *cmd_live_rebase
=
1056 "git rebase (--continue | --abort | --skip)";
1058 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
1059 die(_("It seems that there is already a %s directory, and\n"
1060 "I wonder if you are in the middle of another rebase. "
1062 "case, please try\n\t%s\n"
1063 "If that is not the case, please\n\t%s\n"
1064 "and run me again. I am stopping in case you still "
1066 "valuable there.\n"),
1067 state_dir_base
, cmd_live_rebase
, buf
.buf
);
1070 if (!(options
.flags
& REBASE_NO_QUIET
))
1071 strbuf_addstr(&options
.git_am_opt
, " -q");
1073 if (committer_date_is_author_date
) {
1074 strbuf_addstr(&options
.git_am_opt
,
1075 " --committer-date-is-author-date");
1076 options
.flags
|= REBASE_FORCE
;
1079 if (ignore_whitespace
)
1080 strbuf_addstr(&options
.git_am_opt
, " --ignore-whitespace");
1083 strbuf_addstr(&options
.git_am_opt
, " --ignore-date");
1084 options
.flags
|= REBASE_FORCE
;
1087 if (options
.keep_empty
)
1088 imply_interactive(&options
, "--keep-empty");
1091 free(options
.gpg_sign_opt
);
1092 options
.gpg_sign_opt
= xstrfmt("-S%s", gpg_sign
);
1096 strbuf_addf(&options
.git_am_opt
, " -C%d", opt_c
);
1098 if (whitespace
.nr
) {
1101 for (i
= 0; i
< whitespace
.nr
; i
++) {
1102 const char *item
= whitespace
.items
[i
].string
;
1104 strbuf_addf(&options
.git_am_opt
, " --whitespace=%s",
1107 if ((!strcmp(item
, "fix")) || (!strcmp(item
, "strip")))
1108 options
.flags
|= REBASE_FORCE
;
1115 imply_interactive(&options
, "--exec");
1118 for (i
= 0; i
< exec
.nr
; i
++)
1119 strbuf_addf(&buf
, "exec %s\n", exec
.items
[i
].string
);
1120 options
.cmd
= xstrdup(buf
.buf
);
1123 if (rebase_merges
) {
1124 if (!*rebase_merges
)
1125 ; /* default mode; do nothing */
1126 else if (!strcmp("rebase-cousins", rebase_merges
))
1127 options
.rebase_cousins
= 1;
1128 else if (strcmp("no-rebase-cousins", rebase_merges
))
1129 die(_("Unknown mode: %s"), rebase_merges
);
1130 options
.rebase_merges
= 1;
1131 imply_interactive(&options
, "--rebase-merges");
1134 if (strategy_options
.nr
) {
1137 if (!options
.strategy
)
1138 options
.strategy
= "recursive";
1141 for (i
= 0; i
< strategy_options
.nr
; i
++)
1142 strbuf_addf(&buf
, " --%s",
1143 strategy_options
.items
[i
].string
);
1144 options
.strategy_opts
= xstrdup(buf
.buf
);
1147 if (options
.strategy
) {
1148 options
.strategy
= xstrdup(options
.strategy
);
1149 switch (options
.type
) {
1151 die(_("--strategy requires --merge or --interactive"));
1153 case REBASE_INTERACTIVE
:
1154 case REBASE_PRESERVE_MERGES
:
1157 case REBASE_UNSPECIFIED
:
1158 options
.type
= REBASE_MERGE
;
1161 BUG("unhandled rebase type (%d)", options
.type
);
1165 if (options
.root
&& !options
.onto_name
)
1166 imply_interactive(&options
, "--root without --onto");
1168 if (isatty(2) && options
.flags
& REBASE_NO_QUIET
)
1169 strbuf_addstr(&options
.git_format_patch_opt
, " --progress");
1171 switch (options
.type
) {
1173 case REBASE_INTERACTIVE
:
1174 case REBASE_PRESERVE_MERGES
:
1175 options
.state_dir
= merge_dir();
1178 options
.state_dir
= apply_dir();
1181 /* the default rebase backend is `--am` */
1182 options
.type
= REBASE_AM
;
1183 options
.state_dir
= apply_dir();
1187 if (options
.git_am_opt
.len
) {
1190 /* all am options except -q are compatible only with --am */
1192 strbuf_addbuf(&buf
, &options
.git_am_opt
);
1193 strbuf_addch(&buf
, ' ');
1194 while ((p
= strstr(buf
.buf
, " -q ")))
1195 strbuf_splice(&buf
, p
- buf
.buf
, 4, " ", 1);
1198 if (is_interactive(&options
) && buf
.len
)
1199 die(_("error: cannot combine interactive options "
1200 "(--interactive, --exec, --rebase-merges, "
1201 "--preserve-merges, --keep-empty, --root + "
1202 "--onto) with am options (%s)"), buf
.buf
);
1203 if (options
.type
== REBASE_MERGE
&& buf
.len
)
1204 die(_("error: cannot combine merge options (--merge, "
1205 "--strategy, --strategy-option) with am options "
1209 if (options
.signoff
) {
1210 if (options
.type
== REBASE_PRESERVE_MERGES
)
1211 die("cannot combine '--signoff' with "
1212 "'--preserve-merges'");
1213 strbuf_addstr(&options
.git_am_opt
, " --signoff");
1214 options
.flags
|= REBASE_FORCE
;
1217 if (options
.type
== REBASE_PRESERVE_MERGES
)
1219 * Note: incompatibility with --signoff handled in signoff block above
1220 * Note: incompatibility with --interactive is just a strong warning;
1221 * git-rebase.txt caveats with "unless you know what you are doing"
1223 if (options
.rebase_merges
)
1224 die(_("error: cannot combine '--preserve-merges' with "
1225 "'--rebase-merges'"));
1227 if (options
.rebase_merges
) {
1228 if (strategy_options
.nr
)
1229 die(_("error: cannot combine '--rebase-merges' with "
1230 "'--strategy-option'"));
1231 if (options
.strategy
)
1232 die(_("error: cannot combine '--rebase-merges' with "
1236 if (!options
.root
) {
1238 struct branch
*branch
;
1240 branch
= branch_get(NULL
);
1241 options
.upstream_name
= branch_get_upstream(branch
,
1243 if (!options
.upstream_name
)
1244 error_on_missing_default_upstream();
1248 options
.upstream_name
= argv
[0];
1251 if (!strcmp(options
.upstream_name
, "-"))
1252 options
.upstream_name
= "@{-1}";
1254 options
.upstream
= peel_committish(options
.upstream_name
);
1255 if (!options
.upstream
)
1256 die(_("invalid upstream '%s'"), options
.upstream_name
);
1257 options
.upstream_arg
= options
.upstream_name
;
1259 if (!options
.onto_name
) {
1260 if (commit_tree("", 0, the_hash_algo
->empty_tree
, NULL
,
1261 &squash_onto
, NULL
, NULL
) < 0)
1262 die(_("Could not create new root commit"));
1263 options
.squash_onto
= &squash_onto
;
1264 options
.onto_name
= squash_onto_name
=
1265 xstrdup(oid_to_hex(&squash_onto
));
1267 options
.upstream_name
= NULL
;
1268 options
.upstream
= NULL
;
1270 usage_with_options(builtin_rebase_usage
,
1271 builtin_rebase_options
);
1272 options
.upstream_arg
= "--root";
1275 /* Make sure the branch to rebase onto is valid. */
1276 if (!options
.onto_name
)
1277 options
.onto_name
= options
.upstream_name
;
1278 if (strstr(options
.onto_name
, "...")) {
1279 if (get_oid_mb(options
.onto_name
, &merge_base
) < 0)
1280 die(_("'%s': need exactly one merge base"),
1282 options
.onto
= lookup_commit_or_die(&merge_base
,
1285 options
.onto
= peel_committish(options
.onto_name
);
1287 die(_("Does not point to a valid commit '%s'"),
1292 * If the branch to rebase is given, that is the branch we will rebase
1293 * branch_name -- branch/commit being rebased, or
1294 * HEAD (already detached)
1295 * orig_head -- commit object name of tip of the branch before rebasing
1296 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1299 /* Is it "rebase other branchname" or "rebase other commit"? */
1300 branch_name
= argv
[0];
1301 options
.switch_to
= argv
[0];
1303 /* Is it a local branch? */
1305 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
1306 if (!read_ref(buf
.buf
, &options
.orig_head
))
1307 options
.head_name
= xstrdup(buf
.buf
);
1308 /* If not is it a valid ref (branch or commit)? */
1309 else if (!get_oid(branch_name
, &options
.orig_head
))
1310 options
.head_name
= NULL
;
1312 die(_("fatal: no such branch/commit '%s'"),
1314 } else if (argc
== 0) {
1315 /* Do not need to switch branches, we are already on it. */
1317 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL
,
1319 if (!options
.head_name
)
1320 die(_("No such ref: %s"), "HEAD");
1321 if (flags
& REF_ISSYMREF
) {
1322 if (!skip_prefix(options
.head_name
,
1323 "refs/heads/", &branch_name
))
1324 branch_name
= options
.head_name
;
1327 free(options
.head_name
);
1328 options
.head_name
= NULL
;
1329 branch_name
= "HEAD";
1331 if (get_oid("HEAD", &options
.orig_head
))
1332 die(_("Could not resolve HEAD to a revision"));
1334 BUG("unexpected number of arguments left to parse");
1336 if (fork_point
> 0) {
1337 struct commit
*head
=
1338 lookup_commit_reference(the_repository
,
1339 &options
.orig_head
);
1340 options
.restrict_revision
=
1341 get_fork_point(options
.upstream_name
, head
);
1344 if (read_index(the_repository
->index
) < 0)
1345 die(_("could not read index"));
1347 if (options
.autostash
) {
1348 struct lock_file lock_file
= LOCK_INIT
;
1351 fd
= hold_locked_index(&lock_file
, 0);
1352 refresh_cache(REFRESH_QUIET
);
1354 update_index_if_able(&the_index
, &lock_file
);
1355 rollback_lock_file(&lock_file
);
1357 if (has_unstaged_changes(1) || has_uncommitted_changes(1)) {
1358 const char *autostash
=
1359 state_dir_path("autostash", &options
);
1360 struct child_process stash
= CHILD_PROCESS_INIT
;
1361 struct object_id oid
;
1362 struct commit
*head
=
1363 lookup_commit_reference(the_repository
,
1364 &options
.orig_head
);
1366 argv_array_pushl(&stash
.args
,
1367 "stash", "create", "autostash", NULL
);
1371 if (capture_command(&stash
, &buf
, GIT_MAX_HEXSZ
))
1372 die(_("Cannot autostash"));
1373 strbuf_trim_trailing_newline(&buf
);
1374 if (get_oid(buf
.buf
, &oid
))
1375 die(_("Unexpected stash response: '%s'"),
1378 strbuf_add_unique_abbrev(&buf
, &oid
, DEFAULT_ABBREV
);
1380 if (safe_create_leading_directories_const(autostash
))
1381 die(_("Could not create directory for '%s'"),
1383 write_file(autostash
, "%s", oid_to_hex(&oid
));
1384 printf(_("Created autostash: %s\n"), buf
.buf
);
1385 if (reset_head(&head
->object
.oid
, "reset --hard",
1386 NULL
, 0, NULL
, NULL
) < 0)
1387 die(_("could not reset --hard"));
1388 printf(_("HEAD is now at %s"),
1389 find_unique_abbrev(&head
->object
.oid
,
1392 pp_commit_easy(CMIT_FMT_ONELINE
, head
, &buf
);
1394 printf(" %s", buf
.buf
);
1397 if (discard_index(the_repository
->index
) < 0 ||
1398 read_index(the_repository
->index
) < 0)
1399 die(_("could not read index"));
1403 if (require_clean_work_tree("rebase",
1404 _("Please commit or stash them."), 1, 1)) {
1410 * Now we are rebasing commits upstream..orig_head (or with --root,
1411 * everything leading up to orig_head) on top of onto.
1415 * Check if we are already based on onto with linear history,
1416 * but this should be done only when upstream and onto are the same
1417 * and if this is not an interactive rebase.
1419 if (can_fast_forward(options
.onto
, &options
.orig_head
, &merge_base
) &&
1420 !is_interactive(&options
) && !options
.restrict_revision
&&
1422 !oidcmp(&options
.upstream
->object
.oid
, &options
.onto
->object
.oid
)) {
1425 if (!(options
.flags
& REBASE_FORCE
)) {
1426 /* Lazily switch to the target branch if needed... */
1427 if (options
.switch_to
) {
1428 struct object_id oid
;
1430 if (get_oid(options
.switch_to
, &oid
) < 0) {
1431 ret
= !!error(_("could not parse '%s'"),
1437 strbuf_addf(&buf
, "rebase: checkout %s",
1439 if (reset_head(&oid
, "checkout",
1440 options
.head_name
, 0,
1442 ret
= !!error(_("could not switch to "
1449 if (!(options
.flags
& REBASE_NO_QUIET
))
1451 else if (!strcmp(branch_name
, "HEAD") &&
1452 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1453 puts(_("HEAD is up to date."));
1455 printf(_("Current branch %s is up to date.\n"),
1457 ret
= !!finish_rebase(&options
);
1459 } else if (!(options
.flags
& REBASE_NO_QUIET
))
1461 else if (!strcmp(branch_name
, "HEAD") &&
1462 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1463 puts(_("HEAD is up to date, rebase forced."));
1465 printf(_("Current branch %s is up to date, rebase "
1466 "forced.\n"), branch_name
);
1469 /* If a hook exists, give it a chance to interrupt*/
1470 if (!ok_to_skip_pre_rebase
&&
1471 run_hook_le(NULL
, "pre-rebase", options
.upstream_arg
,
1472 argc
? argv
[0] : NULL
, NULL
))
1473 die(_("The pre-rebase hook refused to rebase."));
1475 if (options
.flags
& REBASE_DIFFSTAT
) {
1476 struct diff_options opts
;
1478 if (options
.flags
& REBASE_VERBOSE
)
1479 printf(_("Changes from %s to %s:\n"),
1480 oid_to_hex(&merge_base
),
1481 oid_to_hex(&options
.onto
->object
.oid
));
1483 /* We want color (if set), but no pager */
1485 opts
.stat_width
= -1; /* use full terminal width */
1486 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
1487 opts
.output_format
|=
1488 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1489 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1490 diff_setup_done(&opts
);
1491 diff_tree_oid(&merge_base
, &options
.onto
->object
.oid
,
1493 diffcore_std(&opts
);
1497 if (is_interactive(&options
))
1500 /* Detach HEAD and reset the tree */
1501 if (options
.flags
& REBASE_NO_QUIET
)
1502 printf(_("First, rewinding head to replay your work on top of "
1505 strbuf_addf(&msg
, "rebase: checkout %s", options
.onto_name
);
1506 if (reset_head(&options
.onto
->object
.oid
, "checkout", NULL
, 1,
1508 die(_("Could not detach HEAD"));
1509 strbuf_release(&msg
);
1512 * If the onto is a proper descendant of the tip of the branch, then
1513 * we just fast-forwarded.
1516 if (!oidcmp(&merge_base
, &options
.orig_head
)) {
1517 printf(_("Fast-forwarded %s to %s. \n"),
1518 branch_name
, options
.onto_name
);
1519 strbuf_addf(&msg
, "rebase finished: %s onto %s",
1520 options
.head_name
? options
.head_name
: "detached HEAD",
1521 oid_to_hex(&options
.onto
->object
.oid
));
1522 reset_head(NULL
, "Fast-forwarded", options
.head_name
, 0,
1524 strbuf_release(&msg
);
1525 ret
= !!finish_rebase(&options
);
1529 strbuf_addf(&revisions
, "%s..%s",
1530 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
1531 (options
.restrict_revision
?
1532 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
1533 oid_to_hex(&options
.upstream
->object
.oid
)),
1534 oid_to_hex(&options
.orig_head
));
1536 options
.revisions
= revisions
.buf
;
1539 ret
= !!run_specific_rebase(&options
);
1542 strbuf_release(&revisions
);
1543 free(options
.head_name
);
1544 free(options
.gpg_sign_opt
);
1546 free(squash_onto_name
);