X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=git-add--interactive.perl;h=ac598f88e62fc8f48aaaac8376ccde63cb3e2643;hb=44c637c8021e44253f0f8cb17391092e08b39e73;hp=dc3038091dd7db7dd4bb96cf8832df789b4d127f;hpb=5bac4a671907604b5fb4e24ff682d5b0e8431931;p=git.git diff --git a/git-add--interactive.perl b/git-add--interactive.perl index dc3038091..ac598f88e 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -3,9 +3,16 @@ use strict; sub run_cmd_pipe { - my $fh = undef; - open($fh, '-|', @_) or die; - return <$fh>; + if ($^O eq 'MSWin32') { + my @invalid = grep {m/[":*]/} @_; + die "$^O does not support: @invalid\n" if @invalid; + my @args = map { m/ /o ? "\"$_\"": $_ } @_; + return qx{@args}; + } else { + my $fh = undef; + open($fh, '-|', @_) or die; + return <$fh>; + } } my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir)); @@ -17,7 +24,7 @@ chomp($GIT_DIR); sub refresh { my $fh; - open $fh, '-|', qw(git update-index --refresh) + open $fh, 'git update-index --refresh |' or die; while (<$fh>) { ;# ignore 'needs update' @@ -206,9 +213,13 @@ sub list_and_choose { print ">> "; } my $line = ; - last if (!$line); + if (!$line) { + print "\n"; + $opts->{ON_EOF}->() if $opts->{ON_EOF}; + last; + } chomp $line; - my $donesomething = 0; + last if $line eq ''; for my $choice (split(/[\s,]+/, $line)) { my $choose = 1; my ($bottom, $top); @@ -240,12 +251,11 @@ sub list_and_choose { next TOPLOOP; } for ($i = $bottom-1; $i <= $top-1; $i++) { - next if (@stuff <= $i); + next if (@stuff <= $i || $i < 0); $chosen[$i] = $choose; - $donesomething++; } } - last if (!$donesomething || $opts->{IMMEDIATE}); + last if ($opts->{IMMEDIATE}); } for ($i = 0; $i < @stuff; $i++) { if ($chosen[$i]) { @@ -296,7 +306,7 @@ sub revert_cmd { my @lines = run_cmd_pipe(qw(git ls-tree HEAD --), map { $_->{VALUE} } @update); my $fh; - open $fh, '|-', qw(git update-index --index-info) + open $fh, '| git update-index --index-info' or die; for (@lines) { print $fh $_; @@ -350,7 +360,9 @@ sub hunk_splittable { sub parse_hunk_header { my ($line) = @_; my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = - $line =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/; + $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/; + $o_cnt = 1 unless defined $o_cnt; + $n_cnt = 1 unless defined $n_cnt; return ($o_ofs, $o_cnt, $n_ofs, $n_cnt); } @@ -362,9 +374,8 @@ sub split_hunk { # it can be split, but we would need to take care of # overlaps later. - my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]); + my ($o_ofs, undef, $n_ofs) = parse_hunk_header($text->[0]); my $hunk_start = 1; - my $next_hunk_start; OUTER: while (1) { @@ -431,8 +442,8 @@ sub split_hunk { for my $hunk (@split) { $o_ofs = $hunk->{OLD}; $n_ofs = $hunk->{NEW}; - $o_cnt = $hunk->{OCNT}; - $n_cnt = $hunk->{NCNT}; + my $o_cnt = $hunk->{OCNT}; + my $n_cnt = $hunk->{NCNT}; my $head = ("@@ -$o_ofs" . (($o_cnt != 1) ? ",$o_cnt" : '') . @@ -447,7 +458,7 @@ sub split_hunk { sub find_last_o_ctx { my ($it) = @_; my $text = $it->{TEXT}; - my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]); + my ($o_ofs, $o_cnt) = parse_hunk_header($text->[0]); my $i = @{$text}; my $last_o_ctx = $o_ofs + $o_cnt; while (0 < --$i) { @@ -519,8 +530,7 @@ sub coalesce_overlapping_hunks { for (grep { $_->{USE} } @in) { my $text = $_->{TEXT}; - my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = - parse_hunk_header($text->[0]); + my ($o_ofs) = parse_hunk_header($text->[0]); if (defined $last_o_ctx && $o_ofs <= $last_o_ctx) { merge_hunk($out[-1], $_); @@ -687,7 +697,7 @@ sub patch_update_cmd { @hunk = coalesce_overlapping_hunks(@hunk); - my ($o_lofs, $n_lofs) = (0, 0); + my $n_lofs = 0; my @result = (); for (@hunk) { my $text = $_->{TEXT}; @@ -695,9 +705,6 @@ sub patch_update_cmd { parse_hunk_header($text->[0]); if (!$_->{USE}) { - if (!defined $o_cnt) { $o_cnt = 1; } - if (!defined $n_cnt) { $n_cnt = 1; } - # We would have added ($n_cnt - $o_cnt) lines # to the postimage if we were to use this hunk, # but we didn't. So the line number that the next @@ -709,10 +716,10 @@ sub patch_update_cmd { if ($n_lofs) { $n_ofs += $n_lofs; $text->[0] = ("@@ -$o_ofs" . - ((defined $o_cnt) + (($o_cnt != 1) ? ",$o_cnt" : '') . " +$n_ofs" . - ((defined $n_cnt) + (($n_cnt != 1) ? ",$n_cnt" : '') . " @@\n"); } @@ -725,7 +732,7 @@ sub patch_update_cmd { if (@result) { my $fh; - open $fh, '|-', qw(git apply --cached); + open $fh, '| git apply --cached'; for (@{$head->{TEXT}}, @result) { print $fh $_; } @@ -784,6 +791,7 @@ sub main_loop { SINGLETON => 1, LIST_FLAT => 4, HEADER => '*** Commands ***', + ON_EOF => \&quit_cmd, IMMEDIATE => 1 }, @cmd); if ($it) { eval { @@ -796,8 +804,6 @@ sub main_loop { } } -my @z; - refresh(); status_cmd(); main_loop();