Fossil

Check-in [f3961f453a]
Login

Check-in [f3961f453a]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the --www option to the various "diff" commands.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f3961f453aee1ef5642190e567e816ab86dd55656bec043f43e71456cf7d718a
User & Date: drh 2021-08-25 23:08:51.703
References
2021-08-26
14:01
Rename the new --www option (check-in [f3961f453aee1ef5]) to --browser and -b. Add the --by option as shorthand for --browser --sidebyside, as that is expected to be a common usage pattern. ... (check-in: 9a3372eec5 user: drh tags: trunk)
Context
2021-08-25
23:17
Fix a harmless compiler warning in the previous check-in. ... (check-in: 4291b9c87f user: drh tags: trunk)
23:08
Add the --www option to the various "diff" commands. ... (check-in: f3961f453a user: drh tags: trunk)
20:56
Improved internal interfaces for diff. For --webpage, omit the CSS used only for side-by-side diffs when doing a unified diff. ... (check-in: eb6611c4dc user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/diff.c.
41
42
43
44
45
46
47

48
49
50
51
52
53
54
#define DIFF_NOOPT        (((u64)0x01)<<32) /* Suppress optimizations (debug) */
#define DIFF_INVERT       (((u64)0x02)<<32) /* Invert the diff (debug) */
#define DIFF_CONTEXT_EX   (((u64)0x04)<<32) /* Use context even if zero */
#define DIFF_NOTTOOBIG    (((u64)0x08)<<32) /* Only display if not too big */
#define DIFF_STRIP_EOLCR  (((u64)0x10)<<32) /* Strip trailing CR */
#define DIFF_SLOW_SBS     (((u64)0x20)<<32) /* Better but slower side-by-side */
#define DIFF_WEBPAGE      (((u64)0x40)<<32) /* Complete webpage */


/*
** These error messages are shared in multiple locations.  They are defined
** here for consistency.
*/
#define DIFF_CANNOT_COMPUTE_BINARY \
    "cannot compute difference between binary files\n"







>







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#define DIFF_NOOPT        (((u64)0x01)<<32) /* Suppress optimizations (debug) */
#define DIFF_INVERT       (((u64)0x02)<<32) /* Invert the diff (debug) */
#define DIFF_CONTEXT_EX   (((u64)0x04)<<32) /* Use context even if zero */
#define DIFF_NOTTOOBIG    (((u64)0x08)<<32) /* Only display if not too big */
#define DIFF_STRIP_EOLCR  (((u64)0x10)<<32) /* Strip trailing CR */
#define DIFF_SLOW_SBS     (((u64)0x20)<<32) /* Better but slower side-by-side */
#define DIFF_WEBPAGE      (((u64)0x40)<<32) /* Complete webpage */
#define DIFF_WWW          (((u64)0x80)<<32) /* The --www option */

/*
** These error messages are shared in multiple locations.  They are defined
** here for consistency.
*/
#define DIFF_CANNOT_COMPUTE_BINARY \
    "cannot compute difference between binary files\n"
2039
2040
2041
2042
2043
2044
2045



2046
2047
2048
2049
2050
2051
2052
  if( find_option("noopt",0,0)!=0 ) diffFlags |= DIFF_NOOPT;
  if( find_option("numstat",0,0)!=0 ) diffFlags |= DIFF_NUMSTAT;
  if( find_option("invert",0,0)!=0 ) diffFlags |= DIFF_INVERT;
  if( find_option("brief",0,0)!=0 ) diffFlags |= DIFF_BRIEF;
  if( find_option("webpage",0,0)!=0 ){
    diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO;
  }



  return diffFlags;
}

