add --interactive: allow `update` to stage deleted files

The scripted version of `git add -i` used `git update-index --add --remove`, but the built-in version implemented only the `--add` part. This fixes https://github.com/msys2/MSYS2-packages/issues/3066 Reported-by: Christoph Reiter <reiter.christoph@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 28, 2022 at 22:22 UTC 4788e8b25692a8ae1a005554d3ad12f8ee4ee29e
2 files changed +19 -2
add-interactive.c
+10 -2
@@ -665,8 +665,16 @@ static int run_update(struct add_i_state *s, const struct pathspec *ps,
665
666 for (i = 0; i < files->items.nr; i++) {
667 const char *name = files->items.items[i].string;
668 - if (files->selected[i] &&
669 - add_file_to_index(s->r->index, name, 0) < 0) {
668 + struct stat st;
669 +
670 + if (!files->selected[i])
671 + continue;
672 + if (lstat(name, &st) && is_missing_file_error(errno)) {
673 + if (remove_file_from_index(s->r->index, name) < 0) {
674 + res = error(_("could not stage '%s'"), name);
675 + break;
676 + }
677 + } else if (add_file_to_index(s->r->index, name, 0) < 0) {
678 res = error(_("could not stage '%s'"), name);
679 break;
680 }
t/t3701-add-interactive.sh
+9
@@ -71,6 +71,15 @@ test_expect_success 'status works (commit)' '
71 grep "+1/-0 *+2/-0 file" output
72 '
73
74 +test_expect_success 'update can stage deletions' '
75 + >to-delete &&
76 + git add to-delete &&
77 + rm to-delete &&
78 + test_write_lines u t "" | git add -i &&
79 + git ls-files to-delete >output &&
80 + test_must_be_empty output
81 +'
82 +
83 test_expect_success 'setup expected' '
84 cat >expected <<-\EOF
85 index 180b47c..b6f2c08 100644