wt-status.h: drop stdio.h include
[gitweb.git] / rebase-interactive.c
index 807f8370dbc7597ae3d799dba5b091c69e6d7147..aa18ae82b724812ea0b2939842395ac78f77bab1 100644 (file)
@@ -87,35 +87,40 @@ void append_todo_help(unsigned keep_empty, int command_count,
        }
 }
 
-int edit_todo_list(struct repository *r, unsigned flags)
+int edit_todo_list(struct repository *r, struct todo_list *todo_list,
+                  struct todo_list *new_todo, const char *shortrevisions,
+                  const char *shortonto, unsigned flags)
 {
        const char *todo_file = rebase_path_todo();
-       struct todo_list todo_list = TODO_LIST_INIT;
-       int res = 0;
-
-       if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
-               return error_errno(_("could not read '%s'."), todo_file);
-
-       strbuf_stripspace(&todo_list.buf, 1);
-       todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list);
-       if (todo_list_write_to_file(r, &todo_list, todo_file, NULL, NULL, -1,
-                                   flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP)) {
-               todo_list_release(&todo_list);
-               return -1;
-       }
+       unsigned initial = shortrevisions && shortonto;
 
-       strbuf_reset(&todo_list.buf);
-       if (launch_sequence_editor(todo_file, &todo_list.buf, NULL)) {
-               todo_list_release(&todo_list);
-               return -1;
-       }
+       /* If the user is editing the todo list, we first try to parse
+        * it.  If there is an error, we do not return, because the user
+        * might want to fix it in the first place. */
+       if (!initial)
+               todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list);
 
-       if (!todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list))
-               res = todo_list_write_to_file(r, &todo_list, todo_file, NULL, NULL, -1,
-                                             flags & ~(TODO_LIST_SHORTEN_IDS));
+       if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
+                                   -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
+               return error_errno(_("could not write '%s'"), todo_file);
 
-       todo_list_release(&todo_list);
-       return res;
+       if (initial && copy_file(rebase_path_todo_backup(), todo_file, 0666))
+               return error(_("could not copy '%s' to '%s'."), todo_file,
+                            rebase_path_todo_backup());
+
+       if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
+               return -2;
+
+       strbuf_stripspace(&new_todo->buf, 1);
+       if (initial && new_todo->buf.len == 0)
+               return -3;
+
+       /* For the initial edit, the todo list gets parsed in
+        * complete_action(). */
+       if (!initial)
+               return todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo);
+
+       return 0;
 }
 
 define_commit_slab(commit_seen, unsigned char);