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 ($funcname_color) =
34 $repo->get_color('color.diff.func', ''),
36 my ($diff_plain_color) =
38 $repo->get_color('color.diff.plain', ''),
40 my ($diff_old_color) =
42 $repo->get_color('color.diff.old', 'red'),
44 my ($diff_new_color) =
46 $repo->get_color('color.diff.new', 'green'),
49 my $normal_color = $repo->get_color("", "reset");
51 my $diff_algorithm = $repo->config('diff.algorithm');
59 if ($repo->config_bool("interactive.singlekey")) {
61 require Term
::ReadKey
;
62 Term
::ReadKey
->import;
66 print STDERR
"missing Term::ReadKey, disabling interactive.singlekey\n";
70 my $termcap = Term
::Cap
->Tgetent;
71 foreach (values %$termcap) {
72 $term_escapes{$_} = 1 if /^\e/;
80 my $string = join("", @_);
83 # Put a color code at the beginning of each line, a reset at the end
84 # color after newlines that are not at the end of the string
85 $string =~ s/(\n+)(.)/$1$color$2/g;
86 # reset before newlines
87 $string =~ s/(\n+)/$normal_color$1/g;
88 # codes at beginning and end (if necessary):
89 $string =~ s/^/$color/;
90 $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
95 # command line options
97 my $patch_mode_revision;
100 sub apply_patch_for_checkout_commit
;
101 sub apply_patch_for_stash
;
105 DIFF
=> 'diff-files -p',
106 APPLY
=> sub { apply_patch
'apply --cached', @_; },
107 APPLY_CHECK
=> 'apply --cached',
110 PARTICIPLE
=> 'staging',
111 FILTER
=> 'file-only',
115 DIFF
=> 'diff-index -p HEAD',
116 APPLY
=> sub { apply_patch
'apply --cached', @_; },
117 APPLY_CHECK
=> 'apply --cached',
120 PARTICIPLE
=> 'stashing',
125 DIFF
=> 'diff-index -p --cached',
126 APPLY
=> sub { apply_patch
'apply -R --cached', @_; },
127 APPLY_CHECK
=> 'apply -R --cached',
130 PARTICIPLE
=> 'unstaging',
131 FILTER
=> 'index-only',
135 DIFF
=> 'diff-index -R -p --cached',
136 APPLY
=> sub { apply_patch
'apply --cached', @_; },
137 APPLY_CHECK
=> 'apply --cached',
139 TARGET
=> ' to index',
140 PARTICIPLE
=> 'applying',
141 FILTER
=> 'index-only',
144 'checkout_index' => {
145 DIFF
=> 'diff-files -p',
146 APPLY
=> sub { apply_patch
'apply -R', @_; },
147 APPLY_CHECK
=> 'apply -R',
149 TARGET
=> ' from worktree',
150 PARTICIPLE
=> 'discarding',
151 FILTER
=> 'file-only',
155 DIFF
=> 'diff-index -p',
156 APPLY
=> sub { apply_patch_for_checkout_commit
'-R', @_ },
157 APPLY_CHECK
=> 'apply -R',
159 TARGET
=> ' from index and worktree',
160 PARTICIPLE
=> 'discarding',
164 'checkout_nothead' => {
165 DIFF
=> 'diff-index -R -p',
166 APPLY
=> sub { apply_patch_for_checkout_commit
'', @_ },
167 APPLY_CHECK
=> 'apply',
169 TARGET
=> ' to index and worktree',
170 PARTICIPLE
=> 'applying',
176 my %patch_mode_flavour = %{$patch_modes{stage
}};
179 if ($^O
eq 'MSWin32') {
180 my @invalid = grep {m/[":*]/} @_;
181 die "$^O does not support: @invalid\n" if @invalid;
182 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
186 open($fh, '-|', @_) or die;
191 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
193 if (!defined $GIT_DIR) {
194 exit(1); # rev-parse would have already said "not a git repo"
211 my ($retval, $remainder);
212 if (!/^\042(.*)\042$/) {
215 ($_, $retval) = ($1, "");
216 while (/^([^\\]*)\\(.*)$/) {
220 if (/^([0-3][0-7][0-7])(.*)$/) {
221 $retval .= chr(oct($1));
225 if (/^([\\\042btnvfr])(.*)$/) {
226 $retval .= $cquote_map{$1};
230 # This is malformed -- just return it as-is for now.
241 open $fh, 'git update-index --refresh |'
244 ;# ignore 'needs update'
254 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
257 my $status_fmt = '%12s %12s %s';
258 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
262 sub is_initial_commit
{
263 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
264 unless defined $initial;
270 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
273 sub get_diff_reference
{
275 if (defined $ref and $ref ne 'HEAD') {
277 } elsif (is_initial_commit
()) {
278 return get_empty_tree
();
284 # Returns list of hashes, contents of each of which are:
286 # BINARY: is a binary path
287 # INDEX: is index different from HEAD?
288 # FILE: is file different from index?
289 # INDEX_ADDDEL: is it add/delete between HEAD and index?
290 # FILE_ADDDEL: is it add/delete between index and file?
291 # UNMERGED: is the path unmerged
296 my ($add, $del, $adddel, $file);
303 } run_cmd_pipe
(qw(git ls-files --), @ARGV);
304 return if (!@tracked);
307 my $reference = get_diff_reference
($patch_mode_revision);
308 for (run_cmd_pipe
(qw(git diff-index --cached
309 --numstat --summary), $reference,
311 if (($add, $del, $file) =
312 /^([-\d]+) ([-\d]+) (.*)/) {
314 $file = unquote_path
($file);
315 if ($add eq '-' && $del eq '-') {
320 $change = "+$add/-$del";
328 elsif (($adddel, $file) =
329 /^ (create|delete) mode [0-7]+ (.*)$/) {
330 $file = unquote_path
($file);
331 $data{$file}{INDEX_ADDDEL
} = $adddel;
335 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --raw --), @tracked)) {
336 if (($add, $del, $file) =
337 /^([-\d]+) ([-\d]+) (.*)/) {
338 $file = unquote_path
($file);
340 if ($add eq '-' && $del eq '-') {
345 $change = "+$add/-$del";
347 $data{$file}{FILE
} = $change;
349 $data{$file}{BINARY
} = 1;
352 elsif (($adddel, $file) =
353 /^ (create|delete) mode [0-7]+ (.*)$/) {
354 $file = unquote_path
($file);
355 $data{$file}{FILE_ADDDEL
} = $adddel;
357 elsif (/^:[0-7]+ [0-7]+ [0-9a-f]+ [0-9a-f]+ (.) (.*)$/) {
358 $file = unquote_path
($2);
359 if (!exists $data{$file}) {
361 INDEX
=> 'unchanged',
366 $data{$file}{UNMERGED
} = 1;
371 for (sort keys %data) {
375 if ($only eq 'index-only') {
376 next if ($it->{INDEX
} eq 'unchanged');
378 if ($only eq 'file-only') {
379 next if ($it->{FILE
} eq 'nothing');
391 my ($string, @stuff) = @_;
393 for (my $i = 0; $i < @stuff; $i++) {
397 if ((ref $it) eq 'ARRAY') {
405 if ($it =~ /^$string/) {
409 if (defined $hit && defined $found) {
419 # inserts string into trie and updates count for each character
421 my ($trie, $string) = @_;
422 foreach (split //, $string) {
423 $trie = $trie->{$_} ||= {COUNT
=> 0};
428 # returns an array of tuples (prefix, remainder)
429 sub find_unique_prefixes
{
433 # any single prefix exceeding the soft limit is omitted
434 # if any prefix exceeds the hard limit all are omitted
435 # 0 indicates no limit
439 # build a trie modelling all possible options
441 foreach my $print (@stuff) {
442 if ((ref $print) eq 'ARRAY') {
443 $print = $print->[0];
445 elsif ((ref $print) eq 'HASH') {
446 $print = $print->{VALUE
};
448 update_trie
(\
%trie, $print);
449 push @return, $print;
452 # use the trie to find the unique prefixes
453 for (my $i = 0; $i < @return; $i++) {
454 my $ret = $return[$i];
455 my @letters = split //, $ret;
457 my ($prefix, $remainder);
459 for ($j = 0; $j < @letters; $j++) {
460 my $letter = $letters[$j];
461 if ($search{$letter}{COUNT
} == 1) {
462 $prefix = substr $ret, 0, $j + 1;
463 $remainder = substr $ret, $j + 1;
467 my $prefix = substr $ret, 0, $j;
469 if ($hard_limit && $j + 1 > $hard_limit);
471 %search = %{$search{$letter}};
473 if (ord($letters[0]) > 127 ||
474 ($soft_limit && $j + 1 > $soft_limit)) {
478 $return[$i] = [$prefix, $remainder];
483 # filters out prefixes which have special meaning to list_and_choose()
484 sub is_valid_prefix
{
486 return (defined $prefix) &&
487 !($prefix =~ /[\s,]/) && # separators
488 !($prefix =~ /^-/) && # deselection
489 !($prefix =~ /^\d+/) && # selection
490 ($prefix ne '*') && # "all" wildcard
491 ($prefix ne '?'); # prompt help
494 # given a prefix/remainder tuple return a string with the prefix highlighted
495 # for now use square brackets; later might use ANSI colors (underline, bold)
496 sub highlight_prefix
{
498 my $remainder = shift;
500 if (!defined $prefix) {
504 if (!is_valid_prefix
($prefix)) {
505 return "$prefix$remainder";
508 if (!$menu_use_color) {
509 return "[$prefix]$remainder";
512 return "$prompt_color$prefix$normal_color$remainder";
516 print STDERR colored
$error_color, @_;
519 sub list_and_choose
{
520 my ($opts, @stuff) = @_;
521 my (@chosen, @return);
523 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
529 if ($opts->{HEADER
}) {
530 if (!$opts->{LIST_FLAT
}) {
533 print colored
$header_color, "$opts->{HEADER}\n";
535 for ($i = 0; $i < @stuff; $i++) {
536 my $chosen = $chosen[$i] ?
'*' : ' ';
537 my $print = $stuff[$i];
538 my $ref = ref $print;
539 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
541 if ($ref eq 'ARRAY') {
542 $print = $highlighted || $print->[0];
544 elsif ($ref eq 'HASH') {
545 my $value = $highlighted || $print->{VALUE
};
546 $print = sprintf($status_fmt,
552 $print = $highlighted || $print;
554 printf("%s%2d: %s", $chosen, $i+1, $print);
555 if (($opts->{LIST_FLAT
}) &&
556 (($i + 1) % ($opts->{LIST_FLAT
}))) {
569 return if ($opts->{LIST_ONLY
});
571 print colored
$prompt_color, $opts->{PROMPT
};
572 if ($opts->{SINGLETON
}) {
581 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
588 singleton_prompt_help_cmd
() :
592 for my $choice (split(/[\s,]+/, $line)) {
596 # Input that begins with '-'; unchoose
597 if ($choice =~ s/^-//) {
600 # A range can be specified like 5-7 or 5-.
601 if ($choice =~ /^(\d+)-(\d*)$/) {
602 ($bottom, $top) = ($1, length($2) ?
$2 : 1 + @stuff);
604 elsif ($choice =~ /^\d+$/) {
605 $bottom = $top = $choice;
607 elsif ($choice eq '*') {
612 $bottom = $top = find_unique
($choice, @stuff);
613 if (!defined $bottom) {
614 error_msg
"Huh ($choice)?\n";
618 if ($opts->{SINGLETON
} && $bottom != $top) {
619 error_msg
"Huh ($choice)?\n";
622 for ($i = $bottom-1; $i <= $top-1; $i++) {
623 next if (@stuff <= $i || $i < 0);
624 $chosen[$i] = $choose;
627 last if ($opts->{IMMEDIATE
} || $line eq '*');
629 for ($i = 0; $i < @stuff; $i++) {
631 push @return, $stuff[$i];
637 sub singleton_prompt_help_cmd
{
638 print colored
$help_color, <<\EOF
;
640 1 - select a numbered item
641 foo
- select item based on unique prefix
642 - (empty
) select nothing
646 sub prompt_help_cmd
{
647 print colored
$help_color, <<\EOF
;
649 1 - select a single item
650 3-5 - select a range of items
651 2-3,6-9 - select multiple ranges
652 foo
- select item based on unique prefix
653 -... - unselect specified items
655 - (empty
) finish selecting
660 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
670 print "$cnt paths\n";
678 my @mods = list_modified
('file-only');
681 my @update = list_and_choose
({ PROMPT
=> 'Update',
682 HEADER
=> $status_head, },
685 system(qw(git update-index --add --remove --),
686 map { $_->{VALUE
} } @update);
687 say_n_paths
('updated', @update);
693 my @update = list_and_choose
({ PROMPT
=> 'Revert',
694 HEADER
=> $status_head, },
697 if (is_initial_commit
()) {
698 system(qw(git rm --cached),
699 map { $_->{VALUE
} } @update);
702 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
703 map { $_->{VALUE
} } @update);
705 open $fh, '| git update-index --index-info'
712 if ($_->{INDEX_ADDDEL
} &&
713 $_->{INDEX_ADDDEL
} eq 'create') {
714 system(qw(git update-index --force-remove --),
716 print "note: $_->{VALUE} is untracked now.\n";
721 say_n_paths
('reverted', @update);
726 sub add_untracked_cmd
{
727 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
730 system(qw(git update-index --add --), @add);
731 say_n_paths
('added', @add);
739 open $fh, '| git ' . $cmd . " --recount --allow-overlap";
746 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF
});
747 if (defined $diff_algorithm) {
748 splice @diff_cmd, 1, 0, "--diff-algorithm=${diff_algorithm}";
750 if (defined $patch_mode_revision) {
751 push @diff_cmd, get_diff_reference
($patch_mode_revision);
753 my @diff = run_cmd_pipe
("git", @diff_cmd, "--", $path);
755 if ($diff_use_color) {
756 @colored = run_cmd_pipe
("git", @diff_cmd, qw(--color --), $path);
758 my (@hunk) = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
760 for (my $i = 0; $i < @diff; $i++) {
761 if ($diff[$i] =~ /^@@ /) {
762 push @hunk, { TEXT
=> [], DISPLAY
=> [],
765 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
766 push @
{$hunk[-1]{DISPLAY
}},
767 ($diff_use_color ?
$colored[$i] : $diff[$i]);
772 sub parse_diff_header
{
775 my $head = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
776 my $mode = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'mode' };
777 my $deletion = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'deletion' };
779 for (my $i = 0; $i < @
{$src->{TEXT
}}; $i++) {
781 $src->{TEXT
}->[$i] =~ /^(old|new) mode (\d+)$/ ?
$mode :
782 $src->{TEXT
}->[$i] =~ /^deleted file/ ?
$deletion :
784 push @
{$dest->{TEXT
}}, $src->{TEXT
}->[$i];
785 push @
{$dest->{DISPLAY
}}, $src->{DISPLAY
}->[$i];
787 return ($head, $mode, $deletion);
790 sub hunk_splittable
{
793 my @s = split_hunk
($text);
797 sub parse_hunk_header
{
799 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt, $heading) =
800 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)/;
801 $o_cnt = 1 unless defined $o_cnt;
802 $n_cnt = 1 unless defined $n_cnt;
803 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt, $heading);
807 my ($text, $display) = @_;
809 if (!defined $display) {
812 # If there are context lines in the middle of a hunk,
813 # it can be split, but we would need to take care of
815 my ($o_ofs, undef, $n_ofs, undef, $heading) = parse_hunk_header
($text->[0]);
820 my $next_hunk_start = undef;
821 my $i = $hunk_start - 1;
835 while (++$i < @
$text) {
836 my $line = $text->[$i];
837 my $display = $display->[$i];
839 if ($this->{ADDDEL
} &&
840 !defined $next_hunk_start) {
841 # We have seen leading context and
842 # adds/dels and then here is another
843 # context, which is trailing for this
844 # split hunk and leading for the next
846 $next_hunk_start = $i;
848 push @
{$this->{TEXT
}}, $line;
849 push @
{$this->{DISPLAY
}}, $display;
852 if (defined $next_hunk_start) {
859 if (defined $next_hunk_start) {
860 # We are done with the current hunk and
861 # this is the first real change for the
863 $hunk_start = $next_hunk_start;
864 $o_ofs = $this->{OLD
} + $this->{OCNT
};
865 $n_ofs = $this->{NEW
} + $this->{NCNT
};
866 $o_ofs -= $this->{POSTCTX
};
867 $n_ofs -= $this->{POSTCTX
};
871 push @
{$this->{TEXT
}}, $line;
872 push @
{$this->{DISPLAY
}}, $display;
886 for my $hunk (@split) {
887 $o_ofs = $hunk->{OLD
};
888 $n_ofs = $hunk->{NEW
};
889 my $o_cnt = $hunk->{OCNT
};
890 my $n_cnt = $hunk->{NCNT
};
895 (($o_cnt != 1) ?
",$o_cnt" : ''),
897 (($n_cnt != 1) ?
",$n_cnt" : ''),
900 unshift @
{$hunk->{TEXT
}}, join(
906 unshift @
{$hunk->{DISPLAY
}}, join(
908 $diff_use_color ? colored
($fraginfo_color, $fraginfo) : $fraginfo,
909 $diff_use_color ? colored
($funcname_color, $heading) : $heading,
916 sub find_last_o_ctx
{
918 my $text = $it->{TEXT
};
919 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
921 my $last_o_ctx = $o_ofs + $o_cnt;
923 my $line = $text->[$i];
934 my ($prev, $this) = @_;
935 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
936 parse_hunk_header
($prev->{TEXT
}[0]);
937 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
938 parse_hunk_header
($this->{TEXT
}[0]);
940 my (@line, $i, $ofs, $o_cnt, $n_cnt);
943 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
944 my $line = $prev->{TEXT
}[$i];
945 if ($line =~ /^\+/) {
951 last if ($o1_ofs <= $ofs);
961 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
962 my $line = $this->{TEXT
}[$i];
963 if ($line =~ /^\+/) {
975 my $head = ("@@ -$o0_ofs" .
976 (($o_cnt != 1) ?
",$o_cnt" : '') .
978 (($n_cnt != 1) ?
",$n_cnt" : '') .
980 @
{$prev->{TEXT
}} = ($head, @line);
983 sub coalesce_overlapping_hunks
{
987 my ($last_o_ctx, $last_was_dirty);
989 for (grep { $_->{USE
} } @in) {
990 if ($_->{TYPE
} ne 'hunk') {
994 my $text = $_->{TEXT
};
995 my ($o_ofs) = parse_hunk_header
($text->[0]);
996 if (defined $last_o_ctx &&
997 $o_ofs <= $last_o_ctx &&
1000 merge_hunk
($out[-1], $_);
1005 $last_o_ctx = find_last_o_ctx
($out[-1]);
1006 $last_was_dirty = $_->{DIRTY
};
1011 sub reassemble_patch
{
1015 # Include everything in the header except the beginning of the diff.
1016 push @patch, (grep { !/^[-+]{3}/ } @
$head);
1018 # Then include any headers from the hunk lines, which must
1019 # come before any actual hunk.
1020 while (@_ && $_[0] !~ /^@/) {
1024 # Then begin the diff.
1025 push @patch, grep { /^[-+]{3}/ } @
$head;
1027 # And then the actual hunks.
1035 colored
((/^@/ ?
$fraginfo_color :
1036 /^\+/ ?
$diff_new_color :
1037 /^-/ ?
$diff_old_color :
1043 sub edit_hunk_manually
{
1046 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
1048 open $fh, '>', $hunkfile
1049 or die "failed to open hunk edit file for writing: " . $!;
1050 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
1051 print $fh @
$oldtext;
1052 my $participle = $patch_mode_flavour{PARTICIPLE
};
1053 my $is_reverse = $patch_mode_flavour{IS_REVERSE
};
1054 my ($remove_plus, $remove_minus) = $is_reverse ?
('-', '+') : ('+', '-');
1057 # To remove '$remove_minus' lines, make them ' ' lines (context).
1058 # To remove '$remove_plus' lines, delete them.
1059 # Lines starting with # will be removed.
1061 # If the patch applies cleanly, the edited hunk will immediately be
1062 # marked for $participle. If it does not apply cleanly, you will be given
1063 # an opportunity to edit again. If all lines of the hunk are removed,
1064 # then the edit is aborted and the hunk is left unchanged.
1068 chomp(my $editor = run_cmd_pipe
(qw(git var GIT_EDITOR)));
1069 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
1075 open $fh, '<', $hunkfile
1076 or die "failed to open hunk edit file for reading: " . $!;
1077 my @newtext = grep { !/^#/ } <$fh>;
1081 # Abort if nothing remains
1082 if (!grep { /\S/ } @newtext) {
1086 # Reinsert the first hunk header if the user accidentally deleted it
1087 if ($newtext[0] !~ /^@/) {
1088 unshift @newtext, $oldtext->[0];
1094 return run_git_apply
($patch_mode_flavour{APPLY_CHECK
} . ' --check',
1095 map { @
{$_->{TEXT
}} } @_);
1098 sub _restore_terminal_and_die
{
1104 sub prompt_single_character
{
1106 local $SIG{TERM
} = \
&_restore_terminal_and_die
;
1107 local $SIG{INT
} = \
&_restore_terminal_and_die
;
1109 my $key = ReadKey
0;
1111 if ($use_termcap and $key eq "\e") {
1112 while (!defined $term_escapes{$key}) {
1113 my $next = ReadKey
0.5;
1114 last if (!defined $next);
1119 print "$key" if defined $key;
1130 print colored
$prompt_color, $prompt;
1131 my $line = prompt_single_character
;
1132 return 0 if $line =~ /^n/i;
1133 return 1 if $line =~ /^y/i;
1137 sub edit_hunk_loop
{
1138 my ($head, $hunk, $ix) = @_;
1139 my $text = $hunk->[$ix]->{TEXT
};
1142 $text = edit_hunk_manually
($text);
1143 if (!defined $text) {
1148 TYPE
=> $hunk->[$ix]->{TYPE
},
1152 if (diff_applies
($head,
1155 @
{$hunk}[$ix+1..$#{$hunk}])) {
1156 $newhunk->{DISPLAY
} = [color_diff
(@
{$text})];
1161 'Your edited hunk does not apply. Edit again '
1162 . '(saying "no" discards!) [y/n]? '
1168 sub help_patch_cmd
{
1169 my $verb = lc $patch_mode_flavour{VERB
};
1170 my $target = $patch_mode_flavour{TARGET
};
1171 print colored
$help_color, <<EOF ;
1172 y - $verb this hunk$target
1173 n - do not $verb this hunk$target
1174 q - quit; do not $verb this hunk or any of the remaining ones
1175 a - $verb this hunk and all later hunks in the file
1176 d - do not $verb this hunk or any of the later hunks in the file
1177 g - select a hunk to go to
1178 / - search for a hunk matching the given regex
1179 j - leave this hunk undecided, see next undecided hunk
1180 J - leave this hunk undecided, see next hunk
1181 k - leave this hunk undecided, see previous undecided hunk
1182 K - leave this hunk undecided, see previous hunk
1183 s - split the current hunk into smaller hunks
1184 e - manually edit the current hunk
1191 my $ret = run_git_apply
$cmd, @_;
1198 sub apply_patch_for_checkout_commit
{
1199 my $reverse = shift;
1200 my $applies_index = run_git_apply
'apply '.$reverse.' --cached --check', @_;
1201 my $applies_worktree = run_git_apply
'apply '.$reverse.' --check', @_;
1203 if ($applies_worktree && $applies_index) {
1204 run_git_apply
'apply '.$reverse.' --cached', @_;
1205 run_git_apply
'apply '.$reverse, @_;
1207 } elsif (!$applies_index) {
1208 print colored
$error_color, "The selected hunks do not apply to the index!\n";
1209 if (prompt_yesno
"Apply them to the worktree anyway? ") {
1210 return run_git_apply
'apply '.$reverse, @_;
1212 print colored
$error_color, "Nothing was applied.\n";
1221 sub patch_update_cmd
{
1222 my @all_mods = list_modified
($patch_mode_flavour{FILTER
});
1223 error_msg
"ignoring unmerged: $_->{VALUE}\n"
1224 for grep { $_->{UNMERGED
} } @all_mods;
1225 @all_mods = grep { !$_->{UNMERGED
} } @all_mods;
1227 my @mods = grep { !($_->{BINARY
}) } @all_mods;
1232 print STDERR
"Only binary files changed.\n";
1234 print STDERR
"No changes.\n";
1242 @them = list_and_choose
({ PROMPT
=> 'Patch update',
1243 HEADER
=> $status_head, },
1247 return 0 if patch_update_file
($_->{VALUE
});
1251 # Generate a one line summary of a hunk.
1252 sub summarize_hunk
{
1254 my $summary = $rhunk->{TEXT
}[0];
1256 # Keep the line numbers, discard extra context.
1257 $summary =~ s/@@(.*?)@@.*/$1 /s;
1258 $summary .= " " x
(20 - length $summary);
1260 # Add some user context.
1261 for my $line (@
{$rhunk->{TEXT
}}) {
1262 if ($line =~ m/^[+-].*\w/) {
1269 return substr($summary, 0, 80) . "\n";
1273 # Print a one-line summary of each hunk in the array ref in
1274 # the first argument, starting with the index in the 2nd.
1276 my ($hunks, $i) = @_;
1279 for (; $i < @
$hunks && $ctr < 20; $i++, $ctr++) {
1281 if (defined $hunks->[$i]{USE
}) {
1282 $status = $hunks->[$i]{USE
} ?
"+" : "-";
1287 summarize_hunk
($hunks->[$i]);
1292 sub patch_update_file
{
1296 my ($head, @hunk) = parse_diff
($path);
1297 ($head, my $mode, my $deletion) = parse_diff_header
($head);
1298 for (@
{$head->{DISPLAY
}}) {
1302 if (@
{$mode->{TEXT
}}) {
1303 unshift @hunk, $mode;
1305 if (@
{$deletion->{TEXT
}}) {
1306 foreach my $hunk (@hunk) {
1307 push @
{$deletion->{TEXT
}}, @
{$hunk->{TEXT
}};
1308 push @
{$deletion->{DISPLAY
}}, @
{$hunk->{DISPLAY
}};
1310 @hunk = ($deletion);
1313 $num = scalar @hunk;
1317 my ($prev, $next, $other, $undecided, $i);
1323 for ($i = 0; $i < $ix; $i++) {
1324 if (!defined $hunk[$i]{USE
}) {
1333 for ($i = $ix + 1; $i < $num; $i++) {
1334 if (!defined $hunk[$i]{USE
}) {
1340 if ($ix < $num - 1) {
1346 for ($i = 0; $i < $num; $i++) {
1347 if (!defined $hunk[$i]{USE
}) {
1352 last if (!$undecided);
1354 if ($hunk[$ix]{TYPE
} eq 'hunk' &&
1355 hunk_splittable
($hunk[$ix]{TEXT
})) {
1358 if ($hunk[$ix]{TYPE
} eq 'hunk') {
1361 for (@
{$hunk[$ix]{DISPLAY
}}) {
1364 print colored
$prompt_color, $patch_mode_flavour{VERB
},
1365 ($hunk[$ix]{TYPE
} eq 'mode' ?
' mode change' :
1366 $hunk[$ix]{TYPE
} eq 'deletion' ?
' deletion' :
1368 $patch_mode_flavour{TARGET
},
1369 " [y,n,q,a,d,/$other,?]? ";
1370 my $line = prompt_single_character
;
1372 if ($line =~ /^y/i) {
1373 $hunk[$ix]{USE
} = 1;
1375 elsif ($line =~ /^n/i) {
1376 $hunk[$ix]{USE
} = 0;
1378 elsif ($line =~ /^a/i) {
1379 while ($ix < $num) {
1380 if (!defined $hunk[$ix]{USE
}) {
1381 $hunk[$ix]{USE
} = 1;
1387 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1389 my $no = $ix > 10 ?
$ix - 10 : 0;
1390 while ($response eq '') {
1392 $no = display_hunks
(\
@hunk, $no);
1394 $extra = " (<ret> to see more)";
1396 print "go to which hunk$extra? ";
1397 $response = <STDIN
>;
1398 if (!defined $response) {
1403 if ($response !~ /^\s*\d+\s*$/) {
1404 error_msg
"Invalid number: '$response'\n";
1405 } elsif (0 < $response && $response <= $num) {
1406 $ix = $response - 1;
1408 error_msg
"Sorry, only $num hunks available.\n";
1412 elsif ($line =~ /^d/i) {
1413 while ($ix < $num) {
1414 if (!defined $hunk[$ix]{USE
}) {
1415 $hunk[$ix]{USE
} = 0;
1421 elsif ($line =~ /^q/i) {
1422 for ($i = 0; $i < $num; $i++) {
1423 if (!defined $hunk[$i]{USE
}) {
1430 elsif ($line =~ m
|^/(.*)|) {
1433 print colored
$prompt_color, "search for regex? ";
1435 if (defined $regex) {
1441 $search_string = qr{$regex}m;
1444 my ($err,$exp) = ($@
, $1);
1445 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1446 error_msg
"Malformed search regexp $exp: $err\n";
1451 my $text = join ("", @
{$hunk[$iy]{TEXT
}});
1452 last if ($text =~ $search_string);
1454 $iy = 0 if ($iy >= $num);
1456 error_msg
"No hunk matches the given pattern\n";
1463 elsif ($line =~ /^K/) {
1464 if ($other =~ /K/) {
1468 error_msg
"No previous hunk\n";
1472 elsif ($line =~ /^J/) {
1473 if ($other =~ /J/) {
1477 error_msg
"No next hunk\n";
1481 elsif ($line =~ /^k/) {
1482 if ($other =~ /k/) {
1486 !defined $hunk[$ix]{USE
});
1490 error_msg
"No previous hunk\n";
1494 elsif ($line =~ /^j/) {
1495 if ($other !~ /j/) {
1496 error_msg
"No next hunk\n";
1500 elsif ($other =~ /s/ && $line =~ /^s/) {
1501 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
1503 print colored
$header_color, "Split into ",
1504 scalar(@split), " hunks.\n";
1506 splice (@hunk, $ix, 1, @split);
1507 $num = scalar @hunk;
1510 elsif ($other =~ /e/ && $line =~ /^e/) {
1511 my $newhunk = edit_hunk_loop
($head, \
@hunk, $ix);
1512 if (defined $newhunk) {
1513 splice @hunk, $ix, 1, $newhunk;
1517 help_patch_cmd
($other);
1523 last if ($ix >= $num ||
1524 !defined $hunk[$ix]{USE
});
1529 @hunk = coalesce_overlapping_hunks
(@hunk);
1535 push @result, @
{$_->{TEXT
}};
1540 my @patch = reassemble_patch
($head->{TEXT
}, @result);
1541 my $apply_routine = $patch_mode_flavour{APPLY
};
1542 &$apply_routine(@patch);
1551 my @mods = list_modified
('index-only');
1552 @mods = grep { !($_->{BINARY
}) } @mods;
1554 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
1556 HEADER
=> $status_head, },
1559 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
1560 system(qw(git diff -p --cached), $reference, '--',
1561 map { $_->{VALUE
} } @them);
1570 print colored
$help_color, <<\EOF
;
1571 status
- show paths with changes
1572 update
- add working tree
state to the staged set of changes
1573 revert
- revert staged set of changes back to the HEAD version
1574 patch
- pick hunks
and update selectively
1575 diff
- view diff between HEAD
and index
1576 add untracked
- add contents of untracked files to the staged set of changes
1581 return unless @ARGV;
1582 my $arg = shift @ARGV;
1583 if ($arg =~ /--patch(?:=(.*))?/) {
1585 if ($1 eq 'reset') {
1586 $patch_mode = 'reset_head';
1587 $patch_mode_revision = 'HEAD';
1588 $arg = shift @ARGV or die "missing --";
1590 $patch_mode_revision = $arg;
1591 $patch_mode = ($arg eq 'HEAD' ?
1592 'reset_head' : 'reset_nothead');
1593 $arg = shift @ARGV or die "missing --";
1595 } elsif ($1 eq 'checkout') {
1596 $arg = shift @ARGV or die "missing --";
1598 $patch_mode = 'checkout_index';
1600 $patch_mode_revision = $arg;
1601 $patch_mode = ($arg eq 'HEAD' ?
1602 'checkout_head' : 'checkout_nothead');
1603 $arg = shift @ARGV or die "missing --";
1605 } elsif ($1 eq 'stage' or $1 eq 'stash') {
1607 $arg = shift @ARGV or die "missing --";
1609 die "unknown --patch mode: $1";
1612 $patch_mode = 'stage';
1613 $arg = shift @ARGV or die "missing --";
1615 die "invalid argument $arg, expecting --"
1616 unless $arg eq "--";
1617 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
1619 elsif ($arg ne "--") {
1620 die "invalid argument $arg, expecting --";
1625 my @cmd = ([ 'status', \
&status_cmd
, ],
1626 [ 'update', \
&update_cmd
, ],
1627 [ 'revert', \
&revert_cmd
, ],
1628 [ 'add untracked', \
&add_untracked_cmd
, ],
1629 [ 'patch', \
&patch_update_cmd
, ],
1630 [ 'diff', \
&diff_cmd
, ],
1631 [ 'quit', \
&quit_cmd
, ],
1632 [ 'help', \
&help_cmd
, ],
1635 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1638 HEADER
=> '*** Commands ***',
1639 ON_EOF
=> \
&quit_cmd
,
1640 IMMEDIATE
=> 1 }, @cmd);