8 binmode(STDOUT
, ":raw");
10 my $repo = Git
->repository();
12 my $menu_use_color = $repo->get_colorbool('color.interactive');
13 my ($prompt_color, $header_color, $help_color) =
15 $repo->get_color('color.interactive.prompt', 'bold blue'),
16 $repo->get_color('color.interactive.header', 'bold'),
17 $repo->get_color('color.interactive.help', 'red bold'),
20 if ($menu_use_color) {
21 my $help_color_spec = ($repo->config('color.interactive.help') or
23 $error_color = $repo->get_color('color.interactive.error',
27 my $diff_use_color = $repo->get_colorbool('color.diff');
28 my ($fraginfo_color) =
30 $repo->get_color('color.diff.frag', 'cyan'),
32 my ($diff_plain_color) =
34 $repo->get_color('color.diff.plain', ''),
36 my ($diff_old_color) =
38 $repo->get_color('color.diff.old', 'red'),
40 my ($diff_new_color) =
42 $repo->get_color('color.diff.new', 'green'),
45 my $normal_color = $repo->get_color("", "reset");
50 if ($repo->config_bool("interactive.singlekey")) {
52 require Term
::ReadKey
;
53 Term
::ReadKey
->import;
60 my $string = join("", @_);
63 # Put a color code at the beginning of each line, a reset at the end
64 # color after newlines that are not at the end of the string
65 $string =~ s/(\n+)(.)/$1$color$2/g;
66 # reset before newlines
67 $string =~ s/(\n+)/$normal_color$1/g;
68 # codes at beginning and end (if necessary):
69 $string =~ s/^/$color/;
70 $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
75 # command line options
77 my $patch_mode_revision;
80 sub apply_patch_for_checkout_commit
;
81 sub apply_patch_for_stash
;
85 DIFF
=> 'diff-files -p',
86 APPLY
=> sub { apply_patch
'apply --cached', @_; },
87 APPLY_CHECK
=> 'apply --cached',
90 PARTICIPLE
=> 'staging',
91 FILTER
=> 'file-only',
94 DIFF
=> 'diff-index -p HEAD',
95 APPLY
=> sub { apply_patch
'apply --cached', @_; },
96 APPLY_CHECK
=> 'apply --cached',
99 PARTICIPLE
=> 'stashing',
103 DIFF
=> 'diff-index -p --cached',
104 APPLY
=> sub { apply_patch
'apply -R --cached', @_; },
105 APPLY_CHECK
=> 'apply -R --cached',
108 PARTICIPLE
=> 'unstaging',
109 FILTER
=> 'index-only',
112 DIFF
=> 'diff-index -R -p --cached',
113 APPLY
=> sub { apply_patch
'apply --cached', @_; },
114 APPLY_CHECK
=> 'apply --cached',
116 TARGET
=> ' to index',
117 PARTICIPLE
=> 'applying',
118 FILTER
=> 'index-only',
120 'checkout_index' => {
121 DIFF
=> 'diff-files -p',
122 APPLY
=> sub { apply_patch
'apply -R', @_; },
123 APPLY_CHECK
=> 'apply -R',
125 TARGET
=> ' from worktree',
126 PARTICIPLE
=> 'discarding',
127 FILTER
=> 'file-only',
130 DIFF
=> 'diff-index -p',
131 APPLY
=> sub { apply_patch_for_checkout_commit
'-R', @_ },
132 APPLY_CHECK
=> 'apply -R',
134 TARGET
=> ' from index and worktree',
135 PARTICIPLE
=> 'discarding',
138 'checkout_nothead' => {
139 DIFF
=> 'diff-index -R -p',
140 APPLY
=> sub { apply_patch_for_checkout_commit
'', @_ },
141 APPLY_CHECK
=> 'apply',
143 TARGET
=> ' to index and worktree',
144 PARTICIPLE
=> 'applying',
149 my %patch_mode_flavour = %{$patch_modes{stage
}};
152 if ($^O
eq 'MSWin32' || $^O
eq 'msys') {
153 my @invalid = grep {m/[":*]/} @_;
154 die "$^O does not support: @invalid\n" if @invalid;
155 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
159 open($fh, '-|', @_) or die;
164 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
166 if (!defined $GIT_DIR) {
167 exit(1); # rev-parse would have already said "not a git repo"
184 my ($retval, $remainder);
185 if (!/^\042(.*)\042$/) {
188 ($_, $retval) = ($1, "");
189 while (/^([^\\]*)\\(.*)$/) {
193 if (/^([0-3][0-7][0-7])(.*)$/) {
194 $retval .= chr(oct($1));
198 if (/^([\\\042btnvfr])(.*)$/) {
199 $retval .= $cquote_map{$1};
203 # This is malformed -- just return it as-is for now.
214 open $fh, 'git update-index --refresh |'
217 ;# ignore 'needs update'
227 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
230 my $status_fmt = '%12s %12s %s';
231 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
235 sub is_initial_commit
{
236 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
237 unless defined $initial;
243 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
246 # Returns list of hashes, contents of each of which are:
248 # BINARY: is a binary path
249 # INDEX: is index different from HEAD?
250 # FILE: is file different from index?
251 # INDEX_ADDDEL: is it add/delete between HEAD and index?
252 # FILE_ADDDEL: is it add/delete between index and file?
257 my ($add, $del, $adddel, $file);
264 } run_cmd_pipe
(qw(git ls-files --), @ARGV);
265 return if (!@tracked);
269 if (defined $patch_mode_revision and $patch_mode_revision ne 'HEAD') {
270 $reference = $patch_mode_revision;
271 } elsif (is_initial_commit
()) {
272 $reference = get_empty_tree
();
276 for (run_cmd_pipe
(qw(git diff-index --cached
277 --numstat --summary), $reference,
279 if (($add, $del, $file) =
280 /^([-\d]+) ([-\d]+) (.*)/) {
282 $file = unquote_path
($file);
283 if ($add eq '-' && $del eq '-') {
288 $change = "+$add/-$del";
296 elsif (($adddel, $file) =
297 /^ (create|delete) mode [0-7]+ (.*)$/) {
298 $file = unquote_path
($file);
299 $data{$file}{INDEX_ADDDEL
} = $adddel;
303 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
304 if (($add, $del, $file) =
305 /^([-\d]+) ([-\d]+) (.*)/) {
306 $file = unquote_path
($file);
307 if (!exists $data{$file}) {
309 INDEX
=> 'unchanged',
314 if ($add eq '-' && $del eq '-') {
319 $change = "+$add/-$del";
321 $data{$file}{FILE
} = $change;
323 $data{$file}{BINARY
} = 1;
326 elsif (($adddel, $file) =
327 /^ (create|delete) mode [0-7]+ (.*)$/) {
328 $file = unquote_path
($file);
329 $data{$file}{FILE_ADDDEL
} = $adddel;
333 for (sort keys %data) {
337 if ($only eq 'index-only') {
338 next if ($it->{INDEX
} eq 'unchanged');
340 if ($only eq 'file-only') {
341 next if ($it->{FILE
} eq 'nothing');
353 my ($string, @stuff) = @_;
355 for (my $i = 0; $i < @stuff; $i++) {
359 if ((ref $it) eq 'ARRAY') {
367 if ($it =~ /^$string/) {
371 if (defined $hit && defined $found) {
381 # inserts string into trie and updates count for each character
383 my ($trie, $string) = @_;
384 foreach (split //, $string) {
385 $trie = $trie->{$_} ||= {COUNT
=> 0};
390 # returns an array of tuples (prefix, remainder)
391 sub find_unique_prefixes
{
395 # any single prefix exceeding the soft limit is omitted
396 # if any prefix exceeds the hard limit all are omitted
397 # 0 indicates no limit
401 # build a trie modelling all possible options
403 foreach my $print (@stuff) {
404 if ((ref $print) eq 'ARRAY') {
405 $print = $print->[0];
407 elsif ((ref $print) eq 'HASH') {
408 $print = $print->{VALUE
};
410 update_trie
(\
%trie, $print);
411 push @return, $print;
414 # use the trie to find the unique prefixes
415 for (my $i = 0; $i < @return; $i++) {
416 my $ret = $return[$i];
417 my @letters = split //, $ret;
419 my ($prefix, $remainder);
421 for ($j = 0; $j < @letters; $j++) {
422 my $letter = $letters[$j];
423 if ($search{$letter}{COUNT
} == 1) {
424 $prefix = substr $ret, 0, $j + 1;
425 $remainder = substr $ret, $j + 1;
429 my $prefix = substr $ret, 0, $j;
431 if ($hard_limit && $j + 1 > $hard_limit);
433 %search = %{$search{$letter}};
435 if (ord($letters[0]) > 127 ||
436 ($soft_limit && $j + 1 > $soft_limit)) {
440 $return[$i] = [$prefix, $remainder];
445 # filters out prefixes which have special meaning to list_and_choose()
446 sub is_valid_prefix
{
448 return (defined $prefix) &&
449 !($prefix =~ /[\s,]/) && # separators
450 !($prefix =~ /^-/) && # deselection
451 !($prefix =~ /^\d+/) && # selection
452 ($prefix ne '*') && # "all" wildcard
453 ($prefix ne '?'); # prompt help
456 # given a prefix/remainder tuple return a string with the prefix highlighted
457 # for now use square brackets; later might use ANSI colors (underline, bold)
458 sub highlight_prefix
{
460 my $remainder = shift;
462 if (!defined $prefix) {
466 if (!is_valid_prefix
($prefix)) {
467 return "$prefix$remainder";
470 if (!$menu_use_color) {
471 return "[$prefix]$remainder";
474 return "$prompt_color$prefix$normal_color$remainder";
478 print STDERR colored
$error_color, @_;
481 sub list_and_choose
{
482 my ($opts, @stuff) = @_;
483 my (@chosen, @return);
485 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
491 if ($opts->{HEADER
}) {
492 if (!$opts->{LIST_FLAT
}) {
495 print colored
$header_color, "$opts->{HEADER}\n";
497 for ($i = 0; $i < @stuff; $i++) {
498 my $chosen = $chosen[$i] ?
'*' : ' ';
499 my $print = $stuff[$i];
500 my $ref = ref $print;
501 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
503 if ($ref eq 'ARRAY') {
504 $print = $highlighted || $print->[0];
506 elsif ($ref eq 'HASH') {
507 my $value = $highlighted || $print->{VALUE
};
508 $print = sprintf($status_fmt,
514 $print = $highlighted || $print;
516 printf("%s%2d: %s", $chosen, $i+1, $print);
517 if (($opts->{LIST_FLAT
}) &&
518 (($i + 1) % ($opts->{LIST_FLAT
}))) {
531 return if ($opts->{LIST_ONLY
});
533 print colored
$prompt_color, $opts->{PROMPT
};
534 if ($opts->{SINGLETON
}) {
543 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
550 singleton_prompt_help_cmd
() :
554 for my $choice (split(/[\s,]+/, $line)) {
558 # Input that begins with '-'; unchoose
559 if ($choice =~ s/^-//) {
562 # A range can be specified like 5-7 or 5-.
563 if ($choice =~ /^(\d+)-(\d*)$/) {
564 ($bottom, $top) = ($1, length($2) ?
$2 : 1 + @stuff);
566 elsif ($choice =~ /^\d+$/) {
567 $bottom = $top = $choice;
569 elsif ($choice eq '*') {
574 $bottom = $top = find_unique
($choice, @stuff);
575 if (!defined $bottom) {
576 error_msg
"Huh ($choice)?\n";
580 if ($opts->{SINGLETON
} && $bottom != $top) {
581 error_msg
"Huh ($choice)?\n";
584 for ($i = $bottom-1; $i <= $top-1; $i++) {
585 next if (@stuff <= $i || $i < 0);
586 $chosen[$i] = $choose;
589 last if ($opts->{IMMEDIATE
} || $line eq '*');
591 for ($i = 0; $i < @stuff; $i++) {
593 push @return, $stuff[$i];
599 sub singleton_prompt_help_cmd
{
600 print colored
$help_color, <<\EOF
;
602 1 - select a numbered item
603 foo
- select item based on unique prefix
604 - (empty
) select nothing
608 sub prompt_help_cmd
{
609 print colored
$help_color, <<\EOF
;
611 1 - select a single item
612 3-5 - select a range of items
613 2-3,6-9 - select multiple ranges
614 foo
- select item based on unique prefix
615 -... - unselect specified items
617 - (empty
) finish selecting
622 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
632 print "$cnt paths\n";
640 my @mods = list_modified
('file-only');
643 my @update = list_and_choose
({ PROMPT
=> 'Update',
644 HEADER
=> $status_head, },
647 system(qw(git update-index --add --remove --),
648 map { $_->{VALUE
} } @update);
649 say_n_paths
('updated', @update);
655 my @update = list_and_choose
({ PROMPT
=> 'Revert',
656 HEADER
=> $status_head, },
659 if (is_initial_commit
()) {
660 system(qw(git rm --cached),
661 map { $_->{VALUE
} } @update);
664 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
665 map { $_->{VALUE
} } @update);
667 open $fh, '| git update-index --index-info'
674 if ($_->{INDEX_ADDDEL
} &&
675 $_->{INDEX_ADDDEL
} eq 'create') {
676 system(qw(git update-index --force-remove --),
678 print "note: $_->{VALUE} is untracked now.\n";
683 say_n_paths
('reverted', @update);
688 sub add_untracked_cmd
{
689 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
692 system(qw(git update-index --add --), @add);
693 say_n_paths
('added', @add);
701 open $fh, '| git ' . $cmd;
708 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF
});
709 if (defined $patch_mode_revision) {
710 push @diff_cmd, $patch_mode_revision;
712 my @diff = run_cmd_pipe
("git", @diff_cmd, "--", $path);
714 if ($diff_use_color) {
715 @colored = run_cmd_pipe
("git", @diff_cmd, qw(--color --), $path);
717 my (@hunk) = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
719 for (my $i = 0; $i < @diff; $i++) {
720 if ($diff[$i] =~ /^@@ /) {
721 push @hunk, { TEXT
=> [], DISPLAY
=> [],
724 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
725 push @
{$hunk[-1]{DISPLAY
}},
726 ($diff_use_color ?
$colored[$i] : $diff[$i]);
731 sub parse_diff_header
{
734 my $head = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
735 my $mode = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'mode' };
736 my $deletion = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'deletion' };
738 for (my $i = 0; $i < @
{$src->{TEXT
}}; $i++) {
740 $src->{TEXT
}->[$i] =~ /^(old|new) mode (\d+)$/ ?
$mode :
741 $src->{TEXT
}->[$i] =~ /^deleted file/ ?
$deletion :
743 push @
{$dest->{TEXT
}}, $src->{TEXT
}->[$i];
744 push @
{$dest->{DISPLAY
}}, $src->{DISPLAY
}->[$i];
746 return ($head, $mode, $deletion);
749 sub hunk_splittable
{
752 my @s = split_hunk
($text);
756 sub parse_hunk_header
{
758 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
759 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
760 $o_cnt = 1 unless defined $o_cnt;
761 $n_cnt = 1 unless defined $n_cnt;
762 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
766 my ($text, $display) = @_;
768 if (!defined $display) {
771 # If there are context lines in the middle of a hunk,
772 # it can be split, but we would need to take care of
775 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
780 my $next_hunk_start = undef;
781 my $i = $hunk_start - 1;
795 while (++$i < @
$text) {
796 my $line = $text->[$i];
797 my $display = $display->[$i];
799 if ($this->{ADDDEL
} &&
800 !defined $next_hunk_start) {
801 # We have seen leading context and
802 # adds/dels and then here is another
803 # context, which is trailing for this
804 # split hunk and leading for the next
806 $next_hunk_start = $i;
808 push @
{$this->{TEXT
}}, $line;
809 push @
{$this->{DISPLAY
}}, $display;
812 if (defined $next_hunk_start) {
819 if (defined $next_hunk_start) {
820 # We are done with the current hunk and
821 # this is the first real change for the
823 $hunk_start = $next_hunk_start;
824 $o_ofs = $this->{OLD
} + $this->{OCNT
};
825 $n_ofs = $this->{NEW
} + $this->{NCNT
};
826 $o_ofs -= $this->{POSTCTX
};
827 $n_ofs -= $this->{POSTCTX
};
831 push @
{$this->{TEXT
}}, $line;
832 push @
{$this->{DISPLAY
}}, $display;
846 for my $hunk (@split) {
847 $o_ofs = $hunk->{OLD
};
848 $n_ofs = $hunk->{NEW
};
849 my $o_cnt = $hunk->{OCNT
};
850 my $n_cnt = $hunk->{NCNT
};
852 my $head = ("@@ -$o_ofs" .
853 (($o_cnt != 1) ?
",$o_cnt" : '') .
855 (($n_cnt != 1) ?
",$n_cnt" : '') .
857 my $display_head = $head;
858 unshift @
{$hunk->{TEXT
}}, $head;
859 if ($diff_use_color) {
860 $display_head = colored
($fraginfo_color, $head);
862 unshift @
{$hunk->{DISPLAY
}}, $display_head;
867 sub find_last_o_ctx
{
869 my $text = $it->{TEXT
};
870 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
872 my $last_o_ctx = $o_ofs + $o_cnt;
874 my $line = $text->[$i];
885 my ($prev, $this) = @_;
886 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
887 parse_hunk_header
($prev->{TEXT
}[0]);
888 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
889 parse_hunk_header
($this->{TEXT
}[0]);
891 my (@line, $i, $ofs, $o_cnt, $n_cnt);
894 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
895 my $line = $prev->{TEXT
}[$i];
896 if ($line =~ /^\+/) {
902 last if ($o1_ofs <= $ofs);
912 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
913 my $line = $this->{TEXT
}[$i];
914 if ($line =~ /^\+/) {
926 my $head = ("@@ -$o0_ofs" .
927 (($o_cnt != 1) ?
",$o_cnt" : '') .
929 (($n_cnt != 1) ?
",$n_cnt" : '') .
931 @
{$prev->{TEXT
}} = ($head, @line);
934 sub coalesce_overlapping_hunks
{
938 my ($last_o_ctx, $last_was_dirty);
940 for (grep { $_->{USE
} } @in) {
941 if ($_->{TYPE
} ne 'hunk') {
945 my $text = $_->{TEXT
};
946 my ($o_ofs) = parse_hunk_header
($text->[0]);
947 if (defined $last_o_ctx &&
948 $o_ofs <= $last_o_ctx &&
951 merge_hunk
($out[-1], $_);
956 $last_o_ctx = find_last_o_ctx
($out[-1]);
957 $last_was_dirty = $_->{DIRTY
};
962 sub reassemble_patch
{
966 # Include everything in the header except the beginning of the diff.
967 push @patch, (grep { !/^[-+]{3}/ } @
$head);
969 # Then include any headers from the hunk lines, which must
970 # come before any actual hunk.
971 while (@_ && $_[0] !~ /^@/) {
975 # Then begin the diff.
976 push @patch, grep { /^[-+]{3}/ } @
$head;
978 # And then the actual hunks.
986 colored
((/^@/ ?
$fraginfo_color :
987 /^\+/ ?
$diff_new_color :
988 /^-/ ?
$diff_old_color :
994 sub edit_hunk_manually
{
997 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
999 open $fh, '>', $hunkfile
1000 or die "failed to open hunk edit file for writing: " . $!;
1001 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
1002 print $fh @
$oldtext;
1003 my $participle = $patch_mode_flavour{PARTICIPLE
};
1006 # To remove '-' lines, make them ' ' lines (context).
1007 # To remove '+' lines, delete them.
1008 # Lines starting with # will be removed.
1010 # If the patch applies cleanly, the edited hunk will immediately be
1011 # marked for $participle. If it does not apply cleanly, you will be given
1012 # an opportunity to edit again. If all lines of the hunk are removed,
1013 # then the edit is aborted and the hunk is left unchanged.
1017 chomp(my $editor = run_cmd_pipe
(qw(git var GIT_EDITOR)));
1018 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
1024 open $fh, '<', $hunkfile
1025 or die "failed to open hunk edit file for reading: " . $!;
1026 my @newtext = grep { !/^#/ } <$fh>;
1030 # Abort if nothing remains
1031 if (!grep { /\S/ } @newtext) {
1035 # Reinsert the first hunk header if the user accidentally deleted it
1036 if ($newtext[0] !~ /^@/) {
1037 unshift @newtext, $oldtext->[0];
1044 return run_git_apply
($patch_mode_flavour{APPLY_CHECK
} . ' --recount --check',
1045 map { @
{$_->{TEXT
}} } @_);
1048 sub _restore_terminal_and_die
{
1054 sub prompt_single_character
{
1056 local $SIG{TERM
} = \
&_restore_terminal_and_die
;
1057 local $SIG{INT
} = \
&_restore_terminal_and_die
;
1059 my $key = ReadKey
0;
1061 print "$key" if defined $key;
1072 print colored
$prompt_color, $prompt;
1073 my $line = prompt_single_character
;
1074 return 0 if $line =~ /^n/i;
1075 return 1 if $line =~ /^y/i;
1079 sub edit_hunk_loop
{
1080 my ($head, $hunk, $ix) = @_;
1081 my $text = $hunk->[$ix]->{TEXT
};
1084 $text = edit_hunk_manually
($text);
1085 if (!defined $text) {
1090 TYPE
=> $hunk->[$ix]->{TYPE
},
1094 if (diff_applies
($head,
1097 @
{$hunk}[$ix+1..$#{$hunk}])) {
1098 $newhunk->{DISPLAY
} = [color_diff
(@
{$text})];
1103 'Your edited hunk does not apply. Edit again '
1104 . '(saying "no" discards!) [y/n]? '
1110 sub help_patch_cmd
{
1111 my $verb = lc $patch_mode_flavour{VERB
};
1112 my $target = $patch_mode_flavour{TARGET
};
1113 print colored
$help_color, <<EOF ;
1114 y - $verb this hunk$target
1115 n - do not $verb this hunk$target
1116 q - quit; do not $verb this hunk nor any of the remaining ones
1117 a - $verb this hunk and all later hunks in the file
1118 d - do not $verb this hunk nor any of the later hunks in the file
1119 g - select a hunk to go to
1120 / - search for a hunk matching the given regex
1121 j - leave this hunk undecided, see next undecided hunk
1122 J - leave this hunk undecided, see next hunk
1123 k - leave this hunk undecided, see previous undecided hunk
1124 K - leave this hunk undecided, see previous hunk
1125 s - split the current hunk into smaller hunks
1126 e - manually edit the current hunk
1133 my $ret = run_git_apply
$cmd . ' --recount', @_;
1140 sub apply_patch_for_checkout_commit
{
1141 my $reverse = shift;
1142 my $applies_index = run_git_apply
'apply '.$reverse.' --cached --recount --check', @_;
1143 my $applies_worktree = run_git_apply
'apply '.$reverse.' --recount --check', @_;
1145 if ($applies_worktree && $applies_index) {
1146 run_git_apply
'apply '.$reverse.' --cached --recount', @_;
1147 run_git_apply
'apply '.$reverse.' --recount', @_;
1149 } elsif (!$applies_index) {
1150 print colored
$error_color, "The selected hunks do not apply to the index!\n";
1151 if (prompt_yesno
"Apply them to the worktree anyway? ") {
1152 return run_git_apply
'apply '.$reverse.' --recount', @_;
1154 print colored
$error_color, "Nothing was applied.\n";
1163 sub patch_update_cmd
{
1164 my @all_mods = list_modified
($patch_mode_flavour{FILTER
});
1165 my @mods = grep { !($_->{BINARY
}) } @all_mods;
1170 print STDERR
"Only binary files changed.\n";
1172 print STDERR
"No changes.\n";
1180 @them = list_and_choose
({ PROMPT
=> 'Patch update',
1181 HEADER
=> $status_head, },
1185 return 0 if patch_update_file
($_->{VALUE
});
1189 # Generate a one line summary of a hunk.
1190 sub summarize_hunk
{
1192 my $summary = $rhunk->{TEXT
}[0];
1194 # Keep the line numbers, discard extra context.
1195 $summary =~ s/@@(.*?)@@.*/$1 /s;
1196 $summary .= " " x
(20 - length $summary);
1198 # Add some user context.
1199 for my $line (@
{$rhunk->{TEXT
}}) {
1200 if ($line =~ m/^[+-].*\w/) {
1207 return substr($summary, 0, 80) . "\n";
1211 # Print a one-line summary of each hunk in the array ref in
1212 # the first argument, starting wih the index in the 2nd.
1214 my ($hunks, $i) = @_;
1217 for (; $i < @
$hunks && $ctr < 20; $i++, $ctr++) {
1219 if (defined $hunks->[$i]{USE
}) {
1220 $status = $hunks->[$i]{USE
} ?
"+" : "-";
1225 summarize_hunk
($hunks->[$i]);
1230 sub patch_update_file
{
1234 my ($head, @hunk) = parse_diff
($path);
1235 ($head, my $mode, my $deletion) = parse_diff_header
($head);
1236 for (@
{$head->{DISPLAY
}}) {
1240 if (@
{$mode->{TEXT
}}) {
1241 unshift @hunk, $mode;
1243 if (@
{$deletion->{TEXT
}}) {
1244 foreach my $hunk (@hunk) {
1245 push @
{$deletion->{TEXT
}}, @
{$hunk->{TEXT
}};
1246 push @
{$deletion->{DISPLAY
}}, @
{$hunk->{DISPLAY
}};
1248 @hunk = ($deletion);
1251 $num = scalar @hunk;
1255 my ($prev, $next, $other, $undecided, $i);
1261 for ($i = 0; $i < $ix; $i++) {
1262 if (!defined $hunk[$i]{USE
}) {
1271 for ($i = $ix + 1; $i < $num; $i++) {
1272 if (!defined $hunk[$i]{USE
}) {
1278 if ($ix < $num - 1) {
1284 for ($i = 0; $i < $num; $i++) {
1285 if (!defined $hunk[$i]{USE
}) {
1290 last if (!$undecided);
1292 if ($hunk[$ix]{TYPE
} eq 'hunk' &&
1293 hunk_splittable
($hunk[$ix]{TEXT
})) {
1296 if ($hunk[$ix]{TYPE
} eq 'hunk') {
1299 for (@
{$hunk[$ix]{DISPLAY
}}) {
1302 print colored
$prompt_color, $patch_mode_flavour{VERB
},
1303 ($hunk[$ix]{TYPE
} eq 'mode' ?
' mode change' :
1304 $hunk[$ix]{TYPE
} eq 'deletion' ?
' deletion' :
1306 $patch_mode_flavour{TARGET
},
1307 " [y,n,q,a,d,/$other,?]? ";
1308 my $line = prompt_single_character
;
1310 if ($line =~ /^y/i) {
1311 $hunk[$ix]{USE
} = 1;
1313 elsif ($line =~ /^n/i) {
1314 $hunk[$ix]{USE
} = 0;
1316 elsif ($line =~ /^a/i) {
1317 while ($ix < $num) {
1318 if (!defined $hunk[$ix]{USE
}) {
1319 $hunk[$ix]{USE
} = 1;
1325 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1327 my $no = $ix > 10 ?
$ix - 10 : 0;
1328 while ($response eq '') {
1330 $no = display_hunks
(\
@hunk, $no);
1332 $extra = " (<ret> to see more)";
1334 print "go to which hunk$extra? ";
1335 $response = <STDIN
>;
1336 if (!defined $response) {
1341 if ($response !~ /^\s*\d+\s*$/) {
1342 error_msg
"Invalid number: '$response'\n";
1343 } elsif (0 < $response && $response <= $num) {
1344 $ix = $response - 1;
1346 error_msg
"Sorry, only $num hunks available.\n";
1350 elsif ($line =~ /^d/i) {
1351 while ($ix < $num) {
1352 if (!defined $hunk[$ix]{USE
}) {
1353 $hunk[$ix]{USE
} = 0;
1359 elsif ($line =~ /^q/i) {
1360 while ($ix < $num) {
1361 if (!defined $hunk[$ix]{USE
}) {
1362 $hunk[$ix]{USE
} = 0;
1369 elsif ($line =~ m
|^/(.*)|) {
1372 print colored
$prompt_color, "search for regex? ";
1374 if (defined $regex) {
1380 $search_string = qr{$regex}m;
1383 my ($err,$exp) = ($@
, $1);
1384 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1385 error_msg
"Malformed search regexp $exp: $err\n";
1390 my $text = join ("", @
{$hunk[$iy]{TEXT
}});
1391 last if ($text =~ $search_string);
1393 $iy = 0 if ($iy >= $num);
1395 error_msg
"No hunk matches the given pattern\n";
1402 elsif ($line =~ /^K/) {
1403 if ($other =~ /K/) {
1407 error_msg
"No previous hunk\n";
1411 elsif ($line =~ /^J/) {
1412 if ($other =~ /J/) {
1416 error_msg
"No next hunk\n";
1420 elsif ($line =~ /^k/) {
1421 if ($other =~ /k/) {
1425 !defined $hunk[$ix]{USE
});
1429 error_msg
"No previous hunk\n";
1433 elsif ($line =~ /^j/) {
1434 if ($other !~ /j/) {
1435 error_msg
"No next hunk\n";
1439 elsif ($other =~ /s/ && $line =~ /^s/) {
1440 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
1442 print colored
$header_color, "Split into ",
1443 scalar(@split), " hunks.\n";
1445 splice (@hunk, $ix, 1, @split);
1446 $num = scalar @hunk;
1449 elsif ($other =~ /e/ && $line =~ /^e/) {
1450 my $newhunk = edit_hunk_loop
($head, \
@hunk, $ix);
1451 if (defined $newhunk) {
1452 splice @hunk, $ix, 1, $newhunk;
1456 help_patch_cmd
($other);
1462 last if ($ix >= $num ||
1463 !defined $hunk[$ix]{USE
});
1468 @hunk = coalesce_overlapping_hunks
(@hunk);
1474 push @result, @
{$_->{TEXT
}};
1480 my @patch = reassemble_patch
($head->{TEXT
}, @result);
1481 my $apply_routine = $patch_mode_flavour{APPLY
};
1482 &$apply_routine(@patch);
1491 my @mods = list_modified
('index-only');
1492 @mods = grep { !($_->{BINARY
}) } @mods;
1494 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
1496 HEADER
=> $status_head, },
1499 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
1500 system(qw(git diff -p --cached), $reference, '--',
1501 map { $_->{VALUE
} } @them);
1510 print colored
$help_color, <<\EOF
;
1511 status
- show paths with changes
1512 update
- add working tree
state to the staged set of changes
1513 revert
- revert staged set of changes back to the HEAD version
1514 patch
- pick hunks
and update selectively
1515 diff
- view diff between HEAD
and index
1516 add untracked
- add contents of untracked files to the staged set of changes
1521 return unless @ARGV;
1522 my $arg = shift @ARGV;
1523 if ($arg =~ /--patch(?:=(.*))?/) {
1525 if ($1 eq 'reset') {
1526 $patch_mode = 'reset_head';
1527 $patch_mode_revision = 'HEAD';
1528 $arg = shift @ARGV or die "missing --";
1530 $patch_mode_revision = $arg;
1531 $patch_mode = ($arg eq 'HEAD' ?
1532 'reset_head' : 'reset_nothead');
1533 $arg = shift @ARGV or die "missing --";
1535 } elsif ($1 eq 'checkout') {
1536 $arg = shift @ARGV or die "missing --";
1538 $patch_mode = 'checkout_index';
1540 $patch_mode_revision = $arg;
1541 $patch_mode = ($arg eq 'HEAD' ?
1542 'checkout_head' : 'checkout_nothead');
1543 $arg = shift @ARGV or die "missing --";
1545 } elsif ($1 eq 'stage' or $1 eq 'stash') {
1547 $arg = shift @ARGV or die "missing --";
1549 die "unknown --patch mode: $1";
1552 $patch_mode = 'stage';
1553 $arg = shift @ARGV or die "missing --";
1555 die "invalid argument $arg, expecting --"
1556 unless $arg eq "--";
1557 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
1559 elsif ($arg ne "--") {
1560 die "invalid argument $arg, expecting --";
1565 my @cmd = ([ 'status', \
&status_cmd
, ],
1566 [ 'update', \
&update_cmd
, ],
1567 [ 'revert', \
&revert_cmd
, ],
1568 [ 'add untracked', \
&add_untracked_cmd
, ],
1569 [ 'patch', \
&patch_update_cmd
, ],
1570 [ 'diff', \
&diff_cmd
, ],
1571 [ 'quit', \
&quit_cmd
, ],
1572 [ 'help', \
&help_cmd
, ],
1575 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1578 HEADER
=> '*** Commands ***',
1579 ON_EOF
=> \
&quit_cmd
,
1580 IMMEDIATE
=> 1 }, @cmd);