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
243 # given a prefix/remainder tuple return a string with the prefix highlighted
244 # for now use square brackets; later might use ANSI colors (underline, bold)
245 sub highlight_prefix
{
247 my $remainder = shift;
248 return $remainder unless defined $prefix;
249 return is_valid_prefix
($prefix) ?
250 "[$prefix]$remainder" :
254 sub list_and_choose
{
255 my ($opts, @stuff) = @_;
256 my (@chosen, @return);
258 my @prefixes = find_unique_prefixes
(@stuff) unless $opts->{LIST_ONLY
};
264 if ($opts->{HEADER
}) {
265 if (!$opts->{LIST_FLAT
}) {
268 print "$opts->{HEADER}\n";
270 for ($i = 0; $i < @stuff; $i++) {
271 my $chosen = $chosen[$i] ?
'*' : ' ';
272 my $print = $stuff[$i];
273 my $ref = ref $print;
274 my $highlighted = highlight_prefix
(@
{$prefixes[$i]})
276 if ($ref eq 'ARRAY') {
277 $print = $highlighted || $print->[0];
279 elsif ($ref eq 'HASH') {
280 my $value = $highlighted || $print->{VALUE
};
281 $print = sprintf($status_fmt,
287 $print = $highlighted || $print;
289 printf("%s%2d: %s", $chosen, $i+1, $print);
290 if (($opts->{LIST_FLAT
}) &&
291 (($i + 1) % ($opts->{LIST_FLAT
}))) {
304 return if ($opts->{LIST_ONLY
});
306 print $opts->{PROMPT
};
307 if ($opts->{SINGLETON
}) {
316 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
321 for my $choice (split(/[\s,]+/, $line)) {
325 # Input that begins with '-'; unchoose
326 if ($choice =~ s/^-//) {
329 # A range can be specified like 5-7
330 if ($choice =~ /^(\d+)-(\d+)$/) {
331 ($bottom, $top) = ($1, $2);
333 elsif ($choice =~ /^\d+$/) {
334 $bottom = $top = $choice;
336 elsif ($choice eq '*') {
341 $bottom = $top = find_unique
($choice, @stuff);
342 if (!defined $bottom) {
343 print "Huh ($choice)?\n";
347 if ($opts->{SINGLETON
} && $bottom != $top) {
348 print "Huh ($choice)?\n";
351 for ($i = $bottom-1; $i <= $top-1; $i++) {
352 next if (@stuff <= $i || $i < 0);
353 $chosen[$i] = $choose;
356 last if ($opts->{IMMEDIATE
} || $line eq '*');
358 for ($i = 0; $i < @stuff; $i++) {
360 push @return, $stuff[$i];
367 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
377 print "$cnt paths\n";
385 my @mods = list_modified
('file-only');
388 my @update = list_and_choose
({ PROMPT
=> 'Update',
389 HEADER
=> $status_head, },
392 system(qw(git update-index --add --remove --),
393 map { $_->{VALUE
} } @update);
394 say_n_paths
('updated', @update);
400 my @update = list_and_choose
({ PROMPT
=> 'Revert',
401 HEADER
=> $status_head, },
404 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
405 map { $_->{VALUE
} } @update);
407 open $fh, '| git update-index --index-info'
414 if ($_->{INDEX_ADDDEL
} &&
415 $_->{INDEX_ADDDEL
} eq 'create') {
416 system(qw(git update-index --force-remove --),
418 print "note: $_->{VALUE} is untracked now.\n";
422 say_n_paths
('reverted', @update);
427 sub add_untracked_cmd
{
428 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
431 system(qw(git update-index --add --), @add);
432 say_n_paths
('added', @add);
439 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
440 my (@hunk) = { TEXT
=> [] };
444 push @hunk, { TEXT
=> [] };
446 push @
{$hunk[-1]{TEXT
}}, $_;
451 sub hunk_splittable
{
454 my @s = split_hunk
($text);
458 sub parse_hunk_header
{
460 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
461 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
462 $o_cnt = 1 unless defined $o_cnt;
463 $n_cnt = 1 unless defined $n_cnt;
464 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
471 # If there are context lines in the middle of a hunk,
472 # it can be split, but we would need to take care of
475 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
480 my $next_hunk_start = undef;
481 my $i = $hunk_start - 1;
492 while (++$i < @
$text) {
493 my $line = $text->[$i];
495 if ($this->{ADDDEL
} &&
496 !defined $next_hunk_start) {
497 # We have seen leading context and
498 # adds/dels and then here is another
499 # context, which is trailing for this
500 # split hunk and leading for the next
502 $next_hunk_start = $i;
504 push @
{$this->{TEXT
}}, $line;
507 if (defined $next_hunk_start) {
514 if (defined $next_hunk_start) {
515 # We are done with the current hunk and
516 # this is the first real change for the
518 $hunk_start = $next_hunk_start;
519 $o_ofs = $this->{OLD
} + $this->{OCNT
};
520 $n_ofs = $this->{NEW
} + $this->{NCNT
};
521 $o_ofs -= $this->{POSTCTX
};
522 $n_ofs -= $this->{POSTCTX
};
526 push @
{$this->{TEXT
}}, $line;
540 for my $hunk (@split) {
541 $o_ofs = $hunk->{OLD
};
542 $n_ofs = $hunk->{NEW
};
543 my $o_cnt = $hunk->{OCNT
};
544 my $n_cnt = $hunk->{NCNT
};
546 my $head = ("@@ -$o_ofs" .
547 (($o_cnt != 1) ?
",$o_cnt" : '') .
549 (($n_cnt != 1) ?
",$n_cnt" : '') .
551 unshift @
{$hunk->{TEXT
}}, $head;
553 return map { $_->{TEXT
} } @split;
556 sub find_last_o_ctx
{
558 my $text = $it->{TEXT
};
559 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
561 my $last_o_ctx = $o_ofs + $o_cnt;
563 my $line = $text->[$i];
574 my ($prev, $this) = @_;
575 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
576 parse_hunk_header
($prev->{TEXT
}[0]);
577 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
578 parse_hunk_header
($this->{TEXT
}[0]);
580 my (@line, $i, $ofs, $o_cnt, $n_cnt);
583 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
584 my $line = $prev->{TEXT
}[$i];
585 if ($line =~ /^\+/) {
591 last if ($o1_ofs <= $ofs);
601 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
602 my $line = $this->{TEXT
}[$i];
603 if ($line =~ /^\+/) {
615 my $head = ("@@ -$o0_ofs" .
616 (($o_cnt != 1) ?
",$o_cnt" : '') .
618 (($n_cnt != 1) ?
",$n_cnt" : '') .
620 @
{$prev->{TEXT
}} = ($head, @line);
623 sub coalesce_overlapping_hunks
{
629 for (grep { $_->{USE
} } @in) {
630 my $text = $_->{TEXT
};
631 my ($o_ofs) = parse_hunk_header
($text->[0]);
632 if (defined $last_o_ctx &&
633 $o_ofs <= $last_o_ctx) {
634 merge_hunk
($out[-1], $_);
639 $last_o_ctx = find_last_o_ctx
($out[-1]);
647 n
- do not stage this hunk
648 a
- stage this
and all the remaining hunks
in the file
649 d
- do not stage this hunk nor any of the remaining hunks
in the file
650 j
- leave this hunk undecided
, see
next undecided hunk
651 J
- leave this hunk undecided
, see
next hunk
652 k
- leave this hunk undecided
, see previous undecided hunk
653 K
- leave this hunk undecided
, see previous hunk
654 s
- split the current hunk into smaller hunks
659 sub patch_update_cmd
{
660 my @mods = grep { !($_->{BINARY
}) } list_modified
('file-only');
664 print STDERR
"No changes.\n";
671 @them = list_and_choose
({ PROMPT
=> 'Patch update',
672 HEADER
=> $status_head, },
676 patch_update_file
($_->{VALUE
});
680 sub patch_update_file
{
683 my ($head, @hunk) = parse_diff
($path);
684 for (@
{$head->{TEXT
}}) {
691 my ($prev, $next, $other, $undecided, $i);
697 for ($i = 0; $i < $ix; $i++) {
698 if (!defined $hunk[$i]{USE
}) {
707 for ($i = $ix + 1; $i < $num; $i++) {
708 if (!defined $hunk[$i]{USE
}) {
714 if ($ix < $num - 1) {
717 for ($i = 0; $i < $num; $i++) {
718 if (!defined $hunk[$i]{USE
}) {
723 last if (!$undecided);
725 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
728 for (@
{$hunk[$ix]{TEXT
}}) {
731 print "Stage this hunk [y/n/a/d$other/?]? ";
734 if ($line =~ /^y/i) {
737 elsif ($line =~ /^n/i) {
740 elsif ($line =~ /^a/i) {
742 if (!defined $hunk[$ix]{USE
}) {
749 elsif ($line =~ /^d/i) {
751 if (!defined $hunk[$ix]{USE
}) {
758 elsif ($other =~ /K/ && $line =~ /^K/) {
762 elsif ($other =~ /J/ && $line =~ /^J/) {
766 elsif ($other =~ /k/ && $line =~ /^k/) {
770 !defined $hunk[$ix]{USE
});
774 elsif ($other =~ /j/ && $line =~ /^j/) {
777 last if ($ix >= $num ||
778 !defined $hunk[$ix]{USE
});
782 elsif ($other =~ /s/ && $line =~ /^s/) {
783 my @split = split_hunk
($hunk[$ix]{TEXT
});
786 scalar(@split), " hunks.\n";
788 splice(@hunk, $ix, 1,
789 map { +{ TEXT
=> $_, USE
=> undef } }
795 help_patch_cmd
($other);
801 last if ($ix >= $num ||
802 !defined $hunk[$ix]{USE
});
807 @hunk = coalesce_overlapping_hunks
(@hunk);
812 my $text = $_->{TEXT
};
813 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
814 parse_hunk_header
($text->[0]);
817 # We would have added ($n_cnt - $o_cnt) lines
818 # to the postimage if we were to use this hunk,
819 # but we didn't. So the line number that the next
820 # hunk starts at would be shifted by that much.
821 $n_lofs -= ($n_cnt - $o_cnt);
827 $text->[0] = ("@@ -$o_ofs" .
844 open $fh, '| git apply --cached';
845 for (@
{$head->{TEXT
}}, @result) {
849 for (@
{$head->{TEXT
}}, @result) {
860 my @mods = list_modified
('index-only');
861 @mods = grep { !($_->{BINARY
}) } @mods;
863 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
865 HEADER
=> $status_head, },
868 system(qw(git diff-index -p --cached HEAD --),
869 map { $_->{VALUE
} } @them);
879 status
- show paths with changes
880 update
- add working tree
state to the staged set of changes
881 revert
- revert staged set of changes back to the HEAD version
882 patch
- pick hunks
and update selectively
883 diff
- view diff between HEAD
and index
884 add untracked
- add contents of untracked files to the staged set of changes
890 my $arg = shift @ARGV;
891 if ($arg eq "--patch") {
893 $arg = shift @ARGV or die "missing --";
894 die "invalid argument $arg, expecting --"
897 elsif ($arg ne "--") {
898 die "invalid argument $arg, expecting --";
903 my @cmd = ([ 'status', \
&status_cmd
, ],
904 [ 'update', \
&update_cmd
, ],
905 [ 'revert', \
&revert_cmd
, ],
906 [ 'add untracked', \
&add_untracked_cmd
, ],
907 [ 'patch', \
&patch_update_cmd
, ],
908 [ 'diff', \
&diff_cmd
, ],
909 [ 'quit', \
&quit_cmd
, ],
910 [ 'help', \
&help_cmd
, ],
913 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
916 HEADER
=> '*** Commands ***',
917 ON_EOF
=> \
&quit_cmd
,
918 IMMEDIATE
=> 1 }, @cmd);