Merge branch 'js/rebase-am'
authorJunio C Hamano <gitster@pobox.com>
Thu, 7 Feb 2019 06:05:22 +0000 (22:05 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 7 Feb 2019 06:05:22 +0000 (22:05 -0800)
Instead of going through "git-rebase--am" scriptlet to use the "am"
backend, the built-in version of "git rebase" learned to drive the
"am" backend directly.

* js/rebase-am:
built-in rebase: call `git am` directly
rebase: teach `reset_head()` to optionally skip the worktree
rebase: avoid double reflog entry when switching branches
rebase: move `reset_head()` into a better spot

1  2 
builtin/rebase.c
index 3f9b26f7295e4b66f8a59d72ed2d00ea7900f980,08ec4d52c707cc7b2b26c3cbf72db0a6fd7759d2..8d49e53997b3579623a284845cd49aa9b67b14a7
@@@ -331,6 -364,155 +362,161 @@@ static void add_var(struct strbuf *buf
        }
  }
  
 -#define RESET_HEAD_REFS_ONLY (1<<2)
+ #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
+ #define RESET_HEAD_DETACH (1<<0)
+ #define RESET_HEAD_HARD (1<<1)
 -      prime_cache_tree(the_repository->index, tree);
++#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
++#define RESET_HEAD_REFS_ONLY (1<<3)
+ static int reset_head(struct object_id *oid, const char *action,
+                     const char *switch_to_branch, unsigned flags,
+                     const char *reflog_orig_head, const char *reflog_head)
+ {
+       unsigned detach_head = flags & RESET_HEAD_DETACH;
+       unsigned reset_hard = flags & RESET_HEAD_HARD;
++      unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
+       unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
+       struct object_id head_oid;
+       struct tree_desc desc[2] = { { NULL }, { NULL } };
+       struct lock_file lock = LOCK_INIT;
+       struct unpack_trees_options unpack_tree_opts;
+       struct tree *tree;
+       const char *reflog_action;
+       struct strbuf msg = STRBUF_INIT;
+       size_t prefix_len;
+       struct object_id *orig = NULL, oid_orig,
+               *old_orig = NULL, oid_old_orig;
+       int ret = 0, nr = 0;
+       if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
+               BUG("Not a fully qualified branch: '%s'", switch_to_branch);
+       if (!refs_only && hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
+               ret = -1;
+               goto leave_reset_head;
+       }
+       if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
+               ret = error(_("could not determine HEAD revision"));
+               goto leave_reset_head;
+       }
+       if (!oid)
+               oid = &head_oid;
+       if (refs_only)
+               goto reset_head_refs;
+       memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
+       setup_unpack_trees_porcelain(&unpack_tree_opts, action);
+       unpack_tree_opts.head_idx = 1;
+       unpack_tree_opts.src_index = the_repository->index;
+       unpack_tree_opts.dst_index = the_repository->index;
+       unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
+       unpack_tree_opts.update = 1;
+       unpack_tree_opts.merge = 1;
+       if (!detach_head)
+               unpack_tree_opts.reset = 1;
+       if (read_index_unmerged(the_repository->index) < 0) {
+               ret = error(_("could not read index"));
+               goto leave_reset_head;
+       }
+       if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
+               ret = error(_("failed to find tree of %s"),
+                           oid_to_hex(&head_oid));
+               goto leave_reset_head;
+       }
+       if (!fill_tree_descriptor(&desc[nr++], oid)) {
+               ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
+               goto leave_reset_head;
+       }
+       if (unpack_trees(nr, desc, &unpack_tree_opts)) {
+               ret = -1;
+               goto leave_reset_head;
+       }
+       tree = parse_tree_indirect(oid);
++      prime_cache_tree(the_repository, the_repository->index, tree);
+       if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
+               ret = error(_("could not write index"));
+               goto leave_reset_head;
+       }
+ reset_head_refs:
+       reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
+       strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
+       prefix_len = msg.len;
+       if (!get_oid("ORIG_HEAD", &oid_old_orig))
+               old_orig = &oid_old_orig;
+       if (!get_oid("HEAD", &oid_orig)) {
+               orig = &oid_orig;
+               if (!reflog_orig_head) {
+                       strbuf_addstr(&msg, "updating ORIG_HEAD");
+                       reflog_orig_head = msg.buf;
+               }
+               update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
+                          UPDATE_REFS_MSG_ON_ERR);
+       } else if (old_orig)
+               delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
+       if (!reflog_head) {
+               strbuf_setlen(&msg, prefix_len);
+               strbuf_addstr(&msg, "updating HEAD");
+               reflog_head = msg.buf;
+       }
+       if (!switch_to_branch)
+               ret = update_ref(reflog_head, "HEAD", oid, orig,
+                                detach_head ? REF_NO_DEREF : 0,
+                                UPDATE_REFS_MSG_ON_ERR);
+       else {
+               ret = update_ref(reflog_orig_head, switch_to_branch, oid,
+                                NULL, 0, UPDATE_REFS_MSG_ON_ERR);
+               if (!ret)
+                       ret = create_symref("HEAD", switch_to_branch,
+                                           reflog_head);
+       }
++      if (run_hook)
++              run_hook_le(NULL, "post-checkout",
++                          oid_to_hex(orig ? orig : &null_oid),
++                          oid_to_hex(oid), "1", NULL);
+ leave_reset_head:
+       strbuf_release(&msg);
+       rollback_lock_file(&lock);
+       while (nr)
+               free((void *)desc[--nr].buffer);
+       return ret;
+ }
+ static int move_to_original_branch(struct rebase_options *opts)
+ {
+       struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
+       int ret;
+       if (!opts->head_name)
+               return 0; /* nothing to move back to */
+       if (!opts->onto)
+               BUG("move_to_original_branch without onto");
+       strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s",
+                   opts->head_name, oid_to_hex(&opts->onto->object.oid));
+       strbuf_addf(&head_reflog, "rebase finished: returning to %s",
+                   opts->head_name);
+       ret = reset_head(NULL, "", opts->head_name, RESET_HEAD_REFS_ONLY,
+                        orig_head_reflog.buf, head_reflog.buf);
+       strbuf_release(&orig_head_reflog);
+       strbuf_release(&head_reflog);
+       return ret;
+ }
  static const char *resolvemsg =
  N_("Resolve all conflicts manually, mark them as resolved with\n"
  "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
  "To abort and get back to the state before \"git rebase\", run "
  "\"git rebase --abort\".");
  
 -                       "--no-cover-letter", "--pretty=mboxrd", NULL);
