@matheusbsbs / matheus-bagliano / commits / 8947fdd598
Fork 0

add-interactive: fix deletion of non-empty files

Commit 24ab81a fixed the deletion of empty files, but broke deletion of non-empty files. The approach it took was to factor out the "deleted" line from the patch header into its own hunk, the same way we do for mode changes. However, unlike mode changes, we only showed the special "delete this file" hunk if there were no other hunks. Otherwise, the user would annoyingly be presented with _two_ hunks: one for deleting the file and one for deleting the content. This meant that in the non-empty case, we forgot about the deleted line entirely, and we submitted a bogus patch to git-apply (with "/dev/null" as the destination file, but not marked as a deletion). Instead, this patch combines the file deletion hunk and the content deletion hunk (if there is one) into a single deletion hunk which is either staged or not. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 8, 2009 at 02:49 UTC 8947fdd598848b8ceb2cea9ff42e906c946eae5c
2 files changed +25 -1
git-add--interactive.perl
+5 -1
@@ -1217,7 +1217,11 @@ sub patch_update_file {
1217 if (@{$mode->{TEXT}}) {
1218 unshift @hunk, $mode;
1219 }
1220 - if (@{$deletion->{TEXT}} && !@hunk) {
1220 + if (@{$deletion->{TEXT}}) {
1221 + foreach my $hunk (@hunk) {
1222 + push @{$deletion->{TEXT}}, @{$hunk->{TEXT}};
1223 + push @{$deletion->{DISPLAY}}, @{$hunk->{DISPLAY}};
1224 + }
1225 @hunk = ($deletion);
1226 }
1227
t/t3701-add-interactive.sh
+20
@@ -214,6 +214,26 @@ test_expect_success 'add first line works' '
214 test_cmp expected diff
215 '
216
217 +cat >expected <<EOF
218 +diff --git a/non-empty b/non-empty
219 +deleted file mode 100644
220 +index d95f3ad..0000000
221 +--- a/non-empty
222 ++++ /dev/null
223 +@@ -1 +0,0 @@
224 +-content
225 +EOF
226 +test_expect_success 'deleting a non-empty file' '
227 + git reset --hard &&
228 + echo content >non-empty &&
229 + git add non-empty &&
230 + git commit -m non-empty &&
231 + rm non-empty &&
232 + echo y | git add -p non-empty &&
233 + git diff --cached >diff &&
234 + test_cmp expected diff
235 +'
236 +
237 cat >expected <<EOF
238 diff --git a/empty b/empty
239 deleted file mode 100644