Fossil

Changes On Branch merge-info-html
Login

Changes On Branch merge-info-html

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

Changes In Branch merge-info-html Excluding Merge-Ins

This is equivalent to a diff from de6f88a6f0 to f0a48c8faa

2024-12-10
12:53
Add the /ckout page. ... (check-in: d35d201840 user: drh tags: trunk)
06:25
Merge trunk into merge-info-html branch for mergestat improvements. ... (Leaf check-in: f0a48c8faa user: stephan tags: merge-info-html)
00:36
Begin adding the infrastructure for a /ckout webpage. ... (check-in: c620a8c74c user: drh tags: ckout-page)
00:09
Fix an obscure possible bug in "fossil ui DIR" if there are 96 or more additional arguments. ... (check-in: de6f88a6f0 user: drh tags: trunk)
2024-12-09
23:03
Update the built-in SQLite to the latests trunk version, which includes fixes found in 3.47.2, plus other enhancements. ... (check-in: 077e53a341 user: drh tags: trunk)
08:17
Move filename info into the corresponding merge-info table. Start adding infrastructure for emitting HTML-mode output to the content columns. ... (check-in: e3a3f0a5f2 user: stephan tags: merge-info-html)

Changes to src/default.css.

1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
8.9199065,6.1390961 9.1162816,5.8045312 9.1162816,5.4699662 q 0,-0.5091205 -0.3491113,-0.7927734 \
-0.3491111,-0.2909259 -0.9746021,-0.2909259 -0.5891252,0 -1.2728012,0.247287 \
-0.6836761,0.240014 -1.4255375,0.720042 V 3.0698267 q 0.8800513,-0.3054724 1.6073661,-0.4509353 \
0.7273151,-0.145463 1.403718,-0.145463 1.7746486,0 2.7056104,0.727315 0.930965,0.720042 \
0.930965,2.1092135 0,0.7127686 -0.283654,1.2800746 -0.283652,0.5600324 -0.967329,1.2073428 \
L 10.025425,8.2119439 Q 9.530851,8.6628792 9.3781148,8.9392588 9.2253789,9.2083654 \
9.2253789,9.535657 Z M 6.5997716,10.939376 h 2.6256073 v 2.589241 H 6.5997716 Z' \
style='fill:%23f8f8f8;stroke-width:1.35412836' /%3e%3c/svg%3e ");    
  background-repeat: no-repeat;
  background-position: center;
  /* When not using a background image, this additional style works
     reasonably well along with a ::before content of "?": */
  /*border-width: 1px;
  border-style: outset;
  border-radius: 0.5em;







|







1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
8.9199065,6.1390961 9.1162816,5.8045312 9.1162816,5.4699662 q 0,-0.5091205 -0.3491113,-0.7927734 \
-0.3491111,-0.2909259 -0.9746021,-0.2909259 -0.5891252,0 -1.2728012,0.247287 \
-0.6836761,0.240014 -1.4255375,0.720042 V 3.0698267 q 0.8800513,-0.3054724 1.6073661,-0.4509353 \
0.7273151,-0.145463 1.403718,-0.145463 1.7746486,0 2.7056104,0.727315 0.930965,0.720042 \
0.930965,2.1092135 0,0.7127686 -0.283654,1.2800746 -0.283652,0.5600324 -0.967329,1.2073428 \
L 10.025425,8.2119439 Q 9.530851,8.6628792 9.3781148,8.9392588 9.2253789,9.2083654 \
9.2253789,9.535657 Z M 6.5997716,10.939376 h 2.6256073 v 2.589241 H 6.5997716 Z' \
style='fill:%23f8f8f8;stroke-width:1.35412836' /%3e%3c/svg%3e ");
  background-repeat: no-repeat;
  background-position: center;
  /* When not using a background image, this additional style works
     reasonably well along with a ::before content of "?": */
  /*border-width: 1px;
  border-style: outset;
  border-radius: 0.5em;

Changes to src/diffcmd.c.

221
222
223
224
225
226
227

228
229
230
231
232
233
234
** Default header texts for diff with --webpage
*/
static const char zWebpageHdr[] =
@ <!DOCTYPE html>
@ <html>
@ <head>
@ <meta charset="UTF-8">

@ <style>
@ body {
@    background-color: white;
@ }
@ h1 {
@   font-size: 150%;
@ }







>







221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
** Default header texts for diff with --webpage
*/
static const char zWebpageHdr[] =
@ <!DOCTYPE html>
@ <html>
@ <head>
@ <meta charset="UTF-8">
@ <title></title>
@ <style>
@ body {
@    background-color: white;
@ }
@ h1 {
@   font-size: 150%;
@ }
350
351
352
353
354
355
356

357
358
359
360
361
362
363
@ <body>
;
static const char zWebpageHdrDark[] =
@ <!DOCTYPE html>
@ <html>
@ <head>
@ <meta charset="UTF-8">

