Skip to content

Commit a930eb0

Browse files
agrngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af1fc3a commit a930eb0

File tree

5 files changed

+57
-28
lines changed

5 files changed

+57
-28
lines changed

builtin/rebase--interactive.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ static int rearrange_squash_in_todo_file(void)
6464
return 0;
6565
}
6666

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+
6789
static int get_revision_ranges(const char *upstream, const char *onto,
6890
const char **head_hash,
6991
char **revisions, char **shortrevisions)
@@ -295,7 +317,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
295317
break;
296318
}
297319
case EDIT_TODO:
298-
ret = edit_todo_list(the_repository, flags);
320+
ret = edit_todo_file(flags);
299321
break;
300322
case SHOW_CURRENT_PATCH: {
301323
struct child_process cmd = CHILD_PROCESS_INIT;

rebase-interactive.c

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,35 +87,40 @@ void append_todo_help(unsigned keep_empty, int command_count,
8787
}
8888
}
8989

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)
9193
{
9294
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;
10696

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);
112102

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);
116106

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;
119124
}
120125

121126
define_commit_slab(commit_seen, unsigned char);

rebase-interactive.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ struct todo_list;
88
void append_todo_help(unsigned keep_empty, int command_count,
99
const char *shortrevisions, const char *shortonto,
1010
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);
1214
int todo_list_check(struct todo_list *old_todo, struct todo_list *new_todo);
1315

1416
#endif

sequencer.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ static GIT_PATH_FUNC(rebase_path, "rebase-merge")
5555
* file and written to the tail of 'done'.
5656
*/
5757
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")
6059

6160
/*
6261
* The rebase command lines that have already been processed. A line

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct repository;
1010
const char *git_path_commit_editmsg(void);
1111
const char *git_path_seq_dir(void);
1212
const char *rebase_path_todo(void);
13+
const char *rebase_path_todo_backup(void);
1314

1415
#define APPEND_SIGNOFF_DEDUP (1u << 0)
1516

0 commit comments

Comments
 (0)