From 7aed2c0565bb1a7b4524f93e35a29770286ea630 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Thu, 23 Feb 2023 20:55:00 +0000 Subject: rebase -i: match whole word in is_command() When matching an unabbreviated command is_command() only does a prefix match which means it parses "pickled" as TODO_PICK. parse_insn_line() does error out because is_command() only advances as far as the end of "pick" so it looks like the command name is not followed by a space but the error message is "missing arguments for pick" rather than telling the user that the "pickled" is not a valid command. Fix this by ensuring the match is follow by whitespace or the end of the string as we already do for abbreviated commands. The (*bol = p) at the end of the condition is a bit cute for my taste but I decided to leave it be for now. Rather than add new tests the existing tests for bad commands are adapted to use a bad command name that triggers the prefix matching bug. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- sequencer.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'sequencer.c') diff --git a/sequencer.c b/sequencer.c index f51d403b7d..416412ca33 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2469,12 +2469,11 @@ static int is_command(enum todo_command command, const char **bol) { const char *str = todo_command_info[command].str; const char nick = todo_command_info[command].c; - const char *p = *bol + 1; + const char *p = *bol; - return skip_prefix(*bol, str, bol) || - ((nick && **bol == nick) && - (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) && - (*bol = p)); + return (skip_prefix(p, str, &p) || (nick && *p++ == nick)) && + (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) && + (*bol = p); } static int parse_insn_line(struct repository *r, struct todo_item *item, @@ -2503,7 +2502,8 @@ static int parse_insn_line(struct repository *r, struct todo_item *item, break; } if (i >= TODO_COMMENT) - return -1; + return error(_("invalid command '%.*s'"), + (int)strcspn(bol, " \t\r\n"), bol); /* Eat up extra spaces/ tabs before object name */ padding = strspn(bol, " \t"); -- cgit v1.3 From 666b6e1135c12925efe608c4d5f03234c54e2d0c Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Thu, 23 Feb 2023 20:55:01 +0000 Subject: rebase -i: fix parsing of "fixup -C" If the user omits the space between "-C" and the commit in a fixup command then it is parsed as an ordinary fixup and the commit message is not updated as it should be. Fix this by making the space between "-C" and "" optional as it is for the "merge" command. Note that set_replace_editor() is changed to set $GIT_SEQUENCE_EDITOR instead of $EDITOR in order to be able to replace the todo list and reword commits with $FAKE_COMMIT_MESSAGE. This is safe as all the existing users are using set_replace_editor() to replace the todo list. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- sequencer.c | 6 ++---- t/lib-rebase.sh | 8 ++++++-- t/t3437-rebase-fixup-options.sh | 26 ++++++++++++++++++++++++++ t/test-lib-functions.sh | 8 ++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) (limited to 'sequencer.c') diff --git a/sequencer.c b/sequencer.c index 416412ca33..3c34e70872 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2532,12 +2532,10 @@ static int parse_insn_line(struct repository *r, struct todo_item *item, } if (item->command == TODO_FIXUP) { - if (skip_prefix(bol, "-C", &bol) && - (*bol == ' ' || *bol == '\t')) { + if (skip_prefix(bol, "-C", &bol)) { bol += strspn(bol, " \t"); item->flags |= TODO_REPLACE_FIXUP_MSG; - } else if (skip_prefix(bol, "-c", &bol) && - (*bol == ' ' || *bol == '\t')) { + } else if (skip_prefix(bol, "-c", &bol)) { bol += strspn(bol, " \t"); item->flags |= TODO_EDIT_FIXUP_MSG; } diff --git a/t/lib-rebase.sh b/t/lib-rebase.sh index 1d2f0429ae..7ca5b918f0 100644 --- a/t/lib-rebase.sh +++ b/t/lib-rebase.sh @@ -211,6 +211,9 @@ check_reworded_commits () { # usage: set_replace_editor # # Replace the todo file with the exact contents of the given file. +# N.B. sets GIT_SEQUENCE_EDITOR rather than EDITOR so it can be +# combined with set_fake_editor to reword commits and replace the +# todo list set_replace_editor () { cat >script <<-\EOF && cat FILENAME >"$1" @@ -219,6 +222,7 @@ set_replace_editor () { cat "$1" EOF - sed -e "s/FILENAME/$1/g"