Documentation: color.* = true means "auto"
[git/dscho.git] / git-add--interactive.perl
blob335c2c6b56875b97ad6cf8f4406218833afc53ef
1 #!/usr/bin/perl -w
3 use strict;
5 # command line options
6 my $patch_mode;
8 sub run_cmd_pipe {
9 if ($^O eq 'MSWin32') {
10 my @invalid = grep {m/[":*]/} @_;
11 die "$^O does not support: @invalid\n" if @invalid;
12 my @args = map { m/ /o ? "\"$_\"": $_ } @_;
13 return qx{@args};
14 } else {
15 my $fh = undef;
16 open($fh, '-|', @_) or die;
17 return <$fh>;
21 my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir));
23 if (!defined $GIT_DIR) {
24 exit(1); # rev-parse would have already said "not a git repo"
26 chomp($GIT_DIR);
28 sub refresh {
29 my $fh;
30 open $fh, 'git update-index --refresh |'
31 or die;
32 while (<$fh>) {
33 ;# ignore 'needs update'
35 close $fh;
38 sub list_untracked {
39 map {
40 chomp $_;
41 $_;
43 run_cmd_pipe(qw(git ls-files --others --exclude-standard --), @ARGV);
46 my $status_fmt = '%12s %12s %s';
47 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
49 # Returns list of hashes, contents of each of which are:
50 # VALUE: pathname
51 # BINARY: is a binary path
52 # INDEX: is index different from HEAD?
53 # FILE: is file different from index?
54 # INDEX_ADDDEL: is it add/delete between HEAD and index?
55 # FILE_ADDDEL: is it add/delete between index and file?
57 sub list_modified {
58 my ($only) = @_;
59 my (%data, @return);
60 my ($add, $del, $adddel, $file);
61 my @tracked = ();
63 if (@ARGV) {
64 @tracked = map {
65 chomp $_; $_;
66 } run_cmd_pipe(qw(git ls-files --exclude-standard --), @ARGV);
67 return if (!@tracked);
70 for (run_cmd_pipe(qw(git diff-index --cached
71 --numstat --summary HEAD --), @tracked)) {
72 if (($add, $del, $file) =
73 /^([-\d]+) ([-\d]+) (.*)/) {
74 my ($change, $bin);
75 if ($add eq '-' && $del eq '-') {
76 $change = 'binary';
77 $bin = 1;
79 else {
80 $change = "+$add/-$del";
82 $data{$file} = {
83 INDEX => $change,
84 BINARY => $bin,
85 FILE => 'nothing',
88 elsif (($adddel, $file) =
89 /^ (create|delete) mode [0-7]+ (.*)$/) {
90 $data{$file}{INDEX_ADDDEL} = $adddel;
94 for (run_cmd_pipe(qw(git diff-files --numstat --summary --), @tracked)) {
95 if (($add, $del, $file) =
96 /^([-\d]+) ([-\d]+) (.*)/) {
97 if (!exists $data{$file}) {
98 $data{$file} = +{
99 INDEX => 'unchanged',
100 BINARY => 0,
103 my ($change, $bin);
104 if ($add eq '-' && $del eq '-') {
105 $change = 'binary';
106 $bin = 1;
108 else {
109 $change = "+$add/-$del";
111 $data{$file}{FILE} = $change;
112 if ($bin) {
113 $data{$file}{BINARY} = 1;
116 elsif (($adddel, $file) =
117 /^ (create|delete) mode [0-7]+ (.*)$/) {
118 $data{$file}{FILE_ADDDEL} = $adddel;
122 for (sort keys %data) {
123 my $it = $data{$_};
125 if ($only) {
126 if ($only eq 'index-only') {
127 next if ($it->{INDEX} eq 'unchanged');
129 if ($only eq 'file-only') {
130 next if ($it->{FILE} eq 'nothing');
133 push @return, +{
134 VALUE => $_,
135 %$it,
138 return @return;
141 sub find_unique {
142 my ($string, @stuff) = @_;
143 my $found = undef;
144 for (my $i = 0; $i < @stuff; $i++) {
145 my $it = $stuff[$i];
146 my $hit = undef;
147 if (ref $it) {
148 if ((ref $it) eq 'ARRAY') {
149 $it = $it->[0];
151 else {
152 $it = $it->{VALUE};
155 eval {
156 if ($it =~ /^$string/) {
157 $hit = 1;
160 if (defined $hit && defined $found) {
161 return undef;
163 if ($hit) {
164 $found = $i + 1;
167 return $found;
170 # inserts string into trie and updates count for each character
171 sub update_trie {
172 my ($trie, $string) = @_;
173 foreach (split //, $string) {
174 $trie = $trie->{$_} ||= {COUNT => 0};
175 $trie->{COUNT}++;
179 # returns an array of tuples (prefix, remainder)
180 sub find_unique_prefixes {
181 my @stuff = @_;
182 my @return = ();
184 # any single prefix exceeding the soft limit is omitted
185 # if any prefix exceeds the hard limit all are omitted
186 # 0 indicates no limit
187 my $soft_limit = 0;
188 my $hard_limit = 3;
190 # build a trie modelling all possible options
191 my %trie;
192 foreach my $print (@stuff) {
193 if ((ref $print) eq 'ARRAY') {
194 $print = $print->[0];
196 elsif ((ref $print) eq 'HASH') {
197 $print = $print->{VALUE};
199 update_trie(\%trie, $print);
200 push @return, $print;
203 # use the trie to find the unique prefixes
204 for (my $i = 0; $i < @return; $i++) {
205 my $ret = $return[$i];
206 my @letters = split //, $ret;
207 my %search = %trie;
208 my ($prefix, $remainder);
209 my $j;
210 for ($j = 0; $j < @letters; $j++) {
211 my $letter = $letters[$j];
212 if ($search{$letter}{COUNT} == 1) {
213 $prefix = substr $ret, 0, $j + 1;
214 $remainder = substr $ret, $j + 1;
215 last;
217 else {
218 my $prefix = substr $ret, 0, $j;
219 return ()
220 if ($hard_limit && $j + 1 > $hard_limit);
222 %search = %{$search{$letter}};
224 if ($soft_limit && $j + 1 > $soft_limit) {
225 $prefix = undef;
226 $remainder = $ret;
228 $return[$i] = [$prefix, $remainder];
230 return @return;
233 # filters out prefixes which have special meaning to list_and_choose()
234 sub is_valid_prefix {
235 my $prefix = shift;
236 return (defined $prefix) &&
237 !($prefix =~ /[\s,]/) && # separators
238 !($prefix =~ /^-/) && # deselection
239 !($prefix =~ /^\d+/) && # selection
240 ($prefix ne '*') && # "all" wildcard
241 ($prefix ne '?'); # prompt help
244 # given a prefix/remainder tuple return a string with the prefix highlighted
245 # for now use square brackets; later might use ANSI colors (underline, bold)
246 sub highlight_prefix {
247 my $prefix = shift;
248 my $remainder = shift;
249 return $remainder unless defined $prefix;
250 return is_valid_prefix($prefix) ?
251 "[$prefix]$remainder" :
252 "$prefix$remainder";
255 sub list_and_choose {
256 my ($opts, @stuff) = @_;
257 my (@chosen, @return);
258 my $i;
259 my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY};
261 TOPLOOP:
262 while (1) {
263 my $last_lf = 0;
265 if ($opts->{HEADER}) {
266 if (!$opts->{LIST_FLAT}) {
267 print " ";
269 print "$opts->{HEADER}\n";
271 for ($i = 0; $i < @stuff; $i++) {
272 my $chosen = $chosen[$i] ? '*' : ' ';
273 my $print = $stuff[$i];
274 my $ref = ref $print;
275 my $highlighted = highlight_prefix(@{$prefixes[$i]})
276 if @prefixes;
277 if ($ref eq 'ARRAY') {
278 $print = $highlighted || $print->[0];
280 elsif ($ref eq 'HASH') {
281 my $value = $highlighted || $print->{VALUE};
282 $print = sprintf($status_fmt,
283 $print->{INDEX},
284 $print->{FILE},
285 $value);
287 else {
288 $print = $highlighted || $print;
290 printf("%s%2d: %s", $chosen, $i+1, $print);
291 if (($opts->{LIST_FLAT}) &&
292 (($i + 1) % ($opts->{LIST_FLAT}))) {
293 print "\t";
294 $last_lf = 0;
296 else {
297 print "\n";
298 $last_lf = 1;
301 if (!$last_lf) {
302 print "\n";
305 return if ($opts->{LIST_ONLY});
307 print $opts->{PROMPT};
308 if ($opts->{SINGLETON}) {
309 print "> ";
311 else {
312 print ">> ";
314 my $line = <STDIN>;
315 if (!$line) {
316 print "\n";
317 $opts->{ON_EOF}->() if $opts->{ON_EOF};
318 last;
320 chomp $line;
321 last if $line eq '';
322 if ($line eq '?') {
323 $opts->{SINGLETON} ?
324 singleton_prompt_help_cmd() :
325 prompt_help_cmd();
326 next TOPLOOP;
328 for my $choice (split(/[\s,]+/, $line)) {
329 my $choose = 1;
330 my ($bottom, $top);
332 # Input that begins with '-'; unchoose
333 if ($choice =~ s/^-//) {
334 $choose = 0;
336 # A range can be specified like 5-7
337 if ($choice =~ /^(\d+)-(\d+)$/) {
338 ($bottom, $top) = ($1, $2);
340 elsif ($choice =~ /^\d+$/) {
341 $bottom = $top = $choice;
343 elsif ($choice eq '*') {
344 $bottom = 1;
345 $top = 1 + @stuff;
347 else {
348 $bottom = $top = find_unique($choice, @stuff);
349 if (!defined $bottom) {
350 print "Huh ($choice)?\n";
351 next TOPLOOP;
354 if ($opts->{SINGLETON} && $bottom != $top) {
355 print "Huh ($choice)?\n";
356 next TOPLOOP;
358 for ($i = $bottom-1; $i <= $top-1; $i++) {
359 next if (@stuff <= $i || $i < 0);
360 $chosen[$i] = $choose;
363 last if ($opts->{IMMEDIATE} || $line eq '*');
365 for ($i = 0; $i < @stuff; $i++) {
366 if ($chosen[$i]) {
367 push @return, $stuff[$i];
370 return @return;
373 sub singleton_prompt_help_cmd {
374 print <<\EOF ;
375 Prompt help:
376 1 - select a numbered item
377 foo - select item based on unique prefix
378 - (empty) select nothing
382 sub prompt_help_cmd {
383 print <<\EOF ;
384 Prompt help:
385 1 - select a single item
386 3-5 - select a range of items
387 2-3,6-9 - select multiple ranges
388 foo - select item based on unique prefix
389 -... - unselect specified items
390 * - choose all items
391 - (empty) finish selecting
395 sub status_cmd {
396 list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
397 list_modified());
398 print "\n";
401 sub say_n_paths {
402 my $did = shift @_;
403 my $cnt = scalar @_;
404 print "$did ";
405 if (1 < $cnt) {
406 print "$cnt paths\n";
408 else {
409 print "one path\n";
413 sub update_cmd {
414 my @mods = list_modified('file-only');
415 return if (!@mods);
417 my @update = list_and_choose({ PROMPT => 'Update',
418 HEADER => $status_head, },
419 @mods);
420 if (@update) {
421 system(qw(git update-index --add --remove --),
422 map { $_->{VALUE} } @update);
423 say_n_paths('updated', @update);
425 print "\n";
428 sub revert_cmd {
429 my @update = list_and_choose({ PROMPT => 'Revert',
430 HEADER => $status_head, },
431 list_modified());
432 if (@update) {
433 my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
434 map { $_->{VALUE} } @update);
435 my $fh;
436 open $fh, '| git update-index --index-info'
437 or die;
438 for (@lines) {
439 print $fh $_;
441 close($fh);
442 for (@update) {
443 if ($_->{INDEX_ADDDEL} &&
444 $_->{INDEX_ADDDEL} eq 'create') {
445 system(qw(git update-index --force-remove --),
446 $_->{VALUE});
447 print "note: $_->{VALUE} is untracked now.\n";
450 refresh();
451 say_n_paths('reverted', @update);
453 print "\n";
456 sub add_untracked_cmd {
457 my @add = list_and_choose({ PROMPT => 'Add untracked' },
458 list_untracked());
459 if (@add) {
460 system(qw(git update-index --add --), @add);
461 say_n_paths('added', @add);
463 print "\n";
466 sub parse_diff {
467 my ($path) = @_;
468 my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
469 my (@hunk) = { TEXT => [] };
471 for (@diff) {
472 if (/^@@ /) {
473 push @hunk, { TEXT => [] };
475 push @{$hunk[-1]{TEXT}}, $_;
477 return @hunk;
480 sub hunk_splittable {
481 my ($text) = @_;
483 my @s = split_hunk($text);
484 return (1 < @s);
487 sub parse_hunk_header {
488 my ($line) = @_;
489 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
490 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
491 $o_cnt = 1 unless defined $o_cnt;
492 $n_cnt = 1 unless defined $n_cnt;
493 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
496 sub split_hunk {
497 my ($text) = @_;
498 my @split = ();
500 # If there are context lines in the middle of a hunk,
501 # it can be split, but we would need to take care of
502 # overlaps later.
504 my ($o_ofs, undef, $n_ofs) = parse_hunk_header($text->[0]);
505 my $hunk_start = 1;
507 OUTER:
508 while (1) {
509 my $next_hunk_start = undef;
510 my $i = $hunk_start - 1;
511 my $this = +{
512 TEXT => [],
513 OLD => $o_ofs,
514 NEW => $n_ofs,
515 OCNT => 0,
516 NCNT => 0,
517 ADDDEL => 0,
518 POSTCTX => 0,
521 while (++$i < @$text) {
522 my $line = $text->[$i];
523 if ($line =~ /^ /) {
524 if ($this->{ADDDEL} &&
525 !defined $next_hunk_start) {
526 # We have seen leading context and
527 # adds/dels and then here is another
528 # context, which is trailing for this
529 # split hunk and leading for the next
530 # one.
531 $next_hunk_start = $i;
533 push @{$this->{TEXT}}, $line;
534 $this->{OCNT}++;
535 $this->{NCNT}++;
536 if (defined $next_hunk_start) {
537 $this->{POSTCTX}++;
539 next;
542 # add/del
543 if (defined $next_hunk_start) {
544 # We are done with the current hunk and
545 # this is the first real change for the
546 # next split one.
547 $hunk_start = $next_hunk_start;
548 $o_ofs = $this->{OLD} + $this->{OCNT};
549 $n_ofs = $this->{NEW} + $this->{NCNT};
550 $o_ofs -= $this->{POSTCTX};
551 $n_ofs -= $this->{POSTCTX};
552 push @split, $this;
553 redo OUTER;
555 push @{$this->{TEXT}}, $line;
556 $this->{ADDDEL}++;
557 if ($line =~ /^-/) {
558 $this->{OCNT}++;
560 else {
561 $this->{NCNT}++;
565 push @split, $this;
566 last;
569 for my $hunk (@split) {
570 $o_ofs = $hunk->{OLD};
571 $n_ofs = $hunk->{NEW};
572 my $o_cnt = $hunk->{OCNT};
573 my $n_cnt = $hunk->{NCNT};
575 my $head = ("@@ -$o_ofs" .
576 (($o_cnt != 1) ? ",$o_cnt" : '') .
577 " +$n_ofs" .
578 (($n_cnt != 1) ? ",$n_cnt" : '') .
579 " @@\n");
580 unshift @{$hunk->{TEXT}}, $head;
582 return map { $_->{TEXT} } @split;
585 sub find_last_o_ctx {
586 my ($it) = @_;
587 my $text = $it->{TEXT};
588 my ($o_ofs, $o_cnt) = parse_hunk_header($text->[0]);
589 my $i = @{$text};
590 my $last_o_ctx = $o_ofs + $o_cnt;
591 while (0 < --$i) {
592 my $line = $text->[$i];
593 if ($line =~ /^ /) {
594 $last_o_ctx--;
595 next;
597 last;
599 return $last_o_ctx;
602 sub merge_hunk {
603 my ($prev, $this) = @_;
604 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
605 parse_hunk_header($prev->{TEXT}[0]);
606 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
607 parse_hunk_header($this->{TEXT}[0]);
609 my (@line, $i, $ofs, $o_cnt, $n_cnt);
610 $ofs = $o0_ofs;
611 $o_cnt = $n_cnt = 0;
612 for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
613 my $line = $prev->{TEXT}[$i];
614 if ($line =~ /^\+/) {
615 $n_cnt++;
616 push @line, $line;
617 next;
620 last if ($o1_ofs <= $ofs);
622 $o_cnt++;
623 $ofs++;
624 if ($line =~ /^ /) {
625 $n_cnt++;
627 push @line, $line;
630 for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
631 my $line = $this->{TEXT}[$i];
632 if ($line =~ /^\+/) {
633 $n_cnt++;
634 push @line, $line;
635 next;
637 $ofs++;
638 $o_cnt++;
639 if ($line =~ /^ /) {
640 $n_cnt++;
642 push @line, $line;
644 my $head = ("@@ -$o0_ofs" .
645 (($o_cnt != 1) ? ",$o_cnt" : '') .
646 " +$n0_ofs" .
647 (($n_cnt != 1) ? ",$n_cnt" : '') .
648 " @@\n");
649 @{$prev->{TEXT}} = ($head, @line);
652 sub coalesce_overlapping_hunks {
653 my (@in) = @_;
654 my @out = ();
656 my ($last_o_ctx);
658 for (grep { $_->{USE} } @in) {
659 my $text = $_->{TEXT};
660 my ($o_ofs) = parse_hunk_header($text->[0]);
661 if (defined $last_o_ctx &&
662 $o_ofs <= $last_o_ctx) {
663 merge_hunk($out[-1], $_);
665 else {
666 push @out, $_;
668 $last_o_ctx = find_last_o_ctx($out[-1]);
670 return @out;
673 sub help_patch_cmd {
674 print <<\EOF ;
675 y - stage this hunk
676 n - do not stage this hunk
677 a - stage this and all the remaining hunks in the file
678 d - do not stage this hunk nor any of the remaining hunks in the file
679 j - leave this hunk undecided, see next undecided hunk
680 J - leave this hunk undecided, see next hunk
681 k - leave this hunk undecided, see previous undecided hunk
682 K - leave this hunk undecided, see previous hunk
683 s - split the current hunk into smaller hunks
684 ? - print help
688 sub patch_update_cmd {
689 my @mods = grep { !($_->{BINARY}) } list_modified('file-only');
690 my @them;
692 if (!@mods) {
693 print STDERR "No changes.\n";
694 return 0;
696 if ($patch_mode) {
697 @them = @mods;
699 else {
700 @them = list_and_choose({ PROMPT => 'Patch update',
701 HEADER => $status_head, },
702 @mods);
704 for (@them) {
705 patch_update_file($_->{VALUE});
709 sub patch_update_file {
710 my ($ix, $num);
711 my $path = shift;
712 my ($head, @hunk) = parse_diff($path);
713 for (@{$head->{TEXT}}) {
714 print;
716 $num = scalar @hunk;
717 $ix = 0;
719 while (1) {
720 my ($prev, $next, $other, $undecided, $i);
721 $other = '';
723 if ($num <= $ix) {
724 $ix = 0;
726 for ($i = 0; $i < $ix; $i++) {
727 if (!defined $hunk[$i]{USE}) {
728 $prev = 1;
729 $other .= '/k';
730 last;
733 if ($ix) {
734 $other .= '/K';
736 for ($i = $ix + 1; $i < $num; $i++) {
737 if (!defined $hunk[$i]{USE}) {
738 $next = 1;
739 $other .= '/j';
740 last;
743 if ($ix < $num - 1) {
744 $other .= '/J';
746 for ($i = 0; $i < $num; $i++) {
747 if (!defined $hunk[$i]{USE}) {
748 $undecided = 1;
749 last;
752 last if (!$undecided);
754 if (hunk_splittable($hunk[$ix]{TEXT})) {
755 $other .= '/s';
757 for (@{$hunk[$ix]{TEXT}}) {
758 print;
760 print "Stage this hunk [y/n/a/d$other/?]? ";
761 my $line = <STDIN>;
762 if ($line) {
763 if ($line =~ /^y/i) {
764 $hunk[$ix]{USE} = 1;
766 elsif ($line =~ /^n/i) {
767 $hunk[$ix]{USE} = 0;
769 elsif ($line =~ /^a/i) {
770 while ($ix < $num) {
771 if (!defined $hunk[$ix]{USE}) {
772 $hunk[$ix]{USE} = 1;
774 $ix++;
776 next;
778 elsif ($line =~ /^d/i) {
779 while ($ix < $num) {
780 if (!defined $hunk[$ix]{USE}) {
781 $hunk[$ix]{USE} = 0;
783 $ix++;
785 next;
787 elsif ($other =~ /K/ && $line =~ /^K/) {
788 $ix--;
789 next;
791 elsif ($other =~ /J/ && $line =~ /^J/) {
792 $ix++;
793 next;
795 elsif ($other =~ /k/ && $line =~ /^k/) {
796 while (1) {
797 $ix--;
798 last if (!$ix ||
799 !defined $hunk[$ix]{USE});
801 next;
803 elsif ($other =~ /j/ && $line =~ /^j/) {
804 while (1) {
805 $ix++;
806 last if ($ix >= $num ||
807 !defined $hunk[$ix]{USE});
809 next;
811 elsif ($other =~ /s/ && $line =~ /^s/) {
812 my @split = split_hunk($hunk[$ix]{TEXT});
813 if (1 < @split) {
814 print "Split into ",
815 scalar(@split), " hunks.\n";
817 splice(@hunk, $ix, 1,
818 map { +{ TEXT => $_, USE => undef } }
819 @split);
820 $num = scalar @hunk;
821 next;
823 else {
824 help_patch_cmd($other);
825 next;
827 # soft increment
828 while (1) {
829 $ix++;
830 last if ($ix >= $num ||
831 !defined $hunk[$ix]{USE});
836 @hunk = coalesce_overlapping_hunks(@hunk);
838 my $n_lofs = 0;
839 my @result = ();
840 for (@hunk) {
841 my $text = $_->{TEXT};
842 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
843 parse_hunk_header($text->[0]);
845 if (!$_->{USE}) {
846 # We would have added ($n_cnt - $o_cnt) lines
847 # to the postimage if we were to use this hunk,
848 # but we didn't. So the line number that the next
849 # hunk starts at would be shifted by that much.
850 $n_lofs -= ($n_cnt - $o_cnt);
851 next;
853 else {
854 if ($n_lofs) {
855 $n_ofs += $n_lofs;
856 $text->[0] = ("@@ -$o_ofs" .
857 (($o_cnt != 1)
858 ? ",$o_cnt" : '') .
859 " +$n_ofs" .
860 (($n_cnt != 1)
861 ? ",$n_cnt" : '') .
862 " @@\n");
864 for (@$text) {
865 push @result, $_;
870 if (@result) {
871 my $fh;
873 open $fh, '| git apply --cached';
874 for (@{$head->{TEXT}}, @result) {
875 print $fh $_;
877 if (!close $fh) {
878 for (@{$head->{TEXT}}, @result) {
879 print STDERR $_;
882 refresh();
885 print "\n";
888 sub diff_cmd {
889 my @mods = list_modified('index-only');
890 @mods = grep { !($_->{BINARY}) } @mods;
891 return if (!@mods);
892 my (@them) = list_and_choose({ PROMPT => 'Review diff',
893 IMMEDIATE => 1,
894 HEADER => $status_head, },
895 @mods);
896 return if (!@them);
897 system(qw(git diff-index -p --cached HEAD --),
898 map { $_->{VALUE} } @them);
901 sub quit_cmd {
902 print "Bye.\n";
903 exit(0);
906 sub help_cmd {
907 print <<\EOF ;
908 status - show paths with changes
909 update - add working tree state to the staged set of changes
910 revert - revert staged set of changes back to the HEAD version
911 patch - pick hunks and update selectively
912 diff - view diff between HEAD and index
913 add untracked - add contents of untracked files to the staged set of changes
917 sub process_args {
918 return unless @ARGV;
919 my $arg = shift @ARGV;
920 if ($arg eq "--patch") {
921 $patch_mode = 1;
922 $arg = shift @ARGV or die "missing --";
923 die "invalid argument $arg, expecting --"
924 unless $arg eq "--";
926 elsif ($arg ne "--") {
927 die "invalid argument $arg, expecting --";
931 sub main_loop {
932 my @cmd = ([ 'status', \&status_cmd, ],
933 [ 'update', \&update_cmd, ],
934 [ 'revert', \&revert_cmd, ],
935 [ 'add untracked', \&add_untracked_cmd, ],
936 [ 'patch', \&patch_update_cmd, ],
937 [ 'diff', \&diff_cmd, ],
938 [ 'quit', \&quit_cmd, ],
939 [ 'help', \&help_cmd, ],
941 while (1) {
942 my ($it) = list_and_choose({ PROMPT => 'What now',
943 SINGLETON => 1,
944 LIST_FLAT => 4,
945 HEADER => '*** Commands ***',
946 ON_EOF => \&quit_cmd,
947 IMMEDIATE => 1 }, @cmd);
948 if ($it) {
949 eval {
950 $it->[1]->();
952 if ($@) {
953 print "$@";
959 process_args();
960 refresh();
961 if ($patch_mode) {
962 patch_update_cmd();
964 else {
965 status_cmd();
966 main_loop();