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];
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 # given a prefix/remainder tuple return a string with the prefix highlighted
234 # for now use square brackets; later might use ANSI colors (underline, bold)
235 sub highlight_prefix
{
237 my $remainder = shift;
238 return (defined $prefix) ?
"[$prefix]$remainder" : $remainder;
241 sub list_and_choose
{
242 my ($opts, @stuff) = @_;
243 my (@chosen, @return);
245 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
251 if ($opts->{HEADER
}) {
252 if (!$opts->{LIST_FLAT
}) {
255 print "$opts->{HEADER}\n";
257 for ($i = 0; $i < @stuff; $i++) {
258 my $chosen = $chosen[$i] ?
'*' : ' ';
259 my $print = $stuff[$i];
261 if ((ref $print) eq 'ARRAY') {
263 highlight_prefix
(@
{$prefixes[$i]}) :
267 my $value = @prefixes ?
268 highlight_prefix
(@
{$prefixes[$i]}) :
270 $print = sprintf($status_fmt,
276 printf("%s%2d: %s", $chosen, $i+1, $print);
277 if (($opts->{LIST_FLAT
}) &&
278 (($i + 1) % ($opts->{LIST_FLAT
}))) {
291 return if ($opts->{LIST_ONLY
});
293 print $opts->{PROMPT
};
294 if ($opts->{SINGLETON
}) {
303 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
308 for my $choice (split(/[\s,]+/, $line)) {
312 # Input that begins with '-'; unchoose
313 if ($choice =~ s/^-//) {
316 # A range can be specified like 5-7
317 if ($choice =~ /^(\d+)-(\d+)$/) {
318 ($bottom, $top) = ($1, $2);
320 elsif ($choice =~ /^\d+$/) {
321 $bottom = $top = $choice;
323 elsif ($choice eq '*') {
328 $bottom = $top = find_unique
($choice, @stuff);
329 if (!defined $bottom) {
330 print "Huh ($choice)?\n";
334 if ($opts->{SINGLETON
} && $bottom != $top) {
335 print "Huh ($choice)?\n";
338 for ($i = $bottom-1; $i <= $top-1; $i++) {
339 next if (@stuff <= $i || $i < 0);
340 $chosen[$i] = $choose;
343 last if ($opts->{IMMEDIATE
} || $line eq '*');
345 for ($i = 0; $i < @stuff; $i++) {
347 push @return, $stuff[$i];
354 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
364 print "$cnt paths\n";
372 my @mods = list_modified
('file-only');
375 my @update = list_and_choose
({ PROMPT
=> 'Update',
376 HEADER
=> $status_head, },
379 system(qw(git update-index --add --remove --),
380 map { $_->{VALUE
} } @update);
381 say_n_paths
('updated', @update);
387 my @update = list_and_choose
({ PROMPT
=> 'Revert',
388 HEADER
=> $status_head, },
391 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
392 map { $_->{VALUE
} } @update);
394 open $fh, '| git update-index --index-info'
401 if ($_->{INDEX_ADDDEL
} &&
402 $_->{INDEX_ADDDEL
} eq 'create') {
403 system(qw(git update-index --force-remove --),
405 print "note: $_->{VALUE} is untracked now.\n";
409 say_n_paths
('reverted', @update);
414 sub add_untracked_cmd
{
415 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
418 system(qw(git update-index --add --), @add);
419 say_n_paths
('added', @add);
426 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
427 my (@hunk) = { TEXT
=> [] };
431 push @hunk, { TEXT
=> [] };
433 push @
{$hunk[-1]{TEXT
}}, $_;
438 sub hunk_splittable
{
441 my @s = split_hunk
($text);
445 sub parse_hunk_header
{
447 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
448 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
449 $o_cnt = 1 unless defined $o_cnt;
450 $n_cnt = 1 unless defined $n_cnt;
451 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
458 # If there are context lines in the middle of a hunk,
459 # it can be split, but we would need to take care of
462 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
467 my $next_hunk_start = undef;
468 my $i = $hunk_start - 1;
479 while (++$i < @
$text) {
480 my $line = $text->[$i];
482 if ($this->{ADDDEL
} &&
483 !defined $next_hunk_start) {
484 # We have seen leading context and
485 # adds/dels and then here is another
486 # context, which is trailing for this
487 # split hunk and leading for the next
489 $next_hunk_start = $i;
491 push @
{$this->{TEXT
}}, $line;
494 if (defined $next_hunk_start) {
501 if (defined $next_hunk_start) {
502 # We are done with the current hunk and
503 # this is the first real change for the
505 $hunk_start = $next_hunk_start;
506 $o_ofs = $this->{OLD
} + $this->{OCNT
};
507 $n_ofs = $this->{NEW
} + $this->{NCNT
};
508 $o_ofs -= $this->{POSTCTX
};
509 $n_ofs -= $this->{POSTCTX
};
513 push @
{$this->{TEXT
}}, $line;
527 for my $hunk (@split) {
528 $o_ofs = $hunk->{OLD
};
529 $n_ofs = $hunk->{NEW
};
530 my $o_cnt = $hunk->{OCNT
};
531 my $n_cnt = $hunk->{NCNT
};
533 my $head = ("@@ -$o_ofs" .
534 (($o_cnt != 1) ?
",$o_cnt" : '') .
536 (($n_cnt != 1) ?
",$n_cnt" : '') .
538 unshift @
{$hunk->{TEXT
}}, $head;
540 return map { $_->{TEXT
} } @split;
543 sub find_last_o_ctx
{
545 my $text = $it->{TEXT
};
546 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
548 my $last_o_ctx = $o_ofs + $o_cnt;
550 my $line = $text->[$i];
561 my ($prev, $this) = @_;
562 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
563 parse_hunk_header
($prev->{TEXT
}[0]);
564 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
565 parse_hunk_header
($this->{TEXT
}[0]);
567 my (@line, $i, $ofs, $o_cnt, $n_cnt);
570 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
571 my $line = $prev->{TEXT
}[$i];
572 if ($line =~ /^\+/) {
578 last if ($o1_ofs <= $ofs);
588 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
589 my $line = $this->{TEXT
}[$i];
590 if ($line =~ /^\+/) {
602 my $head = ("@@ -$o0_ofs" .
603 (($o_cnt != 1) ?
",$o_cnt" : '') .
605 (($n_cnt != 1) ?
",$n_cnt" : '') .
607 @
{$prev->{TEXT
}} = ($head, @line);
610 sub coalesce_overlapping_hunks
{
616 for (grep { $_->{USE
} } @in) {
617 my $text = $_->{TEXT
};
618 my ($o_ofs) = parse_hunk_header
($text->[0]);
619 if (defined $last_o_ctx &&
620 $o_ofs <= $last_o_ctx) {
621 merge_hunk
($out[-1], $_);
626 $last_o_ctx = find_last_o_ctx
($out[-1]);
634 n
- do not stage this hunk
635 a
- stage this
and all the remaining hunks
in the file
636 d
- do not stage this hunk nor any of the remaining hunks
in the file
637 j
- leave this hunk undecided
, see
next undecided hunk
638 J
- leave this hunk undecided
, see
next hunk
639 k
- leave this hunk undecided
, see previous undecided hunk
640 K
- leave this hunk undecided
, see previous hunk
641 s
- split the current hunk into smaller hunks
646 sub patch_update_cmd
{
647 my @mods = grep { !($_->{BINARY
}) } list_modified
('file-only');
651 print STDERR
"No changes.\n";
658 @them = list_and_choose
({ PROMPT
=> 'Patch update',
659 HEADER
=> $status_head, },
663 patch_update_file
($_->{VALUE
});
667 sub patch_update_file
{
670 my ($head, @hunk) = parse_diff
($path);
671 for (@
{$head->{TEXT
}}) {
678 my ($prev, $next, $other, $undecided, $i);
684 for ($i = 0; $i < $ix; $i++) {
685 if (!defined $hunk[$i]{USE
}) {
694 for ($i = $ix + 1; $i < $num; $i++) {
695 if (!defined $hunk[$i]{USE
}) {
701 if ($ix < $num - 1) {
704 for ($i = 0; $i < $num; $i++) {
705 if (!defined $hunk[$i]{USE
}) {
710 last if (!$undecided);
712 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
715 for (@
{$hunk[$ix]{TEXT
}}) {
718 print "Stage this hunk [y/n/a/d$other/?]? ";
721 if ($line =~ /^y/i) {
724 elsif ($line =~ /^n/i) {
727 elsif ($line =~ /^a/i) {
729 if (!defined $hunk[$ix]{USE
}) {
736 elsif ($line =~ /^d/i) {
738 if (!defined $hunk[$ix]{USE
}) {
745 elsif ($other =~ /K/ && $line =~ /^K/) {
749 elsif ($other =~ /J/ && $line =~ /^J/) {
753 elsif ($other =~ /k/ && $line =~ /^k/) {
757 !defined $hunk[$ix]{USE
});
761 elsif ($other =~ /j/ && $line =~ /^j/) {
764 last if ($ix >= $num ||
765 !defined $hunk[$ix]{USE
});
769 elsif ($other =~ /s/ && $line =~ /^s/) {
770 my @split = split_hunk
($hunk[$ix]{TEXT
});
773 scalar(@split), " hunks.\n";
775 splice(@hunk, $ix, 1,
776 map { +{ TEXT
=> $_, USE
=> undef } }
782 help_patch_cmd
($other);
788 last if ($ix >= $num ||
789 !defined $hunk[$ix]{USE
});
794 @hunk = coalesce_overlapping_hunks
(@hunk);
799 my $text = $_->{TEXT
};
800 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
801 parse_hunk_header
($text->[0]);
804 # We would have added ($n_cnt - $o_cnt) lines
805 # to the postimage if we were to use this hunk,
806 # but we didn't. So the line number that the next
807 # hunk starts at would be shifted by that much.
808 $n_lofs -= ($n_cnt - $o_cnt);
814 $text->[0] = ("@@ -$o_ofs" .
831 open $fh, '| git apply --cached';
832 for (@
{$head->{TEXT
}}, @result) {
836 for (@
{$head->{TEXT
}}, @result) {
847 my @mods = list_modified
('index-only');
848 @mods = grep { !($_->{BINARY
}) } @mods;
850 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
852 HEADER
=> $status_head, },
855 system(qw(git diff-index -p --cached HEAD --),
856 map { $_->{VALUE
} } @them);
866 status
- show paths with changes
867 update
- add working tree
state to the staged set of changes
868 revert
- revert staged set of changes back to the HEAD version
869 patch
- pick hunks
and update selectively
870 diff
- view diff between HEAD
and index
871 add untracked
- add contents of untracked files to the staged set of changes
877 my $arg = shift @ARGV;
878 if ($arg eq "--patch") {
880 $arg = shift @ARGV or die "missing --";
881 die "invalid argument $arg, expecting --"
884 elsif ($arg ne "--") {
885 die "invalid argument $arg, expecting --";
890 my @cmd = ([ 'status', \
&status_cmd
, ],
891 [ 'update', \
&update_cmd
, ],
892 [ 'revert', \
&revert_cmd
, ],
893 [ 'add untracked', \
&add_untracked_cmd
, ],
894 [ 'patch', \
&patch_update_cmd
, ],
895 [ 'diff', \
&diff_cmd
, ],
896 [ 'quit', \
&quit_cmd
, ],
897 [ 'help', \
&help_cmd
, ],
900 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
903 HEADER
=> '*** Commands ***',
904 ON_EOF
=> \
&quit_cmd
,
905 IMMEDIATE
=> 1 }, @cmd);