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"
26 static char const * const builtin_rebase_usage
[] = {
27 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
28 "[<upstream>] [<branch>]"),
29 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
31 N_("git rebase --continue | --abort | --skip | --edit-todo"),
35 static GIT_PATH_FUNC(apply_dir
, "rebase-apply")
36 static GIT_PATH_FUNC(merge_dir
, "rebase-merge")
39 REBASE_UNSPECIFIED
= -1,
43 REBASE_PRESERVE_MERGES
46 static int use_builtin_rebase(void)
48 struct child_process cp
= CHILD_PROCESS_INIT
;
49 struct strbuf out
= STRBUF_INIT
;
52 argv_array_pushl(&cp
.args
,
53 "config", "--bool", "rebase.usebuiltin", NULL
);
55 if (capture_command(&cp
, &out
, 6)) {
61 ret
= !strcmp("true", out
.buf
);
66 static int apply_autostash(void)
72 struct rebase_options
{
73 enum rebase_type type
;
74 const char *state_dir
;
75 struct commit
*upstream
;
76 const char *upstream_name
;
77 const char *upstream_arg
;
79 struct object_id orig_head
;
81 const char *onto_name
;
82 const char *revisions
;
83 const char *switch_to
;
85 struct commit
*restrict_revision
;
86 int dont_finish_rebase
;
88 REBASE_NO_QUIET
= 1<<0,
89 REBASE_VERBOSE
= 1<<1,
90 REBASE_DIFFSTAT
= 1<<2,
92 REBASE_INTERACTIVE_EXPLICIT
= 1<<4,
94 struct strbuf git_am_opt
;
98 static int is_interactive(struct rebase_options
*opts
)
100 return opts
->type
== REBASE_INTERACTIVE
||
101 opts
->type
== REBASE_PRESERVE_MERGES
;
104 /* Returns the filename prefixed by the state_dir */
105 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
107 static struct strbuf path
= STRBUF_INIT
;
108 static size_t prefix_len
;
111 strbuf_addf(&path
, "%s/", opts
->state_dir
);
112 prefix_len
= path
.len
;
115 strbuf_setlen(&path
, prefix_len
);
116 strbuf_addstr(&path
, filename
);
120 /* Read one file, then strip line endings */
121 static int read_one(const char *path
, struct strbuf
*buf
)
123 if (strbuf_read_file(buf
, path
, 0) < 0)
124 return error_errno(_("could not read '%s'"), path
);
125 strbuf_trim_trailing_newline(buf
);
129 /* Initialize the rebase options from the state directory. */
130 static int read_basic_state(struct rebase_options
*opts
)
132 struct strbuf head_name
= STRBUF_INIT
;
133 struct strbuf buf
= STRBUF_INIT
;
134 struct object_id oid
;
136 if (read_one(state_dir_path("head-name", opts
), &head_name
) ||
137 read_one(state_dir_path("onto", opts
), &buf
))
139 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
140 xstrdup(head_name
.buf
) : NULL
;
141 strbuf_release(&head_name
);
142 if (get_oid(buf
.buf
, &oid
))
143 return error(_("could not get 'onto': '%s'"), buf
.buf
);
144 opts
->onto
= lookup_commit_or_die(&oid
, buf
.buf
);
147 * We always write to orig-head, but interactive rebase used to write to
148 * head. Fall back to reading from head to cover for the case that the
149 * user upgraded git with an ongoing interactive rebase.
152 if (file_exists(state_dir_path("orig-head", opts
))) {
153 if (read_one(state_dir_path("orig-head", opts
), &buf
))
155 } else if (read_one(state_dir_path("head", opts
), &buf
))
157 if (get_oid(buf
.buf
, &opts
->orig_head
))
158 return error(_("invalid orig-head: '%s'"), buf
.buf
);
161 if (read_one(state_dir_path("quiet", opts
), &buf
))
164 opts
->flags
&= ~REBASE_NO_QUIET
;
166 opts
->flags
|= REBASE_NO_QUIET
;
168 if (file_exists(state_dir_path("verbose", opts
)))
169 opts
->flags
|= REBASE_VERBOSE
;
171 strbuf_release(&buf
);
176 static int finish_rebase(struct rebase_options
*opts
)
178 struct strbuf dir
= STRBUF_INIT
;
179 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
181 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
183 close_all_packs(the_repository
->objects
);
185 * We ignore errors in 'gc --auto', since the
186 * user should see them.
188 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
189 strbuf_addstr(&dir
, opts
->state_dir
);
190 remove_dir_recursively(&dir
, 0);
191 strbuf_release(&dir
);
196 static struct commit
*peel_committish(const char *name
)
199 struct object_id oid
;
201 if (get_oid(name
, &oid
))
203 obj
= parse_object(the_repository
, &oid
);
204 return (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
207 static void add_var(struct strbuf
*buf
, const char *name
, const char *value
)
210 strbuf_addf(buf
, "unset %s; ", name
);
212 strbuf_addf(buf
, "%s=", name
);
213 sq_quote_buf(buf
, value
);
214 strbuf_addstr(buf
, "; ");
218 static int run_specific_rebase(struct rebase_options
*opts
)
220 const char *argv
[] = { NULL
, NULL
};
221 struct strbuf script_snippet
= STRBUF_INIT
;
223 const char *backend
, *backend_func
;
225 add_var(&script_snippet
, "GIT_DIR", absolute_path(get_git_dir()));
226 add_var(&script_snippet
, "state_dir", opts
->state_dir
);
228 add_var(&script_snippet
, "upstream_name", opts
->upstream_name
);
229 add_var(&script_snippet
, "upstream", opts
->upstream
?
230 oid_to_hex(&opts
->upstream
->object
.oid
) : NULL
);
231 add_var(&script_snippet
, "head_name",
232 opts
->head_name
? opts
->head_name
: "detached HEAD");
233 add_var(&script_snippet
, "orig_head", oid_to_hex(&opts
->orig_head
));
234 add_var(&script_snippet
, "onto", opts
->onto
?
235 oid_to_hex(&opts
->onto
->object
.oid
) : NULL
);
236 add_var(&script_snippet
, "onto_name", opts
->onto_name
);
237 add_var(&script_snippet
, "revisions", opts
->revisions
);
238 add_var(&script_snippet
, "restrict_revision", opts
->restrict_revision
?
239 oid_to_hex(&opts
->restrict_revision
->object
.oid
) : NULL
);
240 add_var(&script_snippet
, "GIT_QUIET",
241 opts
->flags
& REBASE_NO_QUIET
? "" : "t");
242 add_var(&script_snippet
, "git_am_opt", opts
->git_am_opt
.buf
);
243 add_var(&script_snippet
, "verbose",
244 opts
->flags
& REBASE_VERBOSE
? "t" : "");
245 add_var(&script_snippet
, "diffstat",
246 opts
->flags
& REBASE_DIFFSTAT
? "t" : "");
247 add_var(&script_snippet
, "force_rebase",
248 opts
->flags
& REBASE_FORCE
? "t" : "");
250 add_var(&script_snippet
, "switch_to", opts
->switch_to
);
251 add_var(&script_snippet
, "action", opts
->action
? opts
->action
: "");
253 switch (opts
->type
) {
255 backend
= "git-rebase--am";
256 backend_func
= "git_rebase__am";
258 case REBASE_INTERACTIVE
:
259 backend
= "git-rebase--interactive";
260 backend_func
= "git_rebase__interactive";
263 backend
= "git-rebase--merge";
264 backend_func
= "git_rebase__merge";
266 case REBASE_PRESERVE_MERGES
:
267 backend
= "git-rebase--preserve-merges";
268 backend_func
= "git_rebase__preserve_merges";
271 BUG("Unhandled rebase type %d", opts
->type
);
275 strbuf_addf(&script_snippet
,
276 ". git-sh-setup && . git-rebase--common &&"
277 " . %s && %s", backend
, backend_func
);
278 argv
[0] = script_snippet
.buf
;
280 status
= run_command_v_opt(argv
, RUN_USING_SHELL
);
281 if (opts
->dont_finish_rebase
)
283 else if (status
== 0) {
284 if (!file_exists(state_dir_path("stopped-sha", opts
)))
286 } else if (status
== 2) {
287 struct strbuf dir
= STRBUF_INIT
;
290 strbuf_addstr(&dir
, opts
->state_dir
);
291 remove_dir_recursively(&dir
, 0);
292 strbuf_release(&dir
);
293 die("Nothing to do");
296 strbuf_release(&script_snippet
);
298 return status
? -1 : 0;
301 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
303 static int reset_head(struct object_id
*oid
, const char *action
,
304 const char *switch_to_branch
, int detach_head
)
306 struct object_id head_oid
;
307 struct tree_desc desc
;
308 struct lock_file lock
= LOCK_INIT
;
309 struct unpack_trees_options unpack_tree_opts
;
311 const char *reflog_action
;
312 struct strbuf msg
= STRBUF_INIT
;
314 struct object_id
*orig
= NULL
, oid_orig
,
315 *old_orig
= NULL
, oid_old_orig
;
318 if (switch_to_branch
&& !starts_with(switch_to_branch
, "refs/"))
319 BUG("Not a fully qualified branch: '%s'", switch_to_branch
);
321 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0)
325 if (get_oid("HEAD", &head_oid
)) {
326 rollback_lock_file(&lock
);
327 return error(_("could not determine HEAD revision"));
332 memset(&unpack_tree_opts
, 0, sizeof(unpack_tree_opts
));
333 setup_unpack_trees_porcelain(&unpack_tree_opts
, action
);
334 unpack_tree_opts
.head_idx
= 1;
335 unpack_tree_opts
.src_index
= the_repository
->index
;
336 unpack_tree_opts
.dst_index
= the_repository
->index
;
337 unpack_tree_opts
.fn
= oneway_merge
;
338 unpack_tree_opts
.update
= 1;
339 unpack_tree_opts
.merge
= 1;
341 unpack_tree_opts
.reset
= 1;
343 if (read_index_unmerged(the_repository
->index
) < 0) {
344 rollback_lock_file(&lock
);
345 return error(_("could not read index"));
348 if (!fill_tree_descriptor(&desc
, oid
)) {
349 error(_("failed to find tree of %s"), oid_to_hex(oid
));
350 rollback_lock_file(&lock
);
351 free((void *)desc
.buffer
);
355 if (unpack_trees(1, &desc
, &unpack_tree_opts
)) {
356 rollback_lock_file(&lock
);
357 free((void *)desc
.buffer
);
361 tree
= parse_tree_indirect(oid
);
362 prime_cache_tree(the_repository
->index
, tree
);
364 if (write_locked_index(the_repository
->index
, &lock
, COMMIT_LOCK
) < 0)
365 ret
= error(_("could not write index"));
366 free((void *)desc
.buffer
);
371 reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
372 strbuf_addf(&msg
, "%s: ", reflog_action
? reflog_action
: "rebase");
373 prefix_len
= msg
.len
;
375 if (!get_oid("ORIG_HEAD", &oid_old_orig
))
376 old_orig
= &oid_old_orig
;
377 if (!get_oid("HEAD", &oid_orig
)) {
379 strbuf_addstr(&msg
, "updating ORIG_HEAD");
380 update_ref(msg
.buf
, "ORIG_HEAD", orig
, old_orig
, 0,
381 UPDATE_REFS_MSG_ON_ERR
);
383 delete_ref(NULL
, "ORIG_HEAD", old_orig
, 0);
384 strbuf_setlen(&msg
, prefix_len
);
385 strbuf_addstr(&msg
, "updating HEAD");
386 if (!switch_to_branch
)
387 ret
= update_ref(msg
.buf
, "HEAD", oid
, orig
, REF_NO_DEREF
,
388 UPDATE_REFS_MSG_ON_ERR
);
390 ret
= create_symref("HEAD", switch_to_branch
, msg
.buf
);
392 ret
= update_ref(msg
.buf
, "HEAD", oid
, NULL
, 0,
393 UPDATE_REFS_MSG_ON_ERR
);
396 strbuf_release(&msg
);
400 static int rebase_config(const char *var
, const char *value
, void *data
)
402 struct rebase_options
*opts
= data
;
404 if (!strcmp(var
, "rebase.stat")) {
405 if (git_config_bool(var
, value
))
406 opts
->flags
|= REBASE_DIFFSTAT
;
408 opts
->flags
&= !REBASE_DIFFSTAT
;
412 return git_default_config(var
, value
, data
);
416 * Determines whether the commits in from..to are linear, i.e. contain
417 * no merge commits. This function *expects* `from` to be an ancestor of
420 static int is_linear_history(struct commit
*from
, struct commit
*to
)
422 while (to
&& to
!= from
) {
426 if (to
->parents
->next
)
428 to
= to
->parents
->item
;
433 static int can_fast_forward(struct commit
*onto
, struct object_id
*head_oid
,
434 struct object_id
*merge_base
)
436 struct commit
*head
= lookup_commit(the_repository
, head_oid
);
437 struct commit_list
*merge_bases
;
443 merge_bases
= get_merge_bases(onto
, head
);
444 if (merge_bases
&& !merge_bases
->next
) {
445 oidcpy(merge_base
, &merge_bases
->item
->object
.oid
);
446 res
= !oidcmp(merge_base
, &onto
->object
.oid
);
448 oidcpy(merge_base
, &null_oid
);
451 free_commit_list(merge_bases
);
452 return res
&& is_linear_history(onto
, head
);
455 /* -i followed by -m is still -i */
456 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
458 struct rebase_options
*opts
= opt
->value
;
460 if (!is_interactive(opts
))
461 opts
->type
= REBASE_MERGE
;
466 /* -i followed by -p is still explicitly interactive, but -p alone is not */
467 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
470 struct rebase_options
*opts
= opt
->value
;
472 opts
->type
= REBASE_INTERACTIVE
;
473 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
478 int cmd_rebase(int argc
, const char **argv
, const char *prefix
)
480 struct rebase_options options
= {
481 .type
= REBASE_UNSPECIFIED
,
482 .flags
= REBASE_NO_QUIET
,
483 .git_am_opt
= STRBUF_INIT
,
485 const char *branch_name
;
486 int ret
, flags
, total_argc
, in_progress
= 0;
487 int ok_to_skip_pre_rebase
= 0;
488 struct strbuf msg
= STRBUF_INIT
;
489 struct strbuf revisions
= STRBUF_INIT
;
490 struct strbuf buf
= STRBUF_INIT
;
491 struct object_id merge_base
;
499 ACTION_SHOW_CURRENT_PATCH
,
500 } action
= NO_ACTION
;
501 struct option builtin_rebase_options
[] = {
502 OPT_STRING(0, "onto", &options
.onto_name
,
504 N_("rebase onto given branch instead of upstream")),
505 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
506 N_("allow pre-rebase hook to run")),
507 OPT_NEGBIT('q', "quiet", &options
.flags
,
508 N_("be quiet. implies --no-stat"),
509 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
510 OPT_BIT('v', "verbose", &options
.flags
,
511 N_("display a diffstat of what changed upstream"),
512 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
513 {OPTION_NEGBIT
, 'n', "no-stat", &options
.flags
, NULL
,
514 N_("do not show diffstat of what changed upstream"),
515 PARSE_OPT_NOARG
, NULL
, REBASE_DIFFSTAT
},
516 OPT_BIT('f', "force-rebase", &options
.flags
,
517 N_("cherry-pick all commits, even if unchanged"),
519 OPT_BIT(0, "no-ff", &options
.flags
,
520 N_("cherry-pick all commits, even if unchanged"),
522 OPT_CMDMODE(0, "continue", &action
, N_("continue"),
524 OPT_CMDMODE(0, "skip", &action
,
525 N_("skip current patch and continue"), ACTION_SKIP
),
526 OPT_CMDMODE(0, "abort", &action
,
527 N_("abort and check out the original branch"),
529 OPT_CMDMODE(0, "quit", &action
,
530 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
531 OPT_CMDMODE(0, "edit-todo", &action
, N_("edit the todo list "
532 "during an interactive rebase"), ACTION_EDIT_TODO
),
533 OPT_CMDMODE(0, "show-current-patch", &action
,
534 N_("show the patch file being applied or merged"),
535 ACTION_SHOW_CURRENT_PATCH
),
536 { OPTION_CALLBACK
, 'm', "merge", &options
, NULL
,
537 N_("use merging strategies to rebase"),
538 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
540 { OPTION_CALLBACK
, 'i', "interactive", &options
, NULL
,
541 N_("let the user edit the list of commits to rebase"),
542 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
543 parse_opt_interactive
},
544 OPT_SET_INT('p', "preserve-merges", &options
.type
,
545 N_("try to recreate merges instead of ignoring "
546 "them"), REBASE_PRESERVE_MERGES
),
551 * NEEDSWORK: Once the builtin rebase has been tested enough
552 * and git-legacy-rebase.sh is retired to contrib/, this preamble
556 if (!use_builtin_rebase()) {
557 const char *path
= mkpath("%s/git-legacy-rebase",
560 if (sane_execvp(path
, (char **)argv
) < 0)
561 die_errno(_("could not exec %s"), path
);
563 BUG("sane_execvp() returned???");
566 if (argc
== 2 && !strcmp(argv
[1], "-h"))
567 usage_with_options(builtin_rebase_usage
,
568 builtin_rebase_options
);
570 prefix
= setup_git_directory();
571 trace_repo_setup(prefix
);
574 git_config(rebase_config
, &options
);
577 strbuf_addf(&buf
, "%s/applying", apply_dir());
578 if(file_exists(buf
.buf
))
579 die(_("It looks like 'git am' is in progress. Cannot rebase."));
581 if (is_directory(apply_dir())) {
582 options
.type
= REBASE_AM
;
583 options
.state_dir
= apply_dir();
584 } else if (is_directory(merge_dir())) {
586 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
587 if (is_directory(buf
.buf
)) {
588 options
.type
= REBASE_PRESERVE_MERGES
;
589 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
592 strbuf_addf(&buf
, "%s/interactive", merge_dir());
593 if(file_exists(buf
.buf
)) {
594 options
.type
= REBASE_INTERACTIVE
;
595 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
597 options
.type
= REBASE_MERGE
;
599 options
.state_dir
= merge_dir();
602 if (options
.type
!= REBASE_UNSPECIFIED
)
606 argc
= parse_options(argc
, argv
, prefix
,
607 builtin_rebase_options
,
608 builtin_rebase_usage
, 0);
610 if (action
!= NO_ACTION
&& total_argc
!= 2) {
611 usage_with_options(builtin_rebase_usage
,
612 builtin_rebase_options
);
616 usage_with_options(builtin_rebase_usage
,
617 builtin_rebase_options
);
619 if (action
!= NO_ACTION
&& !in_progress
)
620 die(_("No rebase in progress?"));
622 if (action
== ACTION_EDIT_TODO
&& !is_interactive(&options
))
623 die(_("The --edit-todo action can only be used during "
624 "interactive rebase."));
627 case ACTION_CONTINUE
: {
628 struct object_id head
;
629 struct lock_file lock_file
= LOCK_INIT
;
632 options
.action
= "continue";
635 if (get_oid("HEAD", &head
))
636 die(_("Cannot read HEAD"));
638 fd
= hold_locked_index(&lock_file
, 0);
639 if (read_index(the_repository
->index
) < 0)
640 die(_("could not read index"));
641 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
644 update_index_if_able(the_repository
->index
,
646 rollback_lock_file(&lock_file
);
648 if (has_unstaged_changes(1)) {
649 puts(_("You must edit all merge conflicts and then\n"
650 "mark them as resolved using git add"));
653 if (read_basic_state(&options
))
658 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
660 options
.action
= "skip";
662 rerere_clear(&merge_rr
);
663 string_list_clear(&merge_rr
, 1);
665 if (reset_head(NULL
, "reset", NULL
, 0) < 0)
666 die(_("could not discard worktree changes"));
667 if (read_basic_state(&options
))
672 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
673 options
.action
= "abort";
675 rerere_clear(&merge_rr
);
676 string_list_clear(&merge_rr
, 1);
678 if (read_basic_state(&options
))
680 if (reset_head(&options
.orig_head
, "reset",
681 options
.head_name
, 0) < 0)
682 die(_("could not move back to %s"),
683 oid_to_hex(&options
.orig_head
));
684 ret
= finish_rebase(&options
);
689 strbuf_addstr(&buf
, options
.state_dir
);
690 ret
= !!remove_dir_recursively(&buf
, 0);
692 die(_("could not remove '%s'"), options
.state_dir
);
695 case ACTION_EDIT_TODO
:
696 options
.action
= "edit-todo";
697 options
.dont_finish_rebase
= 1;
699 case ACTION_SHOW_CURRENT_PATCH
:
700 options
.action
= "show-current-patch";
701 options
.dont_finish_rebase
= 1;
706 BUG("action: %d", action
);
709 /* Make sure no rebase is in progress */
711 const char *last_slash
= strrchr(options
.state_dir
, '/');
712 const char *state_dir_base
=
713 last_slash
? last_slash
+ 1 : options
.state_dir
;
714 const char *cmd_live_rebase
=
715 "git rebase (--continue | --abort | --skip)";
717 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
718 die(_("It seems that there is already a %s directory, and\n"
719 "I wonder if you are in the middle of another rebase. "
721 "case, please try\n\t%s\n"
722 "If that is not the case, please\n\t%s\n"
723 "and run me again. I am stopping in case you still "
725 "valuable there.\n"),
726 state_dir_base
, cmd_live_rebase
, buf
.buf
);
729 if (!(options
.flags
& REBASE_NO_QUIET
))
730 strbuf_addstr(&options
.git_am_opt
, " -q");
732 switch (options
.type
) {
734 case REBASE_INTERACTIVE
:
735 case REBASE_PRESERVE_MERGES
:
736 options
.state_dir
= merge_dir();
739 options
.state_dir
= apply_dir();
742 /* the default rebase backend is `--am` */
743 options
.type
= REBASE_AM
;
744 options
.state_dir
= apply_dir();
750 die("TODO: handle @{upstream}");
752 options
.upstream_name
= argv
[0];
755 if (!strcmp(options
.upstream_name
, "-"))
756 options
.upstream_name
= "@{-1}";
758 options
.upstream
= peel_committish(options
.upstream_name
);
759 if (!options
.upstream
)
760 die(_("invalid upstream '%s'"), options
.upstream_name
);
761 options
.upstream_arg
= options
.upstream_name
;
763 die("TODO: upstream for --root");
765 /* Make sure the branch to rebase onto is valid. */
766 if (!options
.onto_name
)
767 options
.onto_name
= options
.upstream_name
;
768 if (strstr(options
.onto_name
, "...")) {
769 if (get_oid_mb(options
.onto_name
, &merge_base
) < 0)
770 die(_("'%s': need exactly one merge base"),
772 options
.onto
= lookup_commit_or_die(&merge_base
,
775 options
.onto
= peel_committish(options
.onto_name
);
777 die(_("Does not point to a valid commit '%s'"),
782 * If the branch to rebase is given, that is the branch we will rebase
783 * branch_name -- branch/commit being rebased, or
784 * HEAD (already detached)
785 * orig_head -- commit object name of tip of the branch before rebasing
786 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
789 /* Is it "rebase other branchname" or "rebase other commit"? */
790 branch_name
= argv
[0];
791 options
.switch_to
= argv
[0];
793 /* Is it a local branch? */
795 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
796 if (!read_ref(buf
.buf
, &options
.orig_head
))
797 options
.head_name
= xstrdup(buf
.buf
);
798 /* If not is it a valid ref (branch or commit)? */
799 else if (!get_oid(branch_name
, &options
.orig_head
))
800 options
.head_name
= NULL
;
802 die(_("fatal: no such branch/commit '%s'"),
804 } else if (argc
== 0) {
805 /* Do not need to switch branches, we are already on it. */
807 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL
,
809 if (!options
.head_name
)
810 die(_("No such ref: %s"), "HEAD");
811 if (flags
& REF_ISSYMREF
) {
812 if (!skip_prefix(options
.head_name
,
813 "refs/heads/", &branch_name
))
814 branch_name
= options
.head_name
;
817 free(options
.head_name
);
818 options
.head_name
= NULL
;
819 branch_name
= "HEAD";
821 if (get_oid("HEAD", &options
.orig_head
))
822 die(_("Could not resolve HEAD to a revision"));
824 BUG("unexpected number of arguments left to parse");
826 if (read_index(the_repository
->index
) < 0)
827 die(_("could not read index"));
829 if (require_clean_work_tree("rebase",
830 _("Please commit or stash them."), 1, 1)) {
836 * Now we are rebasing commits upstream..orig_head (or with --root,
837 * everything leading up to orig_head) on top of onto.
841 * Check if we are already based on onto with linear history,
842 * but this should be done only when upstream and onto are the same
843 * and if this is not an interactive rebase.
845 if (can_fast_forward(options
.onto
, &options
.orig_head
, &merge_base
) &&
846 !is_interactive(&options
) && !options
.restrict_revision
&&
847 !oidcmp(&options
.upstream
->object
.oid
, &options
.onto
->object
.oid
)) {
850 if (!(options
.flags
& REBASE_FORCE
)) {
851 /* Lazily switch to the target branch if needed... */
852 if (options
.switch_to
) {
853 struct object_id oid
;
855 if (get_oid(options
.switch_to
, &oid
) < 0) {
856 ret
= !!error(_("could not parse '%s'"),
862 strbuf_addf(&buf
, "rebase: checkout %s",
864 if (reset_head(&oid
, "checkout",
865 options
.head_name
, 0) < 0) {
866 ret
= !!error(_("could not switch to "
873 if (!(options
.flags
& REBASE_NO_QUIET
))
875 else if (!strcmp(branch_name
, "HEAD") &&
876 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
877 puts(_("HEAD is up to date."));
879 printf(_("Current branch %s is up to date.\n"),
881 ret
= !!finish_rebase(&options
);
883 } else if (!(options
.flags
& REBASE_NO_QUIET
))
885 else if (!strcmp(branch_name
, "HEAD") &&
886 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
887 puts(_("HEAD is up to date, rebase forced."));
889 printf(_("Current branch %s is up to date, rebase "
890 "forced.\n"), branch_name
);
893 /* If a hook exists, give it a chance to interrupt*/
894 if (!ok_to_skip_pre_rebase
&&
895 run_hook_le(NULL
, "pre-rebase", options
.upstream_arg
,
896 argc
? argv
[0] : NULL
, NULL
))
897 die(_("The pre-rebase hook refused to rebase."));
899 if (options
.flags
& REBASE_DIFFSTAT
) {
900 struct diff_options opts
;
902 if (options
.flags
& REBASE_VERBOSE
)
903 printf(_("Changes from %s to %s:\n"),
904 oid_to_hex(&merge_base
),
905 oid_to_hex(&options
.onto
->object
.oid
));
907 /* We want color (if set), but no pager */
909 opts
.stat_width
= -1; /* use full terminal width */
910 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
911 opts
.output_format
|=
912 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
913 opts
.detect_rename
= DIFF_DETECT_RENAME
;
914 diff_setup_done(&opts
);
915 diff_tree_oid(&merge_base
, &options
.onto
->object
.oid
,
921 if (is_interactive(&options
))
924 /* Detach HEAD and reset the tree */
925 if (options
.flags
& REBASE_NO_QUIET
)
926 printf(_("First, rewinding head to replay your work on top of "
929 strbuf_addf(&msg
, "rebase: checkout %s", options
.onto_name
);
930 if (reset_head(&options
.onto
->object
.oid
, "checkout", NULL
, 1))
931 die(_("Could not detach HEAD"));
932 strbuf_release(&msg
);
934 strbuf_addf(&revisions
, "%s..%s",
935 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
936 (options
.restrict_revision
?
937 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
938 oid_to_hex(&options
.upstream
->object
.oid
)),
939 oid_to_hex(&options
.orig_head
));
941 options
.revisions
= revisions
.buf
;
944 ret
= !!run_specific_rebase(&options
);
947 strbuf_release(&revisions
);
948 free(options
.head_name
);