/*
** COMMAND: test-rawdiff
**
** Usage: %fossil test-rawdiff FILE1 FILE2







>
>
>







2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
  if( find_option("noopt",0,0)!=0 ) diffFlags |= DIFF_NOOPT;
  if( find_option("numstat",0,0)!=0 ) diffFlags |= DIFF_NUMSTAT;
  if( find_option("invert",0,0)!=0 ) diffFlags |= DIFF_INVERT;
  if( find_option("brief",0,0)!=0 ) diffFlags |= DIFF_BRIEF;
  if( find_option("webpage",0,0)!=0 ){
    diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO;
  }
  if( find_option("www",0,0)!=0 ){
    diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO|DIFF_WWW;
  }
  return diffFlags;
}

/*
** COMMAND: test-rawdiff
**
** Usage: %fossil test-rawdiff FILE1 FILE2
Changes to src/diffcmd.c.
17
18
19
20
21
22
23





24
25
26
27
28
29
30
**
** This file contains code used to implement the "diff" command
*/
#include "config.h"
#include "diffcmd.h"
#include <assert.h>






/*
** Use the right null device for the platform.
*/
#if defined(_WIN32)
#  define NULL_DEVICE "NUL"
#else
#  define NULL_DEVICE "/dev/null"







>
>
>
>
>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
**
** This file contains code used to implement the "diff" command
*/
#include "config.h"
#include "diffcmd.h"
#include <assert.h>

/* Need to catch the interrupt signal on unix */
#ifndef _WIN32
# include <signal.h>
#endif

/*
** Use the right null device for the platform.
*/
#if defined(_WIN32)
#  define NULL_DEVICE "NUL"
#else
#  define NULL_DEVICE "/dev/null"
230
231
232
233
234
235
236































237






238
239














240
241
242
243
244
245
246
247
248
249
250
251
252







253
254
255
256
257
258
259
260











261
262
263
264
265
266
267
;
const char zWebpageEnd[] = 
@ </body>
@ </html>
;

/*































** Do preliminary output before computing a diff.






*/
void diff_begin(u64 diffFlags){














  if( (diffFlags & DIFF_WEBPAGE)!=0 ){
    const char *zExtra;
    if( diffFlags & DIFF_SIDEBYSIDE ){
      zExtra = zSbsCss;
    }else{
      zExtra = "";
    }
    fossil_print(zWebpageHdr/*works-like:"%s"*/, zExtra);
  }
}

/* Do any final output required by a diff and complete the diff
** process.







*/
void diff_end(u64 diffFlags, int nErr){
  if( (diffFlags & DIFF_WEBPAGE)!=0 ){
    if( diffFlags & DIFF_SIDEBYSIDE ){
      const unsigned char *zJs = builtin_file("sbsdiff.js", 0);
      fossil_print("<script>\n%s</script>\n", zJs);
    }
    fossil_print("%s", zWebpageEnd);











  }
}

/*
** Show the difference between two files, one in memory and one on disk.
**
** The difference is the set of edits needed to transform pFile1 into







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>













>
>
>
>
>
>
>








>
>
>
>
>
>
>
>
>
>
>







235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
;
const char zWebpageEnd[] = 
@ </body>
@ </html>
;

/*
** State variables used by the --www option for diff
*/
static char *tempDiffFilename;  /* File holding the diff HTML */
static FILE *diffOut;           /* Open to write into tempDiffFilename */

/* Amount of delay (in milliseconds) between launching the
** web browser and deleting the temporary file used by --www
*/
#ifndef FOSSIL_WWW_DIFF_DELAY
# define FOSSIL_WWW_DIFF_DELAY 5000  /* 5 seconds by default */
#endif

/*
** If we catch a single while writing the temporary file for the --www
** diff output, then delete the temporary file and exit.
*/
static void diff_www_interrupt(int NotUsed){
  (void)NotUsed;
  if( diffOut ) fclose(diffOut);
  if( tempDiffFilename ) file_delete(tempDiffFilename);
  exit(1);
}
#ifdef _WIN32
static BOOL WINAPI diff_console_ctrl_handler(DWORD dwCtrlType){
  if( dwCtrlType==CTRL_C_EVENT ) diff_www_interrupt(0);
  return FALSE;
}
#endif


