git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] add --interactive: allow `update` to stage deleted files
@ 2022-06-28 22:22 Johannes Schindelin via GitGitGadget
  2022-06-28 22:35 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2022-06-28 22:22 UTC (permalink / raw)
  To: git; +Cc: Christoph Reiter, Johannes Schindelin, Johannes Schindelin

From: Johannes Schindelin <johannes.schindelin@gmx.de>

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>
---
    add --interactive: fix bug in built-in variant
    
    This fixes a bug when using the built-in version of git add -i to update
    a file that has been deleted (in order to stage its deletion), where it
    fails with:
    
    fatal: unable to stat 'myfile': No such file or directory
    
    
    Since the built-in version of git add -i has been made the default in
    v2.37.0, from the users' point of view this is a regression, and this
    patch fixes it. I therefore consider this v2.37.1 material.
    
    This addresses https://github.com/msys2/MSYS2-packages/issues/3066

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1273%2Fdscho%2Fadd-i-update-deleted-file-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1273/dscho/add-i-update-deleted-file-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1273

 add-interactive.c          | 12 ++++++++++--
 t/t3701-add-interactive.sh |  9 +++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/add-interactive.c b/add-interactive.c
index 6047e8f6489..22fcd3412ca 100644
--- a/add-interactive.c
+++ b/add-interactive.c
@@ -697,8 +697,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;
 		}
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index fc26cb8bae8..b354fb39de8 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -103,6 +103,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

base-commit: e4a4b31577c7419497ac30cebe30d755b97752c5
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-29 10:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-28 22:22 [PATCH] add --interactive: allow `update` to stage deleted files Johannes Schindelin via GitGitGadget
2022-06-28 22:35 ` Junio C Hamano
2022-06-28 23:08   ` Taylor Blau
2022-06-29 10:22     ` Ævar Arnfjörð Bjarmason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).