git-add--interactive.perlon commit git-add--interactive: Allow Ctrl-D to exit (c95c024)
   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                my $donesomething = 0;
 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);
 255                                $chosen[$i] = $choose;
 256                                $donesomething++;
 257                        }
 258                }
 259                last if (!$donesomething || $opts->{IMMEDIATE});
 260        }
 261        for ($i = 0; $i < @stuff; $i++) {
 262                if ($chosen[$i]) {
 263                        push @return, $stuff[$i];
 264                }
 265        }
 266        return @return;
 267}
 268
 269sub status_cmd {
 270        list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
 271                        list_modified());
 272        print "\n";
 273}
 274
 275sub say_n_paths {
 276        my $did = shift @_;
 277        my $cnt = scalar @_;
 278        print "$did ";
 279        if (1 < $cnt) {
 280                print "$cnt paths\n";
 281        }
 282        else {
 283                print "one path\n";
 284        }
 285}
 286
 287sub update_cmd {
 288        my @mods = list_modified('file-only');
 289        return if (!@mods);
 290
 291        my @update = list_and_choose({ PROMPT => 'Update',
 292                                       HEADER => $status_head, },
 293                                     @mods);
 294        if (@update) {
 295                system(qw(git update-index --add --remove --),
 296                       map { $_->{VALUE} } @update);
 297                say_n_paths('updated', @update);
 298        }
 299        print "\n";
 300}
 301
 302sub revert_cmd {
 303        my @update = list_and_choose({ PROMPT => 'Revert',
 304                                       HEADER => $status_head, },
 305                                     list_modified());
 306        if (@update) {
 307                my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
 308                                         map { $_->{VALUE} } @update);
 309                my $fh;
 310                open $fh, '| git update-index --index-info'
 311                    or die;
 312                for (@lines) {
 313                        print $fh $_;
 314                }
 315                close($fh);
 316                for (@update) {
 317                        if ($_->{INDEX_ADDDEL} &&
 318                            $_->{INDEX_ADDDEL} eq 'create') {
 319                                system(qw(git update-index --force-remove --),
 320                                       $_->{VALUE});
 321                                print "note: $_->{VALUE} is untracked now.\n";
 322                        }
 323                }
 324                refresh();
 325                say_n_paths('reverted', @update);
 326        }
 327        print "\n";
 328}
 329
 330sub add_untracked_cmd {
 331        my @add = list_and_choose({ PROMPT => 'Add untracked' },
 332                                  list_untracked());
 333        if (@add) {
 334                system(qw(git update-index --add --), @add);
 335                say_n_paths('added', @add);
 336        }
 337        print "\n";
 338}
 339
 340sub parse_diff {
 341        my ($path) = @_;
 342        my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
 343        my (@hunk) = { TEXT => [] };
 344
 345        for (@diff) {
 346                if (/^@@ /) {
 347                        push @hunk, { TEXT => [] };
 348                }
 349                push @{$hunk[-1]{TEXT}}, $_;
 350        }
 351        return @hunk;
 352}
 353
 354sub hunk_splittable {
 355        my ($text) = @_;
 356
 357        my @s = split_hunk($text);
 358        return (1 < @s);
 359}
 360
 361sub parse_hunk_header {
 362        my ($line) = @_;
 363        my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
 364            $line =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
 365        return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
 366}
 367
 368sub split_hunk {
 369        my ($text) = @_;
 370        my @split = ();
 371
 372        # If there are context lines in the middle of a hunk,
 373        # it can be split, but we would need to take care of
 374        # overlaps later.
 375
 376        my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
 377        my $hunk_start = 1;
 378        my $next_hunk_start;
 379
 380      OUTER:
 381        while (1) {
 382                my $next_hunk_start = undef;
 383                my $i = $hunk_start - 1;
 384                my $this = +{
 385                        TEXT => [],
 386                        OLD => $o_ofs,
 387                        NEW => $n_ofs,
 388                        OCNT => 0,
 389                        NCNT => 0,
 390                        ADDDEL => 0,
 391                        POSTCTX => 0,
 392                };
 393
 394                while (++$i < @$text) {
 395                        my $line = $text->[$i];
 396                        if ($line =~ /^ /) {
 397                                if ($this->{ADDDEL} &&
 398                                    !defined $next_hunk_start) {
 399                                        # We have seen leading context and
 400                                        # adds/dels and then here is another
 401                                        # context, which is trailing for this
 402                                        # split hunk and leading for the next
 403                                        # one.
 404                                        $next_hunk_start = $i;
 405                                }
 406                                push @{$this->{TEXT}}, $line;
 407                                $this->{OCNT}++;
 408                                $this->{NCNT}++;
 409                                if (defined $next_hunk_start) {
 410                                        $this->{POSTCTX}++;
 411                                }
 412                                next;
 413                        }
 414
 415                        # add/del
 416                        if (defined $next_hunk_start) {
 417                                # We are done with the current hunk and
 418                                # this is the first real change for the
 419                                # next split one.
 420                                $hunk_start = $next_hunk_start;
 421                                $o_ofs = $this->{OLD} + $this->{OCNT};
 422                                $n_ofs = $this->{NEW} + $this->{NCNT};
 423                                $o_ofs -= $this->{POSTCTX};
 424                                $n_ofs -= $this->{POSTCTX};
 425                                push @split, $this;
 426                                redo OUTER;
 427                        }
 428                        push @{$this->{TEXT}}, $line;
 429                        $this->{ADDDEL}++;
 430                        if ($line =~ /^-/) {
 431                                $this->{OCNT}++;
 432                        }
 433                        else {
 434                                $this->{NCNT}++;
 435                        }
 436                }
 437
 438                push @split, $this;
 439                last;
 440        }
 441
 442        for my $hunk (@split) {
 443                $o_ofs = $hunk->{OLD};
 444                $n_ofs = $hunk->{NEW};
 445                $o_cnt = $hunk->{OCNT};
 446                $n_cnt = $hunk->{NCNT};
 447
 448                my $head = ("@@ -$o_ofs" .
 449                            (($o_cnt != 1) ? ",$o_cnt" : '') .
 450                            " +$n_ofs" .
 451                            (($n_cnt != 1) ? ",$n_cnt" : '') .
 452                            " @@\n");
 453                unshift @{$hunk->{TEXT}}, $head;
 454        }
 455        return map { $_->{TEXT} } @split;
 456}
 457
 458sub find_last_o_ctx {
 459        my ($it) = @_;
 460        my $text = $it->{TEXT};
 461        my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
 462        my $i = @{$text};
 463        my $last_o_ctx = $o_ofs + $o_cnt;
 464        while (0 < --$i) {
 465                my $line = $text->[$i];
 466                if ($line =~ /^ /) {
 467                        $last_o_ctx--;
 468                        next;
 469                }
 470                last;
 471        }
 472        return $last_o_ctx;
 473}
 474
 475sub merge_hunk {
 476        my ($prev, $this) = @_;
 477        my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
 478            parse_hunk_header($prev->{TEXT}[0]);
 479        my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
 480            parse_hunk_header($this->{TEXT}[0]);
 481
 482        my (@line, $i, $ofs, $o_cnt, $n_cnt);
 483        $ofs = $o0_ofs;
 484        $o_cnt = $n_cnt = 0;
 485        for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
 486                my $line = $prev->{TEXT}[$i];
 487                if ($line =~ /^\+/) {
 488                        $n_cnt++;
 489                        push @line, $line;
 490                        next;
 491                }
 492
 493                last if ($o1_ofs <= $ofs);
 494
 495                $o_cnt++;
 496                $ofs++;
 497                if ($line =~ /^ /) {
 498                        $n_cnt++;
 499                }
 500                push @line, $line;
 501        }
 502
 503        for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
 504                my $line = $this->{TEXT}[$i];
 505                if ($line =~ /^\+/) {
 506                        $n_cnt++;
 507                        push @line, $line;
 508                        next;
 509                }
 510                $ofs++;
 511                $o_cnt++;
 512                if ($line =~ /^ /) {
 513                        $n_cnt++;
 514                }
 515                push @line, $line;
 516        }
 517        my $head = ("@@ -$o0_ofs" .
 518                    (($o_cnt != 1) ? ",$o_cnt" : '') .
 519                    " +$n0_ofs" .
 520                    (($n_cnt != 1) ? ",$n_cnt" : '') .
 521                    " @@\n");
 522        @{$prev->{TEXT}} = ($head, @line);
 523}
 524
 525sub coalesce_overlapping_hunks {
 526        my (@in) = @_;
 527        my @out = ();
 528
 529        my ($last_o_ctx);
 530
 531        for (grep { $_->{USE} } @in) {
 532                my $text = $_->{TEXT};
 533                my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
 534                    parse_hunk_header($text->[0]);
 535                if (defined $last_o_ctx &&
 536                    $o_ofs <= $last_o_ctx) {
 537                        merge_hunk($out[-1], $_);
 538                }
 539                else {
 540                        push @out, $_;
 541                }
 542                $last_o_ctx = find_last_o_ctx($out[-1]);
 543        }
 544        return @out;
 545}
 546
 547sub help_patch_cmd {
 548        print <<\EOF ;
 549y - stage this hunk
 550n - do not stage this hunk
 551a - stage this and all the remaining hunks
 552d - do not stage this hunk nor any of the remaining hunks
 553j - leave this hunk undecided, see next undecided hunk
 554J - leave this hunk undecided, see next hunk
 555k - leave this hunk undecided, see previous undecided hunk
 556K - leave this hunk undecided, see previous hunk
 557s - split the current hunk into smaller hunks
 558EOF
 559}
 560
 561sub patch_update_cmd {
 562        my @mods = list_modified('file-only');
 563        @mods = grep { !($_->{BINARY}) } @mods;
 564        return if (!@mods);
 565
 566        my ($it) = list_and_choose({ PROMPT => 'Patch update',
 567                                     SINGLETON => 1,
 568                                     IMMEDIATE => 1,
 569                                     HEADER => $status_head, },
 570                                   @mods);
 571        return if (!$it);
 572
 573        my ($ix, $num);
 574        my $path = $it->{VALUE};
 575        my ($head, @hunk) = parse_diff($path);
 576        for (@{$head->{TEXT}}) {
 577                print;
 578        }
 579        $num = scalar @hunk;
 580        $ix = 0;
 581
 582        while (1) {
 583                my ($prev, $next, $other, $undecided, $i);
 584                $other = '';
 585
 586                if ($num <= $ix) {
 587                        $ix = 0;
 588                }
 589                for ($i = 0; $i < $ix; $i++) {
 590                        if (!defined $hunk[$i]{USE}) {
 591                                $prev = 1;
 592                                $other .= '/k';
 593                                last;
 594                        }
 595                }
 596                if ($ix) {
 597                        $other .= '/K';
 598                }
 599                for ($i = $ix + 1; $i < $num; $i++) {
 600                        if (!defined $hunk[$i]{USE}) {
 601                                $next = 1;
 602                                $other .= '/j';
 603                                last;
 604                        }
 605                }
 606                if ($ix < $num - 1) {
 607                        $other .= '/J';
 608                }
 609                for ($i = 0; $i < $num; $i++) {
 610                        if (!defined $hunk[$i]{USE}) {
 611                                $undecided = 1;
 612                                last;
 613                        }
 614                }
 615                last if (!$undecided);
 616
 617                if (hunk_splittable($hunk[$ix]{TEXT})) {
 618                        $other .= '/s';
 619                }
 620                for (@{$hunk[$ix]{TEXT}}) {
 621                        print;
 622                }
 623                print "Stage this hunk [y/n/a/d$other/?]? ";
 624                my $line = <STDIN>;
 625                if ($line) {
 626                        if ($line =~ /^y/i) {
 627                                $hunk[$ix]{USE} = 1;
 628                        }
 629                        elsif ($line =~ /^n/i) {
 630                                $hunk[$ix]{USE} = 0;
 631                        }
 632                        elsif ($line =~ /^a/i) {
 633                                while ($ix < $num) {
 634                                        if (!defined $hunk[$ix]{USE}) {
 635                                                $hunk[$ix]{USE} = 1;
 636                                        }
 637                                        $ix++;
 638                                }
 639                                next;
 640                        }
 641                        elsif ($line =~ /^d/i) {
 642                                while ($ix < $num) {
 643                                        if (!defined $hunk[$ix]{USE}) {
 644                                                $hunk[$ix]{USE} = 0;
 645                                        }
 646                                        $ix++;
 647                                }
 648                                next;
 649                        }
 650                        elsif ($other =~ /K/ && $line =~ /^K/) {
 651                                $ix--;
 652                                next;
 653                        }
 654                        elsif ($other =~ /J/ && $line =~ /^J/) {
 655                                $ix++;
 656                                next;
 657                        }
 658                        elsif ($other =~ /k/ && $line =~ /^k/) {
 659                                while (1) {
 660                                        $ix--;
 661                                        last if (!$ix ||
 662                                                 !defined $hunk[$ix]{USE});
 663                                }
 664                                next;
 665                        }
 666                        elsif ($other =~ /j/ && $line =~ /^j/) {
 667                                while (1) {
 668                                        $ix++;
 669                                        last if ($ix >= $num ||
 670                                                 !defined $hunk[$ix]{USE});
 671                                }
 672                                next;
 673                        }
 674                        elsif ($other =~ /s/ && $line =~ /^s/) {
 675                                my @split = split_hunk($hunk[$ix]{TEXT});
 676                                if (1 < @split) {
 677                                        print "Split into ",
 678                                        scalar(@split), " hunks.\n";
 679                                }
 680                                splice(@hunk, $ix, 1,
 681                                       map { +{ TEXT => $_, USE => undef } }
 682                                       @split);
 683                                $num = scalar @hunk;
 684                                next;
 685                        }
 686                        else {
 687                                help_patch_cmd($other);
 688                                next;
 689                        }
 690                        # soft increment
 691                        while (1) {
 692                                $ix++;
 693                                last if ($ix >= $num ||
 694                                         !defined $hunk[$ix]{USE});
 695                        }
 696                }
 697        }
 698
 699        @hunk = coalesce_overlapping_hunks(@hunk);
 700
 701        my ($o_lofs, $n_lofs) = (0, 0);
 702        my @result = ();
 703        for (@hunk) {
 704                my $text = $_->{TEXT};
 705                my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
 706                    parse_hunk_header($text->[0]);
 707
 708                if (!$_->{USE}) {
 709                        if (!defined $o_cnt) { $o_cnt = 1; }
 710                        if (!defined $n_cnt) { $n_cnt = 1; }
 711
 712                        # We would have added ($n_cnt - $o_cnt) lines
 713                        # to the postimage if we were to use this hunk,
 714                        # but we didn't.  So the line number that the next
 715                        # hunk starts at would be shifted by that much.
 716                        $n_lofs -= ($n_cnt - $o_cnt);
 717                        next;
 718                }
 719                else {
 720                        if ($n_lofs) {
 721                                $n_ofs += $n_lofs;
 722                                $text->[0] = ("@@ -$o_ofs" .
 723                                              ((defined $o_cnt)
 724                                               ? ",$o_cnt" : '') .
 725                                              " +$n_ofs" .
 726                                              ((defined $n_cnt)
 727                                               ? ",$n_cnt" : '') .
 728                                              " @@\n");
 729                        }
 730                        for (@$text) {
 731                                push @result, $_;
 732                        }
 733                }
 734        }
 735
 736        if (@result) {
 737                my $fh;
 738
 739                open $fh, '| git apply --cached';
 740                for (@{$head->{TEXT}}, @result) {
 741                        print $fh $_;
 742                }
 743                if (!close $fh) {
 744                        for (@{$head->{TEXT}}, @result) {
 745                                print STDERR $_;
 746                        }
 747                }
 748                refresh();
 749        }
 750
 751        print "\n";
 752}
 753
 754sub diff_cmd {
 755        my @mods = list_modified('index-only');
 756        @mods = grep { !($_->{BINARY}) } @mods;
 757        return if (!@mods);
 758        my (@them) = list_and_choose({ PROMPT => 'Review diff',
 759                                     IMMEDIATE => 1,
 760                                     HEADER => $status_head, },
 761                                   @mods);
 762        return if (!@them);
 763        system(qw(git diff-index -p --cached HEAD --),
 764               map { $_->{VALUE} } @them);
 765}
 766
 767sub quit_cmd {
 768        print "Bye.\n";
 769        exit(0);
 770}
 771
 772sub help_cmd {
 773        print <<\EOF ;
 774status        - show paths with changes
 775update        - add working tree state to the staged set of changes
 776revert        - revert staged set of changes back to the HEAD version
 777patch         - pick hunks and update selectively
 778diff          - view diff between HEAD and index
 779add untracked - add contents of untracked files to the staged set of changes
 780EOF
 781}
 782
 783sub main_loop {
 784        my @cmd = ([ 'status', \&status_cmd, ],
 785                   [ 'update', \&update_cmd, ],
 786                   [ 'revert', \&revert_cmd, ],
 787                   [ 'add untracked', \&add_untracked_cmd, ],
 788                   [ 'patch', \&patch_update_cmd, ],
 789                   [ 'diff', \&diff_cmd, ],
 790                   [ 'quit', \&quit_cmd, ],
 791                   [ 'help', \&help_cmd, ],
 792        );
 793        while (1) {
 794                my ($it) = list_and_choose({ PROMPT => 'What now',
 795                                             SINGLETON => 1,
 796                                             LIST_FLAT => 4,
 797                                             HEADER => '*** Commands ***',
 798                                             ON_EOF => \&quit_cmd,
 799                                             IMMEDIATE => 1 }, @cmd);
 800                if ($it) {
 801                        eval {
 802                                $it->[1]->();
 803                        };
 804                        if ($@) {
 805                                print "$@";
 806                        }
 807                }
 808        }
 809}
 810
 811my @z;
 812
 813refresh();
 814status_cmd();
 815main_loop();