6 my $repo = Git
->repository();
8 my $menu_use_color = $repo->get_colorbool('color.interactive');
9 my ($prompt_color, $header_color, $help_color) =
11 $repo->get_color('color.interactive.prompt', 'bold blue'),
12 $repo->get_color('color.interactive.header', 'bold'),
13 $repo->get_color('color.interactive.help', 'red bold'),
16 my $diff_use_color = $repo->get_colorbool('color.diff');
17 my ($fraginfo_color) =
19 $repo->get_color('color.diff.frag', 'cyan'),
22 my $normal_color = $repo->get_color("", "reset");
26 my $string = join("", @_);
29 # Put a color code at the beginning of each line, a reset at the end
30 # color after newlines that are not at the end of the string
31 $string =~ s/(\n+)(.)/$1$color$2/g;
32 # reset before newlines
33 $string =~ s/(\n+)/$normal_color$1/g;
34 # codes at beginning and end (if necessary):
35 $string =~ s/^/$color/;
36 $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
41 # command line options
45 if ($^O
eq 'MSWin32') {
46 my @invalid = grep {m/[":*]/} @_;
47 die "$^O does not support: @invalid\n" if @invalid;
48 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
52 open($fh, '-|', @_) or die;
57 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
59 if (!defined $GIT_DIR) {
60 exit(1); # rev-parse would have already said "not a git repo"
66 open $fh, 'git update-index --refresh |'
69 ;# ignore 'needs update'
79 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
82 my $status_fmt = '%12s %12s %s';
83 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
85 # Returns list of hashes, contents of each of which are:
87 # BINARY: is a binary path
88 # INDEX: is index different from HEAD?
89 # FILE: is file different from index?
90 # INDEX_ADDDEL: is it add/delete between HEAD and index?
91 # FILE_ADDDEL: is it add/delete between index and file?
96 my ($add, $del, $adddel, $file);
102 } run_cmd_pipe
(qw(git ls-files --exclude-standard --), @ARGV);
103 return if (!@tracked);
106 for (run_cmd_pipe
(qw(git diff-index --cached
107 --numstat --summary HEAD --), @tracked)) {
108 if (($add, $del, $file) =
109 /^([-\d]+) ([-\d]+) (.*)/) {
111 if ($add eq '-' && $del eq '-') {
116 $change = "+$add/-$del";
124 elsif (($adddel, $file) =
125 /^ (create|delete) mode [0-7]+ (.*)$/) {
126 $data{$file}{INDEX_ADDDEL
} = $adddel;
130 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
131 if (($add, $del, $file) =
132 /^([-\d]+) ([-\d]+) (.*)/) {
133 if (!exists $data{$file}) {
135 INDEX
=> 'unchanged',
140 if ($add eq '-' && $del eq '-') {
145 $change = "+$add/-$del";
147 $data{$file}{FILE
} = $change;
149 $data{$file}{BINARY
} = 1;
152 elsif (($adddel, $file) =
153 /^ (create|delete) mode [0-7]+ (.*)$/) {
154 $data{$file}{FILE_ADDDEL
} = $adddel;
158 for (sort keys %data) {
162 if ($only eq 'index-only') {
163 next if ($it->{INDEX
} eq 'unchanged');
165 if ($only eq 'file-only') {
166 next if ($it->{FILE
} eq 'nothing');
178 my ($string, @stuff) = @_;
180 for (my $i = 0; $i < @stuff; $i++) {
184 if ((ref $it) eq 'ARRAY') {
192 if ($it =~ /^$string/) {
196 if (defined $hit && defined $found) {
206 # inserts string into trie and updates count for each character
208 my ($trie, $string) = @_;
209 foreach (split //, $string) {
210 $trie = $trie->{$_} ||= {COUNT
=> 0};
215 # returns an array of tuples (prefix, remainder)
216 sub find_unique_prefixes
{
220 # any single prefix exceeding the soft limit is omitted
221 # if any prefix exceeds the hard limit all are omitted
222 # 0 indicates no limit
226 # build a trie modelling all possible options
228 foreach my $print (@stuff) {
229 if ((ref $print) eq 'ARRAY') {
230 $print = $print->[0];
232 elsif ((ref $print) eq 'HASH') {
233 $print = $print->{VALUE
};
235 update_trie
(\
%trie, $print);
236 push @return, $print;
239 # use the trie to find the unique prefixes
240 for (my $i = 0; $i < @return; $i++) {
241 my $ret = $return[$i];
242 my @letters = split //, $ret;
244 my ($prefix, $remainder);
246 for ($j = 0; $j < @letters; $j++) {
247 my $letter = $letters[$j];
248 if ($search{$letter}{COUNT
} == 1) {
249 $prefix = substr $ret, 0, $j + 1;
250 $remainder = substr $ret, $j + 1;
254 my $prefix = substr $ret, 0, $j;
256 if ($hard_limit && $j + 1 > $hard_limit);
258 %search = %{$search{$letter}};
260 if ($soft_limit && $j + 1 > $soft_limit) {
264 $return[$i] = [$prefix, $remainder];
269 # filters out prefixes which have special meaning to list_and_choose()
270 sub is_valid_prefix
{
272 return (defined $prefix) &&
273 !($prefix =~ /[\s,]/) && # separators
274 !($prefix =~ /^-/) && # deselection
275 !($prefix =~ /^\d+/) && # selection
276 ($prefix ne '*') && # "all" wildcard
277 ($prefix ne '?'); # prompt help
280 # given a prefix/remainder tuple return a string with the prefix highlighted
281 # for now use square brackets; later might use ANSI colors (underline, bold)
282 sub highlight_prefix
{
284 my $remainder = shift;
286 if (!defined $prefix) {
290 if (!is_valid_prefix
($prefix)) {
291 return "$prefix$remainder";
294 if (!$menu_use_color) {
295 return "[$prefix]$remainder";
298 return "$prompt_color$prefix$normal_color$remainder";
301 sub list_and_choose
{
302 my ($opts, @stuff) = @_;
303 my (@chosen, @return);
305 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
311 if ($opts->{HEADER
}) {
312 if (!$opts->{LIST_FLAT
}) {
315 print colored
$header_color, "$opts->{HEADER}\n";
317 for ($i = 0; $i < @stuff; $i++) {
318 my $chosen = $chosen[$i] ?
'*' : ' ';
319 my $print = $stuff[$i];
320 my $ref = ref $print;
321 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
323 if ($ref eq 'ARRAY') {
324 $print = $highlighted || $print->[0];
326 elsif ($ref eq 'HASH') {
327 my $value = $highlighted || $print->{VALUE
};
328 $print = sprintf($status_fmt,
334 $print = $highlighted || $print;
336 printf("%s%2d: %s", $chosen, $i+1, $print);
337 if (($opts->{LIST_FLAT
}) &&
338 (($i + 1) % ($opts->{LIST_FLAT
}))) {
351 return if ($opts->{LIST_ONLY
});
353 print colored
$prompt_color, $opts->{PROMPT
};
354 if ($opts->{SINGLETON
}) {
363 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
370 singleton_prompt_help_cmd
() :
374 for my $choice (split(/[\s,]+/, $line)) {
378 # Input that begins with '-'; unchoose
379 if ($choice =~ s/^-//) {
382 # A range can be specified like 5-7
383 if ($choice =~ /^(\d+)-(\d+)$/) {
384 ($bottom, $top) = ($1, $2);
386 elsif ($choice =~ /^\d+$/) {
387 $bottom = $top = $choice;
389 elsif ($choice eq '*') {
394 $bottom = $top = find_unique
($choice, @stuff);
395 if (!defined $bottom) {
396 print "Huh ($choice)?\n";
400 if ($opts->{SINGLETON
} && $bottom != $top) {
401 print "Huh ($choice)?\n";
404 for ($i = $bottom-1; $i <= $top-1; $i++) {
405 next if (@stuff <= $i || $i < 0);
406 $chosen[$i] = $choose;
409 last if ($opts->{IMMEDIATE
} || $line eq '*');
411 for ($i = 0; $i < @stuff; $i++) {
413 push @return, $stuff[$i];
419 sub singleton_prompt_help_cmd
{
420 print colored
$help_color, <<\EOF
;
422 1 - select a numbered item
423 foo
- select item based on unique prefix
424 - (empty
) select nothing
428 sub prompt_help_cmd
{
429 print colored
$help_color, <<\EOF
;
431 1 - select a single item
432 3-5 - select a range of items
433 2-3,6-9 - select multiple ranges
434 foo
- select item based on unique prefix
435 -... - unselect specified items
437 - (empty
) finish selecting
442 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
452 print "$cnt paths\n";
460 my @mods = list_modified
('file-only');
463 my @update = list_and_choose
({ PROMPT
=> 'Update',
464 HEADER
=> $status_head, },
467 system(qw(git update-index --add --remove --),
468 map { $_->{VALUE
} } @update);
469 say_n_paths
('updated', @update);
475 my @update = list_and_choose
({ PROMPT
=> 'Revert',
476 HEADER
=> $status_head, },
479 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
480 map { $_->{VALUE
} } @update);
482 open $fh, '| git update-index --index-info'
489 if ($_->{INDEX_ADDDEL
} &&
490 $_->{INDEX_ADDDEL
} eq 'create') {
491 system(qw(git update-index --force-remove --),
493 print "note: $_->{VALUE} is untracked now.\n";
497 say_n_paths
('reverted', @update);
502 sub add_untracked_cmd
{
503 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
506 system(qw(git update-index --add --), @add);
507 say_n_paths
('added', @add);
514 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
516 if ($diff_use_color) {
517 @colored = run_cmd_pipe
(qw(git diff-files -p --color --), $path);
519 my (@hunk) = { TEXT
=> [], DISPLAY
=> [] };
521 for (my $i = 0; $i < @diff; $i++) {
522 if ($diff[$i] =~ /^@@ /) {
523 push @hunk, { TEXT
=> [], DISPLAY
=> [] };
525 push @
{$hunk[-1]{TEXT
}}, $diff[$i];
526 push @
{$hunk[-1]{DISPLAY
}},
527 ($diff_use_color ?
$colored[$i] : $diff[$i]);
532 sub hunk_splittable
{
535 my @s = split_hunk
($text);
539 sub parse_hunk_header
{
541 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
542 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
543 $o_cnt = 1 unless defined $o_cnt;
544 $n_cnt = 1 unless defined $n_cnt;
545 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
549 my ($text, $display) = @_;
551 if (!defined $display) {
554 # If there are context lines in the middle of a hunk,
555 # it can be split, but we would need to take care of
558 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
563 my $next_hunk_start = undef;
564 my $i = $hunk_start - 1;
577 while (++$i < @
$text) {
578 my $line = $text->[$i];
579 my $display = $display->[$i];
581 if ($this->{ADDDEL
} &&
582 !defined $next_hunk_start) {
583 # We have seen leading context and
584 # adds/dels and then here is another
585 # context, which is trailing for this
586 # split hunk and leading for the next
588 $next_hunk_start = $i;
590 push @
{$this->{TEXT
}}, $line;
591 push @
{$this->{DISPLAY
}}, $display;
594 if (defined $next_hunk_start) {
601 if (defined $next_hunk_start) {
602 # We are done with the current hunk and
603 # this is the first real change for the
605 $hunk_start = $next_hunk_start;
606 $o_ofs = $this->{OLD
} + $this->{OCNT
};
607 $n_ofs = $this->{NEW
} + $this->{NCNT
};
608 $o_ofs -= $this->{POSTCTX
};
609 $n_ofs -= $this->{POSTCTX
};
613 push @
{$this->{TEXT
}}, $line;
614 push @
{$this->{DISPLAY
}}, $display;
628 for my $hunk (@split) {
629 $o_ofs = $hunk->{OLD
};
630 $n_ofs = $hunk->{NEW
};
631 my $o_cnt = $hunk->{OCNT
};
632 my $n_cnt = $hunk->{NCNT
};
634 my $head = ("@@ -$o_ofs" .
635 (($o_cnt != 1) ?
",$o_cnt" : '') .
637 (($n_cnt != 1) ?
",$n_cnt" : '') .
639 my $display_head = $head;
640 unshift @
{$hunk->{TEXT
}}, $head;
641 if ($diff_use_color) {
642 $display_head = colored
($fraginfo_color, $head);
644 unshift @
{$hunk->{DISPLAY
}}, $display_head;
649 sub find_last_o_ctx
{
651 my $text = $it->{TEXT
};
652 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
654 my $last_o_ctx = $o_ofs + $o_cnt;
656 my $line = $text->[$i];
667 my ($prev, $this) = @_;
668 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
669 parse_hunk_header
($prev->{TEXT
}[0]);
670 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
671 parse_hunk_header
($this->{TEXT
}[0]);
673 my (@line, $i, $ofs, $o_cnt, $n_cnt);
676 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
677 my $line = $prev->{TEXT
}[$i];
678 if ($line =~ /^\+/) {
684 last if ($o1_ofs <= $ofs);
694 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
695 my $line = $this->{TEXT
}[$i];
696 if ($line =~ /^\+/) {
708 my $head = ("@@ -$o0_ofs" .
709 (($o_cnt != 1) ?
",$o_cnt" : '') .
711 (($n_cnt != 1) ?
",$n_cnt" : '') .
713 @
{$prev->{TEXT
}} = ($head, @line);
716 sub coalesce_overlapping_hunks
{
722 for (grep { $_->{USE
} } @in) {
723 my $text = $_->{TEXT
};
724 my ($o_ofs) = parse_hunk_header
($text->[0]);
725 if (defined $last_o_ctx &&
726 $o_ofs <= $last_o_ctx) {
727 merge_hunk
($out[-1], $_);
732 $last_o_ctx = find_last_o_ctx
($out[-1]);
738 print colored
$help_color, <<\EOF
;
740 n
- do not stage this hunk
741 a
- stage this
and all the remaining hunks
in the file
742 d
- do not stage this hunk nor any of the remaining hunks
in the file
743 j
- leave this hunk undecided
, see
next undecided hunk
744 J
- leave this hunk undecided
, see
next hunk
745 k
- leave this hunk undecided
, see previous undecided hunk
746 K
- leave this hunk undecided
, see previous hunk
747 s
- split the current hunk into smaller hunks
752 sub patch_update_cmd
{
753 my @mods = grep { !($_->{BINARY
}) } list_modified
('file-only');
757 print STDERR
"No changes.\n";
764 @them = list_and_choose
({ PROMPT
=> 'Patch update',
765 HEADER
=> $status_head, },
769 patch_update_file
($_->{VALUE
});
773 sub patch_update_file
{
776 my ($head, @hunk) = parse_diff
($path);
777 for (@
{$head->{DISPLAY
}}) {
784 my ($prev, $next, $other, $undecided, $i);
790 for ($i = 0; $i < $ix; $i++) {
791 if (!defined $hunk[$i]{USE
}) {
800 for ($i = $ix + 1; $i < $num; $i++) {
801 if (!defined $hunk[$i]{USE
}) {
807 if ($ix < $num - 1) {
810 for ($i = 0; $i < $num; $i++) {
811 if (!defined $hunk[$i]{USE
}) {
816 last if (!$undecided);
818 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
821 for (@
{$hunk[$ix]{DISPLAY
}}) {
824 print colored
$prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
827 if ($line =~ /^y/i) {
830 elsif ($line =~ /^n/i) {
833 elsif ($line =~ /^a/i) {
835 if (!defined $hunk[$ix]{USE
}) {
842 elsif ($line =~ /^d/i) {
844 if (!defined $hunk[$ix]{USE
}) {
851 elsif ($other =~ /K/ && $line =~ /^K/) {
855 elsif ($other =~ /J/ && $line =~ /^J/) {
859 elsif ($other =~ /k/ && $line =~ /^k/) {
863 !defined $hunk[$ix]{USE
});
867 elsif ($other =~ /j/ && $line =~ /^j/) {
870 last if ($ix >= $num ||
871 !defined $hunk[$ix]{USE
});
875 elsif ($other =~ /s/ && $line =~ /^s/) {
876 my @split = split_hunk
($hunk[$ix]{TEXT
}, $hunk[$ix]{DISPLAY
});
878 print colored
$header_color, "Split into ",
879 scalar(@split), " hunks.\n";
881 splice (@hunk, $ix, 1, @split);
886 help_patch_cmd
($other);
892 last if ($ix >= $num ||
893 !defined $hunk[$ix]{USE
});
898 @hunk = coalesce_overlapping_hunks
(@hunk);
903 my $text = $_->{TEXT
};
904 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
905 parse_hunk_header
($text->[0]);
908 # We would have added ($n_cnt - $o_cnt) lines
909 # to the postimage if we were to use this hunk,
910 # but we didn't. So the line number that the next
911 # hunk starts at would be shifted by that much.
912 $n_lofs -= ($n_cnt - $o_cnt);
918 $text->[0] = ("@@ -$o_ofs" .
935 open $fh, '| git apply --cached';
936 for (@
{$head->{TEXT
}}, @result) {
940 for (@
{$head->{TEXT
}}, @result) {
951 my @mods = list_modified
('index-only');
952 @mods = grep { !($_->{BINARY
}) } @mods;
954 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
956 HEADER
=> $status_head, },
959 system(qw(git diff -p --cached HEAD --), map { $_->{VALUE
} } @them);
968 print colored
$help_color, <<\EOF
;
969 status
- show paths with changes
970 update
- add working tree
state to the staged set of changes
971 revert
- revert staged set of changes back to the HEAD version
972 patch
- pick hunks
and update selectively
973 diff
- view diff between HEAD
and index
974 add untracked
- add contents of untracked files to the staged set of changes
980 my $arg = shift @ARGV;
981 if ($arg eq "--patch") {
983 $arg = shift @ARGV or die "missing --";
984 die "invalid argument $arg, expecting --"
987 elsif ($arg ne "--") {
988 die "invalid argument $arg, expecting --";
993 my @cmd = ([ 'status', \
&status_cmd
, ],
994 [ 'update', \
&update_cmd
, ],
995 [ 'revert', \
&revert_cmd
, ],
996 [ 'add untracked', \
&add_untracked_cmd
, ],
997 [ 'patch', \
&patch_update_cmd
, ],
998 [ 'diff', \
&diff_cmd
, ],
999 [ 'quit', \
&quit_cmd
, ],
1000 [ 'help', \
&help_cmd
, ],
1003 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
1006 HEADER
=> '*** Commands ***',
1007 ON_EOF
=> \
&quit_cmd
,
1008 IMMEDIATE
=> 1 }, @cmd);