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