7 my ($prompt_color, $header_color, $help_color, $normal_color);
11 my ($use_color, $diff_use_color);
12 my $repo = Git
->repository();
14 $use_color = $repo->get_colorbool('color.interactive');
17 # Set interactive colors:
19 # Grab the 3 main colors in git color string format, with sane
21 $prompt_color = $repo->get_color("color.interactive.prompt", "bold blue");
22 $header_color = $repo->get_color("color.interactive.header", "bold");
23 $help_color = $repo->get_color("color.interactive.help", "red bold");
24 $normal_color = $repo->get_color("", "reset");
26 # Do we also set diff colors?
27 $diff_use_color = $repo->get_colorbool('color.diff');
28 if ($diff_use_color) {
29 $fraginfo_color = $repo->get_color("color.diff.frag", "cyan");
35 my $string = join("", @_);
38 # Put a color code at the beginning of each line, a reset at the end
39 # color after newlines that are not at the end of the string
40 $string =~ s/(\n+)(.)/$1$color$2/g;
41 # reset before newlines
42 $string =~ s/(\n+)/$normal_color$1/g;
43 # codes at beginning and end (if necessary):
44 $string =~ s/^/$color/;
45 $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
50 # command line options
54 if ($^O
eq 'MSWin32') {
55 my @invalid = grep {m/[":*]/} @_;
56 die "$^O does not support: @invalid\n" if @invalid;
57 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
61 open($fh, '-|', @_) or die;
66 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
68 if (!defined $GIT_DIR) {
69 exit(1); # rev-parse would have already said "not a git repo"
75 open $fh, 'git update-index --refresh |'
78 ;# ignore 'needs update'
88 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
91 my $status_fmt = '%12s %12s %s';
92 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
94 # Returns list of hashes, contents of each of which are:
96 # BINARY: is a binary path
97 # INDEX: is index different from HEAD?
98 # FILE: is file different from index?
99 # INDEX_ADDDEL: is it add/delete between HEAD and index?
100 # FILE_ADDDEL: is it add/delete between index and file?
105 my ($add, $del, $adddel, $file);
111 } run_cmd_pipe
(qw(git ls-files --exclude-standard --), @ARGV);
112 return if (!@tracked);
115 for (run_cmd_pipe
(qw(git diff-index --cached
116 --numstat --summary HEAD --), @tracked)) {
117 if (($add, $del, $file) =
118 /^([-\d]+) ([-\d]+) (.*)/) {
120 if ($add eq '-' && $del eq '-') {
125 $change = "+$add/-$del";
133 elsif (($adddel, $file) =
134 /^ (create|delete) mode [0-7]+ (.*)$/) {
135 $data{$file}{INDEX_ADDDEL
} = $adddel;
139 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
140 if (($add, $del, $file) =
141 /^([-\d]+) ([-\d]+) (.*)/) {
142 if (!exists $data{$file}) {
144 INDEX
=> 'unchanged',
149 if ($add eq '-' && $del eq '-') {
154 $change = "+$add/-$del";
156 $data{$file}{FILE
} = $change;
158 $data{$file}{BINARY
} = 1;
161 elsif (($adddel, $file) =
162 /^ (create|delete) mode [0-7]+ (.*)$/) {
163 $data{$file}{FILE_ADDDEL
} = $adddel;
167 for (sort keys %data) {
171 if ($only eq 'index-only') {
172 next if ($it->{INDEX
} eq 'unchanged');
174 if ($only eq 'file-only') {
175 next if ($it->{FILE
} eq 'nothing');
187 my ($string, @stuff) = @_;
189 for (my $i = 0; $i < @stuff; $i++) {
193 if ((ref $it) eq 'ARRAY') {
201 if ($it =~ /^$string/) {
205 if (defined $hit && defined $found) {
215 # inserts string into trie and updates count for each character
217 my ($trie, $string) = @_;
218 foreach (split //, $string) {
219 $trie = $trie->{$_} ||= {COUNT
=> 0};
224 # returns an array of tuples (prefix, remainder)
225 sub find_unique_prefixes
{
229 # any single prefix exceeding the soft limit is omitted
230 # if any prefix exceeds the hard limit all are omitted
231 # 0 indicates no limit
235 # build a trie modelling all possible options
237 foreach my $print (@stuff) {
238 if ((ref $print) eq 'ARRAY') {
239 $print = $print->[0];
241 elsif ((ref $print) eq 'HASH') {
242 $print = $print->{VALUE
};
244 update_trie
(\
%trie, $print);
245 push @return, $print;
248 # use the trie to find the unique prefixes
249 for (my $i = 0; $i < @return; $i++) {
250 my $ret = $return[$i];
251 my @letters = split //, $ret;
253 my ($prefix, $remainder);
255 for ($j = 0; $j < @letters; $j++) {
256 my $letter = $letters[$j];
257 if ($search{$letter}{COUNT
} == 1) {
258 $prefix = substr $ret, 0, $j + 1;
259 $remainder = substr $ret, $j + 1;
263 my $prefix = substr $ret, 0, $j;
265 if ($hard_limit && $j + 1 > $hard_limit);
267 %search = %{$search{$letter}};
269 if ($soft_limit && $j + 1 > $soft_limit) {
273 $return[$i] = [$prefix, $remainder];
278 # filters out prefixes which have special meaning to list_and_choose()
279 sub is_valid_prefix
{
281 return (defined $prefix) &&
282 !($prefix =~ /[\s,]/) && # separators
283 !($prefix =~ /^-/) && # deselection
284 !($prefix =~ /^\d+/) && # selection
285 ($prefix ne '*') && # "all" wildcard
286 ($prefix ne '?'); # prompt help
289 # given a prefix/remainder tuple return a string with the prefix highlighted
290 # for now use square brackets; later might use ANSI colors (underline, bold)
291 sub highlight_prefix
{
293 my $remainder = shift;
295 if (!defined $prefix) {
299 if (!is_valid_prefix
($prefix)) {
300 return "$prefix$remainder";
304 return "[$prefix]$remainder";
307 return "$prompt_color$prefix$normal_color$remainder";
310 sub list_and_choose
{
311 my ($opts, @stuff) = @_;
312 my (@chosen, @return);
314 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
320 if ($opts->{HEADER
}) {
321 if (!$opts->{LIST_FLAT
}) {
324 print colored
$header_color, "$opts->{HEADER}\n";
326 for ($i = 0; $i < @stuff; $i++) {
327 my $chosen = $chosen[$i] ?
'*' : ' ';
328 my $print = $stuff[$i];
329 my $ref = ref $print;
330 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
332 if ($ref eq 'ARRAY') {
333 $print = $highlighted || $print->[0];
335 elsif ($ref eq 'HASH') {
336 my $value = $highlighted || $print->{VALUE
};
337 $print = sprintf($status_fmt,
343 $print = $highlighted || $print;
345 printf("%s%2d: %s", $chosen, $i+1, $print);
346 if (($opts->{LIST_FLAT
}) &&
347 (($i + 1) % ($opts->{LIST_FLAT
}))) {
360 return if ($opts->{LIST_ONLY
});
362 print colored
$prompt_color, $opts->{PROMPT
};
363 if ($opts->{SINGLETON
}) {
372 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
379 singleton_prompt_help_cmd
() :
383 for my $choice (split(/[\s,]+/, $line)) {
387 # Input that begins with '-'; unchoose
388 if ($choice =~ s/^-//) {
391 # A range can be specified like 5-7
392 if ($choice =~ /^(\d+)-(\d+)$/) {
393 ($bottom, $top) = ($1, $2);
395 elsif ($choice =~ /^\d+$/) {
396 $bottom = $top = $choice;
398 elsif ($choice eq '*') {
403 $bottom = $top = find_unique
($choice, @stuff);
404 if (!defined $bottom) {
405 print "Huh ($choice)?\n";
409 if ($opts->{SINGLETON
} && $bottom != $top) {
410 print "Huh ($choice)?\n";
413 for ($i = $bottom-1; $i <= $top-1; $i++) {
414 next if (@stuff <= $i || $i < 0);
415 $chosen[$i] = $choose;
418 last if ($opts->{IMMEDIATE
} || $line eq '*');
420 for ($i = 0; $i < @stuff; $i++) {
422 push @return, $stuff[$i];
428 sub singleton_prompt_help_cmd
{
429 print colored
$help_color, <<\EOF
;
431 1 - select a numbered item
432 foo
- select item based on unique prefix
433 - (empty
) select nothing
437 sub prompt_help_cmd
{
438 print colored
$help_color, <<\EOF
;
440 1 - select a single item
441 3-5 - select a range of items
442 2-3,6-9 - select multiple ranges
443 foo
- select item based on unique prefix
444 -... - unselect specified items
446 - (empty
) finish selecting
451 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
461 print "$cnt paths\n";
469 my @mods = list_modified
('file-only');
472 my @update = list_and_choose
({ PROMPT
=> 'Update',
473 HEADER
=> $status_head, },
476 system(qw(git update-index --add --remove --),
477 map { $_->{VALUE
} } @update);
478 say_n_paths
('updated', @update);
484 my @update = list_and_choose
({ PROMPT
=> 'Revert',
485 HEADER
=> $status_head, },
488 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
489 map { $_->{VALUE
} } @update);
491 open $fh, '| git update-index --index-info'
498 if ($_->{INDEX_ADDDEL
} &&
499 $_->{INDEX_ADDDEL
} eq 'create') {
500 system(qw(git update-index --force-remove --),
502 print "note: $_->{VALUE} is untracked now.\n";
506 say_n_paths
('reverted', @update);
511 sub add_untracked_cmd
{
512 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
515 system(qw(git update-index --add --), @add);
516 say_n_paths
('added', @add);
523 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
525 if ($diff_use_color) {
526 @colored = run_cmd_pipe
(qw(git diff-files -p --color --), $path);
528 my (@hunk) = { TEXT
=> [], DISPLAY
=> [] };
530 for (my $i = 0; $i < @diff; $i++) {
531 if ($diff[$i] =~ /^@@ /) {
532 push @hunk, { TEXT
=> [], DISPLAY
=> [] };
534 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
535 push @
{$hunk[-1]{DISPLAY
}},
536 ($diff_use_color ?
$colored[$i] : $diff[$i]);
541 sub hunk_splittable
{
544 my @s = split_hunk
($text);
548 sub parse_hunk_header
{
550 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
551 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
552 $o_cnt = 1 unless defined $o_cnt;
553 $n_cnt = 1 unless defined $n_cnt;
554 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
558 my ($text, $display) = @_;
560 if (!defined $display) {
563 # If there are context lines in the middle of a hunk,
564 # it can be split, but we would need to take care of
567 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
572 my $next_hunk_start = undef;
573 my $i = $hunk_start - 1;
586 while (++$i < @
$text) {
587 my $line = $text->[$i];
588 my $display = $display->[$i];
590 if ($this->{ADDDEL
} &&
591 !defined $next_hunk_start) {
592 # We have seen leading context and
593 # adds/dels and then here is another
594 # context, which is trailing for this
595 # split hunk and leading for the next
597 $next_hunk_start = $i;
599 push @
{$this->{TEXT
}}, $line;
600 push @
{$this->{DISPLAY
}}, $display;
603 if (defined $next_hunk_start) {
610 if (defined $next_hunk_start) {
611 # We are done with the current hunk and
612 # this is the first real change for the
614 $hunk_start = $next_hunk_start;
615 $o_ofs = $this->{OLD
} + $this->{OCNT
};
616 $n_ofs = $this->{NEW
} + $this->{NCNT
};
617 $o_ofs -= $this->{POSTCTX
};
618 $n_ofs -= $this->{POSTCTX
};
622 push @
{$this->{TEXT
}}, $line;
623 push @
{$this->{DISPLAY
}}, $display;
637 for my $hunk (@split) {
638 $o_ofs = $hunk->{OLD
};
639 $n_ofs = $hunk->{NEW
};
640 my $o_cnt = $hunk->{OCNT
};
641 my $n_cnt = $hunk->{NCNT
};
643 my $head = ("@@ -$o_ofs" .
644 (($o_cnt != 1) ?
",$o_cnt" : '') .
646 (($n_cnt != 1) ?
",$n_cnt" : '') .
648 my $display_head = $head;
649 unshift @
{$hunk->{TEXT
}}, $head;
650 if ($diff_use_color) {
651 $display_head = colored
($fraginfo_color, $head);
653 unshift @
{$hunk->{DISPLAY
}}, $display_head;
658 sub find_last_o_ctx
{
660 my $text = $it->{TEXT
};
661 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
663 my $last_o_ctx = $o_ofs + $o_cnt;
665 my $line = $text->[$i];
676 my ($prev, $this) = @_;
677 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
678 parse_hunk_header
($prev->{TEXT
}[0]);
679 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
680 parse_hunk_header
($this->{TEXT
}[0]);
682 my (@line, $i, $ofs, $o_cnt, $n_cnt);
685 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
686 my $line = $prev->{TEXT
}[$i];
687 if ($line =~ /^\+/) {
693 last if ($o1_ofs <= $ofs);
703 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
704 my $line = $this->{TEXT
}[$i];
705 if ($line =~ /^\+/) {
717 my $head = ("@@ -$o0_ofs" .
718 (($o_cnt != 1) ?
",$o_cnt" : '') .
720 (($n_cnt != 1) ?
",$n_cnt" : '') .
722 @
{$prev->{TEXT
}} = ($head, @line);
725 sub coalesce_overlapping_hunks
{
731 for (grep { $_->{USE
} } @in) {
732 my $text = $_->{TEXT
};
733 my ($o_ofs) = parse_hunk_header
($text->[0]);
734 if (defined $last_o_ctx &&
735 $o_ofs <= $last_o_ctx) {
736 merge_hunk
($out[-1], $_);
741 $last_o_ctx = find_last_o_ctx
($out[-1]);
747 print colored
$help_color, <<\EOF
;
749 n
- do not stage this hunk
750 a
- stage this
and all the remaining hunks
in the file
751 d
- do not stage this hunk nor any of the remaining hunks
in the file
752 j
- leave this hunk undecided
, see
next undecided hunk
753 J
- leave this hunk undecided
, see
next hunk
754 k
- leave this hunk undecided
, see previous undecided hunk
755 K
- leave this hunk undecided
, see previous hunk
756 s
- split the current hunk into smaller hunks
761 sub patch_update_cmd
{
762 my @mods = grep { !($_->{BINARY
}) } list_modified
('file-only');
766 print STDERR
"No changes.\n";
773 @them = list_and_choose
({ PROMPT
=> 'Patch update',
774 HEADER
=> $status_head, },
778 patch_update_file
($_->{VALUE
});
782 sub patch_update_file
{
785 my ($head, @hunk) = parse_diff
($path);
786 for (@
{$head->{DISPLAY
}}) {
793 my ($prev, $next, $other, $undecided, $i);
799 for ($i = 0; $i < $ix; $i++) {
800 if (!defined $hunk[$i]{USE
}) {
809 for ($i = $ix + 1; $i < $num; $i++) {
810 if (!defined $hunk[$i]{USE
}) {
816 if ($ix < $num - 1) {
819 for ($i = 0; $i < $num; $i++) {
820 if (!defined $hunk[$i]{USE
}) {
825 last if (!$undecided);
827 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
830 for (@
{$hunk[$ix]{DISPLAY
}}) {
833 print colored
$prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
836 if ($line =~ /^y/i) {
839 elsif ($line =~ /^n/i) {
842 elsif ($line =~ /^a/i) {
844 if (!defined $hunk[$ix]{USE
}) {
851 elsif ($line =~ /^d/i) {
853 if (!defined $hunk[$ix]{USE
}) {
860 elsif ($other =~ /K/ && $line =~ /^K/) {
864 elsif ($other =~ /J/ && $line =~ /^J/) {
868 elsif ($other =~ /k/ && $line =~ /^k/) {
872 !defined $hunk[$ix]{USE
});
876 elsif ($other =~ /j/ && $line =~ /^j/) {
879 last if ($ix >= $num ||
880 !defined $hunk[$ix]{USE
});
884 elsif ($other =~ /s/ && $line =~ /^s/) {
885 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
887 print colored
$header_color, "Split into ",
888 scalar(@split), " hunks.\n";
890 splice (@hunk, $ix, 1, @split);
895 help_patch_cmd
($other);
901 last if ($ix >= $num ||
902 !defined $hunk[$ix]{USE
});
907 @hunk = coalesce_overlapping_hunks
(@hunk);
912 my $text = $_->{TEXT
};
913 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
914 parse_hunk_header
($text->[0]);
917 # We would have added ($n_cnt - $o_cnt) lines
918 # to the postimage if we were to use this hunk,
919 # but we didn't. So the line number that the next
920 # hunk starts at would be shifted by that much.
921 $n_lofs -= ($n_cnt - $o_cnt);
927 $text->[0] = ("@@ -$o_ofs" .
944 open $fh, '| git apply --cached';
945 for (@
{$head->{TEXT
}}, @result) {
949 for (@
{$head->{TEXT
}}, @result) {
960 my @mods = list_modified
('index-only');
961 @mods = grep { !($_->{BINARY
}) } @mods;
963 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
965 HEADER
=> $status_head, },
968 system(qw(git diff -p --cached HEAD --), map { $_->{VALUE
} } @them);
977 print colored
$help_color, <<\EOF
;
978 status
- show paths with changes
979 update
- add working tree
state to the staged set of changes
980 revert
- revert staged set of changes back to the HEAD version
981 patch
- pick hunks
and update selectively
982 diff
- view diff between HEAD
and index
983 add untracked
- add contents of untracked files to the staged set of changes
989 my $arg = shift @ARGV;
990 if ($arg eq "--patch") {
992 $arg = shift @ARGV or die "missing --";
993 die "invalid argument $arg, expecting --"
996 elsif ($arg ne "--") {
997 die "invalid argument $arg, expecting --";
1002 my @cmd = ([ 'status', \
&status_cmd
, ],
1003 [ 'update', \
&update_cmd
, ],
1004 [ 'revert', \
&revert_cmd
, ],
1005 [ 'add untracked', \
&add_untracked_cmd
, ],
1006 [ 'patch', \
&patch_update_cmd
, ],
1007 [ 'diff', \
&diff_cmd
, ],
1008 [ 'quit', \
&quit_cmd
, ],
1009 [ 'help', \
&help_cmd
, ],
1012 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1015 HEADER
=> '*** Commands ***',
1016 ON_EOF
=> \
&quit_cmd
,
1017 IMMEDIATE
=> 1 }, @cmd);