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
80 DIFF
=> 'diff-files -p',
81 APPLY
=> sub { apply_patch
'apply --cached', @_; },
82 APPLY_CHECK
=> 'apply --cached',
85 PARTICIPLE
=> 'staging',
86 FILTER
=> 'file-only',
90 my %patch_mode_flavour = %{$patch_modes{stage
}};
93 if ($^O
eq 'MSWin32' || $^O
eq 'msys') {
94 my @invalid = grep {m/[":*]/} @_;
95 die "$^O does not support: @invalid\n" if @invalid;
96 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
100 open($fh, '-|', @_) or die;
105 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
107 if (!defined $GIT_DIR) {
108 exit(1); # rev-parse would have already said "not a git repo"
125 my ($retval, $remainder);
126 if (!/^\042(.*)\042$/) {
129 ($_, $retval) = ($1, "");
130 while (/^([^\\]*)\\(.*)$/) {
134 if (/^([0-3][0-7][0-7])(.*)$/) {
135 $retval .= chr(oct($1));
139 if (/^([\\\042btnvfr])(.*)$/) {
140 $retval .= $cquote_map{$1};
144 # This is malformed -- just return it as-is for now.
155 open $fh, 'git update-index --refresh |'
158 ;# ignore 'needs update'
168 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
171 my $status_fmt = '%12s %12s %s';
172 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
176 sub is_initial_commit
{
177 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
178 unless defined $initial;
184 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
187 # Returns list of hashes, contents of each of which are:
189 # BINARY: is a binary path
190 # INDEX: is index different from HEAD?
191 # FILE: is file different from index?
192 # INDEX_ADDDEL: is it add/delete between HEAD and index?
193 # FILE_ADDDEL: is it add/delete between index and file?
198 my ($add, $del, $adddel, $file);
205 } run_cmd_pipe
(qw(git ls-files --exclude-standard --), @ARGV);
206 return if (!@tracked);
209 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
210 for (run_cmd_pipe
(qw(git diff-index --cached
211 --numstat --summary), $reference,
213 if (($add, $del, $file) =
214 /^([-\d]+) ([-\d]+) (.*)/) {
216 $file = unquote_path
($file);
217 if ($add eq '-' && $del eq '-') {
222 $change = "+$add/-$del";
230 elsif (($adddel, $file) =
231 /^ (create|delete) mode [0-7]+ (.*)$/) {
232 $file = unquote_path
($file);
233 $data{$file}{INDEX_ADDDEL
} = $adddel;
237 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
238 if (($add, $del, $file) =
239 /^([-\d]+) ([-\d]+) (.*)/) {
240 $file = unquote_path
($file);
241 if (!exists $data{$file}) {
243 INDEX
=> 'unchanged',
248 if ($add eq '-' && $del eq '-') {
253 $change = "+$add/-$del";
255 $data{$file}{FILE
} = $change;
257 $data{$file}{BINARY
} = 1;
260 elsif (($adddel, $file) =
261 /^ (create|delete) mode [0-7]+ (.*)$/) {
262 $file = unquote_path
($file);
263 $data{$file}{FILE_ADDDEL
} = $adddel;
267 for (sort keys %data) {
271 if ($only eq 'index-only') {
272 next if ($it->{INDEX
} eq 'unchanged');
274 if ($only eq 'file-only') {
275 next if ($it->{FILE
} eq 'nothing');
287 my ($string, @stuff) = @_;
289 for (my $i = 0; $i < @stuff; $i++) {
293 if ((ref $it) eq 'ARRAY') {
301 if ($it =~ /^$string/) {
305 if (defined $hit && defined $found) {
315 # inserts string into trie and updates count for each character
317 my ($trie, $string) = @_;
318 foreach (split //, $string) {
319 $trie = $trie->{$_} ||= {COUNT
=> 0};
324 # returns an array of tuples (prefix, remainder)
325 sub find_unique_prefixes
{
329 # any single prefix exceeding the soft limit is omitted
330 # if any prefix exceeds the hard limit all are omitted
331 # 0 indicates no limit
335 # build a trie modelling all possible options
337 foreach my $print (@stuff) {
338 if ((ref $print) eq 'ARRAY') {
339 $print = $print->[0];
341 elsif ((ref $print) eq 'HASH') {
342 $print = $print->{VALUE
};
344 update_trie
(\
%trie, $print);
345 push @return, $print;
348 # use the trie to find the unique prefixes
349 for (my $i = 0; $i < @return; $i++) {
350 my $ret = $return[$i];
351 my @letters = split //, $ret;
353 my ($prefix, $remainder);
355 for ($j = 0; $j < @letters; $j++) {
356 my $letter = $letters[$j];
357 if ($search{$letter}{COUNT
} == 1) {
358 $prefix = substr $ret, 0, $j + 1;
359 $remainder = substr $ret, $j + 1;
363 my $prefix = substr $ret, 0, $j;
365 if ($hard_limit && $j + 1 > $hard_limit);
367 %search = %{$search{$letter}};
369 if (ord($letters[0]) > 127 ||
370 ($soft_limit && $j + 1 > $soft_limit)) {
374 $return[$i] = [$prefix, $remainder];
379 # filters out prefixes which have special meaning to list_and_choose()
380 sub is_valid_prefix
{
382 return (defined $prefix) &&
383 !($prefix =~ /[\s,]/) && # separators
384 !($prefix =~ /^-/) && # deselection
385 !($prefix =~ /^\d+/) && # selection
386 ($prefix ne '*') && # "all" wildcard
387 ($prefix ne '?'); # prompt help
390 # given a prefix/remainder tuple return a string with the prefix highlighted
391 # for now use square brackets; later might use ANSI colors (underline, bold)
392 sub highlight_prefix
{
394 my $remainder = shift;
396 if (!defined $prefix) {
400 if (!is_valid_prefix
($prefix)) {
401 return "$prefix$remainder";
404 if (!$menu_use_color) {
405 return "[$prefix]$remainder";
408 return "$prompt_color$prefix$normal_color$remainder";
412 print STDERR colored
$error_color, @_;
415 sub list_and_choose
{
416 my ($opts, @stuff) = @_;
417 my (@chosen, @return);
419 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
425 if ($opts->{HEADER
}) {
426 if (!$opts->{LIST_FLAT
}) {
429 print colored
$header_color, "$opts->{HEADER}\n";
431 for ($i = 0; $i < @stuff; $i++) {
432 my $chosen = $chosen[$i] ?
'*' : ' ';
433 my $print = $stuff[$i];
434 my $ref = ref $print;
435 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
437 if ($ref eq 'ARRAY') {
438 $print = $highlighted || $print->[0];
440 elsif ($ref eq 'HASH') {
441 my $value = $highlighted || $print->{VALUE
};
442 $print = sprintf($status_fmt,
448 $print = $highlighted || $print;
450 printf("%s%2d: %s", $chosen, $i+1, $print);
451 if (($opts->{LIST_FLAT
}) &&
452 (($i + 1) % ($opts->{LIST_FLAT
}))) {
465 return if ($opts->{LIST_ONLY
});
467 print colored
$prompt_color, $opts->{PROMPT
};
468 if ($opts->{SINGLETON
}) {
477 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
484 singleton_prompt_help_cmd
() :
488 for my $choice (split(/[\s,]+/, $line)) {
492 # Input that begins with '-'; unchoose
493 if ($choice =~ s/^-//) {
496 # A range can be specified like 5-7 or 5-.
497 if ($choice =~ /^(\d+)-(\d*)$/) {
498 ($bottom, $top) = ($1, length($2) ?
$2 : 1 + @stuff);
500 elsif ($choice =~ /^\d+$/) {
501 $bottom = $top = $choice;
503 elsif ($choice eq '*') {
508 $bottom = $top = find_unique
($choice, @stuff);
509 if (!defined $bottom) {
510 error_msg
"Huh ($choice)?\n";
514 if ($opts->{SINGLETON
} && $bottom != $top) {
515 error_msg
"Huh ($choice)?\n";
518 for ($i = $bottom-1; $i <= $top-1; $i++) {
519 next if (@stuff <= $i || $i < 0);
520 $chosen[$i] = $choose;
523 last if ($opts->{IMMEDIATE
} || $line eq '*');
525 for ($i = 0; $i < @stuff; $i++) {
527 push @return, $stuff[$i];
533 sub singleton_prompt_help_cmd
{
534 print colored
$help_color, <<\EOF
;
536 1 - select a numbered item
537 foo
- select item based on unique prefix
538 - (empty
) select nothing
542 sub prompt_help_cmd
{
543 print colored
$help_color, <<\EOF
;
545 1 - select a single item
546 3-5 - select a range of items
547 2-3,6-9 - select multiple ranges
548 foo
- select item based on unique prefix
549 -... - unselect specified items
551 - (empty
) finish selecting
556 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
566 print "$cnt paths\n";
574 my @mods = list_modified
('file-only');
577 my @update = list_and_choose
({ PROMPT
=> 'Update',
578 HEADER
=> $status_head, },
581 system(qw(git update-index --add --remove --),
582 map { $_->{VALUE
} } @update);
583 say_n_paths
('updated', @update);
589 my @update = list_and_choose
({ PROMPT
=> 'Revert',
590 HEADER
=> $status_head, },
593 if (is_initial_commit
()) {
594 system(qw(git rm --cached),
595 map { $_->{VALUE
} } @update);
598 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
599 map { $_->{VALUE
} } @update);
601 open $fh, '| git update-index --index-info'
608 if ($_->{INDEX_ADDDEL
} &&
609 $_->{INDEX_ADDDEL
} eq 'create') {
610 system(qw(git update-index --force-remove --),
612 print "note: $_->{VALUE} is untracked now.\n";
617 say_n_paths
('reverted', @update);
622 sub add_untracked_cmd
{
623 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
626 system(qw(git update-index --add --), @add);
627 say_n_paths
('added', @add);
635 open $fh, '| git ' . $cmd;
642 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF
});
643 my @diff = run_cmd_pipe
("git", @diff_cmd, "--", $path);
645 if ($diff_use_color) {
646 @colored = run_cmd_pipe
("git", @diff_cmd, qw(--color --), $path);
648 my (@hunk) = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
650 for (my $i = 0; $i < @diff; $i++) {
651 if ($diff[$i] =~ /^@@ /) {
652 push @hunk, { TEXT
=> [], DISPLAY
=> [],
655 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
656 push @
{$hunk[-1]{DISPLAY
}},
657 ($diff_use_color ?
$colored[$i] : $diff[$i]);
662 sub parse_diff_header
{
665 my $head = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'header' };
666 my $mode = { TEXT
=> [], DISPLAY
=> [], TYPE
=> 'mode' };
668 for (my $i = 0; $i < @
{$src->{TEXT
}}; $i++) {
669 my $dest = $src->{TEXT
}->[$i] =~ /^(old|new) mode (\d+)$/ ?
671 push @
{$dest->{TEXT
}}, $src->{TEXT
}->[$i];
672 push @
{$dest->{DISPLAY
}}, $src->{DISPLAY
}->[$i];
674 return ($head, $mode);
677 sub hunk_splittable
{
680 my @s = split_hunk
($text);
684 sub parse_hunk_header
{
686 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
687 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
688 $o_cnt = 1 unless defined $o_cnt;
689 $n_cnt = 1 unless defined $n_cnt;
690 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
694 my ($text, $display) = @_;
696 if (!defined $display) {
699 # If there are context lines in the middle of a hunk,
700 # it can be split, but we would need to take care of
703 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
708 my $next_hunk_start = undef;
709 my $i = $hunk_start - 1;
723 while (++$i < @
$text) {
724 my $line = $text->[$i];
725 my $display = $display->[$i];
727 if ($this->{ADDDEL
} &&
728 !defined $next_hunk_start) {
729 # We have seen leading context and
730 # adds/dels and then here is another
731 # context, which is trailing for this
732 # split hunk and leading for the next
734 $next_hunk_start = $i;
736 push @
{$this->{TEXT
}}, $line;
737 push @
{$this->{DISPLAY
}}, $display;
740 if (defined $next_hunk_start) {
747 if (defined $next_hunk_start) {
748 # We are done with the current hunk and
749 # this is the first real change for the
751 $hunk_start = $next_hunk_start;
752 $o_ofs = $this->{OLD
} + $this->{OCNT
};
753 $n_ofs = $this->{NEW
} + $this->{NCNT
};
754 $o_ofs -= $this->{POSTCTX
};
755 $n_ofs -= $this->{POSTCTX
};
759 push @
{$this->{TEXT
}}, $line;
760 push @
{$this->{DISPLAY
}}, $display;
774 for my $hunk (@split) {
775 $o_ofs = $hunk->{OLD
};
776 $n_ofs = $hunk->{NEW
};
777 my $o_cnt = $hunk->{OCNT
};
778 my $n_cnt = $hunk->{NCNT
};
780 my $head = ("@@ -$o_ofs" .
781 (($o_cnt != 1) ?
",$o_cnt" : '') .
783 (($n_cnt != 1) ?
",$n_cnt" : '') .
785 my $display_head = $head;
786 unshift @
{$hunk->{TEXT
}}, $head;
787 if ($diff_use_color) {
788 $display_head = colored
($fraginfo_color, $head);
790 unshift @
{$hunk->{DISPLAY
}}, $display_head;
795 sub find_last_o_ctx
{
797 my $text = $it->{TEXT
};
798 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
800 my $last_o_ctx = $o_ofs + $o_cnt;
802 my $line = $text->[$i];
813 my ($prev, $this) = @_;
814 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
815 parse_hunk_header
($prev->{TEXT
}[0]);
816 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
817 parse_hunk_header
($this->{TEXT
}[0]);
819 my (@line, $i, $ofs, $o_cnt, $n_cnt);
822 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
823 my $line = $prev->{TEXT
}[$i];
824 if ($line =~ /^\+/) {
830 last if ($o1_ofs <= $ofs);
840 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
841 my $line = $this->{TEXT
}[$i];
842 if ($line =~ /^\+/) {
854 my $head = ("@@ -$o0_ofs" .
855 (($o_cnt != 1) ?
",$o_cnt" : '') .
857 (($n_cnt != 1) ?
",$n_cnt" : '') .
859 @
{$prev->{TEXT
}} = ($head, @line);
862 sub coalesce_overlapping_hunks
{
866 my ($last_o_ctx, $last_was_dirty);
868 for (grep { $_->{USE
} } @in) {
869 my $text = $_->{TEXT
};
870 my ($o_ofs) = parse_hunk_header
($text->[0]);
871 if (defined $last_o_ctx &&
872 $o_ofs <= $last_o_ctx &&
875 merge_hunk
($out[-1], $_);
880 $last_o_ctx = find_last_o_ctx
($out[-1]);
881 $last_was_dirty = $_->{DIRTY
};
888 colored
((/^@/ ?
$fraginfo_color :
889 /^\+/ ?
$diff_new_color :
890 /^-/ ?
$diff_old_color :
896 sub edit_hunk_manually
{
899 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
901 open $fh, '>', $hunkfile
902 or die "failed to open hunk edit file for writing: " . $!;
903 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
905 my $participle = $patch_mode_flavour{PARTICIPLE
};
908 # To remove '-' lines, make them ' ' lines (context).
909 # To remove '+' lines, delete them.
910 # Lines starting with # will be removed.
912 # If the patch applies cleanly, the edited hunk will immediately be
913 # marked for $participle. If it does not apply cleanly, you will be given
914 # an opportunity to edit again. If all lines of the hunk are removed,
915 # then the edit is aborted and the hunk is left unchanged.
919 my $editor = $ENV{GIT_EDITOR
} || $repo->config("core.editor")
920 || $ENV{VISUAL
} || $ENV{EDITOR
} || "vi";
921 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
927 open $fh, '<', $hunkfile
928 or die "failed to open hunk edit file for reading: " . $!;
929 my @newtext = grep { !/^#/ } <$fh>;
933 # Abort if nothing remains
934 if (!grep { /\S/ } @newtext) {
938 # Reinsert the first hunk header if the user accidentally deleted it
939 if ($newtext[0] !~ /^@/) {
940 unshift @newtext, $oldtext->[0];
947 return run_git_apply
($patch_mode_flavour{APPLY_CHECK
} . ' --recount --check',
948 map { @
{$_->{TEXT
}} } @_);
951 sub _restore_terminal_and_die
{
957 sub prompt_single_character
{
959 local $SIG{TERM
} = \
&_restore_terminal_and_die
;
960 local $SIG{INT
} = \
&_restore_terminal_and_die
;
964 print "$key" if defined $key;
975 print colored
$prompt_color, $prompt;
976 my $line = prompt_single_character
;
977 return 0 if $line =~ /^n/i;
978 return 1 if $line =~ /^y/i;
983 my ($head, $hunk, $ix) = @_;
984 my $text = $hunk->[$ix]->{TEXT
};
987 $text = edit_hunk_manually
($text);
988 if (!defined $text) {
993 TYPE
=> $hunk->[$ix]->{TYPE
},
997 if (diff_applies
($head,
1000 @
{$hunk}[$ix+1..$#{$hunk}])) {
1001 $newhunk->{DISPLAY
} = [color_diff
(@
{$text})];
1006 'Your edited hunk does not apply. Edit again '
1007 . '(saying "no" discards!) [y/n]? '
1013 sub help_patch_cmd
{
1014 my $verb = lc $patch_mode_flavour{VERB
};
1015 my $target = $patch_mode_flavour{TARGET
};
1016 print colored
$help_color, <<EOF ;
1017 y - $verb this hunk$target
1018 n - do not $verb this hunk$target
1019 q - quit, do not $verb this hunk nor any of the remaining ones
1020 a - $verb this and all the remaining hunks in the file
1021 d - do not $verb this hunk nor any of the remaining hunks in the file
1022 g - select a hunk to go to
1023 / - search for a hunk matching the given regex
1024 j - leave this hunk undecided, see next undecided hunk
1025 J - leave this hunk undecided, see next hunk
1026 k - leave this hunk undecided, see previous undecided hunk
1027 K - leave this hunk undecided, see previous hunk
1028 s - split the current hunk into smaller hunks
1029 e - manually edit the current hunk
1036 my $ret = run_git_apply
$cmd . ' --recount', @_;
1043 sub patch_update_cmd
{
1044 my @all_mods = list_modified
($patch_mode_flavour{FILTER
});
1045 my @mods = grep { !($_->{BINARY
}) } @all_mods;
1050 print STDERR
"Only binary files changed.\n";
1052 print STDERR
"No changes.\n";
1060 @them = list_and_choose
({ PROMPT
=> 'Patch update',
1061 HEADER
=> $status_head, },
1065 return 0 if patch_update_file
($_->{VALUE
});
1069 # Generate a one line summary of a hunk.
1070 sub summarize_hunk
{
1072 my $summary = $rhunk->{TEXT
}[0];
1074 # Keep the line numbers, discard extra context.
1075 $summary =~ s/@@(.*?)@@.*/$1 /s;
1076 $summary .= " " x
(20 - length $summary);
1078 # Add some user context.
1079 for my $line (@
{$rhunk->{TEXT
}}) {
1080 if ($line =~ m/^[+-].*\w/) {
1087 return substr($summary, 0, 80) . "\n";
1091 # Print a one-line summary of each hunk in the array ref in
1092 # the first argument, starting wih the index in the 2nd.
1094 my ($hunks, $i) = @_;
1097 for (; $i < @
$hunks && $ctr < 20; $i++, $ctr++) {
1099 if (defined $hunks->[$i]{USE
}) {
1100 $status = $hunks->[$i]{USE
} ?
"+" : "-";
1105 summarize_hunk
($hunks->[$i]);
1110 sub patch_update_file
{
1114 my ($head, @hunk) = parse_diff
($path);
1115 ($head, my $mode) = parse_diff_header
($head);
1116 for (@
{$head->{DISPLAY
}}) {
1120 if (@
{$mode->{TEXT
}}) {
1121 unshift @hunk, $mode;
1124 $num = scalar @hunk;
1128 my ($prev, $next, $other, $undecided, $i);
1134 for ($i = 0; $i < $ix; $i++) {
1135 if (!defined $hunk[$i]{USE
}) {
1144 for ($i = $ix + 1; $i < $num; $i++) {
1145 if (!defined $hunk[$i]{USE
}) {
1151 if ($ix < $num - 1) {
1157 for ($i = 0; $i < $num; $i++) {
1158 if (!defined $hunk[$i]{USE
}) {
1163 last if (!$undecided);
1165 if ($hunk[$ix]{TYPE
} eq 'hunk' &&
1166 hunk_splittable
($hunk[$ix]{TEXT
})) {
1169 if ($hunk[$ix]{TYPE
} eq 'hunk') {
1172 for (@
{$hunk[$ix]{DISPLAY
}}) {
1175 print colored
$prompt_color, $patch_mode_flavour{VERB
},
1176 ($hunk[$ix]{TYPE
} eq 'mode' ?
' mode change' : ' this hunk'),
1177 $patch_mode_flavour{TARGET
},
1178 " [y,n,q,a,d,/$other,?]? ";
1179 my $line = prompt_single_character
;
1181 if ($line =~ /^y/i) {
1182 $hunk[$ix]{USE
} = 1;
1184 elsif ($line =~ /^n/i) {
1185 $hunk[$ix]{USE
} = 0;
1187 elsif ($line =~ /^a/i) {
1188 while ($ix < $num) {
1189 if (!defined $hunk[$ix]{USE
}) {
1190 $hunk[$ix]{USE
} = 1;
1196 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1198 my $no = $ix > 10 ?
$ix - 10 : 0;
1199 while ($response eq '') {
1201 $no = display_hunks
(\
@hunk, $no);
1203 $extra = " (<ret> to see more)";
1205 print "go to which hunk$extra? ";
1206 $response = <STDIN
>;
1207 if (!defined $response) {
1212 if ($response !~ /^\s*\d+\s*$/) {
1213 error_msg
"Invalid number: '$response'\n";
1214 } elsif (0 < $response && $response <= $num) {
1215 $ix = $response - 1;
1217 error_msg
"Sorry, only $num hunks available.\n";
1221 elsif ($line =~ /^d/i) {
1222 while ($ix < $num) {
1223 if (!defined $hunk[$ix]{USE
}) {
1224 $hunk[$ix]{USE
} = 0;
1230 elsif ($line =~ /^q/i) {
1231 while ($ix < $num) {
1232 if (!defined $hunk[$ix]{USE
}) {
1233 $hunk[$ix]{USE
} = 0;
1240 elsif ($line =~ m
|^/(.*)|) {
1243 print colored
$prompt_color, "search for regex? ";
1245 if (defined $regex) {
1251 $search_string = qr{$regex}m;
1254 my ($err,$exp) = ($@
, $1);
1255 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1256 error_msg
"Malformed search regexp $exp: $err\n";
1261 my $text = join ("", @
{$hunk[$iy]{TEXT
}});
1262 last if ($text =~ $search_string);
1264 $iy = 0 if ($iy >= $num);
1266 error_msg
"No hunk matches the given pattern\n";
1273 elsif ($line =~ /^K/) {
1274 if ($other =~ /K/) {
1278 error_msg
"No previous hunk\n";
1282 elsif ($line =~ /^J/) {
1283 if ($other =~ /J/) {
1287 error_msg
"No next hunk\n";
1291 elsif ($line =~ /^k/) {
1292 if ($other =~ /k/) {
1296 !defined $hunk[$ix]{USE
});
1300 error_msg
"No previous hunk\n";
1304 elsif ($line =~ /^j/) {
1305 if ($other !~ /j/) {
1306 error_msg
"No next hunk\n";
1310 elsif ($other =~ /s/ && $line =~ /^s/) {
1311 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
1313 print colored
$header_color, "Split into ",
1314 scalar(@split), " hunks.\n";
1316 splice (@hunk, $ix, 1, @split);
1317 $num = scalar @hunk;
1320 elsif ($other =~ /e/ && $line =~ /^e/) {
1321 my $newhunk = edit_hunk_loop
($head, \
@hunk, $ix);
1322 if (defined $newhunk) {
1323 splice @hunk, $ix, 1, $newhunk;
1327 help_patch_cmd
($other);
1333 last if ($ix >= $num ||
1334 !defined $hunk[$ix]{USE
});
1339 @hunk = coalesce_overlapping_hunks
(@hunk);
1345 push @result, @
{$_->{TEXT
}};
1351 my @patch = (@
{$head->{TEXT
}}, @result);
1352 my $apply_routine = $patch_mode_flavour{APPLY
};
1353 &$apply_routine(@patch);
1362 my @mods = list_modified
('index-only');
1363 @mods = grep { !($_->{BINARY
}) } @mods;
1365 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
1367 HEADER
=> $status_head, },
1370 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
1371 system(qw(git diff -p --cached), $reference, '--',
1372 map { $_->{VALUE
} } @them);
1381 print colored
$help_color, <<\EOF
;
1382 status
- show paths with changes
1383 update
- add working tree
state to the staged set of changes
1384 revert
- revert staged set of changes back to the HEAD version
1385 patch
- pick hunks
and update selectively
1386 diff
- view diff between HEAD
and index
1387 add untracked
- add contents of untracked files to the staged set of changes
1392 return unless @ARGV;
1393 my $arg = shift @ARGV;
1394 if ($arg eq "--patch") {
1396 $arg = shift @ARGV or die "missing --";
1397 die "invalid argument $arg, expecting --"
1398 unless $arg eq "--";
1400 elsif ($arg ne "--") {
1401 die "invalid argument $arg, expecting --";
1406 my @cmd = ([ 'status', \
&status_cmd
, ],
1407 [ 'update', \
&update_cmd
, ],
1408 [ 'revert', \
&revert_cmd
, ],
1409 [ 'add untracked', \
&add_untracked_cmd
, ],
1410 [ 'patch', \
&patch_update_cmd
, ],
1411 [ 'diff', \
&diff_cmd
, ],
1412 [ 'quit', \
&quit_cmd
, ],
1413 [ 'help', \
&help_cmd
, ],
1416 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1419 HEADER
=> '*** Commands ***',
1420 ON_EOF
=> \
&quit_cmd
,
1421 IMMEDIATE
=> 1 }, @cmd);