Skip to content

Commit 24ab81a

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

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

git-add--interactive.perl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -731,14 +731,17 @@ sub parse_diff_header {
731731

732732
my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
733733
my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
734+
my $deletion = { TEXT => [], DISPLAY => [], TYPE => 'deletion' };
734735

735736
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;
738741
push @{$dest->{TEXT}}, $src->{TEXT}->[$i];
739742
push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];
740743
}
741-
return ($head, $mode);
744+
return ($head, $mode, $deletion);
742745
}
743746

744747
sub hunk_splittable {
@@ -1206,14 +1209,17 @@ sub patch_update_file {
12061209
my ($ix, $num);
12071210
my $path = shift;
12081211
my ($head, @hunk) = parse_diff($path);
1209-
($head, my $mode) = parse_diff_header($head);
1212+
($head, my $mode, my $deletion) = parse_diff_header($head);
12101213
for (@{$head->{DISPLAY}}) {
12111214
print;
12121215
}
12131216

12141217
if (@{$mode->{TEXT}}) {
12151218
unshift @hunk, $mode;
12161219
}
1220+
if (@{$deletion->{TEXT}} && !@hunk) {
1221+
@hunk = ($deletion);
1222+
}
12171223

12181224
$num = scalar @hunk;
12191225
$ix = 0;
@@ -1267,7 +1273,9 @@ sub patch_update_file {
12671273
print;
12681274
}
12691275
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'),
12711279
$patch_mode_flavour{TARGET},
12721280
" [y,n,q,a,d,/$other,?]? ";
12731281
my $line = prompt_single_character;

t/t3701-add-interactive.sh

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

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+
217234
test_done

0 commit comments

Comments
 (0)