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