]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/rebase.c
packfile: close commit-graph in close_all_packs
[thirdparty/git.git] / builtin / rebase.c
CommitLineData
55071ea2
PK
1/*
2 * "git rebase" builtin command
3 *
4 * Copyright (c) 2018 Pratik Karki
5 */
6
f8adbec9 7#define USE_THE_INDEX_COMPATIBILITY_MACROS
55071ea2
PK
8#include "builtin.h"
9#include "run-command.h"
10#include "exec-cmd.h"
11#include "argv-array.h"
12#include "dir.h"
ac7f467f
PK
13#include "packfile.h"
14#include "refs.h"
15#include "quote.h"
16#include "config.h"
17#include "cache-tree.h"
18#include "unpack-trees.h"
19#include "lockfile.h"
f28d40d3 20#include "parse-options.h"
075bc852 21#include "commit.h"
bff014da 22#include "diff.h"
e0333e5c 23#include "wt-status.h"
9a48a615 24#include "revision.h"
e0720a38 25#include "commit-reach.h"
122420c2 26#include "rerere.h"
5aec9271 27#include "branch.h"
f28d40d3
PK
28
29static char const * const builtin_rebase_usage[] = {
30 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
31 "[<upstream>] [<branch>]"),
32 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
33 "--root [<branch>]"),
34 N_("git rebase --continue | --abort | --skip | --edit-todo"),
35 NULL
36};
ac7f467f
PK
37
38static GIT_PATH_FUNC(apply_dir, "rebase-apply")
39static GIT_PATH_FUNC(merge_dir, "rebase-merge")
40
41enum rebase_type {
42 REBASE_UNSPECIFIED = -1,
43 REBASE_AM,
44 REBASE_MERGE,
45 REBASE_INTERACTIVE,
46 REBASE_PRESERVE_MERGES
47};
55071ea2
PK
48
49static int use_builtin_rebase(void)
50{
51 struct child_process cp = CHILD_PROCESS_INIT;
52 struct strbuf out = STRBUF_INIT;
62c23938
ÆAB
53 int ret, env = git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1);
54
55 if (env != -1)
56 return env;
55071ea2
PK
57
58 argv_array_pushl(&cp.args,
59 "config", "--bool", "rebase.usebuiltin", NULL);
60 cp.git_cmd = 1;
61 if (capture_command(&cp, &out, 6)) {
62 strbuf_release(&out);
5541bd5b 63 return 1;
55071ea2
PK
64 }
65
66 strbuf_trim(&out);
67 ret = !strcmp("true", out.buf);
68 strbuf_release(&out);
69 return ret;
70}
71
ac7f467f
PK
72struct rebase_options {
73 enum rebase_type type;
74 const char *state_dir;
75 struct commit *upstream;
76 const char *upstream_name;
06e4775a 77 const char *upstream_arg;
ac7f467f
PK
78 char *head_name;
79 struct object_id orig_head;
80 struct commit *onto;
81 const char *onto_name;
82 const char *revisions;
e65123a7 83 const char *switch_to;
ac7f467f 84 int root;
9dba809a 85 struct object_id *squash_onto;
ac7f467f
PK
86 struct commit *restrict_revision;
87 int dont_finish_rebase;
b4c8eb02
PK
88 enum {
89 REBASE_NO_QUIET = 1<<0,
bff014da
PK
90 REBASE_VERBOSE = 1<<1,
91 REBASE_DIFFSTAT = 1<<2,
1ed9c14f 92 REBASE_FORCE = 1<<3,
c54dacb5 93 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
b4c8eb02 94 } flags;
f5769680 95 struct argv_array git_am_opts;
f9573628 96 const char *action;
73d51ed0 97 int signoff;
ead98c11 98 int allow_rerere_autoupdate;
002ee2fe 99 int keep_empty;
051910a9 100 int autosquash;
12026a41 101 char *gpg_sign_opt;
6defce2b 102 int autostash;
68e46d78 103 char *cmd;
9b3a448b 104 int allow_empty_message;
3c3588c7 105 int rebase_merges, rebase_cousins;
ba1905a5 106 char *strategy, *strategy_opts;
cda614e4 107 struct strbuf git_format_patch_opt;
d421afa0 108 int reschedule_failed_exec;
ac7f467f
PK
109};
110
9a48a615
PK
111static int is_interactive(struct rebase_options *opts)
112{
113 return opts->type == REBASE_INTERACTIVE ||
114 opts->type == REBASE_PRESERVE_MERGES;
115}
116
002ee2fe
PK
117static void imply_interactive(struct rebase_options *opts, const char *option)
118{
119 switch (opts->type) {
120 case REBASE_AM:
121 die(_("%s requires an interactive rebase"), option);
122 break;
123 case REBASE_INTERACTIVE:
124 case REBASE_PRESERVE_MERGES:
125 break;
126 case REBASE_MERGE:
68aa495b 127 /* we now implement --merge via --interactive */
002ee2fe
PK
128 default:
129 opts->type = REBASE_INTERACTIVE; /* implied */
130 break;
131 }
132}
133
ac7f467f
PK
134/* Returns the filename prefixed by the state_dir */
135static const char *state_dir_path(const char *filename, struct rebase_options *opts)
136{
137 static struct strbuf path = STRBUF_INIT;
138 static size_t prefix_len;
139
140 if (!prefix_len) {
141 strbuf_addf(&path, "%s/", opts->state_dir);
142 prefix_len = path.len;
143 }
144
145 strbuf_setlen(&path, prefix_len);
146 strbuf_addstr(&path, filename);
147 return path.buf;
148}
149
f9573628
PK
150/* Read one file, then strip line endings */
151static int read_one(const char *path, struct strbuf *buf)
152{
153 if (strbuf_read_file(buf, path, 0) < 0)
154 return error_errno(_("could not read '%s'"), path);
155 strbuf_trim_trailing_newline(buf);
156 return 0;
157}
158
159/* Initialize the rebase options from the state directory. */
160static int read_basic_state(struct rebase_options *opts)
161{
162 struct strbuf head_name = STRBUF_INIT;
163 struct strbuf buf = STRBUF_INIT;
164 struct object_id oid;
165
166 if (read_one(state_dir_path("head-name", opts), &head_name) ||
167 read_one(state_dir_path("onto", opts), &buf))
168 return -1;
169 opts->head_name = starts_with(head_name.buf, "refs/") ?
170 xstrdup(head_name.buf) : NULL;
171 strbuf_release(&head_name);
172 if (get_oid(buf.buf, &oid))
173 return error(_("could not get 'onto': '%s'"), buf.buf);
174 opts->onto = lookup_commit_or_die(&oid, buf.buf);
175
176 /*
177 * We always write to orig-head, but interactive rebase used to write to
178 * head. Fall back to reading from head to cover for the case that the
179 * user upgraded git with an ongoing interactive rebase.
180 */
181 strbuf_reset(&buf);
182 if (file_exists(state_dir_path("orig-head", opts))) {
183 if (read_one(state_dir_path("orig-head", opts), &buf))
184 return -1;
185 } else if (read_one(state_dir_path("head", opts), &buf))
186 return -1;
187 if (get_oid(buf.buf, &opts->orig_head))
188 return error(_("invalid orig-head: '%s'"), buf.buf);
189
899b49c4 190 if (file_exists(state_dir_path("quiet", opts)))
f9573628
PK
191 opts->flags &= ~REBASE_NO_QUIET;
192 else
193 opts->flags |= REBASE_NO_QUIET;
194
195 if (file_exists(state_dir_path("verbose", opts)))
196 opts->flags |= REBASE_VERBOSE;
197
73d51ed0
PK
198 if (file_exists(state_dir_path("signoff", opts))) {
199 opts->signoff = 1;
200 opts->flags |= REBASE_FORCE;
201 }
202
ead98c11
PK
203 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
204 strbuf_reset(&buf);
205 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
206 &buf))
207 return -1;
208 if (!strcmp(buf.buf, "--rerere-autoupdate"))
209 opts->allow_rerere_autoupdate = 1;
210 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
211 opts->allow_rerere_autoupdate = 0;
212 else
213 warning(_("ignoring invalid allow_rerere_autoupdate: "
214 "'%s'"), buf.buf);
215 } else
216 opts->allow_rerere_autoupdate = -1;
217
12026a41
PK
218 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
219 strbuf_reset(&buf);
220 if (read_one(state_dir_path("gpg_sign_opt", opts),
221 &buf))
222 return -1;
223 free(opts->gpg_sign_opt);
224 opts->gpg_sign_opt = xstrdup(buf.buf);
225 }
226
ba1905a5
PK
227 if (file_exists(state_dir_path("strategy", opts))) {
228 strbuf_reset(&buf);
229 if (read_one(state_dir_path("strategy", opts), &buf))
230 return -1;
231 free(opts->strategy);
232 opts->strategy = xstrdup(buf.buf);
233 }
234
235 if (file_exists(state_dir_path("strategy_opts", opts))) {
236 strbuf_reset(&buf);
237 if (read_one(state_dir_path("strategy_opts", opts), &buf))
238 return -1;
239 free(opts->strategy_opts);
240 opts->strategy_opts = xstrdup(buf.buf);
241 }
242
f9573628
PK
243 strbuf_release(&buf);
244
245 return 0;
246}
247
21853626
JS
248static int write_basic_state(struct rebase_options *opts)
249{
250 write_file(state_dir_path("head-name", opts), "%s",
251 opts->head_name ? opts->head_name : "detached HEAD");
252 write_file(state_dir_path("onto", opts), "%s",
253 opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
254 write_file(state_dir_path("orig-head", opts), "%s",
255 oid_to_hex(&opts->orig_head));
256 write_file(state_dir_path("quiet", opts), "%s",
257 opts->flags & REBASE_NO_QUIET ? "" : "t");
258 if (opts->flags & REBASE_VERBOSE)
259 write_file(state_dir_path("verbose", opts), "%s", "");
260 if (opts->strategy)
261 write_file(state_dir_path("strategy", opts), "%s",
262 opts->strategy);
263 if (opts->strategy_opts)
264 write_file(state_dir_path("strategy_opts", opts), "%s",
265 opts->strategy_opts);
266 if (opts->allow_rerere_autoupdate >= 0)
267 write_file(state_dir_path("allow_rerere_autoupdate", opts),
268 "-%s-rerere-autoupdate",
269 opts->allow_rerere_autoupdate ? "" : "-no");
270 if (opts->gpg_sign_opt)
271 write_file(state_dir_path("gpg_sign_opt", opts), "%s",
272 opts->gpg_sign_opt);
273 if (opts->signoff)
274 write_file(state_dir_path("strategy", opts), "--signoff");
275
276 return 0;
277}
278
6defce2b
PK
279static int apply_autostash(struct rebase_options *opts)
280{
281 const char *path = state_dir_path("autostash", opts);
282 struct strbuf autostash = STRBUF_INIT;
283 struct child_process stash_apply = CHILD_PROCESS_INIT;
284
285 if (!file_exists(path))
286 return 0;
287
71064e60 288 if (read_one(path, &autostash))
6defce2b 289 return error(_("Could not read '%s'"), path);
b98e914e
JS
290 /* Ensure that the hash is not mistaken for a number */
291 strbuf_addstr(&autostash, "^0");
6defce2b
PK
292 argv_array_pushl(&stash_apply.args,
293 "stash", "apply", autostash.buf, NULL);
294 stash_apply.git_cmd = 1;
295 stash_apply.no_stderr = stash_apply.no_stdout =
296 stash_apply.no_stdin = 1;
297 if (!run_command(&stash_apply))
298 printf(_("Applied autostash.\n"));
299 else {
300 struct argv_array args = ARGV_ARRAY_INIT;
301 int res = 0;
302
303 argv_array_pushl(&args,
304 "stash", "store", "-m", "autostash", "-q",
305 autostash.buf, NULL);
306 if (run_command_v_opt(args.argv, RUN_GIT_CMD))
307 res = error(_("Cannot store %s"), autostash.buf);
308 argv_array_clear(&args);
309 strbuf_release(&autostash);
310 if (res)
311 return res;
312
313 fprintf(stderr,
314 _("Applying autostash resulted in conflicts.\n"
315 "Your changes are safe in the stash.\n"
316 "You can run \"git stash pop\" or \"git stash drop\" "
317 "at any time.\n"));
318 }
319
320 strbuf_release(&autostash);
321 return 0;
322}
323
ac7f467f
PK
324static int finish_rebase(struct rebase_options *opts)
325{
326 struct strbuf dir = STRBUF_INIT;
327 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
328
329 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
6defce2b 330 apply_autostash(opts);
ac7f467f
PK
331 close_all_packs(the_repository->objects);
332 /*
333 * We ignore errors in 'gc --auto', since the
334 * user should see them.
335 */
336 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
337 strbuf_addstr(&dir, opts->state_dir);
338 remove_dir_recursively(&dir, 0);
339 strbuf_release(&dir);
340
341 return 0;
342}
343
344static struct commit *peel_committish(const char *name)
345{
346 struct object *obj;
347 struct object_id oid;
348
349 if (get_oid(name, &oid))
350 return NULL;
351 obj = parse_object(the_repository, &oid);
352 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
353}
354
355static void add_var(struct strbuf *buf, const char *name, const char *value)
356{
357 if (!value)
358 strbuf_addf(buf, "unset %s; ", name);
359 else {
360 strbuf_addf(buf, "%s=", name);
361 sq_quote_buf(buf, value);
362 strbuf_addstr(buf, "; ");
363 }
364}
365
c5233708
JS
366#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
367
368#define RESET_HEAD_DETACH (1<<0)
369#define RESET_HEAD_HARD (1<<1)
e52c6bbd
JH
370#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
371#define RESET_HEAD_REFS_ONLY (1<<3)
c5233708
JS
372
373static int reset_head(struct object_id *oid, const char *action,
374 const char *switch_to_branch, unsigned flags,
375 const char *reflog_orig_head, const char *reflog_head)
376{
377 unsigned detach_head = flags & RESET_HEAD_DETACH;
378 unsigned reset_hard = flags & RESET_HEAD_HARD;
e52c6bbd 379 unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
414f3360 380 unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
c5233708
JS
381 struct object_id head_oid;
382 struct tree_desc desc[2] = { { NULL }, { NULL } };
383 struct lock_file lock = LOCK_INIT;
384 struct unpack_trees_options unpack_tree_opts;
385 struct tree *tree;
386 const char *reflog_action;
387 struct strbuf msg = STRBUF_INIT;
388 size_t prefix_len;
389 struct object_id *orig = NULL, oid_orig,
390 *old_orig = NULL, oid_old_orig;
391 int ret = 0, nr = 0;
392
393 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
394 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
395
414f3360 396 if (!refs_only && hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
c5233708
JS
397 ret = -1;
398 goto leave_reset_head;
399 }
400
401 if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
402 ret = error(_("could not determine HEAD revision"));
403 goto leave_reset_head;
404 }
405
406 if (!oid)
407 oid = &head_oid;
408
414f3360
JS
409 if (refs_only)
410 goto reset_head_refs;
411
c5233708
JS
412 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
413 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
414 unpack_tree_opts.head_idx = 1;
415 unpack_tree_opts.src_index = the_repository->index;
416 unpack_tree_opts.dst_index = the_repository->index;
417 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
418 unpack_tree_opts.update = 1;
419 unpack_tree_opts.merge = 1;
420 if (!detach_head)
421 unpack_tree_opts.reset = 1;
422
7589e636 423 if (repo_read_index_unmerged(the_repository) < 0) {
c5233708
JS
424 ret = error(_("could not read index"));
425 goto leave_reset_head;
426 }
427
428 if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
429 ret = error(_("failed to find tree of %s"),
430 oid_to_hex(&head_oid));
431 goto leave_reset_head;
432 }
433
434 if (!fill_tree_descriptor(&desc[nr++], oid)) {
435 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
436 goto leave_reset_head;
437 }
438
439 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
440 ret = -1;
441 goto leave_reset_head;
442 }
443
444 tree = parse_tree_indirect(oid);
e52c6bbd 445 prime_cache_tree(the_repository, the_repository->index, tree);
c5233708
JS
446
447 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
448 ret = error(_("could not write index"));
449 goto leave_reset_head;
450 }
451
414f3360 452reset_head_refs:
c5233708
JS
453 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
454 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
455 prefix_len = msg.len;
456
457 if (!get_oid("ORIG_HEAD", &oid_old_orig))
458 old_orig = &oid_old_orig;
459 if (!get_oid("HEAD", &oid_orig)) {
460 orig = &oid_orig;
461 if (!reflog_orig_head) {
462 strbuf_addstr(&msg, "updating ORIG_HEAD");
463 reflog_orig_head = msg.buf;
464 }
465 update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
466 UPDATE_REFS_MSG_ON_ERR);
467 } else if (old_orig)
468 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
469 if (!reflog_head) {
470 strbuf_setlen(&msg, prefix_len);
471 strbuf_addstr(&msg, "updating HEAD");
472 reflog_head = msg.buf;
473 }
474 if (!switch_to_branch)
475 ret = update_ref(reflog_head, "HEAD", oid, orig,
476 detach_head ? REF_NO_DEREF : 0,
477 UPDATE_REFS_MSG_ON_ERR);
478 else {
5b2237a8
JS
479 ret = update_ref(reflog_orig_head, switch_to_branch, oid,
480 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
c5233708 481 if (!ret)
5b2237a8
JS
482 ret = create_symref("HEAD", switch_to_branch,
483 reflog_head);
c5233708 484 }
e52c6bbd
JH
485 if (run_hook)
486 run_hook_le(NULL, "post-checkout",
487 oid_to_hex(orig ? orig : &null_oid),
488 oid_to_hex(oid), "1", NULL);
c5233708
JS
489
490leave_reset_head:
491 strbuf_release(&msg);
492 rollback_lock_file(&lock);
493 while (nr)
494 free((void *)desc[--nr].buffer);
495 return ret;
496}
497
21853626
JS
498static int move_to_original_branch(struct rebase_options *opts)
499{
500 struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
501 int ret;
502
503 if (!opts->head_name)
504 return 0; /* nothing to move back to */
505
506 if (!opts->onto)
507 BUG("move_to_original_branch without onto");
508
509 strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s",
510 opts->head_name, oid_to_hex(&opts->onto->object.oid));
511 strbuf_addf(&head_reflog, "rebase finished: returning to %s",
512 opts->head_name);
513 ret = reset_head(NULL, "", opts->head_name, RESET_HEAD_REFS_ONLY,
514 orig_head_reflog.buf, head_reflog.buf);
515
516 strbuf_release(&orig_head_reflog);
517 strbuf_release(&head_reflog);
518 return ret;
519}
520
bc24382c
JS
521static const char *resolvemsg =
522N_("Resolve all conflicts manually, mark them as resolved with\n"
523"\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
524"You can instead skip this commit: run \"git rebase --skip\".\n"
525"To abort and get back to the state before \"git rebase\", run "
526"\"git rebase --abort\".");
527
21853626
JS
528static int run_am(struct rebase_options *opts)
529{
530 struct child_process am = CHILD_PROCESS_INIT;
531 struct child_process format_patch = CHILD_PROCESS_INIT;
532 struct strbuf revisions = STRBUF_INIT;
533 int status;
534 char *rebased_patches;
535
536 am.git_cmd = 1;
537 argv_array_push(&am.args, "am");
538
539 if (opts->action && !strcmp("continue", opts->action)) {
540 argv_array_push(&am.args, "--resolved");
541 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
542 if (opts->gpg_sign_opt)
543 argv_array_push(&am.args, opts->gpg_sign_opt);
544 status = run_command(&am);
545 if (status)
546 return status;
547
548 return move_to_original_branch(opts);
549 }
550 if (opts->action && !strcmp("skip", opts->action)) {
551 argv_array_push(&am.args, "--skip");
552 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
553 status = run_command(&am);
554 if (status)
555 return status;
556
557 return move_to_original_branch(opts);
558 }
559 if (opts->action && !strcmp("show-current-patch", opts->action)) {
560 argv_array_push(&am.args, "--show-current-patch");
561 return run_command(&am);
562 }
563
564 strbuf_addf(&revisions, "%s...%s",
565 oid_to_hex(opts->root ?
566 /* this is now equivalent to !opts->upstream */
567 &opts->onto->object.oid :
568 &opts->upstream->object.oid),
569 oid_to_hex(&opts->orig_head));
570
571 rebased_patches = xstrdup(git_path("rebased-patches"));
572 format_patch.out = open(rebased_patches,
573 O_WRONLY | O_CREAT | O_TRUNC, 0666);
574 if (format_patch.out < 0) {
575 status = error_errno(_("could not open '%s' for writing"),
576 rebased_patches);
577 free(rebased_patches);
578 argv_array_clear(&am.args);
579 return status;
580 }
581
582 format_patch.git_cmd = 1;
583 argv_array_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
584 "--full-index", "--cherry-pick", "--right-only",
585 "--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
e52c6bbd 586 "--no-cover-letter", "--pretty=mboxrd", "--topo-order", NULL);
21853626
JS
587 if (opts->git_format_patch_opt.len)
588 argv_array_split(&format_patch.args,
589 opts->git_format_patch_opt.buf);
590 argv_array_push(&format_patch.args, revisions.buf);
591 if (opts->restrict_revision)
592 argv_array_pushf(&format_patch.args, "^%s",
593 oid_to_hex(&opts->restrict_revision->object.oid));
594
595 status = run_command(&format_patch);
596 if (status) {
597 unlink(rebased_patches);
598 free(rebased_patches);
599 argv_array_clear(&am.args);
600
601 reset_head(&opts->orig_head, "checkout", opts->head_name, 0,
602 "HEAD", NULL);
603 error(_("\ngit encountered an error while preparing the "
604 "patches to replay\n"
605 "these revisions:\n"
606 "\n %s\n\n"
607 "As a result, git cannot rebase them."),
608 opts->revisions);
609
610 strbuf_release(&revisions);
611 return status;
612 }
613 strbuf_release(&revisions);
614
615 am.in = open(rebased_patches, O_RDONLY);
616 if (am.in < 0) {
617 status = error_errno(_("could not open '%s' for reading"),
618 rebased_patches);
619 free(rebased_patches);
620 argv_array_clear(&am.args);
621 return status;
622 }
623
624 argv_array_pushv(&am.args, opts->git_am_opts.argv);
625 argv_array_push(&am.args, "--rebasing");
626 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
627 argv_array_push(&am.args, "--patch-format=mboxrd");
628 if (opts->allow_rerere_autoupdate > 0)
629 argv_array_push(&am.args, "--rerere-autoupdate");
630 else if (opts->allow_rerere_autoupdate == 0)
631 argv_array_push(&am.args, "--no-rerere-autoupdate");
632 if (opts->gpg_sign_opt)
633 argv_array_push(&am.args, opts->gpg_sign_opt);
634 status = run_command(&am);
635 unlink(rebased_patches);
636 free(rebased_patches);
637
638 if (!status) {
639 return move_to_original_branch(opts);
640 }
641
642 if (is_directory(opts->state_dir))
643 write_basic_state(opts);
644
645 return status;
646}
647
ac7f467f
PK
648static int run_specific_rebase(struct rebase_options *opts)
649{
650 const char *argv[] = { NULL, NULL };
f5769680 651 struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT;
ac7f467f
PK
652 int status;
653 const char *backend, *backend_func;
654
bc24382c
JS
655 if (opts->type == REBASE_INTERACTIVE) {
656 /* Run builtin interactive rebase */
657 struct child_process child = CHILD_PROCESS_INIT;
658
659 argv_array_pushf(&child.env_array, "GIT_CHERRY_PICK_HELP=%s",
660 resolvemsg);
661 if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
891d4a03
PW
662 argv_array_push(&child.env_array,
663 "GIT_SEQUENCE_EDITOR=:");
bc24382c
JS
664 opts->autosquash = 0;
665 }
666
667 child.git_cmd = 1;
668 argv_array_push(&child.args, "rebase--interactive");
669
670 if (opts->action)
671 argv_array_pushf(&child.args, "--%s", opts->action);
672 if (opts->keep_empty)
673 argv_array_push(&child.args, "--keep-empty");
674 if (opts->rebase_merges)
675 argv_array_push(&child.args, "--rebase-merges");
676 if (opts->rebase_cousins)
677 argv_array_push(&child.args, "--rebase-cousins");
678 if (opts->autosquash)
679 argv_array_push(&child.args, "--autosquash");
680 if (opts->flags & REBASE_VERBOSE)
681 argv_array_push(&child.args, "--verbose");
682 if (opts->flags & REBASE_FORCE)
683 argv_array_push(&child.args, "--no-ff");
684 if (opts->restrict_revision)
685 argv_array_pushf(&child.args,
686 "--restrict-revision=^%s",
687 oid_to_hex(&opts->restrict_revision->object.oid));
688 if (opts->upstream)
689 argv_array_pushf(&child.args, "--upstream=%s",
690 oid_to_hex(&opts->upstream->object.oid));
691 if (opts->onto)
692 argv_array_pushf(&child.args, "--onto=%s",
693 oid_to_hex(&opts->onto->object.oid));
694 if (opts->squash_onto)
695 argv_array_pushf(&child.args, "--squash-onto=%s",
696 oid_to_hex(opts->squash_onto));
697 if (opts->onto_name)
698 argv_array_pushf(&child.args, "--onto-name=%s",
699 opts->onto_name);
700 argv_array_pushf(&child.args, "--head-name=%s",
701 opts->head_name ?
702 opts->head_name : "detached HEAD");
703 if (opts->strategy)
704 argv_array_pushf(&child.args, "--strategy=%s",
705 opts->strategy);
706 if (opts->strategy_opts)
707 argv_array_pushf(&child.args, "--strategy-opts=%s",
708 opts->strategy_opts);
709 if (opts->switch_to)
710 argv_array_pushf(&child.args, "--switch-to=%s",
711 opts->switch_to);
712 if (opts->cmd)
713 argv_array_pushf(&child.args, "--cmd=%s", opts->cmd);
714 if (opts->allow_empty_message)
715 argv_array_push(&child.args, "--allow-empty-message");
716 if (opts->allow_rerere_autoupdate > 0)
717 argv_array_push(&child.args, "--rerere-autoupdate");
718 else if (opts->allow_rerere_autoupdate == 0)
719 argv_array_push(&child.args, "--no-rerere-autoupdate");
720 if (opts->gpg_sign_opt)
721 argv_array_push(&child.args, opts->gpg_sign_opt);
722 if (opts->signoff)
723 argv_array_push(&child.args, "--signoff");
d421afa0
JS
724 if (opts->reschedule_failed_exec)
725 argv_array_push(&child.args, "--reschedule-failed-exec");
bc24382c
JS
726
727 status = run_command(&child);
728 goto finished_rebase;
729 }
730
21853626
JS
731 if (opts->type == REBASE_AM) {
732 status = run_am(opts);
733 goto finished_rebase;
734 }
735
ac7f467f
PK
736 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
737 add_var(&script_snippet, "state_dir", opts->state_dir);
738
739 add_var(&script_snippet, "upstream_name", opts->upstream_name);
f9573628
PK
740 add_var(&script_snippet, "upstream", opts->upstream ?
741 oid_to_hex(&opts->upstream->object.oid) : NULL);
d4c569f8
PK
742 add_var(&script_snippet, "head_name",
743 opts->head_name ? opts->head_name : "detached HEAD");
ac7f467f 744 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
f9573628
PK
745 add_var(&script_snippet, "onto", opts->onto ?
746 oid_to_hex(&opts->onto->object.oid) : NULL);
ac7f467f
PK
747 add_var(&script_snippet, "onto_name", opts->onto_name);
748 add_var(&script_snippet, "revisions", opts->revisions);
749 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
750 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
b4c8eb02
PK
751 add_var(&script_snippet, "GIT_QUIET",
752 opts->flags & REBASE_NO_QUIET ? "" : "t");
f5769680
JS
753 sq_quote_argv_pretty(&buf, opts->git_am_opts.argv);
754 add_var(&script_snippet, "git_am_opt", buf.buf);
755 strbuf_release(&buf);
bff014da
PK
756 add_var(&script_snippet, "verbose",
757 opts->flags & REBASE_VERBOSE ? "t" : "");
758 add_var(&script_snippet, "diffstat",
759 opts->flags & REBASE_DIFFSTAT ? "t" : "");
1ed9c14f
PK
760 add_var(&script_snippet, "force_rebase",
761 opts->flags & REBASE_FORCE ? "t" : "");
e65123a7
PK
762 if (opts->switch_to)
763 add_var(&script_snippet, "switch_to", opts->switch_to);
f9573628 764 add_var(&script_snippet, "action", opts->action ? opts->action : "");
73d51ed0 765 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
ead98c11
PK
766 add_var(&script_snippet, "allow_rerere_autoupdate",
767 opts->allow_rerere_autoupdate < 0 ? "" :
768 opts->allow_rerere_autoupdate ?
769 "--rerere-autoupdate" : "--no-rerere-autoupdate");
002ee2fe 770 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
051910a9 771 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
12026a41 772 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
68e46d78 773 add_var(&script_snippet, "cmd", opts->cmd);
9b3a448b
PK
774 add_var(&script_snippet, "allow_empty_message",
775 opts->allow_empty_message ? "--allow-empty-message" : "");
3c3588c7
PK
776 add_var(&script_snippet, "rebase_merges",
777 opts->rebase_merges ? "t" : "");
778 add_var(&script_snippet, "rebase_cousins",
779 opts->rebase_cousins ? "t" : "");
ba1905a5
PK
780 add_var(&script_snippet, "strategy", opts->strategy);
781 add_var(&script_snippet, "strategy_opts", opts->strategy_opts);
9dba809a
PK
782 add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
783 add_var(&script_snippet, "squash_onto",
784 opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
cda614e4
PK
785 add_var(&script_snippet, "git_format_patch_opt",
786 opts->git_format_patch_opt.buf);
ac7f467f 787
3dba9d08
PK
788 if (is_interactive(opts) &&
789 !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
790 strbuf_addstr(&script_snippet,
891d4a03 791 "GIT_SEQUENCE_EDITOR=:; export GIT_SEQUENCE_EDITOR; ");
3dba9d08
PK
792 opts->autosquash = 0;
793 }
ac7f467f
PK
794
795 switch (opts->type) {
796 case REBASE_AM:
797 backend = "git-rebase--am";
798 backend_func = "git_rebase__am";
799 break;
ac7f467f
PK
800 case REBASE_PRESERVE_MERGES:
801 backend = "git-rebase--preserve-merges";
802 backend_func = "git_rebase__preserve_merges";
803 break;
804 default:
805 BUG("Unhandled rebase type %d", opts->type);
806 break;
807 }
808
809 strbuf_addf(&script_snippet,
810 ". git-sh-setup && . git-rebase--common &&"
811 " . %s && %s", backend, backend_func);
812 argv[0] = script_snippet.buf;
813
814 status = run_command_v_opt(argv, RUN_USING_SHELL);
bc24382c 815finished_rebase:
ac7f467f
PK
816 if (opts->dont_finish_rebase)
817 ; /* do nothing */
bc24382c
JS
818 else if (opts->type == REBASE_INTERACTIVE)
819 ; /* interactive rebase cleans up after itself */
ac7f467f
PK
820 else if (status == 0) {
821 if (!file_exists(state_dir_path("stopped-sha", opts)))
822 finish_rebase(opts);
823 } else if (status == 2) {
824 struct strbuf dir = STRBUF_INIT;
825
6defce2b 826 apply_autostash(opts);
ac7f467f
PK
827 strbuf_addstr(&dir, opts->state_dir);
828 remove_dir_recursively(&dir, 0);
829 strbuf_release(&dir);
830 die("Nothing to do");
831 }
832
833 strbuf_release(&script_snippet);
834
835 return status ? -1 : 0;
836}
837
bff014da
PK
838static int rebase_config(const char *var, const char *value, void *data)
839{
840 struct rebase_options *opts = data;
841
842 if (!strcmp(var, "rebase.stat")) {
843 if (git_config_bool(var, value))
844 opts->flags |= REBASE_DIFFSTAT;
845 else
846 opts->flags &= !REBASE_DIFFSTAT;
847 return 0;
848 }
849
051910a9
PK
850 if (!strcmp(var, "rebase.autosquash")) {
851 opts->autosquash = git_config_bool(var, value);
852 return 0;
853 }
854
12026a41
PK
855 if (!strcmp(var, "commit.gpgsign")) {
856 free(opts->gpg_sign_opt);
857 opts->gpg_sign_opt = git_config_bool(var, value) ?
858 xstrdup("-S") : NULL;
859 return 0;
860 }
861
6defce2b
PK
862 if (!strcmp(var, "rebase.autostash")) {
863 opts->autostash = git_config_bool(var, value);
864 return 0;
865 }
866
969de3ff
JS
867 if (!strcmp(var, "rebase.reschedulefailedexec")) {
868 opts->reschedule_failed_exec = git_config_bool(var, value);
869 return 0;
870 }
871
bff014da
PK
872 return git_default_config(var, value, data);
873}
874
9a48a615
PK
875/*
876 * Determines whether the commits in from..to are linear, i.e. contain
877 * no merge commits. This function *expects* `from` to be an ancestor of
878 * `to`.
879 */
880static int is_linear_history(struct commit *from, struct commit *to)
881{
882 while (to && to != from) {
883 parse_commit(to);
884 if (!to->parents)
885 return 1;
886 if (to->parents->next)
887 return 0;
888 to = to->parents->item;
889 }
890 return 1;
891}
892
893static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
894 struct object_id *merge_base)
895{
896 struct commit *head = lookup_commit(the_repository, head_oid);
897 struct commit_list *merge_bases;
898 int res;
899
900 if (!head)
901 return 0;
902
903 merge_bases = get_merge_bases(onto, head);
904 if (merge_bases && !merge_bases->next) {
905 oidcpy(merge_base, &merge_bases->item->object.oid);
b17ca8f9 906 res = oideq(merge_base, &onto->object.oid);
9a48a615
PK
907 } else {
908 oidcpy(merge_base, &null_oid);
909 res = 0;
910 }
911 free_commit_list(merge_bases);
912 return res && is_linear_history(onto, head);
913}
914
361badd3
PK
915/* -i followed by -m is still -i */
916static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
917{
918 struct rebase_options *opts = opt->value;
919
517fe807
JK
920 BUG_ON_OPT_NEG(unset);
921 BUG_ON_OPT_ARG(arg);
922
361badd3
PK
923 if (!is_interactive(opts))
924 opts->type = REBASE_MERGE;
925
926 return 0;
927}
928
929/* -i followed by -p is still explicitly interactive, but -p alone is not */
930static int parse_opt_interactive(const struct option *opt, const char *arg,
931 int unset)
932{
933 struct rebase_options *opts = opt->value;
934
517fe807
JK
935 BUG_ON_OPT_NEG(unset);
936 BUG_ON_OPT_ARG(arg);
937
361badd3
PK
938 opts->type = REBASE_INTERACTIVE;
939 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
940
941 return 0;
942}
943
8f5986d9
PK
944static void NORETURN error_on_missing_default_upstream(void)
945{
946 struct branch *current_branch = branch_get(NULL);
947
948 printf(_("%s\n"
949 "Please specify which branch you want to rebase against.\n"
950 "See git-rebase(1) for details.\n"
951 "\n"
952 " git rebase '<branch>'\n"
953 "\n"),
954 current_branch ? _("There is no tracking information for "
955 "the current branch.") :
956 _("You are not currently on a branch."));
957
958 if (current_branch) {
959 const char *remote = current_branch->remote_name;
960
961 if (!remote)
962 remote = _("<remote>");
963
964 printf(_("If you wish to set tracking information for this "
965 "branch you can do so with:\n"
966 "\n"
967 " git branch --set-upstream-to=%s/<branch> %s\n"
968 "\n"),
969 remote, current_branch->name);
970 }
971 exit(1);
972}
973
13a5a9f0
JS
974static void set_reflog_action(struct rebase_options *options)
975{
976 const char *env;
977 struct strbuf buf = STRBUF_INIT;
978
979 if (!is_interactive(options))
980 return;
981
982 env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
983 if (env && strcmp("rebase", env))
984 return; /* only override it if it is "rebase" */
985
986 strbuf_addf(&buf, "rebase -i (%s)", options->action);
987 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1);
988 strbuf_release(&buf);
989}
990
c762aada
PW
991static int check_exec_cmd(const char *cmd)
992{
993 if (strchr(cmd, '\n'))
994 return error(_("exec commands cannot contain newlines"));
995
996 /* Does the command consist purely of whitespace? */
997 if (!cmd[strspn(cmd, " \t\r\f\v")])
998 return error(_("empty exec command"));
999
1000 return 0;
1001}
1002
1003
55071ea2
PK
1004int cmd_rebase(int argc, const char **argv, const char *prefix)
1005{
ac7f467f
PK
1006 struct rebase_options options = {
1007 .type = REBASE_UNSPECIFIED,
b4c8eb02 1008 .flags = REBASE_NO_QUIET,
f5769680 1009 .git_am_opts = ARGV_ARRAY_INIT,
ead98c11 1010 .allow_rerere_autoupdate = -1,
9b3a448b 1011 .allow_empty_message = 1,
cda614e4 1012 .git_format_patch_opt = STRBUF_INIT,
ac7f467f
PK
1013 };
1014 const char *branch_name;
f9573628 1015 int ret, flags, total_argc, in_progress = 0;
06e4775a 1016 int ok_to_skip_pre_rebase = 0;
ac7f467f
PK
1017 struct strbuf msg = STRBUF_INIT;
1018 struct strbuf revisions = STRBUF_INIT;
c54dacb5 1019 struct strbuf buf = STRBUF_INIT;
075bc852 1020 struct object_id merge_base;
f9573628
PK
1021 enum {
1022 NO_ACTION,
1023 ACTION_CONTINUE,
122420c2 1024 ACTION_SKIP,
5e5d9619 1025 ACTION_ABORT,
5a614945 1026 ACTION_QUIT,
51e9ea6d
PK
1027 ACTION_EDIT_TODO,
1028 ACTION_SHOW_CURRENT_PATCH,
f9573628 1029 } action = NO_ACTION;
12026a41 1030 const char *gpg_sign = NULL;
68e46d78 1031 struct string_list exec = STRING_LIST_INIT_NODUP;
3c3588c7 1032 const char *rebase_merges = NULL;
92d0d74e 1033 int fork_point = -1;
ba1905a5 1034 struct string_list strategy_options = STRING_LIST_INIT_NODUP;
9dba809a
PK
1035 struct object_id squash_onto;
1036 char *squash_onto_name = NULL;
f28d40d3
PK
1037 struct option builtin_rebase_options[] = {
1038 OPT_STRING(0, "onto", &options.onto_name,
1039 N_("revision"),
1040 N_("rebase onto given branch instead of upstream")),
06e4775a
PK
1041 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
1042 N_("allow pre-rebase hook to run")),
b4c8eb02
PK
1043 OPT_NEGBIT('q', "quiet", &options.flags,
1044 N_("be quiet. implies --no-stat"),
bff014da
PK
1045 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
1046 OPT_BIT('v', "verbose", &options.flags,
1047 N_("display a diffstat of what changed upstream"),
1048 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1049 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
1050 N_("do not show diffstat of what changed upstream"),
1051 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
73d51ed0
PK
1052 OPT_BOOL(0, "signoff", &options.signoff,
1053 N_("add a Signed-off-by: line to each commit")),
f5769680
JS
1054 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options.git_am_opts,
1055 NULL, N_("passed to 'git am'"),
1056 PARSE_OPT_NOARG),
1057 OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
1058 &options.git_am_opts, NULL,
1059 N_("passed to 'git am'"), PARSE_OPT_NOARG),
1060 OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL,
1061 N_("passed to 'git am'"), PARSE_OPT_NOARG),
1062 OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
1063 N_("passed to 'git apply'"), 0),
1064 OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
1065 N_("action"), N_("passed to 'git apply'"), 0),
1ed9c14f
PK
1066 OPT_BIT('f', "force-rebase", &options.flags,
1067 N_("cherry-pick all commits, even if unchanged"),
1068 REBASE_FORCE),
1069 OPT_BIT(0, "no-ff", &options.flags,
1070 N_("cherry-pick all commits, even if unchanged"),
1071 REBASE_FORCE),
f9573628
PK
1072 OPT_CMDMODE(0, "continue", &action, N_("continue"),
1073 ACTION_CONTINUE),
122420c2
PK
1074 OPT_CMDMODE(0, "skip", &action,
1075 N_("skip current patch and continue"), ACTION_SKIP),
5e5d9619
PK
1076 OPT_CMDMODE(0, "abort", &action,
1077 N_("abort and check out the original branch"),
1078 ACTION_ABORT),
5a614945
PK
1079 OPT_CMDMODE(0, "quit", &action,
1080 N_("abort but keep HEAD where it is"), ACTION_QUIT),
51e9ea6d
PK
1081 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
1082 "during an interactive rebase"), ACTION_EDIT_TODO),
1083 OPT_CMDMODE(0, "show-current-patch", &action,
1084 N_("show the patch file being applied or merged"),
1085 ACTION_SHOW_CURRENT_PATCH),
361badd3
PK
1086 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
1087 N_("use merging strategies to rebase"),
1088 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1089 parse_opt_merge },
1090 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
1091 N_("let the user edit the list of commits to rebase"),
1092 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1093 parse_opt_interactive },
1094 OPT_SET_INT('p', "preserve-merges", &options.type,
1095 N_("try to recreate merges instead of ignoring "
1096 "them"), REBASE_PRESERVE_MERGES),
ead98c11
PK
1097 OPT_BOOL(0, "rerere-autoupdate",
1098 &options.allow_rerere_autoupdate,
eff199a6 1099 N_("allow rerere to update index with resolved "
ead98c11 1100 "conflict")),
002ee2fe
PK
1101 OPT_BOOL('k', "keep-empty", &options.keep_empty,
1102 N_("preserve empty commits during rebase")),
051910a9
PK
1103 OPT_BOOL(0, "autosquash", &options.autosquash,
1104 N_("move commits that begin with "
1105 "squash!/fixup! under -i")),
12026a41
PK
1106 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
1107 N_("GPG-sign commits"),
1108 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
6defce2b
PK
1109 OPT_BOOL(0, "autostash", &options.autostash,
1110 N_("automatically stash/stash pop before and after")),
68e46d78
PK
1111 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
1112 N_("add exec lines after each commit of the "
1113 "editable list")),
9b3a448b
PK
1114 OPT_BOOL(0, "allow-empty-message",
1115 &options.allow_empty_message,
1116 N_("allow rebasing commits with empty messages")),
3c3588c7
PK
1117 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
1118 N_("mode"),
1119 N_("try to rebase merges instead of skipping them"),
1120 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
92d0d74e
PK
1121 OPT_BOOL(0, "fork-point", &fork_point,
1122 N_("use 'merge-base --fork-point' to refine upstream")),
ba1905a5
PK
1123 OPT_STRING('s', "strategy", &options.strategy,
1124 N_("strategy"), N_("use the given merge strategy")),
1125 OPT_STRING_LIST('X', "strategy-option", &strategy_options,
1126 N_("option"),
1127 N_("pass the argument through to the merge "
1128 "strategy")),
9dba809a
PK
1129 OPT_BOOL(0, "root", &options.root,
1130 N_("rebase all reachable commits up to the root(s)")),
d421afa0
JS
1131 OPT_BOOL(0, "reschedule-failed-exec",
1132 &options.reschedule_failed_exec,
1133 N_("automatically re-schedule any `exec` that fails")),
f28d40d3
PK
1134 OPT_END(),
1135 };
f5769680 1136 int i;
ac7f467f 1137
55071ea2
PK
1138 /*
1139 * NEEDSWORK: Once the builtin rebase has been tested enough
1140 * and git-legacy-rebase.sh is retired to contrib/, this preamble
1141 * can be removed.
1142 */
1143
1144 if (!use_builtin_rebase()) {
1145 const char *path = mkpath("%s/git-legacy-rebase",
1146 git_exec_path());
1147
1148 if (sane_execvp(path, (char **)argv) < 0)
1149 die_errno(_("could not exec %s"), path);
1150 else
1151 BUG("sane_execvp() returned???");
1152 }
1153
f28d40d3
PK
1154 if (argc == 2 && !strcmp(argv[1], "-h"))
1155 usage_with_options(builtin_rebase_usage,
1156 builtin_rebase_options);
1157
55071ea2
PK
1158 prefix = setup_git_directory();
1159 trace_repo_setup(prefix);
1160 setup_work_tree();
1161
bff014da
PK
1162 git_config(rebase_config, &options);
1163
0eabf4b9
PK
1164 strbuf_reset(&buf);
1165 strbuf_addf(&buf, "%s/applying", apply_dir());
1166 if(file_exists(buf.buf))
1167 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1168
c54dacb5
PK
1169 if (is_directory(apply_dir())) {
1170 options.type = REBASE_AM;
1171 options.state_dir = apply_dir();
1172 } else if (is_directory(merge_dir())) {
1173 strbuf_reset(&buf);
1174 strbuf_addf(&buf, "%s/rewritten", merge_dir());
1175 if (is_directory(buf.buf)) {
1176 options.type = REBASE_PRESERVE_MERGES;
1177 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1178 } else {
1179 strbuf_reset(&buf);
1180 strbuf_addf(&buf, "%s/interactive", merge_dir());
1181 if(file_exists(buf.buf)) {
1182 options.type = REBASE_INTERACTIVE;
1183 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1184 } else
1185 options.type = REBASE_MERGE;
1186 }
1187 options.state_dir = merge_dir();
1188 }
1189
1190 if (options.type != REBASE_UNSPECIFIED)
1191 in_progress = 1;
1192
f9573628 1193 total_argc = argc;
f28d40d3
PK
1194 argc = parse_options(argc, argv, prefix,
1195 builtin_rebase_options,
1196 builtin_rebase_usage, 0);
1197
f9573628
PK
1198 if (action != NO_ACTION && total_argc != 2) {
1199 usage_with_options(builtin_rebase_usage,
1200 builtin_rebase_options);
1201 }
1202
f28d40d3
PK
1203 if (argc > 2)
1204 usage_with_options(builtin_rebase_usage,
1205 builtin_rebase_options);
ac7f467f 1206
d732a570
PK
1207 if (action != NO_ACTION && !in_progress)
1208 die(_("No rebase in progress?"));
13a5a9f0 1209 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0);
d732a570 1210
51e9ea6d
PK
1211 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
1212 die(_("The --edit-todo action can only be used during "
1213 "interactive rebase."));
1214
f9573628
PK
1215 switch (action) {
1216 case ACTION_CONTINUE: {
1217 struct object_id head;
1218 struct lock_file lock_file = LOCK_INIT;
1219 int fd;
1220
1221 options.action = "continue";
13a5a9f0 1222 set_reflog_action(&options);
f9573628
PK
1223
1224 /* Sanity check */
1225 if (get_oid("HEAD", &head))
1226 die(_("Cannot read HEAD"));
1227
1228 fd = hold_locked_index(&lock_file, 0);
e1ff0a32 1229 if (repo_read_index(the_repository) < 0)
f9573628
PK
1230 die(_("could not read index"));
1231 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1232 NULL);
1233 if (0 <= fd)
1b0d968b 1234 repo_update_index_if_able(the_repository, &lock_file);
f9573628
PK
1235 rollback_lock_file(&lock_file);
1236
5b02ca38 1237 if (has_unstaged_changes(the_repository, 1)) {
f9573628
PK
1238 puts(_("You must edit all merge conflicts and then\n"
1239 "mark them as resolved using git add"));
1240 exit(1);
1241 }
1242 if (read_basic_state(&options))
1243 exit(1);
1244 goto run_rebase;
1245 }
122420c2
PK
1246 case ACTION_SKIP: {
1247 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1248
1249 options.action = "skip";
13a5a9f0 1250 set_reflog_action(&options);
122420c2 1251
55e6b354 1252 rerere_clear(the_repository, &merge_rr);
122420c2
PK
1253 string_list_clear(&merge_rr, 1);
1254
bac2a1e3
JS
1255 if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1256 NULL, NULL) < 0)
122420c2 1257 die(_("could not discard worktree changes"));
cde55548 1258 remove_branch_state(the_repository);
122420c2
PK
1259 if (read_basic_state(&options))
1260 exit(1);
1261 goto run_rebase;
1262 }
5e5d9619
PK
1263 case ACTION_ABORT: {
1264 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1265 options.action = "abort";
13a5a9f0 1266 set_reflog_action(&options);
5e5d9619 1267
55e6b354 1268 rerere_clear(the_repository, &merge_rr);
5e5d9619
PK
1269 string_list_clear(&merge_rr, 1);
1270
1271 if (read_basic_state(&options))
1272 exit(1);
1273 if (reset_head(&options.orig_head, "reset",
bac2a1e3
JS
1274 options.head_name, RESET_HEAD_HARD,
1275 NULL, NULL) < 0)
5e5d9619
PK
1276 die(_("could not move back to %s"),
1277 oid_to_hex(&options.orig_head));
cde55548 1278 remove_branch_state(the_repository);
5e5d9619
PK
1279 ret = finish_rebase(&options);
1280 goto cleanup;
1281 }
5a614945
PK
1282 case ACTION_QUIT: {
1283 strbuf_reset(&buf);
1284 strbuf_addstr(&buf, options.state_dir);
1285 ret = !!remove_dir_recursively(&buf, 0);
1286 if (ret)
1287 die(_("could not remove '%s'"), options.state_dir);
1288 goto cleanup;
1289 }
51e9ea6d
PK
1290 case ACTION_EDIT_TODO:
1291 options.action = "edit-todo";
1292 options.dont_finish_rebase = 1;
1293 goto run_rebase;
1294 case ACTION_SHOW_CURRENT_PATCH:
1295 options.action = "show-current-patch";
1296 options.dont_finish_rebase = 1;
1297 goto run_rebase;
1298 case NO_ACTION:
1299 break;
f9573628 1300 default:
51e9ea6d 1301 BUG("action: %d", action);
f9573628
PK
1302 }
1303
c54dacb5
PK
1304 /* Make sure no rebase is in progress */
1305 if (in_progress) {
1306 const char *last_slash = strrchr(options.state_dir, '/');
1307 const char *state_dir_base =
1308 last_slash ? last_slash + 1 : options.state_dir;
1309 const char *cmd_live_rebase =
1310 "git rebase (--continue | --abort | --skip)";
1311 strbuf_reset(&buf);
1312 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1313 die(_("It seems that there is already a %s directory, and\n"
1314 "I wonder if you are in the middle of another rebase. "
1315 "If that is the\n"
1316 "case, please try\n\t%s\n"
1317 "If that is not the case, please\n\t%s\n"
1318 "and run me again. I am stopping in case you still "
1319 "have something\n"
1320 "valuable there.\n"),
1321 state_dir_base, cmd_live_rebase, buf.buf);
1322 }
1323
f5769680 1324 for (i = 0; i < options.git_am_opts.argc; i++) {
04519d72 1325 const char *option = options.git_am_opts.argv[i], *p;
f5769680
JS
1326 if (!strcmp(option, "--committer-date-is-author-date") ||
1327 !strcmp(option, "--ignore-date") ||
1328 !strcmp(option, "--whitespace=fix") ||
1329 !strcmp(option, "--whitespace=strip"))
1330 options.flags |= REBASE_FORCE;
04519d72
JS
1331 else if (skip_prefix(option, "-C", &p)) {
1332 while (*p)
1333 if (!isdigit(*(p++)))
1334 die(_("switch `C' expects a "
1335 "numerical value"));
1336 } else if (skip_prefix(option, "--whitespace=", &p)) {
1337 if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1338 strcmp(p, "error") && strcmp(p, "error-all"))
1339 die("Invalid whitespace option: '%s'", p);
1340 }
38dbcef2
PK
1341 }
1342
c762aada
PW
1343 for (i = 0; i < exec.nr; i++)
1344 if (check_exec_cmd(exec.items[i].string))
1345 exit(1);
1346
f5769680
JS
1347 if (!(options.flags & REBASE_NO_QUIET))
1348 argv_array_push(&options.git_am_opts, "-q");
53f9e5be 1349
002ee2fe
PK
1350 if (options.keep_empty)
1351 imply_interactive(&options, "--keep-empty");
1352
12026a41
PK
1353 if (gpg_sign) {
1354 free(options.gpg_sign_opt);
1355 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1356 }
1357
68e46d78
PK
1358 if (exec.nr) {
1359 int i;
1360
1361 imply_interactive(&options, "--exec");
1362
1363 strbuf_reset(&buf);
1364 for (i = 0; i < exec.nr; i++)
1365 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1366 options.cmd = xstrdup(buf.buf);
1367 }
1368
3c3588c7
PK
1369 if (rebase_merges) {
1370 if (!*rebase_merges)
1371 ; /* default mode; do nothing */
1372 else if (!strcmp("rebase-cousins", rebase_merges))
1373 options.rebase_cousins = 1;
1374 else if (strcmp("no-rebase-cousins", rebase_merges))
1375 die(_("Unknown mode: %s"), rebase_merges);
1376 options.rebase_merges = 1;
1377 imply_interactive(&options, "--rebase-merges");
1378 }
1379
ba1905a5
PK
1380 if (strategy_options.nr) {
1381 int i;
1382
1383 if (!options.strategy)
1384 options.strategy = "recursive";
1385
1386 strbuf_reset(&buf);
1387 for (i = 0; i < strategy_options.nr; i++)
1388 strbuf_addf(&buf, " --%s",
1389 strategy_options.items[i].string);
1390 options.strategy_opts = xstrdup(buf.buf);
1391 }
1392
1393 if (options.strategy) {
1394 options.strategy = xstrdup(options.strategy);
1395 switch (options.type) {
1396 case REBASE_AM:
1397 die(_("--strategy requires --merge or --interactive"));
1398 case REBASE_MERGE:
1399 case REBASE_INTERACTIVE:
1400 case REBASE_PRESERVE_MERGES:
1401 /* compatible */
1402 break;
1403 case REBASE_UNSPECIFIED:
1404 options.type = REBASE_MERGE;
1405 break;
1406 default:
1407 BUG("unhandled rebase type (%d)", options.type);
1408 }
1409 }
1410
68aa495b
EN
1411 if (options.type == REBASE_MERGE)
1412 imply_interactive(&options, "--merge");
1413
9dba809a
PK
1414 if (options.root && !options.onto_name)
1415 imply_interactive(&options, "--root without --onto");
1416
cda614e4
PK
1417 if (isatty(2) && options.flags & REBASE_NO_QUIET)
1418 strbuf_addstr(&options.git_format_patch_opt, " --progress");
1419
ac7f467f
PK
1420 switch (options.type) {
1421 case REBASE_MERGE:
1422 case REBASE_INTERACTIVE:
1423 case REBASE_PRESERVE_MERGES:
1424 options.state_dir = merge_dir();
1425 break;
1426 case REBASE_AM:
1427 options.state_dir = apply_dir();
1428 break;
1429 default:
1430 /* the default rebase backend is `--am` */
1431 options.type = REBASE_AM;
1432 options.state_dir = apply_dir();
1433 break;
1434 }
1435
d421afa0 1436 if (options.reschedule_failed_exec && !is_interactive(&options))
32ceace3 1437 die(_("%s requires an interactive rebase"), "--reschedule-failed-exec");
d421afa0 1438
f5769680 1439 if (options.git_am_opts.argc) {
b361bd75 1440 /* all am options except -q are compatible only with --am */
f5769680
JS
1441 for (i = options.git_am_opts.argc - 1; i >= 0; i--)
1442 if (strcmp(options.git_am_opts.argv[i], "-q"))
1443 break;
b361bd75 1444
f5769680 1445 if (is_interactive(&options) && i >= 0)
68aa495b
EN
1446 die(_("cannot combine am options with either "
1447 "interactive or merge options"));
b361bd75
PK
1448 }
1449
73d51ed0
PK
1450 if (options.signoff) {
1451 if (options.type == REBASE_PRESERVE_MERGES)
1452 die("cannot combine '--signoff' with "
1453 "'--preserve-merges'");
f5769680 1454 argv_array_push(&options.git_am_opts, "--signoff");
73d51ed0
PK
1455 options.flags |= REBASE_FORCE;
1456 }
1457
d421afa0 1458 if (options.type == REBASE_PRESERVE_MERGES) {
b361bd75
PK
1459 /*
1460 * Note: incompatibility with --signoff handled in signoff block above
1461 * Note: incompatibility with --interactive is just a strong warning;
1462 * git-rebase.txt caveats with "unless you know what you are doing"
1463 */
1464 if (options.rebase_merges)
c913c596 1465 die(_("cannot combine '--preserve-merges' with "
b361bd75
PK
1466 "'--rebase-merges'"));
1467
d421afa0
JS
1468 if (options.reschedule_failed_exec)
1469 die(_("error: cannot combine '--preserve-merges' with "
1470 "'--reschedule-failed-exec'"));
1471 }
1472
b361bd75
PK
1473 if (options.rebase_merges) {
1474 if (strategy_options.nr)
c913c596 1475 die(_("cannot combine '--rebase-merges' with "
b361bd75
PK
1476 "'--strategy-option'"));
1477 if (options.strategy)
c913c596 1478 die(_("cannot combine '--rebase-merges' with "
b361bd75
PK
1479 "'--strategy'"));
1480 }
1481
ac7f467f 1482 if (!options.root) {
8f5986d9
PK
1483 if (argc < 1) {
1484 struct branch *branch;
1485
1486 branch = branch_get(NULL);
1487 options.upstream_name = branch_get_upstream(branch,
1488 NULL);
1489 if (!options.upstream_name)
1490 error_on_missing_default_upstream();
1491 if (fork_point < 0)
1492 fork_point = 1;
1493 } else {
f28d40d3 1494 options.upstream_name = argv[0];
ac7f467f
PK
1495 argc--;
1496 argv++;
1497 if (!strcmp(options.upstream_name, "-"))
1498 options.upstream_name = "@{-1}";
1499 }
1500 options.upstream = peel_committish(options.upstream_name);
1501 if (!options.upstream)
1502 die(_("invalid upstream '%s'"), options.upstream_name);
06e4775a 1503 options.upstream_arg = options.upstream_name;
9dba809a
PK
1504 } else {
1505 if (!options.onto_name) {
1506 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1507 &squash_onto, NULL, NULL) < 0)
1508 die(_("Could not create new root commit"));
1509 options.squash_onto = &squash_onto;
1510 options.onto_name = squash_onto_name =
1511 xstrdup(oid_to_hex(&squash_onto));
1512 }
1513 options.upstream_name = NULL;
1514 options.upstream = NULL;
1515 if (argc > 1)
1516 usage_with_options(builtin_rebase_usage,
1517 builtin_rebase_options);
1518 options.upstream_arg = "--root";
1519 }
ac7f467f
PK
1520
1521 /* Make sure the branch to rebase onto is valid. */
1522 if (!options.onto_name)
1523 options.onto_name = options.upstream_name;
1524 if (strstr(options.onto_name, "...")) {
075bc852
PK
1525 if (get_oid_mb(options.onto_name, &merge_base) < 0)
1526 die(_("'%s': need exactly one merge base"),
1527 options.onto_name);
1528 options.onto = lookup_commit_or_die(&merge_base,
1529 options.onto_name);
ac7f467f
PK
1530 } else {
1531 options.onto = peel_committish(options.onto_name);
1532 if (!options.onto)
1533 die(_("Does not point to a valid commit '%s'"),
1534 options.onto_name);
1535 }
1536
1537 /*
1538 * If the branch to rebase is given, that is the branch we will rebase
1539 * branch_name -- branch/commit being rebased, or
1540 * HEAD (already detached)
1541 * orig_head -- commit object name of tip of the branch before rebasing
d4c569f8 1542 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
ac7f467f 1543 */
e65123a7
PK
1544 if (argc == 1) {
1545 /* Is it "rebase other branchname" or "rebase other commit"? */
1546 branch_name = argv[0];
1547 options.switch_to = argv[0];
1548
1549 /* Is it a local branch? */
1550 strbuf_reset(&buf);
1551 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1552 if (!read_ref(buf.buf, &options.orig_head))
1553 options.head_name = xstrdup(buf.buf);
1554 /* If not is it a valid ref (branch or commit)? */
1555 else if (!get_oid(branch_name, &options.orig_head))
1556 options.head_name = NULL;
1557 else
1558 die(_("fatal: no such branch/commit '%s'"),
1559 branch_name);
1560 } else if (argc == 0) {
ac7f467f
PK
1561 /* Do not need to switch branches, we are already on it. */
1562 options.head_name =
1563 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1564 &flags));
1565 if (!options.head_name)
1566 die(_("No such ref: %s"), "HEAD");
1567 if (flags & REF_ISSYMREF) {
1568 if (!skip_prefix(options.head_name,
1569 "refs/heads/", &branch_name))
1570 branch_name = options.head_name;
1571
1572 } else {
d4c569f8
PK
1573 free(options.head_name);
1574 options.head_name = NULL;
ac7f467f
PK
1575 branch_name = "HEAD";
1576 }
1577 if (get_oid("HEAD", &options.orig_head))
1578 die(_("Could not resolve HEAD to a revision"));
e65123a7
PK
1579 } else
1580 BUG("unexpected number of arguments left to parse");
ac7f467f 1581
92d0d74e
PK
1582 if (fork_point > 0) {
1583 struct commit *head =
1584 lookup_commit_reference(the_repository,
1585 &options.orig_head);
1586 options.restrict_revision =
1587 get_fork_point(options.upstream_name, head);
1588 }
1589
e1ff0a32 1590 if (repo_read_index(the_repository) < 0)
e0333e5c
PK
1591 die(_("could not read index"));
1592
6defce2b
PK
1593 if (options.autostash) {
1594 struct lock_file lock_file = LOCK_INIT;
1595 int fd;
1596
1597 fd = hold_locked_index(&lock_file, 0);
1598 refresh_cache(REFRESH_QUIET);
1599 if (0 <= fd)
1b0d968b 1600 repo_update_index_if_able(the_repository, &lock_file);
6defce2b
PK
1601 rollback_lock_file(&lock_file);
1602
5b02ca38
NTND
1603 if (has_unstaged_changes(the_repository, 1) ||
1604 has_uncommitted_changes(the_repository, 1)) {
6defce2b
PK
1605 const char *autostash =
1606 state_dir_path("autostash", &options);
1607 struct child_process stash = CHILD_PROCESS_INIT;
1608 struct object_id oid;
1609 struct commit *head =
1610 lookup_commit_reference(the_repository,
1611 &options.orig_head);
1612
1613 argv_array_pushl(&stash.args,
1614 "stash", "create", "autostash", NULL);
1615 stash.git_cmd = 1;
1616 stash.no_stdin = 1;
1617 strbuf_reset(&buf);
1618 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1619 die(_("Cannot autostash"));
1620 strbuf_trim_trailing_newline(&buf);
1621 if (get_oid(buf.buf, &oid))
1622 die(_("Unexpected stash response: '%s'"),
1623 buf.buf);
1624 strbuf_reset(&buf);
1625 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1626
1627 if (safe_create_leading_directories_const(autostash))
1628 die(_("Could not create directory for '%s'"),
1629 options.state_dir);
12aeb00a 1630 write_file(autostash, "%s", oid_to_hex(&oid));
6defce2b
PK
1631 printf(_("Created autostash: %s\n"), buf.buf);
1632 if (reset_head(&head->object.oid, "reset --hard",
bac2a1e3 1633 NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
6defce2b
PK
1634 die(_("could not reset --hard"));
1635 printf(_("HEAD is now at %s"),
1636 find_unique_abbrev(&head->object.oid,
1637 DEFAULT_ABBREV));
1638 strbuf_reset(&buf);
1639 pp_commit_easy(CMIT_FMT_ONELINE, head, &buf);
1640 if (buf.len > 0)
1641 printf(" %s", buf.buf);
1642 putchar('\n');
1643
1644 if (discard_index(the_repository->index) < 0 ||
e1ff0a32 1645 repo_read_index(the_repository) < 0)
6defce2b
PK
1646 die(_("could not read index"));
1647 }
1648 }
1649
5b02ca38 1650 if (require_clean_work_tree(the_repository, "rebase",
e0333e5c
PK
1651 _("Please commit or stash them."), 1, 1)) {
1652 ret = 1;
1653 goto cleanup;
ac7f467f
PK
1654 }
1655
9a48a615
PK
1656 /*
1657 * Now we are rebasing commits upstream..orig_head (or with --root,
1658 * everything leading up to orig_head) on top of onto.
1659 */
1660
1661 /*
1662 * Check if we are already based on onto with linear history,
1663 * but this should be done only when upstream and onto are the same
1664 * and if this is not an interactive rebase.
1665 */
1666 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
1667 !is_interactive(&options) && !options.restrict_revision &&
9dba809a 1668 options.upstream &&
9a48a615
PK
1669 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1670 int flag;
1671
1ed9c14f 1672 if (!(options.flags & REBASE_FORCE)) {
e65123a7
PK
1673 /* Lazily switch to the target branch if needed... */
1674 if (options.switch_to) {
1675 struct object_id oid;
1676
1677 if (get_oid(options.switch_to, &oid) < 0) {
1678 ret = !!error(_("could not parse '%s'"),
1679 options.switch_to);
1680 goto cleanup;
1681 }
1682
1683 strbuf_reset(&buf);
13a5a9f0
JS
1684 strbuf_addf(&buf, "%s: checkout %s",
1685 getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
e65123a7
PK
1686 options.switch_to);
1687 if (reset_head(&oid, "checkout",
8581df65
OS
1688 options.head_name,
1689 RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
13a5a9f0 1690 NULL, buf.buf) < 0) {
e65123a7
PK
1691 ret = !!error(_("could not switch to "
1692 "%s"),
1693 options.switch_to);
1694 goto cleanup;
1695 }
1696 }
1697
1ed9c14f
PK
1698 if (!(options.flags & REBASE_NO_QUIET))
1699 ; /* be quiet */
1700 else if (!strcmp(branch_name, "HEAD") &&
1701 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1702 puts(_("HEAD is up to date."));
1703 else
1704 printf(_("Current branch %s is up to date.\n"),
1705 branch_name);
1706 ret = !!finish_rebase(&options);
1707 goto cleanup;
1708 } else if (!(options.flags & REBASE_NO_QUIET))
9a48a615
PK
1709 ; /* be quiet */
1710 else if (!strcmp(branch_name, "HEAD") &&
1711 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1712 puts(_("HEAD is up to date, rebase forced."));
1713 else
1714 printf(_("Current branch %s is up to date, rebase "
1715 "forced.\n"), branch_name);
1716 }
1717
06e4775a
PK
1718 /* If a hook exists, give it a chance to interrupt*/
1719 if (!ok_to_skip_pre_rebase &&
1720 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1721 argc ? argv[0] : NULL, NULL))
1722 die(_("The pre-rebase hook refused to rebase."));
1723
bff014da
PK
1724 if (options.flags & REBASE_DIFFSTAT) {
1725 struct diff_options opts;
1726
8797f0f0
JS
1727 if (options.flags & REBASE_VERBOSE) {
1728 if (is_null_oid(&merge_base))
1729 printf(_("Changes to %s:\n"),
1730 oid_to_hex(&options.onto->object.oid));
1731 else
1732 printf(_("Changes from %s to %s:\n"),
1733 oid_to_hex(&merge_base),
1734 oid_to_hex(&options.onto->object.oid));
1735 }
bff014da
PK
1736
1737 /* We want color (if set), but no pager */
1738 diff_setup(&opts);
1739 opts.stat_width = -1; /* use full terminal width */
1740 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1741 opts.output_format |=
1742 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1743 opts.detect_rename = DIFF_DETECT_RENAME;
1744 diff_setup_done(&opts);
8797f0f0
JS
1745 diff_tree_oid(is_null_oid(&merge_base) ?
1746 the_hash_algo->empty_tree : &merge_base,
1747 &options.onto->object.oid, "", &opts);
bff014da
PK
1748 diffcore_std(&opts);
1749 diff_flush(&opts);
1750 }
1751
361badd3
PK
1752 if (is_interactive(&options))
1753 goto run_rebase;
1754
bff014da
PK
1755 /* Detach HEAD and reset the tree */
1756 if (options.flags & REBASE_NO_QUIET)
1757 printf(_("First, rewinding head to replay your work on top of "
1758 "it...\n"));
1759
13a5a9f0
JS
1760 strbuf_addf(&msg, "%s: checkout %s",
1761 getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
73d6d7b2 1762 if (reset_head(&options.onto->object.oid, "checkout", NULL,
8581df65
OS
1763 RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1764 NULL, msg.buf))
ac7f467f
PK
1765 die(_("Could not detach HEAD"));
1766 strbuf_release(&msg);
1767
7eecfa56
PK
1768 /*
1769 * If the onto is a proper descendant of the tip of the branch, then
1770 * we just fast-forwarded.
1771 */
1772 strbuf_reset(&msg);
1773 if (!oidcmp(&merge_base, &options.orig_head)) {
eff199a6 1774 printf(_("Fast-forwarded %s to %s.\n"),
7eecfa56
PK
1775 branch_name, options.onto_name);
1776 strbuf_addf(&msg, "rebase finished: %s onto %s",
1777 options.head_name ? options.head_name : "detached HEAD",
1778 oid_to_hex(&options.onto->object.oid));
1779 reset_head(NULL, "Fast-forwarded", options.head_name, 0,
1780 "HEAD", msg.buf);
1781 strbuf_release(&msg);
1782 ret = !!finish_rebase(&options);
1783 goto cleanup;
1784 }
1785
ac7f467f
PK
1786 strbuf_addf(&revisions, "%s..%s",
1787 options.root ? oid_to_hex(&options.onto->object.oid) :
1788 (options.restrict_revision ?
1789 oid_to_hex(&options.restrict_revision->object.oid) :
1790 oid_to_hex(&options.upstream->object.oid)),
1791 oid_to_hex(&options.orig_head));
1792
1793 options.revisions = revisions.buf;
1794
f9573628 1795run_rebase:
ac7f467f
PK
1796 ret = !!run_specific_rebase(&options);
1797
e0333e5c 1798cleanup:
ac7f467f
PK
1799 strbuf_release(&revisions);
1800 free(options.head_name);
12026a41 1801 free(options.gpg_sign_opt);
68e46d78 1802 free(options.cmd);
9dba809a 1803 free(squash_onto_name);
ac7f467f 1804 return ret;
55071ea2 1805}