Merge branch 'bp/refresh-index-using-preload'
authorJunio C Hamano <gitster@pobox.com>
Tue, 13 Nov 2018 13:37:25 +0000 (22:37 +0900)
committerJunio C Hamano <gitster@pobox.com>
Tue, 13 Nov 2018 13:37:26 +0000 (22:37 +0900)
The helper function to refresh the cached stat information in the
in-core index has learned to perform the lstat() part of the
operation in parallel on multi-core platforms.

* bp/refresh-index-using-preload:
refresh_index: remove unnecessary calls to preload_index()
speed up refresh_index() by utilizing preload_index()

1  2 
cache.h
read-cache.c
sequencer.c
diff --combined cache.h
index 8ac4adb3644f789ef644c630a2ab1bb86941fd61,883099db083124ca69d95bbc36654df61951955c..ca36b44ee0b5851f837616c322a201ac420a90d2
+++ b/cache.h
@@@ -486,8 -486,6 +486,8 @@@ static inline enum object_type object_t
  #define INFOATTRIBUTES_FILE "info/attributes"
  #define ATTRIBUTE_MACRO_PREFIX "[attr]"
  #define GITMODULES_FILE ".gitmodules"
 +#define GITMODULES_INDEX ":.gitmodules"
 +#define GITMODULES_HEAD "HEAD:.gitmodules"
  #define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
  #define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
  #define GIT_NOTES_DISPLAY_REF_ENVIRONMENT "GIT_NOTES_DISPLAY_REF"
@@@ -661,6 -659,9 +661,9 @@@ extern int daemonize(void)
  /* Initialize and use the cache information */
  struct lock_file;
  extern int read_index(struct index_state *);
+ extern void preload_index(struct index_state *index,
+                         const struct pathspec *pathspec,
+                         unsigned int refresh_flags);
  extern int read_index_preload(struct index_state *,
                              const struct pathspec *pathspec,
                              unsigned int refresh_flags);