/*
** Do preliminary setup and output before computing a diff.
**
** For --www, redirect stdout to a temporary file that will
** hold the result.  Make arrangements to delete that temporary
** file if the diff is interrupted.
**
** For --www and --webpage, output the HTML header.
*/
void diff_begin(u64 diffFlags){
  if( (diffFlags & DIFF_WWW)!=0 ){
    tempDiffFilename = fossil_temp_filename();
    tempDiffFilename = sqlite3_mprintf("%z.html", tempDiffFilename);
    diffOut = freopen(tempDiffFilename,"wb",stdout);
    if( diffOut==0 ){
      fossil_fatal("unable to create temporary file \"%s\"", 
                   tempDiffFilename);
    }
#ifndef _WIN32
    signal(SIGINT, diff_www_interrupt);
#else
    SetConsoleCtrlHandler(diff_console_ctrl_handler, TRUE);
#endif
  }
  if( (diffFlags & DIFF_WEBPAGE)!=0 ){
    const char *zExtra;
    if( diffFlags & DIFF_SIDEBYSIDE ){
      zExtra = zSbsCss;
    }else{
      zExtra = "";
    }
    fossil_print(zWebpageHdr/*works-like:"%s"*/, zExtra);
  }
}

/* Do any final output required by a diff and complete the diff
** process.
**
** For --www and --webpage, output any javascript required by 
** the diff.  (Currently JS is only needed for side-by-side diffs).
**
** For --www, close the connection to the temporary file, then
** launch a web browser to view the file.  After a delay
** of FOSSIL_WWW_DIFF_DELAY milliseconds, delete the temp file.
*/
void diff_end(u64 diffFlags, int nErr){
  if( (diffFlags & DIFF_WEBPAGE)!=0 ){
    if( diffFlags & DIFF_SIDEBYSIDE ){
      const unsigned char *zJs = builtin_file("sbsdiff.js", 0);
      fossil_print("<script>\n%s</script>\n", zJs);
    }
    fossil_print("%s", zWebpageEnd);
  }
  if( (diffFlags & DIFF_WWW)!=0 && nErr==0 ){
    char *zCmd = mprintf("%$ %$", fossil_web_browser(), tempDiffFilename);
    fclose(diffOut);
    freopen(NULL_DEVICE, "wb", stdout);
    fossil_system(zCmd);
    fossil_free(zCmd);
    sqlite3_sleep(FOSSIL_WWW_DIFF_DELAY);
    file_delete(tempDiffFilename);
    sqlite3_free(tempDiffFilename);
    tempDiffFilename = 0;
  }
}

/*
** Show the difference between two files, one in memory and one on disk.
**
** The difference is the set of edits needed to transform pFile1 into
963
964
965
966
967
968
969

970
971
972
973
974
975
976
**   --tk                        Launch a Tcl/Tk GUI for display
**   --to VERSION                Select VERSION as target for the diff
**   --undo                      Diff against the "undo" buffer
**   --unified                   Unified diff
**   -v|--verbose                Output complete text of added or deleted files
**   --webpage                   Format output as a stand-alone HTML webpage
**   -W|--width N                Width of lines in side-by-side diff

**   -Z|--ignore-trailing-space  Ignore changes to end-of-line whitespace
*/
void diff_cmd(void){
  int isGDiff;               /* True for gdiff.  False for normal diff */
  int isInternDiff;          /* True for internal diff */
  int verboseFlag;           /* True if -v or --verbose flag is used */
  const char *zFrom;         /* Source version number */







>







1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
**   --tk                        Launch a Tcl/Tk GUI for display
**   --to VERSION                Select VERSION as target for the diff
**   --undo                      Diff against the "undo" buffer
**   --unified                   Unified diff
**   -v|--verbose                Output complete text of added or deleted files
**   --webpage                   Format output as a stand-alone HTML webpage
**   -W|--width N                Width of lines in side-by-side diff
**   --www                       Show the diff output in a web-browser
**   -Z|--ignore-trailing-space  Ignore changes to end-of-line whitespace
*/
void diff_cmd(void){
  int isGDiff;               /* True for gdiff.  False for normal diff */
  int isInternDiff;          /* True for internal diff */
  int verboseFlag;           /* True if -v or --verbose flag is used */
  const char *zFrom;         /* Source version number */