Skip to content

Commit ca72468

Browse files
peffgitster
authored andcommitted
add--interactive: allow user to choose mode update
When using the 'p'atch command, instead of just throwing out any mode change, present it to the user in the same way that we show hunks. This way, the mode change can be staged independently from the changes to the contents. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b717a62 commit ca72468

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

git-add--interactive.perl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,36 @@ sub patch_update_file {
814814
for (@{$head->{DISPLAY}}) {
815815
print;
816816
}
817+
818+
if (@{$mode->{TEXT}}) {
819+
while (1) {
820+
print @{$mode->{DISPLAY}};
821+
print colored $prompt_color,
822+
"Stage mode change [y/n/a/d/?]? ";
823+
my $line = <STDIN>;
824+
if ($line =~ /^y/i) {
825+
$mode->{USE} = 1;
826+
last;
827+
}
828+
elsif ($line =~ /^n/i) {
829+
$mode->{USE} = 0;
830+
last;
831+
}
832+
elsif ($line =~ /^a/i) {
833+
$_->{USE} = 1 foreach ($mode, @hunk);
834+
last;
835+
}
836+
elsif ($line =~ /^d/i) {
837+
$_->{USE} = 0 foreach ($mode, @hunk);
838+
last;
839+
}
840+
else {
841+
help_patch_cmd('');
842+
next;
843+
}
844+
}
845+
}
846+
817847
$num = scalar @hunk;
818848
$ix = 0;
819849

@@ -936,6 +966,9 @@ sub patch_update_file {
936966

937967
my $n_lofs = 0;
938968
my @result = ();
969+
if ($mode->{USE}) {
970+
push @result, @{$mode->{TEXT}};
971+
}
939972
for (@hunk) {
940973
my $text = $_->{TEXT};
941974
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =

t/t3701-add-interactive.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,19 @@ test_expect_success 'patch does not affect mode' '
7070
git reset --hard &&
7171
echo content >>file &&
7272
chmod +x file &&
73-
printf "y\\n" | git add -p &&
73+
printf "n\\ny\\n" | git add -p &&
7474
git show :file | grep content &&
7575
git diff file | grep "new mode"
7676
'
7777

78+
test_expect_success 'stage mode but not hunk' '
79+
git reset --hard &&
80+
echo content >>file &&
81+
chmod +x file &&
82+
printf "y\\nn\\n" | git add -p &&
83+
git diff --cached file | grep "new mode" &&
84+
git diff file | grep "+content"
85+
'
86+
87+
7888
test_done

0 commit comments

Comments
 (0)