Skip to content

Commit 843a783

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ca9b2b3 commit 843a783

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

sequencer.c

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,23 @@ static int count_commands(struct todo_list *todo_list)
18701870
return count;
18711871
}
18721872

1873+
static int get_item_line_offset(struct todo_list *todo_list, int index)
1874+
{
1875+
return index < todo_list->nr ?
1876+
todo_list->items[index].offset_in_buf : todo_list->buf.len;
1877+
}
1878+
1879+
static const char *get_item_line(struct todo_list *todo_list, int index)
1880+
{
1881+
return todo_list->buf.buf + get_item_line_offset(todo_list, index);
1882+
}
1883+
1884+
static int get_item_line_length(struct todo_list *todo_list, int index)
1885+
{
1886+
return get_item_line_offset(todo_list, index + 1)
1887+
- get_item_line_offset(todo_list, index);
1888+
}
1889+
18731890
static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
18741891
{
18751892
int fd;
@@ -2244,29 +2261,27 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
22442261
fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
22452262
if (fd < 0)
22462263
return error_errno(_("could not lock '%s'"), todo_path);
2247-
offset = next < todo_list->nr ?
2248-
todo_list->items[next].offset_in_buf : todo_list->buf.len;
2264+
offset = get_item_line_offset(todo_list, next);
22492265
if (write_in_full(fd, todo_list->buf.buf + offset,
22502266
todo_list->buf.len - offset) < 0)
22512267
return error_errno(_("could not write to '%s'"), todo_path);
22522268
if (commit_lock_file(&todo_lock) < 0)
22532269
return error(_("failed to finalize '%s'"), todo_path);
22542270

2255-
if (is_rebase_i(opts)) {
2256-
const char *done_path = rebase_path_done();
2257-
int fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
2258-
int prev_offset = !next ? 0 :
2259-
todo_list->items[next - 1].offset_in_buf;
2271+
if (is_rebase_i(opts) && next > 0) {
2272+
const char *done = rebase_path_done();
2273+
int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666);
2274+
int ret = 0;
22602275

2261-
if (fd >= 0 && offset > prev_offset &&
2262-
write_in_full(fd, todo_list->buf.buf + prev_offset,
2263-
offset - prev_offset) < 0) {
2264-
close(fd);
2265-
return error_errno(_("could not write to '%s'"),
2266-
done_path);
2267-
}
2268-
if (fd >= 0)
2269-
close(fd);
2276+
if (fd < 0)
2277+
return 0;
2278+
if (write_in_full(fd, get_item_line(todo_list, next - 1),
2279+
get_item_line_length(todo_list, next - 1))
2280+
< 0)
2281+
ret = error_errno(_("could not write to '%s'"), done);
2282+
if (close(fd) < 0)
2283+
ret = error_errno(_("failed to finalize '%s'"), done);
2284+
return ret;
22702285
}
22712286
return 0;
22722287
}
@@ -3297,8 +3312,7 @@ int skip_unnecessary_picks(void)
32973312
oid = &item->commit->object.oid;
32983313
}
32993314
if (i > 0) {
3300-
int offset = i < todo_list.nr ?
3301-
todo_list.items[i].offset_in_buf : todo_list.buf.len;
3315+
int offset = get_item_line_offset(&todo_list, i);
33023316
const char *done_path = rebase_path_done();
33033317

33043318
fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
@@ -3478,12 +3492,10 @@ int rearrange_squash(void)
34783492
continue;
34793493

34803494
while (cur >= 0) {
3481-
int offset = todo_list.items[cur].offset_in_buf;
3482-
int end_offset = cur + 1 < todo_list.nr ?
3483-
todo_list.items[cur + 1].offset_in_buf :
3484-
todo_list.buf.len;
3485-
char *bol = todo_list.buf.buf + offset;
3486-
char *eol = todo_list.buf.buf + end_offset;
3495+
const char *bol =
3496+
get_item_line(&todo_list, cur);
3497+
const char *eol =
3498+
get_item_line(&todo_list, cur + 1);
34873499

34883500
/* replace 'pick', by 'fixup' or 'squash' */
34893501
command = todo_list.items[cur].command;

0 commit comments

Comments
 (0)