Skip to content

Commit b717a62

Browse files
peffgitster
authored andcommitted
add--interactive: ignore mode change in 'p'atch command
When a path is examined in the patch subcommand, any mode changes in the file are given to use in the diff header by git-diff. If no hunks are staged, then we throw out that header and do not touch the path. But if _any_ hunks are staged, we use the header, and the mode is changed together with the contents. Since the 'p'atch command should just be dealing with hunks that are shown to the user, it makes sense to just ignore mode changes entirely. We do squirrel away the mode, though, since the next patch will allow users to select the mode update separately. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1768905 commit b717a62

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

git-add--interactive.perl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,21 @@ sub parse_diff {
550550
return @hunk;
551551
}
552552

553+
sub parse_diff_header {
554+
my $src = shift;
555+
556+
my $head = { TEXT => [], DISPLAY => [] };
557+
my $mode = { TEXT => [], DISPLAY => [] };
558+
559+
for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
560+
my $dest = $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ?
561+
$mode : $head;
562+
push @{$dest->{TEXT}}, $src->{TEXT}->[$i];
563+
push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];
564+
}
565+
return ($head, $mode);
566+
}
567+
553568
sub hunk_splittable {
554569
my ($text) = @_;
555570

@@ -795,6 +810,7 @@ sub patch_update_file {
795810
my ($ix, $num);
796811
my $path = shift;
797812
my ($head, @hunk) = parse_diff($path);
813+
($head, my $mode) = parse_diff_header($head);
798814
for (@{$head->{DISPLAY}}) {
799815
print;
800816
}

t/t3701-add-interactive.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,13 @@ test_expect_success 'revert works (commit)' '
6666
grep "unchanged *+3/-0 file" output
6767
'
6868

69+
test_expect_success 'patch does not affect mode' '
70+
git reset --hard &&
71+
echo content >>file &&
72+
chmod +x file &&
73+
printf "y\\n" | git add -p &&
74+
git show :file | grep content &&
75+
git diff file | grep "new mode"
76+
'
77+
6978
test_done

0 commit comments

Comments
 (0)