@matheusbsbs / matheus-bagliano / commits / 3f6aff6889
Fork 0

Add subroutine to display one-line summary of hunks

This commit implements a rather simple-minded mechanism to display a one-line summary of the hunks in an array ref. The display consists of the line numbers and the first changed line, truncated to 80 characters. 20 lines are displayed at a time, and the index of the first undisplayed line is returned, allowing the caller to display more if desired. (The 20 and 80 should be made configurable.) Signed-off-by: William Pursell <bill.pursell@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

William Pursell committed Dec 4, 2008 at 10:00 UTC 3f6aff68896f8490e101eb5c4ad8f0fa5b8b67bb
1 file changed +41
git-add--interactive.perl
+41
@@ -836,6 +836,47 @@ sub patch_update_cmd {
836 }
837 }
838
839 +# Generate a one line summary of a hunk.
840 +sub summarize_hunk {
841 + my $rhunk = shift;
842 + my $summary = $rhunk->{TEXT}[0];
843 +
844 + # Keep the line numbers, discard extra context.
845 + $summary =~ s/@@(.*?)@@.*/$1 /s;
846 + $summary .= " " x (20 - length $summary);
847 +
848 + # Add some user context.
849 + for my $line (@{$rhunk->{TEXT}}) {
850 + if ($line =~ m/^[+-].*\w/) {
851 + $summary .= $line;
852 + last;
853 + }
854 + }
855 +
856 + chomp $summary;
857 + return substr($summary, 0, 80) . "\n";
858 +}
859 +
860 +
861 +# Print a one-line summary of each hunk in the array ref in
862 +# the first argument, starting wih the index in the 2nd.
863 +sub display_hunks {
864 + my ($hunks, $i) = @_;
865 + my $ctr = 0;
866 + $i ||= 0;
867 + for (; $i < @$hunks && $ctr < 20; $i++, $ctr++) {
868 + my $status = " ";
869 + if (defined $hunks->[$i]{USE}) {
870 + $status = $hunks->[$i]{USE} ? "+" : "-";
871 + }
872 + printf "%s%2d: %s",
873 + $status,
874 + $i + 1,
875 + summarize_hunk($hunks->[$i]);
876 + }
877 + return $i;
878 +}
879 +
880 sub patch_update_file {
881 my ($ix, $num);
882 my $path = shift;