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 --), @ARGV);
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);
64 } run_cmd_pipe
(qw(git ls-files --exclude-standard --), @ARGV);
65 return if (!@tracked);
68 for (run_cmd_pipe
(qw(git diff-index --cached
69 --numstat --summary HEAD --), @tracked)) {
70 if (($add, $del, $file) =
71 /^([-\d]+) ([-\d]+) (.*)/) {
73 if ($add eq '-' && $del eq '-') {
78 $change = "+$add/-$del";
86 elsif (($adddel, $file) =
87 /^ (create|delete) mode [0-7]+ (.*)$/) {
88 $data{$file}{INDEX_ADDDEL
} = $adddel;
92 for (run_cmd_pipe
(qw(git diff-files --numstat --summary --), @tracked)) {
93 if (($add, $del, $file) =
94 /^([-\d]+) ([-\d]+) (.*)/) {
95 if (!exists $data{$file}) {
102 if ($add eq '-' && $del eq '-') {
107 $change = "+$add/-$del";
109 $data{$file}{FILE
} = $change;
111 $data{$file}{BINARY
} = 1;
114 elsif (($adddel, $file) =
115 /^ (create|delete) mode [0-7]+ (.*)$/) {
116 $data{$file}{FILE_ADDDEL
} = $adddel;
120 for (sort keys %data) {
124 if ($only eq 'index-only') {
125 next if ($it->{INDEX
} eq 'unchanged');
127 if ($only eq 'file-only') {
128 next if ($it->{FILE
} eq 'nothing');
133 PRINT
=> (sprintf $status_fmt,
134 $it->{INDEX
}, $it->{FILE
}, $_),
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 sub list_and_choose
{
171 my ($opts, @stuff) = @_;
172 my (@chosen, @return);
179 if ($opts->{HEADER
}) {
180 if (!$opts->{LIST_FLAT
}) {
183 print "$opts->{HEADER}\n";
185 for ($i = 0; $i < @stuff; $i++) {
186 my $chosen = $chosen[$i] ?
'*' : ' ';
187 my $print = $stuff[$i];
189 if ((ref $print) eq 'ARRAY') {
190 $print = $print->[0];
193 $print = $print->{PRINT
};
196 printf("%s%2d: %s", $chosen, $i+1, $print);
197 if (($opts->{LIST_FLAT
}) &&
198 (($i + 1) % ($opts->{LIST_FLAT
}))) {
211 return if ($opts->{LIST_ONLY
});
213 print $opts->{PROMPT
};
214 if ($opts->{SINGLETON
}) {
223 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
228 for my $choice (split(/[\s,]+/, $line)) {
232 # Input that begins with '-'; unchoose
233 if ($choice =~ s/^-//) {
236 # A range can be specified like 5-7
237 if ($choice =~ /^(\d+)-(\d+)$/) {
238 ($bottom, $top) = ($1, $2);
240 elsif ($choice =~ /^\d+$/) {
241 $bottom = $top = $choice;
243 elsif ($choice eq '*') {
248 $bottom = $top = find_unique
($choice, @stuff);
249 if (!defined $bottom) {
250 print "Huh ($choice)?\n";
254 if ($opts->{SINGLETON
} && $bottom != $top) {
255 print "Huh ($choice)?\n";
258 for ($i = $bottom-1; $i <= $top-1; $i++) {
259 next if (@stuff <= $i || $i < 0);
260 $chosen[$i] = $choose;
263 last if ($opts->{IMMEDIATE
} || $line eq '*');
265 for ($i = 0; $i < @stuff; $i++) {
267 push @return, $stuff[$i];
274 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
284 print "$cnt paths\n";
292 my @mods = list_modified
('file-only');
295 my @update = list_and_choose
({ PROMPT
=> 'Update',
296 HEADER
=> $status_head, },
299 system(qw(git update-index --add --remove --),
300 map { $_->{VALUE
} } @update);
301 say_n_paths
('updated', @update);
307 my @update = list_and_choose
({ PROMPT
=> 'Revert',
308 HEADER
=> $status_head, },
311 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
312 map { $_->{VALUE
} } @update);
314 open $fh, '| git update-index --index-info'
321 if ($_->{INDEX_ADDDEL
} &&
322 $_->{INDEX_ADDDEL
} eq 'create') {
323 system(qw(git update-index --force-remove --),
325 print "note: $_->{VALUE} is untracked now.\n";
329 say_n_paths
('reverted', @update);
334 sub add_untracked_cmd
{
335 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
338 system(qw(git update-index --add --), @add);
339 say_n_paths
('added', @add);
346 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
347 my (@hunk) = { TEXT
=> [] };
351 push @hunk, { TEXT
=> [] };
353 push @
{$hunk[-1]{TEXT
}}, $_;
358 sub hunk_splittable
{
361 my @s = split_hunk
($text);
365 sub parse_hunk_header
{
367 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
368 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
369 $o_cnt = 1 unless defined $o_cnt;
370 $n_cnt = 1 unless defined $n_cnt;
371 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
378 # If there are context lines in the middle of a hunk,
379 # it can be split, but we would need to take care of
382 my ($o_ofs, undef, $n_ofs) = parse_hunk_header
($text->[0]);
387 my $next_hunk_start = undef;
388 my $i = $hunk_start - 1;
399 while (++$i < @
$text) {
400 my $line = $text->[$i];
402 if ($this->{ADDDEL
} &&
403 !defined $next_hunk_start) {
404 # We have seen leading context and
405 # adds/dels and then here is another
406 # context, which is trailing for this
407 # split hunk and leading for the next
409 $next_hunk_start = $i;
411 push @
{$this->{TEXT
}}, $line;
414 if (defined $next_hunk_start) {
421 if (defined $next_hunk_start) {
422 # We are done with the current hunk and
423 # this is the first real change for the
425 $hunk_start = $next_hunk_start;
426 $o_ofs = $this->{OLD
} + $this->{OCNT
};
427 $n_ofs = $this->{NEW
} + $this->{NCNT
};
428 $o_ofs -= $this->{POSTCTX
};
429 $n_ofs -= $this->{POSTCTX
};
433 push @
{$this->{TEXT
}}, $line;
447 for my $hunk (@split) {
448 $o_ofs = $hunk->{OLD
};
449 $n_ofs = $hunk->{NEW
};
450 my $o_cnt = $hunk->{OCNT
};
451 my $n_cnt = $hunk->{NCNT
};
453 my $head = ("@@ -$o_ofs" .
454 (($o_cnt != 1) ?
",$o_cnt" : '') .
456 (($n_cnt != 1) ?
",$n_cnt" : '') .
458 unshift @
{$hunk->{TEXT
}}, $head;
460 return map { $_->{TEXT
} } @split;
463 sub find_last_o_ctx
{
465 my $text = $it->{TEXT
};
466 my ($o_ofs, $o_cnt) = parse_hunk_header
($text->[0]);
468 my $last_o_ctx = $o_ofs + $o_cnt;
470 my $line = $text->[$i];
481 my ($prev, $this) = @_;
482 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
483 parse_hunk_header
($prev->{TEXT
}[0]);
484 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
485 parse_hunk_header
($this->{TEXT
}[0]);
487 my (@line, $i, $ofs, $o_cnt, $n_cnt);
490 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
491 my $line = $prev->{TEXT
}[$i];
492 if ($line =~ /^\+/) {
498 last if ($o1_ofs <= $ofs);
508 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
509 my $line = $this->{TEXT
}[$i];
510 if ($line =~ /^\+/) {
522 my $head = ("@@ -$o0_ofs" .
523 (($o_cnt != 1) ?
",$o_cnt" : '') .
525 (($n_cnt != 1) ?
",$n_cnt" : '') .
527 @
{$prev->{TEXT
}} = ($head, @line);
530 sub coalesce_overlapping_hunks
{
536 for (grep { $_->{USE
} } @in) {
537 my $text = $_->{TEXT
};
538 my ($o_ofs) = parse_hunk_header
($text->[0]);
539 if (defined $last_o_ctx &&
540 $o_ofs <= $last_o_ctx) {
541 merge_hunk
($out[-1], $_);
546 $last_o_ctx = find_last_o_ctx
($out[-1]);
554 n
- do not stage this hunk
555 a
- stage this
and all the remaining hunks
556 d
- do not stage this hunk nor any of the remaining hunks
557 j
- leave this hunk undecided
, see
next undecided hunk
558 J
- leave this hunk undecided
, see
next hunk
559 k
- leave this hunk undecided
, see previous undecided hunk
560 K
- leave this hunk undecided
, see previous hunk
561 s
- split the current hunk into smaller hunks
565 sub patch_update_cmd
{
566 my @mods = list_modified
('file-only');
567 @mods = grep { !($_->{BINARY
}) } @mods;
570 my (@them) = list_and_choose
({ PROMPT
=> 'Patch update',
571 HEADER
=> $status_head, },
574 patch_update_file
($_->{VALUE
});
578 sub patch_update_file
{
581 my ($head, @hunk) = parse_diff
($path);
582 for (@
{$head->{TEXT
}}) {
589 my ($prev, $next, $other, $undecided, $i);
595 for ($i = 0; $i < $ix; $i++) {
596 if (!defined $hunk[$i]{USE
}) {
605 for ($i = $ix + 1; $i < $num; $i++) {
606 if (!defined $hunk[$i]{USE
}) {
612 if ($ix < $num - 1) {
615 for ($i = 0; $i < $num; $i++) {
616 if (!defined $hunk[$i]{USE
}) {
621 last if (!$undecided);
623 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
626 for (@
{$hunk[$ix]{TEXT
}}) {
629 print "Stage this hunk [y/n/a/d$other/?]? ";
632 if ($line =~ /^y/i) {
635 elsif ($line =~ /^n/i) {
638 elsif ($line =~ /^a/i) {
640 if (!defined $hunk[$ix]{USE
}) {
647 elsif ($line =~ /^d/i) {
649 if (!defined $hunk[$ix]{USE
}) {
656 elsif ($other =~ /K/ && $line =~ /^K/) {
660 elsif ($other =~ /J/ && $line =~ /^J/) {
664 elsif ($other =~ /k/ && $line =~ /^k/) {
668 !defined $hunk[$ix]{USE
});
672 elsif ($other =~ /j/ && $line =~ /^j/) {
675 last if ($ix >= $num ||
676 !defined $hunk[$ix]{USE
});
680 elsif ($other =~ /s/ && $line =~ /^s/) {
681 my @split = split_hunk
($hunk[$ix]{TEXT
});
684 scalar(@split), " hunks.\n";
686 splice(@hunk, $ix, 1,
687 map { +{ TEXT
=> $_, USE
=> undef } }
693 help_patch_cmd
($other);
699 last if ($ix >= $num ||
700 !defined $hunk[$ix]{USE
});
705 @hunk = coalesce_overlapping_hunks
(@hunk);
710 my $text = $_->{TEXT
};
711 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
712 parse_hunk_header
($text->[0]);
715 # We would have added ($n_cnt - $o_cnt) lines
716 # to the postimage if we were to use this hunk,
717 # but we didn't. So the line number that the next
718 # hunk starts at would be shifted by that much.
719 $n_lofs -= ($n_cnt - $o_cnt);
725 $text->[0] = ("@@ -$o_ofs" .
742 open $fh, '| git apply --cached';
743 for (@
{$head->{TEXT
}}, @result) {
747 for (@
{$head->{TEXT
}}, @result) {
758 my @mods = list_modified
('index-only');
759 @mods = grep { !($_->{BINARY
}) } @mods;
761 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
763 HEADER
=> $status_head, },
766 system(qw(git diff-index -p --cached HEAD --),
767 map { $_->{VALUE
} } @them);
777 status
- show paths with changes
778 update
- add working tree
state to the staged set of changes
779 revert
- revert staged set of changes back to the HEAD version
780 patch
- pick hunks
and update selectively
781 diff
- view diff between HEAD
and index
782 add untracked
- add contents of untracked files to the staged set of changes
787 my @cmd = ([ 'status', \
&status_cmd
, ],
788 [ 'update', \
&update_cmd
, ],
789 [ 'revert', \
&revert_cmd
, ],
790 [ 'add untracked', \
&add_untracked_cmd
, ],
791 [ 'patch', \
&patch_update_cmd
, ],
792 [ 'diff', \
&diff_cmd
, ],
793 [ 'quit', \
&quit_cmd
, ],
794 [ 'help', \
&help_cmd
, ],
797 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
800 HEADER
=> '*** Commands ***',
801 ON_EOF
=> \
&quit_cmd
,
802 IMMEDIATE
=> 1 }, @cmd);