Fossil SCM

fossil-scm / src / merge.c
Blame History Raw 1767 lines
1
/*
2
** Copyright (c) 2007 D. Richard Hipp
3
**
4
** This program is free software; you can redistribute it and/or
5
** modify it under the terms of the Simplified BSD License (also
6
** known as the "2-Clause License" or "FreeBSD License".)
7
8
** This program is distributed in the hope that it will be useful,
9
** but without any warranty; without even the implied warranty of
10
** merchantability or fitness for a particular purpose.
11
**
12
** Author contact information:
13
** [email protected]
14
** http://www.hwaci.com/drh/
15
**
16
*******************************************************************************
17
**
18
** This file contains code used to merge two or more branches into
19
** a single tree.
20
*/
21
#include "config.h"
22
#include "merge.h"
23
#include <assert.h>
24
25
26
/*
27
** Bring up a Tcl/Tk GUI to show details of the most recent merge.
28
*/
29
static void merge_info_tk(int bDark, int bAll, int nContext){
30
int i;
31
Blob script;
32
const char *zTempFile = 0;
33
int bDebug;
34
char *zCmd;
35
const char *zTclsh;
36
zTclsh = find_option("tclsh",0,1);
37
if( zTclsh==0 ){
38
zTclsh = db_get("tclsh",0);
39
}
40
/* The undocumented --script FILENAME option causes the Tk script to
41
** be written into the FILENAME instead of being run. This is used
42
** for testing and debugging. */
43
zTempFile = find_option("script",0,1);
44
bDebug = find_option("tkdebug",0,0)!=0;
45
verify_all_options();
46
47
blob_zero(&script);
48
blob_appendf(&script, "set ncontext %d\n", nContext);
49
blob_appendf(&script, "set fossilexe {\"%/\"}\n", g.nameOfExe);
50
blob_appendf(&script, "set fossilcmd {| \"%/\" merge-info}\n",
51
g.nameOfExe);
52
blob_appendf(&script, "set filelist [list");
53
if( g.argc==2 ){
54
/* No files named on the command-line. Use every file mentioned
55
** in the MERGESTAT table to generate the file list. */
56
Stmt q;
57
int cnt = 0;
58
db_prepare(&q,
59
"WITH priority(op,pri) AS (VALUES('CONFLICT',0),('ERROR',0),"
60
"('MERGE',1),('ADDED',2),('UPDATE',2))"
61
"SELECT coalesce(fnr,fn), op FROM mergestat JOIN priority USING(op)"
62
" %s ORDER BY pri, 1",
63
bAll ? "" : "WHERE op IN ('MERGE','CONFLICT')" /*safe-for-%s*/
64
);
65
while( db_step(&q)==SQLITE_ROW ){
66
blob_appendf(&script," %s ", db_column_text(&q,1));
67
blob_append_tcl_literal(&script, db_column_text(&q,0),
68
db_column_bytes(&q,0));
69
cnt++;
70
}
71
db_finalize(&q);
72
if( cnt==0 ){
73
fossil_print(
74
"No interesting changes in this merge. Use --all to see everything\n"
75
);
76
return;
77
}
78
}else{
79
/* Use only files named on the command-line in the file list.
80
** But verify each file named is actually found in the MERGESTAT
81
** table first. */
82
for(i=2; i<g.argc; i++){
83
char *zFile; /* Input filename */
84
char *zTreename; /* Name of the file in the tree */
85
Blob fname; /* Filename relative to root */
86
char *zOp; /* Operation on this file */
87
zFile = mprintf("%/", g.argv[i]);
88
file_tree_name(zFile, &fname, 0, 1);
89
fossil_free(zFile);
90
zTreename = blob_str(&fname);
91
zOp = db_text(0, "SELECT op FROM mergestat WHERE fn=%Q or fnr=%Q",
92
zTreename, zTreename);
93
blob_appendf(&script, " %s ", zOp);
94
fossil_free(zOp);
95
blob_append_tcl_literal(&script, zTreename, (int)strlen(zTreename));
96
blob_reset(&fname);
97
}
98
}
99
blob_appendf(&script, "]\n");
100
blob_appendf(&script, "set darkmode %d\n", bDark!=0);
101
blob_appendf(&script, "set debug %d\n", bDebug!=0);
102
blob_appendf(&script, "%s", builtin_file("merge.tcl", 0));
103
if( zTempFile ){
104
blob_write_to_file(&script, zTempFile);
105
fossil_print("To see the merge, run: %s \"%s\"\n", zTclsh, zTempFile);
106
}else{
107
#if defined(FOSSIL_ENABLE_TCL)
108
Th_FossilInit(TH_INIT_DEFAULT);
109
if( evaluateTclWithEvents(g.interp, &g.tcl, blob_str(&script),
110
blob_size(&script), 1, 1, 0)==TCL_OK ){
111
blob_reset(&script);
112
return;
113
}
114
/*
115
* If evaluation of the Tcl script fails, the reason may be that Tk
116
* could not be found by the loaded Tcl, or that Tcl cannot be loaded
117
* dynamically (e.g. x64 Tcl with x86 Fossil). Therefore, fallback
118
* to using the external "tclsh", if available.
119
*/
120
#endif
121
zTempFile = write_blob_to_temp_file(&script);
122
zCmd = mprintf("%$ %$", zTclsh, zTempFile);
123
if( bDebug ){
124
fossil_print("%s\n", zCmd);
125
fflush(stdout);
126
}
127
fossil_system(zCmd);
128
file_delete(zTempFile);
129
fossil_free(zCmd);
130
}
131
blob_reset(&script);
132
}
133
134
/*
135
** Generate a TCL list on standard output that can be fed into the
136
** merge.tcl script to show the details of the most recent merge
137
** command associated with file "zFName". zFName must be the filename
138
** relative to the root of the check-in - in other words a "tree name".
139
**
140
** When this routine is called, we know that the mergestat table
141
** exists, but we do not know if zFName is mentioned in that table.
142
**
143
** The diffMode variable has these values:
144
**
145
** 0 Standard 3-way diff
146
** 12 2-way diff between baseline and local
147
** 13 2-way diff between baseline and merge-in
148
** 14 2-way diff from baseline to checkout
149
** 23 2-way diff between local and merge-in
150
** 24 2-way diff from local to checkout
151
** 34 2-way diff from merge-in to checkout
152
**
153
** For non-zero values of diffMode the two digits represent the left
154
** and right content for a two-way merge, with these meanings:
155
**
156
** 1 The baseline or pivot of the 3-way (baseline)
157
** 2 The local value of the file prior to merge (local)
158
** 3 The file content being merged in (merge-in)
159
** 4 The file content currently in the checkout (current)
160
*/
161
static void merge_info_tcl(const char *zFName, int nContext, int diffMode){
162
const char *zTreename; /* Name of the file in the tree */
163
Stmt q; /* To query the MERGESTAT table */
164
MergeBuilder mb; /* The merge builder object */
165
Blob pivot,v1,v2,cx,out; /* Blobs for holding content */
166
const char *zFN; /* A filename */
167
int rid; /* RID value */
168
int sz; /* File size value */
169
170
zTreename = zFName;
171
db_prepare(&q,
172
/* 0 1 2 3 4 5 6 7 */
173
"SELECT fnp, ridp, fn, ridv, sz, fnm, ridm, fnr"
174
" FROM mergestat"
175
" WHERE fnp=%Q OR fnr=%Q",
176
zTreename, zTreename
177
);
178
if( db_step(&q)!=SQLITE_ROW ){
179
db_finalize(&q);
180
fossil_print("ERROR {don't know anything about file: %s}\n", zTreename);
181
return;
182
}
183
mergebuilder_init_tcl(&mb);
184
mb.nContext = nContext;
185
186
blob_zero(&pivot);
187
if( diffMode<=14 ){
188
/* For modes 0, 12, 13, and 14, load the baseline content in pivot */
189
zFN = db_column_text(&q, 0);
190
if( zFN==0 ){
191
/* No pivot because the file was added */
192
mb.zPivot = "(no baseline)";
193
}else{
194
mb.zPivot = mprintf("%s (baseline)", file_tail(zFN));
195
rid = db_column_int(&q, 1);
196
content_get(rid, &pivot);
197
}
198
mb.pPivot = &pivot;
199
}else{
200
/* for modes 23, 24, and 34, pivot is not used */
201
}
202
203
blob_zero(&cx);
204
if( (diffMode % 10)==4 ){
205
/* For modes 14, 24, and 34, read current value of the file from
206
** disk into out. */
207
char *zFile;
208
zFN = db_column_text(&q, 7);
209
zFile = mprintf("%s%s", g.zLocalRoot, zFN);
210
blob_read_from_file(&cx, zFile, ExtFILE);
211
fossil_free(zFile);
212
}else{
213
/* cx is uninitialized for modes 0, 12, 13, and 23. */
214
}
215
216
blob_zero(&v2);
217
if( diffMode==0 || diffMode==34 || (diffMode % 10)==3 ){
218
/* For modes 0, 13, 23, and 34 read the merge-in content into v2 */
219
zFN = db_column_text(&q, 5);
220
if( zFN==0 ){
221
/* File deleted in the merged-in branch */
222
mb.zV2 = "(deleted file)";
223
}else{
224
mb.zV2 = mprintf("%s (merge-in)", file_tail(zFN));
225
rid = db_column_int(&q, 6);
226
content_get(rid, &v2);
227
}
228
mb.pV2 = &v2;
229
}else{
230
/* v2 is uninitialized for modes 12, 14, and 24 */
231
}
232
233
blob_zero(&v1);
234
if( diffMode==0 || diffMode==12 || (diffMode/10)==2 ){
235
/* For modes 0, 12, 23, and 24, read the local content into v1 */
236
zFN = db_column_text(&q, 2);
237
if( zFN==0 ){
238
/* File added by merge */
239
mb.zV1 = "(no original)";
240
}else{
241
mb.zV1 = mprintf("%s (local)", file_tail(zFN));
242
rid = db_column_int(&q, 3);
243
sz = db_column_int(&q, 4);
244
if( rid==0 && sz>0 ){
245
/* The origin file had been edited so we'll have to pull its
246
** original content out of the undo buffer */
247
Stmt q2;
248
db_prepare(&q2,
249
"SELECT content FROM undo"
250
" WHERE pathname=%Q AND octet_length(content)=%d",
251
zFN, sz
252
);
253
blob_zero(&v1);
254
if( db_step(&q2)==SQLITE_ROW ){
255
db_column_blob(&q2, 0, &v1);
256
}else{
257
mb.zV1 = "(local content missing)";
258
}
259
db_finalize(&q2);
260
}else{
261
/* The origin file was unchanged when the merge first occurred */
262
content_get(rid, &v1);
263
}
264
}
265
mb.pV1 = &v1;
266
}else{
267
/* v1 is uninitialized for modes 12, 13, and 14 */
268
}
269
270
/* For two-way merges, here is where content is loaded
271
**
272
** left right description
273
** ----- ----- ---------------------------
274
** 12 pivot v1 baseline vs local
275
** 13 pivot v2 baseline vs merge-in
276
** 14 pivot cx baseline vs current
277
** 23 v1 v2 local vs merge-in
278
** 24 v1 cx local vs current
279
** 34 v2 cx merge-in vs current
280
*/
281
blob_zero(&out);
282
if( diffMode==0 ){
283
/* Set up the output and do a 3-way diff */
284
zFN = db_column_text(&q, 7);
285
if( zFN==0 ){
286
mb.zOut = "(Merge Result)";
287
}else{
288
mb.zOut = mprintf("%s (after merge)", file_tail(zFN));
289
}
290
mb.pOut = &out;
291
merge_three_blobs(&mb);
292
}else{
293
/* Set up to do a two-way diff */
294
Blob *pLeft, *pRight;
295
const char *zTagLeft, *zTagRight;
296
DiffConfig cfg;
297
memset(&cfg, 0, sizeof(cfg));
298
cfg.diffFlags = DIFF_TCL;
299
cfg.nContext = mb.nContext;
300
switch( diffMode/10 ){
301
case 1: {
302
pLeft = &pivot;
303
zTagLeft = "baseline";
304
break;
305
}
306
case 2: {
307
pLeft = &v1;
308
zTagLeft = "local";
309
break;
310
}
311
default:
312
case 3: {
313
pLeft = &v2;
314
zTagLeft = "merge-in";
315
break;
316
}
317
}
318
switch( diffMode % 10 ){
319
case 2: {
320
pRight = &v1;
321
zTagRight = "local";
322
break;
323
}
324
case 3: {
325
pRight = &v2;
326
zTagRight = "merge-in";
327
break;
328
}
329
default:
330
case 4: {
331
pRight = &cx;
332
zTagRight = "current";
333
break;
334
}
335
}
336
cfg.azLabel[0] = mprintf("%s (%s)", zFName, zTagLeft);
337
cfg.azLabel[1] = mprintf("%s (%s)", zFName, zTagRight);
338
diff_print_filenames("", "", &cfg, &out);
339
text_diff(pLeft, pRight, &out, &cfg);
340
fossil_free((char*)cfg.azLabel[0]);
341
fossil_free((char*)cfg.azLabel[1]);
342
}
343
blob_write_to_file(&out, "-");
344
mb.xDestroy(&mb);
345
blob_reset(&pivot);
346
blob_reset(&v1);
347
blob_reset(&v2);
348
blob_reset(&cx);
349
blob_reset(&out);
350
db_finalize(&q);
351
}
352
353
/*
354
** COMMAND: merge-info
355
**
356
** Usage: %fossil merge-info [OPTIONS]
357
**
358
** Display information about the most recent merge operation.
359
**
360
** Options:
361
** -a|--all Show all file changes that happened because of
362
** the merge. Normally only MERGE, CONFLICT, and ERROR
363
** lines are shown
364
** -c|--context N Show N lines of context around each change,
365
** with negative N meaning show all content. Only
366
** meaningful in combination with --tcl or --tk.
367
** --dark Use dark mode for the Tcl/Tk-based GUI
368
** -v|--verbose Show extra information about each merge operation
369
** --tk Bring up a Tcl/Tk GUI that shows the changes
370
** associated with the most recent merge.
371
**
372
** Options used internally by --tk:
373
** --diff12 FILE Bring up a separate --tk diff for just the baseline
374
** and local variants of FILE.
375
** --diff13 FILE Like --diff12 but for baseline versus merge-in
376
** --diff14 FILE Like --diff12 but for baseline versus current
377
** --diff23 FILE Like --diff12 but for local versus merge-in
378
** --diff24 FILE Like --diff12 but for local versus current
379
** --diff34 FILE Like --diff12 but for merge-in versus current
380
** --tcl FILE Generate (to stdout) a TCL list containing
381
** information needed to display the changes to
382
** FILE caused by the most recent merge. FILE must
383
** be a pathname relative to the root of the check-out.
384
**
385
** Debugging options available only when --tk is used:
386
** --tkdebug Show sub-commands run to implement --tk
387
** --script FILE Write script used to implement --tk into FILE
388
*/
389
void merge_info_cmd(void){
390
const char *zCnt;
391
const char *zTcl;
392
int bTk;
393
int bDark;
394
int bAll;
395
int bDetail;
396
int nContext;
397
Stmt q;
398
const char *zWhere;
399
int cnt = 0;
400
const char *zDiff2 = 0;
401
int diffMode = 0;
402
403
db_must_be_within_tree();
404
bTk = find_option("tk", 0, 0)!=0;
405
zTcl = find_option("tcl", 0, 1);
406
zCnt = find_option("context", "c", 1);
407
bDark = find_option("dark", 0, 0)!=0;
408
bAll = find_option("all", "a", 0)!=0;
409
bDetail = find_option("verbose", "v", 0)!=0;
410
if( (zDiff2 = find_option("diff12", 0, 1))!=0 ){
411
diffMode = 12;
412
}else
413
if( (zDiff2 = find_option("diff13", 0, 1))!=0 ){
414
diffMode = 13;
415
}else
416
if( (zDiff2 = find_option("diff14", 0, 1))!=0 ){
417
diffMode = 14;
418
}else
419
if( (zDiff2 = find_option("diff23", 0, 1))!=0 ){
420
diffMode = 23;
421
}else
422
if( (zDiff2 = find_option("diff24", 0, 1))!=0 ){
423
diffMode = 24;
424
}else
425
if( (zDiff2 = find_option("diff34", 0, 1))!=0 ){
426
diffMode = 34;
427
}
428
429
if( zCnt ){
430
nContext = atoi(zCnt);
431
if( nContext<0 ) nContext = 0xfffffff;
432
}else{
433
nContext = 6;
434
}
435
if( !db_table_exists("localdb","mergestat") ){
436
if( zTcl ){
437
fossil_print("ERROR {no merge data available}\n");
438
}else{
439
fossil_print("No merge data is available\n");
440
}
441
return;
442
}
443
if( bTk ){
444
merge_info_tk(bDark, bAll, nContext);
445
return;
446
}
447
if( zTcl ){
448
if( diffMode ) zTcl = zDiff2;
449
merge_info_tcl(zTcl, nContext, diffMode);
450
return;
451
}
452
if( diffMode ){
453
char *zCmd;
454
zCmd = mprintf("merge-info --diff%d %!$ -c %d%s",
455
diffMode, zDiff2, nContext, bDark ? " --dark" : "");
456
diff_tk(zCmd, g.argc);
457
fossil_free(zCmd);
458
return;
459
}
460
461
verify_all_options();
462
if( g.argc>2 ){
463
usage("[OPTIONS]");
464
}
465
466
if( bAll ){
467
zWhere = "";
468
}else{
469
zWhere = "WHERE op IN ('MERGE','CONFLICT','ERROR')";
470
}
471
if( !bDetail ){
472
db_prepare(&q,
473
"WITH priority(op,pri) AS (VALUES('CONFLICT',0),('ERROR',0),"
474
"('MERGE',1),('ADDED',2),('UPDATE',2))"
475
476
/* 0 1 2 */
477
"SELECT op, coalesce(fnr,fn), msg"
478
" FROM mergestat JOIN priority USING(op)"
479
" %s"
480
" ORDER BY pri, coalesce(fnr,fn)",
481
zWhere /*safe-for-%s*/
482
);
483
while( db_step(&q)==SQLITE_ROW ){
484
const char *zOp = db_column_text(&q, 0);
485
const char *zName = db_column_text(&q, 1);
486
const char *zErr = db_column_text(&q, 2);
487
if( zErr && fossil_strcmp(zOp,"CONFLICT")!=0 ){
488
fossil_print("%-9s %s (%s)\n", zOp, zName, zErr);
489
}else{
490
fossil_print("%-9s %s\n", zOp, zName);
491
}
492
cnt++;
493
}
494
}else{
495
fossil_print(
496
" Status Local Pivot Merge-in Filename\n"
497
"--------- ---------- ---------- ---------- ----------------\n");
498
db_prepare(&q,
499
"WITH priority(op,pri) AS (VALUES('CONFLICT',0),('ERROR',0),"
500
"('MERGE',1),('ADDED',2),('UPDATE',2))"
501
502
/* 0 1 2 */
503
"SELECT op, coalesce(fnr,fn), msg,"
504
" (SELECT uuid FROM blob WHERE blob.rid=mergestat.ridv)," /* 3: local */
505
" (SELECT uuid FROM blob WHERE blob.rid=mergestat.ridp)," /* 4: pivot */
506
" (SELECT uuid FROM blob WHERE blob.rid=mergestat.ridm)" /* 5: mergein*/
507
" FROM mergestat JOIN priority USING(op)"
508
" %s"
509
" ORDER BY pri, coalesce(fnr,fn)",
510
zWhere /*safe-for-%s*/
511
);
512
while( db_step(&q)==SQLITE_ROW ){
513
const char *zOp = db_column_text(&q, 0);
514
const char *zName = db_column_text(&q, 1);
515
const char *zErr = db_column_text(&q, 2);
516
const char *zLocal = db_column_text(&q, 3);
517
const char *zPivot = db_column_text(&q, 4);
518
const char *zMergein = db_column_text(&q, 5);
519
if( zLocal==0 ) zLocal = "";
520
if( zErr && fossil_strcmp(zOp,"CONFLICT")!=0 ){
521
fossil_print("%-9s %10.10s %10.10s %10.10s %s (%s)\n",
522
zOp, zLocal, zPivot, zMergein, zName, zErr);
523
}else{
524
fossil_print("%-9s %10.10s %10.10s %10.10s %s\n",
525
zOp, zLocal, zPivot, zMergein, zName);
526
}
527
cnt++;
528
}
529
}
530
db_finalize(&q);
531
if( !bAll && cnt==0 ){
532
fossil_print(
533
"No interesting changes in this merge. Use --all to see everything.\n"
534
);
535
}
536
}
537
538
/*
539
** Erase all information about prior merges. Do this, for example, after
540
** a commit.
541
*/
542
void merge_info_forget(void){
543
db_multi_exec(
544
"DROP TABLE IF EXISTS localdb.mergestat;"
545
"DELETE FROM localdb.vvar WHERE name glob 'mergestat-*';"
546
);
547
}
548
549
550
/*
551
** Initialize the MERGESTAT table.
552
**
553
** Notes about mergestat:
554
**
555
** * ridv is a positive integer and sz is NULL if the V file contained
556
** no local edits prior to the merge. If the V file was modified prior
557
** to the merge then ridv is NULL and sz is the size of the file prior
558
** to merge.
559
**
560
** * fnp, ridp, fn, ridv, and sz are all NULL for a file that was
561
** added by merge.
562
*/
563
void merge_info_init(void){
564
merge_info_forget();
565
db_multi_exec(
566
"CREATE TABLE localdb.mergestat(\n"
567
" op TEXT, -- 'UPDATE', 'ADDED', 'MERGE', etc...\n"
568
" fnp TEXT, -- Name of the pivot file (P)\n"
569
" ridp INT, -- RID for the pivot file\n"
570
" fn TEXT, -- Name of origin file (V)\n"
571
" ridv INT, -- RID for origin file, or NULL if previously edited\n"
572
" sz INT, -- Size of origin file in bytes, NULL if unedited\n"
573
" fnm TEXT, -- Name of the file being merged in (M)\n"
574
" ridm INT, -- RID for the merge-in file\n"
575
" fnr TEXT, -- Name of the final output file, after all renaming\n"
576
" nc INT DEFAULT 0, -- Number of conflicts\n"
577
" msg TEXT -- Error message\n"
578
");"
579
);
580
}
581
582
/*
583
** Print information about a particular check-in.
584
*/
585
void print_checkin_description(int rid, int indent, const char *zLabel){
586
Stmt q;
587
db_prepare(&q,
588
"SELECT datetime(mtime,toLocal()),"
589
" coalesce(euser,user), coalesce(ecomment,comment),"
590
" (SELECT uuid FROM blob WHERE rid=%d),"
591
" (SELECT group_concat(substr(tagname,5), ', ') FROM tag, tagxref"
592
" WHERE tagname GLOB 'sym-*' AND tag.tagid=tagxref.tagid"
593
" AND tagxref.rid=%d AND tagxref.tagtype>0)"
594
" FROM event WHERE objid=%d", rid, rid, rid);
595
if( db_step(&q)==SQLITE_ROW ){
596
const char *zTagList = db_column_text(&q, 4);
597
char *zCom;
598
if( zTagList && zTagList[0] ){
599
zCom = mprintf("%s (%s)", db_column_text(&q, 2), zTagList);
600
}else{
601
zCom = fossil_strdup(db_column_text(&q,2));
602
}
603
fossil_print("%-*s [%S] by %s on %s\n%*s",
604
indent-1, zLabel,
605
db_column_text(&q, 3),
606
db_column_text(&q, 1),
607
db_column_text(&q, 0),
608
indent, "");
609
comment_print(zCom, db_column_text(&q,2), indent, -1, get_comment_format());
610
fossil_free(zCom);
611
}
612
db_finalize(&q);
613
}
614
615
616
/* Pick the most recent leaf that is (1) not equal to vid and (2)
617
** has not already been merged into vid and (3) the leaf is not
618
** closed and (4) the leaf is in the same branch as vid.
619
**
620
** Set vmergeFlag to control whether the vmerge table is checked.
621
*/
622
int fossil_find_nearest_fork(int vid, int vmergeFlag){
623
Blob sql;
624
Stmt q;
625
int rid = 0;
626
627
blob_zero(&sql);
628
blob_append_sql(&sql,
629
"SELECT leaf.rid"
630
" FROM leaf, event"
631
" WHERE leaf.rid=event.objid"
632
" AND leaf.rid!=%d", /* Constraint (1) */
633
vid
634
);
635
if( vmergeFlag ){
636
blob_append_sql(&sql,
637
" AND leaf.rid NOT IN (SELECT merge FROM vmerge)" /* Constraint (2) */
638
);
639
}
640
blob_append_sql(&sql,
641
" AND NOT EXISTS(SELECT 1 FROM tagxref" /* Constraint (3) */
642
" WHERE rid=leaf.rid"
643
" AND tagid=%d"
644
" AND tagtype>0)"
645
" AND (SELECT value FROM tagxref" /* Constraint (4) */
646
" WHERE tagid=%d AND rid=%d AND tagtype>0) ="
647
" (SELECT value FROM tagxref"
648
" WHERE tagid=%d AND rid=leaf.rid AND tagtype>0)"
649
" ORDER BY event.mtime DESC LIMIT 1",
650
TAG_CLOSED, TAG_BRANCH, vid, TAG_BRANCH
651
);
652
db_prepare(&q, "%s", blob_sql_text(&sql));
653
blob_reset(&sql);
654
if( db_step(&q)==SQLITE_ROW ){
655
rid = db_column_int(&q, 0);
656
}
657
db_finalize(&q);
658
return rid;
659
}
660
661
/*
662
** Check content that was received with rcvid and return true if any
663
** fork was created.
664
*/
665
int fossil_any_has_fork(int rcvid){
666
static Stmt q;
667
int fForkSeen = 0;
668
669
if( rcvid==0 ) return 0;
670
db_static_prepare(&q,
671
" SELECT pid FROM plink WHERE pid>0 AND isprim"
672
" AND cid IN (SELECT blob.rid FROM blob"
673
" WHERE rcvid=:rcvid)");
674
db_bind_int(&q, ":rcvid", rcvid);
675
while( !fForkSeen && db_step(&q)==SQLITE_ROW ){
676
int pid = db_column_int(&q, 0);
677
if( count_nonbranch_children(pid)>1 ){
678
compute_leaves(pid,1);
679
if( db_int(0, "SELECT count(*) FROM leaves")>1 ){
680
int rid = db_int(0, "SELECT rid FROM leaves, event"
681
" WHERE event.objid=leaves.rid"
682
" ORDER BY event.mtime DESC LIMIT 1");
683
fForkSeen = fossil_find_nearest_fork(rid, db_open_local(0))!=0;
684
}
685
}
686
}
687
db_finalize(&q);
688
return fForkSeen;
689
}
690
691
/*
692
** Add an entry to the FV table for all files renamed between
693
** version N and the version specified by vid.
694
*/
695
static void add_renames(
696
const char *zFnCol, /* The FV column for the filename in vid */
697
int vid, /* The desired version's- RID */
698
int nid, /* The check-in rid for the name pivot */
699
int revOK, /* OK to move backwards (child->parent) if true */
700
const char *zDebug /* Generate trace output if not NULL */
701
){
702
int nChng; /* Number of file name changes */
703
int *aChng; /* An array of file name changes */
704
int i; /* Loop counter */
705
find_filename_changes(nid, vid, revOK, &nChng, &aChng, zDebug);
706
if( nChng==0 ) return;
707
for(i=0; i<nChng; i++){
708
char *zN, *zV;
709
zN = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2]);
710
zV = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2+1]);
711
db_multi_exec(
712
"INSERT OR IGNORE INTO fv(%s,fnn) VALUES(%Q,%Q)",
713
zFnCol /*safe-for-%s*/, zV, zN
714
);
715
if( db_changes()==0 ){
716
db_multi_exec(
717
"UPDATE fv SET %s=%Q WHERE fnn=%Q",
718
zFnCol /*safe-for-%s*/, zV, zN
719
);
720
}
721
free(zN);
722
free(zV);
723
}
724
free(aChng);
725
}
726
727
/* Make an entry in the vmerge table for the given id, and rid.
728
*/
729
static void vmerge_insert(int id, int rid){
730
db_multi_exec(
731
"INSERT OR IGNORE INTO vmerge(id,merge,mhash)"
732
"VALUES(%d,%d,(SELECT uuid FROM blob WHERE rid=%d))",
733
id, rid, rid
734
);
735
}
736
737
/*
738
** Print the contents of the "fv" table on standard output, for debugging
739
** purposes.
740
**
741
** Only show entries where a file has changed, unless showAll is true.
742
*/
743
static void debug_fv_dump(int showAll){
744
Stmt q;
745
if( showAll ){
746
db_prepare(&q,
747
"SELECT rowid, fn, fnp, fnm, chnged, ridv, ridp, ridm, "
748
" isexe, islinkv, islinkm, fnn FROM fv"
749
);
750
}else{
751
db_prepare(&q,
752
"SELECT rowid, fn, fnp, fnm, chnged, ridv, ridp, ridm, "
753
" isexe, islinkv, islinkm, fnn FROM fv"
754
" WHERE chnged OR (ridv!=ridm AND ridm!=ridp)"
755
);
756
}
757
while( db_step(&q)==SQLITE_ROW ){
758
fossil_print("%3d: ridv=%-4d ridp=%-4d ridm=%-4d chnged=%d isexe=%d "
759
" islinkv=%d islinkm=%d\n",
760
db_column_int(&q, 0),
761
db_column_int(&q, 5),
762
db_column_int(&q, 6),
763
db_column_int(&q, 7),
764
db_column_int(&q, 4),
765
db_column_int(&q, 8),
766
db_column_int(&q, 9),
767
db_column_int(&q, 10));
768
fossil_print(" fn = [%s]\n", db_column_text(&q, 1));
769
fossil_print(" fnp = [%s]\n", db_column_text(&q, 2));
770
fossil_print(" fnm = [%s]\n", db_column_text(&q, 3));
771
fossil_print(" fnn = [%s]\n", db_column_text(&q, 11));
772
}
773
db_finalize(&q);
774
}
775
776
/*
777
** Print the content of the VFILE table on standard output, for
778
** debugging purposes.
779
*/
780
static void debug_show_vfile(void){
781
Stmt q;
782
int pvid = -1;
783
db_prepare(&q,
784
"SELECT vid, id, chnged, deleted, isexe, islink, rid, mrid, mtime,"
785
" pathname, origname, mhash FROM vfile"
786
" ORDER BY vid, pathname"
787
);
788
while( db_step(&q)==SQLITE_ROW ){
789
int vid = db_column_int(&q, 0);
790
int chnged = db_column_int(&q, 2);
791
int dltd = db_column_int(&q, 3);
792
int isexe = db_column_int(&q, 4);
793
int islnk = db_column_int(&q, 5);
794
int rid = db_column_int(&q, 6);
795
int mrid = db_column_int(&q, 7);
796
const char *zPath = db_column_text(&q, 9);
797
const char *zOrig = db_column_text(&q, 10);
798
if( vid!=pvid ){
799
fossil_print("VFILE vid=%d (%z):\n", vid,
800
db_text(0, "SELECT uuid FROM blob WHERE rid=%d", vid));
801
pvid = vid;
802
}
803
fossil_print(" rid %-6d mrid %-6d %4s %3s %3s %3s %s",
804
rid, mrid,
805
chnged ? "chng" : "",
806
dltd ? "del" : "",
807
isexe ? "exe" : "",
808
islnk ? "lnk" : "", zPath);
809
if( zOrig && zOrig[0] ){
810
fossil_print(" <- %s\n", zOrig);
811
}else{
812
fossil_print("\n");
813
}
814
}
815
db_finalize(&q);
816
}
817
818
/*
819
** COMMAND: test-show-vfile
820
** Usage: %fossil test-show-vfile
821
**
822
** Show the content of the VFILE table in a local check-out.
823
*/
824
void test_show_vfile_cmd(void){
825
if( g.argc!=2 ){
826
fossil_fatal("unknown arguments to the %s command\n", g.argv[1]);
827
}
828
verify_all_options();
829
db_must_be_within_tree();
830
debug_show_vfile();
831
}
832
833
834
/*
835
** COMMAND: merge
836
** COMMAND: cherry-pick alias
837
** COMMAND: cherrypick
838
**
839
** Usage: %fossil merge ?OPTIONS? ?VERSION ...?
840
** Or: %fossil cherrypick ?OPTIONS? ?VERSION ...?
841
**
842
** The argument VERSION is a version that should be merged into the
843
** current check-out. All changes from VERSION back to the nearest
844
** common ancestor are merged. Except, if either of the --cherrypick
845
** or --backout options are used only the changes associated with the
846
** single check-in VERSION are merged. The --backout option causes
847
** the changes associated with VERSION to be removed from the current
848
** check-out rather than added. When invoked with the name
849
** "cherrypick" instead of "merge", this command works exactly like
850
** "merge --cherrypick".
851
**
852
** Files which are renamed in the merged-in branch will be renamed in
853
** the current check-out.
854
**
855
** If the VERSION argument is omitted, then Fossil attempts to find
856
** a recent fork on the current branch to merge.
857
**
858
** Note that this command does not commit the merge, as that is a
859
** separate step.
860
**
861
** If there are multiple VERSION arguments, then each VERSION is merged
862
** (or cherrypicked) in the order that they appear on the command-line.
863
**
864
** Options:
865
** --backout Do a reverse cherrypick merge against VERSION.
866
** In other words, back out the changes that were
867
** added by VERSION.
868
** --baseline BASELINE Use BASELINE as the "pivot" of the merge instead
869
** of the nearest common ancestor. This allows
870
** a sequence of changes in a branch to be merged
871
** without having to merge the entire branch.
872
** --binary GLOBPATTERN Treat files that match GLOBPATTERN as binary
873
** and do not try to merge parallel changes. This
874
** option overrides the "binary-glob" setting.
875
** --cherrypick Do a cherrypick merge VERSION into the current
876
** check-out. A cherrypick merge pulls in the changes
877
** of the single check-in VERSION, rather than all
878
** changes back to the nearest common ancestor.
879
** -f|--force Force the merge even if it would be a no-op
880
** --force-missing Force the merge even if there is missing content
881
** --integrate Merged branch will be closed when committing
882
** -K|--keep-merge-files On merge conflict, retain the temporary files
883
** used for merging, named *-baseline, *-original,
884
** and *-merge.
885
** -n|--dry-run Do not actually change files on disk
886
** --nosync Do not auto-sync prior to merging
887
** --noundo Do not record changes in the undo log
888
** -v|--verbose Show additional details of the merge
889
*/
890
void merge_cmd(void){
891
int vid; /* Current version "V" */
892
int mid; /* Version we are merging from "M" */
893
int pid = 0; /* The pivot version - most recent common ancestor P */
894
int nid = 0; /* The name pivot version "N" */
895
int verboseFlag; /* True if the -v|--verbose option is present */
896
int integrateFlag; /* True if the --integrate option is present */
897
int pickFlag; /* True if the --cherrypick option is present */
898
int backoutFlag; /* True if the --backout option is present */
899
int dryRunFlag; /* True if the --dry-run or -n option is present */
900
int forceFlag; /* True if the --force or -f option is present */
901
int forceMissingFlag; /* True if the --force-missing option is present */
902
const char *zBinGlob; /* The value of --binary */
903
const char *zPivot; /* The value of --baseline */
904
int debugFlag; /* True if --debug is present */
905
int showVfileFlag; /* True if the --show-vfile flag is present */
906
int keepMergeFlag; /* True if --keep-merge-files is present */
907
int nConflict = 0; /* Number of conflicts seen */
908
int nOverwrite = 0; /* Number of unmanaged files overwritten */
909
char vAncestor = 'p'; /* If P is an ancestor of V then 'p', else 'n' */
910
const char *zVersion; /* The VERSION argument */
911
int bMultiMerge = 0; /* True if there are two or more VERSION arguments */
912
int nMerge = 0; /* Number of prior merges processed */
913
int useUndo = 1; /* True to record changes in the undo log */
914
Stmt q; /* SQL statement used for merge processing */
915
916
917
/* Notation:
918
**
919
** V The current check-out
920
** M The version being merged in
921
** P The "pivot" - the most recent common ancestor of V and M.
922
** N The "name pivot" - for detecting renames
923
*/
924
925
undo_capture_command_line();
926
verboseFlag = find_option("verbose","v",0)!=0;
927
forceMissingFlag = find_option("force-missing",0,0)!=0;
928
if( !verboseFlag ){
929
verboseFlag = find_option("detail",0,0)!=0; /* deprecated */
930
}
931
pickFlag = find_option("cherrypick",0,0)!=0;
932
if('c'==*g.zCmdName /*called as cherrypick, possibly a short form*/){
933
pickFlag = 1;
934
}
935
integrateFlag = find_option("integrate",0,0)!=0;
936
backoutFlag = find_option("backout",0,0)!=0;
937
zBinGlob = find_option("binary",0,1);
938
dryRunFlag = find_option("dry-run","n",0)!=0;
939
if( !dryRunFlag ){
940
dryRunFlag = find_option("nochange",0,0)!=0; /* deprecated */
941
}
942
if( find_option("nosync",0,0) ) g.fNoSync = 1;
943
forceFlag = find_option("force","f",0)!=0;
944
zPivot = find_option("baseline",0,1);
945
keepMergeFlag = find_option("keep-merge-files", "K",0)!=0;
946
947
/* Undocumented --debug and --show-vfile options:
948
**
949
** When included on the command-line, --debug causes lots of state
950
** information to be displayed. This option is undocumented as it
951
** might change or be eliminated in future releases.
952
**
953
** The --show-vfile flag does a dump of the VFILE table for reference.
954
**
955
** Hints:
956
** * Combine --debug and --verbose for still more output.
957
** * The --dry-run option is also useful in combination with --debug.
958
*/
959
debugFlag = find_option("debug",0,0)!=0;
960
if( debugFlag && verboseFlag ) debugFlag = 2;
961
showVfileFlag = find_option("show-vfile",0,0)!=0;
962
useUndo = find_option("noundo",0,0)==0;
963
if( dryRunFlag ) useUndo = 0;
964
965
verify_all_options();
966
db_must_be_within_tree();
967
if( zBinGlob==0 ) zBinGlob = db_get("binary-glob",0);
968
vid = db_lget_int("checkout", 0);
969
if( vid==0 ){
970
fossil_fatal("nothing is checked out");
971
}
972
if( forceFlag==0 && leaf_is_closed(vid) ){
973
fossil_fatal("cannot merge into a closed leaf. Use --force to override");
974
}
975
if( !dryRunFlag ){
976
if( autosync_loop(SYNC_PULL + SYNC_VERBOSE*verboseFlag, 1, "merge") ){
977
fossil_fatal("merge abandoned due to sync failure");
978
}
979
}
980
981
/*
982
** A "multi-merge" means two or more other check-ins are being merged
983
** into the current check-in. In other words, there are two or more
984
** VERSION arguments on the command-line. Multi-merge works by doing
985
** the merges one by one, as long as there are no conflicts. At the
986
** bottom of this routine, a jump is made back up to this point if there
987
** are more merges yet to be done and no errors have yet been seen.
988
**
989
** Related variables:
990
** bMultiMerge True if there are one or more merges yet to do
991
** zVersion The name of the current checking being merged in
992
** nMerge Number of prior merges
993
*/
994
merge_next_child:
995
996
/* Find mid, the artifactID of the version to be merged into
997
** the current check-out.
998
*/
999
if( g.argc>=3 ){
1000
int i;
1001
/* Mid is specified as an argument on the command-line */
1002
zVersion = g.argv[2];
1003
mid = name_to_typed_rid(zVersion, "ci");
1004
if( mid==0 || !is_a_version(mid) ){
1005
fossil_fatal("not a version: %s", zVersion);
1006
}
1007
bMultiMerge = g.argc>3;
1008
if( bMultiMerge ){
1009
for(i=3; i<g.argc; i++) g.argv[i-1] = g.argv[i];
1010
g.argc--;
1011
}
1012
}else if( g.argc==2 ){
1013
/* No version specified on the command-line so pick the most recent
1014
** leaf that is (1) not the version currently checked out and (2)
1015
** has not already been merged into the current check-out and (3)
1016
** the leaf is not closed and (4) the leaf is in the same branch
1017
** as the current check-out.
1018
*/
1019
Stmt q;
1020
if( pickFlag || backoutFlag || integrateFlag){
1021
fossil_fatal("cannot use --backout, --cherrypick or --integrate "
1022
"with a fork merge");
1023
}
1024
mid = fossil_find_nearest_fork(vid, db_open_local(0));
1025
if( mid==0 ){
1026
fossil_fatal("no unmerged forks of branch \"%s\"",
1027
db_text(0, "SELECT value FROM tagxref"
1028
" WHERE tagid=%d AND rid=%d AND tagtype>0",
1029
TAG_BRANCH, vid)
1030
);
1031
}
1032
db_prepare(&q,
1033
"SELECT blob.uuid,"
1034
" datetime(event.mtime,toLocal()),"
1035
" coalesce(ecomment, comment),"
1036
" coalesce(euser, user)"
1037
" FROM event, blob"
1038
" WHERE event.objid=%d AND blob.rid=%d",
1039
mid, mid
1040
);
1041
zVersion = 0;
1042
if( db_step(&q)==SQLITE_ROW ){
1043
char *zCom = mprintf("Merging fork [%S] at %s by %s: \"%s\"",
1044
db_column_text(&q, 0), db_column_text(&q, 1),
1045
db_column_text(&q, 3), db_column_text(&q, 2));
1046
comment_print(zCom, db_column_text(&q,2), 0, -1, get_comment_format());
1047
fossil_free(zCom);
1048
zVersion = mprintf("%S",db_column_text(&q,0));
1049
}
1050
db_finalize(&q);
1051
}else{
1052
usage("?OPTIONS? ?VERSION?");
1053
return;
1054
}
1055
1056
if( zPivot ){
1057
pid = name_to_typed_rid(zPivot, "ci");
1058
if( pid==0 || !is_a_version(pid) ){
1059
fossil_fatal("not a version: %s", zPivot);
1060
}
1061
if( pickFlag ){
1062
fossil_fatal("incompatible options: --cherrypick and --baseline");
1063
}
1064
}
1065
if( pickFlag || backoutFlag ){
1066
if( integrateFlag ){
1067
fossil_fatal("incompatible options: --integrate and --cherrypick "
1068
"with --backout");
1069
}
1070
pid = db_int(0, "SELECT pid FROM plink WHERE cid=%d AND isprim", mid);
1071
if( pid<=0 ){
1072
fossil_fatal("cannot find an ancestor for %s", zVersion);
1073
}
1074
}else{
1075
if( !zPivot ){
1076
pivot_set_primary(mid);
1077
pivot_set_secondary(vid);
1078
db_prepare(&q, "SELECT merge FROM vmerge WHERE id=0");
1079
while( db_step(&q)==SQLITE_ROW ){
1080
pivot_set_secondary(db_column_int(&q,0));
1081
}
1082
db_finalize(&q);
1083
pid = pivot_find(0);
1084
if( pid<=0 ){
1085
fossil_fatal("cannot find a common ancestor between the current "
1086
"check-out and %s", zVersion);
1087
}
1088
}
1089
pivot_set_primary(mid);
1090
pivot_set_secondary(vid);
1091
nid = pivot_find(1);
1092
if( nid!=pid ){
1093
pivot_set_primary(nid);
1094
pivot_set_secondary(pid);
1095
nid = pivot_find(1);
1096
}
1097
}
1098
if( backoutFlag ){
1099
int t = pid;
1100
pid = mid;
1101
mid = t;
1102
}
1103
if( nid==0 ) nid = pid;
1104
if( !is_a_version(pid) ){
1105
fossil_fatal("not a version: record #%d", pid);
1106
}
1107
if( !forceFlag && (mid==pid || mid==vid) ){
1108
fossil_print("Merge skipped because it is a no-op. "
1109
" Use --force to override.\n");
1110
return;
1111
}
1112
if( integrateFlag && !is_a_leaf(mid)){
1113
fossil_warning("ignoring --integrate: %s is not a leaf", zVersion);
1114
integrateFlag = 0;
1115
}
1116
if( integrateFlag && content_is_private(mid) ){
1117
fossil_warning(
1118
"ignoring --integrate: %s is on a private branch"
1119
"\n Use \"fossil amend --close\" (after commit) to close the leaf.",
1120
zVersion);
1121
integrateFlag = 0;
1122
}
1123
if( verboseFlag ){
1124
print_checkin_description(mid, 12,
1125
integrateFlag ? "integrate:" : "merge-from:");
1126
print_checkin_description(pid, 12, "baseline:");
1127
}
1128
vfile_check_signature(vid, CKSIG_ENOTFILE);
1129
if( nMerge==0 ) db_begin_transaction();
1130
if( useUndo && nMerge==0 ) undo_begin();
1131
if( load_vfile_from_rid(mid) && !forceMissingFlag ){
1132
fossil_fatal("missing content, unable to merge");
1133
}
1134
if( load_vfile_from_rid(pid) && !forceMissingFlag ){
1135
fossil_fatal("missing content, unable to merge");
1136
}
1137
if( zPivot ){
1138
vAncestor = db_exists(
1139
"WITH RECURSIVE ancestor(id) AS ("
1140
" VALUES(%d)"
1141
" UNION"
1142
" SELECT pid FROM plink, ancestor"
1143
" WHERE cid=ancestor.id AND pid!=%d AND cid!=%d)"
1144
"SELECT 1 FROM ancestor WHERE id=%d LIMIT 1",
1145
vid, nid, pid, pid
1146
) ? 'p' : 'n';
1147
}
1148
if( debugFlag ){
1149
char *z;
1150
z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", nid);
1151
fossil_print("N=%-4d %z (file rename pivot)\n", nid, z);
1152
z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", pid);
1153
fossil_print("P=%-4d %z (file content pivot)\n", pid, z);
1154
z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", mid);
1155
fossil_print("M=%-4d %z (merged-in version)\n", mid, z);
1156
z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", vid);
1157
fossil_print("V=%-4d %z (current version)\n", vid, z);
1158
fossil_print("vAncestor = '%c'\n", vAncestor);
1159
}
1160
if( showVfileFlag ) debug_show_vfile();
1161
1162
/*
1163
** The vfile.pathname field is used to match files against each other. The
1164
** FV table contains one row for each unique filename in
1165
** in the current check-out, the pivot, and the version being merged.
1166
*/
1167
db_multi_exec(
1168
"DROP TABLE IF EXISTS fv;"
1169
"CREATE TEMP TABLE fv(\n"
1170
" fn TEXT UNIQUE %s,\n" /* The filename */
1171
" idv INTEGER DEFAULT 0,\n" /* VFILE entry for current version */
1172
" idp INTEGER DEFAULT 0,\n" /* VFILE entry for the pivot */
1173
" idm INTEGER DEFAULT 0,\n" /* VFILE entry for version merging in */
1174
" chnged BOOLEAN,\n" /* True if current version has been edited */
1175
" ridv INTEGER DEFAULT 0,\n" /* Record ID for current version */
1176
" ridp INTEGER DEFAULT 0,\n" /* Record ID for pivot */
1177
" ridm INTEGER DEFAULT 0,\n" /* Record ID for merge */
1178
" isexe BOOLEAN,\n" /* Execute permission enabled */
1179
" fnp TEXT UNIQUE %s,\n" /* The filename in the pivot */
1180
" fnm TEXT UNIQUE %s,\n" /* The filename in the merged version */
1181
" fnn TEXT UNIQUE %s,\n" /* The filename in the name pivot */
1182
" islinkv BOOLEAN,\n" /* True if current version is a symlink */
1183
" islinkm BOOLEAN\n" /* True if merged version in is a symlink */
1184
");",
1185
filename_collation(), filename_collation(), filename_collation(),
1186
filename_collation()
1187
);
1188
1189
/*
1190
** Compute name changes from N to V, P, and M
1191
*/
1192
add_renames("fn", vid, nid, 0, debugFlag ? "N->V" : 0);
1193
add_renames("fnp", pid, nid, 0, debugFlag ? "N->P" : 0);
1194
add_renames("fnm", mid, nid, backoutFlag, debugFlag ? "N->M" : 0);
1195
if( debugFlag ){
1196
fossil_print("******** FV after name change search *******\n");
1197
debug_fv_dump(1);
1198
}
1199
if( nid!=pid ){
1200
/* See forum thread https://fossil-scm.org/forum/forumpost/549700437b
1201
**
1202
** If a filename changes between nid and one of the other check-ins
1203
** pid, vid, or mid, then it might not have changed for all of them.
1204
** try to fill in the appropriate filename in all slots where the
1205
** name is missing.
1206
**
1207
** This does not work if
1208
** (1) The filename changes more than once in between nid and vid/mid
1209
** (2) Two or more filenames swap places - for example if A is renamed
1210
** to B and B is renamed to A.
1211
** The Fossil merge algorithm breaks down in those cases. It will need
1212
** to be completely rewritten to handle such complex cases. Such cases
1213
** appear to be rare, and also confusing to humans.
1214
*/
1215
db_multi_exec(
1216
"UPDATE OR IGNORE fv SET fnp=vfile.pathname FROM vfile"
1217
" WHERE fnp IS NULL"
1218
" AND vfile.pathname = fv.fnn"
1219
" AND vfile.vid=%d;",
1220
pid
1221
);
1222
db_multi_exec(
1223
"UPDATE OR IGNORE fv SET fn=vfile.pathname FROM vfile"
1224
" WHERE fn IS NULL"
1225
" AND vfile.pathname = coalesce(fv.fnp,fv.fnn)"
1226
" AND vfile.vid=%d;",
1227
vid
1228
);
1229
db_multi_exec(
1230
"UPDATE OR IGNORE fv SET fnm=vfile.pathname FROM vfile"
1231
" WHERE fnm IS NULL"
1232
" AND vfile.pathname = coalesce(fv.fnp,fv.fnn)"
1233
" AND vfile.vid=%d;",
1234
mid
1235
);
1236
db_multi_exec(
1237
"UPDATE OR IGNORE fv SET fnp=vfile.pathname FROM vfile"
1238
" WHERE fnp IS NULL"
1239
" AND vfile.pathname IN (fv.fnm,fv.fn)"
1240
" AND vfile.vid=%d;",
1241
pid
1242
);
1243
db_multi_exec(
1244
"UPDATE OR IGNORE fv SET fn=vfile.pathname FROM vfile"
1245
" WHERE fn IS NULL"
1246
" AND vfile.pathname = fv.fnm"
1247
" AND vfile.vid=%d;",
1248
vid
1249
);
1250
db_multi_exec(
1251
"UPDATE OR IGNORE fv SET fnm=vfile.pathname FROM vfile"
1252
" WHERE fnm IS NULL"
1253
" AND vfile.pathname = fv.fn"
1254
" AND vfile.vid=%d;",
1255
mid
1256
);
1257
}
1258
if( debugFlag ){
1259
fossil_print("******** FV after name change fill-in *******\n");
1260
debug_fv_dump(1);
1261
}
1262
1263
/*
1264
** Add files found in V
1265
*/
1266
db_multi_exec(
1267
"UPDATE OR IGNORE fv SET fn=coalesce(fn%c,fnn) WHERE fn IS NULL;"
1268
"REPLACE INTO fv(fn,fnp,fnm,fnn,idv,ridv,islinkv,isexe,chnged)"
1269
" SELECT pathname, fnp, fnm, fnn, id, rid, islink, vf.isexe, vf.chnged"
1270
" FROM vfile vf"
1271
" LEFT JOIN fv ON fn=coalesce(origname,pathname)"
1272
" AND rid>0 AND vf.chnged NOT IN (3,5)"
1273
" WHERE vid=%d;",
1274
vAncestor, vid
1275
);
1276
if( debugFlag>=2 ){
1277
fossil_print("******** FV after adding files in current version *******\n");
1278
debug_fv_dump(1);
1279
}
1280
1281
/*
1282
** Add files found in P
1283
*/
1284
db_multi_exec(
1285
"UPDATE OR IGNORE fv SET fnp=coalesce(fnn,"
1286
" (SELECT coalesce(origname,pathname) FROM vfile WHERE id=idv))"
1287
" WHERE fnp IS NULL;"
1288
"INSERT OR IGNORE INTO fv(fnp)"
1289
" SELECT coalesce(origname,pathname) FROM vfile WHERE vid=%d;",
1290
pid
1291
);
1292
if( debugFlag>=2 ){
1293
fossil_print("******** FV after adding pivot files *******\n");
1294
debug_fv_dump(1);
1295
}
1296
1297
/*
1298
** Add files found in M
1299
*/
1300
db_multi_exec(
1301
"UPDATE OR IGNORE fv SET fnm=fnp WHERE fnm IS NULL;"
1302
"INSERT OR IGNORE INTO fv(fnm)"
1303
" SELECT pathname FROM vfile WHERE vid=%d;",
1304
mid
1305
);
1306
if( debugFlag>=2 ){
1307
fossil_print("******** FV after adding merge-in files *******\n");
1308
debug_fv_dump(1);
1309
}
1310
1311
/*
1312
** Compute the file version ids for P and M
1313
*/
1314
if( pid==vid ){
1315
db_multi_exec(
1316
"UPDATE fv SET idp=idv, ridp=ridv WHERE ridv>0 AND chnged NOT IN (3,5)"
1317
);
1318
}else{
1319
db_multi_exec(
1320
"UPDATE fv SET idp=coalesce(vfile.id,0), ridp=coalesce(vfile.rid,0)"
1321
" FROM vfile"
1322
" WHERE vfile.vid=%d AND fv.fnp=vfile.pathname",
1323
pid
1324
);
1325
}
1326
db_multi_exec(
1327
"UPDATE fv SET"
1328
" idm=coalesce(vfile.id,0),"
1329
" ridm=coalesce(vfile.rid,0),"
1330
" islinkm=coalesce(vfile.islink,0),"
1331
" isexe=coalesce(vfile.isexe,fv.isexe)"
1332
" FROM vfile"
1333
" WHERE vid=%d AND fnm=pathname",
1334
mid
1335
);
1336
1337
/*
1338
** Update the execute bit on files where it's changed from P->M but not P->V
1339
*/
1340
db_prepare(&q,
1341
"SELECT idv, fn, fv.isexe FROM fv, vfile p, vfile v"
1342
" WHERE p.id=idp AND v.id=idv AND fv.isexe!=p.isexe AND v.isexe=p.isexe"
1343
);
1344
while( db_step(&q)==SQLITE_ROW ){
1345
int idv = db_column_int(&q, 0);
1346
const char *zName = db_column_text(&q, 1);
1347
int isExe = db_column_int(&q, 2);
1348
fossil_print("%s %s\n", isExe ? "EXECUTABLE" : "UNEXEC", zName);
1349
if( !dryRunFlag ){
1350
char *zFullPath = mprintf("%s/%s", g.zLocalRoot, zName);
1351
file_setexe(zFullPath, isExe);
1352
free(zFullPath);
1353
db_multi_exec("UPDATE vfile SET isexe=%d WHERE id=%d", isExe, idv);
1354
}
1355
}
1356
db_finalize(&q);
1357
if( debugFlag ){
1358
fossil_print("******** FV final *******\n");
1359
debug_fv_dump( debugFlag>=2 );
1360
}
1361
1362
/************************************************************************
1363
** All of the information needed to do the merge is now contained in the
1364
** FV table. Starting here, we begin to actually carry out the merge.
1365
**
1366
** Begin by constructing the localdb.mergestat table.
1367
*/
1368
merge_info_init();
1369
1370
/*
1371
** Find files that have changed from P->M but not P->V.
1372
** Copy the M content over into V.
1373
*/
1374
db_prepare(&q,
1375
/* 0 1 2 3 4 5 6 7 */
1376
"SELECT idv, ridm, fn, islinkm, fnp, ridp, ridv, fnm FROM fv"
1377
" WHERE idp>0 AND idv>0 AND idm>0"
1378
" AND ridm!=ridp AND ridv=ridp AND NOT chnged"
1379
);
1380
while( db_step(&q)==SQLITE_ROW ){
1381
int idv = db_column_int(&q, 0);
1382
int ridm = db_column_int(&q, 1);
1383
const char *zName = db_column_text(&q, 2);
1384
int islinkm = db_column_int(&q, 3);
1385
/* Copy content from idm over into idv. Overwrite idv. */
1386
fossil_print("UPDATE %s\n", zName);
1387
if( useUndo ) undo_save(zName);
1388
if( !dryRunFlag ){
1389
db_multi_exec(
1390
"UPDATE vfile SET mtime=0, mrid=%d, chnged=%d, islink=%d,"
1391
" mhash=CASE WHEN rid<>%d"
1392
" THEN (SELECT uuid FROM blob WHERE blob.rid=%d) END"
1393
" WHERE id=%d", ridm, integrateFlag?4:2, islinkm, ridm, ridm, idv
1394
);
1395
vfile_to_disk(0, idv, 0, 0);
1396
}
1397
db_multi_exec(
1398
"INSERT INTO mergestat(op,fnp,ridp,fn,ridv,fnm,ridm,fnr)"
1399
"VALUES('UPDATE',%Q,%d,%Q,%d,%Q,%d,%Q)",
1400
/* fnp */ db_column_text(&q, 4),
1401
/* ridp */ db_column_int(&q,5),
1402
/* fn */ zName,
1403
/* ridv */ db_column_int(&q,6),
1404
/* fnm */ db_column_text(&q, 7),
1405
/* ridm */ ridm,
1406
/* fnr */ zName
1407
);
1408
}
1409
db_finalize(&q);
1410
1411
/*
1412
** Do a three-way merge on files that have changes on both P->M and P->V.
1413
**
1414
** Proceed even if the file doesn't exist on P, just like the common ancestor
1415
** of M and V is an empty file. In this case, merge conflict marks will be
1416
** added to the file and user will be forced to take a decision.
1417
*/
1418
db_prepare(&q,
1419
/* 0 1 2 3 4 5 6 7 8 */
1420
"SELECT ridm, idv, ridp, ridv, %z, fn, isexe, islinkv, islinkm,"
1421
/* 9 10 11 */
1422
" fnp, fnm, chnged"
1423
" FROM fv"
1424
" WHERE idv>0 AND idm>0"
1425
" AND ridm!=ridp AND (ridv!=ridp OR chnged)",
1426
glob_expr("fv.fn", zBinGlob)
1427
);
1428
while( db_step(&q)==SQLITE_ROW ){
1429
int ridm = db_column_int(&q, 0);
1430
int idv = db_column_int(&q, 1);
1431
int ridp = db_column_int(&q, 2);
1432
int ridv = db_column_int(&q, 3);
1433
int isBinary = db_column_int(&q, 4);
1434
const char *zName = db_column_text(&q, 5);
1435
int isExe = db_column_int(&q, 6);
1436
int islinkv = db_column_int(&q, 7);
1437
int islinkm = db_column_int(&q, 8);
1438
int chnged = db_column_int(&q, 11);
1439
int rc;
1440
char *zFullPath;
1441
const char *zType = "MERGE";
1442
Blob m, p, r;
1443
/* Do a 3-way merge of idp->idm into idp->idv. The results go into idv. */
1444
if( verboseFlag ){
1445
fossil_print("MERGE %s (pivot=%d v1=%d v2=%d)\n",
1446
zName, ridp, ridm, ridv);
1447
}else{
1448
fossil_print("MERGE %s\n", zName);
1449
}
1450
if( islinkv || islinkm ){
1451
fossil_print("***** Cannot merge symlink %s\n", zName);
1452
nConflict++;
1453
db_multi_exec(
1454
"INSERT INTO mergestat(op,fnp,ridp,fn,ridv,fnm,ridm,fnr,nc,msg)"
1455
"VALUES('ERROR',%Q,%d,%Q,%d,%Q,%d,%Q,1,'cannot merge symlink')",
1456
/* fnp */ db_column_text(&q, 9),
1457
/* ridp */ ridp,
1458
/* fn */ zName,
1459
/* ridv */ ridv,
1460
/* fnm */ db_column_text(&q, 10),
1461
/* ridm */ ridm,
1462
/* fnr */ zName
1463
);
1464
}else{
1465
i64 sz;
1466
const char *zErrMsg = 0;
1467
int nc = 0;
1468
1469
if( useUndo ) undo_save(zName);
1470
zFullPath = mprintf("%s/%s", g.zLocalRoot, zName);
1471
sz = file_size(zFullPath, ExtFILE);
1472
content_get(ridp, &p);
1473
content_get(ridm, &m);
1474
if( isBinary ){
1475
rc = -1;
1476
blob_zero(&r);
1477
}else{
1478
unsigned mergeFlags = dryRunFlag ? MERGE_DRYRUN : 0;
1479
if(keepMergeFlag!=0) mergeFlags |= MERGE_KEEP_FILES;
1480
rc = merge_3way(&p, zFullPath, &m, &r, mergeFlags);
1481
}
1482
if( rc>=0 ){
1483
if( !dryRunFlag ){
1484
blob_write_to_file(&r, zFullPath);
1485
file_setexe(zFullPath, isExe);
1486
}
1487
db_multi_exec("UPDATE vfile SET mtime=0 WHERE id=%d", idv);
1488
if( rc>0 ){
1489
fossil_print("***** %d merge conflict%s in %s\n",
1490
rc, rc>1 ? "s" : "", zName);
1491
nConflict++;
1492
nc = rc;
1493
zErrMsg = "merge conflicts";
1494
zType = "CONFLICT";
1495
}
1496
}else{
1497
fossil_print("***** Cannot merge binary file %s\n", zName);
1498
nConflict++;
1499
nc = 1;
1500
zErrMsg = "cannot merge binary file";
1501
zType = "ERROR";
1502
}
1503
db_multi_exec(
1504
"INSERT INTO mergestat(op,fnp,ridp,fn,ridv,sz,fnm,ridm,fnr,nc,msg)"
1505
"VALUES(%Q,%Q,%d,%Q,iif(%d,%d,NULL),iif(%d,%lld,NULL),%Q,%d,"
1506
"%Q,%d,%Q)",
1507
/* op */ zType,
1508
/* fnp */ db_column_text(&q, 9),
1509
/* ridp */ ridp,
1510
/* fn */ zName,
1511
/* ridv */ chnged==0, ridv,
1512
/* sz */ chnged!=0, sz,
1513
/* fnm */ db_column_text(&q, 10),
1514
/* ridm */ ridm,
1515
/* fnr */ zName,
1516
/* nc */ nc,
1517
/* msg */ zErrMsg
1518
);
1519
fossil_free(zFullPath);
1520
blob_reset(&p);
1521
blob_reset(&m);
1522
blob_reset(&r);
1523
}
1524
vmerge_insert(idv, ridm);
1525
}
1526
db_finalize(&q);
1527
1528
/*
1529
** Drop files that are in P and V but not in M
1530
*/
1531
db_prepare(&q,
1532
"SELECT idv, fn, chnged, ridv FROM fv"
1533
" WHERE idp>0 AND idv>0 AND idm=0"
1534
);
1535
while( db_step(&q)==SQLITE_ROW ){
1536
int idv = db_column_int(&q, 0);
1537
const char *zName = db_column_text(&q, 1);
1538
int chnged = db_column_int(&q, 2);
1539
int ridv = db_column_int(&q, 3);
1540
int sz = -1;
1541
const char *zErrMsg = 0;
1542
int nc = 0;
1543
/* Delete the file idv */
1544
fossil_print("DELETE %s\n", zName);
1545
if( chnged ){
1546
char *zFullPath;
1547
fossil_warning("WARNING: local edits lost for %s", zName);
1548
nConflict++;
1549
ridv = 0;
1550
nc = 1;
1551
zErrMsg = "local edits lost";
1552
zFullPath = mprintf("%s/%s", g.zLocalRoot, zName);
1553
sz = file_size(zFullPath, ExtFILE);
1554
fossil_free(zFullPath);
1555
}
1556
if( useUndo ) undo_save(zName);
1557
db_multi_exec(
1558
"UPDATE vfile SET deleted=1 WHERE id=%d", idv
1559
);
1560
if( !dryRunFlag ){
1561
char *zFullPath = mprintf("%s%s", g.zLocalRoot, zName);
1562
file_delete(zFullPath);
1563
free(zFullPath);
1564
}
1565
db_multi_exec(
1566
"INSERT INTO localdb.mergestat(op,fnp,ridp,fn,ridv,sz,fnm,ridm,nc,msg)"
1567
"VALUES('DELETE',NULL,NULL,%Q,iif(%d,%d,NULL),iif(%d,%d,NULL),"
1568
"NULL,NULL,%d,%Q)",
1569
/* fn */ zName,
1570
/* ridv */ chnged==0, ridv,
1571
/* sz */ chnged!=0, sz,
1572
/* nc */ nc,
1573
/* msg */ zErrMsg
1574
);
1575
}
1576
db_finalize(&q);
1577
1578
/* For certain sets of renames (e.g. A -> B and B -> A), a file that is
1579
** being renamed must first be moved to a temporary location to avoid
1580
** being overwritten by another rename operation. A row is added to the
1581
** TMPRN table for each of these temporary renames.
1582
*/
1583
db_multi_exec(
1584
"DROP TABLE IF EXISTS tmprn;"
1585
"CREATE TEMP TABLE tmprn(fn UNIQUE, tmpfn);"
1586
);
1587
1588
/*
1589
** Rename files that have taken a rename on P->M but which keep the same
1590
** name on P->V. If a file is renamed on P->V only or on both P->V and
1591
** P->M then we retain the V name of the file.
1592
*/
1593
db_prepare(&q,
1594
"SELECT idv, fnp, fnm, isexe FROM fv"
1595
" WHERE idv>0 AND idp>0 AND idm>0 AND fnp=fn AND fnm!=fnp"
1596
);
1597
while( db_step(&q)==SQLITE_ROW ){
1598
int idv = db_column_int(&q, 0);
1599
const char *zOldName = db_column_text(&q, 1);
1600
const char *zNewName = db_column_text(&q, 2);
1601
int isExe = db_column_int(&q, 3);
1602
fossil_print("RENAME %s -> %s\n", zOldName, zNewName);
1603
if( useUndo ) undo_save(zOldName);
1604
if( useUndo ) undo_save(zNewName);
1605
db_multi_exec(
1606
"UPDATE mergestat SET fnr=fnm WHERE fnp=%Q",
1607
zOldName
1608
);
1609
db_multi_exec(
1610
"UPDATE vfile SET pathname=NULL, origname=pathname"
1611
" WHERE vid=%d AND pathname=%Q;"
1612
"UPDATE vfile SET pathname=%Q, origname=coalesce(origname,pathname)"
1613
" WHERE id=%d;",
1614
vid, zNewName, zNewName, idv
1615
);
1616
if( !dryRunFlag ){
1617
char *zFullOldPath, *zFullNewPath;
1618
zFullOldPath = db_text(0,"SELECT tmpfn FROM tmprn WHERE fn=%Q", zOldName);
1619
if( !zFullOldPath ){
1620
zFullOldPath = mprintf("%s%s", g.zLocalRoot, zOldName);
1621
}
1622
zFullNewPath = mprintf("%s%s", g.zLocalRoot, zNewName);
1623
if( file_size(zFullNewPath, RepoFILE)>=0 ){
1624
Blob tmpPath;
1625
file_tempname(&tmpPath, "", 0);
1626
db_multi_exec("INSERT INTO tmprn(fn,tmpfn) VALUES(%Q,%Q)",
1627
zNewName, blob_str(&tmpPath));
1628
if( file_islink(zFullNewPath) ){
1629
symlink_copy(zFullNewPath, blob_str(&tmpPath));
1630
}else{
1631
file_copy(zFullNewPath, blob_str(&tmpPath));
1632
}
1633
blob_reset(&tmpPath);
1634
}
1635
if( file_islink(zFullOldPath) ){
1636
symlink_copy(zFullOldPath, zFullNewPath);
1637
}else{
1638
file_copy(zFullOldPath, zFullNewPath);
1639
}
1640
file_setexe(zFullNewPath, isExe);
1641
file_delete(zFullOldPath);
1642
free(zFullNewPath);
1643
free(zFullOldPath);
1644
}
1645
}
1646
db_finalize(&q);
1647
1648
/* A file that has been deleted and replaced by a renamed file will have a
1649
** NULL pathname. Change it to something that makes the output of "status"
1650
** and similar commands make sense for such files and that will (most likely)
1651
** not be an actual existing pathname.
1652
*/
1653
db_multi_exec(
1654
"UPDATE vfile SET pathname=origname || ' (overwritten by rename)'"
1655
" WHERE pathname IS NULL"
1656
);
1657
1658
/*
1659
** Insert into V any files that are not in V or P but are in M.
1660
*/
1661
db_prepare(&q,
1662
"SELECT idm, fnm, ridm FROM fv"
1663
" WHERE idp=0 AND idv=0 AND idm>0"
1664
);
1665
while( db_step(&q)==SQLITE_ROW ){
1666
int idm = db_column_int(&q, 0);
1667
const char *zName;
1668
char *zFullName;
1669
db_multi_exec(
1670
"REPLACE INTO vfile(vid,chnged,deleted,rid,mrid,"
1671
"isexe,islink,pathname,mhash)"
1672
" SELECT %d,%d,0,rid,mrid,isexe,islink,pathname,"
1673
"CASE WHEN rid<>mrid"
1674
" THEN (SELECT uuid FROM blob WHERE blob.rid=vfile.mrid) END "
1675
"FROM vfile WHERE id=%d",
1676
vid, integrateFlag?5:3, idm
1677
);
1678
zName = db_column_text(&q, 1);
1679
zFullName = mprintf("%s%s", g.zLocalRoot, zName);
1680
if( file_isfile_or_link(zFullName)
1681
&& !db_exists("SELECT 1 FROM fv WHERE fn=%Q", zName) ){
1682
/* Name of backup file with Original content */
1683
char *zOrig = file_newname(zFullName, "original", 1);
1684
/* Backup previously unmanaged file before being overwritten */
1685
file_copy(zFullName, zOrig);
1686
fossil_free(zOrig);
1687
fossil_print("ADDED %s (overwrites an unmanaged file)", zName);
1688
if( !dryRunFlag ) fossil_print(", original copy backed up locally");
1689
fossil_print("\n");
1690
nOverwrite++;
1691
}else{
1692
fossil_print("ADDED %s\n", zName);
1693
}
1694
fossil_free(zFullName);
1695
db_multi_exec(
1696
"INSERT INTO mergestat(op,fnm,ridm,fnr)"
1697
"VALUES('ADDED',%Q,%d,%Q)",
1698
/* fnm */ zName,
1699
/* ridm */ db_column_int(&q,2),
1700
/* fnr */ zName
1701
);
1702
if( useUndo ) undo_save(zName);
1703
if( !dryRunFlag ){
1704
vfile_to_disk(0, idm, 0, 0);
1705
}
1706
}
1707
db_finalize(&q);
1708
1709
/* Report on conflicts
1710
*/
1711
if( nConflict ){
1712
fossil_warning("WARNING: %d merge conflicts", nConflict);
1713
if( bMultiMerge ){
1714
int i;
1715
Blob msg;
1716
blob_init(&msg, 0, 0);
1717
blob_appendf(&msg,
1718
"The following %ss were not attempted due to prior conflicts:",
1719
pickFlag ? "cherrypick" : backoutFlag ? "backout" : "merge"
1720
);
1721
for(i=2; i<g.argc; i++){
1722
blob_appendf(&msg, " %s", g.argv[i]);
1723
}
1724
fossil_warning("%s", blob_str(&msg));
1725
blob_zero(&msg);
1726
}
1727
}
1728
if( nOverwrite ){
1729
fossil_warning("WARNING: %d unmanaged files were overwritten",
1730
nOverwrite);
1731
}
1732
if( dryRunFlag && !bMultiMerge ){
1733
fossil_warning("REMINDER: this was a dry run -"
1734
" no files were actually changed.");
1735
}
1736
1737
/*
1738
** Clean up the mid and pid VFILE entries. Then commit the changes.
1739
*/
1740
db_multi_exec("DELETE FROM vfile WHERE vid!=%d", vid);
1741
if( pickFlag ){
1742
vmerge_insert(-1, mid);
1743
/* For a cherrypick merge, make the default check-in comment the same
1744
** as the check-in comment on the check-in that is being merged in. */
1745
db_multi_exec(
1746
"REPLACE INTO vvar(name,value)"
1747
" SELECT 'ci-comment', coalesce(ecomment,comment) FROM event"
1748
" WHERE type='ci' AND objid=%d",
1749
mid
1750
);
1751
}else if( backoutFlag ){
1752
vmerge_insert(-2, pid);
1753
}else if( integrateFlag ){
1754
vmerge_insert(-4, mid);
1755
}else{
1756
vmerge_insert(0, mid);
1757
}
1758
if( bMultiMerge && nConflict==0 ){
1759
nMerge++;
1760
goto merge_next_child;
1761
}
1762
if( !dryRunFlag ) vfile_check_names_on_disk(vid);
1763
if( useUndo ) undo_finish();
1764
1765
db_end_transaction(dryRunFlag);
1766
}
1767

Keyboard Shortcuts

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