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