blob: 642cce1ac6e158207f4273d4e1bf1e7928865b18 [file] [log] [blame]
Ævar Arnfjörð Bjarmason3328ace2010-09-24 20:00:53 +00001#!/usr/bin/perl
Junio C Hamano5cde71d2006-12-10 20:55:50 -08002
Ævar Arnfjörð Bjarmasond48b2842010-09-24 20:00:52 +00003use 5.008;
Junio C Hamano5cde71d2006-12-10 20:55:50 -08004use strict;
Ævar Arnfjörð Bjarmason3328ace2010-09-24 20:00:53 +00005use warnings;
Junio C Hamanob4c61ed2007-12-05 00:50:23 -08006use Git;
7
Junio C Hamano8851f482009-02-16 22:43:43 -08008binmode(STDOUT, ":raw");
9
Junio C Hamanob4c61ed2007-12-05 00:50:23 -080010my $repo = Git->repository();
11
Jeff Kingf87e3102008-01-04 03:35:21 -050012my $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 ) : ();
Thomas Rasta3019732009-02-05 09:28:27 +010019my $error_color = ();
20if ($menu_use_color) {
Stephan Beyer9aad6cb2009-02-08 18:40:39 +010021 my $help_color_spec = ($repo->config('color.interactive.help') or
22 'red bold');
Thomas Rasta3019732009-02-05 09:28:27 +010023 $error_color = $repo->get_color('color.interactive.error',
24 $help_color_spec);
25}
Junio C Hamanob4c61ed2007-12-05 00:50:23 -080026
Jeff Kingf87e3102008-01-04 03:35:21 -050027my $diff_use_color = $repo->get_colorbool('color.diff');
28my ($fraginfo_color) =
29 $diff_use_color ? (
30 $repo->get_color('color.diff.frag', 'cyan'),
31 ) : ();
Thomas Rastac083c42008-07-03 00:00:00 +020032my ($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 ) : ();
Junio C Hamanob4c61ed2007-12-05 00:50:23 -080044
Jeff Kingf87e3102008-01-04 03:35:21 -050045my $normal_color = $repo->get_color("", "reset");
Junio C Hamanob4c61ed2007-12-05 00:50:23 -080046
John Keeping2cc0f532013-06-12 19:44:10 +010047my $diff_algorithm = $repo->config('diff.algorithm');
Jeff King46e3d172016-06-16 08:27:29 -040048my $diff_compaction_heuristic = $repo->config_bool('diff.compactionheuristic');
Jeff King01143842016-02-27 00:37:06 -050049my $diff_filter = $repo->config('interactive.difffilter');
John Keeping2cc0f532013-06-12 19:44:10 +010050
Thomas Rastca6ac7f2009-02-05 09:28:26 +010051my $use_readkey = 0;
Thomas Rastb5cc0032011-05-17 17:19:08 +020052my $use_termcap = 0;
53my %term_escapes;
54
Thomas Rast748aa682009-02-06 20:30:01 +010055sub ReadMode;
56sub ReadKey;
Thomas Rastca6ac7f2009-02-05 09:28:26 +010057if ($repo->config_bool("interactive.singlekey")) {
58 eval {
Thomas Rast748aa682009-02-06 20:30:01 +010059 require Term::ReadKey;
60 Term::ReadKey->import;
Thomas Rastca6ac7f2009-02-05 09:28:26 +010061 $use_readkey = 1;
62 };
Simon Ruderichb2940972014-03-03 22:16:12 +010063 if (!$use_readkey) {
64 print STDERR "missing Term::ReadKey, disabling interactive.singlekey\n";
65 }
Thomas Rastb5cc0032011-05-17 17:19:08 +020066 eval {
67 require Term::Cap;
68 my $termcap = Term::Cap->Tgetent;
69 foreach (values %$termcap) {
70 $term_escapes{$_} = 1 if /^\e/;
71 }
72 $use_termcap = 1;
73 };
Thomas Rastca6ac7f2009-02-05 09:28:26 +010074}
75
Junio C Hamanob4c61ed2007-12-05 00:50:23 -080076sub colored {
77 my $color = shift;
78 my $string = join("", @_);
79
Jeff Kingf87e3102008-01-04 03:35:21 -050080 if (defined $color) {
Junio C Hamanob4c61ed2007-12-05 00:50:23 -080081 # Put a color code at the beginning of each line, a reset at the end
82 # color after newlines that are not at the end of the string
83 $string =~ s/(\n+)(.)/$1$color$2/g;
84 # reset before newlines
85 $string =~ s/(\n+)/$normal_color$1/g;
86 # codes at beginning and end (if necessary):
87 $string =~ s/^/$color/;
88 $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
89 }
90 return $string;
91}
Junio C Hamano5cde71d2006-12-10 20:55:50 -080092
Wincent Colaiutab63e9952007-11-25 14:15:42 +010093# command line options
94my $patch_mode;
Thomas Rastd002ef42009-08-15 13:48:31 +020095my $patch_mode_revision;
Wincent Colaiutab63e9952007-11-25 14:15:42 +010096
Thomas Rast8f0bef62009-08-13 14:29:39 +020097sub apply_patch;
Thomas Rast4f353652009-08-15 13:48:30 +020098sub apply_patch_for_checkout_commit;
Thomas Rastdda1f2a2009-08-13 14:29:44 +020099sub apply_patch_for_stash;
Thomas Rast8f0bef62009-08-13 14:29:39 +0200100
101my %patch_modes = (
102 'stage' => {
103 DIFF => 'diff-files -p',
104 APPLY => sub { apply_patch 'apply --cached', @_; },
105 APPLY_CHECK => 'apply --cached',
106 VERB => 'Stage',
107 TARGET => '',
108 PARTICIPLE => 'staging',
109 FILTER => 'file-only',
Jonathan "Duke" Leto7b8c7052010-10-27 17:49:20 -0700110 IS_REVERSE => 0,
Thomas Rast8f0bef62009-08-13 14:29:39 +0200111 },
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200112 'stash' => {
113 DIFF => 'diff-index -p HEAD',
114 APPLY => sub { apply_patch 'apply --cached', @_; },
115 APPLY_CHECK => 'apply --cached',
116 VERB => 'Stash',
117 TARGET => '',
118 PARTICIPLE => 'stashing',
119 FILTER => undef,
Jonathan "Duke" Leto7b8c7052010-10-27 17:49:20 -0700120 IS_REVERSE => 0,
Thomas Rastdda1f2a2009-08-13 14:29:44 +0200121 },
Thomas Rastd002ef42009-08-15 13:48:31 +0200122 'reset_head' => {
123 DIFF => 'diff-index -p --cached',
124 APPLY => sub { apply_patch 'apply -R --cached', @_; },
125 APPLY_CHECK => 'apply -R --cached',
126 VERB => 'Unstage',
127 TARGET => '',
128 PARTICIPLE => 'unstaging',
129 FILTER => 'index-only',
Jonathan "Duke" Leto7b8c7052010-10-27 17:49:20 -0700130 IS_REVERSE => 1,
Thomas Rastd002ef42009-08-15 13:48:31 +0200131 },
132 'reset_nothead' => {
133 DIFF => 'diff-index -R -p --cached',
134 APPLY => sub { apply_patch 'apply --cached', @_; },
135 APPLY_CHECK => 'apply --cached',
136 VERB => 'Apply',
137 TARGET => ' to index',
138 PARTICIPLE => 'applying',
139 FILTER => 'index-only',
Jonathan "Duke" Leto7b8c7052010-10-27 17:49:20 -0700140 IS_REVERSE => 0,
Thomas Rastd002ef42009-08-15 13:48:31 +0200141 },
Thomas Rast4f353652009-08-15 13:48:30 +0200142 'checkout_index' => {
143 DIFF => 'diff-files -p',
144 APPLY => sub { apply_patch 'apply -R', @_; },
145 APPLY_CHECK => 'apply -R',
146 VERB => 'Discard',
147 TARGET => ' from worktree',
148 PARTICIPLE => 'discarding',
149 FILTER => 'file-only',
Jonathan "Duke" Leto7b8c7052010-10-27 17:49:20 -0700150 IS_REVERSE => 1,
Thomas Rast4f353652009-08-15 13:48:30 +0200151 },
152 'checkout_head' => {
153 DIFF => 'diff-index -p',
154 APPLY => sub { apply_patch_for_checkout_commit '-R', @_ },
155 APPLY_CHECK => 'apply -R',
156 VERB => 'Discard',
157 TARGET => ' from index and worktree',
158 PARTICIPLE => 'discarding',
159 FILTER => undef,
Jonathan "Duke" Leto7b8c7052010-10-27 17:49:20 -0700160 IS_REVERSE => 1,
Thomas Rast4f353652009-08-15 13:48:30 +0200161 },
162 'checkout_nothead' => {
163 DIFF => 'diff-index -R -p',
164 APPLY => sub { apply_patch_for_checkout_commit '', @_ },
165 APPLY_CHECK => 'apply',
166 VERB => 'Apply',
167 TARGET => ' to index and worktree',
168 PARTICIPLE => 'applying',
169 FILTER => undef,
Jonathan "Duke" Leto7b8c7052010-10-27 17:49:20 -0700170 IS_REVERSE => 0,
Thomas Rast4f353652009-08-15 13:48:30 +0200171 },
Thomas Rast8f0bef62009-08-13 14:29:39 +0200172);
173
174my %patch_mode_flavour = %{$patch_modes{stage}};
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800175
176sub run_cmd_pipe {
Johannes Sixtdf17e772013-09-04 09:24:47 +0200177 if ($^O eq 'MSWin32') {
Alex Riesen21e97572007-08-01 14:57:43 +0200178 my @invalid = grep {m/[":*]/} @_;
179 die "$^O does not support: @invalid\n" if @invalid;
180 my @args = map { m/ /o ? "\"$_\"": $_ } @_;
181 return qx{@args};
182 } else {
183 my $fh = undef;
184 open($fh, '-|', @_) or die;
185 return <$fh>;
186 }
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800187}
188
189my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir));
190
191if (!defined $GIT_DIR) {
192 exit(1); # rev-parse would have already said "not a git repo"
193}
194chomp($GIT_DIR);
195
Junio C Hamano8851f482009-02-16 22:43:43 -0800196my %cquote_map = (
197 "b" => chr(8),
198 "t" => chr(9),
199 "n" => chr(10),
200 "v" => chr(11),
201 "f" => chr(12),
202 "r" => chr(13),
203 "\\" => "\\",
204 "\042" => "\042",
205);
206
207sub unquote_path {
208 local ($_) = @_;
209 my ($retval, $remainder);
210 if (!/^\042(.*)\042$/) {
211 return $_;
212 }
213 ($_, $retval) = ($1, "");
214 while (/^([^\\]*)\\(.*)$/) {
215 $remainder = $2;
216 $retval .= $1;
217 for ($remainder) {
218 if (/^([0-3][0-7][0-7])(.*)$/) {
219 $retval .= chr(oct($1));
220 $_ = $2;
221 last;
222 }
223 if (/^([\\\042btnvfr])(.*)$/) {
224 $retval .= $cquote_map{$1};
225 $_ = $2;
226 last;
227 }
228 # This is malformed -- just return it as-is for now.
229 return $_[0];
230 }
231 $_ = $remainder;
232 }
233 $retval .= $_;
234 return $retval;
235}
236
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800237sub refresh {
238 my $fh;
Alex Riesen21e97572007-08-01 14:57:43 +0200239 open $fh, 'git update-index --refresh |'
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800240 or die;
241 while (<$fh>) {
242 ;# ignore 'needs update'
243 }
244 close $fh;
245}
246
247sub list_untracked {
248 map {
249 chomp $_;
Junio C Hamano8851f482009-02-16 22:43:43 -0800250 unquote_path($_);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800251 }
Wincent Colaiuta4c841682007-11-22 02:36:24 +0100252 run_cmd_pipe(qw(git ls-files --others --exclude-standard --), @ARGV);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800253}
254
255my $status_fmt = '%12s %12s %s';
256my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
257
Jeff King18bc7612008-02-13 05:50:51 -0500258{
259 my $initial;
260 sub is_initial_commit {
261 $initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
262 unless defined $initial;
263 return $initial;
264 }
265}
266
267sub get_empty_tree {
268 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
269}
270
Jeff King954312a2013-10-25 02:52:30 -0400271sub get_diff_reference {
272 my $ref = shift;
273 if (defined $ref and $ref ne 'HEAD') {
274 return $ref;
275 } elsif (is_initial_commit()) {
276 return get_empty_tree();
277 } else {
278 return 'HEAD';
279 }
280}
281
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800282# Returns list of hashes, contents of each of which are:
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800283# VALUE: pathname
284# BINARY: is a binary path
285# INDEX: is index different from HEAD?
286# FILE: is file different from index?
287# INDEX_ADDDEL: is it add/delete between HEAD and index?
288# FILE_ADDDEL: is it add/delete between index and file?
Jeff King4066bd62012-04-05 08:30:08 -0400289# UNMERGED: is the path unmerged
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800290
291sub list_modified {
292 my ($only) = @_;
293 my (%data, @return);
294 my ($add, $del, $adddel, $file);
Wincent Colaiuta4c841682007-11-22 02:36:24 +0100295 my @tracked = ();
296
297 if (@ARGV) {
298 @tracked = map {
Junio C Hamano8851f482009-02-16 22:43:43 -0800299 chomp $_;
300 unquote_path($_);
Pauli Virtanenb145b212009-10-10 18:51:45 +0300301 } run_cmd_pipe(qw(git ls-files --), @ARGV);
Wincent Colaiuta4c841682007-11-22 02:36:24 +0100302 return if (!@tracked);
303 }
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800304
Jeff King954312a2013-10-25 02:52:30 -0400305 my $reference = get_diff_reference($patch_mode_revision);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800306 for (run_cmd_pipe(qw(git diff-index --cached
Jeff King18bc7612008-02-13 05:50:51 -0500307 --numstat --summary), $reference,
308 '--', @tracked)) {
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800309 if (($add, $del, $file) =
310 /^([-\d]+) ([-\d]+) (.*)/) {
311 my ($change, $bin);
Junio C Hamano8851f482009-02-16 22:43:43 -0800312 $file = unquote_path($file);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800313 if ($add eq '-' && $del eq '-') {
314 $change = 'binary';
315 $bin = 1;
316 }
317 else {
318 $change = "+$add/-$del";
319 }
320 $data{$file} = {
321 INDEX => $change,
322 BINARY => $bin,
323 FILE => 'nothing',
324 }
325 }
326 elsif (($adddel, $file) =
327 /^ (create|delete) mode [0-7]+ (.*)$/) {
Junio C Hamano8851f482009-02-16 22:43:43 -0800328 $file = unquote_path($file);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800329 $data{$file}{INDEX_ADDDEL} = $adddel;
330 }
331 }
332
Jeff King4066bd62012-04-05 08:30:08 -0400333 for (run_cmd_pipe(qw(git diff-files --numstat --summary --raw --), @tracked)) {
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800334 if (($add, $del, $file) =
335 /^([-\d]+) ([-\d]+) (.*)/) {
Junio C Hamano8851f482009-02-16 22:43:43 -0800336 $file = unquote_path($file);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800337 my ($change, $bin);
338 if ($add eq '-' && $del eq '-') {
339 $change = 'binary';
340 $bin = 1;
341 }
342 else {
343 $change = "+$add/-$del";
344 }
345 $data{$file}{FILE} = $change;
346 if ($bin) {
347 $data{$file}{BINARY} = 1;
348 }
349 }
350 elsif (($adddel, $file) =
351 /^ (create|delete) mode [0-7]+ (.*)$/) {
Junio C Hamano8851f482009-02-16 22:43:43 -0800352 $file = unquote_path($file);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800353 $data{$file}{FILE_ADDDEL} = $adddel;
354 }
Jeff King4066bd62012-04-05 08:30:08 -0400355 elsif (/^:[0-7]+ [0-7]+ [0-9a-f]+ [0-9a-f]+ (.) (.*)$/) {
356 $file = unquote_path($2);
357 if (!exists $data{$file}) {
358 $data{$file} = +{
359 INDEX => 'unchanged',
360 BINARY => 0,
361 };
362 }
363 if ($1 eq 'U') {
364 $data{$file}{UNMERGED} = 1;
365 }
366 }
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800367 }
368
369 for (sort keys %data) {
370 my $it = $data{$_};
371
372 if ($only) {
373 if ($only eq 'index-only') {
374 next if ($it->{INDEX} eq 'unchanged');
375 }
376 if ($only eq 'file-only') {
377 next if ($it->{FILE} eq 'nothing');
378 }
379 }
380 push @return, +{
381 VALUE => $_,
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800382 %$it,
383 };
384 }
385 return @return;
386}
387
388sub find_unique {
389 my ($string, @stuff) = @_;
390 my $found = undef;
391 for (my $i = 0; $i < @stuff; $i++) {
392 my $it = $stuff[$i];
393 my $hit = undef;
394 if (ref $it) {
395 if ((ref $it) eq 'ARRAY') {
396 $it = $it->[0];
397 }
398 else {
399 $it = $it->{VALUE};
400 }
401 }
402 eval {
403 if ($it =~ /^$string/) {
404 $hit = 1;
405 };
406 };
407 if (defined $hit && defined $found) {
408 return undef;
409 }
410 if ($hit) {
411 $found = $i + 1;
412 }
413 }
414 return $found;
415}
416
Wincent Colaiuta14cb5032007-11-29 13:00:38 +0100417# inserts string into trie and updates count for each character
418sub update_trie {
419 my ($trie, $string) = @_;
420 foreach (split //, $string) {
421 $trie = $trie->{$_} ||= {COUNT => 0};
422 $trie->{COUNT}++;
423 }
424}
425
426# returns an array of tuples (prefix, remainder)
427sub find_unique_prefixes {
428 my @stuff = @_;
429 my @return = ();
430
431 # any single prefix exceeding the soft limit is omitted
432 # if any prefix exceeds the hard limit all are omitted
433 # 0 indicates no limit
434 my $soft_limit = 0;
435 my $hard_limit = 3;
436
437 # build a trie modelling all possible options
438 my %trie;
439 foreach my $print (@stuff) {
440 if ((ref $print) eq 'ARRAY') {
441 $print = $print->[0];
442 }
Wincent Colaiuta63320982007-12-02 14:44:11 +0100443 elsif ((ref $print) eq 'HASH') {
Wincent Colaiuta14cb5032007-11-29 13:00:38 +0100444 $print = $print->{VALUE};
445 }
446 update_trie(\%trie, $print);
447 push @return, $print;
448 }
449
450 # use the trie to find the unique prefixes
451 for (my $i = 0; $i < @return; $i++) {
452 my $ret = $return[$i];
453 my @letters = split //, $ret;
454 my %search = %trie;
455 my ($prefix, $remainder);
456 my $j;
457 for ($j = 0; $j < @letters; $j++) {
458 my $letter = $letters[$j];
459 if ($search{$letter}{COUNT} == 1) {
460 $prefix = substr $ret, 0, $j + 1;
461 $remainder = substr $ret, $j + 1;
462 last;
463 }
464 else {
465 my $prefix = substr $ret, 0, $j;
466 return ()
467 if ($hard_limit && $j + 1 > $hard_limit);
468 }
469 %search = %{$search{$letter}};
470 }
Junio C Hamano8851f482009-02-16 22:43:43 -0800471 if (ord($letters[0]) > 127 ||
472 ($soft_limit && $j + 1 > $soft_limit)) {
Wincent Colaiuta14cb5032007-11-29 13:00:38 +0100473 $prefix = undef;
474 $remainder = $ret;
475 }
476 $return[$i] = [$prefix, $remainder];
477 }
478 return @return;
479}
480
Wincent Colaiuta63320982007-12-02 14:44:11 +0100481# filters out prefixes which have special meaning to list_and_choose()
482sub is_valid_prefix {
483 my $prefix = shift;
484 return (defined $prefix) &&
485 !($prefix =~ /[\s,]/) && # separators
486 !($prefix =~ /^-/) && # deselection
487 !($prefix =~ /^\d+/) && # selection
Wincent Colaiuta7e018be2007-12-03 09:09:43 +0100488 ($prefix ne '*') && # "all" wildcard
489 ($prefix ne '?'); # prompt help
Wincent Colaiuta63320982007-12-02 14:44:11 +0100490}
491
Wincent Colaiuta14cb5032007-11-29 13:00:38 +0100492# given a prefix/remainder tuple return a string with the prefix highlighted
493# for now use square brackets; later might use ANSI colors (underline, bold)
494sub highlight_prefix {
495 my $prefix = shift;
496 my $remainder = shift;
Junio C Hamanob4c61ed2007-12-05 00:50:23 -0800497
498 if (!defined $prefix) {
499 return $remainder;
500 }
501
502 if (!is_valid_prefix($prefix)) {
503 return "$prefix$remainder";
504 }
505
Jeff Kingf87e3102008-01-04 03:35:21 -0500506 if (!$menu_use_color) {
Junio C Hamanob4c61ed2007-12-05 00:50:23 -0800507 return "[$prefix]$remainder";
508 }
509
510 return "$prompt_color$prefix$normal_color$remainder";
Wincent Colaiuta14cb5032007-11-29 13:00:38 +0100511}
512
Thomas Rasta3019732009-02-05 09:28:27 +0100513sub error_msg {
514 print STDERR colored $error_color, @_;
515}
516
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800517sub list_and_choose {
518 my ($opts, @stuff) = @_;
519 my (@chosen, @return);
Alexander Kuleshova9c46412015-01-22 14:39:44 +0600520 if (!@stuff) {
521 return @return;
522 }
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800523 my $i;
Wincent Colaiuta14cb5032007-11-29 13:00:38 +0100524 my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY};
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800525
526 TOPLOOP:
527 while (1) {
528 my $last_lf = 0;
529
530 if ($opts->{HEADER}) {
531 if (!$opts->{LIST_FLAT}) {
532 print " ";
533 }
Junio C Hamanob4c61ed2007-12-05 00:50:23 -0800534 print colored $header_color, "$opts->{HEADER}\n";
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800535 }
536 for ($i = 0; $i < @stuff; $i++) {
537 my $chosen = $chosen[$i] ? '*' : ' ';
538 my $print = $stuff[$i];
Wincent Colaiuta63320982007-12-02 14:44:11 +0100539 my $ref = ref $print;
540 my $highlighted = highlight_prefix(@{$prefixes[$i]})
541 if @prefixes;
542 if ($ref eq 'ARRAY') {
543 $print = $highlighted || $print->[0];
544 }
545 elsif ($ref eq 'HASH') {
546 my $value = $highlighted || $print->{VALUE};
547 $print = sprintf($status_fmt,
548 $print->{INDEX},
549 $print->{FILE},
550 $value);
551 }
552 else {
553 $print = $highlighted || $print;
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800554 }
555 printf("%s%2d: %s", $chosen, $i+1, $print);
556 if (($opts->{LIST_FLAT}) &&
557 (($i + 1) % ($opts->{LIST_FLAT}))) {
558 print "\t";
559 $last_lf = 0;
560 }
561 else {
562 print "\n";
563 $last_lf = 1;
564 }
565 }
566 if (!$last_lf) {
567 print "\n";
568 }
569
570 return if ($opts->{LIST_ONLY});
571
Junio C Hamanob4c61ed2007-12-05 00:50:23 -0800572 print colored $prompt_color, $opts->{PROMPT};
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800573 if ($opts->{SINGLETON}) {
574 print "> ";
575 }
576 else {
577 print ">> ";
578 }
579 my $line = <STDIN>;
Jean-Luc Herrenc95c0242007-09-26 15:56:19 +0200580 if (!$line) {
581 print "\n";
582 $opts->{ON_EOF}->() if $opts->{ON_EOF};
583 last;
584 }
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800585 chomp $line;
Jean-Luc Herren6a6eb3d2007-09-26 16:05:01 +0200586 last if $line eq '';
Wincent Colaiuta7e018be2007-12-03 09:09:43 +0100587 if ($line eq '?') {
588 $opts->{SINGLETON} ?
589 singleton_prompt_help_cmd() :
590 prompt_help_cmd();
591 next TOPLOOP;
592 }
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800593 for my $choice (split(/[\s,]+/, $line)) {
594 my $choose = 1;
595 my ($bottom, $top);
596
597 # Input that begins with '-'; unchoose
598 if ($choice =~ s/^-//) {
599 $choose = 0;
600 }
Ciaran McCreesh1e5aaa62008-07-14 19:29:37 +0100601 # A range can be specified like 5-7 or 5-.
602 if ($choice =~ /^(\d+)-(\d*)$/) {
603 ($bottom, $top) = ($1, length($2) ? $2 : 1 + @stuff);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800604 }
605 elsif ($choice =~ /^\d+$/) {
606 $bottom = $top = $choice;
607 }
608 elsif ($choice eq '*') {
609 $bottom = 1;
610 $top = 1 + @stuff;
611 }
612 else {
613 $bottom = $top = find_unique($choice, @stuff);
614 if (!defined $bottom) {
Thomas Rasta3019732009-02-05 09:28:27 +0100615 error_msg "Huh ($choice)?\n";
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800616 next TOPLOOP;
617 }
618 }
619 if ($opts->{SINGLETON} && $bottom != $top) {
Thomas Rasta3019732009-02-05 09:28:27 +0100620 error_msg "Huh ($choice)?\n";
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800621 next TOPLOOP;
622 }
623 for ($i = $bottom-1; $i <= $top-1; $i++) {
Jean-Luc Herren6a6eb3d2007-09-26 16:05:01 +0200624 next if (@stuff <= $i || $i < 0);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800625 $chosen[$i] = $choose;
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800626 }
627 }
Junio C Hamano12db3342007-11-22 01:47:13 -0800628 last if ($opts->{IMMEDIATE} || $line eq '*');
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800629 }
630 for ($i = 0; $i < @stuff; $i++) {
631 if ($chosen[$i]) {
632 push @return, $stuff[$i];
633 }
634 }
635 return @return;
636}
637
Wincent Colaiuta7e018be2007-12-03 09:09:43 +0100638sub singleton_prompt_help_cmd {
Junio C Hamanob4c61ed2007-12-05 00:50:23 -0800639 print colored $help_color, <<\EOF ;
Wincent Colaiuta7e018be2007-12-03 09:09:43 +0100640Prompt help:
6411 - select a numbered item
642foo - select item based on unique prefix
643 - (empty) select nothing
644EOF
645}
646
647sub prompt_help_cmd {
Junio C Hamanob4c61ed2007-12-05 00:50:23 -0800648 print colored $help_color, <<\EOF ;
Wincent Colaiuta7e018be2007-12-03 09:09:43 +0100649Prompt help:
6501 - select a single item
6513-5 - select a range of items
6522-3,6-9 - select multiple ranges
653foo - select item based on unique prefix
654-... - unselect specified items
655* - choose all items
656 - (empty) finish selecting
657EOF
658}
659
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800660sub status_cmd {
661 list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
662 list_modified());
663 print "\n";
664}
665
666sub say_n_paths {
667 my $did = shift @_;
668 my $cnt = scalar @_;
669 print "$did ";
670 if (1 < $cnt) {
671 print "$cnt paths\n";
672 }
673 else {
674 print "one path\n";
675 }
676}
677
678sub update_cmd {
679 my @mods = list_modified('file-only');
680 return if (!@mods);
681
682 my @update = list_and_choose({ PROMPT => 'Update',
683 HEADER => $status_head, },
684 @mods);
685 if (@update) {
Junio C Hamanoa4f71122007-02-07 10:56:38 -0800686 system(qw(git update-index --add --remove --),
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800687 map { $_->{VALUE} } @update);
688 say_n_paths('updated', @update);
689 }
690 print "\n";
691}
692
693sub revert_cmd {
694 my @update = list_and_choose({ PROMPT => 'Revert',
695 HEADER => $status_head, },
696 list_modified());
697 if (@update) {
Jeff King18bc7612008-02-13 05:50:51 -0500698 if (is_initial_commit()) {
699 system(qw(git rm --cached),
700 map { $_->{VALUE} } @update);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800701 }
Jeff King18bc7612008-02-13 05:50:51 -0500702 else {
703 my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
704 map { $_->{VALUE} } @update);
705 my $fh;
706 open $fh, '| git update-index --index-info'
707 or die;
708 for (@lines) {
709 print $fh $_;
710 }
711 close($fh);
712 for (@update) {
713 if ($_->{INDEX_ADDDEL} &&
714 $_->{INDEX_ADDDEL} eq 'create') {
715 system(qw(git update-index --force-remove --),
716 $_->{VALUE});
717 print "note: $_->{VALUE} is untracked now.\n";
718 }
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800719 }
720 }
721 refresh();
722 say_n_paths('reverted', @update);
723 }
724 print "\n";
725}
726
727sub add_untracked_cmd {
728 my @add = list_and_choose({ PROMPT => 'Add untracked' },
729 list_untracked());
730 if (@add) {
731 system(qw(git update-index --add --), @add);
732 say_n_paths('added', @add);
Alexander Kuleshova9c46412015-01-22 14:39:44 +0600733 } else {
734 print "No untracked files.\n";
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800735 }
736 print "\n";
737}
738
Thomas Rast8f0bef62009-08-13 14:29:39 +0200739sub run_git_apply {
740 my $cmd = shift;
741 my $fh;
Junio C Hamano933e44d2011-04-06 14:20:57 -0700742 open $fh, '| git ' . $cmd . " --recount --allow-overlap";
Thomas Rast8f0bef62009-08-13 14:29:39 +0200743 print $fh @_;
744 return close $fh;
745}
746
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800747sub parse_diff {
748 my ($path) = @_;
Thomas Rast8f0bef62009-08-13 14:29:39 +0200749 my @diff_cmd = split(" ", $patch_mode_flavour{DIFF});
John Keeping2cc0f532013-06-12 19:44:10 +0100750 if (defined $diff_algorithm) {
Junio C Hamanoe5c29092013-06-23 12:19:05 -0700751 splice @diff_cmd, 1, 0, "--diff-algorithm=${diff_algorithm}";
John Keeping2cc0f532013-06-12 19:44:10 +0100752 }
Jeff King46e3d172016-06-16 08:27:29 -0400753 if ($diff_compaction_heuristic) {
754 splice @diff_cmd, 1, 0, "--compaction-heuristic";
755 }
Thomas Rastd002ef42009-08-15 13:48:31 +0200756 if (defined $patch_mode_revision) {
Jeff King954312a2013-10-25 02:52:30 -0400757 push @diff_cmd, get_diff_reference($patch_mode_revision);
Thomas Rastd002ef42009-08-15 13:48:31 +0200758 }
Thomas Rast8f0bef62009-08-13 14:29:39 +0200759 my @diff = run_cmd_pipe("git", @diff_cmd, "--", $path);
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100760 my @colored = ();
761 if ($diff_use_color) {
Jeff King01143842016-02-27 00:37:06 -0500762 my @display_cmd = ("git", @diff_cmd, qw(--color --), $path);
763 if (defined $diff_filter) {
764 # quotemeta is overkill, but sufficient for shell-quoting
765 my $diff = join(' ', map { quotemeta } @display_cmd);
766 @display_cmd = ("$diff | $diff_filter");
767 }
768
769 @colored = run_cmd_pipe(@display_cmd);
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100770 }
Jeff King03925132009-04-16 03:14:15 -0400771 my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800772
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100773 for (my $i = 0; $i < @diff; $i++) {
774 if ($diff[$i] =~ /^@@ /) {
Jeff King03925132009-04-16 03:14:15 -0400775 push @hunk, { TEXT => [], DISPLAY => [],
776 TYPE => 'hunk' };
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800777 }
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100778 push @{$hunk[-1]{TEXT}}, $diff[$i];
779 push @{$hunk[-1]{DISPLAY}},
Jeff King01143842016-02-27 00:37:06 -0500780 (@colored ? $colored[$i] : $diff[$i]);
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800781 }
782 return @hunk;
783}
784
Jeff Kingb717a622008-03-27 03:30:43 -0400785sub parse_diff_header {
786 my $src = shift;
787
Jeff King03925132009-04-16 03:14:15 -0400788 my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
789 my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
Jeff King24ab81a2009-10-27 20:52:57 -0400790 my $deletion = { TEXT => [], DISPLAY => [], TYPE => 'deletion' };
Jeff Kingb717a622008-03-27 03:30:43 -0400791
792 for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
Jeff King24ab81a2009-10-27 20:52:57 -0400793 my $dest =
794 $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ? $mode :
795 $src->{TEXT}->[$i] =~ /^deleted file/ ? $deletion :
796 $head;
Jeff Kingb717a622008-03-27 03:30:43 -0400797 push @{$dest->{TEXT}}, $src->{TEXT}->[$i];
798 push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];
799 }
Jeff King24ab81a2009-10-27 20:52:57 -0400800 return ($head, $mode, $deletion);
Jeff Kingb717a622008-03-27 03:30:43 -0400801}
802
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800803sub hunk_splittable {
804 my ($text) = @_;
805
806 my @s = split_hunk($text);
807 return (1 < @s);
808}
809
810sub parse_hunk_header {
811 my ($line) = @_;
812 my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
Jean-Luc Herren7288ed82007-10-09 21:29:26 +0200813 $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
814 $o_cnt = 1 unless defined $o_cnt;
815 $n_cnt = 1 unless defined $n_cnt;
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800816 return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
817}
818
819sub split_hunk {
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100820 my ($text, $display) = @_;
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800821 my @split = ();
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100822 if (!defined $display) {
823 $display = $text;
824 }
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800825 # If there are context lines in the middle of a hunk,
826 # it can be split, but we would need to take care of
827 # overlaps later.
828
Jean-Luc Herren7b40a452007-10-09 21:34:17 +0200829 my ($o_ofs, undef, $n_ofs) = parse_hunk_header($text->[0]);
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800830 my $hunk_start = 1;
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800831
832 OUTER:
833 while (1) {
834 my $next_hunk_start = undef;
835 my $i = $hunk_start - 1;
836 my $this = +{
837 TEXT => [],
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100838 DISPLAY => [],
Jeff King03925132009-04-16 03:14:15 -0400839 TYPE => 'hunk',
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800840 OLD => $o_ofs,
841 NEW => $n_ofs,
842 OCNT => 0,
843 NCNT => 0,
844 ADDDEL => 0,
845 POSTCTX => 0,
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100846 USE => undef,
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800847 };
848
849 while (++$i < @$text) {
850 my $line = $text->[$i];
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100851 my $display = $display->[$i];
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800852 if ($line =~ /^ /) {
853 if ($this->{ADDDEL} &&
854 !defined $next_hunk_start) {
855 # We have seen leading context and
856 # adds/dels and then here is another
857 # context, which is trailing for this
858 # split hunk and leading for the next
859 # one.
860 $next_hunk_start = $i;
861 }
862 push @{$this->{TEXT}}, $line;
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100863 push @{$this->{DISPLAY}}, $display;
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800864 $this->{OCNT}++;
865 $this->{NCNT}++;
866 if (defined $next_hunk_start) {
867 $this->{POSTCTX}++;
868 }
869 next;
870 }
871
872 # add/del
873 if (defined $next_hunk_start) {
874 # We are done with the current hunk and
875 # this is the first real change for the
876 # next split one.
877 $hunk_start = $next_hunk_start;
878 $o_ofs = $this->{OLD} + $this->{OCNT};
879 $n_ofs = $this->{NEW} + $this->{NCNT};
880 $o_ofs -= $this->{POSTCTX};
881 $n_ofs -= $this->{POSTCTX};
882 push @split, $this;
883 redo OUTER;
884 }
885 push @{$this->{TEXT}}, $line;
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100886 push @{$this->{DISPLAY}}, $display;
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800887 $this->{ADDDEL}++;
888 if ($line =~ /^-/) {
889 $this->{OCNT}++;
890 }
891 else {
892 $this->{NCNT}++;
893 }
894 }
895
896 push @split, $this;
897 last;
898 }
899
900 for my $hunk (@split) {
901 $o_ofs = $hunk->{OLD};
902 $n_ofs = $hunk->{NEW};
Jean-Luc Herren7b40a452007-10-09 21:34:17 +0200903 my $o_cnt = $hunk->{OCNT};
904 my $n_cnt = $hunk->{NCNT};
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800905
906 my $head = ("@@ -$o_ofs" .
907 (($o_cnt != 1) ? ",$o_cnt" : '') .
908 " +$n_ofs" .
909 (($n_cnt != 1) ? ",$n_cnt" : '') .
910 " @@\n");
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100911 my $display_head = $head;
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800912 unshift @{$hunk->{TEXT}}, $head;
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100913 if ($diff_use_color) {
914 $display_head = colored($fraginfo_color, $head);
915 }
916 unshift @{$hunk->{DISPLAY}}, $display_head;
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800917 }
Wincent Colaiuta4af756f2007-12-07 13:35:10 +0100918 return @split;
Junio C Hamano835b2ae2006-12-11 17:09:26 -0800919}
920
Junio C Hamano7a26e652009-05-16 10:48:23 -0700921sub find_last_o_ctx {
922 my ($it) = @_;
923 my $text = $it->{TEXT};
924 my ($o_ofs, $o_cnt) = parse_hunk_header($text->[0]);
925 my $i = @{$text};
926 my $last_o_ctx = $o_ofs + $o_cnt;
927 while (0 < --$i) {
928 my $line = $text->[$i];
929 if ($line =~ /^ /) {
930 $last_o_ctx--;
931 next;
932 }
933 last;
934 }
935 return $last_o_ctx;
936}
937
938sub merge_hunk {
939 my ($prev, $this) = @_;
940 my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
941 parse_hunk_header($prev->{TEXT}[0]);
942 my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
943 parse_hunk_header($this->{TEXT}[0]);
944
945 my (@line, $i, $ofs, $o_cnt, $n_cnt);
946 $ofs = $o0_ofs;
947 $o_cnt = $n_cnt = 0;
948 for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
949 my $line = $prev->{TEXT}[$i];
950 if ($line =~ /^\+/) {
951 $n_cnt++;
952 push @line, $line;
953 next;
954 }
955
956 last if ($o1_ofs <= $ofs);
957
958 $o_cnt++;
959 $ofs++;
960 if ($line =~ /^ /) {
961 $n_cnt++;
962 }
963 push @line, $line;
964 }
965
966 for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
967 my $line = $this->{TEXT}[$i];
968 if ($line =~ /^\+/) {
969 $n_cnt++;
970 push @line, $line;
971 next;
972 }
973 $ofs++;
974 $o_cnt++;
975 if ($line =~ /^ /) {
976 $n_cnt++;
977 }
978 push @line, $line;
979 }
980 my $head = ("@@ -$o0_ofs" .
981 (($o_cnt != 1) ? ",$o_cnt" : '') .
982 " +$n0_ofs" .
983 (($n_cnt != 1) ? ",$n_cnt" : '') .
984 " @@\n");
985 @{$prev->{TEXT}} = ($head, @line);
986}
987
988sub coalesce_overlapping_hunks {
989 my (@in) = @_;
990 my @out = ();
991
992 my ($last_o_ctx, $last_was_dirty);
993
994 for (grep { $_->{USE} } @in) {
Thomas Rast3d792162009-08-15 15:56:39 +0200995 if ($_->{TYPE} ne 'hunk') {
996 push @out, $_;
997 next;
998 }
Junio C Hamano7a26e652009-05-16 10:48:23 -0700999 my $text = $_->{TEXT};
1000 my ($o_ofs) = parse_hunk_header($text->[0]);
1001 if (defined $last_o_ctx &&
1002 $o_ofs <= $last_o_ctx &&
1003 !$_->{DIRTY} &&
1004 !$last_was_dirty) {
1005 merge_hunk($out[-1], $_);
1006 }
1007 else {
1008 push @out, $_;
1009 }
1010 $last_o_ctx = find_last_o_ctx($out[-1]);
1011 $last_was_dirty = $_->{DIRTY};
1012 }
1013 return @out;
1014}
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001015
Jeff Kinge1327ed2010-02-22 20:05:44 -05001016sub reassemble_patch {
1017 my $head = shift;
1018 my @patch;
1019
1020 # Include everything in the header except the beginning of the diff.
1021 push @patch, (grep { !/^[-+]{3}/ } @$head);
1022
1023 # Then include any headers from the hunk lines, which must
1024 # come before any actual hunk.
1025 while (@_ && $_[0] !~ /^@/) {
1026 push @patch, shift;
1027 }
1028
1029 # Then begin the diff.
1030 push @patch, grep { /^[-+]{3}/ } @$head;
1031
1032 # And then the actual hunks.
1033 push @patch, @_;
1034
1035 return @patch;
1036}
1037
Thomas Rastac083c42008-07-03 00:00:00 +02001038sub color_diff {
1039 return map {
1040 colored((/^@/ ? $fraginfo_color :
1041 /^\+/ ? $diff_new_color :
1042 /^-/ ? $diff_old_color :
1043 $diff_plain_color),
1044 $_);
1045 } @_;
1046}
1047
1048sub edit_hunk_manually {
1049 my ($oldtext) = @_;
1050
1051 my $hunkfile = $repo->repo_path . "/addp-hunk-edit.diff";
1052 my $fh;
1053 open $fh, '>', $hunkfile
1054 or die "failed to open hunk edit file for writing: " . $!;
1055 print $fh "# Manual hunk edit mode -- see bottom for a quick guide\n";
1056 print $fh @$oldtext;
Thomas Rast8f0bef62009-08-13 14:29:39 +02001057 my $participle = $patch_mode_flavour{PARTICIPLE};
Jonathan "Duke" Leto7b8c7052010-10-27 17:49:20 -07001058 my $is_reverse = $patch_mode_flavour{IS_REVERSE};
1059 my ($remove_plus, $remove_minus) = $is_reverse ? ('-', '+') : ('+', '-');
Thomas Rastac083c42008-07-03 00:00:00 +02001060 print $fh <<EOF;
1061# ---
Jonathan "Duke" Leto7b8c7052010-10-27 17:49:20 -07001062# To remove '$remove_minus' lines, make them ' ' lines (context).
1063# To remove '$remove_plus' lines, delete them.
Thomas Rastac083c42008-07-03 00:00:00 +02001064# Lines starting with # will be removed.
1065#
1066# If the patch applies cleanly, the edited hunk will immediately be
Thomas Rast8f0bef62009-08-13 14:29:39 +02001067# marked for $participle. If it does not apply cleanly, you will be given
Thomas Rastac083c42008-07-03 00:00:00 +02001068# an opportunity to edit again. If all lines of the hunk are removed,
1069# then the edit is aborted and the hunk is left unchanged.
1070EOF
1071 close $fh;
1072
Jonathan Niederb4479f02009-10-30 20:42:34 -05001073 chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR)));
Thomas Rastac083c42008-07-03 00:00:00 +02001074 system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
1075
Deskin Miller1d398a02009-02-12 00:19:41 -05001076 if ($? != 0) {
1077 return undef;
1078 }
1079
Thomas Rastac083c42008-07-03 00:00:00 +02001080 open $fh, '<', $hunkfile
1081 or die "failed to open hunk edit file for reading: " . $!;
1082 my @newtext = grep { !/^#/ } <$fh>;
1083 close $fh;
1084 unlink $hunkfile;
1085
1086 # Abort if nothing remains
1087 if (!grep { /\S/ } @newtext) {
1088 return undef;
1089 }
1090
1091 # Reinsert the first hunk header if the user accidentally deleted it
1092 if ($newtext[0] !~ /^@/) {
1093 unshift @newtext, $oldtext->[0];
1094 }
1095 return \@newtext;
1096}
1097
1098sub diff_applies {
Junio C Hamano9dce8322011-04-06 14:12:34 -07001099 return run_git_apply($patch_mode_flavour{APPLY_CHECK} . ' --check',
Thomas Rast8f0bef62009-08-13 14:29:39 +02001100 map { @{$_->{TEXT}} } @_);
Thomas Rastac083c42008-07-03 00:00:00 +02001101}
1102
Thomas Rastca6ac7f2009-02-05 09:28:26 +01001103sub _restore_terminal_and_die {
1104 ReadMode 'restore';
1105 print "\n";
1106 exit 1;
1107}
1108
1109sub prompt_single_character {
1110 if ($use_readkey) {
1111 local $SIG{TERM} = \&_restore_terminal_and_die;
1112 local $SIG{INT} = \&_restore_terminal_and_die;
1113 ReadMode 'cbreak';
1114 my $key = ReadKey 0;
1115 ReadMode 'restore';
Thomas Rastb5cc0032011-05-17 17:19:08 +02001116 if ($use_termcap and $key eq "\e") {
1117 while (!defined $term_escapes{$key}) {
1118 my $next = ReadKey 0.5;
1119 last if (!defined $next);
1120 $key .= $next;
1121 }
1122 $key =~ s/\e/^[/;
1123 }
Thomas Rastca6ac7f2009-02-05 09:28:26 +01001124 print "$key" if defined $key;
1125 print "\n";
1126 return $key;
1127 } else {
1128 return <STDIN>;
1129 }
1130}
1131
Thomas Rastac083c42008-07-03 00:00:00 +02001132sub prompt_yesno {
1133 my ($prompt) = @_;
1134 while (1) {
1135 print colored $prompt_color, $prompt;
Thomas Rastca6ac7f2009-02-05 09:28:26 +01001136 my $line = prompt_single_character;
Thomas Rastac083c42008-07-03 00:00:00 +02001137 return 0 if $line =~ /^n/i;
1138 return 1 if $line =~ /^y/i;
1139 }
1140}
1141
1142sub edit_hunk_loop {
1143 my ($head, $hunk, $ix) = @_;
1144 my $text = $hunk->[$ix]->{TEXT};
1145
1146 while (1) {
1147 $text = edit_hunk_manually($text);
1148 if (!defined $text) {
1149 return undef;
1150 }
Jeff King03925132009-04-16 03:14:15 -04001151 my $newhunk = {
1152 TEXT => $text,
1153 TYPE => $hunk->[$ix]->{TYPE},
Junio C Hamano7a26e652009-05-16 10:48:23 -07001154 USE => 1,
1155 DIRTY => 1,
Jeff King03925132009-04-16 03:14:15 -04001156 };
Thomas Rastac083c42008-07-03 00:00:00 +02001157 if (diff_applies($head,
1158 @{$hunk}[0..$ix-1],
1159 $newhunk,
1160 @{$hunk}[$ix+1..$#{$hunk}])) {
1161 $newhunk->{DISPLAY} = [color_diff(@{$text})];
1162 return $newhunk;
1163 }
1164 else {
1165 prompt_yesno(
1166 'Your edited hunk does not apply. Edit again '
1167 . '(saying "no" discards!) [y/n]? '
1168 ) or return undef;
1169 }
1170 }
1171}
1172
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001173sub help_patch_cmd {
Thomas Rast8f0bef62009-08-13 14:29:39 +02001174 my $verb = lc $patch_mode_flavour{VERB};
1175 my $target = $patch_mode_flavour{TARGET};
1176 print colored $help_color, <<EOF ;
1177y - $verb this hunk$target
1178n - do not $verb this hunk$target
Justin Lebar235e8d52014-03-31 15:11:47 -07001179q - quit; do not $verb this hunk or any of the remaining ones
Jonathan Nieder74e42ce2010-06-12 22:32:51 -05001180a - $verb this hunk and all later hunks in the file
Justin Lebar235e8d52014-03-31 15:11:47 -07001181d - do not $verb this hunk or any of the later hunks in the file
William Pursell070434d2008-12-04 10:22:40 +00001182g - select a hunk to go to
William Purselldd971cc2008-11-27 04:07:57 +00001183/ - search for a hunk matching the given regex
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001184j - leave this hunk undecided, see next undecided hunk
1185J - leave this hunk undecided, see next hunk
1186k - leave this hunk undecided, see previous undecided hunk
1187K - leave this hunk undecided, see previous hunk
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001188s - split the current hunk into smaller hunks
Thomas Rastac083c42008-07-03 00:00:00 +02001189e - manually edit the current hunk
Ralf Wildenhues280e50c2007-11-28 19:21:42 +01001190? - print help
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001191EOF
1192}
1193
Thomas Rast8f0bef62009-08-13 14:29:39 +02001194sub apply_patch {
1195 my $cmd = shift;
Junio C Hamano9dce8322011-04-06 14:12:34 -07001196 my $ret = run_git_apply $cmd, @_;
Thomas Rast8f0bef62009-08-13 14:29:39 +02001197 if (!$ret) {
1198 print STDERR @_;
1199 }
1200 return $ret;
1201}
1202
Thomas Rast4f353652009-08-15 13:48:30 +02001203sub apply_patch_for_checkout_commit {
1204 my $reverse = shift;
Junio C Hamano9dce8322011-04-06 14:12:34 -07001205 my $applies_index = run_git_apply 'apply '.$reverse.' --cached --check', @_;
1206 my $applies_worktree = run_git_apply 'apply '.$reverse.' --check', @_;
Thomas Rast4f353652009-08-15 13:48:30 +02001207
1208 if ($applies_worktree && $applies_index) {
Junio C Hamano9dce8322011-04-06 14:12:34 -07001209 run_git_apply 'apply '.$reverse.' --cached', @_;
1210 run_git_apply 'apply '.$reverse, @_;
Thomas Rast4f353652009-08-15 13:48:30 +02001211 return 1;
1212 } elsif (!$applies_index) {
1213 print colored $error_color, "The selected hunks do not apply to the index!\n";
1214 if (prompt_yesno "Apply them to the worktree anyway? ") {
Junio C Hamano9dce8322011-04-06 14:12:34 -07001215 return run_git_apply 'apply '.$reverse, @_;
Thomas Rast4f353652009-08-15 13:48:30 +02001216 } else {
1217 print colored $error_color, "Nothing was applied.\n";
1218 return 0;
1219 }
1220 } else {
1221 print STDERR @_;
1222 return 0;
1223 }
1224}
1225
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001226sub patch_update_cmd {
Thomas Rast8f0bef62009-08-13 14:29:39 +02001227 my @all_mods = list_modified($patch_mode_flavour{FILTER});
Jeff King4066bd62012-04-05 08:30:08 -04001228 error_msg "ignoring unmerged: $_->{VALUE}\n"
1229 for grep { $_->{UNMERGED} } @all_mods;
1230 @all_mods = grep { !$_->{UNMERGED} } @all_mods;
1231
Thomas Rast9fe7a642008-10-26 20:37:06 +01001232 my @mods = grep { !($_->{BINARY}) } @all_mods;
Wincent Colaiutab63e9952007-11-25 14:15:42 +01001233 my @them;
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001234
Wincent Colaiutab63e9952007-11-25 14:15:42 +01001235 if (!@mods) {
Thomas Rast9fe7a642008-10-26 20:37:06 +01001236 if (@all_mods) {
1237 print STDERR "Only binary files changed.\n";
1238 } else {
1239 print STDERR "No changes.\n";
1240 }
Wincent Colaiutab63e9952007-11-25 14:15:42 +01001241 return 0;
1242 }
1243 if ($patch_mode) {
1244 @them = @mods;
1245 }
1246 else {
1247 @them = list_and_choose({ PROMPT => 'Patch update',
1248 HEADER => $status_head, },
1249 @mods);
1250 }
Junio C Hamano12db3342007-11-22 01:47:13 -08001251 for (@them) {
Matthieu Moy9a7a1e02009-04-10 16:57:01 +02001252 return 0 if patch_update_file($_->{VALUE});
Junio C Hamano12db3342007-11-22 01:47:13 -08001253 }
Wincent Colaiutaa7d9da62007-11-21 13:36:38 +01001254}
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001255
William Pursell3f6aff62008-12-04 10:00:24 +00001256# Generate a one line summary of a hunk.
1257sub summarize_hunk {
1258 my $rhunk = shift;
1259 my $summary = $rhunk->{TEXT}[0];
1260
1261 # Keep the line numbers, discard extra context.
1262 $summary =~ s/@@(.*?)@@.*/$1 /s;
1263 $summary .= " " x (20 - length $summary);
1264
1265 # Add some user context.
1266 for my $line (@{$rhunk->{TEXT}}) {
1267 if ($line =~ m/^[+-].*\w/) {
1268 $summary .= $line;
1269 last;
1270 }
1271 }
1272
1273 chomp $summary;
1274 return substr($summary, 0, 80) . "\n";
1275}
1276
1277
1278# Print a one-line summary of each hunk in the array ref in
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02001279# the first argument, starting with the index in the 2nd.
William Pursell3f6aff62008-12-04 10:00:24 +00001280sub display_hunks {
1281 my ($hunks, $i) = @_;
1282 my $ctr = 0;
1283 $i ||= 0;
1284 for (; $i < @$hunks && $ctr < 20; $i++, $ctr++) {
1285 my $status = " ";
1286 if (defined $hunks->[$i]{USE}) {
1287 $status = $hunks->[$i]{USE} ? "+" : "-";
1288 }
1289 printf "%s%2d: %s",
1290 $status,
1291 $i + 1,
1292 summarize_hunk($hunks->[$i]);
1293 }
1294 return $i;
1295}
1296
Wincent Colaiutaa7d9da62007-11-21 13:36:38 +01001297sub patch_update_file {
Matthieu Moy9a7a1e02009-04-10 16:57:01 +02001298 my $quit = 0;
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001299 my ($ix, $num);
Wincent Colaiutaa7d9da62007-11-21 13:36:38 +01001300 my $path = shift;
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001301 my ($head, @hunk) = parse_diff($path);
Jeff King24ab81a2009-10-27 20:52:57 -04001302 ($head, my $mode, my $deletion) = parse_diff_header($head);
Wincent Colaiuta4af756f2007-12-07 13:35:10 +01001303 for (@{$head->{DISPLAY}}) {
1304 print;
1305 }
Jeff Kingca724682008-03-27 03:32:25 -04001306
1307 if (@{$mode->{TEXT}}) {
Jeff King03925132009-04-16 03:14:15 -04001308 unshift @hunk, $mode;
Jeff Kingca724682008-03-27 03:32:25 -04001309 }
Jeff King8947fdd2009-12-08 02:49:35 -05001310 if (@{$deletion->{TEXT}}) {
1311 foreach my $hunk (@hunk) {
1312 push @{$deletion->{TEXT}}, @{$hunk->{TEXT}};
1313 push @{$deletion->{DISPLAY}}, @{$hunk->{DISPLAY}};
1314 }
Jeff King24ab81a2009-10-27 20:52:57 -04001315 @hunk = ($deletion);
1316 }
Jeff Kingca724682008-03-27 03:32:25 -04001317
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001318 $num = scalar @hunk;
1319 $ix = 0;
1320
1321 while (1) {
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001322 my ($prev, $next, $other, $undecided, $i);
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001323 $other = '';
1324
1325 if ($num <= $ix) {
1326 $ix = 0;
1327 }
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001328 for ($i = 0; $i < $ix; $i++) {
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001329 if (!defined $hunk[$i]{USE}) {
1330 $prev = 1;
William Pursell57886bc2008-11-27 04:07:52 +00001331 $other .= ',k';
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001332 last;
1333 }
1334 }
1335 if ($ix) {
William Pursell57886bc2008-11-27 04:07:52 +00001336 $other .= ',K';
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001337 }
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001338 for ($i = $ix + 1; $i < $num; $i++) {
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001339 if (!defined $hunk[$i]{USE}) {
1340 $next = 1;
William Pursell57886bc2008-11-27 04:07:52 +00001341 $other .= ',j';
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001342 last;
1343 }
1344 }
1345 if ($ix < $num - 1) {
William Pursell57886bc2008-11-27 04:07:52 +00001346 $other .= ',J';
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001347 }
William Pursell070434d2008-12-04 10:22:40 +00001348 if ($num > 1) {
Thomas Rast4404b2e2009-02-02 22:46:28 +01001349 $other .= ',g';
William Pursell070434d2008-12-04 10:22:40 +00001350 }
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001351 for ($i = 0; $i < $num; $i++) {
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001352 if (!defined $hunk[$i]{USE}) {
1353 $undecided = 1;
1354 last;
1355 }
1356 }
1357 last if (!$undecided);
1358
Jeff King03925132009-04-16 03:14:15 -04001359 if ($hunk[$ix]{TYPE} eq 'hunk' &&
1360 hunk_splittable($hunk[$ix]{TEXT})) {
William Pursell57886bc2008-11-27 04:07:52 +00001361 $other .= ',s';
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001362 }
Jeff King03925132009-04-16 03:14:15 -04001363 if ($hunk[$ix]{TYPE} eq 'hunk') {
1364 $other .= ',e';
1365 }
Wincent Colaiuta4af756f2007-12-07 13:35:10 +01001366 for (@{$hunk[$ix]{DISPLAY}}) {
1367 print;
1368 }
Thomas Rast8f0bef62009-08-13 14:29:39 +02001369 print colored $prompt_color, $patch_mode_flavour{VERB},
Jeff King24ab81a2009-10-27 20:52:57 -04001370 ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' :
1371 $hunk[$ix]{TYPE} eq 'deletion' ? ' deletion' :
1372 ' this hunk'),
Thomas Rast8f0bef62009-08-13 14:29:39 +02001373 $patch_mode_flavour{TARGET},
Wincent Colaiutaa2fc8d62009-04-20 11:42:52 +02001374 " [y,n,q,a,d,/$other,?]? ";
Thomas Rastca6ac7f2009-02-05 09:28:26 +01001375 my $line = prompt_single_character;
Jeff Kinga8bec7a2014-12-15 11:35:27 -05001376 last unless defined $line;
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001377 if ($line) {
1378 if ($line =~ /^y/i) {
1379 $hunk[$ix]{USE} = 1;
1380 }
1381 elsif ($line =~ /^n/i) {
1382 $hunk[$ix]{USE} = 0;
1383 }
1384 elsif ($line =~ /^a/i) {
1385 while ($ix < $num) {
1386 if (!defined $hunk[$ix]{USE}) {
1387 $hunk[$ix]{USE} = 1;
1388 }
1389 $ix++;
1390 }
1391 next;
1392 }
William Pursell070434d2008-12-04 10:22:40 +00001393 elsif ($other =~ /g/ && $line =~ /^g(.*)/) {
1394 my $response = $1;
1395 my $no = $ix > 10 ? $ix - 10 : 0;
1396 while ($response eq '') {
1397 my $extra = "";
1398 $no = display_hunks(\@hunk, $no);
1399 if ($no < $num) {
1400 $extra = " (<ret> to see more)";
1401 }
1402 print "go to which hunk$extra? ";
1403 $response = <STDIN>;
Thomas Rast68c02d72009-02-02 22:46:29 +01001404 if (!defined $response) {
1405 $response = '';
1406 }
William Pursell070434d2008-12-04 10:22:40 +00001407 chomp $response;
1408 }
1409 if ($response !~ /^\s*\d+\s*$/) {
Thomas Rasta3019732009-02-05 09:28:27 +01001410 error_msg "Invalid number: '$response'\n";
William Pursell070434d2008-12-04 10:22:40 +00001411 } elsif (0 < $response && $response <= $num) {
1412 $ix = $response - 1;
1413 } else {
Thomas Rasta3019732009-02-05 09:28:27 +01001414 error_msg "Sorry, only $num hunks available.\n";
William Pursell070434d2008-12-04 10:22:40 +00001415 }
1416 next;
1417 }
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001418 elsif ($line =~ /^d/i) {
1419 while ($ix < $num) {
1420 if (!defined $hunk[$ix]{USE}) {
1421 $hunk[$ix]{USE} = 0;
1422 }
1423 $ix++;
1424 }
1425 next;
1426 }
Matthieu Moy9a7a1e02009-04-10 16:57:01 +02001427 elsif ($line =~ /^q/i) {
Junio C Hamanof5ea3f22011-04-29 15:12:32 -07001428 for ($i = 0; $i < $num; $i++) {
1429 if (!defined $hunk[$i]{USE}) {
1430 $hunk[$i]{USE} = 0;
Matthieu Moy9a7a1e02009-04-10 16:57:01 +02001431 }
Matthieu Moy9a7a1e02009-04-10 16:57:01 +02001432 }
1433 $quit = 1;
Junio C Hamanof5ea3f22011-04-29 15:12:32 -07001434 last;
Matthieu Moy9a7a1e02009-04-10 16:57:01 +02001435 }
William Purselldd971cc2008-11-27 04:07:57 +00001436 elsif ($line =~ m|^/(.*)|) {
Thomas Rastca6ac7f2009-02-05 09:28:26 +01001437 my $regex = $1;
1438 if ($1 eq "") {
1439 print colored $prompt_color, "search for regex? ";
1440 $regex = <STDIN>;
1441 if (defined $regex) {
1442 chomp $regex;
1443 }
1444 }
William Purselldd971cc2008-11-27 04:07:57 +00001445 my $search_string;
1446 eval {
Thomas Rastca6ac7f2009-02-05 09:28:26 +01001447 $search_string = qr{$regex}m;
William Purselldd971cc2008-11-27 04:07:57 +00001448 };
1449 if ($@) {
1450 my ($err,$exp) = ($@, $1);
1451 $err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
Thomas Rasta3019732009-02-05 09:28:27 +01001452 error_msg "Malformed search regexp $exp: $err\n";
William Purselldd971cc2008-11-27 04:07:57 +00001453 next;
1454 }
1455 my $iy = $ix;
1456 while (1) {
1457 my $text = join ("", @{$hunk[$iy]{TEXT}});
1458 last if ($text =~ $search_string);
1459 $iy++;
1460 $iy = 0 if ($iy >= $num);
1461 if ($ix == $iy) {
Thomas Rasta3019732009-02-05 09:28:27 +01001462 error_msg "No hunk matches the given pattern\n";
William Purselldd971cc2008-11-27 04:07:57 +00001463 last;
1464 }
1465 }
1466 $ix = $iy;
1467 next;
1468 }
William Pursellace30ba2008-11-27 04:08:03 +00001469 elsif ($line =~ /^K/) {
1470 if ($other =~ /K/) {
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001471 $ix--;
William Pursellace30ba2008-11-27 04:08:03 +00001472 }
1473 else {
Thomas Rasta3019732009-02-05 09:28:27 +01001474 error_msg "No previous hunk\n";
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001475 }
1476 next;
1477 }
William Pursellace30ba2008-11-27 04:08:03 +00001478 elsif ($line =~ /^J/) {
1479 if ($other =~ /J/) {
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001480 $ix++;
William Pursellace30ba2008-11-27 04:08:03 +00001481 }
1482 else {
Thomas Rasta3019732009-02-05 09:28:27 +01001483 error_msg "No next hunk\n";
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001484 }
1485 next;
1486 }
William Pursellace30ba2008-11-27 04:08:03 +00001487 elsif ($line =~ /^k/) {
1488 if ($other =~ /k/) {
1489 while (1) {
1490 $ix--;
1491 last if (!$ix ||
1492 !defined $hunk[$ix]{USE});
1493 }
1494 }
1495 else {
Thomas Rasta3019732009-02-05 09:28:27 +01001496 error_msg "No previous hunk\n";
William Pursellace30ba2008-11-27 04:08:03 +00001497 }
1498 next;
1499 }
1500 elsif ($line =~ /^j/) {
1501 if ($other !~ /j/) {
Thomas Rasta3019732009-02-05 09:28:27 +01001502 error_msg "No next hunk\n";
William Pursellace30ba2008-11-27 04:08:03 +00001503 next;
1504 }
1505 }
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001506 elsif ($other =~ /s/ && $line =~ /^s/) {
Wincent Colaiuta4af756f2007-12-07 13:35:10 +01001507 my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY});
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001508 if (1 < @split) {
Junio C Hamanob4c61ed2007-12-05 00:50:23 -08001509 print colored $header_color, "Split into ",
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001510 scalar(@split), " hunks.\n";
1511 }
Wincent Colaiuta4af756f2007-12-07 13:35:10 +01001512 splice (@hunk, $ix, 1, @split);
Junio C Hamano835b2ae2006-12-11 17:09:26 -08001513 $num = scalar @hunk;
1514 next;
1515 }
Jeff King03925132009-04-16 03:14:15 -04001516 elsif ($other =~ /e/ && $line =~ /^e/) {
Thomas Rastac083c42008-07-03 00:00:00 +02001517 my $newhunk = edit_hunk_loop($head, \@hunk, $ix);
1518 if (defined $newhunk) {
1519 splice @hunk, $ix, 1, $newhunk;
1520 }
1521 }
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001522 else {
1523 help_patch_cmd($other);
1524 next;
1525 }
1526 # soft increment
1527 while (1) {
1528 $ix++;
1529 last if ($ix >= $num ||
1530 !defined $hunk[$ix]{USE});
1531 }
1532 }
1533 }
1534
Junio C Hamano7a26e652009-05-16 10:48:23 -07001535 @hunk = coalesce_overlapping_hunks(@hunk);
1536
Jean-Luc Herren7b40a452007-10-09 21:34:17 +02001537 my $n_lofs = 0;
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001538 my @result = ();
1539 for (@hunk) {
Thomas Rast8cbd4312008-07-02 23:59:16 +02001540 if ($_->{USE}) {
1541 push @result, @{$_->{TEXT}};
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001542 }
1543 }
1544
1545 if (@result) {
Jeff Kinge1327ed2010-02-22 20:05:44 -05001546 my @patch = reassemble_patch($head->{TEXT}, @result);
Thomas Rast8f0bef62009-08-13 14:29:39 +02001547 my $apply_routine = $patch_mode_flavour{APPLY};
1548 &$apply_routine(@patch);
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001549 refresh();
1550 }
1551
1552 print "\n";
Matthieu Moy9a7a1e02009-04-10 16:57:01 +02001553 return $quit;
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001554}
1555
1556sub diff_cmd {
1557 my @mods = list_modified('index-only');
1558 @mods = grep { !($_->{BINARY}) } @mods;
1559 return if (!@mods);
1560 my (@them) = list_and_choose({ PROMPT => 'Review diff',
1561 IMMEDIATE => 1,
1562 HEADER => $status_head, },
1563 @mods);
1564 return if (!@them);
Jeff King18bc7612008-02-13 05:50:51 -05001565 my $reference = is_initial_commit() ? get_empty_tree() : 'HEAD';
1566 system(qw(git diff -p --cached), $reference, '--',
1567 map { $_->{VALUE} } @them);
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001568}
1569
1570sub quit_cmd {
1571 print "Bye.\n";
1572 exit(0);
1573}
1574
1575sub help_cmd {
Junio C Hamanob4c61ed2007-12-05 00:50:23 -08001576 print colored $help_color, <<\EOF ;
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001577status - show paths with changes
1578update - add working tree state to the staged set of changes
1579revert - revert staged set of changes back to the HEAD version
1580patch - pick hunks and update selectively
1581diff - view diff between HEAD and index
1582add untracked - add contents of untracked files to the staged set of changes
1583EOF
1584}
1585
Wincent Colaiutab63e9952007-11-25 14:15:42 +01001586sub process_args {
1587 return unless @ARGV;
1588 my $arg = shift @ARGV;
Thomas Rastd002ef42009-08-15 13:48:31 +02001589 if ($arg =~ /--patch(?:=(.*))?/) {
1590 if (defined $1) {
1591 if ($1 eq 'reset') {
1592 $patch_mode = 'reset_head';
1593 $patch_mode_revision = 'HEAD';
1594 $arg = shift @ARGV or die "missing --";
1595 if ($arg ne '--') {
1596 $patch_mode_revision = $arg;
1597 $patch_mode = ($arg eq 'HEAD' ?
1598 'reset_head' : 'reset_nothead');
1599 $arg = shift @ARGV or die "missing --";
1600 }
Thomas Rast4f353652009-08-15 13:48:30 +02001601 } elsif ($1 eq 'checkout') {
1602 $arg = shift @ARGV or die "missing --";
1603 if ($arg eq '--') {
1604 $patch_mode = 'checkout_index';
1605 } else {
1606 $patch_mode_revision = $arg;
1607 $patch_mode = ($arg eq 'HEAD' ?
1608 'checkout_head' : 'checkout_nothead');
1609 $arg = shift @ARGV or die "missing --";
1610 }
Thomas Rastdda1f2a2009-08-13 14:29:44 +02001611 } elsif ($1 eq 'stage' or $1 eq 'stash') {
1612 $patch_mode = $1;
Thomas Rastd002ef42009-08-15 13:48:31 +02001613 $arg = shift @ARGV or die "missing --";
1614 } else {
1615 die "unknown --patch mode: $1";
1616 }
1617 } else {
1618 $patch_mode = 'stage';
1619 $arg = shift @ARGV or die "missing --";
1620 }
Wincent Colaiutab63e9952007-11-25 14:15:42 +01001621 die "invalid argument $arg, expecting --"
1622 unless $arg eq "--";
Thomas Rastd002ef42009-08-15 13:48:31 +02001623 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
Wincent Colaiutab63e9952007-11-25 14:15:42 +01001624 }
1625 elsif ($arg ne "--") {
1626 die "invalid argument $arg, expecting --";
1627 }
1628}
1629
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001630sub main_loop {
1631 my @cmd = ([ 'status', \&status_cmd, ],
1632 [ 'update', \&update_cmd, ],
1633 [ 'revert', \&revert_cmd, ],
1634 [ 'add untracked', \&add_untracked_cmd, ],
1635 [ 'patch', \&patch_update_cmd, ],
1636 [ 'diff', \&diff_cmd, ],
1637 [ 'quit', \&quit_cmd, ],
1638 [ 'help', \&help_cmd, ],
1639 );
1640 while (1) {
1641 my ($it) = list_and_choose({ PROMPT => 'What now',
1642 SINGLETON => 1,
1643 LIST_FLAT => 4,
1644 HEADER => '*** Commands ***',
Jean-Luc Herrenc95c0242007-09-26 15:56:19 +02001645 ON_EOF => \&quit_cmd,
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001646 IMMEDIATE => 1 }, @cmd);
1647 if ($it) {
1648 eval {
1649 $it->[1]->();
1650 };
1651 if ($@) {
1652 print "$@";
1653 }
1654 }
1655 }
1656}
1657
Wincent Colaiutab63e9952007-11-25 14:15:42 +01001658process_args();
Junio C Hamano5cde71d2006-12-10 20:55:50 -08001659refresh();
Wincent Colaiutab63e9952007-11-25 14:15:42 +01001660if ($patch_mode) {
1661 patch_update_cmd();
1662}
1663else {
1664 status_cmd();
1665 main_loop();
1666}