Skip to content

Commit 6ad656d

Browse files
agrngitster
authored andcommitted
sequencer: remove the 'arg' field from todo_item
The 'arg' field of todo_item used to store the address of the first byte of the parameter of a command in a todo list. It was associated with the length of the parameter (the 'arg_len' field). This replaces the 'arg' field by 'arg_offset'. This new field does not store the address of the parameter, but the position of the first character of the parameter in the buffer. todo_item_get_arg() is added to return the address of the parameter of an item. This will prevent todo_list_add_exec_commands() from having to do awful pointer arithmetics when growing the todo list buffer. Signed-off-by: Alban Gruin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d94d54 commit 6ad656d

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

sequencer.c

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,8 +1999,14 @@ static struct todo_item *append_new_todo(struct todo_list *todo_list)
19991999
return todo_list->items + todo_list->nr++;
20002000
}
20012001

2002+
const char *todo_item_get_arg(struct todo_list *todo_list,
2003+
struct todo_item *item)
2004+
{
2005+
return todo_list->buf.buf + item->arg_offset;
2006+
}
2007+
20022008
static int parse_insn_line(struct repository *r, struct todo_item *item,
2003-
const char *bol, char *eol)
2009+
const char *buf, const char *bol, char *eol)
20042010
{
20052011
struct object_id commit_oid;
20062012
char *end_of_object_name;
@@ -2014,7 +2020,7 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
20142020
if (bol == eol || *bol == '\r' || *bol == comment_line_char) {
20152021
item->command = TODO_COMMENT;
20162022
item->commit = NULL;
2017-
item->arg = bol;
2023+
item->arg_offset = bol - buf;
20182024
item->arg_len = eol - bol;
20192025
return 0;
20202026
}
@@ -2041,7 +2047,7 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
20412047
return error(_("%s does not accept arguments: '%s'"),
20422048
command_to_string(item->command), bol);
20432049
item->commit = NULL;
2044-
item->arg = bol;
2050+
item->arg_offset = bol - buf;
20452051
item->arg_len = eol - bol;
20462052
return 0;
20472053
}
@@ -2053,7 +2059,7 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
20532059
if (item->command == TODO_EXEC || item->command == TODO_LABEL ||
20542060
item->command == TODO_RESET) {
20552061
item->commit = NULL;
2056-
item->arg = bol;
2062+
item->arg_offset = bol - buf;
20572063
item->arg_len = (int)(eol - bol);
20582064
return 0;
20592065
}
@@ -2067,7 +2073,7 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
20672073
} else {
20682074
item->flags |= TODO_EDIT_MERGE_MSG;
20692075
item->commit = NULL;
2070-
item->arg = bol;
2076+
item->arg_offset = bol - buf;
20712077
item->arg_len = (int)(eol - bol);
20722078
return 0;
20732079
}
@@ -2079,8 +2085,9 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
20792085
status = get_oid(bol, &commit_oid);
20802086
*end_of_object_name = saved;
20812087

2082-
item->arg = end_of_object_name + strspn(end_of_object_name, " \t");
2083-
item->arg_len = (int)(eol - item->arg);
2088+
bol = end_of_object_name + strspn(end_of_object_name, " \t");
2089+
item->arg_offset = bol - buf;
2090+
item->arg_len = (int)(eol - bol);
20842091