+ static int run_am(struct rebase_options *opts)
+ {
+       struct child_process am = CHILD_PROCESS_INIT;
+       struct child_process format_patch = CHILD_PROCESS_INIT;
+       struct strbuf revisions = STRBUF_INIT;
+       int status;
+       char *rebased_patches;
+       am.git_cmd = 1;
+       argv_array_push(&am.args, "am");
+       if (opts->action && !strcmp("continue", opts->action)) {
+               argv_array_push(&am.args, "--resolved");
+               argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
+               if (opts->gpg_sign_opt)
+                       argv_array_push(&am.args, opts->gpg_sign_opt);
+               status = run_command(&am);
+               if (status)
+                       return status;
+               return move_to_original_branch(opts);
+       }
+       if (opts->action && !strcmp("skip", opts->action)) {
+               argv_array_push(&am.args, "--skip");
+               argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
+               status = run_command(&am);
+               if (status)
+                       return status;
+               return move_to_original_branch(opts);
+       }
+       if (opts->action && !strcmp("show-current-patch", opts->action)) {
+               argv_array_push(&am.args, "--show-current-patch");
+               return run_command(&am);
+       }
+       strbuf_addf(&revisions, "%s...%s",
+                   oid_to_hex(opts->root ?
+                              /* this is now equivalent to !opts->upstream */
+                              &opts->onto->object.oid :
+                              &opts->upstream->object.oid),
+                   oid_to_hex(&opts->orig_head));
+       rebased_patches = xstrdup(git_path("rebased-patches"));
+       format_patch.out = open(rebased_patches,
+                               O_WRONLY | O_CREAT | O_TRUNC, 0666);
+       if (format_patch.out < 0) {
+               status = error_errno(_("could not open '%s' for writing"),
+                                    rebased_patches);
+               free(rebased_patches);
+               argv_array_clear(&am.args);
+               return status;
+       }
+       format_patch.git_cmd = 1;
+       argv_array_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
+                        "--full-index", "--cherry-pick", "--right-only",
+                        "--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
++                       "--no-cover-letter", "--pretty=mboxrd", "--topo-order", NULL);
+       if (opts->git_format_patch_opt.len)
+               argv_array_split(&format_patch.args,
+                                opts->git_format_patch_opt.buf);
+       argv_array_push(&format_patch.args, revisions.buf);
+       if (opts->restrict_revision)
+               argv_array_pushf(&format_patch.args, "^%s",
+                                oid_to_hex(&opts->restrict_revision->object.oid));
+       status = run_command(&format_patch);
+       if (status) {
+               unlink(rebased_patches);
+               free(rebased_patches);
+               argv_array_clear(&am.args);
+               reset_head(&opts->orig_head, "checkout", opts->head_name, 0,
+                          "HEAD", NULL);
+               error(_("\ngit encountered an error while preparing the "
+                       "patches to replay\n"
+                       "these revisions:\n"
+                       "\n    %s\n\n"
+                       "As a result, git cannot rebase them."),
+                     opts->revisions);
+               strbuf_release(&revisions);
+               return status;
+       }
+       strbuf_release(&revisions);
+       am.in = open(rebased_patches, O_RDONLY);
+       if (am.in < 0) {
+               status = error_errno(_("could not open '%s' for reading"),
+                                    rebased_patches);
+               free(rebased_patches);
+               argv_array_clear(&am.args);
+               return status;
+       }
+       argv_array_pushv(&am.args, opts->git_am_opts.argv);
+       argv_array_push(&am.args, "--rebasing");
+       argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
+       argv_array_push(&am.args, "--patch-format=mboxrd");
+       if (opts->allow_rerere_autoupdate > 0)
+               argv_array_push(&am.args, "--rerere-autoupdate");
+       else if (opts->allow_rerere_autoupdate == 0)
+               argv_array_push(&am.args, "--no-rerere-autoupdate");
+       if (opts->gpg_sign_opt)
+               argv_array_push(&am.args, opts->gpg_sign_opt);
+       status = run_command(&am);
+       unlink(rebased_patches);
+       free(rebased_patches);
+       if (!status) {
+               return move_to_original_branch(opts);
+       }
+       if (is_directory(opts->state_dir))
+               write_basic_state(opts);
+       return status;
+ }
  static int run_specific_rebase(struct rebase_options *opts)
  {
        const char *argv[] = { NULL, NULL };