add -i: add function to format hunk header

This code is duplicated in a couple of places so make it into a function as we're going to add some more callers shortly. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Feb 19, 2018 at 11:29 UTC 492e60c82456b228a24ff0021e6accee946b7a1c
1 file changed +11 -10
git-add--interactive.perl
+11 -10
@@ -751,6 +751,15 @@ sub parse_hunk_header {
751 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
752 }
753
754 +sub format_hunk_header {
755 + my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = @_;
756 + return ("@@ -$o_ofs" .
757 + (($o_cnt != 1) ? ",$o_cnt" : '') .
758 + " +$n_ofs" .
759 + (($n_cnt != 1) ? ",$n_cnt" : '') .
760 + " @@\n");
761 +}
762 +
763 sub split_hunk {
764 my ($text, $display) = @_;
765 my @split = ();
@@ -838,11 +847,7 @@ sub split_hunk {
847 my $o_cnt = $hunk->{OCNT};
848 my $n_cnt = $hunk->{NCNT};
849
841 - my $head = ("@@ -$o_ofs" .
842 - (($o_cnt != 1) ? ",$o_cnt" : '') .
843 - " +$n_ofs" .
844 - (($n_cnt != 1) ? ",$n_cnt" : '') .
845 - " @@\n");
850 + my $head = format_hunk_header($o_ofs, $o_cnt, $n_ofs, $n_cnt);
851 my $display_head = $head;
852 unshift @{$hunk->{TEXT}}, $head;
853 if ($diff_use_color) {
@@ -912,11 +917,7 @@ sub merge_hunk {
917 }
918 push @line, $line;
919 }
915 - my $head = ("@@ -$o0_ofs" .
916 - (($o_cnt != 1) ? ",$o_cnt" : '') .
917 - " +$n0_ofs" .
918 - (($n_cnt != 1) ? ",$n_cnt" : '') .
919 - " @@\n");
920 + my $head = format_hunk_header($o0_ofs, $o_cnt, $n0_ofs, $n_cnt);
921 @{$prev->{TEXT}} = ($head, @line);
922 }
923