From 851f06c3bb180396f0383b2bb3549ad12ad29c3b Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 14 Jan 2020 20:45:08 +0100 Subject: [PATCH 1/2] built-in add -i: do not try to `patch`/`diff` an empty list of files When the user does not select any files to `patch` or `diff`, there is no need to call `run_add_p()` on them. Even worse: we _have_ to avoid calling `parse_pathspec()` with an empty list because that would trigger this error: BUG: pathspec.c:557: PATHSPEC_PREFER_CWD requires arguments So let's avoid doing any work on a list of files that is empty anyway. This fixes https://github.com/git-for-windows/git/issues/2466. Signed-off-by: Johannes Schindelin --- add-interactive.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/add-interactive.c b/add-interactive.c index 39c38964949e6d..b767f12c0729b8 100644 --- a/add-interactive.c +++ b/add-interactive.c @@ -931,7 +931,7 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps, opts->prompt = N_("Patch update"); count = list_and_choose(s, files, opts); - if (count >= 0) { + if (count > 0) { struct argv_array args = ARGV_ARRAY_INIT; struct pathspec ps_selected = { 0 }; @@ -972,7 +972,7 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps, opts->flags = IMMEDIATE; count = list_and_choose(s, files, opts); opts->flags = 0; - if (count >= 0) { + if (count > 0) { struct argv_array args = ARGV_ARRAY_INIT; argv_array_pushl(&args, "git", "diff", "-p", "--cached", From 3e4036ffff628974408151526ff33f6d60c1d12e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 14 Jan 2020 21:30:40 +0100 Subject: [PATCH 2/2] 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 https://github.com/git-for-windows/git/issues/2466#issuecomment-574142760. Let's fix this, and add a test case to verify that this stays fixed forever. Signed-off-by: Johannes Schindelin --- add-interactive.c | 5 ++++- t/t3701-add-interactive.sh | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/add-interactive.c b/add-interactive.c index b767f12c0729b8..4a9bf85cac033b 100644 --- a/add-interactive.c +++ b/add-interactive.c @@ -344,7 +344,10 @@ static ssize_t list_and_choose(struct add_i_state *s, if (endp == p + sep) to = from + 1; else if (*endp == '-') { - to = strtoul(++endp, &endp, 10); + if (isdigit(*(++endp))) + to = strtoul(endp, &endp, 10); + else + to = items->items.nr; /* extra characters after the range? */ if (endp != p + sep) from = -1; diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 2915065fad47c1..3af91eca18f57f 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -68,6 +68,15 @@ test_expect_success 'revert works (initial)' ' ! grep . output ' +test_expect_success 'add untracked (multiple)' ' + test_when_finished "git reset && rm [1-9]" && + touch $(test_seq 9) && + test_write_lines a "2-5 8-" | git add -i -- [1-9] && + test_write_lines 2 3 4 5 8 9 >expected && + git ls-files [1-9] >output && + test_cmp expected output +' + test_expect_success 'setup (commit)' ' echo baseline >file && git add file &&