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