sequencer: refactor how original todo list lines are accessed

Previously, we did a lot of arithmetic gymnastics to get at the line in
the todo list (as stored in todo_list.buf). This might have been fast,
but only in terms of execution speed, not in terms of developer time.

Let's refactor this to make it a lot easier to read, and hence to
reason about the correctness of the code. It is not performance-critical
code anyway.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Johannes Schindelin 2018-04-25 14:28:29 +02:00 committed by Junio C Hamano
parent 2f6b1d13aa
commit a01c2a5f59

View File

@ -1871,6 +1871,23 @@ static int count_commands(struct todo_list *todo_list)
return count; return count;
} }
static int get_item_line_offset(struct todo_list *todo_list, int index)
{
return index < todo_list->nr ?
todo_list->items[index].offset_in_buf : todo_list->buf.len;
}
static const char *get_item_line(struct todo_list *todo_list, int index)
{
return todo_list->buf.buf + get_item_line_offset(todo_list, index);
}
static int get_item_line_length(struct todo_list *todo_list, int index)
{
return get_item_line_offset(todo_list, index + 1)
- get_item_line_offset(todo_list, index);
}
static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path) static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
{ {
int fd; int fd;
@ -2250,29 +2267,27 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
if (fd < 0) if (fd < 0)
return error_errno(_("could not lock '%s'"), todo_path); return error_errno(_("could not lock '%s'"), todo_path);
offset = next < todo_list->nr ? offset = get_item_line_offset(todo_list, next);
todo_list->items[next].offset_in_buf : todo_list->buf.len;
if (write_in_full(fd, todo_list->buf.buf + offset, if (write_in_full(fd, todo_list->buf.buf + offset,
todo_list->buf.len - offset) < 0) todo_list->buf.len - offset) < 0)
return error_errno(_("could not write to '%s'"), todo_path); return error_errno(_("could not write to '%s'"), todo_path);
if (commit_lock_file(&todo_lock) < 0) if (commit_lock_file(&todo_lock) < 0)
return error(_("failed to finalize '%s'"), todo_path); return error(_("failed to finalize '%s'"), todo_path);
if (is_rebase_i(opts)) { if (is_rebase_i(opts) && next > 0) {
const char *done_path = rebase_path_done(); const char *done = rebase_path_done();
int fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666); int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666);
int prev_offset = !next ? 0 : int ret = 0;
todo_list->items[next - 1].offset_in_buf;
if (fd >= 0 && offset > prev_offset && if (fd < 0)
write_in_full(fd, todo_list->buf.buf + prev_offset, return 0;
offset - prev_offset) < 0) { if (write_in_full(fd, get_item_line(todo_list, next - 1),
close(fd); get_item_line_length(todo_list, next - 1))
return error_errno(_("could not write to '%s'"), < 0)
done_path); ret = error_errno(_("could not write to '%s'"), done);
} if (close(fd) < 0)
if (fd >= 0) ret = error_errno(_("failed to finalize '%s'"), done);
close(fd); return ret;
} }
return 0; return 0;
} }
@ -3307,8 +3322,7 @@ int skip_unnecessary_picks(void)
oid = &item->commit->object.oid; oid = &item->commit->object.oid;
} }
if (i > 0) { if (i > 0) {
int offset = i < todo_list.nr ? int offset = get_item_line_offset(&todo_list, i);
todo_list.items[i].offset_in_buf : todo_list.buf.len;
const char *done_path = rebase_path_done(); const char *done_path = rebase_path_done();
fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666); fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
@ -3488,12 +3502,10 @@ int rearrange_squash(void)
continue; continue;
while (cur >= 0) { while (cur >= 0) {
int offset = todo_list.items[cur].offset_in_buf; const char *bol =
int end_offset = cur + 1 < todo_list.nr ? get_item_line(&todo_list, cur);
todo_list.items[cur + 1].offset_in_buf : const char *eol =
todo_list.buf.len; get_item_line(&todo_list, cur + 1);
char *bol = todo_list.buf.buf + offset;
char *eol = todo_list.buf.buf + end_offset;
/* replace 'pick', by 'fixup' or 'squash' */ /* replace 'pick', by 'fixup' or 'squash' */
command = todo_list.items[cur].command; command = todo_list.items[cur].command;