@ <style>
@ body {
@    background-color: #353535;
@    color: #ffffff;
@ }
@ h1 {
@   font-size: 150%;







>







351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
@ <body>
;
static const char zWebpageHdrDark[] =
@ <!DOCTYPE html>
@ <html>
@ <head>
@ <meta charset="UTF-8">
@ <title></title>
@ <style>
@ body {
@    background-color: #353535;
@    color: #ffffff;
@ }
@ h1 {
@   font-size: 150%;
451
452
453
454
455
456
457
458
459
460
461

















462
463
464
465
466
467
468
@   font-weight: bold;
@ }
@
@ </style>
@ </head>
@ <body>
;
const char zWebpageEnd[] =
@ </body>
@ </html>
;


















/*
** State variables used by the --browser option for diff.  These must
** be static variables, not elements of DiffConfig, since they are
** used by the interrupt handler.
*/
static char *tempDiffFilename;  /* File holding the diff HTML */







|



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







453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
@   font-weight: bold;
@ }
@
@ </style>
@ </head>
@ <body>
;
static const char zWebpageEnd[] =
@ </body>
@ </html>
;

/*
** Returns the diff webpage header, which includes the whole page
** prefix up to and including the BODY tag. If bDark is true, a
** dark-mode version is returned. The non-dark impl relies on browser
** preferences to determine whether to use dark mode.
*/
const char * diff_webpage_header(int bDark){
  return bDark ? zWebpageHdrDark : zWebpageHdr;
}
/*
** Returns the diff webpage footer, which closes the body tag and
** page.
*/
const char * diff_webpage_footer(void){
  return zWebpageEnd;
}

/*
** State variables used by the --browser option for diff.  These must
** be static variables, not elements of DiffConfig, since they are
** used by the interrupt handler.
*/
static char *tempDiffFilename;  /* File holding the diff HTML */
514
515
516
517
518
519
520

521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#ifndef _WIN32
    signal(SIGINT, diff_www_interrupt);
#else
    SetConsoleCtrlHandler(diff_console_ctrl_handler, TRUE);
#endif
  }
  if( (pCfg->diffFlags & DIFF_WEBPAGE)!=0 ){

    fossil_print("%s",(pCfg->diffFlags & DIFF_DARKMODE)!=0 ? zWebpageHdrDark :
                                                             zWebpageHdr);
    fflush(stdout);
  }
}

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







>
|
<




















|







533
534
535
536
537
538
539
540
541

542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#ifndef _WIN32
    signal(SIGINT, diff_www_interrupt);
#else
    SetConsoleCtrlHandler(diff_console_ctrl_handler, TRUE);
#endif
  }
  if( (pCfg->diffFlags & DIFF_WEBPAGE)!=0 ){
    fossil_print("%s",diff_webpage_header(
                   (pCfg->diffFlags & DIFF_DARKMODE)!=0));

    fflush(stdout);
  }
}

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

