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