9 if ($^O
eq 'MSWin32') {
10 my @invalid = grep {m/[":*]/} @_;
11 die "$^O does not support: @invalid\n" if @invalid;
12 my @args = map { m/ /o ?
"\"$_\"": $_ } @_;
16 open($fh, '-|', @_) or die;
21 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
23 if (!defined $GIT_DIR) {
24 exit(1); # rev-parse would have already said "not a git repo"
30 open $fh, 'git update-index --refresh |'
33 ;# ignore 'needs update'
43 run_cmd_pipe
(qw(git ls-files --others --exclude-standard --), @ARGV);
46 my $status_fmt = '%12s %12s %s';
47 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
49 # Returns list of hashes, contents of each of which are:
51 # BINARY: is a binary path
52 # INDEX: is index different from HEAD?
53 # FILE: is file different from index?
54 # INDEX_ADDDEL: is it add/delete between HEAD and index?
55 # FILE_ADDDEL: is it add/delete between index and file?
60 my ($add, $del, $adddel, $file);
66 } run_cmd_pipe
(qw(git ls-files --exclude-standard --), @ARGV);
67 return if (!@tracked);
70 for (run_cmd_pipe
(qw(git diff-index --cached
71 --numstat --summary HEAD --), @tracked)) {
72 if (($add, $del, $file) =
73 /^([-\d]+) ([-\d]+) (.*)/) {
75 if ($add eq '-' && $del eq '-') {
80 $change = "+$add/-$del";
88 elsif (($adddel, $file) =
89 /^ (create|delete) mode [0-7]+ (.*)$/) {
90 $data{$file}{INDEX_ADDDEL
} = $adddel;
94 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
95 if (($add, $del, $file) =
96 /^([-\d]+) ([-\d]+) (.*)/) {
97 if (!exists $data{$file}) {
104 if ($add eq '-' && $del eq '-') {
109 $change = "+$add/-$del";
111 $data{$file}{FILE
} = $change;
113 $data{$file}{BINARY
} = 1;
116 elsif (($adddel, $file) =
117 /^ (create|delete) mode [0-7]+ (.*)$/) {
118 $data{$file}{FILE_ADDDEL
} = $adddel;
122 for (sort keys %data) {
126 if ($only eq 'index-only') {
127 next if ($it->{INDEX
} eq 'unchanged');
129 if ($only eq 'file-only') {
130 next if ($it->{FILE
} eq 'nothing');
142 my ($string, @stuff) = @_;
144 for (my $i = 0; $i < @stuff; $i++) {
148 if ((ref $it) eq 'ARRAY') {
156 if ($it =~ /^$string/) {
160 if (defined $hit && defined $found) {
170 # inserts string into trie and updates count for each character
172 my ($trie, $string) = @_;
173 foreach (split //, $string) {
174 $trie = $trie->{$_} ||= {COUNT
=> 0};
179 # returns an array of tuples (prefix, remainder)
180 sub find_unique_prefixes
{
184 # any single prefix exceeding the soft limit is omitted
185 # if any prefix exceeds the hard limit all are omitted
186 # 0 indicates no limit
190 # build a trie modelling all possible options
192 foreach my $print (@stuff) {
193 if ((ref $print) eq 'ARRAY') {
194 $print = $print->[0];
196 elsif ((ref $print) eq 'HASH') {
197 $print = $print->{VALUE
};
199 update_trie
(\
%trie, $print);
200 push @return, $print;
203 # use the trie to find the unique prefixes
204 for (my $i = 0; $i < @return; $i++) {
205 my $ret = $return[$i];
206 my @letters = split //, $ret;
208 my ($prefix, $remainder);
210 for ($j = 0; $j < @letters; $j++) {
211 my $letter = $letters[$j];
212 if ($search{$letter}{COUNT
} == 1) {
213 $prefix = substr $ret, 0, $j + 1;
214 $remainder = substr $ret, $j + 1;
218 my $prefix = substr $ret, 0, $j;
220 if ($hard_limit && $j + 1 > $hard_limit);
222 %search = %{$search{$letter}};
224 if ($soft_limit && $j + 1 > $soft_limit) {
228 $return[$i] = [$prefix, $remainder];
233 # filters out prefixes which have special meaning to list_and_choose()
234 sub is_valid_prefix
{
236 return (defined $prefix) &&
237 !($prefix =~ /[\s,]/) && # separators
238 !($prefix =~ /^-/) && # deselection
239 !($prefix =~ /^\d+/) && # selection
240 ($prefix ne '*') && # "all" wildcard
241 ($prefix ne '?'); # prompt help
244 # given a prefix/remainder tuple return a string with the prefix highlighted
245 # for now use square brackets; later might use ANSI colors (underline, bold)
246 sub highlight_prefix
{
248 my $remainder = shift;
249 return $remainder unless defined $prefix;
250 return is_valid_prefix
($prefix) ?
251 "[$prefix]$remainder" :
255 sub list_and_choose
{
256 my ($opts, @stuff) = @_;
257 my (@chosen, @return);
259 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
265 if ($opts->{HEADER
}) {
266 if (!$opts->{LIST_FLAT
}) {
269 print "$opts->{HEADER}\n";
271 for ($i = 0; $i < @stuff; $i++) {
272 my $chosen = $chosen[$i] ?
'*' : ' ';
273 my $print = $stuff[$i];
274 my $ref = ref $print;
275 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
277 if ($ref eq 'ARRAY') {
278 $print = $highlighted || $print->[0];
280 elsif ($ref eq 'HASH') {
281 my $value = $highlighted || $print->{VALUE
};
282 $print = sprintf($status_fmt,
288 $print = $highlighted || $print;
290 printf("%s%2d: %s", $chosen, $i+1, $print);
291 if (($opts->{LIST_FLAT
}) &&
292 (($i + 1) % ($opts->{LIST_FLAT
}))) {
305 return if ($opts->{LIST_ONLY
});
307 print $opts->{PROMPT
};
308 if ($opts->{SINGLETON
}) {
317 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
324 singleton_prompt_help_cmd
() :
328 for my $choice (split(/[\s,]+/, $line)) {
332 # Input that begins with '-'; unchoose
333 if ($choice =~ s/^-//) {
336 # A range can be specified like 5-7
337 if ($choice =~ /^(\d+)-(\d+)$/) {
338 ($bottom, $top) = ($1, $2);
340 elsif ($choice =~ /^\d+$/) {
341 $bottom = $top = $choice;
343 elsif ($choice eq '*') {
348 $bottom = $top = find_unique
($choice, @stuff);
349 if (!defined $bottom) {
350 print "Huh ($choice)?\n";
354 if ($opts->{SINGLETON
} && $bottom != $top) {
355 print "Huh ($choice)?\n";
358 for ($i = $bottom-1; $i <= $top-1; $i++) {
359 next if (@stuff <= $i || $i < 0);
360 $chosen[$i] = $choose;
363 last if ($opts->{IMMEDIATE
} || $line eq '*');
365 for ($i = 0; $i < @stuff; $i++) {
367 push @return, $stuff[$i];
373 sub singleton_prompt_help_cmd
{
376 1 - select a numbered item
377 foo
- select item based on unique prefix
378 - (empty
) select nothing
382 sub prompt_help_cmd
{
385 1 - select a single item
386 3-5 - select a range of items
387 2-3,6-9 - select multiple ranges
388 foo
- select item based on unique prefix
389 -... - unselect specified items
391 - (empty
) finish selecting
396 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
406 print "$cnt paths\n";
414 my @mods = list_modified
('file-only');
417 my @update = list_and_choose
({ PROMPT
=> 'Update',
418 HEADER
=> $status_head, },
421 system(qw(git update-index --add --remove --),
422 map { $_->{VALUE
} } @update);
423 say_n_paths
('updated', @update);
429 my @update = list_and_choose
({ PROMPT
=> 'Revert',
430 HEADER
=> $status_head, },
433 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
434 map { $_->{VALUE
} } @update);
436 open $fh, '| git update-index --index-info'
443 if ($_->{INDEX_ADDDEL
} &&
444 $_->{INDEX_ADDDEL
} eq 'create') {
445 system(qw(git update-index --force-remove --),
447 print "note: $_->{VALUE} is untracked now.\n";
451 say_n_paths
('reverted', @update);
456 sub add_untracked_cmd
{
457 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
460 system(qw(git update-index --add --), @add);
461 say_n_paths
('added', @add);
468 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
469 my (@hunk) = { TEXT
=> [] };
473 push @hunk, { TEXT
=> [] };
475 push @
{$hunk[-1]{TEXT
}}, $_;
480 sub hunk_splittable
{
483 my @s = split_hunk
($text);
487 sub parse_hunk_header
{
489 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
490 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
491 $o_cnt = 1 unless defined $o_cnt;
492 $n_cnt = 1 unless defined $n_cnt;
493 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
500 # If there are context lines in the middle of a hunk,
501 # it can be split, but we would need to take care of
504 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
509 my $next_hunk_start = undef;
510 my $i = $hunk_start - 1;
521 while (++$i < @
$text) {
522 my $line = $text->[$i];
524 if ($this->{ADDDEL
} &&
525 !defined $next_hunk_start) {
526 # We have seen leading context and
527 # adds/dels and then here is another
528 # context, which is trailing for this
529 # split hunk and leading for the next
531 $next_hunk_start = $i;
533 push @
{$this->{TEXT
}}, $line;
536 if (defined $next_hunk_start) {
543 if (defined $next_hunk_start) {
544 # We are done with the current hunk and
545 # this is the first real change for the
547 $hunk_start = $next_hunk_start;
548 $o_ofs = $this->{OLD
} + $this->{OCNT
};
549 $n_ofs = $this->{NEW
} + $this->{NCNT
};
550 $o_ofs -= $this->{POSTCTX
};
551 $n_ofs -= $this->{POSTCTX
};
555 push @
{$this->{TEXT
}}, $line;
569 for my $hunk (@split) {
570 $o_ofs = $hunk->{OLD
};
571 $n_ofs = $hunk->{NEW
};
572 my $o_cnt = $hunk->{OCNT
};
573 my $n_cnt = $hunk->{NCNT
};
575 my $head = ("@@ -$o_ofs" .
576 (($o_cnt != 1) ?
",$o_cnt" : '') .
578 (($n_cnt != 1) ?
",$n_cnt" : '') .
580 unshift @
{$hunk->{TEXT
}}, $head;
582 return map { $_->{TEXT
} } @split;
585 sub find_last_o_ctx
{
587 my $text = $it->{TEXT
};
588 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
590 my $last_o_ctx = $o_ofs + $o_cnt;
592 my $line = $text->[$i];
603 my ($prev, $this) = @_;
604 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
605 parse_hunk_header
($prev->{TEXT
}[0]);
606 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
607 parse_hunk_header
($this->{TEXT
}[0]);
609 my (@line, $i, $ofs, $o_cnt, $n_cnt);
612 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
613 my $line = $prev->{TEXT
}[$i];
614 if ($line =~ /^\+/) {
620 last if ($o1_ofs <= $ofs);
630 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
631 my $line = $this->{TEXT
}[$i];
632 if ($line =~ /^\+/) {
644 my $head = ("@@ -$o0_ofs" .
645 (($o_cnt != 1) ?
",$o_cnt" : '') .
647 (($n_cnt != 1) ?
",$n_cnt" : '') .
649 @
{$prev->{TEXT
}} = ($head, @line);
652 sub coalesce_overlapping_hunks
{
658 for (grep { $_->{USE
} } @in) {
659 my $text = $_->{TEXT
};
660 my ($o_ofs) = parse_hunk_header
($text->[0]);
661 if (defined $last_o_ctx &&
662 $o_ofs <= $last_o_ctx) {
663 merge_hunk
($out[-1], $_);
668 $last_o_ctx = find_last_o_ctx
($out[-1]);
676 n
- do not stage this hunk
677 a
- stage this
and all the remaining hunks
in the file
678 d
- do not stage this hunk nor any of the remaining hunks
in the file
679 j
- leave this hunk undecided
, see
next undecided hunk
680 J
- leave this hunk undecided
, see
next hunk
681 k
- leave this hunk undecided
, see previous undecided hunk
682 K
- leave this hunk undecided
, see previous hunk
683 s
- split the current hunk into smaller hunks
688 sub patch_update_cmd
{
689 my @mods = grep { !($_->{BINARY
}) } list_modified
('file-only');
693 print STDERR
"No changes.\n";
700 @them = list_and_choose
({ PROMPT
=> 'Patch update',
701 HEADER
=> $status_head, },
705 patch_update_file
($_->{VALUE
});
709 sub patch_update_file
{
712 my ($head, @hunk) = parse_diff
($path);
713 for (@
{$head->{TEXT
}}) {
720 my ($prev, $next, $other, $undecided, $i);
726 for ($i = 0; $i < $ix; $i++) {
727 if (!defined $hunk[$i]{USE
}) {
736 for ($i = $ix + 1; $i < $num; $i++) {
737 if (!defined $hunk[$i]{USE
}) {
743 if ($ix < $num - 1) {
746 for ($i = 0; $i < $num; $i++) {
747 if (!defined $hunk[$i]{USE
}) {
752 last if (!$undecided);
754 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
757 for (@
{$hunk[$ix]{TEXT
}}) {
760 print "Stage this hunk [y/n/a/d$other/?]? ";
763 if ($line =~ /^y/i) {
766 elsif ($line =~ /^n/i) {
769 elsif ($line =~ /^a/i) {
771 if (!defined $hunk[$ix]{USE
}) {
778 elsif ($line =~ /^d/i) {
780 if (!defined $hunk[$ix]{USE
}) {
787 elsif ($other =~ /K/ && $line =~ /^K/) {
791 elsif ($other =~ /J/ && $line =~ /^J/) {
795 elsif ($other =~ /k/ && $line =~ /^k/) {
799 !defined $hunk[$ix]{USE
});
803 elsif ($other =~ /j/ && $line =~ /^j/) {
806 last if ($ix >= $num ||
807 !defined $hunk[$ix]{USE
});
811 elsif ($other =~ /s/ && $line =~ /^s/) {
812 my @split = split_hunk
($hunk[$ix]{TEXT
});
815 scalar(@split), " hunks.\n";
817 splice(@hunk, $ix, 1,
818 map { +{ TEXT
=> $_, USE
=> undef } }
824 help_patch_cmd
($other);
830 last if ($ix >= $num ||
831 !defined $hunk[$ix]{USE
});
836 @hunk = coalesce_overlapping_hunks
(@hunk);
841 my $text = $_->{TEXT
};
842 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
843 parse_hunk_header
($text->[0]);
846 # We would have added ($n_cnt - $o_cnt) lines
847 # to the postimage if we were to use this hunk,
848 # but we didn't. So the line number that the next
849 # hunk starts at would be shifted by that much.
850 $n_lofs -= ($n_cnt - $o_cnt);
856 $text->[0] = ("@@ -$o_ofs" .
873 open $fh, '| git apply --cached';
874 for (@
{$head->{TEXT
}}, @result) {
878 for (@
{$head->{TEXT
}}, @result) {
889 my @mods = list_modified
('index-only');
890 @mods = grep { !($_->{BINARY
}) } @mods;
892 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
894 HEADER
=> $status_head, },
897 system(qw(git diff-index -p --cached HEAD --),
898 map { $_->{VALUE
} } @them);
908 status
- show paths with changes
909 update
- add working tree
state to the staged set of changes
910 revert
- revert staged set of changes back to the HEAD version
911 patch
- pick hunks
and update selectively
912 diff
- view diff between HEAD
and index
913 add untracked
- add contents of untracked files to the staged set of changes
919 my $arg = shift @ARGV;
920 if ($arg eq "--patch") {
922 $arg = shift @ARGV or die "missing --";
923 die "invalid argument $arg, expecting --"
926 elsif ($arg ne "--") {
927 die "invalid argument $arg, expecting --";
932 my @cmd = ([ 'status', \
&status_cmd
, ],
933 [ 'update', \
&update_cmd
, ],
934 [ 'revert', \
&revert_cmd
, ],
935 [ 'add untracked', \
&add_untracked_cmd
, ],
936 [ 'patch', \
&patch_update_cmd
, ],
937 [ 'diff', \
&diff_cmd
, ],
938 [ 'quit', \
&quit_cmd
, ],
939 [ 'help', \
&help_cmd
, ],
942 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
945 HEADER
=> '*** Commands ***',
946 ON_EOF
=> \
&quit_cmd
,
947 IMMEDIATE
=> 1 }, @cmd);