* Add commands after pick and (series of) squash/fixup commands * in the todo list. */
| 6241 | * in the todo list. |
| 6242 | */ |
| 6243 | static void todo_list_add_exec_commands(struct todo_list *todo_list, |
| 6244 | struct string_list *commands) |
| 6245 | { |
| 6246 | struct strbuf *buf = &todo_list->buf; |
| 6247 | size_t base_offset = buf->len; |
| 6248 | int i, insert, nr = 0, alloc = 0; |
| 6249 | struct todo_item *items = NULL, *base_items = NULL; |
| 6250 | |
| 6251 | CALLOC_ARRAY(base_items, commands->nr); |
| 6252 | for (i = 0; i < commands->nr; i++) { |
| 6253 | size_t command_len = strlen(commands->items[i].string); |
| 6254 | |
| 6255 | strbuf_addstr(buf, commands->items[i].string); |
| 6256 | strbuf_addch(buf, '\n'); |
| 6257 | |
| 6258 | base_items[i].command = TODO_EXEC; |
| 6259 | base_items[i].offset_in_buf = base_offset; |
| 6260 | base_items[i].arg_offset = base_offset; |
| 6261 | base_items[i].arg_len = command_len; |
| 6262 | |
| 6263 | base_offset += command_len + 1; |
| 6264 | } |
| 6265 | |
| 6266 | /* |
| 6267 | * Insert <commands> after every pick. Here, fixup/squash chains |
| 6268 | * are considered part of the pick, so we insert the commands *after* |
| 6269 | * those chains if there are any. |
| 6270 | * |
| 6271 | * As we insert the exec commands immediately after rearranging |
| 6272 | * any fixups and before the user edits the list, a fixup chain |
| 6273 | * can never contain comments (any comments are empty picks that |
| 6274 | * have been commented out because the user did not specify |
| 6275 | * --keep-empty). So, it is safe to insert an exec command |
| 6276 | * without looking at the command following a comment. |
| 6277 | */ |
| 6278 | insert = 0; |
| 6279 | for (i = 0; i < todo_list->nr; i++) { |
| 6280 | enum todo_command command = todo_list->items[i].command; |
| 6281 | if (insert && !is_fixup(command)) { |
| 6282 | ALLOC_GROW(items, nr + commands->nr, alloc); |
| 6283 | COPY_ARRAY(items + nr, base_items, commands->nr); |
| 6284 | nr += commands->nr; |
| 6285 | |
| 6286 | insert = 0; |
| 6287 | } |
| 6288 | |
| 6289 | ALLOC_GROW(items, nr + 1, alloc); |
| 6290 | items[nr++] = todo_list->items[i]; |
| 6291 | |
| 6292 | if (command == TODO_PICK || command == TODO_MERGE) |
| 6293 | insert = 1; |
| 6294 | } |
| 6295 | |
| 6296 | /* insert or append final <commands> */ |
| 6297 | if (insert) { |
| 6298 | ALLOC_GROW(items, nr + commands->nr, alloc); |
| 6299 | COPY_ARRAY(items + nr, base_items, commands->nr); |
| 6300 | nr += commands->nr; |
no test coverage detected