builtin rebase: support the `verbose` and `diffstat` options
[git/git.git] / builtin / rebase.c
CommitLineData
55071ea2
PK
1/*
2 * "git rebase" builtin command
3 *
4 * Copyright (c) 2018 Pratik Karki
5 */
6
7#include "builtin.h"
8#include "run-command.h"
9#include "exec-cmd.h"
10#include "argv-array.h"
11#include "dir.h"
ac7f467f
PK
12#include "packfile.h"
13#include "refs.h"
14#include "quote.h"
15#include "config.h"
16#include "cache-tree.h"
17#include "unpack-trees.h"
18#include "lockfile.h"
f28d40d3 19#include "parse-options.h"
075bc852 20#include "commit.h"
bff014da 21#include "diff.h"
f28d40d3
PK
22
23static char const * const builtin_rebase_usage[] = {
24 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
25 "[<upstream>] [<branch>]"),
26 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
27 "--root [<branch>]"),
28 N_("git rebase --continue | --abort | --skip | --edit-todo"),
29 NULL
30};
ac7f467f
PK
31
32static GIT_PATH_FUNC(apply_dir, "rebase-apply")
33static GIT_PATH_FUNC(merge_dir, "rebase-merge")
34
35enum rebase_type {
36 REBASE_UNSPECIFIED = -1,
37 REBASE_AM,
38 REBASE_MERGE,
39 REBASE_INTERACTIVE,
40 REBASE_PRESERVE_MERGES
41};
55071ea2
PK
42
43static int use_builtin_rebase(void)
44{
45 struct child_process cp = CHILD_PROCESS_INIT;
46 struct strbuf out = STRBUF_INIT;
47 int ret;
48
49 argv_array_pushl(&cp.args,
50 "config", "--bool", "rebase.usebuiltin", NULL);
51 cp.git_cmd = 1;
52 if (capture_command(&cp, &out, 6)) {
53 strbuf_release(&out);
54 return 0;
55 }
56
57 strbuf_trim(&out);
58 ret = !strcmp("true", out.buf);
59 strbuf_release(&out);
60 return ret;
61}
62
ac7f467f
PK
63static int apply_autostash(void)
64{
65 warning("TODO");
66 return 0;
67}
68
69struct rebase_options {
70 enum rebase_type type;
71 const char *state_dir;
72 struct commit *upstream;
73 const char *upstream_name;
06e4775a 74 const char *upstream_arg;
ac7f467f
PK
75 char *head_name;
76 struct object_id orig_head;
77 struct commit *onto;
78 const char *onto_name;
79 const char *revisions;
80 int root;
81 struct commit *restrict_revision;
82 int dont_finish_rebase;
b4c8eb02
PK
83 enum {
84 REBASE_NO_QUIET = 1<<0,
bff014da
PK
85 REBASE_VERBOSE = 1<<1,
86 REBASE_DIFFSTAT = 1<<2,
b4c8eb02
PK
87 } flags;
88 struct strbuf git_am_opt;
ac7f467f
PK
89};
90
91/* Returns the filename prefixed by the state_dir */
92static const char *state_dir_path(const char *filename, struct rebase_options *opts)
93{
94 static struct strbuf path = STRBUF_INIT;
95 static size_t prefix_len;
96
97 if (!prefix_len) {
98 strbuf_addf(&path, "%s/", opts->state_dir);
99 prefix_len = path.len;
100 }
101
102 strbuf_setlen(&path, prefix_len);
103 strbuf_addstr(&path, filename);
104 return path.buf;
105}
106
107static int finish_rebase(struct rebase_options *opts)
108{
109 struct strbuf dir = STRBUF_INIT;
110 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
111
112 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
113 apply_autostash();
114 close_all_packs(the_repository->objects);
115 /*
116 * We ignore errors in 'gc --auto', since the
117 * user should see them.
118 */
119 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
120 strbuf_addstr(&dir, opts->state_dir);
121 remove_dir_recursively(&dir, 0);
122 strbuf_release(&dir);
123
124 return 0;
125}
126
127static struct commit *peel_committish(const char *name)
128{
129 struct object *obj;
130 struct object_id oid;
131
132 if (get_oid(name, &oid))
133 return NULL;
134 obj = parse_object(the_repository, &oid);
135 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
136}
137
138static void add_var(struct strbuf *buf, const char *name, const char *value)
139{
140 if (!value)
141 strbuf_addf(buf, "unset %s; ", name);
142 else {
143 strbuf_addf(buf, "%s=", name);
144 sq_quote_buf(buf, value);
145 strbuf_addstr(buf, "; ");
146 }
147}
148
149static int run_specific_rebase(struct rebase_options *opts)
150{
151 const char *argv[] = { NULL, NULL };
152 struct strbuf script_snippet = STRBUF_INIT;
153 int status;
154 const char *backend, *backend_func;
155
156 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
157 add_var(&script_snippet, "state_dir", opts->state_dir);
158
159 add_var(&script_snippet, "upstream_name", opts->upstream_name);
160 add_var(&script_snippet, "upstream",
161 oid_to_hex(&opts->upstream->object.oid));
162 add_var(&script_snippet, "head_name", opts->head_name);
163 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
164 add_var(&script_snippet, "onto", oid_to_hex(&opts->onto->object.oid));
165 add_var(&script_snippet, "onto_name", opts->onto_name);
166 add_var(&script_snippet, "revisions", opts->revisions);
167 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
168 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
b4c8eb02
PK
169 add_var(&script_snippet, "GIT_QUIET",
170 opts->flags & REBASE_NO_QUIET ? "" : "t");
171 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
bff014da
PK
172 add_var(&script_snippet, "verbose",
173 opts->flags & REBASE_VERBOSE ? "t" : "");
174 add_var(&script_snippet, "diffstat",
175 opts->flags & REBASE_DIFFSTAT ? "t" : "");
ac7f467f
PK
176
177 switch (opts->type) {
178 case REBASE_AM:
179 backend = "git-rebase--am";
180 backend_func = "git_rebase__am";
181 break;
182 case REBASE_INTERACTIVE:
183 backend = "git-rebase--interactive";
184 backend_func = "git_rebase__interactive";
185 break;
186 case REBASE_MERGE:
187 backend = "git-rebase--merge";
188 backend_func = "git_rebase__merge";
189 break;
190 case REBASE_PRESERVE_MERGES:
191 backend = "git-rebase--preserve-merges";
192 backend_func = "git_rebase__preserve_merges";
193 break;
194 default:
195 BUG("Unhandled rebase type %d", opts->type);
196 break;
197 }
198
199 strbuf_addf(&script_snippet,
200 ". git-sh-setup && . git-rebase--common &&"
201 " . %s && %s", backend, backend_func);
202 argv[0] = script_snippet.buf;
203
204 status = run_command_v_opt(argv, RUN_USING_SHELL);
205 if (opts->dont_finish_rebase)
206 ; /* do nothing */
207 else if (status == 0) {
208 if (!file_exists(state_dir_path("stopped-sha", opts)))
209 finish_rebase(opts);
210 } else if (status == 2) {
211 struct strbuf dir = STRBUF_INIT;
212
213 apply_autostash();
214 strbuf_addstr(&dir, opts->state_dir);
215 remove_dir_recursively(&dir, 0);
216 strbuf_release(&dir);
217 die("Nothing to do");
218 }
219
220 strbuf_release(&script_snippet);
221
222 return status ? -1 : 0;
223}
224
225#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
226
227static int reset_head(struct object_id *oid, const char *action,
228 const char *switch_to_branch, int detach_head)
229{
230 struct object_id head_oid;
231 struct tree_desc desc;
232 struct lock_file lock = LOCK_INIT;
233 struct unpack_trees_options unpack_tree_opts;
234 struct tree *tree;
235 const char *reflog_action;
236 struct strbuf msg = STRBUF_INIT;
237 size_t prefix_len;
238 struct object_id *orig = NULL, oid_orig,
239 *old_orig = NULL, oid_old_orig;
240 int ret = 0;
241
242 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
243 return -1;
244
245 if (!oid) {
246 if (get_oid("HEAD", &head_oid)) {
247 rollback_lock_file(&lock);
248 return error(_("could not determine HEAD revision"));
249 }
250 oid = &head_oid;
251 }
252
253 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
254 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
255 unpack_tree_opts.head_idx = 1;
256 unpack_tree_opts.src_index = the_repository->index;
257 unpack_tree_opts.dst_index = the_repository->index;
258 unpack_tree_opts.fn = oneway_merge;
259 unpack_tree_opts.update = 1;
260 unpack_tree_opts.merge = 1;
261 if (!detach_head)
262 unpack_tree_opts.reset = 1;
263
264 if (read_index_unmerged(the_repository->index) < 0) {
265 rollback_lock_file(&lock);
266 return error(_("could not read index"));
267 }
268
269 if (!fill_tree_descriptor(&desc, oid)) {
270 error(_("failed to find tree of %s"), oid_to_hex(oid));
271 rollback_lock_file(&lock);
272 free((void *)desc.buffer);
273 return -1;
274 }
275
276 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
277 rollback_lock_file(&lock);
278 free((void *)desc.buffer);
279 return -1;
280 }
281
282 tree = parse_tree_indirect(oid);
283 prime_cache_tree(the_repository->index, tree);
284
285 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
286 ret = error(_("could not write index"));
287 free((void *)desc.buffer);
288
289 if (ret)
290 return ret;
291
292 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
293 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
294 prefix_len = msg.len;
295
296 if (!get_oid("ORIG_HEAD", &oid_old_orig))
297 old_orig = &oid_old_orig;
298 if (!get_oid("HEAD", &oid_orig)) {
299 orig = &oid_orig;
300 strbuf_addstr(&msg, "updating ORIG_HEAD");
301 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
302 UPDATE_REFS_MSG_ON_ERR);
303 } else if (old_orig)
304 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
305 strbuf_setlen(&msg, prefix_len);
306 strbuf_addstr(&msg, "updating HEAD");
307 if (!switch_to_branch)
308 ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
309 UPDATE_REFS_MSG_ON_ERR);
310 else {
311 ret = create_symref("HEAD", switch_to_branch, msg.buf);
312 if (!ret)
313 ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
314 UPDATE_REFS_MSG_ON_ERR);
315 }
316
317 strbuf_release(&msg);
318 return ret;
319}
320
bff014da
PK
321static int rebase_config(const char *var, const char *value, void *data)
322{
323 struct rebase_options *opts = data;
324
325 if (!strcmp(var, "rebase.stat")) {
326 if (git_config_bool(var, value))
327 opts->flags |= REBASE_DIFFSTAT;
328 else
329 opts->flags &= !REBASE_DIFFSTAT;
330 return 0;
331 }
332
333 return git_default_config(var, value, data);
334}
335
55071ea2
PK
336int cmd_rebase(int argc, const char **argv, const char *prefix)
337{
ac7f467f
PK
338 struct rebase_options options = {
339 .type = REBASE_UNSPECIFIED,
b4c8eb02
PK
340 .flags = REBASE_NO_QUIET,
341 .git_am_opt = STRBUF_INIT,
ac7f467f
PK
342 };
343 const char *branch_name;
344 int ret, flags;
06e4775a 345 int ok_to_skip_pre_rebase = 0;
ac7f467f
PK
346 struct strbuf msg = STRBUF_INIT;
347 struct strbuf revisions = STRBUF_INIT;
075bc852 348 struct object_id merge_base;
f28d40d3
PK
349 struct option builtin_rebase_options[] = {
350 OPT_STRING(0, "onto", &options.onto_name,
351 N_("revision"),
352 N_("rebase onto given branch instead of upstream")),
06e4775a
PK
353 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
354 N_("allow pre-rebase hook to run")),
b4c8eb02
PK
355 OPT_NEGBIT('q', "quiet", &options.flags,
356 N_("be quiet. implies --no-stat"),
bff014da
PK
357 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
358 OPT_BIT('v', "verbose", &options.flags,
359 N_("display a diffstat of what changed upstream"),
360 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
361 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
362 N_("do not show diffstat of what changed upstream"),
363 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
f28d40d3
PK
364 OPT_END(),
365 };
ac7f467f 366
55071ea2
PK
367 /*
368 * NEEDSWORK: Once the builtin rebase has been tested enough
369 * and git-legacy-rebase.sh is retired to contrib/, this preamble
370 * can be removed.
371 */
372
373 if (!use_builtin_rebase()) {
374 const char *path = mkpath("%s/git-legacy-rebase",
375 git_exec_path());
376
377 if (sane_execvp(path, (char **)argv) < 0)
378 die_errno(_("could not exec %s"), path);
379 else
380 BUG("sane_execvp() returned???");
381 }
382
f28d40d3
PK
383 if (argc == 2 && !strcmp(argv[1], "-h"))
384 usage_with_options(builtin_rebase_usage,
385 builtin_rebase_options);
386
55071ea2
PK
387 prefix = setup_git_directory();
388 trace_repo_setup(prefix);
389 setup_work_tree();
390
bff014da
PK
391 git_config(rebase_config, &options);
392
f28d40d3
PK
393 argc = parse_options(argc, argv, prefix,
394 builtin_rebase_options,
395 builtin_rebase_usage, 0);
396
397 if (argc > 2)
398 usage_with_options(builtin_rebase_usage,
399 builtin_rebase_options);
ac7f467f 400
b4c8eb02
PK
401 if (!(options.flags & REBASE_NO_QUIET))
402 strbuf_addstr(&options.git_am_opt, " -q");
403
ac7f467f
PK
404 switch (options.type) {
405 case REBASE_MERGE:
406 case REBASE_INTERACTIVE:
407 case REBASE_PRESERVE_MERGES:
408 options.state_dir = merge_dir();
409 break;
410 case REBASE_AM:
411 options.state_dir = apply_dir();
412 break;
413 default:
414 /* the default rebase backend is `--am` */
415 options.type = REBASE_AM;
416 options.state_dir = apply_dir();
417 break;
418 }
419
420 if (!options.root) {
f28d40d3 421 if (argc < 1)
ac7f467f
PK
422 die("TODO: handle @{upstream}");
423 else {
f28d40d3 424 options.upstream_name = argv[0];
ac7f467f
PK
425 argc--;
426 argv++;
427 if (!strcmp(options.upstream_name, "-"))
428 options.upstream_name = "@{-1}";
429 }
430 options.upstream = peel_committish(options.upstream_name);
431 if (!options.upstream)
432 die(_("invalid upstream '%s'"), options.upstream_name);
06e4775a 433 options.upstream_arg = options.upstream_name;
ac7f467f
PK
434 } else
435 die("TODO: upstream for --root");
436
437 /* Make sure the branch to rebase onto is valid. */
438 if (!options.onto_name)
439 options.onto_name = options.upstream_name;
440 if (strstr(options.onto_name, "...")) {
075bc852
PK
441 if (get_oid_mb(options.onto_name, &merge_base) < 0)
442 die(_("'%s': need exactly one merge base"),
443 options.onto_name);
444 options.onto = lookup_commit_or_die(&merge_base,
445 options.onto_name);
ac7f467f
PK
446 } else {
447 options.onto = peel_committish(options.onto_name);
448 if (!options.onto)
449 die(_("Does not point to a valid commit '%s'"),
450 options.onto_name);
451 }
452
453 /*
454 * If the branch to rebase is given, that is the branch we will rebase
455 * branch_name -- branch/commit being rebased, or
456 * HEAD (already detached)
457 * orig_head -- commit object name of tip of the branch before rebasing
458 * head_name -- refs/heads/<that-branch> or "detached HEAD"
459 */
f28d40d3 460 if (argc > 0)
ac7f467f
PK
461 die("TODO: handle switch_to");
462 else {
463 /* Do not need to switch branches, we are already on it. */
464 options.head_name =
465 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
466 &flags));
467 if (!options.head_name)
468 die(_("No such ref: %s"), "HEAD");
469 if (flags & REF_ISSYMREF) {
470 if (!skip_prefix(options.head_name,
471 "refs/heads/", &branch_name))
472 branch_name = options.head_name;
473
474 } else {
475 options.head_name = xstrdup("detached HEAD");
476 branch_name = "HEAD";
477 }
478 if (get_oid("HEAD", &options.orig_head))
479 die(_("Could not resolve HEAD to a revision"));
480 }
481
06e4775a
PK
482 /* If a hook exists, give it a chance to interrupt*/
483 if (!ok_to_skip_pre_rebase &&
484 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
485 argc ? argv[0] : NULL, NULL))
486 die(_("The pre-rebase hook refused to rebase."));
487
bff014da
PK
488 if (options.flags & REBASE_DIFFSTAT) {
489 struct diff_options opts;
490
491 if (options.flags & REBASE_VERBOSE)
492 printf(_("Changes from %s to %s:\n"),
493 oid_to_hex(&merge_base),
494 oid_to_hex(&options.onto->object.oid));
495
496 /* We want color (if set), but no pager */
497 diff_setup(&opts);
498 opts.stat_width = -1; /* use full terminal width */
499 opts.stat_graph_width = -1; /* respect statGraphWidth config */
500 opts.output_format |=
501 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
502 opts.detect_rename = DIFF_DETECT_RENAME;
503 diff_setup_done(&opts);
504 diff_tree_oid(&merge_base, &options.onto->object.oid,
505 "", &opts);
506 diffcore_std(&opts);
507 diff_flush(&opts);
508 }
509
510 /* Detach HEAD and reset the tree */
511 if (options.flags & REBASE_NO_QUIET)
512 printf(_("First, rewinding head to replay your work on top of "
513 "it...\n"));
514
ac7f467f
PK
515 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
516 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
517 die(_("Could not detach HEAD"));
518 strbuf_release(&msg);
519
520 strbuf_addf(&revisions, "%s..%s",
521 options.root ? oid_to_hex(&options.onto->object.oid) :
522 (options.restrict_revision ?
523 oid_to_hex(&options.restrict_revision->object.oid) :
524 oid_to_hex(&options.upstream->object.oid)),
525 oid_to_hex(&options.orig_head));
526
527 options.revisions = revisions.buf;
528
529 ret = !!run_specific_rebase(&options);
530
531 strbuf_release(&revisions);
532 free(options.head_name);
533 return ret;
55071ea2 534}