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
41 --exclude-per-directory=.gitignore),
42 "--exclude-from=$GIT_DIR/info/exclude",
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);
63 for (run_cmd_pipe
(qw(git diff-index --cached
64 --numstat --summary HEAD))) {
65 if (($add, $del, $file) =
66 /^([-\d]+) ([-\d]+) (.*)/) {
68 if ($add eq '-' && $del eq '-') {
73 $change = "+$add/-$del";
81 elsif (($adddel, $file) =
82 /^ (create|delete) mode [0-7]+ (.*)$/) {
83 $data{$file}{INDEX_ADDDEL
} = $adddel;
87 for (run_cmd_pipe
(qw(git diff-files --numstat --summary))) {
88 if (($add, $del, $file) =
89 /^([-\d]+) ([-\d]+) (.*)/) {
90 if (!exists $data{$file}) {
97 if ($add eq '-' && $del eq '-') {
102 $change = "+$add/-$del";
104 $data{$file}{FILE
} = $change;
106 $data{$file}{BINARY
} = 1;
109 elsif (($adddel, $file) =
110 /^ (create|delete) mode [0-7]+ (.*)$/) {
111 $data{$file}{FILE_ADDDEL
} = $adddel;
115 for (sort keys %data) {
119 if ($only eq 'index-only') {
120 next if ($it->{INDEX
} eq 'unchanged');
122 if ($only eq 'file-only') {
123 next if ($it->{FILE
} eq 'nothing');
128 PRINT
=> (sprintf $status_fmt,
129 $it->{INDEX
}, $it->{FILE
}, $_),
137 my ($string, @stuff) = @_;
139 for (my $i = 0; $i < @stuff; $i++) {
143 if ((ref $it) eq 'ARRAY') {
151 if ($it =~ /^$string/) {
155 if (defined $hit && defined $found) {
165 sub list_and_choose
{
166 my ($opts, @stuff) = @_;
167 my (@chosen, @return);
174 if ($opts->{HEADER
}) {
175 if (!$opts->{LIST_FLAT
}) {
178 print "$opts->{HEADER}\n";
180 for ($i = 0; $i < @stuff; $i++) {
181 my $chosen = $chosen[$i] ?
'*' : ' ';
182 my $print = $stuff[$i];
184 if ((ref $print) eq 'ARRAY') {
185 $print = $print->[0];
188 $print = $print->{PRINT
};
191 printf("%s%2d: %s", $chosen, $i+1, $print);
192 if (($opts->{LIST_FLAT
}) &&
193 (($i + 1) % ($opts->{LIST_FLAT
}))) {
206 return if ($opts->{LIST_ONLY
});
208 print $opts->{PROMPT
};
209 if ($opts->{SINGLETON
}) {
218 $opts->{ON_EOF
}->() if $opts->{ON_EOF
};
223 for my $choice (split(/[\s,]+/, $line)) {
227 # Input that begins with '-'; unchoose
228 if ($choice =~ s/^-//) {
231 # A range can be specified like 5-7
232 if ($choice =~ /^(\d+)-(\d+)$/) {
233 ($bottom, $top) = ($1, $2);
235 elsif ($choice =~ /^\d+$/) {
236 $bottom = $top = $choice;
238 elsif ($choice eq '*') {
243 $bottom = $top = find_unique
($choice, @stuff);
244 if (!defined $bottom) {
245 print "Huh ($choice)?\n";
249 if ($opts->{SINGLETON
} && $bottom != $top) {
250 print "Huh ($choice)?\n";
253 for ($i = $bottom-1; $i <= $top-1; $i++) {
254 next if (@stuff <= $i || $i < 0);
255 $chosen[$i] = $choose;
258 last if ($opts->{IMMEDIATE
});
260 for ($i = 0; $i < @stuff; $i++) {
262 push @return, $stuff[$i];
269 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
279 print "$cnt paths\n";
287 my @mods = list_modified
('file-only');
290 my @update = list_and_choose
({ PROMPT
=> 'Update',
291 HEADER
=> $status_head, },
294 system(qw(git update-index --add --remove --),
295 map { $_->{VALUE
} } @update);
296 say_n_paths
('updated', @update);
302 my @update = list_and_choose
({ PROMPT
=> 'Revert',
303 HEADER
=> $status_head, },
306 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
307 map { $_->{VALUE
} } @update);
309 open $fh, '| git update-index --index-info'
316 if ($_->{INDEX_ADDDEL
} &&
317 $_->{INDEX_ADDDEL
} eq 'create') {
318 system(qw(git update-index --force-remove --),
320 print "note: $_->{VALUE} is untracked now.\n";
324 say_n_paths
('reverted', @update);
329 sub add_untracked_cmd
{
330 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
333 system(qw(git update-index --add --), @add);
334 say_n_paths
('added', @add);
341 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
342 my (@hunk) = { TEXT
=> [] };
346 push @hunk, { TEXT
=> [] };
348 push @
{$hunk[-1]{TEXT
}}, $_;
353 sub hunk_splittable
{
356 my @s = split_hunk
($text);
360 sub parse_hunk_header
{
362 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
363 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
364 $o_cnt = 1 unless defined $o_cnt;
365 $n_cnt = 1 unless defined $n_cnt;
366 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
373 # If there are context lines in the middle of a hunk,
374 # it can be split, but we would need to take care of
377 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header
($text->[0]);
383 my $next_hunk_start = undef;
384 my $i = $hunk_start - 1;
395 while (++$i < @
$text) {
396 my $line = $text->[$i];
398 if ($this->{ADDDEL
} &&
399 !defined $next_hunk_start) {
400 # We have seen leading context and
401 # adds/dels and then here is another
402 # context, which is trailing for this
403 # split hunk and leading for the next
405 $next_hunk_start = $i;
407 push @
{$this->{TEXT
}}, $line;
410 if (defined $next_hunk_start) {
417 if (defined $next_hunk_start) {
418 # We are done with the current hunk and
419 # this is the first real change for the
421 $hunk_start = $next_hunk_start;
422 $o_ofs = $this->{OLD
} + $this->{OCNT
};
423 $n_ofs = $this->{NEW
} + $this->{NCNT
};
424 $o_ofs -= $this->{POSTCTX
};
425 $n_ofs -= $this->{POSTCTX
};
429 push @
{$this->{TEXT
}}, $line;
443 for my $hunk (@split) {
444 $o_ofs = $hunk->{OLD
};
445 $n_ofs = $hunk->{NEW
};
446 $o_cnt = $hunk->{OCNT
};
447 $n_cnt = $hunk->{NCNT
};
449 my $head = ("@@ -$o_ofs" .
450 (($o_cnt != 1) ?
",$o_cnt" : '') .
452 (($n_cnt != 1) ?
",$n_cnt" : '') .
454 unshift @
{$hunk->{TEXT
}}, $head;
456 return map { $_->{TEXT
} } @split;
459 sub find_last_o_ctx
{
461 my $text = $it->{TEXT
};
462 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header
($text->[0]);
464 my $last_o_ctx = $o_ofs + $o_cnt;
466 my $line = $text->[$i];
477 my ($prev, $this) = @_;
478 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
479 parse_hunk_header
($prev->{TEXT
}[0]);
480 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
481 parse_hunk_header
($this->{TEXT
}[0]);
483 my (@line, $i, $ofs, $o_cnt, $n_cnt);
486 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
487 my $line = $prev->{TEXT
}[$i];
488 if ($line =~ /^\+/) {
494 last if ($o1_ofs <= $ofs);
504 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
505 my $line = $this->{TEXT
}[$i];
506 if ($line =~ /^\+/) {
518 my $head = ("@@ -$o0_ofs" .
519 (($o_cnt != 1) ?
",$o_cnt" : '') .
521 (($n_cnt != 1) ?
",$n_cnt" : '') .
523 @
{$prev->{TEXT
}} = ($head, @line);
526 sub coalesce_overlapping_hunks
{
532 for (grep { $_->{USE
} } @in) {
533 my $text = $_->{TEXT
};
534 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
535 parse_hunk_header
($text->[0]);
536 if (defined $last_o_ctx &&
537 $o_ofs <= $last_o_ctx) {
538 merge_hunk
($out[-1], $_);
543 $last_o_ctx = find_last_o_ctx
($out[-1]);
551 n
- do not stage this hunk
552 a
- stage this
and all the remaining hunks
553 d
- do not stage this hunk nor any of the remaining hunks
554 j
- leave this hunk undecided
, see
next undecided hunk
555 J
- leave this hunk undecided
, see
next hunk
556 k
- leave this hunk undecided
, see previous undecided hunk
557 K
- leave this hunk undecided
, see previous hunk
558 s
- split the current hunk into smaller hunks
562 sub patch_update_cmd
{
563 my @mods = list_modified
('file-only');
564 @mods = grep { !($_->{BINARY
}) } @mods;
567 my ($it) = list_and_choose
({ PROMPT
=> 'Patch update',
570 HEADER
=> $status_head, },
575 my $path = $it->{VALUE
};
576 my ($head, @hunk) = parse_diff
($path);
577 for (@
{$head->{TEXT
}}) {
584 my ($prev, $next, $other, $undecided, $i);
590 for ($i = 0; $i < $ix; $i++) {
591 if (!defined $hunk[$i]{USE
}) {
600 for ($i = $ix + 1; $i < $num; $i++) {
601 if (!defined $hunk[$i]{USE
}) {
607 if ($ix < $num - 1) {
610 for ($i = 0; $i < $num; $i++) {
611 if (!defined $hunk[$i]{USE
}) {
616 last if (!$undecided);
618 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
621 for (@
{$hunk[$ix]{TEXT
}}) {
624 print "Stage this hunk [y/n/a/d$other/?]? ";
627 if ($line =~ /^y/i) {
630 elsif ($line =~ /^n/i) {
633 elsif ($line =~ /^a/i) {
635 if (!defined $hunk[$ix]{USE
}) {
642 elsif ($line =~ /^d/i) {
644 if (!defined $hunk[$ix]{USE
}) {
651 elsif ($other =~ /K/ && $line =~ /^K/) {
655 elsif ($other =~ /J/ && $line =~ /^J/) {
659 elsif ($other =~ /k/ && $line =~ /^k/) {
663 !defined $hunk[$ix]{USE
});
667 elsif ($other =~ /j/ && $line =~ /^j/) {
670 last if ($ix >= $num ||
671 !defined $hunk[$ix]{USE
});
675 elsif ($other =~ /s/ && $line =~ /^s/) {
676 my @split = split_hunk
($hunk[$ix]{TEXT
});
679 scalar(@split), " hunks.\n";
681 splice(@hunk, $ix, 1,
682 map { +{ TEXT
=> $_, USE
=> undef } }
688 help_patch_cmd
($other);
694 last if ($ix >= $num ||
695 !defined $hunk[$ix]{USE
});
700 @hunk = coalesce_overlapping_hunks
(@hunk);
702 my ($o_lofs, $n_lofs) = (0, 0);
705 my $text = $_->{TEXT
};
706 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
707 parse_hunk_header
($text->[0]);
710 # We would have added ($n_cnt - $o_cnt) lines
711 # to the postimage if we were to use this hunk,
712 # but we didn't. So the line number that the next
713 # hunk starts at would be shifted by that much.
714 $n_lofs -= ($n_cnt - $o_cnt);
720 $text->[0] = ("@@ -$o_ofs" .
737 open $fh, '| git apply --cached';
738 for (@
{$head->{TEXT
}}, @result) {
742 for (@
{$head->{TEXT
}}, @result) {
753 my @mods = list_modified
('index-only');
754 @mods = grep { !($_->{BINARY
}) } @mods;
756 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
758 HEADER
=> $status_head, },
761 system(qw(git diff-index -p --cached HEAD --),
762 map { $_->{VALUE
} } @them);
772 status
- show paths with changes
773 update
- add working tree
state to the staged set of changes
774 revert
- revert staged set of changes back to the HEAD version
775 patch
- pick hunks
and update selectively
776 diff
- view diff between HEAD
and index
777 add untracked
- add contents of untracked files to the staged set of changes
782 my @cmd = ([ 'status', \
&status_cmd
, ],
783 [ 'update', \
&update_cmd
, ],
784 [ 'revert', \
&revert_cmd
, ],
785 [ 'add untracked', \
&add_untracked_cmd
, ],
786 [ 'patch', \
&patch_update_cmd
, ],
787 [ 'diff', \
&diff_cmd
, ],
788 [ 'quit', \
&quit_cmd
, ],
789 [ 'help', \
&help_cmd
, ],
792 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
795 HEADER
=> '*** Commands ***',
796 ON_EOF
=> \
&quit_cmd
,
797 IMMEDIATE
=> 1 }, @cmd);