Merge branch 'jc/push-cert' into pu
[git/jrn.git] / git-add--interactive.perl
blobac5763dafbcb8c44f538f617e859c758a78f9238
1 #!/usr/bin/perl
3 use 5.008;
4 use strict;
5 use warnings;
6 use Git;
8 binmode(STDOUT, ":raw");
10 my $repo = Git->repository();
12 my $menu_use_color = $repo->get_colorbool('color.interactive');
13 my ($prompt_color, $header_color, $help_color) =
14 $menu_use_color ? (
15 $repo->get_color('color.interactive.prompt', 'bold blue'),
16 $repo->get_color('color.interactive.header', 'bold'),
17 $repo->get_color('color.interactive.help', 'red bold'),
18 ) : ();
19 my $error_color = ();
20 if ($menu_use_color) {
21 my $help_color_spec = ($repo->config('color.interactive.help') or
22 'red bold');
23 $error_color = $repo->get_color('color.interactive.error',
24 $help_color_spec);
27 my $diff_use_color = $repo->get_colorbool('color.diff');
28 my ($fraginfo_color) =
29 $diff_use_color ? (
30 $repo->get_color('color.diff.frag', 'cyan'),
31 ) : ();
32 my ($funcname_color) =
33 $diff_use_color ? (
34 $repo->get_color('color.diff.func', ''),
35 ) : ();
36 my ($diff_plain_color) =
37 $diff_use_color ? (
38 $repo->get_color('color.diff.plain', ''),
39 ) : ();
40 my ($diff_old_color) =
41 $diff_use_color ? (
42 $repo->get_color('color.diff.old', 'red'),
43 ) : ();
44 my ($diff_new_color) =
45 $diff_use_color ? (
46 $repo->get_color('color.diff.new', 'green'),
47 ) : ();
49 my $normal_color = $repo->get_color("", "reset");
51 my $diff_algorithm = $repo->config('diff.algorithm');
53 my $use_readkey = 0;
54 my $use_termcap = 0;
55 my %term_escapes;
57 sub ReadMode;
58 sub ReadKey;
59 if ($repo->config_bool("interactive.singlekey")) {
60 eval {
61 require Term::ReadKey;
62 Term::ReadKey->import;
63 $use_readkey = 1;
65 if (!$use_readkey) {
66 print STDERR "missing Term::ReadKey, disabling interactive.singlekey\n";
68 eval {
69 require Term::Cap;
70 my $termcap = Term::Cap->Tgetent;
71 foreach (values %$termcap) {
72 $term_escapes{$_} = 1 if /^\e/;
74 $use_termcap = 1;
78 sub colored {
79 my $color = shift;
80 my $string = join("", @_);
82 if (defined $color) {
83 # Put a color code at the beginning of each line, a reset at the end
84 # color after newlines that are not at the end of the string
85 $string =~ s/(\n+)(.)/$1$color$2/g;
86 # reset before newlines
87 $string =~ s/(\n+)/$normal_color$1/g;
88 # codes at beginning and end (if necessary):
89 $string =~ s/^/$color/;
90 $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
92 return $string;
95 # command line options
96 my $patch_mode;
97 my $patch_mode_revision;
99 sub apply_patch;
100 sub apply_patch_for_checkout_commit;
101 sub apply_patch_for_stash;
103 my %patch_modes = (
104 'stage' => {
105 DIFF => 'diff-files -p',
106 APPLY => sub { apply_patch 'apply --cached', @_; },
107 APPLY_CHECK => 'apply --cached',
108 VERB => 'Stage',
109 TARGET => '',
110 PARTICIPLE => 'staging',
111 FILTER => 'file-only',
112 IS_REVERSE => 0,
114 'stash' => {
115 DIFF => 'diff-index -p HEAD',
116 APPLY => sub { apply_patch 'apply --cached', @_; },
117 APPLY_CHECK => 'apply --cached',
118 VERB => 'Stash',
119 TARGET => '',
120 PARTICIPLE => 'stashing',
121 FILTER => undef,
122 IS_REVERSE => 0,
124 'reset_head' => {
125 DIFF => 'diff-index -p --cached',
126 APPLY => sub { apply_patch 'apply -R --cached', @_; },
127 APPLY_CHECK => 'apply -R --cached',
128 VERB => 'Unstage',
129 TARGET => '',
130 PARTICIPLE => 'unstaging',
131 FILTER => 'index-only',
132 IS_REVERSE => 1,
134 'reset_nothead' => {
135 DIFF => 'diff-index -R -p --cached',
136 APPLY => sub { apply_patch 'apply --cached', @_; },
137 APPLY_CHECK => 'apply --cached',
138 VERB => 'Apply',
139 TARGET => ' to index',
140 PARTICIPLE => 'applying',
141 FILTER => 'index-only',
142 IS_REVERSE => 0,
144 'checkout_index' => {
145 DIFF => 'diff-files -p',
146 APPLY => sub { apply_patch 'apply -R', @_; },
147 APPLY_CHECK => 'apply -R',
148 VERB => 'Discard',
149 TARGET => ' from worktree',
150 PARTICIPLE => 'discarding',
151 FILTER => 'file-only',
152 IS_REVERSE => 1,
154 'checkout_head' => {
155 DIFF => 'diff-index -p',
156 APPLY => sub { apply_patch_for_checkout_commit '-R', @_ },
157 APPLY_CHECK => 'apply -R',
158 VERB => 'Discard',
159 TARGET => ' from index and worktree',
160 PARTICIPLE => 'discarding',
161 FILTER => undef,
162 IS_REVERSE => 1,
164 'checkout_nothead' => {
165 DIFF => 'diff-index -R -p',
166 APPLY => sub { apply_patch_for_checkout_commit '', @_ },
167 APPLY_CHECK => 'apply',
168 VERB => 'Apply',
169 TARGET => ' to index and worktree',
170 PARTICIPLE => 'applying',
171 FILTER => undef,
172 IS_REVERSE => 0,
176 my %patch_mode_flavour = %{$patch_modes{stage}};
178 sub run_cmd_pipe {
179 if ($^O eq 'MSWin32') {
180 my @invalid = grep {m/[":*]/} @_;
181 die "$^O does not support: @invalid\n" if @invalid;
182 my @args = map { m/ /o ? "\"$_\"": $_ } @_;
183 return qx{@args};
184 } else {
185 my $fh = undef;
186 open($fh, '-|', @_) or die;
187 return <$fh>;
191 my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir));
193 if (!defined $GIT_DIR) {
194 exit(1); # rev-parse would have already said "not a git repo"
196 chomp($GIT_DIR);
198 my %cquote_map = (
199 "b" => chr(8),
200 "t" => chr(9),
201 "n" => chr(10),
202 "v" => chr(11),
203 "f" => chr(12),
204 "r" => chr(13),
205 "\\" => "\\",
206 "\042" => "\042",
209 sub unquote_path {
210 local ($_) = @_;
211 my ($retval, $remainder);
212 if (!/^\042(.*)\042$/) {
213 return $_;
215 ($_, $retval) = ($1, "");
216 while (/^([^\\]*)\\(.*)$/) {
217 $remainder = $2;
218 $retval .= $1;
219 for ($remainder) {
220 if (/^([0-3][0-7][0-7])(.*)$/) {
221 $retval .= chr(oct($1));
222 $_ = $2;
223 last;
225 if (/^([\\\042btnvfr])(.*)$/) {
226 $retval .= $cquote_map{$1};
227 $_ = $2;
228 last;
230 # This is malformed -- just return it as-is for now.
231 return $_[0];
233 $_ = $remainder;
235 $retval .= $_;
236 return $retval;
239 sub refresh {
240 my $fh;
241 open $fh, 'git update-index --refresh |'
242 or die;
243 while (<$fh>) {
244 ;# ignore 'needs update'
246 close $fh;
249 sub list_untracked {
250 map {
251 chomp $_;
252 unquote_path($_);
254 run_cmd_pipe(qw(git ls-files --others --exclude-standard --), @ARGV);
257 my $status_fmt = '%12s %12s %s';
258 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
261 my $initial;
262 sub is_initial_commit {
263 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
264 unless defined $initial;
265 return $initial;
269 sub get_empty_tree {
270 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
273 sub get_diff_reference {
274 my $ref = shift;
275 if (defined $ref and $ref ne 'HEAD') {
276 return $ref;
277 } elsif (is_initial_commit()) {
278 return get_empty_tree();
279 } else {
280 return 'HEAD';
284 # Returns list of hashes, contents of each of which are:
285 # VALUE: pathname
286 # BINARY: is a binary path
287 # INDEX: is index different from HEAD?
288 # FILE: is file different from index?
289 # INDEX_ADDDEL: is it add/delete between HEAD and index?
290 # FILE_ADDDEL: is it add/delete between index and file?
291 # UNMERGED: is the path unmerged
293 sub list_modified {
294 my ($only) = @_;
295 my (%data, @return);
296 my ($add, $del, $adddel, $file);
297 my @tracked = ();
299 if (@ARGV) {
300 @tracked = map {
301 chomp $_;
302 unquote_path($_);
303 } run_cmd_pipe(qw(git ls-files --), @ARGV);
304 return if (!@tracked);
307 my $reference = get_diff_reference($patch_mode_revision);
308 for (run_cmd_pipe(qw(git diff-index --cached
309 --numstat --summary), $reference,
310 '--', @tracked)) {
311 if (($add, $del, $file) =
312 /^([-\d]+) ([-\d]+) (.*)/) {
313 my ($change, $bin);
314 $file = unquote_path($file);
315 if ($add eq '-' && $del eq '-') {
316 $change = 'binary';
317 $bin = 1;
319 else {
320 $change = "+$add/-$del";
322 $data{$file} = {
323 INDEX => $change,
324 BINARY => $bin,
325 FILE => 'nothing',
328 elsif (($adddel, $file) =
329 /^ (create|delete) mode [0-7]+ (.*)$/) {
330 $file = unquote_path($file);
331 $data{$file}{INDEX_ADDDEL} = $adddel;
335 for (run_cmd_pipe(qw(git diff-files --numstat --summary --raw --), @tracked)) {
336 if (($add, $del, $file) =
337 /^([-\d]+) ([-\d]+) (.*)/) {
338 $file = unquote_path($file);
339 my ($change, $bin);
340 if ($add eq '-' && $del eq '-') {
341 $change = 'binary';
342 $bin = 1;
344 else {
345 $change = "+$add/-$del";
347 $data{$file}{FILE} = $change;
348 if ($bin) {
349 $data{$file}{BINARY} = 1;
352 elsif (($adddel, $file) =
353 /^ (create|delete) mode [0-7]+ (.*)$/) {
354 $file = unquote_path($file);
355 $data{$file}{FILE_ADDDEL} = $adddel;
357 elsif (/^:[0-7]+ [0-7]+ [0-9a-f]+ [0-9a-f]+ (.) (.*)$/) {
358 $file = unquote_path($2);
359 if (!exists $data{$file}) {
360 $data{$file} = +{
361 INDEX => 'unchanged',
362 BINARY => 0,
365 if ($1 eq 'U') {
366 $data{$file}{UNMERGED} = 1;
371 for (sort keys %data) {
372 my $it = $data{$_};
374 if ($only) {
375 if ($only eq 'index-only') {
376 next if ($it->{INDEX} eq 'unchanged');
378 if ($only eq 'file-only') {
379 next if ($it->{FILE} eq 'nothing');
382 push @return, +{
383 VALUE => $_,
384 %$it,
387 return @return;
390 sub find_unique {
391 my ($string, @stuff) = @_;
392 my $found = undef;
393 for (my $i = 0; $i < @stuff; $i++) {
394 my $it = $stuff[$i];
395 my $hit = undef;
396 if (ref $it) {
397 if ((ref $it) eq 'ARRAY') {
398 $it = $it->[0];
400 else {
401 $it = $it->{VALUE};
404 eval {
405 if ($it =~ /^$string/) {
406 $hit = 1;
409 if (defined $hit && defined $found) {
410 return undef;
412 if ($hit) {
413 $found = $i + 1;
416 return $found;
419 # inserts string into trie and updates count for each character
420 sub update_trie {
421 my ($trie, $string) = @_;
422 foreach (split //, $string) {
423 $trie = $trie->{$_} ||= {COUNT => 0};
424 $trie->{COUNT}++;
428 # returns an array of tuples (prefix, remainder)
429 sub find_unique_prefixes {
430 my @stuff = @_;
431 my @return = ();
433 # any single prefix exceeding the soft limit is omitted
434 # if any prefix exceeds the hard limit all are omitted
435 # 0 indicates no limit
436 my $soft_limit = 0;
437 my $hard_limit = 3;
439 # build a trie modelling all possible options
440 my %trie;
441 foreach my $print (@stuff) {
442 if ((ref $print) eq 'ARRAY') {
443 $print = $print->[0];
445 elsif ((ref $print) eq 'HASH') {
446 $print = $print->{VALUE};
448 update_trie(\%trie, $print);
449 push @return, $print;
452 # use the trie to find the unique prefixes
453 for (my $i = 0; $i < @return; $i++) {
454 my $ret = $return[$i];
455 my @letters = split //, $ret;
456 my %search = %trie;
457 my ($prefix, $remainder);
458 my $j;
459 for ($j = 0; $j < @letters; $j++) {
460 my $letter = $letters[$j];
461 if ($search{$letter}{COUNT} == 1) {
462 $prefix = substr $ret, 0, $j + 1;
463 $remainder = substr $ret, $j + 1;
464 last;
466 else {
467 my $prefix = substr $ret, 0, $j;
468 return ()
469 if ($hard_limit && $j + 1 > $hard_limit);
471 %search = %{$search{$letter}};
473 if (ord($letters[0]) > 127 ||
474 ($soft_limit && $j + 1 > $soft_limit)) {
475 $prefix = undef;
476 $remainder = $ret;
478 $return[$i] = [$prefix, $remainder];
480 return @return;
483 # filters out prefixes which have special meaning to list_and_choose()
484 sub is_valid_prefix {
485 my $prefix = shift;
486 return (defined $prefix) &&
487 !($prefix =~ /[\s,]/) && # separators
488 !($prefix =~ /^-/) && # deselection
489 !($prefix =~ /^\d+/) && # selection
490 ($prefix ne '*') && # "all" wildcard
491 ($prefix ne '?'); # prompt help
494 # given a prefix/remainder tuple return a string with the prefix highlighted
495 # for now use square brackets; later might use ANSI colors (underline, bold)
496 sub highlight_prefix {
497 my $prefix = shift;
498 my $remainder = shift;
500 if (!defined $prefix) {
501 return $remainder;
504 if (!is_valid_prefix($prefix)) {
505 return "$prefix$remainder";
508 if (!$menu_use_color) {
509 return "[$prefix]$remainder";
512 return "$prompt_color$prefix$normal_color$remainder";
515 sub error_msg {
516 print STDERR colored $error_color, @_;
519 sub list_and_choose {
520 my ($opts, @stuff) = @_;
521 my (@chosen, @return);
522 my $i;
523 my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY};
525 TOPLOOP:
526 while (1) {
527 my $last_lf = 0;
529 if ($opts->{HEADER}) {
530 if (!$opts->{LIST_FLAT}) {
531 print " ";
533 print colored $header_color, "$opts->{HEADER}\n";
535 for ($i = 0; $i < @stuff; $i++) {
536 my $chosen = $chosen[$i] ? '*' : ' ';
537 my $print = $stuff[$i];
538 my $ref = ref $print;
539 my $highlighted = highlight_prefix(@{$prefixes[$i]})
540 if @prefixes;
541 if ($ref eq 'ARRAY') {
542 $print = $highlighted || $print->[0];
544 elsif ($ref eq 'HASH') {
545 my $value = $highlighted || $print->{VALUE};
546 $print = sprintf($status_fmt,
547 $print->{INDEX},
548 $print->{FILE},
549 $value);
551 else {
552 $print = $highlighted || $print;
554 printf("%s%2d: %s", $chosen, $i+1, $print);
555 if (($opts->{LIST_FLAT}) &&
556 (($i + 1) % ($opts->{LIST_FLAT}))) {
557 print "\t";
558 $last_lf = 0;
560 else {
561 print "\n";
562 $last_lf = 1;
565 if (!$last_lf) {
566 print "\n";
569 return if ($opts->{LIST_ONLY});
571 print colored $prompt_color, $opts->{PROMPT};
572 if ($opts->{SINGLETON}) {
573 print "> ";
575 else {
576 print ">> ";
578 my $line = <STDIN>;
579 if (!$line) {
580 print "\n";
581 $opts->{ON_EOF}->() if $opts->{ON_EOF};
582 last;
584 chomp $line;
585 last if $line eq '';
586 if ($line eq '?') {
587 $opts->{SINGLETON} ?
588 singleton_prompt_help_cmd() :
589 prompt_help_cmd();
590 next TOPLOOP;
592 for my $choice (split(/[\s,]+/, $line)) {
593 my $choose = 1;
594 my ($bottom, $top);
596 # Input that begins with '-'; unchoose
597 if ($choice =~ s/^-//) {
598 $choose = 0;
600 # A range can be specified like 5-7 or 5-.
601 if ($choice =~ /^(\d+)-(\d*)$/) {
602 ($bottom, $top) = ($1, length($2) ? $2 : 1 + @stuff);
604 elsif ($choice =~ /^\d+$/) {
605 $bottom = $top = $choice;
607 elsif ($choice eq '*') {
608 $bottom = 1;
609 $top = 1 + @stuff;
611 else {
612 $bottom = $top = find_unique($choice, @stuff);
613 if (!defined $bottom) {
614 error_msg "Huh ($choice)?\n";
615 next TOPLOOP;
618 if ($opts->{SINGLETON} && $bottom != $top) {
619 error_msg "Huh ($choice)?\n";
620 next TOPLOOP;
622 for ($i = $bottom-1; $i <= $top-1; $i++) {
623 next if (@stuff <= $i || $i < 0);
624 $chosen[$i] = $choose;
627 last if ($opts->{IMMEDIATE} || $line eq '*');
629 for ($i = 0; $i < @stuff; $i++) {
630 if ($chosen[$i]) {
631 push @return, $stuff[$i];
634 return @return;
637 sub singleton_prompt_help_cmd {
638 print colored $help_color, <<\EOF ;
639 Prompt help:
640 1 - select a numbered item
641 foo - select item based on unique prefix
642 - (empty) select nothing
646 sub prompt_help_cmd {
647 print colored $help_color, <<\EOF ;
648 Prompt help:
649 1 - select a single item
650 3-5 - select a range of items
651 2-3,6-9 - select multiple ranges
652 foo - select item based on unique prefix
653 -... - unselect specified items
654 * - choose all items
655 - (empty) finish selecting
659 sub status_cmd {
660 list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
661 list_modified());
662 print "\n";
665 sub say_n_paths {
666 my $did = shift @_;
667 my $cnt = scalar @_;
668 print "$did ";
669 if (1 < $cnt) {
670 print "$cnt paths\n";
672 else {
673 print "one path\n";
677 sub update_cmd {
678 my @mods = list_modified('file-only');
679 return if (!@mods);
681 my @update = list_and_choose({ PROMPT => 'Update',
682 HEADER => $status_head, },
683 @mods);
684 if (@update) {
685 system(qw(git update-index --add --remove --),
686 map { $_->{VALUE} } @update);
687 say_n_paths('updated', @update);
689 print "\n";
692 sub revert_cmd {
693 my @update = list_and_choose({ PROMPT => 'Revert',
694 HEADER => $status_head, },
695 list_modified());
696 if (@update) {
697 if (is_initial_commit()) {
698 system(qw(git rm --cached),
699 map { $_->{VALUE} } @update);
701 else {
702 my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
703 map { $_->{VALUE} } @update);
704 my $fh;
705 open $fh, '| git update-index --index-info'
706 or die;
707 for (@lines) {
708 print $fh $_;
710 close($fh);
711 for (@update) {
712 if ($_->{INDEX_ADDDEL} &&
713 $_->{INDEX_ADDDEL} eq 'create') {
714 system(qw(git update-index --force-remove --),
715 $_->{VALUE});
716 print "note: $_->{VALUE} is untracked now.\n";
720 refresh();
721 say_n_paths('reverted', @update);
723 print "\n";
726 sub add_untracked_cmd {
727 my @add = list_and_choose({ PROMPT => 'Add untracked' },
728 list_untracked());
729 if (@add) {
730 system(qw(git update-index --add --), @add);
731 say_n_paths('added', @add);
733 print "\n";
736 sub run_git_apply {
737 my $cmd = shift;
738 my $fh;
739 open $fh, '| git ' . $cmd . " --recount --allow-overlap";
740 print $fh @_;
741 return close $fh;
744 sub parse_diff {
745 my ($path) = @_;
746 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF});
747 if (defined $diff_algorithm) {
748 splice @diff_cmd, 1, 0, "--diff-algorithm=${diff_algorithm}";
750 if (defined $patch_mode_revision) {
751 push @diff_cmd, get_diff_reference($patch_mode_revision);
753 my @diff = run_cmd_pipe("git", @diff_cmd, "--", $path);
754 my @colored = ();
755 if ($diff_use_color) {
756 @colored = run_cmd_pipe("git", @diff_cmd, qw(--color --), $path);
758 my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
760 for (my $i = 0; $i < @diff; $i++) {
761 if ($diff[$i] =~ /^@@ /) {
762 push @hunk, { TEXT => [], DISPLAY => [],
763 TYPE => 'hunk' };
765 push @{$hunk[-1]{TEXT}}, $diff[$i];
766 push @{$hunk[-1]{DISPLAY}},
767 ($diff_use_color ? $colored[$i] : $diff[$i]);
769 return @hunk;
772 sub parse_diff_header {
773 my $src = shift;
775 my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
776 my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
777 my $deletion = { TEXT => [], DISPLAY => [], TYPE => 'deletion' };
779 for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
780 my $dest =
781 $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ? $mode :
782 $src->{TEXT}->[$i] =~ /^deleted file/ ? $deletion :
783 $head;
784 push @{$dest->{TEXT}}, $src->{TEXT}->[$i];
785 push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];
787 return ($head, $mode, $deletion);
790 sub hunk_splittable {
791 my ($text) = @_;
793 my @s = split_hunk($text);
794 return (1 < @s);
797 sub parse_hunk_header {
798 my ($line) = @_;
799 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt, $heading) =
800 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)/;
801 $o_cnt = 1 unless defined $o_cnt;
802 $n_cnt = 1 unless defined $n_cnt;
803 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt, $heading);
806 sub split_hunk {
807 my ($text, $display) = @_;
808 my @split = ();
809 if (!defined $display) {
810 $display = $text;
812 # If there are context lines in the middle of a hunk,
813 # it can be split, but we would need to take care of
814 # overlaps later.
815 my ($o_ofs, undef, $n_ofs, undef, $heading) = parse_hunk_header($text->[0]);
816 my $hunk_start = 1;
818 OUTER:
819 while (1) {
820 my $next_hunk_start = undef;
821 my $i = $hunk_start - 1;
822 my $this = +{
823 TEXT => [],
824 DISPLAY => [],
825 TYPE => 'hunk',
826 OLD => $o_ofs,
827 NEW => $n_ofs,
828 OCNT => 0,
829 NCNT => 0,
830 ADDDEL => 0,
831 POSTCTX => 0,
832 USE => undef,
835 while (++$i < @$text) {
836 my $line = $text->[$i];
837 my $display = $display->[$i];
838 if ($line =~ /^ /) {
839 if ($this->{ADDDEL} &&
840 !defined $next_hunk_start) {
841 # We have seen leading context and
842 # adds/dels and then here is another
843 # context, which is trailing for this
844 # split hunk and leading for the next
845 # one.
846 $next_hunk_start = $i;
848 push @{$this->{TEXT}}, $line;
849 push @{$this->{DISPLAY}}, $display;
850 $this->{OCNT}++;
851 $this->{NCNT}++;
852 if (defined $next_hunk_start) {
853 $this->{POSTCTX}++;
855 next;
858 # add/del
859 if (defined $next_hunk_start) {
860 # We are done with the current hunk and
861 # this is the first real change for the
862 # next split one.
863 $hunk_start = $next_hunk_start;
864 $o_ofs = $this->{OLD} + $this->{OCNT};
865 $n_ofs = $this->{NEW} + $this->{NCNT};
866 $o_ofs -= $this->{POSTCTX};
867 $n_ofs -= $this->{POSTCTX};
868 push @split, $this;
869 redo OUTER;
871 push @{$this->{TEXT}}, $line;
872 push @{$this->{DISPLAY}}, $display;
873 $this->{ADDDEL}++;
874 if ($line =~ /^-/) {
875 $this->{OCNT}++;
877 else {
878 $this->{NCNT}++;
882 push @split, $this;
883 last;
886 for my $hunk (@split) {
887 $o_ofs = $hunk->{OLD};
888 $n_ofs = $hunk->{NEW};
889 my $o_cnt = $hunk->{OCNT};
890 my $n_cnt = $hunk->{NCNT};
892 my $fraginfo = join(
894 "@@ -$o_ofs",
895 (($o_cnt != 1) ? ",$o_cnt" : ''),
896 " +$n_ofs",
897 (($n_cnt != 1) ? ",$n_cnt" : ''),
898 " @@"
900 unshift @{$hunk->{TEXT}}, join(
902 $fraginfo,
903 $heading,
904 "\n"
906 unshift @{$hunk->{DISPLAY}}, join(
908 $diff_use_color ? colored($fraginfo_color, $fraginfo) : $fraginfo,
909 $diff_use_color ? colored($funcname_color, $heading) : $heading,
910 "\n"
913 return @split;
916 sub find_last_o_ctx {
917 my ($it) = @_;
918 my $text = $it->{TEXT};
919 my ($o_ofs, $o_cnt) = parse_hunk_header($text->[0]);
920 my $i = @{$text};
921 my $last_o_ctx = $o_ofs + $o_cnt;
922 while (0 < --$i) {
923 my $line = $text->[$i];
924 if ($line =~ /^ /) {
925 $last_o_ctx--;
926 next;
928 last;
930 return $last_o_ctx;
933 sub merge_hunk {
934 my ($prev, $this) = @_;
935 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
936 parse_hunk_header($prev->{TEXT}[0]);
937 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
938 parse_hunk_header($this->{TEXT}[0]);
940 my (@line, $i, $ofs, $o_cnt, $n_cnt);
941 $ofs = $o0_ofs;
942 $o_cnt = $n_cnt = 0;
943 for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
944 my $line = $prev->{TEXT}[$i];
945 if ($line =~ /^\+/) {
946 $n_cnt++;
947 push @line, $line;
948 next;
951 last if ($o1_ofs <= $ofs);
953 $o_cnt++;
954 $ofs++;
955 if ($line =~ /^ /) {
956 $n_cnt++;
958 push @line, $line;
961 for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
962 my $line = $this->{TEXT}[$i];
963 if ($line =~ /^\+/) {
964 $n_cnt++;
965 push @line, $line;
966 next;
968 $ofs++;
969 $o_cnt++;
970 if ($line =~ /^ /) {
971 $n_cnt++;
973 push @line, $line;
975 my $head = ("@@ -$o0_ofs" .
976 (($o_cnt != 1) ? ",$o_cnt" : '') .
977 " +$n0_ofs" .
978 (($n_cnt != 1) ? ",$n_cnt" : '') .
979 " @@\n");
980 @{$prev->{TEXT}} = ($head, @line);
983 sub coalesce_overlapping_hunks {
984 my (@in) = @_;
985 my @out = ();
987 my ($last_o_ctx, $last_was_dirty);
989 for (grep { $_->{USE} } @in) {
990 if ($_->{TYPE} ne 'hunk') {
991 push @out, $_;
992 next;
994 my $text = $_->{TEXT};
995 my ($o_ofs) = parse_hunk_header($text->[0]);
996 if (defined $last_o_ctx &&
997 $o_ofs <= $last_o_ctx &&
998 !$_->{DIRTY} &&
999 !$last_was_dirty) {
1000 merge_hunk($out[-1], $_);
1002 else {
1003 push @out, $_;
1005 $last_o_ctx = find_last_o_ctx($out[-1]);
1006 $last_was_dirty = $_->{DIRTY};
1008 return @out;
1011 sub reassemble_patch {
1012 my $head = shift;
1013 my @patch;
1015 # Include everything in the header except the beginning of the diff.
1016 push @patch, (grep { !/^[-+]{3}/ } @$head);
1018 # Then include any headers from the hunk lines, which must
1019 # come before any actual hunk.
1020 while (@_ && $_[0] !~ /^@/) {
1021 push @patch, shift;
1024 # Then begin the diff.
1025 push @patch, grep { /^[-+]{3}/ } @$head;
1027 # And then the actual hunks.
1028 push @patch, @_;
1030 return @patch;
1033 sub color_diff {
1034 return map {
1035 colored((/^@/ ? $fraginfo_color :
1036 /^\+/ ? $diff_new_color :
1037 /^-/ ? $diff_old_color :
1038 $diff_plain_color),
1039 $_);
1040 } @_;
1043 sub edit_hunk_manually {
1044 my ($oldtext) = @_;
1046 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
1047 my $fh;
1048 open $fh, '>', $hunkfile
1049 or die "failed to open hunk edit file for writing: " . $!;
1050 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
1051 print $fh @$oldtext;
1052 my $participle = $patch_mode_flavour{PARTICIPLE};
1053 my $is_reverse = $patch_mode_flavour{IS_REVERSE};
1054 my ($remove_plus, $remove_minus) = $is_reverse ? ('-', '+') : ('+', '-');
1055 print $fh <<EOF;
1056 # ---
1057 # To remove '$remove_minus' lines, make them ' ' lines (context).
1058 # To remove '$remove_plus' lines, delete them.
1059 # Lines starting with # will be removed.
1061 # If the patch applies cleanly, the edited hunk will immediately be
1062 # marked for $participle. If it does not apply cleanly, you will be given
1063 # an opportunity to edit again. If all lines of the hunk are removed,
1064 # then the edit is aborted and the hunk is left unchanged.
1066 close $fh;
1068 chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR)));
1069 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
1071 if ($? != 0) {
1072 return undef;
1075 open $fh, '<', $hunkfile
1076 or die "failed to open hunk edit file for reading: " . $!;
1077 my @newtext = grep { !/^#/ } <$fh>;
1078 close $fh;
1079 unlink $hunkfile;
1081 # Abort if nothing remains
1082 if (!grep { /\S/ } @newtext) {
1083 return undef;
1086 # Reinsert the first hunk header if the user accidentally deleted it
1087 if ($newtext[0] !~ /^@/) {
1088 unshift @newtext, $oldtext->[0];
1090 return \@newtext;
1093 sub diff_applies {
1094 return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --check',
1095 map { @{$_->{TEXT}} } @_);
1098 sub _restore_terminal_and_die {
1099 ReadMode 'restore';
1100 print "\n";
1101 exit 1;
1104 sub prompt_single_character {
1105 if ($use_readkey) {
1106 local $SIG{TERM} = \&_restore_terminal_and_die;
1107 local $SIG{INT} = \&_restore_terminal_and_die;
1108 ReadMode 'cbreak';
1109 my $key = ReadKey 0;
1110 ReadMode 'restore';
1111 if ($use_termcap and $key eq "\e") {
1112 while (!defined $term_escapes{$key}) {
1113 my $next = ReadKey 0.5;
1114 last if (!defined $next);
1115 $key .= $next;
1117 $key =~ s/\e/^[/;
1119 print "$key" if defined $key;
1120 print "\n";
1121 return $key;
1122 } else {
1123 return <STDIN>;
1127 sub prompt_yesno {
1128 my ($prompt) = @_;
1129 while (1) {
1130 print colored $prompt_color, $prompt;
1131 my $line = prompt_single_character;
1132 return 0 if $line =~ /^n/i;
1133 return 1 if $line =~ /^y/i;
1137 sub edit_hunk_loop {
1138 my ($head, $hunk, $ix) = @_;
1139 my $text = $hunk->[$ix]->{TEXT};
1141 while (1) {
1142 $text = edit_hunk_manually($text);
1143 if (!defined $text) {
1144 return undef;
1146 my $newhunk = {
1147 TEXT => $text,
1148 TYPE => $hunk->[$ix]->{TYPE},
1149 USE => 1,
1150 DIRTY => 1,
1152 if (diff_applies($head,
1153 @{$hunk}[0..$ix-1],
1154 $newhunk,
1155 @{$hunk}[$ix+1..$#{$hunk}])) {
1156 $newhunk->{DISPLAY} = [color_diff(@{$text})];
1157 return $newhunk;
1159 else {
1160 prompt_yesno(
1161 'Your edited hunk does not apply. Edit again '
1162 . '(saying "no" discards!) [y/n]? '
1163 ) or return undef;
1168 sub help_patch_cmd {
1169 my $verb = lc $patch_mode_flavour{VERB};
1170 my $target = $patch_mode_flavour{TARGET};
1171 print colored $help_color, <<EOF ;
1172 y - $verb this hunk$target
1173 n - do not $verb this hunk$target
1174 q - quit; do not $verb this hunk or any of the remaining ones
1175 a - $verb this hunk and all later hunks in the file
1176 d - do not $verb this hunk or any of the later hunks in the file
1177 g - select a hunk to go to
1178 / - search for a hunk matching the given regex
1179 j - leave this hunk undecided, see next undecided hunk
1180 J - leave this hunk undecided, see next hunk
1181 k - leave this hunk undecided, see previous undecided hunk
1182 K - leave this hunk undecided, see previous hunk
1183 s - split the current hunk into smaller hunks
1184 e - manually edit the current hunk
1185 ? - print help
1189 sub apply_patch {
1190 my $cmd = shift;
1191 my $ret = run_git_apply $cmd, @_;
1192 if (!$ret) {
1193 print STDERR @_;
1195 return $ret;
1198 sub apply_patch_for_checkout_commit {
1199 my $reverse = shift;
1200 my $applies_index = run_git_apply 'apply '.$reverse.' --cached --check', @_;
1201 my $applies_worktree = run_git_apply 'apply '.$reverse.' --check', @_;
1203 if ($applies_worktree && $applies_index) {
1204 run_git_apply 'apply '.$reverse.' --cached', @_;
1205 run_git_apply 'apply '.$reverse, @_;
1206 return 1;
1207 } elsif (!$applies_index) {
1208 print colored $error_color, "The selected hunks do not apply to the index!\n";
1209 if (prompt_yesno "Apply them to the worktree anyway? ") {
1210 return run_git_apply 'apply '.$reverse, @_;
1211 } else {
1212 print colored $error_color, "Nothing was applied.\n";
1213 return 0;
1215 } else {
1216 print STDERR @_;
1217 return 0;
1221 sub patch_update_cmd {
1222 my @all_mods = list_modified($patch_mode_flavour{FILTER});
1223 error_msg "ignoring unmerged: $_->{VALUE}\n"
1224 for grep { $_->{UNMERGED} } @all_mods;
1225 @all_mods = grep { !$_->{UNMERGED} } @all_mods;
1227 my @mods = grep { !($_->{BINARY}) } @all_mods;
1228 my @them;
1230 if (!@mods) {
1231 if (@all_mods) {
1232 print STDERR "Only binary files changed.\n";
1233 } else {
1234 print STDERR "No changes.\n";
1236 return 0;
1238 if ($patch_mode) {
1239 @them = @mods;
1241 else {
1242 @them = list_and_choose({ PROMPT => 'Patch update',
1243 HEADER => $status_head, },
1244 @mods);
1246 for (@them) {
1247 return 0 if patch_update_file($_->{VALUE});
1251 # Generate a one line summary of a hunk.
1252 sub summarize_hunk {
1253 my $rhunk = shift;
1254 my $summary = $rhunk->{TEXT}[0];
1256 # Keep the line numbers, discard extra context.
1257 $summary =~ s/@@(.*?)@@.*/$1 /s;
1258 $summary .= " " x (20 - length $summary);
1260 # Add some user context.
1261 for my $line (@{$rhunk->{TEXT}}) {
1262 if ($line =~ m/^[+-].*\w/) {
1263 $summary .= $line;
1264 last;
1268 chomp $summary;
1269 return substr($summary, 0, 80) . "\n";
1273 # Print a one-line summary of each hunk in the array ref in
1274 # the first argument, starting with the index in the 2nd.
1275 sub display_hunks {
1276 my ($hunks, $i) = @_;
1277 my $ctr = 0;
1278 $i ||= 0;
1279 for (; $i < @$hunks && $ctr < 20; $i++, $ctr++) {
1280 my $status = " ";
1281 if (defined $hunks->[$i]{USE}) {
1282 $status = $hunks->[$i]{USE} ? "+" : "-";
1284 printf "%s%2d: %s",
1285 $status,
1286 $i + 1,
1287 summarize_hunk($hunks->[$i]);
1289 return $i;
1292 sub patch_update_file {
1293 my $quit = 0;
1294 my ($ix, $num);
1295 my $path = shift;
1296 my ($head, @hunk) = parse_diff($path);
1297 ($head, my $mode, my $deletion) = parse_diff_header($head);
1298 for (@{$head->{DISPLAY}}) {
1299 print;
1302 if (@{$mode->{TEXT}}) {
1303 unshift @hunk, $mode;
1305 if (@{$deletion->{TEXT}}) {
1306 foreach my $hunk (@hunk) {
1307 push @{$deletion->{TEXT}}, @{$hunk->{TEXT}};
1308 push @{$deletion->{DISPLAY}}, @{$hunk->{DISPLAY}};
1310 @hunk = ($deletion);
1313 $num = scalar @hunk;
1314 $ix = 0;
1316 while (1) {
1317 my ($prev, $next, $other, $undecided, $i);
1318 $other = '';
1320 if ($num <= $ix) {
1321 $ix = 0;
1323 for ($i = 0; $i < $ix; $i++) {
1324 if (!defined $hunk[$i]{USE}) {
1325 $prev = 1;
1326 $other .= ',k';
1327 last;
1330 if ($ix) {
1331 $other .= ',K';
1333 for ($i = $ix + 1; $i < $num; $i++) {
1334 if (!defined $hunk[$i]{USE}) {
1335 $next = 1;
1336 $other .= ',j';
1337 last;
1340 if ($ix < $num - 1) {
1341 $other .= ',J';
1343 if ($num > 1) {
1344 $other .= ',g';
1346 for ($i = 0; $i < $num; $i++) {
1347 if (!defined $hunk[$i]{USE}) {
1348 $undecided = 1;
1349 last;
1352 last if (!$undecided);
1354 if ($hunk[$ix]{TYPE} eq 'hunk' &&
1355 hunk_splittable($hunk[$ix]{TEXT})) {
1356 $other .= ',s';
1358 if ($hunk[$ix]{TYPE} eq 'hunk') {
1359 $other .= ',e';
1361 for (@{$hunk[$ix]{DISPLAY}}) {
1362 print;
1364 print colored $prompt_color, $patch_mode_flavour{VERB},
1365 ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' :
1366 $hunk[$ix]{TYPE} eq 'deletion' ? ' deletion' :
1367 ' this hunk'),
1368 $patch_mode_flavour{TARGET},
1369 " [y,n,q,a,d,/$other,?]? ";
1370 my $line = prompt_single_character;
1371 if ($line) {
1372 if ($line =~ /^y/i) {
1373 $hunk[$ix]{USE} = 1;
1375 elsif ($line =~ /^n/i) {
1376 $hunk[$ix]{USE} = 0;
1378 elsif ($line =~ /^a/i) {
1379 while ($ix < $num) {
1380 if (!defined $hunk[$ix]{USE}) {
1381 $hunk[$ix]{USE} = 1;
1383 $ix++;
1385 next;
1387 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1388 my $response = $1;
1389 my $no = $ix > 10 ? $ix - 10 : 0;
1390 while ($response eq '') {
1391 my $extra = "";
1392 $no = display_hunks(\@hunk, $no);
1393 if ($no < $num) {
1394 $extra = " (<ret> to see more)";
1396 print "go to which hunk$extra? ";
1397 $response = <STDIN>;
1398 if (!defined $response) {
1399 $response = '';
1401 chomp $response;
1403 if ($response !~ /^\s*\d+\s*$/) {
1404 error_msg "Invalid number: '$response'\n";
1405 } elsif (0 < $response && $response <= $num) {
1406 $ix = $response - 1;
1407 } else {
1408 error_msg "Sorry, only $num hunks available.\n";
1410 next;
1412 elsif ($line =~ /^d/i) {
1413 while ($ix < $num) {
1414 if (!defined $hunk[$ix]{USE}) {
1415 $hunk[$ix]{USE} = 0;
1417 $ix++;
1419 next;
1421 elsif ($line =~ /^q/i) {
1422 for ($i = 0; $i < $num; $i++) {
1423 if (!defined $hunk[$i]{USE}) {
1424 $hunk[$i]{USE} = 0;
1427 $quit = 1;
1428 last;
1430 elsif ($line =~ m|^/(.*)|) {
1431 my $regex = $1;
1432 if ($1 eq "") {
1433 print colored $prompt_color, "search for regex? ";
1434 $regex = <STDIN>;
1435 if (defined $regex) {
1436 chomp $regex;
1439 my $search_string;
1440 eval {
1441 $search_string = qr{$regex}m;
1443 if ($@) {
1444 my ($err,$exp) = ($@, $1);
1445 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
1446 error_msg "Malformed search regexp $exp: $err\n";
1447 next;
1449 my $iy = $ix;
1450 while (1) {
1451 my $text = join ("", @{$hunk[$iy]{TEXT}});
1452 last if ($text =~ $search_string);
1453 $iy++;
1454 $iy = 0 if ($iy >= $num);
1455 if ($ix == $iy) {
1456 error_msg "No hunk matches the given pattern\n";
1457 last;
1460 $ix = $iy;
1461 next;
1463 elsif ($line =~ /^K/) {
1464 if ($other =~ /K/) {
1465 $ix--;
1467 else {
1468 error_msg "No previous hunk\n";
1470 next;
1472 elsif ($line =~ /^J/) {
1473 if ($other =~ /J/) {
1474 $ix++;
1476 else {
1477 error_msg "No next hunk\n";
1479 next;
1481 elsif ($line =~ /^k/) {
1482 if ($other =~ /k/) {
1483 while (1) {
1484 $ix--;
1485 last if (!$ix ||
1486 !defined $hunk[$ix]{USE});
1489 else {
1490 error_msg "No previous hunk\n";
1492 next;
1494 elsif ($line =~ /^j/) {
1495 if ($other !~ /j/) {
1496 error_msg "No next hunk\n";
1497 next;
1500 elsif ($other =~ /s/ && $line =~ /^s/) {
1501 my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY});
1502 if (1 < @split) {
1503 print colored $header_color, "Split into ",
1504 scalar(@split), " hunks.\n";
1506 splice (@hunk, $ix, 1, @split);
1507 $num = scalar @hunk;
1508 next;
1510 elsif ($other =~ /e/ && $line =~ /^e/) {
1511 my $newhunk = edit_hunk_loop($head, \@hunk, $ix);
1512 if (defined $newhunk) {
1513 splice @hunk, $ix, 1, $newhunk;
1516 else {
1517 help_patch_cmd($other);
1518 next;
1520 # soft increment
1521 while (1) {
1522 $ix++;
1523 last if ($ix >= $num ||
1524 !defined $hunk[$ix]{USE});
1529 @hunk = coalesce_overlapping_hunks(@hunk);
1531 my $n_lofs = 0;
1532 my @result = ();
1533 for (@hunk) {
1534 if ($_->{USE}) {
1535 push @result, @{$_->{TEXT}};
1539 if (@result) {
1540 my @patch = reassemble_patch($head->{TEXT}, @result);
1541 my $apply_routine = $patch_mode_flavour{APPLY};
1542 &$apply_routine(@patch);
1543 refresh();
1546 print "\n";
1547 return $quit;
1550 sub diff_cmd {
1551 my @mods = list_modified('index-only');
1552 @mods = grep { !($_->{BINARY}) } @mods;
1553 return if (!@mods);
1554 my (@them) = list_and_choose({ PROMPT => 'Review diff',
1555 IMMEDIATE => 1,
1556 HEADER => $status_head, },
1557 @mods);
1558 return if (!@them);
1559 my $reference = is_initial_commit() ? get_empty_tree() : 'HEAD';
1560 system(qw(git diff -p --cached), $reference, '--',
1561 map { $_->{VALUE} } @them);
1564 sub quit_cmd {
1565 print "Bye.\n";
1566 exit(0);
1569 sub help_cmd {
1570 print colored $help_color, <<\EOF ;
1571 status - show paths with changes
1572 update - add working tree state to the staged set of changes
1573 revert - revert staged set of changes back to the HEAD version
1574 patch - pick hunks and update selectively
1575 diff - view diff between HEAD and index
1576 add untracked - add contents of untracked files to the staged set of changes
1580 sub process_args {
1581 return unless @ARGV;
1582 my $arg = shift @ARGV;
1583 if ($arg =~ /--patch(?:=(.*))?/) {
1584 if (defined $1) {
1585 if ($1 eq 'reset') {
1586 $patch_mode = 'reset_head';
1587 $patch_mode_revision = 'HEAD';
1588 $arg = shift @ARGV or die "missing --";
1589 if ($arg ne '--') {
1590 $patch_mode_revision = $arg;
1591 $patch_mode = ($arg eq 'HEAD' ?
1592 'reset_head' : 'reset_nothead');
1593 $arg = shift @ARGV or die "missing --";
1595 } elsif ($1 eq 'checkout') {
1596 $arg = shift @ARGV or die "missing --";
1597 if ($arg eq '--') {
1598 $patch_mode = 'checkout_index';
1599 } else {
1600 $patch_mode_revision = $arg;
1601 $patch_mode = ($arg eq 'HEAD' ?
1602 'checkout_head' : 'checkout_nothead');
1603 $arg = shift @ARGV or die "missing --";
1605 } elsif ($1 eq 'stage' or $1 eq 'stash') {
1606 $patch_mode = $1;
1607 $arg = shift @ARGV or die "missing --";
1608 } else {
1609 die "unknown --patch mode: $1";
1611 } else {
1612 $patch_mode = 'stage';
1613 $arg = shift @ARGV or die "missing --";
1615 die "invalid argument $arg, expecting --"
1616 unless $arg eq "--";
1617 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
1619 elsif ($arg ne "--") {
1620 die "invalid argument $arg, expecting --";
1624 sub main_loop {
1625 my @cmd = ([ 'status', \&status_cmd, ],
1626 [ 'update', \&update_cmd, ],
1627 [ 'revert', \&revert_cmd, ],
1628 [ 'add untracked', \&add_untracked_cmd, ],
1629 [ 'patch', \&patch_update_cmd, ],
1630 [ 'diff', \&diff_cmd, ],
1631 [ 'quit', \&quit_cmd, ],
1632 [ 'help', \&help_cmd, ],
1634 while (1) {
1635 my ($it) = list_and_choose({ PROMPT => 'What now',
1636 SINGLETON => 1,
1637 LIST_FLAT => 4,
1638 HEADER => '*** Commands ***',
1639 ON_EOF => \&quit_cmd,
1640 IMMEDIATE => 1 }, @cmd);
1641 if ($it) {
1642 eval {
1643 $it->[1]->();
1645 if ($@) {
1646 print "$@";
1652 process_args();
1653 refresh();
1654 if ($patch_mode) {
1655 patch_update_cmd();
1657 else {
1658 status_cmd();
1659 main_loop();