6 binmode(STDOUT
, ":raw");
8 my $repo = Git
->repository();
10 my $menu_use_color = $repo->get_colorbool('color.interactive');
11 my ($prompt_color, $header_color, $help_color) =
13 $repo->get_color('color.interactive.prompt', 'bold blue'),
14 $repo->get_color('color.interactive.header', 'bold'),
15 $repo->get_color('color.interactive.help', 'red bold'),
18 if ($menu_use_color) {
19 my $help_color_spec = ($repo->config('color.interactive.help') or
21 $error_color = $repo->get_color('color.interactive.error',
25 my $diff_use_color = $repo->get_colorbool('color.diff');
26 my ($fraginfo_color) =
28 $repo->get_color('color.diff.frag', 'cyan'),
30 my ($diff_plain_color) =
32 $repo->get_color('color.diff.plain', ''),
34 my ($diff_old_color) =
36 $repo->get_color('color.diff.old', 'red'),
38 my ($diff_new_color) =
40 $repo->get_color('color.diff.new', 'green'),
43 my $normal_color = $repo->get_color("", "reset");
48 if ($repo->config_bool("interactive.singlekey")) {
50 require Term
::ReadKey
;
51 Term
::ReadKey
->import;
58 my $string = join("", @_);
61 # Put a color code at the beginning of each line, a reset at the end
62 # color after newlines that are not at the end of the string
63 $string =~ s/(\n+)(.)/$1$color$2/g;
64 # reset before newlines
65 $string =~ s/(\n+)/$normal_color$1/g;
66 # codes at beginning and end (if necessary):
67 $string =~ s/^/$color/;
68 $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
73 # command line options
75 my $patch_mode_revision;
78 sub apply_patch_for_checkout_commit
;
79 sub apply_patch_for_stash
;
83 DIFF
=> 'diff-files -p',
84 APPLY
=> sub { apply_patch
'apply --cached', @_; },
85 APPLY_CHECK
=> 'apply --cached',
88 PARTICIPLE
=> 'staging',
89 FILTER
=> 'file-only',
93 DIFF
=> 'diff-index -p HEAD',
94 APPLY
=> sub { apply_patch
'apply --cached', @_; },
95 APPLY_CHECK
=> 'apply --cached',
98 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',
113 DIFF
=> 'diff-index -R -p --cached',
114 APPLY
=> sub { apply_patch
'apply --cached', @_; },
115 APPLY_CHECK
=> 'apply --cached',
117 TARGET
=> ' to index',
118 PARTICIPLE
=> 'applying',
119 FILTER
=> 'index-only',
122 'checkout_index' => {
123 DIFF
=> 'diff-files -p',
124 APPLY
=> sub { apply_patch
'apply -R', @_; },
125 APPLY_CHECK
=> 'apply -R',
127 TARGET
=> ' from worktree',
128 PARTICIPLE
=> 'discarding',
129 FILTER
=> 'file-only',
133 DIFF
=> 'diff-index -p',
134 APPLY
=> sub { apply_patch_for_checkout_commit
'-R', @_ },
135 APPLY_CHECK
=> 'apply -R',
137 TARGET
=> ' from index and worktree',
138 PARTICIPLE
=> 'discarding',
142 'checkout_nothead' => {
143 DIFF
=> 'diff-index -R -p',
144 APPLY
=> sub { apply_patch_for_checkout_commit
'', @_ },
145 APPLY_CHECK
=> 'apply',
147 TARGET
=> ' to index and worktree',
148 PARTICIPLE
=> 'applying',
154 my %patch_mode_flavour = %{$patch_modes{stage
}};
157 if ($^O
eq 'MSWin32' || $^O
eq 'msys') {
158 my @invalid = grep {m/[":*]/} @_;
159 die "$^O does not support: @invalid\n" if @invalid;
160 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
164 open($fh, '-|', @_) or die;
169 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
171 if (!defined $GIT_DIR) {
172 exit(1); # rev-parse would have already said "not a git repo"
189 my ($retval, $remainder);
190 if (!/^\042(.*)\042$/) {
193 ($_, $retval) = ($1, "");
194 while (/^([^\\]*)\\(.*)$/) {
198 if (/^([0-3][0-7][0-7])(.*)$/) {
199 $retval .= chr(oct($1));
203 if (/^([\\\042btnvfr])(.*)$/) {
204 $retval .= $cquote_map{$1};
208 # This is malformed -- just return it as-is for now.
219 open $fh, 'git update-index --refresh |'
222 ;# ignore 'needs update'
232 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
235 my $status_fmt = '%12s %12s %s';
236 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
240 sub is_initial_commit
{
241 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
242 unless defined $initial;
248 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
251 # Returns list of hashes, contents of each of which are:
253 # BINARY: is a binary path
254 # INDEX: is index different from HEAD?
255 # FILE: is file different from index?
256 # INDEX_ADDDEL: is it add/delete between HEAD and index?
257 # FILE_ADDDEL: is it add/delete between index and file?
262 my ($add, $del, $adddel, $file);
269 } run_cmd_pipe
(qw(git ls-files --), @ARGV);
270 return if (!@tracked);
274 if (defined $patch_mode_revision and $patch_mode_revision ne 'HEAD') {
275 $reference = $patch_mode_revision;
276 } elsif (is_initial_commit
()) {
277 $reference = get_empty_tree
();
281 for (run_cmd_pipe
(qw(git diff-index --cached
282 --numstat --summary), $reference,
284 if (($add, $del, $file) =
285 /^([-\d]+) ([-\d]+) (.*)/) {
287 $file = unquote_path
($file);
288 if ($add eq '-' && $del eq '-') {
293 $change = "+$add/-$del";
301 elsif (($adddel, $file) =
302 /^ (create|delete) mode [0-7]+ (.*)$/) {
303 $file = unquote_path
($file);
304 $data{$file}{INDEX_ADDDEL
} = $adddel;
308 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
309 if (($add, $del, $file) =
310 /^([-\d]+) ([-\d]+) (.*)/) {
311 $file = unquote_path
($file);
312 if (!exists $data{$file}) {
314 INDEX
=> 'unchanged',
319 if ($add eq '-' && $del eq '-') {
324 $change = "+$add/-$del";
326 $data{$file}{FILE
} = $change;
328 $data{$file}{BINARY
} = 1;
331 elsif (($adddel, $file) =
332 /^ (create|delete) mode [0-7]+ (.*)$/) {
333 $file = unquote_path
($file);
334 $data{$file}{FILE_ADDDEL
} = $adddel;
338 for (sort keys %data) {
342 if ($only eq 'index-only') {
343 next if ($it->{INDEX
} eq 'unchanged');
345 if ($only eq 'file-only') {
346 next if ($it->{FILE
} eq 'nothing');
358 my ($string, @stuff) = @_;
360 for (my $i = 0; $i < @stuff; $i++) {
364 if ((ref $it) eq 'ARRAY') {
372 if ($it =~ /^$string/) {
376 if (defined $hit && defined $found) {
386 # inserts string into trie and updates count for each character
388 my ($trie, $string) = @_;
389 foreach (split //, $string) {
390 $trie = $trie->{$_} ||= {COUNT
=> 0};
395 # returns an array of tuples (prefix, remainder)
396 sub find_unique_prefixes
{
400 # any single prefix exceeding the soft limit is omitted
401 # if any prefix exceeds the hard limit all are omitted
402 # 0 indicates no limit
406 # build a trie modelling all possible options
408 foreach my $print (@stuff) {
409 if ((ref $print) eq 'ARRAY') {
410 $print = $print->[0];
412 elsif ((ref $print) eq 'HASH') {
413 $print = $print->{VALUE
};
415 update_trie
(\
%trie, $print);
416 push @return, $print;
419 # use the trie to find the unique prefixes
420 for (my $i = 0; $i < @return; $i++) {
421 my $ret = $return[$i];
422 my @letters = split //, $ret;
424 my ($prefix, $remainder);
426 for ($j = 0; $j < @letters; $j++) {
427 my $letter = $letters[$j];
428 if ($search{$letter}{COUNT
} == 1) {
429 $prefix = substr $ret, 0, $j + 1;
430 $remainder = substr $ret, $j + 1;
434 my $prefix = substr $ret, 0, $j;
436 if ($hard_limit && $j + 1 > $hard_limit);
438 %search = %{$search{$letter}};
440 if (ord($letters[0]) > 127 ||
441 ($soft_limit && $j + 1 > $soft_limit)) {
445 $return[$i] = [$prefix, $remainder];
450 # filters out prefixes which have special meaning to list_and_choose()
451 sub is_valid_prefix
{
453 return (defined $prefix) &&
454 !($prefix =~ /[\s,]/) && # separators
455 !($prefix =~ /^-/) && # deselection
456 !($prefix =~ /^\d+/) && # selection
457 ($prefix ne '*') && # "all" wildcard
458 ($prefix ne '?'); # prompt help
461 # given a prefix/remainder tuple return a string with the prefix highlighted
462 # for now use square brackets; later might use ANSI colors (underline, bold)
463 sub highlight_prefix
{
465 my $remainder = shift;
467 if (!defined $prefix) {
471 if (!is_valid_prefix
($prefix)) {
472 return "$prefix$remainder";
475 if (!$menu_use_color) {
476 return "[$prefix]$remainder";
479 return "$prompt_color$prefix$normal_color$remainder";
483 print STDERR colored
$error_color, @_;
486 sub list_and_choose
{
487 my ($opts, @stuff) = @_;
488 my (@chosen, @return);
490 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
496 if ($opts->{HEADER
}) {
497 if (!$opts->{LIST_FLAT
}) {
500 print colored
$header_color, "$opts->{HEADER}\n";
502 for ($i = 0; $i < @stuff; $i++) {
503 my $chosen = $chosen[$i] ?
'*' : ' ';
504 my $print = $stuff[$i];
505 my $ref = ref $print;
506 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
508 if ($ref eq 'ARRAY') {
509 $print = $highlighted || $print->[0];
511 elsif ($ref eq 'HASH') {
512 my $value = $highlighted || $print->{VALUE
};
513 $print = sprintf($status_fmt,
519 $print = $highlighted || $print;
521 printf("%s%2d: %s", $chosen, $i+1, $print);
522 if (($opts->{LIST_FLAT
}) &&
523 (($i + 1) % ($opts->{LIST_FLAT
}))) {
536 return if ($opts->{LIST_ONLY
});
538 print colored
$prompt_color, $opts->{PROMPT
};
539 if ($opts->{SINGLETON
}) {
548 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
555 singleton_prompt_help_cmd
() :
559 for my $choice (split(/[\s,]+/, $line)) {
563 # Input that begins with '-'; unchoose
564 if ($choice =~ s/^-//) {
567 # A range can be specified like 5-7 or 5-.
568 if ($choice =~ /^(\d+)-(\d*)$/) {
569 ($bottom, $top) = ($1, length($2) ?
$2 : 1 + @stuff);
571 elsif ($choice =~ /^\d+$/) {
572 $bottom = $top = $choice;
574 elsif ($choice eq '*') {
579 $bottom = $top = find_unique
($choice, @stuff);
580 if (!defined $bottom) {
581 error_msg
"Huh ($choice)?\n";
585 if ($opts->{SINGLETON
} && $bottom != $top) {
586 error_msg
"Huh ($choice)?\n";
589 for ($i = $bottom-1; $i <= $top-1; $i++) {
590 next if (@stuff <= $i || $i < 0);
591 $chosen[$i] = $choose;
594 last if ($opts->{IMMEDIATE
} || $line eq '*');
596 for ($i = 0; $i < @stuff; $i++) {
598 push @return, $stuff[$i];
604 sub singleton_prompt_help_cmd
{
605 print colored
$help_color, <<\EOF
;
607 1 - select a numbered item
608 foo
- select item based on unique prefix
609 - (empty
) select nothing
613 sub prompt_help_cmd
{
614 print colored
$help_color, <<\EOF
;
616 1 - select a single item
617 3-5 - select a range of items
618 2-3,6-9 - select multiple ranges
619 foo
- select item based on unique prefix
620 -... - unselect specified items
622 - (empty
) finish selecting
627 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
637 print "$cnt paths\n";
645 my @mods = list_modified
('file-only');
648 my @update = list_and_choose
({ PROMPT
=> 'Update',
649 HEADER
=> $status_head, },
652 system(qw(git update-index --add --remove --),
653 map { $_->{VALUE
} } @update);
654 say_n_paths
('updated', @update);
660 my @update = list_and_choose
({ PROMPT
=> 'Revert',
661 HEADER
=> $status_head, },
664 if (is_initial_commit
()) {
665 system(qw(git rm --cached),
666 map { $_->{VALUE
} } @update);
669 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
670 map { $_->{VALUE
} } @update);
672 open $fh, '| git update-index --index-info'
679 if ($_->{INDEX_ADDDEL
} &&
680 $_->{INDEX_ADDDEL
} eq 'create') {
681 system(qw(git update-index --force-remove --),
683 print "note: $_->{VALUE} is untracked now.\n";
688 say_n_paths
('reverted', @update);
693 sub add_untracked_cmd
{
694 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
697 system(qw(git update-index --add --), @add);
698 say_n_paths
('added', @add);
706 open $fh, '| git ' . $cmd;
713 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF
});
714 if (defined $patch_mode_revision) {
715 push @diff_cmd, $patch_mode_revision;
717 my @diff = run_cmd_pipe
("git", @diff_cmd, "--", $path);
719 if ($diff_use_color) {
720 @colored = run_cmd_pipe
("git", @diff_cmd, qw(--color --), $path);
722 my (@hunk) = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
724 for (my $i = 0; $i < @diff; $i++) {
725 if ($diff[$i] =~ /^@@ /) {
726 push @hunk, { TEXT
=> [], DISPLAY
=> [],
729 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
730 push @
{$hunk[-1]{DISPLAY
}},
731 ($diff_use_color ?
$colored[$i] : $diff[$i]);
736 sub parse_diff_header
{
739 my $head = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
740 my $mode = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'mode' };
741 my $deletion = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'deletion' };
743 for (my $i = 0; $i < @
{$src->{TEXT
}}; $i++) {
745 $src->{TEXT
}->[$i] =~ /^(old|new) mode (\d+)$/ ?
$mode :
746 $src->{TEXT
}->[$i] =~ /^deleted file/ ?
$deletion :
748 push @
{$dest->{TEXT
}}, $src->{TEXT
}->[$i];
749 push @
{$dest->{DISPLAY
}}, $src->{DISPLAY
}->[$i];
751 return ($head, $mode, $deletion);
754 sub hunk_splittable
{
757 my @s = split_hunk
($text);
761 sub parse_hunk_header
{
763 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
764 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
765 $o_cnt = 1 unless defined $o_cnt;
766 $n_cnt = 1 unless defined $n_cnt;
767 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
771 my ($text, $display) = @_;
773 if (!defined $display) {
776 # If there are context lines in the middle of a hunk,
777 # it can be split, but we would need to take care of
780 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
785 my $next_hunk_start = undef;
786 my $i = $hunk_start - 1;
800 while (++$i < @
$text) {
801 my $line = $text->[$i];
802 my $display = $display->[$i];
804 if ($this->{ADDDEL
} &&
805 !defined $next_hunk_start) {
806 # We have seen leading context and
807 # adds/dels and then here is another
808 # context, which is trailing for this
809 # split hunk and leading for the next
811 $next_hunk_start = $i;
813 push @
{$this->{TEXT
}}, $line;
814 push @
{$this->{DISPLAY
}}, $display;
817 if (defined $next_hunk_start) {
824 if (defined $next_hunk_start) {
825 # We are done with the current hunk and
826 # this is the first real change for the
828 $hunk_start = $next_hunk_start;
829 $o_ofs = $this->{OLD
} + $this->{OCNT
};
830 $n_ofs = $this->{NEW
} + $this->{NCNT
};
831 $o_ofs -= $this->{POSTCTX
};
832 $n_ofs -= $this->{POSTCTX
};
836 push @
{$this->{TEXT
}}, $line;
837 push @
{$this->{DISPLAY
}}, $display;
851 for my $hunk (@split) {
852 $o_ofs = $hunk->{OLD
};
853 $n_ofs = $hunk->{NEW
};
854 my $o_cnt = $hunk->{OCNT
};
855 my $n_cnt = $hunk->{NCNT
};
857 my $head = ("@@ -$o_ofs" .
858 (($o_cnt != 1) ?
",$o_cnt" : '') .
860 (($n_cnt != 1) ?
",$n_cnt" : '') .
862 my $display_head = $head;
863 unshift @
{$hunk->{TEXT
}}, $head;
864 if ($diff_use_color) {
865 $display_head = colored
($fraginfo_color, $head);
867 unshift @
{$hunk->{DISPLAY
}}, $display_head;
872 sub find_last_o_ctx
{
874 my $text = $it->{TEXT
};
875 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
877 my $last_o_ctx = $o_ofs + $o_cnt;
879 my $line = $text->[$i];
890 my ($prev, $this) = @_;
891 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
892 parse_hunk_header
($prev->{TEXT
}[0]);
893 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
894 parse_hunk_header
($this->{TEXT
}[0]);
896 my (@line, $i, $ofs, $o_cnt, $n_cnt);
899 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
900 my $line = $prev->{TEXT
}[$i];
901 if ($line =~ /^\+/) {
907 last if ($o1_ofs <= $ofs);
917 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
918 my $line = $this->{TEXT
}[$i];
919 if ($line =~ /^\+/) {
931 my $head = ("@@ -$o0_ofs" .
932 (($o_cnt != 1) ?
",$o_cnt" : '') .
934 (($n_cnt != 1) ?
",$n_cnt" : '') .
936 @
{$prev->{TEXT
}} = ($head, @line);
939 sub coalesce_overlapping_hunks
{
943 my ($last_o_ctx, $last_was_dirty);
945 for (grep { $_->{USE
} } @in) {
946 if ($_->{TYPE
} ne 'hunk') {
950 my $text = $_->{TEXT
};
951 my ($o_ofs) = parse_hunk_header
($text->[0]);
952 if (defined $last_o_ctx &&
953 $o_ofs <= $last_o_ctx &&
956 merge_hunk
($out[-1], $_);
961 $last_o_ctx = find_last_o_ctx
($out[-1]);
962 $last_was_dirty = $_->{DIRTY
};
967 sub reassemble_patch
{
971 # Include everything in the header except the beginning of the diff.
972 push @patch, (grep { !/^[-+]{3}/ } @
$head);
974 # Then include any headers from the hunk lines, which must
975 # come before any actual hunk.
976 while (@_ && $_[0] !~ /^@/) {
980 # Then begin the diff.
981 push @patch, grep { /^[-+]{3}/ } @
$head;
983 # And then the actual hunks.
991 colored
((/^@/ ?
$fraginfo_color :
992 /^\+/ ?
$diff_new_color :
993 /^-/ ?
$diff_old_color :
999 sub edit_hunk_manually
{
1002 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
1004 open $fh, '>', $hunkfile
1005 or die "failed to open hunk edit file for writing: " . $!;
1006 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
1007 print $fh @
$oldtext;
1008 my $participle = $patch_mode_flavour{PARTICIPLE
};
1009 my $is_reverse = $patch_mode_flavour{IS_REVERSE
};
1010 my ($remove_plus, $remove_minus) = $is_reverse ?
('-', '+') : ('+', '-');
1013 # To remove '$remove_minus' lines, make them ' ' lines (context).
1014 # To remove '$remove_plus' lines, delete them.
1015 # Lines starting with # will be removed.
1017 # If the patch applies cleanly, the edited hunk will immediately be
1018 # marked for $participle. If it does not apply cleanly, you will be given
1019 # an opportunity to edit again. If all lines of the hunk are removed,
1020 # then the edit is aborted and the hunk is left unchanged.
1024 chomp(my $editor = run_cmd_pipe
(qw(git var GIT_EDITOR)));
1025 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
1031 open $fh, '<', $hunkfile
1032 or die "failed to open hunk edit file for reading: " . $!;
1033 my @newtext = grep { !/^#/ } <$fh>;
1037 # Abort if nothing remains
1038 if (!grep { /\S/ } @newtext) {
1042 # Reinsert the first hunk header if the user accidentally deleted it
1043 if ($newtext[0] !~ /^@/) {
1044 unshift @newtext, $oldtext->[0];
1051 return run_git_apply
($patch_mode_flavour{APPLY_CHECK
} . ' --recount --check',
1052 map { @
{$_->{TEXT
}} } @_);
1055 sub _restore_terminal_and_die
{
1061 sub prompt_single_character
{
1063 local $SIG{TERM
} = \
&_restore_terminal_and_die
;
1064 local $SIG{INT
} = \
&_restore_terminal_and_die
;
1066 my $key = ReadKey
0;
1068 print "$key" if defined $key;
1079 print colored
$prompt_color, $prompt;
1080 my $line = prompt_single_character
;
1081 return 0 if $line =~ /^n/i;
1082 return 1 if $line =~ /^y/i;
1086 sub edit_hunk_loop
{
1087 my ($head, $hunk, $ix) = @_;
1088 my $text = $hunk->[$ix]->{TEXT
};
1091 $text = edit_hunk_manually
($text);
1092 if (!defined $text) {
1097 TYPE
=> $hunk->[$ix]->{TYPE
},
1101 if (diff_applies
($head,
1104 @
{$hunk}[$ix+1..$#{$hunk}])) {
1105 $newhunk->{DISPLAY
} = [color_diff
(@
{$text})];
1110 'Your edited hunk does not apply. Edit again '
1111 . '(saying "no" discards!) [y/n]? '
1117 sub help_patch_cmd
{
1118 my $verb = lc $patch_mode_flavour{VERB
};
1119 my $target = $patch_mode_flavour{TARGET
};
1120 print colored
$help_color, <<EOF ;
1121 y - $verb this hunk$target
1122 n - do not $verb this hunk$target
1123 q - quit; do not $verb this hunk nor any of the remaining ones
1124 a - $verb this hunk and all later hunks in the file
1125 d - do not $verb this hunk nor any of the later hunks in the file
1126 g - select a hunk to go to
1127 / - search for a hunk matching the given regex
1128 j - leave this hunk undecided, see next undecided hunk
1129 J - leave this hunk undecided, see next hunk
1130 k - leave this hunk undecided, see previous undecided hunk
1131 K - leave this hunk undecided, see previous hunk
1132 s - split the current hunk into smaller hunks
1133 e - manually edit the current hunk
1140 my $ret = run_git_apply
$cmd . ' --recount', @_;
1147 sub apply_patch_for_checkout_commit
{
1148 my $reverse = shift;
1149 my $applies_index = run_git_apply
'apply '.$reverse.' --cached --recount --check', @_;
1150 my $applies_worktree = run_git_apply
'apply '.$reverse.' --recount --check', @_;
1152 if ($applies_worktree && $applies_index) {
1153 run_git_apply
'apply '.$reverse.' --cached --recount', @_;
1154 run_git_apply
'apply '.$reverse.' --recount', @_;
1156 } elsif (!$applies_index) {
1157 print colored
$error_color, "The selected hunks do not apply to the index!\n";
1158 if (prompt_yesno
"Apply them to the worktree anyway? ") {
1159 return run_git_apply
'apply '.$reverse.' --recount', @_;
1161 print colored
$error_color, "Nothing was applied.\n";
1170 sub patch_update_cmd
{
1171 my @all_mods = list_modified
($patch_mode_flavour{FILTER
});
1172 my @mods = grep { !($_->{BINARY
}) } @all_mods;
1177 print STDERR
"Only binary files changed.\n";
1179 print STDERR
"No changes.\n";
1187 @them = list_and_choose
({ PROMPT
=> 'Patch update',
1188 HEADER
=> $status_head, },
1192 return 0 if patch_update_file
($_->{VALUE
});
1196 # Generate a one line summary of a hunk.
1197 sub summarize_hunk
{
1199 my $summary = $rhunk->{TEXT
}[0];
1201 # Keep the line numbers, discard extra context.
1202 $summary =~ s/@@(.*?)@@.*/$1 /s;
1203 $summary .= " " x
(20 - length $summary);
1205 # Add some user context.
1206 for my $line (@
{$rhunk->{TEXT
}}) {
1207 if ($line =~ m/^[+-].*\w/) {
1214 return substr($summary, 0, 80) . "\n";
1218 # Print a one-line summary of each hunk in the array ref in
1219 # the first argument, starting wih the index in the 2nd.
1221 my ($hunks, $i) = @_;
1224 for (; $i < @
$hunks && $ctr < 20; $i++, $ctr++) {
1226 if (defined $hunks->[$i]{USE
}) {
1227 $status = $hunks->[$i]{USE
} ?
"+" : "-";
1232 summarize_hunk
($hunks->[$i]);
1237 sub patch_update_file
{
1241 my ($head, @hunk) = parse_diff
($path);
1242 ($head, my $mode, my $deletion) = parse_diff_header
($head);
1243 for (@
{$head->{DISPLAY
}}) {
1247 if (@
{$mode->{TEXT
}}) {
1248 unshift @hunk, $mode;
1250 if (@
{$deletion->{TEXT
}}) {
1251 foreach my $hunk (@hunk) {
1252 push @
{$deletion->{TEXT
}}, @
{$hunk->{TEXT
}};
1253 push @
{$deletion->{DISPLAY
}}, @
{$hunk->{DISPLAY
}};
1255 @hunk = ($deletion);
1258 $num = scalar @hunk;
1262 my ($prev, $next, $other, $undecided, $i);
1268 for ($i = 0; $i < $ix; $i++) {
1269 if (!defined $hunk[$i]{USE
}) {
1278 for ($i = $ix + 1; $i < $num; $i++) {
1279 if (!defined $hunk[$i]{USE
}) {
1285 if ($ix < $num - 1) {
1291 for ($i = 0; $i < $num; $i++) {
1292 if (!defined $hunk[$i]{USE
}) {
1297 last if (!$undecided);
1299 if ($hunk[$ix]{TYPE
} eq 'hunk' &&
1300 hunk_splittable
($hunk[$ix]{TEXT
})) {
1303 if ($hunk[$ix]{TYPE
} eq 'hunk') {
1306 for (@
{$hunk[$ix]{DISPLAY
}}) {
1309 print colored
$prompt_color, $patch_mode_flavour{VERB
},
1310 ($hunk[$ix]{TYPE
} eq 'mode' ?
' mode change' :
1311 $hunk[$ix]{TYPE
} eq 'deletion' ?
' deletion' :
1313 $patch_mode_flavour{TARGET
},
1314 " [y,n,q,a,d,/$other,?]? ";
1315 my $line = prompt_single_character
;
1317 if ($line =~ /^y/i) {
1318 $hunk[$ix]{USE
} = 1;
1320 elsif ($line =~ /^n/i) {
1321 $hunk[$ix]{USE
} = 0;
1323 elsif ($line =~ /^a/i) {
1324 while ($ix < $num) {
1325 if (!defined $hunk[$ix]{USE
}) {
1326 $hunk[$ix]{USE
} = 1;
1332 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1334 my $no = $ix > 10 ?
$ix - 10 : 0;
1335 while ($response eq '') {
1337 $no = display_hunks
(\
@hunk, $no);
1339 $extra = " (<ret> to see more)";
1341 print "go to which hunk$extra? ";
1342 $response = <STDIN
>;
1343 if (!defined $response) {
1348 if ($response !~ /^\s*\d+\s*$/) {
1349 error_msg
"Invalid number: '$response'\n";
1350 } elsif (0 < $response && $response <= $num) {
1351 $ix = $response - 1;
1353 error_msg
"Sorry, only $num hunks available.\n";
1357 elsif ($line =~ /^d/i) {
1358 while ($ix < $num) {
1359 if (!defined $hunk[$ix]{USE
}) {
1360 $hunk[$ix]{USE
} = 0;
1366 elsif ($line =~ /^q/i) {
1367 while ($ix < $num) {
1368 if (!defined $hunk[$ix]{USE
}) {
1369 $hunk[$ix]{USE
} = 0;
1376 elsif ($line =~ m
|^/(.*)|) {
1379 print colored
$prompt_color, "search for regex? ";
1381 if (defined $regex) {
1387 $search_string = qr{$regex}m;
1390 my ($err,$exp) = ($@
, $1);
1391 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1392 error_msg
"Malformed search regexp $exp: $err\n";
1397 my $text = join ("", @
{$hunk[$iy]{TEXT
}});
1398 last if ($text =~ $search_string);
1400 $iy = 0 if ($iy >= $num);
1402 error_msg
"No hunk matches the given pattern\n";
1409 elsif ($line =~ /^K/) {
1410 if ($other =~ /K/) {
1414 error_msg
"No previous hunk\n";
1418 elsif ($line =~ /^J/) {
1419 if ($other =~ /J/) {
1423 error_msg
"No next hunk\n";
1427 elsif ($line =~ /^k/) {
1428 if ($other =~ /k/) {
1432 !defined $hunk[$ix]{USE
});
1436 error_msg
"No previous hunk\n";
1440 elsif ($line =~ /^j/) {
1441 if ($other !~ /j/) {
1442 error_msg
"No next hunk\n";
1446 elsif ($other =~ /s/ && $line =~ /^s/) {
1447 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
1449 print colored
$header_color, "Split into ",
1450 scalar(@split), " hunks.\n";
1452 splice (@hunk, $ix, 1, @split);
1453 $num = scalar @hunk;
1456 elsif ($other =~ /e/ && $line =~ /^e/) {
1457 my $newhunk = edit_hunk_loop
($head, \
@hunk, $ix);
1458 if (defined $newhunk) {
1459 splice @hunk, $ix, 1, $newhunk;
1463 help_patch_cmd
($other);
1469 last if ($ix >= $num ||
1470 !defined $hunk[$ix]{USE
});
1475 @hunk = coalesce_overlapping_hunks
(@hunk);
1481 push @result, @
{$_->{TEXT
}};
1487 my @patch = reassemble_patch
($head->{TEXT
}, @result);
1488 my $apply_routine = $patch_mode_flavour{APPLY
};
1489 &$apply_routine(@patch);
1498 my @mods = list_modified
('index-only');
1499 @mods = grep { !($_->{BINARY
}) } @mods;
1501 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
1503 HEADER
=> $status_head, },
1506 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
1507 system(qw(git diff -p --cached), $reference, '--',
1508 map { $_->{VALUE
} } @them);
1517 print colored
$help_color, <<\EOF
;
1518 status
- show paths with changes
1519 update
- add working tree
state to the staged set of changes
1520 revert
- revert staged set of changes back to the HEAD version
1521 patch
- pick hunks
and update selectively
1522 diff
- view diff between HEAD
and index
1523 add untracked
- add contents of untracked files to the staged set of changes
1528 return unless @ARGV;
1529 my $arg = shift @ARGV;
1530 if ($arg =~ /--patch(?:=(.*))?/) {
1532 if ($1 eq 'reset') {
1533 $patch_mode = 'reset_head';
1534 $patch_mode_revision = 'HEAD';
1535 $arg = shift @ARGV or die "missing --";
1537 $patch_mode_revision = $arg;
1538 $patch_mode = ($arg eq 'HEAD' ?
1539 'reset_head' : 'reset_nothead');
1540 $arg = shift @ARGV or die "missing --";
1542 } elsif ($1 eq 'checkout') {
1543 $arg = shift @ARGV or die "missing --";
1545 $patch_mode = 'checkout_index';
1547 $patch_mode_revision = $arg;
1548 $patch_mode = ($arg eq 'HEAD' ?
1549 'checkout_head' : 'checkout_nothead');
1550 $arg = shift @ARGV or die "missing --";
1552 } elsif ($1 eq 'stage' or $1 eq 'stash') {
1554 $arg = shift @ARGV or die "missing --";
1556 die "unknown --patch mode: $1";
1559 $patch_mode = 'stage';
1560 $arg = shift @ARGV or die "missing --";
1562 die "invalid argument $arg, expecting --"
1563 unless $arg eq "--";
1564 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
1566 elsif ($arg ne "--") {
1567 die "invalid argument $arg, expecting --";
1572 my @cmd = ([ 'status', \
&status_cmd
, ],
1573 [ 'update', \
&update_cmd
, ],
1574 [ 'revert', \
&revert_cmd
, ],
1575 [ 'add untracked', \
&add_untracked_cmd
, ],
1576 [ 'patch', \
&patch_update_cmd
, ],
1577 [ 'diff', \
&diff_cmd
, ],
1578 [ 'quit', \
&quit_cmd
, ],
1579 [ 'help', \
&help_cmd
, ],
1582 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1585 HEADER
=> '*** Commands ***',
1586 ON_EOF
=> \
&quit_cmd
,
1587 IMMEDIATE
=> 1 }, @cmd);