Skip to content

Commit 6bfeb7f

Browse files
agrngitster
authored andcommitted
sequencer: refactor skip_unnecessary_picks() to work on a todo_list
This refactors skip_unnecessary_picks() to work on a todo_list. As this function is only called by complete_action() (and thus is not used by rebase -p), the file-handling logic is completely dropped here. Instead of truncating the todo list’s buffer, the items are moved to the beginning of the list, eliminating the need to reparse the list. This also means its buffer cannot be directly written to the disk. rewrite_file() is then removed, as it is now unused. Signed-off-by: Alban Gruin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 79d7e88 commit 6bfeb7f

File tree

1 file changed

+19
-63
lines changed

1 file changed

+19
-63
lines changed

sequencer.c

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4700,52 +4700,22 @@ int check_todo_list_from_file(struct repository *r)
47004700
return res;
47014701
}
47024702

4703-
static int rewrite_file(const char *path, const char *buf, size_t len)
4704-
{
4705-
int rc = 0;
4706-
int fd = open(path, O_WRONLY | O_TRUNC);
4707-
if (fd < 0)
4708-
return error_errno(_("could not open '%s' for writing"), path);
4709-
if (write_in_full(fd, buf, len) < 0)
4710-
rc = error_errno(_("could not write to '%s'"), path);
4711-
if (close(fd) && !rc)
4712-
rc = error_errno(_("could not close '%s'"), path);
4713-
return rc;
4714-
}
4715-
47164703
/* skip picking commits whose parents are unchanged */
4717-
static int skip_unnecessary_picks(struct repository *r, struct object_id *output_oid)
4704+
static int skip_unnecessary_picks(struct repository *r,
4705+
struct todo_list *todo_list,
4706+
struct object_id *base_oid)
47184707
{
4719-
const char *todo_file = rebase_path_todo();
4720-
struct strbuf buf = STRBUF_INIT;
4721-
struct todo_list todo_list = TODO_LIST_INIT;
47224708
struct object_id *parent_oid;
4723-
int fd, i;
4724-
4725-
if (!read_oneliner(&buf, rebase_path_onto(), 0))
4726-
return error(_("could not read 'onto'"));
4727-
if (get_oid(buf.buf, output_oid)) {
4728-
strbuf_release(&buf);
4729-
return error(_("need a HEAD to fixup"));
4730-
}
4731-
strbuf_release(&buf);
4732-
4733-
if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
4734-
return -1;
4735-
if (todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list) < 0) {
4736-
todo_list_release(&todo_list);
4737-
return -1;
4738-
}
4709+
int i;
47394710

4740-
for (i = 0; i < todo_list.nr; i++) {
4741-
struct todo_item *item = todo_list.items + i;
4711+
for (i = 0; i < todo_list->nr; i++) {
4712+
struct todo_item *item = todo_list->items + i;
47424713

47434714
if (item->command >= TODO_NOOP)
47444715
continue;
47454716
if (item->command != TODO_PICK)
47464717
break;
47474718
if (parse_commit(item->commit)) {
4748-
todo_list_release(&todo_list);
47494719
return error(_("could not parse commit '%s'"),
47504720
oid_to_hex(&item->commit->object.oid));
47514721
}
@@ -4754,42 +4724,26 @@ static int skip_unnecessary_picks(struct repository *r, struct object_id *output
47544724
if (item->commit->parents->next)
47554725
break; /* merge commit */
47564726
parent_oid = &item->commit->parents->item->object.oid;
4757-
if (!oideq(parent_oid, output_oid))
4727+
if (!oideq(parent_oid, base_oid))
47584728
break;
4759-
oidcpy(output_oid, &item->commit->object.oid);
4729+
oidcpy(base_oid, &item->commit->object.oid);
47604730
}
47614731
if (i > 0) {
4762-
int offset = get_item_line_offset(&todo_list, i);
47634732
const char *done_path = rebase_path_done();
47644733

4765-
fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
4766-
if (fd < 0) {
4767-
error_errno(_("could not open '%s' for writing"),
4768-
done_path);
4769-
todo_list_release(&todo_list);
4770-
return -1;
4771-
}
4772-
if (write_in_full(fd, todo_list.buf.buf, offset) < 0) {
4734+
if (todo_list_write_to_file(r, todo_list, done_path, NULL, NULL, i, 0)) {
47734735
error_errno(_("could not write to '%s'"), done_path);
4774-
todo_list_release(&todo_list);
4775-
close(fd);
47764736
return -1;
47774737
}
4778-
close(fd);
47794738

4780-
if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset,
4781-
todo_list.buf.len - offset) < 0) {
4782-
todo_list_release(&todo_list);
4783-
return -1;
4784-
}
4739+
MOVE_ARRAY(todo_list->items, todo_list->items + i, todo_list->nr - i);
4740+
todo_list->nr -= i;
4741+
todo_list->current = 0;
47854742

4786-
todo_list.current = i;
4787-
if (is_fixup(peek_command(&todo_list, 0)))
4788-
record_in_rewritten(output_oid, peek_command(&todo_list, 0));
4743+
if (is_fixup(peek_command(todo_list, 0)))
4744+
record_in_rewritten(base_oid, peek_command(todo_list, 0));
47894745
}
47904746

4791-
todo_list_release(&todo_list);
4792-
47934747
return 0;
47944748
}
47954749

@@ -4860,6 +4814,11 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
48604814
return -1;
48614815
}
48624816

4817+
if (opts->allow_ff && skip_unnecessary_picks(r, &new_todo, &oid)) {
4818+
todo_list_release(&new_todo);
4819+
return error(_("could not skip unnecessary pick commands"));
4820+
}
4821+
48634822
if (todo_list_write_to_file(r, &new_todo, todo_file, NULL, NULL, -1,
48644823
flags & ~(TODO_LIST_SHORTEN_IDS))) {
48654824
todo_list_release(&new_todo);
@@ -4868,9 +4827,6 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
48684827

48694828
todo_list_release(&new_todo);
48704829

4871-
if (opts->allow_ff && skip_unnecessary_picks(r, &oid))
4872-
return error(_("could not skip unnecessary pick commands"));
4873-
48744830
if (checkout_onto(opts, onto_name, oid_to_hex(&oid), orig_head))
48754831
return -1;
48764832

0 commit comments

Comments
 (0)