6 if ($^O eq 'MSWin32') {
7 my @invalid = grep {m/[":*]/} @_;
8 die "$^O does not support: @invalid\n" if @invalid;
9 my @args = map { m/ /o ? "\"$_\"": $_ } @_;
13 open($fh, '-|', @_) or die;
18 my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir));
20 if (!defined $GIT_DIR) {
21 exit(1); # rev-parse would have already said "not a git repo"
27 open $fh, 'git update-index --refresh |'
30 ;# ignore 'needs update'
40 run_cmd_pipe(qw(git ls-files --others --exclude-standard --), @_);
43 my $status_fmt = '%12s %12s %s';
44 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
46 # Returns list of hashes, contents of each of which are:
47 # PRINT: print message
49 # BINARY: is a binary path
50 # INDEX: is index different from HEAD?
51 # FILE: is file different from index?
52 # INDEX_ADDDEL: is it add/delete between HEAD and index?
53 # FILE_ADDDEL: is it add/delete between index and file?
58 my ($add, $del, $adddel, $file);
60 for (run_cmd_pipe(qw(git diff-index --cached
61 --numstat --summary HEAD))) {
62 if (($add, $del, $file) =
63 /^([-\d]+) ([-\d]+) (.*)/) {
65 if ($add eq '-' && $del eq '-') {
70 $change = "+$add/-$del";
78 elsif (($adddel, $file) =
79 /^ (create|delete) mode [0-7]+ (.*)$/) {
80 $data{$file}{INDEX_ADDDEL} = $adddel;
84 for (run_cmd_pipe(qw(git diff-files --numstat --summary))) {
85 if (($add, $del, $file) =
86 /^([-\d]+) ([-\d]+) (.*)/) {
87 if (!exists $data{$file}) {
94 if ($add eq '-' && $del eq '-') {
99 $change = "+$add/-$del";
101 $data{$file}{FILE} = $change;
103 $data{$file}{BINARY} = 1;
106 elsif (($adddel, $file) =
107 /^ (create|delete) mode [0-7]+ (.*)$/) {
108 $data{$file}{FILE_ADDDEL} = $adddel;
112 for (sort keys %data) {
116 if ($only eq 'index-only') {
117 next if ($it->{INDEX} eq 'unchanged');
119 if ($only eq 'file-only') {
120 next if ($it->{FILE} eq 'nothing');
125 PRINT => (sprintf $status_fmt,
126 $it->{INDEX}, $it->{FILE}, $_),
134 my ($string, @stuff) = @_;
136 for (my $i = 0; $i < @stuff; $i++) {
140 if ((ref $it) eq 'ARRAY') {
148 if ($it =~ /^$string/) {
152 if (defined $hit && defined $found) {
162 sub list_and_choose {
163 my ($opts, @stuff) = @_;
164 my (@chosen, @return);
171 if ($opts->{HEADER}) {
172 if (!$opts->{LIST_FLAT}) {
175 print "$opts->{HEADER}\n";
177 for ($i = 0; $i < @stuff; $i++) {
178 my $chosen = $chosen[$i] ? '*' : ' ';
179 my $print = $stuff[$i];
181 if ((ref $print) eq 'ARRAY') {
182 $print = $print->[0];
185 $print = $print->{PRINT};
188 printf("%s%2d: %s", $chosen, $i+1, $print);
189 if (($opts->{LIST_FLAT}) &&
190 (($i + 1) % ($opts->{LIST_FLAT}))) {
203 return if ($opts->{LIST_ONLY});
205 print $opts->{PROMPT};
206 if ($opts->{SINGLETON}) {
215 $opts->{ON_EOF}->() if $opts->{ON_EOF};
220 for my $choice (split(/[\s,]+/, $line)) {
224 # Input that begins with '-'; unchoose
225 if ($choice =~ s/^-//) {
228 # A range can be specified like 5-7
229 if ($choice =~ /^(\d+)-(\d+)$/) {
230 ($bottom, $top) = ($1, $2);
232 elsif ($choice =~ /^\d+$/) {
233 $bottom = $top = $choice;
235 elsif ($choice eq '*') {
240 $bottom = $top = find_unique($choice, @stuff);
241 if (!defined $bottom) {
242 print "Huh ($choice)?\n";
246 if ($opts->{SINGLETON} && $bottom != $top) {
247 print "Huh ($choice)?\n";
250 for ($i = $bottom-1; $i <= $top-1; $i++) {
251 next if (@stuff <= $i || $i < 0);
252 $chosen[$i] = $choose;
255 last if ($opts->{IMMEDIATE});
257 for ($i = 0; $i < @stuff; $i++) {
259 push @return, $stuff[$i];
266 list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
276 print "$cnt paths\n";
284 my @mods = list_modified('file-only');
287 my @update = list_and_choose({ PROMPT => 'Update',
288 HEADER => $status_head, },
291 system(qw(git update-index --add --remove --),
292 map { $_->{VALUE} } @update);
293 say_n_paths('updated', @update);
299 my @update = list_and_choose({ PROMPT => 'Revert',
300 HEADER => $status_head, },
303 my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
304 map { $_->{VALUE} } @update);
306 open $fh, '| git update-index --index-info'
313 if ($_->{INDEX_ADDDEL} &&
314 $_->{INDEX_ADDDEL} eq 'create') {
315 system(qw(git update-index --force-remove --),
317 print "note: $_->{VALUE} is untracked now.\n";
321 say_n_paths('reverted', @update);
326 sub add_untracked_cmd {
327 my @add = list_and_choose({ PROMPT => 'Add untracked' },
330 system(qw(git update-index --add --), @add);
331 say_n_paths('added', @add);
338 my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
339 my (@hunk) = { TEXT => [] };
343 push @hunk, { TEXT => [] };
345 push @{$hunk[-1]{TEXT}}, $_;
350 sub hunk_splittable {
353 my @s = split_hunk($text);
357 sub parse_hunk_header {
359 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
360 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
361 $o_cnt = 1 unless defined $o_cnt;
362 $n_cnt = 1 unless defined $n_cnt;
363 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
370 # If there are context lines in the middle of a hunk,
371 # it can be split, but we would need to take care of
374 my ($o_ofs, undef, $n_ofs) = parse_hunk_header($text->[0]);
379 my $next_hunk_start = undef;
380 my $i = $hunk_start - 1;
391 while (++$i < @$text) {
392 my $line = $text->[$i];
394 if ($this->{ADDDEL} &&
395 !defined $next_hunk_start) {
396 # We have seen leading context and
397 # adds/dels and then here is another
398 # context, which is trailing for this
399 # split hunk and leading for the next
401 $next_hunk_start = $i;
403 push @{$this->{TEXT}}, $line;
406 if (defined $next_hunk_start) {
413 if (defined $next_hunk_start) {
414 # We are done with the current hunk and
415 # this is the first real change for the
417 $hunk_start = $next_hunk_start;
418 $o_ofs = $this->{OLD} + $this->{OCNT};
419 $n_ofs = $this->{NEW} + $this->{NCNT};
420 $o_ofs -= $this->{POSTCTX};
421 $n_ofs -= $this->{POSTCTX};
425 push @{$this->{TEXT}}, $line;
439 for my $hunk (@split) {
440 $o_ofs = $hunk->{OLD};
441 $n_ofs = $hunk->{NEW};
442 my $o_cnt = $hunk->{OCNT};
443 my $n_cnt = $hunk->{NCNT};
445 my $head = ("@@ -$o_ofs" .
446 (($o_cnt != 1) ? ",$o_cnt" : '') .
448 (($n_cnt != 1) ? ",$n_cnt" : '') .
450 unshift @{$hunk->{TEXT}}, $head;
452 return map { $_->{TEXT} } @split;
455 sub find_last_o_ctx {
457 my $text = $it->{TEXT};
458 my ($o_ofs, $o_cnt) = parse_hunk_header($text->[0]);
460 my $last_o_ctx = $o_ofs + $o_cnt;
462 my $line = $text->[$i];
473 my ($prev, $this) = @_;
474 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
475 parse_hunk_header($prev->{TEXT}[0]);
476 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
477 parse_hunk_header($this->{TEXT}[0]);
479 my (@line, $i, $ofs, $o_cnt, $n_cnt);
482 for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
483 my $line = $prev->{TEXT}[$i];
484 if ($line =~ /^\+/) {
490 last if ($o1_ofs <= $ofs);
500 for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
501 my $line = $this->{TEXT}[$i];
502 if ($line =~ /^\+/) {
514 my $head = ("@@ -$o0_ofs" .
515 (($o_cnt != 1) ? ",$o_cnt" : '') .
517 (($n_cnt != 1) ? ",$n_cnt" : '') .
519 @{$prev->{TEXT}} = ($head, @line);
522 sub coalesce_overlapping_hunks {
528 for (grep { $_->{USE} } @in) {
529 my $text = $_->{TEXT};
530 my ($o_ofs) = parse_hunk_header($text->[0]);
531 if (defined $last_o_ctx &&
532 $o_ofs <= $last_o_ctx) {
533 merge_hunk($out[-1], $_);
538 $last_o_ctx = find_last_o_ctx($out[-1]);
546 n - do not stage this hunk
547 a - stage this and all the remaining hunks
548 d - do not stage this hunk nor any of the remaining hunks
549 j - leave this hunk undecided, see next undecided hunk
550 J - leave this hunk undecided, see next hunk
551 k - leave this hunk undecided, see previous undecided hunk
552 K - leave this hunk undecided, see previous hunk
553 s - split the current hunk into smaller hunks
557 sub patch_update_cmd {
558 my @mods = list_modified('file-only');
559 @mods = grep { !($_->{BINARY}) } @mods;
562 my ($it) = list_and_choose({ PROMPT => 'Patch update',
565 HEADER => $status_head, },
567 patch_update_file($it->{VALUE}) if ($it);
570 sub patch_update_file {
573 my ($head, @hunk) = parse_diff($path);
574 for (@{$head->{TEXT}}) {
581 my ($prev, $next, $other, $undecided, $i);
587 for ($i = 0; $i < $ix; $i++) {
588 if (!defined $hunk[$i]{USE}) {
597 for ($i = $ix + 1; $i < $num; $i++) {
598 if (!defined $hunk[$i]{USE}) {
604 if ($ix < $num - 1) {
607 for ($i = 0; $i < $num; $i++) {
608 if (!defined $hunk[$i]{USE}) {
613 last if (!$undecided);
615 if (hunk_splittable($hunk[$ix]{TEXT})) {
618 for (@{$hunk[$ix]{TEXT}}) {
621 print "Stage this hunk [y/n/a/d$other/?]? ";
624 if ($line =~ /^y/i) {
627 elsif ($line =~ /^n/i) {
630 elsif ($line =~ /^a/i) {
632 if (!defined $hunk[$ix]{USE}) {
639 elsif ($line =~ /^d/i) {
641 if (!defined $hunk[$ix]{USE}) {
648 elsif ($other =~ /K/ && $line =~ /^K/) {
652 elsif ($other =~ /J/ && $line =~ /^J/) {
656 elsif ($other =~ /k/ && $line =~ /^k/) {
660 !defined $hunk[$ix]{USE});
664 elsif ($other =~ /j/ && $line =~ /^j/) {
667 last if ($ix >= $num ||
668 !defined $hunk[$ix]{USE});
672 elsif ($other =~ /s/ && $line =~ /^s/) {
673 my @split = split_hunk($hunk[$ix]{TEXT});
676 scalar(@split), " hunks.\n";
678 splice(@hunk, $ix, 1,
679 map { +{ TEXT => $_, USE => undef } }
685 help_patch_cmd($other);
691 last if ($ix >= $num ||
692 !defined $hunk[$ix]{USE});
697 @hunk = coalesce_overlapping_hunks(@hunk);
702 my $text = $_->{TEXT};
703 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
704 parse_hunk_header($text->[0]);
707 # We would have added ($n_cnt - $o_cnt) lines
708 # to the postimage if we were to use this hunk,
709 # but we didn't. So the line number that the next
710 # hunk starts at would be shifted by that much.
711 $n_lofs -= ($n_cnt - $o_cnt);
717 $text->[0] = ("@@ -$o_ofs" .
734 open $fh, '| git apply --cached';
735 for (@{$head->{TEXT}}, @result) {
739 for (@{$head->{TEXT}}, @result) {
750 my @mods = list_modified('index-only');
751 @mods = grep { !($_->{BINARY}) } @mods;
753 my (@them) = list_and_choose({ PROMPT => 'Review diff',
755 HEADER => $status_head, },
758 system(qw(git diff-index -p --cached HEAD --),
759 map { $_->{VALUE} } @them);
769 status - show paths with changes
770 update - add working tree state to the staged set of changes
771 revert - revert staged set of changes back to the HEAD version
772 patch - pick hunks and update selectively
773 diff - view diff between HEAD and index
774 add untracked - add contents of untracked files to the staged set of changes
779 my @cmd = ([ 'status', \&status_cmd, ],
780 [ 'update', \&update_cmd, ],
781 [ 'revert', \&revert_cmd, ],
782 [ 'add untracked', \&add_untracked_cmd, ],
783 [ 'patch', \&patch_update_cmd, ],
784 [ 'diff', \&diff_cmd, ],
785 [ 'quit', \&quit_cmd, ],
786 [ 'help', \&help_cmd, ],
789 my ($it) = list_and_choose({ PROMPT => 'What now',
792 HEADER => '*** Commands ***',
793 ON_EOF => \&quit_cmd,
794 IMMEDIATE => 1 }, @cmd);