rebase-interactive: rewrite edit_todo_list() to handle the initial edit
authorAlban Gruin <alban.gruin@gmail.com>
Tue, 5 Mar 2019 19:18:03 +0000 (20:18 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 7 Mar 2019 00:17:57 +0000 (09:17 +0900)
edit_todo_list() is changed to work on a todo_list, and to handle the
initial edition of the todo list (ie. making a backup of the todo
list).

It does not check for dropped commits yet, as todo_list_check() does not
take the commits that have already been processed by the rebase (ie. the
todo list is edited in the middle of a rebase session).

Signed-off-by: Alban Gruin <alban.gruin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rebase--interactive.c
rebase-interactive.c
rebase-interactive.h
sequencer.c
sequencer.h
index ab2c6fcd998908eb9615b1950beb709678b8a3fd..b277239f21444f804e17a68845a859d3232ed69f 100644 (file)
@@ -64,6 +64,28 @@ static int rearrange_squash_in_todo_file(void)
        return 0;
 }
 
        return 0;
 }
 
+static int edit_todo_file(unsigned flags)
+{
+       const char *todo_file = rebase_path_todo();
+       struct todo_list todo_list = TODO_LIST_INIT,
+               new_todo = 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);
+       res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
+       if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
+                                           NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
+               res = error_errno(_("could not write '%s'"), todo_file);
+
+       todo_list_release(&todo_list);
+       todo_list_release(&new_todo);
+
+       return res;
+}
+
 static int get_revision_ranges(const char *upstream, const char *onto,
                               const char **head_hash,
                               char **revisions, char **shortrevisions)
 static int get_revision_ranges(const char *upstream, const char *onto,
                               const char **head_hash,
                               char **revisions, char **shortrevisions)
@@ -295,7 +317,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
                break;
        }
        case EDIT_TODO:
                break;
        }
        case EDIT_TODO:
-               ret = edit_todo_list(the_repository, flags);
+               ret = edit_todo_file(flags);
                break;
        case SHOW_CURRENT_PATCH: {
                struct child_process cmd = CHILD_PROCESS_INIT;
                break;
        case SHOW_CURRENT_PATCH: {
                struct child_process cmd = CHILD_PROCESS_INIT;
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();
 {
        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);
 }
 
 define_commit_slab(commit_seen, unsigned char);
index 0e5925e3aa0e3ed8164152ccc78453d58c92f78a..44dbb06311a84168d8a5970267a14ade85671a98 100644 (file)
@@ -8,7 +8,9 @@ struct todo_list;
 void append_todo_help(unsigned keep_empty, int command_count,
                      const char *shortrevisions, const char *shortonto,
                      struct strbuf *buf);
 void append_todo_help(unsigned keep_empty, int command_count,
                      const char *shortrevisions, const char *shortonto,
                      struct strbuf *buf);
-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);
 int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo);
 
 #endif
 int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo);
 
 #endif
index 8f3836c479b4c349130a80a9e42daa8c04dcaaf8..7d46f76a8bff43a1b86752036b563609fea7f99d 100644 (file)
@@ -55,8 +55,7 @@ static GIT_PATH_FUNC(rebase_path, "rebase-merge")
  * file and written to the tail of 'done'.
  */
 GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")
  * file and written to the tail of 'done'.
  */
 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")
+GIT_PATH_FUNC(rebase_path_todo_backup, "rebase-merge/git-rebase-todo.backup")
 
 /*
  * The rebase command lines that have already been processed. A line
 
 /*
  * The rebase command lines that have already been processed. A line
index fb31a30d15ecf90d5702f1cb992964b58d4d0e6b..195891a2670ca446a70ce83b3d95e3e58a9cd389 100644 (file)
@@ -10,6 +10,7 @@ struct repository;
 const char *git_path_commit_editmsg(void);
 const char *git_path_seq_dir(void);
 const char *rebase_path_todo(void);
 const char *git_path_commit_editmsg(void);
 const char *git_path_seq_dir(void);
 const char *rebase_path_todo(void);
+const char *rebase_path_todo_backup(void);
 
 #define APPEND_SIGNOFF_DEDUP (1u << 0)
 
 
 #define APPEND_SIGNOFF_DEDUP (1u << 0)