]> git.ipfire.org Git - thirdparty/git.git/blame - git-add--interactive.perl
git-config: document interactive.singlekey requires Term::ReadKey
[thirdparty/git.git] / git-add--interactive.perl
CommitLineData
3328aced 1#!/usr/bin/perl
5cde71d6 2
d48b2841 3use 5.008;
5cde71d6 4use strict;
3328aced 5use warnings;
b4c61ed6
JH
6use Git;
7
8851f480
JH
8binmode(STDOUT, ":raw");
9
b4c61ed6
JH
10my $repo = Git->repository();
11
f87e310d
JK
12my $menu_use_color = $repo->get_colorbool('color.interactive');
13my ($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 ) : ();
a3019736
TR
19my $error_color = ();
20if ($menu_use_color) {
9aad6cba
SB
21 my $help_color_spec = ($repo->config('color.interactive.help') or
22 'red bold');
a3019736
TR
23 $error_color = $repo->get_color('color.interactive.error',
24 $help_color_spec);
25}
b4c61ed6 26
f87e310d
JK
27my $diff_use_color = $repo->get_colorbool('color.diff');
28my ($fraginfo_color) =
29 $diff_use_color ? (
30 $repo->get_color('color.diff.frag', 'cyan'),
31 ) : ();
ac083c47
TR
32my ($diff_plain_color) =
33 $diff_use_color ? (
34 $repo->get_color('color.diff.plain', ''),
35 ) : ();
36my ($diff_old_color) =
37 $diff_use_color ? (
38 $repo->get_color('color.diff.old', 'red'),
39 ) : ();
40my ($diff_new_color) =
41 $diff_use_color ? (
42 $repo->get_color('color.diff.new', 'green'),
43 ) : ();
b4c61ed6 44
f87e310d 45my $normal_color = $repo->get_color("", "reset");
b4c61ed6 46
2cc0f53b
JK
47my $diff_algorithm = $repo->config('diff.algorithm');
48
ca6ac7f1 49my $use_readkey = 0;
b5cc0032
TR
50my $use_termcap = 0;
51my %term_escapes;
52
748aa689
TR
53sub ReadMode;
54sub ReadKey;
ca6ac7f1
TR
55if ($repo->config_bool("interactive.singlekey")) {
56 eval {
748aa689
TR
57 require Term::ReadKey;
58 Term::ReadKey->import;
ca6ac7f1
TR
59 $use_readkey = 1;
60 };
b5cc0032
TR
61 eval {
62 require Term::Cap;
63 my $termcap = Term::Cap->Tgetent;
64 foreach (values %$termcap) {
65 $term_escapes{$_} = 1 if /^\e/;
66 }
67 $use_termcap = 1;
68 };
ca6ac7f1
TR
69}
70
b4c61ed6
JH
71sub colored {
72 my $color = shift;
73 my $string = join("", @_);
74
f87e310d 75 if (defined $color) {
b4c61ed6
JH
76 # Put a color code at the beginning of each line, a reset at the end
77 # color after newlines that are not at the end of the string
78 $string =~ s/(\n+)(.)/$1$color$2/g;
79 # reset before newlines
80 $string =~ s/(\n+)/$normal_color$1/g;
81 # codes at beginning and end (if necessary):
82 $string =~ s/^/$color/;
83 $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
84 }
85 return $string;
86}
5cde71d6 87
b63e9950
WC
88# command line options
89my $patch_mode;
d002ef4d 90my $patch_mode_revision;
b63e9950 91
8f0bef6d 92sub apply_patch;
4f353658 93sub apply_patch_for_checkout_commit;
dda1f2a5 94sub apply_patch_for_stash;
8f0bef6d
TR
95
96my %patch_modes = (
97 'stage' => {
98 DIFF => 'diff-files -p',
99 APPLY => sub { apply_patch 'apply --cached', @_; },
100 APPLY_CHECK => 'apply --cached',
101 VERB => 'Stage',
102 TARGET => '',
103 PARTICIPLE => 'staging',
104 FILTER => 'file-only',
7b8c7051 105 IS_REVERSE => 0,
8f0bef6d 106 },
dda1f2a5
TR
107 'stash' => {
108 DIFF => 'diff-index -p HEAD',
109 APPLY => sub { apply_patch 'apply --cached', @_; },
110 APPLY_CHECK => 'apply --cached',
111 VERB => 'Stash',
112 TARGET => '',
113 PARTICIPLE => 'stashing',
114 FILTER => undef,
7b8c7051 115 IS_REVERSE => 0,
dda1f2a5 116 },
d002ef4d
TR
117 'reset_head' => {
118 DIFF => 'diff-index -p --cached',
119 APPLY => sub { apply_patch 'apply -R --cached', @_; },
120 APPLY_CHECK => 'apply -R --cached',
121 VERB => 'Unstage',
122 TARGET => '',
123 PARTICIPLE => 'unstaging',
124 FILTER => 'index-only',
7b8c7051 125 IS_REVERSE => 1,
d002ef4d
TR
126 },
127 'reset_nothead' => {
128 DIFF => 'diff-index -R -p --cached',
129 APPLY => sub { apply_patch 'apply --cached', @_; },
130 APPLY_CHECK => 'apply --cached',
131 VERB => 'Apply',
132 TARGET => ' to index',
133 PARTICIPLE => 'applying',
134 FILTER => 'index-only',
7b8c7051 135 IS_REVERSE => 0,
d002ef4d 136 },
4f353658
TR
137 'checkout_index' => {
138 DIFF => 'diff-files -p',
139 APPLY => sub { apply_patch 'apply -R', @_; },
140 APPLY_CHECK => 'apply -R',
141 VERB => 'Discard',
142 TARGET => ' from worktree',
143 PARTICIPLE => 'discarding',
144 FILTER => 'file-only',
7b8c7051 145 IS_REVERSE => 1,
4f353658
TR
146 },
147 'checkout_head' => {
148 DIFF => 'diff-index -p',
149 APPLY => sub { apply_patch_for_checkout_commit '-R', @_ },
150 APPLY_CHECK => 'apply -R',
151 VERB => 'Discard',
152 TARGET => ' from index and worktree',
153 PARTICIPLE => 'discarding',
154 FILTER => undef,
7b8c7051 155 IS_REVERSE => 1,
4f353658
TR
156 },
157 'checkout_nothead' => {
158 DIFF => 'diff-index -R -p',
159 APPLY => sub { apply_patch_for_checkout_commit '', @_ },
160 APPLY_CHECK => 'apply',
161 VERB => 'Apply',
162 TARGET => ' to index and worktree',
163 PARTICIPLE => 'applying',
164 FILTER => undef,
7b8c7051 165 IS_REVERSE => 0,
4f353658 166 },
8f0bef6d
TR
167);
168
169my %patch_mode_flavour = %{$patch_modes{stage}};
b63e9950 170
5cde71d6 171sub run_cmd_pipe {
df17e77c 172 if ($^O eq 'MSWin32') {
21e9757e
AR
173 my @invalid = grep {m/[":*]/} @_;
174 die "$^O does not support: @invalid\n" if @invalid;
175 my @args = map { m/ /o ? "\"$_\"": $_ } @_;
176 return qx{@args};
177 } else {
178 my $fh = undef;
179 open($fh, '-|', @_) or die;
180 return <$fh>;
181 }
5cde71d6
JH
182}
183
184my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir));
185
186if (!defined $GIT_DIR) {
187 exit(1); # rev-parse would have already said "not a git repo"
188}
189chomp($GIT_DIR);
190
8851f480
JH
191my %cquote_map = (
192 "b" => chr(8),
193 "t" => chr(9),
194 "n" => chr(10),
195 "v" => chr(11),
196 "f" => chr(12),
197 "r" => chr(13),
198 "\\" => "\\",
199 "\042" => "\042",
200);
201
202sub unquote_path {
203 local ($_) = @_;
204 my ($retval, $remainder);
205 if (!/^\042(.*)\042$/) {
206 return $_;
207 }
208 ($_, $retval) = ($1, "");
209 while (/^([^\\]*)\\(.*)$/) {
210 $remainder = $2;
211 $retval .= $1;
212 for ($remainder) {
213 if (/^([0-3][0-7][0-7])(.*)$/) {
214 $retval .= chr(oct($1));
215 $_ = $2;
216 last;
217 }
218 if (/^([\\\042btnvfr])(.*)$/) {
219 $retval .= $cquote_map{$1};
220 $_ = $2;
221 last;
222 }
223 # This is malformed -- just return it as-is for now.
224 return $_[0];
225 }
226 $_ = $remainder;
227 }
228 $retval .= $_;
229 return $retval;
230}
231
5cde71d6
JH
232sub refresh {
233 my $fh;
21e9757e 234 open $fh, 'git update-index --refresh |'
5cde71d6
JH
235 or die;
236 while (<$fh>) {
237 ;# ignore 'needs update'
238 }
239 close $fh;
240}
241
242sub list_untracked {
243 map {
244 chomp $_;
8851f480 245 unquote_path($_);
5cde71d6 246 }
4c841684 247 run_cmd_pipe(qw(git ls-files --others --exclude-standard --), @ARGV);
5cde71d6
JH
248}
249
250my $status_fmt = '%12s %12s %s';
251my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
252
18bc7616
JK
253{
254 my $initial;
255 sub is_initial_commit {
256 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
257 unless defined $initial;
258 return $initial;
259 }
260}
261
262sub get_empty_tree {
263 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
264}
265
954312a3
JK
266sub get_diff_reference {
267 my $ref = shift;
268 if (defined $ref and $ref ne 'HEAD') {
269 return $ref;
270 } elsif (is_initial_commit()) {
271 return get_empty_tree();
272 } else {
273 return 'HEAD';
274 }
275}
276
5cde71d6 277# Returns list of hashes, contents of each of which are:
5cde71d6
JH
278# VALUE: pathname
279# BINARY: is a binary path
280# INDEX: is index different from HEAD?
281# FILE: is file different from index?
282# INDEX_ADDDEL: is it add/delete between HEAD and index?
283# FILE_ADDDEL: is it add/delete between index and file?
4066bd67 284# UNMERGED: is the path unmerged
5cde71d6
JH
285
286sub list_modified {
287 my ($only) = @_;
288 my (%data, @return);
289 my ($add, $del, $adddel, $file);
4c841684
WC
290 my @tracked = ();
291
292 if (@ARGV) {
293 @tracked = map {
8851f480
JH
294 chomp $_;
295 unquote_path($_);
b145b211 296 } run_cmd_pipe(qw(git ls-files --), @ARGV);
4c841684
WC
297 return if (!@tracked);
298 }
5cde71d6 299
954312a3 300 my $reference = get_diff_reference($patch_mode_revision);
5cde71d6 301 for (run_cmd_pipe(qw(git diff-index --cached
18bc7616
JK
302 --numstat --summary), $reference,
303 '--', @tracked)) {
5cde71d6
JH
304 if (($add, $del, $file) =
305 /^([-\d]+) ([-\d]+) (.*)/) {
306 my ($change, $bin);
8851f480 307 $file = unquote_path($file);
5cde71d6
JH
308 if ($add eq '-' && $del eq '-') {
309 $change = 'binary';
310 $bin = 1;
311 }
312 else {
313 $change = "+$add/-$del";
314 }
315 $data{$file} = {
316 INDEX => $change,
317 BINARY => $bin,
318 FILE => 'nothing',
319 }
320 }
321 elsif (($adddel, $file) =
322 /^ (create|delete) mode [0-7]+ (.*)$/) {
8851f480 323 $file = unquote_path($file);
5cde71d6
JH
324 $data{$file}{INDEX_ADDDEL} = $adddel;
325 }
326 }
327
4066bd67 328 for (run_cmd_pipe(qw(git diff-files --numstat --summary --raw --), @tracked)) {
5cde71d6
JH
329 if (($add, $del, $file) =
330 /^([-\d]+) ([-\d]+) (.*)/) {
8851f480 331 $file = unquote_path($file);
5cde71d6
JH
332 my ($change, $bin);
333 if ($add eq '-' && $del eq '-') {
334 $change = 'binary';
335 $bin = 1;
336 }
337 else {
338 $change = "+$add/-$del";
339 }
340 $data{$file}{FILE} = $change;
341 if ($bin) {
342 $data{$file}{BINARY} = 1;
343 }
344 }
345 elsif (($adddel, $file) =
346 /^ (create|delete) mode [0-7]+ (.*)$/) {
8851f480 347 $file = unquote_path($file);
5cde71d6
JH
348 $data{$file}{FILE_ADDDEL} = $adddel;
349 }
4066bd67
JK
350 elsif (/^:[0-7]+ [0-7]+ [0-9a-f]+ [0-9a-f]+ (.) (.*)$/) {
351 $file = unquote_path($2);
352 if (!exists $data{$file}) {
353 $data{$file} = +{
354 INDEX => 'unchanged',
355 BINARY => 0,
356 };
357 }
358 if ($1 eq 'U') {
359 $data{$file}{UNMERGED} = 1;
360 }
361 }
5cde71d6
JH
362 }
363
364 for (sort keys %data) {
365 my $it = $data{$_};
366
367 if ($only) {
368 if ($only eq 'index-only') {
369 next if ($it->{INDEX} eq 'unchanged');
370 }
371 if ($only eq 'file-only') {
372 next if ($it->{FILE} eq 'nothing');
373 }
374 }
375 push @return, +{
376 VALUE => $_,
5cde71d6
JH
377 %$it,
378 };
379 }
380 return @return;
381}
382
383sub find_unique {
384 my ($string, @stuff) = @_;
385 my $found = undef;
386 for (my $i = 0; $i < @stuff; $i++) {
387 my $it = $stuff[$i];
388 my $hit = undef;
389 if (ref $it) {
390 if ((ref $it) eq 'ARRAY') {
391 $it = $it->[0];
392 }
393 else {
394 $it = $it->{VALUE};
395 }
396 }
397 eval {
398 if ($it =~ /^$string/) {
399 $hit = 1;
400 };
401 };
402 if (defined $hit && defined $found) {
403 return undef;
404 }
405 if ($hit) {
406 $found = $i + 1;
407 }
408 }
409 return $found;
410}
411
14cb5038
WC
412# inserts string into trie and updates count for each character
413sub update_trie {
414 my ($trie, $string) = @_;
415 foreach (split //, $string) {
416 $trie = $trie->{$_} ||= {COUNT => 0};
417 $trie->{COUNT}++;
418 }
419}
420
421# returns an array of tuples (prefix, remainder)
422sub find_unique_prefixes {
423 my @stuff = @_;
424 my @return = ();
425
426 # any single prefix exceeding the soft limit is omitted
427 # if any prefix exceeds the hard limit all are omitted
428 # 0 indicates no limit
429 my $soft_limit = 0;
430 my $hard_limit = 3;
431
432 # build a trie modelling all possible options
433 my %trie;
434 foreach my $print (@stuff) {
435 if ((ref $print) eq 'ARRAY') {
436 $print = $print->[0];
437 }
63320989 438 elsif ((ref $print) eq 'HASH') {
14cb5038
WC
439 $print = $print->{VALUE};
440 }
441 update_trie(\%trie, $print);
442 push @return, $print;
443 }
444
445 # use the trie to find the unique prefixes
446 for (my $i = 0; $i < @return; $i++) {
447 my $ret = $return[$i];
448 my @letters = split //, $ret;
449 my %search = %trie;
450 my ($prefix, $remainder);
451 my $j;
452 for ($j = 0; $j < @letters; $j++) {
453 my $letter = $letters[$j];
454 if ($search{$letter}{COUNT} == 1) {
455 $prefix = substr $ret, 0, $j + 1;
456 $remainder = substr $ret, $j + 1;
457 last;
458 }
459 else {
460 my $prefix = substr $ret, 0, $j;
461 return ()
462 if ($hard_limit && $j + 1 > $hard_limit);
463 }
464 %search = %{$search{$letter}};
465 }
8851f480
JH
466 if (ord($letters[0]) > 127 ||
467 ($soft_limit && $j + 1 > $soft_limit)) {
14cb5038
WC
468 $prefix = undef;
469 $remainder = $ret;
470 }
471 $return[$i] = [$prefix, $remainder];
472 }
473 return @return;
474}
475
63320989
WC
476# filters out prefixes which have special meaning to list_and_choose()
477sub is_valid_prefix {
478 my $prefix = shift;
479 return (defined $prefix) &&
480 !($prefix =~ /[\s,]/) && # separators
481 !($prefix =~ /^-/) && # deselection
482 !($prefix =~ /^\d+/) && # selection
7e018be2
WC
483 ($prefix ne '*') && # "all" wildcard
484 ($prefix ne '?'); # prompt help
63320989
WC
485}
486
14cb5038
WC
487# given a prefix/remainder tuple return a string with the prefix highlighted
488# for now use square brackets; later might use ANSI colors (underline, bold)
489sub highlight_prefix {
490 my $prefix = shift;
491 my $remainder = shift;
b4c61ed6
JH
492
493 if (!defined $prefix) {
494 return $remainder;
495 }
496
497 if (!is_valid_prefix($prefix)) {
498 return "$prefix$remainder";
499 }
500
f87e310d 501 if (!$menu_use_color) {
b4c61ed6
JH
502 return "[$prefix]$remainder";
503 }
504
505 return "$prompt_color$prefix$normal_color$remainder";
14cb5038
WC
506}
507
a3019736
TR
508sub error_msg {
509 print STDERR colored $error_color, @_;
510}
511
5cde71d6
JH
512sub list_and_choose {
513 my ($opts, @stuff) = @_;
514 my (@chosen, @return);
515 my $i;
14cb5038 516 my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY};
5cde71d6
JH
517
518 TOPLOOP:
519 while (1) {
520 my $last_lf = 0;
521
522 if ($opts->{HEADER}) {
523 if (!$opts->{LIST_FLAT}) {
524 print " ";
525 }
b4c61ed6 526 print colored $header_color, "$opts->{HEADER}\n";
5cde71d6
JH
527 }
528 for ($i = 0; $i < @stuff; $i++) {
529 my $chosen = $chosen[$i] ? '*' : ' ';
530 my $print = $stuff[$i];
63320989
WC
531 my $ref = ref $print;
532 my $highlighted = highlight_prefix(@{$prefixes[$i]})
533 if @prefixes;
534 if ($ref eq 'ARRAY') {
535 $print = $highlighted || $print->[0];
536 }
537 elsif ($ref eq 'HASH') {
538 my $value = $highlighted || $print->{VALUE};
539 $print = sprintf($status_fmt,
540 $print->{INDEX},
541 $print->{FILE},
542 $value);
543 }
544 else {
545 $print = $highlighted || $print;
5cde71d6
JH
546 }
547 printf("%s%2d: %s", $chosen, $i+1, $print);
548 if (($opts->{LIST_FLAT}) &&
549 (($i + 1) % ($opts->{LIST_FLAT}))) {
550 print "\t";
551 $last_lf = 0;
552 }
553 else {
554 print "\n";
555 $last_lf = 1;
556 }
557 }
558 if (!$last_lf) {
559 print "\n";
560 }
561
562 return if ($opts->{LIST_ONLY});
563
b4c61ed6 564 print colored $prompt_color, $opts->{PROMPT};
5cde71d6
JH
565 if ($opts->{SINGLETON}) {
566 print "> ";
567 }
568 else {
569 print ">> ";
570 }
571 my $line = <STDIN>;
c95c0248
JLH
572 if (!$line) {
573 print "\n";
574 $opts->{ON_EOF}->() if $opts->{ON_EOF};
575 last;
576 }
5cde71d6 577 chomp $line;
6a6eb3d0 578 last if $line eq '';
7e018be2
WC
579 if ($line eq '?') {
580 $opts->{SINGLETON} ?
581 singleton_prompt_help_cmd() :
582 prompt_help_cmd();
583 next TOPLOOP;
584 }
5cde71d6
JH
585 for my $choice (split(/[\s,]+/, $line)) {
586 my $choose = 1;
587 my ($bottom, $top);
588
589 # Input that begins with '-'; unchoose
590 if ($choice =~ s/^-//) {
591 $choose = 0;
592 }
1e5aaa6d
CM
593 # A range can be specified like 5-7 or 5-.
594 if ($choice =~ /^(\d+)-(\d*)$/) {
595 ($bottom, $top) = ($1, length($2) ? $2 : 1 + @stuff);
5cde71d6
JH
596 }
597 elsif ($choice =~ /^\d+$/) {
598 $bottom = $top = $choice;
599 }
600 elsif ($choice eq '*') {
601 $bottom = 1;
602 $top = 1 + @stuff;
603 }
604 else {
605 $bottom = $top = find_unique($choice, @stuff);
606 if (!defined $bottom) {
a3019736 607 error_msg "Huh ($choice)?\n";
5cde71d6
JH
608 next TOPLOOP;
609 }
610 }
611 if ($opts->{SINGLETON} && $bottom != $top) {
a3019736 612 error_msg "Huh ($choice)?\n";
5cde71d6
JH
613 next TOPLOOP;
614 }
615 for ($i = $bottom-1; $i <= $top-1; $i++) {
6a6eb3d0 616 next if (@stuff <= $i || $i < 0);
5cde71d6 617 $chosen[$i] = $choose;
5cde71d6
JH
618 }
619 }
12db334e 620 last if ($opts->{IMMEDIATE} || $line eq '*');
5cde71d6
JH
621 }
622 for ($i = 0; $i < @stuff; $i++) {
623 if ($chosen[$i]) {
624 push @return, $stuff[$i];
625 }
626 }
627 return @return;
628}
629
7e018be2 630sub singleton_prompt_help_cmd {
b4c61ed6 631 print colored $help_color, <<\EOF ;
7e018be2
WC
632Prompt help:
6331 - select a numbered item
634foo - select item based on unique prefix
635 - (empty) select nothing
636EOF
637}
638
639sub prompt_help_cmd {
b4c61ed6 640 print colored $help_color, <<\EOF ;
7e018be2
WC
641Prompt help:
6421 - select a single item
6433-5 - select a range of items
6442-3,6-9 - select multiple ranges
645foo - select item based on unique prefix
646-... - unselect specified items
647* - choose all items
648 - (empty) finish selecting
649EOF
650}
651
5cde71d6
JH
652sub status_cmd {
653 list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
654 list_modified());
655 print "\n";
656}
657
658sub say_n_paths {
659 my $did = shift @_;
660 my $cnt = scalar @_;
661 print "$did ";
662 if (1 < $cnt) {
663 print "$cnt paths\n";
664 }
665 else {
666 print "one path\n";
667 }
668}
669
670sub update_cmd {
671 my @mods = list_modified('file-only');
672 return if (!@mods);
673
674 my @update = list_and_choose({ PROMPT => 'Update',
675 HEADER => $status_head, },
676 @mods);
677 if (@update) {
a4f7112f 678 system(qw(git update-index --add --remove --),
5cde71d6
JH
679 map { $_->{VALUE} } @update);
680 say_n_paths('updated', @update);
681 }
682 print "\n";
683}
684
685sub revert_cmd {
686 my @update = list_and_choose({ PROMPT => 'Revert',
687 HEADER => $status_head, },
688 list_modified());
689 if (@update) {
18bc7616
JK
690 if (is_initial_commit()) {
691 system(qw(git rm --cached),
692 map { $_->{VALUE} } @update);
5cde71d6 693 }
18bc7616
JK
694 else {
695 my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
696 map { $_->{VALUE} } @update);
697 my $fh;
698 open $fh, '| git update-index --index-info'
699 or die;
700 for (@lines) {
701 print $fh $_;
702 }
703 close($fh);
704 for (@update) {
705 if ($_->{INDEX_ADDDEL} &&
706 $_->{INDEX_ADDDEL} eq 'create') {
707 system(qw(git update-index --force-remove --),
708 $_->{VALUE});
709 print "note: $_->{VALUE} is untracked now.\n";
710 }
5cde71d6
JH
711 }
712 }
713 refresh();
714 say_n_paths('reverted', @update);
715 }
716 print "\n";
717}
718
719sub add_untracked_cmd {
720 my @add = list_and_choose({ PROMPT => 'Add untracked' },
721 list_untracked());
722 if (@add) {
723 system(qw(git update-index --add --), @add);
724 say_n_paths('added', @add);
725 }
726 print "\n";
727}
728
8f0bef6d
TR
729sub run_git_apply {
730 my $cmd = shift;
731 my $fh;
933e44d3 732 open $fh, '| git ' . $cmd . " --recount --allow-overlap";
8f0bef6d
TR
733 print $fh @_;
734 return close $fh;
735}
736
5cde71d6
JH
737sub parse_diff {
738 my ($path) = @_;
8f0bef6d 739 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF});
2cc0f53b 740 if (defined $diff_algorithm) {
e5c29097 741 splice @diff_cmd, 1, 0, "--diff-algorithm=${diff_algorithm}";
2cc0f53b 742 }
d002ef4d 743 if (defined $patch_mode_revision) {
954312a3 744 push @diff_cmd, get_diff_reference($patch_mode_revision);
d002ef4d 745 }
8f0bef6d 746 my @diff = run_cmd_pipe("git", @diff_cmd, "--", $path);
4af756f3
WC
747 my @colored = ();
748 if ($diff_use_color) {
8f0bef6d 749 @colored = run_cmd_pipe("git", @diff_cmd, qw(--color --), $path);
5cde71d6 750 }
0392513f 751 my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
b4c61ed6 752
4af756f3
WC
753 for (my $i = 0; $i < @diff; $i++) {
754 if ($diff[$i] =~ /^@@ /) {
0392513f
JK
755 push @hunk, { TEXT => [], DISPLAY => [],
756 TYPE => 'hunk' };
b4c61ed6 757 }
4af756f3
WC
758 push @{$hunk[-1]{TEXT}}, $diff[$i];
759 push @{$hunk[-1]{DISPLAY}},
760 ($diff_use_color ? $colored[$i] : $diff[$i]);
b4c61ed6 761 }
4af756f3 762 return @hunk;
b4c61ed6
JH
763}
764
b717a627
JK
765sub parse_diff_header {
766 my $src = shift;
767
0392513f
JK
768 my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
769 my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
24ab81ae 770 my $deletion = { TEXT => [], DISPLAY => [], TYPE => 'deletion' };
b717a627
JK
771
772 for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
24ab81ae
JK
773 my $dest =
774 $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ? $mode :
775 $src->{TEXT}->[$i] =~ /^deleted file/ ? $deletion :
776 $head;
b717a627
JK
777 push @{$dest->{TEXT}}, $src->{TEXT}->[$i];
778 push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];
779 }
24ab81ae 780 return ($head, $mode, $deletion);
b717a627
JK
781}
782
835b2aeb
JH
783sub hunk_splittable {
784 my ($text) = @_;
785
786 my @s = split_hunk($text);
787 return (1 < @s);
788}
789
790sub parse_hunk_header {
791 my ($line) = @_;
792 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
7288ed8e
JLH
793 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
794 $o_cnt = 1 unless defined $o_cnt;
795 $n_cnt = 1 unless defined $n_cnt;
835b2aeb
JH
796 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
797}
798
799sub split_hunk {
4af756f3 800 my ($text, $display) = @_;
835b2aeb 801 my @split = ();
4af756f3
WC
802 if (!defined $display) {
803 $display = $text;
804 }
835b2aeb
JH
805 # If there are context lines in the middle of a hunk,
806 # it can be split, but we would need to take care of
807 # overlaps later.
808
7b40a455 809 my ($o_ofs, undef, $n_ofs) = parse_hunk_header($text->[0]);
835b2aeb 810 my $hunk_start = 1;
835b2aeb
JH
811
812 OUTER:
813 while (1) {
814 my $next_hunk_start = undef;
815 my $i = $hunk_start - 1;
816 my $this = +{
817 TEXT => [],
4af756f3 818 DISPLAY => [],
0392513f 819 TYPE => 'hunk',
835b2aeb
JH
820 OLD => $o_ofs,
821 NEW => $n_ofs,
822 OCNT => 0,
823 NCNT => 0,
824 ADDDEL => 0,
825 POSTCTX => 0,
4af756f3 826 USE => undef,
835b2aeb
JH
827 };
828
829 while (++$i < @$text) {
830 my $line = $text->[$i];
4af756f3 831 my $display = $display->[$i];
835b2aeb
JH
832 if ($line =~ /^ /) {
833 if ($this->{ADDDEL} &&
834 !defined $next_hunk_start) {
835 # We have seen leading context and
836 # adds/dels and then here is another
837 # context, which is trailing for this
838 # split hunk and leading for the next
839 # one.
840 $next_hunk_start = $i;
841 }
842 push @{$this->{TEXT}}, $line;
4af756f3 843 push @{$this->{DISPLAY}}, $display;
835b2aeb
JH
844 $this->{OCNT}++;
845 $this->{NCNT}++;
846 if (defined $next_hunk_start) {
847 $this->{POSTCTX}++;
848 }
849 next;
850 }
851
852 # add/del
853 if (defined $next_hunk_start) {
854 # We are done with the current hunk and
855 # this is the first real change for the
856 # next split one.
857 $hunk_start = $next_hunk_start;
858 $o_ofs = $this->{OLD} + $this->{OCNT};
859 $n_ofs = $this->{NEW} + $this->{NCNT};
860 $o_ofs -= $this->{POSTCTX};
861 $n_ofs -= $this->{POSTCTX};
862 push @split, $this;
863 redo OUTER;
864 }
865 push @{$this->{TEXT}}, $line;
4af756f3 866 push @{$this->{DISPLAY}}, $display;
835b2aeb
JH
867 $this->{ADDDEL}++;
868 if ($line =~ /^-/) {
869 $this->{OCNT}++;
870 }
871 else {
872 $this->{NCNT}++;
873 }
874 }
875
876 push @split, $this;
877 last;
878 }
879
880 for my $hunk (@split) {
881 $o_ofs = $hunk->{OLD};
882 $n_ofs = $hunk->{NEW};
7b40a455
JLH
883 my $o_cnt = $hunk->{OCNT};
884 my $n_cnt = $hunk->{NCNT};
835b2aeb
JH
885
886 my $head = ("@@ -$o_ofs" .
887 (($o_cnt != 1) ? ",$o_cnt" : '') .
888 " +$n_ofs" .
889 (($n_cnt != 1) ? ",$n_cnt" : '') .
890 " @@\n");
4af756f3 891 my $display_head = $head;
835b2aeb 892 unshift @{$hunk->{TEXT}}, $head;
4af756f3
WC
893 if ($diff_use_color) {
894 $display_head = colored($fraginfo_color, $head);
895 }
896 unshift @{$hunk->{DISPLAY}}, $display_head;
835b2aeb 897 }
4af756f3 898 return @split;
835b2aeb
JH
899}
900
7a26e653
JH
901sub find_last_o_ctx {
902 my ($it) = @_;
903 my $text = $it->{TEXT};
904 my ($o_ofs, $o_cnt) = parse_hunk_header($text->[0]);
905 my $i = @{$text};
906 my $last_o_ctx = $o_ofs + $o_cnt;
907 while (0 < --$i) {
908 my $line = $text->[$i];
909 if ($line =~ /^ /) {
910 $last_o_ctx--;
911 next;
912 }
913 last;
914 }
915 return $last_o_ctx;
916}
917
918sub merge_hunk {
919 my ($prev, $this) = @_;
920 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
921 parse_hunk_header($prev->{TEXT}[0]);
922 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
923 parse_hunk_header($this->{TEXT}[0]);
924
925 my (@line, $i, $ofs, $o_cnt, $n_cnt);
926 $ofs = $o0_ofs;
927 $o_cnt = $n_cnt = 0;
928 for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
929 my $line = $prev->{TEXT}[$i];
930 if ($line =~ /^\+/) {
931 $n_cnt++;
932 push @line, $line;
933 next;
934 }
935
936 last if ($o1_ofs <= $ofs);
937
938 $o_cnt++;
939 $ofs++;
940 if ($line =~ /^ /) {
941 $n_cnt++;
942 }
943 push @line, $line;
944 }
945
946 for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
947 my $line = $this->{TEXT}[$i];
948 if ($line =~ /^\+/) {
949 $n_cnt++;
950 push @line, $line;
951 next;
952 }
953 $ofs++;
954 $o_cnt++;
955 if ($line =~ /^ /) {
956 $n_cnt++;
957 }
958 push @line, $line;
959 }
960 my $head = ("@@ -$o0_ofs" .
961 (($o_cnt != 1) ? ",$o_cnt" : '') .
962 " +$n0_ofs" .
963 (($n_cnt != 1) ? ",$n_cnt" : '') .
964 " @@\n");
965 @{$prev->{TEXT}} = ($head, @line);
966}
967
968sub coalesce_overlapping_hunks {
969 my (@in) = @_;
970 my @out = ();
971
972 my ($last_o_ctx, $last_was_dirty);
973
974 for (grep { $_->{USE} } @in) {
3d792161
TR
975 if ($_->{TYPE} ne 'hunk') {
976 push @out, $_;
977 next;
978 }
7a26e653
JH
979 my $text = $_->{TEXT};
980 my ($o_ofs) = parse_hunk_header($text->[0]);
981 if (defined $last_o_ctx &&
982 $o_ofs <= $last_o_ctx &&
983 !$_->{DIRTY} &&
984 !$last_was_dirty) {
985 merge_hunk($out[-1], $_);
986 }
987 else {
988 push @out, $_;
989 }
990 $last_o_ctx = find_last_o_ctx($out[-1]);
991 $last_was_dirty = $_->{DIRTY};
992 }
993 return @out;
994}
835b2aeb 995
e1327ed5
JK
996sub reassemble_patch {
997 my $head = shift;
998 my @patch;
999
1000 # Include everything in the header except the beginning of the diff.
1001 push @patch, (grep { !/^[-+]{3}/ } @$head);
1002
1003 # Then include any headers from the hunk lines, which must
1004 # come before any actual hunk.
1005 while (@_ && $_[0] !~ /^@/) {
1006 push @patch, shift;
1007 }
1008
1009 # Then begin the diff.
1010 push @patch, grep { /^[-+]{3}/ } @$head;
1011
1012 # And then the actual hunks.
1013 push @patch, @_;
1014
1015 return @patch;
1016}
1017
ac083c47
TR
1018sub color_diff {
1019 return map {
1020 colored((/^@/ ? $fraginfo_color :
1021 /^\+/ ? $diff_new_color :
1022 /^-/ ? $diff_old_color :
1023 $diff_plain_color),
1024 $_);
1025 } @_;
1026}
1027
1028sub edit_hunk_manually {
1029 my ($oldtext) = @_;
1030
1031 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
1032 my $fh;
1033 open $fh, '>', $hunkfile
1034 or die "failed to open hunk edit file for writing: " . $!;
1035 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
1036 print $fh @$oldtext;
8f0bef6d 1037 my $participle = $patch_mode_flavour{PARTICIPLE};
7b8c7051
JDL
1038 my $is_reverse = $patch_mode_flavour{IS_REVERSE};
1039 my ($remove_plus, $remove_minus) = $is_reverse ? ('-', '+') : ('+', '-');
ac083c47
TR
1040 print $fh <<EOF;
1041# ---
7b8c7051
JDL
1042# To remove '$remove_minus' lines, make them ' ' lines (context).
1043# To remove '$remove_plus' lines, delete them.
ac083c47
TR
1044# Lines starting with # will be removed.
1045#
1046# If the patch applies cleanly, the edited hunk will immediately be
8f0bef6d 1047# marked for $participle. If it does not apply cleanly, you will be given
ac083c47
TR
1048# an opportunity to edit again. If all lines of the hunk are removed,
1049# then the edit is aborted and the hunk is left unchanged.
1050EOF
1051 close $fh;
1052
b4479f07 1053 chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR)));
ac083c47
TR
1054 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
1055
1d398a03
DM
1056 if ($? != 0) {
1057 return undef;
1058 }
1059
ac083c47
TR
1060 open $fh, '<', $hunkfile
1061 or die "failed to open hunk edit file for reading: " . $!;
1062 my @newtext = grep { !/^#/ } <$fh>;
1063 close $fh;
1064 unlink $hunkfile;
1065
1066 # Abort if nothing remains
1067 if (!grep { /\S/ } @newtext) {
1068 return undef;
1069 }
1070
1071 # Reinsert the first hunk header if the user accidentally deleted it
1072 if ($newtext[0] !~ /^@/) {
1073 unshift @newtext, $oldtext->[0];
1074 }
1075 return \@newtext;
1076}
1077
1078sub diff_applies {
9dce8323 1079 return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --check',
8f0bef6d 1080 map { @{$_->{TEXT}} } @_);
ac083c47
TR
1081}
1082
ca6ac7f1
TR
1083sub _restore_terminal_and_die {
1084 ReadMode 'restore';
1085 print "\n";
1086 exit 1;
1087}
1088
1089sub prompt_single_character {
1090 if ($use_readkey) {
1091 local $SIG{TERM} = \&_restore_terminal_and_die;
1092 local $SIG{INT} = \&_restore_terminal_and_die;
1093 ReadMode 'cbreak';
1094 my $key = ReadKey 0;
1095 ReadMode 'restore';
b5cc0032
TR
1096 if ($use_termcap and $key eq "\e") {
1097 while (!defined $term_escapes{$key}) {
1098 my $next = ReadKey 0.5;
1099 last if (!defined $next);
1100 $key .= $next;
1101 }
1102 $key =~ s/\e/^[/;
1103 }
ca6ac7f1
TR
1104 print "$key" if defined $key;
1105 print "\n";
1106 return $key;
1107 } else {
1108 return <STDIN>;
1109 }
1110}
1111
ac083c47
TR
1112sub prompt_yesno {
1113 my ($prompt) = @_;
1114 while (1) {
1115 print colored $prompt_color, $prompt;
ca6ac7f1 1116 my $line = prompt_single_character;
ac083c47
TR
1117 return 0 if $line =~ /^n/i;
1118 return 1 if $line =~ /^y/i;
1119 }
1120}
1121
1122sub edit_hunk_loop {
1123 my ($head, $hunk, $ix) = @_;
1124 my $text = $hunk->[$ix]->{TEXT};
1125
1126 while (1) {
1127 $text = edit_hunk_manually($text);
1128 if (!defined $text) {
1129 return undef;
1130 }
0392513f
JK
1131 my $newhunk = {
1132 TEXT => $text,
1133 TYPE => $hunk->[$ix]->{TYPE},
7a26e653
JH
1134 USE => 1,
1135 DIRTY => 1,
0392513f 1136 };
ac083c47
TR
1137 if (diff_applies($head,
1138 @{$hunk}[0..$ix-1],
1139 $newhunk,
1140 @{$hunk}[$ix+1..$#{$hunk}])) {
1141 $newhunk->{DISPLAY} = [color_diff(@{$text})];
1142 return $newhunk;
1143 }
1144 else {
1145 prompt_yesno(
1146 'Your edited hunk does not apply. Edit again '
1147 . '(saying "no" discards!) [y/n]? '
1148 ) or return undef;
1149 }
1150 }
1151}
1152
5cde71d6 1153sub help_patch_cmd {
8f0bef6d
TR
1154 my $verb = lc $patch_mode_flavour{VERB};
1155 my $target = $patch_mode_flavour{TARGET};
1156 print colored $help_color, <<EOF ;
1157y - $verb this hunk$target
1158n - do not $verb this hunk$target
74e42ce1
JN
1159q - quit; do not $verb this hunk nor any of the remaining ones
1160a - $verb this hunk and all later hunks in the file
1161d - do not $verb this hunk nor any of the later hunks in the file
070434d0 1162g - select a hunk to go to
dd971cc9 1163/ - search for a hunk matching the given regex
5cde71d6
JH
1164j - leave this hunk undecided, see next undecided hunk
1165J - leave this hunk undecided, see next hunk
1166k - leave this hunk undecided, see previous undecided hunk
1167K - leave this hunk undecided, see previous hunk
835b2aeb 1168s - split the current hunk into smaller hunks
ac083c47 1169e - manually edit the current hunk
280e50c7 1170? - print help
5cde71d6
JH
1171EOF
1172}
1173
8f0bef6d
TR
1174sub apply_patch {
1175 my $cmd = shift;
9dce8323 1176 my $ret = run_git_apply $cmd, @_;
8f0bef6d
TR
1177 if (!$ret) {
1178 print STDERR @_;
1179 }
1180 return $ret;
1181}
1182
4f353658
TR
1183sub apply_patch_for_checkout_commit {
1184 my $reverse = shift;
9dce8323
JH
1185 my $applies_index = run_git_apply 'apply '.$reverse.' --cached --check', @_;
1186 my $applies_worktree = run_git_apply 'apply '.$reverse.' --check', @_;
4f353658
TR
1187
1188 if ($applies_worktree && $applies_index) {
9dce8323
JH
1189 run_git_apply 'apply '.$reverse.' --cached', @_;
1190 run_git_apply 'apply '.$reverse, @_;
4f353658
TR
1191 return 1;
1192 } elsif (!$applies_index) {
1193 print colored $error_color, "The selected hunks do not apply to the index!\n";
1194 if (prompt_yesno "Apply them to the worktree anyway? ") {
9dce8323 1195 return run_git_apply 'apply '.$reverse, @_;
4f353658
TR
1196 } else {
1197 print colored $error_color, "Nothing was applied.\n";
1198 return 0;
1199 }
1200 } else {
1201 print STDERR @_;
1202 return 0;
1203 }
1204}
1205
5cde71d6 1206sub patch_update_cmd {
8f0bef6d 1207 my @all_mods = list_modified($patch_mode_flavour{FILTER});
4066bd67
JK
1208 error_msg "ignoring unmerged: $_->{VALUE}\n"
1209 for grep { $_->{UNMERGED} } @all_mods;
1210 @all_mods = grep { !$_->{UNMERGED} } @all_mods;
1211
9fe7a643 1212 my @mods = grep { !($_->{BINARY}) } @all_mods;
b63e9950 1213 my @them;
5cde71d6 1214
b63e9950 1215 if (!@mods) {
9fe7a643
TR
1216 if (@all_mods) {
1217 print STDERR "Only binary files changed.\n";
1218 } else {
1219 print STDERR "No changes.\n";
1220 }
b63e9950
WC
1221 return 0;
1222 }
1223 if ($patch_mode) {
1224 @them = @mods;
1225 }
1226 else {
1227 @them = list_and_choose({ PROMPT => 'Patch update',
1228 HEADER => $status_head, },
1229 @mods);
1230 }
12db334e 1231 for (@them) {
9a7a1e03 1232 return 0 if patch_update_file($_->{VALUE});
12db334e 1233 }
a7d9da6c 1234}
5cde71d6 1235
3f6aff68
WP
1236# Generate a one line summary of a hunk.
1237sub summarize_hunk {
1238 my $rhunk = shift;
1239 my $summary = $rhunk->{TEXT}[0];
1240
1241 # Keep the line numbers, discard extra context.
1242 $summary =~ s/@@(.*?)@@.*/$1 /s;
1243 $summary .= " " x (20 - length $summary);
1244
1245 # Add some user context.
1246 for my $line (@{$rhunk->{TEXT}}) {
1247 if ($line =~ m/^[+-].*\w/) {
1248 $summary .= $line;
1249 last;
1250 }
1251 }
1252
1253 chomp $summary;
1254 return substr($summary, 0, 80) . "\n";
1255}
1256
1257
1258# Print a one-line summary of each hunk in the array ref in
41ccfdd9 1259# the first argument, starting with the index in the 2nd.
3f6aff68
WP
1260sub display_hunks {
1261 my ($hunks, $i) = @_;
1262 my $ctr = 0;
1263 $i ||= 0;
1264 for (; $i < @$hunks && $ctr < 20; $i++, $ctr++) {
1265 my $status = " ";
1266 if (defined $hunks->[$i]{USE}) {
1267 $status = $hunks->[$i]{USE} ? "+" : "-";
1268 }
1269 printf "%s%2d: %s",
1270 $status,
1271 $i + 1,
1272 summarize_hunk($hunks->[$i]);
1273 }
1274 return $i;
1275}
1276
a7d9da6c 1277sub patch_update_file {
9a7a1e03 1278 my $quit = 0;
5cde71d6 1279 my ($ix, $num);
a7d9da6c 1280 my $path = shift;
5cde71d6 1281 my ($head, @hunk) = parse_diff($path);
24ab81ae 1282 ($head, my $mode, my $deletion) = parse_diff_header($head);
4af756f3
WC
1283 for (@{$head->{DISPLAY}}) {
1284 print;
1285 }
ca724686
JK
1286
1287 if (@{$mode->{TEXT}}) {
0392513f 1288 unshift @hunk, $mode;
ca724686 1289 }
8947fdd5
JK
1290 if (@{$deletion->{TEXT}}) {
1291 foreach my $hunk (@hunk) {
1292 push @{$deletion->{TEXT}}, @{$hunk->{TEXT}};
1293 push @{$deletion->{DISPLAY}}, @{$hunk->{DISPLAY}};
1294 }
24ab81ae
JK
1295 @hunk = ($deletion);
1296 }
ca724686 1297
5cde71d6
JH
1298 $num = scalar @hunk;
1299 $ix = 0;
1300
1301 while (1) {
835b2aeb 1302 my ($prev, $next, $other, $undecided, $i);
5cde71d6
JH
1303 $other = '';
1304
1305 if ($num <= $ix) {
1306 $ix = 0;
1307 }
835b2aeb 1308 for ($i = 0; $i < $ix; $i++) {
5cde71d6
JH
1309 if (!defined $hunk[$i]{USE}) {
1310 $prev = 1;
57886bc7 1311 $other .= ',k';
5cde71d6
JH
1312 last;
1313 }
1314 }
1315 if ($ix) {
57886bc7 1316 $other .= ',K';
5cde71d6 1317 }
835b2aeb 1318 for ($i = $ix + 1; $i < $num; $i++) {
5cde71d6
JH
1319 if (!defined $hunk[$i]{USE}) {
1320 $next = 1;
57886bc7 1321 $other .= ',j';
5cde71d6
JH
1322 last;
1323 }
1324 }
1325 if ($ix < $num - 1) {
57886bc7 1326 $other .= ',J';
5cde71d6 1327 }
070434d0 1328 if ($num > 1) {
4404b2e3 1329 $other .= ',g';
070434d0 1330 }
835b2aeb 1331 for ($i = 0; $i < $num; $i++) {
5cde71d6
JH
1332 if (!defined $hunk[$i]{USE}) {
1333 $undecided = 1;
1334 last;
1335 }
1336 }
1337 last if (!$undecided);
1338
0392513f
JK
1339 if ($hunk[$ix]{TYPE} eq 'hunk' &&
1340 hunk_splittable($hunk[$ix]{TEXT})) {
57886bc7 1341 $other .= ',s';
835b2aeb 1342 }
0392513f
JK
1343 if ($hunk[$ix]{TYPE} eq 'hunk') {
1344 $other .= ',e';
1345 }
4af756f3
WC
1346 for (@{$hunk[$ix]{DISPLAY}}) {
1347 print;
1348 }
8f0bef6d 1349 print colored $prompt_color, $patch_mode_flavour{VERB},
24ab81ae
JK
1350 ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' :
1351 $hunk[$ix]{TYPE} eq 'deletion' ? ' deletion' :
1352 ' this hunk'),
8f0bef6d 1353 $patch_mode_flavour{TARGET},
a2fc8d65 1354 " [y,n,q,a,d,/$other,?]? ";
ca6ac7f1 1355 my $line = prompt_single_character;
5cde71d6
JH
1356 if ($line) {
1357 if ($line =~ /^y/i) {
1358 $hunk[$ix]{USE} = 1;
1359 }
1360 elsif ($line =~ /^n/i) {
1361 $hunk[$ix]{USE} = 0;
1362 }
1363 elsif ($line =~ /^a/i) {
1364 while ($ix < $num) {
1365 if (!defined $hunk[$ix]{USE}) {
1366 $hunk[$ix]{USE} = 1;
1367 }
1368 $ix++;
1369 }
1370 next;
1371 }
070434d0
WP
1372 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1373 my $response = $1;
1374 my $no = $ix > 10 ? $ix - 10 : 0;
1375 while ($response eq '') {
1376 my $extra = "";
1377 $no = display_hunks(\@hunk, $no);
1378 if ($no < $num) {
1379 $extra = " (<ret> to see more)";
1380 }
1381 print "go to which hunk$extra? ";
1382 $response = <STDIN>;
68c02d7c
TR
1383 if (!defined $response) {
1384 $response = '';
1385 }
070434d0
WP
1386 chomp $response;
1387 }
1388 if ($response !~ /^\s*\d+\s*$/) {
a3019736 1389 error_msg "Invalid number: '$response'\n";
070434d0
WP
1390 } elsif (0 < $response && $response <= $num) {
1391 $ix = $response - 1;
1392 } else {
a3019736 1393 error_msg "Sorry, only $num hunks available.\n";
070434d0
WP
1394 }
1395 next;
1396 }
5cde71d6
JH
1397 elsif ($line =~ /^d/i) {
1398 while ($ix < $num) {
1399 if (!defined $hunk[$ix]{USE}) {
1400 $hunk[$ix]{USE} = 0;
1401 }
1402 $ix++;
1403 }
1404 next;
1405 }
9a7a1e03 1406 elsif ($line =~ /^q/i) {
f5ea3f2b
JH
1407 for ($i = 0; $i < $num; $i++) {
1408 if (!defined $hunk[$i]{USE}) {
1409 $hunk[$i]{USE} = 0;
9a7a1e03 1410 }
9a7a1e03
MM
1411 }
1412 $quit = 1;
f5ea3f2b 1413 last;
9a7a1e03 1414 }
dd971cc9 1415 elsif ($line =~ m|^/(.*)|) {
ca6ac7f1
TR
1416 my $regex = $1;
1417 if ($1 eq "") {
1418 print colored $prompt_color, "search for regex? ";
1419 $regex = <STDIN>;
1420 if (defined $regex) {
1421 chomp $regex;
1422 }
1423 }
dd971cc9
WP
1424 my $search_string;
1425 eval {
ca6ac7f1 1426 $search_string = qr{$regex}m;
dd971cc9
WP
1427 };
1428 if ($@) {
1429 my ($err,$exp) = ($@, $1);
1430 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
a3019736 1431 error_msg "Malformed search regexp $exp: $err\n";
dd971cc9
WP
1432 next;
1433 }
1434 my $iy = $ix;
1435 while (1) {
1436 my $text = join ("", @{$hunk[$iy]{TEXT}});
1437 last if ($text =~ $search_string);
1438 $iy++;
1439 $iy = 0 if ($iy >= $num);
1440 if ($ix == $iy) {
a3019736 1441 error_msg "No hunk matches the given pattern\n";
dd971cc9
WP
1442 last;
1443 }
1444 }
1445 $ix = $iy;
1446 next;
1447 }
ace30ba8
WP
1448 elsif ($line =~ /^K/) {
1449 if ($other =~ /K/) {
1450 $ix--;
1451 }
1452 else {
a3019736 1453 error_msg "No previous hunk\n";
ace30ba8 1454 }
5cde71d6
JH
1455 next;
1456 }
ace30ba8
WP
1457 elsif ($line =~ /^J/) {
1458 if ($other =~ /J/) {
1459 $ix++;
1460 }
1461 else {
a3019736 1462 error_msg "No next hunk\n";
ace30ba8 1463 }
5cde71d6
JH
1464 next;
1465 }
ace30ba8
WP
1466 elsif ($line =~ /^k/) {
1467 if ($other =~ /k/) {
1468 while (1) {
1469 $ix--;
1470 last if (!$ix ||
1471 !defined $hunk[$ix]{USE});
1472 }
1473 }
1474 else {
a3019736 1475 error_msg "No previous hunk\n";
5cde71d6
JH
1476 }
1477 next;
1478 }
ace30ba8
WP
1479 elsif ($line =~ /^j/) {
1480 if ($other !~ /j/) {
a3019736 1481 error_msg "No next hunk\n";
ace30ba8 1482 next;
5cde71d6 1483 }
5cde71d6 1484 }
835b2aeb 1485 elsif ($other =~ /s/ && $line =~ /^s/) {
4af756f3 1486 my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY});
835b2aeb 1487 if (1 < @split) {
b4c61ed6 1488 print colored $header_color, "Split into ",
835b2aeb
JH
1489 scalar(@split), " hunks.\n";
1490 }
4af756f3 1491 splice (@hunk, $ix, 1, @split);
835b2aeb
JH
1492 $num = scalar @hunk;
1493 next;
1494 }
0392513f 1495 elsif ($other =~ /e/ && $line =~ /^e/) {
ac083c47
TR
1496 my $newhunk = edit_hunk_loop($head, \@hunk, $ix);
1497 if (defined $newhunk) {
1498 splice @hunk, $ix, 1, $newhunk;
1499 }
1500 }
5cde71d6
JH
1501 else {
1502 help_patch_cmd($other);
1503 next;
1504 }
1505 # soft increment
1506 while (1) {
1507 $ix++;
1508 last if ($ix >= $num ||
1509 !defined $hunk[$ix]{USE});
1510 }
1511 }
1512 }
1513
7a26e653
JH
1514 @hunk = coalesce_overlapping_hunks(@hunk);
1515
7b40a455 1516 my $n_lofs = 0;
5cde71d6
JH
1517 my @result = ();
1518 for (@hunk) {
8cbd4310
TR
1519 if ($_->{USE}) {
1520 push @result, @{$_->{TEXT}};
5cde71d6
JH
1521 }
1522 }
1523
1524 if (@result) {
e1327ed5 1525 my @patch = reassemble_patch($head->{TEXT}, @result);
8f0bef6d
TR
1526 my $apply_routine = $patch_mode_flavour{APPLY};
1527 &$apply_routine(@patch);
5cde71d6
JH
1528 refresh();
1529 }
1530
1531 print "\n";
9a7a1e03 1532 return $quit;
5cde71d6
JH
1533}
1534
1535sub diff_cmd {
1536 my @mods = list_modified('index-only');
1537 @mods = grep { !($_->{BINARY}) } @mods;
1538 return if (!@mods);
1539 my (@them) = list_and_choose({ PROMPT => 'Review diff',
1540 IMMEDIATE => 1,
1541 HEADER => $status_head, },
1542 @mods);
1543 return if (!@them);
18bc7616
JK
1544 my $reference = is_initial_commit() ? get_empty_tree() : 'HEAD';
1545 system(qw(git diff -p --cached), $reference, '--',
1546 map { $_->{VALUE} } @them);
5cde71d6
JH
1547}
1548
1549sub quit_cmd {
1550 print "Bye.\n";
1551 exit(0);
1552}
1553
1554sub help_cmd {
b4c61ed6 1555 print colored $help_color, <<\EOF ;
5cde71d6
JH
1556status - show paths with changes
1557update - add working tree state to the staged set of changes
1558revert - revert staged set of changes back to the HEAD version
1559patch - pick hunks and update selectively
1560diff - view diff between HEAD and index
1561add untracked - add contents of untracked files to the staged set of changes
1562EOF
1563}
1564
b63e9950
WC
1565sub process_args {
1566 return unless @ARGV;
1567 my $arg = shift @ARGV;
d002ef4d
TR
1568 if ($arg =~ /--patch(?:=(.*))?/) {
1569 if (defined $1) {
1570 if ($1 eq 'reset') {
1571 $patch_mode = 'reset_head';
1572 $patch_mode_revision = 'HEAD';
1573 $arg = shift @ARGV or die "missing --";
1574 if ($arg ne '--') {
1575 $patch_mode_revision = $arg;
1576 $patch_mode = ($arg eq 'HEAD' ?
1577 'reset_head' : 'reset_nothead');
1578 $arg = shift @ARGV or die "missing --";
1579 }
4f353658
TR
1580 } elsif ($1 eq 'checkout') {
1581 $arg = shift @ARGV or die "missing --";
1582 if ($arg eq '--') {
1583 $patch_mode = 'checkout_index';
1584 } else {
1585 $patch_mode_revision = $arg;
1586 $patch_mode = ($arg eq 'HEAD' ?
1587 'checkout_head' : 'checkout_nothead');
1588 $arg = shift @ARGV or die "missing --";
1589 }
dda1f2a5
TR
1590 } elsif ($1 eq 'stage' or $1 eq 'stash') {
1591 $patch_mode = $1;
d002ef4d
TR
1592 $arg = shift @ARGV or die "missing --";
1593 } else {
1594 die "unknown --patch mode: $1";
1595 }
1596 } else {
1597 $patch_mode = 'stage';
1598 $arg = shift @ARGV or die "missing --";
1599 }
b63e9950
WC
1600 die "invalid argument $arg, expecting --"
1601 unless $arg eq "--";
d002ef4d 1602 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
b63e9950
WC
1603 }
1604 elsif ($arg ne "--") {
1605 die "invalid argument $arg, expecting --";
1606 }
1607}
1608
5cde71d6
JH
1609sub main_loop {
1610 my @cmd = ([ 'status', \&status_cmd, ],
1611 [ 'update', \&update_cmd, ],
1612 [ 'revert', \&revert_cmd, ],
1613 [ 'add untracked', \&add_untracked_cmd, ],
1614 [ 'patch', \&patch_update_cmd, ],
1615 [ 'diff', \&diff_cmd, ],
1616 [ 'quit', \&quit_cmd, ],
1617 [ 'help', \&help_cmd, ],
1618 );
1619 while (1) {
1620 my ($it) = list_and_choose({ PROMPT => 'What now',
1621 SINGLETON => 1,
1622 LIST_FLAT => 4,
1623 HEADER => '*** Commands ***',
c95c0248 1624 ON_EOF => \&quit_cmd,
5cde71d6
JH
1625 IMMEDIATE => 1 }, @cmd);
1626 if ($it) {
1627 eval {
1628 $it->[1]->();
1629 };
1630 if ($@) {
1631 print "$@";
1632 }
1633 }
1634 }
1635}
1636
b63e9950 1637process_args();
5cde71d6 1638refresh();
b63e9950
WC
1639if ($patch_mode) {
1640 patch_update_cmd();
1641}
1642else {
1643 status_cmd();
1644 main_loop();
1645}