Fossil SCM

Improvements to the diff algorithm. See [forum:/forumpost/515e0d43425d7164|forum thread 515e0d43425d7164] for discussion.

drh 2023-12-07 19:57 trunk merge
Commit a45c7f0a560a90541967f790c4b5e03db754e511299312f842b95a67f1db3f25
2 files changed +2 -2 +18
+2 -2
--- src/diff.c
+++ src/diff.c
@@ -422,11 +422,11 @@
422422
R = p->aEdit;
423423
mxr = p->nEdit;
424424
while( mxr>2 && R[mxr-1]==0 && R[mxr-2]==0 ){ mxr -= 3; }
425425
for(r=0; r<mxr; r += 3*nr){
426426
/* Figure out how many triples to show in a single block */
427
- for(nr=1; R[r+nr*3]>0 && R[r+nr*3]<nContext*2; nr++){}
427
+ for(nr=1; 3*nr<mxr && R[r+nr*3]>0 && R[r+nr*3]<(int)nContext*2; nr++){}
428428
/* printf("r=%d nr=%d\n", r, nr); */
429429
430430
/* For the current block comprising nr triples, figure out
431431
** how many lines of A and B are to be displayed
432432
*/
@@ -2223,11 +2223,11 @@
22232223
mxr = p->nEdit;
22242224
while( mxr>2 && R[mxr-1]==0 && R[mxr-2]==0 ){ mxr -= 3; }
22252225
22262226
for(r=0; r<mxr; r += 3*nr){
22272227
/* Figure out how many triples to show in a single block */
2228
- for(nr=1; R[r+nr*3]>0 && R[r+nr*3]<(int)nContext*2; nr++){}
2228
+ for(nr=1; 3*nr<mxr && R[r+nr*3]>0 && R[r+nr*3]<(int)nContext*2; nr++){}
22292229
22302230
/* If there is a regex, skip this block (generate no diff output)
22312231
** if the regex matches or does not match both insert and delete.
22322232
** Only display the block if one side matches but the other side does
22332233
** not.
22342234
--- src/diff.c
+++ src/diff.c
@@ -422,11 +422,11 @@
422 R = p->aEdit;
423 mxr = p->nEdit;
424 while( mxr>2 && R[mxr-1]==0 && R[mxr-2]==0 ){ mxr -= 3; }
425 for(r=0; r<mxr; r += 3*nr){
426 /* Figure out how many triples to show in a single block */
427 for(nr=1; R[r+nr*3]>0 && R[r+nr*3]<nContext*2; nr++){}
428 /* printf("r=%d nr=%d\n", r, nr); */
429
430 /* For the current block comprising nr triples, figure out
431 ** how many lines of A and B are to be displayed
432 */
@@ -2223,11 +2223,11 @@
2223 mxr = p->nEdit;
2224 while( mxr>2 && R[mxr-1]==0 && R[mxr-2]==0 ){ mxr -= 3; }
2225
2226 for(r=0; r<mxr; r += 3*nr){
2227 /* Figure out how many triples to show in a single block */
2228 for(nr=1; R[r+nr*3]>0 && R[r+nr*3]<(int)nContext*2; nr++){}
2229
2230 /* If there is a regex, skip this block (generate no diff output)
2231 ** if the regex matches or does not match both insert and delete.
2232 ** Only display the block if one side matches but the other side does
2233 ** not.
2234
--- src/diff.c
+++ src/diff.c
@@ -422,11 +422,11 @@
422 R = p->aEdit;
423 mxr = p->nEdit;
424 while( mxr>2 && R[mxr-1]==0 && R[mxr-2]==0 ){ mxr -= 3; }
425 for(r=0; r<mxr; r += 3*nr){
426 /* Figure out how many triples to show in a single block */
427 for(nr=1; 3*nr<mxr && R[r+nr*3]>0 && R[r+nr*3]<(int)nContext*2; nr++){}
428 /* printf("r=%d nr=%d\n", r, nr); */
429
430 /* For the current block comprising nr triples, figure out
431 ** how many lines of A and B are to be displayed
432 */
@@ -2223,11 +2223,11 @@
2223 mxr = p->nEdit;
2224 while( mxr>2 && R[mxr-1]==0 && R[mxr-2]==0 ){ mxr -= 3; }
2225
2226 for(r=0; r<mxr; r += 3*nr){
2227 /* Figure out how many triples to show in a single block */
2228 for(nr=1; 3*nr<mxr && R[r+nr*3]>0 && R[r+nr*3]<(int)nContext*2; nr++){}
2229
2230 /* If there is a regex, skip this block (generate no diff output)
2231 ** if the regex matches or does not match both insert and delete.
2232 ** Only display the block if one side matches but the other side does
2233 ** not.
2234
--- test/diff.test
+++ test/diff.test
@@ -108,9 +108,27 @@
108108
test diff-file5-1 {[normalize_result] eq {Index: file5.dat
109109
==================================================================
110110
--- file5.dat
111111
+++ file5.dat
112112
cannot compute difference between binary files}}
113
+
114
+###############################################################################
115
+
116
+write_file file6a.dat "{\n \"abc\": {\n \"def\": false,\n \"ghi\": false\n }\n}\n"
117
+write_file file6b.dat "{\n \"abc\": {\n \"def\": false,\n \"ghi\": false\n },\n \"jkl\": {\n \"mno\": {\n \"pqr\": false\n }\n }\n}\n"
118
+fossil xdiff -y -W 16 file6a.dat file6b.dat
119
+test diff-file-6-1 {[normalize_result] eq {========== file6a.dat ===== versus ===== file6b.dat =====
120
+ 1 { 1 {
121
+ 2 "abc": { 2 "abc": {
122
+ 3 "def": false, 3 "def": false,
123
+ 4 "ghi": false 4 "ghi": false
124
+ > 5 },
125
+ > 6 "jkl": {
126
+ > 7 "mno": {
127
+ > 8 "pqr": false
128
+ > 9 }
129
+ 5 } 10 }
130
+ 6 } 11 }}}
113131
114132
###############################################################################
115133
116134
test_cleanup
117135
--- test/diff.test
+++ test/diff.test
@@ -108,9 +108,27 @@
108 test diff-file5-1 {[normalize_result] eq {Index: file5.dat
109 ==================================================================
110 --- file5.dat
111 +++ file5.dat
112 cannot compute difference between binary files}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
114 ###############################################################################
115
116 test_cleanup
117
--- test/diff.test
+++ test/diff.test
@@ -108,9 +108,27 @@
108 test diff-file5-1 {[normalize_result] eq {Index: file5.dat
109 ==================================================================
110 --- file5.dat
111 +++ file5.dat
112 cannot compute difference between binary files}}
113
114 ###############################################################################
115
116 write_file file6a.dat "{\n \"abc\": {\n \"def\": false,\n \"ghi\": false\n }\n}\n"
117 write_file file6b.dat "{\n \"abc\": {\n \"def\": false,\n \"ghi\": false\n },\n \"jkl\": {\n \"mno\": {\n \"pqr\": false\n }\n }\n}\n"
118 fossil xdiff -y -W 16 file6a.dat file6b.dat
119 test diff-file-6-1 {[normalize_result] eq {========== file6a.dat ===== versus ===== file6b.dat =====
120 1 { 1 {
121 2 "abc": { 2 "abc": {
122 3 "def": false, 3 "def": false,
123 4 "ghi": false 4 "ghi": false
124 > 5 },
125 > 6 "jkl": {
126 > 7 "mno": {
127 > 8 "pqr": false
128 > 9 }
129 5 } 10 }
130 6 } 11 }}}
131
132 ###############################################################################
133
134 test_cleanup
135

Keyboard Shortcuts

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