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:
50 # PRINT: print message
52 # BINARY: is a binary path
53 # INDEX: is index different from HEAD?
54 # FILE: is file different from index?
55 # INDEX_ADDDEL: is it add/delete between HEAD and index?
56 # FILE_ADDDEL: is it add/delete between index and file?
61 my ($add, $del, $adddel, $file);
67 } run_cmd_pipe(qw(git ls-files --exclude-standard --), @ARGV);
68 return if (!@tracked);
71 for (run_cmd_pipe(qw(git diff-index --cached
72 --numstat --summary HEAD --), @tracked)) {
73 if (($add, $del, $file) =
74 /^([-\d]+) ([-\d]+) (.*)/) {
76 if ($add eq '-' && $del eq '-') {
81 $change = "+$add/-$del";
89 elsif (($adddel, $file) =
90 /^ (create|delete) mode [0-7]+ (.*)$/) {
91 $data{$file}{INDEX_ADDDEL} = $adddel;
95 for (run_cmd_pipe(qw(git diff-files --numstat --summary --), @tracked)) {
96 if (($add, $del, $file) =
97 /^([-\d]+) ([-\d]+) (.*)/) {
98 if (!exists $data{$file}) {
100 INDEX => 'unchanged',
105 if ($add eq '-' && $del eq '-') {
110 $change = "+$add/-$del";
112 $data{$file}{FILE} = $change;
114 $data{$file}{BINARY} = 1;
117 elsif (($adddel, $file) =
118 /^ (create|delete) mode [0-7]+ (.*)$/) {
119 $data{$file}{FILE_ADDDEL} = $adddel;
123 for (sort keys %data) {
127 if ($only eq 'index-only') {
128 next if ($it->{INDEX} eq 'unchanged');
130 if ($only eq 'file-only') {
131 next if ($it->{FILE} eq 'nothing');
136 PRINT => (sprintf $status_fmt,
137 $it->{INDEX}, $it->{FILE}, $_),
145 my ($string, @stuff) = @_;
147 for (my $i = 0; $i < @stuff; $i++) {
151 if ((ref $it) eq 'ARRAY') {
159 if ($it =~ /^$string/) {
163 if (defined $hit && defined $found) {
173 sub list_and_choose {
174 my ($opts, @stuff) = @_;
175 my (@chosen, @return);
182 if ($opts->{HEADER}) {
183 if (!$opts->{LIST_FLAT}) {
186 print "$opts->{HEADER}\n";
188 for ($i = 0; $i < @stuff; $i++) {
189 my $chosen = $chosen[$i] ? '*' : ' ';
190 my $print = $stuff[$i];
192 if ((ref $print) eq 'ARRAY') {
193 $print = $print->[0];
196 $print = $print->{PRINT};
199 printf("%s%2d: %s", $chosen, $i+1, $print);
200 if (($opts->{LIST_FLAT}) &&
201 (($i + 1) % ($opts->{LIST_FLAT}))) {
214 return if ($opts->{LIST_ONLY});
216 print $opts->{PROMPT};
217 if ($opts->{SINGLETON}) {
226 $opts->{ON_EOF}->() if $opts->{ON_EOF};
231 for my $choice (split(/[\s,]+/, $line)) {
235 # Input that begins with '-'; unchoose
236 if ($choice =~ s/^-//) {
239 # A range can be specified like 5-7
240 if ($choice =~ /^(\d+)-(\d+)$/) {
241 ($bottom, $top) = ($1, $2);
243 elsif ($choice =~ /^\d+$/) {
244 $bottom = $top = $choice;
246 elsif ($choice eq '*') {
251 $bottom = $top = find_unique($choice, @stuff);
252 if (!defined $bottom) {
253 print "Huh ($choice)?\n";
257 if ($opts->{SINGLETON} && $bottom != $top) {
258 print "Huh ($choice)?\n";
261 for ($i = $bottom-1; $i <= $top-1; $i++) {
262 next if (@stuff <= $i || $i < 0);
263 $chosen[$i] = $choose;
266 last if ($opts->{IMMEDIATE} || $line eq '*');
268 for ($i = 0; $i < @stuff; $i++) {
270 push @return, $stuff[$i];
277 list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
287 print "$cnt paths\n";
295 my @mods = list_modified('file-only');
298 my @update = list_and_choose({ PROMPT => 'Update',
299 HEADER => $status_head, },
302 system(qw(git update-index --add --remove --),
303 map { $_->{VALUE} } @update);
304 say_n_paths('updated', @update);
310 my @update = list_and_choose({ PROMPT => 'Revert',
311 HEADER => $status_head, },
314 my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
315 map { $_->{VALUE} } @update);
317 open $fh, '| git update-index --index-info'
324 if ($_->{INDEX_ADDDEL} &&
325 $_->{INDEX_ADDDEL} eq 'create') {
326 system(qw(git update-index --force-remove --),
328 print "note: $_->{VALUE} is untracked now.\n";
332 say_n_paths('reverted', @update);
337 sub add_untracked_cmd {
338 my @add = list_and_choose({ PROMPT => 'Add untracked' },
341 system(qw(git update-index --add --), @add);
342 say_n_paths('added', @add);
349 my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
350 my (@hunk) = { TEXT => [] };
354 push @hunk, { TEXT => [] };
356 push @{$hunk[-1]{TEXT}}, $_;
361 sub hunk_splittable {
364 my @s = split_hunk($text);
368 sub parse_hunk_header {
370 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
371 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
372 $o_cnt = 1 unless defined $o_cnt;
373 $n_cnt = 1 unless defined $n_cnt;
374 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
381 # If there are context lines in the middle of a hunk,
382 # it can be split, but we would need to take care of
385 my ($o_ofs, undef, $n_ofs) = parse_hunk_header($text->[0]);
390 my $next_hunk_start = undef;
391 my $i = $hunk_start - 1;
402 while (++$i < @$text) {
403 my $line = $text->[$i];
405 if ($this->{ADDDEL} &&
406 !defined $next_hunk_start) {
407 # We have seen leading context and
408 # adds/dels and then here is another
409 # context, which is trailing for this
410 # split hunk and leading for the next
412 $next_hunk_start = $i;
414 push @{$this->{TEXT}}, $line;
417 if (defined $next_hunk_start) {
424 if (defined $next_hunk_start) {
425 # We are done with the current hunk and
426 # this is the first real change for the
428 $hunk_start = $next_hunk_start;
429 $o_ofs = $this->{OLD} + $this->{OCNT};
430 $n_ofs = $this->{NEW} + $this->{NCNT};
431 $o_ofs -= $this->{POSTCTX};
432 $n_ofs -= $this->{POSTCTX};
436 push @{$this->{TEXT}}, $line;
450 for my $hunk (@split) {
451 $o_ofs = $hunk->{OLD};
452 $n_ofs = $hunk->{NEW};
453 my $o_cnt = $hunk->{OCNT};
454 my $n_cnt = $hunk->{NCNT};
456 my $head = ("@@ -$o_ofs" .
457 (($o_cnt != 1) ? ",$o_cnt" : '') .
459 (($n_cnt != 1) ? ",$n_cnt" : '') .
461 unshift @{$hunk->{TEXT}}, $head;
463 return map { $_->{TEXT} } @split;
466 sub find_last_o_ctx {
468 my $text = $it->{TEXT};
469 my ($o_ofs, $o_cnt) = parse_hunk_header($text->[0]);
471 my $last_o_ctx = $o_ofs + $o_cnt;
473 my $line = $text->[$i];
484 my ($prev, $this) = @_;
485 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
486 parse_hunk_header($prev->{TEXT}[0]);
487 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
488 parse_hunk_header($this->{TEXT}[0]);
490 my (@line, $i, $ofs, $o_cnt, $n_cnt);
493 for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
494 my $line = $prev->{TEXT}[$i];
495 if ($line =~ /^\+/) {
501 last if ($o1_ofs <= $ofs);
511 for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
512 my $line = $this->{TEXT}[$i];
513 if ($line =~ /^\+/) {
525 my $head = ("@@ -$o0_ofs" .
526 (($o_cnt != 1) ? ",$o_cnt" : '') .
528 (($n_cnt != 1) ? ",$n_cnt" : '') .
530 @{$prev->{TEXT}} = ($head, @line);
533 sub coalesce_overlapping_hunks {
539 for (grep { $_->{USE} } @in) {
540 my $text = $_->{TEXT};
541 my ($o_ofs) = parse_hunk_header($text->[0]);
542 if (defined $last_o_ctx &&
543 $o_ofs <= $last_o_ctx) {
544 merge_hunk($out[-1], $_);
549 $last_o_ctx = find_last_o_ctx($out[-1]);
557 n - do not stage this hunk
558 a - stage this and all the remaining hunks in the file
559 d - do not stage this hunk nor any of the remaining hunks in the file
560 j - leave this hunk undecided, see next undecided hunk
561 J - leave this hunk undecided, see next hunk
562 k - leave this hunk undecided, see previous undecided hunk
563 K - leave this hunk undecided, see previous hunk
564 s - split the current hunk into smaller hunks
569 sub patch_update_cmd {
570 my @mods = grep { !($_->{BINARY}) } list_modified('file-only');
574 print STDERR "No changes.\n";
581 @them = list_and_choose({ PROMPT => 'Patch update',
582 HEADER => $status_head, },
586 patch_update_file($_->{VALUE});
590 sub patch_update_file {
593 my ($head, @hunk) = parse_diff($path);
594 for (@{$head->{TEXT}}) {
601 my ($prev, $next, $other, $undecided, $i);
607 for ($i = 0; $i < $ix; $i++) {
608 if (!defined $hunk[$i]{USE}) {
617 for ($i = $ix + 1; $i < $num; $i++) {
618 if (!defined $hunk[$i]{USE}) {
624 if ($ix < $num - 1) {
627 for ($i = 0; $i < $num; $i++) {
628 if (!defined $hunk[$i]{USE}) {
633 last if (!$undecided);
635 if (hunk_splittable($hunk[$ix]{TEXT})) {
638 for (@{$hunk[$ix]{TEXT}}) {
641 print "Stage this hunk [y/n/a/d$other/?]? ";
644 if ($line =~ /^y/i) {
647 elsif ($line =~ /^n/i) {
650 elsif ($line =~ /^a/i) {
652 if (!defined $hunk[$ix]{USE}) {
659 elsif ($line =~ /^d/i) {
661 if (!defined $hunk[$ix]{USE}) {
668 elsif ($other =~ /K/ && $line =~ /^K/) {
672 elsif ($other =~ /J/ && $line =~ /^J/) {
676 elsif ($other =~ /k/ && $line =~ /^k/) {
680 !defined $hunk[$ix]{USE});
684 elsif ($other =~ /j/ && $line =~ /^j/) {
687 last if ($ix >= $num ||
688 !defined $hunk[$ix]{USE});
692 elsif ($other =~ /s/ && $line =~ /^s/) {
693 my @split = split_hunk($hunk[$ix]{TEXT});
696 scalar(@split), " hunks.\n";
698 splice(@hunk, $ix, 1,
699 map { +{ TEXT => $_, USE => undef } }
705 help_patch_cmd($other);
711 last if ($ix >= $num ||
712 !defined $hunk[$ix]{USE});
717 @hunk = coalesce_overlapping_hunks(@hunk);
722 my $text = $_->{TEXT};
723 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
724 parse_hunk_header($text->[0]);
727 # We would have added ($n_cnt - $o_cnt) lines
728 # to the postimage if we were to use this hunk,
729 # but we didn't. So the line number that the next
730 # hunk starts at would be shifted by that much.
731 $n_lofs -= ($n_cnt - $o_cnt);
737 $text->[0] = ("@@ -$o_ofs" .
754 open $fh, '| git apply --cached';
755 for (@{$head->{TEXT}}, @result) {
759 for (@{$head->{TEXT}}, @result) {
770 my @mods = list_modified('index-only');
771 @mods = grep { !($_->{BINARY}) } @mods;
773 my (@them) = list_and_choose({ PROMPT => 'Review diff',
775 HEADER => $status_head, },
778 system(qw(git diff-index -p --cached HEAD --),
779 map { $_->{VALUE} } @them);
789 status - show paths with changes
790 update - add working tree state to the staged set of changes
791 revert - revert staged set of changes back to the HEAD version
792 patch - pick hunks and update selectively
793 diff - view diff between HEAD and index
794 add untracked - add contents of untracked files to the staged set of changes
800 my $arg = shift @ARGV;
801 if ($arg eq "--patch") {
803 $arg = shift @ARGV or die "missing --";
804 die "invalid argument $arg, expecting --"
807 elsif ($arg ne "--") {
808 die "invalid argument $arg, expecting --";
813 my @cmd = ([ 'status', \&status_cmd, ],
814 [ 'update', \&update_cmd, ],
815 [ 'revert', \&revert_cmd, ],
816 [ 'add untracked', \&add_untracked_cmd, ],
817 [ 'patch', \&patch_update_cmd, ],
818 [ 'diff', \&diff_cmd, ],
819 [ 'quit', \&quit_cmd, ],
820 [ 'help', \&help_cmd, ],
823 my ($it) = list_and_choose({ PROMPT => 'What now',
826 HEADER => '*** Commands ***',
827 ON_EOF => \&quit_cmd,
828 IMMEDIATE => 1 }, @cmd);