8 open($fh, '-|', @_) or die;
12 my ($GIT_DIR) = run_cmd_pipe
(qw(git rev-parse --git-dir));
14 if (!defined $GIT_DIR) {
15 exit(1); # rev-parse would have already said "not a git repo"
21 open $fh, '-|', qw(git update-index --refresh)
24 ;# ignore 'needs update'
34 run_cmd_pipe
(qw(git ls-files --others
35 --exclude-per-directory=.gitignore),
36 "--exclude-from=$GIT_DIR/info/exclude",
40 my $status_fmt = '%12s %12s %s';
41 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
43 # Returns list of hashes, contents of each of which are:
44 # PRINT: print message
46 # BINARY: is a binary path
47 # INDEX: is index different from HEAD?
48 # FILE: is file different from index?
49 # INDEX_ADDDEL: is it add/delete between HEAD and index?
50 # FILE_ADDDEL: is it add/delete between index and file?
55 my ($add, $del, $adddel, $file);
57 for (run_cmd_pipe
(qw(git diff-index --cached
58 --numstat --summary HEAD))) {
59 if (($add, $del, $file) =
60 /^([-\d]+) ([-\d]+) (.*)/) {
62 if ($add eq '-' && $del eq '-') {
67 $change = "+$add/-$del";
75 elsif (($adddel, $file) =
76 /^ (create|delete) mode [0-7]+ (.*)$/) {
77 $data{$file}{INDEX_ADDDEL
} = $adddel;
81 for (run_cmd_pipe
(qw(git diff-files --numstat --summary))) {
82 if (($add, $del, $file) =
83 /^([-\d]+) ([-\d]+) (.*)/) {
84 if (!exists $data{$file}) {
91 if ($add eq '-' && $del eq '-') {
96 $change = "+$add/-$del";
98 $data{$file}{FILE
} = $change;
100 $data{$file}{BINARY
} = 1;
103 elsif (($adddel, $file) =
104 /^ (create|delete) mode [0-7]+ (.*)$/) {
105 $data{$file}{FILE_ADDDEL
} = $adddel;
109 for (sort keys %data) {
113 if ($only eq 'index-only') {
114 next if ($it->{INDEX
} eq 'unchanged');
116 if ($only eq 'file-only') {
117 next if ($it->{FILE
} eq 'nothing');
122 PRINT
=> (sprintf $status_fmt,
123 $it->{INDEX
}, $it->{FILE
}, $_),
131 my ($string, @stuff) = @_;
133 for (my $i = 0; $i < @stuff; $i++) {
137 if ((ref $it) eq 'ARRAY') {
145 if ($it =~ /^$string/) {
149 if (defined $hit && defined $found) {
159 sub list_and_choose
{
160 my ($opts, @stuff) = @_;
161 my (@chosen, @return);
168 if ($opts->{HEADER
}) {
169 if (!$opts->{LIST_FLAT
}) {
172 print "$opts->{HEADER}\n";
174 for ($i = 0; $i < @stuff; $i++) {
175 my $chosen = $chosen[$i] ?
'*' : ' ';
176 my $print = $stuff[$i];
178 if ((ref $print) eq 'ARRAY') {
179 $print = $print->[0];
182 $print = $print->{PRINT
};
185 printf("%s%2d: %s", $chosen, $i+1, $print);
186 if (($opts->{LIST_FLAT
}) &&
187 (($i + 1) % ($opts->{LIST_FLAT
}))) {
200 return if ($opts->{LIST_ONLY
});
202 print $opts->{PROMPT
};
203 if ($opts->{SINGLETON
}) {
212 my $donesomething = 0;
213 for my $choice (split(/[\s,]+/, $line)) {
217 # Input that begins with '-'; unchoose
218 if ($choice =~ s/^-//) {
221 # A range can be specified like 5-7
222 if ($choice =~ /^(\d+)-(\d+)$/) {
223 ($bottom, $top) = ($1, $2);
225 elsif ($choice =~ /^\d+$/) {
226 $bottom = $top = $choice;
228 elsif ($choice eq '*') {
233 $bottom = $top = find_unique
($choice, @stuff);
234 if (!defined $bottom) {
235 print "Huh ($choice)?\n";
239 if ($opts->{SINGLETON
} && $bottom != $top) {
240 print "Huh ($choice)?\n";
243 for ($i = $bottom-1; $i <= $top-1; $i++) {
244 next if (@stuff <= $i);
245 $chosen[$i] = $choose;
249 last if (!$donesomething || $opts->{IMMEDIATE
});
251 for ($i = 0; $i < @stuff; $i++) {
253 push @return, $stuff[$i];
260 list_and_choose
({ LIST_ONLY
=> 1, HEADER
=> $status_head },
270 print "$cnt paths\n";
278 my @mods = list_modified
('file-only');
281 my @update = list_and_choose
({ PROMPT
=> 'Update',
282 HEADER
=> $status_head, },
285 system(qw(git update-index --add --),
286 map { $_->{VALUE
} } @update);
287 say_n_paths
('updated', @update);
293 my @update = list_and_choose
({ PROMPT
=> 'Revert',
294 HEADER
=> $status_head, },
297 my @lines = run_cmd_pipe
(qw(git ls-tree HEAD --),
298 map { $_->{VALUE
} } @update);
300 open $fh, '|-', qw(git update-index --index-info)
307 if ($_->{INDEX_ADDDEL
} &&
308 $_->{INDEX_ADDDEL
} eq 'create') {
309 system(qw(git update-index --force-remove --),
311 print "note: $_->{VALUE} is untracked now.\n";
315 say_n_paths
('reverted', @update);
320 sub add_untracked_cmd
{
321 my @add = list_and_choose
({ PROMPT
=> 'Add untracked' },
324 system(qw(git update-index --add --), @add);
325 say_n_paths
('added', @add);
332 my @diff = run_cmd_pipe
(qw(git diff-files -p --), $path);
333 my (@hunk) = { TEXT
=> [] };
337 push @hunk, { TEXT
=> [] };
339 push @
{$hunk[-1]{TEXT
}}, $_;
344 sub hunk_splittable
{
347 my @s = split_hunk
($text);
351 sub parse_hunk_header
{
353 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
354 $line =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
355 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
362 # If there are context lines in the middle of a hunk,
363 # it can be split, but we would need to take care of
366 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header
($text->[0]);
372 my $next_hunk_start = undef;
373 my $i = $hunk_start - 1;
384 while (++$i < @
$text) {
385 my $line = $text->[$i];
387 if ($this->{ADDDEL
} &&
388 !defined $next_hunk_start) {
389 # We have seen leading context and
390 # adds/dels and then here is another
391 # context, which is trailing for this
392 # split hunk and leading for the next
394 $next_hunk_start = $i;
396 push @
{$this->{TEXT
}}, $line;
399 if (defined $next_hunk_start) {
406 if (defined $next_hunk_start) {
407 # We are done with the current hunk and
408 # this is the first real change for the
410 $hunk_start = $next_hunk_start;
411 $o_ofs = $this->{OLD
} + $this->{OCNT
};
412 $n_ofs = $this->{NEW
} + $this->{NCNT
};
413 $o_ofs -= $this->{POSTCTX
};
414 $n_ofs -= $this->{POSTCTX
};
418 push @
{$this->{TEXT
}}, $line;
432 for my $hunk (@split) {
433 $o_ofs = $hunk->{OLD
};
434 $n_ofs = $hunk->{NEW
};
435 $o_cnt = $hunk->{OCNT
};
436 $n_cnt = $hunk->{NCNT
};
438 my $head = ("@@ -$o_ofs" .
439 (($o_cnt != 1) ?
",$o_cnt" : '') .
441 (($n_cnt != 1) ?
",$n_cnt" : '') .
443 unshift @
{$hunk->{TEXT
}}, $head;
445 return map { $_->{TEXT
} } @split;
448 sub find_last_o_ctx
{
450 my $text = $it->{TEXT
};
451 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header
($text->[0]);
453 my $last_o_ctx = $o_ofs + $o_cnt;
455 my $line = $text->[$i];
466 my ($prev, $this) = @_;
467 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
468 parse_hunk_header
($prev->{TEXT
}[0]);
469 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
470 parse_hunk_header
($this->{TEXT
}[0]);
472 my (@line, $i, $ofs, $o_cnt, $n_cnt);
475 for ($i = 1; $i < @
{$prev->{TEXT
}}; $i++) {
476 my $line = $prev->{TEXT
}[$i];
477 if ($line =~ /^\+/) {
483 last if ($o1_ofs <= $ofs);
493 for ($i = 1; $i < @
{$this->{TEXT
}}; $i++) {
494 my $line = $this->{TEXT
}[$i];
495 if ($line =~ /^\+/) {
507 my $head = ("@@ -$o0_ofs" .
508 (($o_cnt != 1) ?
",$o_cnt" : '') .
510 (($n_cnt != 1) ?
",$n_cnt" : '') .
512 @
{$prev->{TEXT
}} = ($head, @line);
515 sub coalesce_overlapping_hunks
{
521 for (grep { $_->{USE
} } @in) {
522 my $text = $_->{TEXT
};
523 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
524 parse_hunk_header
($text->[0]);
525 if (defined $last_o_ctx &&
526 $o_ofs <= $last_o_ctx) {
527 merge_hunk
($out[-1], $_);
532 $last_o_ctx = find_last_o_ctx
($out[-1]);
540 n
- do not stage this hunk
541 a
- stage this
and all the remaining hunks
542 d
- do not stage this hunk nor any of the remaining hunks
543 j
- leave this hunk undecided
, see
next undecided hunk
544 J
- leave this hunk undecided
, see
next hunk
545 k
- leave this hunk undecided
, see previous undecided hunk
546 K
- leave this hunk undecided
, see previous hunk
547 s
- split the current hunk into smaller hunks
551 sub patch_update_cmd
{
552 my @mods = list_modified
('file-only');
553 @mods = grep { !($_->{BINARY
}) } @mods;
556 my ($it) = list_and_choose
({ PROMPT
=> 'Patch update',
559 HEADER
=> $status_head, },
564 my $path = $it->{VALUE
};
565 my ($head, @hunk) = parse_diff
($path);
566 for (@
{$head->{TEXT
}}) {
573 my ($prev, $next, $other, $undecided, $i);
579 for ($i = 0; $i < $ix; $i++) {
580 if (!defined $hunk[$i]{USE
}) {
589 for ($i = $ix + 1; $i < $num; $i++) {
590 if (!defined $hunk[$i]{USE
}) {
596 if ($ix < $num - 1) {
599 for ($i = 0; $i < $num; $i++) {
600 if (!defined $hunk[$i]{USE
}) {
605 last if (!$undecided);
607 if (hunk_splittable
($hunk[$ix]{TEXT
})) {
610 for (@
{$hunk[$ix]{TEXT
}}) {
613 print "Stage this hunk [y/n/a/d$other/?]? ";
616 if ($line =~ /^y/i) {
619 elsif ($line =~ /^n/i) {
622 elsif ($line =~ /^a/i) {
624 if (!defined $hunk[$ix]{USE
}) {
631 elsif ($line =~ /^d/i) {
633 if (!defined $hunk[$ix]{USE
}) {
640 elsif ($other =~ /K/ && $line =~ /^K/) {
644 elsif ($other =~ /J/ && $line =~ /^J/) {
648 elsif ($other =~ /k/ && $line =~ /^k/) {
652 !defined $hunk[$ix]{USE
});
656 elsif ($other =~ /j/ && $line =~ /^j/) {
659 last if ($ix >= $num ||
660 !defined $hunk[$ix]{USE
});
664 elsif ($other =~ /s/ && $line =~ /^s/) {
665 my @split = split_hunk
($hunk[$ix]{TEXT
});
668 scalar(@split), " hunks.\n";
670 splice(@hunk, $ix, 1,
671 map { +{ TEXT
=> $_, USE
=> undef } }
677 help_patch_cmd
($other);
683 last if ($ix >= $num ||
684 !defined $hunk[$ix]{USE
});
689 @hunk = coalesce_overlapping_hunks
(@hunk);
691 my ($o_lofs, $n_lofs) = (0, 0);
694 my $text = $_->{TEXT
};
695 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
696 parse_hunk_header
($text->[0]);
699 if (!defined $o_cnt) { $o_cnt = 1; }
700 if (!defined $n_cnt) { $n_cnt = 1; }
702 # We would have added ($n_cnt - $o_cnt) lines
703 # to the postimage if we were to use this hunk,
704 # but we didn't. So the line number that the next
705 # hunk starts at would be shifted by that much.
706 $n_lofs -= ($n_cnt - $o_cnt);
712 $text->[0] = ("@@ -$o_ofs" .
729 open $fh, '|-', qw(git apply --cached);
730 for (@
{$head->{TEXT
}}, @result) {
734 for (@
{$head->{TEXT
}}, @result) {
745 my @mods = list_modified
('index-only');
746 @mods = grep { !($_->{BINARY
}) } @mods;
748 my (@them) = list_and_choose
({ PROMPT
=> 'Review diff',
750 HEADER
=> $status_head, },
753 system(qw(git diff-index -p --cached HEAD --),
754 map { $_->{VALUE
} } @them);
764 status
- show paths with changes
765 update
- add working tree
state to the staged set of changes
766 revert
- revert staged set of changes back to the HEAD version
767 patch
- pick hunks
and update selectively
768 diff
- view diff between HEAD
and index
769 add untracked
- add contents of untracked files to the staged set of changes
774 my @cmd = ([ 'status', \
&status_cmd
, ],
775 [ 'update', \
&update_cmd
, ],
776 [ 'revert', \
&revert_cmd
, ],
777 [ 'add untracked', \
&add_untracked_cmd
, ],
778 [ 'patch', \
&patch_update_cmd
, ],
779 [ 'diff', \
&diff_cmd
, ],
780 [ 'quit', \
&quit_cmd
, ],
781 [ 'help', \
&help_cmd
, ],
784 my ($it) = list_and_choose
({ PROMPT
=> 'What now',
787 HEADER
=> '*** Commands ***',
788 IMMEDIATE
=> 1 }, @cmd);