]> git.ipfire.org Git - thirdparty/git.git/commitdiff
add --interactive: allow `update` to stage deleted files
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Tue, 28 Jun 2022 22:22:44 +0000 (22:22 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 28 Jun 2022 22:37:50 +0000 (15:37 -0700)
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>
add-interactive.c
t/t3701-add-interactive.sh

index f395d54c08df577e557ef1590780c7e1f7c92296..63bc1c1d671949f89d8b60af5c26f8c7aec41539 100644 (file)
@@ -665,8 +665,16 @@ static int run_update(struct add_i_state *s, const struct pathspec *ps,
 
        for (i = 0; i < files->items.nr; i++) {
                const char *name = files->items.items[i].string;
-               if (files->selected[i] &&
-                   add_file_to_index(s->r->index, name, 0) < 0) {
+               struct stat st;
+
+               if (!files->selected[i])
+                       continue;
+               if (lstat(name, &st) && is_missing_file_error(errno)) {
+                       if (remove_file_from_index(s->r->index, name) < 0) {
+                               res = error(_("could not stage '%s'"), name);
+                               break;
+                       }
+               } else if (add_file_to_index(s->r->index, name, 0) < 0) {
                        res = error(_("could not stage '%s'"), name);
                        break;
                }
index d4f9386621b468ae805febf7382268ed8bf53d8d..02c919c5da4e0e9c842a2a27a6b7129566f5e9d6 100755 (executable)
@@ -71,6 +71,15 @@ test_expect_success 'status works (commit)' '
        grep "+1/-0 *+2/-0 file" output
 '
 
+test_expect_success 'update can stage deletions' '
+       >to-delete &&
+       git add to-delete &&
+       rm to-delete &&
+       test_write_lines u t "" | git add -i &&
+       git ls-files to-delete >output &&
+       test_must_be_empty output
+'
+
 test_expect_success 'setup expected' '
        cat >expected <<-\EOF
        index 180b47c..b6f2c08 100644