Skip to content

Commit

Permalink
Add 'g' command to go to a hunk
Browse files Browse the repository at this point in the history
When a minor change is made while the working directory is in a bit of a
mess, it is somewhat difficult to wade through all of the hunks using git
add --patch.  This allows one to jump to the hunk that needs to be staged
without having to respond 'n' to each preceding hunk.

Signed-off-by: William Pursell <bill.pursell@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
William Pursell authored and Junio C Hamano committed Dec 5, 2008
1 parent 3f6aff6 commit 070434d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions git-add--interactive.perl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -945,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;
Expand Down Expand Up @@ -978,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}) {
Expand Down

0 comments on commit 070434d

Please sign in to comment.