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 <liambeguin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Liam Beguin committed Dec 5, 2017 at 12:52 UTC 8dccc7a6b2deb05783ea5d57b53548dab32d99ae
3 files changed +31 -44
builtin/rebase--helper.c
+2 -2
@@ -55,9 +55,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
55 if (command == MAKE_SCRIPT && argc > 1)
56 return !!sequencer_make_script(keep_empty, stdout, argc, argv);
57 if (command == SHORTEN_SHA1S && argc == 1)
58 - return !!transform_todo_ids(1);
58 + return !!transform_todos(1);
59 if (command == EXPAND_SHA1S && argc == 1)
60 - return !!transform_todo_ids(0);
60 + return !!transform_todos(0);
61 if (command == CHECK_TODO_LIST && argc == 1)
62 return !!check_todo_list();
63 if (command == SKIP_UNNECESSARY_PICKS && argc == 1)
sequencer.c
+28 -41
@@ -2494,60 +2494,47 @@ int sequencer_make_script(int keep_empty, FILE *out,
2494 }
2495
2496
2497 -int transform_todo_ids(int shorten_ids)
2497 +int transform_todos(int shorten_ids)
2498 {
2499 const char *todo_file = rebase_path_todo();
2500 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;
2504
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)
2506 return error(_("could not read '%s'."), todo_file);
2511 - }
2512 - close(fd);
2507
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)) {
2509 todo_list_release(&todo_list);
2510 return error(_("unusable todo list: '%s'"), todo_file);
2511 }
2512
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);
2530 }
2531 + /* add all the rest */
2532 + strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg);
2533 }
2548 - fclose(out);
2534 +
2535 + i = write_message(buf.buf, buf.len, todo_file, 0);
2536 todo_list_release(&todo_list);
2550 - return 0;
2537 + return i;
2538 }
2539
2540 enum check_level {
sequencer.h
+1 -1
@@ -48,7 +48,7 @@ int sequencer_remove_state(struct replay_opts *opts);
48 int sequencer_make_script(int keep_empty, FILE *out,
49 int argc, const char **argv);
50
51 -int transform_todo_ids(int shorten_ids);
51 +int transform_todos(int shorten_ids);
52 int check_todo_list(void);
53 int skip_unnecessary_picks(void);
54 int rearrange_squash(void);