rebase-interactive: rewrite edit_todo_list() to handle the initial edit

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>

Alban Gruin committed Mar 5, 2019 at 20:18 UTC a930eb03a8299b5b29284dd9e3c253c38187167a
5 files changed +57 -28
builtin/rebase--interactive.c
+23 -1
@@ -64,6 +64,28 @@ static int rearrange_squash_in_todo_file(void)
64 return 0;
65 }
66
67 +static int edit_todo_file(unsigned flags)
68 +{
69 + const char *todo_file = rebase_path_todo();
70 + struct todo_list todo_list = TODO_LIST_INIT,
71 + new_todo = TODO_LIST_INIT;
72 + int res = 0;
73 +
74 + if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
75 + return error_errno(_("could not read '%s'."), todo_file);
76 +
77 + strbuf_stripspace(&todo_list.buf, 1);
78 + res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
79 + if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
80 + NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
81 + res = error_errno(_("could not write '%s'"), todo_file);
82 +
83 + todo_list_release(&todo_list);
84 + todo_list_release(&new_todo);
85 +
86 + return res;
87 +}
88 +
89 static int get_revision_ranges(const char *upstream, const char *onto,
90 const char **head_hash,
91 char **revisions, char **shortrevisions)
@@ -295,7 +317,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
317 break;
318 }
319 case EDIT_TODO:
298 - ret = edit_todo_list(the_repository, flags);
320 + ret = edit_todo_file(flags);
321 break;
322 case SHOW_CURRENT_PATCH: {
323 struct child_process cmd = CHILD_PROCESS_INIT;
rebase-interactive.c
+29 -24
@@ -87,35 +87,40 @@ void append_todo_help(unsigned keep_empty, int command_count,
87 }
88 }
89
90 -int edit_todo_list(struct repository *r, unsigned flags)
90 +int edit_todo_list(struct repository *r, struct todo_list *todo_list,
91 + struct todo_list *new_todo, const char *shortrevisions,
92 + const char *shortonto, unsigned flags)
93 {
94 const char *todo_file = rebase_path_todo();
93 - struct todo_list todo_list = TODO_LIST_INIT;
94 - int res = 0;
95 -
96 - if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
97 - return error_errno(_("could not read '%s'."), todo_file);
98 -
99 - strbuf_stripspace(&todo_list.buf, 1);
100 - todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list);
101 - if (todo_list_write_to_file(r, &todo_list, todo_file, NULL, NULL, -1,
102 - flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP)) {
103 - todo_list_release(&todo_list);
104 - return -1;
105 - }
95 + unsigned initial = shortrevisions && shortonto;
96
107 - strbuf_reset(&todo_list.buf);
108 - if (launch_sequence_editor(todo_file, &todo_list.buf, NULL)) {
109 - todo_list_release(&todo_list);
110 - return -1;
111 - }
97 + /* If the user is editing the todo list, we first try to parse
98 + * it. If there is an error, we do not return, because the user
99 + * might want to fix it in the first place. */
100 + if (!initial)
101 + todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list);
102
113 - if (!todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list))
114 - res = todo_list_write_to_file(r, &todo_list, todo_file, NULL, NULL, -1,
115 - flags & ~(TODO_LIST_SHORTEN_IDS));
103 + if (todo_list_write_to_file(r, todo_list, todo_file, shortrevisions, shortonto,
104 + -1, flags | TODO_LIST_SHORTEN_IDS | TODO_LIST_APPEND_TODO_HELP))
105 + return error_errno(_("could not write '%s'"), todo_file);
106
117 - todo_list_release(&todo_list);
118 - return res;
107 + if (initial && copy_file(rebase_path_todo_backup(), todo_file, 0666))
108 + return error(_("could not copy '%s' to '%s'."), todo_file,
109 + rebase_path_todo_backup());
110 +
111 + if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
112 + return -2;
113 +
114 + strbuf_stripspace(&new_todo->buf, 1);
115 + if (initial && new_todo->buf.len == 0)
116 + return -3;
117 +
118 + /* For the initial edit, the todo list gets parsed in
119 + * complete_action(). */
120 + if (!initial)
121 + return todo_list_parse_insn_buffer(r, new_todo->buf.buf, new_todo);
122 +
123 + return 0;
124 }
125
126 define_commit_slab(commit_seen, unsigned char);
rebase-interactive.h
+3 -1
@@ -8,7 +8,9 @@ struct todo_list;
8 void append_todo_help(unsigned keep_empty, int command_count,
9 const char *shortrevisions, const char *shortonto,
10 struct strbuf *buf);
11 -int edit_todo_list(struct repository *r, unsigned flags);
11 +int edit_todo_list(struct repository *r, struct todo_list *todo_list,
12 + struct todo_list *new_todo, const char *shortrevisions,
13 + const char *shortonto, unsigned flags);
14 int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo);
15
16 #endif
sequencer.c
+1 -2
@@ -55,8 +55,7 @@ static GIT_PATH_FUNC(rebase_path, "rebase-merge")
55 * file and written to the tail of 'done'.
56 */
57 GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")
58 -static GIT_PATH_FUNC(rebase_path_todo_backup,
59 - "rebase-merge/git-rebase-todo.backup")
58 +GIT_PATH_FUNC(rebase_path_todo_backup, "rebase-merge/git-rebase-todo.backup")
59
60 /*
61 * The rebase command lines that have already been processed. A line
sequencer.h
+1
@@ -10,6 +10,7 @@ struct repository;
10 const char *git_path_commit_editmsg(void);
11 const char *git_path_seq_dir(void);
12 const char *rebase_path_todo(void);
13 +const char *rebase_path_todo_backup(void);
14
15 #define APPEND_SIGNOFF_DEDUP (1u << 0)
16