]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/rebase.c
builtin rebase: optionally pass custom reflogs to reset_head()
[thirdparty/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"
122420c2 24#include "rerere.h"
f28d40d3
PK
25
26static char const * const builtin_rebase_usage[] = {
27 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
28 "[<upstream>] [<branch>]"),
29 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
30 "--root [<branch>]"),
31 N_("git rebase --continue | --abort | --skip | --edit-todo"),
32 NULL
33};
ac7f467f
PK
34
35static GIT_PATH_FUNC(apply_dir, "rebase-apply")
36static GIT_PATH_FUNC(merge_dir, "rebase-merge")
37
38enum rebase_type {
39 REBASE_UNSPECIFIED = -1,
40 REBASE_AM,
41 REBASE_MERGE,
42 REBASE_INTERACTIVE,
43 REBASE_PRESERVE_MERGES
44};
55071ea2
PK
45
46static int use_builtin_rebase(void)
47{
48 struct child_process cp = CHILD_PROCESS_INIT;
49 struct strbuf out = STRBUF_INIT;
50 int ret;
51
52 argv_array_pushl(&cp.args,
53 "config", "--bool", "rebase.usebuiltin", NULL);
54 cp.git_cmd = 1;
55 if (capture_command(&cp, &out, 6)) {
56 strbuf_release(&out);
57 return 0;
58 }
59
60 strbuf_trim(&out);
61 ret = !strcmp("true", out.buf);
62 strbuf_release(&out);
63 return ret;
64}
65
ac7f467f
PK
66struct rebase_options {
67 enum rebase_type type;
68 const char *state_dir;
69 struct commit *upstream;
70 const char *upstream_name;
06e4775a 71 const char *upstream_arg;
ac7f467f
PK
72 char *head_name;
73 struct object_id orig_head;
74 struct commit *onto;
75 const char *onto_name;
76 const char *revisions;
e65123a7 77 const char *switch_to;
ac7f467f 78 int root;
9dba809a 79 struct object_id *squash_onto;
ac7f467f
PK
80 struct commit *restrict_revision;
81 int dont_finish_rebase;
b4c8eb02
PK
82 enum {
83 REBASE_NO_QUIET = 1<<0,
bff014da
PK
84 REBASE_VERBOSE = 1<<1,
85 REBASE_DIFFSTAT = 1<<2,
1ed9c14f 86 REBASE_FORCE = 1<<3,
c54dacb5 87 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
b4c8eb02
PK
88 } flags;
89 struct strbuf git_am_opt;
f9573628 90 const char *action;
73d51ed0 91 int signoff;
ead98c11 92 int allow_rerere_autoupdate;
002ee2fe 93 int keep_empty;
051910a9 94 int autosquash;
12026a41 95 char *gpg_sign_opt;
6defce2b 96 int autostash;
68e46d78 97 char *cmd;
9b3a448b 98 int allow_empty_message;
3c3588c7 99 int rebase_merges, rebase_cousins;
ba1905a5 100 char *strategy, *strategy_opts;
ac7f467f
PK
101};
102
9a48a615
PK
103static int is_interactive(struct rebase_options *opts)
104{
105 return opts->type == REBASE_INTERACTIVE ||
106 opts->type == REBASE_PRESERVE_MERGES;
107}
108
002ee2fe
PK
109static void imply_interactive(struct rebase_options *opts, const char *option)
110{
111 switch (opts->type) {
112 case REBASE_AM:
113 die(_("%s requires an interactive rebase"), option);
114 break;
115 case REBASE_INTERACTIVE:
116 case REBASE_PRESERVE_MERGES:
117 break;
118 case REBASE_MERGE:
119 /* we silently *upgrade* --merge to --interactive if needed */
120 default:
121 opts->type = REBASE_INTERACTIVE; /* implied */
122 break;
123 }
124}
125
ac7f467f
PK
126/* Returns the filename prefixed by the state_dir */
127static const char *state_dir_path(const char *filename, struct rebase_options *opts)
128{
129 static struct strbuf path = STRBUF_INIT;
130 static size_t prefix_len;
131
132 if (!prefix_len) {
133 strbuf_addf(&path, "%s/", opts->state_dir);
134 prefix_len = path.len;
135 }
136
137 strbuf_setlen(&path, prefix_len);
138 strbuf_addstr(&path, filename);
139 return path.buf;
140}
141
f9573628
PK
142/* Read one file, then strip line endings */
143static int read_one(const char *path, struct strbuf *buf)
144{
145 if (strbuf_read_file(buf, path, 0) < 0)
146 return error_errno(_("could not read '%s'"), path);
147 strbuf_trim_trailing_newline(buf);
148 return 0;
149}
150
151/* Initialize the rebase options from the state directory. */
152static int read_basic_state(struct rebase_options *opts)
153{
154 struct strbuf head_name = STRBUF_INIT;
155 struct strbuf buf = STRBUF_INIT;
156 struct object_id oid;
157
158 if (read_one(state_dir_path("head-name", opts), &head_name) ||
159 read_one(state_dir_path("onto", opts), &buf))
160 return -1;
161 opts->head_name = starts_with(head_name.buf, "refs/") ?
162 xstrdup(head_name.buf) : NULL;
163 strbuf_release(&head_name);
164 if (get_oid(buf.buf, &oid))
165 return error(_("could not get 'onto': '%s'"), buf.buf);
166 opts->onto = lookup_commit_or_die(&oid, buf.buf);
167
168 /*
169 * We always write to orig-head, but interactive rebase used to write to
170 * head. Fall back to reading from head to cover for the case that the
171 * user upgraded git with an ongoing interactive rebase.
172 */
173 strbuf_reset(&buf);
174 if (file_exists(state_dir_path("orig-head", opts))) {
175 if (read_one(state_dir_path("orig-head", opts), &buf))
176 return -1;
177 } else if (read_one(state_dir_path("head", opts), &buf))
178 return -1;
179 if (get_oid(buf.buf, &opts->orig_head))
180 return error(_("invalid orig-head: '%s'"), buf.buf);
181
182 strbuf_reset(&buf);
183 if (read_one(state_dir_path("quiet", opts), &buf))
184 return -1;
185 if (buf.len)
186 opts->flags &= ~REBASE_NO_QUIET;
187 else
188 opts->flags |= REBASE_NO_QUIET;
189
190 if (file_exists(state_dir_path("verbose", opts)))
191 opts->flags |= REBASE_VERBOSE;
192
73d51ed0
PK
193 if (file_exists(state_dir_path("signoff", opts))) {
194 opts->signoff = 1;
195 opts->flags |= REBASE_FORCE;
196 }
197
ead98c11
PK
198 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
199 strbuf_reset(&buf);
200 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
201 &buf))
202 return -1;
203 if (!strcmp(buf.buf, "--rerere-autoupdate"))
204 opts->allow_rerere_autoupdate = 1;
205 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
206 opts->allow_rerere_autoupdate = 0;
207 else
208 warning(_("ignoring invalid allow_rerere_autoupdate: "
209 "'%s'"), buf.buf);
210 } else
211 opts->allow_rerere_autoupdate = -1;
212
12026a41
PK
213 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
214 strbuf_reset(&buf);
215 if (read_one(state_dir_path("gpg_sign_opt", opts),
216 &buf))
217 return -1;
218 free(opts->gpg_sign_opt);
219 opts->gpg_sign_opt = xstrdup(buf.buf);
220 }
221
ba1905a5
PK
222 if (file_exists(state_dir_path("strategy", opts))) {
223 strbuf_reset(&buf);
224 if (read_one(state_dir_path("strategy", opts), &buf))
225 return -1;
226 free(opts->strategy);
227 opts->strategy = xstrdup(buf.buf);
228 }
229
230 if (file_exists(state_dir_path("strategy_opts", opts))) {
231 strbuf_reset(&buf);
232 if (read_one(state_dir_path("strategy_opts", opts), &buf))
233 return -1;
234 free(opts->strategy_opts);
235 opts->strategy_opts = xstrdup(buf.buf);
236 }
237
f9573628
PK
238 strbuf_release(&buf);
239
240 return 0;
241}
242
6defce2b
PK
243static int apply_autostash(struct rebase_options *opts)
244{
245 const char *path = state_dir_path("autostash", opts);
246 struct strbuf autostash = STRBUF_INIT;
247 struct child_process stash_apply = CHILD_PROCESS_INIT;
248
249 if (!file_exists(path))
250 return 0;
251
252 if (read_one(state_dir_path("autostash", opts), &autostash))
253 return error(_("Could not read '%s'"), path);
254 argv_array_pushl(&stash_apply.args,
255 "stash", "apply", autostash.buf, NULL);
256 stash_apply.git_cmd = 1;
257 stash_apply.no_stderr = stash_apply.no_stdout =
258 stash_apply.no_stdin = 1;
259 if (!run_command(&stash_apply))
260 printf(_("Applied autostash.\n"));
261 else {
262 struct argv_array args = ARGV_ARRAY_INIT;
263 int res = 0;
264
265 argv_array_pushl(&args,
266 "stash", "store", "-m", "autostash", "-q",
267 autostash.buf, NULL);
268 if (run_command_v_opt(args.argv, RUN_GIT_CMD))
269 res = error(_("Cannot store %s"), autostash.buf);
270 argv_array_clear(&args);
271 strbuf_release(&autostash);
272 if (res)
273 return res;
274
275 fprintf(stderr,
276 _("Applying autostash resulted in conflicts.\n"
277 "Your changes are safe in the stash.\n"
278 "You can run \"git stash pop\" or \"git stash drop\" "
279 "at any time.\n"));
280 }
281
282 strbuf_release(&autostash);
283 return 0;
284}
285
ac7f467f
PK
286static int finish_rebase(struct rebase_options *opts)
287{
288 struct strbuf dir = STRBUF_INIT;
289 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
290
291 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
6defce2b 292 apply_autostash(opts);
ac7f467f
PK
293 close_all_packs(the_repository->objects);
294 /*
295 * We ignore errors in 'gc --auto', since the
296 * user should see them.
297 */
298 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
299 strbuf_addstr(&dir, opts->state_dir);
300 remove_dir_recursively(&dir, 0);
301 strbuf_release(&dir);
302
303 return 0;
304}
305
306static struct commit *peel_committish(const char *name)
307{
308 struct object *obj;
309 struct object_id oid;
310
311 if (get_oid(name, &oid))
312 return NULL;
313 obj = parse_object(the_repository, &oid);
314 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
315}
316
317static void add_var(struct strbuf *buf, const char *name, const char *value)
318{
319 if (!value)
320 strbuf_addf(buf, "unset %s; ", name);
321 else {
322 strbuf_addf(buf, "%s=", name);
323 sq_quote_buf(buf, value);
324 strbuf_addstr(buf, "; ");
325 }
326}
327
328static int run_specific_rebase(struct rebase_options *opts)
329{
330 const char *argv[] = { NULL, NULL };
331 struct strbuf script_snippet = STRBUF_INIT;
332 int status;
333 const char *backend, *backend_func;
334
335 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
336 add_var(&script_snippet, "state_dir", opts->state_dir);
337
338 add_var(&script_snippet, "upstream_name", opts->upstream_name);
f9573628
PK
339 add_var(&script_snippet, "upstream", opts->upstream ?
340 oid_to_hex(&opts->upstream->object.oid) : NULL);
d4c569f8
PK
341 add_var(&script_snippet, "head_name",
342 opts->head_name ? opts->head_name : "detached HEAD");
ac7f467f 343 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
f9573628
PK
344 add_var(&script_snippet, "onto", opts->onto ?
345 oid_to_hex(&opts->onto->object.oid) : NULL);
ac7f467f
PK
346 add_var(&script_snippet, "onto_name", opts->onto_name);
347 add_var(&script_snippet, "revisions", opts->revisions);
348 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
349 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
b4c8eb02
PK
350 add_var(&script_snippet, "GIT_QUIET",
351 opts->flags & REBASE_NO_QUIET ? "" : "t");
352 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
bff014da
PK
353 add_var(&script_snippet, "verbose",
354 opts->flags & REBASE_VERBOSE ? "t" : "");
355 add_var(&script_snippet, "diffstat",
356 opts->flags & REBASE_DIFFSTAT ? "t" : "");
1ed9c14f
PK
357 add_var(&script_snippet, "force_rebase",
358 opts->flags & REBASE_FORCE ? "t" : "");
e65123a7
PK
359 if (opts->switch_to)
360 add_var(&script_snippet, "switch_to", opts->switch_to);
f9573628 361 add_var(&script_snippet, "action", opts->action ? opts->action : "");
73d51ed0 362 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
ead98c11
PK
363 add_var(&script_snippet, "allow_rerere_autoupdate",
364 opts->allow_rerere_autoupdate < 0 ? "" :
365 opts->allow_rerere_autoupdate ?
366 "--rerere-autoupdate" : "--no-rerere-autoupdate");
002ee2fe 367 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
051910a9 368 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
12026a41 369 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
68e46d78 370 add_var(&script_snippet, "cmd", opts->cmd);
9b3a448b
PK
371 add_var(&script_snippet, "allow_empty_message",
372 opts->allow_empty_message ? "--allow-empty-message" : "");
3c3588c7
PK
373 add_var(&script_snippet, "rebase_merges",
374 opts->rebase_merges ? "t" : "");
375 add_var(&script_snippet, "rebase_cousins",
376 opts->rebase_cousins ? "t" : "");
ba1905a5
PK
377 add_var(&script_snippet, "strategy", opts->strategy);
378 add_var(&script_snippet, "strategy_opts", opts->strategy_opts);
9dba809a
PK
379 add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
380 add_var(&script_snippet, "squash_onto",
381 opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
ac7f467f
PK
382
383 switch (opts->type) {
384 case REBASE_AM:
385 backend = "git-rebase--am";
386 backend_func = "git_rebase__am";
387 break;
388 case REBASE_INTERACTIVE:
389 backend = "git-rebase--interactive";
390 backend_func = "git_rebase__interactive";
391 break;
392 case REBASE_MERGE:
393 backend = "git-rebase--merge";
394 backend_func = "git_rebase__merge";
395 break;
396 case REBASE_PRESERVE_MERGES:
397 backend = "git-rebase--preserve-merges";
398 backend_func = "git_rebase__preserve_merges";
399 break;
400 default:
401 BUG("Unhandled rebase type %d", opts->type);
402 break;
403 }
404
405 strbuf_addf(&script_snippet,
406 ". git-sh-setup && . git-rebase--common &&"
407 " . %s && %s", backend, backend_func);
408 argv[0] = script_snippet.buf;
409
410 status = run_command_v_opt(argv, RUN_USING_SHELL);
411 if (opts->dont_finish_rebase)
412 ; /* do nothing */
413 else if (status == 0) {
414 if (!file_exists(state_dir_path("stopped-sha", opts)))
415 finish_rebase(opts);
416 } else if (status == 2) {
417 struct strbuf dir = STRBUF_INIT;
418
6defce2b 419 apply_autostash(opts);
ac7f467f
PK
420 strbuf_addstr(&dir, opts->state_dir);
421 remove_dir_recursively(&dir, 0);
422 strbuf_release(&dir);
423 die("Nothing to do");
424 }
425
426 strbuf_release(&script_snippet);
427
428 return status ? -1 : 0;
429}
430
431#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
432
433static int reset_head(struct object_id *oid, const char *action,
fa443d40
PK
434 const char *switch_to_branch, int detach_head,
435 const char *reflog_orig_head, const char *reflog_head)
ac7f467f
PK
436{
437 struct object_id head_oid;
438 struct tree_desc desc;
439 struct lock_file lock = LOCK_INIT;
440 struct unpack_trees_options unpack_tree_opts;
441 struct tree *tree;
442 const char *reflog_action;
443 struct strbuf msg = STRBUF_INIT;
444 size_t prefix_len;
445 struct object_id *orig = NULL, oid_orig,
446 *old_orig = NULL, oid_old_orig;
447 int ret = 0;
448
d4c569f8
PK
449 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
450 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
451
ac7f467f
PK
452 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
453 return -1;
454
455 if (!oid) {
456 if (get_oid("HEAD", &head_oid)) {
457 rollback_lock_file(&lock);
458 return error(_("could not determine HEAD revision"));
459 }
460 oid = &head_oid;
461 }
462
463 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
464 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
465 unpack_tree_opts.head_idx = 1;
466 unpack_tree_opts.src_index = the_repository->index;
467 unpack_tree_opts.dst_index = the_repository->index;
468 unpack_tree_opts.fn = oneway_merge;
469 unpack_tree_opts.update = 1;
470 unpack_tree_opts.merge = 1;
471 if (!detach_head)
472 unpack_tree_opts.reset = 1;
473
474 if (read_index_unmerged(the_repository->index) < 0) {
475 rollback_lock_file(&lock);
476 return error(_("could not read index"));
477 }
478
479 if (!fill_tree_descriptor(&desc, oid)) {
480 error(_("failed to find tree of %s"), oid_to_hex(oid));
481 rollback_lock_file(&lock);
482 free((void *)desc.buffer);
483 return -1;
484 }
485
486 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
487 rollback_lock_file(&lock);
488 free((void *)desc.buffer);
489 return -1;
490 }
491
492 tree = parse_tree_indirect(oid);
493 prime_cache_tree(the_repository->index, tree);
494
495 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
496 ret = error(_("could not write index"));
497 free((void *)desc.buffer);
498
499 if (ret)
500 return ret;
501
502 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
503 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
504 prefix_len = msg.len;
505
506 if (!get_oid("ORIG_HEAD", &oid_old_orig))
507 old_orig = &oid_old_orig;
508 if (!get_oid("HEAD", &oid_orig)) {
509 orig = &oid_orig;
fa443d40
PK
510 if (!reflog_orig_head) {
511 strbuf_addstr(&msg, "updating ORIG_HEAD");
512 reflog_orig_head = msg.buf;
513 }
514 update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
ac7f467f
PK
515 UPDATE_REFS_MSG_ON_ERR);
516 } else if (old_orig)
517 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
fa443d40
PK
518 if (!reflog_head) {
519 strbuf_setlen(&msg, prefix_len);
520 strbuf_addstr(&msg, "updating HEAD");
521 reflog_head = msg.buf;
522 }
ac7f467f 523 if (!switch_to_branch)
fa443d40 524 ret = update_ref(reflog_head, "HEAD", oid, orig, REF_NO_DEREF,
ac7f467f
PK
525 UPDATE_REFS_MSG_ON_ERR);
526 else {
527 ret = create_symref("HEAD", switch_to_branch, msg.buf);
528 if (!ret)
fa443d40 529 ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
ac7f467f
PK
530 UPDATE_REFS_MSG_ON_ERR);
531 }
532
533 strbuf_release(&msg);
534 return ret;
535}
536
bff014da
PK
537static int rebase_config(const char *var, const char *value, void *data)
538{
539 struct rebase_options *opts = data;
540
541 if (!strcmp(var, "rebase.stat")) {
542 if (git_config_bool(var, value))
543 opts->flags |= REBASE_DIFFSTAT;
544 else
545 opts->flags &= !REBASE_DIFFSTAT;
546 return 0;
547 }
548
051910a9
PK
549 if (!strcmp(var, "rebase.autosquash")) {
550 opts->autosquash = git_config_bool(var, value);
551 return 0;
552 }
553
12026a41
PK
554 if (!strcmp(var, "commit.gpgsign")) {
555 free(opts->gpg_sign_opt);
556 opts->gpg_sign_opt = git_config_bool(var, value) ?
557 xstrdup("-S") : NULL;
558 return 0;
559 }
560
6defce2b
PK
561 if (!strcmp(var, "rebase.autostash")) {
562 opts->autostash = git_config_bool(var, value);
563 return 0;
564 }
565
bff014da
PK
566 return git_default_config(var, value, data);
567}
568
9a48a615
PK
569/*
570 * Determines whether the commits in from..to are linear, i.e. contain
571 * no merge commits. This function *expects* `from` to be an ancestor of
572 * `to`.
573 */
574static int is_linear_history(struct commit *from, struct commit *to)
575{
576 while (to && to != from) {
577 parse_commit(to);
578 if (!to->parents)
579 return 1;
580 if (to->parents->next)
581 return 0;
582 to = to->parents->item;
583 }
584 return 1;
585}
586
587static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
588 struct object_id *merge_base)
589{
590 struct commit *head = lookup_commit(the_repository, head_oid);
591 struct commit_list *merge_bases;
592 int res;
593
594 if (!head)
595 return 0;
596
597 merge_bases = get_merge_bases(onto, head);
598 if (merge_bases && !merge_bases->next) {
599 oidcpy(merge_base, &merge_bases->item->object.oid);
600 res = !oidcmp(merge_base, &onto->object.oid);
601 } else {
602 oidcpy(merge_base, &null_oid);
603 res = 0;
604 }
605 free_commit_list(merge_bases);
606 return res && is_linear_history(onto, head);
607}
608
361badd3
PK
609/* -i followed by -m is still -i */
610static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
611{
612 struct rebase_options *opts = opt->value;
613
614 if (!is_interactive(opts))
615 opts->type = REBASE_MERGE;
616
617 return 0;
618}
619
620/* -i followed by -p is still explicitly interactive, but -p alone is not */
621static int parse_opt_interactive(const struct option *opt, const char *arg,
622 int unset)
623{
624 struct rebase_options *opts = opt->value;
625
626 opts->type = REBASE_INTERACTIVE;
627 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
628
629 return 0;
630}
631
8f5986d9
PK
632static void NORETURN error_on_missing_default_upstream(void)
633{
634 struct branch *current_branch = branch_get(NULL);
635
636 printf(_("%s\n"
637 "Please specify which branch you want to rebase against.\n"
638 "See git-rebase(1) for details.\n"
639 "\n"
640 " git rebase '<branch>'\n"
641 "\n"),
642 current_branch ? _("There is no tracking information for "
643 "the current branch.") :
644 _("You are not currently on a branch."));
645
646 if (current_branch) {
647 const char *remote = current_branch->remote_name;
648
649 if (!remote)
650 remote = _("<remote>");
651
652 printf(_("If you wish to set tracking information for this "
653 "branch you can do so with:\n"
654 "\n"
655 " git branch --set-upstream-to=%s/<branch> %s\n"
656 "\n"),
657 remote, current_branch->name);
658 }
659 exit(1);
660}
661
55071ea2
PK
662int cmd_rebase(int argc, const char **argv, const char *prefix)
663{
ac7f467f
PK
664 struct rebase_options options = {
665 .type = REBASE_UNSPECIFIED,
b4c8eb02
PK
666 .flags = REBASE_NO_QUIET,
667 .git_am_opt = STRBUF_INIT,
ead98c11 668 .allow_rerere_autoupdate = -1,
9b3a448b 669 .allow_empty_message = 1,
ac7f467f
PK
670 };
671 const char *branch_name;
f9573628 672 int ret, flags, total_argc, in_progress = 0;
06e4775a 673 int ok_to_skip_pre_rebase = 0;
ac7f467f
PK
674 struct strbuf msg = STRBUF_INIT;
675 struct strbuf revisions = STRBUF_INIT;
c54dacb5 676 struct strbuf buf = STRBUF_INIT;
075bc852 677 struct object_id merge_base;
f9573628
PK
678 enum {
679 NO_ACTION,
680 ACTION_CONTINUE,
122420c2 681 ACTION_SKIP,
5e5d9619 682 ACTION_ABORT,
5a614945 683 ACTION_QUIT,
51e9ea6d
PK
684 ACTION_EDIT_TODO,
685 ACTION_SHOW_CURRENT_PATCH,
f9573628 686 } action = NO_ACTION;
38dbcef2 687 int committer_date_is_author_date = 0;
53f9e5be 688 int ignore_date = 0;
99d8cc7c 689 int ignore_whitespace = 0;
12026a41 690 const char *gpg_sign = NULL;
7998dbe1
PK
691 int opt_c = -1;
692 struct string_list whitespace = STRING_LIST_INIT_NODUP;
68e46d78 693 struct string_list exec = STRING_LIST_INIT_NODUP;
3c3588c7 694 const char *rebase_merges = NULL;
92d0d74e 695 int fork_point = -1;
ba1905a5 696 struct string_list strategy_options = STRING_LIST_INIT_NODUP;
9dba809a
PK
697 struct object_id squash_onto;
698 char *squash_onto_name = NULL;
f28d40d3
PK
699 struct option builtin_rebase_options[] = {
700 OPT_STRING(0, "onto", &options.onto_name,
701 N_("revision"),
702 N_("rebase onto given branch instead of upstream")),
06e4775a
PK
703 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
704 N_("allow pre-rebase hook to run")),
b4c8eb02
PK
705 OPT_NEGBIT('q', "quiet", &options.flags,
706 N_("be quiet. implies --no-stat"),
bff014da
PK
707 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
708 OPT_BIT('v', "verbose", &options.flags,
709 N_("display a diffstat of what changed upstream"),
710 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
711 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
712 N_("do not show diffstat of what changed upstream"),
713 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
99d8cc7c
PK
714 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
715 N_("passed to 'git apply'")),
73d51ed0
PK
716 OPT_BOOL(0, "signoff", &options.signoff,
717 N_("add a Signed-off-by: line to each commit")),
38dbcef2
PK
718 OPT_BOOL(0, "committer-date-is-author-date",
719 &committer_date_is_author_date,
720 N_("passed to 'git am'")),
53f9e5be
PK
721 OPT_BOOL(0, "ignore-date", &ignore_date,
722 N_("passed to 'git am'")),
1ed9c14f
PK
723 OPT_BIT('f', "force-rebase", &options.flags,
724 N_("cherry-pick all commits, even if unchanged"),
725 REBASE_FORCE),
726 OPT_BIT(0, "no-ff", &options.flags,
727 N_("cherry-pick all commits, even if unchanged"),
728 REBASE_FORCE),
f9573628
PK
729 OPT_CMDMODE(0, "continue", &action, N_("continue"),
730 ACTION_CONTINUE),
122420c2
PK
731 OPT_CMDMODE(0, "skip", &action,
732 N_("skip current patch and continue"), ACTION_SKIP),
5e5d9619
PK
733 OPT_CMDMODE(0, "abort", &action,
734 N_("abort and check out the original branch"),
735 ACTION_ABORT),
5a614945
PK
736 OPT_CMDMODE(0, "quit", &action,
737 N_("abort but keep HEAD where it is"), ACTION_QUIT),
51e9ea6d
PK
738 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
739 "during an interactive rebase"), ACTION_EDIT_TODO),
740 OPT_CMDMODE(0, "show-current-patch", &action,
741 N_("show the patch file being applied or merged"),
742 ACTION_SHOW_CURRENT_PATCH),
361badd3
PK
743 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
744 N_("use merging strategies to rebase"),
745 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
746 parse_opt_merge },
747 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
748 N_("let the user edit the list of commits to rebase"),
749 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
750 parse_opt_interactive },
751 OPT_SET_INT('p', "preserve-merges", &options.type,
752 N_("try to recreate merges instead of ignoring "
753 "them"), REBASE_PRESERVE_MERGES),
ead98c11
PK
754 OPT_BOOL(0, "rerere-autoupdate",
755 &options.allow_rerere_autoupdate,
756 N_("allow rerere to update index with resolved "
757 "conflict")),
002ee2fe
PK
758 OPT_BOOL('k', "keep-empty", &options.keep_empty,
759 N_("preserve empty commits during rebase")),
051910a9
PK
760 OPT_BOOL(0, "autosquash", &options.autosquash,
761 N_("move commits that begin with "
762 "squash!/fixup! under -i")),
12026a41
PK
763 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
764 N_("GPG-sign commits"),
765 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
7998dbe1
PK
766 OPT_STRING_LIST(0, "whitespace", &whitespace,
767 N_("whitespace"), N_("passed to 'git apply'")),
768 OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"),
769 REBASE_AM),
6defce2b
PK
770 OPT_BOOL(0, "autostash", &options.autostash,
771 N_("automatically stash/stash pop before and after")),
68e46d78
PK
772 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
773 N_("add exec lines after each commit of the "
774 "editable list")),
9b3a448b
PK
775 OPT_BOOL(0, "allow-empty-message",
776 &options.allow_empty_message,
777 N_("allow rebasing commits with empty messages")),
3c3588c7
PK
778 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
779 N_("mode"),
780 N_("try to rebase merges instead of skipping them"),
781 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
92d0d74e
PK
782 OPT_BOOL(0, "fork-point", &fork_point,
783 N_("use 'merge-base --fork-point' to refine upstream")),
ba1905a5
PK
784 OPT_STRING('s', "strategy", &options.strategy,
785 N_("strategy"), N_("use the given merge strategy")),
786 OPT_STRING_LIST('X', "strategy-option", &strategy_options,
787 N_("option"),
788 N_("pass the argument through to the merge "
789 "strategy")),
9dba809a
PK
790 OPT_BOOL(0, "root", &options.root,
791 N_("rebase all reachable commits up to the root(s)")),
f28d40d3
PK
792 OPT_END(),
793 };
ac7f467f 794
55071ea2
PK
795 /*
796 * NEEDSWORK: Once the builtin rebase has been tested enough
797 * and git-legacy-rebase.sh is retired to contrib/, this preamble
798 * can be removed.
799 */
800
801 if (!use_builtin_rebase()) {
802 const char *path = mkpath("%s/git-legacy-rebase",
803 git_exec_path());
804
805 if (sane_execvp(path, (char **)argv) < 0)
806 die_errno(_("could not exec %s"), path);
807 else
808 BUG("sane_execvp() returned???");
809 }
810
f28d40d3
PK
811 if (argc == 2 && !strcmp(argv[1], "-h"))
812 usage_with_options(builtin_rebase_usage,
813 builtin_rebase_options);
814
55071ea2
PK
815 prefix = setup_git_directory();
816 trace_repo_setup(prefix);
817 setup_work_tree();
818
bff014da
PK
819 git_config(rebase_config, &options);
820
0eabf4b9
PK
821 strbuf_reset(&buf);
822 strbuf_addf(&buf, "%s/applying", apply_dir());
823 if(file_exists(buf.buf))
824 die(_("It looks like 'git am' is in progress. Cannot rebase."));
825
c54dacb5
PK
826 if (is_directory(apply_dir())) {
827 options.type = REBASE_AM;
828 options.state_dir = apply_dir();
829 } else if (is_directory(merge_dir())) {
830 strbuf_reset(&buf);
831 strbuf_addf(&buf, "%s/rewritten", merge_dir());
832 if (is_directory(buf.buf)) {
833 options.type = REBASE_PRESERVE_MERGES;
834 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
835 } else {
836 strbuf_reset(&buf);
837 strbuf_addf(&buf, "%s/interactive", merge_dir());
838 if(file_exists(buf.buf)) {
839 options.type = REBASE_INTERACTIVE;
840 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
841 } else
842 options.type = REBASE_MERGE;
843 }
844 options.state_dir = merge_dir();
845 }
846
847 if (options.type != REBASE_UNSPECIFIED)
848 in_progress = 1;
849
f9573628 850 total_argc = argc;
f28d40d3
PK
851 argc = parse_options(argc, argv, prefix,
852 builtin_rebase_options,
853 builtin_rebase_usage, 0);
854
f9573628
PK
855 if (action != NO_ACTION && total_argc != 2) {
856 usage_with_options(builtin_rebase_usage,
857 builtin_rebase_options);
858 }
859
f28d40d3
PK
860 if (argc > 2)
861 usage_with_options(builtin_rebase_usage,
862 builtin_rebase_options);
ac7f467f 863
d732a570
PK
864 if (action != NO_ACTION && !in_progress)
865 die(_("No rebase in progress?"));
866
51e9ea6d
PK
867 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
868 die(_("The --edit-todo action can only be used during "
869 "interactive rebase."));
870
f9573628
PK
871 switch (action) {
872 case ACTION_CONTINUE: {
873 struct object_id head;
874 struct lock_file lock_file = LOCK_INIT;
875 int fd;
876
877 options.action = "continue";
878
879 /* Sanity check */
880 if (get_oid("HEAD", &head))
881 die(_("Cannot read HEAD"));
882
883 fd = hold_locked_index(&lock_file, 0);
884 if (read_index(the_repository->index) < 0)
885 die(_("could not read index"));
886 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
887 NULL);
888 if (0 <= fd)
889 update_index_if_able(the_repository->index,
890 &lock_file);
891 rollback_lock_file(&lock_file);
892
893 if (has_unstaged_changes(1)) {
894 puts(_("You must edit all merge conflicts and then\n"
895 "mark them as resolved using git add"));
896 exit(1);
897 }
898 if (read_basic_state(&options))
899 exit(1);
900 goto run_rebase;
901 }
122420c2
PK
902 case ACTION_SKIP: {
903 struct string_list merge_rr = STRING_LIST_INIT_DUP;
904
905 options.action = "skip";
906
907 rerere_clear(&merge_rr);
908 string_list_clear(&merge_rr, 1);
909
fa443d40 910 if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0)
122420c2
PK
911 die(_("could not discard worktree changes"));
912 if (read_basic_state(&options))
913 exit(1);
914 goto run_rebase;
915 }
5e5d9619
PK
916 case ACTION_ABORT: {
917 struct string_list merge_rr = STRING_LIST_INIT_DUP;
918 options.action = "abort";
919
920 rerere_clear(&merge_rr);
921 string_list_clear(&merge_rr, 1);
922
923 if (read_basic_state(&options))
924 exit(1);
925 if (reset_head(&options.orig_head, "reset",
fa443d40 926 options.head_name, 0, NULL, NULL) < 0)
5e5d9619
PK
927 die(_("could not move back to %s"),
928 oid_to_hex(&options.orig_head));
929 ret = finish_rebase(&options);
930 goto cleanup;
931 }
5a614945
PK
932 case ACTION_QUIT: {
933 strbuf_reset(&buf);
934 strbuf_addstr(&buf, options.state_dir);
935 ret = !!remove_dir_recursively(&buf, 0);
936 if (ret)
937 die(_("could not remove '%s'"), options.state_dir);
938 goto cleanup;
939 }
51e9ea6d
PK
940 case ACTION_EDIT_TODO:
941 options.action = "edit-todo";
942 options.dont_finish_rebase = 1;
943 goto run_rebase;
944 case ACTION_SHOW_CURRENT_PATCH:
945 options.action = "show-current-patch";
946 options.dont_finish_rebase = 1;
947 goto run_rebase;
948 case NO_ACTION:
949 break;
f9573628 950 default:
51e9ea6d 951 BUG("action: %d", action);
f9573628
PK
952 }
953
c54dacb5
PK
954 /* Make sure no rebase is in progress */
955 if (in_progress) {
956 const char *last_slash = strrchr(options.state_dir, '/');
957 const char *state_dir_base =
958 last_slash ? last_slash + 1 : options.state_dir;
959 const char *cmd_live_rebase =
960 "git rebase (--continue | --abort | --skip)";
961 strbuf_reset(&buf);
962 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
963 die(_("It seems that there is already a %s directory, and\n"
964 "I wonder if you are in the middle of another rebase. "
965 "If that is the\n"
966 "case, please try\n\t%s\n"
967 "If that is not the case, please\n\t%s\n"
968 "and run me again. I am stopping in case you still "
969 "have something\n"
970 "valuable there.\n"),
971 state_dir_base, cmd_live_rebase, buf.buf);
972 }
973
b4c8eb02
PK
974 if (!(options.flags & REBASE_NO_QUIET))
975 strbuf_addstr(&options.git_am_opt, " -q");
976
38dbcef2
PK
977 if (committer_date_is_author_date) {
978 strbuf_addstr(&options.git_am_opt,
979 " --committer-date-is-author-date");
980 options.flags |= REBASE_FORCE;
981 }
982
99d8cc7c
PK
983 if (ignore_whitespace)
984 strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
985
53f9e5be
PK
986 if (ignore_date) {
987 strbuf_addstr(&options.git_am_opt, " --ignore-date");
988 options.flags |= REBASE_FORCE;
989 }
990
002ee2fe
PK
991 if (options.keep_empty)
992 imply_interactive(&options, "--keep-empty");
993
12026a41
PK
994 if (gpg_sign) {
995 free(options.gpg_sign_opt);
996 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
997 }
998
7998dbe1
PK
999 if (opt_c >= 0)
1000 strbuf_addf(&options.git_am_opt, " -C%d", opt_c);
1001
1002 if (whitespace.nr) {
1003 int i;
1004
1005 for (i = 0; i < whitespace.nr; i++) {
1006 const char *item = whitespace.items[i].string;
1007
1008 strbuf_addf(&options.git_am_opt, " --whitespace=%s",
1009 item);
1010
1011 if ((!strcmp(item, "fix")) || (!strcmp(item, "strip")))
1012 options.flags |= REBASE_FORCE;
1013 }
1014 }
1015
68e46d78
PK
1016 if (exec.nr) {
1017 int i;
1018
1019 imply_interactive(&options, "--exec");
1020
1021 strbuf_reset(&buf);
1022 for (i = 0; i < exec.nr; i++)
1023 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1024 options.cmd = xstrdup(buf.buf);
1025 }
1026
3c3588c7
PK
1027 if (rebase_merges) {
1028 if (!*rebase_merges)
1029 ; /* default mode; do nothing */
1030 else if (!strcmp("rebase-cousins", rebase_merges))
1031 options.rebase_cousins = 1;
1032 else if (strcmp("no-rebase-cousins", rebase_merges))
1033 die(_("Unknown mode: %s"), rebase_merges);
1034 options.rebase_merges = 1;
1035 imply_interactive(&options, "--rebase-merges");
1036 }
1037
ba1905a5
PK
1038 if (strategy_options.nr) {
1039 int i;
1040
1041 if (!options.strategy)
1042 options.strategy = "recursive";
1043
1044 strbuf_reset(&buf);
1045 for (i = 0; i < strategy_options.nr; i++)
1046 strbuf_addf(&buf, " --%s",
1047 strategy_options.items[i].string);
1048 options.strategy_opts = xstrdup(buf.buf);
1049 }
1050
1051 if (options.strategy) {
1052 options.strategy = xstrdup(options.strategy);
1053 switch (options.type) {
1054 case REBASE_AM:
1055 die(_("--strategy requires --merge or --interactive"));
1056 case REBASE_MERGE:
1057 case REBASE_INTERACTIVE:
1058 case REBASE_PRESERVE_MERGES:
1059 /* compatible */
1060 break;
1061 case REBASE_UNSPECIFIED:
1062 options.type = REBASE_MERGE;
1063 break;
1064 default:
1065 BUG("unhandled rebase type (%d)", options.type);
1066 }
1067 }
1068
9dba809a
PK
1069 if (options.root && !options.onto_name)
1070 imply_interactive(&options, "--root without --onto");
1071
ac7f467f
PK
1072 switch (options.type) {
1073 case REBASE_MERGE:
1074 case REBASE_INTERACTIVE:
1075 case REBASE_PRESERVE_MERGES:
1076 options.state_dir = merge_dir();
1077 break;
1078 case REBASE_AM:
1079 options.state_dir = apply_dir();
1080 break;
1081 default:
1082 /* the default rebase backend is `--am` */
1083 options.type = REBASE_AM;
1084 options.state_dir = apply_dir();
1085 break;
1086 }
1087
73d51ed0
PK
1088 if (options.signoff) {
1089 if (options.type == REBASE_PRESERVE_MERGES)
1090 die("cannot combine '--signoff' with "
1091 "'--preserve-merges'");
1092 strbuf_addstr(&options.git_am_opt, " --signoff");
1093 options.flags |= REBASE_FORCE;
1094 }
1095
ac7f467f 1096 if (!options.root) {
8f5986d9
PK
1097 if (argc < 1) {
1098 struct branch *branch;
1099
1100 branch = branch_get(NULL);
1101 options.upstream_name = branch_get_upstream(branch,
1102 NULL);
1103 if (!options.upstream_name)
1104 error_on_missing_default_upstream();
1105 if (fork_point < 0)
1106 fork_point = 1;
1107 } else {
f28d40d3 1108 options.upstream_name = argv[0];
ac7f467f
PK
1109 argc--;
1110 argv++;
1111 if (!strcmp(options.upstream_name, "-"))
1112 options.upstream_name = "@{-1}";
1113 }
1114 options.upstream = peel_committish(options.upstream_name);
1115 if (!options.upstream)
1116 die(_("invalid upstream '%s'"), options.upstream_name);
06e4775a 1117 options.upstream_arg = options.upstream_name;
9dba809a
PK
1118 } else {
1119 if (!options.onto_name) {
1120 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1121 &squash_onto, NULL, NULL) < 0)
1122 die(_("Could not create new root commit"));
1123 options.squash_onto = &squash_onto;
1124 options.onto_name = squash_onto_name =
1125 xstrdup(oid_to_hex(&squash_onto));
1126 }
1127 options.upstream_name = NULL;
1128 options.upstream = NULL;
1129 if (argc > 1)
1130 usage_with_options(builtin_rebase_usage,
1131 builtin_rebase_options);
1132 options.upstream_arg = "--root";
1133 }
ac7f467f
PK
1134
1135 /* Make sure the branch to rebase onto is valid. */
1136 if (!options.onto_name)
1137 options.onto_name = options.upstream_name;
1138 if (strstr(options.onto_name, "...")) {
075bc852
PK
1139 if (get_oid_mb(options.onto_name, &merge_base) < 0)
1140 die(_("'%s': need exactly one merge base"),
1141 options.onto_name);
1142 options.onto = lookup_commit_or_die(&merge_base,
1143 options.onto_name);
ac7f467f
PK
1144 } else {
1145 options.onto = peel_committish(options.onto_name);
1146 if (!options.onto)
1147 die(_("Does not point to a valid commit '%s'"),
1148 options.onto_name);
1149 }
1150
1151 /*
1152 * If the branch to rebase is given, that is the branch we will rebase
1153 * branch_name -- branch/commit being rebased, or
1154 * HEAD (already detached)
1155 * orig_head -- commit object name of tip of the branch before rebasing
d4c569f8 1156 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
ac7f467f 1157 */
e65123a7
PK
1158 if (argc == 1) {
1159 /* Is it "rebase other branchname" or "rebase other commit"? */
1160 branch_name = argv[0];
1161 options.switch_to = argv[0];
1162
1163 /* Is it a local branch? */
1164 strbuf_reset(&buf);
1165 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1166 if (!read_ref(buf.buf, &options.orig_head))
1167 options.head_name = xstrdup(buf.buf);
1168 /* If not is it a valid ref (branch or commit)? */
1169 else if (!get_oid(branch_name, &options.orig_head))
1170 options.head_name = NULL;
1171 else
1172 die(_("fatal: no such branch/commit '%s'"),
1173 branch_name);
1174 } else if (argc == 0) {
ac7f467f
PK
1175 /* Do not need to switch branches, we are already on it. */
1176 options.head_name =
1177 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1178 &flags));
1179 if (!options.head_name)
1180 die(_("No such ref: %s"), "HEAD");
1181 if (flags & REF_ISSYMREF) {
1182 if (!skip_prefix(options.head_name,
1183 "refs/heads/", &branch_name))
1184 branch_name = options.head_name;
1185
1186 } else {
d4c569f8
PK
1187 free(options.head_name);
1188 options.head_name = NULL;
ac7f467f
PK
1189 branch_name = "HEAD";
1190 }
1191 if (get_oid("HEAD", &options.orig_head))
1192 die(_("Could not resolve HEAD to a revision"));
e65123a7
PK
1193 } else
1194 BUG("unexpected number of arguments left to parse");
ac7f467f 1195
92d0d74e
PK
1196 if (fork_point > 0) {
1197 struct commit *head =
1198 lookup_commit_reference(the_repository,
1199 &options.orig_head);
1200 options.restrict_revision =
1201 get_fork_point(options.upstream_name, head);
1202 }
1203
e0333e5c
PK
1204 if (read_index(the_repository->index) < 0)
1205 die(_("could not read index"));
1206
6defce2b
PK
1207 if (options.autostash) {
1208 struct lock_file lock_file = LOCK_INIT;
1209 int fd;
1210
1211 fd = hold_locked_index(&lock_file, 0);
1212 refresh_cache(REFRESH_QUIET);
1213 if (0 <= fd)
1214 update_index_if_able(&the_index, &lock_file);
1215 rollback_lock_file(&lock_file);
1216
1217 if (has_unstaged_changes(0) || has_uncommitted_changes(0)) {
1218 const char *autostash =
1219 state_dir_path("autostash", &options);
1220 struct child_process stash = CHILD_PROCESS_INIT;
1221 struct object_id oid;
1222 struct commit *head =
1223 lookup_commit_reference(the_repository,
1224 &options.orig_head);
1225
1226 argv_array_pushl(&stash.args,
1227 "stash", "create", "autostash", NULL);
1228 stash.git_cmd = 1;
1229 stash.no_stdin = 1;
1230 strbuf_reset(&buf);
1231 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1232 die(_("Cannot autostash"));
1233 strbuf_trim_trailing_newline(&buf);
1234 if (get_oid(buf.buf, &oid))
1235 die(_("Unexpected stash response: '%s'"),
1236 buf.buf);
1237 strbuf_reset(&buf);
1238 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1239
1240 if (safe_create_leading_directories_const(autostash))
1241 die(_("Could not create directory for '%s'"),
1242 options.state_dir);
1243 write_file(autostash, "%s", buf.buf);
1244 printf(_("Created autostash: %s\n"), buf.buf);
1245 if (reset_head(&head->object.oid, "reset --hard",
fa443d40 1246 NULL, 0, NULL, NULL) < 0)
6defce2b
PK
1247 die(_("could not reset --hard"));
1248 printf(_("HEAD is now at %s"),
1249 find_unique_abbrev(&head->object.oid,
1250 DEFAULT_ABBREV));
1251 strbuf_reset(&buf);
1252 pp_commit_easy(CMIT_FMT_ONELINE, head, &buf);
1253 if (buf.len > 0)
1254 printf(" %s", buf.buf);
1255 putchar('\n');
1256
1257 if (discard_index(the_repository->index) < 0 ||
1258 read_index(the_repository->index) < 0)
1259 die(_("could not read index"));
1260 }
1261 }
1262
e0333e5c
PK
1263 if (require_clean_work_tree("rebase",
1264 _("Please commit or stash them."), 1, 1)) {
1265 ret = 1;
1266 goto cleanup;
1267 }
1268
9a48a615
PK
1269 /*
1270 * Now we are rebasing commits upstream..orig_head (or with --root,
1271 * everything leading up to orig_head) on top of onto.
1272 */
1273
1274 /*
1275 * Check if we are already based on onto with linear history,
1276 * but this should be done only when upstream and onto are the same
1277 * and if this is not an interactive rebase.
1278 */
1279 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
1280 !is_interactive(&options) && !options.restrict_revision &&
9dba809a 1281 options.upstream &&
9a48a615
PK
1282 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1283 int flag;
1284
1ed9c14f 1285 if (!(options.flags & REBASE_FORCE)) {
e65123a7
PK
1286 /* Lazily switch to the target branch if needed... */
1287 if (options.switch_to) {
1288 struct object_id oid;
1289
1290 if (get_oid(options.switch_to, &oid) < 0) {
1291 ret = !!error(_("could not parse '%s'"),
1292 options.switch_to);
1293 goto cleanup;
1294 }
1295
1296 strbuf_reset(&buf);
1297 strbuf_addf(&buf, "rebase: checkout %s",
1298 options.switch_to);
1299 if (reset_head(&oid, "checkout",
fa443d40
PK
1300 options.head_name, 0,
1301 NULL, NULL) < 0) {
e65123a7
PK
1302 ret = !!error(_("could not switch to "
1303 "%s"),
1304 options.switch_to);
1305 goto cleanup;
1306 }
1307 }
1308
1ed9c14f
PK
1309 if (!(options.flags & REBASE_NO_QUIET))
1310 ; /* be quiet */
1311 else if (!strcmp(branch_name, "HEAD") &&
1312 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1313 puts(_("HEAD is up to date."));
1314 else
1315 printf(_("Current branch %s is up to date.\n"),
1316 branch_name);
1317 ret = !!finish_rebase(&options);
1318 goto cleanup;
1319 } else if (!(options.flags & REBASE_NO_QUIET))
9a48a615
PK
1320 ; /* be quiet */
1321 else if (!strcmp(branch_name, "HEAD") &&
1322 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1323 puts(_("HEAD is up to date, rebase forced."));
1324 else
1325 printf(_("Current branch %s is up to date, rebase "
1326 "forced.\n"), branch_name);
1327 }
1328
06e4775a
PK
1329 /* If a hook exists, give it a chance to interrupt*/
1330 if (!ok_to_skip_pre_rebase &&
1331 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1332 argc ? argv[0] : NULL, NULL))
1333 die(_("The pre-rebase hook refused to rebase."));
1334
bff014da
PK
1335 if (options.flags & REBASE_DIFFSTAT) {
1336 struct diff_options opts;
1337
1338 if (options.flags & REBASE_VERBOSE)
1339 printf(_("Changes from %s to %s:\n"),
1340 oid_to_hex(&merge_base),
1341 oid_to_hex(&options.onto->object.oid));
1342
1343 /* We want color (if set), but no pager */
1344 diff_setup(&opts);
1345 opts.stat_width = -1; /* use full terminal width */
1346 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1347 opts.output_format |=
1348 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1349 opts.detect_rename = DIFF_DETECT_RENAME;
1350 diff_setup_done(&opts);
1351 diff_tree_oid(&merge_base, &options.onto->object.oid,
1352 "", &opts);
1353 diffcore_std(&opts);
1354 diff_flush(&opts);
1355 }
1356
361badd3
PK
1357 if (is_interactive(&options))
1358 goto run_rebase;
1359
bff014da
PK
1360 /* Detach HEAD and reset the tree */
1361 if (options.flags & REBASE_NO_QUIET)
1362 printf(_("First, rewinding head to replay your work on top of "
1363 "it...\n"));
1364
ac7f467f 1365 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
fa443d40
PK
1366 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1,
1367 NULL, msg.buf))
ac7f467f
PK
1368 die(_("Could not detach HEAD"));
1369 strbuf_release(&msg);
1370
1371 strbuf_addf(&revisions, "%s..%s",
1372 options.root ? oid_to_hex(&options.onto->object.oid) :
1373 (options.restrict_revision ?
1374 oid_to_hex(&options.restrict_revision->object.oid) :
1375 oid_to_hex(&options.upstream->object.oid)),
1376 oid_to_hex(&options.orig_head));
1377
1378 options.revisions = revisions.buf;
1379
f9573628 1380run_rebase:
ac7f467f
PK
1381 ret = !!run_specific_rebase(&options);
1382
e0333e5c 1383cleanup:
ac7f467f
PK
1384 strbuf_release(&revisions);
1385 free(options.head_name);
12026a41 1386 free(options.gpg_sign_opt);
68e46d78 1387 free(options.cmd);
9dba809a 1388 free(squash_onto_name);
ac7f467f 1389 return ret;
55071ea2 1390}