6 if ($^O
eq 'MSWin32' || $^O
eq 'msys') {
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 my $donesomething = 0;
219 for my $choice (split(/[\s,]+/, $line)) {
223 # Input that begins with '-'; unchoose
224 if ($choice =~ s/^-//) {
227 # A range can be specified like 5-7
228 if ($choice =~ /^(\d+)-(\d+)$/) {
229 ($bottom, $top) = ($1, $2);
231 elsif ($choice =~ /^\d+$/) {
232 $bottom = $top = $choice;
234 elsif ($choice eq '*') {
239 $bottom = $top = find_unique
($choice, @stuff);
240 if (!defined $bottom) {
241 print "Huh ($choice)?\n";
245 if ($opts->{SINGLETON
} && $bottom != $top) {
246 print "Huh ($choice)?\n";
249 for ($i = $bottom-1; $i <= $top-1; $i++) {
250 next if (@stuff <= $i);
251 $chosen[$i] = $choose;
255 last if (!$donesomething || $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 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
368 # If there are context lines in the middle of a hunk,
369 # it can be split, but we would need to take care of
372 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header
($text->[0]);
378 my $next_hunk_start = undef;
379 my $i = $hunk_start - 1;
390 while (++$i < @
$text) {
391 my $line = $text->[$i];
393 if ($this->{ADDDEL
} &&
394 !defined $next_hunk_start) {
395 # We have seen leading context and
396 # adds/dels and then here is another
397 # context, which is trailing for this
398 # split hunk and leading for the next
400 $next_hunk_start = $i;
402 push @
{$this->{TEXT
}}, $line;
405 if (defined $next_hunk_start) {
412 if (defined $next_hunk_start) {
413 # We are done with the current hunk and
414 # this is the first real change for the
416 $hunk_start = $next_hunk_start;
417 $o_ofs = $this->{OLD
} + $this->{OCNT
};
418 $n_ofs = $this->{NEW
} + $this->{NCNT
};
419 $o_ofs -= $this->{POSTCTX
};
420 $n_ofs -= $this->{POSTCTX
};
424 push @
{$this->{TEXT
}}, $line;
438 for my $hunk (@split) {
439 $o_ofs = $hunk->{OLD
};
440 $n_ofs = $hunk->{NEW
};
441 $o_cnt = $hunk->{OCNT
};
442 $n_cnt = $hunk->{NCNT
};
444 my $head = ("@@ -$o_ofs" .
445 (($o_cnt != 1) ?
",$o_cnt" : '') .
447 (($n_cnt != 1) ?
",$n_cnt" : '') .
449 unshift @
{$hunk->{TEXT
}}, $head;
451 return map { $_->{TEXT
} } @split;
454 sub find_last_o_ctx
{
456 my $text = $it->{TEXT
};
457 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header
($text->[0]);
459 my $last_o_ctx = $o_ofs + $o_cnt;
461 my $line = $text->[$i];
472 my ($prev, $this) = @_;
473 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
474 parse_hunk_header
($prev->{TEXT
}[0]);
475 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
476 parse_hunk_header
($this->{TEXT
}[0]);
478 my (@line, $i, $ofs, $o_cnt, $n_cnt);
481 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
482 my $line = $prev->{TEXT
}[$i];
483 if ($line =~ /^\+/) {
489 last if ($o1_ofs <= $ofs);
499 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
500 my $line = $this->{TEXT
}[$i];
501 if ($line =~ /^\+/) {
513 my $head = ("@@ -$o0_ofs" .
514 (($o_cnt != 1) ?
",$o_cnt" : '') .
516 (($n_cnt != 1) ?
",$n_cnt" : '') .
518 @
{$prev->{TEXT
}} = ($head, @line);
521 sub coalesce_overlapping_hunks
{
527 for (grep { $_->{USE
} } @in) {
528 my $text = $_->{TEXT
};
529 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
530 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, },
570 my $path = $it->{VALUE
};
571 my ($head, @hunk) = parse_diff
($path);
572 for (@
{$head->{TEXT
}}) {
579 my ($prev, $next, $other, $undecided, $i);
585 for ($i = 0; $i < $ix; $i++) {
586 if (!defined $hunk[$i]{USE
}) {
595 for ($i = $ix + 1; $i < $num; $i++) {
596 if (!defined $hunk[$i]{USE
}) {
602 if ($ix < $num - 1) {
605 for ($i = 0; $i < $num; $i++) {
606 if (!defined $hunk[$i]{USE
}) {
611 last if (!$undecided);
613 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
616 for (@
{$hunk[$ix]{TEXT
}}) {
619 print "Stage this hunk [y/n/a/d$other/?]? ";
622 if ($line =~ /^y/i) {
625 elsif ($line =~ /^n/i) {
628 elsif ($line =~ /^a/i) {
630 if (!defined $hunk[$ix]{USE
}) {
637 elsif ($line =~ /^d/i) {
639 if (!defined $hunk[$ix]{USE
}) {
646 elsif ($other =~ /K/ && $line =~ /^K/) {
650 elsif ($other =~ /J/ && $line =~ /^J/) {
654 elsif ($other =~ /k/ && $line =~ /^k/) {
658 !defined $hunk[$ix]{USE
});
662 elsif ($other =~ /j/ && $line =~ /^j/) {
665 last if ($ix >= $num ||
666 !defined $hunk[$ix]{USE
});
670 elsif ($other =~ /s/ && $line =~ /^s/) {
671 my @split = split_hunk
($hunk[$ix]{TEXT
});
674 scalar(@split), " hunks.\n";
676 splice(@hunk, $ix, 1,
677 map { +{ TEXT
=> $_, USE
=> undef } }
683 help_patch_cmd
($other);
689 last if ($ix >= $num ||
690 !defined $hunk[$ix]{USE
});
695 @hunk = coalesce_overlapping_hunks
(@hunk);
697 my ($o_lofs, $n_lofs) = (0, 0);
700 my $text = $_->{TEXT
};
701 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
702 parse_hunk_header
($text->[0]);
705 if (!defined $o_cnt) { $o_cnt = 1; }
706 if (!defined $n_cnt) { $n_cnt = 1; }
708 # We would have added ($n_cnt - $o_cnt) lines
709 # to the postimage if we were to use this hunk,
710 # but we didn't. So the line number that the next
711 # hunk starts at would be shifted by that much.
712 $n_lofs -= ($n_cnt - $o_cnt);
718 $text->[0] = ("@@ -$o_ofs" .
735 open $fh, '| git apply --cached';
736 for (@
{$head->{TEXT
}}, @result) {
740 for (@
{$head->{TEXT
}}, @result) {
751 my @mods = list_modified
('index-only');
752 @mods = grep { !($_->{BINARY
}) } @mods;
754 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
756 HEADER
=> $status_head, },
759 system(qw(git diff-index -p --cached HEAD --),
760 map { $_->{VALUE
} } @them);
770 status
- show paths with changes
771 update
- add working tree
state to the staged set of changes
772 revert
- revert staged set of changes back to the HEAD version
773 patch
- pick hunks
and update selectively
774 diff
- view diff between HEAD
and index
775 add untracked
- add contents of untracked files to the staged set of changes
780 my @cmd = ([ 'status', \
&status_cmd
, ],
781 [ 'update', \
&update_cmd
, ],
782 [ 'revert', \
&revert_cmd
, ],
783 [ 'add untracked', \
&add_untracked_cmd
, ],
784 [ 'patch', \
&patch_update_cmd
, ],
785 [ 'diff', \
&diff_cmd
, ],
786 [ 'quit', \
&quit_cmd
, ],
787 [ 'help', \
&help_cmd
, ],
790 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
793 HEADER
=> '*** Commands ***',
794 IMMEDIATE
=> 1 }, @cmd);