Skip to content

Commit a01c2a5

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 2f6b1d1 commit a01c2a5

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
@@ -1871,6 +1871,23 @@ static int count_commands(struct todo_list *todo_list)
18711871
return count;
18721872
}
18731873

1874+
static int get_item_line_offset(struct todo_list *todo_list, int index)
1875+
{
1876+
return index < todo_list->nr ?
1877+
todo_list->items[index].offset_in_buf : todo_list->buf.len;
1878+
}
1879+
1880+
static const char *get_item_line(struct todo_list *todo_list, int index)
1881+
{
1882+
return todo_list->buf.buf + get_item_line_offset(todo_list, index);
1883+
}
1884+
1885+
static int get_item_line_length(struct todo_list *todo_list, int index)
1886+
{
1887+
return get_item_line_offset(todo_list, index + 1)
1888+
- get_item_line_offset(todo_list, index);
1889+
}
1890+
18741891
static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
18751892
{
18761893
int fd;
@@ -2250,29 +2267,27 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
22502267
fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
22512268
if (fd < 0)
22522269
return error_errno(_("could not lock '%s'"), todo_path);
2253-
offset = next < todo_list->nr ?
2254-
todo_list->items[next].offset_in_buf : todo_list->buf.len;
2270+
offset = get_item_line_offset(todo_list, next);
22552271
if (write_in_full(fd, todo_list->buf.buf + offset,
22562272
todo_list->buf.len - offset) < 0)
22572273
return error_errno(_("could not write to '%s'"), todo_path);
22582274
if (commit_lock_file(&todo_lock) < 0)
22592275
return error(_("failed to finalize '%s'"), todo_path);
22602276

2261-
if (is_rebase_i(opts)) {
2262-
const char *done_path = rebase_path_done();
2263-
int fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
2264-
int prev_offset = !next ? 0 :
2265-
todo_list->items[next - 1].offset_in_buf;
2277+
if (is_rebase_i(opts) && next > 0) {
2278+
const char *done = rebase_path_done();
2279+
int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666);
2280+
int ret = 0;
22662281

2267-
if (fd >= 0 && offset > prev_offset &&
2268-
write_in_full(fd, todo_list->buf.buf + prev_offset,
2269-
offset - prev_offset) < 0) {
2270-
close(fd);
2271-
return error_errno(_("could not write to '%s'"),
2272-
done_path);
2273-
}
2274-
if (fd >= 0)
2275-
close(fd);
2282+
if (fd < 0)
2283+
return 0;
2284+
if (write_in_full(fd, get_item_line(todo_list, next - 1),
2285+
get_item_line_length(todo_list, next - 1))
2286+
< 0)
2287+
ret = error_errno(_("could not write to '%s'"), done);
2288+
if (close(fd) < 0)
2289+
ret = error_errno(_("failed to finalize '%s'"), done);
2290+
return ret;
22762291
}
22772292
return 0;
22782293
}
@@ -3307,8 +3322,7 @@ int skip_unnecessary_picks(void)
33073322
oid = &item->commit->object.oid;
33083323
}
33093324
if (i > 0) {
3310-
int offset = i < todo_list.nr ?
3311-
todo_list.items[i].offset_in_buf : todo_list.buf.len;
3325+
int offset = get_item_line_offset(&todo_list, i);
33123326
const char *done_path = rebase_path_done();
33133327

33143328
fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
@@ -3488,12 +3502,10 @@ int rearrange_squash(void)
34883502
continue;
34893503

34903504
while (cur >= 0) {
3491-
int offset = todo_list.items[cur].offset_in_buf;
3492-
int end_offset = cur + 1 < todo_list.nr ?
3493-
todo_list.items[cur + 1].offset_in_buf :
3494-
todo_list.buf.len;
3495-
char *bol = todo_list.buf.buf + offset;
3496-
char *eol = todo_list.buf.buf + end_offset;
3505+
const char *bol =
3506+
get_item_line(&todo_list, cur);
3507+
const char *eol =
3508+
get_item_line(&todo_list, cur + 1);
34973509

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

0 commit comments

Comments
 (0)