Skip to content

Commit 7535e5a

Browse files
peffgitster
authored andcommitted
add-interactive: refactor mode hunk handling
The original implementation considered the mode separately from the rest of the hunks, asking about it outside the main hunk-selection loop. This patch instead places a mode change as the first hunk in the loop. This has two advantages: 1. less duplicated code (since we use the main selection loop). This also cleans up an inconsistency, which is that the main selection loop separates options with a comma, whereas the mode prompt used slashes. 2. users can now skip the mode change and come back to it, search for it (via "/mode"), etc, as they can with other hunks. To facilitate this, each hunk is now marked with a "type". Mode hunks are not considered for splitting (which would make no sense, and also confuses the split_hunk function), nor are they editable. In theory, one could edit the mode lines and change to a new mode. In practice, there are only two modes that git cares about (0644 and 0755), so either you want to move from one to the other or not (and you can do that by staging or not staging). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cbd3a01 commit 7535e5a

File tree

1 file changed

+21
-43
lines changed

1 file changed

+21
-43
lines changed

git-add--interactive.perl

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,12 @@ sub parse_diff {
620620
if ($diff_use_color) {
621621
@colored = run_cmd_pipe(qw(git diff-files -p --color --), $path);
622622
}
623-
my (@hunk) = { TEXT => [], DISPLAY => [] };
623+
my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
624624

625625
for (my $i = 0; $i < @diff; $i++) {
626626
if ($diff[$i] =~ /^@@ /) {
627-
push @hunk, { TEXT => [], DISPLAY => [] };
627+
push @hunk, { TEXT => [], DISPLAY => [],
628+
TYPE => 'hunk' };
628629
}
629630
push @{$hunk[-1]{TEXT}}, $diff[$i];
630631
push @{$hunk[-1]{DISPLAY}},
@@ -636,8 +637,8 @@ sub parse_diff {
636637
sub parse_diff_header {
637638
my $src = shift;
638639

639-
my $head = { TEXT => [], DISPLAY => [] };
640-
my $mode = { TEXT => [], DISPLAY => [] };
640+
my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
641+
my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
641642

642643
for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
643644
my $dest = $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ?
@@ -684,6 +685,7 @@ sub split_hunk {
684685
my $this = +{
685686
TEXT => [],
686687
DISPLAY => [],
688+
TYPE => 'hunk',
687689
OLD => $o_ofs,
688690
NEW => $n_ofs,
689691
OCNT => 0,
@@ -869,7 +871,11 @@ sub edit_hunk_loop {
869871
if (!defined $text) {
870872
return undef;
871873
}
872-
my $newhunk = { TEXT => $text, USE => 1 };
874+
my $newhunk = {
875+
TEXT => $text,
876+
TYPE => $hunk->[$ix]->{TYPE},
877+
USE => 1
878+
};
873879
if (diff_applies($head,
874880
@{$hunk}[0..$ix-1],
875881
$newhunk,
@@ -983,37 +989,7 @@ sub patch_update_file {
983989
}
984990

985991
if (@{$mode->{TEXT}}) {
986-
while (1) {
987-
print @{$mode->{DISPLAY}};
988-
print colored $prompt_color,
989-
"Stage mode change [y/n/a/d/?]? ";
990-
my $line = prompt_single_character;
991-
if ($line =~ /^y/i) {
992-
$mode->{USE} = 1;
993-
last;
994-
}
995-
elsif ($line =~ /^n/i) {
996-
$mode->{USE} = 0;
997-
last;
998-
}
999-
elsif ($line =~ /^a/i) {
1000-
$_->{USE} = 1 foreach ($mode, @hunk);
1001-
last;
1002-
}
1003-
elsif ($line =~ /^d/i) {
1004-
$_->{USE} = 0 foreach ($mode, @hunk);
1005-
last;
1006-
}
1007-
elsif ($line =~ /^q/i) {
1008-
$_->{USE} = 0 foreach ($mode, @hunk);
1009-
$quit = 1;
1010-
last;
1011-
}
1012-
else {
1013-
help_patch_cmd('');
1014-
next;
1015-
}
1016-
}
992+
unshift @hunk, $mode;
1017993
}
1018994

1019995
$num = scalar @hunk;
@@ -1057,14 +1033,19 @@ sub patch_update_file {
10571033
}
10581034
last if (!$undecided);
10591035

1060-
if (hunk_splittable($hunk[$ix]{TEXT})) {
1036+
if ($hunk[$ix]{TYPE} eq 'hunk' &&
1037+
hunk_splittable($hunk[$ix]{TEXT})) {
10611038
$other .= ',s';
10621039
}
1063-
$other .= ',e';
1040+
if ($hunk[$ix]{TYPE} eq 'hunk') {
1041+
$other .= ',e';
1042+
}
10641043
for (@{$hunk[$ix]{DISPLAY}}) {
10651044
print;
10661045
}
1067-
print colored $prompt_color, "Stage this hunk [y,n,a,d,/$other,?]? ";
1046+
print colored $prompt_color, 'Stage ',
1047+
($hunk[$ix]{TYPE} eq 'mode' ? 'mode change' : 'this hunk'),
1048+
" [y,n,a,d,/$other,?]? ";
10681049
my $line = prompt_single_character;
10691050
if ($line) {
10701051
if ($line =~ /^y/i) {
@@ -1206,7 +1187,7 @@ sub patch_update_file {
12061187
$num = scalar @hunk;
12071188
next;
12081189
}
1209-
elsif ($line =~ /^e/) {
1190+
elsif ($other =~ /e/ && $line =~ /^e/) {
12101191
my $newhunk = edit_hunk_loop($head, \@hunk, $ix);
12111192
if (defined $newhunk) {
12121193
splice @hunk, $ix, 1, $newhunk;
@@ -1227,9 +1208,6 @@ sub patch_update_file {
12271208

12281209
my $n_lofs = 0;
12291210
my @result = ();
1230-
if ($mode->{USE}) {
1231-
push @result, @{$mode->{TEXT}};
1232-
}
12331211
for (@hunk) {
12341212
if ($_->{USE}) {
12351213
push @result, @{$_->{TEXT}};

0 commit comments

Comments
 (0)