7 my ($prompt_color, $header_color, $help_color, $normal_color);
9 my ($new_color, $old_color, $fraginfo_color, $metainfo_color, $whitespace_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 $new_color = $repo->get_color("color.diff.new", "green");
30 $old_color = $repo->get_color("color.diff.old", "red");
31 $fraginfo_color = $repo->get_color("color.diff.frag", "cyan");
32 $metainfo_color = $repo->get_color("color.diff.meta", "bold");
33 $whitespace_color = $repo->get_color("color.diff.whitespace", "normal red");
39 my $string = join("", @_);
42 # Put a color code at the beginning of each line, a reset at the end
43 # color after newlines that are not at the end of the string
44 $string =~ s/(\n+)(.)/$1$color$2/g;
45 # reset before newlines
46 $string =~ s/(\n+)/$normal_color$1/g;
47 # codes at beginning and end (if necessary):
48 $string =~ s/^/$color/;
49 $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
54 # command line options
58 if ($^O
eq 'MSWin32') {
59 my @invalid = grep {m/[":*]/} @_;
60 die "$^O does not support: @invalid\n" if @invalid;
61 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
65 open($fh, '-|', @_) or die;
70 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
72 if (!defined $GIT_DIR) {
73 exit(1); # rev-parse would have already said "not a git repo"
79 open $fh, 'git update-index --refresh |'
82 ;# ignore 'needs update'
92 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
95 my $status_fmt = '%12s %12s %s';
96 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
98 # Returns list of hashes, contents of each of which are:
100 # BINARY: is a binary path
101 # INDEX: is index different from HEAD?
102 # FILE: is file different from index?
103 # INDEX_ADDDEL: is it add/delete between HEAD and index?
104 # FILE_ADDDEL: is it add/delete between index and file?
109 my ($add, $del, $adddel, $file);
115 } run_cmd_pipe
(qw(git ls-files --exclude-standard --), @ARGV);
116 return if (!@tracked);
119 for (run_cmd_pipe
(qw(git diff-index --cached
120 --numstat --summary HEAD --), @tracked)) {
121 if (($add, $del, $file) =
122 /^([-\d]+) ([-\d]+) (.*)/) {
124 if ($add eq '-' && $del eq '-') {
129 $change = "+$add/-$del";
137 elsif (($adddel, $file) =
138 /^ (create|delete) mode [0-7]+ (.*)$/) {
139 $data{$file}{INDEX_ADDDEL
} = $adddel;
143 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
144 if (($add, $del, $file) =
145 /^([-\d]+) ([-\d]+) (.*)/) {
146 if (!exists $data{$file}) {
148 INDEX
=> 'unchanged',
153 if ($add eq '-' && $del eq '-') {
158 $change = "+$add/-$del";
160 $data{$file}{FILE
} = $change;
162 $data{$file}{BINARY
} = 1;
165 elsif (($adddel, $file) =
166 /^ (create|delete) mode [0-7]+ (.*)$/) {
167 $data{$file}{FILE_ADDDEL
} = $adddel;
171 for (sort keys %data) {
175 if ($only eq 'index-only') {
176 next if ($it->{INDEX
} eq 'unchanged');
178 if ($only eq 'file-only') {
179 next if ($it->{FILE
} eq 'nothing');
191 my ($string, @stuff) = @_;
193 for (my $i = 0; $i < @stuff; $i++) {
197 if ((ref $it) eq 'ARRAY') {
205 if ($it =~ /^$string/) {
209 if (defined $hit && defined $found) {
219 # inserts string into trie and updates count for each character
221 my ($trie, $string) = @_;
222 foreach (split //, $string) {
223 $trie = $trie->{$_} ||= {COUNT
=> 0};
228 # returns an array of tuples (prefix, remainder)
229 sub find_unique_prefixes
{
233 # any single prefix exceeding the soft limit is omitted
234 # if any prefix exceeds the hard limit all are omitted
235 # 0 indicates no limit
239 # build a trie modelling all possible options
241 foreach my $print (@stuff) {
242 if ((ref $print) eq 'ARRAY') {
243 $print = $print->[0];
245 elsif ((ref $print) eq 'HASH') {
246 $print = $print->{VALUE
};
248 update_trie
(\
%trie, $print);
249 push @return, $print;
252 # use the trie to find the unique prefixes
253 for (my $i = 0; $i < @return; $i++) {
254 my $ret = $return[$i];
255 my @letters = split //, $ret;
257 my ($prefix, $remainder);
259 for ($j = 0; $j < @letters; $j++) {
260 my $letter = $letters[$j];
261 if ($search{$letter}{COUNT
} == 1) {
262 $prefix = substr $ret, 0, $j + 1;
263 $remainder = substr $ret, $j + 1;
267 my $prefix = substr $ret, 0, $j;
269 if ($hard_limit && $j + 1 > $hard_limit);
271 %search = %{$search{$letter}};
273 if ($soft_limit && $j + 1 > $soft_limit) {
277 $return[$i] = [$prefix, $remainder];
282 # filters out prefixes which have special meaning to list_and_choose()
283 sub is_valid_prefix
{
285 return (defined $prefix) &&
286 !($prefix =~ /[\s,]/) && # separators
287 !($prefix =~ /^-/) && # deselection
288 !($prefix =~ /^\d+/) && # selection
289 ($prefix ne '*') && # "all" wildcard
290 ($prefix ne '?'); # prompt help
293 # given a prefix/remainder tuple return a string with the prefix highlighted
294 # for now use square brackets; later might use ANSI colors (underline, bold)
295 sub highlight_prefix
{
297 my $remainder = shift;
299 if (!defined $prefix) {
303 if (!is_valid_prefix
($prefix)) {
304 return "$prefix$remainder";
308 return "[$prefix]$remainder";
311 return "$prompt_color$prefix$normal_color$remainder";
314 sub list_and_choose
{
315 my ($opts, @stuff) = @_;
316 my (@chosen, @return);
318 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
324 if ($opts->{HEADER
}) {
325 if (!$opts->{LIST_FLAT
}) {
328 print colored
$header_color, "$opts->{HEADER}\n";
330 for ($i = 0; $i < @stuff; $i++) {
331 my $chosen = $chosen[$i] ?
'*' : ' ';
332 my $print = $stuff[$i];
333 my $ref = ref $print;
334 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
336 if ($ref eq 'ARRAY') {
337 $print = $highlighted || $print->[0];
339 elsif ($ref eq 'HASH') {
340 my $value = $highlighted || $print->{VALUE
};
341 $print = sprintf($status_fmt,
347 $print = $highlighted || $print;
349 printf("%s%2d: %s", $chosen, $i+1, $print);
350 if (($opts->{LIST_FLAT
}) &&
351 (($i + 1) % ($opts->{LIST_FLAT
}))) {
364 return if ($opts->{LIST_ONLY
});
366 print colored
$prompt_color, $opts->{PROMPT
};
367 if ($opts->{SINGLETON
}) {
376 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
383 singleton_prompt_help_cmd
() :
387 for my $choice (split(/[\s,]+/, $line)) {
391 # Input that begins with '-'; unchoose
392 if ($choice =~ s/^-//) {
395 # A range can be specified like 5-7
396 if ($choice =~ /^(\d+)-(\d+)$/) {
397 ($bottom, $top) = ($1, $2);
399 elsif ($choice =~ /^\d+$/) {
400 $bottom = $top = $choice;
402 elsif ($choice eq '*') {
407 $bottom = $top = find_unique
($choice, @stuff);
408 if (!defined $bottom) {
409 print "Huh ($choice)?\n";
413 if ($opts->{SINGLETON
} && $bottom != $top) {
414 print "Huh ($choice)?\n";
417 for ($i = $bottom-1; $i <= $top-1; $i++) {
418 next if (@stuff <= $i || $i < 0);
419 $chosen[$i] = $choose;
422 last if ($opts->{IMMEDIATE
} || $line eq '*');
424 for ($i = 0; $i < @stuff; $i++) {
426 push @return, $stuff[$i];
432 sub singleton_prompt_help_cmd
{
433 print colored
$help_color, <<\EOF
;
435 1 - select a numbered item
436 foo
- select item based on unique prefix
437 - (empty
) select nothing
441 sub prompt_help_cmd
{
442 print colored
$help_color, <<\EOF
;
444 1 - select a single item
445 3-5 - select a range of items
446 2-3,6-9 - select multiple ranges
447 foo
- select item based on unique prefix
448 -... - unselect specified items
450 - (empty
) finish selecting
455 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
465 print "$cnt paths\n";
473 my @mods = list_modified
('file-only');
476 my @update = list_and_choose
({ PROMPT
=> 'Update',
477 HEADER
=> $status_head, },
480 system(qw(git update-index --add --remove --),
481 map { $_->{VALUE
} } @update);
482 say_n_paths
('updated', @update);
488 my @update = list_and_choose
({ PROMPT
=> 'Revert',
489 HEADER
=> $status_head, },
492 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
493 map { $_->{VALUE
} } @update);
495 open $fh, '| git update-index --index-info'
502 if ($_->{INDEX_ADDDEL
} &&
503 $_->{INDEX_ADDDEL
} eq 'create') {
504 system(qw(git update-index --force-remove --),
506 print "note: $_->{VALUE} is untracked now.\n";
510 say_n_paths
('reverted', @update);
515 sub add_untracked_cmd
{
516 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
519 system(qw(git update-index --add --), @add);
520 say_n_paths
('added', @add);
527 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
528 my (@hunk) = { TEXT
=> [] };
532 push @hunk, { TEXT
=> [] };
534 push @
{$hunk[-1]{TEXT
}}, $_;
539 sub colored_diff_hunk
{
541 # return the text, so that it can be passed to print()
544 if (!$diff_use_color) {
550 push @ret, colored
($new_color, $_);
552 push @ret, colored
($old_color, $_);
554 push @ret, colored
($fraginfo_color, $_);
556 push @ret, colored
($normal_color, $_);
558 push @ret, colored
($metainfo_color, $_);
564 sub hunk_splittable
{
567 my @s = split_hunk
($text);
571 sub parse_hunk_header
{
573 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
574 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
575 $o_cnt = 1 unless defined $o_cnt;
576 $n_cnt = 1 unless defined $n_cnt;
577 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
584 # If there are context lines in the middle of a hunk,
585 # it can be split, but we would need to take care of
588 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
593 my $next_hunk_start = undef;
594 my $i = $hunk_start - 1;
605 while (++$i < @
$text) {
606 my $line = $text->[$i];
608 if ($this->{ADDDEL
} &&
609 !defined $next_hunk_start) {
610 # We have seen leading context and
611 # adds/dels and then here is another
612 # context, which is trailing for this
613 # split hunk and leading for the next
615 $next_hunk_start = $i;
617 push @
{$this->{TEXT
}}, $line;
620 if (defined $next_hunk_start) {
627 if (defined $next_hunk_start) {
628 # We are done with the current hunk and
629 # this is the first real change for the
631 $hunk_start = $next_hunk_start;
632 $o_ofs = $this->{OLD
} + $this->{OCNT
};
633 $n_ofs = $this->{NEW
} + $this->{NCNT
};
634 $o_ofs -= $this->{POSTCTX
};
635 $n_ofs -= $this->{POSTCTX
};
639 push @
{$this->{TEXT
}}, $line;
653 for my $hunk (@split) {
654 $o_ofs = $hunk->{OLD
};
655 $n_ofs = $hunk->{NEW
};
656 my $o_cnt = $hunk->{OCNT
};
657 my $n_cnt = $hunk->{NCNT
};
659 my $head = ("@@ -$o_ofs" .
660 (($o_cnt != 1) ?
",$o_cnt" : '') .
662 (($n_cnt != 1) ?
",$n_cnt" : '') .
664 unshift @
{$hunk->{TEXT
}}, $head;
666 return map { $_->{TEXT
} } @split;
669 sub find_last_o_ctx
{
671 my $text = $it->{TEXT
};
672 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
674 my $last_o_ctx = $o_ofs + $o_cnt;
676 my $line = $text->[$i];
687 my ($prev, $this) = @_;
688 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
689 parse_hunk_header
($prev->{TEXT
}[0]);
690 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
691 parse_hunk_header
($this->{TEXT
}[0]);
693 my (@line, $i, $ofs, $o_cnt, $n_cnt);
696 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
697 my $line = $prev->{TEXT
}[$i];
698 if ($line =~ /^\+/) {
704 last if ($o1_ofs <= $ofs);
714 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
715 my $line = $this->{TEXT
}[$i];
716 if ($line =~ /^\+/) {
728 my $head = ("@@ -$o0_ofs" .
729 (($o_cnt != 1) ?
",$o_cnt" : '') .
731 (($n_cnt != 1) ?
",$n_cnt" : '') .
733 @
{$prev->{TEXT
}} = ($head, @line);
736 sub coalesce_overlapping_hunks
{
742 for (grep { $_->{USE
} } @in) {
743 my $text = $_->{TEXT
};
744 my ($o_ofs) = parse_hunk_header
($text->[0]);
745 if (defined $last_o_ctx &&
746 $o_ofs <= $last_o_ctx) {
747 merge_hunk
($out[-1], $_);
752 $last_o_ctx = find_last_o_ctx
($out[-1]);
758 print colored
$help_color, <<\EOF
;
760 n
- do not stage this hunk
761 a
- stage this
and all the remaining hunks
in the file
762 d
- do not stage this hunk nor any of the remaining hunks
in the file
763 j
- leave this hunk undecided
, see
next undecided hunk
764 J
- leave this hunk undecided
, see
next hunk
765 k
- leave this hunk undecided
, see previous undecided hunk
766 K
- leave this hunk undecided
, see previous hunk
767 s
- split the current hunk into smaller hunks
772 sub patch_update_cmd
{
773 my @mods = grep { !($_->{BINARY
}) } list_modified
('file-only');
777 print STDERR
"No changes.\n";
784 @them = list_and_choose
({ PROMPT
=> 'Patch update',
785 HEADER
=> $status_head, },
789 patch_update_file
($_->{VALUE
});
793 sub patch_update_file
{
796 my ($head, @hunk) = parse_diff
($path);
797 print colored_diff_hunk
($head->{TEXT
});
802 my ($prev, $next, $other, $undecided, $i);
808 for ($i = 0; $i < $ix; $i++) {
809 if (!defined $hunk[$i]{USE
}) {
818 for ($i = $ix + 1; $i < $num; $i++) {
819 if (!defined $hunk[$i]{USE
}) {
825 if ($ix < $num - 1) {
828 for ($i = 0; $i < $num; $i++) {
829 if (!defined $hunk[$i]{USE
}) {
834 last if (!$undecided);
836 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
839 print colored_diff_hunk
($hunk[$ix]{TEXT
});
840 print colored
$prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
843 if ($line =~ /^y/i) {
846 elsif ($line =~ /^n/i) {
849 elsif ($line =~ /^a/i) {
851 if (!defined $hunk[$ix]{USE
}) {
858 elsif ($line =~ /^d/i) {
860 if (!defined $hunk[$ix]{USE
}) {
867 elsif ($other =~ /K/ && $line =~ /^K/) {
871 elsif ($other =~ /J/ && $line =~ /^J/) {
875 elsif ($other =~ /k/ && $line =~ /^k/) {
879 !defined $hunk[$ix]{USE
});
883 elsif ($other =~ /j/ && $line =~ /^j/) {
886 last if ($ix >= $num ||
887 !defined $hunk[$ix]{USE
});
891 elsif ($other =~ /s/ && $line =~ /^s/) {
892 my @split = split_hunk
($hunk[$ix]{TEXT
});
894 print colored
$header_color, "Split into ",
895 scalar(@split), " hunks.\n";
897 splice(@hunk, $ix, 1,
898 map { +{ TEXT
=> $_, USE
=> undef } }
904 help_patch_cmd
($other);
910 last if ($ix >= $num ||
911 !defined $hunk[$ix]{USE
});
916 @hunk = coalesce_overlapping_hunks
(@hunk);
921 my $text = $_->{TEXT
};
922 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
923 parse_hunk_header
($text->[0]);
926 # We would have added ($n_cnt - $o_cnt) lines
927 # to the postimage if we were to use this hunk,
928 # but we didn't. So the line number that the next
929 # hunk starts at would be shifted by that much.
930 $n_lofs -= ($n_cnt - $o_cnt);
936 $text->[0] = ("@@ -$o_ofs" .
953 open $fh, '| git apply --cached';
954 for (@
{$head->{TEXT
}}, @result) {
958 for (@
{$head->{TEXT
}}, @result) {
969 my @mods = list_modified
('index-only');
970 @mods = grep { !($_->{BINARY
}) } @mods;
972 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
974 HEADER
=> $status_head, },
977 system(qw(git diff -p --cached HEAD --), map { $_->{VALUE
} } @them);
986 print colored
$help_color, <<\EOF
;
987 status
- show paths with changes
988 update
- add working tree
state to the staged set of changes
989 revert
- revert staged set of changes back to the HEAD version
990 patch
- pick hunks
and update selectively
991 diff
- view diff between HEAD
and index
992 add untracked
- add contents of untracked files to the staged set of changes
998 my $arg = shift @ARGV;
999 if ($arg eq "--patch") {
1001 $arg = shift @ARGV or die "missing --";
1002 die "invalid argument $arg, expecting --"
1003 unless $arg eq "--";
1005 elsif ($arg ne "--") {
1006 die "invalid argument $arg, expecting --";
1011 my @cmd = ([ 'status', \
&status_cmd
, ],
1012 [ 'update', \
&update_cmd
, ],
1013 [ 'revert', \
&revert_cmd
, ],
1014 [ 'add untracked', \
&add_untracked_cmd
, ],
1015 [ 'patch', \
&patch_update_cmd
, ],
1016 [ 'diff', \
&diff_cmd
, ],
1017 [ 'quit', \
&quit_cmd
, ],
1018 [ 'help', \
&help_cmd
, ],
1021 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1024 HEADER
=> '*** Commands ***',
1025 ON_EOF
=> \
&quit_cmd
,
1026 IMMEDIATE
=> 1 }, @cmd);