Fossil SCM

Improved internal interfaces for diff. For --webpage, omit the CSS used only for side-by-side diffs when doing a unified diff.

drh 2021-08-25 20:56 trunk
Commit eb6611c4dc76f4a370b6772bc6df34ca64d605144969fd234173702d60b5b50b
+2 -2
--- src/diff.c
+++ src/diff.c
@@ -2101,17 +2101,17 @@
21012101
}
21022102
diffFlag = diff_options();
21032103
verify_all_options();
21042104
if( g.argc!=4 ) usage("FILE1 FILE2");
21052105
blob_zero(&out);
2106
- diff_header(diffFlag, &out);
2106
+ diff_begin(diffFlag);
21072107
diff_print_filenames(g.argv[2], g.argv[3], diffFlag, &out);
21082108
blob_read_from_file(&a, g.argv[2], ExtFILE);
21092109
blob_read_from_file(&b, g.argv[3], ExtFILE);
21102110
text_diff(&a, &b, &out, pRe, diffFlag);
2111
- diff_footer(diffFlag, &out);
21122111
blob_write_to_file(&out, "-");
2112
+ diff_end(diffFlag, 0);
21132113
re_free(pRe);
21142114
}
21152115
21162116
/**************************************************************************
21172117
** The basic difference engine is above. What follows is the annotation
21182118
--- src/diff.c
+++ src/diff.c
@@ -2101,17 +2101,17 @@
2101 }
2102 diffFlag = diff_options();
2103 verify_all_options();
2104 if( g.argc!=4 ) usage("FILE1 FILE2");
2105 blob_zero(&out);
2106 diff_header(diffFlag, &out);
2107 diff_print_filenames(g.argv[2], g.argv[3], diffFlag, &out);
2108 blob_read_from_file(&a, g.argv[2], ExtFILE);
2109 blob_read_from_file(&b, g.argv[3], ExtFILE);
2110 text_diff(&a, &b, &out, pRe, diffFlag);
2111 diff_footer(diffFlag, &out);
2112 blob_write_to_file(&out, "-");
 
2113 re_free(pRe);
2114 }
2115
2116 /**************************************************************************
2117 ** The basic difference engine is above. What follows is the annotation
2118
--- src/diff.c
+++ src/diff.c
@@ -2101,17 +2101,17 @@
2101 }
2102 diffFlag = diff_options();
2103 verify_all_options();
2104 if( g.argc!=4 ) usage("FILE1 FILE2");
2105 blob_zero(&out);
2106 diff_begin(diffFlag);
2107 diff_print_filenames(g.argv[2], g.argv[3], diffFlag, &out);
2108 blob_read_from_file(&a, g.argv[2], ExtFILE);
2109 blob_read_from_file(&b, g.argv[3], ExtFILE);
2110 text_diff(&a, &b, &out, pRe, diffFlag);
 
2111 blob_write_to_file(&out, "-");
2112 diff_end(diffFlag, 0);
2113 re_free(pRe);
2114 }
2115
2116 /**************************************************************************
2117 ** The basic difference engine is above. What follows is the annotation
2118
+31 -31
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -162,18 +162,13 @@
162162
}
163163
fossil_free(z);
164164
}
165165
166166
/*
167
-** Default header text for diff with --webpage
167
+** Extra CSS for side-by-side diffs
168168
*/
169
-static const char zWebpageHdr[] =
170
-@ <!DOCTYPE html>
171
-@ <html>
172
-@ <head>
173
-@ <meta charset="UTF-8">
174
-@ <style>
169
+static const char zSbsCss[] =
175170
@ table.sbsdiffcols {
176171
@ width: 90%;
177172
@ border-spacing: 0;
178173
@ font-size: xx-small;
179174
@ }
@@ -199,11 +194,22 @@
199194
@ overflow-x: auto;
200195
@ }
201196
@ div.diffmkrcol {
202197
@ padding: 0 1em;
203198
@ }
204
-@ span.diffchng {
199
+;
200
+
201
+/*
202
+** Default header text for diff with --webpage
203
+*/
204
+static const char zWebpageHdr[] =
205
+@ <!DOCTYPE html>
206
+@ <html>
207
+@ <head>
208
+@ <meta charset="UTF-8">
209
+@ <style>
210
+@ %sspan.diffchng {
205211
@ background-color: #c0c0ff;
206212
@ }
207213
@ span.diffadd {
208214
@ background-color: #c0ffc0;
209215
@ }
@@ -226,40 +232,34 @@
226232
@ </body>
227233
@ </html>
228234
;
229235
230236
/*
231
-** Print a header or footer on the overall diff output.
232
-**
233
-** This is only a factor for --webpage, in which case the header
234
-** is the HTML header CSS definitions and the footer is the HTML
235
-** close tags.
237
+** Do preliminary output before computing a diff.
236238
*/
237
-void diff_header(u64 diffFlags, Blob *pOut){
239
+void diff_begin(u64 diffFlags){
238240
if( (diffFlags & DIFF_WEBPAGE)!=0 ){
239
- if( pOut ){
240
- blob_append(pOut, zWebpageHdr, -1);
241
+ const char *zExtra;
242
+ if( diffFlags & DIFF_SIDEBYSIDE ){
243
+ zExtra = zSbsCss;
241244
}else{
242
- fossil_print("%s", zWebpageHdr);
245
+ zExtra = "";
243246
}
247
+ fossil_print(zWebpageHdr/*works-like:"%s"*/, zExtra);
244248
}
245249
}
246
-void diff_footer(u64 diffFlags, Blob *pOut){
250
+
251
+/* Do any final output required by a diff and complete the diff
252
+** process.
253
+*/
254
+void diff_end(u64 diffFlags, int nErr){
247255
if( (diffFlags & DIFF_WEBPAGE)!=0 ){
248256
if( diffFlags & DIFF_SIDEBYSIDE ){
249257
const unsigned char *zJs = builtin_file("sbsdiff.js", 0);
250
- if( pOut ){
251
- blob_appendf(pOut, "<script>\n%s</script>\n", zJs);
252
- }else{
253
- fossil_print("<script>\n%s</script>\n", zJs);
254
- }
255
- }
256
- if( pOut ){
257
- blob_append(pOut, zWebpageEnd, -1);
258
- }else{
259
- fossil_print("%s", zWebpageEnd);
260
- }
258
+ fossil_print("<script>\n%s</script>\n", zJs);
259
+ }
260
+ fossil_print("%s", zWebpageEnd);
261261
}
262262
}
263263
264264
/*
265265
** Show the difference between two files, one in memory and one on disk.
@@ -1058,11 +1058,11 @@
10581058
ridTo);
10591059
if( zFrom==0 ){
10601060
fossil_fatal("check-in %s has no parent", zTo);
10611061
}
10621062
}
1063
- diff_header(diffFlags, 0);
1063
+ diff_begin(diffFlags);
10641064
if( againstUndo ){
10651065
if( db_lget_int("undo_available",0)==0 ){
10661066
fossil_print("No undo or redo is available\n");
10671067
return;
10681068
}
@@ -1086,11 +1086,11 @@
10861086
}
10871087
fossil_free(pFileDir[i].zName);
10881088
}
10891089
fossil_free(pFileDir);
10901090
}
1091
- diff_footer(diffFlags, 0);
1091
+ diff_end(diffFlags, 0);
10921092
if ( diffFlags & DIFF_NUMSTAT ){
10931093
fossil_print("%10d %10d TOTAL over %d changed files\n",
10941094
g.diffCnt[1], g.diffCnt[2], g.diffCnt[0]);
10951095
}
10961096
}
10971097
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -162,18 +162,13 @@
162 }
163 fossil_free(z);
164 }
165
166 /*
167 ** Default header text for diff with --webpage
168 */
169 static const char zWebpageHdr[] =
170 @ <!DOCTYPE html>
171 @ <html>
172 @ <head>
173 @ <meta charset="UTF-8">
174 @ <style>
175 @ table.sbsdiffcols {
176 @ width: 90%;
177 @ border-spacing: 0;
178 @ font-size: xx-small;
179 @ }
@@ -199,11 +194,22 @@
199 @ overflow-x: auto;
200 @ }
201 @ div.diffmkrcol {
202 @ padding: 0 1em;
203 @ }
204 @ span.diffchng {
 
 
 
 
 
 
 
 
 
 
 
205 @ background-color: #c0c0ff;
206 @ }
207 @ span.diffadd {
208 @ background-color: #c0ffc0;
209 @ }
@@ -226,40 +232,34 @@
226 @ </body>
227 @ </html>
228 ;
229
230 /*
231 ** Print a header or footer on the overall diff output.
232 **
233 ** This is only a factor for --webpage, in which case the header
234 ** is the HTML header CSS definitions and the footer is the HTML
235 ** close tags.
236 */
237 void diff_header(u64 diffFlags, Blob *pOut){
238 if( (diffFlags & DIFF_WEBPAGE)!=0 ){
239 if( pOut ){
240 blob_append(pOut, zWebpageHdr, -1);
 
241 }else{
242 fossil_print("%s", zWebpageHdr);
243 }
 
244 }
245 }
246 void diff_footer(u64 diffFlags, Blob *pOut){
 
 
 
 
247 if( (diffFlags & DIFF_WEBPAGE)!=0 ){
248 if( diffFlags & DIFF_SIDEBYSIDE ){
249 const unsigned char *zJs = builtin_file("sbsdiff.js", 0);
250 if( pOut ){
251 blob_appendf(pOut, "<script>\n%s</script>\n", zJs);
252 }else{
253 fossil_print("<script>\n%s</script>\n", zJs);
254 }
255 }
256 if( pOut ){
257 blob_append(pOut, zWebpageEnd, -1);
258 }else{
259 fossil_print("%s", zWebpageEnd);
260 }
261 }
262 }
263
264 /*
265 ** Show the difference between two files, one in memory and one on disk.
@@ -1058,11 +1058,11 @@
1058 ridTo);
1059 if( zFrom==0 ){
1060 fossil_fatal("check-in %s has no parent", zTo);
1061 }
1062 }
1063 diff_header(diffFlags, 0);
1064 if( againstUndo ){
1065 if( db_lget_int("undo_available",0)==0 ){
1066 fossil_print("No undo or redo is available\n");
1067 return;
1068 }
@@ -1086,11 +1086,11 @@
1086 }
1087 fossil_free(pFileDir[i].zName);
1088 }
1089 fossil_free(pFileDir);
1090 }
1091 diff_footer(diffFlags, 0);
1092 if ( diffFlags & DIFF_NUMSTAT ){
1093 fossil_print("%10d %10d TOTAL over %d changed files\n",
1094 g.diffCnt[1], g.diffCnt[2], g.diffCnt[0]);
1095 }
1096 }
1097
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -162,18 +162,13 @@
162 }
163 fossil_free(z);
164 }
165
166 /*
167 ** Extra CSS for side-by-side diffs
168 */
169 static const char zSbsCss[] =
 
 
 
 
 
170 @ table.sbsdiffcols {
171 @ width: 90%;
172 @ border-spacing: 0;
173 @ font-size: xx-small;
174 @ }
@@ -199,11 +194,22 @@
194 @ overflow-x: auto;
195 @ }
196 @ div.diffmkrcol {
197 @ padding: 0 1em;
198 @ }
199 ;
200
201 /*
202 ** Default header text for diff with --webpage
203 */
204 static const char zWebpageHdr[] =
205 @ <!DOCTYPE html>
206 @ <html>
207 @ <head>
208 @ <meta charset="UTF-8">
209 @ <style>
210 @ %sspan.diffchng {
211 @ background-color: #c0c0ff;
212 @ }
213 @ span.diffadd {
214 @ background-color: #c0ffc0;
215 @ }
@@ -226,40 +232,34 @@
232 @ </body>
233 @ </html>
234 ;
235
236 /*
237 ** Do preliminary output before computing a diff.
 
 
 
 
238 */
239 void diff_begin(u64 diffFlags){
240 if( (diffFlags & DIFF_WEBPAGE)!=0 ){
241 const char *zExtra;
242 if( diffFlags & DIFF_SIDEBYSIDE ){
243 zExtra = zSbsCss;
244 }else{
245 zExtra = "";
246 }
247 fossil_print(zWebpageHdr/*works-like:"%s"*/, zExtra);
248 }
249 }
250
251 /* Do any final output required by a diff and complete the diff
252 ** process.
253 */
254 void diff_end(u64 diffFlags, int nErr){
255 if( (diffFlags & DIFF_WEBPAGE)!=0 ){
256 if( diffFlags & DIFF_SIDEBYSIDE ){
257 const unsigned char *zJs = builtin_file("sbsdiff.js", 0);
258 fossil_print("<script>\n%s</script>\n", zJs);
259 }
260 fossil_print("%s", zWebpageEnd);
 
 
 
 
 
 
 
 
261 }
262 }
263
264 /*
265 ** Show the difference between two files, one in memory and one on disk.
@@ -1058,11 +1058,11 @@
1058 ridTo);
1059 if( zFrom==0 ){
1060 fossil_fatal("check-in %s has no parent", zTo);
1061 }
1062 }
1063 diff_begin(diffFlags);
1064 if( againstUndo ){
1065 if( db_lget_int("undo_available",0)==0 ){
1066 fossil_print("No undo or redo is available\n");
1067 return;
1068 }
@@ -1086,11 +1086,11 @@
1086 }
1087 fossil_free(pFileDir[i].zName);
1088 }
1089 fossil_free(pFileDir);
1090 }
1091 diff_end(diffFlags, 0);
1092 if ( diffFlags & DIFF_NUMSTAT ){
1093 fossil_print("%10d %10d TOTAL over %d changed files\n",
1094 g.diffCnt[1], g.diffCnt[2], g.diffCnt[0]);
1095 }
1096 }
1097
+2 -2
--- src/patch.c
+++ src/patch.c
@@ -764,11 +764,11 @@
764764
"in the %s repository", zBaseline, g.zRepositoryName);
765765
}
766766
}
767767
}
768768
769
- diff_header(diffFlags, 0);
769
+ diff_begin(diffFlags);
770770
db_prepare(&q,
771771
"SELECT"
772772
" (SELECT blob.rid FROM blob WHERE blob.uuid=chng.hash),"
773773
" pathname," /* 1: new pathname */
774774
" origname," /* 2: original pathname. Null if not renamed */
@@ -836,11 +836,11 @@
836836
blob_reset(&b);
837837
blob_reset(&delta);
838838
}
839839
}
840840
db_finalize(&q);
841
- diff_footer(diffFlags, 0);
841
+ diff_end(diffFlags, nErr);
842842
if( nErr ) fossil_fatal("abort due to prior errors");
843843
}
844844
845845
846846
/*
847847
--- src/patch.c
+++ src/patch.c
@@ -764,11 +764,11 @@
764 "in the %s repository", zBaseline, g.zRepositoryName);
765 }
766 }
767 }
768
769 diff_header(diffFlags, 0);
770 db_prepare(&q,
771 "SELECT"
772 " (SELECT blob.rid FROM blob WHERE blob.uuid=chng.hash),"
773 " pathname," /* 1: new pathname */
774 " origname," /* 2: original pathname. Null if not renamed */
@@ -836,11 +836,11 @@
836 blob_reset(&b);
837 blob_reset(&delta);
838 }
839 }
840 db_finalize(&q);
841 diff_footer(diffFlags, 0);
842 if( nErr ) fossil_fatal("abort due to prior errors");
843 }
844
845
846 /*
847
--- src/patch.c
+++ src/patch.c
@@ -764,11 +764,11 @@
764 "in the %s repository", zBaseline, g.zRepositoryName);
765 }
766 }
767 }
768
769 diff_begin(diffFlags);
770 db_prepare(&q,
771 "SELECT"
772 " (SELECT blob.rid FROM blob WHERE blob.uuid=chng.hash),"
773 " pathname," /* 1: new pathname */
774 " origname," /* 2: original pathname. Null if not renamed */
@@ -836,11 +836,11 @@
836 blob_reset(&b);
837 blob_reset(&delta);
838 }
839 }
840 db_finalize(&q);
841 diff_end(diffFlags, nErr);
842 if( nErr ) fossil_fatal("abort due to prior errors");
843 }
844
845
846 /*
847
+2 -2
--- src/stash.c
+++ src/stash.c
@@ -412,11 +412,11 @@
412412
){
413413
Stmt q;
414414
Blob empty;
415415
int bWebpage = (diffFlags & DIFF_WEBPAGE)!=0;
416416
blob_zero(&empty);
417
- diff_header(diffFlags, 0);
417
+ diff_begin(diffFlags);
418418
db_prepare(&q,
419419
"SELECT blob.rid, isRemoved, isExec, isLink, origname, newname, delta"
420420
" FROM stashfile, blob WHERE stashid=%d AND blob.uuid=stashfile.hash",
421421
stashid
422422
);
@@ -475,11 +475,11 @@
475475
}
476476
blob_reset(&delta);
477477
}
478478
}
479479
db_finalize(&q);
480
- diff_footer(diffFlags, 0);
480
+ diff_end(diffFlags, 0);
481481
}
482482
483483
/*
484484
** Drop the indicated stash
485485
*/
486486
--- src/stash.c
+++ src/stash.c
@@ -412,11 +412,11 @@
412 ){
413 Stmt q;
414 Blob empty;
415 int bWebpage = (diffFlags & DIFF_WEBPAGE)!=0;
416 blob_zero(&empty);
417 diff_header(diffFlags, 0);
418 db_prepare(&q,
419 "SELECT blob.rid, isRemoved, isExec, isLink, origname, newname, delta"
420 " FROM stashfile, blob WHERE stashid=%d AND blob.uuid=stashfile.hash",
421 stashid
422 );
@@ -475,11 +475,11 @@
475 }
476 blob_reset(&delta);
477 }
478 }
479 db_finalize(&q);
480 diff_footer(diffFlags, 0);
481 }
482
483 /*
484 ** Drop the indicated stash
485 */
486
--- src/stash.c
+++ src/stash.c
@@ -412,11 +412,11 @@
412 ){
413 Stmt q;
414 Blob empty;
415 int bWebpage = (diffFlags & DIFF_WEBPAGE)!=0;
416 blob_zero(&empty);
417 diff_begin(diffFlags);
418 db_prepare(&q,
419 "SELECT blob.rid, isRemoved, isExec, isLink, origname, newname, delta"
420 " FROM stashfile, blob WHERE stashid=%d AND blob.uuid=stashfile.hash",
421 stashid
422 );
@@ -475,11 +475,11 @@
475 }
476 blob_reset(&delta);
477 }
478 }
479 db_finalize(&q);
480 diff_end(diffFlags, 0);
481 }
482
483 /*
484 ** Drop the indicated stash
485 */
486

Keyboard Shortcuts

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