Skip to content

Commit a2d2b52

Browse files
committed
Merge branch 'pw/rebase-i-parse-fix'
Fixes to code that parses the todo file used in "rebase -i". * pw/rebase-i-parse-fix: rebase -i: fix parsing of "fixup -C<commit>" rebase -i: match whole word in is_command()
2 parents b2893ea + 666b6e1 commit a2d2b52

File tree

5 files changed

+56
-18
lines changed

5 files changed

+56
-18
lines changed

sequencer.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,12 +2479,11 @@ static int is_command(enum todo_command command, const char **bol)
24792479
{
24802480
const char *str = todo_command_info[command].str;
24812481
const char nick = todo_command_info[command].c;
2482-
const char *p = *bol + 1;
2482+
const char *p = *bol;
24832483

2484-
return skip_prefix(*bol, str, bol) ||
2485-
((nick && **bol == nick) &&
2486-
(*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) &&
2487-
(*bol = p));
2484+
return (skip_prefix(p, str, &p) || (nick && *p++ == nick)) &&
2485+
(*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) &&
2486+
(*bol = p);
24882487
}
24892488

24902489
static int check_label_or_ref_arg(enum todo_command command, const char *arg)
@@ -2541,7 +2540,8 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
25412540
break;
25422541
}
25432542
if (i >= TODO_COMMENT)
2544-
return -1;
2543+
return error(_("invalid command '%.*s'"),
2544+
(int)strcspn(bol, " \t\r\n"), bol);
25452545

25462546
/* Eat up extra spaces/ tabs before object name */
25472547
padding = strspn(bol, " \t");
@@ -2579,12 +2579,10 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
25792579
}
25802580

25812581
if (item->command == TODO_FIXUP) {
2582-
if (skip_prefix(bol, "-C", &bol) &&
2583-
(*bol == ' ' || *bol == '\t')) {
2582+
if (skip_prefix(bol, "-C", &bol)) {
25842583
bol += strspn(bol, " \t");
25852584
item->flags |= TODO_REPLACE_FIXUP_MSG;
2586-
} else if (skip_prefix(bol, "-c", &bol) &&
2587-
(*bol == ' ' || *bol == '\t')) {
2585+
} else if (skip_prefix(bol, "-c", &bol)) {
25882586
bol += strspn(bol, " \t");
25892587
item->flags |= TODO_EDIT_FIXUP_MSG;
25902588
}

t/lib-rebase.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ set_fake_editor () {
6060
">")
6161
echo >> "$1";;
6262
bad)
63-
action="badcmd";;
63+
action="pickled";;
6464
fakesha)
6565
test \& != "$action" || action=pick
6666
echo "$action XXXXXXX False commit" >> "$1"
@@ -211,6 +211,9 @@ check_reworded_commits () {
211211
# usage: set_replace_editor <file>
212212
#
213213
# Replace the todo file with the exact contents of the given file.
214+
# N.B. sets GIT_SEQUENCE_EDITOR rather than EDITOR so it can be
215+
# combined with set_fake_editor to reword commits and replace the
216+
# todo list
214217
set_replace_editor () {
215218
cat >script <<-\EOF &&
216219
cat FILENAME >"$1"
@@ -219,6 +222,7 @@ set_replace_editor () {
219222
cat "$1"
220223
EOF
221224

222-
sed -e "s/FILENAME/$1/g" <script | write_script fake-editor.sh &&
223-
test_set_editor "$(pwd)/fake-editor.sh"
225+
sed -e "s/FILENAME/$1/g" script |
226+
write_script fake-sequence-editor.sh &&
227+
test_set_sequence_editor "$(pwd)/fake-sequence-editor.sh"
224228
}

t/t3404-rebase-interactive.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,14 +1449,15 @@ test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ig
14491449

14501450
test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
14511451
cat >expect <<-EOF &&
1452-
error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1452+
error: invalid command '\''pickled'\''
1453+
error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
14531454
Warning: some commits may have been dropped accidentally.
14541455
Dropped commits (newer to older):
14551456
- $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
14561457
- $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
14571458
To avoid this message, use "drop" to explicitly remove a commit.
14581459
EOF
1459-
head -n4 expect >expect.2 &&
1460+
head -n5 expect >expect.2 &&
14601461
tail -n1 expect >>expect.2 &&
14611462
tail -n4 expect.2 >expect.3 &&
14621463
test_config rebase.missingCommitsCheck warn &&
@@ -1467,7 +1468,7 @@ test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = wa
14671468
git rebase -i --root &&
14681469
cp .git/rebase-merge/git-rebase-todo.backup orig &&
14691470
FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1470-
head -n6 actual.2 >actual &&
1471+
head -n7 actual.2 >actual &&
14711472
test_cmp expect actual &&
14721473
cp orig .git/rebase-merge/git-rebase-todo &&
14731474
FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
@@ -1483,7 +1484,8 @@ test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = wa
14831484

14841485
test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
14851486
cat >expect <<-EOF &&
1486-
error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1487+
error: invalid command '\''pickled'\''
1488+
error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
14871489
Warning: some commits may have been dropped accidentally.
14881490
Dropped commits (newer to older):
14891491
- $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
@@ -1583,7 +1585,7 @@ test_expect_success 'static check of bad command' '
15831585
set_fake_editor &&
15841586
test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
15851587
git rebase -i --root 2>actual &&
1586-
test_i18ngrep "badcmd $(git rev-list --oneline -1 primary~1)" \
1588+
test_i18ngrep "pickled $(git rev-list --oneline -1 primary~1)" \
15871589
actual &&
15881590
test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
15891591
actual &&

t/t3437-rebase-fixup-options.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ test_expect_success 'setup' '
5151
body
5252
EOF
5353
54+
test_commit initial &&
5455
test_commit A A &&
5556
test_commit B B &&
5657
get_author HEAD >expected-author &&
@@ -209,4 +210,29 @@ test_expect_success 'fixup -C works upon --autosquash with amend!' '
209210
actual-squash-message
210211
'
211212

213+
test_expect_success 'fixup -[Cc]<commit> works' '
214+
test_when_finished "test_might_fail git rebase --abort" &&
215+
cat >todo <<-\EOF &&
216+
pick A
217+
fixup -CA1
218+
pick B
219+
fixup -cA2
220+
EOF
221+
(
222+
set_replace_editor todo &&
223+
FAKE_COMMIT_MESSAGE="edited and fixed up" \
224+
git rebase -i initial initial
225+
) &&
226+
git log --pretty=format:%B initial.. >actual &&
227+
cat >expect <<-EOF &&
228+
edited and fixed up
229+
$EMPTY
230+
new subject
231+
$EMPTY
232+
new
233+
body
234+
EOF
235+
test_cmp expect actual
236+
'
237+
212238
test_done

t/test-lib-functions.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ test_set_editor () {
3232
export EDITOR
3333
}
3434

35+
# Like test_set_editor but sets GIT_SEQUENCE_EDITOR instead of EDITOR
36+
test_set_sequence_editor () {
37+
FAKE_SEQUENCE_EDITOR="$1"
38+
export FAKE_SEQUENCE_EDITOR
39+
GIT_SEQUENCE_EDITOR='"$FAKE_SEQUENCE_EDITOR"'
40+
export GIT_SEQUENCE_EDITOR
41+
}
42+
3543
test_decode_color () {
3644
awk '
3745
function name(n) {

0 commit comments

Comments
 (0)