Skip to content

Commit 750eb11

Browse files
committed
Merge branch 'js/rebase-merges-exec-fix'
The "--exec" option to "git rebase --rebase-merges" placed the exec commands at wrong places, which has been corrected. * js/rebase-merges-exec-fix: rebase --exec: make it work with --rebase-merges t3430: demonstrate what -r, --autosquash & --exec should do
2 parents 14677d2 + 1ace63b commit 750eb11

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

sequencer.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

t/t3430-rebase-merges.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,21 @@ test_expect_success 'octopus merges' '
363363
EOF
364364
'
365365

366+
test_expect_success 'with --autosquash and --exec' '
367+
git checkout -b with-exec H &&
368+
echo Booh >B.t &&
369+
test_tick &&
370+
git commit --fixup B B.t &&
371+
write_script show.sh <<-\EOF &&
372+
subject="$(git show -s --format=%s HEAD)"
373+
content="$(git diff HEAD^! | tail -n 1)"
374+
echo "$subject: $content"
375+
EOF
376+
test_tick &&
377+
git rebase -ir --autosquash --exec ./show.sh A >actual &&
378+
grep "B: +Booh" actual &&
379+
grep "E: +Booh" actual &&
380+
grep "G: +G" actual
381+
'
382+
366383
test_done

0 commit comments

Comments
 (0)