Skip to content

Commit cbef27d

Browse files
agrngitster
authored andcommitted
sequencer: refactor transform_todos() to work on a todo_list
This refactors transform_todos() to work on a todo_list. The function is renamed todo_list_transform(). As rebase -p still need to check the todo list from the disk, a new function is introduced, transform_todo_file(). It is still used by complete_action() and edit_todo_list() for now, but they will be replaced in a future commit. todo_list_transform() is not a static function, because it will be used by edit_todo_list() from rebase-interactive.c in a future commit. Signed-off-by: Alban Gruin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6ad656d commit cbef27d

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

builtin/rebase--interactive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
253253
}
254254
case SHORTEN_OIDS:
255255
case EXPAND_OIDS:
256-
ret = transform_todos(the_repository, flags);
256+
ret = transform_todo_file(the_repository, flags);
257257
break;
258258
case CHECK_TODO_LIST:
259259
ret = check_todo_list(the_repository);

rebase-interactive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int edit_todo_list(struct repository *r, unsigned flags)
6969

7070
strbuf_release(&buf);
7171

72-
transform_todos(r, flags | TODO_LIST_SHORTEN_IDS);
72+
transform_todo_file(r, flags | TODO_LIST_SHORTEN_IDS);
7373

7474
if (strbuf_read_file(&buf, todo_file, 0) < 0)
7575
return error_errno(_("could not read '%s'."), todo_file);
@@ -85,7 +85,7 @@ int edit_todo_list(struct repository *r, unsigned flags)
8585
if (launch_sequence_editor(todo_file, NULL, NULL))
8686
return -1;
8787

88-
transform_todos(r, flags & ~(TODO_LIST_SHORTEN_IDS));
88+
transform_todo_file(r, flags & ~(TODO_LIST_SHORTEN_IDS));
8989

9090
return 0;
9191
}

sequencer.c

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4562,27 +4562,18 @@ int sequencer_add_exec_commands(struct repository *r,
45624562
return i;
45634563
}
45644564

4565-
int transform_todos(struct repository *r, unsigned flags)
4565+
void todo_list_transform(struct repository *r, struct todo_list *todo_list,
4566+
unsigned flags)
45664567
{
4567-
const char *todo_file = rebase_path_todo();
4568-
struct todo_list todo_list = TODO_LIST_INIT;
45694568
struct strbuf buf = STRBUF_INIT;
45704569
struct todo_item *item;
45714570
int i;
45724571

4573-
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
4574-
return error(_("could not read '%s'."), todo_file);
4575-
4576-
if (todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
4577-
todo_list_release(&todo_list);
4578-
return error(_("unusable todo list: '%s'"), todo_file);
4579-
}
4580-
4581-
for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
4572+
for (item = todo_list->items, i = 0; i < todo_list->nr; i++, item++) {
45824573
/* if the item is not a command write it and continue */
45834574
if (item->command >= TODO_COMMENT) {
45844575
strbuf_addf(&buf, "%.*s\n", item->arg_len,
4585-
todo_item_get_arg(&todo_list, item));
4576+
todo_item_get_arg(todo_list, item));
45864577
continue;
45874578
}
45884579

@@ -4613,12 +4604,39 @@ int transform_todos(struct repository *r, unsigned flags)
46134604
strbuf_addch(&buf, '\n');
46144605
else
46154606
strbuf_addf(&buf, " %.*s\n", item->arg_len,
4616-
todo_item_get_arg(&todo_list, item));
4607+
todo_item_get_arg(todo_list, item));
46174608
}
46184609

4619-
i = write_message(buf.buf, buf.len, todo_file, 0);
4610+
strbuf_reset(&todo_list->buf);
4611+
strbuf_add(&todo_list->buf, buf.buf, buf.len);
4612+
strbuf_release(&buf);
4613+
4614+
if (todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list))
4615+
BUG("unusable todo list");
4616+
}
4617+
4618+
int transform_todo_file(struct repository *r, unsigned flags)
4619+
{
4620+
const char *todo_file = rebase_path_todo();
4621+
struct todo_list todo_list = TODO_LIST_INIT;
4622+
int res;
4623+
4624+
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
4625+
return error_errno(_("could not read '%s'."), todo_file);
4626+
4627+
if (todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
4628+
todo_list_release(&todo_list);
4629+
return error(_("unusable todo list: '%s'"), todo_file);
4630+
}
4631+
4632+
todo_list_transform(r, &todo_list, flags);
4633+
4634+
res = write_message(todo_list.buf.buf, todo_list.buf.len, todo_file, 0);
46204635
todo_list_release(&todo_list);
4621-
return i;
4636+
4637+
if (res)
4638+
return error_errno(_("could not write '%s'."), todo_file);
4639+
return 0;
46224640
}
46234641

46244642
enum missing_commit_check_level get_missing_commit_check_level(void)
@@ -4880,7 +4898,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
48804898
return error(_("could not copy '%s' to '%s'."), todo_file,
48814899
rebase_path_todo_backup());
48824900

4883-
if (transform_todos(r, flags | TODO_LIST_SHORTEN_IDS))
4901+
if (transform_todo_file(r, flags | TODO_LIST_SHORTEN_IDS))
48844902
return error(_("could not transform the todo list"));
48854903

48864904
strbuf_reset(buf);
@@ -4909,7 +4927,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
49094927
return -1;
49104928
}
49114929

4912-
if (transform_todos(r, flags & ~(TODO_LIST_SHORTEN_IDS)))
4930+
if (transform_todo_file(r, flags & ~(TODO_LIST_SHORTEN_IDS)))
49134931
return error(_("could not transform the todo list"));
49144932

49154933
if (opts->allow_ff && skip_unnecessary_picks(r, &oid))

sequencer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ struct todo_list {
121121

122122
int todo_list_parse_insn_buffer(struct repository *r, char *buf,
123123
struct todo_list *todo_list);
124+
void todo_list_transform(struct repository *r, struct todo_list *todo_list,
125+
unsigned flags);
124126
void todo_list_release(struct todo_list *todo_list);
125127
const char *todo_item_get_arg(struct todo_list *todo_list,
126128
struct todo_item *item);
@@ -148,7 +150,7 @@ int sequencer_make_script(struct repository *repo, FILE *out,
148150
unsigned flags);
149151

150152
int sequencer_add_exec_commands(struct repository *r, const char *command);
151-
int transform_todos(struct repository *r, unsigned flags);
153+
int transform_todo_file(struct repository *r, unsigned flags);
152154
enum missing_commit_check_level get_missing_commit_check_level(void);
153155
int check_todo_list(struct repository *r);
154156
int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags,

0 commit comments

Comments
 (0)