Fossil SCM

Add the --www option to the various "diff" commands.

drh 2021-08-25 23:08 trunk
Commit f3961f453aee1ef5642190e567e816ab86dd55656bec043f43e71456cf7d718a
2 files changed +4 +76 -1
+4
--- src/diff.c
+++ src/diff.c
@@ -43,10 +43,11 @@
4343
#define DIFF_CONTEXT_EX (((u64)0x04)<<32) /* Use context even if zero */
4444
#define DIFF_NOTTOOBIG (((u64)0x08)<<32) /* Only display if not too big */
4545
#define DIFF_STRIP_EOLCR (((u64)0x10)<<32) /* Strip trailing CR */
4646
#define DIFF_SLOW_SBS (((u64)0x20)<<32) /* Better but slower side-by-side */
4747
#define DIFF_WEBPAGE (((u64)0x40)<<32) /* Complete webpage */
48
+#define DIFF_WWW (((u64)0x80)<<32) /* The --www option */
4849
4950
/*
5051
** These error messages are shared in multiple locations. They are defined
5152
** here for consistency.
5253
*/
@@ -2041,10 +2042,13 @@
20412042
if( find_option("invert",0,0)!=0 ) diffFlags |= DIFF_INVERT;
20422043
if( find_option("brief",0,0)!=0 ) diffFlags |= DIFF_BRIEF;
20432044
if( find_option("webpage",0,0)!=0 ){
20442045
diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO;
20452046
}
2047
+ if( find_option("www",0,0)!=0 ){
2048
+ diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO|DIFF_WWW;
2049
+ }
20462050
return diffFlags;
20472051
}
20482052
20492053
/*
20502054
** COMMAND: test-rawdiff
20512055
--- src/diff.c
+++ src/diff.c
@@ -43,10 +43,11 @@
43 #define DIFF_CONTEXT_EX (((u64)0x04)<<32) /* Use context even if zero */
44 #define DIFF_NOTTOOBIG (((u64)0x08)<<32) /* Only display if not too big */
45 #define DIFF_STRIP_EOLCR (((u64)0x10)<<32) /* Strip trailing CR */
46 #define DIFF_SLOW_SBS (((u64)0x20)<<32) /* Better but slower side-by-side */
47 #define DIFF_WEBPAGE (((u64)0x40)<<32) /* Complete webpage */
 
48
49 /*
50 ** These error messages are shared in multiple locations. They are defined
51 ** here for consistency.
52 */
@@ -2041,10 +2042,13 @@
2041 if( find_option("invert",0,0)!=0 ) diffFlags |= DIFF_INVERT;
2042 if( find_option("brief",0,0)!=0 ) diffFlags |= DIFF_BRIEF;
2043 if( find_option("webpage",0,0)!=0 ){
2044 diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO;
2045 }
 
 
 
