1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
** Copyright (c) 2011 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
**
** Author contact information:
** drh@hwaci.com
** http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This file contains code used to manage the "leaf" table of the
** repository.
**
** The LEAF table contains the rids for all leaves in the check-in DAG.
** A leaf is a check-in that has no children in the same branch.
*/
#include "config.h"
#include "leaf.h"
#include <assert.h>
/*
** Return true if the check-in with RID=rid is a leaf.
**
** A leaf has no children in the same branch.
*/
int is_a_leaf(int rid){
int rc;
static const char zSql[] =
@ SELECT 1 FROM plink
@ WHERE pid=%d
@ AND coalesce((SELECT value FROM tagxref
@ WHERE tagid=%d AND rid=plink.pid), 'trunk')
@ =coalesce((SELECT value FROM tagxref
@ WHERE tagid=%d AND rid=plink.cid), 'trunk')
;
rc = db_int(0, zSql /*works-like:"%d,%d,%d"*/,
rid, TAG_BRANCH, TAG_BRANCH);
return rc==0;
}
/*
** Count the number of primary non-branch children for the given check-in.
**
** A primary child is one where the parent is the primary parent, not
** a merge parent. A "leaf" is a node that has zero children of any
** kind. This routine counts only primary children.
**
** A non-branch child is one which is on the same branch as the parent.
*/
int count_nonbranch_children(int pid){
int nNonBranch = 0;
static Stmt q;
static const char zSql[] =
@ SELECT count(*) FROM plink
@ WHERE pid=:pid AND isprim
@ AND coalesce((SELECT value FROM tagxref
@ WHERE tagid=%d AND rid=plink.pid), 'trunk')
@ =coalesce((SELECT value FROM tagxref
@ WHERE tagid=%d AND rid=plink.cid), 'trunk')
;
db_static_prepare(&q, zSql /*works-like: "%d,%d"*/, TAG_BRANCH, TAG_BRANCH);
db_bind_int(&q, ":pid", pid);
if( db_step(&q)==SQLITE_ROW ){
nNonBranch = db_column_int(&q, 0);
}
db_reset(&q);
return nNonBranch;
}
/*
** Recompute the entire LEAF table.
**
** This can be expensive (5 seconds or so) for a really large repository.
** So it is only done for things like a rebuild.
*/
void leaf_rebuild(void){
db_multi_exec(
"DELETE FROM leaf;"
"INSERT OR IGNORE INTO leaf"
" SELECT cid FROM plink"
" EXCEPT"
" SELECT pid FROM plink"
" WHERE coalesce((SELECT value FROM tagxref"
" WHERE tagid=%d AND rid=plink.pid),'trunk')"
" == coalesce((SELECT value FROM tagxref"
" WHERE tagid=%d AND rid=plink.cid),'trunk')",
TAG_BRANCH, TAG_BRANCH
);
}
/*
** A bag of check-ins whose leaf status needs to be checked.
*/
static Bag needToCheck;
/*
** Check to see if check-in "rid" is a leaf and either add it to the LEAF
** table if it is, or remove it if it is not.
*/
void leaf_check(int rid){
static Stmt checkIfLeaf;
static Stmt addLeaf;
static Stmt removeLeaf;
int rc;
db_static_prepare(&checkIfLeaf,
"SELECT 1 FROM plink"
" WHERE pid=:rid"
" AND coalesce((SELECT value FROM tagxref"
" WHERE tagid=%d AND rid=:rid),'trunk')"
" == coalesce((SELECT value FROM tagxref"
" WHERE tagid=%d AND rid=plink.cid),'trunk');",
TAG_BRANCH, TAG_BRANCH
);
db_bind_int(&checkIfLeaf, ":rid", rid);
rc = db_step(&checkIfLeaf);
db_reset(&checkIfLeaf);
if( rc==SQLITE_ROW ){
db_static_prepare(&removeLeaf, "DELETE FROM leaf WHERE rid=:rid");
db_bind_int(&removeLeaf, ":rid", rid);
db_step(&removeLeaf);
db_reset(&removeLeaf);
}else{
db_static_prepare(&addLeaf, "INSERT OR IGNORE INTO leaf VALUES(:rid)");
db_bind_int(&addLeaf, ":rid", rid);
db_step(&addLeaf);
db_reset(&addLeaf);
}
}
/*
** Return an SQL expression (stored in memory obtained from fossil_malloc())
** that is true if the SQL variable named "zVar" contains the rid with
** a CLOSED tag. In other words, return true if the leaf is closed.
**
** The result can be prefaced with a NOT operator to get all leaves that
** are open.
*/
char *leaf_is_closed_sql(const char *zVar){
return mprintf(
"EXISTS(SELECT 1 FROM tagxref AS tx"
" WHERE tx.rid=%s"
" AND tx.tagid=%d"
" AND tx.tagtype>0)",
zVar, TAG_CLOSED
);
}
/*
** Returns true if vid refers to a closed leaf, else false. vid is
** assumed to refer to a manifest, but this function does not verify
** that.
*/
int leaf_is_closed(int vid){
return db_exists("SELECT 1 FROM tagxref"
" WHERE tagid=%d AND rid=%d AND tagtype>0",
TAG_CLOSED, vid);
}
/*
** Schedule a leaf check for "rid" and its parents.
*/
void leaf_eventually_check(int rid){
static Stmt parentsOf;
db_static_prepare(&parentsOf,
"SELECT pid FROM plink WHERE cid=:rid AND pid>0"
);
db_bind_int(&parentsOf, ":rid", rid);
bag_insert(&needToCheck, rid);
while( db_step(&parentsOf)==SQLITE_ROW ){
bag_insert(&needToCheck, db_column_int(&parentsOf, 0));
}
db_reset(&parentsOf);
}
/*
** Do all pending leaf checks.
*/
void leaf_do_pending_checks(void){
int rid;
for(rid=bag_first(&needToCheck); rid; rid=bag_next(&needToCheck,rid)){
leaf_check(rid);
}
bag_clear(&needToCheck);
}
/*
** If check-in rid is an open-leaf and there exists another
** open leaf on the same branch, then return 1.
**
** If check-in rid is not an open leaf, or if it is the only open leaf
** on its branch, then return 0.
*/
int leaf_ambiguity(int rid){
int rc; /* Result */
char zVal[30];
if( !is_a_leaf(rid) ) return 0;
sqlite3_snprintf(sizeof(zVal), zVal, "%d", rid);
rc = db_exists(
"SELECT 1 FROM leaf"
" WHERE NOT %z"
" AND rid<>%d"
" AND (SELECT value FROM tagxref WHERE tagid=%d AND rid=leaf.rid)="
" (SELECT value FROM tagxref WHERE tagid=%d AND rid=%d)"
" AND NOT %z",
leaf_is_closed_sql(zVal), rid, TAG_BRANCH, TAG_BRANCH, rid,
leaf_is_closed_sql("leaf.rid"));
return rc;
}
/*
** If check-in rid is an open-leaf and there exists another open leaf
** on the same branch, then print a detailed warning showing all open
** leaves on that branch.
*/
int leaf_ambiguity_warning(int rid, int currentCkout){
char *zBr;
Stmt q;
int n = 0;
Blob msg;
const char *zMainBranch;
if( leaf_ambiguity(rid)==0 ) return 0;
zMainBranch = db_main_branch();
zBr = db_text(0, "SELECT value FROM tagxref WHERE tagid=%d AND rid=%d",
TAG_BRANCH, rid);
if( zBr==0 ) zBr = fossil_strdup(zMainBranch);
blob_init(&msg, 0, 0);
blob_appendf(&msg, "WARNING: multiple open leaf check-ins on %s:", zBr);
db_prepare(&q,
"SELECT"
" (SELECT uuid FROM blob WHERE rid=leaf.rid),"
" (SELECT datetime(mtime,toLocal()) FROM event WHERE objid=leaf.rid),"
" leaf.rid"
" FROM leaf"
" WHERE (SELECT value FROM tagxref WHERE tagid=%d AND rid=leaf.rid)=%Q"
" AND NOT %z"
" ORDER BY 2 DESC",
TAG_BRANCH, zBr, leaf_is_closed_sql("leaf.rid")
);
while( db_step(&q)==SQLITE_ROW ){
blob_appendf(&msg, "\n (%d) %s [%S]%s",
++n, db_column_text(&q,1), db_column_text(&q,0),
db_column_int(&q,2)==currentCkout ? " (current)" : "");
}
db_finalize(&q);
fossil_warning("%s",blob_str(&msg));
blob_reset(&msg);
return 1;
}
/*
** COMMAND: test-leaf-ambiguity
**
** Usage: %fossil NAME ...
**
** Resolve each name on the command line and call leaf_ambiguity_warning()
** for each resulting RID.
*/
void leaf_ambiguity_warning_test(void){
int i;
int rid;
int rc;
db_find_and_open_repository(0,0);
verify_all_options();
for(i=2; i<g.argc; i++){
char *zUuid;
rid = name_to_typed_rid(g.argv[i], "ci");
zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
fossil_print("%s (rid=%d) %S ", g.argv[i], rid, zUuid ? zUuid : "(none)");
fossil_free(zUuid);
rc = leaf_ambiguity_warning(rid, rid);
if( rc==0 ) fossil_print(" ok\n");
}
}
|