builtin-add: simplify (and increase accuracy of) exclude handling
[git/dscho.git] / git-add--interactive.perl
blobdc3038091dd7db7dd4bb96cf8832df789b4d127f
1 #!/usr/bin/perl -w
3 use strict;
5 sub run_cmd_pipe {
6 my $fh = undef;
7 open($fh, '-|', @_) or die;
8 return <$fh>;
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"
16 chomp($GIT_DIR);
18 sub refresh {
19 my $fh;
20 open $fh, '-|', qw(git update-index --refresh)
21 or die;
22 while (<$fh>) {
23 ;# ignore 'needs update'
25 close $fh;
28 sub list_untracked {
29 map {
30 chomp $_;
31 $_;
33 run_cmd_pipe(qw(git ls-files --others
34 --exclude-per-directory=.gitignore),
35 "--exclude-from=$GIT_DIR/info/exclude",
36 '--', @_);
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
44 # VALUE: pathname
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?
51 sub list_modified {
52 my ($only) = @_;
53 my (%data, @return);
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]+) (.*)/) {
60 my ($change, $bin);
61 if ($add eq '-' && $del eq '-') {
62 $change = 'binary';
63 $bin = 1;
65 else {
66 $change = "+$add/-$del";
68 $data{$file} = {
69 INDEX => $change,
70 BINARY => $bin,
71 FILE => 'nothing',
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}) {
84 $data{$file} = +{
85 INDEX => 'unchanged',
86 BINARY => 0,
89 my ($change, $bin);
90 if ($add eq '-' && $del eq '-') {
91 $change = 'binary';
92 $bin = 1;
94 else {
95 $change = "+$add/-$del";
97 $data{$file}{FILE} = $change;
98 if ($bin) {
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) {
109 my $it = $data{$_};
111 if ($only) {
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');
119 push @return, +{
120 VALUE => $_,
121 PRINT => (sprintf $status_fmt,
122 $it->{INDEX}, $it->{FILE}, $_),
123 %$it,
126 return @return;
129 sub find_unique {
130 my ($string, @stuff) = @_;
131 my $found = undef;
132 for (my $i = 0; $i < @stuff; $i++) {
133 my $it = $stuff[$i];
134 my $hit = undef;
135 if (ref $it) {
136 if ((ref $it) eq 'ARRAY') {
137 $it = $it->[0];
139 else {
140 $it = $it->{VALUE};
143 eval {
144 if ($it =~ /^$string/) {
145 $hit = 1;
148 if (defined $hit && defined $found) {
149 return undef;
151 if ($hit) {
152 $found = $i + 1;
155 return $found;
158 sub list_and_choose {
159 my ($opts, @stuff) = @_;
160 my (@chosen, @return);
161 my $i;
163 TOPLOOP:
164 while (1) {
165 my $last_lf = 0;
167 if ($opts->{HEADER}) {
168 if (!$opts->{LIST_FLAT}) {
169 print " ";
171 print "$opts->{HEADER}\n";
173 for ($i = 0; $i < @stuff; $i++) {
174 my $chosen = $chosen[$i] ? '*' : ' ';
175 my $print = $stuff[$i];
176 if (ref $print) {
177 if ((ref $print) eq 'ARRAY') {
178 $print = $print->[0];
180 else {
181 $print = $print->{PRINT};
184 printf("%s%2d: %s", $chosen, $i+1, $print);
185 if (($opts->{LIST_FLAT}) &&
186 (($i + 1) % ($opts->{LIST_FLAT}))) {
187 print "\t";
188 $last_lf = 0;
190 else {
191 print "\n";
192 $last_lf = 1;
195 if (!$last_lf) {
196 print "\n";
199 return if ($opts->{LIST_ONLY});
201 print $opts->{PROMPT};
202 if ($opts->{SINGLETON}) {
203 print "> ";
205 else {
206 print ">> ";
208 my $line = <STDIN>;
209 last if (!$line);
210 chomp $line;
211 my $donesomething = 0;
212 for my $choice (split(/[\s,]+/, $line)) {
213 my $choose = 1;
214 my ($bottom, $top);
216 # Input that begins with '-'; unchoose
217 if ($choice =~ s/^-//) {
218 $choose = 0;
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 '*') {
228 $bottom = 1;
229 $top = 1 + @stuff;
231 else {
232 $bottom = $top = find_unique($choice, @stuff);
233 if (!defined $bottom) {
234 print "Huh ($choice)?\n";
235 next TOPLOOP;
238 if ($opts->{SINGLETON} && $bottom != $top) {
239 print "Huh ($choice)?\n";
240 next TOPLOOP;
242 for ($i = $bottom-1; $i <= $top-1; $i++) {
243 next if (@stuff <= $i);
244 $chosen[$i] = $choose;
245 $donesomething++;
248 last if (!$donesomething || $opts->{IMMEDIATE});
250 for ($i = 0; $i < @stuff; $i++) {
251 if ($chosen[$i]) {
252 push @return, $stuff[$i];
255 return @return;
258 sub status_cmd {
259 list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
260 list_modified());
261 print "\n";
264 sub say_n_paths {
265 my $did = shift @_;
266 my $cnt = scalar @_;
267 print "$did ";
268 if (1 < $cnt) {
269 print "$cnt paths\n";
271 else {
272 print "one path\n";
276 sub update_cmd {
277 my @mods = list_modified('file-only');
278 return if (!@mods);
280 my @update = list_and_choose({ PROMPT => 'Update',
281 HEADER => $status_head, },
282 @mods);
283 if (@update) {
284 system(qw(git update-index --add --remove --),
285 map { $_->{VALUE} } @update);
286 say_n_paths('updated', @update);
288 print "\n";
291 sub revert_cmd {
292 my @update = list_and_choose({ PROMPT => 'Revert',
293 HEADER => $status_head, },
294 list_modified());
295 if (@update) {
296 my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
297 map { $_->{VALUE} } @update);
298 my $fh;
299 open $fh, '|-', qw(git update-index --index-info)
300 or die;
301 for (@lines) {
302 print $fh $_;
304 close($fh);
305 for (@update) {
306 if ($_->{INDEX_ADDDEL} &&
307 $_->{INDEX_ADDDEL} eq 'create') {
308 system(qw(git update-index --force-remove --),
309 $_->{VALUE});
310 print "note: $_->{VALUE} is untracked now.\n";
313 refresh();
314 say_n_paths('reverted', @update);
316 print "\n";
319 sub add_untracked_cmd {
320 my @add = list_and_choose({ PROMPT => 'Add untracked' },
321 list_untracked());
322 if (@add) {
323 system(qw(git update-index --add --), @add);
324 say_n_paths('added', @add);
326 print "\n";
329 sub parse_diff {
330 my ($path) = @_;
331 my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
332 my (@hunk) = { TEXT => [] };
334 for (@diff) {
335 if (/^@@ /) {
336 push @hunk, { TEXT => [] };
338 push @{$hunk[-1]{TEXT}}, $_;
340 return @hunk;
343 sub hunk_splittable {
344 my ($text) = @_;
346 my @s = split_hunk($text);
347 return (1 < @s);
350 sub parse_hunk_header {
351 my ($line) = @_;
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);
357 sub split_hunk {
358 my ($text) = @_;
359 my @split = ();
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
363 # overlaps later.
365 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
366 my $hunk_start = 1;
367 my $next_hunk_start;
369 OUTER:
370 while (1) {
371 my $next_hunk_start = undef;
372 my $i = $hunk_start - 1;
373 my $this = +{
374 TEXT => [],
375 OLD => $o_ofs,
376 NEW => $n_ofs,
377 OCNT => 0,
378 NCNT => 0,
379 ADDDEL => 0,
380 POSTCTX => 0,
383 while (++$i < @$text) {
384 my $line = $text->[$i];
385 if ($line =~ /^ /) {
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
392 # one.
393 $next_hunk_start = $i;
395 push @{$this->{TEXT}}, $line;
396 $this->{OCNT}++;
397 $this->{NCNT}++;
398 if (defined $next_hunk_start) {
399 $this->{POSTCTX}++;
401 next;
404 # add/del
405 if (defined $next_hunk_start) {
406 # We are done with the current hunk and
407 # this is the first real change for the
408 # next split one.
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};
414 push @split, $this;
415 redo OUTER;
417 push @{$this->{TEXT}}, $line;
418 $this->{ADDDEL}++;
419 if ($line =~ /^-/) {
420 $this->{OCNT}++;
422 else {
423 $this->{NCNT}++;
427 push @split, $this;
428 last;
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" : '') .
439 " +$n_ofs" .
440 (($n_cnt != 1) ? ",$n_cnt" : '') .
441 " @@\n");
442 unshift @{$hunk->{TEXT}}, $head;
444 return map { $_->{TEXT} } @split;
447 sub find_last_o_ctx {
448 my ($it) = @_;
449 my $text = $it->{TEXT};
450 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
451 my $i = @{$text};
452 my $last_o_ctx = $o_ofs + $o_cnt;
453 while (0 < --$i) {
454 my $line = $text->[$i];
455 if ($line =~ /^ /) {
456 $last_o_ctx--;
457 next;
459 last;
461 return $last_o_ctx;
464 sub merge_hunk {
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);
472 $ofs = $o0_ofs;
473 $o_cnt = $n_cnt = 0;
474 for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
475 my $line = $prev->{TEXT}[$i];
476 if ($line =~ /^\+/) {
477 $n_cnt++;
478 push @line, $line;
479 next;
482 last if ($o1_ofs <= $ofs);
484 $o_cnt++;
485 $ofs++;
486 if ($line =~ /^ /) {
487 $n_cnt++;
489 push @line, $line;
492 for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
493 my $line = $this->{TEXT}[$i];
494 if ($line =~ /^\+/) {
495 $n_cnt++;
496 push @line, $line;
497 next;
499 $ofs++;
500 $o_cnt++;
501 if ($line =~ /^ /) {
502 $n_cnt++;
504 push @line, $line;
506 my $head = ("@@ -$o0_ofs" .
507 (($o_cnt != 1) ? ",$o_cnt" : '') .
508 " +$n0_ofs" .
509 (($n_cnt != 1) ? ",$n_cnt" : '') .
510 " @@\n");
511 @{$prev->{TEXT}} = ($head, @line);
514 sub coalesce_overlapping_hunks {
515 my (@in) = @_;
516 my @out = ();
518 my ($last_o_ctx);
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], $_);
528 else {
529 push @out, $_;
531 $last_o_ctx = find_last_o_ctx($out[-1]);
533 return @out;
536 sub help_patch_cmd {
537 print <<\EOF ;
538 y - stage this hunk
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;
553 return if (!@mods);
555 my ($it) = list_and_choose({ PROMPT => 'Patch update',
556 SINGLETON => 1,
557 IMMEDIATE => 1,
558 HEADER => $status_head, },
559 @mods);
560 return if (!$it);
562 my ($ix, $num);
563 my $path = $it->{VALUE};
564 my ($head, @hunk) = parse_diff($path);
565 for (@{$head->{TEXT}}) {
566 print;
568 $num = scalar @hunk;
569 $ix = 0;
571 while (1) {
572 my ($prev, $next, $other, $undecided, $i);
573 $other = '';
575 if ($num <= $ix) {
576 $ix = 0;
578 for ($i = 0; $i < $ix; $i++) {
579 if (!defined $hunk[$i]{USE}) {
580 $prev = 1;
581 $other .= '/k';
582 last;
585 if ($ix) {
586 $other .= '/K';
588 for ($i = $ix + 1; $i < $num; $i++) {
589 if (!defined $hunk[$i]{USE}) {
590 $next = 1;
591 $other .= '/j';
592 last;
595 if ($ix < $num - 1) {
596 $other .= '/J';
598 for ($i = 0; $i < $num; $i++) {
599 if (!defined $hunk[$i]{USE}) {
600 $undecided = 1;
601 last;
604 last if (!$undecided);
606 if (hunk_splittable($hunk[$ix]{TEXT})) {
607 $other .= '/s';
609 for (@{$hunk[$ix]{TEXT}}) {
610 print;
612 print "Stage this hunk [y/n/a/d$other/?]? ";
613 my $line = <STDIN>;
614 if ($line) {
615 if ($line =~ /^y/i) {
616 $hunk[$ix]{USE} = 1;
618 elsif ($line =~ /^n/i) {
619 $hunk[$ix]{USE} = 0;
621 elsif ($line =~ /^a/i) {
622 while ($ix < $num) {
623 if (!defined $hunk[$ix]{USE}) {
624 $hunk[$ix]{USE} = 1;
626 $ix++;
628 next;
630 elsif ($line =~ /^d/i) {
631 while ($ix < $num) {
632 if (!defined $hunk[$ix]{USE}) {
633 $hunk[$ix]{USE} = 0;
635 $ix++;
637 next;
639 elsif ($other =~ /K/ && $line =~ /^K/) {
640 $ix--;
641 next;
643 elsif ($other =~ /J/ && $line =~ /^J/) {
644 $ix++;
645 next;
647 elsif ($other =~ /k/ && $line =~ /^k/) {
648 while (1) {
649 $ix--;
650 last if (!$ix ||
651 !defined $hunk[$ix]{USE});
653 next;
655 elsif ($other =~ /j/ && $line =~ /^j/) {
656 while (1) {
657 $ix++;
658 last if ($ix >= $num ||
659 !defined $hunk[$ix]{USE});
661 next;
663 elsif ($other =~ /s/ && $line =~ /^s/) {
664 my @split = split_hunk($hunk[$ix]{TEXT});
665 if (1 < @split) {
666 print "Split into ",
667 scalar(@split), " hunks.\n";
669 splice(@hunk, $ix, 1,
670 map { +{ TEXT => $_, USE => undef } }
671 @split);
672 $num = scalar @hunk;
673 next;
675 else {
676 help_patch_cmd($other);
677 next;
679 # soft increment
680 while (1) {
681 $ix++;
682 last if ($ix >= $num ||
683 !defined $hunk[$ix]{USE});
688 @hunk = coalesce_overlapping_hunks(@hunk);
690 my ($o_lofs, $n_lofs) = (0, 0);
691 my @result = ();
692 for (@hunk) {
693 my $text = $_->{TEXT};
694 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
695 parse_hunk_header($text->[0]);
697 if (!$_->{USE}) {
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);
706 next;
708 else {
709 if ($n_lofs) {
710 $n_ofs += $n_lofs;
711 $text->[0] = ("@@ -$o_ofs" .
712 ((defined $o_cnt)
713 ? ",$o_cnt" : '') .
714 " +$n_ofs" .
715 ((defined $n_cnt)
716 ? ",$n_cnt" : '') .
717 " @@\n");
719 for (@$text) {
720 push @result, $_;
725 if (@result) {
726 my $fh;
728 open $fh, '|-', qw(git apply --cached);
729 for (@{$head->{TEXT}}, @result) {
730 print $fh $_;
732 if (!close $fh) {
733 for (@{$head->{TEXT}}, @result) {
734 print STDERR $_;
737 refresh();
740 print "\n";
743 sub diff_cmd {
744 my @mods = list_modified('index-only');
745 @mods = grep { !($_->{BINARY}) } @mods;
746 return if (!@mods);
747 my (@them) = list_and_choose({ PROMPT => 'Review diff',
748 IMMEDIATE => 1,
749 HEADER => $status_head, },
750 @mods);
751 return if (!@them);
752 system(qw(git diff-index -p --cached HEAD --),
753 map { $_->{VALUE} } @them);
756 sub quit_cmd {
757 print "Bye.\n";
758 exit(0);
761 sub help_cmd {
762 print <<\EOF ;
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
772 sub main_loop {
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, ],
782 while (1) {
783 my ($it) = list_and_choose({ PROMPT => 'What now',
784 SINGLETON => 1,
785 LIST_FLAT => 4,
786 HEADER => '*** Commands ***',
787 IMMEDIATE => 1 }, @cmd);
788 if ($it) {
789 eval {
790 $it->[1]->();
792 if ($@) {
793 print "$@";
799 my @z;
801 refresh();
802 status_cmd();
803 main_loop();