This refactors skip_unnecessary_picks() to work on a todo_list.  The
file-handling logic is completely dropped here, as its only usage is
made by complete_action().

Signed-off-by: Alban Gruin <alban.gr...@gmail.com>
---
 sequencer.c | 56 +++++++++++++++--------------------------------------
 1 file changed, 16 insertions(+), 40 deletions(-)

diff --git a/sequencer.c b/sequencer.c
index b37935e5ab..a432b64048 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -4545,38 +4545,20 @@ static int rewrite_file(const char *path, const char 
*buf, size_t len)
 }
 
 /* skip picking commits whose parents are unchanged */
-static int skip_unnecessary_picks(struct object_id *output_oid)
+static int skip_unnecessary_picks(struct todo_list *todo_list,
+                                 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 *parent_oid;
        int fd, i;
 
-       if (!read_oneliner(&buf, rebase_path_onto(), 0))
-               return error(_("could not read 'onto'"));
-       if (get_oid(buf.buf, output_oid)) {
-               strbuf_release(&buf);
-               return error(_("need a HEAD to fixup"));
-       }
-       strbuf_release(&buf);
-
-       if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
-               return -1;
-       if (todo_list_parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
-               todo_list_release(&todo_list);
-               return -1;
-       }
-
-       for (i = 0; i < todo_list.nr; i++) {
-               struct todo_item *item = todo_list.items + i;
+       for (i = 0; i < todo_list->nr; i++) {
+               struct todo_item *item = todo_list->items + i;
 
                if (item->command >= TODO_NOOP)
                        continue;
                if (item->command != TODO_PICK)
                        break;
                if (parse_commit(item->commit)) {
-                       todo_list_release(&todo_list);
                        return error(_("could not parse commit '%s'"),
                                oid_to_hex(&item->commit->object.oid));
                }
@@ -4590,37 +4572,29 @@ static int skip_unnecessary_picks(struct object_id 
*output_oid)
                oidcpy(output_oid, &item->commit->object.oid);
        }
        if (i > 0) {
-               int offset = get_item_line_offset(&todo_list, i);
+               int offset = get_item_line_offset(todo_list, i);
                const char *done_path = rebase_path_done();
 
                fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
                if (fd < 0) {
                        error_errno(_("could not open '%s' for writing"),
                                    done_path);
-                       todo_list_release(&todo_list);
                        return -1;
                }
-               if (write_in_full(fd, todo_list.buf.buf, offset) < 0) {
+               if (write_in_full(fd, todo_list->buf.buf, offset) < 0) {
                        error_errno(_("could not write to '%s'"), done_path);
-                       todo_list_release(&todo_list);
                        close(fd);
                        return -1;
                }
                close(fd);
 
-               if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset,
-                                todo_list.buf.len - offset) < 0) {
-                       todo_list_release(&todo_list);
-                       return -1;
-               }
+               strbuf_splice(&todo_list->buf, 0, offset, NULL, 0);
 
-               todo_list.current = i;
-               if (is_fixup(peek_command(&todo_list, 0)))
-                       record_in_rewritten(output_oid, 
peek_command(&todo_list, 0));
+               todo_list->current = i;
+               if (is_fixup(peek_command(todo_list, 0)))
+                       record_in_rewritten(output_oid, peek_command(todo_list, 
0));
        }
 
-       todo_list_release(&todo_list);
-
        return 0;
 }
 
@@ -4701,6 +4675,11 @@ int complete_action(struct replay_opts *opts, unsigned 
flags,
 
        todo_list_transform(&new_todo, flags & ~(TODO_LIST_SHORTEN_IDS));
 
+       if (opts->allow_ff && skip_unnecessary_picks(&new_todo, &oid)) {
+               todo_list_release(&new_todo);
+               return error(_("could not skip unnecessary pick commands"));
+       }
+
        if (rewrite_file(todo_file, new_todo.buf.buf, new_todo.buf.len) < 0) {
                todo_list_release(&new_todo);
                return error_errno(_("could not write '%s'"), todo_file);
@@ -4708,12 +4687,9 @@ int complete_action(struct replay_opts *opts, unsigned 
flags,
 
        todo_list_release(&new_todo);
 
-       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;
 
-- 
2.19.1

Reply via email to