X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=git-add--interactive.perl;h=8f0839d205e0c4010e256bb5cf81c73cc2f438ab;hb=fdec2eb8ebbf995a77e2ab9971565c792b52944a;hp=cd43c3491260cb2aa51f0d19fd18ab66e4ad8217;hpb=c444c16589f95ac22d8e3ffe603cd7f0613512ce;p=git.git diff --git a/git-add--interactive.perl b/git-add--interactive.perl index cd43c3491..8f0839d20 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -1,6 +1,8 @@ -#!/usr/bin/perl -w +#!/usr/bin/perl +use 5.008; use strict; +use warnings; use Git; binmode(STDOUT, ":raw"); @@ -43,6 +45,9 @@ my ($diff_new_color) = my $normal_color = $repo->get_color("", "reset"); my $use_readkey = 0; +my $use_termcap = 0; +my %term_escapes; + sub ReadMode; sub ReadKey; if ($repo->config_bool("interactive.singlekey")) { @@ -51,6 +56,14 @@ if ($repo->config_bool("interactive.singlekey")) { Term::ReadKey->import; $use_readkey = 1; }; + eval { + require Term::Cap; + my $termcap = Term::Cap->Tgetent; + foreach (values %$termcap) { + $term_escapes{$_} = 1 if /^\e/; + } + $use_termcap = 1; + }; } sub colored { @@ -87,6 +100,7 @@ my %patch_modes = ( TARGET => '', PARTICIPLE => 'staging', FILTER => 'file-only', + IS_REVERSE => 0, }, 'stash' => { DIFF => 'diff-index -p HEAD', @@ -96,6 +110,7 @@ my %patch_modes = ( TARGET => '', PARTICIPLE => 'stashing', FILTER => undef, + IS_REVERSE => 0, }, 'reset_head' => { DIFF => 'diff-index -p --cached', @@ -105,6 +120,7 @@ my %patch_modes = ( TARGET => '', PARTICIPLE => 'unstaging', FILTER => 'index-only', + IS_REVERSE => 1, }, 'reset_nothead' => { DIFF => 'diff-index -R -p --cached', @@ -114,6 +130,7 @@ my %patch_modes = ( TARGET => ' to index', PARTICIPLE => 'applying', FILTER => 'index-only', + IS_REVERSE => 0, }, 'checkout_index' => { DIFF => 'diff-files -p', @@ -123,6 +140,7 @@ my %patch_modes = ( TARGET => ' from worktree', PARTICIPLE => 'discarding', FILTER => 'file-only', + IS_REVERSE => 1, }, 'checkout_head' => { DIFF => 'diff-index -p', @@ -132,6 +150,7 @@ my %patch_modes = ( TARGET => ' from index and worktree', PARTICIPLE => 'discarding', FILTER => undef, + IS_REVERSE => 1, }, 'checkout_nothead' => { DIFF => 'diff-index -R -p', @@ -141,6 +160,7 @@ my %patch_modes = ( TARGET => ' to index and worktree', PARTICIPLE => 'applying', FILTER => undef, + IS_REVERSE => 0, }, ); @@ -696,7 +716,7 @@ sub add_untracked_cmd { sub run_git_apply { my $cmd = shift; my $fh; - open $fh, '| git ' . $cmd; + open $fh, '| git ' . $cmd . " --recount --allow-overlap"; print $fh @_; return close $fh; } @@ -957,6 +977,28 @@ sub coalesce_overlapping_hunks { return @out; } +sub reassemble_patch { + my $head = shift; + my @patch; + + # Include everything in the header except the beginning of the diff. + push @patch, (grep { !/^[-+]{3}/ } @$head); + + # Then include any headers from the hunk lines, which must + # come before any actual hunk. + while (@_ && $_[0] !~ /^@/) { + push @patch, shift; + } + + # Then begin the diff. + push @patch, grep { /^[-+]{3}/ } @$head; + + # And then the actual hunks. + push @patch, @_; + + return @patch; +} + sub color_diff { return map { colored((/^@/ ? $fraginfo_color : @@ -977,10 +1019,12 @@ sub edit_hunk_manually { print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n"; print $fh @$oldtext; my $participle = $patch_mode_flavour{PARTICIPLE}; + my $is_reverse = $patch_mode_flavour{IS_REVERSE}; + my ($remove_plus, $remove_minus) = $is_reverse ? ('-', '+') : ('+', '-'); print $fh <{TEXT}} } @_); } @@ -1034,6 +1078,14 @@ sub prompt_single_character { ReadMode 'cbreak'; my $key = ReadKey 0; ReadMode 'restore'; + if ($use_termcap and $key eq "\e") { + while (!defined $term_escapes{$key}) { + my $next = ReadKey 0.5; + last if (!defined $next); + $key .= $next; + } + $key =~ s/\e/^[/; + } print "$key" if defined $key; print "\n"; return $key; @@ -1089,9 +1141,9 @@ sub help_patch_cmd { print colored $help_color, <{TEXT}}, @result); + my @patch = reassemble_patch($head->{TEXT}, @result); my $apply_routine = $patch_mode_flavour{APPLY}; &$apply_routine(@patch); refresh();