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',
95 DIFF
=> 'diff-index -p HEAD',
96 APPLY
=> sub { apply_patch
'apply --cached', @_; },
97 APPLY_CHECK
=> 'apply --cached',
100 PARTICIPLE
=> 'stashing',
105 DIFF
=> 'diff-index -p --cached',
106 APPLY
=> sub { apply_patch
'apply -R --cached', @_; },
107 APPLY_CHECK
=> 'apply -R --cached',
110 PARTICIPLE
=> 'unstaging',
111 FILTER
=> 'index-only',
115 DIFF
=> 'diff-index -R -p --cached',
116 APPLY
=> sub { apply_patch
'apply --cached', @_; },
117 APPLY_CHECK
=> 'apply --cached',
119 TARGET
=> ' to index',
120 PARTICIPLE
=> 'applying',
121 FILTER
=> 'index-only',
124 'checkout_index' => {
125 DIFF
=> 'diff-files -p',
126 APPLY
=> sub { apply_patch
'apply -R', @_; },
127 APPLY_CHECK
=> 'apply -R',
129 TARGET
=> ' from worktree',
130 PARTICIPLE
=> 'discarding',
131 FILTER
=> 'file-only',
135 DIFF
=> 'diff-index -p',
136 APPLY
=> sub { apply_patch_for_checkout_commit
'-R', @_ },
137 APPLY_CHECK
=> 'apply -R',
139 TARGET
=> ' from index and worktree',
140 PARTICIPLE
=> 'discarding',
144 'checkout_nothead' => {
145 DIFF
=> 'diff-index -R -p',
146 APPLY
=> sub { apply_patch_for_checkout_commit
'', @_ },
147 APPLY_CHECK
=> 'apply',
149 TARGET
=> ' to index and worktree',
150 PARTICIPLE
=> 'applying',
156 my %patch_mode_flavour = %{$patch_modes{stage
}};
159 if ($^O
eq 'MSWin32' || $^O
eq 'msys') {
160 my @invalid = grep {m/[":*]/} @_;
161 die "$^O does not support: @invalid\n" if @invalid;
162 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
166 open($fh, '-|', @_) or die;
171 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
173 if (!defined $GIT_DIR) {
174 exit(1); # rev-parse would have already said "not a git repo"
191 my ($retval, $remainder);
192 if (!/^\042(.*)\042$/) {
195 ($_, $retval) = ($1, "");
196 while (/^([^\\]*)\\(.*)$/) {
200 if (/^([0-3][0-7][0-7])(.*)$/) {
201 $retval .= chr(oct($1));
205 if (/^([\\\042btnvfr])(.*)$/) {
206 $retval .= $cquote_map{$1};
210 # This is malformed -- just return it as-is for now.
221 open $fh, 'git update-index --refresh |'
224 ;# ignore 'needs update'
234 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
237 my $status_fmt = '%12s %12s %s';
238 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
242 sub is_initial_commit
{
243 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
244 unless defined $initial;
250 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
253 # Returns list of hashes, contents of each of which are:
255 # BINARY: is a binary path
256 # INDEX: is index different from HEAD?
257 # FILE: is file different from index?
258 # INDEX_ADDDEL: is it add/delete between HEAD and index?
259 # FILE_ADDDEL: is it add/delete between index and file?
264 my ($add, $del, $adddel, $file);
271 } run_cmd_pipe
(qw(git ls-files --), @ARGV);
272 return if (!@tracked);
276 if (defined $patch_mode_revision and $patch_mode_revision ne 'HEAD') {
277 $reference = $patch_mode_revision;
278 } elsif (is_initial_commit
()) {
279 $reference = get_empty_tree
();
283 for (run_cmd_pipe
(qw(git diff-index --cached
284 --numstat --summary), $reference,
286 if (($add, $del, $file) =
287 /^([-\d]+) ([-\d]+) (.*)/) {
289 $file = unquote_path
($file);
290 if ($add eq '-' && $del eq '-') {
295 $change = "+$add/-$del";
303 elsif (($adddel, $file) =
304 /^ (create|delete) mode [0-7]+ (.*)$/) {
305 $file = unquote_path
($file);
306 $data{$file}{INDEX_ADDDEL
} = $adddel;
310 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
311 if (($add, $del, $file) =
312 /^([-\d]+) ([-\d]+) (.*)/) {
313 $file = unquote_path
($file);
314 if (!exists $data{$file}) {
316 INDEX
=> 'unchanged',
321 if ($add eq '-' && $del eq '-') {
326 $change = "+$add/-$del";
328 $data{$file}{FILE
} = $change;
330 $data{$file}{BINARY
} = 1;
333 elsif (($adddel, $file) =
334 /^ (create|delete) mode [0-7]+ (.*)$/) {
335 $file = unquote_path
($file);
336 $data{$file}{FILE_ADDDEL
} = $adddel;
340 for (sort keys %data) {
344 if ($only eq 'index-only') {
345 next if ($it->{INDEX
} eq 'unchanged');
347 if ($only eq 'file-only') {
348 next if ($it->{FILE
} eq 'nothing');
360 my ($string, @stuff) = @_;
362 for (my $i = 0; $i < @stuff; $i++) {
366 if ((ref $it) eq 'ARRAY') {
374 if ($it =~ /^$string/) {
378 if (defined $hit && defined $found) {
388 # inserts string into trie and updates count for each character
390 my ($trie, $string) = @_;
391 foreach (split //, $string) {
392 $trie = $trie->{$_} ||= {COUNT
=> 0};
397 # returns an array of tuples (prefix, remainder)
398 sub find_unique_prefixes
{
402 # any single prefix exceeding the soft limit is omitted
403 # if any prefix exceeds the hard limit all are omitted
404 # 0 indicates no limit
408 # build a trie modelling all possible options
410 foreach my $print (@stuff) {
411 if ((ref $print) eq 'ARRAY') {
412 $print = $print->[0];
414 elsif ((ref $print) eq 'HASH') {
415 $print = $print->{VALUE
};
417 update_trie
(\
%trie, $print);
418 push @return, $print;
421 # use the trie to find the unique prefixes
422 for (my $i = 0; $i < @return; $i++) {
423 my $ret = $return[$i];
424 my @letters = split //, $ret;
426 my ($prefix, $remainder);
428 for ($j = 0; $j < @letters; $j++) {
429 my $letter = $letters[$j];
430 if ($search{$letter}{COUNT
} == 1) {
431 $prefix = substr $ret, 0, $j + 1;
432 $remainder = substr $ret, $j + 1;
436 my $prefix = substr $ret, 0, $j;
438 if ($hard_limit && $j + 1 > $hard_limit);
440 %search = %{$search{$letter}};
442 if (ord($letters[0]) > 127 ||
443 ($soft_limit && $j + 1 > $soft_limit)) {
447 $return[$i] = [$prefix, $remainder];
452 # filters out prefixes which have special meaning to list_and_choose()
453 sub is_valid_prefix
{
455 return (defined $prefix) &&
456 !($prefix =~ /[\s,]/) && # separators
457 !($prefix =~ /^-/) && # deselection
458 !($prefix =~ /^\d+/) && # selection
459 ($prefix ne '*') && # "all" wildcard
460 ($prefix ne '?'); # prompt help
463 # given a prefix/remainder tuple return a string with the prefix highlighted
464 # for now use square brackets; later might use ANSI colors (underline, bold)
465 sub highlight_prefix
{
467 my $remainder = shift;
469 if (!defined $prefix) {
473 if (!is_valid_prefix
($prefix)) {
474 return "$prefix$remainder";
477 if (!$menu_use_color) {
478 return "[$prefix]$remainder";
481 return "$prompt_color$prefix$normal_color$remainder";
485 print STDERR colored
$error_color, @_;
488 sub list_and_choose
{
489 my ($opts, @stuff) = @_;
490 my (@chosen, @return);
492 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
498 if ($opts->{HEADER
}) {
499 if (!$opts->{LIST_FLAT
}) {
502 print colored
$header_color, "$opts->{HEADER}\n";
504 for ($i = 0; $i < @stuff; $i++) {
505 my $chosen = $chosen[$i] ?
'*' : ' ';
506 my $print = $stuff[$i];
507 my $ref = ref $print;
508 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
510 if ($ref eq 'ARRAY') {
511 $print = $highlighted || $print->[0];
513 elsif ($ref eq 'HASH') {
514 my $value = $highlighted || $print->{VALUE
};
515 $print = sprintf($status_fmt,
521 $print = $highlighted || $print;
523 printf("%s%2d: %s", $chosen, $i+1, $print);
524 if (($opts->{LIST_FLAT
}) &&
525 (($i + 1) % ($opts->{LIST_FLAT
}))) {
538 return if ($opts->{LIST_ONLY
});
540 print colored
$prompt_color, $opts->{PROMPT
};
541 if ($opts->{SINGLETON
}) {
550 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
557 singleton_prompt_help_cmd
() :
561 for my $choice (split(/[\s,]+/, $line)) {
565 # Input that begins with '-'; unchoose
566 if ($choice =~ s/^-//) {
569 # A range can be specified like 5-7 or 5-.
570 if ($choice =~ /^(\d+)-(\d*)$/) {
571 ($bottom, $top) = ($1, length($2) ?
$2 : 1 + @stuff);
573 elsif ($choice =~ /^\d+$/) {
574 $bottom = $top = $choice;
576 elsif ($choice eq '*') {
581 $bottom = $top = find_unique
($choice, @stuff);
582 if (!defined $bottom) {
583 error_msg
"Huh ($choice)?\n";
587 if ($opts->{SINGLETON
} && $bottom != $top) {
588 error_msg
"Huh ($choice)?\n";
591 for ($i = $bottom-1; $i <= $top-1; $i++) {
592 next if (@stuff <= $i || $i < 0);
593 $chosen[$i] = $choose;
596 last if ($opts->{IMMEDIATE
} || $line eq '*');
598 for ($i = 0; $i < @stuff; $i++) {
600 push @return, $stuff[$i];
606 sub singleton_prompt_help_cmd
{
607 print colored
$help_color, <<\EOF
;
609 1 - select a numbered item
610 foo
- select item based on unique prefix
611 - (empty
) select nothing
615 sub prompt_help_cmd
{
616 print colored
$help_color, <<\EOF
;
618 1 - select a single item
619 3-5 - select a range of items
620 2-3,6-9 - select multiple ranges
621 foo
- select item based on unique prefix
622 -... - unselect specified items
624 - (empty
) finish selecting
629 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
639 print "$cnt paths\n";
647 my @mods = list_modified
('file-only');
650 my @update = list_and_choose
({ PROMPT
=> 'Update',
651 HEADER
=> $status_head, },
654 system(qw(git update-index --add --remove --),
655 map { $_->{VALUE
} } @update);
656 say_n_paths
('updated', @update);
662 my @update = list_and_choose
({ PROMPT
=> 'Revert',
663 HEADER
=> $status_head, },
666 if (is_initial_commit
()) {
667 system(qw(git rm --cached),
668 map { $_->{VALUE
} } @update);
671 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
672 map { $_->{VALUE
} } @update);
674 open $fh, '| git update-index --index-info'
681 if ($_->{INDEX_ADDDEL
} &&
682 $_->{INDEX_ADDDEL
} eq 'create') {
683 system(qw(git update-index --force-remove --),
685 print "note: $_->{VALUE} is untracked now.\n";
690 say_n_paths
('reverted', @update);
695 sub add_untracked_cmd
{
696 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
699 system(qw(git update-index --add --), @add);
700 say_n_paths
('added', @add);
708 open $fh, '| git ' . $cmd;
715 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF
});
716 if (defined $patch_mode_revision) {
717 push @diff_cmd, $patch_mode_revision;
719 my @diff = run_cmd_pipe
("git", @diff_cmd, "--", $path);
721 if ($diff_use_color) {
722 @colored = run_cmd_pipe
("git", @diff_cmd, qw(--color --), $path);
724 my (@hunk) = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
726 for (my $i = 0; $i < @diff; $i++) {
727 if ($diff[$i] =~ /^@@ /) {
728 push @hunk, { TEXT
=> [], DISPLAY
=> [],
731 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
732 push @
{$hunk[-1]{DISPLAY
}},
733 ($diff_use_color ?
$colored[$i] : $diff[$i]);
738 sub parse_diff_header
{
741 my $head = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
742 my $mode = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'mode' };
743 my $deletion = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'deletion' };
745 for (my $i = 0; $i < @
{$src->{TEXT
}}; $i++) {
747 $src->{TEXT
}->[$i] =~ /^(old|new) mode (\d+)$/ ?
$mode :
748 $src->{TEXT
}->[$i] =~ /^deleted file/ ?
$deletion :
750 push @
{$dest->{TEXT
}}, $src->{TEXT
}->[$i];
751 push @
{$dest->{DISPLAY
}}, $src->{DISPLAY
}->[$i];
753 return ($head, $mode, $deletion);
756 sub hunk_splittable
{
759 my @s = split_hunk
($text);
763 sub parse_hunk_header
{
765 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
766 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
767 $o_cnt = 1 unless defined $o_cnt;
768 $n_cnt = 1 unless defined $n_cnt;
769 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
773 my ($text, $display) = @_;
775 if (!defined $display) {
778 # If there are context lines in the middle of a hunk,
779 # it can be split, but we would need to take care of
782 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
787 my $next_hunk_start = undef;
788 my $i = $hunk_start - 1;
802 while (++$i < @
$text) {
803 my $line = $text->[$i];
804 my $display = $display->[$i];
806 if ($this->{ADDDEL
} &&
807 !defined $next_hunk_start) {
808 # We have seen leading context and
809 # adds/dels and then here is another
810 # context, which is trailing for this
811 # split hunk and leading for the next
813 $next_hunk_start = $i;
815 push @
{$this->{TEXT
}}, $line;
816 push @
{$this->{DISPLAY
}}, $display;
819 if (defined $next_hunk_start) {
826 if (defined $next_hunk_start) {
827 # We are done with the current hunk and
828 # this is the first real change for the
830 $hunk_start = $next_hunk_start;
831 $o_ofs = $this->{OLD
} + $this->{OCNT
};
832 $n_ofs = $this->{NEW
} + $this->{NCNT
};
833 $o_ofs -= $this->{POSTCTX
};
834 $n_ofs -= $this->{POSTCTX
};
838 push @
{$this->{TEXT
}}, $line;
839 push @
{$this->{DISPLAY
}}, $display;
853 for my $hunk (@split) {
854 $o_ofs = $hunk->{OLD
};
855 $n_ofs = $hunk->{NEW
};
856 my $o_cnt = $hunk->{OCNT
};
857 my $n_cnt = $hunk->{NCNT
};
859 my $head = ("@@ -$o_ofs" .
860 (($o_cnt != 1) ?
",$o_cnt" : '') .
862 (($n_cnt != 1) ?
",$n_cnt" : '') .
864 my $display_head = $head;
865 unshift @
{$hunk->{TEXT
}}, $head;
866 if ($diff_use_color) {
867 $display_head = colored
($fraginfo_color, $head);
869 unshift @
{$hunk->{DISPLAY
}}, $display_head;
874 sub find_last_o_ctx
{
876 my $text = $it->{TEXT
};
877 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
879 my $last_o_ctx = $o_ofs + $o_cnt;
881 my $line = $text->[$i];
892 my ($prev, $this) = @_;
893 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
894 parse_hunk_header
($prev->{TEXT
}[0]);
895 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
896 parse_hunk_header
($this->{TEXT
}[0]);
898 my (@line, $i, $ofs, $o_cnt, $n_cnt);
901 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
902 my $line = $prev->{TEXT
}[$i];
903 if ($line =~ /^\+/) {
909 last if ($o1_ofs <= $ofs);
919 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
920 my $line = $this->{TEXT
}[$i];
921 if ($line =~ /^\+/) {
933 my $head = ("@@ -$o0_ofs" .
934 (($o_cnt != 1) ?
",$o_cnt" : '') .
936 (($n_cnt != 1) ?
",$n_cnt" : '') .
938 @
{$prev->{TEXT
}} = ($head, @line);
941 sub coalesce_overlapping_hunks
{
945 my ($last_o_ctx, $last_was_dirty);
947 for (grep { $_->{USE
} } @in) {
948 if ($_->{TYPE
} ne 'hunk') {
952 my $text = $_->{TEXT
};
953 my ($o_ofs) = parse_hunk_header
($text->[0]);
954 if (defined $last_o_ctx &&
955 $o_ofs <= $last_o_ctx &&
958 merge_hunk
($out[-1], $_);
963 $last_o_ctx = find_last_o_ctx
($out[-1]);
964 $last_was_dirty = $_->{DIRTY
};
969 sub reassemble_patch
{
973 # Include everything in the header except the beginning of the diff.
974 push @patch, (grep { !/^[-+]{3}/ } @
$head);
976 # Then include any headers from the hunk lines, which must
977 # come before any actual hunk.
978 while (@_ && $_[0] !~ /^@/) {
982 # Then begin the diff.
983 push @patch, grep { /^[-+]{3}/ } @
$head;
985 # And then the actual hunks.
993 colored
((/^@/ ?
$fraginfo_color :
994 /^\+/ ?
$diff_new_color :
995 /^-/ ?
$diff_old_color :
1001 sub edit_hunk_manually
{
1004 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
1006 open $fh, '>', $hunkfile
1007 or die "failed to open hunk edit file for writing: " . $!;
1008 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
1009 print $fh @
$oldtext;
1010 my $participle = $patch_mode_flavour{PARTICIPLE
};
1011 my $is_reverse = $patch_mode_flavour{IS_REVERSE
};
1012 my ($remove_plus, $remove_minus) = $is_reverse ?
('-', '+') : ('+', '-');
1015 # To remove '$remove_minus' lines, make them ' ' lines (context).
1016 # To remove '$remove_plus' lines, delete them.
1017 # Lines starting with # will be removed.
1019 # If the patch applies cleanly, the edited hunk will immediately be
1020 # marked for $participle. If it does not apply cleanly, you will be given
1021 # an opportunity to edit again. If all lines of the hunk are removed,
1022 # then the edit is aborted and the hunk is left unchanged.
1026 chomp(my $editor = run_cmd_pipe
(qw(git var GIT_EDITOR)));
1027 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
1033 open $fh, '<', $hunkfile
1034 or die "failed to open hunk edit file for reading: " . $!;
1035 my @newtext = grep { !/^#/ } <$fh>;
1039 # Abort if nothing remains
1040 if (!grep { /\S/ } @newtext) {
1044 # Reinsert the first hunk header if the user accidentally deleted it
1045 if ($newtext[0] !~ /^@/) {
1046 unshift @newtext, $oldtext->[0];
1053 return run_git_apply
($patch_mode_flavour{APPLY_CHECK
} . ' --recount --check',
1054 map { @
{$_->{TEXT
}} } @_);
1057 sub _restore_terminal_and_die
{
1063 sub prompt_single_character
{
1065 local $SIG{TERM
} = \
&_restore_terminal_and_die
;
1066 local $SIG{INT
} = \
&_restore_terminal_and_die
;
1068 my $key = ReadKey
0;
1070 print "$key" if defined $key;
1081 print colored
$prompt_color, $prompt;
1082 my $line = prompt_single_character
;
1083 return 0 if $line =~ /^n/i;
1084 return 1 if $line =~ /^y/i;
1088 sub edit_hunk_loop
{
1089 my ($head, $hunk, $ix) = @_;
1090 my $text = $hunk->[$ix]->{TEXT
};
1093 $text = edit_hunk_manually
($text);
1094 if (!defined $text) {
1099 TYPE
=> $hunk->[$ix]->{TYPE
},
1103 if (diff_applies
($head,
1106 @
{$hunk}[$ix+1..$#{$hunk}])) {
1107 $newhunk->{DISPLAY
} = [color_diff
(@
{$text})];
1112 'Your edited hunk does not apply. Edit again '
1113 . '(saying "no" discards!) [y/n]? '
1119 sub help_patch_cmd
{
1120 my $verb = lc $patch_mode_flavour{VERB
};
1121 my $target = $patch_mode_flavour{TARGET
};
1122 print colored
$help_color, <<EOF ;
1123 y - $verb this hunk$target
1124 n - do not $verb this hunk$target
1125 q - quit; do not $verb this hunk nor any of the remaining ones
1126 a - $verb this hunk and all later hunks in the file
1127 d - do not $verb this hunk nor any of the later hunks in the file
1128 g - select a hunk to go to
1129 / - search for a hunk matching the given regex
1130 j - leave this hunk undecided, see next undecided hunk
1131 J - leave this hunk undecided, see next hunk
1132 k - leave this hunk undecided, see previous undecided hunk
1133 K - leave this hunk undecided, see previous hunk
1134 s - split the current hunk into smaller hunks
1135 e - manually edit the current hunk
1142 my $ret = run_git_apply
$cmd . ' --recount', @_;
1149 sub apply_patch_for_checkout_commit
{
1150 my $reverse = shift;
1151 my $applies_index = run_git_apply
'apply '.$reverse.' --cached --recount --check', @_;
1152 my $applies_worktree = run_git_apply
'apply '.$reverse.' --recount --check', @_;
1154 if ($applies_worktree && $applies_index) {
1155 run_git_apply
'apply '.$reverse.' --cached --recount', @_;
1156 run_git_apply
'apply '.$reverse.' --recount', @_;
1158 } elsif (!$applies_index) {
1159 print colored
$error_color, "The selected hunks do not apply to the index!\n";
1160 if (prompt_yesno
"Apply them to the worktree anyway? ") {
1161 return run_git_apply
'apply '.$reverse.' --recount', @_;
1163 print colored
$error_color, "Nothing was applied.\n";
1172 sub patch_update_cmd
{
1173 my @all_mods = list_modified
($patch_mode_flavour{FILTER
});
1174 my @mods = grep { !($_->{BINARY
}) } @all_mods;
1179 print STDERR
"Only binary files changed.\n";
1181 print STDERR
"No changes.\n";
1189 @them = list_and_choose
({ PROMPT
=> 'Patch update',
1190 HEADER
=> $status_head, },
1194 return 0 if patch_update_file
($_->{VALUE
});
1198 # Generate a one line summary of a hunk.
1199 sub summarize_hunk
{
1201 my $summary = $rhunk->{TEXT
}[0];
1203 # Keep the line numbers, discard extra context.
1204 $summary =~ s/@@(.*?)@@.*/$1 /s;
1205 $summary .= " " x
(20 - length $summary);
1207 # Add some user context.
1208 for my $line (@
{$rhunk->{TEXT
}}) {
1209 if ($line =~ m/^[+-].*\w/) {
1216 return substr($summary, 0, 80) . "\n";
1220 # Print a one-line summary of each hunk in the array ref in
1221 # the first argument, starting wih the index in the 2nd.
1223 my ($hunks, $i) = @_;
1226 for (; $i < @
$hunks && $ctr < 20; $i++, $ctr++) {
1228 if (defined $hunks->[$i]{USE
}) {
1229 $status = $hunks->[$i]{USE
} ?
"+" : "-";
1234 summarize_hunk
($hunks->[$i]);
1239 sub patch_update_file
{
1243 my ($head, @hunk) = parse_diff
($path);
1244 ($head, my $mode, my $deletion) = parse_diff_header
($head);
1245 for (@
{$head->{DISPLAY
}}) {
1249 if (@
{$mode->{TEXT
}}) {
1250 unshift @hunk, $mode;
1252 if (@
{$deletion->{TEXT
}}) {
1253 foreach my $hunk (@hunk) {
1254 push @
{$deletion->{TEXT
}}, @
{$hunk->{TEXT
}};
1255 push @
{$deletion->{DISPLAY
}}, @
{$hunk->{DISPLAY
}};
1257 @hunk = ($deletion);
1260 $num = scalar @hunk;
1264 my ($prev, $next, $other, $undecided, $i);
1270 for ($i = 0; $i < $ix; $i++) {
1271 if (!defined $hunk[$i]{USE
}) {
1280 for ($i = $ix + 1; $i < $num; $i++) {
1281 if (!defined $hunk[$i]{USE
}) {
1287 if ($ix < $num - 1) {
1293 for ($i = 0; $i < $num; $i++) {
1294 if (!defined $hunk[$i]{USE
}) {
1299 last if (!$undecided);
1301 if ($hunk[$ix]{TYPE
} eq 'hunk' &&
1302 hunk_splittable
($hunk[$ix]{TEXT
})) {
1305 if ($hunk[$ix]{TYPE
} eq 'hunk') {
1308 for (@
{$hunk[$ix]{DISPLAY
}}) {
1311 print colored
$prompt_color, $patch_mode_flavour{VERB
},
1312 ($hunk[$ix]{TYPE
} eq 'mode' ?
' mode change' :
1313 $hunk[$ix]{TYPE
} eq 'deletion' ?
' deletion' :
1315 $patch_mode_flavour{TARGET
},
1316 " [y,n,q,a,d,/$other,?]? ";
1317 my $line = prompt_single_character
;
1319 if ($line =~ /^y/i) {
1320 $hunk[$ix]{USE
} = 1;
1322 elsif ($line =~ /^n/i) {
1323 $hunk[$ix]{USE
} = 0;
1325 elsif ($line =~ /^a/i) {
1326 while ($ix < $num) {
1327 if (!defined $hunk[$ix]{USE
}) {
1328 $hunk[$ix]{USE
} = 1;
1334 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1336 my $no = $ix > 10 ?
$ix - 10 : 0;
1337 while ($response eq '') {
1339 $no = display_hunks
(\
@hunk, $no);
1341 $extra = " (<ret> to see more)";
1343 print "go to which hunk$extra? ";
1344 $response = <STDIN
>;
1345 if (!defined $response) {
1350 if ($response !~ /^\s*\d+\s*$/) {
1351 error_msg
"Invalid number: '$response'\n";
1352 } elsif (0 < $response && $response <= $num) {
1353 $ix = $response - 1;
1355 error_msg
"Sorry, only $num hunks available.\n";
1359 elsif ($line =~ /^d/i) {
1360 while ($ix < $num) {
1361 if (!defined $hunk[$ix]{USE
}) {
1362 $hunk[$ix]{USE
} = 0;
1368 elsif ($line =~ /^q/i) {
1369 while ($ix < $num) {
1370 if (!defined $hunk[$ix]{USE
}) {
1371 $hunk[$ix]{USE
} = 0;
1378 elsif ($line =~ m
|^/(.*)|) {
1381 print colored
$prompt_color, "search for regex? ";
1383 if (defined $regex) {
1389 $search_string = qr{$regex}m;
1392 my ($err,$exp) = ($@
, $1);
1393 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1394 error_msg
"Malformed search regexp $exp: $err\n";
1399 my $text = join ("", @
{$hunk[$iy]{TEXT
}});
1400 last if ($text =~ $search_string);
1402 $iy = 0 if ($iy >= $num);
1404 error_msg
"No hunk matches the given pattern\n";
1411 elsif ($line =~ /^K/) {
1412 if ($other =~ /K/) {
1416 error_msg
"No previous hunk\n";
1420 elsif ($line =~ /^J/) {
1421 if ($other =~ /J/) {
1425 error_msg
"No next hunk\n";
1429 elsif ($line =~ /^k/) {
1430 if ($other =~ /k/) {
1434 !defined $hunk[$ix]{USE
});
1438 error_msg
"No previous hunk\n";
1442 elsif ($line =~ /^j/) {
1443 if ($other !~ /j/) {
1444 error_msg
"No next hunk\n";
1448 elsif ($other =~ /s/ && $line =~ /^s/) {
1449 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
1451 print colored
$header_color, "Split into ",
1452 scalar(@split), " hunks.\n";
1454 splice (@hunk, $ix, 1, @split);
1455 $num = scalar @hunk;
1458 elsif ($other =~ /e/ && $line =~ /^e/) {
1459 my $newhunk = edit_hunk_loop
($head, \
@hunk, $ix);
1460 if (defined $newhunk) {
1461 splice @hunk, $ix, 1, $newhunk;
1465 help_patch_cmd
($other);
1471 last if ($ix >= $num ||
1472 !defined $hunk[$ix]{USE
});
1477 @hunk = coalesce_overlapping_hunks
(@hunk);
1483 push @result, @
{$_->{TEXT
}};
1489 my @patch = reassemble_patch
($head->{TEXT
}, @result);
1490 my $apply_routine = $patch_mode_flavour{APPLY
};
1491 &$apply_routine(@patch);
1500 my @mods = list_modified
('index-only');
1501 @mods = grep { !($_->{BINARY
}) } @mods;
1503 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
1505 HEADER
=> $status_head, },
1508 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
1509 system(qw(git diff -p --cached), $reference, '--',
1510 map { $_->{VALUE
} } @them);
1519 print colored
$help_color, <<\EOF
;
1520 status
- show paths with changes
1521 update
- add working tree
state to the staged set of changes
1522 revert
- revert staged set of changes back to the HEAD version
1523 patch
- pick hunks
and update selectively
1524 diff
- view diff between HEAD
and index
1525 add untracked
- add contents of untracked files to the staged set of changes
1530 return unless @ARGV;
1531 my $arg = shift @ARGV;
1532 if ($arg =~ /--patch(?:=(.*))?/) {
1534 if ($1 eq 'reset') {
1535 $patch_mode = 'reset_head';
1536 $patch_mode_revision = 'HEAD';
1537 $arg = shift @ARGV or die "missing --";
1539 $patch_mode_revision = $arg;
1540 $patch_mode = ($arg eq 'HEAD' ?
1541 'reset_head' : 'reset_nothead');
1542 $arg = shift @ARGV or die "missing --";
1544 } elsif ($1 eq 'checkout') {
1545 $arg = shift @ARGV or die "missing --";
1547 $patch_mode = 'checkout_index';
1549 $patch_mode_revision = $arg;
1550 $patch_mode = ($arg eq 'HEAD' ?
1551 'checkout_head' : 'checkout_nothead');
1552 $arg = shift @ARGV or die "missing --";
1554 } elsif ($1 eq 'stage' or $1 eq 'stash') {
1556 $arg = shift @ARGV or die "missing --";
1558 die "unknown --patch mode: $1";
1561 $patch_mode = 'stage';
1562 $arg = shift @ARGV or die "missing --";
1564 die "invalid argument $arg, expecting --"
1565 unless $arg eq "--";
1566 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
1568 elsif ($arg ne "--") {
1569 die "invalid argument $arg, expecting --";
1574 my @cmd = ([ 'status', \
&status_cmd
, ],
1575 [ 'update', \
&update_cmd
, ],
1576 [ 'revert', \
&revert_cmd
, ],
1577 [ 'add untracked', \
&add_untracked_cmd
, ],
1578 [ 'patch', \
&patch_update_cmd
, ],
1579 [ 'diff', \
&diff_cmd
, ],
1580 [ 'quit', \
&quit_cmd
, ],
1581 [ 'help', \
&help_cmd
, ],
1584 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1587 HEADER
=> '*** Commands ***',
1588 ON_EOF
=> \
&quit_cmd
,
1589 IMMEDIATE
=> 1 }, @cmd);