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;
81 DIFF
=> 'diff-files -p',
82 APPLY
=> sub { apply_patch
'apply --cached', @_; },
83 APPLY_CHECK
=> 'apply --cached',
86 PARTICIPLE
=> 'staging',
87 FILTER
=> 'file-only',
90 DIFF
=> 'diff-index -p --cached',
91 APPLY
=> sub { apply_patch
'apply -R --cached', @_; },
92 APPLY_CHECK
=> 'apply -R --cached',
95 PARTICIPLE
=> 'unstaging',
96 FILTER
=> 'index-only',
99 DIFF
=> 'diff-index -R -p --cached',
100 APPLY
=> sub { apply_patch
'apply --cached', @_; },
101 APPLY_CHECK
=> 'apply --cached',
103 TARGET
=> ' to index',
104 PARTICIPLE
=> 'applying',
105 FILTER
=> 'index-only',
109 my %patch_mode_flavour = %{$patch_modes{stage
}};
112 if ($^O
eq 'MSWin32' || $^O
eq 'msys') {
113 my @invalid = grep {m/[":*]/} @_;
114 die "$^O does not support: @invalid\n" if @invalid;
115 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
119 open($fh, '-|', @_) or die;
124 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
126 if (!defined $GIT_DIR) {
127 exit(1); # rev-parse would have already said "not a git repo"
144 my ($retval, $remainder);
145 if (!/^\042(.*)\042$/) {
148 ($_, $retval) = ($1, "");
149 while (/^([^\\]*)\\(.*)$/) {
153 if (/^([0-3][0-7][0-7])(.*)$/) {
154 $retval .= chr(oct($1));
158 if (/^([\\\042btnvfr])(.*)$/) {
159 $retval .= $cquote_map{$1};
163 # This is malformed -- just return it as-is for now.
174 open $fh, 'git update-index --refresh |'
177 ;# ignore 'needs update'
187 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
190 my $status_fmt = '%12s %12s %s';
191 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
195 sub is_initial_commit
{
196 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
197 unless defined $initial;
203 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
206 # Returns list of hashes, contents of each of which are:
208 # BINARY: is a binary path
209 # INDEX: is index different from HEAD?
210 # FILE: is file different from index?
211 # INDEX_ADDDEL: is it add/delete between HEAD and index?
212 # FILE_ADDDEL: is it add/delete between index and file?
217 my ($add, $del, $adddel, $file);
224 } run_cmd_pipe
(qw(git ls-files --exclude-standard --), @ARGV);
225 return if (!@tracked);
229 if (defined $patch_mode_revision and $patch_mode_revision ne 'HEAD') {
230 $reference = $patch_mode_revision;
231 } elsif (is_initial_commit
()) {
232 $reference = get_empty_tree
();
236 for (run_cmd_pipe
(qw(git diff-index --cached
237 --numstat --summary), $reference,
239 if (($add, $del, $file) =
240 /^([-\d]+) ([-\d]+) (.*)/) {
242 $file = unquote_path
($file);
243 if ($add eq '-' && $del eq '-') {
248 $change = "+$add/-$del";
256 elsif (($adddel, $file) =
257 /^ (create|delete) mode [0-7]+ (.*)$/) {
258 $file = unquote_path
($file);
259 $data{$file}{INDEX_ADDDEL
} = $adddel;
263 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
264 if (($add, $del, $file) =
265 /^([-\d]+) ([-\d]+) (.*)/) {
266 $file = unquote_path
($file);
267 if (!exists $data{$file}) {
269 INDEX
=> 'unchanged',
274 if ($add eq '-' && $del eq '-') {
279 $change = "+$add/-$del";
281 $data{$file}{FILE
} = $change;
283 $data{$file}{BINARY
} = 1;
286 elsif (($adddel, $file) =
287 /^ (create|delete) mode [0-7]+ (.*)$/) {
288 $file = unquote_path
($file);
289 $data{$file}{FILE_ADDDEL
} = $adddel;
293 for (sort keys %data) {
297 if ($only eq 'index-only') {
298 next if ($it->{INDEX
} eq 'unchanged');
300 if ($only eq 'file-only') {
301 next if ($it->{FILE
} eq 'nothing');
313 my ($string, @stuff) = @_;
315 for (my $i = 0; $i < @stuff; $i++) {
319 if ((ref $it) eq 'ARRAY') {
327 if ($it =~ /^$string/) {
331 if (defined $hit && defined $found) {
341 # inserts string into trie and updates count for each character
343 my ($trie, $string) = @_;
344 foreach (split //, $string) {
345 $trie = $trie->{$_} ||= {COUNT
=> 0};
350 # returns an array of tuples (prefix, remainder)
351 sub find_unique_prefixes
{
355 # any single prefix exceeding the soft limit is omitted
356 # if any prefix exceeds the hard limit all are omitted
357 # 0 indicates no limit
361 # build a trie modelling all possible options
363 foreach my $print (@stuff) {
364 if ((ref $print) eq 'ARRAY') {
365 $print = $print->[0];
367 elsif ((ref $print) eq 'HASH') {
368 $print = $print->{VALUE
};
370 update_trie
(\
%trie, $print);
371 push @return, $print;
374 # use the trie to find the unique prefixes
375 for (my $i = 0; $i < @return; $i++) {
376 my $ret = $return[$i];
377 my @letters = split //, $ret;
379 my ($prefix, $remainder);
381 for ($j = 0; $j < @letters; $j++) {
382 my $letter = $letters[$j];
383 if ($search{$letter}{COUNT
} == 1) {
384 $prefix = substr $ret, 0, $j + 1;
385 $remainder = substr $ret, $j + 1;
389 my $prefix = substr $ret, 0, $j;
391 if ($hard_limit && $j + 1 > $hard_limit);
393 %search = %{$search{$letter}};
395 if (ord($letters[0]) > 127 ||
396 ($soft_limit && $j + 1 > $soft_limit)) {
400 $return[$i] = [$prefix, $remainder];
405 # filters out prefixes which have special meaning to list_and_choose()
406 sub is_valid_prefix
{
408 return (defined $prefix) &&
409 !($prefix =~ /[\s,]/) && # separators
410 !($prefix =~ /^-/) && # deselection
411 !($prefix =~ /^\d+/) && # selection
412 ($prefix ne '*') && # "all" wildcard
413 ($prefix ne '?'); # prompt help
416 # given a prefix/remainder tuple return a string with the prefix highlighted
417 # for now use square brackets; later might use ANSI colors (underline, bold)
418 sub highlight_prefix
{
420 my $remainder = shift;
422 if (!defined $prefix) {
426 if (!is_valid_prefix
($prefix)) {
427 return "$prefix$remainder";
430 if (!$menu_use_color) {
431 return "[$prefix]$remainder";
434 return "$prompt_color$prefix$normal_color$remainder";
438 print STDERR colored
$error_color, @_;
441 sub list_and_choose
{
442 my ($opts, @stuff) = @_;
443 my (@chosen, @return);
445 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
451 if ($opts->{HEADER
}) {
452 if (!$opts->{LIST_FLAT
}) {
455 print colored
$header_color, "$opts->{HEADER}\n";
457 for ($i = 0; $i < @stuff; $i++) {
458 my $chosen = $chosen[$i] ?
'*' : ' ';
459 my $print = $stuff[$i];
460 my $ref = ref $print;
461 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
463 if ($ref eq 'ARRAY') {
464 $print = $highlighted || $print->[0];
466 elsif ($ref eq 'HASH') {
467 my $value = $highlighted || $print->{VALUE
};
468 $print = sprintf($status_fmt,
474 $print = $highlighted || $print;
476 printf("%s%2d: %s", $chosen, $i+1, $print);
477 if (($opts->{LIST_FLAT
}) &&
478 (($i + 1) % ($opts->{LIST_FLAT
}))) {
491 return if ($opts->{LIST_ONLY
});
493 print colored
$prompt_color, $opts->{PROMPT
};
494 if ($opts->{SINGLETON
}) {
503 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
510 singleton_prompt_help_cmd
() :
514 for my $choice (split(/[\s,]+/, $line)) {
518 # Input that begins with '-'; unchoose
519 if ($choice =~ s/^-//) {
522 # A range can be specified like 5-7 or 5-.
523 if ($choice =~ /^(\d+)-(\d*)$/) {
524 ($bottom, $top) = ($1, length($2) ?
$2 : 1 + @stuff);
526 elsif ($choice =~ /^\d+$/) {
527 $bottom = $top = $choice;
529 elsif ($choice eq '*') {
534 $bottom = $top = find_unique
($choice, @stuff);
535 if (!defined $bottom) {
536 error_msg
"Huh ($choice)?\n";
540 if ($opts->{SINGLETON
} && $bottom != $top) {
541 error_msg
"Huh ($choice)?\n";
544 for ($i = $bottom-1; $i <= $top-1; $i++) {
545 next if (@stuff <= $i || $i < 0);
546 $chosen[$i] = $choose;
549 last if ($opts->{IMMEDIATE
} || $line eq '*');
551 for ($i = 0; $i < @stuff; $i++) {
553 push @return, $stuff[$i];
559 sub singleton_prompt_help_cmd
{
560 print colored
$help_color, <<\EOF
;
562 1 - select a numbered item
563 foo
- select item based on unique prefix
564 - (empty
) select nothing
568 sub prompt_help_cmd
{
569 print colored
$help_color, <<\EOF
;
571 1 - select a single item
572 3-5 - select a range of items
573 2-3,6-9 - select multiple ranges
574 foo
- select item based on unique prefix
575 -... - unselect specified items
577 - (empty
) finish selecting
582 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
592 print "$cnt paths\n";
600 my @mods = list_modified
('file-only');
603 my @update = list_and_choose
({ PROMPT
=> 'Update',
604 HEADER
=> $status_head, },
607 system(qw(git update-index --add --remove --),
608 map { $_->{VALUE
} } @update);
609 say_n_paths
('updated', @update);
615 my @update = list_and_choose
({ PROMPT
=> 'Revert',
616 HEADER
=> $status_head, },
619 if (is_initial_commit
()) {
620 system(qw(git rm --cached),
621 map { $_->{VALUE
} } @update);
624 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
625 map { $_->{VALUE
} } @update);
627 open $fh, '| git update-index --index-info'
634 if ($_->{INDEX_ADDDEL
} &&
635 $_->{INDEX_ADDDEL
} eq 'create') {
636 system(qw(git update-index --force-remove --),
638 print "note: $_->{VALUE} is untracked now.\n";
643 say_n_paths
('reverted', @update);
648 sub add_untracked_cmd
{
649 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
652 system(qw(git update-index --add --), @add);
653 say_n_paths
('added', @add);
661 open $fh, '| git ' . $cmd;
668 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF
});
669 if (defined $patch_mode_revision) {
670 push @diff_cmd, $patch_mode_revision;
672 my @diff = run_cmd_pipe
("git", @diff_cmd, "--", $path);
674 if ($diff_use_color) {
675 @colored = run_cmd_pipe
("git", @diff_cmd, qw(--color --), $path);
677 my (@hunk) = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
679 for (my $i = 0; $i < @diff; $i++) {
680 if ($diff[$i] =~ /^@@ /) {
681 push @hunk, { TEXT
=> [], DISPLAY
=> [],
684 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
685 push @
{$hunk[-1]{DISPLAY
}},
686 ($diff_use_color ?
$colored[$i] : $diff[$i]);
691 sub parse_diff_header
{
694 my $head = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
695 my $mode = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'mode' };
697 for (my $i = 0; $i < @
{$src->{TEXT
}}; $i++) {
698 my $dest = $src->{TEXT
}->[$i] =~ /^(old|new) mode (\d+)$/ ?
700 push @
{$dest->{TEXT
}}, $src->{TEXT
}->[$i];
701 push @
{$dest->{DISPLAY
}}, $src->{DISPLAY
}->[$i];
703 return ($head, $mode);
706 sub hunk_splittable
{
709 my @s = split_hunk
($text);
713 sub parse_hunk_header
{
715 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
716 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
717 $o_cnt = 1 unless defined $o_cnt;
718 $n_cnt = 1 unless defined $n_cnt;
719 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
723 my ($text, $display) = @_;
725 if (!defined $display) {
728 # If there are context lines in the middle of a hunk,
729 # it can be split, but we would need to take care of
732 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
737 my $next_hunk_start = undef;
738 my $i = $hunk_start - 1;
752 while (++$i < @
$text) {
753 my $line = $text->[$i];
754 my $display = $display->[$i];
756 if ($this->{ADDDEL
} &&
757 !defined $next_hunk_start) {
758 # We have seen leading context and
759 # adds/dels and then here is another
760 # context, which is trailing for this
761 # split hunk and leading for the next
763 $next_hunk_start = $i;
765 push @
{$this->{TEXT
}}, $line;
766 push @
{$this->{DISPLAY
}}, $display;
769 if (defined $next_hunk_start) {
776 if (defined $next_hunk_start) {
777 # We are done with the current hunk and
778 # this is the first real change for the
780 $hunk_start = $next_hunk_start;
781 $o_ofs = $this->{OLD
} + $this->{OCNT
};
782 $n_ofs = $this->{NEW
} + $this->{NCNT
};
783 $o_ofs -= $this->{POSTCTX
};
784 $n_ofs -= $this->{POSTCTX
};
788 push @
{$this->{TEXT
}}, $line;
789 push @
{$this->{DISPLAY
}}, $display;
803 for my $hunk (@split) {
804 $o_ofs = $hunk->{OLD
};
805 $n_ofs = $hunk->{NEW
};
806 my $o_cnt = $hunk->{OCNT
};
807 my $n_cnt = $hunk->{NCNT
};
809 my $head = ("@@ -$o_ofs" .
810 (($o_cnt != 1) ?
",$o_cnt" : '') .
812 (($n_cnt != 1) ?
",$n_cnt" : '') .
814 my $display_head = $head;
815 unshift @
{$hunk->{TEXT
}}, $head;
816 if ($diff_use_color) {
817 $display_head = colored
($fraginfo_color, $head);
819 unshift @
{$hunk->{DISPLAY
}}, $display_head;
824 sub find_last_o_ctx
{
826 my $text = $it->{TEXT
};
827 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
829 my $last_o_ctx = $o_ofs + $o_cnt;
831 my $line = $text->[$i];
842 my ($prev, $this) = @_;
843 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
844 parse_hunk_header
($prev->{TEXT
}[0]);
845 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
846 parse_hunk_header
($this->{TEXT
}[0]);
848 my (@line, $i, $ofs, $o_cnt, $n_cnt);
851 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
852 my $line = $prev->{TEXT
}[$i];
853 if ($line =~ /^\+/) {
859 last if ($o1_ofs <= $ofs);
869 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
870 my $line = $this->{TEXT
}[$i];
871 if ($line =~ /^\+/) {
883 my $head = ("@@ -$o0_ofs" .
884 (($o_cnt != 1) ?
",$o_cnt" : '') .
886 (($n_cnt != 1) ?
",$n_cnt" : '') .
888 @
{$prev->{TEXT
}} = ($head, @line);
891 sub coalesce_overlapping_hunks
{
895 my ($last_o_ctx, $last_was_dirty);
897 for (grep { $_->{USE
} } @in) {
898 my $text = $_->{TEXT
};
899 my ($o_ofs) = parse_hunk_header
($text->[0]);
900 if (defined $last_o_ctx &&
901 $o_ofs <= $last_o_ctx &&
904 merge_hunk
($out[-1], $_);
909 $last_o_ctx = find_last_o_ctx
($out[-1]);
910 $last_was_dirty = $_->{DIRTY
};
917 colored
((/^@/ ?
$fraginfo_color :
918 /^\+/ ?
$diff_new_color :
919 /^-/ ?
$diff_old_color :
925 sub edit_hunk_manually
{
928 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
930 open $fh, '>', $hunkfile
931 or die "failed to open hunk edit file for writing: " . $!;
932 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
934 my $participle = $patch_mode_flavour{PARTICIPLE
};
937 # To remove '-' lines, make them ' ' lines (context).
938 # To remove '+' lines, delete them.
939 # Lines starting with # will be removed.
941 # If the patch applies cleanly, the edited hunk will immediately be
942 # marked for $participle. If it does not apply cleanly, you will be given
943 # an opportunity to edit again. If all lines of the hunk are removed,
944 # then the edit is aborted and the hunk is left unchanged.
948 my $editor = $ENV{GIT_EDITOR
} || $repo->config("core.editor")
949 || $ENV{VISUAL
} || $ENV{EDITOR
} || "vi";
950 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
956 open $fh, '<', $hunkfile
957 or die "failed to open hunk edit file for reading: " . $!;
958 my @newtext = grep { !/^#/ } <$fh>;
962 # Abort if nothing remains
963 if (!grep { /\S/ } @newtext) {
967 # Reinsert the first hunk header if the user accidentally deleted it
968 if ($newtext[0] !~ /^@/) {
969 unshift @newtext, $oldtext->[0];
976 return run_git_apply
($patch_mode_flavour{APPLY_CHECK
} . ' --recount --check',
977 map { @
{$_->{TEXT
}} } @_);
980 sub _restore_terminal_and_die
{
986 sub prompt_single_character
{
988 local $SIG{TERM
} = \
&_restore_terminal_and_die
;
989 local $SIG{INT
} = \
&_restore_terminal_and_die
;
993 print "$key" if defined $key;
1004 print colored
$prompt_color, $prompt;
1005 my $line = prompt_single_character
;
1006 return 0 if $line =~ /^n/i;
1007 return 1 if $line =~ /^y/i;
1011 sub edit_hunk_loop
{
1012 my ($head, $hunk, $ix) = @_;
1013 my $text = $hunk->[$ix]->{TEXT
};
1016 $text = edit_hunk_manually
($text);
1017 if (!defined $text) {
1022 TYPE
=> $hunk->[$ix]->{TYPE
},
1026 if (diff_applies
($head,
1029 @
{$hunk}[$ix+1..$#{$hunk}])) {
1030 $newhunk->{DISPLAY
} = [color_diff
(@
{$text})];
1035 'Your edited hunk does not apply. Edit again '
1036 . '(saying "no" discards!) [y/n]? '
1042 sub help_patch_cmd
{
1043 my $verb = lc $patch_mode_flavour{VERB
};
1044 my $target = $patch_mode_flavour{TARGET
};
1045 print colored
$help_color, <<EOF ;
1046 y - $verb this hunk$target
1047 n - do not $verb this hunk$target
1048 q - quit, do not $verb this hunk nor any of the remaining ones
1049 a - $verb this and all the remaining hunks in the file
1050 d - do not $verb this hunk nor any of the remaining hunks in the file
1051 g - select a hunk to go to
1052 / - search for a hunk matching the given regex
1053 j - leave this hunk undecided, see next undecided hunk
1054 J - leave this hunk undecided, see next hunk
1055 k - leave this hunk undecided, see previous undecided hunk
1056 K - leave this hunk undecided, see previous hunk
1057 s - split the current hunk into smaller hunks
1058 e - manually edit the current hunk
1065 my $ret = run_git_apply
$cmd . ' --recount', @_;
1072 sub patch_update_cmd
{
1073 my @all_mods = list_modified
($patch_mode_flavour{FILTER
});
1074 my @mods = grep { !($_->{BINARY
}) } @all_mods;
1079 print STDERR
"Only binary files changed.\n";
1081 print STDERR
"No changes.\n";
1089 @them = list_and_choose
({ PROMPT
=> 'Patch update',
1090 HEADER
=> $status_head, },
1094 return 0 if patch_update_file
($_->{VALUE
});
1098 # Generate a one line summary of a hunk.
1099 sub summarize_hunk
{
1101 my $summary = $rhunk->{TEXT
}[0];
1103 # Keep the line numbers, discard extra context.
1104 $summary =~ s/@@(.*?)@@.*/$1 /s;
1105 $summary .= " " x
(20 - length $summary);
1107 # Add some user context.
1108 for my $line (@
{$rhunk->{TEXT
}}) {
1109 if ($line =~ m/^[+-].*\w/) {
1116 return substr($summary, 0, 80) . "\n";
1120 # Print a one-line summary of each hunk in the array ref in
1121 # the first argument, starting wih the index in the 2nd.
1123 my ($hunks, $i) = @_;
1126 for (; $i < @
$hunks && $ctr < 20; $i++, $ctr++) {
1128 if (defined $hunks->[$i]{USE
}) {
1129 $status = $hunks->[$i]{USE
} ?
"+" : "-";
1134 summarize_hunk
($hunks->[$i]);
1139 sub patch_update_file
{
1143 my ($head, @hunk) = parse_diff
($path);
1144 ($head, my $mode) = parse_diff_header
($head);
1145 for (@
{$head->{DISPLAY
}}) {
1149 if (@
{$mode->{TEXT
}}) {
1150 unshift @hunk, $mode;
1153 $num = scalar @hunk;
1157 my ($prev, $next, $other, $undecided, $i);
1163 for ($i = 0; $i < $ix; $i++) {
1164 if (!defined $hunk[$i]{USE
}) {
1173 for ($i = $ix + 1; $i < $num; $i++) {
1174 if (!defined $hunk[$i]{USE
}) {
1180 if ($ix < $num - 1) {
1186 for ($i = 0; $i < $num; $i++) {
1187 if (!defined $hunk[$i]{USE
}) {
1192 last if (!$undecided);
1194 if ($hunk[$ix]{TYPE
} eq 'hunk' &&
1195 hunk_splittable
($hunk[$ix]{TEXT
})) {
1198 if ($hunk[$ix]{TYPE
} eq 'hunk') {
1201 for (@
{$hunk[$ix]{DISPLAY
}}) {
1204 print colored
$prompt_color, $patch_mode_flavour{VERB
},
1205 ($hunk[$ix]{TYPE
} eq 'mode' ?
' mode change' : ' this hunk'),
1206 $patch_mode_flavour{TARGET
},
1207 " [y,n,q,a,d,/$other,?]? ";
1208 my $line = prompt_single_character
;
1210 if ($line =~ /^y/i) {
1211 $hunk[$ix]{USE
} = 1;
1213 elsif ($line =~ /^n/i) {
1214 $hunk[$ix]{USE
} = 0;
1216 elsif ($line =~ /^a/i) {
1217 while ($ix < $num) {
1218 if (!defined $hunk[$ix]{USE
}) {
1219 $hunk[$ix]{USE
} = 1;
1225 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1227 my $no = $ix > 10 ?
$ix - 10 : 0;
1228 while ($response eq '') {
1230 $no = display_hunks
(\
@hunk, $no);
1232 $extra = " (<ret> to see more)";
1234 print "go to which hunk$extra? ";
1235 $response = <STDIN
>;
1236 if (!defined $response) {
1241 if ($response !~ /^\s*\d+\s*$/) {
1242 error_msg
"Invalid number: '$response'\n";
1243 } elsif (0 < $response && $response <= $num) {
1244 $ix = $response - 1;
1246 error_msg
"Sorry, only $num hunks available.\n";
1250 elsif ($line =~ /^d/i) {
1251 while ($ix < $num) {
1252 if (!defined $hunk[$ix]{USE
}) {
1253 $hunk[$ix]{USE
} = 0;
1259 elsif ($line =~ /^q/i) {
1260 while ($ix < $num) {
1261 if (!defined $hunk[$ix]{USE
}) {
1262 $hunk[$ix]{USE
} = 0;
1269 elsif ($line =~ m
|^/(.*)|) {
1272 print colored
$prompt_color, "search for regex? ";
1274 if (defined $regex) {
1280 $search_string = qr{$regex}m;
1283 my ($err,$exp) = ($@
, $1);
1284 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1285 error_msg
"Malformed search regexp $exp: $err\n";
1290 my $text = join ("", @
{$hunk[$iy]{TEXT
}});
1291 last if ($text =~ $search_string);
1293 $iy = 0 if ($iy >= $num);
1295 error_msg
"No hunk matches the given pattern\n";
1302 elsif ($line =~ /^K/) {
1303 if ($other =~ /K/) {
1307 error_msg
"No previous hunk\n";
1311 elsif ($line =~ /^J/) {
1312 if ($other =~ /J/) {
1316 error_msg
"No next hunk\n";
1320 elsif ($line =~ /^k/) {
1321 if ($other =~ /k/) {
1325 !defined $hunk[$ix]{USE
});
1329 error_msg
"No previous hunk\n";
1333 elsif ($line =~ /^j/) {
1334 if ($other !~ /j/) {
1335 error_msg
"No next hunk\n";
1339 elsif ($other =~ /s/ && $line =~ /^s/) {
1340 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
1342 print colored
$header_color, "Split into ",
1343 scalar(@split), " hunks.\n";
1345 splice (@hunk, $ix, 1, @split);
1346 $num = scalar @hunk;
1349 elsif ($other =~ /e/ && $line =~ /^e/) {
1350 my $newhunk = edit_hunk_loop
($head, \
@hunk, $ix);
1351 if (defined $newhunk) {
1352 splice @hunk, $ix, 1, $newhunk;
1356 help_patch_cmd
($other);
1362 last if ($ix >= $num ||
1363 !defined $hunk[$ix]{USE
});
1368 @hunk = coalesce_overlapping_hunks
(@hunk);
1374 push @result, @
{$_->{TEXT
}};
1380 my @patch = (@
{$head->{TEXT
}}, @result);
1381 my $apply_routine = $patch_mode_flavour{APPLY
};
1382 &$apply_routine(@patch);
1391 my @mods = list_modified
('index-only');
1392 @mods = grep { !($_->{BINARY
}) } @mods;
1394 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
1396 HEADER
=> $status_head, },
1399 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
1400 system(qw(git diff -p --cached), $reference, '--',
1401 map { $_->{VALUE
} } @them);
1410 print colored
$help_color, <<\EOF
;
1411 status
- show paths with changes
1412 update
- add working tree
state to the staged set of changes
1413 revert
- revert staged set of changes back to the HEAD version
1414 patch
- pick hunks
and update selectively
1415 diff
- view diff between HEAD
and index
1416 add untracked
- add contents of untracked files to the staged set of changes
1421 return unless @ARGV;
1422 my $arg = shift @ARGV;
1423 if ($arg =~ /--patch(?:=(.*))?/) {
1425 if ($1 eq 'reset') {
1426 $patch_mode = 'reset_head';
1427 $patch_mode_revision = 'HEAD';
1428 $arg = shift @ARGV or die "missing --";
1430 $patch_mode_revision = $arg;
1431 $patch_mode = ($arg eq 'HEAD' ?
1432 'reset_head' : 'reset_nothead');
1433 $arg = shift @ARGV or die "missing --";
1435 } elsif ($1 eq 'stage') {
1436 $patch_mode = 'stage';
1437 $arg = shift @ARGV or die "missing --";
1439 die "unknown --patch mode: $1";
1442 $patch_mode = 'stage';
1443 $arg = shift @ARGV or die "missing --";
1445 die "invalid argument $arg, expecting --"
1446 unless $arg eq "--";
1447 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
1449 elsif ($arg ne "--") {
1450 die "invalid argument $arg, expecting --";
1455 my @cmd = ([ 'status', \
&status_cmd
, ],
1456 [ 'update', \
&update_cmd
, ],
1457 [ 'revert', \
&revert_cmd
, ],
1458 [ 'add untracked', \
&add_untracked_cmd
, ],
1459 [ 'patch', \
&patch_update_cmd
, ],
1460 [ 'diff', \
&diff_cmd
, ],
1461 [ 'quit', \
&quit_cmd
, ],
1462 [ 'help', \
&help_cmd
, ],
1465 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1468 HEADER
=> '*** Commands ***',
1469 ON_EOF
=> \
&quit_cmd
,
1470 IMMEDIATE
=> 1 }, @cmd);