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
77 if ($^O
eq 'MSWin32' || $^O
eq 'msys') {
78 my @invalid = grep {m/[":*]/} @_;
79 die "$^O does not support: @invalid\n" if @invalid;
80 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
84 open($fh, '-|', @_) or die;
89 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
91 if (!defined $GIT_DIR) {
92 exit(1); # rev-parse would have already said "not a git repo"
109 my ($retval, $remainder);
110 if (!/^\042(.*)\042$/) {
113 ($_, $retval) = ($1, "");
114 while (/^([^\\]*)\\(.*)$/) {
118 if (/^([0-3][0-7][0-7])(.*)$/) {
119 $retval .= chr(oct($1));
123 if (/^([\\\042btnvfr])(.*)$/) {
124 $retval .= $cquote_map{$1};
128 # This is malformed -- just return it as-is for now.
139 open $fh, 'git update-index --refresh |'
142 ;# ignore 'needs update'
152 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
155 my $status_fmt = '%12s %12s %s';
156 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
160 sub is_initial_commit
{
161 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
162 unless defined $initial;
168 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
171 # Returns list of hashes, contents of each of which are:
173 # BINARY: is a binary path
174 # INDEX: is index different from HEAD?
175 # FILE: is file different from index?
176 # INDEX_ADDDEL: is it add/delete between HEAD and index?
177 # FILE_ADDDEL: is it add/delete between index and file?
182 my ($add, $del, $adddel, $file);
189 } run_cmd_pipe
(qw(git ls-files --exclude-standard --), @ARGV);
190 return if (!@tracked);
193 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
194 for (run_cmd_pipe
(qw(git diff-index --cached
195 --numstat --summary), $reference,
197 if (($add, $del, $file) =
198 /^([-\d]+) ([-\d]+) (.*)/) {
200 $file = unquote_path
($file);
201 if ($add eq '-' && $del eq '-') {
206 $change = "+$add/-$del";
214 elsif (($adddel, $file) =
215 /^ (create|delete) mode [0-7]+ (.*)$/) {
216 $file = unquote_path
($file);
217 $data{$file}{INDEX_ADDDEL
} = $adddel;
221 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
222 if (($add, $del, $file) =
223 /^([-\d]+) ([-\d]+) (.*)/) {
224 $file = unquote_path
($file);
225 if (!exists $data{$file}) {
227 INDEX
=> 'unchanged',
232 if ($add eq '-' && $del eq '-') {
237 $change = "+$add/-$del";
239 $data{$file}{FILE
} = $change;
241 $data{$file}{BINARY
} = 1;
244 elsif (($adddel, $file) =
245 /^ (create|delete) mode [0-7]+ (.*)$/) {
246 $file = unquote_path
($file);
247 $data{$file}{FILE_ADDDEL
} = $adddel;
251 for (sort keys %data) {
255 if ($only eq 'index-only') {
256 next if ($it->{INDEX
} eq 'unchanged');
258 if ($only eq 'file-only') {
259 next if ($it->{FILE
} eq 'nothing');
271 my ($string, @stuff) = @_;
273 for (my $i = 0; $i < @stuff; $i++) {
277 if ((ref $it) eq 'ARRAY') {
285 if ($it =~ /^$string/) {
289 if (defined $hit && defined $found) {
299 # inserts string into trie and updates count for each character
301 my ($trie, $string) = @_;
302 foreach (split //, $string) {
303 $trie = $trie->{$_} ||= {COUNT
=> 0};
308 # returns an array of tuples (prefix, remainder)
309 sub find_unique_prefixes
{
313 # any single prefix exceeding the soft limit is omitted
314 # if any prefix exceeds the hard limit all are omitted
315 # 0 indicates no limit
319 # build a trie modelling all possible options
321 foreach my $print (@stuff) {
322 if ((ref $print) eq 'ARRAY') {
323 $print = $print->[0];
325 elsif ((ref $print) eq 'HASH') {
326 $print = $print->{VALUE
};
328 update_trie
(\
%trie, $print);
329 push @return, $print;
332 # use the trie to find the unique prefixes
333 for (my $i = 0; $i < @return; $i++) {
334 my $ret = $return[$i];
335 my @letters = split //, $ret;
337 my ($prefix, $remainder);
339 for ($j = 0; $j < @letters; $j++) {
340 my $letter = $letters[$j];
341 if ($search{$letter}{COUNT
} == 1) {
342 $prefix = substr $ret, 0, $j + 1;
343 $remainder = substr $ret, $j + 1;
347 my $prefix = substr $ret, 0, $j;
349 if ($hard_limit && $j + 1 > $hard_limit);
351 %search = %{$search{$letter}};
353 if (ord($letters[0]) > 127 ||
354 ($soft_limit && $j + 1 > $soft_limit)) {
358 $return[$i] = [$prefix, $remainder];
363 # filters out prefixes which have special meaning to list_and_choose()
364 sub is_valid_prefix
{
366 return (defined $prefix) &&
367 !($prefix =~ /[\s,]/) && # separators
368 !($prefix =~ /^-/) && # deselection
369 !($prefix =~ /^\d+/) && # selection
370 ($prefix ne '*') && # "all" wildcard
371 ($prefix ne '?'); # prompt help
374 # given a prefix/remainder tuple return a string with the prefix highlighted
375 # for now use square brackets; later might use ANSI colors (underline, bold)
376 sub highlight_prefix
{
378 my $remainder = shift;
380 if (!defined $prefix) {
384 if (!is_valid_prefix
($prefix)) {
385 return "$prefix$remainder";
388 if (!$menu_use_color) {
389 return "[$prefix]$remainder";
392 return "$prompt_color$prefix$normal_color$remainder";
396 print STDERR colored
$error_color, @_;
399 sub list_and_choose
{
400 my ($opts, @stuff) = @_;
401 my (@chosen, @return);
403 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
409 if ($opts->{HEADER
}) {
410 if (!$opts->{LIST_FLAT
}) {
413 print colored
$header_color, "$opts->{HEADER}\n";
415 for ($i = 0; $i < @stuff; $i++) {
416 my $chosen = $chosen[$i] ?
'*' : ' ';
417 my $print = $stuff[$i];
418 my $ref = ref $print;
419 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
421 if ($ref eq 'ARRAY') {
422 $print = $highlighted || $print->[0];
424 elsif ($ref eq 'HASH') {
425 my $value = $highlighted || $print->{VALUE
};
426 $print = sprintf($status_fmt,
432 $print = $highlighted || $print;
434 printf("%s%2d: %s", $chosen, $i+1, $print);
435 if (($opts->{LIST_FLAT
}) &&
436 (($i + 1) % ($opts->{LIST_FLAT
}))) {
449 return if ($opts->{LIST_ONLY
});
451 print colored
$prompt_color, $opts->{PROMPT
};
452 if ($opts->{SINGLETON
}) {
461 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
468 singleton_prompt_help_cmd
() :
472 for my $choice (split(/[\s,]+/, $line)) {
476 # Input that begins with '-'; unchoose
477 if ($choice =~ s/^-//) {
480 # A range can be specified like 5-7 or 5-.
481 if ($choice =~ /^(\d+)-(\d*)$/) {
482 ($bottom, $top) = ($1, length($2) ?
$2 : 1 + @stuff);
484 elsif ($choice =~ /^\d+$/) {
485 $bottom = $top = $choice;
487 elsif ($choice eq '*') {
492 $bottom = $top = find_unique
($choice, @stuff);
493 if (!defined $bottom) {
494 error_msg
"Huh ($choice)?\n";
498 if ($opts->{SINGLETON
} && $bottom != $top) {
499 error_msg
"Huh ($choice)?\n";
502 for ($i = $bottom-1; $i <= $top-1; $i++) {
503 next if (@stuff <= $i || $i < 0);
504 $chosen[$i] = $choose;
507 last if ($opts->{IMMEDIATE
} || $line eq '*');
509 for ($i = 0; $i < @stuff; $i++) {
511 push @return, $stuff[$i];
517 sub singleton_prompt_help_cmd
{
518 print colored
$help_color, <<\EOF
;
520 1 - select a numbered item
521 foo
- select item based on unique prefix
522 - (empty
) select nothing
526 sub prompt_help_cmd
{
527 print colored
$help_color, <<\EOF
;
529 1 - select a single item
530 3-5 - select a range of items
531 2-3,6-9 - select multiple ranges
532 foo
- select item based on unique prefix
533 -... - unselect specified items
535 - (empty
) finish selecting
540 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
550 print "$cnt paths\n";
558 my @mods = list_modified
('file-only');
561 my @update = list_and_choose
({ PROMPT
=> 'Update',
562 HEADER
=> $status_head, },
565 system(qw(git update-index --add --remove --),
566 map { $_->{VALUE
} } @update);
567 say_n_paths
('updated', @update);
573 my @update = list_and_choose
({ PROMPT
=> 'Revert',
574 HEADER
=> $status_head, },
577 if (is_initial_commit
()) {
578 system(qw(git rm --cached),
579 map { $_->{VALUE
} } @update);
582 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
583 map { $_->{VALUE
} } @update);
585 open $fh, '| git update-index --index-info'
592 if ($_->{INDEX_ADDDEL
} &&
593 $_->{INDEX_ADDDEL
} eq 'create') {
594 system(qw(git update-index --force-remove --),
596 print "note: $_->{VALUE} is untracked now.\n";
601 say_n_paths
('reverted', @update);
606 sub add_untracked_cmd
{
607 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
610 system(qw(git update-index --add --), @add);
611 say_n_paths
('added', @add);
618 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
620 if ($diff_use_color) {
621 @colored = run_cmd_pipe
(qw(git diff-files -p --color --), $path);
623 my (@hunk) = { TEXT
=> [], DISPLAY
=> [] };
625 for (my $i = 0; $i < @diff; $i++) {
626 if ($diff[$i] =~ /^@@ /) {
627 push @hunk, { TEXT
=> [], DISPLAY
=> [] };
629 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
630 push @
{$hunk[-1]{DISPLAY
}},
631 ($diff_use_color ?
$colored[$i] : $diff[$i]);
636 sub parse_diff_header
{
639 my $head = { TEXT
=> [], DISPLAY
=> [] };
640 my $mode = { TEXT
=> [], DISPLAY
=> [] };
642 for (my $i = 0; $i < @
{$src->{TEXT
}}; $i++) {
643 my $dest = $src->{TEXT
}->[$i] =~ /^(old|new) mode (\d+)$/ ?
645 push @
{$dest->{TEXT
}}, $src->{TEXT
}->[$i];
646 push @
{$dest->{DISPLAY
}}, $src->{DISPLAY
}->[$i];
648 return ($head, $mode);
651 sub hunk_splittable
{
654 my @s = split_hunk
($text);
658 sub parse_hunk_header
{
660 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
661 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
662 $o_cnt = 1 unless defined $o_cnt;
663 $n_cnt = 1 unless defined $n_cnt;
664 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
668 my ($text, $display) = @_;
670 if (!defined $display) {
673 # If there are context lines in the middle of a hunk,
674 # it can be split, but we would need to take care of
677 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
682 my $next_hunk_start = undef;
683 my $i = $hunk_start - 1;
696 while (++$i < @
$text) {
697 my $line = $text->[$i];
698 my $display = $display->[$i];
700 if ($this->{ADDDEL
} &&
701 !defined $next_hunk_start) {
702 # We have seen leading context and
703 # adds/dels and then here is another
704 # context, which is trailing for this
705 # split hunk and leading for the next
707 $next_hunk_start = $i;
709 push @
{$this->{TEXT
}}, $line;
710 push @
{$this->{DISPLAY
}}, $display;
713 if (defined $next_hunk_start) {
720 if (defined $next_hunk_start) {
721 # We are done with the current hunk and
722 # this is the first real change for the
724 $hunk_start = $next_hunk_start;
725 $o_ofs = $this->{OLD
} + $this->{OCNT
};
726 $n_ofs = $this->{NEW
} + $this->{NCNT
};
727 $o_ofs -= $this->{POSTCTX
};
728 $n_ofs -= $this->{POSTCTX
};
732 push @
{$this->{TEXT
}}, $line;
733 push @
{$this->{DISPLAY
}}, $display;
747 for my $hunk (@split) {
748 $o_ofs = $hunk->{OLD
};
749 $n_ofs = $hunk->{NEW
};
750 my $o_cnt = $hunk->{OCNT
};
751 my $n_cnt = $hunk->{NCNT
};
753 my $head = ("@@ -$o_ofs" .
754 (($o_cnt != 1) ?
",$o_cnt" : '') .
756 (($n_cnt != 1) ?
",$n_cnt" : '') .
758 my $display_head = $head;
759 unshift @
{$hunk->{TEXT
}}, $head;
760 if ($diff_use_color) {
761 $display_head = colored
($fraginfo_color, $head);
763 unshift @
{$hunk->{DISPLAY
}}, $display_head;
771 colored
((/^@/ ?
$fraginfo_color :
772 /^\+/ ?
$diff_new_color :
773 /^-/ ?
$diff_old_color :
779 sub edit_hunk_manually
{
782 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
784 open $fh, '>', $hunkfile
785 or die "failed to open hunk edit file for writing: " . $!;
786 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
790 # To remove '-' lines, make them ' ' lines (context).
791 # To remove '+' lines, delete them.
792 # Lines starting with # will be removed.
794 # If the patch applies cleanly, the edited hunk will immediately be
795 # marked for staging. If it does not apply cleanly, you will be given
796 # an opportunity to edit again. If all lines of the hunk are removed,
797 # then the edit is aborted and the hunk is left unchanged.
801 my $editor = $ENV{GIT_EDITOR
} || $repo->config("core.editor")
802 || $ENV{VISUAL
} || $ENV{EDITOR
} || "vi";
803 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
809 open $fh, '<', $hunkfile
810 or die "failed to open hunk edit file for reading: " . $!;
811 my @newtext = grep { !/^#/ } <$fh>;
815 # Abort if nothing remains
816 if (!grep { /\S/ } @newtext) {
820 # Reinsert the first hunk header if the user accidentally deleted it
821 if ($newtext[0] !~ /^@/) {
822 unshift @newtext, $oldtext->[0];
829 open $fh, '| git apply --recount --cached --check';
831 print $fh @
{$h->{TEXT
}};
836 sub _restore_terminal_and_die
{
842 sub prompt_single_character
{
844 local $SIG{TERM
} = \
&_restore_terminal_and_die
;
845 local $SIG{INT
} = \
&_restore_terminal_and_die
;
849 print "$key" if defined $key;
860 print colored
$prompt_color, $prompt;
861 my $line = prompt_single_character
;
862 return 0 if $line =~ /^n/i;
863 return 1 if $line =~ /^y/i;
868 my ($head, $hunk, $ix) = @_;
869 my $text = $hunk->[$ix]->{TEXT
};
872 $text = edit_hunk_manually
($text);
873 if (!defined $text) {
876 my $newhunk = { TEXT
=> $text, USE
=> 1 };
877 if (diff_applies
($head,
880 @
{$hunk}[$ix+1..$#{$hunk}])) {
881 $newhunk->{DISPLAY
} = [color_diff
(@
{$text})];
886 'Your edited hunk does not apply. Edit again '
887 . '(saying "no" discards!) [y/n]? '
894 print colored
$help_color, <<\EOF
;
896 n
- do not stage this hunk
897 a
- stage this
and all the remaining hunks
in the file
898 d
- do not stage this hunk nor any of the remaining hunks
in the file
899 g
- select a hunk to go to
900 / - search
for a hunk matching the
given regex
901 j
- leave this hunk undecided
, see
next undecided hunk
902 J
- leave this hunk undecided
, see
next hunk
903 k
- leave this hunk undecided
, see previous undecided hunk
904 K
- leave this hunk undecided
, see previous hunk
905 s
- split the current hunk into smaller hunks
906 e
- manually edit the current hunk
911 sub patch_update_cmd
{
912 my @all_mods = list_modified
('file-only');
913 my @mods = grep { !($_->{BINARY
}) } @all_mods;
918 print STDERR
"Only binary files changed.\n";
920 print STDERR
"No changes.\n";
928 @them = list_and_choose
({ PROMPT
=> 'Patch update',
929 HEADER
=> $status_head, },
933 patch_update_file
($_->{VALUE
});
937 # Generate a one line summary of a hunk.
940 my $summary = $rhunk->{TEXT
}[0];
942 # Keep the line numbers, discard extra context.
943 $summary =~ s/@@(.*?)@@.*/$1 /s;
944 $summary .= " " x
(20 - length $summary);
946 # Add some user context.
947 for my $line (@
{$rhunk->{TEXT
}}) {
948 if ($line =~ m/^[+-].*\w/) {
955 return substr($summary, 0, 80) . "\n";
959 # Print a one-line summary of each hunk in the array ref in
960 # the first argument, starting wih the index in the 2nd.
962 my ($hunks, $i) = @_;
965 for (; $i < @
$hunks && $ctr < 20; $i++, $ctr++) {
967 if (defined $hunks->[$i]{USE
}) {
968 $status = $hunks->[$i]{USE
} ?
"+" : "-";
973 summarize_hunk
($hunks->[$i]);
978 sub patch_update_file
{
981 my ($head, @hunk) = parse_diff
($path);
982 ($head, my $mode) = parse_diff_header
($head);
983 for (@
{$head->{DISPLAY
}}) {
987 if (@
{$mode->{TEXT
}}) {
989 print @
{$mode->{DISPLAY
}};
990 print colored
$prompt_color,
991 "Stage mode change [y/n/a/d/?]? ";
992 my $line = prompt_single_character
;
993 if ($line =~ /^y/i) {
997 elsif ($line =~ /^n/i) {
1001 elsif ($line =~ /^a/i) {
1002 $_->{USE
} = 1 foreach ($mode, @hunk);
1005 elsif ($line =~ /^d/i) {
1006 $_->{USE
} = 0 foreach ($mode, @hunk);
1016 $num = scalar @hunk;
1020 my ($prev, $next, $other, $undecided, $i);
1026 for ($i = 0; $i < $ix; $i++) {
1027 if (!defined $hunk[$i]{USE
}) {
1036 for ($i = $ix + 1; $i < $num; $i++) {
1037 if (!defined $hunk[$i]{USE
}) {
1043 if ($ix < $num - 1) {
1049 for ($i = 0; $i < $num; $i++) {
1050 if (!defined $hunk[$i]{USE
}) {
1055 last if (!$undecided);
1057 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
1061 for (@
{$hunk[$ix]{DISPLAY
}}) {
1064 print colored
$prompt_color, "Stage this hunk [y,n,a,d,/$other,?]? ";
1065 my $line = prompt_single_character
;
1067 if ($line =~ /^y/i) {
1068 $hunk[$ix]{USE
} = 1;
1070 elsif ($line =~ /^n/i) {
1071 $hunk[$ix]{USE
} = 0;
1073 elsif ($line =~ /^a/i) {
1074 while ($ix < $num) {
1075 if (!defined $hunk[$ix]{USE
}) {
1076 $hunk[$ix]{USE
} = 1;
1082 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1084 my $no = $ix > 10 ?
$ix - 10 : 0;
1085 while ($response eq '') {
1087 $no = display_hunks
(\
@hunk, $no);
1089 $extra = " (<ret> to see more)";
1091 print "go to which hunk$extra? ";
1092 $response = <STDIN
>;
1093 if (!defined $response) {
1098 if ($response !~ /^\s*\d+\s*$/) {
1099 error_msg
"Invalid number: '$response'\n";
1100 } elsif (0 < $response && $response <= $num) {
1101 $ix = $response - 1;
1103 error_msg
"Sorry, only $num hunks available.\n";
1107 elsif ($line =~ /^d/i) {
1108 while ($ix < $num) {
1109 if (!defined $hunk[$ix]{USE
}) {
1110 $hunk[$ix]{USE
} = 0;
1116 elsif ($line =~ m
|^/(.*)|) {
1119 print colored
$prompt_color, "search for regex? ";
1121 if (defined $regex) {
1127 $search_string = qr{$regex}m;
1130 my ($err,$exp) = ($@
, $1);
1131 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1132 error_msg
"Malformed search regexp $exp: $err\n";
1137 my $text = join ("", @
{$hunk[$iy]{TEXT
}});
1138 last if ($text =~ $search_string);
1140 $iy = 0 if ($iy >= $num);
1142 error_msg
"No hunk matches the given pattern\n";
1149 elsif ($line =~ /^K/) {
1150 if ($other =~ /K/) {
1154 error_msg
"No previous hunk\n";
1158 elsif ($line =~ /^J/) {
1159 if ($other =~ /J/) {
1163 error_msg
"No next hunk\n";
1167 elsif ($line =~ /^k/) {
1168 if ($other =~ /k/) {
1172 !defined $hunk[$ix]{USE
});
1176 error_msg
"No previous hunk\n";
1180 elsif ($line =~ /^j/) {
1181 if ($other !~ /j/) {
1182 error_msg
"No next hunk\n";
1186 elsif ($other =~ /s/ && $line =~ /^s/) {
1187 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
1189 print colored
$header_color, "Split into ",
1190 scalar(@split), " hunks.\n";
1192 splice (@hunk, $ix, 1, @split);
1193 $num = scalar @hunk;
1196 elsif ($line =~ /^e/) {
1197 my $newhunk = edit_hunk_loop
($head, \
@hunk, $ix);
1198 if (defined $newhunk) {
1199 splice @hunk, $ix, 1, $newhunk;
1203 help_patch_cmd
($other);
1209 last if ($ix >= $num ||
1210 !defined $hunk[$ix]{USE
});
1218 push @result, @
{$mode->{TEXT
}};
1222 push @result, @
{$_->{TEXT
}};
1229 open $fh, '| git apply --cached --recount';
1230 for (@
{$head->{TEXT
}}, @result) {
1234 for (@
{$head->{TEXT
}}, @result) {
1245 my @mods = list_modified
('index-only');
1246 @mods = grep { !($_->{BINARY
}) } @mods;
1248 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
1250 HEADER
=> $status_head, },
1253 my $reference = is_initial_commit
() ? get_empty_tree
() : 'HEAD';
1254 system(qw(git diff -p --cached), $reference, '--',
1255 map { $_->{VALUE
} } @them);
1264 print colored
$help_color, <<\EOF
;
1265 status
- show paths with changes
1266 update
- add working tree
state to the staged set of changes
1267 revert
- revert staged set of changes back to the HEAD version
1268 patch
- pick hunks
and update selectively
1269 diff
- view diff between HEAD
and index
1270 add untracked
- add contents of untracked files to the staged set of changes
1275 return unless @ARGV;
1276 my $arg = shift @ARGV;
1277 if ($arg eq "--patch") {
1279 $arg = shift @ARGV or die "missing --";
1280 die "invalid argument $arg, expecting --"
1281 unless $arg eq "--";
1283 elsif ($arg ne "--") {
1284 die "invalid argument $arg, expecting --";
1289 my @cmd = ([ 'status', \
&status_cmd
, ],
1290 [ 'update', \
&update_cmd
, ],
1291 [ 'revert', \
&revert_cmd
, ],
1292 [ 'add untracked', \
&add_untracked_cmd
, ],
1293 [ 'patch', \
&patch_update_cmd
, ],
1294 [ 'diff', \
&diff_cmd
, ],
1295 [ 'quit', \
&quit_cmd
, ],
1296 [ 'help', \
&help_cmd
, ],
1299 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1302 HEADER
=> '*** Commands ***',
1303 ON_EOF
=> \
&quit_cmd
,
1304 IMMEDIATE
=> 1 }, @cmd);