Skip to content

Commit 4788e8b

Browse files
dschogitster
authored andcommitted
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 msys2/MSYS2-packages#3066 Reported-by: Christoph Reiter <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3beff38 commit 4788e8b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

add-interactive.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,16 @@ static int run_update(struct add_i_state *s, const struct pathspec *ps,
665665

666666
for (i = 0; i < files->items.nr; i++) {
667667
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) {
670678
res = error(_("could not stage '%s'"), name);
671679
break;
672680
}

t/t3701-add-interactive.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ test_expect_success 'status works (commit)' '
7171
grep "+1/-0 *+2/-0 file" output
7272
'
7373

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+
7483
test_expect_success 'setup expected' '
7584
cat >expected <<-\EOF
7685
index 180b47c..b6f2c08 100644

0 commit comments

Comments
 (0)