4562
return i;
4563
}
4564
4565
-void todo_list_transform(struct repository *r, struct todo_list *todo_list,
4566
- unsigned flags)
4565
+static void todo_list_to_strbuf(struct repository *r, struct todo_list *todo_list,
4566
+ struct strbuf *buf, int num, unsigned flags)
4567
{
4568
- struct strbuf buf = STRBUF_INIT;
4568
struct todo_item *item;
4570
- int i;
4569
+ int i, max = todo_list->nr;
4570
4572
- for (item = todo_list->items, i = 0; i < todo_list->nr; i++, item++) {
4571
+ if (num > 0 && num < max)
4572
+ max = num;
4573
+
4574
+ for (item = todo_list->items, i = 0; i < max; i++, item++) {
4575
/* if the item is not a command write it and continue */
4576
if (item->command >= TODO_COMMENT) {
4575
- strbuf_addf(&buf, "%.*s\n", item->arg_len,
4577
+ strbuf_addf(buf, "%.*s\n", item->arg_len,
4578
todo_item_get_arg(todo_list, item));
4579
continue;
4580
}
4581
4582
/* add command to the buffer */
4583
if (flags & TODO_LIST_ABBREVIATE_CMDS)
4582
- strbuf_addch(&buf, command_to_char(item->command));
4584
+ strbuf_addch(buf, command_to_char(item->command));
4585
else
4584
- strbuf_addstr(&buf, command_to_string(item->command));
4586
+ strbuf_addstr(buf, command_to_string(item->command));
4587
4588
/* add commit id */
4589
if (item->commit) {
4593
4594
if (item->command == TODO_MERGE) {
4595
if (item->flags & TODO_EDIT_MERGE_MSG)
4594
- strbuf_addstr(&buf, " -c");
4596
+ strbuf_addstr(buf, " -c");
4597
else
4596
- strbuf_addstr(&buf, " -C");
4598
+ strbuf_addstr(buf, " -C");
4599
}
4600
4599
- strbuf_addf(&buf, " %s", oid);
4601
+ strbuf_addf(buf, " %s", oid);
4602
}
4603
4604
/* add all the rest */
4605
if (!item->arg_len)
4604
- strbuf_addch(&buf, '\n');
4606
+ strbuf_addch(buf, '\n');
4607
else
4606
- strbuf_addf(&buf, " %.*s\n", item->arg_len,
4608
+ strbuf_addf(buf, " %.*s\n", item->arg_len,
4609
todo_item_get_arg(todo_list, item));
4610
}
4611
+}
4612
4610
- strbuf_reset(&todo_list->buf);
4611
- strbuf_add(&todo_list->buf, buf.buf, buf.len);
4613
+int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list,
4614
+ const char *file, const char *shortrevisions,
4615
+ const char *shortonto, int num, unsigned flags)
4616
+{
4617
+ int edit_todo = !(shortrevisions && shortonto), res;
4618
+ struct strbuf buf = STRBUF_INIT;
4619
+
4620
+ todo_list_to_strbuf(r, todo_list, &buf, num, flags);
4621
+
4622
+ if (flags & TODO_LIST_APPEND_TODO_HELP) {
4623
+ int command_count = count_commands(todo_list);
4624
+ if (!edit_todo) {
4625
+ strbuf_addch(&buf, '\n');
4626
+ strbuf_commented_addf(&buf, Q_("Rebase %s onto %s (%d command)",
4627
+ "Rebase %s onto %s (%d commands)",
4628
+ command_count),
4629
+ shortrevisions, shortonto, command_count);
4630
+ }
4631
+ append_todo_help(edit_todo, flags & TODO_LIST_KEEP_EMPTY, &buf);
4632
+ }
4633
+
4634
+ res = write_message(buf.buf, buf.len, file, 0);
4635
strbuf_release(&buf);
4636
4614
- if (todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list))
4615
- BUG("unusable todo list");
4637
+ return res;
4638
}
4639
4640
int transform_todo_file(struct repository *r, unsigned flags)
4651
return error(_("unusable todo list: '%s'"), todo_file);
4652
}
4653
4632
- todo_list_transform(r, &todo_list, flags);
4633
-
4634
- res = write_message(todo_list.buf.buf, todo_list.buf.len, todo_file, 0);
4654
+ res = todo_list_write_to_file(r, &todo_list, todo_file,
4655
+ NULL, NULL, -1, flags);
4656
todo_list_release(&todo_list);
4657
4658
if (res)