Commit 34005378 authored by Junio C Hamano's avatar Junio C Hamano

Merge branch 'wp/add-p-goto'

* wp/add-p-goto:
  Add 'g' command to go to a hunk
  Add subroutine to display one-line summary of hunks
parents 2fc64700 070434d0
......@@ -800,6 +800,7 @@ sub help_patch_cmd {
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
......@@ -836,6 +837,47 @@ sub patch_update_cmd {
}
}
# Generate a one line summary of a hunk.
sub summarize_hunk {
my $rhunk = shift;
my $summary = $rhunk->{TEXT}[0];
# Keep the line numbers, discard extra context.
$summary =~ s/@@(.*?)@@.*/$1 /s;
$summary .= " " x (20 - length $summary);
# Add some user context.
for my $line (@{$rhunk->{TEXT}}) {
if ($line =~ m/^[+-].*\w/) {
$summary .= $line;
last;
}
}
chomp $summary;
return substr($summary, 0, 80) . "\n";
}
# Print a one-line summary of each hunk in the array ref in
# the first argument, starting wih the index in the 2nd.
sub display_hunks {
my ($hunks, $i) = @_;
my $ctr = 0;
$i ||= 0;
for (; $i < @$hunks && $ctr < 20; $i++, $ctr++) {
my $status = " ";
if (defined $hunks->[$i]{USE}) {
$status = $hunks->[$i]{USE} ? "+" : "-";
}
printf "%s%2d: %s",
$status,
$i + 1,
summarize_hunk($hunks->[$i]);
}
return $i;
}
sub patch_update_file {
my ($ix, $num);
my $path = shift;
......@@ -904,6 +946,9 @@ sub patch_update_file {
if ($ix < $num - 1) {
$other .= '/J';
}
if ($num > 1) {
$other .= '/g';
}
for ($i = 0; $i < $num; $i++) {
if (!defined $hunk[$i]{USE}) {
$undecided = 1;
......@@ -937,6 +982,28 @@ sub patch_update_file {
}
next;
}
elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
my $response = $1;
my $no = $ix > 10 ? $ix - 10 : 0;
while ($response eq '') {
my $extra = "";
$no = display_hunks(\@hunk, $no);
if ($no < $num) {
$extra = " (<ret> to see more)";
}
print "go to which hunk$extra? ";
$response = <STDIN>;
chomp $response;
}
if ($response !~ /^\s*\d+\s*$/) {
print STDERR "Invalid number: '$response'\n";
} elsif (0 < $response && $response <= $num) {
$ix = $response - 1;
} else {
print STDERR "Sorry, only $num hunks available.\n";
}
next;
}
elsif ($line =~ /^d/i) {
while ($ix < $num) {
if (!defined $hunk[$ix]{USE}) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment