Code

Fix off-by-one in read_tree_recursive
[git.git] / git-add--interactive.perl
index 30ddab293b4b4eb2862d068b352e8ccedc31ea5e..566e3710f5275bb751965744ff1d53ecaa809c97 100755 (executable)
@@ -3,6 +3,8 @@
 use strict;
 use Git;
 
+binmode(STDOUT, ":raw");
+
 my $repo = Git->repository();
 
 my $menu_use_color = $repo->get_colorbool('color.interactive');
@@ -12,6 +14,13 @@ my ($prompt_color, $header_color, $help_color) =
                $repo->get_color('color.interactive.header', 'bold'),
                $repo->get_color('color.interactive.help', 'red bold'),
        ) : ();
+my $error_color = ();
+if ($menu_use_color) {
+       my $help_color_spec = ($repo->config('color.interactive.help') or
+                               'red bold');
+       $error_color = $repo->get_color('color.interactive.error',
+                                       $help_color_spec);
+}
 
 my $diff_use_color = $repo->get_colorbool('color.diff');
 my ($fraginfo_color) =
@@ -33,6 +42,17 @@ my ($diff_new_color) =
 
 my $normal_color = $repo->get_color("", "reset");
 
+my $use_readkey = 0;
+sub ReadMode;
+sub ReadKey;
+if ($repo->config_bool("interactive.singlekey")) {
+       eval {
+               require Term::ReadKey;
+               Term::ReadKey->import;
+               $use_readkey = 1;
+       };
+}
+
 sub colored {
        my $color = shift;
        my $string = join("", @_);
@@ -73,6 +93,47 @@ if (!defined $GIT_DIR) {
 }
 chomp($GIT_DIR);
 
+my %cquote_map = (
+ "b" => chr(8),
+ "t" => chr(9),
+ "n" => chr(10),
+ "v" => chr(11),
+ "f" => chr(12),
+ "r" => chr(13),
+ "\\" => "\\",
+ "\042" => "\042",
+);
+
+sub unquote_path {
+       local ($_) = @_;
+       my ($retval, $remainder);
+       if (!/^\042(.*)\042$/) {
+               return $_;
+       }
+       ($_, $retval) = ($1, "");
+       while (/^([^\\]*)\\(.*)$/) {
+               $remainder = $2;
+               $retval .= $1;
+               for ($remainder) {
+                       if (/^([0-3][0-7][0-7])(.*)$/) {
+                               $retval .= chr(oct($1));
+                               $_ = $2;
+                               last;
+                       }
+                       if (/^([\\\042btnvfr])(.*)$/) {
+                               $retval .= $cquote_map{$1};
+                               $_ = $2;
+                               last;
+                       }
+                       # This is malformed -- just return it as-is for now.
+                       return $_[0];
+               }
+               $_ = $remainder;
+       }
+       $retval .= $_;
+       return $retval;
+}
+
 sub refresh {
        my $fh;
        open $fh, 'git update-index --refresh |'
@@ -86,7 +147,7 @@ sub refresh {
 sub list_untracked {
        map {
                chomp $_;
-               $_;
+               unquote_path($_);
        }
        run_cmd_pipe(qw(git ls-files --others --exclude-standard --), @ARGV);
 }
@@ -123,7 +184,8 @@ sub list_modified {
 
        if (@ARGV) {
                @tracked = map {
-                       chomp $_; $_;
+                       chomp $_;
+                       unquote_path($_);
                } run_cmd_pipe(qw(git ls-files --exclude-standard --), @ARGV);
                return if (!@tracked);
        }
@@ -135,6 +197,7 @@ sub list_modified {
                if (($add, $del, $file) =
                    /^([-\d]+)  ([-\d]+)        (.*)/) {
                        my ($change, $bin);
+                       $file = unquote_path($file);
                        if ($add eq '-' && $del eq '-') {
                                $change = 'binary';
                                $bin = 1;
@@ -150,6 +213,7 @@ sub list_modified {
                }
                elsif (($adddel, $file) =
                       /^ (create|delete) mode [0-7]+ (.*)$/) {
+                       $file = unquote_path($file);
                        $data{$file}{INDEX_ADDDEL} = $adddel;
                }
        }
@@ -157,6 +221,7 @@ sub list_modified {
        for (run_cmd_pipe(qw(git diff-files --numstat --summary --), @tracked)) {
                if (($add, $del, $file) =
                    /^([-\d]+)  ([-\d]+)        (.*)/) {
+                       $file = unquote_path($file);
                        if (!exists $data{$file}) {
                                $data{$file} = +{
                                        INDEX => 'unchanged',
@@ -178,6 +243,7 @@ sub list_modified {
                }
                elsif (($adddel, $file) =
                       /^ (create|delete) mode [0-7]+ (.*)$/) {
+                       $file = unquote_path($file);
                        $data{$file}{FILE_ADDDEL} = $adddel;
                }
        }
@@ -284,7 +350,8 @@ sub find_unique_prefixes {
                        }
                        %search = %{$search{$letter}};
                }
-               if ($soft_limit && $j + 1 > $soft_limit) {
+               if (ord($letters[0]) > 127 ||
+                   ($soft_limit && $j + 1 > $soft_limit)) {
                        $prefix = undef;
                        $remainder = $ret;
                }
@@ -325,6 +392,10 @@ sub highlight_prefix {
        return "$prompt_color$prefix$normal_color$remainder";
 }
 
+sub error_msg {
+       print STDERR colored $error_color, @_;
+}
+
 sub list_and_choose {
        my ($opts, @stuff) = @_;
        my (@chosen, @return);
@@ -420,12 +491,12 @@ sub list_and_choose {
                        else {
                                $bottom = $top = find_unique($choice, @stuff);
                                if (!defined $bottom) {
-                                       print "Huh ($choice)?\n";
+                                       error_msg "Huh ($choice)?\n";
                                        next TOPLOOP;
                                }
                        }
                        if ($opts->{SINGLETON} && $bottom != $top) {
-                               print "Huh ($choice)?\n";
+                               error_msg "Huh ($choice)?\n";
                                next TOPLOOP;
                        }
                        for ($i = $bottom-1; $i <= $top-1; $i++) {
@@ -549,11 +620,12 @@ sub parse_diff {
        if ($diff_use_color) {
                @colored = run_cmd_pipe(qw(git diff-files -p --color --), $path);
        }
-       my (@hunk) = { TEXT => [], DISPLAY => [] };
+       my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
 
        for (my $i = 0; $i < @diff; $i++) {
                if ($diff[$i] =~ /^@@ /) {
-                       push @hunk, { TEXT => [], DISPLAY => [] };
+                       push @hunk, { TEXT => [], DISPLAY => [],
+                               TYPE => 'hunk' };
                }
                push @{$hunk[-1]{TEXT}}, $diff[$i];
                push @{$hunk[-1]{DISPLAY}},
@@ -565,8 +637,8 @@ sub parse_diff {
 sub parse_diff_header {
        my $src = shift;
 
-       my $head = { TEXT => [], DISPLAY => [] };
-       my $mode = { TEXT => [], DISPLAY => [] };
+       my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
+       my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
 
        for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
                my $dest = $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ?
@@ -613,6 +685,7 @@ sub split_hunk {
                my $this = +{
                        TEXT => [],
                        DISPLAY => [],
+                       TYPE => 'hunk',
                        OLD => $o_ofs,
                        NEW => $n_ofs,
                        OCNT => 0,
@@ -758,11 +831,32 @@ sub diff_applies {
        return close $fh;
 }
 
+sub _restore_terminal_and_die {
+       ReadMode 'restore';
+       print "\n";
+       exit 1;
+}
+
+sub prompt_single_character {
+       if ($use_readkey) {
+               local $SIG{TERM} = \&_restore_terminal_and_die;
+               local $SIG{INT} = \&_restore_terminal_and_die;
+               ReadMode 'cbreak';
+               my $key = ReadKey 0;
+               ReadMode 'restore';
+               print "$key" if defined $key;
+               print "\n";
+               return $key;
+       } else {
+               return <STDIN>;
+       }
+}
+
 sub prompt_yesno {
        my ($prompt) = @_;
        while (1) {
                print colored $prompt_color, $prompt;
-               my $line = <STDIN>;
+               my $line = prompt_single_character;
                return 0 if $line =~ /^n/i;
                return 1 if $line =~ /^y/i;
        }
@@ -777,7 +871,11 @@ sub edit_hunk_loop {
                if (!defined $text) {
                        return undef;
                }
-               my $newhunk = { TEXT => $text, USE => 1 };
+               my $newhunk = {
+                       TEXT => $text,
+                       TYPE => $hunk->[$ix]->{TYPE},
+                       USE => 1
+               };
                if (diff_applies($head,
                                 @{$hunk}[0..$ix-1],
                                 $newhunk,
@@ -798,6 +896,7 @@ sub help_patch_cmd {
        print colored $help_color, <<\EOF ;
 y - stage this hunk
 n - do not stage this hunk
+q - quit, do not stage this hunk nor any of the remaining ones
 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
@@ -834,7 +933,7 @@ sub patch_update_cmd {
                                        @mods);
        }
        for (@them) {
-               patch_update_file($_->{VALUE});
+               return 0 if patch_update_file($_->{VALUE});
        }
 }
 
@@ -880,6 +979,7 @@ sub display_hunks {
 }
 
 sub patch_update_file {
+       my $quit = 0;
        my ($ix, $num);
        my $path = shift;
        my ($head, @hunk) = parse_diff($path);
@@ -889,32 +989,7 @@ sub patch_update_file {
        }
 
        if (@{$mode->{TEXT}}) {
-               while (1) {
-                       print @{$mode->{DISPLAY}};
-                       print colored $prompt_color,
-                               "Stage mode change [y/n/a/d/?]? ";
-                       my $line = <STDIN>;
-                       if ($line =~ /^y/i) {
-                               $mode->{USE} = 1;
-                               last;
-                       }
-                       elsif ($line =~ /^n/i) {
-                               $mode->{USE} = 0;
-                               last;
-                       }
-                       elsif ($line =~ /^a/i) {
-                               $_->{USE} = 1 foreach ($mode, @hunk);
-                               last;
-                       }
-                       elsif ($line =~ /^d/i) {
-                               $_->{USE} = 0 foreach ($mode, @hunk);
-                               last;
-                       }
-                       else {
-                               help_patch_cmd('');
-                               next;
-                       }
-               }
+               unshift @hunk, $mode;
        }
 
        $num = scalar @hunk;
@@ -948,7 +1023,7 @@ sub patch_update_file {
                        $other .= ',J';
                }
                if ($num > 1) {
-                       $other .= '/g';
+                       $other .= ',g';
                }
                for ($i = 0; $i < $num; $i++) {
                        if (!defined $hunk[$i]{USE}) {
@@ -958,15 +1033,20 @@ sub patch_update_file {
                }
                last if (!$undecided);
 
-               if (hunk_splittable($hunk[$ix]{TEXT})) {
+               if ($hunk[$ix]{TYPE} eq 'hunk' &&
+                   hunk_splittable($hunk[$ix]{TEXT})) {
                        $other .= ',s';
                }
-               $other .= ',e';
+               if ($hunk[$ix]{TYPE} eq 'hunk') {
+                       $other .= ',e';
+               }
                for (@{$hunk[$ix]{DISPLAY}}) {
                        print;
                }
-               print colored $prompt_color, "Stage this hunk [y,n,a,d,/$other,?]? ";
-               my $line = <STDIN>;
+               print colored $prompt_color, 'Stage ',
+                 ($hunk[$ix]{TYPE} eq 'mode' ? 'mode change' : 'this hunk'),
+                 " [y,n,a,d,/$other,?]? ";
+               my $line = prompt_single_character;
                if ($line) {
                        if ($line =~ /^y/i) {
                                $hunk[$ix]{USE} = 1;
@@ -994,14 +1074,17 @@ sub patch_update_file {
                                        }
                                        print "go to which hunk$extra? ";
                                        $response = <STDIN>;
+                                       if (!defined $response) {
+                                               $response = '';
+                                       }
                                        chomp $response;
                                }
                                if ($response !~ /^\s*\d+\s*$/) {
-                                       print STDERR "Invalid number: '$response'\n";
+                                       error_msg "Invalid number: '$response'\n";
                                } elsif (0 < $response && $response <= $num) {
                                        $ix = $response - 1;
                                } else {
-                                       print STDERR "Sorry, only $num hunks available.\n";
+                                       error_msg "Sorry, only $num hunks available.\n";
                                }
                                next;
                        }
@@ -1014,15 +1097,33 @@ sub patch_update_file {
                                }
                                next;
                        }
+                       elsif ($line =~ /^q/i) {
+                               while ($ix < $num) {
+                                       if (!defined $hunk[$ix]{USE}) {
+                                               $hunk[$ix]{USE} = 0;
+                                       }
+                                       $ix++;
+                               }
+                               $quit = 1;
+                               next;
+                       }
                        elsif ($line =~ m|^/(.*)|) {
+                               my $regex = $1;
+                               if ($1 eq "") {
+                                       print colored $prompt_color, "search for regex? ";
+                                       $regex = <STDIN>;
+                                       if (defined $regex) {
+                                               chomp $regex;
+                                       }
+                               }
                                my $search_string;
                                eval {
-                                       $search_string = qr{$1}m;
+                                       $search_string = qr{$regex}m;
                                };
                                if ($@) {
                                        my ($err,$exp) = ($@, $1);
                                        $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
-                                       print STDERR "Malformed search regexp $exp: $err\n";
+                                       error_msg "Malformed search regexp $exp: $err\n";
                                        next;
                                }
                                my $iy = $ix;
@@ -1032,7 +1133,7 @@ sub patch_update_file {
                                        $iy++;
                                        $iy = 0 if ($iy >= $num);
                                        if ($ix == $iy) {
-                                               print STDERR "No hunk matches the given pattern\n";
+                                               error_msg "No hunk matches the given pattern\n";
                                                last;
                                        }
                                }
@@ -1044,7 +1145,7 @@ sub patch_update_file {
                                        $ix--;
                                }
                                else {
-                                       print STDERR "No previous hunk\n";
+                                       error_msg "No previous hunk\n";
                                }
                                next;
                        }
@@ -1053,7 +1154,7 @@ sub patch_update_file {
                                        $ix++;
                                }
                                else {
-                                       print STDERR "No next hunk\n";
+                                       error_msg "No next hunk\n";
                                }
                                next;
                        }
@@ -1066,13 +1167,13 @@ sub patch_update_file {
                                        }
                                }
                                else {
-                                       print STDERR "No previous hunk\n";
+                                       error_msg "No previous hunk\n";
                                }
                                next;
                        }
                        elsif ($line =~ /^j/) {
                                if ($other !~ /j/) {
-                                       print STDERR "No next hunk\n";
+                                       error_msg "No next hunk\n";
                                        next;
                                }
                        }
@@ -1086,7 +1187,7 @@ sub patch_update_file {
                                $num = scalar @hunk;
                                next;
                        }
-                       elsif ($line =~ /^e/) {
+                       elsif ($other =~ /e/ && $line =~ /^e/) {
                                my $newhunk = edit_hunk_loop($head, \@hunk, $ix);
                                if (defined $newhunk) {
                                        splice @hunk, $ix, 1, $newhunk;
@@ -1107,9 +1208,6 @@ sub patch_update_file {
 
        my $n_lofs = 0;
        my @result = ();
-       if ($mode->{USE}) {
-               push @result, @{$mode->{TEXT}};
-       }
        for (@hunk) {
                if ($_->{USE}) {
                        push @result, @{$_->{TEXT}};
@@ -1132,6 +1230,7 @@ sub patch_update_file {
        }
 
        print "\n";
+       return $quit;
 }
 
 sub diff_cmd {