Skip to content

Commit b29c4d9

Browse files
committed
rebase --exec: make it work with --rebase-merges
The idea of `--exec` is to append an `exec` call after each `pick`. Since the introduction of fixup!/squash! commits, this idea was extended to apply to "pick, possibly followed by a fixup/squash chain", i.e. an exec would not be inserted between a `pick` and any of its corresponding `fixup` or `squash` lines. The current implementation uses a dirty trick to achieve that: it assumes that there are only pick/fixup/squash commands, and then *inserts* the `exec` lines before any `pick` but the first, and appends a final one. With the todo lists generated by `git rebase --rebase-merges`, this simple implementation shows its problems: it produces the exact wrong thing when there are `label`, `reset` and `merge` commands. Let's change the implementation to do exactly what we want: look for `pick` lines, skip any fixup/squash chains, and then insert the `exec` line. Lather, rinse, repeat. While at it, also add `exec` lines after `merge` commands, because they are similar in spirit to `pick` commands: they add new commits. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 1d82eb4 commit b29c4d9

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

sequencer.c

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4244,10 +4244,9 @@ int sequencer_add_exec_commands(const char *commands)
42444244
{
42454245
const char *todo_file = rebase_path_todo();
42464246
struct todo_list todo_list = TODO_LIST_INIT;
4247-
struct todo_item *item;
42484247
struct strbuf *buf = &todo_list.buf;
42494248
size_t offset = 0, commands_len = strlen(commands);
4250-
int i, first;
4249+
int i, insert_final_commands;
42514250

42524251
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
42534252
return error(_("could not read '%s'."), todo_file);
@@ -4257,19 +4256,57 @@ int sequencer_add_exec_commands(const char *commands)
42574256
return error(_("unusable todo list: '%s'"), todo_file);
42584257
}
42594258

4260-
first = 1;
4261-
/* insert <commands> before every pick except the first one */
4262-
for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
4263-
if (item->command == TODO_PICK && !first) {
4264-
strbuf_insert(buf, item->offset_in_buf + offset,
4265-
commands, commands_len);
4266-
offset += commands_len;
4259+
/*
4260+
* Insert <commands> after every pick. Here, fixup/squash chains
4261+
* are considered part of the pick, so we insert the commands *after*
4262+
* those chains if there are any.
4263+
*/
4264+
insert_final_commands = 1;
4265+
for (i = 0; i < todo_list.nr; ) {
4266+
enum todo_command command = todo_list.items[i].command;
4267+
int j = 0;
4268+
4269+
if (command != TODO_PICK && command != TODO_MERGE) {
4270+
i++;
4271+
continue;
4272+
}
4273+
4274+
/* skip fixup/squash chain, if any */
4275+
for (i++; i < todo_list.nr; i++, j = 0) {
4276+
command = todo_list.items[i].command;
4277+
4278+
if (is_fixup(command))
4279+
continue;
4280+
4281+
if (command != TODO_COMMENT)
4282+
break;
4283+
4284+
/* skip comment if followed by any fixup/squash */
4285+
for (j = i + 1; j < todo_list.nr; j++)
4286+
if (todo_list.items[j].command != TODO_COMMENT)
4287+
break;
4288+
if (j < todo_list.nr &&
4289+
is_fixup(todo_list.items[j].command)) {
4290+
i = j;
4291+
continue;
4292+
}
4293+
break;
42674294
}
4268-
first = 0;
4295+
4296+
if (i >= todo_list.nr) {
4297+
insert_final_commands = 1;
4298+
break;
4299+
}
4300+
4301+
strbuf_insert(buf, todo_list.items[i].offset_in_buf + offset,
4302+
commands, commands_len);
4303+
offset += commands_len;
4304+
insert_final_commands = 0;
42694305
}
42704306

42714307
/* append final <commands> */
4272-
strbuf_add(buf, commands, commands_len);
4308+
if (insert_final_commands)
4309+
strbuf_add(buf, commands, commands_len);
42734310

42744311
i = write_message(buf->buf, buf->len, todo_file, 0);
42754312
todo_list_release(&todo_list);

t/t3430-rebase-merges.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ test_expect_success 'octopus merges' '
363363
EOF
364364
'
365365

366-
test_expect_failure 'with --autosquash and --exec' '
366+
test_expect_success 'with --autosquash and --exec' '
367367
git checkout -b with-exec H &&
368368
echo Booh >B.t &&
369369
test_tick &&

0 commit comments

Comments
 (0)