Skip to content

Commit 8947fdd

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 24ab81a commit 8947fdd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

git-add--interactive.perl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,11 @@ sub patch_update_file {
12171217
if (@{$mode->{TEXT}}) {
12181218
unshift @hunk, $mode;
12191219
}
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+
}
12211225
@hunk = ($deletion);
12221226
}
12231227

t/t3701-add-interactive.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,26 @@ test_expect_success 'add first line works' '
214214
test_cmp expected diff
215215
'
216216

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+
217237
cat >expected <<EOF
218238
diff --git a/empty b/empty
219239
deleted file mode 100644

0 commit comments

Comments
 (0)