Changes to src/merge.c.

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    mb.zV1 = mprintf("%s (local)", file_tail(zFN));
    rid = db_column_int(&q, 3);
    sz = db_column_int(&q, 4);
    if( rid==0 && sz>0 ){
      /* The origin file had been edited so we'll have to pull its
      ** original content out of the undo buffer */
      Stmt q2;
      db_prepare(&q2, 
        "SELECT content FROM undo"
        " WHERE pathname=%Q AND octet_length(content)=%d",
        zFN, sz
      );
      blob_zero(&v1);
      if( db_step(&q2)==SQLITE_ROW ){
        db_column_blob(&q2, 0, &v1);







|







193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    mb.zV1 = mprintf("%s (local)", file_tail(zFN));
    rid = db_column_int(&q, 3);
    sz = db_column_int(&q, 4);
    if( rid==0 && sz>0 ){
      /* The origin file had been edited so we'll have to pull its
      ** original content out of the undo buffer */
      Stmt q2;
      db_prepare(&q2,
        "SELECT content FROM undo"
        " WHERE pathname=%Q AND octet_length(content)=%d",
        zFN, sz
      );
      blob_zero(&v1);
      if( db_step(&q2)==SQLITE_ROW ){
        db_column_blob(&q2, 0, &v1);
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
268
269
270
271
272
273
274
275
276


277
278
279
280
281
282
283
284
285
286
287
  mb.xDestroy(&mb);
  blob_reset(&pivot);
  blob_reset(&v1);
  blob_reset(&v2);
  blob_reset(&out);
  db_finalize(&q);
}







































































































































































































































/*
** COMMAND: merge-info
**
** Usage: %fossil merge-info [OPTIONS]
**
** Display information about the most recent merge operation.
**
** Options:
**   -a|--all             Show all file changes that happened because of
**                        the merge.  Normally only MERGE, CONFLICT, and ERROR
**                        lines are shown
**   -c|--context N       Show N lines of context around each change,
**                        with negative N meaning show all content.  Only
**                        meaningful in combination with --tcl or --tk.
**   --dark               Use dark mode for the Tcl/Tk-based GUI
**   --tcl FILE           Generate (to stdout) a TCL list containing
**                        information needed to display the changes to
**                        FILE caused by the most recent merge.  FILE must
**                        be a pathname relative to the root of the check-out.
**   --tk                 Bring up a Tcl/Tk GUI that shows the changes
**                        associated with the most recent merge.
**

*/
void merge_info_cmd(void){
  const char *zCnt;
  const char *zTcl;
  int bTk;


  int bDark;
  int bAll;
  int nContext;
  Stmt q;
  const char *zWhere;
  int cnt = 0;

  db_must_be_within_tree();
  zTcl = find_option("tcl", 0, 1);
  bTk = find_option("tk", 0, 0)!=0;


  zCnt = find_option("context", "c", 1);
  bDark = find_option("dark", 0, 0)!=0;
  bAll = find_option("all", "a", 0)!=0;
  if( bTk==0 ){
    verify_all_options();
    if( g.argc>2 ){
      usage("[OPTIONS]");
    }
  }
  if( zCnt ){
    nContext = atoi(zCnt);







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











|



|






|
>





>
>










>
>



|







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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  mb.xDestroy(&mb);
  blob_reset(&pivot);
  blob_reset(&v1);
  blob_reset(&v2);
  blob_reset(&out);
  db_finalize(&q);
}

/*
** Append STYLE tag for (merge-info --html) to p.
**
** Design notes...
**
** Layout is based on the /vdiff view, with 4 main content columns:
**
** baseline | local | merged-in | merge-result
**
** With columns between each to mark the line numbers (here "#") and
** the change-type indicators (here "C"):
**
** # baseline C # local C # merged-in C # merge-result
**
** == 11 columns
**
** The C columns are, the context of a merge, initially only
** for spacing between an LHS and its RHS's line numbers, but
** we should probably apply change status markers like the
** diff view does.
*/
static void merge_info_html_css(Blob *p){
  blob_append(p, "<style>\n", -1);
  blob_append(p, "table.diff {\n"
              "margin-bottom: 1em;"
              "}\n", -1);
  blob_append(p, "tr.diffchunk {\n"
              "display: grid; gap: 0px 0px;\n"
              "grid-template-rows: 1fr;\n"
              "grid-template-columns: "
              "auto 1fr auto " /* # baseline sep */
              "auto 1fr auto " /* # local sep */
              "auto 1fr auto " /* # merged-in sep */
              "auto 1fr" /* # merge-result */
              ";\n"
              "grid-template-areas: \""
              "mrgBaseLn mrgBase mrgBaseSep "
              "mrgLocalLn mrgLocal mrgLocalSep "
              "mrgMILn mrgMI mrgMISep "
              "mrgResLn mrgRes\";\n"
              "}\n", -1);
#define GA(N) blob_append(p,"td." # N " {grid-area: " # N "}\n", -1)
  GA(mrgBaseLn);
  GA(mrgBase);
  GA(mrgBaseSep);
  GA(mrgLocalLn);
  GA(mrgLocal);
  GA(mrgLocalSep);
  GA(mrgMILn);
  GA(mrgMI);
  GA(mrgMISep);
  GA(mrgResLn);
  GA(mrgRes);
#undef GA
  blob_append(p, "</style>\n", -1);
}

/*static void merge_info_html_one(MergeBuilderHtml *pB, Blob *pOut,
                                const char *zOp, const char *zFile){
}*/

/*
** The HTML counterpart of merge_info_tk().
*/
static void merge_info_html(int bBrowser,  /* 0=HTML only, no browser */
                            int bDark,     /* use dark mode */
                            int bAll,      /* All changes, not just merged content */
                            int nContext   /* Diff context lines */){
  MergeBuilderHtml mbh;               /* Merge builder */
  MergeBuilder * mb = &mbh.base;      /* Merge builder base class ref */
  Blob pivot = empty_blob,
    v1 = empty_blob, v2 = empty_blob,
    out = empty_blob;                 /* Merge builder content */
  Stmt q;                             /* MERGESTAT query */

  /* Figure out which files to process. We do this level of indirection
  ** so that the loop which follows is identical for both the all-files and
  ** the CLI-list-of-files cases work identically.
  */
  db_multi_exec("CREATE TEMP TABLE mi_html ("
                "mirid INTEGER UNIQUE ON CONFLICT IGNORE"
                ")");
  if( g.argc==2 ){
    /* No files named on the command-line.  Use every file mentioned
    ** in the MERGESTAT table to generate the file list. */
    db_multi_exec("INSERT INTO mi_html (mirid) "
                  "SELECT rowid FROM mergestat %s ",
                  bAll ? ""
                  : "WHERE op IN ('MERGE','CONFLICT')" /*safe-for-%s*/);
  }else{
    /* Use only files named on the command-line in the file list.
    ** But verify each file named is actually found in the MERGESTAT
    ** table first. */
    int i;
    for(i=2; i<g.argc; i++){
      char *zFile;          /* Input filename */
      char const *zTreename;/* Name of the file in the tree */
      Blob fname;           /* Filename relative to root */
      int gotRid;           /* mergestat row ID */
      zFile = mprintf("%/", g.argv[i]);
      file_tree_name(zFile, &fname, 0, 1);
      fossil_free(zFile);
      zTreename = blob_str(&fname);
      gotRid = db_int(0, "SELECT rowid FROM mergestat WHERE fnr=%Q or fn=%Q",
                      zTreename, zTreename);
      if( !gotRid ){
        fossil_fatal("Don't have merge info for %s", zTreename);
      }
      db_multi_exec("INSERT INTO mi_html (mirid) VALUES (%d)",
                    gotRid);
      blob_reset(&fname);
    }
  }

  blob_append(&out, diff_webpage_header(bDark), -1);
  merge_info_html_css(&out);
  mergebuilder_init_html(&mbh);
  mb->nContext = nContext;

  db_prepare(&q,
       /*   0    1     2   3     4   5    6     7 */
    "SELECT fnp, ridp, fn, ridv, sz, fnm, ridm, fnr"
    "  FROM mergestat WHERE rowid IN mi_html"
    "  ORDER BY coalesce(fnr,fn,fnp)"
  );
  while( SQLITE_ROW==db_step(&q) ){
    const char * zFN;                /* A filename */
    char * zToFree[5] = {0,0,0,0,0}; /* String memory to free */
    unsigned zToFreeNdx = 0;         /* Current index into zToFree */
    int rid = 0;                     /* A blob rid */
    int sz;                          /* File size */
    unsigned int i;                  /* Loop counter */

    /* Most of this loop is copy/paste/slight adjust from
    ** merge_info_tcl(). We can possibly consolidate this setup into a
    ** separate function. */

    /* Set up the baseline/pivot... */
    zFN  = db_column_text(&q, 0);
    if( zFN==0 ){
      /* No pivot because the file was added */
      mb->zPivot = "(no baseline)";
      blob_reset(&pivot);
    }else{
      mb->zPivot = zToFree[zToFreeNdx++] =
        mprintf("%s (baseline)", file_tail(zFN));
      rid = db_column_int(&q, 1);
      content_get(rid, &pivot);
    }
    mb->pPivot = &pivot;

    /* Set up the merge-in as V2 */
    zFN = db_column_text(&q, 5);
    if( zFN==0 ){
      /* File deleted in the merged-in branch */
      mb->zV2 = "(deleted file)";
      blob_zero(&v2);
    }else{
      mb->zV2  = zToFree[zToFreeNdx++] =
        mprintf("%s (merge-in)", file_tail(zFN));
      rid = db_column_int(&q, 6);
      content_get(rid, &v2);
    }
    mb->pV2 = &v1;

    /* Set up the merge-in as V1 */
    zFN = db_column_text(&q, 2);
    if( zFN==0 ){
      /* File added by merge */
      mb->zV1 = "(no original)";
      blob_zero(&v1);
    }else{
      mb->zV1 = zToFree[zToFreeNdx++] =
        mprintf("%s (local)", file_tail(zFN));
      rid = db_column_int(&q, 3);
      sz = db_column_int(&q, 4);
      if( rid==0 && sz>0 ){
        /* The origin file had been edited so we'll have to pull its
        ** original content out of the undo buffer */
        Stmt q2;
        db_prepare(&q2,
          "SELECT content FROM undo"
          " WHERE pathname=%Q AND octet_length(content)=%d",
          zFN, sz
        );
        blob_zero(&v1);
        if( db_step(&q2)==SQLITE_ROW ){
          db_column_blob(&q, 0, &v1);
        }else{
          mb->zV1 = "(local content missing)";
        }
        db_finalize(&q2);
      }else{
        /* The origin file was unchanged when the merge first occurred */
        content_get(rid, &v1);
      }
    }
    mb->pV1 = &v2;

    /* Set up the output */
    zFN = db_column_text(&q, 7);
    if( zFN==0 ){
      mb->zOut = "(Merge Result)";
    }else{
      mb->zOut = zToFree[zToFreeNdx++] =
        mprintf("%s (after merge)", file_tail(zFN));
    }
    mb->pOut = &out;

    assert( zToFreeNdx <= sizeof(zToFree)/sizeof(zToFree[0]) );

    merge_three_blobs(mb);
    for(i = 0; i < zToFreeNdx; ++i ){
      fossil_free(zToFree[i]);
      zToFree[i] = 0;
    }
  }/* for-each-file loop */
  db_finalize(&q);
  mb->xDestroy(mb);

  blob_append(&out, diff_webpage_footer(), -1);
  blob_reset(&v1);
  blob_reset(&v2);
  blob_reset(&pivot);
  blob_append_char(&out, '\n');
  blob_write_to_file(&out, "-");
  blob_reset(&out);
  db_multi_exec("DROP TABLE mi_html");
}

/*
** COMMAND: merge-info
**
** Usage: %fossil merge-info [OPTIONS]
**
** Display information about the most recent merge operation.
**
** Options:
**   -a|--all             Show all file changes that happened because of
**                        the merge.  Normally only MERGE, CONFLICT, and ERROR
**                        lines are shown.
**   -c|--context N       Show N lines of context around each change,
**                        with negative N meaning show all content.  Only
**                        meaningful in combination with --tcl or --tk.
**   --dark               Use dark mode for the Tcl/Tk/HTML output modes.
**   --tcl FILE           Generate (to stdout) a TCL list containing
**                        information needed to display the changes to
**                        FILE caused by the most recent merge.  FILE must
**                        be a pathname relative to the root of the check-out.
**   --tk                 Bring up a Tcl/Tk GUI that shows the changes
**                        associated with the most recent merge.
**   --html               Like --tk but emits HTML to stdout.
**   -b|--browser         Like --html but show the result in a web browser.
*/
void merge_info_cmd(void){
  const char *zCnt;
  const char *zTcl;
  int bTk;
  int bBrowser;
  int bHtml;
  int bDark;
  int bAll;
  int nContext;
  Stmt q;
  const char *zWhere;
  int cnt = 0;

  db_must_be_within_tree();
  zTcl = find_option("tcl", 0, 1);
  bTk = find_option("tk", 0, 0)!=0;
  bBrowser = find_option("browser", "b", 0)!=0;
  bHtml = find_option("html", 0, 0)!=0 || bBrowser;
  zCnt = find_option("context", "c", 1);
  bDark = find_option("dark", 0, 0)!=0;
  bAll = find_option("all", "a", 0)!=0;
  if( (bTk + bHtml)==0 ){
    verify_all_options();
    if( g.argc>2 ){
      usage("[OPTIONS]");
    }
  }
  if( zCnt ){
    nContext = atoi(zCnt);
296
297
298
299
300
301
302
303


304
305
306
307
308
309
310
311
      fossil_print("No merge data is available\n");
    }
    return;
  }
  if( bTk ){
    merge_info_tk(bDark, bAll, nContext);
    return;
  }


  if( zTcl ){
    merge_info_tcl(zTcl, nContext);
    return;
  }
  if( bAll ){
    zWhere = "";
  }else{
    zWhere = "WHERE op IN ('MERGE','CONFLICT','ERROR')";







|
>
>
|







531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
      fossil_print("No merge data is available\n");
    }
    return;
  }
  if( bTk ){
    merge_info_tk(bDark, bAll, nContext);
    return;
  }else if( bHtml ){
    merge_info_html(bBrowser, bDark, bAll, nContext);
    return;
  }else if( zTcl ){
    merge_info_tcl(zTcl, nContext);
    return;
  }
  if( bAll ){
    zWhere = "";
  }else{
    zWhere = "WHERE op IN ('MERGE','CONFLICT','ERROR')";

Changes to src/merge3.c.

164
165
166
167
168
169
170












171
172
















173
174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
189
190
191
192
193
194
195
  unsigned int lnPivot;      /* Lines read from pivot */
  unsigned int lnV1;         /* Lines read from v1 */
  unsigned int lnV2;         /* Lines read from v2 */
  unsigned int lnOut;        /* Lines written to out */
  unsigned int nConflict;    /* Number of conflicts seen */
  u64 diffFlags;             /* Flags for difference engine */
};












#endif /* INTERFACE */


















/************************* Generic MergeBuilder ******************************/
/* These are generic methods for MergeBuilder.  They just output debugging
** information.  But some of them are useful as base methods for other useful
** implementations of MergeBuilder.
*/

/* xStart() and xEnd() are called to generate header and fotter information
** in the output.  This is a no-op in the generic implementation.

*/
static void dbgStartEnd(MergeBuilder *p){  (void)p; }

/* The next N lines of PIVOT are unchanged in both V1 and V2
*/
static void dbgSame(MergeBuilder *p, unsigned int N){
  blob_appendf(p->pOut, 
     "COPY %u from BASELINE(%u..%u) or V1(%u..%u) or V2(%u..%u)\n",
     N, p->lnPivot+1, p->lnPivot+N, p->lnV1+1, p->lnV1+N,
     p->lnV2+1, p->lnV2+N);
  p->lnPivot += N;
  p->lnV1 += N;
  p->lnV2 += N;
}







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


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







|
|
>






|







164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  unsigned int lnPivot;      /* Lines read from pivot */
  unsigned int lnV1;         /* Lines read from v1 */
  unsigned int lnV2;         /* Lines read from v2 */
  unsigned int lnOut;        /* Lines written to out */
  unsigned int nConflict;    /* Number of conflicts seen */
  u64 diffFlags;             /* Flags for difference engine */
};

struct MergeBuilderHtml {
  struct MergeBuilder base;
  /*
  ** Columns for each of the entries in this summary of the HTML
  ** layout:
  **
  ** # baseline C # local C # merged-in C # merge-result
  ** 0-1--------2-3-4-----5-6-7---------8-9-10
  */
  Blob aCol[11];
};
#endif /* INTERFACE */

/*
** Column IDs for MergeBuilderHtml::aCol.
*/
enum MBHtml {
  MBH_COL_BASELINE_LN = 0,
  MBH_COL_BASELINE = 1,
  MBH_COL_BASELINE_SEP = 2,
  MBH_COL_LOCAL_LN = 3,
  MBH_COL_LOCAL = 4,
  MBH_COL_LOCAL_SEP = 5,
  MBH_COL_MERGEDIN_LN = 6,
  MBH_COL_MERGEDIN = 7,
  MBH_COL_MERGEDIN_SEP = 8,
  MBH_COL_RESULT_LN = 9,
  MBH_COL_RESULT = 10
};

/************************* Generic MergeBuilder ******************************/
/* These are generic methods for MergeBuilder.  They just output debugging
** information.  But some of them are useful as base methods for other useful
** implementations of MergeBuilder.
*/

/* xStart() and xEnd() are called to generate header and footer
** information in the output.  This is a no-op in the generic
** implementation.
*/
static void dbgStartEnd(MergeBuilder *p){  (void)p; }

/* The next N lines of PIVOT are unchanged in both V1 and V2
*/
static void dbgSame(MergeBuilder *p, unsigned int N){
  blob_appendf(p->pOut,
     "COPY %u from BASELINE(%u..%u) or V1(%u..%u) or V2(%u..%u)\n",
     N, p->lnPivot+1, p->lnPivot+N, p->lnV1+1, p->lnV1+N,
     p->lnV2+1, p->lnV2+N);
  p->lnPivot += N;
  p->lnV1 += N;
  p->lnV2 += N;
}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
*/
static void dbgConflict(
  MergeBuilder *p,
  unsigned int nPivot,
  unsigned int nV1,
  unsigned int nV2
){
  blob_appendf(p->pOut, 
   "CONFLICT %u,%u,%u BASELINE(%u..%u) versus V1(%u..%u) versus V2(%u..%u)\n",
       nPivot, nV1, nV2,
       p->lnPivot+1, p->lnPivot+nPivot,
       p->lnV1+1, p->lnV1+nV1,
       p->lnV2+1, p->lnV2+nV2);
  p->lnV1 += nV1;
  p->lnPivot += nPivot;







|







260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
*/
static void dbgConflict(
  MergeBuilder *p,
  unsigned int nPivot,
  unsigned int nV1,
  unsigned int nV2
){
  blob_appendf(p->pOut,
   "CONFLICT %u,%u,%u BASELINE(%u..%u) versus V1(%u..%u) versus V2(%u..%u)\n",
       nPivot, nV1, nV2,
       p->lnPivot+1, p->lnPivot+nPivot,
       p->lnV1+1, p->lnV1+nV1,
       p->lnV2+1, p->lnV2+nV2);
  p->lnV1 += nV1;
  p->lnPivot += nPivot;
321
322
323
324
325
326
327
328

329
330
331
332
333
334
335
** described by the first four parameters, which are the same as the
** arguments to the xConflict method of the MergeBuilder object.
** This routine attempts to resolve the conflict by looking at
** elements of the conflict region that are finer grain than complete
** lines of text.
**
** The result is written into Blob pOut.  pOut is initialized by this
** routine.

*/
int merge_try_to_resolve_conflict(
  MergeBuilder *pMB,     /* MergeBuilder that encounter conflict */
  unsigned int nPivot,   /* Lines of conflict in the pivot */
  unsigned int nV1,      /* Lines of conflict in V1 */
  unsigned int nV2,      /* Lines of conflict in V2 */
  Blob *pOut             /* Write resolution text here */







|
>







350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
** described by the first four parameters, which are the same as the
** arguments to the xConflict method of the MergeBuilder object.
** This routine attempts to resolve the conflict by looking at
** elements of the conflict region that are finer grain than complete
** lines of text.
**
** The result is written into Blob pOut.  pOut is initialized by this
** routine. Returns the result of passing the merge state to
** merge_three_blobs().
*/
int merge_try_to_resolve_conflict(
  MergeBuilder *pMB,     /* MergeBuilder that encounter conflict */
  unsigned int nPivot,   /* Lines of conflict in the pivot */
  unsigned int nV1,      /* Lines of conflict in V1 */
  unsigned int nV2,      /* Lines of conflict in V2 */
  Blob *pOut             /* Write resolution text here */
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
  MergeBuilder *p,
  unsigned int nPivot,
  unsigned int nV1,
  unsigned int nV2
){
  int nRes;   /* Lines in the computed conflict resolution */
  Blob res;   /* Text of the conflict resolution */
  
  merge_try_to_resolve_conflict(p, nPivot, nV1, nV2, &res);
  nRes = blob_linecount(&res);

  append_merge_mark(p->pOut, 0, p->lnV1+1, p->useCrLf);
  blob_copy_lines(p->pOut, p->pV1, nV1);         p->lnV1 += nV1;

  if( nRes>0 ){







|







431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
  MergeBuilder *p,
  unsigned int nPivot,
  unsigned int nV1,
  unsigned int nV2
){
  int nRes;   /* Lines in the computed conflict resolution */
  Blob res;   /* Text of the conflict resolution */

  merge_try_to_resolve_conflict(p, nPivot, nV1, nV2, &res);
  nRes = blob_linecount(&res);

  append_merge_mark(p->pOut, 0, p->lnV1+1, p->useCrLf);
  blob_copy_lines(p->pOut, p->pV1, nV1);         p->lnV1 += nV1;

  if( nRes>0 ){
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
  unsigned int nV1,
  unsigned int nV2
){
  int mx = nPivot;
  int i;
  int nRes;
  Blob res;
  
  merge_try_to_resolve_conflict(p, nPivot, nV1, nV2, &res);
  nRes = blob_linecount(&res);
  if( nV1>mx ) mx = nV1;
  if( nV2>mx ) mx = nV2;
  if( nRes>mx ) mx = nRes;
  if( nRes>0 ){
    blob_appendf(p->pOut, "\"S0 0 0 %d\" . . .\n", nV2+2);







|







658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  unsigned int nV1,
  unsigned int nV2
){
  int mx = nPivot;
  int i;
  int nRes;
  Blob res;

  merge_try_to_resolve_conflict(p, nPivot, nV1, nV2, &res);
  nRes = blob_linecount(&res);
  if( nV1>mx ) mx = nV1;
  if( nV2>mx ) mx = nV2;
  if( nRes>mx ) mx = nRes;
  if( nRes>0 ){
    blob_appendf(p->pOut, "\"S0 0 0 %d\" . . .\n", nV2+2);
680
681
682
683
684
685
686











































































































687
688
689
690
691
692
693
  p->xStart = tclStart;
  p->xSame = tclSame;
  p->xChngV1 = tclChngV1;
  p->xChngV2 = tclChngV2;
  p->xChngBoth = tclChngBoth;
  p->xConflict = tclConflict;
}











































































































/*****************************************************************************/

/*
** The aC[] array contains triples of integers.  Within each triple, the
** elements are:
**
**   (0)  The number of lines to copy







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







710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
  p->xStart = tclStart;
  p->xSame = tclSame;
  p->xChngV1 = tclChngV1;
  p->xChngV2 = tclChngV2;
  p->xChngBoth = tclChngBoth;
  p->xConflict = tclConflict;
}

/*
** Works like blob_copy_lines() except that if pTo is not NULL, the
** copied content is HTML-escaped.
*/
/*static*/ void htmlCopyLines(Blob *pTo, Blob *pFrom, int N){
  const unsigned int iStart = pFrom->iCursor;
  blob_copy_lines(0, pFrom, N);
  if( pTo ){
    assert( iStart <= pFrom->iCursor );
    htmlize_to_blob(pTo, blob_str(pFrom) + iStart, (int)(pFrom->iCursor - iStart));
  }
}

/* MergeBuilderHtml::xStart() */
static void htmlStart(MergeBuilder *p){
  MergeBuilderHtml *pH = (MergeBuilderHtml*)p;
  unsigned int i;

  for(i = 0; i < sizeof(pH->aCol)/sizeof(Blob); ++i){
    blob_zero(&pH->aCol[i]);
  }
  /* Reminder: MergeBuilder does not currently contain enough state to
  ** let us include version info in this diff. We have the raw content
  ** of p->pPivot and p->pV2, but p->pV1 may be locally edited.  We
  ** can't readily know whether we need to use SHA1 or SHA3 to find it
  ** in the blob table. */
  blob_append(p->pOut,
              "<table class='diff'>", -1);
  blob_append(p->pOut, "<thead><tr class='diffchunk'>", -1);
#define DCOL(KEY,KLASS,DUMMY,FILENAME)                                  \
  blob_appendf(&pH->aCol[KEY], "<td class='" KLASS "'><pre>%h", DUMMY); \
  blob_append(p->pOut, "<td class='" KLASS "'>", -1 );                  \
  if( FILENAME ) blob_appendf(p->pOut, "%h", FILENAME);                 \
  blob_append(p->pOut, "</td>", -1)
  DCOL(MBH_COL_BASELINE_LN,  "mrgBaseLn diffln", "###", 0);
  DCOL(MBH_COL_BASELINE,     "mrgBase", "", p->zPivot);
  DCOL(MBH_COL_BASELINE_SEP, "mrgBaseSep diffsep", " ", 0);
  DCOL(MBH_COL_LOCAL_LN,     "mrgLocalLn diffln", "###", 0);
  DCOL(MBH_COL_LOCAL,        "mrgLocal", "", p->zV1);
  DCOL(MBH_COL_LOCAL_SEP,    "mrgLocalSep diffsep", " ", 0);
  DCOL(MBH_COL_MERGEDIN_LN,  "mrgMILn diffln", "###", 0);
  DCOL(MBH_COL_MERGEDIN,     "mrgMI", "", p->zV2);
  DCOL(MBH_COL_MERGEDIN_SEP, "mrgMISep diffsep", " ", 0);
  DCOL(MBH_COL_RESULT_LN,    "mrgResLn diffln", "###", 0);
  DCOL(MBH_COL_RESULT,       "mrgRes", "", p->zOut);
#undef DCOL
  blob_append(p->pOut, "</tr></thead>\n"
              "<tbody><tr class='diffchunk'>\n", -1);
}

/* MergeBuilderHtml::xEnd() */
static void htmlEnd(MergeBuilder *p){
  MergeBuilderHtml *pH = (MergeBuilderHtml*)p;
  unsigned int i;

  /* TODO: flush pH->aCol to p->pOut and close HTML table */
  for(i = 0; i < sizeof(pH->aCol)/sizeof(Blob); ++i){
    blob_appendf(&pH->aCol[i], "</pre></td><!-- end col-#%u -->", i);
    blob_append_xfer(p->pOut, &pH->aCol[i]);
  }
  blob_append(p->pOut, "</tbody></table>\n", -1);
  p->pV1 = p->pV2 = p->pPivot = p->pOut = 0;
  p->zPivot = p->zV1 = p->zV2 = p->zOut = 0;
}

/* MergeBuilderHtml::xSame() */
static void htmlSame(MergeBuilder *p, unsigned int N){
  /*dbgSame(p, N);*/
}
/* MergeBuilderHtml::xChngV1() */
static void htmlChngV1(MergeBuilder *p, unsigned int nPivot, unsigned int nV1){
  /*dbgChngV1(p, nPivot, nV1);*/
}
/* MergeBuilderHtml::xChngV2() */
static void htmlChngV2(MergeBuilder *p, unsigned int nPivot, unsigned int nV2){
  /*dbgChngV2(p, nPivot, nV2);*/
}
/* MergeBuilderHtml::xChngBoth() */
static void htmlChngBoth(MergeBuilder *p, unsigned int nPivot, unsigned int nV){
  /*dbgChngBoth(p, nPivot, nV);*/
}
/* MergeBuilderHtml::xConflict() */
static void htmlConflict(
  MergeBuilder *p,
  unsigned int nPivot,
  unsigned int nV1,
  unsigned int nV2
){
  /*dbgConflict(p, nPivot, nV1, nV2);*/
}
void mergebuilder_init_html(MergeBuilderHtml *pH){
  MergeBuilder *p = &pH->base;
  unsigned int i;
  mergebuilder_init(p);
  p->xStart = htmlStart;
  p->xEnd = htmlEnd;
  p->xSame = htmlSame;
  p->xChngV1 = htmlChngV1;
  p->xChngV2 = htmlChngV2;
  p->xChngBoth = htmlChngBoth;
  p->xConflict = htmlConflict;
  for(i = 0; i < sizeof(pH->aCol)/sizeof(Blob); ++i){
    pH->aCol[i] = empty_blob;
  }
}

/*****************************************************************************/

/*
** The aC[] array contains triples of integers.  Within each triple, the
** elements are:
**
**   (0)  The number of lines to copy
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
** The merge is an edit against pV2.  Both pV1 and pV2 have a
** common origin at pPivot.  Apply the changes of pPivot ==> pV1
** to pV2.
**
** The return is 0 upon complete success. If any input file is binary,
** -1 is returned and pOut is unmodified.  If there are merge
** conflicts, the merge proceeds as best as it can and the number
** of conflicts is returns
*/
int merge_three_blobs(MergeBuilder *p){
  int *aC1;              /* Changes from pPivot to pV1 */
  int *aC2;              /* Changes from pPivot to pV2 */
  int i1, i2;            /* Index into aC1[] and aC2[] */
  int nCpy, nDel, nIns;  /* Number of lines to copy, delete, or insert */
  int limit1, limit2;    /* Sizes of aC1[] and aC2[] */







|







879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
** The merge is an edit against pV2.  Both pV1 and pV2 have a
** common origin at pPivot.  Apply the changes of pPivot ==> pV1
** to pV2.
**
** The return is 0 upon complete success. If any input file is binary,
** -1 is returned and pOut is unmodified.  If there are merge
** conflicts, the merge proceeds as best as it can and the number
** of conflicts is returned.
*/
int merge_three_blobs(MergeBuilder *p){
  int *aC1;              /* Changes from pPivot to pV1 */
  int *aC2;              /* Changes from pPivot to pV2 */
  int i1, i2;            /* Index into aC1[] and aC2[] */
  int nCpy, nDel, nIns;  /* Number of lines to copy, delete, or insert */
  int limit1, limit2;    /* Sizes of aC1[] and aC2[] */
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
      while( !ends_with_copy(&aC1[i1], sz) || !ends_with_copy(&aC2[i2], sz) ){
        sz++;
      }
      i1 = skip_conflict(aC1, i1, sz, &nV1);
      i2 = skip_conflict(aC2, i2, sz, &nV2);
      p->xConflict(p, sz, nV1, nV2);
    }
 
    /* If we are finished with an edit triple, advance to the next
    ** triple.
    */
    if( i1<limit1 && aC1[i1]==0 && aC1[i1+1]==0 && aC1[i1+2]==0 ) i1+=3;
    if( i2<limit2 && aC2[i2]==0 && aC2[i2+1]==0 && aC2[i2+2]==0 ) i2+=3;
  }








|







983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
      while( !ends_with_copy(&aC1[i1], sz) || !ends_with_copy(&aC2[i2], sz) ){
        sz++;
      }
      i1 = skip_conflict(aC1, i1, sz, &nV1);
      i2 = skip_conflict(aC2, i2, sz, &nV2);
      p->xConflict(p, sz, nV1, nV2);
    }

    /* If we are finished with an edit triple, advance to the next
    ** triple.
    */
    if( i1<limit1 && aC1[i1]==0 && aC1[i1+1]==0 && aC1[i1+2]==0 ) i1+=3;
    if( i2<limit2 && aC2[i2]==0 && aC2[i2+1]==0 && aC2[i2+2]==0 ) i2+=3;
  }