Merge branch 'pw/add-p-single'
authorJunio C Hamano <gitster@pobox.com>
Mon, 2 Apr 2018 17:10:54 +0000 (10:10 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 2 Apr 2018 17:10:55 +0000 (10:10 -0700)
Hotfix.

* pw/add-p-single:
add -p: fix 2.17.0-rc* regression due to moved code

1  2 
git-add--interactive.perl
index d190469cd8b5e1dc427b4029d0d1fb937faef584,811b3730c0798f98fede7a9a47538ec55aa81b49..c1f52e457f6c5b03947766555e10fba33e7e1b38
@@@ -677,7 -677,7 +677,7 @@@ sub add_untracked_cmd 
  sub run_git_apply {
        my $cmd = shift;
        my $fh;
 -      open $fh, '| git ' . $cmd . " --recount --allow-overlap";
 +      open $fh, '| git ' . $cmd . " --allow-overlap";
        print $fh @_;
        return close $fh;
  }
@@@ -705,14 -705,6 +705,14 @@@ sub parse_diff 
        }
        my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
  
 +      if (@colored && @colored != @diff) {
 +              print STDERR
 +                "fatal: mismatched output from interactive.diffFilter\n",
 +                "hint: Your filter must maintain a one-to-one correspondence\n",
 +                "hint: between its input and output lines.\n";
 +              exit 1;
 +      }
 +
        for (my $i = 0; $i < @diff; $i++) {
                if ($diff[$i] =~ /^@@ /) {
                        push @hunk, { TEXT => [], DISPLAY => [],
@@@ -759,15 -751,6 +759,15 @@@ sub parse_hunk_header 
        return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
  }
  
 +sub format_hunk_header {
 +      my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = @_;
 +      return ("@@ -$o_ofs" .
 +              (($o_cnt != 1) ? ",$o_cnt" : '') .
 +              " +$n_ofs" .
 +              (($n_cnt != 1) ? ",$n_cnt" : '') .
 +              " @@\n");
 +}
 +
  sub split_hunk {
        my ($text, $display) = @_;
        my @split = ();
                while (++$i < @$text) {
                        my $line = $text->[$i];
                        my $display = $display->[$i];
 +                      if ($line =~ /^\\/) {
 +                              push @{$this->{TEXT}}, $line;
 +                              push @{$this->{DISPLAY}}, $display;
 +                              next;
 +                      }
                        if ($line =~ /^ /) {
                                if ($this->{ADDDEL} &&
                                    !defined $next_hunk_start) {
                my $o_cnt = $hunk->{OCNT};
                my $n_cnt = $hunk->{NCNT};
  
 -              my $head = ("@@ -$o_ofs" .
 -                          (($o_cnt != 1) ? ",$o_cnt" : '') .
 -                          " +$n_ofs" .
 -                          (($n_cnt != 1) ? ",$n_cnt" : '') .
 -                          " @@\n");
 +              my $head = format_hunk_header($o_ofs, $o_cnt, $n_ofs, $n_cnt);
                my $display_head = $head;
                unshift @{$hunk->{TEXT}}, $head;
                if ($diff_use_color) {
@@@ -904,9 -886,6 +904,9 @@@ sub merge_hunk 
                        $n_cnt++;
                        push @line, $line;
                        next;
 +              } elsif ($line =~ /^\\/) {
 +                      push @line, $line;
 +                      next;
                }
  
                last if ($o1_ofs <= $ofs);
                        $n_cnt++;
                        push @line, $line;
                        next;
 +              } elsif ($line =~ /^\\/) {
 +                      push @line, $line;
 +                      next;
                }
                $ofs++;
                $o_cnt++;
                }
                push @line, $line;
        }
 -      my $head = ("@@ -$o0_ofs" .
 -                  (($o_cnt != 1) ? ",$o_cnt" : '') .
 -                  " +$n0_ofs" .
 -                  (($n_cnt != 1) ? ",$n_cnt" : '') .
 -                  " @@\n");
 +      my $head = format_hunk_header($o0_ofs, $o_cnt, $n0_ofs, $n_cnt);
        @{$prev->{TEXT}} = ($head, @line);
  }
  
@@@ -945,35 -925,14 +945,35 @@@ sub coalesce_overlapping_hunks 
        my @out = ();
  
        my ($last_o_ctx, $last_was_dirty);
 +      my $ofs_delta = 0;
  
 -      for (grep { $_->{USE} } @in) {
 +      for (@in) {
                if ($_->{TYPE} ne 'hunk') {
                        push @out, $_;
                        next;
                }
                my $text = $_->{TEXT};
 -              my ($o_ofs) = parse_hunk_header($text->[0]);
 +              my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
 +                                              parse_hunk_header($text->[0]);
 +              unless ($_->{USE}) {
 +                      $ofs_delta += $o_cnt - $n_cnt;
 +                      # If this hunk has been edited then subtract
 +                      # the delta that is due to the edit.
 +                      if ($_->{OFS_DELTA}) {
 +                              $ofs_delta -= $_->{OFS_DELTA};
 +                      }
 +                      next;
 +              }
 +              if ($ofs_delta) {
 +                      $n_ofs += $ofs_delta;
 +                      $_->{TEXT}->[0] = format_hunk_header($o_ofs, $o_cnt,
 +                                                           $n_ofs, $n_cnt);
 +              }
 +              # If this hunk was edited then adjust the offset delta
 +              # to reflect the edit.
 +              if ($_->{OFS_DELTA}) {
 +                      $ofs_delta += $_->{OFS_DELTA};
 +              }
                if (defined $last_o_ctx &&
                    $o_ofs <= $last_o_ctx &&
                    !$_->{DIRTY} &&
@@@ -1045,30 -1004,6 +1045,30 @@@ marked for discarding.")
  marked for applying."),
  );
  
 +sub recount_edited_hunk {
 +      local $_;
 +      my ($oldtext, $newtext) = @_;
 +      my ($o_cnt, $n_cnt) = (0, 0);
 +      for (@{$newtext}[1..$#{$newtext}]) {
 +              my $mode = substr($_, 0, 1);
 +              if ($mode eq '-') {
 +                      $o_cnt++;
 +              } elsif ($mode eq '+') {
 +                      $n_cnt++;
 +              } elsif ($mode eq ' ') {
 +                      $o_cnt++;
 +                      $n_cnt++;
 +              }
 +      }
 +      my ($o_ofs, undef, $n_ofs, undef) =
 +                                      parse_hunk_header($newtext->[0]);
 +      $newtext->[0] = format_hunk_header($o_ofs, $o_cnt, $n_ofs, $n_cnt);
 +      my (undef, $orig_o_cnt, undef, $orig_n_cnt) =
 +                                      parse_hunk_header($oldtext->[0]);
 +      # Return the change in the number of lines inserted by this hunk
 +      return $orig_o_cnt - $orig_n_cnt - $o_cnt + $n_cnt;
 +}
 +
  sub edit_hunk_manually {
        my ($oldtext) = @_;
  
@@@ -1167,32 -1102,25 +1167,32 @@@ sub prompt_yesno 
  }
  
  sub edit_hunk_loop {
 -      my ($head, $hunk, $ix) = @_;
 -      my $text = $hunk->[$ix]->{TEXT};
 +      my ($head, $hunks, $ix) = @_;
 +      my $hunk = $hunks->[$ix];
 +      my $text = $hunk->{TEXT};
  
        while (1) {
 -              $text = edit_hunk_manually($text);
 -              if (!defined $text) {
 +              my $newtext = edit_hunk_manually($text);
 +              if (!defined $newtext) {
                        return undef;
                }
                my $newhunk = {
 -                      TEXT => $text,
 -                      TYPE => $hunk->[$ix]->{TYPE},
 +                      TEXT => $newtext,
 +                      TYPE => $hunk->{TYPE},
                        USE => 1,
                        DIRTY => 1,
                };
 +              $newhunk->{OFS_DELTA} = recount_edited_hunk($text, $newtext);
 +              # If this hunk has already been edited then add the
 +              # offset delta of the previous edit to get the real
 +              # delta from the original unedited hunk.
 +              $hunk->{OFS_DELTA} and
 +                              $newhunk->{OFS_DELTA} += $hunk->{OFS_DELTA};
                if (diff_applies($head,
 -                               @{$hunk}[0..$ix-1],
 +                               @{$hunks}[0..$ix-1],
                                 $newhunk,
 -                               @{$hunk}[$ix+1..$#{$hunk}])) {
 -                      $newhunk->{DISPLAY} = [color_diff(@{$text})];
 +                               @{$hunks}[$ix+1..$#{$hunks}])) {
 +                      $newhunk->{DISPLAY} = [color_diff(@{$newtext})];
                        return $newhunk;
                }
                else {
@@@ -1564,7 -1492,7 +1564,7 @@@ sub patch_update_file 
                                        error_msg __("No other hunks to search\n");
                                        next;
                                }
-                               if ($1 eq "") {
+                               if ($regex eq "") {
                                        print colored $prompt_color, __("search for regex? ");
                                        $regex = <STDIN>;
                                        if (defined $regex) {