23a5bb30030b5287e5d74e349664b61d1ad56ace
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"
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"
19#include "parse-options.h"
20#include "commit.h"
21#include "diff.h"
22#include "wt-status.h"
23#include "revision.h"
24#include "rerere.h"
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};
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};
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
66static int apply_autostash(void)
67{
68 warning("TODO");
69 return 0;
70}
71
72struct rebase_options {
73 enum rebase_type type;
74 const char *state_dir;
75 struct commit *upstream;
76 const char *upstream_name;
77 const char *upstream_arg;
78 char *head_name;
79 struct object_id orig_head;
80 struct commit *onto;
81 const char *onto_name;
82 const char *revisions;
83 const char *switch_to;
84 int root;
85 struct commit *restrict_revision;
86 int dont_finish_rebase;
87 enum {
88 REBASE_NO_QUIET = 1<<0,
89 REBASE_VERBOSE = 1<<1,
90 REBASE_DIFFSTAT = 1<<2,
91 REBASE_FORCE = 1<<3,
92 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
93 } flags;
94 struct strbuf git_am_opt;
95 const char *action;
96 int signoff;
97 int allow_rerere_autoupdate;
98 int keep_empty;
99 int autosquash;
100 char *gpg_sign_opt;
101};
102
103static int is_interactive(struct rebase_options *opts)
104{
105 return opts->type == REBASE_INTERACTIVE ||
106 opts->type == REBASE_PRESERVE_MERGES;
107}
108
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
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
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
193 if (file_exists(state_dir_path("signoff", opts))) {
194 opts->signoff = 1;
195 opts->flags |= REBASE_FORCE;
196 }
197
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
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
222 strbuf_release(&buf);
223
224 return 0;
225}
226
227static int finish_rebase(struct rebase_options *opts)
228{
229 struct strbuf dir = STRBUF_INIT;
230 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
231
232 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
233 apply_autostash();
234 close_all_packs(the_repository->objects);
235 /*
236 * We ignore errors in 'gc --auto', since the
237 * user should see them.
238 */
239 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
240 strbuf_addstr(&dir, opts->state_dir);
241 remove_dir_recursively(&dir, 0);
242 strbuf_release(&dir);
243
244 return 0;
245}
246
247static struct commit *peel_committish(const char *name)
248{
249 struct object *obj;
250 struct object_id oid;
251
252 if (get_oid(name, &oid))
253 return NULL;
254 obj = parse_object(the_repository, &oid);
255 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
256}
257
258static void add_var(struct strbuf *buf, const char *name, const char *value)
259{
260 if (!value)
261 strbuf_addf(buf, "unset %s; ", name);
262 else {
263 strbuf_addf(buf, "%s=", name);
264 sq_quote_buf(buf, value);
265 strbuf_addstr(buf, "; ");
266 }
267}
268
269static int run_specific_rebase(struct rebase_options *opts)
270{
271 const char *argv[] = { NULL, NULL };
272 struct strbuf script_snippet = STRBUF_INIT;
273 int status;
274 const char *backend, *backend_func;
275
276 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
277 add_var(&script_snippet, "state_dir", opts->state_dir);
278
279 add_var(&script_snippet, "upstream_name", opts->upstream_name);
280 add_var(&script_snippet, "upstream", opts->upstream ?
281 oid_to_hex(&opts->upstream->object.oid) : NULL);
282 add_var(&script_snippet, "head_name",
283 opts->head_name ? opts->head_name : "detached HEAD");
284 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
285 add_var(&script_snippet, "onto", opts->onto ?
286 oid_to_hex(&opts->onto->object.oid) : NULL);
287 add_var(&script_snippet, "onto_name", opts->onto_name);
288 add_var(&script_snippet, "revisions", opts->revisions);
289 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
290 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
291 add_var(&script_snippet, "GIT_QUIET",
292 opts->flags & REBASE_NO_QUIET ? "" : "t");
293 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
294 add_var(&script_snippet, "verbose",
295 opts->flags & REBASE_VERBOSE ? "t" : "");
296 add_var(&script_snippet, "diffstat",
297 opts->flags & REBASE_DIFFSTAT ? "t" : "");
298 add_var(&script_snippet, "force_rebase",
299 opts->flags & REBASE_FORCE ? "t" : "");
300 if (opts->switch_to)
301 add_var(&script_snippet, "switch_to", opts->switch_to);
302 add_var(&script_snippet, "action", opts->action ? opts->action : "");
303 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
304 add_var(&script_snippet, "allow_rerere_autoupdate",
305 opts->allow_rerere_autoupdate < 0 ? "" :
306 opts->allow_rerere_autoupdate ?
307 "--rerere-autoupdate" : "--no-rerere-autoupdate");
308 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
309 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
310 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
311
312 switch (opts->type) {
313 case REBASE_AM:
314 backend = "git-rebase--am";
315 backend_func = "git_rebase__am";
316 break;
317 case REBASE_INTERACTIVE:
318 backend = "git-rebase--interactive";
319 backend_func = "git_rebase__interactive";
320 break;
321 case REBASE_MERGE:
322 backend = "git-rebase--merge";
323 backend_func = "git_rebase__merge";
324 break;
325 case REBASE_PRESERVE_MERGES:
326 backend = "git-rebase--preserve-merges";
327 backend_func = "git_rebase__preserve_merges";
328 break;
329 default:
330 BUG("Unhandled rebase type %d", opts->type);
331 break;
332 }
333
334 strbuf_addf(&script_snippet,
335 ". git-sh-setup && . git-rebase--common &&"
336 " . %s && %s", backend, backend_func);
337 argv[0] = script_snippet.buf;
338
339 status = run_command_v_opt(argv, RUN_USING_SHELL);
340 if (opts->dont_finish_rebase)
341 ; /* do nothing */
342 else if (status == 0) {
343 if (!file_exists(state_dir_path("stopped-sha", opts)))
344 finish_rebase(opts);
345 } else if (status == 2) {
346 struct strbuf dir = STRBUF_INIT;
347
348 apply_autostash();
349 strbuf_addstr(&dir, opts->state_dir);
350 remove_dir_recursively(&dir, 0);
351 strbuf_release(&dir);
352 die("Nothing to do");
353 }
354
355 strbuf_release(&script_snippet);
356
357 return status ? -1 : 0;
358}
359
360#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
361
362static int reset_head(struct object_id *oid, const char *action,
363 const char *switch_to_branch, int detach_head)
364{
365 struct object_id head_oid;
366 struct tree_desc desc;
367 struct lock_file lock = LOCK_INIT;
368 struct unpack_trees_options unpack_tree_opts;
369 struct tree *tree;
370 const char *reflog_action;
371 struct strbuf msg = STRBUF_INIT;
372 size_t prefix_len;
373 struct object_id *orig = NULL, oid_orig,
374 *old_orig = NULL, oid_old_orig;
375 int ret = 0;
376
377 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
378 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
379
380 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
381 return -1;
382
383 if (!oid) {
384 if (get_oid("HEAD", &head_oid)) {
385 rollback_lock_file(&lock);
386 return error(_("could not determine HEAD revision"));
387 }
388 oid = &head_oid;
389 }
390
391 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
392 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
393 unpack_tree_opts.head_idx = 1;
394 unpack_tree_opts.src_index = the_repository->index;
395 unpack_tree_opts.dst_index = the_repository->index;
396 unpack_tree_opts.fn = oneway_merge;
397 unpack_tree_opts.update = 1;
398 unpack_tree_opts.merge = 1;
399 if (!detach_head)
400 unpack_tree_opts.reset = 1;
401
402 if (read_index_unmerged(the_repository->index) < 0) {
403 rollback_lock_file(&lock);
404 return error(_("could not read index"));
405 }
406
407 if (!fill_tree_descriptor(&desc, oid)) {
408 error(_("failed to find tree of %s"), oid_to_hex(oid));
409 rollback_lock_file(&lock);
410 free((void *)desc.buffer);
411 return -1;
412 }
413
414 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
415 rollback_lock_file(&lock);
416 free((void *)desc.buffer);
417 return -1;
418 }
419
420 tree = parse_tree_indirect(oid);
421 prime_cache_tree(the_repository->index, tree);
422
423 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
424 ret = error(_("could not write index"));
425 free((void *)desc.buffer);
426
427 if (ret)
428 return ret;
429
430 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
431 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
432 prefix_len = msg.len;
433
434 if (!get_oid("ORIG_HEAD", &oid_old_orig))
435 old_orig = &oid_old_orig;
436 if (!get_oid("HEAD", &oid_orig)) {
437 orig = &oid_orig;
438 strbuf_addstr(&msg, "updating ORIG_HEAD");
439 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
440 UPDATE_REFS_MSG_ON_ERR);
441 } else if (old_orig)
442 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
443 strbuf_setlen(&msg, prefix_len);
444 strbuf_addstr(&msg, "updating HEAD");
445 if (!switch_to_branch)
446 ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
447 UPDATE_REFS_MSG_ON_ERR);
448 else {
449 ret = create_symref("HEAD", switch_to_branch, msg.buf);
450 if (!ret)
451 ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
452 UPDATE_REFS_MSG_ON_ERR);
453 }
454
455 strbuf_release(&msg);
456 return ret;
457}
458
459static int rebase_config(const char *var, const char *value, void *data)
460{
461 struct rebase_options *opts = data;
462
463 if (!strcmp(var, "rebase.stat")) {
464 if (git_config_bool(var, value))
465 opts->flags |= REBASE_DIFFSTAT;
466 else
467 opts->flags &= !REBASE_DIFFSTAT;
468 return 0;
469 }
470
471 if (!strcmp(var, "rebase.autosquash")) {
472 opts->autosquash = git_config_bool(var, value);
473 return 0;
474 }
475
476 if (!strcmp(var, "commit.gpgsign")) {
477 free(opts->gpg_sign_opt);
478 opts->gpg_sign_opt = git_config_bool(var, value) ?
479 xstrdup("-S") : NULL;
480 return 0;
481 }
482
483 return git_default_config(var, value, data);
484}
485
486/*
487 * Determines whether the commits in from..to are linear, i.e. contain
488 * no merge commits. This function *expects* `from` to be an ancestor of
489 * `to`.
490 */
491static int is_linear_history(struct commit *from, struct commit *to)
492{
493 while (to && to != from) {
494 parse_commit(to);
495 if (!to->parents)
496 return 1;
497 if (to->parents->next)
498 return 0;
499 to = to->parents->item;
500 }
501 return 1;
502}
503
504static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
505 struct object_id *merge_base)
506{
507 struct commit *head = lookup_commit(the_repository, head_oid);
508 struct commit_list *merge_bases;
509 int res;
510
511 if (!head)
512 return 0;
513
514 merge_bases = get_merge_bases(onto, head);
515 if (merge_bases && !merge_bases->next) {
516 oidcpy(merge_base, &merge_bases->item->object.oid);
517 res = !oidcmp(merge_base, &onto->object.oid);
518 } else {
519 oidcpy(merge_base, &null_oid);
520 res = 0;
521 }
522 free_commit_list(merge_bases);
523 return res && is_linear_history(onto, head);
524}
525
526/* -i followed by -m is still -i */
527static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
528{
529 struct rebase_options *opts = opt->value;
530
531 if (!is_interactive(opts))
532 opts->type = REBASE_MERGE;
533
534 return 0;
535}
536
537/* -i followed by -p is still explicitly interactive, but -p alone is not */
538static int parse_opt_interactive(const struct option *opt, const char *arg,
539 int unset)
540{
541 struct rebase_options *opts = opt->value;
542
543 opts->type = REBASE_INTERACTIVE;
544 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
545
546 return 0;
547}
548
549int cmd_rebase(int argc, const char **argv, const char *prefix)
550{
551 struct rebase_options options = {
552 .type = REBASE_UNSPECIFIED,
553 .flags = REBASE_NO_QUIET,
554 .git_am_opt = STRBUF_INIT,
555 .allow_rerere_autoupdate = -1,
556 };
557 const char *branch_name;
558 int ret, flags, total_argc, in_progress = 0;
559 int ok_to_skip_pre_rebase = 0;
560 struct strbuf msg = STRBUF_INIT;
561 struct strbuf revisions = STRBUF_INIT;
562 struct strbuf buf = STRBUF_INIT;
563 struct object_id merge_base;
564 enum {
565 NO_ACTION,
566 ACTION_CONTINUE,
567 ACTION_SKIP,
568 ACTION_ABORT,
569 ACTION_QUIT,
570 ACTION_EDIT_TODO,
571 ACTION_SHOW_CURRENT_PATCH,
572 } action = NO_ACTION;
573 int committer_date_is_author_date = 0;
574 int ignore_date = 0;
575 int ignore_whitespace = 0;
576 const char *gpg_sign = NULL;
577 struct option builtin_rebase_options[] = {
578 OPT_STRING(0, "onto", &options.onto_name,
579 N_("revision"),
580 N_("rebase onto given branch instead of upstream")),
581 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
582 N_("allow pre-rebase hook to run")),
583 OPT_NEGBIT('q', "quiet", &options.flags,
584 N_("be quiet. implies --no-stat"),
585 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
586 OPT_BIT('v', "verbose", &options.flags,
587 N_("display a diffstat of what changed upstream"),
588 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
589 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
590 N_("do not show diffstat of what changed upstream"),
591 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
592 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
593 N_("passed to 'git apply'")),
594 OPT_BOOL(0, "signoff", &options.signoff,
595 N_("add a Signed-off-by: line to each commit")),
596 OPT_BOOL(0, "committer-date-is-author-date",
597 &committer_date_is_author_date,
598 N_("passed to 'git am'")),
599 OPT_BOOL(0, "ignore-date", &ignore_date,
600 N_("passed to 'git am'")),
601 OPT_BIT('f', "force-rebase", &options.flags,
602 N_("cherry-pick all commits, even if unchanged"),
603 REBASE_FORCE),
604 OPT_BIT(0, "no-ff", &options.flags,
605 N_("cherry-pick all commits, even if unchanged"),
606 REBASE_FORCE),
607 OPT_CMDMODE(0, "continue", &action, N_("continue"),
608 ACTION_CONTINUE),
609 OPT_CMDMODE(0, "skip", &action,
610 N_("skip current patch and continue"), ACTION_SKIP),
611 OPT_CMDMODE(0, "abort", &action,
612 N_("abort and check out the original branch"),
613 ACTION_ABORT),
614 OPT_CMDMODE(0, "quit", &action,
615 N_("abort but keep HEAD where it is"), ACTION_QUIT),
616 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
617 "during an interactive rebase"), ACTION_EDIT_TODO),
618 OPT_CMDMODE(0, "show-current-patch", &action,
619 N_("show the patch file being applied or merged"),
620 ACTION_SHOW_CURRENT_PATCH),
621 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
622 N_("use merging strategies to rebase"),
623 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
624 parse_opt_merge },
625 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
626 N_("let the user edit the list of commits to rebase"),
627 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
628 parse_opt_interactive },
629 OPT_SET_INT('p', "preserve-merges", &options.type,
630 N_("try to recreate merges instead of ignoring "
631 "them"), REBASE_PRESERVE_MERGES),
632 OPT_BOOL(0, "rerere-autoupdate",
633 &options.allow_rerere_autoupdate,
634 N_("allow rerere to update index with resolved "
635 "conflict")),
636 OPT_BOOL('k', "keep-empty", &options.keep_empty,
637 N_("preserve empty commits during rebase")),
638 OPT_BOOL(0, "autosquash", &options.autosquash,
639 N_("move commits that begin with "
640 "squash!/fixup! under -i")),
641 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
642 N_("GPG-sign commits"),
643 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
644 OPT_END(),
645 };
646
647 /*
648 * NEEDSWORK: Once the builtin rebase has been tested enough
649 * and git-legacy-rebase.sh is retired to contrib/, this preamble
650 * can be removed.
651 */
652
653 if (!use_builtin_rebase()) {
654 const char *path = mkpath("%s/git-legacy-rebase",
655 git_exec_path());
656
657 if (sane_execvp(path, (char **)argv) < 0)
658 die_errno(_("could not exec %s"), path);
659 else
660 BUG("sane_execvp() returned???");
661 }
662
663 if (argc == 2 && !strcmp(argv[1], "-h"))
664 usage_with_options(builtin_rebase_usage,
665 builtin_rebase_options);
666
667 prefix = setup_git_directory();
668 trace_repo_setup(prefix);
669 setup_work_tree();
670
671 git_config(rebase_config, &options);
672
673 strbuf_reset(&buf);
674 strbuf_addf(&buf, "%s/applying", apply_dir());
675 if(file_exists(buf.buf))
676 die(_("It looks like 'git am' is in progress. Cannot rebase."));
677
678 if (is_directory(apply_dir())) {
679 options.type = REBASE_AM;
680 options.state_dir = apply_dir();
681 } else if (is_directory(merge_dir())) {
682 strbuf_reset(&buf);
683 strbuf_addf(&buf, "%s/rewritten", merge_dir());
684 if (is_directory(buf.buf)) {
685 options.type = REBASE_PRESERVE_MERGES;
686 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
687 } else {
688 strbuf_reset(&buf);
689 strbuf_addf(&buf, "%s/interactive", merge_dir());
690 if(file_exists(buf.buf)) {
691 options.type = REBASE_INTERACTIVE;
692 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
693 } else
694 options.type = REBASE_MERGE;
695 }
696 options.state_dir = merge_dir();
697 }
698
699 if (options.type != REBASE_UNSPECIFIED)
700 in_progress = 1;
701
702 total_argc = argc;
703 argc = parse_options(argc, argv, prefix,
704 builtin_rebase_options,
705 builtin_rebase_usage, 0);
706
707 if (action != NO_ACTION && total_argc != 2) {
708 usage_with_options(builtin_rebase_usage,
709 builtin_rebase_options);
710 }
711
712 if (argc > 2)
713 usage_with_options(builtin_rebase_usage,
714 builtin_rebase_options);
715
716 if (action != NO_ACTION && !in_progress)
717 die(_("No rebase in progress?"));
718
719 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
720 die(_("The --edit-todo action can only be used during "
721 "interactive rebase."));
722
723 switch (action) {
724 case ACTION_CONTINUE: {
725 struct object_id head;
726 struct lock_file lock_file = LOCK_INIT;
727 int fd;
728
729 options.action = "continue";
730
731 /* Sanity check */
732 if (get_oid("HEAD", &head))
733 die(_("Cannot read HEAD"));
734
735 fd = hold_locked_index(&lock_file, 0);
736 if (read_index(the_repository->index) < 0)
737 die(_("could not read index"));
738 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
739 NULL);
740 if (0 <= fd)
741 update_index_if_able(the_repository->index,
742 &lock_file);
743 rollback_lock_file(&lock_file);
744
745 if (has_unstaged_changes(1)) {
746 puts(_("You must edit all merge conflicts and then\n"
747 "mark them as resolved using git add"));
748 exit(1);
749 }
750 if (read_basic_state(&options))
751 exit(1);
752 goto run_rebase;
753 }
754 case ACTION_SKIP: {
755 struct string_list merge_rr = STRING_LIST_INIT_DUP;
756
757 options.action = "skip";
758
759 rerere_clear(&merge_rr);
760 string_list_clear(&merge_rr, 1);
761
762 if (reset_head(NULL, "reset", NULL, 0) < 0)
763 die(_("could not discard worktree changes"));
764 if (read_basic_state(&options))
765 exit(1);
766 goto run_rebase;
767 }
768 case ACTION_ABORT: {
769 struct string_list merge_rr = STRING_LIST_INIT_DUP;
770 options.action = "abort";
771
772 rerere_clear(&merge_rr);
773 string_list_clear(&merge_rr, 1);
774
775 if (read_basic_state(&options))
776 exit(1);
777 if (reset_head(&options.orig_head, "reset",
778 options.head_name, 0) < 0)
779 die(_("could not move back to %s"),
780 oid_to_hex(&options.orig_head));
781 ret = finish_rebase(&options);
782 goto cleanup;
783 }
784 case ACTION_QUIT: {
785 strbuf_reset(&buf);
786 strbuf_addstr(&buf, options.state_dir);
787 ret = !!remove_dir_recursively(&buf, 0);
788 if (ret)
789 die(_("could not remove '%s'"), options.state_dir);
790 goto cleanup;
791 }
792 case ACTION_EDIT_TODO:
793 options.action = "edit-todo";
794 options.dont_finish_rebase = 1;
795 goto run_rebase;
796 case ACTION_SHOW_CURRENT_PATCH:
797 options.action = "show-current-patch";
798 options.dont_finish_rebase = 1;
799 goto run_rebase;
800 case NO_ACTION:
801 break;
802 default:
803 BUG("action: %d", action);
804 }
805
806 /* Make sure no rebase is in progress */
807 if (in_progress) {
808 const char *last_slash = strrchr(options.state_dir, '/');
809 const char *state_dir_base =
810 last_slash ? last_slash + 1 : options.state_dir;
811 const char *cmd_live_rebase =
812 "git rebase (--continue | --abort | --skip)";
813 strbuf_reset(&buf);
814 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
815 die(_("It seems that there is already a %s directory, and\n"
816 "I wonder if you are in the middle of another rebase. "
817 "If that is the\n"
818 "case, please try\n\t%s\n"
819 "If that is not the case, please\n\t%s\n"
820 "and run me again. I am stopping in case you still "
821 "have something\n"
822 "valuable there.\n"),
823 state_dir_base, cmd_live_rebase, buf.buf);
824 }
825
826 if (!(options.flags & REBASE_NO_QUIET))
827 strbuf_addstr(&options.git_am_opt, " -q");
828
829 if (committer_date_is_author_date) {
830 strbuf_addstr(&options.git_am_opt,
831 " --committer-date-is-author-date");
832 options.flags |= REBASE_FORCE;
833 }
834
835 if (ignore_whitespace)
836 strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
837
838 if (ignore_date) {
839 strbuf_addstr(&options.git_am_opt, " --ignore-date");
840 options.flags |= REBASE_FORCE;
841 }
842
843 if (options.keep_empty)
844 imply_interactive(&options, "--keep-empty");
845
846 if (gpg_sign) {
847 free(options.gpg_sign_opt);
848 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
849 }
850
851 switch (options.type) {
852 case REBASE_MERGE:
853 case REBASE_INTERACTIVE:
854 case REBASE_PRESERVE_MERGES:
855 options.state_dir = merge_dir();
856 break;
857 case REBASE_AM:
858 options.state_dir = apply_dir();
859 break;
860 default:
861 /* the default rebase backend is `--am` */
862 options.type = REBASE_AM;
863 options.state_dir = apply_dir();
864 break;
865 }
866
867 if (options.signoff) {
868 if (options.type == REBASE_PRESERVE_MERGES)
869 die("cannot combine '--signoff' with "
870 "'--preserve-merges'");
871 strbuf_addstr(&options.git_am_opt, " --signoff");
872 options.flags |= REBASE_FORCE;
873 }
874
875 if (!options.root) {
876 if (argc < 1)
877 die("TODO: handle @{upstream}");
878 else {
879 options.upstream_name = argv[0];
880 argc--;
881 argv++;
882 if (!strcmp(options.upstream_name, "-"))
883 options.upstream_name = "@{-1}";
884 }
885 options.upstream = peel_committish(options.upstream_name);
886 if (!options.upstream)
887 die(_("invalid upstream '%s'"), options.upstream_name);
888 options.upstream_arg = options.upstream_name;
889 } else
890 die("TODO: upstream for --root");
891
892 /* Make sure the branch to rebase onto is valid. */
893 if (!options.onto_name)
894 options.onto_name = options.upstream_name;
895 if (strstr(options.onto_name, "...")) {
896 if (get_oid_mb(options.onto_name, &merge_base) < 0)
897 die(_("'%s': need exactly one merge base"),
898 options.onto_name);
899 options.onto = lookup_commit_or_die(&merge_base,
900 options.onto_name);
901 } else {
902 options.onto = peel_committish(options.onto_name);
903 if (!options.onto)
904 die(_("Does not point to a valid commit '%s'"),
905 options.onto_name);
906 }
907
908 /*
909 * If the branch to rebase is given, that is the branch we will rebase
910 * branch_name -- branch/commit being rebased, or
911 * HEAD (already detached)
912 * orig_head -- commit object name of tip of the branch before rebasing
913 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
914 */
915 if (argc == 1) {
916 /* Is it "rebase other branchname" or "rebase other commit"? */
917 branch_name = argv[0];
918 options.switch_to = argv[0];
919
920 /* Is it a local branch? */
921 strbuf_reset(&buf);
922 strbuf_addf(&buf, "refs/heads/%s", branch_name);
923 if (!read_ref(buf.buf, &options.orig_head))
924 options.head_name = xstrdup(buf.buf);
925 /* If not is it a valid ref (branch or commit)? */
926 else if (!get_oid(branch_name, &options.orig_head))
927 options.head_name = NULL;
928 else
929 die(_("fatal: no such branch/commit '%s'"),
930 branch_name);
931 } else if (argc == 0) {
932 /* Do not need to switch branches, we are already on it. */
933 options.head_name =
934 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
935 &flags));
936 if (!options.head_name)
937 die(_("No such ref: %s"), "HEAD");
938 if (flags & REF_ISSYMREF) {
939 if (!skip_prefix(options.head_name,
940 "refs/heads/", &branch_name))
941 branch_name = options.head_name;
942
943 } else {
944 free(options.head_name);
945 options.head_name = NULL;
946 branch_name = "HEAD";
947 }
948 if (get_oid("HEAD", &options.orig_head))
949 die(_("Could not resolve HEAD to a revision"));
950 } else
951 BUG("unexpected number of arguments left to parse");
952
953 if (read_index(the_repository->index) < 0)
954 die(_("could not read index"));
955
956 if (require_clean_work_tree("rebase",
957 _("Please commit or stash them."), 1, 1)) {
958 ret = 1;
959 goto cleanup;
960 }
961
962 /*
963 * Now we are rebasing commits upstream..orig_head (or with --root,
964 * everything leading up to orig_head) on top of onto.
965 */
966
967 /*
968 * Check if we are already based on onto with linear history,
969 * but this should be done only when upstream and onto are the same
970 * and if this is not an interactive rebase.
971 */
972 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
973 !is_interactive(&options) && !options.restrict_revision &&
974 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
975 int flag;
976
977 if (!(options.flags & REBASE_FORCE)) {
978 /* Lazily switch to the target branch if needed... */
979 if (options.switch_to) {
980 struct object_id oid;
981
982 if (get_oid(options.switch_to, &oid) < 0) {
983 ret = !!error(_("could not parse '%s'"),
984 options.switch_to);
985 goto cleanup;
986 }
987
988 strbuf_reset(&buf);
989 strbuf_addf(&buf, "rebase: checkout %s",
990 options.switch_to);
991 if (reset_head(&oid, "checkout",
992 options.head_name, 0) < 0) {
993 ret = !!error(_("could not switch to "
994 "%s"),
995 options.switch_to);
996 goto cleanup;
997 }
998 }
999
1000 if (!(options.flags & REBASE_NO_QUIET))
1001 ; /* be quiet */
1002 else if (!strcmp(branch_name, "HEAD") &&
1003 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1004 puts(_("HEAD is up to date."));
1005 else
1006 printf(_("Current branch %s is up to date.\n"),
1007 branch_name);
1008 ret = !!finish_rebase(&options);
1009 goto cleanup;
1010 } else if (!(options.flags & REBASE_NO_QUIET))
1011 ; /* be quiet */
1012 else if (!strcmp(branch_name, "HEAD") &&
1013 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1014 puts(_("HEAD is up to date, rebase forced."));
1015 else
1016 printf(_("Current branch %s is up to date, rebase "
1017 "forced.\n"), branch_name);
1018 }
1019
1020 /* If a hook exists, give it a chance to interrupt*/
1021 if (!ok_to_skip_pre_rebase &&
1022 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1023 argc ? argv[0] : NULL, NULL))
1024 die(_("The pre-rebase hook refused to rebase."));
1025
1026 if (options.flags & REBASE_DIFFSTAT) {
1027 struct diff_options opts;
1028
1029 if (options.flags & REBASE_VERBOSE)
1030 printf(_("Changes from %s to %s:\n"),
1031 oid_to_hex(&merge_base),
1032 oid_to_hex(&options.onto->object.oid));
1033
1034 /* We want color (if set), but no pager */
1035 diff_setup(&opts);
1036 opts.stat_width = -1; /* use full terminal width */
1037 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1038 opts.output_format |=
1039 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1040 opts.detect_rename = DIFF_DETECT_RENAME;
1041 diff_setup_done(&opts);
1042 diff_tree_oid(&merge_base, &options.onto->object.oid,
1043 "", &opts);
1044 diffcore_std(&opts);
1045 diff_flush(&opts);
1046 }
1047
1048 if (is_interactive(&options))
1049 goto run_rebase;
1050
1051 /* Detach HEAD and reset the tree */
1052 if (options.flags & REBASE_NO_QUIET)
1053 printf(_("First, rewinding head to replay your work on top of "
1054 "it...\n"));
1055
1056 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1057 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
1058 die(_("Could not detach HEAD"));
1059 strbuf_release(&msg);
1060
1061 strbuf_addf(&revisions, "%s..%s",
1062 options.root ? oid_to_hex(&options.onto->object.oid) :
1063 (options.restrict_revision ?
1064 oid_to_hex(&options.restrict_revision->object.oid) :
1065 oid_to_hex(&options.upstream->object.oid)),
1066 oid_to_hex(&options.orig_head));
1067
1068 options.revisions = revisions.buf;
1069
1070run_rebase:
1071 ret = !!run_specific_rebase(&options);
1072
1073cleanup:
1074 strbuf_release(&revisions);
1075 free(options.head_name);
1076 free(options.gpg_sign_opt);
1077 return ret;
1078}