Skip to content

Commit 8dccc7a

Browse files
liambeguingitster
authored andcommitted
rebase -i: refactor transform_todo_ids
The transform_todo_ids function is a little hard to read. Lets try to make it easier by using more of the strbuf API. Also, since we'll soon be adding command abbreviations, let's rename the function so it's name reflects that change. Signed-off-by: Liam Beguin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7dcbb3c commit 8dccc7a

File tree

3 files changed

+31
-44
lines changed

3 files changed

+31
-44
lines changed

builtin/rebase--helper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5555
if (command == MAKE_SCRIPT && argc > 1)
5656
return !!sequencer_make_script(keep_empty, stdout, argc, argv);
5757
if (command == SHORTEN_SHA1S && argc == 1)
58-
return !!transform_todo_ids(1);
58+
return !!transform_todos(1);
5959
if (command == EXPAND_SHA1S && argc == 1)
60-
return !!transform_todo_ids(0);
60+
return !!transform_todos(0);
6161
if (command == CHECK_TODO_LIST && argc == 1)
6262
return !!check_todo_list();
6363
if (command == SKIP_UNNECESSARY_PICKS && argc == 1)

sequencer.c

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,60 +2494,47 @@ int sequencer_make_script(int keep_empty, FILE *out,
24942494
}
24952495

24962496

2497-
int transform_todo_ids(int shorten_ids)
2497+
int transform_todos(int shorten_ids)
24982498
{
24992499
const char *todo_file = rebase_path_todo();
25002500
struct todo_list todo_list = TODO_LIST_INIT;
2501-
int fd, res, i;
2502-
FILE *out;
2501+
struct strbuf buf = STRBUF_INIT;
2502+
struct todo_item *item;
2503+
int i;
25032504

2504-
strbuf_reset(&todo_list.buf);
2505-
fd = open(todo_file, O_RDONLY);
2506-
if (fd < 0)
2507-
return error_errno(_("could not open '%s'"), todo_file);
2508-
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2509-
close(fd);
2505+
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
25102506
return error(_("could not read '%s'."), todo_file);
2511-
}
2512-
close(fd);
25132507

2514-
res = parse_insn_buffer(todo_list.buf.buf, &todo_list);
2515-
if (res) {
2508+
if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) {
25162509
todo_list_release(&todo_list);
25172510
return error(_("unusable todo list: '%s'"), todo_file);
25182511
}
25192512

2520-
out = fopen(todo_file, "w");
2521-
if (!out) {
2522-
todo_list_release(&todo_list);
2523-
return error(_("unable to open '%s' for writing"), todo_file);
2524-
}
2525-
for (i = 0; i < todo_list.nr; i++) {
2526-
struct todo_item *item = todo_list.items + i;
2527-
int bol = item->offset_in_buf;
2528-
const char *p = todo_list.buf.buf + bol;
2529-
int eol = i + 1 < todo_list.nr ?
2530-
todo_list.items[i + 1].offset_in_buf :
2531-
todo_list.buf.len;
2532-
2533-
if (item->command >= TODO_EXEC && item->command != TODO_DROP)
2534-
fwrite(p, eol - bol, 1, out);
2535-
else {
2536-
const char *id = shorten_ids ?
2537-
short_commit_name(item->commit) :
2538-
oid_to_hex(&item->commit->object.oid);
2539-
int len;
2540-
2541-
p += strspn(p, " \t"); /* left-trim command */
2542-
len = strcspn(p, " \t"); /* length of command */
2543-
2544-
fprintf(out, "%.*s %s %.*s\n",
2545-
len, p, id, item->arg_len, item->arg);
2513+
for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
2514+
/* if the item is not a command write it and continue */
2515+
if (item->command >= TODO_COMMENT) {
2516+
strbuf_addf(&buf, "%.*s\n", item->arg_len, item->arg);
2517+
continue;
2518+
}
2519+
2520+
/* add command to the buffer */
2521+
strbuf_addstr(&buf, command_to_string(item->command));
2522+
2523+
/* add commit id */
2524+
if (item->commit) {
2525+
const char *oid = shorten_ids ?
2526+
short_commit_name(item->commit) :
2527+
oid_to_hex(&item->commit->object.oid);
2528+
2529+
strbuf_addf(&buf, " %s", oid);
25462530
}
2531+
/* add all the rest */
2532+
strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg);
25472533
}
2548-
fclose(out);
2534+
2535+
i = write_message(buf.buf, buf.len, todo_file, 0);
25492536
todo_list_release(&todo_list);
2550-
return 0;
2537+
return i;
25512538
}
25522539

25532540
enum check_level {

sequencer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int sequencer_remove_state(struct replay_opts *opts);
4848
int sequencer_make_script(int keep_empty, FILE *out,
4949
int argc, const char **argv);
5050

51-
int transform_todo_ids(int shorten_ids);
51+
int transform_todos(int shorten_ids);
5252
int check_todo_list(void);
5353
int skip_unnecessary_picks(void);
5454
int rearrange_squash(void);

0 commit comments

Comments
 (0)