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"
28 static char const * const builtin_rebase_usage
[] = {
29 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
30 "[<upstream>] [<branch>]"),
31 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
33 N_("git rebase --continue | --abort | --skip | --edit-todo"),
37 static GIT_PATH_FUNC(apply_dir
, "rebase-apply")
38 static GIT_PATH_FUNC(merge_dir
, "rebase-merge")
41 REBASE_UNSPECIFIED
= -1,
45 REBASE_PRESERVE_MERGES
48 static int use_builtin_rebase(void)
50 struct child_process cp
= CHILD_PROCESS_INIT
;
51 struct strbuf out
= STRBUF_INIT
;
52 int ret
, env
= git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1);
57 argv_array_pushl(&cp
.args
,
58 "config", "--bool", "rebase.usebuiltin", NULL
);
60 if (capture_command(&cp
, &out
, 6)) {
66 ret
= !strcmp("true", out
.buf
);
71 struct rebase_options
{
72 enum rebase_type type
;
73 const char *state_dir
;
74 struct commit
*upstream
;
75 const char *upstream_name
;
76 const char *upstream_arg
;
78 struct object_id orig_head
;
80 const char *onto_name
;
81 const char *revisions
;
82 const char *switch_to
;
84 struct object_id
*squash_onto
;
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 argv_array git_am_opts
;
97 int allow_rerere_autoupdate
;
103 int allow_empty_message
;
104 int rebase_merges
, rebase_cousins
;
105 char *strategy
, *strategy_opts
;
106 struct strbuf git_format_patch_opt
;
107 int reschedule_failed_exec
;
110 static int is_interactive(struct rebase_options
*opts
)
112 return opts
->type
== REBASE_INTERACTIVE
||
113 opts
->type
== REBASE_PRESERVE_MERGES
;
116 static void imply_interactive(struct rebase_options
*opts
, const char *option
)
118 switch (opts
->type
) {
120 die(_("%s requires an interactive rebase"), option
);
122 case REBASE_INTERACTIVE
:
123 case REBASE_PRESERVE_MERGES
:
126 /* we silently *upgrade* --merge to --interactive if needed */
128 opts
->type
= REBASE_INTERACTIVE
; /* implied */
133 /* Returns the filename prefixed by the state_dir */
134 static const char *state_dir_path(const char *filename
, struct rebase_options
*opts
)
136 static struct strbuf path
= STRBUF_INIT
;
137 static size_t prefix_len
;
140 strbuf_addf(&path
, "%s/", opts
->state_dir
);
141 prefix_len
= path
.len
;
144 strbuf_setlen(&path
, prefix_len
);
145 strbuf_addstr(&path
, filename
);
149 /* Read one file, then strip line endings */
150 static int read_one(const char *path
, struct strbuf
*buf
)
152 if (strbuf_read_file(buf
, path
, 0) < 0)
153 return error_errno(_("could not read '%s'"), path
);
154 strbuf_trim_trailing_newline(buf
);
158 /* Initialize the rebase options from the state directory. */
159 static int read_basic_state(struct rebase_options
*opts
)
161 struct strbuf head_name
= STRBUF_INIT
;
162 struct strbuf buf
= STRBUF_INIT
;
163 struct object_id oid
;
165 if (read_one(state_dir_path("head-name", opts
), &head_name
) ||
166 read_one(state_dir_path("onto", opts
), &buf
))
168 opts
->head_name
= starts_with(head_name
.buf
, "refs/") ?
169 xstrdup(head_name
.buf
) : NULL
;
170 strbuf_release(&head_name
);
171 if (get_oid(buf
.buf
, &oid
))
172 return error(_("could not get 'onto': '%s'"), buf
.buf
);
173 opts
->onto
= lookup_commit_or_die(&oid
, buf
.buf
);
176 * We always write to orig-head, but interactive rebase used to write to
177 * head. Fall back to reading from head to cover for the case that the
178 * user upgraded git with an ongoing interactive rebase.
181 if (file_exists(state_dir_path("orig-head", opts
))) {
182 if (read_one(state_dir_path("orig-head", opts
), &buf
))
184 } else if (read_one(state_dir_path("head", opts
), &buf
))
186 if (get_oid(buf
.buf
, &opts
->orig_head
))
187 return error(_("invalid orig-head: '%s'"), buf
.buf
);
190 if (read_one(state_dir_path("quiet", opts
), &buf
))
193 opts
->flags
&= ~REBASE_NO_QUIET
;
195 opts
->flags
|= REBASE_NO_QUIET
;
197 if (file_exists(state_dir_path("verbose", opts
)))
198 opts
->flags
|= REBASE_VERBOSE
;
200 if (file_exists(state_dir_path("signoff", opts
))) {
202 opts
->flags
|= REBASE_FORCE
;
205 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts
))) {
207 if (read_one(state_dir_path("allow_rerere_autoupdate", opts
),
210 if (!strcmp(buf
.buf
, "--rerere-autoupdate"))
211 opts
->allow_rerere_autoupdate
= 1;
212 else if (!strcmp(buf
.buf
, "--no-rerere-autoupdate"))
213 opts
->allow_rerere_autoupdate
= 0;
215 warning(_("ignoring invalid allow_rerere_autoupdate: "
218 opts
->allow_rerere_autoupdate
= -1;
220 if (file_exists(state_dir_path("gpg_sign_opt", opts
))) {
222 if (read_one(state_dir_path("gpg_sign_opt", opts
),
225 free(opts
->gpg_sign_opt
);
226 opts
->gpg_sign_opt
= xstrdup(buf
.buf
);
229 if (file_exists(state_dir_path("strategy", opts
))) {
231 if (read_one(state_dir_path("strategy", opts
), &buf
))
233 free(opts
->strategy
);
234 opts
->strategy
= xstrdup(buf
.buf
);
237 if (file_exists(state_dir_path("strategy_opts", opts
))) {
239 if (read_one(state_dir_path("strategy_opts", opts
), &buf
))
241 free(opts
->strategy_opts
);
242 opts
->strategy_opts
= xstrdup(buf
.buf
);
245 strbuf_release(&buf
);
250 static int apply_autostash(struct rebase_options
*opts
)
252 const char *path
= state_dir_path("autostash", opts
);
253 struct strbuf autostash
= STRBUF_INIT
;
254 struct child_process stash_apply
= CHILD_PROCESS_INIT
;
256 if (!file_exists(path
))
259 if (read_one(path
, &autostash
))
260 return error(_("Could not read '%s'"), path
);
261 /* Ensure that the hash is not mistaken for a number */
262 strbuf_addstr(&autostash
, "^0");
263 argv_array_pushl(&stash_apply
.args
,
264 "stash", "apply", autostash
.buf
, NULL
);
265 stash_apply
.git_cmd
= 1;
266 stash_apply
.no_stderr
= stash_apply
.no_stdout
=
267 stash_apply
.no_stdin
= 1;
268 if (!run_command(&stash_apply
))
269 printf(_("Applied autostash.\n"));
271 struct argv_array args
= ARGV_ARRAY_INIT
;
274 argv_array_pushl(&args
,
275 "stash", "store", "-m", "autostash", "-q",
276 autostash
.buf
, NULL
);
277 if (run_command_v_opt(args
.argv
, RUN_GIT_CMD
))
278 res
= error(_("Cannot store %s"), autostash
.buf
);
279 argv_array_clear(&args
);
280 strbuf_release(&autostash
);
285 _("Applying autostash resulted in conflicts.\n"
286 "Your changes are safe in the stash.\n"
287 "You can run \"git stash pop\" or \"git stash drop\" "
291 strbuf_release(&autostash
);
295 static int finish_rebase(struct rebase_options
*opts
)
297 struct strbuf dir
= STRBUF_INIT
;
298 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
300 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
301 apply_autostash(opts
);
302 close_all_packs(the_repository
->objects
);
304 * We ignore errors in 'gc --auto', since the
305 * user should see them.
307 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
308 strbuf_addstr(&dir
, opts
->state_dir
);
309 remove_dir_recursively(&dir
, 0);
310 strbuf_release(&dir
);
315 static struct commit
*peel_committish(const char *name
)
318 struct object_id oid
;
320 if (get_oid(name
, &oid
))
322 obj
= parse_object(the_repository
, &oid
);
323 return (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
326 static void add_var(struct strbuf
*buf
, const char *name
, const char *value
)
329 strbuf_addf(buf
, "unset %s; ", name
);
331 strbuf_addf(buf
, "%s=", name
);
332 sq_quote_buf(buf
, value
);
333 strbuf_addstr(buf
, "; ");
337 static const char *resolvemsg
=
338 N_("Resolve all conflicts manually, mark them as resolved with\n"
339 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
340 "You can instead skip this commit: run \"git rebase --skip\".\n"
341 "To abort and get back to the state before \"git rebase\", run "
342 "\"git rebase --abort\".");
344 static int run_specific_rebase(struct rebase_options
*opts
)
346 const char *argv
[] = { NULL
, NULL
};
347 struct strbuf script_snippet
= STRBUF_INIT
, buf
= STRBUF_INIT
;
349 const char *backend
, *backend_func
;
351 if (opts
->type
== REBASE_INTERACTIVE
) {
352 /* Run builtin interactive rebase */
353 struct child_process child
= CHILD_PROCESS_INIT
;
355 argv_array_pushf(&child
.env_array
, "GIT_CHERRY_PICK_HELP=%s",
357 if (!(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
)) {
358 argv_array_push(&child
.env_array
, "GIT_EDITOR=:");
359 opts
->autosquash
= 0;
363 argv_array_push(&child
.args
, "rebase--interactive");
366 argv_array_pushf(&child
.args
, "--%s", opts
->action
);
367 if (opts
->keep_empty
)
368 argv_array_push(&child
.args
, "--keep-empty");
369 if (opts
->rebase_merges
)
370 argv_array_push(&child
.args
, "--rebase-merges");
371 if (opts
->rebase_cousins
)
372 argv_array_push(&child
.args
, "--rebase-cousins");
373 if (opts
->autosquash
)
374 argv_array_push(&child
.args
, "--autosquash");
375 if (opts
->flags
& REBASE_VERBOSE
)
376 argv_array_push(&child
.args
, "--verbose");
377 if (opts
->flags
& REBASE_FORCE
)
378 argv_array_push(&child
.args
, "--no-ff");
379 if (opts
->restrict_revision
)
380 argv_array_pushf(&child
.args
,
381 "--restrict-revision=^%s",
382 oid_to_hex(&opts
->restrict_revision
->object
.oid
));
384 argv_array_pushf(&child
.args
, "--upstream=%s",
385 oid_to_hex(&opts
->upstream
->object
.oid
));
387 argv_array_pushf(&child
.args
, "--onto=%s",
388 oid_to_hex(&opts
->onto
->object
.oid
));
389 if (opts
->squash_onto
)
390 argv_array_pushf(&child
.args
, "--squash-onto=%s",
391 oid_to_hex(opts
->squash_onto
));
393 argv_array_pushf(&child
.args
, "--onto-name=%s",
395 argv_array_pushf(&child
.args
, "--head-name=%s",
397 opts
->head_name
: "detached HEAD");
399 argv_array_pushf(&child
.args
, "--strategy=%s",
401 if (opts
->strategy_opts
)
402 argv_array_pushf(&child
.args
, "--strategy-opts=%s",
403 opts
->strategy_opts
);
405 argv_array_pushf(&child
.args
, "--switch-to=%s",
408 argv_array_pushf(&child
.args
, "--cmd=%s", opts
->cmd
);
409 if (opts
->allow_empty_message
)
410 argv_array_push(&child
.args
, "--allow-empty-message");
411 if (opts
->allow_rerere_autoupdate
> 0)
412 argv_array_push(&child
.args
, "--rerere-autoupdate");
413 else if (opts
->allow_rerere_autoupdate
== 0)
414 argv_array_push(&child
.args
, "--no-rerere-autoupdate");
415 if (opts
->gpg_sign_opt
)
416 argv_array_push(&child
.args
, opts
->gpg_sign_opt
);
418 argv_array_push(&child
.args
, "--signoff");
419 if (opts
->reschedule_failed_exec
)
420 argv_array_push(&child
.args
, "--reschedule-failed-exec");
422 status
= run_command(&child
);
423 goto finished_rebase
;
426 add_var(&script_snippet
, "GIT_DIR", absolute_path(get_git_dir()));
427 add_var(&script_snippet
, "state_dir", opts
->state_dir
);
429 add_var(&script_snippet
, "upstream_name", opts
->upstream_name
);
430 add_var(&script_snippet
, "upstream", opts
->upstream
?
431 oid_to_hex(&opts
->upstream
->object
.oid
) : NULL
);
432 add_var(&script_snippet
, "head_name",
433 opts
->head_name
? opts
->head_name
: "detached HEAD");
434 add_var(&script_snippet
, "orig_head", oid_to_hex(&opts
->orig_head
));
435 add_var(&script_snippet
, "onto", opts
->onto
?
436 oid_to_hex(&opts
->onto
->object
.oid
) : NULL
);
437 add_var(&script_snippet
, "onto_name", opts
->onto_name
);
438 add_var(&script_snippet
, "revisions", opts
->revisions
);
439 add_var(&script_snippet
, "restrict_revision", opts
->restrict_revision
?
440 oid_to_hex(&opts
->restrict_revision
->object
.oid
) : NULL
);
441 add_var(&script_snippet
, "GIT_QUIET",
442 opts
->flags
& REBASE_NO_QUIET
? "" : "t");
443 sq_quote_argv_pretty(&buf
, opts
->git_am_opts
.argv
);
444 add_var(&script_snippet
, "git_am_opt", buf
.buf
);
445 strbuf_release(&buf
);
446 add_var(&script_snippet
, "verbose",
447 opts
->flags
& REBASE_VERBOSE
? "t" : "");
448 add_var(&script_snippet
, "diffstat",
449 opts
->flags
& REBASE_DIFFSTAT
? "t" : "");
450 add_var(&script_snippet
, "force_rebase",
451 opts
->flags
& REBASE_FORCE
? "t" : "");
453 add_var(&script_snippet
, "switch_to", opts
->switch_to
);
454 add_var(&script_snippet
, "action", opts
->action
? opts
->action
: "");
455 add_var(&script_snippet
, "signoff", opts
->signoff
? "--signoff" : "");
456 add_var(&script_snippet
, "allow_rerere_autoupdate",
457 opts
->allow_rerere_autoupdate
< 0 ? "" :
458 opts
->allow_rerere_autoupdate
?
459 "--rerere-autoupdate" : "--no-rerere-autoupdate");
460 add_var(&script_snippet
, "keep_empty", opts
->keep_empty
? "yes" : "");
461 add_var(&script_snippet
, "autosquash", opts
->autosquash
? "t" : "");
462 add_var(&script_snippet
, "gpg_sign_opt", opts
->gpg_sign_opt
);
463 add_var(&script_snippet
, "cmd", opts
->cmd
);
464 add_var(&script_snippet
, "allow_empty_message",
465 opts
->allow_empty_message
? "--allow-empty-message" : "");
466 add_var(&script_snippet
, "rebase_merges",
467 opts
->rebase_merges
? "t" : "");
468 add_var(&script_snippet
, "rebase_cousins",
469 opts
->rebase_cousins
? "t" : "");
470 add_var(&script_snippet
, "strategy", opts
->strategy
);
471 add_var(&script_snippet
, "strategy_opts", opts
->strategy_opts
);
472 add_var(&script_snippet
, "rebase_root", opts
->root
? "t" : "");
473 add_var(&script_snippet
, "squash_onto",
474 opts
->squash_onto
? oid_to_hex(opts
->squash_onto
) : "");
475 add_var(&script_snippet
, "git_format_patch_opt",
476 opts
->git_format_patch_opt
.buf
);
478 if (is_interactive(opts
) &&
479 !(opts
->flags
& REBASE_INTERACTIVE_EXPLICIT
)) {
480 strbuf_addstr(&script_snippet
,
481 "GIT_EDITOR=:; export GIT_EDITOR; ");
482 opts
->autosquash
= 0;
485 switch (opts
->type
) {
487 backend
= "git-rebase--am";
488 backend_func
= "git_rebase__am";
491 backend
= "git-rebase--merge";
492 backend_func
= "git_rebase__merge";
494 case REBASE_PRESERVE_MERGES
:
495 backend
= "git-rebase--preserve-merges";
496 backend_func
= "git_rebase__preserve_merges";
499 BUG("Unhandled rebase type %d", opts
->type
);
503 strbuf_addf(&script_snippet
,
504 ". git-sh-setup && . git-rebase--common &&"
505 " . %s && %s", backend
, backend_func
);
506 argv
[0] = script_snippet
.buf
;
508 status
= run_command_v_opt(argv
, RUN_USING_SHELL
);
510 if (opts
->dont_finish_rebase
)
512 else if (opts
->type
== REBASE_INTERACTIVE
)
513 ; /* interactive rebase cleans up after itself */
514 else if (status
== 0) {
515 if (!file_exists(state_dir_path("stopped-sha", opts
)))
517 } else if (status
== 2) {
518 struct strbuf dir
= STRBUF_INIT
;
520 apply_autostash(opts
);
521 strbuf_addstr(&dir
, opts
->state_dir
);
522 remove_dir_recursively(&dir
, 0);
523 strbuf_release(&dir
);
524 die("Nothing to do");
527 strbuf_release(&script_snippet
);
529 return status
? -1 : 0;
532 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
534 #define RESET_HEAD_DETACH (1<<0)
535 #define RESET_HEAD_HARD (1<<1)
537 static int reset_head(struct object_id
*oid
, const char *action
,
538 const char *switch_to_branch
, unsigned flags
,
539 const char *reflog_orig_head
, const char *reflog_head
)
541 unsigned detach_head
= flags
& RESET_HEAD_DETACH
;
542 unsigned reset_hard
= flags
& RESET_HEAD_HARD
;
543 struct object_id head_oid
;
544 struct tree_desc desc
[2] = { { NULL
}, { NULL
} };
545 struct lock_file lock
= LOCK_INIT
;
546 struct unpack_trees_options unpack_tree_opts
;
548 const char *reflog_action
;
549 struct strbuf msg
= STRBUF_INIT
;
551 struct object_id
*orig
= NULL
, oid_orig
,
552 *old_orig
= NULL
, oid_old_orig
;
555 if (switch_to_branch
&& !starts_with(switch_to_branch
, "refs/"))
556 BUG("Not a fully qualified branch: '%s'", switch_to_branch
);
558 if (hold_locked_index(&lock
, LOCK_REPORT_ON_ERROR
) < 0) {
560 goto leave_reset_head
;
563 if ((!oid
|| !reset_hard
) && get_oid("HEAD", &head_oid
)) {
564 ret
= error(_("could not determine HEAD revision"));
565 goto leave_reset_head
;
571 memset(&unpack_tree_opts
, 0, sizeof(unpack_tree_opts
));
572 setup_unpack_trees_porcelain(&unpack_tree_opts
, action
);
573 unpack_tree_opts
.head_idx
= 1;
574 unpack_tree_opts
.src_index
= the_repository
->index
;
575 unpack_tree_opts
.dst_index
= the_repository
->index
;
576 unpack_tree_opts
.fn
= reset_hard
? oneway_merge
: twoway_merge
;
577 unpack_tree_opts
.update
= 1;
578 unpack_tree_opts
.merge
= 1;
580 unpack_tree_opts
.reset
= 1;
582 if (read_index_unmerged(the_repository
->index
) < 0) {
583 ret
= error(_("could not read index"));
584 goto leave_reset_head
;
587 if (!reset_hard
&& !fill_tree_descriptor(&desc
[nr
++], &head_oid
)) {
588 ret
= error(_("failed to find tree of %s"),
589 oid_to_hex(&head_oid
));
590 goto leave_reset_head
;
593 if (!fill_tree_descriptor(&desc
[nr
++], oid
)) {
594 ret
= error(_("failed to find tree of %s"), oid_to_hex(oid
));
595 goto leave_reset_head
;
598 if (unpack_trees(nr
, desc
, &unpack_tree_opts
)) {
600 goto leave_reset_head
;
603 tree
= parse_tree_indirect(oid
);
604 prime_cache_tree(the_repository
->index
, tree
);
606 if (write_locked_index(the_repository
->index
, &lock
, COMMIT_LOCK
) < 0) {
607 ret
= error(_("could not write index"));
608 goto leave_reset_head
;
611 reflog_action
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
612 strbuf_addf(&msg
, "%s: ", reflog_action
? reflog_action
: "rebase");
613 prefix_len
= msg
.len
;
615 if (!get_oid("ORIG_HEAD", &oid_old_orig
))
616 old_orig
= &oid_old_orig
;
617 if (!get_oid("HEAD", &oid_orig
)) {
619 if (!reflog_orig_head
) {
620 strbuf_addstr(&msg
, "updating ORIG_HEAD");
621 reflog_orig_head
= msg
.buf
;
623 update_ref(reflog_orig_head
, "ORIG_HEAD", orig
, old_orig
, 0,
624 UPDATE_REFS_MSG_ON_ERR
);
626 delete_ref(NULL
, "ORIG_HEAD", old_orig
, 0);
628 strbuf_setlen(&msg
, prefix_len
);
629 strbuf_addstr(&msg
, "updating HEAD");
630 reflog_head
= msg
.buf
;
632 if (!switch_to_branch
)
633 ret
= update_ref(reflog_head
, "HEAD", oid
, orig
,
634 detach_head
? REF_NO_DEREF
: 0,
635 UPDATE_REFS_MSG_ON_ERR
);
637 ret
= create_symref("HEAD", switch_to_branch
, msg
.buf
);
639 ret
= update_ref(reflog_head
, "HEAD", oid
, NULL
, 0,
640 UPDATE_REFS_MSG_ON_ERR
);
644 strbuf_release(&msg
);
645 rollback_lock_file(&lock
);
647 free((void *)desc
[--nr
].buffer
);
651 static int rebase_config(const char *var
, const char *value
, void *data
)
653 struct rebase_options
*opts
= data
;
655 if (!strcmp(var
, "rebase.stat")) {
656 if (git_config_bool(var
, value
))
657 opts
->flags
|= REBASE_DIFFSTAT
;
659 opts
->flags
&= !REBASE_DIFFSTAT
;
663 if (!strcmp(var
, "rebase.autosquash")) {
664 opts
->autosquash
= git_config_bool(var
, value
);
668 if (!strcmp(var
, "commit.gpgsign")) {
669 free(opts
->gpg_sign_opt
);
670 opts
->gpg_sign_opt
= git_config_bool(var
, value
) ?
671 xstrdup("-S") : NULL
;
675 if (!strcmp(var
, "rebase.autostash")) {
676 opts
->autostash
= git_config_bool(var
, value
);
680 if (!strcmp(var
, "rebase.reschedulefailedexec")) {
681 opts
->reschedule_failed_exec
= git_config_bool(var
, value
);
685 return git_default_config(var
, value
, data
);
689 * Determines whether the commits in from..to are linear, i.e. contain
690 * no merge commits. This function *expects* `from` to be an ancestor of
693 static int is_linear_history(struct commit
*from
, struct commit
*to
)
695 while (to
&& to
!= from
) {
699 if (to
->parents
->next
)
701 to
= to
->parents
->item
;
706 static int can_fast_forward(struct commit
*onto
, struct object_id
*head_oid
,
707 struct object_id
*merge_base
)
709 struct commit
*head
= lookup_commit(the_repository
, head_oid
);
710 struct commit_list
*merge_bases
;
716 merge_bases
= get_merge_bases(onto
, head
);
717 if (merge_bases
&& !merge_bases
->next
) {
718 oidcpy(merge_base
, &merge_bases
->item
->object
.oid
);
719 res
= oideq(merge_base
, &onto
->object
.oid
);
721 oidcpy(merge_base
, &null_oid
);
724 free_commit_list(merge_bases
);
725 return res
&& is_linear_history(onto
, head
);
728 /* -i followed by -m is still -i */
729 static int parse_opt_merge(const struct option
*opt
, const char *arg
, int unset
)
731 struct rebase_options
*opts
= opt
->value
;
733 BUG_ON_OPT_NEG(unset
);
736 if (!is_interactive(opts
))
737 opts
->type
= REBASE_MERGE
;
742 /* -i followed by -p is still explicitly interactive, but -p alone is not */
743 static int parse_opt_interactive(const struct option
*opt
, const char *arg
,
746 struct rebase_options
*opts
= opt
->value
;
748 BUG_ON_OPT_NEG(unset
);
751 opts
->type
= REBASE_INTERACTIVE
;
752 opts
->flags
|= REBASE_INTERACTIVE_EXPLICIT
;
757 static void NORETURN
error_on_missing_default_upstream(void)
759 struct branch
*current_branch
= branch_get(NULL
);
762 "Please specify which branch you want to rebase against.\n"
763 "See git-rebase(1) for details.\n"
765 " git rebase '<branch>'\n"
767 current_branch
? _("There is no tracking information for "
768 "the current branch.") :
769 _("You are not currently on a branch."));
771 if (current_branch
) {
772 const char *remote
= current_branch
->remote_name
;
775 remote
= _("<remote>");
777 printf(_("If you wish to set tracking information for this "
778 "branch you can do so with:\n"
780 " git branch --set-upstream-to=%s/<branch> %s\n"
782 remote
, current_branch
->name
);
787 static void set_reflog_action(struct rebase_options
*options
)
790 struct strbuf buf
= STRBUF_INIT
;
792 if (!is_interactive(options
))
795 env
= getenv(GIT_REFLOG_ACTION_ENVIRONMENT
);
796 if (env
&& strcmp("rebase", env
))
797 return; /* only override it if it is "rebase" */
799 strbuf_addf(&buf
, "rebase -i (%s)", options
->action
);
800 setenv(GIT_REFLOG_ACTION_ENVIRONMENT
, buf
.buf
, 1);
801 strbuf_release(&buf
);
804 int cmd_rebase(int argc
, const char **argv
, const char *prefix
)
806 struct rebase_options options
= {
807 .type
= REBASE_UNSPECIFIED
,
808 .flags
= REBASE_NO_QUIET
,
809 .git_am_opts
= ARGV_ARRAY_INIT
,
810 .allow_rerere_autoupdate
= -1,
811 .allow_empty_message
= 1,
812 .git_format_patch_opt
= STRBUF_INIT
,
814 const char *branch_name
;
815 int ret
, flags
, total_argc
, in_progress
= 0;
816 int ok_to_skip_pre_rebase
= 0;
817 struct strbuf msg
= STRBUF_INIT
;
818 struct strbuf revisions
= STRBUF_INIT
;
819 struct strbuf buf
= STRBUF_INIT
;
820 struct object_id merge_base
;
828 ACTION_SHOW_CURRENT_PATCH
,
829 } action
= NO_ACTION
;
830 const char *gpg_sign
= NULL
;
831 struct string_list exec
= STRING_LIST_INIT_NODUP
;
832 const char *rebase_merges
= NULL
;
834 struct string_list strategy_options
= STRING_LIST_INIT_NODUP
;
835 struct object_id squash_onto
;
836 char *squash_onto_name
= NULL
;
837 struct option builtin_rebase_options
[] = {
838 OPT_STRING(0, "onto", &options
.onto_name
,
840 N_("rebase onto given branch instead of upstream")),
841 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase
,
842 N_("allow pre-rebase hook to run")),
843 OPT_NEGBIT('q', "quiet", &options
.flags
,
844 N_("be quiet. implies --no-stat"),
845 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
846 OPT_BIT('v', "verbose", &options
.flags
,
847 N_("display a diffstat of what changed upstream"),
848 REBASE_NO_QUIET
| REBASE_VERBOSE
| REBASE_DIFFSTAT
),
849 {OPTION_NEGBIT
, 'n', "no-stat", &options
.flags
, NULL
,
850 N_("do not show diffstat of what changed upstream"),
851 PARSE_OPT_NOARG
, NULL
, REBASE_DIFFSTAT
},
852 OPT_BOOL(0, "signoff", &options
.signoff
,
853 N_("add a Signed-off-by: line to each commit")),
854 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options
.git_am_opts
,
855 NULL
, N_("passed to 'git am'"),
857 OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
858 &options
.git_am_opts
, NULL
,
859 N_("passed to 'git am'"), PARSE_OPT_NOARG
),
860 OPT_PASSTHRU_ARGV(0, "ignore-date", &options
.git_am_opts
, NULL
,
861 N_("passed to 'git am'"), PARSE_OPT_NOARG
),
862 OPT_PASSTHRU_ARGV('C', NULL
, &options
.git_am_opts
, N_("n"),
863 N_("passed to 'git apply'"), 0),
864 OPT_PASSTHRU_ARGV(0, "whitespace", &options
.git_am_opts
,
865 N_("action"), N_("passed to 'git apply'"), 0),
866 OPT_BIT('f', "force-rebase", &options
.flags
,
867 N_("cherry-pick all commits, even if unchanged"),
869 OPT_BIT(0, "no-ff", &options
.flags
,
870 N_("cherry-pick all commits, even if unchanged"),
872 OPT_CMDMODE(0, "continue", &action
, N_("continue"),
874 OPT_CMDMODE(0, "skip", &action
,
875 N_("skip current patch and continue"), ACTION_SKIP
),
876 OPT_CMDMODE(0, "abort", &action
,
877 N_("abort and check out the original branch"),
879 OPT_CMDMODE(0, "quit", &action
,
880 N_("abort but keep HEAD where it is"), ACTION_QUIT
),
881 OPT_CMDMODE(0, "edit-todo", &action
, N_("edit the todo list "
882 "during an interactive rebase"), ACTION_EDIT_TODO
),
883 OPT_CMDMODE(0, "show-current-patch", &action
,
884 N_("show the patch file being applied or merged"),
885 ACTION_SHOW_CURRENT_PATCH
),
886 { OPTION_CALLBACK
, 'm', "merge", &options
, NULL
,
887 N_("use merging strategies to rebase"),
888 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
890 { OPTION_CALLBACK
, 'i', "interactive", &options
, NULL
,
891 N_("let the user edit the list of commits to rebase"),
892 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
,
893 parse_opt_interactive
},
894 OPT_SET_INT('p', "preserve-merges", &options
.type
,
895 N_("try to recreate merges instead of ignoring "
896 "them"), REBASE_PRESERVE_MERGES
),
897 OPT_BOOL(0, "rerere-autoupdate",
898 &options
.allow_rerere_autoupdate
,
899 N_("allow rerere to update index with resolved "
901 OPT_BOOL('k', "keep-empty", &options
.keep_empty
,
902 N_("preserve empty commits during rebase")),
903 OPT_BOOL(0, "autosquash", &options
.autosquash
,
904 N_("move commits that begin with "
905 "squash!/fixup! under -i")),
906 { OPTION_STRING
, 'S', "gpg-sign", &gpg_sign
, N_("key-id"),
907 N_("GPG-sign commits"),
908 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
909 OPT_BOOL(0, "autostash", &options
.autostash
,
910 N_("automatically stash/stash pop before and after")),
911 OPT_STRING_LIST('x', "exec", &exec
, N_("exec"),
912 N_("add exec lines after each commit of the "
914 OPT_BOOL(0, "allow-empty-message",
915 &options
.allow_empty_message
,
916 N_("allow rebasing commits with empty messages")),
917 {OPTION_STRING
, 'r', "rebase-merges", &rebase_merges
,
919 N_("try to rebase merges instead of skipping them"),
920 PARSE_OPT_OPTARG
, NULL
, (intptr_t)""},
921 OPT_BOOL(0, "fork-point", &fork_point
,
922 N_("use 'merge-base --fork-point' to refine upstream")),
923 OPT_STRING('s', "strategy", &options
.strategy
,
924 N_("strategy"), N_("use the given merge strategy")),
925 OPT_STRING_LIST('X', "strategy-option", &strategy_options
,
927 N_("pass the argument through to the merge "
929 OPT_BOOL(0, "root", &options
.root
,
930 N_("rebase all reachable commits up to the root(s)")),
931 OPT_BOOL(0, "reschedule-failed-exec",
932 &options
.reschedule_failed_exec
,
933 N_("automatically re-schedule any `exec` that fails")),
939 * NEEDSWORK: Once the builtin rebase has been tested enough
940 * and git-legacy-rebase.sh is retired to contrib/, this preamble
944 if (!use_builtin_rebase()) {
945 const char *path
= mkpath("%s/git-legacy-rebase",
948 if (sane_execvp(path
, (char **)argv
) < 0)
949 die_errno(_("could not exec %s"), path
);
951 BUG("sane_execvp() returned???");
954 if (argc
== 2 && !strcmp(argv
[1], "-h"))
955 usage_with_options(builtin_rebase_usage
,
956 builtin_rebase_options
);
958 prefix
= setup_git_directory();
959 trace_repo_setup(prefix
);
962 git_config(rebase_config
, &options
);
965 strbuf_addf(&buf
, "%s/applying", apply_dir());
966 if(file_exists(buf
.buf
))
967 die(_("It looks like 'git am' is in progress. Cannot rebase."));
969 if (is_directory(apply_dir())) {
970 options
.type
= REBASE_AM
;
971 options
.state_dir
= apply_dir();
972 } else if (is_directory(merge_dir())) {
974 strbuf_addf(&buf
, "%s/rewritten", merge_dir());
975 if (is_directory(buf
.buf
)) {
976 options
.type
= REBASE_PRESERVE_MERGES
;
977 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
980 strbuf_addf(&buf
, "%s/interactive", merge_dir());
981 if(file_exists(buf
.buf
)) {
982 options
.type
= REBASE_INTERACTIVE
;
983 options
.flags
|= REBASE_INTERACTIVE_EXPLICIT
;
985 options
.type
= REBASE_MERGE
;
987 options
.state_dir
= merge_dir();
990 if (options
.type
!= REBASE_UNSPECIFIED
)
994 argc
= parse_options(argc
, argv
, prefix
,
995 builtin_rebase_options
,
996 builtin_rebase_usage
, 0);
998 if (action
!= NO_ACTION
&& total_argc
!= 2) {
999 usage_with_options(builtin_rebase_usage
,
1000 builtin_rebase_options
);
1004 usage_with_options(builtin_rebase_usage
,
1005 builtin_rebase_options
);
1007 if (action
!= NO_ACTION
&& !in_progress
)
1008 die(_("No rebase in progress?"));
1009 setenv(GIT_REFLOG_ACTION_ENVIRONMENT
, "rebase", 0);
1011 if (action
== ACTION_EDIT_TODO
&& !is_interactive(&options
))
1012 die(_("The --edit-todo action can only be used during "
1013 "interactive rebase."));
1016 case ACTION_CONTINUE
: {
1017 struct object_id head
;
1018 struct lock_file lock_file
= LOCK_INIT
;
1021 options
.action
= "continue";
1022 set_reflog_action(&options
);
1025 if (get_oid("HEAD", &head
))
1026 die(_("Cannot read HEAD"));
1028 fd
= hold_locked_index(&lock_file
, 0);
1029 if (read_index(the_repository
->index
) < 0)
1030 die(_("could not read index"));
1031 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
,
1034 update_index_if_able(the_repository
->index
,
1036 rollback_lock_file(&lock_file
);
1038 if (has_unstaged_changes(1)) {
1039 puts(_("You must edit all merge conflicts and then\n"
1040 "mark them as resolved using git add"));
1043 if (read_basic_state(&options
))
1048 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1050 options
.action
= "skip";
1051 set_reflog_action(&options
);
1053 rerere_clear(&merge_rr
);
1054 string_list_clear(&merge_rr
, 1);
1056 if (reset_head(NULL
, "reset", NULL
, RESET_HEAD_HARD
,
1058 die(_("could not discard worktree changes"));
1059 remove_branch_state();
1060 if (read_basic_state(&options
))
1064 case ACTION_ABORT
: {
1065 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1066 options
.action
= "abort";
1067 set_reflog_action(&options
);
1069 rerere_clear(&merge_rr
);
1070 string_list_clear(&merge_rr
, 1);
1072 if (read_basic_state(&options
))
1074 if (reset_head(&options
.orig_head
, "reset",
1075 options
.head_name
, RESET_HEAD_HARD
,
1077 die(_("could not move back to %s"),
1078 oid_to_hex(&options
.orig_head
));
1079 remove_branch_state();
1080 ret
= finish_rebase(&options
);
1085 strbuf_addstr(&buf
, options
.state_dir
);
1086 ret
= !!remove_dir_recursively(&buf
, 0);
1088 die(_("could not remove '%s'"), options
.state_dir
);
1091 case ACTION_EDIT_TODO
:
1092 options
.action
= "edit-todo";
1093 options
.dont_finish_rebase
= 1;
1095 case ACTION_SHOW_CURRENT_PATCH
:
1096 options
.action
= "show-current-patch";
1097 options
.dont_finish_rebase
= 1;
1102 BUG("action: %d", action
);
1105 /* Make sure no rebase is in progress */
1107 const char *last_slash
= strrchr(options
.state_dir
, '/');
1108 const char *state_dir_base
=
1109 last_slash
? last_slash
+ 1 : options
.state_dir
;
1110 const char *cmd_live_rebase
=
1111 "git rebase (--continue | --abort | --skip)";
1113 strbuf_addf(&buf
, "rm -fr \"%s\"", options
.state_dir
);
1114 die(_("It seems that there is already a %s directory, and\n"
1115 "I wonder if you are in the middle of another rebase. "
1117 "case, please try\n\t%s\n"
1118 "If that is not the case, please\n\t%s\n"
1119 "and run me again. I am stopping in case you still "
1121 "valuable there.\n"),
1122 state_dir_base
, cmd_live_rebase
, buf
.buf
);
1125 for (i
= 0; i
< options
.git_am_opts
.argc
; i
++) {
1126 const char *option
= options
.git_am_opts
.argv
[i
], *p
;
1127 if (!strcmp(option
, "--committer-date-is-author-date") ||
1128 !strcmp(option
, "--ignore-date") ||
1129 !strcmp(option
, "--whitespace=fix") ||
1130 !strcmp(option
, "--whitespace=strip"))
1131 options
.flags
|= REBASE_FORCE
;
1132 else if (skip_prefix(option
, "-C", &p
)) {
1134 if (!isdigit(*(p
++)))
1135 die(_("switch `C' expects a "
1136 "numerical value"));
1137 } else if (skip_prefix(option
, "--whitespace=", &p
)) {
1138 if (*p
&& strcmp(p
, "warn") && strcmp(p
, "nowarn") &&
1139 strcmp(p
, "error") && strcmp(p
, "error-all"))
1140 die("Invalid whitespace option: '%s'", p
);
1144 if (!(options
.flags
& REBASE_NO_QUIET
))
1145 argv_array_push(&options
.git_am_opts
, "-q");
1147 if (options
.keep_empty
)
1148 imply_interactive(&options
, "--keep-empty");
1151 free(options
.gpg_sign_opt
);
1152 options
.gpg_sign_opt
= xstrfmt("-S%s", gpg_sign
);
1158 imply_interactive(&options
, "--exec");
1161 for (i
= 0; i
< exec
.nr
; i
++)
1162 strbuf_addf(&buf
, "exec %s\n", exec
.items
[i
].string
);
1163 options
.cmd
= xstrdup(buf
.buf
);
1166 if (rebase_merges
) {
1167 if (!*rebase_merges
)
1168 ; /* default mode; do nothing */
1169 else if (!strcmp("rebase-cousins", rebase_merges
))
1170 options
.rebase_cousins
= 1;
1171 else if (strcmp("no-rebase-cousins", rebase_merges
))
1172 die(_("Unknown mode: %s"), rebase_merges
);
1173 options
.rebase_merges
= 1;
1174 imply_interactive(&options
, "--rebase-merges");
1177 if (strategy_options
.nr
) {
1180 if (!options
.strategy
)
1181 options
.strategy
= "recursive";
1184 for (i
= 0; i
< strategy_options
.nr
; i
++)
1185 strbuf_addf(&buf
, " --%s",
1186 strategy_options
.items
[i
].string
);
1187 options
.strategy_opts
= xstrdup(buf
.buf
);
1190 if (options
.strategy
) {
1191 options
.strategy
= xstrdup(options
.strategy
);
1192 switch (options
.type
) {
1194 die(_("--strategy requires --merge or --interactive"));
1196 case REBASE_INTERACTIVE
:
1197 case REBASE_PRESERVE_MERGES
:
1200 case REBASE_UNSPECIFIED
:
1201 options
.type
= REBASE_MERGE
;
1204 BUG("unhandled rebase type (%d)", options
.type
);
1208 if (options
.root
&& !options
.onto_name
)
1209 imply_interactive(&options
, "--root without --onto");
1211 if (isatty(2) && options
.flags
& REBASE_NO_QUIET
)
1212 strbuf_addstr(&options
.git_format_patch_opt
, " --progress");
1214 switch (options
.type
) {
1216 case REBASE_INTERACTIVE
:
1217 case REBASE_PRESERVE_MERGES
:
1218 options
.state_dir
= merge_dir();
1221 options
.state_dir
= apply_dir();
1224 /* the default rebase backend is `--am` */
1225 options
.type
= REBASE_AM
;
1226 options
.state_dir
= apply_dir();
1230 if (options
.reschedule_failed_exec
&& !is_interactive(&options
))
1231 die(_("--reschedule-failed-exec requires an interactive rebase"));
1233 if (options
.git_am_opts
.argc
) {
1234 /* all am options except -q are compatible only with --am */
1235 for (i
= options
.git_am_opts
.argc
- 1; i
>= 0; i
--)
1236 if (strcmp(options
.git_am_opts
.argv
[i
], "-q"))
1239 if (is_interactive(&options
) && i
>= 0)
1240 die(_("error: cannot combine interactive options "
1241 "(--interactive, --exec, --rebase-merges, "
1242 "--preserve-merges, --keep-empty, --root + "
1243 "--onto) with am options (%s)"), buf
.buf
);
1244 if (options
.type
== REBASE_MERGE
&& i
>= 0)
1245 die(_("error: cannot combine merge options (--merge, "
1246 "--strategy, --strategy-option) with am options "
1250 if (options
.signoff
) {
1251 if (options
.type
== REBASE_PRESERVE_MERGES
)
1252 die("cannot combine '--signoff' with "
1253 "'--preserve-merges'");
1254 argv_array_push(&options
.git_am_opts
, "--signoff");
1255 options
.flags
|= REBASE_FORCE
;
1258 if (options
.type
== REBASE_PRESERVE_MERGES
) {
1260 * Note: incompatibility with --signoff handled in signoff block above
1261 * Note: incompatibility with --interactive is just a strong warning;
1262 * git-rebase.txt caveats with "unless you know what you are doing"
1264 if (options
.rebase_merges
)
1265 die(_("error: cannot combine '--preserve-merges' with "
1266 "'--rebase-merges'"));
1268 if (options
.reschedule_failed_exec
)
1269 die(_("error: cannot combine '--preserve-merges' with "
1270 "'--reschedule-failed-exec'"));
1273 if (options
.rebase_merges
) {
1274 if (strategy_options
.nr
)
1275 die(_("error: cannot combine '--rebase-merges' with "
1276 "'--strategy-option'"));
1277 if (options
.strategy
)
1278 die(_("error: cannot combine '--rebase-merges' with "
1282 if (!options
.root
) {
1284 struct branch
*branch
;
1286 branch
= branch_get(NULL
);
1287 options
.upstream_name
= branch_get_upstream(branch
,
1289 if (!options
.upstream_name
)
1290 error_on_missing_default_upstream();
1294 options
.upstream_name
= argv
[0];
1297 if (!strcmp(options
.upstream_name
, "-"))
1298 options
.upstream_name
= "@{-1}";
1300 options
.upstream
= peel_committish(options
.upstream_name
);
1301 if (!options
.upstream
)
1302 die(_("invalid upstream '%s'"), options
.upstream_name
);
1303 options
.upstream_arg
= options
.upstream_name
;
1305 if (!options
.onto_name
) {
1306 if (commit_tree("", 0, the_hash_algo
->empty_tree
, NULL
,
1307 &squash_onto
, NULL
, NULL
) < 0)
1308 die(_("Could not create new root commit"));
1309 options
.squash_onto
= &squash_onto
;
1310 options
.onto_name
= squash_onto_name
=
1311 xstrdup(oid_to_hex(&squash_onto
));
1313 options
.upstream_name
= NULL
;
1314 options
.upstream
= NULL
;
1316 usage_with_options(builtin_rebase_usage
,
1317 builtin_rebase_options
);
1318 options
.upstream_arg
= "--root";
1321 /* Make sure the branch to rebase onto is valid. */
1322 if (!options
.onto_name
)
1323 options
.onto_name
= options
.upstream_name
;
1324 if (strstr(options
.onto_name
, "...")) {
1325 if (get_oid_mb(options
.onto_name
, &merge_base
) < 0)
1326 die(_("'%s': need exactly one merge base"),
1328 options
.onto
= lookup_commit_or_die(&merge_base
,
1331 options
.onto
= peel_committish(options
.onto_name
);
1333 die(_("Does not point to a valid commit '%s'"),
1338 * If the branch to rebase is given, that is the branch we will rebase
1339 * branch_name -- branch/commit being rebased, or
1340 * HEAD (already detached)
1341 * orig_head -- commit object name of tip of the branch before rebasing
1342 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1345 /* Is it "rebase other branchname" or "rebase other commit"? */
1346 branch_name
= argv
[0];
1347 options
.switch_to
= argv
[0];
1349 /* Is it a local branch? */
1351 strbuf_addf(&buf
, "refs/heads/%s", branch_name
);
1352 if (!read_ref(buf
.buf
, &options
.orig_head
))
1353 options
.head_name
= xstrdup(buf
.buf
);
1354 /* If not is it a valid ref (branch or commit)? */
1355 else if (!get_oid(branch_name
, &options
.orig_head
))
1356 options
.head_name
= NULL
;
1358 die(_("fatal: no such branch/commit '%s'"),
1360 } else if (argc
== 0) {
1361 /* Do not need to switch branches, we are already on it. */
1363 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL
,
1365 if (!options
.head_name
)
1366 die(_("No such ref: %s"), "HEAD");
1367 if (flags
& REF_ISSYMREF
) {
1368 if (!skip_prefix(options
.head_name
,
1369 "refs/heads/", &branch_name
))
1370 branch_name
= options
.head_name
;
1373 free(options
.head_name
);
1374 options
.head_name
= NULL
;
1375 branch_name
= "HEAD";
1377 if (get_oid("HEAD", &options
.orig_head
))
1378 die(_("Could not resolve HEAD to a revision"));
1380 BUG("unexpected number of arguments left to parse");
1382 if (fork_point
> 0) {
1383 struct commit
*head
=
1384 lookup_commit_reference(the_repository
,
1385 &options
.orig_head
);
1386 options
.restrict_revision
=
1387 get_fork_point(options
.upstream_name
, head
);
1390 if (read_index(the_repository
->index
) < 0)
1391 die(_("could not read index"));
1393 if (options
.autostash
) {
1394 struct lock_file lock_file
= LOCK_INIT
;
1397 fd
= hold_locked_index(&lock_file
, 0);
1398 refresh_cache(REFRESH_QUIET
);
1400 update_index_if_able(&the_index
, &lock_file
);
1401 rollback_lock_file(&lock_file
);
1403 if (has_unstaged_changes(1) || has_uncommitted_changes(1)) {
1404 const char *autostash
=
1405 state_dir_path("autostash", &options
);
1406 struct child_process stash
= CHILD_PROCESS_INIT
;
1407 struct object_id oid
;
1408 struct commit
*head
=
1409 lookup_commit_reference(the_repository
,
1410 &options
.orig_head
);
1412 argv_array_pushl(&stash
.args
,
1413 "stash", "create", "autostash", NULL
);
1417 if (capture_command(&stash
, &buf
, GIT_MAX_HEXSZ
))
1418 die(_("Cannot autostash"));
1419 strbuf_trim_trailing_newline(&buf
);
1420 if (get_oid(buf
.buf
, &oid
))
1421 die(_("Unexpected stash response: '%s'"),
1424 strbuf_add_unique_abbrev(&buf
, &oid
, DEFAULT_ABBREV
);
1426 if (safe_create_leading_directories_const(autostash
))
1427 die(_("Could not create directory for '%s'"),
1429 write_file(autostash
, "%s", oid_to_hex(&oid
));
1430 printf(_("Created autostash: %s\n"), buf
.buf
);
1431 if (reset_head(&head
->object
.oid
, "reset --hard",
1432 NULL
, RESET_HEAD_HARD
, NULL
, NULL
) < 0)
1433 die(_("could not reset --hard"));
1434 printf(_("HEAD is now at %s"),
1435 find_unique_abbrev(&head
->object
.oid
,
1438 pp_commit_easy(CMIT_FMT_ONELINE
, head
, &buf
);
1440 printf(" %s", buf
.buf
);
1443 if (discard_index(the_repository
->index
) < 0 ||
1444 read_index(the_repository
->index
) < 0)
1445 die(_("could not read index"));
1449 if (require_clean_work_tree("rebase",
1450 _("Please commit or stash them."), 1, 1)) {
1456 * Now we are rebasing commits upstream..orig_head (or with --root,
1457 * everything leading up to orig_head) on top of onto.
1461 * Check if we are already based on onto with linear history,
1462 * but this should be done only when upstream and onto are the same
1463 * and if this is not an interactive rebase.
1465 if (can_fast_forward(options
.onto
, &options
.orig_head
, &merge_base
) &&
1466 !is_interactive(&options
) && !options
.restrict_revision
&&
1468 !oidcmp(&options
.upstream
->object
.oid
, &options
.onto
->object
.oid
)) {
1471 if (!(options
.flags
& REBASE_FORCE
)) {
1472 /* Lazily switch to the target branch if needed... */
1473 if (options
.switch_to
) {
1474 struct object_id oid
;
1476 if (get_oid(options
.switch_to
, &oid
) < 0) {
1477 ret
= !!error(_("could not parse '%s'"),
1483 strbuf_addf(&buf
, "%s: checkout %s",
1484 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
),
1486 if (reset_head(&oid
, "checkout",
1487 options
.head_name
, 0,
1488 NULL
, buf
.buf
) < 0) {
1489 ret
= !!error(_("could not switch to "
1496 if (!(options
.flags
& REBASE_NO_QUIET
))
1498 else if (!strcmp(branch_name
, "HEAD") &&
1499 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1500 puts(_("HEAD is up to date."));
1502 printf(_("Current branch %s is up to date.\n"),
1504 ret
= !!finish_rebase(&options
);
1506 } else if (!(options
.flags
& REBASE_NO_QUIET
))
1508 else if (!strcmp(branch_name
, "HEAD") &&
1509 resolve_ref_unsafe("HEAD", 0, NULL
, &flag
))
1510 puts(_("HEAD is up to date, rebase forced."));
1512 printf(_("Current branch %s is up to date, rebase "
1513 "forced.\n"), branch_name
);
1516 /* If a hook exists, give it a chance to interrupt*/
1517 if (!ok_to_skip_pre_rebase
&&
1518 run_hook_le(NULL
, "pre-rebase", options
.upstream_arg
,
1519 argc
? argv
[0] : NULL
, NULL
))
1520 die(_("The pre-rebase hook refused to rebase."));
1522 if (options
.flags
& REBASE_DIFFSTAT
) {
1523 struct diff_options opts
;
1525 if (options
.flags
& REBASE_VERBOSE
) {
1526 if (is_null_oid(&merge_base
))
1527 printf(_("Changes to %s:\n"),
1528 oid_to_hex(&options
.onto
->object
.oid
));
1530 printf(_("Changes from %s to %s:\n"),
1531 oid_to_hex(&merge_base
),
1532 oid_to_hex(&options
.onto
->object
.oid
));
1535 /* We want color (if set), but no pager */
1537 opts
.stat_width
= -1; /* use full terminal width */
1538 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
1539 opts
.output_format
|=
1540 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
1541 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1542 diff_setup_done(&opts
);
1543 diff_tree_oid(is_null_oid(&merge_base
) ?
1544 the_hash_algo
->empty_tree
: &merge_base
,
1545 &options
.onto
->object
.oid
, "", &opts
);
1546 diffcore_std(&opts
);
1550 if (is_interactive(&options
))
1553 /* Detach HEAD and reset the tree */
1554 if (options
.flags
& REBASE_NO_QUIET
)
1555 printf(_("First, rewinding head to replay your work on top of "
1558 strbuf_addf(&msg
, "%s: checkout %s",
1559 getenv(GIT_REFLOG_ACTION_ENVIRONMENT
), options
.onto_name
);
1560 if (reset_head(&options
.onto
->object
.oid
, "checkout", NULL
,
1561 RESET_HEAD_DETACH
, NULL
, msg
.buf
))
1562 die(_("Could not detach HEAD"));
1563 strbuf_release(&msg
);
1566 * If the onto is a proper descendant of the tip of the branch, then
1567 * we just fast-forwarded.
1570 if (!oidcmp(&merge_base
, &options
.orig_head
)) {
1571 printf(_("Fast-forwarded %s to %s.\n"),
1572 branch_name
, options
.onto_name
);
1573 strbuf_addf(&msg
, "rebase finished: %s onto %s",
1574 options
.head_name
? options
.head_name
: "detached HEAD",
1575 oid_to_hex(&options
.onto
->object
.oid
));
1576 reset_head(NULL
, "Fast-forwarded", options
.head_name
, 0,
1578 strbuf_release(&msg
);
1579 ret
= !!finish_rebase(&options
);
1583 strbuf_addf(&revisions
, "%s..%s",
1584 options
.root
? oid_to_hex(&options
.onto
->object
.oid
) :
1585 (options
.restrict_revision
?
1586 oid_to_hex(&options
.restrict_revision
->object
.oid
) :
1587 oid_to_hex(&options
.upstream
->object
.oid
)),
1588 oid_to_hex(&options
.orig_head
));
1590 options
.revisions
= revisions
.buf
;
1593 ret
= !!run_specific_rebase(&options
);
1596 strbuf_release(&revisions
);
1597 free(options
.head_name
);
1598 free(options
.gpg_sign_opt
);
1600 free(squash_onto_name
);