Add 'g' command to go to a hunk
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>
William Pursell committed
Dec 4, 2008 at 10:22 UTC
070434d02b96ae0fac0a3364c202136f5eb5a7a9
1 file changed
+26
git-add--interactive.perl
+26
@@ -800,6 +800,7 @@ y - stage this hunk
800
n - do not stage this hunk
801
a - stage this and all the remaining hunks in the file
802
d - do not stage this hunk nor any of the remaining hunks in the file
803
+g - select a hunk to go to
804
j - leave this hunk undecided, see next undecided hunk
805
J - leave this hunk undecided, see next hunk
806
k - leave this hunk undecided, see previous undecided hunk
@@ -945,6 +946,9 @@ sub patch_update_file {
946
if ($ix < $num - 1) {
947
$other .= '/J';
948
}
949
+ if ($num > 1) {
950
+ $other .= '/g';
951
+ }
952
for ($i = 0; $i < $num; $i++) {
953
if (!defined $hunk[$i]{USE}) {
954
$undecided = 1;
@@ -978,6 +982,28 @@ sub patch_update_file {
982
}
983
next;
984
}
985
+ elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
986
+ my $response = $1;
987
+ my $no = $ix > 10 ? $ix - 10 : 0;
988
+ while ($response eq '') {
989
+ my $extra = "";
990
+ $no = display_hunks(\@hunk, $no);
991
+ if ($no < $num) {
992
+ $extra = " (<ret> to see more)";
993
+ }
994
+ print "go to which hunk$extra? ";
995
+ $response = <STDIN>;
996
+ chomp $response;
997
+ }
998
+ if ($response !~ /^\s*\d+\s*$/) {
999
+ print STDERR "Invalid number: '$response'\n";
1000
+ } elsif (0 < $response && $response <= $num) {
1001
+ $ix = $response - 1;
1002
+ } else {
1003
+ print STDERR "Sorry, only $num hunks available.\n";
1004
+ }
1005
+ next;
1006
+ }
1007
elsif ($line =~ /^d/i) {
1008
while ($ix < $num) {
1009
if (!defined $hunk[$ix]{USE}) {