Skip to content

Commit 7e4bf4b

Browse files
committed
built-in add -i: accept open-ended ranges again
The interactive `add` command allows selecting multiple files for some of its sub-commands, via unique prefixes, indices or index ranges. When re-implementing `git add -i` in C, we even added a code comment talking about ranges with a missing end index, such as `2-`, but the code did not actually accept those, as pointed out in git-for-windows#2466 (comment). Let's fix this, and add a test case to verify that this stays fixed forever. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 353c748 commit 7e4bf4b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

add-interactive.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,10 @@ static ssize_t list_and_choose(struct add_i_state *s,
328328
if (endp == p + sep)
329329
to = from + 1;
330330
else if (*endp == '-') {
331-
to = strtoul(++endp, &endp, 10);
331+
if (isdigit(*(++endp)))
332+
to = strtoul(endp, &endp, 10);
333+
else
334+
to = items->items.nr;
332335
/* extra characters after the range? */
333336
if (endp != p + sep)
334337
from = -1;

t/t3701-add-interactive.sh

+9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ test_expect_success 'revert works (initial)' '
5757
! grep . output
5858
'
5959

60+
test_expect_success 'add untracked (multiple)' '
61+
test_when_finished "git reset && rm [1-9]" &&
62+
touch $(test_seq 9) &&
63+
test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
64+
test_write_lines 2 3 4 5 8 9 >expected &&
65+
git ls-files [1-9] >output &&
66+
test_cmp expected output
67+
'
68+
6069
test_expect_success 'setup (commit)' '
6170
echo baseline >file &&
6271
git add file &&

0 commit comments

Comments
 (0)