git-add--interactive.perlon commit Merge branch 'maint' (2af89f1)
   1#!/usr/bin/perl -w
   2
   3use strict;
   4
   5sub run_cmd_pipe {
   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 ? "\"$_\"": $_ } @_;
  10                return qx{@args};
  11        } else {
  12                my $fh = undef;
  13                open($fh, '-|', @_) or die;
  14                return <$fh>;
  15        }
  16}
  17
  18my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir));
  19
  20if (!defined $GIT_DIR) {
  21        exit(1); # rev-parse would have already said "not a git repo"
  22}
  23chomp($GIT_DIR);
  24
  25sub refresh {
  26        my $fh;
  27        open $fh, 'git update-index --refresh |'
  28            or die;
  29        while (<$fh>) {
  30                ;# ignore 'needs update'
  31        }
  32        close $fh;
  33}
  34
  35sub list_untracked {
  36        map {
  37                chomp $_;
  38                $_;
  39        }
  40        run_cmd_pipe(qw(git ls-files --others
  41                        --exclude-per-directory=.gitignore),
  42                     "--exclude-from=$GIT_DIR/info/exclude",
  43                     '--', @_);
  44}
  45
  46my $status_fmt = '%12s %12s %s';
  47my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
  48
  49# Returns list of hashes, contents of each of which are:
  50# PRINT:        print message
  51# VALUE:        pathname
  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?
  57
  58sub list_modified {
  59        my ($only) = @_;
  60        my (%data, @return);
  61        my ($add, $del, $adddel, $file);
  62
  63        for (run_cmd_pipe(qw(git diff-index --cached
  64                             --numstat --summary HEAD))) {
  65                if (($add, $del, $file) =
  66                    /^([-\d]+)  ([-\d]+)        (.*)/) {
  67                        my ($change, $bin);
  68                        if ($add eq '-' && $del eq '-') {
  69                                $change = 'binary';
  70                                $bin = 1;
  71                        }
  72                        else {
  73                                $change = "+$add/-$del";
  74                        }
  75                        $data{$file} = {
  76                                INDEX => $change,
  77                                BINARY => $bin,
  78                                FILE => 'nothing',
  79                        }
  80                }
  81                elsif (($adddel, $file) =
  82                       /^ (create|delete) mode [0-7]+ (.*)$/) {
  83                        $data{$file}{INDEX_ADDDEL} = $adddel;
  84                }
  85        }
  86
  87        for (run_cmd_pipe(qw(git diff-files --numstat --summary))) {
  88                if (($add, $del, $file) =
  89                    /^([-\d]+)  ([-\d]+)        (.*)/) {
  90                        if (!exists $data{$file}) {
  91                                $data{$file} = +{
  92                                        INDEX => 'unchanged',
  93                                        BINARY => 0,
  94                                };
  95                        }
  96                        my ($change, $bin);
  97                        if ($add eq '-' && $del eq '-') {
  98                                $change = 'binary';
  99                                $bin = 1;
 100                        }
 101                        else {
 102                                $change = "+$add/-$del";
 103                        }
 104                        $data{$file}{FILE} = $change;
 105                        if ($bin) {
 106                                $data{$file}{BINARY} = 1;
 107                        }
 108                }
 109                elsif (($adddel, $file) =
 110                       /^ (create|delete) mode [0-7]+ (.*)$/) {
 111                        $data{$file}{FILE_ADDDEL} = $adddel;
 112                }
 113        }
 114
 115        for (sort keys %data) {
 116                my $it = $data{$_};
 117
 118                if ($only) {
 119                        if ($only eq 'index-only') {
 120                                next if ($it->{INDEX} eq 'unchanged');
 121                        }
 122                        if ($only eq 'file-only') {
 123                                next if ($it->{FILE} eq 'nothing');
 124                        }
 125                }
 126                push @return, +{
 127                        VALUE => $_,
 128                        PRINT => (sprintf $status_fmt,
 129                                  $it->{INDEX}, $it->{FILE}, $_),
 130                        %$it,
 131                };
 132        }
 133        return @return;
 134}
 135
 136sub find_unique {
 137        my ($string, @stuff) = @_;
 138        my $found = undef;
 139        for (my $i = 0; $i < @stuff; $i++) {
 140                my $it = $stuff[$i];
 141                my $hit = undef;
 142                if (ref $it) {
 143                        if ((ref $it) eq 'ARRAY') {
 144                                $it = $it->[0];
 145                        }
 146                        else {
 147                                $it = $it->{VALUE};
 148                        }
 149                }
 150                eval {
 151                        if ($it =~ /^$string/) {
 152                                $hit = 1;
 153                        };
 154                };
 155                if (defined $hit && defined $found) {
 156                        return undef;
 157                }
 158                if ($hit) {
 159                        $found = $i + 1;
 160                }
 161        }
 162        return $found;
 163}
 164
 165sub list_and_choose {
 166        my ($opts, @stuff) = @_;
 167        my (@chosen, @return);
 168        my $i;
 169
 170      TOPLOOP:
 171        while (1) {
 172                my $last_lf = 0;
 173
 174                if ($opts->{HEADER}) {
 175                        if (!$opts->{LIST_FLAT}) {
 176                                print "     ";
 177                        }
 178                        print "$opts->{HEADER}\n";
 179                }
 180                for ($i = 0; $i < @stuff; $i++) {
 181                        my $chosen = $chosen[$i] ? '*' : ' ';
 182                        my $print = $stuff[$i];
 183                        if (ref $print) {
 184                                if ((ref $print) eq 'ARRAY') {
 185                                        $print = $print->[0];
 186                                }
 187                                else {
 188                                        $print = $print->{PRINT};
 189                                }
 190                        }
 191                        printf("%s%2d: %s", $chosen, $i+1, $print);
 192                        if (($opts->{LIST_FLAT}) &&
 193                            (($i + 1) % ($opts->{LIST_FLAT}))) {
 194                                print "\t";
 195                                $last_lf = 0;
 196                        }
 197                        else {
 198                                print "\n";
 199                                $last_lf = 1;
 200                        }
 201                }
 202                if (!$last_lf) {
 203                        print "\n";
 204                }
 205
 206                return if ($opts->{LIST_ONLY});
 207
 208                print $opts->{PROMPT};
 209                if ($opts->{SINGLETON}) {
 210                        print "> ";
 211                }
 212                else {
 213                        print ">> ";
 214                }
 215                my $line = <STDIN>;
 216                if (!$line) {
 217                        print "\n";
 218                        $opts->{ON_EOF}->() if $opts->{ON_EOF};
 219                        last;
 220                }
 221                chomp $line;
 222                last if $line eq '';
 223                for my $choice (split(/[\s,]+/, $line)) {
 224                        my $choose = 1;
 225                        my ($bottom, $top);
 226
 227                        # Input that begins with '-'; unchoose
 228                        if ($choice =~ s/^-//) {
 229                                $choose = 0;
 230                        }
 231                        # A range can be specified like 5-7
 232                        if ($choice =~ /^(\d+)-(\d+)$/) {
 233                                ($bottom, $top) = ($1, $2);
 234                        }
 235                        elsif ($choice =~ /^\d+$/) {
 236                                $bottom = $top = $choice;
 237                        }
 238                        elsif ($choice eq '*') {
 239                                $bottom = 1;
 240                                $top = 1 + @stuff;
 241                        }
 242                        else {
 243                                $bottom = $top = find_unique($choice, @stuff);
 244                                if (!defined $bottom) {
 245                                        print "Huh ($choice)?\n";
 246                                        next TOPLOOP;
 247                                }
 248                        }
 249                        if ($opts->{SINGLETON} && $bottom != $top) {
 250                                print "Huh ($choice)?\n";
 251                                next TOPLOOP;
 252                        }
 253                        for ($i = $bottom-1; $i <= $top-1; $i++) {
 254                                next if (@stuff <= $i || $i < 0);
 255                                $chosen[$i] = $choose;
 256                        }
 257                }
 258                last if ($opts->{IMMEDIATE});
 259        }
 260        for ($i = 0; $i < @stuff; $i++) {
 261                if ($chosen[$i]) {
 262                        push @return, $stuff[$i];
 263                }
 264        }
 265        return @return;
 266}
 267
 268sub status_cmd {
 269        list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
 270                        list_modified());
 271        print "\n";
 272}
 273
 274sub say_n_paths {
 275        my $did = shift @_;
 276        my $cnt = scalar @_;
 277        print "$did ";
 278        if (1 < $cnt) {
 279                print "$cnt paths\n";
 280        }
 281        else {
 282                print "one path\n";
 283        }
 284}
 285
 286sub update_cmd {
 287        my @mods = list_modified('file-only');
 288        return if (!@mods);
 289
 290        my @update = list_and_choose({ PROMPT => 'Update',
 291                                       HEADER => $status_head, },
 292                                     @mods);
 293        if (@update) {
 294                system(qw(git update-index --add --remove --),
 295                       map { $_->{VALUE} } @update);
 296                say_n_paths('updated', @update);
 297        }
 298        print "\n";
 299}
 300
 301sub revert_cmd {
 302        my @update = list_and_choose({ PROMPT => 'Revert',
 303                                       HEADER => $status_head, },
 304                                     list_modified());
 305        if (@update) {
 306                my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
 307                                         map { $_->{VALUE} } @update);
 308                my $fh;
 309                open $fh, '| git update-index --index-info'
 310                    or die;
 311                for (@lines) {
 312                        print $fh $_;
 313                }
 314                close($fh);
 315                for (@update) {
 316                        if ($_->{INDEX_ADDDEL} &&
 317                            $_->{INDEX_ADDDEL} eq 'create') {
 318                                system(qw(git update-index --force-remove --),
 319                                       $_->{VALUE});
 320                                print "note: $_->{VALUE} is untracked now.\n";
 321                        }
 322                }
 323                refresh();
 324                say_n_paths('reverted', @update);
 325        }
 326        print "\n";
 327}
 328
 329sub add_untracked_cmd {
 330        my @add = list_and_choose({ PROMPT => 'Add untracked' },
 331                                  list_untracked());
 332        if (@add) {
 333                system(qw(git update-index --add --), @add);
 334                say_n_paths('added', @add);
 335        }
 336        print "\n";
 337}
 338
 339sub parse_diff {
 340        my ($path) = @_;
 341        my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
 342        my (@hunk) = { TEXT => [] };
 343
 344        for (@diff) {
 345                if (/^@@ /) {
 346                        push @hunk, { TEXT => [] };
 347                }
 348                push @{$hunk[-1]{TEXT}}, $_;
 349        }
 350        return @hunk;
 351}
 352
 353sub hunk_splittable {
 354        my ($text) = @_;
 355
 356        my @s = split_hunk($text);
 357        return (1 < @s);
 358}
 359
 360sub parse_hunk_header {
 361        my ($line) = @_;
 362        my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
 363            $line =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
 364        return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
 365}
 366
 367sub split_hunk {
 368        my ($text) = @_;
 369        my @split = ();
 370
 371        # If there are context lines in the middle of a hunk,
 372        # it can be split, but we would need to take care of
 373        # overlaps later.
 374
 375        my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
 376        my $hunk_start = 1;
 377        my $next_hunk_start;
 378
 379      OUTER:
 380        while (1) {
 381                my $next_hunk_start = undef;
 382                my $i = $hunk_start - 1;
 383                my $this = +{
 384                        TEXT => [],
 385                        OLD => $o_ofs,
 386                        NEW => $n_ofs,
 387                        OCNT => 0,
 388                        NCNT => 0,
 389                        ADDDEL => 0,
 390                        POSTCTX => 0,
 391                };
 392
 393                while (++$i < @$text) {
 394                        my $line = $text->[$i];
 395                        if ($line =~ /^ /) {
 396                                if ($this->{ADDDEL} &&
 397                                    !defined $next_hunk_start) {
 398                                        # We have seen leading context and
 399                                        # adds/dels and then here is another
 400                                        # context, which is trailing for this
 401                                        # split hunk and leading for the next
 402                                        # one.
 403                                        $next_hunk_start = $i;
 404                                }
 405                                push @{$this->{TEXT}}, $line;
 406                                $this->{OCNT}++;
 407                                $this->{NCNT}++;
 408                                if (defined $next_hunk_start) {
 409                                        $this->{POSTCTX}++;
 410                                }
 411                                next;
 412                        }
 413
 414                        # add/del
 415                        if (defined $next_hunk_start) {
 416                                # We are done with the current hunk and
 417                                # this is the first real change for the
 418                                # next split one.
 419                                $hunk_start = $next_hunk_start;
 420                                $o_ofs = $this->{OLD} + $this->{OCNT};
 421                                $n_ofs = $this->{NEW} + $this->{NCNT};
 422                                $o_ofs -= $this->{POSTCTX};
 423                                $n_ofs -= $this->{POSTCTX};
 424                                push @split, $this;
 425                                redo OUTER;
 426                        }
 427                        push @{$this->{TEXT}}, $line;
 428                        $this->{ADDDEL}++;
 429                        if ($line =~ /^-/) {
 430                                $this->{OCNT}++;
 431                        }
 432                        else {
 433                                $this->{NCNT}++;
 434                        }
 435                }
 436
 437                push @split, $this;
 438                last;
 439        }
 440
 441        for my $hunk (@split) {
 442                $o_ofs = $hunk->{OLD};
 443                $n_ofs = $hunk->{NEW};
 444                $o_cnt = $hunk->{OCNT};
 445                $n_cnt = $hunk->{NCNT};
 446
 447                my $head = ("@@ -$o_ofs" .
 448                            (($o_cnt != 1) ? ",$o_cnt" : '') .
 449                            " +$n_ofs" .
 450                            (($n_cnt != 1) ? ",$n_cnt" : '') .
 451                            " @@\n");
 452                unshift @{$hunk->{TEXT}}, $head;
 453        }
 454        return map { $_->{TEXT} } @split;
 455}
 456
 457sub find_last_o_ctx {
 458        my ($it) = @_;
 459        my $text = $it->{TEXT};
 460        my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
 461        my $i = @{$text};
 462        my $last_o_ctx = $o_ofs + $o_cnt;
 463        while (0 < --$i) {
 464                my $line = $text->[$i];
 465                if ($line =~ /^ /) {
 466                        $last_o_ctx--;
 467                        next;
 468                }
 469                last;
 470        }
 471        return $last_o_ctx;
 472}
 473
 474sub merge_hunk {
 475        my ($prev, $this) = @_;
 476        my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
 477            parse_hunk_header($prev->{TEXT}[0]);
 478        my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
 479            parse_hunk_header($this->{TEXT}[0]);
 480
 481        my (@line, $i, $ofs, $o_cnt, $n_cnt);
 482        $ofs = $o0_ofs;
 483        $o_cnt = $n_cnt = 0;
 484        for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
 485                my $line = $prev->{TEXT}[$i];
 486                if ($line =~ /^\+/) {
 487                        $n_cnt++;
 488                        push @line, $line;
 489                        next;
 490                }
 491
 492                last if ($o1_ofs <= $ofs);
 493
 494                $o_cnt++;
 495                $ofs++;
 496                if ($line =~ /^ /) {
 497                        $n_cnt++;
 498                }
 499                push @line, $line;
 500        }
 501
 502        for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
 503                my $line = $this->{TEXT}[$i];
 504                if ($line =~ /^\+/) {
 505                        $n_cnt++;
 506                        push @line, $line;
 507                        next;
 508                }
 509                $ofs++;
 510                $o_cnt++;
 511                if ($line =~ /^ /) {
 512                        $n_cnt++;
 513                }
 514                push @line, $line;
 515        }
 516        my $head = ("@@ -$o0_ofs" .
 517                    (($o_cnt != 1) ? ",$o_cnt" : '') .
 518                    " +$n0_ofs" .
 519                    (($n_cnt != 1) ? ",$n_cnt" : '') .
 520                    " @@\n");
 521        @{$prev->{TEXT}} = ($head, @line);
 522}
 523
 524sub coalesce_overlapping_hunks {
 525        my (@in) = @_;
 526        my @out = ();
 527
 528        my ($last_o_ctx);
 529
 530        for (grep { $_->{USE} } @in) {
 531                my $text = $_->{TEXT};
 532                my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
 533                    parse_hunk_header($text->[0]);
 534                if (defined $last_o_ctx &&
 535                    $o_ofs <= $last_o_ctx) {
 536                        merge_hunk($out[-1], $_);
 537                }
 538                else {
 539                        push @out, $_;
 540                }
 541                $last_o_ctx = find_last_o_ctx($out[-1]);
 542        }
 543        return @out;
 544}
 545
 546sub help_patch_cmd {
 547        print <<\EOF ;
 548y - stage this hunk
 549n - do not stage this hunk
 550a - stage this and all the remaining hunks
 551d - do not stage this hunk nor any of the remaining hunks
 552j - leave this hunk undecided, see next undecided hunk
 553J - leave this hunk undecided, see next hunk
 554k - leave this hunk undecided, see previous undecided hunk
 555K - leave this hunk undecided, see previous hunk
 556s - split the current hunk into smaller hunks
 557EOF
 558}
 559
 560sub patch_update_cmd {
 561        my @mods = list_modified('file-only');
 562        @mods = grep { !($_->{BINARY}) } @mods;
 563        return if (!@mods);
 564
 565        my ($it) = list_and_choose({ PROMPT => 'Patch update',
 566                                     SINGLETON => 1,
 567                                     IMMEDIATE => 1,
 568                                     HEADER => $status_head, },
 569                                   @mods);
 570        return if (!$it);
 571
 572        my ($ix, $num);
 573        my $path = $it->{VALUE};
 574        my ($head, @hunk) = parse_diff($path);
 575        for (@{$head->{TEXT}}) {
 576                print;
 577        }
 578        $num = scalar @hunk;
 579        $ix = 0;
 580
 581        while (1) {
 582                my ($prev, $next, $other, $undecided, $i);
 583                $other = '';
 584
 585                if ($num <= $ix) {
 586                        $ix = 0;
 587                }
 588                for ($i = 0; $i < $ix; $i++) {
 589                        if (!defined $hunk[$i]{USE}) {
 590                                $prev = 1;
 591                                $other .= '/k';
 592                                last;
 593                        }
 594                }
 595                if ($ix) {
 596                        $other .= '/K';
 597                }
 598                for ($i = $ix + 1; $i < $num; $i++) {
 599                        if (!defined $hunk[$i]{USE}) {
 600                                $next = 1;
 601                                $other .= '/j';
 602                                last;
 603                        }
 604                }
 605                if ($ix < $num - 1) {
 606                        $other .= '/J';
 607                }
 608                for ($i = 0; $i < $num; $i++) {
 609                        if (!defined $hunk[$i]{USE}) {
 610                                $undecided = 1;
 611                                last;
 612                        }
 613                }
 614                last if (!$undecided);
 615
 616                if (hunk_splittable($hunk[$ix]{TEXT})) {
 617                        $other .= '/s';
 618                }
 619                for (@{$hunk[$ix]{TEXT}}) {
 620                        print;
 621                }
 622                print "Stage this hunk [y/n/a/d$other/?]? ";
 623                my $line = <STDIN>;
 624                if ($line) {
 625                        if ($line =~ /^y/i) {
 626                                $hunk[$ix]{USE} = 1;
 627                        }
 628                        elsif ($line =~ /^n/i) {
 629                                $hunk[$ix]{USE} = 0;
 630                        }
 631                        elsif ($line =~ /^a/i) {
 632                                while ($ix < $num) {
 633                                        if (!defined $hunk[$ix]{USE}) {
 634                                                $hunk[$ix]{USE} = 1;
 635                                        }
 636                                        $ix++;
 637                                }
 638                                next;
 639                        }
 640                        elsif ($line =~ /^d/i) {
 641                                while ($ix < $num) {
 642                                        if (!defined $hunk[$ix]{USE}) {
 643                                                $hunk[$ix]{USE} = 0;
 644                                        }
 645                                        $ix++;
 646                                }
 647                                next;
 648                        }
 649                        elsif ($other =~ /K/ && $line =~ /^K/) {
 650                                $ix--;
 651                                next;
 652                        }
 653                        elsif ($other =~ /J/ && $line =~ /^J/) {
 654                                $ix++;
 655                                next;
 656                        }
 657                        elsif ($other =~ /k/ && $line =~ /^k/) {
 658                                while (1) {
 659                                        $ix--;
 660                                        last if (!$ix ||
 661                                                 !defined $hunk[$ix]{USE});
 662                                }
 663                                next;
 664                        }
 665                        elsif ($other =~ /j/ && $line =~ /^j/) {
 666                                while (1) {
 667                                        $ix++;
 668                                        last if ($ix >= $num ||
 669                                                 !defined $hunk[$ix]{USE});
 670                                }
 671                                next;
 672                        }
 673                        elsif ($other =~ /s/ && $line =~ /^s/) {
 674                                my @split = split_hunk($hunk[$ix]{TEXT});
 675                                if (1 < @split) {
 676                                        print "Split into ",
 677                                        scalar(@split), " hunks.\n";
 678                                }
 679                                splice(@hunk, $ix, 1,
 680                                       map { +{ TEXT => $_, USE => undef } }
 681                                       @split);
 682                                $num = scalar @hunk;
 683                                next;
 684                        }
 685                        else {
 686                                help_patch_cmd($other);
 687                                next;
 688                        }
 689                        # soft increment
 690                        while (1) {
 691                                $ix++;
 692                                last if ($ix >= $num ||
 693                                         !defined $hunk[$ix]{USE});
 694                        }
 695                }
 696        }
 697
 698        @hunk = coalesce_overlapping_hunks(@hunk);
 699
 700        my ($o_lofs, $n_lofs) = (0, 0);
 701        my @result = ();
 702        for (@hunk) {
 703                my $text = $_->{TEXT};
 704                my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
 705                    parse_hunk_header($text->[0]);
 706
 707                if (!$_->{USE}) {
 708                        if (!defined $o_cnt) { $o_cnt = 1; }
 709                        if (!defined $n_cnt) { $n_cnt = 1; }
 710
 711                        # We would have added ($n_cnt - $o_cnt) lines
 712                        # to the postimage if we were to use this hunk,
 713                        # but we didn't.  So the line number that the next
 714                        # hunk starts at would be shifted by that much.
 715                        $n_lofs -= ($n_cnt - $o_cnt);
 716                        next;
 717                }
 718                else {
 719                        if ($n_lofs) {
 720                                $n_ofs += $n_lofs;
 721                                $text->[0] = ("@@ -$o_ofs" .
 722                                              ((defined $o_cnt)
 723                                               ? ",$o_cnt" : '') .
 724                                              " +$n_ofs" .
 725                                              ((defined $n_cnt)
 726                                               ? ",$n_cnt" : '') .
 727                                              " @@\n");
 728                        }
 729                        for (@$text) {
 730                                push @result, $_;
 731                        }
 732                }
 733        }
 734
 735        if (@result) {
 736                my $fh;
 737
 738                open $fh, '| git apply --cached';
 739                for (@{$head->{TEXT}}, @result) {
 740                        print $fh $_;
 741                }
 742                if (!close $fh) {
 743                        for (@{$head->{TEXT}}, @result) {
 744                                print STDERR $_;
 745                        }
 746                }
 747                refresh();
 748        }
 749
 750        print "\n";
 751}
 752
 753sub diff_cmd {
 754        my @mods = list_modified('index-only');
 755        @mods = grep { !($_->{BINARY}) } @mods;
 756        return if (!@mods);
 757        my (@them) = list_and_choose({ PROMPT => 'Review diff',
 758                                     IMMEDIATE => 1,
 759                                     HEADER => $status_head, },
 760                                   @mods);
 761        return if (!@them);
 762        system(qw(git diff-index -p --cached HEAD --),
 763               map { $_->{VALUE} } @them);
 764}
 765
 766sub quit_cmd {
 767        print "Bye.\n";
 768        exit(0);
 769}
 770
 771sub help_cmd {
 772        print <<\EOF ;
 773status        - show paths with changes
 774update        - add working tree state to the staged set of changes
 775revert        - revert staged set of changes back to the HEAD version
 776patch         - pick hunks and update selectively
 777diff          - view diff between HEAD and index
 778add untracked - add contents of untracked files to the staged set of changes
 779EOF
 780}
 781
 782sub main_loop {
 783        my @cmd = ([ 'status', \&status_cmd, ],
 784                   [ 'update', \&update_cmd, ],
 785                   [ 'revert', \&revert_cmd, ],
 786                   [ 'add untracked', \&add_untracked_cmd, ],
 787                   [ 'patch', \&patch_update_cmd, ],
 788                   [ 'diff', \&diff_cmd, ],
 789                   [ 'quit', \&quit_cmd, ],
 790                   [ 'help', \&help_cmd, ],
 791        );
 792        while (1) {
 793                my ($it) = list_and_choose({ PROMPT => 'What now',
 794                                             SINGLETON => 1,
 795                                             LIST_FLAT => 4,
 796                                             HEADER => '*** Commands ***',
 797                                             ON_EOF => \&quit_cmd,
 798                                             IMMEDIATE => 1 }, @cmd);
 799                if ($it) {
 800                        eval {
 801                                $it->[1]->();
 802                        };
 803                        if ($@) {
 804                                print "$@";
 805                        }
 806                }
 807        }
 808}
 809
 810my @z;
 811
 812refresh();
 813status_cmd();
 814main_loop();