2046 return diffFlags;
2047 }
2048
2049 /*
2050 ** COMMAND: test-rawdiff
2051
--- src/diff.c
+++ src/diff.c
@@ -43,10 +43,11 @@
43 #define DIFF_CONTEXT_EX (((u64)0x04)<<32) /* Use context even if zero */
44 #define DIFF_NOTTOOBIG (((u64)0x08)<<32) /* Only display if not too big */
45 #define DIFF_STRIP_EOLCR (((u64)0x10)<<32) /* Strip trailing CR */
46 #define DIFF_SLOW_SBS (((u64)0x20)<<32) /* Better but slower side-by-side */
47 #define DIFF_WEBPAGE (((u64)0x40)<<32) /* Complete webpage */
48 #define DIFF_WWW (((u64)0x80)<<32) /* The --www option */
49
50 /*
51 ** These error messages are shared in multiple locations. They are defined
52 ** here for consistency.
53 */
@@ -2041,10 +2042,13 @@
2042 if( find_option("invert",0,0)!=0 ) diffFlags |= DIFF_INVERT;
2043 if( find_option("brief",0,0)!=0 ) diffFlags |= DIFF_BRIEF;
2044 if( find_option("webpage",0,0)!=0 ){
2045 diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO;
2046 }
2047 if( find_option("www",0,0)!=0 ){
2048 diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO|DIFF_WWW;
2049 }
2050 return diffFlags;
2051 }
2052
2053 /*
2054 ** COMMAND: test-rawdiff
2055
+76 -1
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -19,10 +19,15 @@
1919
*/
2020
#include "config.h"
2121
#include "diffcmd.h"
2222
#include <assert.h>
2323
24
+/* Need to catch the interrupt signal on unix */
25
+#ifndef _WIN32
26
+# include <signal.h>
27
+#endif
28
+
2429
/*
2530
** Use the right null device for the platform.
2631
*/
2732
#if defined(_WIN32)
2833
# define NULL_DEVICE "NUL"
@@ -232,13 +237,64 @@
232237
@ </body>
233238
@ </html>
234239
;
235240
236241
/*
237
-** Do preliminary output before computing a diff.
242
+** State variables used by the --www option for diff
243
+*/
244
+static char *tempDiffFilename; /* File holding the diff HTML */
245
+static FILE *diffOut; /* Open to write into tempDiffFilename */
246
+
247
+/* Amount of delay (in milliseconds) between launching the
248
+** web browser and deleting the temporary file used by --www
249
+*/
250
+#ifndef FOSSIL_WWW_DIFF_DELAY
251
+# define FOSSIL_WWW_DIFF_DELAY 5000 /* 5 seconds by default */
252
+#endif
253
+
254
+/*
255
+** If we catch a single while writing the temporary file for the --www
256
+** diff output, then delete the temporary file and exit.
257
+*/
258
+static void diff_www_interrupt(int NotUsed){
259
+ (void)NotUsed;
260
+ if( diffOut ) fclose(diffOut);
261
+ if( tempDiffFilename ) file_delete(tempDiffFilename);
262
+ exit(1);
263
+}
264
+#ifdef _WIN32
265
+static BOOL WINAPI diff_console_ctrl_handler(DWORD dwCtrlType){
266
+ if( dwCtrlType==CTRL_C_EVENT ) diff_www_interrupt(0);
267
+ return FALSE;
268
+}
269
+#endif
270
+
271
+
272
+/*
273
+** Do preliminary setup and output before computing a diff.
274
+**
275
+** For --www, redirect stdout to a temporary file that will
276
+** hold the result. Make arrangements to delete that temporary
277
+** file if the diff is interrupted.
278
+**
279
+** For --www and --webpage, output the HTML header.
238280
*/
239281
void diff_begin(u64 diffFlags){
282
+ if( (diffFlags & DIFF_WWW)!=0 ){
283
+ tempDiffFilename = fossil_temp_filename();
284
+ tempDiffFilename = sqlite3_mprintf("%z.html", tempDiffFilename);
285
+ diffOut = freopen(tempDiffFilename,"wb",stdout);
286
+ if( diffOut==0 ){
287
+ fossil_fatal("unable to create temporary file \"%s\"",
288
+ tempDiffFilename);
289
+ }
290
+#ifndef _WIN32
291
+ signal(SIGINT, diff_www_interrupt);
292
+#else
293
+ SetConsoleCtrlHandler(diff_console_ctrl_handler, TRUE);
294
+#endif
295
+ }
240296
if( (diffFlags & DIFF_WEBPAGE)!=0 ){
241297
const char *zExtra;
242298
if( diffFlags & DIFF_SIDEBYSIDE ){
243299
zExtra = zSbsCss;
244300
}else{
@@ -248,18 +304,36 @@
248304
}
249305
}
250306
251307
/* Do any final output required by a diff and complete the diff
252308
** process.
309
+**
310
+** For --www and --webpage, output any javascript required by
311
+** the diff. (Currently JS is only needed for side-by-side diffs).
312
+**
313
+** For --www, close the connection to the temporary file, then
314
+** launch a web browser to view the file. After a delay
315
+** of FOSSIL_WWW_DIFF_DELAY milliseconds, delete the temp file.
253316
*/
254317
void diff_end(u64 diffFlags, int nErr){
255318
if( (diffFlags & DIFF_WEBPAGE)!=0 ){
256319
if( diffFlags & DIFF_SIDEBYSIDE ){
257320
const unsigned char *zJs = builtin_file("sbsdiff.js", 0);
258321
fossil_print("<script>\n%s</script>\n", zJs);
259322
}
260323
fossil_print("%s", zWebpageEnd);
324
+ }
325
+ if( (diffFlags & DIFF_WWW)!=0 && nErr==0 ){
326
+ char *zCmd = mprintf("%$ %$", fossil_web_browser(), tempDiffFilename);
327
+ fclose(diffOut);
328
+ freopen(NULL_DEVICE, "wb", stdout);
329
+ fossil_system(zCmd);
330
+ fossil_free(zCmd);
331
+ sqlite3_sleep(FOSSIL_WWW_DIFF_DELAY);
332
+ file_delete(tempDiffFilename);
333
+ sqlite3_free(tempDiffFilename);
334
+ tempDiffFilename = 0;
261335
}
262336
}
263337
264338
/*
265339
** Show the difference between two files, one in memory and one on disk.
@@ -965,10 +1039,11 @@
9651039
** --undo Diff against the "undo" buffer
9661040
** --unified Unified diff
9671041
** -v|--verbose Output complete text of added or deleted files
9681042
** --webpage Format output as a stand-alone HTML webpage
9691043
** -W|--width N Width of lines in side-by-side diff
1044
+** --www Show the diff output in a web-browser
9701045
** -Z|--ignore-trailing-space Ignore changes to end-of-line whitespace
9711046
*/
9721047
void diff_cmd(void){
9731048
int isGDiff; /* True for gdiff. False for normal diff */
9741049
int isInternDiff; /* True for internal diff */
9751050
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -19,10 +19,15 @@
19 */
20 #include "config.h"
21 #include "diffcmd.h"
22 #include <assert.h>
23
 
 
 
 
 
24 /*
25 ** Use the right null device for the platform.
26 */
27 #if defined(_WIN32)
28 # define NULL_DEVICE "NUL"
@@ -232,13 +237,64 @@
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{
@@ -248,18 +304,36 @@
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.
@@ -965,10 +1039,11 @@
965 ** --undo Diff against the "undo" buffer
966 ** --unified Unified diff
967 ** -v|--verbose Output complete text of added or deleted files
968 ** --webpage Format output as a stand-alone HTML webpage
969 ** -W|--width N Width of lines in side-by-side diff
 
970 ** -Z|--ignore-trailing-space Ignore changes to end-of-line whitespace
971 */
972 void diff_cmd(void){
973 int isGDiff; /* True for gdiff. False for normal diff */
974 int isInternDiff; /* True for internal diff */
975
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -19,10 +19,15 @@
19 */
20 #include "config.h"
21 #include "diffcmd.h"
22 #include <assert.h>
23
24 /* Need to catch the interrupt signal on unix */
25 #ifndef _WIN32
26 # include <signal.h>
27 #endif
28
29 /*
30 ** Use the right null device for the platform.
31 */
32 #if defined(_WIN32)
33 # define NULL_DEVICE "NUL"
@@ -232,13 +237,64 @@
237 @ </body>
238 @ </html>
239 ;
240
241 /*
242 ** State variables used by the --www option for diff
243 */
244 static char *tempDiffFilename; /* File holding the diff HTML */
245 static FILE *diffOut; /* Open to write into tempDiffFilename */
246
247 /* Amount of delay (in milliseconds) between launching the
248 ** web browser and deleting the temporary file used by --www
249 */
250 #ifndef FOSSIL_WWW_DIFF_DELAY
251 # define FOSSIL_WWW_DIFF_DELAY 5000 /* 5 seconds by default */
252 #endif
253
254 /*
255 ** If we catch a single while writing the temporary file for the --www
256 ** diff output, then delete the temporary file and exit.
257 */
258 static void diff_www_interrupt(int NotUsed){
259 (void)NotUsed;
260 if( diffOut ) fclose(diffOut);
261 if( tempDiffFilename ) file_delete(tempDiffFilename);
262 exit(1);
263 }
264 #ifdef _WIN32
265 static BOOL WINAPI diff_console_ctrl_handler(DWORD dwCtrlType){
266 if( dwCtrlType==CTRL_C_EVENT ) diff_www_interrupt(0);
267 return FALSE;
268 }
269 #endif
270
271
272 /*
273 ** Do preliminary setup and output before computing a diff.
274 **
275 ** For --www, redirect stdout to a temporary file that will
276 ** hold the result. Make arrangements to delete that temporary
277 ** file if the diff is interrupted.
278 **
279 ** For --www and --webpage, output the HTML header.
280 */
281 void diff_begin(u64 diffFlags){
282 if( (diffFlags & DIFF_WWW)!=0 ){
283 tempDiffFilename = fossil_temp_filename();
284 tempDiffFilename = sqlite3_mprintf("%z.html", tempDiffFilename);
285 diffOut = freopen(tempDiffFilename,"wb",stdout);
286 if( diffOut==0 ){
287 fossil_fatal("unable to create temporary file \"%s\"",
288 tempDiffFilename);
289 }
290 #ifndef _WIN32
291 signal(SIGINT, diff_www_interrupt);
292 #else
293 SetConsoleCtrlHandler(diff_console_ctrl_handler, TRUE);
294 #endif
295 }
296 if( (diffFlags & DIFF_WEBPAGE)!=0 ){
297 const char *zExtra;
298 if( diffFlags & DIFF_SIDEBYSIDE ){
299 zExtra = zSbsCss;
300 }else{
@@ -248,18 +304,36 @@
304 }
305 }
306
307 /* Do any final output required by a diff and complete the diff
308 ** process.
309 **
310 ** For --www and --webpage, output any javascript required by
311 ** the diff. (Currently JS is only needed for side-by-side diffs).
312 **
313 ** For --www, close the connection to the temporary file, then
314 ** launch a web browser to view the file. After a delay
315 ** of FOSSIL_WWW_DIFF_DELAY milliseconds, delete the temp file.
316 */
317 void diff_end(u64 diffFlags, int nErr){
318 if( (diffFlags & DIFF_WEBPAGE)!=0 ){
319 if( diffFlags & DIFF_SIDEBYSIDE ){
320 const unsigned char *zJs = builtin_file("sbsdiff.js", 0);
321 fossil_print("<script>\n%s</script>\n", zJs);
322 }
323 fossil_print("%s", zWebpageEnd);
324 }
325 if( (diffFlags & DIFF_WWW)!=0 && nErr==0 ){
326 char *zCmd = mprintf("%$ %$", fossil_web_browser(), tempDiffFilename);
327 fclose(diffOut);
328 freopen(NULL_DEVICE, "wb", stdout);
329 fossil_system(zCmd);
330 fossil_free(zCmd);
331 sqlite3_sleep(FOSSIL_WWW_DIFF_DELAY);
332 file_delete(tempDiffFilename);
333 sqlite3_free(tempDiffFilename);
334 tempDiffFilename = 0;
335 }
336 }
337
338 /*
339 ** Show the difference between two files, one in memory and one on disk.
@@ -965,10 +1039,11 @@
1039 ** --undo Diff against the "undo" buffer
1040 ** --unified Unified diff
1041 ** -v|--verbose Output complete text of added or deleted files
1042 ** --webpage Format output as a stand-alone HTML webpage
1043 ** -W|--width N Width of lines in side-by-side diff
1044 ** --www Show the diff output in a web-browser
1045 ** -Z|--ignore-trailing-space Ignore changes to end-of-line whitespace
1046 */
1047 void diff_cmd(void){
1048 int isGDiff; /* True for gdiff. False for normal diff */
1049 int isInternDiff; /* True for internal diff */
1050

Keyboard Shortcuts

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