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