]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/rebase.c
builtin rebase: support `-C` and `--whitespace=<type>`
[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
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;
06e4775a 77 const char *upstream_arg;
ac7f467f
PK
78 char *head_name;
79 struct object_id orig_head;
80 struct commit *onto;
81 const char *onto_name;
82 const char *revisions;
e65123a7 83 const char *switch_to;
ac7f467f
PK
84 int root;
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
PK
93 } flags;
94 struct strbuf git_am_opt;
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;
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
f9573628
PK
222 strbuf_release(&buf);
223
224 return 0;
225}
226
ac7f467f
PK
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);
f9573628
PK
280 add_var(&script_snippet, "upstream", opts->upstream ?
281 oid_to_hex(&opts->upstream->object.oid) : NULL);
d4c569f8
PK
282 add_var(&script_snippet, "head_name",
283 opts->head_name ? opts->head_name : "detached HEAD");
ac7f467f 284 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
f9573628
PK
285 add_var(&script_snippet, "onto", opts->onto ?
286 oid_to_hex(&opts->onto->object.oid) : NULL);
ac7f467f
PK
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);
b4c8eb02
PK
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);
bff014da
PK
294 add_var(&script_snippet, "verbose",
295 opts->flags & REBASE_VERBOSE ? "t" : "");
296 add_var(&script_snippet, "diffstat",
297 opts->flags & REBASE_DIFFSTAT ? "t" : "");
1ed9c14f
PK
298 add_var(&script_snippet, "force_rebase",
299 opts->flags & REBASE_FORCE ? "t" : "");
e65123a7
PK
300 if (opts->switch_to)
301 add_var(&script_snippet, "switch_to", opts->switch_to);
f9573628 302 add_var(&script_snippet, "action", opts->action ? opts->action : "");
73d51ed0 303 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
ead98c11
PK
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");
002ee2fe 308 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
051910a9 309 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
12026a41 310 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
ac7f467f
PK
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
d4c569f8
PK
377 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
378 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
379
ac7f467f
PK
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
bff014da
PK
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
051910a9
PK
471 if (!strcmp(var, "rebase.autosquash")) {
472 opts->autosquash = git_config_bool(var, value);
473 return 0;
474 }
475
12026a41
PK
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
bff014da
PK
483 return git_default_config(var, value, data);
484}
485
9a48a615
PK
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
361badd3
PK
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
55071ea2
PK
549int cmd_rebase(int argc, const char **argv, const char *prefix)
550{
ac7f467f
PK
551 struct rebase_options options = {
552 .type = REBASE_UNSPECIFIED,
b4c8eb02
PK
553 .flags = REBASE_NO_QUIET,
554 .git_am_opt = STRBUF_INIT,
ead98c11 555 .allow_rerere_autoupdate = -1,
ac7f467f
PK
556 };
557 const char *branch_name;
f9573628 558 int ret, flags, total_argc, in_progress = 0;
06e4775a 559 int ok_to_skip_pre_rebase = 0;
ac7f467f
PK
560 struct strbuf msg = STRBUF_INIT;
561 struct strbuf revisions = STRBUF_INIT;
c54dacb5 562 struct strbuf buf = STRBUF_INIT;
075bc852 563 struct object_id merge_base;
f9573628
PK
564 enum {
565 NO_ACTION,
566 ACTION_CONTINUE,
122420c2 567 ACTION_SKIP,
5e5d9619 568 ACTION_ABORT,
5a614945 569 ACTION_QUIT,
51e9ea6d
PK
570 ACTION_EDIT_TODO,
571 ACTION_SHOW_CURRENT_PATCH,
f9573628 572 } action = NO_ACTION;
38dbcef2 573 int committer_date_is_author_date = 0;
53f9e5be 574 int ignore_date = 0;
99d8cc7c 575 int ignore_whitespace = 0;
12026a41 576 const char *gpg_sign = NULL;
7998dbe1
PK
577 int opt_c = -1;
578 struct string_list whitespace = STRING_LIST_INIT_NODUP;
f28d40d3
PK
579 struct option builtin_rebase_options[] = {
580 OPT_STRING(0, "onto", &options.onto_name,
581 N_("revision"),
582 N_("rebase onto given branch instead of upstream")),
06e4775a
PK
583 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
584 N_("allow pre-rebase hook to run")),
b4c8eb02
PK
585 OPT_NEGBIT('q', "quiet", &options.flags,
586 N_("be quiet. implies --no-stat"),
bff014da
PK
587 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
588 OPT_BIT('v', "verbose", &options.flags,
589 N_("display a diffstat of what changed upstream"),
590 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
591 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
592 N_("do not show diffstat of what changed upstream"),
593 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
99d8cc7c
PK
594 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
595 N_("passed to 'git apply'")),
73d51ed0
PK
596 OPT_BOOL(0, "signoff", &options.signoff,
597 N_("add a Signed-off-by: line to each commit")),
38dbcef2
PK
598 OPT_BOOL(0, "committer-date-is-author-date",
599 &committer_date_is_author_date,
600 N_("passed to 'git am'")),
53f9e5be
PK
601 OPT_BOOL(0, "ignore-date", &ignore_date,
602 N_("passed to 'git am'")),
1ed9c14f
PK
603 OPT_BIT('f', "force-rebase", &options.flags,
604 N_("cherry-pick all commits, even if unchanged"),
605 REBASE_FORCE),
606 OPT_BIT(0, "no-ff", &options.flags,
607 N_("cherry-pick all commits, even if unchanged"),
608 REBASE_FORCE),
f9573628
PK
609 OPT_CMDMODE(0, "continue", &action, N_("continue"),
610 ACTION_CONTINUE),
122420c2
PK
611 OPT_CMDMODE(0, "skip", &action,
612 N_("skip current patch and continue"), ACTION_SKIP),
5e5d9619
PK
613 OPT_CMDMODE(0, "abort", &action,
614 N_("abort and check out the original branch"),
615 ACTION_ABORT),
5a614945
PK
616 OPT_CMDMODE(0, "quit", &action,
617 N_("abort but keep HEAD where it is"), ACTION_QUIT),
51e9ea6d
PK
618 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
619 "during an interactive rebase"), ACTION_EDIT_TODO),
620 OPT_CMDMODE(0, "show-current-patch", &action,
621 N_("show the patch file being applied or merged"),
622 ACTION_SHOW_CURRENT_PATCH),
361badd3
PK
623 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
624 N_("use merging strategies to rebase"),
625 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
626 parse_opt_merge },
627 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
628 N_("let the user edit the list of commits to rebase"),
629 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
630 parse_opt_interactive },
631 OPT_SET_INT('p', "preserve-merges", &options.type,
632 N_("try to recreate merges instead of ignoring "
633 "them"), REBASE_PRESERVE_MERGES),
ead98c11
PK
634 OPT_BOOL(0, "rerere-autoupdate",
635 &options.allow_rerere_autoupdate,
636 N_("allow rerere to update index with resolved "
637 "conflict")),
002ee2fe
PK
638 OPT_BOOL('k', "keep-empty", &options.keep_empty,
639 N_("preserve empty commits during rebase")),
051910a9
PK
640 OPT_BOOL(0, "autosquash", &options.autosquash,
641 N_("move commits that begin with "
642 "squash!/fixup! under -i")),
12026a41
PK
643 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
644 N_("GPG-sign commits"),
645 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
7998dbe1
PK
646 OPT_STRING_LIST(0, "whitespace", &whitespace,
647 N_("whitespace"), N_("passed to 'git apply'")),
648 OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"),
649 REBASE_AM),
f28d40d3
PK
650 OPT_END(),
651 };
ac7f467f 652
55071ea2
PK
653 /*
654 * NEEDSWORK: Once the builtin rebase has been tested enough
655 * and git-legacy-rebase.sh is retired to contrib/, this preamble
656 * can be removed.
657 */
658
659 if (!use_builtin_rebase()) {
660 const char *path = mkpath("%s/git-legacy-rebase",
661 git_exec_path());
662
663 if (sane_execvp(path, (char **)argv) < 0)
664 die_errno(_("could not exec %s"), path);
665 else
666 BUG("sane_execvp() returned???");
667 }
668
f28d40d3
PK
669 if (argc == 2 && !strcmp(argv[1], "-h"))
670 usage_with_options(builtin_rebase_usage,
671 builtin_rebase_options);
672
55071ea2
PK
673 prefix = setup_git_directory();
674 trace_repo_setup(prefix);
675 setup_work_tree();
676
bff014da
PK
677 git_config(rebase_config, &options);
678
0eabf4b9
PK
679 strbuf_reset(&buf);
680 strbuf_addf(&buf, "%s/applying", apply_dir());
681 if(file_exists(buf.buf))
682 die(_("It looks like 'git am' is in progress. Cannot rebase."));
683
c54dacb5
PK
684 if (is_directory(apply_dir())) {
685 options.type = REBASE_AM;
686 options.state_dir = apply_dir();
687 } else if (is_directory(merge_dir())) {
688 strbuf_reset(&buf);
689 strbuf_addf(&buf, "%s/rewritten", merge_dir());
690 if (is_directory(buf.buf)) {
691 options.type = REBASE_PRESERVE_MERGES;
692 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
693 } else {
694 strbuf_reset(&buf);
695 strbuf_addf(&buf, "%s/interactive", merge_dir());
696 if(file_exists(buf.buf)) {
697 options.type = REBASE_INTERACTIVE;
698 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
699 } else
700 options.type = REBASE_MERGE;
701 }
702 options.state_dir = merge_dir();
703 }
704
705 if (options.type != REBASE_UNSPECIFIED)
706 in_progress = 1;
707
f9573628 708 total_argc = argc;
f28d40d3
PK
709 argc = parse_options(argc, argv, prefix,
710 builtin_rebase_options,
711 builtin_rebase_usage, 0);
712
f9573628
PK
713 if (action != NO_ACTION && total_argc != 2) {
714 usage_with_options(builtin_rebase_usage,
715 builtin_rebase_options);
716 }
717
f28d40d3
PK
718 if (argc > 2)
719 usage_with_options(builtin_rebase_usage,
720 builtin_rebase_options);
ac7f467f 721
d732a570
PK
722 if (action != NO_ACTION && !in_progress)
723 die(_("No rebase in progress?"));
724
51e9ea6d
PK
725 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
726 die(_("The --edit-todo action can only be used during "
727 "interactive rebase."));
728
f9573628
PK
729 switch (action) {
730 case ACTION_CONTINUE: {
731 struct object_id head;
732 struct lock_file lock_file = LOCK_INIT;
733 int fd;
734
735 options.action = "continue";
736
737 /* Sanity check */
738 if (get_oid("HEAD", &head))
739 die(_("Cannot read HEAD"));
740
741 fd = hold_locked_index(&lock_file, 0);
742 if (read_index(the_repository->index) < 0)
743 die(_("could not read index"));
744 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
745 NULL);
746 if (0 <= fd)
747 update_index_if_able(the_repository->index,
748 &lock_file);
749 rollback_lock_file(&lock_file);
750
751 if (has_unstaged_changes(1)) {
752 puts(_("You must edit all merge conflicts and then\n"
753 "mark them as resolved using git add"));
754 exit(1);
755 }
756 if (read_basic_state(&options))
757 exit(1);
758 goto run_rebase;
759 }
122420c2
PK
760 case ACTION_SKIP: {
761 struct string_list merge_rr = STRING_LIST_INIT_DUP;
762
763 options.action = "skip";
764
765 rerere_clear(&merge_rr);
766 string_list_clear(&merge_rr, 1);
767
768 if (reset_head(NULL, "reset", NULL, 0) < 0)
769 die(_("could not discard worktree changes"));
770 if (read_basic_state(&options))
771 exit(1);
772 goto run_rebase;
773 }
5e5d9619
PK
774 case ACTION_ABORT: {
775 struct string_list merge_rr = STRING_LIST_INIT_DUP;
776 options.action = "abort";
777
778 rerere_clear(&merge_rr);
779 string_list_clear(&merge_rr, 1);
780
781 if (read_basic_state(&options))
782 exit(1);
783 if (reset_head(&options.orig_head, "reset",
784 options.head_name, 0) < 0)
785 die(_("could not move back to %s"),
786 oid_to_hex(&options.orig_head));
787 ret = finish_rebase(&options);
788 goto cleanup;
789 }
5a614945
PK
790 case ACTION_QUIT: {
791 strbuf_reset(&buf);
792 strbuf_addstr(&buf, options.state_dir);
793 ret = !!remove_dir_recursively(&buf, 0);
794 if (ret)
795 die(_("could not remove '%s'"), options.state_dir);
796 goto cleanup;
797 }
51e9ea6d
PK
798 case ACTION_EDIT_TODO:
799 options.action = "edit-todo";
800 options.dont_finish_rebase = 1;
801 goto run_rebase;
802 case ACTION_SHOW_CURRENT_PATCH:
803 options.action = "show-current-patch";
804 options.dont_finish_rebase = 1;
805 goto run_rebase;
806 case NO_ACTION:
807 break;
f9573628 808 default:
51e9ea6d 809 BUG("action: %d", action);
f9573628
PK
810 }
811
c54dacb5
PK
812 /* Make sure no rebase is in progress */
813 if (in_progress) {
814 const char *last_slash = strrchr(options.state_dir, '/');
815 const char *state_dir_base =
816 last_slash ? last_slash + 1 : options.state_dir;
817 const char *cmd_live_rebase =
818 "git rebase (--continue | --abort | --skip)";
819 strbuf_reset(&buf);
820 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
821 die(_("It seems that there is already a %s directory, and\n"
822 "I wonder if you are in the middle of another rebase. "
823 "If that is the\n"
824 "case, please try\n\t%s\n"
825 "If that is not the case, please\n\t%s\n"
826 "and run me again. I am stopping in case you still "
827 "have something\n"
828 "valuable there.\n"),
829 state_dir_base, cmd_live_rebase, buf.buf);
830 }
831
b4c8eb02
PK
832 if (!(options.flags & REBASE_NO_QUIET))
833 strbuf_addstr(&options.git_am_opt, " -q");
834
38dbcef2
PK
835 if (committer_date_is_author_date) {
836 strbuf_addstr(&options.git_am_opt,
837 " --committer-date-is-author-date");
838 options.flags |= REBASE_FORCE;
839 }
840
99d8cc7c
PK
841 if (ignore_whitespace)
842 strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
843
53f9e5be
PK
844 if (ignore_date) {
845 strbuf_addstr(&options.git_am_opt, " --ignore-date");
846 options.flags |= REBASE_FORCE;
847 }
848
002ee2fe
PK
849 if (options.keep_empty)
850 imply_interactive(&options, "--keep-empty");
851
12026a41
PK
852 if (gpg_sign) {
853 free(options.gpg_sign_opt);
854 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
855 }
856
7998dbe1
PK
857 if (opt_c >= 0)
858 strbuf_addf(&options.git_am_opt, " -C%d", opt_c);
859
860 if (whitespace.nr) {
861 int i;
862
863 for (i = 0; i < whitespace.nr; i++) {
864 const char *item = whitespace.items[i].string;
865
866 strbuf_addf(&options.git_am_opt, " --whitespace=%s",
867 item);
868
869 if ((!strcmp(item, "fix")) || (!strcmp(item, "strip")))
870 options.flags |= REBASE_FORCE;
871 }
872 }
873
ac7f467f
PK
874 switch (options.type) {
875 case REBASE_MERGE:
876 case REBASE_INTERACTIVE:
877 case REBASE_PRESERVE_MERGES:
878 options.state_dir = merge_dir();
879 break;
880 case REBASE_AM:
881 options.state_dir = apply_dir();
882 break;
883 default:
884 /* the default rebase backend is `--am` */
885 options.type = REBASE_AM;
886 options.state_dir = apply_dir();
887 break;
888 }
889
73d51ed0
PK
890 if (options.signoff) {
891 if (options.type == REBASE_PRESERVE_MERGES)
892 die("cannot combine '--signoff' with "
893 "'--preserve-merges'");
894 strbuf_addstr(&options.git_am_opt, " --signoff");
895 options.flags |= REBASE_FORCE;
896 }
897
ac7f467f 898 if (!options.root) {
f28d40d3 899 if (argc < 1)
ac7f467f
PK
900 die("TODO: handle @{upstream}");
901 else {
f28d40d3 902 options.upstream_name = argv[0];
ac7f467f
PK
903 argc--;
904 argv++;
905 if (!strcmp(options.upstream_name, "-"))
906 options.upstream_name = "@{-1}";
907 }
908 options.upstream = peel_committish(options.upstream_name);
909 if (!options.upstream)
910 die(_("invalid upstream '%s'"), options.upstream_name);
06e4775a 911 options.upstream_arg = options.upstream_name;
ac7f467f
PK
912 } else
913 die("TODO: upstream for --root");
914
915 /* Make sure the branch to rebase onto is valid. */
916 if (!options.onto_name)
917 options.onto_name = options.upstream_name;
918 if (strstr(options.onto_name, "...")) {
075bc852
PK
919 if (get_oid_mb(options.onto_name, &merge_base) < 0)
920 die(_("'%s': need exactly one merge base"),
921 options.onto_name);
922 options.onto = lookup_commit_or_die(&merge_base,
923 options.onto_name);
ac7f467f
PK
924 } else {
925 options.onto = peel_committish(options.onto_name);
926 if (!options.onto)
927 die(_("Does not point to a valid commit '%s'"),
928 options.onto_name);
929 }
930
931 /*
932 * If the branch to rebase is given, that is the branch we will rebase
933 * branch_name -- branch/commit being rebased, or
934 * HEAD (already detached)
935 * orig_head -- commit object name of tip of the branch before rebasing
d4c569f8 936 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
ac7f467f 937 */
e65123a7
PK
938 if (argc == 1) {
939 /* Is it "rebase other branchname" or "rebase other commit"? */
940 branch_name = argv[0];
941 options.switch_to = argv[0];
942
943 /* Is it a local branch? */
944 strbuf_reset(&buf);
945 strbuf_addf(&buf, "refs/heads/%s", branch_name);
946 if (!read_ref(buf.buf, &options.orig_head))
947 options.head_name = xstrdup(buf.buf);
948 /* If not is it a valid ref (branch or commit)? */
949 else if (!get_oid(branch_name, &options.orig_head))
950 options.head_name = NULL;
951 else
952 die(_("fatal: no such branch/commit '%s'"),
953 branch_name);
954 } else if (argc == 0) {
ac7f467f
PK
955 /* Do not need to switch branches, we are already on it. */
956 options.head_name =
957 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
958 &flags));
959 if (!options.head_name)
960 die(_("No such ref: %s"), "HEAD");
961 if (flags & REF_ISSYMREF) {
962 if (!skip_prefix(options.head_name,
963 "refs/heads/", &branch_name))
964 branch_name = options.head_name;
965
966 } else {
d4c569f8
PK
967 free(options.head_name);
968 options.head_name = NULL;
ac7f467f
PK
969 branch_name = "HEAD";
970 }
971 if (get_oid("HEAD", &options.orig_head))
972 die(_("Could not resolve HEAD to a revision"));
e65123a7
PK
973 } else
974 BUG("unexpected number of arguments left to parse");
ac7f467f 975
e0333e5c
PK
976 if (read_index(the_repository->index) < 0)
977 die(_("could not read index"));
978
979 if (require_clean_work_tree("rebase",
980 _("Please commit or stash them."), 1, 1)) {
981 ret = 1;
982 goto cleanup;
983 }
984
9a48a615
PK
985 /*
986 * Now we are rebasing commits upstream..orig_head (or with --root,
987 * everything leading up to orig_head) on top of onto.
988 */
989
990 /*
991 * Check if we are already based on onto with linear history,
992 * but this should be done only when upstream and onto are the same
993 * and if this is not an interactive rebase.
994 */
995 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
996 !is_interactive(&options) && !options.restrict_revision &&
997 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
998 int flag;
999
1ed9c14f 1000 if (!(options.flags & REBASE_FORCE)) {
e65123a7
PK
1001 /* Lazily switch to the target branch if needed... */
1002 if (options.switch_to) {
1003 struct object_id oid;
1004
1005 if (get_oid(options.switch_to, &oid) < 0) {
1006 ret = !!error(_("could not parse '%s'"),
1007 options.switch_to);
1008 goto cleanup;
1009 }
1010
1011 strbuf_reset(&buf);
1012 strbuf_addf(&buf, "rebase: checkout %s",
1013 options.switch_to);
1014 if (reset_head(&oid, "checkout",
1015 options.head_name, 0) < 0) {
1016 ret = !!error(_("could not switch to "
1017 "%s"),
1018 options.switch_to);
1019 goto cleanup;
1020 }
1021 }
1022
1ed9c14f
PK
1023 if (!(options.flags & REBASE_NO_QUIET))
1024 ; /* be quiet */
1025 else if (!strcmp(branch_name, "HEAD") &&
1026 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1027 puts(_("HEAD is up to date."));
1028 else
1029 printf(_("Current branch %s is up to date.\n"),
1030 branch_name);
1031 ret = !!finish_rebase(&options);
1032 goto cleanup;
1033 } else if (!(options.flags & REBASE_NO_QUIET))
9a48a615
PK
1034 ; /* be quiet */
1035 else if (!strcmp(branch_name, "HEAD") &&
1036 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1037 puts(_("HEAD is up to date, rebase forced."));
1038 else
1039 printf(_("Current branch %s is up to date, rebase "
1040 "forced.\n"), branch_name);
1041 }
1042
06e4775a
PK
1043 /* If a hook exists, give it a chance to interrupt*/
1044 if (!ok_to_skip_pre_rebase &&
1045 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1046 argc ? argv[0] : NULL, NULL))
1047 die(_("The pre-rebase hook refused to rebase."));
1048
bff014da
PK
1049 if (options.flags & REBASE_DIFFSTAT) {
1050 struct diff_options opts;
1051
1052 if (options.flags & REBASE_VERBOSE)
1053 printf(_("Changes from %s to %s:\n"),
1054 oid_to_hex(&merge_base),
1055 oid_to_hex(&options.onto->object.oid));
1056
1057 /* We want color (if set), but no pager */
1058 diff_setup(&opts);
1059 opts.stat_width = -1; /* use full terminal width */
1060 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1061 opts.output_format |=
1062 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1063 opts.detect_rename = DIFF_DETECT_RENAME;
1064 diff_setup_done(&opts);
1065 diff_tree_oid(&merge_base, &options.onto->object.oid,
1066 "", &opts);
1067 diffcore_std(&opts);
1068 diff_flush(&opts);
1069 }
1070
361badd3
PK
1071 if (is_interactive(&options))
1072 goto run_rebase;
1073
bff014da
PK
1074 /* Detach HEAD and reset the tree */
1075 if (options.flags & REBASE_NO_QUIET)
1076 printf(_("First, rewinding head to replay your work on top of "
1077 "it...\n"));
1078
ac7f467f
PK
1079 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1080 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
1081 die(_("Could not detach HEAD"));
1082 strbuf_release(&msg);
1083
1084 strbuf_addf(&revisions, "%s..%s",
1085 options.root ? oid_to_hex(&options.onto->object.oid) :
1086 (options.restrict_revision ?
1087 oid_to_hex(&options.restrict_revision->object.oid) :
1088 oid_to_hex(&options.upstream->object.oid)),
1089 oid_to_hex(&options.orig_head));
1090
1091 options.revisions = revisions.buf;
1092
f9573628 1093run_rebase:
ac7f467f
PK
1094 ret = !!run_specific_rebase(&options);
1095
e0333e5c 1096cleanup:
ac7f467f
PK
1097 strbuf_release(&revisions);
1098 free(options.head_name);
12026a41 1099 free(options.gpg_sign_opt);
ac7f467f 1100 return ret;
55071ea2 1101}