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 %4s%s %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,
523 $print = $highlighted || $print;
525 printf("%s%s", $chosen, $print);
526 if (($opts->{LIST_FLAT
}) &&
527 (($i + 1) % ($opts->{LIST_FLAT
}))) {
540 return if ($opts->{LIST_ONLY
});
542 print colored
$prompt_color, $opts->{PROMPT
};
543 if ($opts->{SINGLETON
}) {
552 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
559 singleton_prompt_help_cmd
() :
563 for my $choice (split(/[\s,]+/, $line)) {
567 # Input that begins with '-'; unchoose
568 if ($choice =~ s/^-//) {
571 # A range can be specified like 5-7 or 5-.
572 if ($choice =~ /^(\d+)-(\d*)$/) {
573 ($bottom, $top) = ($1, length($2) ?
$2 : 1 + @stuff);
575 elsif ($choice =~ /^\d+$/) {
576 $bottom = $top = $choice;
578 elsif ($choice eq '*') {
583 $bottom = $top = find_unique
($choice, @stuff);
584 if (!defined $bottom) {
585 error_msg
"Huh ($choice)?\n";
589 if ($opts->{SINGLETON
} && $bottom != $top) {
590 error_msg
"Huh ($choice)?\n";
593 for ($i = $bottom-1; $i <= $top-1; $i++) {
594 next if (@stuff <= $i || $i < 0);
595 $chosen[$i] = $choose;
598 last if ($opts->{IMMEDIATE
} || $line eq '*');
600 for ($i = 0; $i < @stuff; $i++) {
602 push @return, $stuff[$i];
608 sub singleton_prompt_help_cmd
{
609 print colored
$help_color, <<\EOF
;
611 1 - select a numbered item
612 foo
- select item based on unique prefix
613 - (empty
) select nothing
617 sub prompt_help_cmd
{
618 print colored
$help_color, <<\EOF
;
620 1 - select a single item
621 3-5 - select a range of items
622 2-3,6-9 - select multiple ranges
623 foo
- select item based on unique prefix
624 -... - unselect specified items
626 - (empty
) finish selecting
631 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
641 print "$cnt paths\n";
649 my @mods = list_modified
('file-only');
652 my @update = list_and_choose
({ PROMPT
=> 'Update',
653 HEADER
=> $status_head, },
656 system(qw(git update-index --add --remove --),
657 map { $_->{VALUE
} } @update);
658 say_n_paths
('updated', @update);
664 my @update = list_and_choose
({ PROMPT
=> 'Revert',
665 HEADER
=> $status_head, },
668 if (is_initial_commit
()) {
669 system(qw(git rm --cached),
670 map { $_->{VALUE
} } @update);
673 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
674 map { $_->{VALUE
} } @update);
676 open $fh, '| git update-index --index-info'
683 if ($_->{INDEX_ADDDEL
} &&
684 $_->{INDEX_ADDDEL
} eq 'create') {
685 system(qw(git update-index --force-remove --),
687 print "note: $_->{VALUE} is untracked now.\n";
692 say_n_paths
('reverted', @update);
697 sub add_untracked_cmd
{
698 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
701 system(qw(git update-index --add --), @add);
702 say_n_paths
('added', @add);
710 open $fh, '| git ' . $cmd;
717 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF
});
718 if (defined $patch_mode_revision) {
719 push @diff_cmd, $patch_mode_revision;
721 my @diff = run_cmd_pipe
("git", @diff_cmd, "--", $path);
723 if ($diff_use_color) {
724 @colored = run_cmd_pipe
("git", @diff_cmd, qw(--color --), $path);
726 my (@hunk) = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
728 for (my $i = 0; $i < @diff; $i++) {
729 if ($diff[$i] =~ /^@@ /) {
730 push @hunk, { TEXT
=> [], DISPLAY
=> [],
733 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
734 push @
{$hunk[-1]{DISPLAY
}},
735 ($diff_use_color ?
$colored[$i] : $diff[$i]);
740 sub parse_diff_header
{
743 my $head = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
744 my $mode = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'mode' };
745 my $deletion = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'deletion' };
747 for (my $i = 0; $i < @
{$src->{TEXT
}}; $i++) {
749 $src->{TEXT
}->[$i] =~ /^(old|new) mode (\d+)$/ ?
$mode :
750 $src->{TEXT
}->[$i] =~ /^deleted file/ ?
$deletion :
752 push @
{$dest->{TEXT
}}, $src->{TEXT
}->[$i];
753 push @
{$dest->{DISPLAY
}}, $src->{DISPLAY
}->[$i];
755 return ($head, $mode, $deletion);
758 sub hunk_splittable
{
761 my @s = split_hunk
($text);
765 sub parse_hunk_header
{
767 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
768 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
769 $o_cnt = 1 unless defined $o_cnt;
770 $n_cnt = 1 unless defined $n_cnt;
771 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
775 my ($text, $display) = @_;
777 if (!defined $display) {
780 # If there are context lines in the middle of a hunk,
781 # it can be split, but we would need to take care of
784 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
789 my $next_hunk_start = undef;
790 my $i = $hunk_start - 1;
804 while (++$i < @
$text) {
805 my $line = $text->[$i];
806 my $display = $display->[$i];
808 if ($this->{ADDDEL
} &&
809 !defined $next_hunk_start) {
810 # We have seen leading context and
811 # adds/dels and then here is another
812 # context, which is trailing for this
813 # split hunk and leading for the next
815 $next_hunk_start = $i;
817 push @
{$this->{TEXT
}}, $line;
818 push @
{$this->{DISPLAY
}}, $display;
821 if (defined $next_hunk_start) {
828 if (defined $next_hunk_start) {
829 # We are done with the current hunk and
830 # this is the first real change for the
832 $hunk_start = $next_hunk_start;
833 $o_ofs = $this->{OLD
} + $this->{OCNT
};
834 $n_ofs = $this->{NEW
} + $this->{NCNT
};
835 $o_ofs -= $this->{POSTCTX
};
836 $n_ofs -= $this->{POSTCTX
};
840 push @
{$this->{TEXT
}}, $line;
841 push @
{$this->{DISPLAY
}}, $display;
855 for my $hunk (@split) {
856 $o_ofs = $hunk->{OLD
};
857 $n_ofs = $hunk->{NEW
};
858 my $o_cnt = $hunk->{OCNT
};
859 my $n_cnt = $hunk->{NCNT
};
861 my $head = ("@@ -$o_ofs" .
862 (($o_cnt != 1) ?
",$o_cnt" : '') .
864 (($n_cnt != 1) ?
",$n_cnt" : '') .
866 my $display_head = $head;
867 unshift @
{$hunk->{TEXT
}}, $head;
868 if ($diff_use_color) {
869 $display_head = colored
($fraginfo_color, $head);
871 unshift @
{$hunk->{DISPLAY
}}, $display_head;
876 sub find_last_o_ctx
{
878 my $text = $it->{TEXT
};
879 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
881 my $last_o_ctx = $o_ofs + $o_cnt;
883 my $line = $text->[$i];
894 my ($prev, $this) = @_;
895 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
896 parse_hunk_header
($prev->{TEXT
}[0]);
897 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
898 parse_hunk_header
($this->{TEXT
}[0]);
900 my (@line, $i, $ofs, $o_cnt, $n_cnt);
903 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
904 my $line = $prev->{TEXT
}[$i];
905 if ($line =~ /^\+/) {
911 last if ($o1_ofs <= $ofs);
921 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
922 my $line = $this->{TEXT
}[$i];
923 if ($line =~ /^\+/) {
935 my $head = ("@@ -$o0_ofs" .
936 (($o_cnt != 1) ?
",$o_cnt" : '') .
938 (($n_cnt != 1) ?
",$n_cnt" : '') .
940 @
{$prev->{TEXT
}} = ($head, @line);
943 sub coalesce_overlapping_hunks
{
947 my ($last_o_ctx, $last_was_dirty);
949 for (grep { $_->{USE
} } @in) {
950 if ($_->{TYPE
} ne 'hunk') {
954 my $text = $_->{TEXT
};
955 my ($o_ofs) = parse_hunk_header
($text->[0]);
956 if (defined $last_o_ctx &&
957 $o_ofs <= $last_o_ctx &&
960 merge_hunk
($out[-1], $_);
965 $last_o_ctx = find_last_o_ctx
($out[-1]);
966 $last_was_dirty = $_->{DIRTY
};
971 sub reassemble_patch
{
975 # Include everything in the header except the beginning of the diff.
976 push @patch, (grep { !/^[-+]{3}/ } @
$head);
978 # Then include any headers from the hunk lines, which must
979 # come before any actual hunk.
980 while (@_ && $_[0] !~ /^@/) {
984 # Then begin the diff.
985 push @patch, grep { /^[-+]{3}/ } @
$head;
987 # And then the actual hunks.
995 colored
((/^@/ ?
$fraginfo_color :
996 /^\+/ ?
$diff_new_color :
997 /^-/ ?
$diff_old_color :
1003 sub edit_hunk_manually
{
1006 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
1008 open $fh, '>', $hunkfile
1009 or die "failed to open hunk edit file for writing: " . $!;
1010 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
1011 print $fh @
$oldtext;
1012 my $participle = $patch_mode_flavour{PARTICIPLE
};
1013 my $is_reverse = $patch_mode_flavour{IS_REVERSE
};
1014 my ($remove_plus, $remove_minus) = $is_reverse ?
('-', '+') : ('+', '-');
1017 # To remove '$remove_minus' lines, make them ' ' lines (context).
1018 # To remove '$remove_plus' lines, delete them.
1019 # Lines starting with # will be removed.
1021 # If the patch applies cleanly, the edited hunk will immediately be
1022 # marked for $participle. If it does not apply cleanly, you will be given
1023 # an opportunity to edit again. If all lines of the hunk are removed,
1024 # then the edit is aborted and the hunk is left unchanged.
1028 chomp(my $editor = run_cmd_pipe
(qw(git var GIT_EDITOR)));
1029 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
1035 open $fh, '<', $hunkfile
1036 or die "failed to open hunk edit file for reading: " . $!;
1037 my @newtext = grep { !/^#/ } <$fh>;
1041 # Abort if nothing remains
1042 if (!grep { /\S/ } @newtext) {
1046 # Reinsert the first hunk header if the user accidentally deleted it
1047 if ($newtext[0] !~ /^@/) {
1048 unshift @newtext, $oldtext->[0];
1055 return run_git_apply
($patch_mode_flavour{APPLY_CHECK
} . ' --recount --check',
1056 map { @
{$_->{TEXT
}} } @_);
1059 sub _restore_terminal_and_die
{
1065 sub prompt_single_character
{
1067 local $SIG{TERM
} = \
&_restore_terminal_and_die
;
1068 local $SIG{INT
} = \
&_restore_terminal_and_die
;
1070 my $key = ReadKey
0;
1072 print "$key" if defined $key;
1083 print colored
$prompt_color, $prompt;
1084 my $line = prompt_single_character
;
1085 return 0 if $line =~ /^n/i;
1086 return 1 if $line =~ /^y/i;
1090 sub edit_hunk_loop
{
1091 my ($head, $hunk, $ix) = @_;
1092 my $text = $hunk->[$ix]->{TEXT
};
1095 $text = edit_hunk_manually
($text);
1096 if (!defined $text) {
1101 TYPE
=> $hunk->[$ix]->{TYPE
},
1105 if (diff_applies
($head,
1108 @
{$hunk}[$ix+1..$#{$hunk}])) {
1109 $newhunk->{DISPLAY
} = [color_diff
(@
{$text})];
1114 'Your edited hunk does not apply. Edit again '
1115 . '(saying "no" discards!) [y/n]? '
1121 sub help_patch_cmd
{
1122 my $verb = lc $patch_mode_flavour{VERB
};
1123 my $target = $patch_mode_flavour{TARGET
};
1124 print colored
$help_color, <<EOF ;
1125 y - $verb this hunk$target
1126 n - do not $verb this hunk$target
1127 q - quit; do not $verb this hunk nor any of the remaining ones
1128 a - $verb this hunk and all later hunks in the file
1129 d - do not $verb this hunk nor any of the later hunks in the file
1130 g - select a hunk to go to
1131 / - search for a hunk matching the given regex
1132 j - leave this hunk undecided, see next undecided hunk
1133 J - leave this hunk undecided, see next hunk
1134 k - leave this hunk undecided, see previous undecided hunk
1135 K - leave this hunk undecided, see previous hunk
1136 s - split the current hunk into smaller hunks
1137 e - manually edit the current hunk
1144 my $ret = run_git_apply
$cmd . ' --recount', @_;
1151 sub apply_patch_for_checkout_commit
{
1152 my $reverse = shift;
1153 my $applies_index = run_git_apply
'apply '.$reverse.' --cached --recount --check', @_;
1154 my $applies_worktree = run_git_apply
'apply '.$reverse.' --recount --check', @_;
1156 if ($applies_worktree && $applies_index) {
1157 run_git_apply
'apply '.$reverse.' --cached --recount', @_;
1158 run_git_apply
'apply '.$reverse.' --recount', @_;
1160 } elsif (!$applies_index) {
1161 print colored
$error_color, "The selected hunks do not apply to the index!\n";
1162 if (prompt_yesno
"Apply them to the worktree anyway? ") {
1163 return run_git_apply
'apply '.$reverse.' --recount', @_;
1165 print colored
$error_color, "Nothing was applied.\n";
1174 sub patch_update_cmd
{
1175 my @all_mods = list_modified
($patch_mode_flavour{FILTER
});
1176 my @mods = grep { !($_->{BINARY
}) } @all_mods;
1181 print STDERR
"Only binary files changed.\n";
1183 print STDERR
"No changes.\n";
1191 @them = list_and_choose
({ PROMPT
=> 'Patch update',
1192 HEADER
=> $status_head, },
1196 return 0 if patch_update_file
($_->{VALUE
});
1200 # Generate a one line summary of a hunk.
1201 sub summarize_hunk
{
1203 my $summary = $rhunk->{TEXT
}[0];
1205 # Keep the line numbers, discard extra context.
1206 $summary =~ s/@@(.*?)@@.*/$1 /s;
1207 $summary .= " " x
(20 - length $summary);
1209 # Add some user context.
1210 for my $line (@
{$rhunk->{TEXT
}}) {
1211 if ($line =~ m/^[+-].*\w/) {
1218 return substr($summary, 0, 80) . "\n";
1222 # Print a one-line summary of each hunk in the array ref in
1223 # the first argument, starting wih the index in the 2nd.
1225 my ($hunks, $i) = @_;
1228 for (; $i < @
$hunks && $ctr < 20; $i++, $ctr++) {
1230 if (defined $hunks->[$i]{USE
}) {
1231 $status = $hunks->[$i]{USE
} ?
"+" : "-";
1236 summarize_hunk
($hunks->[$i]);
1241 sub patch_update_file
{
1245 my ($head, @hunk) = parse_diff
($path);
1246 ($head, my $mode, my $deletion) = parse_diff_header
($head);
1247 for (@
{$head->{DISPLAY
}}) {
1251 if (@
{$mode->{TEXT
}}) {
1252 unshift @hunk, $mode;
1254 if (@
{$deletion->{TEXT
}}) {
1255 foreach my $hunk (@hunk) {
1256 push @
{$deletion->{TEXT
}}, @
{$hunk->{TEXT
}};
1257 push @
{$deletion->{DISPLAY
}}, @
{$hunk->{DISPLAY
}};
1259 @hunk = ($deletion);
1262 $num = scalar @hunk;
1266 my ($prev, $next, $other, $undecided, $i);
1272 for ($i = 0; $i < $ix; $i++) {
1273 if (!defined $hunk[$i]{USE
}) {
1282 for ($i = $ix + 1; $i < $num; $i++) {
1283 if (!defined $hunk[$i]{USE
}) {
1289 if ($ix < $num - 1) {
1295 for ($i = 0; $i < $num; $i++) {
1296 if (!defined $hunk[$i]{USE
}) {
1301 last if (!$undecided);
1303 if ($hunk[$ix]{TYPE
} eq 'hunk' &&
1304 hunk_splittable
($hunk[$ix]{TEXT
})) {
1307 if ($hunk[$ix]{TYPE
} eq 'hunk') {
1310 for (@
{$hunk[$ix]{DISPLAY
}}) {
1313 print colored
$prompt_color, $patch_mode_flavour{VERB
},
1314 ($hunk[$ix]{TYPE
} eq 'mode' ?
' mode change' :
1315 $hunk[$ix]{TYPE
} eq 'deletion' ?
' deletion' :
1317 $patch_mode_flavour{TARGET
},
1318 " [y,n,q,a,d,/$other,?]? ";
1319 my $line = prompt_single_character
;
1321 if ($line =~ /^y/i) {
1322 $hunk[$ix]{USE
} = 1;
1324 elsif ($line =~ /^n/i) {
1325 $hunk[$ix]{USE
} = 0;
1327 elsif ($line =~ /^a/i) {
1328 while ($ix < $num) {
1329 if (!defined $hunk[$ix]{USE
}) {
1330 $hunk[$ix]{USE
} = 1;
1336 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1338 my $no = $ix > 10 ?
$ix - 10 : 0;
1339 while ($response eq '') {
1341 $no = display_hunks
(\
@hunk, $no);
1343 $extra = " (<ret> to see more)";
1345 print "go to which hunk$extra? ";
1346 $response = <STDIN
>;
1347 if (!defined $response) {
1352 if ($response !~ /^\s*\d+\s*$/) {
1353 error_msg
"Invalid number: '$response'\n";
1354 } elsif (0 < $response && $response <= $num) {
1355 $ix = $response - 1;
1357 error_msg
"Sorry, only $num hunks available.\n";
1361 elsif ($line =~ /^d/i) {
1362 while ($ix < $num) {
1363 if (!defined $hunk[$ix]{USE
}) {
1364 $hunk[$ix]{USE
} = 0;
1370 elsif ($line =~ /^q/i) {
1371 while ($ix < $num) {
1372 if (!defined $hunk[$ix]{USE
}) {
1373 $hunk[$ix]{USE
} = 0;
1380 elsif ($line =~ m
|^/(.*)|) {
1383 print colored
$prompt_color, "search for regex? ";
1385 if (defined $regex) {
1391 $search_string = qr{$regex}m;
1394 my ($err,$exp) = ($@
, $1);
1395 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1396 error_msg
"Malformed search regexp $exp: $err\n";
1401 my $text = join ("", @
{$hunk[$iy]{TEXT
}});
1402 last if ($text =~ $search_string);
1404 $iy = 0 if ($iy >= $num);
1406 error_msg
"No hunk matches the given pattern\n";
1413 elsif ($line =~ /^K/) {
1414 if ($other =~ /K/) {
1418 error_msg
"No previous hunk\n";
1422 elsif ($line =~ /^J/) {
1423 if ($other =~ /J/) {
1427 error_msg
"No next hunk\n";
1431 elsif ($line =~ /^k/) {
1432 if ($other =~ /k/) {
1436 !defined $hunk[$ix]{USE
});
1440 error_msg
"No previous hunk\n";
1444 elsif ($line =~ /^j/) {
1445 if ($other !~ /j/) {
1446 error_msg
"No next hunk\n";
1450 elsif ($other =~ /s/ && $line =~ /^s/) {
1451 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
1453 print colored
$header_color, "Split into ",
1454 scalar(@split), " hunks.\n";
1456 splice (@hunk, $ix, 1, @split);
1457 $num = scalar @hunk;
1460 elsif ($other =~ /e/ && $line =~ /^e/) {
1461 my $newhunk = edit_hunk_loop
($head, \
@hunk, $ix);
1462 if (defined $newhunk) {
1463 splice @hunk, $ix, 1, $newhunk;
1467 help_patch_cmd
($other);
1473 last if ($ix >= $num ||
1474 !defined $hunk[$ix]{USE
});
1479 @hunk = coalesce_overlapping_hunks
(@hunk);
1485 push @result, @
{$_->{TEXT
}};
1491 my @patch = reassemble_patch
($head->{TEXT
}, @result);
1492 my $apply_routine = $patch_mode_flavour{APPLY
};
1493 &$apply_routine(@patch);
1502 my @mods = list_modified
('index-only');
1503 @mods = grep { !($_->{BINARY
}) } @mods;
1505 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
1507 HEADER
=> $status_head, },
1510 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
1511 system(qw(git diff -p --cached), $reference, '--',
1512 map { $_->{VALUE
} } @them);
1521 print colored
$help_color, <<\EOF
;
1522 status
- show paths with changes
1523 update
- add working tree
state to the staged set of changes
1524 revert
- revert staged set of changes back to the HEAD version
1525 patch
- pick hunks
and update selectively
1526 diff
- view diff between HEAD
and index
1527 add untracked
- add contents of untracked files to the staged set of changes
1532 return unless @ARGV;
1533 my $arg = shift @ARGV;
1534 if ($arg =~ /--patch(?:=(.*))?/) {
1536 if ($1 eq 'reset') {
1537 $patch_mode = 'reset_head';
1538 $patch_mode_revision = 'HEAD';
1539 $arg = shift @ARGV or die "missing --";
1541 $patch_mode_revision = $arg;
1542 $patch_mode = ($arg eq 'HEAD' ?
1543 'reset_head' : 'reset_nothead');
1544 $arg = shift @ARGV or die "missing --";
1546 } elsif ($1 eq 'checkout') {
1547 $arg = shift @ARGV or die "missing --";
1549 $patch_mode = 'checkout_index';
1551 $patch_mode_revision = $arg;
1552 $patch_mode = ($arg eq 'HEAD' ?
1553 'checkout_head' : 'checkout_nothead');
1554 $arg = shift @ARGV or die "missing --";
1556 } elsif ($1 eq 'stage' or $1 eq 'stash') {
1558 $arg = shift @ARGV or die "missing --";
1560 die "unknown --patch mode: $1";
1563 $patch_mode = 'stage';
1564 $arg = shift @ARGV or die "missing --";
1566 die "invalid argument $arg, expecting --"
1567 unless $arg eq "--";
1568 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
1570 elsif ($arg ne "--") {
1571 die "invalid argument $arg, expecting --";
1576 my @cmd = ([ 'status', \
&status_cmd
, ],
1577 [ 'update', \
&update_cmd
, ],
1578 [ 'revert', \
&revert_cmd
, ],
1579 [ 'add untracked', \
&add_untracked_cmd
, ],
1580 [ 'patch', \
&patch_update_cmd
, ],
1581 [ 'diff', \
&diff_cmd
, ],
1582 [ 'quit', \
&quit_cmd
, ],
1583 [ 'help', \
&help_cmd
, ],
1586 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1589 HEADER
=> '*** Commands ***',
1590 ON_EOF
=> \
&quit_cmd
,
1591 IMMEDIATE
=> 1 }, @cmd);