Fossil SCM

Added two (local) options to sbsdiff (allow expansion and max columns limit), though they can't be changed in runtime yet. Added some missing error handling.

jan 2011-10-14 22:21 jan-sbsdiff
Commit 47cdbbf2fb82664ce8150edce8a6a39c5738394c
2 files changed +40 -25 +2 -2
+40 -25
--- src/diff.c
+++ src/diff.c
@@ -579,28 +579,46 @@
579579
free(c.aTo);
580580
return c.aEdit;
581581
}
582582
}
583583
584
+/*
585
+** Copy a line with a limit. Used for side-by-side diffs to enforce a maximum
586
+** line length limit.
587
+*/
588
+static char *copylimline(char *out, DLine *dl, int lim){
589
+ int len;
590
+ len = dl->h & LENGTH_MASK;
591
+ if( lim && len > lim ){
592
+ memcpy(out, dl->z, lim-3);
593
+ strcpy(&out[lim-3], "...");
594
+ }else{
595
+ memcpy(out, dl->z, len);
596
+ out[len] = '\0';
597
+ }
598
+ return out;
599
+}
584600
585601
/*
586602
* References in the fossil repository:
587603
* /vdiff?from=080d27a&to=4b0f813&detail=1
588604
* /vdiff?from=636804745b&to=c1d78e0556&detail=1
589605
* /vdiff?from=c0b6c28d29&to=25169506b7&detail=1
590606
* /vdiff?from=e3d022dffa&to=48bcfbd47b&detail=1
591607
*/
592
-int *html_sbsdiff(
608
+int html_sbsdiff(
593609
Blob *pA_Blob, /* FROM file */
594610
Blob *pB_Blob, /* TO file */
595611
int nContext, /* Amount of context to unified diff */
596612
int ignoreEolWs /* Ignore whitespace at the end of lines */
597613
){
598614
DContext c;
599615
int i;
600616
int iFrom, iTo;
601617
char *linebuf;
618
+ int collim=0; /* Currently not settable; allows a column limit for diffs */
619
+ int allowExp=0; /* Currently not settable; (dis)allow expansion of rows */
602620
603621
/* Prepare the input files */
604622
memset(&c, 0, sizeof(c));
605623
c.aFrom = break_into_lines(blob_str(pA_Blob), blob_size(pA_Blob),
606624
&c.nFrom, ignoreEolWs);
@@ -607,20 +625,25 @@
607625
c.aTo = break_into_lines(blob_str(pB_Blob), blob_size(pB_Blob),
608626
&c.nTo, ignoreEolWs);
609627
if( c.aFrom==0 || c.aTo==0 ){
610628
free(c.aFrom);
611629
free(c.aTo);
612
- /* TODO Error handling */
630
+ @ <p class="generalError" style="white-space: nowrap">cannot compute
631
+ @ difference between binary files</p>
613632
return 0;
614633
}
634
+
635
+ collim = collim < 4 ? 0 : collim;
615636
616637
/* Compute the difference */
617638
diff_all(&c);
618639
619640
linebuf = fossil_malloc(LENGTH_MASK+1);
620641
if( !linebuf ){
621
- /* TODO Handle error */
642
+ free(c.aFrom);
643
+ free(c.aTo);
644
+ return 0;
622645
}
623646
624647
iFrom=iTo=0;
625648
i=0;
626649
while( i<c.nEdit ){
@@ -629,35 +652,35 @@
629652
for( j=0; j<c.aEdit[i]; j++){
630653
int len;
631654
int dist;
632655
633656
/* Hide lines which are copied and are further away from block boundaries
634
- ** then nConext lines. For each block with hidden lines, show a row
657
+ ** than nConext lines. For each block with hidden lines, show a row
635658
** notifying the user about the hidden rows.
636659
*/
637660
if( j<nContext || j>c.aEdit[i]-nContext-1 ){
638661
@ <tr>
639662
}else if( j==nContext && j<c.aEdit[i]-nContext-1 ){
640663
@ <tr>
641664
@ <td class="meta" colspan="5" style="white-space: nowrap;">
642
- @ %d(c.aEdit[i]-2*nContext) hidden line(s).</td>
665
+ @ %d(c.aEdit[i]-2*nContext) hidden lines</td>
643666
@ </tr>
644
- continue;
667
+ if( !allowExp )
668
+ continue;
669
+ @ <tr style="display:none;">
645670
}else{
671
+ if( !allowExp )
672
+ continue;
646673
@ <tr style="display:none;">
647674
}
648675
649
- len = c.aFrom[iFrom+j].h & LENGTH_MASK;
650
- memcpy(linebuf, c.aFrom[iFrom+j].z, len);
651
- linebuf[len] = '\0';
676
+ copylimline(linebuf, &c.aFrom[iFrom+j], collim);
652677
@ <td class="lineno">%d(iFrom+j+1)</td><td>%s(linebuf)</td>
653678
654679
@ <td> </td>
655680
656
- len = c.aTo[iTo+j].h & LENGTH_MASK;
657
- memcpy(linebuf, c.aTo[iTo+j].z, len);
658
- linebuf[len] = '\0';
681
+ copylimline(linebuf, &c.aTo[iTo+j], collim);
659682
@ <td class="lineno">%d(iTo+j+1)</td><td>%s(linebuf)</td>
660683
661684
@ </tr>
662685
}
663686
iFrom+=c.aEdit[i];
@@ -671,25 +694,21 @@
671694
for( j=0; j<lim; j++ ){
672695
int len;
673696
@ <tr>
674697
675698
if( j<c.aEdit[i+1] ){
676
- len = c.aFrom[iFrom+j].h & LENGTH_MASK;
677
- memcpy(linebuf, c.aFrom[iFrom+j].z, len);
678
- linebuf[len] = '\0';
699
+ copylimline(linebuf, &c.aFrom[iFrom+j], collim);
679700
@ <td class="changed lineno">%d(iFrom+j+1)</td>
680701
@ <td class="changed">%s(linebuf)</td>
681702
}else{
682703
@ <td colspan="2"/>
683704
}
684705
685706
@ <td class="changed">|</td>
686707
687708
if( j<c.aEdit[i+2] ){
688
- len = c.aTo[iTo+j].h & LENGTH_MASK;
689
- memcpy(linebuf, c.aTo[iTo+j].z, len);
690
- linebuf[len] = '\0';
709
+ copylimline(linebuf, &c.aTo[iTo+j], collim);
691710
@ <td class="changed lineno">%d(iTo+j+1)</td>
692711
@ <td class="changed">%s(linebuf)</td>
693712
}else{
694713
@ <td colspan="2"/>
695714
}
@@ -703,13 +722,11 @@
703722
/* Process deleted lines */
704723
for( j=0; j<c.aEdit[i+1]; j++ ){
705724
int len;
706725
@ <tr>
707726
708
- len = c.aFrom[iFrom+j].h & LENGTH_MASK;
709
- memcpy(linebuf, c.aFrom[iFrom+j].z, len);
710
- linebuf[len] = '\0';
727
+ copylimline(linebuf, &c.aFrom[iFrom+j], collim);
711728
@ <td class="removed lineno">%d(iFrom+j+1)</td>
712729
@ <td class="removed">%s(linebuf)</td>
713730
714731
@ <td>&lt;</td>
715732
@@ -725,13 +742,11 @@
725742
@ <tr>
726743
@ <td colspan="2"/>
727744
728745
@ <td>&gt;</td>
729746
730
- len = c.aTo[iTo+j].h & LENGTH_MASK;
731
- memcpy(linebuf, c.aTo[iTo+j].z, len);
732
- linebuf[len] = '\0';
747
+ copylimline(linebuf, &c.aTo[iTo+j], collim);
733748
@ <td class="added lineno">%d(iTo+j+1)</td>
734749
@ <td class="added">%s(linebuf)</td>
735750
736751
@ </tr>
737752
}
@@ -743,11 +758,11 @@
743758
744759
free(linebuf);
745760
free(c.aFrom);
746761
free(c.aTo);
747762
free(c.aEdit);
748
- return 0;
763
+ return 1;
749764
}
750765
751766
752767
/*
753768
** COMMAND: test-rawdiff
754769
--- src/diff.c
+++ src/diff.c
@@ -579,28 +579,46 @@
579 free(c.aTo);
580 return c.aEdit;
581 }
582 }
583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584
585 /*
586 * References in the fossil repository:
587 * /vdiff?from=080d27a&to=4b0f813&detail=1
588 * /vdiff?from=636804745b&to=c1d78e0556&detail=1
589 * /vdiff?from=c0b6c28d29&to=25169506b7&detail=1
590 * /vdiff?from=e3d022dffa&to=48bcfbd47b&detail=1
591 */
592 int *html_sbsdiff(
593 Blob *pA_Blob, /* FROM file */
594 Blob *pB_Blob, /* TO file */
595 int nContext, /* Amount of context to unified diff */
596 int ignoreEolWs /* Ignore whitespace at the end of lines */
597 ){
598 DContext c;
599 int i;
600 int iFrom, iTo;
601 char *linebuf;
 
 
602
603 /* Prepare the input files */
604 memset(&c, 0, sizeof(c));
605 c.aFrom = break_into_lines(blob_str(pA_Blob), blob_size(pA_Blob),
606 &c.nFrom, ignoreEolWs);
@@ -607,20 +625,25 @@
607 c.aTo = break_into_lines(blob_str(pB_Blob), blob_size(pB_Blob),
608 &c.nTo, ignoreEolWs);
609 if( c.aFrom==0 || c.aTo==0 ){
610 free(c.aFrom);
611 free(c.aTo);
612 /* TODO Error handling */
 
613 return 0;
614 }
 
 
615
616 /* Compute the difference */
617 diff_all(&c);
618
619 linebuf = fossil_malloc(LENGTH_MASK+1);
620 if( !linebuf ){
621 /* TODO Handle error */
 
 
622 }
623
624 iFrom=iTo=0;
625 i=0;
626 while( i<c.nEdit ){
@@ -629,35 +652,35 @@
629 for( j=0; j<c.aEdit[i]; j++){
630 int len;
631 int dist;
632
633 /* Hide lines which are copied and are further away from block boundaries
634 ** then nConext lines. For each block with hidden lines, show a row
635 ** notifying the user about the hidden rows.
636 */
637 if( j<nContext || j>c.aEdit[i]-nContext-1 ){
638 @ <tr>
639 }else if( j==nContext && j<c.aEdit[i]-nContext-1 ){
640 @ <tr>
641 @ <td class="meta" colspan="5" style="white-space: nowrap;">
642 @ %d(c.aEdit[i]-2*nContext) hidden line(s).</td>
643 @ </tr>
644 continue;
 
 
645 }else{
 
 
646 @ <tr style="display:none;">
647 }
648
649 len = c.aFrom[iFrom+j].h & LENGTH_MASK;
650 memcpy(linebuf, c.aFrom[iFrom+j].z, len);
651 linebuf[len] = '\0';
652 @ <td class="lineno">%d(iFrom+j+1)</td><td>%s(linebuf)</td>
653
654 @ <td> </td>
655
656 len = c.aTo[iTo+j].h & LENGTH_MASK;
657 memcpy(linebuf, c.aTo[iTo+j].z, len);
658 linebuf[len] = '\0';
659 @ <td class="lineno">%d(iTo+j+1)</td><td>%s(linebuf)</td>
660
661 @ </tr>
662 }
663 iFrom+=c.aEdit[i];
@@ -671,25 +694,21 @@
671 for( j=0; j<lim; j++ ){
672 int len;
673 @ <tr>
674
675 if( j<c.aEdit[i+1] ){
676 len = c.aFrom[iFrom+j].h & LENGTH_MASK;
677 memcpy(linebuf, c.aFrom[iFrom+j].z, len);
678 linebuf[len] = '\0';
679 @ <td class="changed lineno">%d(iFrom+j+1)</td>
680 @ <td class="changed">%s(linebuf)</td>
681 }else{
682 @ <td colspan="2"/>
683 }
684
685 @ <td class="changed">|</td>
686
687 if( j<c.aEdit[i+2] ){
688 len = c.aTo[iTo+j].h & LENGTH_MASK;
689 memcpy(linebuf, c.aTo[iTo+j].z, len);
690 linebuf[len] = '\0';
691 @ <td class="changed lineno">%d(iTo+j+1)</td>
692 @ <td class="changed">%s(linebuf)</td>
693 }else{
694 @ <td colspan="2"/>
695 }
@@ -703,13 +722,11 @@
703 /* Process deleted lines */
704 for( j=0; j<c.aEdit[i+1]; j++ ){
705 int len;
706 @ <tr>
707
708 len = c.aFrom[iFrom+j].h & LENGTH_MASK;
709 memcpy(linebuf, c.aFrom[iFrom+j].z, len);
710 linebuf[len] = '\0';
711 @ <td class="removed lineno">%d(iFrom+j+1)</td>
712 @ <td class="removed">%s(linebuf)</td>
713
714 @ <td>&lt;</td>
715
@@ -725,13 +742,11 @@
725 @ <tr>
726 @ <td colspan="2"/>
727
728 @ <td>&gt;</td>
729
730 len = c.aTo[iTo+j].h & LENGTH_MASK;
731 memcpy(linebuf, c.aTo[iTo+j].z, len);
732 linebuf[len] = '\0';
733 @ <td class="added lineno">%d(iTo+j+1)</td>
734 @ <td class="added">%s(linebuf)</td>
735
736 @ </tr>
737 }
@@ -743,11 +758,11 @@
743
744 free(linebuf);
745 free(c.aFrom);
746 free(c.aTo);
747 free(c.aEdit);
748 return 0;
749 }
750
751
752 /*
753 ** COMMAND: test-rawdiff
754
--- src/diff.c
+++ src/diff.c
@@ -579,28 +579,46 @@
579 free(c.aTo);
580 return c.aEdit;
581 }
582 }
583
584 /*
585 ** Copy a line with a limit. Used for side-by-side diffs to enforce a maximum
586 ** line length limit.
587 */
588 static char *copylimline(char *out, DLine *dl, int lim){
589 int len;
590 len = dl->h & LENGTH_MASK;
591 if( lim && len > lim ){
592 memcpy(out, dl->z, lim-3);
593 strcpy(&out[lim-3], "...");
594 }else{
595 memcpy(out, dl->z, len);
596 out[len] = '\0';
597 }
598 return out;
599 }
600
601 /*
602 * References in the fossil repository:
603 * /vdiff?from=080d27a&to=4b0f813&detail=1
604 * /vdiff?from=636804745b&to=c1d78e0556&detail=1
605 * /vdiff?from=c0b6c28d29&to=25169506b7&detail=1
606 * /vdiff?from=e3d022dffa&to=48bcfbd47b&detail=1
607 */
608 int html_sbsdiff(
609 Blob *pA_Blob, /* FROM file */
610 Blob *pB_Blob, /* TO file */
611 int nContext, /* Amount of context to unified diff */
612 int ignoreEolWs /* Ignore whitespace at the end of lines */
613 ){
614 DContext c;
615 int i;
616 int iFrom, iTo;
617 char *linebuf;
618 int collim=0; /* Currently not settable; allows a column limit for diffs */
619 int allowExp=0; /* Currently not settable; (dis)allow expansion of rows */
620
621 /* Prepare the input files */
622 memset(&c, 0, sizeof(c));
623 c.aFrom = break_into_lines(blob_str(pA_Blob), blob_size(pA_Blob),
624 &c.nFrom, ignoreEolWs);
@@ -607,20 +625,25 @@
625 c.aTo = break_into_lines(blob_str(pB_Blob), blob_size(pB_Blob),
626 &c.nTo, ignoreEolWs);
627 if( c.aFrom==0 || c.aTo==0 ){
628 free(c.aFrom);
629 free(c.aTo);
630 @ <p class="generalError" style="white-space: nowrap">cannot compute
631 @ difference between binary files</p>
632 return 0;
633 }
634
635 collim = collim < 4 ? 0 : collim;
636
637 /* Compute the difference */
638 diff_all(&c);
639
640 linebuf = fossil_malloc(LENGTH_MASK+1);
641 if( !linebuf ){
642 free(c.aFrom);
643 free(c.aTo);
644 return 0;
645 }
646
647 iFrom=iTo=0;
648 i=0;
649 while( i<c.nEdit ){
@@ -629,35 +652,35 @@
652 for( j=0; j<c.aEdit[i]; j++){
653 int len;
654 int dist;
655
656 /* Hide lines which are copied and are further away from block boundaries
657 ** than nConext lines. For each block with hidden lines, show a row
658 ** notifying the user about the hidden rows.
659 */
660 if( j<nContext || j>c.aEdit[i]-nContext-1 ){
661 @ <tr>
662 }else if( j==nContext && j<c.aEdit[i]-nContext-1 ){
663 @ <tr>
664 @ <td class="meta" colspan="5" style="white-space: nowrap;">
665 @ %d(c.aEdit[i]-2*nContext) hidden lines</td>
666 @ </tr>
667 if( !allowExp )
668 continue;
669 @ <tr style="display:none;">
670 }else{
671 if( !allowExp )
672 continue;
673 @ <tr style="display:none;">
674 }
675
676 copylimline(linebuf, &c.aFrom[iFrom+j], collim);
 
 
677 @ <td class="lineno">%d(iFrom+j+1)</td><td>%s(linebuf)</td>
678
679 @ <td> </td>
680
681 copylimline(linebuf, &c.aTo[iTo+j], collim);
 
 
682 @ <td class="lineno">%d(iTo+j+1)</td><td>%s(linebuf)</td>
683
684 @ </tr>
685 }
686 iFrom+=c.aEdit[i];
@@ -671,25 +694,21 @@
694 for( j=0; j<lim; j++ ){
695 int len;
696 @ <tr>
697
698 if( j<c.aEdit[i+1] ){
699 copylimline(linebuf, &c.aFrom[iFrom+j], collim);
 
 
700 @ <td class="changed lineno">%d(iFrom+j+1)</td>
701 @ <td class="changed">%s(linebuf)</td>
702 }else{
703 @ <td colspan="2"/>
704 }
705
706 @ <td class="changed">|</td>
707
708 if( j<c.aEdit[i+2] ){
709 copylimline(linebuf, &c.aTo[iTo+j], collim);
 
 
710 @ <td class="changed lineno">%d(iTo+j+1)</td>
711 @ <td class="changed">%s(linebuf)</td>
712 }else{
713 @ <td colspan="2"/>
714 }
@@ -703,13 +722,11 @@
722 /* Process deleted lines */
723 for( j=0; j<c.aEdit[i+1]; j++ ){
724 int len;
725 @ <tr>
726
727 copylimline(linebuf, &c.aFrom[iFrom+j], collim);
 
 
728 @ <td class="removed lineno">%d(iFrom+j+1)</td>
729 @ <td class="removed">%s(linebuf)</td>
730
731 @ <td>&lt;</td>
732
@@ -725,13 +742,11 @@
742 @ <tr>
743 @ <td colspan="2"/>
744
745 @ <td>&gt;</td>
746
747 copylimline(linebuf, &c.aTo[iTo+j], collim);
 
 
748 @ <td class="added lineno">%d(iTo+j+1)</td>
749 @ <td class="added">%s(linebuf)</td>
750
751 @ </tr>
752 }
@@ -743,11 +758,11 @@
758
759 free(linebuf);
760 free(c.aFrom);
761 free(c.aTo);
762 free(c.aEdit);
763 return 1;
764 }
765
766
767 /*
768 ** COMMAND: test-rawdiff
769
+2 -2
--- src/info.c
+++ src/info.c
@@ -358,12 +358,12 @@
358358
@ version <a href="%s(g.zTop)/artifact/%s(zNew)">[%S(zNew)]</a>
359359
}
360360
if( showDiff ){
361361
if( sideBySide ){
362362
@ <table class="sbsdiff">
363
- @ <tr><th colspan="2" class="diffhdr"">Old (%s(zOld))</th><th/>
364
- @ <th colspan="2" class="diffhdr">New (%s(zNew))</th></tr>
363
+ @ <tr><th colspan="2" class="diffhdr">Old (%S(zOld))</th><th/>
364
+ @ <th colspan="2" class="diffhdr">New (%S(zNew))</th></tr>
365365
generate_sbsdiff(zOld, zNew);
366366
@ </table>
367367
}else{
368368
@ <blockquote><pre>
369369
append_diff(zOld, zNew);
370370
--- src/info.c
+++ src/info.c
@@ -358,12 +358,12 @@
358 @ version <a href="%s(g.zTop)/artifact/%s(zNew)">[%S(zNew)]</a>
359 }
360 if( showDiff ){
361 if( sideBySide ){
362 @ <table class="sbsdiff">
363 @ <tr><th colspan="2" class="diffhdr"">Old (%s(zOld))</th><th/>
364 @ <th colspan="2" class="diffhdr">New (%s(zNew))</th></tr>
365 generate_sbsdiff(zOld, zNew);
366 @ </table>
367 }else{
368 @ <blockquote><pre>
369 append_diff(zOld, zNew);
370
--- src/info.c
+++ src/info.c
@@ -358,12 +358,12 @@
358 @ version <a href="%s(g.zTop)/artifact/%s(zNew)">[%S(zNew)]</a>
359 }
360 if( showDiff ){
361 if( sideBySide ){
362 @ <table class="sbsdiff">
363 @ <tr><th colspan="2" class="diffhdr">Old (%S(zOld))</th><th/>
364 @ <th colspan="2" class="diffhdr">New (%S(zNew))</th></tr>
365 generate_sbsdiff(zOld, zNew);
366 @ </table>
367 }else{
368 @ <blockquote><pre>
369 append_diff(zOld, zNew);
370

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button