20852092
if (status < 0)
20862093
return -1;
@@ -2108,11 +2115,11 @@ int todo_list_parse_insn_buffer(struct repository *r, char *buf,
21082115

21092116
item = append_new_todo(todo_list);
21102117
item->offset_in_buf = p - todo_list->buf.buf;
2111-
if (parse_insn_line(r, item, p, eol)) {
2118+
if (parse_insn_line(r, item, buf, p, eol)) {
21122119
res = error(_("invalid line %d: %.*s"),
21132120
i, (int)(eol - p), p);
21142121
item->command = TODO_COMMENT + 1;
2115-
item->arg = p;
2122+
item->arg_offset = p - buf;
21162123
item->arg_len = (int)(eol - p);
21172124
item->commit = NULL;
21182125
}
@@ -2452,7 +2459,7 @@ static int walk_revs_populate_todo(struct todo_list *todo_list,
24522459

24532460
item->command = command;
24542461
item->commit = commit;
2455-
item->arg = NULL;
2462+
item->arg_offset = 0;
24562463
item->arg_len = 0;
24572464
item->offset_in_buf = todo_list->buf.len;
24582465
subject_len = find_commit_subject(commit_buffer, &subject);
@@ -3491,6 +3498,8 @@ static int pick_commits(struct repository *r,
34913498

34923499
while (todo_list->current < todo_list->nr) {
34933500
struct todo_item *item = todo_list->items + todo_list->current;
3501+
const char *arg = todo_item_get_arg(todo_list, item);
3502+
34943503
if (save_todo(todo_list, opts))
34953504
return -1;
34963505
if (is_rebase_i(opts)) {
@@ -3542,10 +3551,9 @@ static int pick_commits(struct repository *r,
35423551
fprintf(stderr,
35433552
_("Stopped at %s... %.*s\n"),
35443553
short_commit_name(commit),
3545-
item->arg_len, item->arg);
3554+
item->arg_len, arg);
35463555
return error_with_patch(r, commit,
3547-
item->arg, item->arg_len, opts, res,
3548-
!res);
3556+
arg, item->arg_len, opts, res, !res);
35493557
}
35503558
if (is_rebase_i(opts) && !res)
35513559
record_in_rewritten(&item->commit->object.oid,
@@ -3554,7 +3562,7 @@ static int pick_commits(struct repository *r,
35543562
if (res == 1)
35553563
intend_to_amend();
35563564
return error_failed_squash(r, item->commit, opts,
3557-
item->arg_len, item->arg);
3565+
item->arg_len, arg);
35583566
} else if (res && is_rebase_i(opts) && item->commit) {
35593567
int to_amend = 0;
35603568
struct object_id oid;
@@ -3573,16 +3581,16 @@ static int pick_commits(struct repository *r,
35733581
to_amend = 1;
35743582

35753583
return res | error_with_patch(r, item->commit,
3576-
item->arg, item->arg_len, opts,
3584+
arg, item->arg_len, opts,
35773585
res, to_amend);
35783586
}
35793587
} else if (item->command == TODO_EXEC) {
3580-
char *end_of_arg = (char *)(item->arg + item->arg_len);
3588+
char *end_of_arg = (char *)(arg + item->arg_len);
35813589
int saved = *end_of_arg;
35823590
struct stat st;
35833591

35843592
*end_of_arg = '\0';
3585-
res = do_exec(r, item->arg);
3593+
res = do_exec(r, arg);
35863594
*end_of_arg = saved;
35873595

35883596
/* Reread the todo file if it has changed. */
@@ -3599,14 +3607,14 @@ static int pick_commits(struct repository *r,
35993607
todo_list->current = -1;
36003608
}
36013609
} else if (item->command == TODO_LABEL) {
3602-
if ((res = do_label(r, item->arg, item->arg_len)))
3610+
if ((res = do_label(r, arg, item->arg_len)))
36033611
reschedule = 1;
36043612
} else if (item->command == TODO_RESET) {
3605-
if ((res = do_reset(r, item->arg, item->arg_len, opts)))
3613+
if ((res = do_reset(r, arg, item->arg_len, opts)))
36063614
reschedule = 1;
36073615
} else if (item->command == TODO_MERGE) {
36083616
if ((res = do_merge(r, item->commit,
3609-
item->arg, item->arg_len,
3617+
arg, item->arg_len,
36103618
item->flags, opts)) < 0)
36113619
reschedule = 1;
36123620
else if (item->commit)
@@ -3615,9 +3623,8 @@ static int pick_commits(struct repository *r,
36153623
if (res > 0)
36163624
/* failed with merge conflicts */
36173625
return error_with_patch(r, item->commit,
3618-
item->arg,
3619-
item->arg_len, opts,
3620-
res, 0);
3626+
arg, item->arg_len,
3627+
opts, res, 0);
36213628
} else if (!is_noop(item->command))
36223629
return error(_("unknown command %d"), item->command);
36233630

@@ -3632,9 +3639,8 @@ static int pick_commits(struct repository *r,
36323639
if (item->commit)
36333640
return error_with_patch(r,
36343641
item->commit,
3635-
item->arg,
3636-
item->arg_len, opts,
3637-
res, 0);
3642+
arg, item->arg_len,
3643+
opts, res, 0);
36383644
}
36393645

36403646
todo_list->current++;
@@ -4575,7 +4581,8 @@ int transform_todos(struct repository *r, unsigned flags)
45754581
for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
45764582
/* if the item is not a command write it and continue */
45774583
if (item->command >= TODO_COMMENT) {
4578-
strbuf_addf(&buf, "%.*s\n", item->arg_len, item->arg);
4584+
strbuf_addf(&buf, "%.*s\n", item->arg_len,
4585+
todo_item_get_arg(&todo_list, item));
45794586
continue;
45804587
}
45814588

@@ -4605,7 +4612,8 @@ int transform_todos(struct repository *r, unsigned flags)
46054612
if (!item->arg_len)
46064613
strbuf_addch(&buf, '\n');
46074614
else
4608-
strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg);
4615+
strbuf_addf(&buf, " %.*s\n", item->arg_len,
4616+
todo_item_get_arg(&todo_list, item));
46094617
}
46104618

46114619
i = write_message(buf.buf, buf.len, todo_file, 0);
@@ -4681,7 +4689,8 @@ int check_todo_list(struct repository *r)
46814689
if (commit && !*commit_seen_at(&commit_seen, commit)) {
46824690
strbuf_addf(&missing, " - %s %.*s\n",
46834691
short_commit_name(commit),
4684-
item->arg_len, item->arg);
4692+
item->arg_len,
4693+
todo_item_get_arg(&todo_list, item));
46854694
*commit_seen_at(&commit_seen, commit) = 1;
46864695
}
46874696
}

sequencer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ struct todo_item {
104104
enum todo_command command;
105105
struct commit *commit;
106106
unsigned int flags;
107-
const char *arg;
108107
int arg_len;
109-
size_t offset_in_buf;
108+
/* The offset of the command and its argument in the strbuf */
109+
size_t offset_in_buf, arg_offset;
110110
};
111111

112112
struct todo_list {
@@ -122,6 +122,8 @@ struct todo_list {
122122
int todo_list_parse_insn_buffer(struct repository *r, char *buf,
123123
struct todo_list *todo_list);
124124
void todo_list_release(struct todo_list *todo_list);
125+
const char *todo_item_get_arg(struct todo_list *todo_list,
126+
struct todo_item *item);
125127

126128
/* Call this to setup defaults before parsing command line options */
127129
void sequencer_init_config(struct replay_opts *opts);

0 commit comments

Comments
 (0)