{"thread":{"id":"21393","subject":"[PATCH] add-interactive: handle deletion of empty files","startedAt":"2009-10-28T00:52:57Z","lastAt":"2009-10-28T07:18:25Z","messageCount":2,"participants":["Jeff King","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"126076","messageId":"20091028005257.GA5002@sigill.intra.peff.net","threadId":"21393","inReplyTo":null,"subject":"[PATCH] add-interactive: handle deletion of empty files","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-10-28T00:52:57Z","receivedAt":"2009-10-28T00:52:57Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"Usually we show deletion as a big hunk deleting all of the\nfile's text. However, for files with no content, the diff\nshows just the 'deleted file mode ...' line. This patch\ncause \"add -p\" (and related commands) to recognize that line\nand explicitly ask about deleting the file.\n\nWe only add the \"stage this deletion\" hunk for empty files,\nsince other files will already ask about the big content\ndeletion hunk. We could also change those files to simply\ndisplay \"stage this deletion\", but showing the actual\ndeleted content is probably what an interactive user wants.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nThis bit me today. I expect it's pretty rare, but I don't see a reason\nnot to handle the corner case (and nor do I see a reason to change the\nnon-corner case, as explained above).\n\n git-add--interactive.perl  |   18 +++++++++++++-----\n t/t3701-add-interactive.sh |   17 +++++++++++++++++\n 2 files changed, 30 insertions(+), 5 deletions(-)\n\ndiff --git a/git-add--interactive.perl b/git-add--interactive.perl\nindex 69aeaf0..8ce1ec9 100755\n--- a/git-add--interactive.perl\n+++ b/git-add--interactive.perl\n@@ -731,14 +731,17 @@ sub parse_diff_header {\n \n \tmy $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };\n \tmy $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };\n+\tmy $deletion = { TEXT => [], DISPLAY => [], TYPE => 'deletion' };\n \n \tfor (my $i = 0; $i < @{$src->{TEXT}}; $i++) {\n-\t\tmy $dest = $src->{TEXT}->[$i] =~ /^(old|new) mode (\\d+)$/ ?\n-\t\t\t$mode : $head;\n+\t\tmy $dest =\n+\t\t   $src->{TEXT}->[$i] =~ /^(old|new) mode (\\d+)$/ ? $mode :\n+\t\t   $src->{TEXT}->[$i] =~ /^deleted file/ ? $deletion :\n+\t\t   $head;\n \t\tpush @{$dest->{TEXT}}, $src->{TEXT}->[$i];\n \t\tpush @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];\n \t}\n-\treturn ($head, $mode);\n+\treturn ($head, $mode, $deletion);\n }\n \n sub hunk_splittable {\n@@ -1206,7 +1209,7 @@ sub patch_update_file {\n \tmy ($ix, $num);\n \tmy $path = shift;\n \tmy ($head, @hunk) = parse_diff($path);\n-\t($head, my $mode) = parse_diff_header($head);\n+\t($head, my $mode, my $deletion) = parse_diff_header($head);\n \tfor (@{$head->{DISPLAY}}) {\n \t\tprint;\n \t}\n@@ -1214,6 +1217,9 @@ sub patch_update_file {\n \tif (@{$mode->{TEXT}}) {\n \t\tunshift @hunk, $mode;\n \t}\n+\tif (@{$deletion->{TEXT}} && !@hunk) {\n+\t\t@hunk = ($deletion);\n+\t}\n \n \t$num = scalar @hunk;\n \t$ix = 0;\n@@ -1267,7 +1273,9 @@ sub patch_update_file {\n \t\t\tprint;\n \t\t}\n \t\tprint colored $prompt_color, $patch_mode_flavour{VERB},\n-\t\t  ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' : ' this hunk'),\n+\t\t  ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' :\n+\t\t   $hunk[$ix]{TYPE} eq 'deletion' ? ' deletion' :\n+\t\t   ' this hunk'),\n \t\t  $patch_mode_flavour{TARGET},\n \t\t  \" [y,n,q,a,d,/$other,?]? \";\n \t\tmy $line = prompt_single_character;\ndiff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh\nindex 687bd7a..768236b 100755\n--- a/t/t3701-add-interactive.sh\n+++ b/t/t3701-add-interactive.sh\n@@ -228,4 +228,21 @@ test_expect_success 'add first line works' '\n \ttest_cmp expected diff\n '\n \n+cat >expected <<EOF\n+diff --git a/empty b/empty\n+deleted file mode 100644\n+index e69de29..0000000\n+EOF\n+\n+test_expect_success 'deleting an empty file' '\n+\tgit reset --hard &&\n+\t> empty &&\n+\tgit add empty &&\n+\tgit commit -m empty &&\n+\trm empty &&\n+\techo y | git add -p empty &&\n+\tgit diff --cached >diff &&\n+\ttest_cmp expected diff\n+'\n+\n test_done\n-- \n1.6.5.2.193.g57820\n"},{"id":"126099","messageId":"7vbpjst29q.fsf@alter.siamese.dyndns.org","threadId":"21393","inReplyTo":"20091028005257.GA5002@sigill.intra.peff.net","subject":"Re: [PATCH] add-interactive: handle deletion of empty files","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-10-28T07:18:25Z","receivedAt":"2009-10-28T07:18:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks.\n"}]}