@matheusbsbs / matheus-bagliano / commits / 24ab81ae4d
Fork 0

add-interactive: handle deletion of empty files

Usually we show deletion as a big hunk deleting all of the file's text. However, for files with no content, the diff shows just the 'deleted file mode ...' line. This patch cause "add -p" (and related commands) to recognize that line and explicitly ask about deleting the file. We only add the "stage this deletion" hunk for empty files, since other files will already ask about the big content deletion hunk. We could also change those files to simply display "stage this deletion", but showing the actual deleted content is probably what an interactive user wants. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 27, 2009 at 20:52 UTC 24ab81ae4d12c81076da256b3f9cdde45277f126
2 files changed +30 -5
git-add--interactive.perl
+13 -5
@@ -731,14 +731,17 @@ sub parse_diff_header {
731
732 my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
733 my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
734 + my $deletion = { TEXT => [], DISPLAY => [], TYPE => 'deletion' };
735
736 for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
736 - my $dest = $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ?
737 - $mode : $head;
737 + my $dest =
738 + $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ? $mode :
739 + $src->{TEXT}->[$i] =~ /^deleted file/ ? $deletion :
740 + $head;
741 push @{$dest->{TEXT}}, $src->{TEXT}->[$i];
742 push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];
743 }
741 - return ($head, $mode);
744 + return ($head, $mode, $deletion);
745 }
746
747 sub hunk_splittable {
@@ -1206,7 +1209,7 @@ sub patch_update_file {
1209 my ($ix, $num);
1210 my $path = shift;
1211 my ($head, @hunk) = parse_diff($path);
1209 - ($head, my $mode) = parse_diff_header($head);
1212 + ($head, my $mode, my $deletion) = parse_diff_header($head);
1213 for (@{$head->{DISPLAY}}) {
1214 print;
1215 }
@@ -1214,6 +1217,9 @@ sub patch_update_file {
1217 if (@{$mode->{TEXT}}) {
1218 unshift @hunk, $mode;
1219 }
1220 + if (@{$deletion->{TEXT}} && !@hunk) {
1221 + @hunk = ($deletion);
1222 + }
1223
1224 $num = scalar @hunk;
1225 $ix = 0;
@@ -1267,7 +1273,9 @@ sub patch_update_file {
1273 print;
1274 }
1275 print colored $prompt_color, $patch_mode_flavour{VERB},
1270 - ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' : ' this hunk'),
1276 + ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' :
1277 + $hunk[$ix]{TYPE} eq 'deletion' ? ' deletion' :
1278 + ' this hunk'),
1279 $patch_mode_flavour{TARGET},
1280 " [y,n,q,a,d,/$other,?]? ";
1281 my $line = prompt_single_character;
t/t3701-add-interactive.sh
+17
@@ -214,4 +214,21 @@ test_expect_success 'add first line works' '
214 test_cmp expected diff
215 '
216
217 +cat >expected <<EOF
218 +diff --git a/empty b/empty
219 +deleted file mode 100644
220 +index e69de29..0000000
221 +EOF
222 +
223 +test_expect_success 'deleting an empty file' '
224 + git reset --hard &&
225 + > empty &&
226 + git add empty &&
227 + git commit -m empty &&
228 + rm empty &&
229 + echo y | git add -p empty &&
230 + git diff --cached >diff &&
231 + test_cmp expected diff
232 +'
233 +
234 test_done