@@ -4255,10 +4255,9 @@ int sequencer_add_exec_commands(const char *commands)
42554255{
42564256 const char * todo_file = rebase_path_todo ();
42574257 struct todo_list todo_list = TODO_LIST_INIT ;
4258- struct todo_item * item ;
42594258 struct strbuf * buf = & todo_list .buf ;
42604259 size_t offset = 0 , commands_len = strlen (commands );
4261- int i , first ;
4260+ int i , insert ;
42624261
42634262 if (strbuf_read_file (& todo_list .buf , todo_file , 0 ) < 0 )
42644263 return error (_ ("could not read '%s'." ), todo_file );
@@ -4268,19 +4267,40 @@ int sequencer_add_exec_commands(const char *commands)
42684267 return error (_ ("unusable todo list: '%s'" ), todo_file );
42694268 }
42704269
4271- first = 1 ;
4272- /* insert <commands> before every pick except the first one */
4273- for (item = todo_list .items , i = 0 ; i < todo_list .nr ; i ++ , item ++ ) {
4274- if (item -> command == TODO_PICK && !first ) {
4275- strbuf_insert (buf , item -> offset_in_buf + offset ,
4276- commands , commands_len );
4270+ /*
4271+ * Insert <commands> after every pick. Here, fixup/squash chains
4272+ * are considered part of the pick, so we insert the commands *after*
4273+ * those chains if there are any.
4274+ */
4275+ insert = -1 ;
4276+ for (i = 0 ; i < todo_list .nr ; i ++ ) {
4277+ enum todo_command command = todo_list .items [i ].command ;
4278+
4279+ if (insert >= 0 ) {
4280+ /* skip fixup/squash chains */
4281+ if (command == TODO_COMMENT )
4282+ continue ;
4283+ else if (is_fixup (command )) {
4284+ insert = i + 1 ;
4285+ continue ;
4286+ }
4287+ strbuf_insert (buf ,
4288+ todo_list .items [insert ].offset_in_buf +
4289+ offset , commands , commands_len );
42774290 offset += commands_len ;
4291+ insert = -1 ;
42784292 }
4279- first = 0 ;
4293+
4294+ if (command == TODO_PICK || command == TODO_MERGE )
4295+ insert = i + 1 ;
42804296 }
42814297
4282- /* append final <commands> */
4283- strbuf_add (buf , commands , commands_len );
4298+ /* insert or append final <commands> */
4299+ if (insert >= 0 && insert < todo_list .nr )
4300+ strbuf_insert (buf , todo_list .items [insert ].offset_in_buf +
4301+ offset , commands , commands_len );
4302+ else if (insert >= 0 || !offset )
4303+ strbuf_add (buf , commands , commands_len );
42844304
42854305 i = write_message (buf -> buf , buf -> len , todo_file , 0 );
42864306 todo_list_release (& todo_list );
0 commit comments