@@ -4255,10 +4255,9 @@ int sequencer_add_exec_commands(const char *commands)
4255
4255
{
4256
4256
const char * todo_file = rebase_path_todo ();
4257
4257
struct todo_list todo_list = TODO_LIST_INIT ;
4258
- struct todo_item * item ;
4259
4258
struct strbuf * buf = & todo_list .buf ;
4260
4259
size_t offset = 0 , commands_len = strlen (commands );
4261
- int i , first ;
4260
+ int i , insert ;
4262
4261
4263
4262
if (strbuf_read_file (& todo_list .buf , todo_file , 0 ) < 0 )
4264
4263
return error (_ ("could not read '%s'." ), todo_file );
@@ -4268,19 +4267,40 @@ int sequencer_add_exec_commands(const char *commands)
4268
4267
return error (_ ("unusable todo list: '%s'" ), todo_file );
4269
4268
}
4270
4269
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 );
4277
4290
offset += commands_len ;
4291
+ insert = -1 ;
4278
4292
}
4279
- first = 0 ;
4293
+
4294
+ if (command == TODO_PICK || command == TODO_MERGE )
4295
+ insert = i + 1 ;
4280
4296
}
4281
4297
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 );
4284
4304
4285
4305
i = write_message (buf -> buf , buf -> len , todo_file , 0 );
4286
4306
todo_list_release (& todo_list );
0 commit comments