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