@matheusbsbs / matheus-bagliano / commits / b717a62762
Fork 0

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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 27, 2008 at 03:30 UTC b717a627629e6886956af54274d507b9711d49e6
2 files changed +25
git-add--interactive.perl
+16
@@ -550,6 +550,21 @@ sub parse_diff {
550 return @hunk;
551 }
552
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 +
568 sub hunk_splittable {
569 my ($text) = @_;
570
@@ -795,6 +810,7 @@ sub patch_update_file {
810 my ($ix, $num);
811 my $path = shift;
812 my ($head, @hunk) = parse_diff($path);
813 + ($head, my $mode) = parse_diff_header($head);
814 for (@{$head->{DISPLAY}}) {
815 print;
816 }
t/t3701-add-interactive.sh
+9
@@ -66,4 +66,13 @@ test_expect_success 'revert works (commit)' '
66 grep "unchanged *+3/-0 file" output
67 '
68
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 +
78 test_done