@@@ -908,6 -909,14 +911,6 @@@ int use_optional_locks(void)
  extern char comment_line_char;
  extern int auto_comment_line_char;
  
 -/* Windows only */
 -enum hide_dotfiles_type {
 -      HIDE_DOTFILES_FALSE = 0,
 -      HIDE_DOTFILES_TRUE,
 -      HIDE_DOTFILES_DOTGITONLY
 -};
 -extern enum hide_dotfiles_type hide_dotfiles;
 -
  enum log_refs_config {
        LOG_REFS_UNSET = -1,
        LOG_REFS_NONE = 0,
@@@ -956,13 -965,11 +959,13 @@@ extern int grafts_replace_parents
  extern int repository_format_precious_objects;
  extern char *repository_format_partial_clone;
  extern const char *core_partial_clone_filter_default;
 +extern int repository_format_worktree_config;
  
  struct repository_format {
        int version;
        int precious_objects;
        char *partial_clone; /* value of extensions.partialclone */
 +      int worktree_config;
        int is_bare;
        int hash_algo;
        char *work_tree;
@@@ -1483,7 -1490,6 +1486,7 @@@ extern const char *fmt_name(const char 
  extern const char *ident_default_name(void);
  extern const char *ident_default_email(void);
  extern const char *git_editor(void);
 +extern const char *git_sequence_editor(void);
  extern const char *git_pager(int stdout_is_tty);
  extern int is_terminal_dumb(void);
  extern int git_ident_config(const char *, const char *, void *);
diff --combined read-cache.c
index f3a848d61c154ec99b4c45d3404380810c18eec1,53733d651db59881c4378ee5787311eef1f6ff99..8c924506dd96037c1d7c0fa93e4223f9274a851f
@@@ -1496,6 -1496,12 +1496,12 @@@ int refresh_index(struct index_state *i
        typechange_fmt = (in_porcelain ? "T\t%s\n" : "%s needs update\n");
        added_fmt = (in_porcelain ? "A\t%s\n" : "%s needs update\n");
        unmerged_fmt = (in_porcelain ? "U\t%s\n" : "%s: needs merge\n");
+       /*
+        * Use the multi-threaded preload_index() to refresh most of the
+        * cache entries quickly then in the single threaded loop below,
+        * we only have to do the special cases that are left.
+        */
+       preload_index(istate, pathspec, 0);
        for (i = 0; i < istate->cache_nr; i++) {
                struct cache_entry *ce, *new_entry;
                int cache_errno = 0;
@@@ -2297,8 -2303,8 +2303,8 @@@ int read_index_from(struct index_state 
        freshen_shared_index(base_path, 0);
        merge_base_index(istate);
        post_read_index_from(istate);
 -      free(base_path);
        trace_performance_leave("read cache %s", base_path);
 +      free(base_path);
        return ret;
  }
  
diff --combined sequencer.c
index 63a2094cac277359f23c8bdfa102cb5785e70d69,b46dd0fb636724d333f3dacdf43a93fa1c266661..0d87b0739be5c5fdaf686063729aca02559638b8
@@@ -31,7 -31,6 +31,7 @@@
  #include "commit-slab.h"
  #include "alias.h"
  #include "commit-reach.h"
 +#include "rebase-interactive.h"
  
  #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
  
@@@ -54,10 -53,7 +54,10 @@@ static GIT_PATH_FUNC(rebase_path, "reba
   * the lines are processed, they are removed from the front of this
   * file and written to the tail of 'done'.
   */
 -static GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")
 +GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")
 +static GIT_PATH_FUNC(rebase_path_todo_backup,
 +                   "rebase-merge/git-rebase-todo.backup")
 +
  /*
   * The rebase command lines that have already been processed. A line
   * is moved here when it is first handled, before any associated user
@@@ -145,7 -141,7 +145,7 @@@ static GIT_PATH_FUNC(rebase_path_refs_t
  
  /*
   * The following files are written by git-rebase just after parsing the
 - * command-line (and are only consumed, not modified, by the sequencer).
 + * command-line.
   */
  static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
  static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head")
@@@ -157,7 -153,6 +157,7 @@@ static GIT_PATH_FUNC(rebase_path_autost
  static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy")
  static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts")
  static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate")
 +static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet")
  
  static int git_sequencer_config(const char *k, const char *v, void *cb)
  {
@@@ -382,8 -377,8 +382,8 @@@ static void print_advice(int show_hint
        }
  }
  
 -static int write_message(const void *buf, size_t len, const char *filename,
 -                       int append_eol)
 +int write_message(const void *buf, size_t len, const char *filename,
 +                int append_eol)
  {
        struct lock_file msg_file = LOCK_INIT;
  
@@@ -669,131 -664,55 +669,131 @@@ missing_author
        return res;
  }
  
 +/**
 + * Take a series of KEY='VALUE' lines where VALUE part is
 + * sq-quoted, and append <KEY, VALUE> at the end of the string list
 + */
 +static int parse_key_value_squoted(char *buf, struct string_list *list)
 +{
 +      while (*buf) {
 +              struct string_list_item *item;
 +              char *np;
 +              char *cp = strchr(buf, '=');
 +              if (!cp) {
 +                      np = strchrnul(buf, '\n');
 +                      return error(_("no key present in '%.*s'"),
 +                                   (int) (np - buf), buf);
 +              }
 +              np = strchrnul(cp, '\n');
 +              *cp++ = '\0';
 +              item = string_list_append(list, buf);
 +
 +              buf = np + (*np == '\n');
 +              *np = '\0';
 +              cp = sq_dequote(cp);
 +              if (!cp)
 +                      return error(_("unable to dequote value of '%s'"),
 +                                   item->string);
 +              item->util = xstrdup(cp);
 +      }
 +      return 0;
 +}
  
 -/*
 - * write_author_script() used to fail to terminate the last line with a "'" and
 - * also escaped "'" incorrectly as "'\\\\''" rather than "'\\''". We check for
 - * the terminating "'" on the last line to see how "'" has been escaped in case
 - * git was upgraded while rebase was stopped.
 +/**
 + * Reads and parses the state directory's "author-script" file, and sets name,
 + * email and date accordingly.
 + * Returns 0 on success, -1 if the file could not be parsed.
 + *
 + * The author script is of the format:
 + *
 + *    GIT_AUTHOR_NAME='$author_name'
 + *    GIT_AUTHOR_EMAIL='$author_email'
 + *    GIT_AUTHOR_DATE='$author_date'
 + *
 + * where $author_name, $author_email and $author_date are quoted. We are strict
 + * with our parsing, as the file was meant to be eval'd in the old
 + * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs
 + * from what this function expects, it is better to bail out than to do
 + * something that the user does not expect.
   */
 -static int quoting_is_broken(const char *s, size_t n)
 +int read_author_script(const char *path, char **name, char **email, char **date,
 +                     int allow_missing)
  {
 -      /* Skip any empty lines in case the file was hand edited */
 -      while (n > 0 && s[--n] == '\n')
 -              ; /* empty */
 -      if (n > 0 && s[n] != '\'')
 -              return 1;
 +      struct strbuf buf = STRBUF_INIT;
 +      struct string_list kv = STRING_LIST_INIT_DUP;
 +      int retval = -1; /* assume failure */
 +      int i, name_i = -2, email_i = -2, date_i = -2, err = 0;
  
 -      return 0;
 +      if (strbuf_read_file(&buf, path, 256) <= 0) {
 +              strbuf_release(&buf);
 +              if (errno == ENOENT && allow_missing)
 +                      return 0;
 +              else
 +                      return error_errno(_("could not open '%s' for reading"),
 +                                         path);
 +      }
 +
 +      if (parse_key_value_squoted(buf.buf, &kv))
 +              goto finish;
 +
 +      for (i = 0; i < kv.nr; i++) {
 +              if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) {
 +                      if (name_i != -2)
 +                              name_i = error(_("'GIT_AUTHOR_NAME' already given"));
 +                      else
 +                              name_i = i;
 +              } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) {
 +                      if (email_i != -2)
 +                              email_i = error(_("'GIT_AUTHOR_EMAIL' already given"));
 +                      else
 +                              email_i = i;
 +              } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) {
 +                      if (date_i != -2)
 +                              date_i = error(_("'GIT_AUTHOR_DATE' already given"));
 +                      else
 +                              date_i = i;
 +              } else {
 +                      err = error(_("unknown variable '%s'"),
 +                                  kv.items[i].string);
 +              }
 +      }
 +      if (name_i == -2)
 +              error(_("missing 'GIT_AUTHOR_NAME'"));
 +      if (email_i == -2)
 +              error(_("missing 'GIT_AUTHOR_EMAIL'"));
 +      if (date_i == -2)
 +              error(_("missing 'GIT_AUTHOR_DATE'"));
 +      if (date_i < 0 || email_i < 0 || date_i < 0 || err)
 +              goto finish;
 +      *name = kv.items[name_i].util;
 +      *email = kv.items[email_i].util;
 +      *date = kv.items[date_i].util;
 +      retval = 0;
 +finish:
 +      string_list_clear(&kv, !!retval);
 +      strbuf_release(&buf);
 +      return retval;
  }
  
  /*
 - * Read a list of environment variable assignments (such as the author-script
 - * file) into an environment block. Returns -1 on error, 0 otherwise.
 + * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a
 + * file with shell quoting into struct argv_array. Returns -1 on
 + * error, 0 otherwise.
   */
  static int read_env_script(struct argv_array *env)
  {
 -      struct strbuf script = STRBUF_INIT;
 -      int i, count = 0, sq_bug;
 -      const char *p2;
 -      char *p;
 +      char *name, *email, *date;
  
 -      if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
 +      if (read_author_script(rebase_path_author_script(),
 +                             &name, &email, &date, 0))
                return -1;
 -      /* write_author_script() used to quote incorrectly */
 -      sq_bug = quoting_is_broken(script.buf, script.len);
 -      for (p = script.buf; *p; p++)
 -              if (sq_bug && skip_prefix(p, "'\\\\''", &p2))
 -                      strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
 -              else if (skip_prefix(p, "'\\''", &p2))
 -                      strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
 -              else if (*p == '\'')
 -                      strbuf_splice(&script, p-- - script.buf, 1, "", 0);
 -              else if (*p == '\n') {
 -                      *p = '\0';
 -                      count++;
 -              }
  
 -      for (i = 0, p = script.buf; i < count; i++) {
 -              argv_array_push(env, p);
 -              p += strlen(p) + 1;
 -      }
 +      argv_array_pushf(env, "GIT_AUTHOR_NAME=%s", name);
 +      argv_array_pushf(env, "GIT_AUTHOR_EMAIL=%s", email);
 +      argv_array_pushf(env, "GIT_AUTHOR_DATE=%s", date);
 +      free(name);
 +      free(email);
 +      free(date);
  
        return 0;
  }
@@@ -813,28 -732,54 +813,28 @@@ static char *get_author(const char *mes
  /* Read author-script and return an ident line (author <email> timestamp) */
  static const char *read_author_ident(struct strbuf *buf)
  {
 -      const char *keys[] = {
 -              "GIT_AUTHOR_NAME=", "GIT_AUTHOR_EMAIL=", "GIT_AUTHOR_DATE="
 -      };
        struct strbuf out = STRBUF_INIT;
 -      char *in, *eol;
 -      const char *val[3];
 -      int i = 0;
 +      char *name, *email, *date;
  
 -      if (strbuf_read_file(buf, rebase_path_author_script(), 256) <= 0)
 +      if (read_author_script(rebase_path_author_script(),
 +                             &name, &email, &date, 0))
                return NULL;
  
 -      /* dequote values and construct ident line in-place */
 -      for (in = buf->buf; i < 3 && in - buf->buf < buf->len; i++) {
 -              if (!skip_prefix(in, keys[i], (const char **)&in)) {
 -                      warning(_("could not parse '%s' (looking for '%s')"),
 -                              rebase_path_author_script(), keys[i]);
 -                      return NULL;
 -              }
 -
 -              eol = strchrnul(in, '\n');
 -              *eol = '\0';
 -              if (!sq_dequote(in)) {
 -                      warning(_("bad quoting on %s value in '%s'"),
 -                              keys[i], rebase_path_author_script());
 -                      return NULL;
 -              }
 -              val[i] = in;
 -              in = eol + 1;
 -      }
 -
 -      if (i < 3) {
 -              warning(_("could not parse '%s' (looking for '%s')"),
 -                      rebase_path_author_script(), keys[i]);
 -              return NULL;
 -      }
 -
        /* validate date since fmt_ident() will die() on bad value */
 -      if (parse_date(val[2], &out)){
 +      if (parse_date(date, &out)){
                warning(_("invalid date format '%s' in '%s'"),
 -                      val[2], rebase_path_author_script());
 +                      date, rebase_path_author_script());
                strbuf_release(&out);
                return NULL;
        }
  
        strbuf_reset(&out);
 -      strbuf_addstr(&out, fmt_ident(val[0], val[1], val[2], 0));
 +      strbuf_addstr(&out, fmt_ident(name, email, date, 0));
        strbuf_swap(buf, &out);
        strbuf_release(&out);
 +      free(name);
 +      free(email);
 +      free(date);
        return buf->buf;
  }
  
@@@ -859,23 -804,6 +859,23 @@@ N_("you have staged changes in your wor
  #define VERIFY_MSG  (1<<4)
  #define CREATE_ROOT_COMMIT (1<<5)
  
 +static int run_command_silent_on_success(struct child_process *cmd)
 +{
 +      struct strbuf buf = STRBUF_INIT;
 +      int rc;
 +
 +      cmd->stdout_to_stderr = 1;
 +      rc = pipe_command(cmd,
 +                        NULL, 0,
 +                        NULL, 0,
 +                        &buf, 0);
 +
 +      if (rc)
 +              fputs(buf.buf, stderr);
 +      strbuf_release(&buf);
 +      return rc;
 +}
 +
  /*
   * If we are cherry-pick, and if the merge did not result in
   * hand-editing, we will hit this commit and inherit the original
@@@ -937,11 -865,18 +937,11 @@@ static int run_git_commit(const char *d
  
        cmd.git_cmd = 1;
  
 -      if (is_rebase_i(opts)) {
 -              if (!(flags & EDIT_MSG)) {
 -                      cmd.stdout_to_stderr = 1;
 -                      cmd.err = -1;
 -              }
 -
 -              if (read_env_script(&cmd.env_array)) {
 -                      const char *gpg_opt = gpg_sign_opt_quoted(opts);
 +      if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) {
 +              const char *gpg_opt = gpg_sign_opt_quoted(opts);
  
 -                      return error(_(staged_changes_advice),
 -                                   gpg_opt, gpg_opt);
 -              }
 +              return error(_(staged_changes_advice),
 +                           gpg_opt, gpg_opt);
        }
  
        argv_array_push(&cmd.args, "commit");
        if (!(flags & EDIT_MSG))
                argv_array_push(&cmd.args, "--allow-empty-message");
  
 -      if (cmd.err == -1) {
 -              /* hide stderr on success */
 -              struct strbuf buf = STRBUF_INIT;
 -              int rc = pipe_command(&cmd,
 -                                    NULL, 0,
 -                                    /* stdout is already redirected */
 -                                    NULL, 0,
 -                                    &buf, 0);
 -              if (rc)
 -                      fputs(buf.buf, stderr);
 -              strbuf_release(&buf);
 -              return rc;
 -      }
 -
 -      return run_command(&cmd);
 +      if (is_rebase_i(opts) && !(flags & EDIT_MSG))
 +              return run_command_silent_on_success(&cmd);
 +      else
 +              return run_command(&cmd);
  }
  
  static int rest_is_empty(const struct strbuf *sb, int start)
@@@ -1508,7 -1454,6 +1508,7 @@@ enum todo_command 
        TODO_SQUASH,
        /* commands that do something else than handling a single commit */
        TODO_EXEC,
 +      TODO_BREAK,
        TODO_LABEL,
        TODO_RESET,
        TODO_MERGE,
@@@ -1530,7 -1475,6 +1530,7 @@@ static struct 
        { 'f', "fixup" },
        { 's', "squash" },
        { 'x', "exec" },
 +      { 'b', "break" },
        { 'l', "label" },
        { 't', "reset" },
        { 'm', "merge" },
@@@ -1969,7 -1913,7 +1969,7 @@@ static int read_and_refresh_cache(struc
  {
        struct lock_file index_lock = LOCK_INIT;
        int index_fd = hold_locked_index(&index_lock, 0);
-       if (read_index_preload(&the_index, NULL, 0) < 0) {
+       if (read_index(&the_index) < 0) {
                rollback_lock_file(&index_lock);
                return error(_("git %s: failed to read the index"),
                        _(action_name(opts)));
@@@ -2044,8 -1988,7 +2044,8 @@@ static int parse_insn_line(struct todo_
                if (skip_prefix(bol, todo_command_info[i].str, &bol)) {
                        item->command = i;
                        break;
 -              } else if (bol[1] == ' ' && *bol == todo_command_info[i].c) {
 +              } else if ((bol + 1 == eol || bol[1] == ' ') &&
 +                         *bol == todo_command_info[i].c) {
                        bol++;
                        item->command = i;
                        break;
        padding = strspn(bol, " \t");
        bol += padding;
  
 -      if (item->command == TODO_NOOP) {
 +      if (item->command == TODO_NOOP || item->command == TODO_BREAK) {
                if (bol != eol)
                        return error(_("%s does not accept arguments: '%s'"),
                                     command_to_string(item->command), bol);
@@@ -2301,14 -2244,21 +2301,14 @@@ static int populate_opts_cb(const char 
        return 0;
  }
  
 -static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
 +void parse_strategy_opts(struct replay_opts *opts, char *raw_opts)
  {
        int i;
 -      char *strategy_opts_string;
 +      char *strategy_opts_string = raw_opts;
  
 -      strbuf_reset(buf);
 -      if (!read_oneliner(buf, rebase_path_strategy(), 0))
 -              return;
 -      opts->strategy = strbuf_detach(buf, NULL);
 -      if (!read_oneliner(buf, rebase_path_strategy_opts(), 0))
 -              return;
 -
 -      strategy_opts_string = buf->buf;
        if (*strategy_opts_string == ' ')
                strategy_opts_string++;
 +
        opts->xopts_nr = split_cmdline(strategy_opts_string,
                                       (const char ***)&opts->xopts);
        for (i = 0; i < opts->xopts_nr; i++) {
        }
  }
  
 +static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
 +{
 +      strbuf_reset(buf);
 +      if (!read_oneliner(buf, rebase_path_strategy(), 0))
 +              return;
 +      opts->strategy = strbuf_detach(buf, NULL);
 +      if (!read_oneliner(buf, rebase_path_strategy_opts(), 0))
 +              return;
 +
 +      parse_strategy_opts(opts, buf->buf);
 +}
 +
  static int read_populate_opts(struct replay_opts *opts)
  {
        if (is_rebase_i(opts)) {
        return 0;
  }
  
 +static void write_strategy_opts(struct replay_opts *opts)
 +{
 +      int i;
 +      struct strbuf buf = STRBUF_INIT;
 +
 +      for (i = 0; i < opts->xopts_nr; ++i)
 +              strbuf_addf(&buf, " --%s", opts->xopts[i]);
 +
 +      write_file(rebase_path_strategy_opts(), "%s\n", buf.buf);
 +      strbuf_release(&buf);
 +}
 +
 +int write_basic_state(struct replay_opts *opts, const char *head_name,
 +                    const char *onto, const char *orig_head)
 +{
 +      const char *quiet = getenv("GIT_QUIET");
 +
 +      if (head_name)
 +              write_file(rebase_path_head_name(), "%s\n", head_name);
 +      if (onto)
 +              write_file(rebase_path_onto(), "%s\n", onto);
 +      if (orig_head)
 +              write_file(rebase_path_orig_head(), "%s\n", orig_head);
 +
 +      if (quiet)
 +              write_file(rebase_path_quiet(), "%s\n", quiet);
 +      else
 +              write_file(rebase_path_quiet(), "\n");
 +
 +      if (opts->verbose)
 +              write_file(rebase_path_verbose(), "%s", "");
 +      if (opts->strategy)
 +              write_file(rebase_path_strategy(), "%s\n", opts->strategy);
 +      if (opts->xopts_nr > 0)
 +              write_strategy_opts(opts);
 +
 +      if (opts->allow_rerere_auto == RERERE_AUTOUPDATE)
 +              write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n");
 +      else if (opts->allow_rerere_auto == RERERE_NOAUTOUPDATE)
 +              write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n");
 +
 +      if (opts->gpg_sign)
 +              write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign);
 +      if (opts->signoff)
 +              write_file(rebase_path_signoff(), "--signoff\n");
 +
 +      return 0;
 +}
 +
  static int walk_revs_populate_todo(struct todo_list *todo_list,
                                struct replay_opts *opts)
  {
@@@ -2940,7 -2829,7 +2940,7 @@@ static int do_reset(const char *name, i
        struct tree_desc desc;
        struct tree *tree;
        struct unpack_trees_options unpack_tree_opts;
 -      int ret = 0, i;
 +      int ret = 0;
  
        if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
                return -1;
                }
                oidcpy(&oid, &opts->squash_onto);
        } else {
 +              int i;
 +
                /* Determine the length of the label */
                for (i = 0; i < len; i++)
                        if (isspace(name[i]))
 -                              len = i;
 +                              break;
 +              len = i;
  
                strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name);
                if (get_oid(ref_name.buf, &oid) &&
@@@ -3400,73 -3286,6 +3400,73 @@@ static const char *reflog_message(struc
        return buf.buf;
  }
  
 +static int run_git_checkout(struct replay_opts *opts, const char *commit,
 +                          const char *action)
 +{
 +      struct child_process cmd = CHILD_PROCESS_INIT;
 +
 +      cmd.git_cmd = 1;
 +
 +      argv_array_push(&cmd.args, "checkout");
 +      argv_array_push(&cmd.args, commit);
 +      argv_array_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action);
 +
 +      if (opts->verbose)
 +              return run_command(&cmd);
 +      else
 +              return run_command_silent_on_success(&cmd);
 +}
 +
 +int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit)
 +{
 +      const char *action;
 +
 +      if (commit && *commit) {
 +              action = reflog_message(opts, "start", "checkout %s", commit);
 +              if (run_git_checkout(opts, commit, action))
 +                      return error(_("could not checkout %s"), commit);
 +      }
 +
 +      return 0;
 +}
 +
 +static int checkout_onto(struct replay_opts *opts,
 +                       const char *onto_name, const char *onto,
 +                       const char *orig_head)
 +{
 +      struct object_id oid;
 +      const char *action = reflog_message(opts, "start", "checkout %s", onto_name);
 +
 +      if (get_oid(orig_head, &oid))
 +              return error(_("%s: not a valid OID"), orig_head);
 +
 +      if (run_git_checkout(opts, onto, action)) {
 +              apply_autostash(opts);
 +              sequencer_remove_state(opts);
 +              return error(_("could not detach HEAD"));
 +      }
 +
 +      return update_ref(NULL, "ORIG_HEAD", &oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR);
 +}
 +
 +static int stopped_at_head(void)
 +{
 +      struct object_id head;
 +      struct commit *commit;
 +      struct commit_message message;
 +
 +      if (get_oid("HEAD", &head) ||
 +          !(commit = lookup_commit(the_repository, &head)) ||
 +          parse_commit(commit) || get_message(commit, &message))
 +              fprintf(stderr, _("Stopped at HEAD\n"));
 +      else {
 +              fprintf(stderr, _("Stopped at %s\n"), message.label);
 +              free_message(commit, &message);
 +      }
 +      return 0;
 +
 +}
 +
  static const char rescheduled_advice[] =
  N_("Could not execute the todo command\n"
  "\n"
@@@ -3513,9 -3332,6 +3513,9 @@@ static int pick_commits(struct todo_lis
                        unlink(rebase_path_stopped_sha());
                        unlink(rebase_path_amend());
                        delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
 +
 +                      if (item->command == TODO_BREAK)
 +                              return stopped_at_head();
                }
                if (item->command <= TODO_SQUASH) {
                        if (is_rebase_i(opts))
@@@ -4604,20 -4420,24 +4604,20 @@@ int transform_todos(unsigned flags
        return i;
  }
  
 -enum check_level {
 -      CHECK_IGNORE = 0, CHECK_WARN, CHECK_ERROR
 -};
 -
 -static enum check_level get_missing_commit_check_level(void)
 +enum missing_commit_check_level get_missing_commit_check_level(void)
  {
        const char *value;
  
        if (git_config_get_value("rebase.missingcommitscheck", &value) ||
                        !strcasecmp("ignore", value))
 -              return CHECK_IGNORE;
 +              return MISSING_COMMIT_CHECK_IGNORE;
        if (!strcasecmp("warn", value))
 -              return CHECK_WARN;
 +              return MISSING_COMMIT_CHECK_WARN;
        if (!strcasecmp("error", value))
 -              return CHECK_ERROR;
 +              return MISSING_COMMIT_CHECK_ERROR;
        warning(_("unrecognized setting %s for option "
                  "rebase.missingCommitsCheck. Ignoring."), value);
 -      return CHECK_IGNORE;
 +      return MISSING_COMMIT_CHECK_IGNORE;
  }
  
  define_commit_slab(commit_seen, unsigned char);
   */
  int check_todo_list(void)
  {
 -      enum check_level check_level = get_missing_commit_check_level();
 +      enum missing_commit_check_level check_level = get_missing_commit_check_level();
        struct strbuf todo_file = STRBUF_INIT;
        struct todo_list todo_list = TODO_LIST_INIT;
        struct strbuf missing = STRBUF_INIT;
        advise_to_edit_todo = res =
                parse_insn_buffer(todo_list.buf.buf, &todo_list);
  
 -      if (res || check_level == CHECK_IGNORE)
 +      if (res || check_level == MISSING_COMMIT_CHECK_IGNORE)
                goto leave_check;
  
        /* Mark the commits in git-rebase-todo as seen */
        if (!missing.len)
                goto leave_check;
  
 -      if (check_level == CHECK_ERROR)
 +      if (check_level == MISSING_COMMIT_CHECK_ERROR)
                advise_to_edit_todo = res = 1;
  
        fprintf(stderr,
@@@ -4727,17 -4547,17 +4727,17 @@@ static int rewrite_file(const char *pat
  }
  
  /* skip picking commits whose parents are unchanged */
 -int skip_unnecessary_picks(void)
 +static int skip_unnecessary_picks(struct object_id *output_oid)
  {
        const char *todo_file = rebase_path_todo();
        struct strbuf buf = STRBUF_INIT;
        struct todo_list todo_list = TODO_LIST_INIT;
 -      struct object_id onto_oid, *oid = &onto_oid, *parent_oid;
 +      struct object_id *parent_oid;
        int fd, i;
  
        if (!read_oneliner(&buf, rebase_path_onto(), 0))
                return error(_("could not read 'onto'"));
 -      if (get_oid(buf.buf, &onto_oid)) {
 +      if (get_oid(buf.buf, output_oid)) {
                strbuf_release(&buf);
                return error(_("need a HEAD to fixup"));
        }
                if (item->commit->parents->next)
                        break; /* merge commit */
                parent_oid = &item->commit->parents->item->object.oid;
 -              if (!oideq(parent_oid, oid))
 +              if (!oideq(parent_oid, output_oid))
                        break;
 -              oid = &item->commit->object.oid;
 +              oidcpy(output_oid, &item->commit->object.oid);
        }
        if (i > 0) {
                int offset = get_item_line_offset(&todo_list, i);
  
                todo_list.current = i;
                if (is_fixup(peek_command(&todo_list, 0)))
 -                      record_in_rewritten(oid, peek_command(&todo_list, 0));
 +                      record_in_rewritten(output_oid, peek_command(&todo_list, 0));
        }
  
        todo_list_release(&todo_list);
 -      printf("%s\n", oid_to_hex(oid));
  
        return 0;
  }
  
 +int complete_action(struct replay_opts *opts, unsigned flags,
 +                  const char *shortrevisions, const char *onto_name,
 +                  const char *onto, const char *orig_head, const char *cmd,
 +                  unsigned autosquash)
 +{
 +      const char *shortonto, *todo_file = rebase_path_todo();
 +      struct todo_list todo_list = TODO_LIST_INIT;
 +      struct strbuf *buf = &(todo_list.buf);
 +      struct object_id oid;
 +      struct stat st;
 +
 +      get_oid(onto, &oid);
 +      shortonto = find_unique_abbrev(&oid, DEFAULT_ABBREV);
 +
 +      if (!lstat(todo_file, &st) && st.st_size == 0 &&
 +          write_message("noop\n", 5, todo_file, 0))
 +              return -1;
 +
 +      if (autosquash && rearrange_squash())
 +              return -1;
 +
 +      if (cmd && *cmd)
 +              sequencer_add_exec_commands(cmd);
 +
 +      if (strbuf_read_file(buf, todo_file, 0) < 0)
 +              return error_errno(_("could not read '%s'."), todo_file);
 +
 +      if (parse_insn_buffer(buf->buf, &todo_list)) {
 +              todo_list_release(&todo_list);
 +              return error(_("unusable todo list: '%s'"), todo_file);
 +      }
 +
 +      if (count_commands(&todo_list) == 0) {
 +              apply_autostash(opts);
 +              sequencer_remove_state(opts);
 +              todo_list_release(&todo_list);
 +
 +              return error(_("nothing to do"));
 +      }
 +
 +      strbuf_addch(buf, '\n');
 +      strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)",
 +                                    "Rebase %s onto %s (%d commands)",
 +                                    count_commands(&todo_list)),
 +                            shortrevisions, shortonto, count_commands(&todo_list));
 +      append_todo_help(0, flags & TODO_LIST_KEEP_EMPTY, buf);
 +
 +      if (write_message(buf->buf, buf->len, todo_file, 0)) {
 +              todo_list_release(&todo_list);
 +              return -1;
 +      }
 +
 +      if (copy_file(rebase_path_todo_backup(), todo_file, 0666))
 +              return error(_("could not copy '%s' to '%s'."), todo_file,
 +                           rebase_path_todo_backup());
 +
 +      if (transform_todos(flags | TODO_LIST_SHORTEN_IDS))
 +              return error(_("could not transform the todo list"));
 +
 +      strbuf_reset(buf);
 +
 +      if (launch_sequence_editor(todo_file, buf, NULL)) {
 +              apply_autostash(opts);
 +              sequencer_remove_state(opts);
 +              todo_list_release(&todo_list);
 +
 +              return -1;
 +      }
 +
 +      strbuf_stripspace(buf, 1);
 +      if (buf->len == 0) {
 +              apply_autostash(opts);
 +              sequencer_remove_state(opts);
 +              todo_list_release(&todo_list);
 +
 +              return error(_("nothing to do"));
 +      }
 +
 +      todo_list_release(&todo_list);
 +
 +      if (check_todo_list()) {
 +              checkout_onto(opts, onto_name, onto, orig_head);
 +              return -1;
 +      }
 +
 +      if (transform_todos(flags & ~(TODO_LIST_SHORTEN_IDS)))
 +              return error(_("could not transform the todo list"));
 +
 +      if (opts->allow_ff && skip_unnecessary_picks(&oid))
 +              return error(_("could not skip unnecessary pick commands"));
 +
 +      if (checkout_onto(opts, onto_name, oid_to_hex(&oid), orig_head))
 +              return -1;
 +
 +      if (require_clean_work_tree("rebase", "", 1, 1))
 +              return -1;
 +
 +      return sequencer_continue(opts);
 +}
 +
  struct subject2item_entry {
        struct hashmap_entry entry;
        int i;