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 struct rebase_options
{
67 enum rebase_type type
;
68 const char *state_dir
;
69 struct commit
*upstream
;
70 const char *upstream_name
;
71 const char *upstream_arg
;
73 struct object_id orig_head
;
75 const char *onto_name
;
76 const char *revisions
;
77 const char *switch_to
;
79 struct object_id
*squash_onto
;
80 struct commit
*restrict_revision
;
81 int dont_finish_rebase
;
83 REBASE_NO_QUIET
= 1<<0,
84 REBASE_VERBOSE
= 1<<1,
85 REBASE_DIFFSTAT
= 1<<2,
87 REBASE_INTERACTIVE_EXPLICIT
= 1<<4,
89 struct strbuf git_am_opt
;
92 int allow_rerere_autoupdate
;
98 int allow_empty_message
;
99 int rebase_merges
, rebase_cousins
;
100 char *strategy
, *strategy_opts
;
103 static int is_interactive(struct rebase_options
*opts
)
105 return opts
->type
== REBASE_INTERACTIVE
||
106 opts
->type
== REBASE_PRESERVE_MERGES
;
109 static void imply_interactive(struct rebase_options
*opts
, const char *option
)
111 switch (opts
->type
) {
113 die(_("%s requires an interactive rebase"), option
);
115 case REBASE_INTERACTIVE
:
116 case REBASE_PRESERVE_MERGES
:
119 /* we silently *upgrade* --merge to --interactive if needed */
121 opts
->type
= REBASE_INTERACTIVE
; /* implied */
126 /* Returns the filename prefixed by the state_dir */
127 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
129 static struct strbuf path
= STRBUF_INIT
;
130 static size_t prefix_len
;
133 strbuf_addf(&path
, "%s/", opts
->state_dir
);
134 prefix_len
= path
.len
;
137 strbuf_setlen(&path
, prefix_len
);
138 strbuf_addstr(&path
, filename
);
142 /* Read one file, then strip line endings */
143 static int read_one(const char *path
, struct strbuf
*buf
)
145 if (strbuf_read_file(buf
, path
, 0) < 0)
146 return error_errno(_("could not read '%s'"), path
);
147 strbuf_trim_trailing_newline(buf
);
151 /* Initialize the rebase options from the state directory. */
152 static int read_basic_state(struct rebase_options
*opts
)
154 struct strbuf head_name
= STRBUF_INIT
;
155 struct strbuf buf
= STRBUF_INIT
;
156 struct object_id oid
;
158 if (read_one(state_dir_path("head-name", opts
), &head_name
) ||
159 read_one(state_dir_path("onto", opts
), &buf
))
161 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
162 xstrdup(head_name
.buf
) : NULL
;
163 strbuf_release(&head_name
);
164 if (get_oid(buf
.buf
, &oid
))
165 return error(_("could not get 'onto': '%s'"), buf
.buf
);
166 opts
->onto
= lookup_commit_or_die(&oid
, buf
.buf
);
169 * We always write to orig-head, but interactive rebase used to write to
170 * head. Fall back to reading from head to cover for the case that the
171 * user upgraded git with an ongoing interactive rebase.
174 if (file_exists(state_dir_path("orig-head", opts
))) {
175 if (read_one(state_dir_path("orig-head", opts
), &buf
))
177 } else if (read_one(state_dir_path("head", opts
), &buf
))
179 if (get_oid(buf
.buf
, &opts
->orig_head
))
180 return error(_("invalid orig-head: '%s'"), buf
.buf
);
183 if (read_one(state_dir_path("quiet", opts
), &buf
))
186 opts
->flags
&= ~REBASE_NO_QUIET
;
188 opts
->flags
|= REBASE_NO_QUIET
;
190 if (file_exists(state_dir_path("verbose", opts
)))
191 opts
->flags
|= REBASE_VERBOSE
;
193 if (file_exists(state_dir_path("signoff", opts
))) {
195 opts
->flags
|= REBASE_FORCE
;
198 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts
))) {
200 if (read_one(state_dir_path("allow_rerere_autoupdate", opts
),
203 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
204 opts
->allow_rerere_autoupdate
= 1;
205 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
206 opts
->allow_rerere_autoupdate
= 0;
208 warning(_("ignoring invalid allow_rerere_autoupdate: "
211 opts
->allow_rerere_autoupdate
= -1;
213 if (file_exists(state_dir_path("gpg_sign_opt", opts
))) {
215 if (read_one(state_dir_path("gpg_sign_opt", opts
),
218 free(opts
->gpg_sign_opt
);
219 opts
->gpg_sign_opt
= xstrdup(buf
.buf
);
222 if (file_exists(state_dir_path("strategy", opts
))) {
224 if (read_one(state_dir_path("strategy", opts
), &buf
))
226 free(opts
->strategy
);
227 opts
->strategy
= xstrdup(buf
.buf
);
230 if (file_exists(state_dir_path("strategy_opts", opts
))) {
232 if (read_one(state_dir_path("strategy_opts", opts
), &buf
))
234 free(opts
->strategy_opts
);
235 opts
->strategy_opts
= xstrdup(buf
.buf
);
238 strbuf_release(&buf
);
243 static int apply_autostash(struct rebase_options
*opts
)
245 const char *path
= state_dir_path("autostash", opts
);
246 struct strbuf autostash
= STRBUF_INIT
;
247 struct child_process stash_apply
= CHILD_PROCESS_INIT
;
249 if (!file_exists(path
))
252 if (read_one(state_dir_path("autostash", opts
), &autostash
))
253 return error(_("Could not read '%s'"), path
);
254 argv_array_pushl(&stash_apply
.args
,
255 "stash", "apply", autostash
.buf
, NULL
);
256 stash_apply
.git_cmd
= 1;
257 stash_apply
.no_stderr
= stash_apply
.no_stdout
=
258 stash_apply
.no_stdin
= 1;
259 if (!run_command(&stash_apply
))
260 printf(_("Applied autostash.\n"));
262 struct argv_array args
= ARGV_ARRAY_INIT
;
265 argv_array_pushl(&args
,
266 "stash", "store", "-m", "autostash", "-q",
267 autostash
.buf
, NULL
);
268 if (run_command_v_opt(args
.argv
, RUN_GIT_CMD
))
269 res
= error(_("Cannot store %s"), autostash
.buf
);
270 argv_array_clear(&args
);
271 strbuf_release(&autostash
);
276 _("Applying autostash resulted in conflicts.\n"
277 "Your changes are safe in the stash.\n"
278 "You can run \"git stash pop\" or \"git stash drop\" "
282 strbuf_release(&autostash
);
286 static int finish_rebase(struct rebase_options
*opts
)
288 struct strbuf dir
= STRBUF_INIT
;
289 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
291 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
292 apply_autostash(opts
);
293 close_all_packs(the_repository
->objects
);
295 * We ignore errors in 'gc --auto', since the
296 * user should see them.
298 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
299 strbuf_addstr(&dir
, opts
->state_dir
);
300 remove_dir_recursively(&dir
, 0);
301 strbuf_release(&dir
);
306 static struct commit
*peel_committish(const char *name
)
309 struct object_id oid
;
311 if (get_oid(name
, &oid
))
313 obj
= parse_object(the_repository
, &oid
);
314 return (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
317 static void add_var(struct strbuf
*buf
, const char *name
, const char *value
)
320 strbuf_addf(buf
, "unset %s; ", name
);
322 strbuf_addf(buf
, "%s=", name
);
323 sq_quote_buf(buf
, value
);
324 strbuf_addstr(buf
, "; ");
328 static int run_specific_rebase(struct rebase_options
*opts
)
330 const char *argv
[] = { NULL
, NULL
};
331 struct strbuf script_snippet
= STRBUF_INIT
;
333 const char *backend
, *backend_func
;
335 add_var(&script_snippet
, "GIT_DIR", absolute_path(get_git_dir()));
336 add_var(&script_snippet
, "state_dir", opts
->state_dir
);
338 add_var(&script_snippet
, "upstream_name", opts
->upstream_name
);
339 add_var(&script_snippet
, "upstream", opts
->upstream
?
340 oid_to_hex(&opts
->upstream
->object
.oid
) : NULL
);
341 add_var(&script_snippet
, "head_name",
342 opts
->head_name
? opts
->head_name
: "detached HEAD");
343 add_var(&script_snippet
, "orig_head", oid_to_hex(&opts
->orig_head
));
344 add_var(&script_snippet
, "onto", opts
->onto
?
345 oid_to_hex(&opts
->onto
->object
.oid
) : NULL
);
346 add_var(&script_snippet
, "onto_name", opts
->onto_name
);
347 add_var(&script_snippet
, "revisions", opts
->revisions
);
348 add_var(&script_snippet
, "restrict_revision", opts
->restrict_revision
?
349 oid_to_hex(&opts
->restrict_revision
->object
.oid
) : NULL
);
350 add_var(&script_snippet
, "GIT_QUIET",
351 opts
->flags
& REBASE_NO_QUIET
? "" : "t");
352 add_var(&script_snippet
, "git_am_opt", opts
->git_am_opt
.buf
);
353 add_var(&script_snippet
, "verbose",
354 opts
->flags
& REBASE_VERBOSE
? "t" : "");
355 add_var(&script_snippet
, "diffstat",
356 opts
->flags
& REBASE_DIFFSTAT
? "t" : "");
357 add_var(&script_snippet
, "force_rebase",
358 opts
->flags
& REBASE_FORCE
? "t" : "");
360 add_var(&script_snippet
, "switch_to", opts
->switch_to
);
361 add_var(&script_snippet
, "action", opts
->action
? opts
->action
: "");
362 add_var(&script_snippet
, "signoff", opts
->signoff
? "--signoff" : "");
363 add_var(&script_snippet
, "allow_rerere_autoupdate",
364 opts
->allow_rerere_autoupdate
< 0 ? "" :
365 opts
->allow_rerere_autoupdate
?
366 "--rerere-autoupdate" : "--no-rerere-autoupdate");
367 add_var(&script_snippet
, "keep_empty", opts
->keep_empty
? "yes" : "");
368 add_var(&script_snippet
, "autosquash", opts
->autosquash
? "t" : "");
369 add_var(&script_snippet
, "gpg_sign_opt", opts
->gpg_sign_opt
);
370 add_var(&script_snippet
, "cmd", opts
->cmd
);
371 add_var(&script_snippet
, "allow_empty_message",
372 opts
->allow_empty_message
? "--allow-empty-message" : "");
373 add_var(&script_snippet
, "rebase_merges",
374 opts
->rebase_merges
? "t" : "");
375 add_var(&script_snippet
, "rebase_cousins",
376 opts
->rebase_cousins
? "t" : "");
377 add_var(&script_snippet
, "strategy", opts
->strategy
);
378 add_var(&script_snippet
, "strategy_opts", opts
->strategy_opts
);
379 add_var(&script_snippet
, "rebase_root", opts
->root
? "t" : "");
380 add_var(&script_snippet
, "squash_onto",
381 opts
->squash_onto
? oid_to_hex(opts
->squash_onto
) : "");
383 switch (opts
->type
) {
385 backend
= "git-rebase--am";
386 backend_func
= "git_rebase__am";
388 case REBASE_INTERACTIVE
:
389 backend
= "git-rebase--interactive";
390 backend_func
= "git_rebase__interactive";
393 backend
= "git-rebase--merge";
394 backend_func
= "git_rebase__merge";
396 case REBASE_PRESERVE_MERGES
:
397 backend
= "git-rebase--preserve-merges";
398 backend_func
= "git_rebase__preserve_merges";
401 BUG("Unhandled rebase type %d", opts
->type
);
405 strbuf_addf(&script_snippet
,
406 ". git-sh-setup && . git-rebase--common &&"
407 " . %s && %s", backend
, backend_func
);
408 argv
[0] = script_snippet
.buf
;
410 status
= run_command_v_opt(argv
, RUN_USING_SHELL
);
411 if (opts
->dont_finish_rebase
)
413 else if (status
== 0) {
414 if (!file_exists(state_dir_path("stopped-sha", opts
)))
416 } else if (status
== 2) {
417 struct strbuf dir
= STRBUF_INIT
;
419 apply_autostash(opts
);
420 strbuf_addstr(&dir
, opts
->state_dir
);
421 remove_dir_recursively(&dir
, 0);
422 strbuf_release(&dir
);
423 die("Nothing to do");
426 strbuf_release(&script_snippet
);
428 return status
? -1 : 0;
431 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
433 static int reset_head(struct object_id
*oid
, const char *action
,
434 const char *switch_to_branch
, int detach_head
,
435 const char *reflog_orig_head
, const char *reflog_head
)
437 struct object_id head_oid
;
438 struct tree_desc desc
;
439 struct lock_file lock
= LOCK_INIT
;
440 struct unpack_trees_options unpack_tree_opts
;
442 const char *reflog_action
;
443 struct strbuf msg
= STRBUF_INIT
;
445 struct object_id
*orig
= NULL
, oid_orig
,
446 *old_orig
= NULL
, oid_old_orig
;
449 if (switch_to_branch
&& !starts_with(switch_to_branch
, "refs/"))
450 BUG("Not a fully qualified branch: '%s'", switch_to_branch
);
452 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0)
456 if (get_oid("HEAD", &head_oid
)) {
457 rollback_lock_file(&lock
);
458 return error(_("could not determine HEAD revision"));
463 memset(&unpack_tree_opts
, 0, sizeof(unpack_tree_opts
));
464 setup_unpack_trees_porcelain(&unpack_tree_opts
, action
);
465 unpack_tree_opts
.head_idx
= 1;
466 unpack_tree_opts
.src_index
= the_repository
->index
;
467 unpack_tree_opts
.dst_index
= the_repository
->index
;
468 unpack_tree_opts
.fn
= oneway_merge
;
469 unpack_tree_opts
.update
= 1;
470 unpack_tree_opts
.merge
= 1;
472 unpack_tree_opts
.reset
= 1;
474 if (read_index_unmerged(the_repository
->index
) < 0) {
475 rollback_lock_file(&lock
);
476 return error(_("could not read index"));
479 if (!fill_tree_descriptor(&desc
, oid
)) {
480 error(_("failed to find tree of %s"), oid_to_hex(oid
));
481 rollback_lock_file(&lock
);
482 free((void *)desc
.buffer
);
486 if (unpack_trees(1, &desc
, &unpack_tree_opts
)) {
487 rollback_lock_file(&lock
);
488 free((void *)desc
.buffer
);
492 tree
= parse_tree_indirect(oid
);
493 prime_cache_tree(the_repository
->index
, tree
);
495 if (write_locked_index(the_repository
->index
, &lock
, COMMIT_LOCK
) < 0)
496 ret
= error(_("could not write index"));
497 free((void *)desc
.buffer
);
502 reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
503 strbuf_addf(&msg
, "%s: ", reflog_action
? reflog_action
: "rebase");
504 prefix_len
= msg
.len
;
506 if (!get_oid("ORIG_HEAD", &oid_old_orig
))
507 old_orig
= &oid_old_orig
;
508 if (!get_oid("HEAD", &oid_orig
)) {
510 if (!reflog_orig_head
) {
511 strbuf_addstr(&msg
, "updating ORIG_HEAD");
512 reflog_orig_head
= msg
.buf
;
514 update_ref(reflog_orig_head
, "ORIG_HEAD", orig
, old_orig
, 0,
515 UPDATE_REFS_MSG_ON_ERR
);
517 delete_ref(NULL
, "ORIG_HEAD", old_orig
, 0);
519 strbuf_setlen(&msg
, prefix_len
);
520 strbuf_addstr(&msg
, "updating HEAD");
521 reflog_head
= msg
.buf
;
523 if (!switch_to_branch
)
524 ret
= update_ref(reflog_head
, "HEAD", oid
, orig
, REF_NO_DEREF
,
525 UPDATE_REFS_MSG_ON_ERR
);
527 ret
= create_symref("HEAD", switch_to_branch
, msg
.buf
);
529 ret
= update_ref(reflog_head
, "HEAD", oid
, NULL
, 0,
530 UPDATE_REFS_MSG_ON_ERR
);
533 strbuf_release(&msg
);
537 static int rebase_config(const char *var
, const char *value
, void *data
)
539 struct rebase_options
*opts
= data
;
541 if (!strcmp(var
, "rebase.stat")) {
542 if (git_config_bool(var
, value
))
543 opts
->flags
|= REBASE_DIFFSTAT
;
545 opts
->flags
&= !REBASE_DIFFSTAT
;
549 if (!strcmp(var
, "rebase.autosquash")) {
550 opts
->autosquash
= git_config_bool(var
, value
);
554 if (!strcmp(var
, "commit.gpgsign")) {
555 free(opts
->gpg_sign_opt
);
556 opts
->gpg_sign_opt
= git_config_bool(var
, value
) ?
557 xstrdup("-S") : NULL
;
561 if (!strcmp(var
, "rebase.autostash")) {
562 opts
->autostash
= git_config_bool(var
, value
);
566 return git_default_config(var
, value
, data
);
570 * Determines whether the commits in from..to are linear, i.e. contain
571 * no merge commits. This function *expects* `from` to be an ancestor of
574 static int is_linear_history(struct commit
*from
, struct commit
*to
)
576 while (to
&& to
!= from
) {
580 if (to
->parents
->next
)
582 to
= to
->parents
->item
;
587 static int can_fast_forward(struct commit
*onto
, struct object_id
*head_oid
,
588 struct object_id
*merge_base
)
590 struct commit
*head
= lookup_commit(the_repository
, head_oid
);
591 struct commit_list
*merge_bases
;
597 merge_bases
= get_merge_bases(onto
, head
);
598 if (merge_bases
&& !merge_bases
->next
) {
599 oidcpy(merge_base
, &merge_bases
->item
->object
.oid
);
600 res
= !oidcmp(merge_base
, &onto
->object
.oid
);
602 oidcpy(merge_base
, &null_oid
);
605 free_commit_list(merge_bases
);
606 return res
&& is_linear_history(onto
, head
);
609 /* -i followed by -m is still -i */
610 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
612 struct rebase_options
*opts
= opt
->value
;
614 if (!is_interactive(opts
))
615 opts
->type
= REBASE_MERGE
;
620 /* -i followed by -p is still explicitly interactive, but -p alone is not */
621 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
624 struct rebase_options
*opts
= opt
->value
;
626 opts
->type
= REBASE_INTERACTIVE
;
627 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
632 static void NORETURN
error_on_missing_default_upstream(void)
634 struct branch
*current_branch
= branch_get(NULL
);
637 "Please specify which branch you want to rebase against.\n"
638 "See git-rebase(1) for details.\n"
640 " git rebase '<branch>'\n"
642 current_branch
? _("There is no tracking information for "
643 "the current branch.") :
644 _("You are not currently on a branch."));
646 if (current_branch
) {
647 const char *remote
= current_branch
->remote_name
;
650 remote
= _("<remote>");
652 printf(_("If you wish to set tracking information for this "
653 "branch you can do so with:\n"
655 " git branch --set-upstream-to=%s/<branch> %s\n"
657 remote
, current_branch
->name
);
662 int cmd_rebase(int argc
, const char **argv
, const char *prefix
)
664 struct rebase_options options
= {
665 .type
= REBASE_UNSPECIFIED
,
666 .flags
= REBASE_NO_QUIET
,
667 .git_am_opt
= STRBUF_INIT
,
668 .allow_rerere_autoupdate
= -1,
669 .allow_empty_message
= 1,
671 const char *branch_name
;
672 int ret
, flags
, total_argc
, in_progress
= 0;
673 int ok_to_skip_pre_rebase
= 0;
674 struct strbuf msg
= STRBUF_INIT
;
675 struct strbuf revisions
= STRBUF_INIT
;
676 struct strbuf buf
= STRBUF_INIT
;
677 struct object_id merge_base
;
685 ACTION_SHOW_CURRENT_PATCH
,
686 } action
= NO_ACTION
;
687 int committer_date_is_author_date
= 0;
689 int ignore_whitespace
= 0;
690 const char *gpg_sign
= NULL
;
692 struct string_list whitespace
= STRING_LIST_INIT_NODUP
;
693 struct string_list exec
= STRING_LIST_INIT_NODUP
;
694 const char *rebase_merges
= NULL
;
696 struct string_list strategy_options
= STRING_LIST_INIT_NODUP
;
697 struct object_id squash_onto
;
698 char *squash_onto_name
= NULL
;
699 struct option builtin_rebase_options
[] = {
700 OPT_STRING(0, "onto", &options
.onto_name
,
702 N_("rebase onto given branch instead of upstream")),
703 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
704 N_("allow pre-rebase hook to run")),
705 OPT_NEGBIT('q', "quiet", &options
.flags
,
706 N_("be quiet. implies --no-stat"),
707 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
708 OPT_BIT('v', "verbose", &options
.flags
,
709 N_("display a diffstat of what changed upstream"),
710 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
711 {OPTION_NEGBIT
, 'n', "no-stat", &options
.flags
, NULL
,
712 N_("do not show diffstat of what changed upstream"),
713 PARSE_OPT_NOARG
, NULL
, REBASE_DIFFSTAT
},
714 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace
,
715 N_("passed to 'git apply'")),
716 OPT_BOOL(0, "signoff", &options
.signoff
,
717 N_("add a Signed-off-by: line to each commit")),
718 OPT_BOOL(0, "committer-date-is-author-date",
719 &committer_date_is_author_date
,
720 N_("passed to 'git am'")),
721 OPT_BOOL(0, "ignore-date", &ignore_date
,
722 N_("passed to 'git am'")),
723 OPT_BIT('f', "force-rebase", &options
.flags
,
724 N_("cherry-pick all commits, even if unchanged"),
726 OPT_BIT(0, "no-ff", &options
.flags
,
727 N_("cherry-pick all commits, even if unchanged"),
729 OPT_CMDMODE(0, "continue", &action
, N_("continue"),
731 OPT_CMDMODE(0, "skip", &action
,
732 N_("skip current patch and continue"), ACTION_SKIP
),
733 OPT_CMDMODE(0, "abort", &action
,
734 N_("abort and check out the original branch"),
736 OPT_CMDMODE(0, "quit", &action
,
737 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
738 OPT_CMDMODE(0, "edit-todo", &action
, N_("edit the todo list "
739 "during an interactive rebase"), ACTION_EDIT_TODO
),
740 OPT_CMDMODE(0, "show-current-patch", &action
,
741 N_("show the patch file being applied or merged"),
742 ACTION_SHOW_CURRENT_PATCH
),
743 { OPTION_CALLBACK
, 'm', "merge", &options
, NULL
,
744 N_("use merging strategies to rebase"),
745 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
747 { OPTION_CALLBACK
, 'i', "interactive", &options
, NULL
,
748 N_("let the user edit the list of commits to rebase"),
749 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
750 parse_opt_interactive
},
751 OPT_SET_INT('p', "preserve-merges", &options
.type
,
752 N_("try to recreate merges instead of ignoring "
753 "them"), REBASE_PRESERVE_MERGES
),
754 OPT_BOOL(0, "rerere-autoupdate",
755 &options
.allow_rerere_autoupdate
,
756 N_("allow rerere to update index with resolved "
758 OPT_BOOL('k', "keep-empty", &options
.keep_empty
,
759 N_("preserve empty commits during rebase")),
760 OPT_BOOL(0, "autosquash", &options
.autosquash
,
761 N_("move commits that begin with "
762 "squash!/fixup! under -i")),
763 { OPTION_STRING
, 'S', "gpg-sign", &gpg_sign
, N_("key-id"),
764 N_("GPG-sign commits"),
765 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
766 OPT_STRING_LIST(0, "whitespace", &whitespace
,
767 N_("whitespace"), N_("passed to 'git apply'")),
768 OPT_SET_INT('C', NULL
, &opt_c
, N_("passed to 'git apply'"),
770 OPT_BOOL(0, "autostash", &options
.autostash
,
771 N_("automatically stash/stash pop before and after")),
772 OPT_STRING_LIST('x', "exec", &exec
, N_("exec"),
773 N_("add exec lines after each commit of the "
775 OPT_BOOL(0, "allow-empty-message",
776 &options
.allow_empty_message
,
777 N_("allow rebasing commits with empty messages")),
778 {OPTION_STRING
, 'r', "rebase-merges", &rebase_merges
,
780 N_("try to rebase merges instead of skipping them"),
781 PARSE_OPT_OPTARG
, NULL
, (intptr_t)""},
782 OPT_BOOL(0, "fork-point", &fork_point
,
783 N_("use 'merge-base --fork-point' to refine upstream")),
784 OPT_STRING('s', "strategy", &options
.strategy
,
785 N_("strategy"), N_("use the given merge strategy")),
786 OPT_STRING_LIST('X', "strategy-option", &strategy_options
,
788 N_("pass the argument through to the merge "
790 OPT_BOOL(0, "root", &options
.root
,
791 N_("rebase all reachable commits up to the root(s)")),
796 * NEEDSWORK: Once the builtin rebase has been tested enough
797 * and git-legacy-rebase.sh is retired to contrib/, this preamble
801 if (!use_builtin_rebase()) {
802 const char *path
= mkpath("%s/git-legacy-rebase",
805 if (sane_execvp(path
, (char **)argv
) < 0)
806 die_errno(_("could not exec %s"), path
);
808 BUG("sane_execvp() returned???");
811 if (argc
== 2 && !strcmp(argv
[1], "-h"))
812 usage_with_options(builtin_rebase_usage
,
813 builtin_rebase_options
);
815 prefix
= setup_git_directory();
816 trace_repo_setup(prefix
);
819 git_config(rebase_config
, &options
);
822 strbuf_addf(&buf
, "%s/applying", apply_dir());
823 if(file_exists(buf
.buf
))
824 die(_("It looks like 'git am' is in progress. Cannot rebase."));
826 if (is_directory(apply_dir())) {
827 options
.type
= REBASE_AM
;
828 options
.state_dir
= apply_dir();
829 } else if (is_directory(merge_dir())) {
831 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
832 if (is_directory(buf
.buf
)) {
833 options
.type
= REBASE_PRESERVE_MERGES
;
834 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
837 strbuf_addf(&buf
, "%s/interactive", merge_dir());
838 if(file_exists(buf
.buf
)) {
839 options
.type
= REBASE_INTERACTIVE
;
840 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
842 options
.type
= REBASE_MERGE
;
844 options
.state_dir
= merge_dir();
847 if (options
.type
!= REBASE_UNSPECIFIED
)
851 argc
= parse_options(argc
, argv
, prefix
,
852 builtin_rebase_options
,
853 builtin_rebase_usage
, 0);
855 if (action
!= NO_ACTION
&& total_argc
!= 2) {
856 usage_with_options(builtin_rebase_usage
,
857 builtin_rebase_options
);
861 usage_with_options(builtin_rebase_usage
,
862 builtin_rebase_options
);
864 if (action
!= NO_ACTION
&& !in_progress
)
865 die(_("No rebase in progress?"));
867 if (action
== ACTION_EDIT_TODO
&& !is_interactive(&options
))
868 die(_("The --edit-todo action can only be used during "
869 "interactive rebase."));
872 case ACTION_CONTINUE
: {
873 struct object_id head
;
874 struct lock_file lock_file
= LOCK_INIT
;
877 options
.action
= "continue";
880 if (get_oid("HEAD", &head
))
881 die(_("Cannot read HEAD"));
883 fd
= hold_locked_index(&lock_file
, 0);
884 if (read_index(the_repository
->index
) < 0)
885 die(_("could not read index"));
886 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
889 update_index_if_able(the_repository
->index
,
891 rollback_lock_file(&lock_file
);
893 if (has_unstaged_changes(1)) {
894 puts(_("You must edit all merge conflicts and then\n"
895 "mark them as resolved using git add"));
898 if (read_basic_state(&options
))
903 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
905 options
.action
= "skip";
907 rerere_clear(&merge_rr
);
908 string_list_clear(&merge_rr
, 1);
910 if (reset_head(NULL
, "reset", NULL
, 0, NULL
, NULL
) < 0)
911 die(_("could not discard worktree changes"));
912 if (read_basic_state(&options
))
917 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
918 options
.action
= "abort";
920 rerere_clear(&merge_rr
);
921 string_list_clear(&merge_rr
, 1);
923 if (read_basic_state(&options
))
925 if (reset_head(&options
.orig_head
, "reset",
926 options
.head_name
, 0, NULL
, NULL
) < 0)
927 die(_("could not move back to %s"),
928 oid_to_hex(&options
.orig_head
));
929 ret
= finish_rebase(&options
);
934 strbuf_addstr(&buf
, options
.state_dir
);
935 ret
= !!remove_dir_recursively(&buf
, 0);
937 die(_("could not remove '%s'"), options
.state_dir
);
940 case ACTION_EDIT_TODO
:
941 options
.action
= "edit-todo";
942 options
.dont_finish_rebase
= 1;
944 case ACTION_SHOW_CURRENT_PATCH
:
945 options
.action
= "show-current-patch";
946 options
.dont_finish_rebase
= 1;
951 BUG("action: %d", action
);
954 /* Make sure no rebase is in progress */
956 const char *last_slash
= strrchr(options
.state_dir
, '/');
957 const char *state_dir_base
=
958 last_slash
? last_slash
+ 1 : options
.state_dir
;
959 const char *cmd_live_rebase
=
960 "git rebase (--continue | --abort | --skip)";
962 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
963 die(_("It seems that there is already a %s directory, and\n"
964 "I wonder if you are in the middle of another rebase. "
966 "case, please try\n\t%s\n"
967 "If that is not the case, please\n\t%s\n"
968 "and run me again. I am stopping in case you still "
970 "valuable there.\n"),
971 state_dir_base
, cmd_live_rebase
, buf
.buf
);
974 if (!(options
.flags
& REBASE_NO_QUIET
))
975 strbuf_addstr(&options
.git_am_opt
, " -q");
977 if (committer_date_is_author_date
) {
978 strbuf_addstr(&options
.git_am_opt
,
979 " --committer-date-is-author-date");
980 options
.flags
|= REBASE_FORCE
;
983 if (ignore_whitespace
)
984 strbuf_addstr(&options
.git_am_opt
, " --ignore-whitespace");
987 strbuf_addstr(&options
.git_am_opt
, " --ignore-date");
988 options
.flags
|= REBASE_FORCE
;
991 if (options
.keep_empty
)
992 imply_interactive(&options
, "--keep-empty");
995 free(options
.gpg_sign_opt
);
996 options
.gpg_sign_opt
= xstrfmt("-S%s", gpg_sign
);
1000 strbuf_addf(&options
.git_am_opt
, " -C%d", opt_c
);
1002 if (whitespace
.nr
) {
1005 for (i
= 0; i
< whitespace
.nr
; i
++) {
1006 const char *item
= whitespace
.items
[i
].string
;
1008 strbuf_addf(&options
.git_am_opt
, " --whitespace=%s",
1011 if ((!strcmp(item
, "fix")) || (!strcmp(item
, "strip")))
1012 options
.flags
|= REBASE_FORCE
;
1019 imply_interactive(&options
, "--exec");
1022 for (i
= 0; i
< exec
.nr
; i
++)
1023 strbuf_addf(&buf
, "exec %s\n", exec
.items
[i
].string
);
1024 options
.cmd
= xstrdup(buf
.buf
);
1027 if (rebase_merges
) {
1028 if (!*rebase_merges
)
1029 ; /* default mode; do nothing */
1030 else if (!strcmp("rebase-cousins", rebase_merges
))
1031 options
.rebase_cousins
= 1;
1032 else if (strcmp("no-rebase-cousins", rebase_merges
))
1033 die(_("Unknown mode: %s"), rebase_merges
);
1034 options
.rebase_merges
= 1;
1035 imply_interactive(&options
, "--rebase-merges");
1038 if (strategy_options
.nr
) {
1041 if (!options
.strategy
)
1042 options
.strategy
= "recursive";
1045 for (i
= 0; i
< strategy_options
.nr
; i
++)
1046 strbuf_addf(&buf
, " --%s",
1047 strategy_options
.items
[i
].string
);
1048 options
.strategy_opts
= xstrdup(buf
.buf
);
1051 if (options
.strategy
) {
1052 options
.strategy
= xstrdup(options
.strategy
);
1053 switch (options
.type
) {
1055 die(_("--strategy requires --merge or --interactive"));
1057 case REBASE_INTERACTIVE
:
1058 case REBASE_PRESERVE_MERGES
:
1061 case REBASE_UNSPECIFIED
:
1062 options
.type
= REBASE_MERGE
;
1065 BUG("unhandled rebase type (%d)", options
.type
);
1069 if (options
.root
&& !options
.onto_name
)
1070 imply_interactive(&options
, "--root without --onto");
1072 switch (options
.type
) {
1074 case REBASE_INTERACTIVE
:
1075 case REBASE_PRESERVE_MERGES
:
1076 options
.state_dir
= merge_dir();
1079 options
.state_dir
= apply_dir();
1082 /* the default rebase backend is `--am` */
1083 options
.type
= REBASE_AM
;
1084 options
.state_dir
= apply_dir();
1088 if (options
.signoff
) {
1089 if (options
.type
== REBASE_PRESERVE_MERGES
)
1090 die("cannot combine '--signoff' with "
1091 "'--preserve-merges'");
1092 strbuf_addstr(&options
.git_am_opt
, " --signoff");
1093 options
.flags
|= REBASE_FORCE
;
1096 if (!options
.root
) {
1098 struct branch
*branch
;
1100 branch
= branch_get(NULL
);
1101 options
.upstream_name
= branch_get_upstream(branch
,
1103 if (!options
.upstream_name
)
1104 error_on_missing_default_upstream();
1108 options
.upstream_name
= argv
[0];
1111 if (!strcmp(options
.upstream_name
, "-"))
1112 options
.upstream_name
= "@{-1}";
1114 options
.upstream
= peel_committish(options
.upstream_name
);
1115 if (!options
.upstream
)
1116 die(_("invalid upstream '%s'"), options
.upstream_name
);
1117 options
.upstream_arg
= options
.upstream_name
;
1119 if (!options
.onto_name
) {
1120 if (commit_tree("", 0, the_hash_algo
->empty_tree
, NULL
,
1121 &squash_onto
, NULL
, NULL
) < 0)
1122 die(_("Could not create new root commit"));
1123 options
.squash_onto
= &squash_onto
;
1124 options
.onto_name
= squash_onto_name
=
1125 xstrdup(oid_to_hex(&squash_onto
));
1127 options
.upstream_name
= NULL
;
1128 options
.upstream
= NULL
;
1130 usage_with_options(builtin_rebase_usage
,
1131 builtin_rebase_options
);
1132 options
.upstream_arg
= "--root";
1135 /* Make sure the branch to rebase onto is valid. */
1136 if (!options
.onto_name
)
1137 options
.onto_name
= options
.upstream_name
;
1138 if (strstr(options
.onto_name
, "...")) {
1139 if (get_oid_mb(options
.onto_name
, &merge_base
) < 0)
1140 die(_("'%s': need exactly one merge base"),
1142 options
.onto
= lookup_commit_or_die(&merge_base
,
1145 options
.onto
= peel_committish(options
.onto_name
);
1147 die(_("Does not point to a valid commit '%s'"),
1152 * If the branch to rebase is given, that is the branch we will rebase
1153 * branch_name -- branch/commit being rebased, or
1154 * HEAD (already detached)
1155 * orig_head -- commit object name of tip of the branch before rebasing
1156 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1159 /* Is it "rebase other branchname" or "rebase other commit"? */
1160 branch_name
= argv
[0];
1161 options
.switch_to
= argv
[0];
1163 /* Is it a local branch? */
1165 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
1166 if (!read_ref(buf
.buf
, &options
.orig_head
))
1167 options
.head_name
= xstrdup(buf
.buf
);
1168 /* If not is it a valid ref (branch or commit)? */
1169 else if (!get_oid(branch_name
, &options
.orig_head
))
1170 options
.head_name
= NULL
;
1172 die(_("fatal: no such branch/commit '%s'"),
1174 } else if (argc
== 0) {
1175 /* Do not need to switch branches, we are already on it. */
1177 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL
,
1179 if (!options
.head_name
)
1180 die(_("No such ref: %s"), "HEAD");
1181 if (flags
& REF_ISSYMREF
) {
1182 if (!skip_prefix(options
.head_name
,
1183 "refs/heads/", &branch_name
))
1184 branch_name
= options
.head_name
;
1187 free(options
.head_name
);
1188 options
.head_name
= NULL
;
1189 branch_name
= "HEAD";
1191 if (get_oid("HEAD", &options
.orig_head
))
1192 die(_("Could not resolve HEAD to a revision"));
1194 BUG("unexpected number of arguments left to parse");
1196 if (fork_point
> 0) {
1197 struct commit
*head
=
1198 lookup_commit_reference(the_repository
,
1199 &options
.orig_head
);
1200 options
.restrict_revision
=
1201 get_fork_point(options
.upstream_name
, head
);
1204 if (read_index(the_repository
->index
) < 0)
1205 die(_("could not read index"));
1207 if (options
.autostash
) {
1208 struct lock_file lock_file
= LOCK_INIT
;
1211 fd
= hold_locked_index(&lock_file
, 0);
1212 refresh_cache(REFRESH_QUIET
);
1214 update_index_if_able(&the_index
, &lock_file
);
1215 rollback_lock_file(&lock_file
);
1217 if (has_unstaged_changes(0) || has_uncommitted_changes(0)) {
1218 const char *autostash
=
1219 state_dir_path("autostash", &options
);
1220 struct child_process stash
= CHILD_PROCESS_INIT
;
1221 struct object_id oid
;
1222 struct commit
*head
=
1223 lookup_commit_reference(the_repository
,
1224 &options
.orig_head
);
1226 argv_array_pushl(&stash
.args
,
1227 "stash", "create", "autostash", NULL
);
1231 if (capture_command(&stash
, &buf
, GIT_MAX_HEXSZ
))
1232 die(_("Cannot autostash"));
1233 strbuf_trim_trailing_newline(&buf
);
1234 if (get_oid(buf
.buf
, &oid
))
1235 die(_("Unexpected stash response: '%s'"),
1238 strbuf_add_unique_abbrev(&buf
, &oid
, DEFAULT_ABBREV
);
1240 if (safe_create_leading_directories_const(autostash
))
1241 die(_("Could not create directory for '%s'"),
1243 write_file(autostash
, "%s", buf
.buf
);
1244 printf(_("Created autostash: %s\n"), buf
.buf
);
1245 if (reset_head(&head
->object
.oid
, "reset --hard",
1246 NULL
, 0, NULL
, NULL
) < 0)
1247 die(_("could not reset --hard"));
1248 printf(_("HEAD is now at %s"),
1249 find_unique_abbrev(&head
->object
.oid
,
1252 pp_commit_easy(CMIT_FMT_ONELINE
, head
, &buf
);
1254 printf(" %s", buf
.buf
);
1257 if (discard_index(the_repository
->index
) < 0 ||
1258 read_index(the_repository
->index
) < 0)
1259 die(_("could not read index"));
1263 if (require_clean_work_tree("rebase",
1264 _("Please commit or stash them."), 1, 1)) {
1270 * Now we are rebasing commits upstream..orig_head (or with --root,
1271 * everything leading up to orig_head) on top of onto.
1275 * Check if we are already based on onto with linear history,
1276 * but this should be done only when upstream and onto are the same
1277 * and if this is not an interactive rebase.
1279 if (can_fast_forward(options
.onto
, &options
.orig_head
, &merge_base
) &&
1280 !is_interactive(&options
) && !options
.restrict_revision
&&
1282 !oidcmp(&options
.upstream
->object
.oid
, &options
.onto
->object
.oid
)) {
1285 if (!(options
.flags
& REBASE_FORCE
)) {
1286 /* Lazily switch to the target branch if needed... */
1287 if (options
.switch_to
) {
1288 struct object_id oid
;
1290 if (get_oid(options
.switch_to
, &oid
) < 0) {
1291 ret
= !!error(_("could not parse '%s'"),
1297 strbuf_addf(&buf
, "rebase: checkout %s",
1299 if (reset_head(&oid
, "checkout",
1300 options
.head_name
, 0,
1302 ret
= !!error(_("could not switch to "
1309 if (!(options
.flags
& REBASE_NO_QUIET
))
1311 else if (!strcmp(branch_name
, "HEAD") &&
1312 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1313 puts(_("HEAD is up to date."));
1315 printf(_("Current branch %s is up to date.\n"),
1317 ret
= !!finish_rebase(&options
);
1319 } else if (!(options
.flags
& REBASE_NO_QUIET
))
1321 else if (!strcmp(branch_name
, "HEAD") &&
1322 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1323 puts(_("HEAD is up to date, rebase forced."));
1325 printf(_("Current branch %s is up to date, rebase "
1326 "forced.\n"), branch_name
);
1329 /* If a hook exists, give it a chance to interrupt*/
1330 if (!ok_to_skip_pre_rebase
&&
1331 run_hook_le(NULL
, "pre-rebase", options
.upstream_arg
,
1332 argc
? argv
[0] : NULL
, NULL
))
1333 die(_("The pre-rebase hook refused to rebase."));
1335 if (options
.flags
& REBASE_DIFFSTAT
) {
1336 struct diff_options opts
;
1338 if (options
.flags
& REBASE_VERBOSE
)
1339 printf(_("Changes from %s to %s:\n"),
1340 oid_to_hex(&merge_base
),
1341 oid_to_hex(&options
.onto
->object
.oid
));
1343 /* We want color (if set), but no pager */
1345 opts
.stat_width
= -1; /* use full terminal width */
1346 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
1347 opts
.output_format
|=
1348 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1349 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1350 diff_setup_done(&opts
);
1351 diff_tree_oid(&merge_base
, &options
.onto
->object
.oid
,
1353 diffcore_std(&opts
);
1357 if (is_interactive(&options
))
1360 /* Detach HEAD and reset the tree */
1361 if (options
.flags
& REBASE_NO_QUIET
)
1362 printf(_("First, rewinding head to replay your work on top of "
1365 strbuf_addf(&msg
, "rebase: checkout %s", options
.onto_name
);
1366 if (reset_head(&options
.onto
->object
.oid
, "checkout", NULL
, 1,
1368 die(_("Could not detach HEAD"));
1369 strbuf_release(&msg
);
1371 strbuf_addf(&revisions
, "%s..%s",
1372 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
1373 (options
.restrict_revision
?
1374 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
1375 oid_to_hex(&options
.upstream
->object
.oid
)),
1376 oid_to_hex(&options
.orig_head
));
1378 options
.revisions
= revisions
.buf
;
1381 ret
= !!run_specific_rebase(&options
);
1384 strbuf_release(&revisions
);
1385 free(options
.head_name
);
1386 free(options
.gpg_sign_opt
);
1388 free(squash_onto_name
);