git-add --interactive: hunk splitting
This adds hunk splitting and recounting the patch lines. The 'patch' subcommand now allows you to split a large hunk at context lines in the middle. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 11, 2006 at 17:09 UTC
835b2aeba74d64e21bba50f31a7c2a16acf24af7
1 file changed
+242
-8
git-add--interactive.perl
+242
-8
@@ -1,5 +1,6 @@
1
#!/usr/bin/perl -w
2
3
+
4
use strict;
5
6
sub run_cmd_pipe {
@@ -340,6 +341,199 @@ sub parse_diff {
341
return @hunk;
342
}
343
344
+sub hunk_splittable {
345
+ my ($text) = @_;
346
+
347
+ my @s = split_hunk($text);
348
+ return (1 < @s);
349
+}
350
+
351
+sub parse_hunk_header {
352
+ my ($line) = @_;
353
+ my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
354
+ $line =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
355
+ return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
356
+}
357
+
358
+sub split_hunk {
359
+ my ($text) = @_;
360
+ my @split = ();
361
+
362
+ # If there are context lines in the middle of a hunk,
363
+ # it can be split, but we would need to take care of
364
+ # overlaps later.
365
+
366
+ my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
367
+ my $hunk_start = 1;
368
+ my $next_hunk_start;
369
+
370
+ OUTER:
371
+ while (1) {
372
+ my $next_hunk_start = undef;
373
+ my $i = $hunk_start - 1;
374
+ my $this = +{
375
+ TEXT => [],
376
+ OLD => $o_ofs,
377
+ NEW => $n_ofs,
378
+ OCNT => 0,
379
+ NCNT => 0,
380
+ ADDDEL => 0,
381
+ POSTCTX => 0,
382
+ };
383
+
384
+ while (++$i < @$text) {
385
+ my $line = $text->[$i];
386
+ if ($line =~ /^ /) {
387
+ if ($this->{ADDDEL} &&
388
+ !defined $next_hunk_start) {
389
+ # We have seen leading context and
390
+ # adds/dels and then here is another
391
+ # context, which is trailing for this
392
+ # split hunk and leading for the next
393
+ # one.
394
+ $next_hunk_start = $i;
395
+ }
396
+ push @{$this->{TEXT}}, $line;
397
+ $this->{OCNT}++;
398
+ $this->{NCNT}++;
399
+ if (defined $next_hunk_start) {
400
+ $this->{POSTCTX}++;
401
+ }
402
+ next;
403
+ }
404
+
405
+ # add/del
406
+ if (defined $next_hunk_start) {
407
+ # We are done with the current hunk and
408
+ # this is the first real change for the
409
+ # next split one.
410
+ $hunk_start = $next_hunk_start;
411
+ $o_ofs = $this->{OLD} + $this->{OCNT};
412
+ $n_ofs = $this->{NEW} + $this->{NCNT};
413
+ $o_ofs -= $this->{POSTCTX};
414
+ $n_ofs -= $this->{POSTCTX};
415
+ push @split, $this;
416
+ redo OUTER;
417
+ }
418
+ push @{$this->{TEXT}}, $line;
419
+ $this->{ADDDEL}++;
420
+ if ($line =~ /^-/) {
421
+ $this->{OCNT}++;
422
+ }
423
+ else {
424
+ $this->{NCNT}++;
425
+ }
426
+ }
427
+
428
+ push @split, $this;
429
+ last;
430
+ }
431
+
432
+ for my $hunk (@split) {
433
+ $o_ofs = $hunk->{OLD};
434
+ $n_ofs = $hunk->{NEW};
435
+ $o_cnt = $hunk->{OCNT};
436
+ $n_cnt = $hunk->{NCNT};
437
+
438
+ my $head = ("@@ -$o_ofs" .
439
+ (($o_cnt != 1) ? ",$o_cnt" : '') .
440
+ " +$n_ofs" .
441
+ (($n_cnt != 1) ? ",$n_cnt" : '') .
442
+ " @@\n");
443
+ unshift @{$hunk->{TEXT}}, $head;
444
+ }
445
+ return map { $_->{TEXT} } @split;
446
+}
447
+
448
+sub find_last_o_ctx {
449
+ my ($it) = @_;
450
+ my $text = $it->{TEXT};
451
+ my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
452
+ my $i = @{$text};
453
+ my $last_o_ctx = $o_ofs + $o_cnt;
454
+ while (0 < --$i) {
455
+ my $line = $text->[$i];
456
+ if ($line =~ /^ /) {
457
+ $last_o_ctx--;
458
+ next;
459
+ }
460
+ last;
461
+ }
462
+ return $last_o_ctx;
463
+}
464
+
465
+sub merge_hunk {
466
+ my ($prev, $this) = @_;
467
+ my ($o0_ofs, $o0_cnt, $n0_ofs, $n0_cnt) =
468
+ parse_hunk_header($prev->{TEXT}[0]);
469
+ my ($o1_ofs, $o1_cnt, $n1_ofs, $n1_cnt) =
470
+ parse_hunk_header($this->{TEXT}[0]);
471
+
472
+ my (@line, $i, $ofs, $o_cnt, $n_cnt);
473
+ $ofs = $o0_ofs;
474
+ $o_cnt = $n_cnt = 0;
475
+ for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
476
+ my $line = $prev->{TEXT}[$i];
477
+ if ($line =~ /^\+/) {
478
+ $n_cnt++;
479
+ push @line, $line;
480
+ next;
481
+ }
482
+
483
+ last if ($o1_ofs <= $ofs);
484
+
485
+ $o_cnt++;
486
+ $ofs++;
487
+ if ($line =~ /^ /) {
488
+ $n_cnt++;
489
+ }
490
+ push @line, $line;
491
+ }
492
+
493
+ for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
494
+ my $line = $this->{TEXT}[$i];
495
+ if ($line =~ /^\+/) {
496
+ $n_cnt++;
497
+ push @line, $line;
498
+ next;
499
+ }
500
+ $ofs++;
501
+ $o_cnt++;
502
+ if ($line =~ /^ /) {
503
+ $n_cnt++;
504
+ }
505
+ push @line, $line;
506
+ }
507
+ my $head = ("@@ -$o0_ofs" .
508
+ (($o_cnt != 1) ? ",$o_cnt" : '') .
509
+ " +$n0_ofs" .
510
+ (($n_cnt != 1) ? ",$n_cnt" : '') .
511
+ " @@\n");
512
+ @{$prev->{TEXT}} = ($head, @line);
513
+}
514
+
515
+sub coalesce_overlapping_hunks {
516
+ my (@in) = @_;
517
+ my @out = ();
518
+
519
+ my ($last_o_ctx);
520
+
521
+ for (grep { $_->{USE} } @in) {
522
+ my $text = $_->{TEXT};
523
+ my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
524
+ parse_hunk_header($text->[0]);
525
+ if (defined $last_o_ctx &&
526
+ $o_ofs <= $last_o_ctx) {
527
+ merge_hunk($out[-1], $_);
528
+ }
529
+ else {
530
+ push @out, $_;
531
+ }
532
+ $last_o_ctx = find_last_o_ctx($out[-1]);
533
+ }
534
+ return @out;
535
+}
536
+
537
sub help_patch_cmd {
538
print <<\EOF ;
539
y - stage this hunk
@@ -350,6 +544,7 @@ j - leave this hunk undecided, see next undecided hunk
544
J - leave this hunk undecided, see next hunk
545
k - leave this hunk undecided, see previous undecided hunk
546
K - leave this hunk undecided, see previous hunk
547
+s - split the current hunk into smaller hunks
548
EOF
549
}
550
@@ -375,13 +570,13 @@ sub patch_update_cmd {
570
$ix = 0;
571
572
while (1) {
378
- my ($prev, $next, $other, $undecided);
573
+ my ($prev, $next, $other, $undecided, $i);
574
$other = '';
575
576
if ($num <= $ix) {
577
$ix = 0;
578
}
384
- for (my $i = 0; $i < $ix; $i++) {
579
+ for ($i = 0; $i < $ix; $i++) {
580
if (!defined $hunk[$i]{USE}) {
581
$prev = 1;
582
$other .= '/k';
@@ -391,7 +586,7 @@ sub patch_update_cmd {
586
if ($ix) {
587
$other .= '/K';
588
}
394
- for (my $i = $ix + 1; $i < $num; $i++) {
589
+ for ($i = $ix + 1; $i < $num; $i++) {
590
if (!defined $hunk[$i]{USE}) {
591
$next = 1;
592
$other .= '/j';
@@ -401,7 +596,7 @@ sub patch_update_cmd {
596
if ($ix < $num - 1) {
597
$other .= '/J';
598
}
404
- for (my $i = 0; $i < $num; $i++) {
599
+ for ($i = 0; $i < $num; $i++) {
600
if (!defined $hunk[$i]{USE}) {
601
$undecided = 1;
602
last;
@@ -409,6 +604,9 @@ sub patch_update_cmd {
604
}
605
last if (!$undecided);
606
607
+ if (hunk_splittable($hunk[$ix]{TEXT})) {
608
+ $other .= '/s';
609
+ }
610
for (@{$hunk[$ix]{TEXT}}) {
611
print;
612
}
@@ -463,6 +661,18 @@ sub patch_update_cmd {
661
}
662
next;
663
}
664
+ elsif ($other =~ /s/ && $line =~ /^s/) {
665
+ my @split = split_hunk($hunk[$ix]{TEXT});
666
+ if (1 < @split) {
667
+ print "Split into ",
668
+ scalar(@split), " hunks.\n";
669
+ }
670
+ splice(@hunk, $ix, 1,
671
+ map { +{ TEXT => $_, USE => undef } }
672
+ @split);
673
+ $num = scalar @hunk;
674
+ next;
675
+ }
676
else {
677
help_patch_cmd($other);
678
next;
@@ -476,17 +686,37 @@ sub patch_update_cmd {
686
}
687
}
688
479
- my ($o_lno, $n_lno);
689
+ @hunk = coalesce_overlapping_hunks(@hunk);
690
+
691
+ my ($o_lofs, $n_lofs) = (0, 0);
692
my @result = ();
693
for (@hunk) {
694
my $text = $_->{TEXT};
695
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
484
- $text->[0] =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
696
+ parse_hunk_header($text->[0]);
697
+
698
if (!$_->{USE}) {
486
- # Adjust offset here.
699
+ if (!defined $o_cnt) { $o_cnt = 1; }
700
+ if (!defined $n_cnt) { $n_cnt = 1; }
701
+
702
+ # We would have added ($n_cnt - $o_cnt) lines
703
+ # to the postimage if we were to use this hunk,
704
+ # but we didn't. So the line number that the next
705
+ # hunk starts at would be shifted by that much.
706
+ $n_lofs -= ($n_cnt - $o_cnt);
707
next;
708
}
709
else {
710
+ if ($n_lofs) {
711
+ $n_ofs += $n_lofs;
712
+ $text->[0] = ("@@ -$o_ofs" .
713
+ ((defined $o_cnt)
714
+ ? ",$o_cnt" : '') .
715
+ " +$n_ofs" .
716
+ ((defined $n_cnt)
717
+ ? ",$n_cnt" : '') .
718
+ " @@\n");
719
+ }
720
for (@$text) {
721
push @result, $_;
722
}
@@ -500,7 +730,11 @@ sub patch_update_cmd {
730
for (@{$head->{TEXT}}, @result) {
731
print $fh $_;
732
}
503
- close $fh;
733
+ if (!close $fh) {
734
+ for (@{$head->{TEXT}}, @result) {
735
+ print STDERR $_;
736
+ }
737
+ }
738
refresh();
739
}
740