Skip to content

Commit ce47dc0

Browse files
committed
Merge branch 'jk/add-i-mode'
* jk/add-i-mode: add--interactive: allow user to choose mode update add--interactive: ignore mode change in 'p'atch command
2 parents ba9f517 + ca72468 commit ce47dc0

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

git-add--interactive.perl

Lines changed: 49 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,9 +810,40 @@ 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
}
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+
801847
$num = scalar @hunk;
802848
$ix = 0;
803849

@@ -920,6 +966,9 @@ sub patch_update_file {
920966

921967
my $n_lofs = 0;
922968
my @result = ();
969+
if ($mode->{USE}) {
970+
push @result, @{$mode->{TEXT}};
971+
}
923972
for (@hunk) {
924973
my $text = $_->{TEXT};
925974
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =

t/t3701-add-interactive.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,23 @@ 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 "n\\ny\\n" | git add -p &&
74+
git show :file | grep content &&
75+
git diff file | grep "new mode"
76+
'
77+
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+
6988
test_done

0 commit comments

Comments
 (0)