7 open($fh, '-|', @_) or die;
11 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
13 if (!defined $GIT_DIR) {
14 exit(1); # rev-parse would have already said "not a git repo"
20 open $fh, '-|', qw(git update-index --refresh)
23 ;# ignore 'needs update'
33 run_cmd_pipe
(qw(git ls-files --others
34 --exclude-per-directory=.gitignore),
35 "--exclude-from=$GIT_DIR/info/exclude",
39 my $status_fmt = '%12s %12s %s';
40 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
42 # Returns list of hashes, contents of each of which are:
43 # PRINT: print message
45 # BINARY: is a binary path
46 # INDEX: is index different from HEAD?
47 # FILE: is file different from index?
48 # INDEX_ADDDEL: is it add/delete between HEAD and index?
49 # FILE_ADDDEL: is it add/delete between index and file?
54 my ($add, $del, $adddel, $file);
56 for (run_cmd_pipe
(qw(git diff-index --cached
57 --numstat --summary HEAD))) {
58 if (($add, $del, $file) =
59 /^([-\d]+) ([-\d]+) (.*)/) {
61 if ($add eq '-' && $del eq '-') {
66 $change = "+$add/-$del";
74 elsif (($adddel, $file) =
75 /^ (create|delete) mode [0-7]+ (.*)$/) {
76 $data{$file}{INDEX_ADDDEL
} = $adddel;
80 for (run_cmd_pipe
(qw(git diff-files --numstat --summary))) {
81 if (($add, $del, $file) =
82 /^([-\d]+) ([-\d]+) (.*)/) {
83 if (!exists $data{$file}) {
90 if ($add eq '-' && $del eq '-') {
95 $change = "+$add/-$del";
97 $data{$file}{FILE
} = $change;
99 $data{$file}{BINARY
} = 1;
102 elsif (($adddel, $file) =
103 /^ (create|delete) mode [0-7]+ (.*)$/) {
104 $data{$file}{FILE_ADDDEL
} = $adddel;
108 for (sort keys %data) {
112 if ($only eq 'index-only') {
113 next if ($it->{INDEX
} eq 'unchanged');
115 if ($only eq 'file-only') {
116 next if ($it->{FILE
} eq 'nothing');
121 PRINT
=> (sprintf $status_fmt,
122 $it->{INDEX
}, $it->{FILE
}, $_),
130 my ($string, @stuff) = @_;
132 for (my $i = 0; $i < @stuff; $i++) {
136 if ((ref $it) eq 'ARRAY') {
144 if ($it =~ /^$string/) {
148 if (defined $hit && defined $found) {
158 sub list_and_choose
{
159 my ($opts, @stuff) = @_;
160 my (@chosen, @return);
167 if ($opts->{HEADER
}) {
168 if (!$opts->{LIST_FLAT
}) {
171 print "$opts->{HEADER}\n";
173 for ($i = 0; $i < @stuff; $i++) {
174 my $chosen = $chosen[$i] ?
'*' : ' ';
175 my $print = $stuff[$i];
177 if ((ref $print) eq 'ARRAY') {
178 $print = $print->[0];
181 $print = $print->{PRINT
};
184 printf("%s%2d: %s", $chosen, $i+1, $print);
185 if (($opts->{LIST_FLAT
}) &&
186 (($i + 1) % ($opts->{LIST_FLAT
}))) {
199 return if ($opts->{LIST_ONLY
});
201 print $opts->{PROMPT
};
202 if ($opts->{SINGLETON
}) {
211 my $donesomething = 0;
212 for my $choice (split(/[\s,]+/, $line)) {
216 # Input that begins with '-'; unchoose
217 if ($choice =~ s/^-//) {
220 # A range can be specified like 5-7
221 if ($choice =~ /^(\d+)-(\d+)$/) {
222 ($bottom, $top) = ($1, $2);
224 elsif ($choice =~ /^\d+$/) {
225 $bottom = $top = $choice;
227 elsif ($choice eq '*') {
232 $bottom = $top = find_unique
($choice, @stuff);
233 if (!defined $bottom) {
234 print "Huh ($choice)?\n";
238 if ($opts->{SINGLETON
} && $bottom != $top) {
239 print "Huh ($choice)?\n";
242 for ($i = $bottom-1; $i <= $top-1; $i++) {
243 next if (@stuff <= $i);
244 $chosen[$i] = $choose;
248 last if (!$donesomething || $opts->{IMMEDIATE
});
250 for ($i = 0; $i < @stuff; $i++) {
252 push @return, $stuff[$i];
259 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
269 print "$cnt paths\n";
277 my @mods = list_modified
('file-only');
280 my @update = list_and_choose
({ PROMPT
=> 'Update',
281 HEADER
=> $status_head, },
284 system(qw(git update-index --add --remove --),
285 map { $_->{VALUE
} } @update);
286 say_n_paths
('updated', @update);
292 my @update = list_and_choose
({ PROMPT
=> 'Revert',
293 HEADER
=> $status_head, },
296 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
297 map { $_->{VALUE
} } @update);
299 open $fh, '|-', qw(git update-index --index-info)
306 if ($_->{INDEX_ADDDEL
} &&
307 $_->{INDEX_ADDDEL
} eq 'create') {
308 system(qw(git update-index --force-remove --),
310 print "note: $_->{VALUE} is untracked now.\n";
314 say_n_paths
('reverted', @update);
319 sub add_untracked_cmd
{
320 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
323 system(qw(git update-index --add --), @add);
324 say_n_paths
('added', @add);
331 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
332 my (@hunk) = { TEXT
=> [] };
336 push @hunk, { TEXT
=> [] };
338 push @
{$hunk[-1]{TEXT
}}, $_;
343 sub hunk_splittable
{
346 my @s = split_hunk
($text);
350 sub parse_hunk_header
{
352 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
353 $line =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
354 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
361 # If there are context lines in the middle of a hunk,
362 # it can be split, but we would need to take care of
365 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header
($text->[0]);
371 my $next_hunk_start = undef;
372 my $i = $hunk_start - 1;
383 while (++$i < @
$text) {
384 my $line = $text->[$i];
386 if ($this->{ADDDEL
} &&
387 !defined $next_hunk_start) {
388 # We have seen leading context and
389 # adds/dels and then here is another
390 # context, which is trailing for this
391 # split hunk and leading for the next
393 $next_hunk_start = $i;
395 push @
{$this->{TEXT
}}, $line;
398 if (defined $next_hunk_start) {
405 if (defined $next_hunk_start) {
406 # We are done with the current hunk and
407 # this is the first real change for the
409 $hunk_start = $next_hunk_start;
410 $o_ofs = $this->{OLD
} + $this->{OCNT
};
411 $n_ofs = $this->{NEW
} + $this->{NCNT
};
412 $o_ofs -= $this->{POSTCTX
};
413 $n_ofs -= $this->{POSTCTX
};
417 push @
{$this->{TEXT
}}, $line;
431 for my $hunk (@split) {
432 $o_ofs = $hunk->{OLD
};
433 $n_ofs = $hunk->{NEW
};
434 $o_cnt = $hunk->{OCNT
};
435 $n_cnt = $hunk->{NCNT
};
437 my $head = ("@@ -$o_ofs" .
438 (($o_cnt != 1) ?
",$o_cnt" : '') .
440 (($n_cnt != 1) ?
",$n_cnt" : '') .
442 unshift @
{$hunk->{TEXT
}}, $head;
444 return map { $_->{TEXT
} } @split;
447 sub find_last_o_ctx
{
449 my $text = $it->{TEXT
};
450 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header
($text->[0]);
452 my $last_o_ctx = $o_ofs + $o_cnt;
454 my $line = $text->[$i];
465 my ($prev, $this) = @_;
466 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
467 parse_hunk_header
($prev->{TEXT
}[0]);
468 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
469 parse_hunk_header
($this->{TEXT
}[0]);
471 my (@line, $i, $ofs, $o_cnt, $n_cnt);
474 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
475 my $line = $prev->{TEXT
}[$i];
476 if ($line =~ /^\+/) {
482 last if ($o1_ofs <= $ofs);
492 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
493 my $line = $this->{TEXT
}[$i];
494 if ($line =~ /^\+/) {
506 my $head = ("@@ -$o0_ofs" .
507 (($o_cnt != 1) ?
",$o_cnt" : '') .
509 (($n_cnt != 1) ?
",$n_cnt" : '') .
511 @
{$prev->{TEXT
}} = ($head, @line);
514 sub coalesce_overlapping_hunks
{
520 for (grep { $_->{USE
} } @in) {
521 my $text = $_->{TEXT
};
522 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
523 parse_hunk_header
($text->[0]);
524 if (defined $last_o_ctx &&
525 $o_ofs <= $last_o_ctx) {
526 merge_hunk
($out[-1], $_);
531 $last_o_ctx = find_last_o_ctx
($out[-1]);
539 n
- do not stage this hunk
540 a
- stage this
and all the remaining hunks
541 d
- do not stage this hunk nor any of the remaining hunks
542 j
- leave this hunk undecided
, see
next undecided hunk
543 J
- leave this hunk undecided
, see
next hunk
544 k
- leave this hunk undecided
, see previous undecided hunk
545 K
- leave this hunk undecided
, see previous hunk
546 s
- split the current hunk into smaller hunks
550 sub patch_update_cmd
{
551 my @mods = list_modified
('file-only');
552 @mods = grep { !($_->{BINARY
}) } @mods;
555 my ($it) = list_and_choose
({ PROMPT
=> 'Patch update',
558 HEADER
=> $status_head, },
563 my $path = $it->{VALUE
};
564 my ($head, @hunk) = parse_diff
($path);
565 for (@
{$head->{TEXT
}}) {
572 my ($prev, $next, $other, $undecided, $i);
578 for ($i = 0; $i < $ix; $i++) {
579 if (!defined $hunk[$i]{USE
}) {
588 for ($i = $ix + 1; $i < $num; $i++) {
589 if (!defined $hunk[$i]{USE
}) {
595 if ($ix < $num - 1) {
598 for ($i = 0; $i < $num; $i++) {
599 if (!defined $hunk[$i]{USE
}) {
604 last if (!$undecided);
606 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
609 for (@
{$hunk[$ix]{TEXT
}}) {
612 print "Stage this hunk [y/n/a/d$other/?]? ";
615 if ($line =~ /^y/i) {
618 elsif ($line =~ /^n/i) {
621 elsif ($line =~ /^a/i) {
623 if (!defined $hunk[$ix]{USE
}) {
630 elsif ($line =~ /^d/i) {
632 if (!defined $hunk[$ix]{USE
}) {
639 elsif ($other =~ /K/ && $line =~ /^K/) {
643 elsif ($other =~ /J/ && $line =~ /^J/) {
647 elsif ($other =~ /k/ && $line =~ /^k/) {
651 !defined $hunk[$ix]{USE
});
655 elsif ($other =~ /j/ && $line =~ /^j/) {
658 last if ($ix >= $num ||
659 !defined $hunk[$ix]{USE
});
663 elsif ($other =~ /s/ && $line =~ /^s/) {
664 my @split = split_hunk
($hunk[$ix]{TEXT
});
667 scalar(@split), " hunks.\n";
669 splice(@hunk, $ix, 1,
670 map { +{ TEXT
=> $_, USE
=> undef } }
676 help_patch_cmd
($other);
682 last if ($ix >= $num ||
683 !defined $hunk[$ix]{USE
});
688 @hunk = coalesce_overlapping_hunks
(@hunk);
690 my ($o_lofs, $n_lofs) = (0, 0);
693 my $text = $_->{TEXT
};
694 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
695 parse_hunk_header
($text->[0]);
698 if (!defined $o_cnt) { $o_cnt = 1; }
699 if (!defined $n_cnt) { $n_cnt = 1; }
701 # We would have added ($n_cnt - $o_cnt) lines
702 # to the postimage if we were to use this hunk,
703 # but we didn't. So the line number that the next
704 # hunk starts at would be shifted by that much.
705 $n_lofs -= ($n_cnt - $o_cnt);
711 $text->[0] = ("@@ -$o_ofs" .
728 open $fh, '|-', qw(git apply --cached);
729 for (@
{$head->{TEXT
}}, @result) {
733 for (@
{$head->{TEXT
}}, @result) {
744 my @mods = list_modified
('index-only');
745 @mods = grep { !($_->{BINARY
}) } @mods;
747 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
749 HEADER
=> $status_head, },
752 system(qw(git diff-index -p --cached HEAD --),
753 map { $_->{VALUE
} } @them);
763 status
- show paths with changes
764 update
- add working tree
state to the staged set of changes
765 revert
- revert staged set of changes back to the HEAD version
766 patch
- pick hunks
and update selectively
767 diff
- view diff between HEAD
and index
768 add untracked
- add contents of untracked files to the staged set of changes
773 my @cmd = ([ 'status', \
&status_cmd
, ],
774 [ 'update', \
&update_cmd
, ],
775 [ 'revert', \
&revert_cmd
, ],
776 [ 'add untracked', \
&add_untracked_cmd
, ],
777 [ 'patch', \
&patch_update_cmd
, ],
778 [ 'diff', \
&diff_cmd
, ],
779 [ 'quit', \
&quit_cmd
, ],
780 [ 'help', \
&help_cmd
, ],
783 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
786 HEADER
=> '*** Commands ***',
787 IMMEDIATE
=> 1 }, @cmd);