Fossil SCM

fossil-scm / src / name.c
Source Blame History 2671 lines
dbda8d6… drh 1 /*
c19f34c… drh 2 ** Copyright (c) 2006 D. Richard Hipp
dbda8d6… drh 3 **
dbda8d6… drh 4 ** This program is free software; you can redistribute it and/or
c06edd2… drh 5 ** modify it under the terms of the Simplified BSD License (also
c06edd2… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
fd9b7bd… drh 7 **
dbda8d6… drh 8 ** This program is distributed in the hope that it will be useful,
c06edd2… drh 9 ** but without any warranty; without even the implied warranty of
c06edd2… drh 10 ** merchantability or fitness for a particular purpose.
dbda8d6… drh 11 **
dbda8d6… drh 12 ** Author contact information:
dbda8d6… drh 13 ** [email protected]
dbda8d6… drh 14 ** http://www.hwaci.com/drh/
dbda8d6… drh 15 **
dbda8d6… drh 16 *******************************************************************************
dbda8d6… drh 17 **
fd9b7bd… drh 18 ** This file contains code used to resolved user-supplied object names.
dbda8d6… drh 19 */
dbda8d6… drh 20 #include "config.h"
dbda8d6… drh 21 #include "name.h"
dbda8d6… drh 22 #include <assert.h>
fd9b7bd… drh 23
4d8c302… stephan 24 #if INTERFACE
4d8c302… stephan 25 /*
4d8c302… stephan 26 ** An upper boundary on RIDs, provided in order to be able to
4d8c302… stephan 27 ** distinguish real RID values from RID_CKOUT and any future
4d8c302… stephan 28 ** RID_... values.
4d8c302… stephan 29 */
4d8c302… stephan 30 #define RID_MAX 0x7ffffff0
4d8c302… stephan 31 /*
4d8c302… stephan 32 ** A "magic" RID representing the current checkout in some contexts.
4d8c302… stephan 33 */
4d8c302… stephan 34 #define RID_CKOUT (RID_MAX+1)
4d8c302… stephan 35 #endif
4d8c302… stephan 36
7858a39… drh 37 /*
7858a39… drh 38 ** Return TRUE if the string begins with something that looks roughly
7858a39… drh 39 ** like an ISO date/time string. The SQLite date/time functions will
7858a39… drh 40 ** have the final say-so about whether or not the date/time string is
7858a39… drh 41 ** well-formed.
7858a39… drh 42 */
41c3c19… drh 43 int fossil_isdate(const char *z){
2fac809… drh 44 if( !fossil_isdigit(z[0]) ) return 0;
2fac809… drh 45 if( !fossil_isdigit(z[1]) ) return 0;
2fac809… drh 46 if( !fossil_isdigit(z[2]) ) return 0;
2fac809… drh 47 if( !fossil_isdigit(z[3]) ) return 0;
2fac809… drh 48 if( z[4]!='-') return 0;
2fac809… drh 49 if( !fossil_isdigit(z[5]) ) return 0;
2fac809… drh 50 if( !fossil_isdigit(z[6]) ) return 0;
2fac809… drh 51 if( z[7]!='-') return 0;
2fac809… drh 52 if( !fossil_isdigit(z[8]) ) return 0;
2fac809… drh 53 if( !fossil_isdigit(z[9]) ) return 0;
2fac809… drh 54 return 1;
2fac809… drh 55 }
2fac809… drh 56
2fac809… drh 57 /*
437f39b… drh 58 ** Check to see if the string might be a compact date/time that omits
437f39b… drh 59 ** the punctuation. Example: "20190327084549" instead of
437f39b… drh 60 ** "2019-03-27 08:45:49". If the string is of the appropriate form,
437f39b… drh 61 ** then return an alternative string (in static space) that is the same
437f39b… drh 62 ** string with punctuation inserted.
437f39b… drh 63 **
633ea73… drh 64 ** If the bRoundUp parameter is true, then round the resulting date-time
633ea73… drh 65 ** up to the largest date/time that is consistent with the input value.
633ea73… drh 66 ** This is because the result will be used for an mtime<=julianday($DATE)
633ea73… drh 67 ** comparison. In other words:
633ea73… drh 68 **
633ea73… drh 69 ** 20250317123421 -> 2025-03-17 12:34:21.999
633ea73… drh 70 ** ^^^^--- Added
633ea73… drh 71 **
633ea73… drh 72 ** 202503171234 -> 2025-03-17 12:34:59.999
633ea73… drh 73 ** ^^^^^^^--- Added
633ea73… drh 74 ** 20250317 -> 2025-03-17 23:59:59.999
54cfac8… drh 75 ** ^^^^^^^^^^^^^--- Added
515a4d8… drh 76 **
437f39b… drh 77 ** If the bVerifyNotAHash flag is true, then a check is made to see if
515a4d8… drh 78 ** the input string is a hash prefix and NULL is returned if it is. If the
437f39b… drh 79 ** bVerifyNotAHash flag is false, then the result is determined by syntax
437f39b… drh 80 ** of the input string only, without reference to the artifact table.
437f39b… drh 81 */
633ea73… drh 82 char *fossil_expand_datetime(const char *zIn,int bVerifyNotAHash,int bRoundUp){
c464947… drh 83 static char zEDate[24];
437f39b… drh 84 static const char aPunct[] = { 0, 0, '-', '-', ' ', ':', ':' };
437f39b… drh 85 int n = (int)strlen(zIn);
437f39b… drh 86 int i, j;
c464947… drh 87 int addZulu = 0;
437f39b… drh 88
c464947… drh 89 /* These forms are allowed:
c464947… drh 90 **
633ea73… drh 91 ** 123456789 1234 123456789 123456789 1234
633ea73… drh 92 ** (1) YYYYMMDD => YYYY-MM-DD 23:59:59.999
633ea73… drh 93 ** (2) YYYYMMDDHHMM => YYYY-MM-DD HH:MM:59.999
633ea73… drh 94 ** (3) YYYYMMDDHHMMSS => YYYY-MM-DD HH:MM:SS.999
c464947… drh 95 **
3a3ce2f… drh 96 ** An optional "Z" zulu timezone designator is allowed at the end.
437f39b… drh 97 */
3a3ce2f… drh 98 if( n>0 && (zIn[n-1]=='Z' || zIn[n-1]=='z') ){
3a3ce2f… drh 99 n--;
3a3ce2f… drh 100 addZulu = 1;
3a3ce2f… drh 101 }
c464947… drh 102 if( n!=8 && n!=12 && n!=14 ){
3a3ce2f… drh 103 return 0;
c464947… drh 104 }
437f39b… drh 105
437f39b… drh 106 /* Every character must be a digit */
437f39b… drh 107 for(i=0; fossil_isdigit(zIn[i]); i++){}
c464947… drh 108 if( i!=n && (!addZulu || i!=n+1) ) return 0;
437f39b… drh 109
437f39b… drh 110 /* Expand the date */
c464947… drh 111 for(i=j=0; i<n; i++){
437f39b… drh 112 if( i>=4 && (i%2)==0 ){
437f39b… drh 113 zEDate[j++] = aPunct[i/2];
437f39b… drh 114 }
437f39b… drh 115 zEDate[j++] = zIn[i];
c464947… drh 116 }
633ea73… drh 117 if( bRoundUp ){
633ea73… drh 118 if( j==10 ){
633ea73… drh 119 memcpy(&zEDate[10], " 23:59:59.999", 13);
633ea73… drh 120 j += 13;
633ea73… drh 121 }else if( j==16 ){
633ea73… drh 122 memcpy(&zEDate[16], ":59.999",7);
633ea73… drh 123 j += 7;
633ea73… drh 124 }else if( j==19 ){
633ea73… drh 125 memcpy(&zEDate[19], ".999", 4);
633ea73… drh 126 j += 4;
633ea73… drh 127 }
515a4d8… drh 128 }
515a4d8… drh 129 if( addZulu ){
3a3ce2f… drh 130 zEDate[j++] = 'Z';
3a3ce2f… drh 131 }
437f39b… drh 132 zEDate[j] = 0;
437f39b… drh 133
437f39b… drh 134 /* Check for reasonable date values.
437f39b… drh 135 ** Offset references:
437f39b… drh 136 ** YYYY-MM-DD HH:MM:SS
437f39b… drh 137 ** 0123456789 12345678
437f39b… drh 138 */
437f39b… drh 139
437f39b… drh 140 i = atoi(zEDate);
437f39b… drh 141 if( i<1970 || i>2100 ) return 0;
437f39b… drh 142 i = atoi(zEDate+5);
437f39b… drh 143 if( i<1 || i>12 ) return 0;
437f39b… drh 144 i = atoi(zEDate+8);
437f39b… drh 145 if( i<1 || i>31 ) return 0;
437f39b… drh 146 if( n>8 ){
437f39b… drh 147 i = atoi(zEDate+11);
437f39b… drh 148 if( i>24 ) return 0;
437f39b… drh 149 i = atoi(zEDate+14);
437f39b… drh 150 if( i>60 ) return 0;
437f39b… drh 151 if( n==14 && atoi(zEDate+17)>60 ) return 0;
437f39b… drh 152 }
437f39b… drh 153
437f39b… drh 154 /* The string is not also a hash prefix */
c464947… drh 155 if( bVerifyNotAHash && !addZulu ){
437f39b… drh 156 if( db_exists("SELECT 1 FROM blob WHERE uuid GLOB '%q*'",zIn) ) return 0;
437f39b… drh 157 }
437f39b… drh 158
437f39b… drh 159 /* It looks like this may be a date. Return it with punctuation added. */
437f39b… drh 160 return zEDate;
437f39b… drh 161 }
a5f3103… drh 162
a5f3103… drh 163 /*
a5f3103… drh 164 ** The data-time string in the argument is going to be used as an
a5f3103… drh 165 ** upper bound like this: mtime<=julianday(zDate,'localtime').
a5f3103… drh 166 ** But if the zDate parameter omits the fractional seconds or the
a5f3103… drh 167 ** seconds, or the time, that might mess up the == part of the
a5f3103… drh 168 ** comparison. So add in missing factional seconds or seconds or time.
a5f3103… drh 169 **
a5f3103… drh 170 ** The returned string is held in a static buffer that is overwritten
a5f3103… drh 171 ** with each call, or else is just a copy of its input if there are
a5f3103… drh 172 ** no changes.
e588ee2… drh 173 **
e588ee2… drh 174 ** For reference:
e588ee2… drh 175 **
e588ee2… drh 176 ** 0123456789 123456789 1234
e588ee2… drh 177 ** YYYY-MM-DD HH:MM:SS.SSSz
a5f3103… drh 178 */
a5f3103… drh 179 const char *fossil_roundup_date(const char *zDate){
e588ee2… drh 180 static char zUp[28];
e588ee2… drh 181 int n = (int)strlen(zDate);
e588ee2… drh 182 int addZ = 0;
e588ee2… drh 183 if( n>10 && (zDate[n-1]=='z' || zDate[n-1]=='Z') ){
e588ee2… drh 184 n--;
e588ee2… drh 185 addZ = 1;
e588ee2… drh 186 }
5298941… drh 187 if( n>=21 && n<=23 ){ /* YYYY-MM-DD HH:MM:SS.SSS */
5298941… drh 188 memcpy(zUp, zDate, n);
5298941… drh 189 while( n<23 ) zUp[n++] = '9';
5298941… drh 190 /* If milliseconds is less than 999, round up to the next millisecond */
5298941… drh 191 if( strcmp(&zUp[20],"999")<0 ){
cc5b93e… drh 192 if( zUp[22]<'9' ){
5298941… drh 193 zUp[22]++;
5298941… drh 194 }else{
edce45b… drh 195 zUp[22] = '0';
5298941… drh 196 if( zUp[21]<'9' ){
5298941… drh 197 zUp[21]++;
5298941… drh 198 }else{
edce45b… drh 199 zUp[21] = '0';
5298941… drh 200 zUp[20]++;
5298941… drh 201 }
5298941… drh 202 }
5298941… drh 203 }
5298941… drh 204 if( addZ ) zUp[n++] = 'z';
5298941… drh 205 zUp[n] = 0;
5298941… drh 206 return zUp;
5298941… drh 207 }
5298941… drh 208 if( n==19 ){ /* YYYY-MM-DD HH:MM:SS */
e588ee2… drh 209 memcpy(zUp, zDate, 19);
e588ee2… drh 210 memcpy(zUp+19, ".999z", 6);
e588ee2… drh 211 if( !addZ ) zUp[23] = 0;
e588ee2… drh 212 return zUp;
e588ee2… drh 213 }
5298941… drh 214 if( n==16 ){ /* YYYY-MM-DD HH:MM */
e588ee2… drh 215 memcpy(zUp, zDate, 16);
e588ee2… drh 216 memcpy(zUp+16, ":59.999z", 8);
e588ee2… drh 217 if( !addZ ) zUp[23] = 0;
e588ee2… drh 218 return zUp;
e588ee2… drh 219 }
5298941… drh 220 if( n==10 ){ /* YYYY-MM-DD */
e588ee2… drh 221 memcpy(zUp, zDate, 10);
e588ee2… drh 222 memcpy(zUp+10, " 23:59:59.999z", 14);
e588ee2… drh 223 if( !addZ ) zUp[23] = 0;
a5f3103… drh 224 return zUp;
a5f3103… drh 225 }
a5f3103… drh 226 return zDate;
a5f3103… drh 227 }
a5f3103… drh 228
e632a09… drh 229
e632a09… drh 230 /*
a81a47f… drh 231 ** Return the RID that is the "root" of the branch that contains
e632a09… drh 232 ** check-in "rid". Details depending on eType:
e632a09… drh 233 **
e632a09… drh 234 ** eType==0 The check-in of the parent branch off of which
e632a09… drh 235 ** the branch containing RID originally diverged.
e632a09… drh 236 **
e632a09… drh 237 ** eType==1 The first check-in of the branch that contains RID.
e632a09… drh 238 **
e632a09… drh 239 ** eType==2 The youngest ancestor of RID that is on the branch
e632a09… drh 240 ** from which the branch containing RID diverged.
a81a47f… drh 241 */
e632a09… drh 242 int start_of_branch(int rid, int eType){
a81a47f… drh 243 Stmt q;
a81a47f… drh 244 int rc;
e632a09… drh 245 int ans = rid;
e632a09… drh 246 char *zBr = branch_of_rid(rid);
a81a47f… drh 247 db_prepare(&q,
8736de8… drh 248 "WITH RECURSIVE"
8736de8… drh 249 " par(pid, ex, cnt) as ("
8736de8… drh 250 " SELECT pid, EXISTS(SELECT 1 FROM tagxref"
8736de8… drh 251 " WHERE tagid=%d AND tagtype>0"
8736de8… drh 252 " AND value=%Q AND rid=plink.pid), 1"
8736de8… drh 253 " FROM plink WHERE cid=%d AND isprim"
8736de8… drh 254 " UNION ALL "
8736de8… drh 255 " SELECT plink.pid, EXISTS(SELECT 1 FROM tagxref "
4d8c302… stephan 256 " WHERE tagid=%d AND tagtype>0"
8736de8… drh 257 " AND value=%Q AND rid=plink.pid),"
8736de8… drh 258 " 1+par.cnt"
8736de8… drh 259 " FROM plink, par"
8736de8… drh 260 " WHERE cid=par.pid AND isprim AND par.ex "
8736de8… drh 261 " LIMIT 100000 "
8736de8… drh 262 " )"
8736de8… drh 263 " SELECT pid FROM par WHERE ex>=%d ORDER BY cnt DESC LIMIT 1",
8736de8… drh 264 TAG_BRANCH, zBr, ans, TAG_BRANCH, zBr, eType%2
a81a47f… drh 265 );
a81a47f… drh 266 fossil_free(zBr);
8736de8… drh 267 rc = db_step(&q);
4d8c302… stephan 268 if( rc==SQLITE_ROW ){
e632a09… drh 269 ans = db_column_int(&q, 0);
8736de8… drh 270 }
a81a47f… drh 271 db_finalize(&q);
e632a09… drh 272 if( eType==2 && ans>0 ){
e632a09… drh 273 zBr = branch_of_rid(ans);
e632a09… drh 274 ans = compute_youngest_ancestor_in_branch(rid, zBr);
e632a09… drh 275 fossil_free(zBr);
e632a09… drh 276 }
e632a09… drh 277 return ans;
e632a09… drh 278 }
e632a09… drh 279
e632a09… drh 280 /*
fdeebdd… drh 281 ** Find the RID of the most recent object with symbolic tag zTag
fdeebdd… drh 282 ** and having a type that matches zType.
fdeebdd… drh 283 **
fdeebdd… drh 284 ** Return 0 if there are no matches.
fdeebdd… drh 285 **
4d8c302… stephan 286 ** This is a tricky query to do efficiently.
fdeebdd… drh 287 ** If the tag is very common (ex: "trunk") then
015d7b7… drh 288 ** we want to use the query identified below as Q1 - which searches
fdeebdd… drh 289 ** the most recent EVENT table entries for the most recent with the tag.
fdeebdd… drh 290 ** But if the tag is relatively scarce (anything other than "trunk", basically)
4e23c2a… drh 291 ** then we want to do the indexed search shown below as Q2.
fdeebdd… drh 292 */
fdeebdd… drh 293 static int most_recent_event_with_tag(const char *zTag, const char *zType){
fdeebdd… drh 294 return db_int(0,
fdeebdd… drh 295 "SELECT objid FROM ("
fdeebdd… drh 296 /* Q1: Begin by looking for the tag in the 30 most recent events */
fdeebdd… drh 297 "SELECT objid"
fdeebdd… drh 298 " FROM (SELECT * FROM event ORDER BY mtime DESC LIMIT 30) AS ex"
fdeebdd… drh 299 " WHERE type GLOB '%q'"
fdeebdd… drh 300 " AND EXISTS(SELECT 1 FROM tagxref, tag"
fdeebdd… drh 301 " WHERE tag.tagname='sym-%q'"
fdeebdd… drh 302 " AND tagxref.tagid=tag.tagid"
fdeebdd… drh 303 " AND tagxref.tagtype>0"
fdeebdd… drh 304 " AND tagxref.rid=ex.objid)"
fdeebdd… drh 305 " ORDER BY mtime DESC LIMIT 1"
fdeebdd… drh 306 ") UNION ALL SELECT * FROM ("
fdeebdd… drh 307 /* Q2: If the tag is not found in the 30 most recent events, then using
fdeebdd… drh 308 ** the tagxref table to index for the tag */
fdeebdd… drh 309 "SELECT event.objid"
fdeebdd… drh 310 " FROM tag, tagxref, event"
fdeebdd… drh 311 " WHERE tag.tagname='sym-%q'"
fdeebdd… drh 312 " AND tagxref.tagid=tag.tagid"
fdeebdd… drh 313 " AND tagxref.tagtype>0"
fdeebdd… drh 314 " AND event.objid=tagxref.rid"
fdeebdd… drh 315 " AND event.type GLOB '%q'"
fdeebdd… drh 316 " ORDER BY event.mtime DESC LIMIT 1"
fdeebdd… drh 317 ") LIMIT 1;",
fdeebdd… drh 318 zType, zTag, zTag, zType
fdeebdd… drh 319 );
507ebd8… drh 320 }
507ebd8… drh 321
507ebd8… drh 322 /*
507ebd8… drh 323 ** Find the RID for a check-in that is the most recent check-in with
507ebd8… drh 324 ** tag zTag that occurs on or prior to rDate.
507ebd8… drh 325 **
507ebd8… drh 326 ** See also the performance note on most_recent_event_with_tag() which
507ebd8… drh 327 ** applies to this routine too.
507ebd8… drh 328 */
64f141f… drh 329 int last_checkin_with_tag_before_date(const char *zTag, double rLimit){
507ebd8… drh 330 Stmt s;
507ebd8… drh 331 int rid = 0;
507ebd8… drh 332 if( strncmp(zTag, "tag:", 4)==0 ) zTag += 4;
507ebd8… drh 333 db_prepare(&s,
507ebd8… drh 334 "SELECT objid FROM ("
507ebd8… drh 335 /* Q1: Begin by looking for the tag in the 30 most recent events */
507ebd8… drh 336 "SELECT objid"
507ebd8… drh 337 " FROM (SELECT * FROM event WHERE mtime<=:datelimit"
507ebd8… drh 338 " ORDER BY mtime DESC LIMIT 30) AS ex"
507ebd8… drh 339 " WHERE type='ci'"
507ebd8… drh 340 " AND EXISTS(SELECT 1 FROM tagxref, tag"
507ebd8… drh 341 " WHERE tag.tagname='sym-%q'"
507ebd8… drh 342 " AND tagxref.tagid=tag.tagid"
507ebd8… drh 343 " AND tagxref.tagtype>0"
507ebd8… drh 344 " AND tagxref.rid=ex.objid)"
507ebd8… drh 345 " ORDER BY mtime DESC LIMIT 1"
507ebd8… drh 346 ") UNION ALL SELECT * FROM ("
507ebd8… drh 347 /* Q2: If the tag is not found in the 30 most recent events, then using
507ebd8… drh 348 ** the tagxref table to index for the tag */
507ebd8… drh 349 "SELECT event.objid"
507ebd8… drh 350 " FROM tag, tagxref, event"
507ebd8… drh 351 " WHERE tag.tagname='sym-%q'"
507ebd8… drh 352 " AND tagxref.tagid=tag.tagid"
507ebd8… drh 353 " AND tagxref.tagtype>0"
507ebd8… drh 354 " AND event.objid=tagxref.rid"
507ebd8… drh 355 " AND event.type='ci'"
507ebd8… drh 356 " AND event.mtime<=:datelimit"
507ebd8… drh 357 " ORDER BY event.mtime DESC LIMIT 1"
507ebd8… drh 358 ") LIMIT 1;",
507ebd8… drh 359 zTag, zTag
507ebd8… drh 360 );
507ebd8… drh 361 db_bind_double(&s, ":datelimit", rLimit);
64f141f… drh 362 if( db_step(&s)==SQLITE_ROW ){
64f141f… drh 363 rid = db_column_int(&s,0);
64f141f… drh 364 }
64f141f… drh 365 db_finalize(&s);
64f141f… drh 366 return rid;
64f141f… drh 367 }
64f141f… drh 368
64f141f… drh 369 /*
64f141f… drh 370 ** Find the RID of the first check-in (chronologically) after rStart that
64f141f… drh 371 ** has tag zTag.
64f141f… drh 372 **
64f141f… drh 373 ** See also the performance note on most_recent_event_with_tag() which
64f141f… drh 374 ** applies to this routine too.
64f141f… drh 375 */
64f141f… drh 376 int first_checkin_with_tag_after_date(const char *zTag, double rStart){
64f141f… drh 377 Stmt s;
64f141f… drh 378 int rid = 0;
64f141f… drh 379 if( strncmp(zTag, "tag:", 4)==0 ) zTag += 4;
64f141f… drh 380 db_prepare(&s,
64f141f… drh 381 "SELECT objid FROM ("
64f141f… drh 382 /* Q1: Begin by looking for the tag in the 30 most recent events */
64f141f… drh 383 "SELECT objid"
64f141f… drh 384 " FROM (SELECT * FROM event WHERE mtime>=:startdate"
64f141f… drh 385 " ORDER BY mtime LIMIT 30) AS ex"
64f141f… drh 386 " WHERE type='ci'"
64f141f… drh 387 " AND EXISTS(SELECT 1 FROM tagxref, tag"
64f141f… drh 388 " WHERE tag.tagname='sym-%q'"
64f141f… drh 389 " AND tagxref.tagid=tag.tagid"
64f141f… drh 390 " AND tagxref.tagtype>0"
64f141f… drh 391 " AND tagxref.rid=ex.objid)"
64f141f… drh 392 " ORDER BY mtime LIMIT 1"
64f141f… drh 393 ") UNION ALL SELECT * FROM ("
64f141f… drh 394 /* Q2: If the tag is not found in the 30 most recent events, then using
64f141f… drh 395 ** the tagxref table to index for the tag */
64f141f… drh 396 "SELECT event.objid"
64f141f… drh 397 " FROM tag, tagxref, event"
64f141f… drh 398 " WHERE tag.tagname='sym-%q'"
64f141f… drh 399 " AND tagxref.tagid=tag.tagid"
64f141f… drh 400 " AND tagxref.tagtype>0"
64f141f… drh 401 " AND event.objid=tagxref.rid"
64f141f… drh 402 " AND event.type='ci'"
64f141f… drh 403 " AND event.mtime>=:startdate"
64f141f… drh 404 " ORDER BY event.mtime LIMIT 1"
64f141f… drh 405 ") LIMIT 1;",
64f141f… drh 406 zTag, zTag
64f141f… drh 407 );
64f141f… drh 408 db_bind_double(&s, ":startdate", rStart);
507ebd8… drh 409 if( db_step(&s)==SQLITE_ROW ){
507ebd8… drh 410 rid = db_column_int(&s,0);
507ebd8… drh 411 }
507ebd8… drh 412 db_finalize(&s);
507ebd8… drh 413 return rid;
99a319b… wyoung 414 }
99a319b… wyoung 415
99a319b… wyoung 416 /*
d81190b… drh 417 ** Return true if character "c" is a character that might have been
d81190b… drh 418 ** accidentally appended to the end of a URL.
d81190b… drh 419 */
d81190b… drh 420 static int is_trailing_punct(char c){
d81190b… drh 421 return c=='.' || c=='_' || c==')' || c=='>' || c=='!' || c=='?' || c==',';
d81190b… drh 422 }
d81190b… drh 423
fdeebdd… drh 424
fdeebdd… drh 425 /*
7858a39… drh 426 ** Convert a symbolic name into a RID. Acceptable forms:
7858a39… drh 427 **
537b498… drh 428 ** * artifact hash (optionally enclosed in [...])
99a319b… wyoung 429 ** * 4-character or larger prefix of an artifact
7858a39… drh 430 ** * Symbolic Name
7858a39… drh 431 ** * "tag:" + symbolic name
c54c2b0… jan.nijtmans 432 ** * Date or date-time
7858a39… drh 433 ** * "date:" + Date or date-time
7858a39… drh 434 ** * symbolic-name ":" date-time
7858a39… drh 435 ** * "tip"
7858a39… drh 436 **
7858a39… drh 437 ** The following additional forms are available in local checkouts:
7858a39… drh 438 **
7858a39… drh 439 ** * "current"
7858a39… drh 440 ** * "prev" or "previous"
7858a39… drh 441 ** * "next"
7858a39… drh 442 **
20391d1… stephan 443 ** The following modifier prefixes may be applied to the above forms:
20391d1… stephan 444 **
20391d1… stephan 445 ** * "root:BR" = The origin of the branch named BR.
fab8d92… danield 446 ** * "start:BR" = The first check-in of the branch named BR.
20391d1… stephan 447 ** * "merge-in:BR" = The most recent merge-in for the branch named BR.
20391d1… stephan 448 **
20391d1… stephan 449 ** In those forms, BR may be any symbolic form but is assumed to be a
bc36fdc… danield 450 ** check-in. Thus root:2021-02-01 would resolve to a check-in, possibly
20391d1… stephan 451 ** in a branch and possibly in the trunk, but never a wiki edit or
20391d1… stephan 452 ** forum post.
20391d1… stephan 453 **
7858a39… drh 454 ** Return the RID of the matching artifact. Or return 0 if the name does not
db0c512… drh 455 ** match any known object. Or return -1 if the name is ambiguous.
7858a39… drh 456 **
99fcc43… drh 457 ** The zType parameter specifies the type of artifact: ci, t, w, e, g, f.
7858a39… drh 458 ** If zType is NULL or "" or "*" then any type of artifact will serve.
a81a47f… drh 459 ** If zType is "br" then find the first check-in of the named branch
a81a47f… drh 460 ** rather than the last.
e632a09… drh 461 **
7858a39… drh 462 ** zType is "ci" in most use cases since we are usually searching for
4d8c302… stephan 463 ** a check-in. A value of "ci+" works like "ci" but adds these
4d8c302… stephan 464 ** semantics: if zTag is "ckout" and a checkout is open, "ci+" causes
4d8c302… stephan 465 ** RID_CKOUT to be returned, in which case g.localOpen will hold the
4d8c302… stephan 466 ** RID of the checkout. Conversely, passing in the hash, or another
4d8c302… stephan 467 ** symbolic name of the local checkout version, will always result in
4d8c302… stephan 468 ** its RID being returned.
327eee1… drh 469 **
fd9b7bd… drh 470 ** Note that the input zTag for types "t" and "e" is the artifact hash of
99fcc43… drh 471 ** the ticket-change or technote-change artifact, not the randomly generated
327eee1… drh 472 ** hexadecimal identifier assigned to tickets and events. Those identifiers
327eee1… drh 473 ** live in a separate namespace.
7858a39… drh 474 */
6f2a516… stephan 475 int symbolic_name_to_rid(const char *zTag, const char *zType){
7858a39… drh 476 int rid = 0;
1ef6499… stephan 477 int ridCkout = 0;
7858a39… drh 478 int nTag;
7858a39… drh 479 int i;
a81a47f… drh 480 int startOfBranch = 0;
537b498… drh 481 const char *zXTag; /* zTag with optional [...] removed */
537b498… drh 482 int nXTag; /* Size of zXTag */
437f39b… drh 483 const char *zDate; /* Expanded date-time string */
4d8c302… stephan 484 int isCheckin = 0; /* zType==ci = 1, zType==ci+ = 2 */
7858a39… drh 485
a81a47f… drh 486 if( zType==0 || zType[0]==0 ){
a81a47f… drh 487 zType = "*";
a81a47f… drh 488 }else if( zType[0]=='b' ){
a81a47f… drh 489 zType = "ci";
a81a47f… drh 490 startOfBranch = 1;
a81a47f… drh 491 }
7858a39… drh 492 if( zTag==0 || zTag[0]==0 ) return 0;
4d8c302… stephan 493 else if( 'c'==zType[0] ){
4d8c302… stephan 494 if( fossil_strcmp(zType,"ci")==0 ){
4d8c302… stephan 495 isCheckin = 1;
4d8c302… stephan 496 }else if( fossil_strcmp(zType,"ci+")==0 ){
4d8c302… stephan 497 isCheckin = 2;
4d8c302… stephan 498 zType = "ci";
4d8c302… stephan 499 }
4d8c302… stephan 500 }
7858a39… drh 501
7858a39… drh 502 /* special keyword: "tip" */
4d8c302… stephan 503 if( fossil_strcmp(zTag, "tip")==0 && (zType[0]=='*' || isCheckin!=0) ){
7858a39… drh 504 rid = db_int(0,
7858a39… drh 505 "SELECT objid"
7858a39… drh 506 " FROM event"
7858a39… drh 507 " WHERE type='ci'"
7858a39… drh 508 " ORDER BY event.mtime DESC"
7858a39… drh 509 );
7858a39… drh 510 if( rid ) return rid;
7858a39… drh 511 }
7858a39… drh 512
7faa1f4… stephan 513 if( g.localOpen ) {
7faa1f4… stephan 514 ridCkout = db_lget_int("checkout",0);
7faa1f4… stephan 515 }
7faa1f4… stephan 516
4d8c302… stephan 517 /* special keywords: "prev", "previous", "current", "ckout", and
4d8c302… stephan 518 ** "next" */
7faa1f4… stephan 519 if( (zType[0]=='*' || isCheckin!=0) && 0<ridCkout ){
7858a39… drh 520 if( fossil_strcmp(zTag, "current")==0 ){
1ef6499… stephan 521 rid = ridCkout;
c54c2b0… jan.nijtmans 522 }else if( fossil_strcmp(zTag, "prev")==0
7858a39… drh 523 || fossil_strcmp(zTag, "previous")==0 ){
1ef6499… stephan 524 rid = db_int(0, "SELECT pid FROM plink WHERE cid=%d AND isprim",
1ef6499… stephan 525 ridCkout);
7858a39… drh 526 }else if( fossil_strcmp(zTag, "next")==0 ){
7858a39… drh 527 rid = db_int(0, "SELECT cid FROM plink WHERE pid=%d"
1ef6499… stephan 528 " ORDER BY isprim DESC, mtime DESC", ridCkout);
4d8c302… stephan 529 }else if( isCheckin>1 && fossil_strcmp(zTag, "ckout")==0 ){
4d8c302… stephan 530 rid = RID_CKOUT;
15df204… stephan 531 assert(ridCkout>0);
15df204… stephan 532 g.localOpen = ridCkout;
7858a39… drh 533 }
7858a39… drh 534 if( rid ) return rid;
7858a39… drh 535 }
7858a39… drh 536
7858a39… drh 537 /* Date and times */
7858a39… drh 538 if( memcmp(zTag, "date:", 5)==0 ){
633ea73… drh 539 zDate = fossil_expand_datetime(&zTag[5],0,1);
437f39b… drh 540 if( zDate==0 ) zDate = &zTag[5];
c54c2b0… jan.nijtmans 541 rid = db_int(0,
41c3c19… drh 542 "SELECT objid FROM event"
ea63a2d… drh 543 " WHERE mtime<=julianday(%Q,fromLocal()) AND type GLOB '%q'"
41c3c19… drh 544 " ORDER BY mtime DESC LIMIT 1",
a5f3103… drh 545 fossil_roundup_date(zDate), zType);
41c3c19… drh 546 return rid;
41c3c19… drh 547 }
41c3c19… drh 548 if( fossil_isdate(zTag) ){
c54c2b0… jan.nijtmans 549 rid = db_int(0,
41c3c19… drh 550 "SELECT objid FROM event"
ea63a2d… drh 551 " WHERE mtime<=julianday(%Q,fromLocal()) AND type GLOB '%q'"
41c3c19… drh 552 " ORDER BY mtime DESC LIMIT 1",
a5f3103… drh 553 fossil_roundup_date(zTag), zType);
7858a39… drh 554 if( rid) return rid;
7858a39… drh 555 }
7858a39… drh 556
7858a39… drh 557 /* Deprecated date & time formats: "local:" + date-time and
7858a39… drh 558 ** "utc:" + date-time */
7858a39… drh 559 if( memcmp(zTag, "local:", 6)==0 ){
c54c2b0… jan.nijtmans 560 rid = db_int(0,
7858a39… drh 561 "SELECT objid FROM event"
7858a39… drh 562 " WHERE mtime<=julianday(%Q) AND type GLOB '%q'"
7858a39… drh 563 " ORDER BY mtime DESC LIMIT 1",
7858a39… drh 564 &zTag[6], zType);
7858a39… drh 565 return rid;
7858a39… drh 566 }
7858a39… drh 567 if( memcmp(zTag, "utc:", 4)==0 ){
c54c2b0… jan.nijtmans 568 rid = db_int(0,
7858a39… drh 569 "SELECT objid FROM event"
7858a39… drh 570 " WHERE mtime<=julianday('%qz') AND type GLOB '%q'"
7858a39… drh 571 " ORDER BY mtime DESC LIMIT 1",
a5f3103… drh 572 fossil_roundup_date(&zTag[4]), zType);
7858a39… drh 573 return rid;
7858a39… drh 574 }
7858a39… drh 575
7858a39… drh 576 /* "tag:" + symbolic-name */
7858a39… drh 577 if( memcmp(zTag, "tag:", 4)==0 ){
fdeebdd… drh 578 rid = most_recent_event_with_tag(&zTag[4], zType);
a81a47f… drh 579 if( startOfBranch ) rid = start_of_branch(rid,1);
a81a47f… drh 580 return rid;
a81a47f… drh 581 }
a81a47f… drh 582
e632a09… drh 583 /* root:BR -> The origin of the branch named BR */
e632a09… drh 584 if( strncmp(zTag, "root:", 5)==0 ){
a81a47f… drh 585 rid = symbolic_name_to_rid(zTag+5, zType);
a81a47f… drh 586 return start_of_branch(rid, 0);
a81a47f… drh 587 }
fab8d92… danield 588
fab8d92… danield 589 /* start:BR -> The first check-in on branch named BR */
fab8d92… danield 590 if( strncmp(zTag, "start:", 6)==0 ){
fab8d92… danield 591 rid = symbolic_name_to_rid(zTag+6, zType);
fab8d92… danield 592 return start_of_branch(rid, 1);
275da70… danield 593 }
275da70… danield 594
fab8d92… danield 595 /* merge-in:BR -> Most recent merge-in for the branch named BR */
e632a09… drh 596 if( strncmp(zTag, "merge-in:", 9)==0 ){
e632a09… drh 597 rid = symbolic_name_to_rid(zTag+9, zType);
e632a09… drh 598 return start_of_branch(rid, 2);
e632a09… drh 599 }
e632a09… drh 600
7858a39… drh 601 /* symbolic-name ":" date-time */
7858a39… drh 602 nTag = strlen(zTag);
a5f3103… drh 603 for(i=0; i<nTag-8 && zTag[i]!=':'; i++){}
275da70… danield 604 if( zTag[i]==':'
633ea73… drh 605 && (fossil_isdate(&zTag[i+1]) || fossil_expand_datetime(&zTag[i+1],0,0)!=0)
a5f3103… drh 606 ){
4c3e172… danield 607 char *zDate = fossil_strdup(&zTag[i+1]);
7858a39… drh 608 char *zTagBase = mprintf("%.*s", i, zTag);
a5f3103… drh 609 char *zXDate;
7858a39… drh 610 int nDate = strlen(zDate);
7858a39… drh 611 if( sqlite3_strnicmp(&zDate[nDate-3],"utc",3)==0 ){
7858a39… drh 612 zDate[nDate-3] = 'z';
7858a39… drh 613 zDate[nDate-2] = 0;
7858a39… drh 614 }
633ea73… drh 615 zXDate = fossil_expand_datetime(zDate,0,1);
a5f3103… drh 616 if( zXDate==0 ) zXDate = zDate;
7858a39… drh 617 rid = db_int(0,
186405c… drh 618 "SELECT event.objid, max(event.mtime)"
7858a39… drh 619 " FROM tag, tagxref, event"
7858a39… drh 620 " WHERE tag.tagname='sym-%q' "
7858a39… drh 621 " AND tagxref.tagid=tag.tagid AND tagxref.tagtype>0 "
7858a39… drh 622 " AND event.objid=tagxref.rid "
a5f3103… drh 623 " AND event.mtime<=julianday(%Q,fromLocal())"
186405c… drh 624 " AND event.type GLOB '%q'",
a5f3103… drh 625 zTagBase, fossil_roundup_date(zXDate), zType
7858a39… drh 626 );
a5f3103… drh 627 fossil_free(zDate);
a5f3103… drh 628 fossil_free(zTagBase);
7858a39… drh 629 return rid;
7858a39… drh 630 }
7858a39… drh 631
537b498… drh 632 /* Remove optional [...] */
537b498… drh 633 zXTag = zTag;
537b498… drh 634 nXTag = nTag;
537b498… drh 635 if( zXTag[0]=='[' ){
537b498… drh 636 zXTag++;
537b498… drh 637 nXTag--;
537b498… drh 638 }
537b498… drh 639 if( nXTag>0 && zXTag[nXTag-1]==']' ){
537b498… drh 640 nXTag--;
537b498… drh 641 }
537b498… drh 642
fd9b7bd… drh 643 /* artifact hash or prefix */
537b498… drh 644 if( nXTag>=4 && nXTag<=HNAME_MAX && validate16(zXTag, nXTag) ){
7858a39… drh 645 Stmt q;
fd9b7bd… drh 646 char zUuid[HNAME_MAX+1];
537b498… drh 647 memcpy(zUuid, zXTag, nXTag);
537b498… drh 648 zUuid[nXTag] = 0;
537b498… drh 649 canonical16(zUuid, nXTag);
7858a39… drh 650 rid = 0;
7858a39… drh 651 if( zType[0]=='*' ){
49b0ff1… drh 652 db_prepare(&q, "SELECT rid FROM blob WHERE uuid GLOB '%q*'", zUuid);
7858a39… drh 653 }else{
7858a39… drh 654 db_prepare(&q,
7858a39… drh 655 "SELECT blob.rid"
d45542c… drh 656 " FROM blob CROSS JOIN event"
49b0ff1… drh 657 " WHERE blob.uuid GLOB '%q*'"
7858a39… drh 658 " AND event.objid=blob.rid"
7858a39… drh 659 " AND event.type GLOB '%q'",
7858a39… drh 660 zUuid, zType
7858a39… drh 661 );
7858a39… drh 662 }
7858a39… drh 663 if( db_step(&q)==SQLITE_ROW ){
7858a39… drh 664 rid = db_column_int(&q, 0);
7858a39… drh 665 if( db_step(&q)==SQLITE_ROW ) rid = -1;
7858a39… drh 666 }
7858a39… drh 667 db_finalize(&q);
7858a39… drh 668 if( rid ) return rid;
7858a39… drh 669 }
7858a39… drh 670
b40f1ac… drh 671 if( zType[0]=='w' ){
fdeebdd… drh 672 rid = db_int(0,
fdeebdd… drh 673 "SELECT event.objid, max(event.mtime)"
fdeebdd… drh 674 " FROM tag, tagxref, event"
fdeebdd… drh 675 " WHERE tag.tagname='wiki-%q' "
fdeebdd… drh 676 " AND tagxref.tagid=tag.tagid AND tagxref.tagtype>0 "
fdeebdd… drh 677 " AND event.objid=tagxref.rid "
fdeebdd… drh 678 " AND event.type GLOB '%q'",
fdeebdd… drh 679 zTag, zType
fdeebdd… drh 680 );
fdeebdd… drh 681 }else{
fdeebdd… drh 682 rid = most_recent_event_with_tag(zTag, zType);
fdeebdd… drh 683 }
b40f1ac… drh 684
a81a47f… drh 685 if( rid>0 ){
a81a47f… drh 686 if( startOfBranch ) rid = start_of_branch(rid,1);
a81a47f… drh 687 return rid;
a81a47f… drh 688 }
437f39b… drh 689
437f39b… drh 690 /* Pure numeric date/time */
633ea73… drh 691 zDate = fossil_expand_datetime(zTag, 0,1);
437f39b… drh 692 if( zDate ){
437f39b… drh 693 rid = db_int(0,
437f39b… drh 694 "SELECT objid FROM event"
437f39b… drh 695 " WHERE mtime<=julianday(%Q,fromLocal()) AND type GLOB '%q'"
437f39b… drh 696 " ORDER BY mtime DESC LIMIT 1",
a5f3103… drh 697 fossil_roundup_date(zDate), zType);
437f39b… drh 698 if( rid) return rid;
437f39b… drh 699 }
437f39b… drh 700
7858a39… drh 701
7858a39… drh 702 /* Undocumented: numeric tags get translated directly into the RID */
4c80aa2… stephan 703 if( memcmp(zTag, "rid:", 4)==0 ){
4c80aa2… stephan 704 zTag += 4;
4c80aa2… stephan 705 for(i=0; fossil_isdigit(zTag[i]); i++){}
4c80aa2… stephan 706 if( zTag[i]==0 ){
4c80aa2… stephan 707 if( strcmp(zType,"*")==0 ){
4c80aa2… stephan 708 rid = atoi(zTag);
4c80aa2… stephan 709 }else{
c54c2b0… jan.nijtmans 710 rid = db_int(0,
4c80aa2… stephan 711 "SELECT event.objid"
4c80aa2… stephan 712 " FROM event"
4c80aa2… stephan 713 " WHERE event.objid=%s"
49b0ff1… drh 714 " AND event.type GLOB '%q'", zTag /*safe-for-%s*/, zType);
4c80aa2… stephan 715 }
4c80aa2… stephan 716 }
d81190b… drh 717 return rid;
d81190b… drh 718 }
d81190b… drh 719
d81190b… drh 720 /* If nothing matches and the name ends with punctuation,
d81190b… drh 721 ** then the name might have originated from a URL in plain text
d81190b… drh 722 ** that was incorrectly extracted from the text. Try to remove
d81190b… drh 723 ** the extra punctuation and rerun the match.
d81190b… drh 724 */
d81190b… drh 725 if( nTag>4
d81190b… drh 726 && is_trailing_punct(zTag[nTag-1])
d81190b… drh 727 && !is_trailing_punct(zTag[nTag-2])
d81190b… drh 728 ){
d81190b… drh 729 char *zNew = fossil_strndup(zTag, nTag-1);
d81190b… drh 730 rid = symbolic_name_to_rid(zNew,zType);
d81190b… drh 731 fossil_free(zNew);
4d8c302… stephan 732 }else
d81190b… drh 733 if( nTag>5
d81190b… drh 734 && is_trailing_punct(zTag[nTag-1])
d81190b… drh 735 && is_trailing_punct(zTag[nTag-2])
d81190b… drh 736 && !is_trailing_punct(zTag[nTag-3])
d81190b… drh 737 ){
d81190b… drh 738 char *zNew = fossil_strndup(zTag, nTag-2);
d81190b… drh 739 rid = symbolic_name_to_rid(zNew,zType);
d81190b… drh 740 fossil_free(zNew);
7858a39… drh 741 }
7858a39… drh 742 return rid;
54cfac8… drh 743 }
54cfac8… drh 744
54cfac8… drh 745 /*
54cfac8… drh 746 ** Convert a symbolic name used as an argument to the a=, b=, or c=
54cfac8… drh 747 ** query parameters of timeline into a julianday mtime value.
54cfac8… drh 748 **
54cfac8… drh 749 ** If pzDisplay is not null, then display text for the symbolic name might
54cfac8… drh 750 ** be written into *pzDisplay. But that is not guaranteed.
54cfac8… drh 751 **
54cfac8… drh 752 ** If bRoundUp is true and the symbolic name is a timestamp with less
54cfac8… drh 753 ** than millisecond resolution, then the timestamp is rounding up to the
54cfac8… drh 754 ** largest millisecond consistent with that timestamp. If bRoundUp is
54cfac8… drh 755 ** false, then the resulting time is obtained by extending the timestamp
54cfac8… drh 756 ** with zeros (hence rounding down). Use bRoundUp==1 if the result
54cfac8… drh 757 ** will be used in mtime<=$RESULT and use bRoundUp==0 if the result
54cfac8… drh 758 ** will be used in mtime>=$RESULT.
54cfac8… drh 759 */
54cfac8… drh 760 double symbolic_name_to_mtime(
54cfac8… drh 761 const char *z, /* Input symbolic name */
54cfac8… drh 762 const char **pzDisplay, /* Perhaps write display text here, if not NULL */
54cfac8… drh 763 int bRoundUp /* Round up if true */
54cfac8… drh 764 ){
54cfac8… drh 765 double mtime;
54cfac8… drh 766 int rid;
54cfac8… drh 767 const char *zDate;
54cfac8… drh 768 if( z==0 ) return -1.0;
54cfac8… drh 769 if( fossil_isdate(z) ){
54cfac8… drh 770 mtime = db_double(0.0, "SELECT julianday(%Q,fromLocal())", z);
54cfac8… drh 771 if( mtime>0.0 ) return mtime;
54cfac8… drh 772 }
54cfac8… drh 773 zDate = fossil_expand_datetime(z, 1, bRoundUp);
54cfac8… drh 774 if( zDate!=0 ){
54cfac8… drh 775 mtime = db_double(0.0, "SELECT julianday(%Q,fromLocal())",
54cfac8… drh 776 bRoundUp ? fossil_roundup_date(zDate) : zDate);
54cfac8… drh 777 if( mtime>0.0 ){
54cfac8… drh 778 if( pzDisplay ){
54cfac8… drh 779 zDate = fossil_expand_datetime(z,0,0);
54cfac8… drh 780 *pzDisplay = fossil_strdup(zDate);
54cfac8… drh 781 }
54cfac8… drh 782 return mtime;
54cfac8… drh 783 }
54cfac8… drh 784 }
54cfac8… drh 785 rid = symbolic_name_to_rid(z, "*");
54cfac8… drh 786 if( rid ){
54cfac8… drh 787 mtime = mtime_of_rid(rid, 0.0);
54cfac8… drh 788 }else{
54cfac8… drh 789 mtime = db_double(-1.0,
54cfac8… drh 790 "SELECT max(event.mtime) FROM event, tag, tagxref"
54cfac8… drh 791 " WHERE tag.tagname GLOB 'event-%q*'"
54cfac8… drh 792 " AND tagxref.tagid=tag.tagid AND tagxref.tagtype"
54cfac8… drh 793 " AND event.objid=tagxref.rid",
54cfac8… drh 794 z
54cfac8… drh 795 );
54cfac8… drh 796 }
54cfac8… drh 797 return mtime;
15df204… stephan 798 }
15df204… stephan 799
15df204… stephan 800 /*
8ad5e46… wyoung 801 ** This routine takes a user-entered string and tries to convert it to
8ad5e46… wyoung 802 ** an artifact hash.
8ad5e46… wyoung 803 **
8ad5e46… wyoung 804 ** We first try to treat the string as an artifact hash, or at least a
8ad5e46… wyoung 805 ** unique prefix of an artifact hash. The input may be in mixed case.
8ad5e46… wyoung 806 ** If we are passed such a string, this routine has the effect of
8ad5e46… wyoung 807 ** converting the hash [prefix] to canonical form.
7858a39… drh 808 **
8ad5e46… wyoung 809 ** If the input is not a hash or a hash prefix, then try to resolve
7858a39… drh 810 ** the name as a tag. If multiple tags match, pick the latest.
8ad5e46… wyoung 811 ** A caller can force this routine to skip the hash case above by
8ad5e46… wyoung 812 ** prefixing the string with "tag:", a useful property when the tag
8ad5e46… wyoung 813 ** may be misinterpreted as a hex ASCII string. (e.g. "decade" or "facade")
7858a39… drh 814 **
7858a39… drh 815 ** If the input is not a tag, then try to match it as an ISO-8601 date
7858a39… drh 816 ** string YYYY-MM-DD HH:MM:SS and pick the nearest check-in to that date.
c54c2b0… jan.nijtmans 817 ** If the input is of the form "date:*" then always resolve the name as
c54c2b0… jan.nijtmans 818 ** a date. The forms "utc:*" and "local:" are deprecated.
7858a39… drh 819 **
7858a39… drh 820 ** Return 0 on success. Return 1 if the name cannot be resolved.
7858a39… drh 821 ** Return 2 name is ambiguous.
7858a39… drh 822 */
7858a39… drh 823 int name_to_uuid(Blob *pName, int iErrPriority, const char *zType){
7858a39… drh 824 char *zName = blob_str(pName);
7858a39… drh 825 int rid = symbolic_name_to_rid(zName, zType);
7858a39… drh 826 if( rid<0 ){
7858a39… drh 827 fossil_error(iErrPriority, "ambiguous name: %s", zName);
7858a39… drh 828 return 2;
7858a39… drh 829 }else if( rid==0 ){
30f669b… stephan 830 fossil_error(iErrPriority, "cannot resolve name: %s", zName);
7858a39… drh 831 return 1;
7858a39… drh 832 }else{
7858a39… drh 833 blob_reset(pName);
15df204… stephan 834 if( RID_CKOUT==rid ) {
15df204… stephan 835 rid = g.localOpen;
15df204… stephan 836 }
7858a39… drh 837 db_blob(pName, "SELECT uuid FROM blob WHERE rid=%d", rid);
7858a39… drh 838 return 0;
7858a39… drh 839 }
7858a39… drh 840 }
7858a39… drh 841
7858a39… drh 842 /*
c685a35… stephan 843 ** This routine is similar to name_to_uuid() except in the form it
c685a35… stephan 844 ** takes its parameters and returns its value, and in that it does not
8ad5e46… wyoung 845 ** treat errors as fatal. zName must be an artifact hash or prefix of
8ad5e46… wyoung 846 ** a hash. zType is also as described for name_to_uuid(). If
c685a35… stephan 847 ** zName does not resolve, 0 is returned. If it is ambiguous, a
c685a35… stephan 848 ** negative value is returned. On success the rid is returned and
35d43fd… drh 849 ** pUuid (if it is not NULL) is set to a newly-allocated string,
8ad5e46… wyoung 850 ** the full hash, which must eventually be free()d by the caller.
c685a35… stephan 851 */
38421a9… jan.nijtmans 852 int name_to_uuid2(const char *zName, const char *zType, char **pUuid){
c685a35… stephan 853 int rid = symbolic_name_to_rid(zName, zType);
c685a35… stephan 854 if((rid>0) && pUuid){
4d8c302… stephan 855 *pUuid = (rid<RID_MAX)
4d8c302… stephan 856 ? db_text(NULL, "SELECT uuid FROM blob WHERE rid=%d", rid)
4d8c302… stephan 857 : NULL;
c685a35… stephan 858 }
c685a35… stephan 859 return rid;
c685a35… stephan 860 }
c685a35… stephan 861
c685a35… stephan 862
49467d2… drh 863 /*
49467d2… drh 864 ** name_collisions searches through events, blobs, and tickets for
8ad5e46… wyoung 865 ** collisions of a given hash based on its length, counting only
8ad5e46… wyoung 866 ** hashes greater than or equal to 4 hex ASCII characters (16 bits)
8ad5e46… wyoung 867 ** in length.
49467d2… drh 868 */
49467d2… drh 869 int name_collisions(const char *zName){
49467d2… drh 870 int c = 0; /* count of collisions for zName */
49467d2… drh 871 int nLen; /* length of zName */
49467d2… drh 872 nLen = strlen(zName);
fd9b7bd… drh 873 if( nLen>=4 && nLen<=HNAME_MAX && validate16(zName, nLen) ){
421c9ee… drh 874 c = db_int(0,
421c9ee… drh 875 "SELECT"
421c9ee… drh 876 " (SELECT count(*) FROM ticket"
421c9ee… drh 877 " WHERE tkt_uuid GLOB '%q*') +"
421c9ee… drh 878 " (SELECT count(*) FROM tag"
421c9ee… drh 879 " WHERE tagname GLOB 'event-%q*') +"
421c9ee… drh 880 " (SELECT count(*) FROM blob"
421c9ee… drh 881 " WHERE uuid GLOB '%q*');",
421c9ee… drh 882 zName, zName, zName
421c9ee… drh 883 );
421c9ee… drh 884 if( c<2 ) c = 0;
49467d2… drh 885 }
49467d2… drh 886 return c;
49467d2… drh 887 }
c685a35… stephan 888
c685a35… stephan 889 /*
26eef7f… rberteig 890 ** COMMAND: test-name-to-id
26eef7f… rberteig 891 **
4d8c302… stephan 892 ** Usage: %fossil test-name-to-id [--count N] [--type ARTIFACT_TYPE] NAME
e632a09… drh 893 **
e632a09… drh 894 ** Convert a NAME to a full artifact ID. Repeat the conversion N
e632a09… drh 895 ** times (for timing purposes) if the --count option is given.
2fac809… drh 896 */
2fac809… drh 897 void test_name_to_id(void){
2fac809… drh 898 int i;
e632a09… drh 899 int n = 0;
2fac809… drh 900 Blob name;
4d8c302… stephan 901 const char *zType;
4d8c302… stephan 902
2fac809… drh 903 db_must_be_within_tree();
4d8c302… stephan 904 if( (zType = find_option("type","t",1))==0 ){
4d8c302… stephan 905 zType = "*";
4d8c302… stephan 906 }
2fac809… drh 907 for(i=2; i<g.argc; i++){
e632a09… drh 908 if( strcmp(g.argv[i],"--count")==0 && i+1<g.argc ){
e632a09… drh 909 i++;
e632a09… drh 910 n = atoi(g.argv[i]);
e632a09… drh 911 continue;
e632a09… drh 912 }
e632a09… drh 913 do{
e632a09… drh 914 blob_init(&name, g.argv[i], -1);
e632a09… drh 915 fossil_print("%s -> ", g.argv[i]);
4d8c302… stephan 916 if( name_to_uuid(&name, 1, zType) ){
e632a09… drh 917 fossil_print("ERROR: %s\n", g.zErrMsg);
e632a09… drh 918 fossil_error_reset();
e632a09… drh 919 }else{
e632a09… drh 920 fossil_print("%s\n", blob_buffer(&name));
e632a09… drh 921 }
e632a09… drh 922 blob_reset(&name);
e632a09… drh 923 }while( n-- > 0 );
2fac809… drh 924 }
2fac809… drh 925 }
2fac809… drh 926
2fac809… drh 927 /*
7858a39… drh 928 ** Convert a name to a rid. If the name can be any of the various forms
7858a39… drh 929 ** accepted:
7858a39… drh 930 **
fd9b7bd… drh 931 ** * artifact hash or prefix thereof
7858a39… drh 932 ** * symbolic name
7858a39… drh 933 ** * date
7858a39… drh 934 ** * label:date
7858a39… drh 935 ** * prev, previous
7858a39… drh 936 ** * next
7858a39… drh 937 ** * tip
2fac809… drh 938 **
2fac809… drh 939 ** This routine is used by command-line routines to resolve command-line inputs
2fac809… drh 940 ** into a rid.
2fac809… drh 941 */
2a013f0… drh 942 int name_to_typed_rid(const char *zName, const char *zType){
2a013f0… drh 943 int rid;
2a013f0… drh 944
2a013f0… drh 945 if( zName==0 || zName[0]==0 ) return 0;
7858a39… drh 946 rid = symbolic_name_to_rid(zName, zType);
7858a39… drh 947 if( rid<0 ){
a81a47f… drh 948 fossil_fatal("ambiguous name: %s", zName);
7858a39… drh 949 }else if( rid==0 ){
30f669b… stephan 950 fossil_fatal("cannot resolve name: %s", zName);
2a013f0… drh 951 }
a81a47f… drh 952 return rid;
2a013f0… drh 953 }
2a013f0… drh 954 int name_to_rid(const char *zName){
2a013f0… drh 955 return name_to_typed_rid(zName, "*");
30f669b… stephan 956 }
30f669b… stephan 957
30f669b… stephan 958 /*
1ff12ea… drh 959 ** Try to resolve zQP1 into a check-in name. If zQP1 does not exist,
1ff12ea… drh 960 ** return 0. If zQP1 exists but cannot be resolved, then also try to
1ff12ea… drh 961 ** resolve zQP2 if it exists. If zQP1 cannot be resolved but zQP2 does
1ff12ea… drh 962 ** not exist, then raise an error. If both zQP1 and zQP2 exists but
1ff12ea… drh 963 ** neither can be resolved, also raise an error.
452f050… drh 964 **
452f050… drh 965 ** If pzPick is not a NULL pointer, then *pzPick to be the value of whichever
452f050… drh 966 ** query parameter ended up being used.
1ff12ea… drh 967 */
452f050… drh 968 int name_choice(const char *zQP1, const char *zQP2, const char **pzPick){
1ff12ea… drh 969 const char *zName, *zName2;
1ff12ea… drh 970 int rid;
1ff12ea… drh 971 zName = P(zQP1);
1ff12ea… drh 972 if( zName==0 || zName[0]==0 ) return 0;
1ff12ea… drh 973 rid = symbolic_name_to_rid(zName, "ci");
452f050… drh 974 if( rid>0 ){
452f050… drh 975 if( pzPick ) *pzPick = zName;
452f050… drh 976 return rid;
452f050… drh 977 }
1ff12ea… drh 978 if( rid<0 ){
1ff12ea… drh 979 fossil_fatal("ambiguous name: %s", zName);
1ff12ea… drh 980 }
1ff12ea… drh 981 zName2 = P(zQP2);
1ff12ea… drh 982 if( zName2==0 || zName2[0]==0 ){
1ff12ea… drh 983 fossil_fatal("cannot resolve name: %s", zName);
1ff12ea… drh 984 }
452f050… drh 985 if( pzPick ) *pzPick = zName2;
1ff12ea… drh 986 return name_to_typed_rid(zName2, "ci");
1ff12ea… drh 987 }
1ff12ea… drh 988
1ff12ea… drh 989 /*
2fac809… drh 990 ** WEBPAGE: ambiguous
fd9b7bd… drh 991 ** URL: /ambiguous?name=NAME&src=WEBPAGE
c54c2b0… jan.nijtmans 992 **
fd9b7bd… drh 993 ** The NAME given by the name parameter is ambiguous. Display a page
2fac809… drh 994 ** that shows all possible choices and let the user select between them.
9a0132e… drh 995 **
9a0132e… drh 996 ** The src= query parameter is optional. If omitted it defaults
9a0132e… drh 997 ** to "info".
2fac809… drh 998 */
2fac809… drh 999 void ambiguous_page(void){
2fac809… drh 1000 Stmt q;
c54c2b0… jan.nijtmans 1001 const char *zName = P("name");
9a0132e… drh 1002 const char *zSrc = PD("src","info");
2fac809… drh 1003 char *z;
c54c2b0… jan.nijtmans 1004
2fac809… drh 1005 if( zName==0 || zName[0]==0 || zSrc==0 || zSrc[0]==0 ){
2fac809… drh 1006 fossil_redirect_home();
2fac809… drh 1007 }
2fac809… drh 1008 style_header("Ambiguous Artifact ID");
9a0132e… drh 1009 @ <p>The artifact hash prefix <b>%h(zName)</b> is ambiguous and might
2fac809… drh 1010 @ mean any of the following:
2fac809… drh 1011 @ <ol>
4c3e172… danield 1012 z = fossil_strdup(zName);
96722b6… drh 1013 canonical16(z, strlen(z));
96722b6… drh 1014 db_prepare(&q, "SELECT uuid, rid FROM blob WHERE uuid GLOB '%q*'", z);
96722b6… drh 1015 while( db_step(&q)==SQLITE_ROW ){
96722b6… drh 1016 const char *zUuid = db_column_text(&q, 0);
96722b6… drh 1017 int rid = db_column_int(&q, 1);
1fee037… drh 1018 @ <li><p><a href="%R/%T(zSrc)/%!S(zUuid)">
49467d2… drh 1019 @ %s(zUuid)</a> -
2b07e4e… drh 1020 object_description(rid, 0, 0, 0);
49467d2… drh 1021 @ </p></li>
49467d2… drh 1022 }
49467d2… drh 1023 db_finalize(&q);
49467d2… drh 1024 db_prepare(&q,
49467d2… drh 1025 " SELECT tkt_rid, tkt_uuid, title"
49467d2… drh 1026 " FROM ticket, ticketchng"
49467d2… drh 1027 " WHERE ticket.tkt_id = ticketchng.tkt_id"
49467d2… drh 1028 " AND tkt_uuid GLOB '%q*'"
49467d2… drh 1029 " GROUP BY tkt_uuid"
49467d2… drh 1030 " ORDER BY tkt_ctime DESC", z);
49467d2… drh 1031 while( db_step(&q)==SQLITE_ROW ){
fe6d393… jan.nijtmans 1032 int rid = db_column_int(&q, 0);
49467d2… drh 1033 const char *zUuid = db_column_text(&q, 1);
49467d2… drh 1034 const char *zTitle = db_column_text(&q, 2);
1fee037… drh 1035 @ <li><p><a href="%R/%T(zSrc)/%!S(zUuid)">
49467d2… drh 1036 @ %s(zUuid)</a> -
49467d2… drh 1037 @ <ul></ul>
49467d2… drh 1038 @ Ticket
8ad5e46… wyoung 1039 hyperlink_to_version(zUuid);
8e27a5a… drh 1040 @ - %h(zTitle).
49467d2… drh 1041 @ <ul><li>
2b07e4e… drh 1042 object_description(rid, 0, 0, 0);
49467d2… drh 1043 @ </li></ul>
49467d2… drh 1044 @ </p></li>
49467d2… drh 1045 }
49467d2… drh 1046 db_finalize(&q);
49467d2… drh 1047 db_prepare(&q,
49467d2… drh 1048 "SELECT rid, uuid FROM"
49467d2… drh 1049 " (SELECT tagxref.rid AS rid, substr(tagname, 7) AS uuid"
49467d2… drh 1050 " FROM tagxref, tag WHERE tagxref.tagid = tag.tagid"
49467d2… drh 1051 " AND tagname GLOB 'event-%q*') GROUP BY uuid", z);
49467d2… drh 1052 while( db_step(&q)==SQLITE_ROW ){
fe6d393… jan.nijtmans 1053 int rid = db_column_int(&q, 0);
49467d2… drh 1054 const char* zUuid = db_column_text(&q, 1);
1fee037… drh 1055 @ <li><p><a href="%R/%T(zSrc)/%!S(zUuid)">
42e66c1… drh 1056 @ %s(zUuid)</a> -
49467d2… drh 1057 @ <ul><li>
2b07e4e… drh 1058 object_description(rid, 0, 0, 0);
49467d2… drh 1059 @ </li></ul>
96722b6… drh 1060 @ </p></li>
96722b6… drh 1061 }
96722b6… drh 1062 @ </ol>
07e93f3… drh 1063 db_finalize(&q);
112c713… drh 1064 style_finish_page();
96722b6… drh 1065 }
96722b6… drh 1066
96722b6… drh 1067 /*
96722b6… drh 1068 ** Convert the name in CGI parameter zParamName into a rid and return that
96722b6… drh 1069 ** rid. If the CGI parameter is missing or is not a valid artifact tag,
96722b6… drh 1070 ** return 0. If the CGI parameter is ambiguous, redirect to a page that
96722b6… drh 1071 ** shows all possibilities and do not return.
96722b6… drh 1072 */
96722b6… drh 1073 int name_to_rid_www(const char *zParamName){
96722b6… drh 1074 int rid;
96722b6… drh 1075 const char *zName = P(zParamName);
796dcfe… drh 1076 #ifdef FOSSIL_ENABLE_JSON
796dcfe… drh 1077 if(!zName && fossil_has_json()){
796dcfe… drh 1078 zName = json_find_option_cstr(zParamName,NULL,NULL);
796dcfe… drh 1079 }
796dcfe… drh 1080 #endif
7858a39… drh 1081 if( zName==0 || zName[0]==0 ) return 0;
7858a39… drh 1082 rid = symbolic_name_to_rid(zName, "*");
7858a39… drh 1083 if( rid<0 ){
a40e8a0… drh 1084 cgi_redirectf("%R/ambiguous/%T?src=%t", zName, g.zPath);
7858a39… drh 1085 rid = 0;
7858a39… drh 1086 }
7858a39… drh 1087 return rid;
9c3ce9f… drh 1088 }
9c3ce9f… drh 1089
9c3ce9f… drh 1090 /*
41f0838… stephan 1091 ** Given an RID of a structural artifact, which is assumed to be
7c64fe0… drh 1092 ** valid, this function returns one of the CFTYPE_xxx values
7c64fe0… drh 1093 ** describing the record type, or 0 if the RID does not refer to an
7c64fe0… drh 1094 ** artifact record (as determined by reading the event table).
7c64fe0… drh 1095 **
c797abe… stephan 1096 ** Note that this function never returns CFTYPE_ATTACHMENT or
7c64fe0… drh 1097 ** CFTYPE_CLUSTER because those are not used in the event table. Thus
7c64fe0… drh 1098 ** it cannot be used to distinguish those artifact types from
7c64fe0… drh 1099 ** non-artifact file content.
c797abe… stephan 1100 **
c797abe… stephan 1101 ** Potential TODO: if the rid is not found in the timeline, try to
c797abe… stephan 1102 ** match it to a client file and return a hypothetical new
c797abe… stephan 1103 ** CFTYPE_OPAQUE or CFTYPE_NONARTIFACT if a match is found.
41f0838… stephan 1104 */
7c64fe0… drh 1105 int whatis_rid_type(int rid){
41f0838… stephan 1106 Stmt q = empty_Stmt;
7c64fe0… drh 1107 int type = 0;
41f0838… stephan 1108 /* Check for entries on the timeline that reference this object */
41f0838… stephan 1109 db_prepare(&q,
41f0838… stephan 1110 "SELECT type FROM event WHERE objid=%d", rid);
41f0838… stephan 1111 if( db_step(&q)==SQLITE_ROW ){
41f0838… stephan 1112 switch( db_column_text(&q,0)[0] ){
7c64fe0… drh 1113 case 'c': type = CFTYPE_MANIFEST; break;
7c64fe0… drh 1114 case 'w': type = CFTYPE_WIKI; break;
7c64fe0… drh 1115 case 'e': type = CFTYPE_EVENT; break;
7c64fe0… drh 1116 case 'f': type = CFTYPE_FORUM; break;
7c64fe0… drh 1117 case 't': type = CFTYPE_TICKET; break;
7c64fe0… drh 1118 case 'g': type = CFTYPE_CONTROL; break;
41f0838… stephan 1119 default: break;
41f0838… stephan 1120 }
41f0838… stephan 1121 }
41f0838… stephan 1122 db_finalize(&q);
7c64fe0… drh 1123 return type;
7c64fe0… drh 1124 }
7c64fe0… drh 1125
7c64fe0… drh 1126 /*
7c64fe0… drh 1127 ** A proxy for whatis_rid_type() which returns a brief string (in
7c64fe0… drh 1128 ** static memory) describing the record type. Returns NULL if rid does
7c64fe0… drh 1129 ** not refer to an artifact record (as determined by reading the event
7c64fe0… drh 1130 ** table). The returned string is intended to be used in headers which
7c64fe0… drh 1131 ** can refer to different artifact types. It is not "definitive," in
7c64fe0… drh 1132 ** that it does not distinguish between closely-related types like
7c64fe0… drh 1133 ** wiki creation, edit, and removal.
7c64fe0… drh 1134 */
7c64fe0… drh 1135 char const * whatis_rid_type_label(int rid){
7c64fe0… drh 1136 char const * zType = 0;
7c64fe0… drh 1137 switch( whatis_rid_type(rid) ){
7c64fe0… drh 1138 case CFTYPE_MANIFEST: zType = "Check-in"; break;
7c64fe0… drh 1139 case CFTYPE_WIKI: zType = "Wiki-edit"; break;
7c64fe0… drh 1140 case CFTYPE_EVENT: zType = "Technote"; break;
7c64fe0… drh 1141 case CFTYPE_FORUM: zType = "Forum-post"; break;
7c64fe0… drh 1142 case CFTYPE_TICKET: zType = "Ticket-change"; break;
7c64fe0… drh 1143 case CFTYPE_CONTROL: zType = "Tag-change"; break;
7c64fe0… drh 1144 default: break;
7c64fe0… drh 1145 }
41f0838… stephan 1146 return zType;
41f0838… stephan 1147 }
41f0838… stephan 1148
41f0838… stephan 1149 /*
9d50322… drh 1150 ** This is the output routine for whatis_rid(). If pOut is NULL
9d50322… drh 1151 ** then write zKey and zValue on fossil_print(). But if pOut is
9d50322… drh 1152 ** not NULL, append the key and value as a term of a JSON object.
9d50322… drh 1153 */
9d50322… drh 1154 static void whatis_line(
9d50322… drh 1155 sqlite3_str *pOut,
9d50322… drh 1156 const char *zKey,
9d50322… drh 1157 const char *zValue,
9d50322… drh 1158 ...
9d50322… drh 1159 ){
9d50322… drh 1160 char *zArg;
9d50322… drh 1161 va_list ap;
9d50322… drh 1162 va_start(ap, zValue);
9d50322… drh 1163 zArg = sqlite3_vmprintf(zValue,ap);
9d50322… drh 1164 va_end(ap);
9d50322… drh 1165 if( pOut ){
9d50322… drh 1166 int n = sqlite3_str_length(pOut);
9d50322… drh 1167 if( n>1 && sqlite3_str_value(pOut)[n-1]!='{' ){
9d50322… drh 1168 sqlite3_str_append(pOut, ",", 1);
9d50322… drh 1169 }
9d50322… drh 1170 sqlite3_str_appendf(pOut, "%J:%J", zKey, zArg);
9d50322… drh 1171 }else{
9d50322… drh 1172 int n = 11 - (int)strlen(zKey);
9d50322… drh 1173 if( n<=0 ) n = 0;
9d50322… drh 1174 fossil_print("%s:%*s%s\n",zKey,n,"",zArg);
9d50322… drh 1175 }
9d50322… drh 1176 sqlite3_free(zArg);
9d50322… drh 1177 }
9d50322… drh 1178
9d50322… drh 1179 /*
ac6edb3… drh 1180 ** Flag values for whatis_rid().
ac6edb3… drh 1181 */
ac6edb3… drh 1182 #if INTERFACE
5484968… drh 1183 #define WHATIS_VERBOSE 0x01 /* Extra output */
5484968… drh 1184 #define WHATIS_BRIEF 0x02 /* Omit unnecessary output */
5484968… drh 1185 #define WHATIS_REPO 0x04 /* Show repository name */
5484968… drh 1186 #define WHATIS_OMIT_UNK 0x08 /* Do not show "unknown" lines */
49f93f8… drh 1187 #define WHATIS_HASHONLY 0x10 /* Show only the hash */
9d50322… drh 1188 #define WHATIS_JSON 0x20 /* Render as JSON */
ac6edb3… drh 1189 #endif
ac6edb3… drh 1190
ac6edb3… drh 1191 /*
9d50322… drh 1192 ** Generate a description of artifact "rid".
9d50322… drh 1193 **
9d50322… drh 1194 ** Send results to stdout using fossil_print() if pOut==0 or if
9d50322… drh 1195 ** WHATIS_HASHONLY is set. But without WHATIS_HASHONLY and if
9d50322… drh 1196 ** pOut!=0, then write a JSON description of the object into pOut.
933904f… drh 1197 */
9d50322… drh 1198 void whatis_rid(int rid, int flags, sqlite3_str *pOut){
933904f… drh 1199 Stmt q;
5cdc39d… drh 1200 int cnt;
9d50322… drh 1201
9d50322… drh 1202 if( pOut ) sqlite3_str_appendf(pOut, "{\"rid\":%d", rid);
ea63a2d… drh 1203
5cdc39d… drh 1204 /* Basic information about the object. */
933904f… drh 1205 db_prepare(&q,
ea63a2d… drh 1206 "SELECT uuid, size, datetime(mtime,toLocal()), ipaddr"
933904f… drh 1207 " FROM blob, rcvfrom"
933904f… drh 1208 " WHERE rid=%d"
933904f… drh 1209 " AND rcvfrom.rcvid=blob.rcvid",
ea63a2d… drh 1210 rid);
933904f… drh 1211 if( db_step(&q)==SQLITE_ROW ){
49f93f8… drh 1212 if( flags & WHATIS_HASHONLY ){
49f93f8… drh 1213 fossil_print("%s\n", db_column_text(&q,0));
49f93f8… drh 1214 }else if( flags & WHATIS_VERBOSE ){
9d50322… drh 1215 whatis_line(pOut, "artifact", "%s (%d)", db_column_text(&q,0), rid);
9d50322… drh 1216 whatis_line(pOut, "size", "%d bytes", db_column_int(&q,1));
9d50322… drh 1217 whatis_line(pOut, "received", "%s from %s",
933904f… drh 1218 db_column_text(&q, 2),
933904f… drh 1219 db_column_text(&q, 3));
933904f… drh 1220 }else{
9d50322… drh 1221 whatis_line(pOut, "artifact","%s", db_column_text(&q,0));
9d50322… drh 1222 whatis_line(pOut, "size", "%d bytes", db_column_int(&q,1));
5cdc39d… drh 1223 }
9d50322… drh 1224 }else if( pOut ){
9d50322… drh 1225 sqlite3_str_appendf(pOut, ",\"artifact\":null");
5cdc39d… drh 1226 }
5cdc39d… drh 1227 db_finalize(&q);
49f93f8… drh 1228 if( flags & WHATIS_HASHONLY ) return;
5cdc39d… drh 1229
5cdc39d… drh 1230 /* Report any symbolic tags on this artifact */
5cdc39d… drh 1231 db_prepare(&q,
5cdc39d… drh 1232 "SELECT substr(tagname,5)"
5cdc39d… drh 1233 " FROM tag JOIN tagxref ON tag.tagid=tagxref.tagid"
5cdc39d… drh 1234 " WHERE tagxref.rid=%d"
cf6bb07… stephan 1235 " AND tagxref.tagtype<>0"
5cdc39d… drh 1236 " AND tagname GLOB 'sym-*'"
5cdc39d… drh 1237 " ORDER BY 1",
5cdc39d… drh 1238 rid
5cdc39d… drh 1239 );
5cdc39d… drh 1240 cnt = 0;
5cdc39d… drh 1241 while( db_step(&q)==SQLITE_ROW ){
9d50322… drh 1242 const char *zTag = (const char*)db_column_text(&q,0);
9d50322… drh 1243 if( pOut ){
9d50322… drh 1244 const char *zSep = cnt==0 ? ",tags:[" : ",";
9d50322… drh 1245 sqlite3_str_appendf(pOut, "%s%J", zSep, zTag);
9d50322… drh 1246 }else{
9d50322… drh 1247 const char *zPrefix = cnt ? ", " : "tags: ";
9d50322… drh 1248 fossil_print("%s%s", zPrefix, db_column_text(&q,0));
9d50322… drh 1249 }
9d50322… drh 1250 cnt++;
5cdc39d… drh 1251 }
9d50322… drh 1252 if( cnt ){
9d50322… drh 1253 if( pOut ){
9d50322… drh 1254 sqlite3_str_append(pOut, "]", 1);
9d50322… drh 1255 }else{
9d50322… drh 1256 fossil_print("\n");
9d50322… drh 1257 }
9d50322… drh 1258 }
5cdc39d… drh 1259 db_finalize(&q);
5cdc39d… drh 1260
5cdc39d… drh 1261 /* Report any HIDDEN, PRIVATE, CLUSTER, or CLOSED tags on this artifact */
5cdc39d… drh 1262 db_prepare(&q,
5cdc39d… drh 1263 "SELECT tagname"
5cdc39d… drh 1264 " FROM tag JOIN tagxref ON tag.tagid=tagxref.tagid"
5cdc39d… drh 1265 " WHERE tagxref.rid=%d"
5cdc39d… drh 1266 " AND tag.tagid IN (5,6,7,9)"
5cdc39d… drh 1267 " ORDER BY 1",
49b0ff1… drh 1268 rid
5cdc39d… drh 1269 );
5cdc39d… drh 1270 cnt = 0;
5cdc39d… drh 1271 while( db_step(&q)==SQLITE_ROW ){
9d50322… drh 1272 const char *zTag = (const char*)db_column_text(&q,0);
9d50322… drh 1273 if( pOut ){
9d50322… drh 1274 const char *zSep = cnt==0 ? ",\"raw-tags\":[" : ",";
9d50322… drh 1275 sqlite3_str_appendf(pOut, "%s%J", zSep, zTag);
9d50322… drh 1276 }else{
9d50322… drh 1277 const char *zPrefix = cnt ? ", " : "raw-tags: ";
9d50322… drh 1278 fossil_print("%s%s", zPrefix, db_column_text(&q,0));
9d50322… drh 1279 }
9d50322… drh 1280 cnt++;
5cdc39d… drh 1281 }
9d50322… drh 1282 if( cnt ){
9d50322… drh 1283 if( pOut ){
9d50322… drh 1284 sqlite3_str_append(pOut, "]", 1);
9d50322… drh 1285 }else{
9d50322… drh 1286 fossil_print("\n");
9d50322… drh 1287 }
9d50322… drh 1288 }
933904f… drh 1289 db_finalize(&q);
5cdc39d… drh 1290
5cdc39d… drh 1291 /* Check for entries on the timeline that reference this object */
933904f… drh 1292 db_prepare(&q,
86d07e8… drh 1293 "SELECT"
86d07e8… drh 1294 " type,"
86d07e8… drh 1295 " datetime(mtime,toLocal()),"
86d07e8… drh 1296 " coalesce(euser,user),"
86d07e8… drh 1297 " coalesce(ecomment,comment),"
86d07e8… drh 1298 " if(type='t',(SELECT substr(tagname,5) FROM tag"
86d07e8… drh 1299 " WHERE tag.tagid=event.tagid))"
86d07e8… drh 1300 "FROM event WHERE objid=%d",
86d07e8… drh 1301 rid);
933904f… drh 1302 if( db_step(&q)==SQLITE_ROW ){
933904f… drh 1303 const char *zType;
86d07e8… drh 1304 char eType = db_column_text(&q,0)[0];
86d07e8… drh 1305 switch( eType ){
933904f… drh 1306 case 'c': zType = "Check-in"; break;
933904f… drh 1307 case 'w': zType = "Wiki-edit"; break;
99fcc43… drh 1308 case 'e': zType = "Technote"; break;
99fcc43… drh 1309 case 'f': zType = "Forum-post"; break;
933904f… drh 1310 case 't': zType = "Ticket-change"; break;
933904f… drh 1311 case 'g': zType = "Tag-change"; break;
933904f… drh 1312 default: zType = "Unknown"; break;
933904f… drh 1313 }
9d50322… drh 1314 whatis_line(pOut, "type", "%s by %s on %s", zType, db_column_text(&q,2),
933904f… drh 1315 db_column_text(&q, 1));
86d07e8… drh 1316 if( eType=='t' && db_column_type(&q,4)==SQLITE_TEXT ){
9d50322… drh 1317 whatis_line(pOut, "ticket-id", "%s", db_column_text(&q,4));
86d07e8… drh 1318 }
9d50322… drh 1319 if( pOut ){
9d50322… drh 1320 sqlite3_str_appendf(pOut, ",\"comment\":%J", db_column_text(&q,3));
9d50322… drh 1321 }else{
9d50322… drh 1322 fossil_print("comment: ");
9d50322… drh 1323 comment_print(db_column_text(&q,3), 0, 12, -1, get_comment_format());
9d50322… drh 1324 }
d250276… drh 1325 cnt++;
933904f… drh 1326 }
933904f… drh 1327 db_finalize(&q);
5cdc39d… drh 1328
5cdc39d… drh 1329 /* Check to see if this object is used as a file in a check-in */
933904f… drh 1330 db_prepare(&q,
ea63a2d… drh 1331 "SELECT filename.name, blob.uuid, datetime(event.mtime,toLocal()),"
d3896c2… mgagnon 1332 " coalesce(euser,user), coalesce(ecomment,comment),"
d3896c2… mgagnon 1333 " coalesce((SELECT value FROM tagxref"
d3896c2… mgagnon 1334 " WHERE tagid=%d AND tagtype>0 AND rid=mlink.mid),'trunk')"
933904f… drh 1335 " FROM mlink, filename, blob, event"
933904f… drh 1336 " WHERE mlink.fid=%d"
933904f… drh 1337 " AND filename.fnid=mlink.fnid"
933904f… drh 1338 " AND event.objid=mlink.mid"
933904f… drh 1339 " AND blob.rid=mlink.mid"
ac6edb3… drh 1340 " ORDER BY event.mtime %s /*sort*/",
d3896c2… mgagnon 1341 TAG_BRANCH, rid,
ac6edb3… drh 1342 (flags & WHATIS_BRIEF) ? "LIMIT 1" : "DESC");
5cdc39d… drh 1343 while( db_step(&q)==SQLITE_ROW ){
ac6edb3… drh 1344 if( flags & WHATIS_BRIEF ){
9d50322… drh 1345 whatis_line(pOut, "mtime","%s", db_column_text(&q,2));
9d50322… drh 1346 }
9d50322… drh 1347 if( pOut ){
9d50322… drh 1348 whatis_line(pOut,"file","%s", db_column_text(&q,0));
9d50322… drh 1349 whatis_line(pOut,"part-of","%s", db_column_text(&q,1));
9d50322… drh 1350 }else{
9d50322… drh 1351 fossil_print("file: %s\n", db_column_text(&q,0));
9d50322… drh 1352 fossil_print(" part of [%S] on branch %s by %s on %s\n",
9d50322… drh 1353 db_column_text(&q, 1),
9d50322… drh 1354 db_column_text(&q, 5),
9d50322… drh 1355 db_column_text(&q, 3),
9d50322… drh 1356 db_column_text(&q, 2));
9d50322… drh 1357 fossil_print(" ");
9d50322… drh 1358 comment_print(db_column_text(&q,4), 0, 12, -1, get_comment_format());
9d50322… drh 1359 }
d250276… drh 1360 cnt++;
5cdc39d… drh 1361 }
5cdc39d… drh 1362 db_finalize(&q);
5cdc39d… drh 1363
5cdc39d… drh 1364 /* Check to see if this object is used as an attachment */
5cdc39d… drh 1365 db_prepare(&q,
5cdc39d… drh 1366 "SELECT attachment.filename,"
5cdc39d… drh 1367 " attachment.comment,"
5cdc39d… drh 1368 " attachment.user,"
ea63a2d… drh 1369 " datetime(attachment.mtime,toLocal()),"
5cdc39d… drh 1370 " attachment.target,"
5cdc39d… drh 1371 " CASE WHEN EXISTS(SELECT 1 FROM tag WHERE tagname=('tkt-'||target))"
5cdc39d… drh 1372 " THEN 'ticket'"
5cdc39d… drh 1373 " WHEN EXISTS(SELECT 1 FROM tag WHERE tagname=('wiki-'||target))"
5cdc39d… drh 1374 " THEN 'wiki' END,"
5cdc39d… drh 1375 " attachment.attachid,"
5cdc39d… drh 1376 " (SELECT uuid FROM blob WHERE rid=attachid)"
5cdc39d… drh 1377 " FROM attachment JOIN blob ON attachment.src=blob.uuid"
5cdc39d… drh 1378 " WHERE blob.rid=%d",
ea63a2d… drh 1379 rid
5cdc39d… drh 1380 );
933904f… drh 1381 while( db_step(&q)==SQLITE_ROW ){
9d50322… drh 1382 if( pOut ){
9d50322… drh 1383 whatis_line(pOut, "attachment", "%s", db_column_text(&q,0));
9d50322… drh 1384 whatis_line(pOut, "attached-to", "%s %s",
9d50322… drh 1385 db_column_text(&q,5), db_column_text(&q,4));
5cdc39d… drh 1386 }else{
9d50322… drh 1387 fossil_print("attachment: %s\n", db_column_text(&q,0));
9d50322… drh 1388 fossil_print(" attached to %s %s\n",
9d50322… drh 1389 db_column_text(&q,5), db_column_text(&q,4));
9d50322… drh 1390 if( flags & WHATIS_VERBOSE ){
9d50322… drh 1391 fossil_print(" via %s (%d)\n",
9d50322… drh 1392 db_column_text(&q,7), db_column_int(&q,6));
9d50322… drh 1393 }else{
9d50322… drh 1394 fossil_print(" via %s\n",
9d50322… drh 1395 db_column_text(&q,7));
9d50322… drh 1396 }
9d50322… drh 1397 fossil_print(" by user %s on %s\n",
9d50322… drh 1398 db_column_text(&q,2), db_column_text(&q,3));
9d50322… drh 1399 fossil_print(" ");
9d50322… drh 1400 comment_print(db_column_text(&q,1), 0, 12, -1, get_comment_format());
5cdc39d… drh 1401 }
d250276… drh 1402 cnt++;
933904f… drh 1403 }
933904f… drh 1404 db_finalize(&q);
d250276… drh 1405
d250276… drh 1406 /* If other information available, try to describe the object */
9d50322… drh 1407 if( cnt==0 && pOut==0 ){
d250276… drh 1408 char *zWhere = mprintf("=%d", rid);
d250276… drh 1409 char *zDesc;
d250276… drh 1410 describe_artifacts(zWhere);
d250276… drh 1411 free(zWhere);
d250276… drh 1412 zDesc = db_text(0,
d250276… drh 1413 "SELECT printf('%%-12s%%s %%s',type||':',summary,substr(ref,1,16))"
d250276… drh 1414 " FROM description WHERE rid=%d", rid);
d250276… drh 1415 fossil_print("%s\n", zDesc);
d250276… drh 1416 fossil_free(zDesc);
d250276… drh 1417 }
9d50322… drh 1418
9d50322… drh 1419 if( pOut ) sqlite3_str_append(pOut, "}", 1);
a821cbf… mgagnon 1420 }
a821cbf… mgagnon 1421
a821cbf… mgagnon 1422 /*
a821cbf… mgagnon 1423 ** Generate a description of artifact from it symbolic name.
a821cbf… mgagnon 1424 */
a821cbf… mgagnon 1425 void whatis_artifact(
9d50322… drh 1426 const char *zName, /* Symbolic name or full hash */
9d50322… drh 1427 const char *zFileName,/* Optional: original filename (in file mode) */
9d50322… drh 1428 const char *zType, /* Artifact type filter */
9d50322… drh 1429 int mFlags, /* WHATIS_* flags */
9d50322… drh 1430 sqlite3_str *pOut /* Write JSON here, if not NULL */
a821cbf… mgagnon 1431 ){
a821cbf… mgagnon 1432 int rid = symbolic_name_to_rid(zName, zType);
86d07e8… drh 1433 size_t nName;
86d07e8… drh 1434 char *zC = 0;
86d07e8… drh 1435 int nTkt = 0;
9d50322… drh 1436
9d50322… drh 1437 if( pOut ){
9d50322… drh 1438 mFlags &= ~(WHATIS_HASHONLY|WHATIS_REPO);
9d50322… drh 1439 zFileName = 0;
9d50322… drh 1440 }
86d07e8… drh 1441 if( (nName = strlen(zName))>=4 && validate16(zName,nName) ){
86d07e8… drh 1442 zC = fossil_strdup(zName);
86d07e8… drh 1443 canonical16(zC, nName);
86d07e8… drh 1444 nTkt = db_int(0,"SELECT count(*) FROM tag WHERE tagname GLOB 'tkt-%q*'",zC);
86d07e8… drh 1445 if( nTkt>0 ){
86d07e8… drh 1446 if( rid==0 ){
86d07e8… drh 1447 rid = db_int(0,
86d07e8… drh 1448 "SELECT srcid FROM tag, tagxref"
86d07e8… drh 1449 " WHERE tag.tagname GLOB 'tkt-%q*'"
86d07e8… drh 1450 " AND tagxref.tagid=tag.tagid"
86d07e8… drh 1451 " AND tagxref.tagtype=1"
86d07e8… drh 1452 " ORDER BY tagxref.mtime",
86d07e8… drh 1453 zC
86d07e8… drh 1454 );
86d07e8… drh 1455 }else{
86d07e8… drh 1456 rid = -1;
86d07e8… drh 1457 }
86d07e8… drh 1458 }
86d07e8… drh 1459 }
a821cbf… mgagnon 1460 if( rid<0 ){
a821cbf… mgagnon 1461 Stmt q;
a821cbf… mgagnon 1462 int cnt = 0;
5484968… drh 1463 if( mFlags & WHATIS_REPO ){
5484968… drh 1464 fossil_print("\nrepository: %s\n", g.zRepositoryName);
5484968… drh 1465 }
e1e9da2… mgagnon 1466 if( zFileName ){
e1e9da2… mgagnon 1467 fossil_print("%-12s%s\n", "name:", zFileName);
e1e9da2… mgagnon 1468 }
9d50322… drh 1469 if( pOut ){
9d50322… drh 1470 sqlite3_str_appendall(pOut, "{\"ambiguous\":[");
9d50322… drh 1471 }else{
9d50322… drh 1472 fossil_print("%-12s%s (ambiguous)\n", "hash:", zName);
9d50322… drh 1473 }
a821cbf… mgagnon 1474 db_prepare(&q,
a821cbf… mgagnon 1475 "SELECT rid FROM blob WHERE uuid>=lower(%Q) AND uuid<(lower(%Q)||'z')",
a821cbf… mgagnon 1476 zName, zName
86d07e8… drh 1477 );
a821cbf… mgagnon 1478 while( db_step(&q)==SQLITE_ROW ){
9d50322… drh 1479 if( cnt++ ){
9d50322… drh 1480 if( pOut ){
9d50322… drh 1481 sqlite3_str_append(pOut, ",", 1);
9d50322… drh 1482 }else{
9d50322… drh 1483 fossil_print("%12s---- meaning #%d ----\n", " ", cnt);
9d50322… drh 1484 }
9d50322… drh 1485 }
9d50322… drh 1486 whatis_rid(db_column_int(&q, 0), mFlags, pOut);
a821cbf… mgagnon 1487 }
a821cbf… mgagnon 1488 db_finalize(&q);
86d07e8… drh 1489 if( nTkt>0 ){
86d07e8… drh 1490 db_prepare(&q,
86d07e8… drh 1491 "SELECT (SELECT srcid FROM tagxref"
86d07e8… drh 1492 " WHERE tagxref.tagid=tag.tagid"
86d07e8… drh 1493 " AND tagxref.tagtype=1"
86d07e8… drh 1494 " ORDER BY mtime LIMIT 1)"
86d07e8… drh 1495 " FROM tag WHERE tagname GLOB 'tkt-%q*'",
86d07e8… drh 1496 zC
86d07e8… drh 1497 );
86d07e8… drh 1498 while( db_step(&q)==SQLITE_ROW ){
9d50322… drh 1499 if( pOut ){
9d50322… drh 1500 sqlite3_str_append(pOut, ",", 1);
9d50322… drh 1501 }else{
9d50322… drh 1502 fossil_print("%12s---- meaning #%d ----\n", " ", cnt);
9d50322… drh 1503 }
9d50322… drh 1504 whatis_rid(db_column_int(&q, 0), mFlags, pOut);
86d07e8… drh 1505 }
86d07e8… drh 1506 db_finalize(&q);
86d07e8… drh 1507 }
9d50322… drh 1508 if( pOut ) sqlite3_str_append(pOut, "]}", 2);
a821cbf… mgagnon 1509 }else if( rid==0 ){
49f93f8… drh 1510 if( (mFlags & (WHATIS_OMIT_UNK|WHATIS_HASHONLY))==0 ){
5484968… drh 1511 /* 0123456789 12 */
e1e9da2… mgagnon 1512 if( zFileName ){
e1e9da2… mgagnon 1513 fossil_print("%-12s%s\n", "name:", zFileName);
e1e9da2… mgagnon 1514 }
9d50322… drh 1515 if( pOut ) sqlite3_str_append(pOut, "{", 1);
9d50322… drh 1516 whatis_line(pOut, "unknown", "%s", zName);
9d50322… drh 1517 if( pOut ) sqlite3_str_append(pOut, "}", 1);
5484968… drh 1518 }
a821cbf… mgagnon 1519 }else{
5484968… drh 1520 if( mFlags & WHATIS_REPO ){
5484968… drh 1521 fossil_print("\nrepository: %s\n", g.zRepositoryName);
5484968… drh 1522 }
e1e9da2… mgagnon 1523 if( zFileName ){
e1e9da2… mgagnon 1524 zName = zFileName;
e1e9da2… mgagnon 1525 }
9d50322… drh 1526 if( (mFlags & WHATIS_HASHONLY)==0 && pOut==0 ){
9d50322… drh 1527 whatis_line(pOut, "name", "%s", zName);
49f93f8… drh 1528 }
9d50322… drh 1529 whatis_rid(rid, mFlags, pOut);
a821cbf… mgagnon 1530 }
86d07e8… drh 1531 fossil_free(zC);
933904f… drh 1532 }
933904f… drh 1533
933904f… drh 1534 /*
933904f… drh 1535 ** COMMAND: whatis*
c3b5f1d… jan.nijtmans 1536 **
933904f… drh 1537 ** Usage: %fossil whatis NAME
933904f… drh 1538 **
fd9b7bd… drh 1539 ** Resolve the symbol NAME into its canonical artifact hash
933904f… drh 1540 ** artifact name and provide a description of what role that artifact
933904f… drh 1541 ** plays.
0f9d0f0… drh 1542 **
0f9d0f0… drh 1543 ** Options:
0bf2eaa… km 1544 ** -f|--file Find artifacts with the same hash as file NAME.
92d3fdb… km 1545 ** If NAME is "-", read content from standard input.
49f93f8… drh 1546 ** -h|--hash Show only the hash of matching artifacts.
5484968… drh 1547 ** -q|--quiet Show nothing if NAME is not found
32493b8… mgagnon 1548 ** -R REPO_FILE Specifies the repository db to use. Default is
32493b8… mgagnon 1549 ** the current check-out's repository.
0f9d0f0… drh 1550 ** --type TYPE Only find artifacts of TYPE (one of: 'ci', 't',
4cb50c4… stephan 1551 ** 'w', 'g', or 'e')
0f9d0f0… drh 1552 ** -v|--verbose Provide extra information (such as the RID)
933904f… drh 1553 */
933904f… drh 1554 void whatis_cmd(void){
5484968… drh 1555 int mFlags = 0;
a821cbf… mgagnon 1556 int fileFlag;
a73da23… drh 1557 int i;
a81a47f… drh 1558 const char *zType = 0;
9d50322… drh 1559 sqlite3_str *pOut = 0;
5cdc39d… drh 1560 db_find_and_open_repository(0,0);
5484968… drh 1561 if( find_option("verbose","v",0)!=0 ){
5484968… drh 1562 mFlags |= WHATIS_VERBOSE;
49f93f8… drh 1563 }
49f93f8… drh 1564 if( find_option("hash","h",0)!=0 ){
49f93f8… drh 1565 mFlags |= WHATIS_HASHONLY;
74d5ce3… florian 1566 }
74d5ce3… florian 1567 if( g.fQuiet ){
5484968… drh 1568 mFlags |= WHATIS_OMIT_UNK | WHATIS_REPO;
5484968… drh 1569 }
a821cbf… mgagnon 1570 fileFlag = find_option("file","f",0)!=0;
a81a47f… drh 1571 zType = find_option("type",0,1);
9d50322… drh 1572
9d50322… drh 1573 if( find_option("json",0,0)!=0 ){ /* undocumented test option */
9d50322… drh 1574 pOut = sqlite3_str_new(0);
9d50322… drh 1575 }
b9b3ce3… jan.nijtmans 1576
74ac0c9… drh 1577 /* We should be done with options.. */
74ac0c9… drh 1578 verify_all_options();
74ac0c9… drh 1579
57f1eae… rberteig 1580 if( g.argc<3 ) usage("NAME ...");
a73da23… drh 1581 for(i=2; i<g.argc; i++){
a821cbf… mgagnon 1582 const char *zName = g.argv[i];
a73da23… drh 1583 if( i>2 ) fossil_print("%.79c\n",'-');
a821cbf… mgagnon 1584 if( fileFlag ){
a821cbf… mgagnon 1585 Blob in;
a821cbf… mgagnon 1586 Blob hash = empty_blob;
a821cbf… mgagnon 1587 const char *zHash;
a821cbf… mgagnon 1588 /* Always follow symlinks (when applicable) */
a821cbf… mgagnon 1589 blob_read_from_file(&in, zName, ExtFILE);
a821cbf… mgagnon 1590
a821cbf… mgagnon 1591 /* First check the auxiliary hash to see if there is already an artifact
a821cbf… mgagnon 1592 ** that uses the auxiliary hash name */
a821cbf… mgagnon 1593 hname_hash(&in, 1, &hash);
a821cbf… mgagnon 1594 zHash = (const char*)blob_str(&hash);
a821cbf… mgagnon 1595 if( fast_uuid_to_rid(zHash)==0 ){
a821cbf… mgagnon 1596 /* No existing artifact with the auxiliary hash name. Therefore, use
a821cbf… mgagnon 1597 ** the primary hash name. */
a821cbf… mgagnon 1598 blob_reset(&hash);
a821cbf… mgagnon 1599 hname_hash(&in, 0, &hash);
a821cbf… mgagnon 1600 zHash = (const char*)blob_str(&hash);
a821cbf… mgagnon 1601 }
9d50322… drh 1602 whatis_artifact(zHash, zName, zType, mFlags, pOut);
a821cbf… mgagnon 1603 blob_reset(&hash);
a821cbf… mgagnon 1604 }else{
9d50322… drh 1605 whatis_artifact(zName, 0, zType, mFlags, pOut);
9d50322… drh 1606 }
9d50322… drh 1607 if( pOut ){
9d50322… drh 1608 fossil_print("%s\n", sqlite3_str_value(pOut));
9d50322… drh 1609 sqlite3_str_truncate(pOut, 0);
a821cbf… mgagnon 1610 }
a821cbf… mgagnon 1611 }
9d50322… drh 1612 if( pOut ) sqlite3_str_free(pOut);
9d50322… drh 1613 }
9d50322… drh 1614
9d50322… drh 1615 /*
9d50322… drh 1616 ** This is an SQL function that does the rough equivalent of the
9d50322… drh 1617 ** "whatis" command. The argument can be a text identifier, or a
9d50322… drh 1618 ** integer RID.
9d50322… drh 1619 */
9d50322… drh 1620 void whatis_sql_function(
9d50322… drh 1621 sqlite3_context *context,
9d50322… drh 1622 int argc,
9d50322… drh 1623 sqlite3_value **argv
9d50322… drh 1624 ){
9d50322… drh 1625 sqlite3_str *pOut = sqlite3_str_new(0);
9d50322… drh 1626 int n;
9d50322… drh 1627 assert( argc==1 );
9d50322… drh 1628 if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
9d50322… drh 1629 int rid = sqlite3_value_int(argv[0]);
9d50322… drh 1630 whatis_rid(rid, 0, pOut);
9d50322… drh 1631 }else if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
9d50322… drh 1632 const char *zName = (const char*)sqlite3_value_text(argv[0]);
9d50322… drh 1633 whatis_artifact(zName, 0, 0, 0, pOut);
9d50322… drh 1634 }else{
9d50322… drh 1635 sqlite3_str_free(pOut);
9d50322… drh 1636 return;
9d50322… drh 1637 }
9d50322… drh 1638 n = sqlite3_str_length(pOut);
9d50322… drh 1639 sqlite3_result_text64(context, sqlite3_str_finish(pOut), n,
9d50322… drh 1640 sqlite3_free, SQLITE_UTF8);
9d50322… drh 1641 sqlite3_result_subtype(context, 'J');
a821cbf… mgagnon 1642 }
9d50322… drh 1643
5cdc39d… drh 1644
5cdc39d… drh 1645 /*
5cdc39d… drh 1646 ** COMMAND: test-whatis-all
c3b5f1d… jan.nijtmans 1647 **
5cdc39d… drh 1648 ** Usage: %fossil test-whatis-all
5cdc39d… drh 1649 **
5cdc39d… drh 1650 ** Show "whatis" information about every artifact in the repository
5cdc39d… drh 1651 */
5cdc39d… drh 1652 void test_whatis_all_cmd(void){
5cdc39d… drh 1653 Stmt q;
5cdc39d… drh 1654 int cnt = 0;
5cdc39d… drh 1655 db_find_and_open_repository(0,0);
5cdc39d… drh 1656 db_prepare(&q, "SELECT rid FROM blob ORDER BY rid");
5cdc39d… drh 1657 while( db_step(&q)==SQLITE_ROW ){
5cdc39d… drh 1658 if( cnt++ ) fossil_print("%.79c\n", '-');
9d50322… drh 1659 whatis_rid(db_column_int(&q,0), 1, 0);
2aaae64… drh 1660 }
2aaae64… drh 1661 db_finalize(&q);
2aaae64… drh 1662 }
2aaae64… drh 1663
2aaae64… drh 1664
2aaae64… drh 1665 /*
2aaae64… drh 1666 ** COMMAND: test-ambiguous
c3b5f1d… jan.nijtmans 1667 **
2aaae64… drh 1668 ** Usage: %fossil test-ambiguous [--minsize N]
2aaae64… drh 1669 **
fd9b7bd… drh 1670 ** Show a list of ambiguous artifact hash abbreviations of N characters or
2aaae64… drh 1671 ** more where N defaults to 4. Change N to a different value using
2aaae64… drh 1672 ** the "--minsize N" command-line option.
2aaae64… drh 1673 */
2aaae64… drh 1674 void test_ambiguous_cmd(void){
2aaae64… drh 1675 Stmt q, ins;
2aaae64… drh 1676 int i;
2aaae64… drh 1677 int minSize = 4;
2aaae64… drh 1678 const char *zMinsize;
2aaae64… drh 1679 char zPrev[100];
2aaae64… drh 1680 db_find_and_open_repository(0,0);
2aaae64… drh 1681 zMinsize = find_option("minsize",0,1);
2aaae64… drh 1682 if( zMinsize && atoi(zMinsize)>0 ) minSize = atoi(zMinsize);
2aaae64… drh 1683 db_multi_exec("CREATE TEMP TABLE dups(uuid, cnt)");
2aaae64… drh 1684 db_prepare(&ins,"INSERT INTO dups(uuid) VALUES(substr(:uuid,1,:cnt))");
2aaae64… drh 1685 db_prepare(&q,
2aaae64… drh 1686 "SELECT uuid FROM blob "
2aaae64… drh 1687 "UNION "
2aaae64… drh 1688 "SELECT substr(tagname,7) FROM tag WHERE tagname GLOB 'event-*' "
2aaae64… drh 1689 "UNION "
2aaae64… drh 1690 "SELECT tkt_uuid FROM ticket "
2aaae64… drh 1691 "ORDER BY 1"
2aaae64… drh 1692 );
2aaae64… drh 1693 zPrev[0] = 0;
2aaae64… drh 1694 while( db_step(&q)==SQLITE_ROW ){
2aaae64… drh 1695 const char *zUuid = db_column_text(&q, 0);
2aaae64… drh 1696 for(i=0; zUuid[i]==zPrev[i] && zUuid[i]!=0; i++){}
2aaae64… drh 1697 if( i>=minSize ){
2aaae64… drh 1698 db_bind_int(&ins, ":cnt", i);
2aaae64… drh 1699 db_bind_text(&ins, ":uuid", zUuid);
2aaae64… drh 1700 db_step(&ins);
2aaae64… drh 1701 db_reset(&ins);
2aaae64… drh 1702 }
3f487ac… drh 1703 sqlite3_snprintf(sizeof(zPrev), zPrev, "%s", zUuid);
2aaae64… drh 1704 }
2aaae64… drh 1705 db_finalize(&ins);
2aaae64… drh 1706 db_finalize(&q);
2aaae64… drh 1707 db_prepare(&q, "SELECT uuid FROM dups ORDER BY length(uuid) DESC, uuid");
2aaae64… drh 1708 while( db_step(&q)==SQLITE_ROW ){
2aaae64… drh 1709 fossil_print("%s\n", db_column_text(&q, 0));
2aaae64… drh 1710 }
2aaae64… drh 1711 db_finalize(&q);
a81a47f… drh 1712 }
a81a47f… drh 1713
a81a47f… drh 1714 /*
a81a47f… drh 1715 ** Schema for the description table
a81a47f… drh 1716 */
d93f5ee… jan.nijtmans 1717 static const char zDescTab[] =
a81a47f… drh 1718 @ CREATE TEMP TABLE IF NOT EXISTS description(
d93f5ee… jan.nijtmans 1719 @ rid INTEGER PRIMARY KEY, -- RID of the object
fd9b7bd… drh 1720 @ uuid TEXT, -- hash of the object
d93f5ee… jan.nijtmans 1721 @ ctime DATETIME, -- Time of creation
a81a47f… drh 1722 @ isPrivate BOOLEAN DEFAULT 0, -- True for unpublished artifacts
bc36fdc… danield 1723 @ type TEXT, -- file, check-in, wiki, ticket, etc.
a0faf12… drh 1724 @ rcvid INT, -- When the artifact was received
d93f5ee… jan.nijtmans 1725 @ summary TEXT, -- Summary comment for the object
7fd31f6… drh 1726 @ ref TEXT -- hash of an object to link against
a81a47f… drh 1727 @ );
23fc63e… mistachkin 1728 @ CREATE INDEX IF NOT EXISTS desctype
23fc63e… mistachkin 1729 @ ON description(summary) WHERE summary='unknown';
a81a47f… drh 1730 ;
941280a… drh 1731
941280a… drh 1732 /*
941280a… drh 1733 ** Attempt to describe all phantom artifacts. The artifacts are
941280a… drh 1734 ** already loaded into the description table and have summary='unknown'.
941280a… drh 1735 ** This routine attempts to generate a better summary, and possibly
941280a… drh 1736 ** fill in the ref field.
941280a… drh 1737 */
941280a… drh 1738 static void describe_unknown_artifacts(){
941280a… drh 1739 /* Try to figure out the origin of unknown artifacts */
941280a… drh 1740 db_multi_exec(
941280a… drh 1741 "REPLACE INTO description(rid,uuid,isPrivate,type,summary,ref)\n"
941280a… drh 1742 " SELECT description.rid, description.uuid, isPrivate, type,\n"
941280a… drh 1743 " CASE WHEN plink.isprim THEN '' ELSE 'merge ' END ||\n"
941280a… drh 1744 " 'parent of check-in', blob.uuid\n"
941280a… drh 1745 " FROM description, plink, blob\n"
941280a… drh 1746 " WHERE description.summary='unknown'\n"
941280a… drh 1747 " AND plink.pid=description.rid\n"
941280a… drh 1748 " AND blob.rid=plink.cid;"
941280a… drh 1749 );
941280a… drh 1750 db_multi_exec(
941280a… drh 1751 "REPLACE INTO description(rid,uuid,isPrivate,type,summary,ref)\n"
941280a… drh 1752 " SELECT description.rid, description.uuid, isPrivate, type,\n"
941280a… drh 1753 " 'child of check-in', blob.uuid\n"
941280a… drh 1754 " FROM description, plink, blob\n"
941280a… drh 1755 " WHERE description.summary='unknown'\n"
941280a… drh 1756 " AND plink.cid=description.rid\n"
941280a… drh 1757 " AND blob.rid=plink.pid;"
941280a… drh 1758 );
941280a… drh 1759 db_multi_exec(
941280a… drh 1760 "REPLACE INTO description(rid,uuid,isPrivate,type,summary,ref)\n"
941280a… drh 1761 " SELECT description.rid, description.uuid, isPrivate, type,\n"
941280a… drh 1762 " 'check-in referenced by \"'||tag.tagname ||'\" tag',\n"
941280a… drh 1763 " blob.uuid\n"
941280a… drh 1764 " FROM description, tagxref, tag, blob\n"
941280a… drh 1765 " WHERE description.summary='unknown'\n"
941280a… drh 1766 " AND tagxref.origid=description.rid\n"
941280a… drh 1767 " AND tag.tagid=tagxref.tagid\n"
941280a… drh 1768 " AND blob.rid=tagxref.srcid;"
941280a… drh 1769 );
941280a… drh 1770 db_multi_exec(
941280a… drh 1771 "REPLACE INTO description(rid,uuid,isPrivate,type,summary,ref)\n"
941280a… drh 1772 " SELECT description.rid, description.uuid, isPrivate, type,\n"
941280a… drh 1773 " 'file \"'||filename.name||'\"',\n"
941280a… drh 1774 " blob.uuid\n"
941280a… drh 1775 " FROM description, mlink, filename, blob\n"
941280a… drh 1776 " WHERE description.summary='unknown'\n"
941280a… drh 1777 " AND mlink.fid=description.rid\n"
941280a… drh 1778 " AND blob.rid=mlink.mid\n"
941280a… drh 1779 " AND filename.fnid=mlink.fnid;"
941280a… drh 1780 );
941280a… drh 1781 if( !db_exists("SELECT 1 FROM description WHERE summary='unknown'") ){
941280a… drh 1782 return;
941280a… drh 1783 }
941280a… drh 1784 add_content_sql_commands(g.db);
941280a… drh 1785 db_multi_exec(
941280a… drh 1786 "REPLACE INTO description(rid,uuid,isPrivate,type,summary,ref)\n"
941280a… drh 1787 " SELECT description.rid, description.uuid, isPrivate, type,\n"
941280a… drh 1788 " 'referenced by cluster', blob.uuid\n"
941280a… drh 1789 " FROM description, tagxref, blob\n"
941280a… drh 1790 " WHERE description.summary='unknown'\n"
941280a… drh 1791 " AND tagxref.tagid=(SELECT tagid FROM tag WHERE tagname='cluster')\n"
941280a… drh 1792 " AND blob.rid=tagxref.rid\n"
cd624d4… drh 1793 " AND CAST(content(blob.uuid) AS text)"
cd624d4… drh 1794 " GLOB ('*M '||description.uuid||'*');"
941280a… drh 1795 );
941280a… drh 1796 }
a81a47f… drh 1797
a81a47f… drh 1798 /*
a81a47f… drh 1799 ** Create the description table if it does not already exists.
a81a47f… drh 1800 ** Populate fields of this table with descriptions for all artifacts
a81a47f… drh 1801 ** whose RID matches the SQL expression in zWhere.
a81a47f… drh 1802 */
a81a47f… drh 1803 void describe_artifacts(const char *zWhere){
a81a47f… drh 1804 db_multi_exec("%s", zDescTab/*safe-for-%s*/);
a81a47f… drh 1805
6ec2c2e… mistachkin 1806 /* Describe check-ins */
a81a47f… drh 1807 db_multi_exec(
a0faf12… drh 1808 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1809 "SELECT blob.rid, blob.uuid, blob.rcvid, event.mtime, 'checkin',\n"
2ae6b1c… drh 1810 " 'check-in to '\n"
2ae6b1c… drh 1811 " || coalesce((SELECT value FROM tagxref WHERE tagid=%d"
2ae6b1c… drh 1812 " AND tagtype>0 AND tagxref.rid=blob.rid),'trunk')\n"
2ae6b1c… drh 1813 " || ' by ' || coalesce(event.euser,event.user)\n"
2ae6b1c… drh 1814 " || ' on ' || strftime('%%Y-%%m-%%d %%H:%%M',event.mtime)\n"
a81a47f… drh 1815 " FROM event, blob\n"
f3663f9… drh 1816 " WHERE (event.objid %s) AND event.type='ci'\n"
a81a47f… drh 1817 " AND event.objid=blob.rid;",
2ae6b1c… drh 1818 TAG_BRANCH, zWhere /*safe-for-%s*/
a81a47f… drh 1819 );
a81a47f… drh 1820
a81a47f… drh 1821 /* Describe files */
a81a47f… drh 1822 db_multi_exec(
a0faf12… drh 1823 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1824 "SELECT blob.rid, blob.uuid, blob.rcvid, event.mtime,"
a0faf12… drh 1825 " 'file', 'file '||filename.name\n"
a81a47f… drh 1826 " FROM mlink, blob, event, filename\n"
f3663f9… drh 1827 " WHERE (mlink.fid %s)\n"
a81a47f… drh 1828 " AND mlink.mid=event.objid\n"
a81a47f… drh 1829 " AND filename.fnid=mlink.fnid\n"
a81a47f… drh 1830 " AND mlink.fid=blob.rid;",
a81a47f… drh 1831 zWhere /*safe-for-%s*/
a81a47f… drh 1832 );
a81a47f… drh 1833
a81a47f… drh 1834 /* Describe tags */
a81a47f… drh 1835 db_multi_exec(
a0faf12… drh 1836 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1837 "SELECT blob.rid, blob.uuid, blob.rcvid, tagxref.mtime, 'tag',\n"
a81a47f… drh 1838 " 'tag '||substr((SELECT uuid FROM blob WHERE rid=tagxref.rid),1,16)\n"
a81a47f… drh 1839 " FROM tagxref, blob\n"
f3663f9… drh 1840 " WHERE (tagxref.srcid %s) AND tagxref.srcid!=tagxref.rid\n"
a81a47f… drh 1841 " AND tagxref.srcid=blob.rid;",
a81a47f… drh 1842 zWhere /*safe-for-%s*/
a81a47f… drh 1843 );
a81a47f… drh 1844
a81a47f… drh 1845 /* Cluster artifacts */
a81a47f… drh 1846 db_multi_exec(
a0faf12… drh 1847 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1848 "SELECT blob.rid, blob.uuid, blob.rcvid, rcvfrom.mtime,"
a0faf12… drh 1849 " 'cluster', 'cluster'\n"
13b6886… drh 1850 " FROM tagxref, blob, rcvfrom\n"
f3663f9… drh 1851 " WHERE (tagxref.rid %s)\n"
a81a47f… drh 1852 " AND tagxref.tagid=(SELECT tagid FROM tag WHERE tagname='cluster')\n"
13b6886… drh 1853 " AND blob.rid=tagxref.rid"
13b6886… drh 1854 " AND rcvfrom.rcvid=blob.rcvid;",
a81a47f… drh 1855 zWhere /*safe-for-%s*/
a81a47f… drh 1856 );
a81a47f… drh 1857
a81a47f… drh 1858 /* Ticket change artifacts */
a81a47f… drh 1859 db_multi_exec(
a0faf12… drh 1860 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1861 "SELECT blob.rid, blob.uuid, blob.rcvid, tagxref.mtime, 'ticket',\n"
a81a47f… drh 1862 " 'ticket '||substr(tag.tagname,5,21)\n"
a81a47f… drh 1863 " FROM tagxref, tag, blob\n"
f3663f9… drh 1864 " WHERE (tagxref.rid %s)\n"
a81a47f… drh 1865 " AND tag.tagid=tagxref.tagid\n"
a81a47f… drh 1866 " AND tag.tagname GLOB 'tkt-*'"
a81a47f… drh 1867 " AND blob.rid=tagxref.rid;",
a81a47f… drh 1868 zWhere /*safe-for-%s*/
a81a47f… drh 1869 );
a81a47f… drh 1870
a81a47f… drh 1871 /* Wiki edit artifacts */
a81a47f… drh 1872 db_multi_exec(
a0faf12… drh 1873 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1874 "SELECT blob.rid, blob.uuid, blob.rcvid, tagxref.mtime, 'wiki',\n"
a81a47f… drh 1875 " printf('wiki \"%%s\"',substr(tag.tagname,6))\n"
a81a47f… drh 1876 " FROM tagxref, tag, blob\n"
f3663f9… drh 1877 " WHERE (tagxref.rid %s)\n"
a81a47f… drh 1878 " AND tag.tagid=tagxref.tagid\n"
a81a47f… drh 1879 " AND tag.tagname GLOB 'wiki-*'"
a81a47f… drh 1880 " AND blob.rid=tagxref.rid;",
a81a47f… drh 1881 zWhere /*safe-for-%s*/
a81a47f… drh 1882 );
a81a47f… drh 1883
a81a47f… drh 1884 /* Event edit artifacts */
a81a47f… drh 1885 db_multi_exec(
a0faf12… drh 1886 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1887 "SELECT blob.rid, blob.uuid, blob.rcvid, tagxref.mtime, 'event',\n"
a81a47f… drh 1888 " 'event '||substr(tag.tagname,7)\n"
a81a47f… drh 1889 " FROM tagxref, tag, blob\n"
f3663f9… drh 1890 " WHERE (tagxref.rid %s)\n"
a81a47f… drh 1891 " AND tag.tagid=tagxref.tagid\n"
a81a47f… drh 1892 " AND tag.tagname GLOB 'event-*'"
a81a47f… drh 1893 " AND blob.rid=tagxref.rid;",
a81a47f… drh 1894 zWhere /*safe-for-%s*/
a81a47f… drh 1895 );
a81a47f… drh 1896
a81a47f… drh 1897 /* Attachments */
a81a47f… drh 1898 db_multi_exec(
a0faf12… drh 1899 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1900 "SELECT blob.rid, blob.uuid, blob.rcvid, attachment.mtime,"
a0faf12… drh 1901 " 'attach-control',\n"
f3663f9… drh 1902 " 'attachment-control for '||attachment.filename\n"
f3663f9… drh 1903 " FROM attachment, blob\n"
f3663f9… drh 1904 " WHERE (attachment.attachid %s)\n"
f3663f9… drh 1905 " AND blob.rid=attachment.attachid",
f3663f9… drh 1906 zWhere /*safe-for-%s*/
f3663f9… drh 1907 );
f3663f9… drh 1908 db_multi_exec(
a0faf12… drh 1909 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1910 "SELECT blob.rid, blob.uuid, blob.rcvid, attachment.mtime, 'attachment',\n"
a81a47f… drh 1911 " 'attachment '||attachment.filename\n"
a81a47f… drh 1912 " FROM attachment, blob\n"
f3663f9… drh 1913 " WHERE (blob.rid %s)\n"
f3663f9… drh 1914 " AND blob.rid NOT IN (SELECT rid FROM description)\n"
f3663f9… drh 1915 " AND blob.uuid=attachment.src",
a81a47f… drh 1916 zWhere /*safe-for-%s*/
a81a47f… drh 1917 );
a81a47f… drh 1918
99fcc43… drh 1919 /* Forum posts */
99fcc43… drh 1920 if( db_table_exists("repository","forumpost") ){
99fcc43… drh 1921 db_multi_exec(
a0faf12… drh 1922 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,ctime,type,summary)\n"
a0faf12… drh 1923 "SELECT postblob.rid, postblob.uuid, postblob.rcvid,"
a0faf12… drh 1924 " forumpost.fmtime, 'forumpost',\n"
99fcc43… drh 1925 " CASE WHEN fpid=froot THEN 'forum-post '\n"
99fcc43… drh 1926 " ELSE 'forum-reply-to ' END || substr(rootblob.uuid,1,14)\n"
99fcc43… drh 1927 " FROM forumpost, blob AS postblob, blob AS rootblob\n"
99fcc43… drh 1928 " WHERE (forumpost.fpid %s)\n"
99fcc43… drh 1929 " AND postblob.rid=forumpost.fpid"
99fcc43… drh 1930 " AND rootblob.rid=forumpost.froot",
99fcc43… drh 1931 zWhere /*safe-for-%s*/
99fcc43… drh 1932 );
99fcc43… drh 1933 }
99fcc43… drh 1934
7fd31f6… drh 1935 /* Mark all other artifacts as "unknown" for now */
a81a47f… drh 1936 db_multi_exec(
a0faf12… drh 1937 "INSERT OR IGNORE INTO description(rid,uuid,rcvid,type,summary)\n"
7fd31f6… drh 1938 "SELECT blob.rid, blob.uuid,blob.rcvid,\n"
7fd31f6… drh 1939 " CASE WHEN EXISTS(SELECT 1 FROM phantom WHERE rid=blob.rid)\n"
7fd31f6… drh 1940 " THEN 'phantom' ELSE '' END,\n"
a81a47f… drh 1941 " 'unknown'\n"
7fd31f6… drh 1942 " FROM blob\n"
7fd31f6… drh 1943 " WHERE (blob.rid %s)\n"
7fd31f6… drh 1944 " AND (blob.rid NOT IN (SELECT rid FROM description));",
a81a47f… drh 1945 zWhere /*safe-for-%s*/
a81a47f… drh 1946 );
a81a47f… drh 1947
a81a47f… drh 1948 /* Mark private elements */
a81a47f… drh 1949 db_multi_exec(
a81a47f… drh 1950 "UPDATE description SET isPrivate=1 WHERE rid IN private"
a81a47f… drh 1951 );
7fd31f6… drh 1952
941280a… drh 1953 if( db_exists("SELECT 1 FROM description WHERE summary='unknown'") ){
941280a… drh 1954 describe_unknown_artifacts();
941280a… drh 1955 }
a81a47f… drh 1956 }
a81a47f… drh 1957
a81a47f… drh 1958 /*
b78a32f… drh 1959 ** Print the content of the description table on stdout.
b78a32f… drh 1960 **
b78a32f… drh 1961 ** The description table is computed using the WHERE clause zWhere if
b78a32f… drh 1962 ** the zWhere parameter is not NULL. If zWhere is NULL, then this
b78a32f… drh 1963 ** routine assumes that the description table already exists and is
b78a32f… drh 1964 ** populated and merely prints the contents.
a81a47f… drh 1965 */
a81a47f… drh 1966 int describe_artifacts_to_stdout(const char *zWhere, const char *zLabel){
a81a47f… drh 1967 Stmt q;
a81a47f… drh 1968 int cnt = 0;
b78a32f… drh 1969 if( zWhere!=0 ) describe_artifacts(zWhere);
a81a47f… drh 1970 db_prepare(&q,
cd624d4… drh 1971 "SELECT uuid, summary, coalesce(ref,''), isPrivate\n"
a81a47f… drh 1972 " FROM description\n"
a81a47f… drh 1973 " ORDER BY ctime, type;"
a81a47f… drh 1974 );
a81a47f… drh 1975 while( db_step(&q)==SQLITE_ROW ){
a81a47f… drh 1976 if( zLabel ){
a81a47f… drh 1977 fossil_print("%s\n", zLabel);
a81a47f… drh 1978 zLabel = 0;
a81a47f… drh 1979 }
cd624d4… drh 1980 fossil_print(" %.16s %s %s", db_column_text(&q,0),
cd624d4… drh 1981 db_column_text(&q,1), db_column_text(&q,2));
cd624d4… drh 1982 if( db_column_int(&q,3) ) fossil_print(" (private)");
a81a47f… drh 1983 fossil_print("\n");
a81a47f… drh 1984 cnt++;
a81a47f… drh 1985 }
a81a47f… drh 1986 db_finalize(&q);
b78a32f… drh 1987 if( zWhere!=0 ) db_multi_exec("DELETE FROM description;");
a81a47f… drh 1988 return cnt;
a81a47f… drh 1989 }
a81a47f… drh 1990
a81a47f… drh 1991 /*
a81a47f… drh 1992 ** COMMAND: test-describe-artifacts
a81a47f… drh 1993 **
f3663f9… drh 1994 ** Usage: %fossil test-describe-artifacts [--from S] [--count N]
a81a47f… drh 1995 **
a81a47f… drh 1996 ** Display a one-line description of every artifact.
a81a47f… drh 1997 */
a81a47f… drh 1998 void test_describe_artifacts_cmd(void){
f3663f9… drh 1999 int iFrom = 0;
f3663f9… drh 2000 int iCnt = 1000000;
f3663f9… drh 2001 const char *z;
f3663f9… drh 2002 char *zRange;
a81a47f… drh 2003 db_find_and_open_repository(0,0);
f3663f9… drh 2004 z = find_option("from",0,1);
f3663f9… drh 2005 if( z ) iFrom = atoi(z);
f3663f9… drh 2006 z = find_option("count",0,1);
f3663f9… drh 2007 if( z ) iCnt = atoi(z);
f3663f9… drh 2008 zRange = mprintf("BETWEEN %d AND %d", iFrom, iFrom+iCnt-1);
f3663f9… drh 2009 describe_artifacts_to_stdout(zRange, 0);
59121b9… drh 2010 }
59121b9… drh 2011
59121b9… drh 2012 /*
f3663f9… drh 2013 ** WEBPAGE: bloblist
f3663f9… drh 2014 **
3e3bb5f… drh 2015 ** Return a page showing all artifacts in the repository. Query parameters:
3e3bb5f… drh 2016 **
3e3bb5f… drh 2017 ** n=N Show N artifacts
3e3bb5f… drh 2018 ** s=S Start with artifact number S
7b6b567… drh 2019 ** priv Show only unpublished or private artifacts
ea83cda… drh 2020 ** phan Show only phantom artifacts
59a8438… drh 2021 ** hclr Color code hash types (SHA1 vs SHA3)
64ab7d7… drh 2022 ** recent Show the most recent N artifacts
59121b9… drh 2023 */
3e3bb5f… drh 2024 void bloblist_page(void){
59121b9… drh 2025 Stmt q;
59121b9… drh 2026 int s = atoi(PD("s","0"));
59121b9… drh 2027 int n = atoi(PD("n","5000"));
59121b9… drh 2028 int mx = db_int(0, "SELECT max(rid) FROM blob");
7b6b567… drh 2029 int privOnly = PB("priv");
ea83cda… drh 2030 int phantomOnly = PB("phan");
59a8438… drh 2031 int hashClr = PB("hclr");
64ab7d7… drh 2032 int bRecent = PB("recent");
9859eb0… drh 2033 int bUnclst = PB("unclustered");
59121b9… drh 2034 char *zRange;
59a8438… drh 2035 char *zSha1Bg;
59a8438… drh 2036 char *zSha3Bg;
59121b9… drh 2037
59121b9… drh 2038 login_check_credentials();
653dd40… drh 2039 if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
57f1e87… drh 2040 cgi_check_for_malice();
59121b9… drh 2041 style_header("List Of Artifacts");
187424e… andygoth 2042 style_submenu_element("250 Largest", "bigbloblist");
64ab7d7… drh 2043 if( bRecent==0 || n!=250 ){
64ab7d7… drh 2044 style_submenu_element("Recent","bloblist?n=250&recent");
64ab7d7… drh 2045 }
9859eb0… drh 2046 if( bUnclst==0 ){
9859eb0… drh 2047 style_submenu_element("Unclustered","bloblist?unclustered");
9859eb0… drh 2048 }
0594266… drh 2049 if( g.perm.Admin ){
c675484… drh 2050 style_submenu_element("Xfer Log", "rcvfromlist");
b3a346e… drh 2051 }
b3a346e… drh 2052 if( !phantomOnly ){
b3a346e… drh 2053 style_submenu_element("Phantoms", "bloblist?phan");
b3a346e… drh 2054 }
3057775… drh 2055 style_submenu_element("Clusters","clusterlist");
648bc30… drh 2056 if( g.perm.Private || g.perm.Admin ){
7b6b567… drh 2057 if( !privOnly ){
7b6b567… drh 2058 style_submenu_element("Private", "bloblist?priv");
7b6b567… drh 2059 }
7b6b567… drh 2060 }else{
7b6b567… drh 2061 privOnly = 0;
7b6b567… drh 2062 }
7b6b567… drh 2063 if( g.perm.Write ){
7b6b567… drh 2064 style_submenu_element("Artifact Stats", "artifact_stats");
7b6b567… drh 2065 }
9859eb0… drh 2066 if( !privOnly && !phantomOnly && mx>n && P("s")==0 && !bRecent && !bUnclst ){
59121b9… drh 2067 int i;
59121b9… drh 2068 @ <p>Select a range of artifacts to view:</p>
59121b9… drh 2069 @ <ul>
59121b9… drh 2070 for(i=1; i<=mx; i+=n){
f3663f9… drh 2071 @ <li> %z(href("%R/bloblist?s=%d&n=%d",i,n))
59121b9… drh 2072 @ %d(i)..%d(i+n-1<mx?i+n-1:mx)</a>
59121b9… drh 2073 }
64ab7d7… drh 2074 @ <li> %z(href("%R/bloblist?n=250&recent"))250 most recent</a>
9859eb0… drh 2075 @ <li> %z(href("%R/bloblist?unclustered"))All unclustered</a>
59121b9… drh 2076 @ </ul>
112c713… drh 2077 style_finish_page();
59121b9… drh 2078 return;
59121b9… drh 2079 }
7b6b567… drh 2080 if( phantomOnly || privOnly || mx>n ){
187424e… andygoth 2081 style_submenu_element("Index", "bloblist");
483317f… drh 2082 }
7b6b567… drh 2083 if( privOnly ){
9859eb0… drh 2084 @ <h2>Private Artifacts</h2>
c8b1e81… drh 2085 zRange = mprintf("IN private");
ea83cda… drh 2086 }else if( phantomOnly ){
9859eb0… drh 2087 @ <h2>Phantom Artifacts</h2>
28b92ca… drh 2088 zRange = mprintf("IN phantom");
9859eb0… drh 2089 }else if( bUnclst ){
9859eb0… drh 2090 @ <h2>Unclustered Artifacts</h2>
9859eb0… drh 2091 zRange = mprintf("IN unclustered");
64ab7d7… drh 2092 }else if( bRecent ){
9859eb0… drh 2093 @ <h2>%d(n) Most Recent Artifacts</h2>
64ab7d7… drh 2094 zRange = mprintf(">=(SELECT rid FROM blob"
64ab7d7… drh 2095 " ORDER BY rid DESC LIMIT 1 OFFSET %d)",n);
c8b1e81… drh 2096 }else{
c8b1e81… drh 2097 zRange = mprintf("BETWEEN %d AND %d", s, s+n-1);
c8b1e81… drh 2098 }
483317f… drh 2099 describe_artifacts(zRange);
c8b1e81… drh 2100 fossil_free(zRange);
483317f… drh 2101 db_prepare(&q,
9953b16… drh 2102 /* 0 1 2 3 4 5 6 */
9953b16… drh 2103 "SELECT rid, uuid, summary, isPrivate, type='phantom', ref, rcvid, "
9953b16… drh 2104 " datetime(rcvfrom.mtime)"
9953b16… drh 2105 " FROM description LEFT JOIN rcvfrom USING(rcvid)"
9953b16… drh 2106 " ORDER BY rid %s",
9859eb0… drh 2107 ((bRecent||bUnclst)?"DESC":"ASC")/*safe-for-%s*/
483317f… drh 2108 );
59a8438… drh 2109 if( skin_detail_boolean("white-foreground") ){
59a8438… drh 2110 zSha1Bg = "#714417";
59a8438… drh 2111 zSha3Bg = "#177117";
59a8438… drh 2112 }else{
59a8438… drh 2113 zSha1Bg = "#ebffb0";
59a8438… drh 2114 zSha3Bg = "#b0ffb0";
59a8438… drh 2115 }
a0faf12… drh 2116 @ <table cellpadding="2" cellspacing="0" border="1">
9953b16… drh 2117 @ <tr><th>RID<th>Hash<th>Received<th>Description<th>Ref<th>Remarks
483317f… drh 2118 while( db_step(&q)==SQLITE_ROW ){
483317f… drh 2119 int rid = db_column_int(&q,0);
483317f… drh 2120 const char *zUuid = db_column_text(&q, 1);
483317f… drh 2121 const char *zDesc = db_column_text(&q, 2);
483317f… drh 2122 int isPriv = db_column_int(&q,3);
ea83cda… drh 2123 int isPhantom = db_column_int(&q,4);
9953b16… drh 2124 const char *zRef = db_column_text(&q,5);
9953b16… drh 2125 const char *zDate = db_column_text(&q,7);
b3a346e… drh 2126 if( isPriv && !isPhantom && !g.perm.Private && !g.perm.Admin ){
7b6b567… drh 2127 /* Don't show private artifacts to users without Private (x) permission */
7b6b567… drh 2128 continue;
7b6b567… drh 2129 }
59a8438… drh 2130 if( hashClr ){
59a8438… drh 2131 const char *zClr = db_column_bytes(&q,1)>40 ? zSha3Bg : zSha1Bg;
59a8438… drh 2132 @ <tr style='background-color:%s(zClr);'><td align="right">%d(rid)</td>
59a8438… drh 2133 }else{
59a8438… drh 2134 @ <tr><td align="right">%d(rid)</td>
59a8438… drh 2135 }
f982642… drh 2136 @ <td>&nbsp;%z(href("%R/info/%!S",zUuid))%S(zUuid)</a>&nbsp;</td>
a0faf12… drh 2137 if( g.perm.Admin ){
9953b16… drh 2138 int rcvid = db_column_int(&q, 6);
9953b16… drh 2139 @ <td><a href='%R/rcvfrom?rcvid=%d(rcvid)'>%h(zDate)</a>
9953b16… drh 2140 }else{
9953b16… drh 2141 @ <td>%h(zDate)
a0faf12… drh 2142 }
483317f… drh 2143 @ <td align="left">%h(zDesc)</td>
7fd31f6… drh 2144 if( zRef && zRef[0] ){
7fd31f6… drh 2145 @ <td>%z(href("%R/info/%!S",zRef))%S(zRef)</a>
7fd31f6… drh 2146 }else{
7fd31f6… drh 2147 @ <td>&nbsp;
7fd31f6… drh 2148 }
ea83cda… drh 2149 if( isPriv || isPhantom ){
ea83cda… drh 2150 if( isPriv==0 ){
a0faf12… drh 2151 @ <td>phantom</td>
ea83cda… drh 2152 }else if( isPhantom==0 ){
a0faf12… drh 2153 @ <td>private</td>
a0faf12… drh 2154 }else{
a0faf12… drh 2155 @ <td>private,phantom</td>
a0faf12… drh 2156 }
a0faf12… drh 2157 }else{
a0faf12… drh 2158 @ <td>&nbsp;
a0faf12… drh 2159 }
a0faf12… drh 2160 @ </tr>
a0faf12… drh 2161 }
a0faf12… drh 2162 @ </table>
a0faf12… drh 2163 db_finalize(&q);
112c713… drh 2164 style_finish_page();
941280a… drh 2165 }
941280a… drh 2166
941280a… drh 2167 /*
941280a… drh 2168 ** Output HTML that shows a table of all public phantoms.
941280a… drh 2169 */
941280a… drh 2170 void table_of_public_phantoms(void){
941280a… drh 2171 Stmt q;
941280a… drh 2172 char *zRange;
97cac02… drh 2173 double rNow;
941280a… drh 2174 zRange = mprintf("IN (SELECT rid FROM phantom EXCEPT"
941280a… drh 2175 " SELECT rid FROM private)");
941280a… drh 2176 describe_artifacts(zRange);
941280a… drh 2177 fossil_free(zRange);
941280a… drh 2178 db_prepare(&q,
97cac02… drh 2179 "SELECT rid, uuid, summary, ref,"
97cac02… drh 2180 " (SELECT mtime FROM blob, rcvfrom"
97cac02… drh 2181 " WHERE blob.uuid=ref AND rcvfrom.rcvid=blob.rcvid)"
941280a… drh 2182 " FROM description ORDER BY rid"
941280a… drh 2183 );
97cac02… drh 2184 rNow = db_double(0.0, "SELECT julianday('now')");
941280a… drh 2185 @ <table cellpadding="2" cellspacing="0" border="1">
97cac02… drh 2186 @ <tr><th>RID<th>Description<th>Source<th>Age
941280a… drh 2187 while( db_step(&q)==SQLITE_ROW ){
941280a… drh 2188 int rid = db_column_int(&q,0);
941280a… drh 2189 const char *zUuid = db_column_text(&q, 1);
941280a… drh 2190 const char *zDesc = db_column_text(&q, 2);
941280a… drh 2191 const char *zRef = db_column_text(&q,3);
97cac02… drh 2192 double mtime = db_column_double(&q,4);
941280a… drh 2193 @ <tr><td valign="top">%d(rid)</td>
941280a… drh 2194 @ <td valign="top" align="left">%h(zUuid)<br>%h(zDesc)</td>
941280a… drh 2195 if( zRef && zRef[0] ){
941280a… drh 2196 @ <td valign="top">%z(href("%R/info/%!S",zRef))%!S(zRef)</a>
97cac02… drh 2197 if( mtime>0 ){
97cac02… drh 2198 char *zAge = human_readable_age(rNow - mtime);
97cac02… drh 2199 @ <td valign="top">%h(zAge)
97cac02… drh 2200 fossil_free(zAge);
97cac02… drh 2201 }else{
97cac02… drh 2202 @ <td>&nbsp;
97cac02… drh 2203 }
941280a… drh 2204 }else{
97cac02… drh 2205 @ <td>&nbsp;<td>&nbsp;
941280a… drh 2206 }
941280a… drh 2207 @ </tr>
941280a… drh 2208 }
941280a… drh 2209 @ </table>
941280a… drh 2210 db_finalize(&q);
83db244… drh 2211 }
83db244… drh 2212
83db244… drh 2213 /*
83db244… drh 2214 ** WEBPAGE: phantoms
83db244… drh 2215 **
83db244… drh 2216 ** Show a list of all "phantom" artifacts that are not marked as "private".
83db244… drh 2217 **
83db244… drh 2218 ** A "phantom" artifact is an artifact whose hash named appears in some
83db244… drh 2219 ** artifact but whose content is unknown. For example, if a manifest
83db244… drh 2220 ** references a particular SHA3 hash of a file, but that SHA3 hash is
83db244… drh 2221 ** not on the shunning list and is not in the database, then the file
83db244… drh 2222 ** is a phantom. We know it exists, but we do not know its content.
83db244… drh 2223 **
83db244… drh 2224 ** Whenever a sync occurs, both each party looks at its phantom list
83db244… drh 2225 ** and for every phantom that is not also marked private, it asks the
83db244… drh 2226 ** other party to send it the content. This mechanism helps keep all
83db244… drh 2227 ** repositories synced up.
83db244… drh 2228 **
83db244… drh 2229 ** This page is similar to the /bloblist page in that it lists artifacts.
83db244… drh 2230 ** But this page is a special case in that it only shows phantoms that
83db244… drh 2231 ** are not private. In other words, this page shows all phantoms that
83db244… drh 2232 ** generate extra network traffic on every sync request.
83db244… drh 2233 */
83db244… drh 2234 void phantom_list_page(void){
83db244… drh 2235 login_check_credentials();
83db244… drh 2236 if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
83db244… drh 2237 style_header("Public Phantom Artifacts");
83db244… drh 2238 if( g.perm.Admin ){
c675484… drh 2239 style_submenu_element("Xfer Log", "rcvfromlist");
83db244… drh 2240 style_submenu_element("Artifact List", "bloblist");
83db244… drh 2241 }
83db244… drh 2242 if( g.perm.Write ){
83db244… drh 2243 style_submenu_element("Artifact Stats", "artifact_stats");
83db244… drh 2244 }
941280a… drh 2245 table_of_public_phantoms();
112c713… drh 2246 style_finish_page();
483317f… drh 2247 }
483317f… drh 2248
483317f… drh 2249 /*
483317f… drh 2250 ** WEBPAGE: bigbloblist
483317f… drh 2251 **
483317f… drh 2252 ** Return a page showing the largest artifacts in the repository in order
483317f… drh 2253 ** of decreasing size.
483317f… drh 2254 **
00c62a1… drh 2255 ** n=N Show the top N artifacts (default: 250)
483317f… drh 2256 */
483317f… drh 2257 void bigbloblist_page(void){
483317f… drh 2258 Stmt q;
483317f… drh 2259 int n = atoi(PD("n","250"));
483317f… drh 2260
483317f… drh 2261 login_check_credentials();
483317f… drh 2262 if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
0594266… drh 2263 if( g.perm.Admin ){
c675484… drh 2264 style_submenu_element("Xfer Log", "rcvfromlist");
cefadbd… drh 2265 }
cefadbd… drh 2266 if( g.perm.Write ){
4439f15… drh 2267 style_submenu_element("Artifact Stats", "artifact_stats");
0594266… drh 2268 }
0594266… drh 2269 style_submenu_element("All Artifacts", "bloblist");
483317f… drh 2270 style_header("%d Largest Artifacts", n);
483317f… drh 2271 db_multi_exec(
483317f… drh 2272 "CREATE TEMP TABLE toshow(rid INTEGER PRIMARY KEY);"
483317f… drh 2273 "INSERT INTO toshow(rid)"
483317f… drh 2274 " SELECT rid FROM blob"
483317f… drh 2275 " ORDER BY length(content) DESC"
483317f… drh 2276 " LIMIT %d;", n
483317f… drh 2277 );
483317f… drh 2278 describe_artifacts("IN toshow");
483317f… drh 2279 db_prepare(&q,
483317f… drh 2280 "SELECT description.rid, description.uuid, description.summary,"
f982642… drh 2281 " length(blob.content), coalesce(delta.srcid,''),"
f982642… drh 2282 " datetime(description.ctime)"
483317f… drh 2283 " FROM description, blob LEFT JOIN delta ON delta.rid=blob.rid"
483317f… drh 2284 " WHERE description.rid=blob.rid"
483317f… drh 2285 " ORDER BY length(content) DESC"
483317f… drh 2286 );
6b645d6… drh 2287 @ <table cellpadding="2" cellspacing="0" border="1" \
6b645d6… drh 2288 @ class='sortable' data-column-types='NnnttT' data-init-sort='0'>
f982642… drh 2289 @ <thead><tr><th align="right">Size<th align="right">RID
00c62a1… drh 2290 @ <th align="right">From<th>Hash<th>Description<th>Date</tr></thead>
f982642… drh 2291 @ <tbody>
f982642… drh 2292 while( db_step(&q)==SQLITE_ROW ){
f982642… drh 2293 int rid = db_column_int(&q,0);
f982642… drh 2294 const char *zUuid = db_column_text(&q, 1);
f982642… drh 2295 const char *zDesc = db_column_text(&q, 2);
f982642… drh 2296 int sz = db_column_int(&q,3);
f982642… drh 2297 const char *zSrcId = db_column_text(&q,4);
f982642… drh 2298 const char *zDate = db_column_text(&q,5);
f982642… drh 2299 @ <tr><td align="right">%d(sz)</td>
f982642… drh 2300 @ <td align="right">%d(rid)</td>
f982642… drh 2301 @ <td align="right">%s(zSrcId)</td>
f982642… drh 2302 @ <td>&nbsp;%z(href("%R/info/%!S",zUuid))%S(zUuid)</a>&nbsp;</td>
f982642… drh 2303 @ <td align="left">%h(zDesc)</td>
f982642… drh 2304 @ <td align="left">%z(href("%R/timeline?c=%T",zDate))%s(zDate)</a></td>
f982642… drh 2305 @ </tr>
f982642… drh 2306 }
f982642… drh 2307 @ </tbody></table>
f982642… drh 2308 db_finalize(&q);
6b645d6… drh 2309 style_table_sorter();
00c62a1… drh 2310 style_finish_page();
00c62a1… drh 2311 }
00c62a1… drh 2312
00c62a1… drh 2313 /*
00c62a1… drh 2314 ** WEBPAGE: deltachain
00c62a1… drh 2315 **
00c62a1… drh 2316 ** Usage: /deltachain/RID
00c62a1… drh 2317 **
00c62a1… drh 2318 ** The RID query parameter is required. Generate a page with a table
00c62a1… drh 2319 ** showing storage characteristics of RID and other artifacts that are
00c62a1… drh 2320 ** derived from RID via delta.
00c62a1… drh 2321 */
00c62a1… drh 2322 void deltachain_page(void){
00c62a1… drh 2323 Stmt q;
00c62a1… drh 2324 int id = atoi(PD("name","0"));
00c62a1… drh 2325 int top;
00c62a1… drh 2326 i64 nStored = 0;
00c62a1… drh 2327 i64 nExpanded = 0;
00c62a1… drh 2328
00c62a1… drh 2329 login_check_credentials();
00c62a1… drh 2330 if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
00c62a1… drh 2331 top = db_int(id,
00c62a1… drh 2332 "WITH RECURSIVE chain(aa,bb) AS (\n"
00c62a1… drh 2333 " SELECT rid, srcid FROM delta WHERE rid=%d\n"
00c62a1… drh 2334 " UNION ALL\n"
00c62a1… drh 2335 " SELECT bb, delta.srcid"
00c62a1… drh 2336 " FROM chain LEFT JOIN delta ON delta.rid=bb"
00c62a1… drh 2337 " WHERE bb IS NOT NULL\n"
00c62a1… drh 2338 ")\n"
00c62a1… drh 2339 "SELECT aa FROM chain WHERE bb IS NULL",
00c62a1… drh 2340 id
00c62a1… drh 2341 );
00c62a1… drh 2342 style_header("Delta Chain Containing Artifact %d", id);
00c62a1… drh 2343 db_multi_exec(
00c62a1… drh 2344 "CREATE TEMP TABLE toshow(rid INT, gen INT);\n"
00c62a1… drh 2345 "WITH RECURSIVE tx(id,px) AS (\n"
00c62a1… drh 2346 " VALUES(%d,0)\n"
00c62a1… drh 2347 " UNION ALL\n"
00c62a1… drh 2348 " SELECT delta.rid, px+1 FROM tx, delta where delta.srcid=tx.id\n"
00c62a1… drh 2349 " ORDER BY 2\n"
00c62a1… drh 2350 ") "
00c62a1… drh 2351 "INSERT INTO toshow(rid,gen) SELECT id,px FROM tx;",
00c62a1… drh 2352 top
00c62a1… drh 2353 );
00c62a1… drh 2354 db_multi_exec("CREATE INDEX toshow_rid ON toshow(rid);");
00c62a1… drh 2355 describe_artifacts("IN (SELECT rid FROM toshow)");
00c62a1… drh 2356 db_prepare(&q,
00c62a1… drh 2357 "SELECT description.rid, description.uuid, description.summary,"
00c62a1… drh 2358 " length(blob.content), coalesce(delta.srcid,''),"
00c62a1… drh 2359 " datetime(description.ctime), toshow.gen, blob.size"
00c62a1… drh 2360 " FROM description, toshow, blob LEFT JOIN delta ON delta.rid=blob.rid"
00c62a1… drh 2361 " WHERE description.rid=blob.rid"
00c62a1… drh 2362 " AND toshow.rid=description.rid"
00c62a1… drh 2363 " ORDER BY toshow.gen, description.ctime"
00c62a1… drh 2364 );
00c62a1… drh 2365 @ <table cellpadding="2" cellspacing="0" border="1" \
00c62a1… drh 2366 @ class='sortable' data-column-types='nNnnttT' data-init-sort='0'>
00c62a1… drh 2367 @ <thead><tr><th align="right">Level</th>
00c62a1… drh 2368 @ <th align="right">Size<th align="right">RID
00c62a1… drh 2369 @ <th align="right">From<th>Hash<th>Description<th>Date</tr></thead>
00c62a1… drh 2370 @ <tbody>
00c62a1… drh 2371 while( db_step(&q)==SQLITE_ROW ){
00c62a1… drh 2372 int rid = db_column_int(&q,0);
00c62a1… drh 2373 const char *zUuid = db_column_text(&q, 1);
00c62a1… drh 2374 const char *zDesc = db_column_text(&q, 2);
00c62a1… drh 2375 int sz = db_column_int(&q,3);
00c62a1… drh 2376 const char *zSrcId = db_column_text(&q,4);
00c62a1… drh 2377 const char *zDate = db_column_text(&q,5);
00c62a1… drh 2378 int gen = db_column_int(&q,6);
00c62a1… drh 2379 nExpanded += db_column_int(&q,7);
00c62a1… drh 2380 nStored += sz;
00c62a1… drh 2381 @ <tr><td align="right">%d(gen)</td>
00c62a1… drh 2382 @ <td align="right">%d(sz)</td>
00c62a1… drh 2383 if( rid==id ){
00c62a1… drh 2384 @ <td align="right"><b>%d(rid)</b></td>
00c62a1… drh 2385 }else{
00c62a1… drh 2386 @ <td align="right">%d(rid)</td>
00c62a1… drh 2387 }
00c62a1… drh 2388 @ <td align="right">%s(zSrcId)</td>
00c62a1… drh 2389 @ <td>&nbsp;%z(href("%R/info/%!S",zUuid))%S(zUuid)</a>&nbsp;</td>
00c62a1… drh 2390 @ <td align="left">%h(zDesc)</td>
00c62a1… drh 2391 @ <td align="left">%z(href("%R/timeline?c=%T",zDate))%s(zDate)</a></td>
00c62a1… drh 2392 @ </tr>
00c62a1… drh 2393 }
00c62a1… drh 2394 @ </tbody></table>
00c62a1… drh 2395 db_finalize(&q);
00c62a1… drh 2396 style_table_sorter();
00c62a1… drh 2397 @ <p>
00c62a1… drh 2398 @ <table border="0" cellspacing="0" cellpadding="0">
00c62a1… drh 2399 @ <tr><td>Bytes of content</td><td>&nbsp;&nbsp;&nbsp;</td>
00c62a1… drh 2400 @ <td align="right">%,lld(nExpanded)</td></tr>
00c62a1… drh 2401 @ <tr><td>Bytes stored in repository</td><td></td>
00c62a1… drh 2402 @ <td align="right">%,lld(nStored)</td>
00c62a1… drh 2403 @ </table>
00c62a1… drh 2404 @ </p>
112c713… drh 2405 style_finish_page();
a81a47f… drh 2406 }
a81a47f… drh 2407
a81a47f… drh 2408 /*
a81a47f… drh 2409 ** COMMAND: test-unsent
a81a47f… drh 2410 **
a81a47f… drh 2411 ** Usage: %fossil test-unsent
a81a47f… drh 2412 **
a81a47f… drh 2413 ** Show all artifacts in the unsent table
a81a47f… drh 2414 */
a81a47f… drh 2415 void test_unsent_cmd(void){
a81a47f… drh 2416 db_find_and_open_repository(0,0);
a81a47f… drh 2417 describe_artifacts_to_stdout("IN unsent", 0);
a81a47f… drh 2418 }
a81a47f… drh 2419
a81a47f… drh 2420 /*
a81a47f… drh 2421 ** COMMAND: test-unclustered
a81a47f… drh 2422 **
a81a47f… drh 2423 ** Usage: %fossil test-unclustered
a81a47f… drh 2424 **
a81a47f… drh 2425 ** Show all artifacts in the unclustered table
a81a47f… drh 2426 */
a81a47f… drh 2427 void test_unclusterd_cmd(void){
a81a47f… drh 2428 db_find_and_open_repository(0,0);
a81a47f… drh 2429 describe_artifacts_to_stdout("IN unclustered", 0);
a81a47f… drh 2430 }
a81a47f… drh 2431
a81a47f… drh 2432 /*
a81a47f… drh 2433 ** COMMAND: test-phantoms
a81a47f… drh 2434 **
a81a47f… drh 2435 ** Usage: %fossil test-phantoms
a81a47f… drh 2436 **
aac885a… drh 2437 ** Show all phantom artifacts. A phantom artifact is one for which there
aac885a… drh 2438 ** is no content. Options:
aac885a… drh 2439 **
aac885a… drh 2440 ** --count Show only a count of the number of phantoms.
aac885a… drh 2441 ** --delta Show all delta-phantoms. A delta-phantom is a
aac885a… drh 2442 ** artifact for which there is a delta but the delta
aac885a… drh 2443 ** source is a phantom.
aac885a… drh 2444 ** --list Just list the phantoms. Do not try to describe them.
a81a47f… drh 2445 */
d425d23… danield 2446 void test_phantoms_cmd(void){
aac885a… drh 2447 int bDelta;
aac885a… drh 2448 int bList;
aac885a… drh 2449 int bCount;
aac885a… drh 2450 unsigned nPhantom = 0;
aac885a… drh 2451 unsigned nDeltaPhantom = 0;
a81a47f… drh 2452 db_find_and_open_repository(0,0);
aac885a… drh 2453 bDelta = find_option("delta", 0, 0)!=0;
aac885a… drh 2454 bList = find_option("list", 0, 0)!=0;
aac885a… drh 2455 bCount = find_option("count", 0, 0)!=0;
aac885a… drh 2456 verify_all_options();
aac885a… drh 2457 if( bList || bCount ){
aac885a… drh 2458 Stmt q1, q2;
aac885a… drh 2459 db_prepare(&q1, "SELECT rid, uuid FROM blob WHERE size<0");
aac885a… drh 2460 while( db_step(&q1)==SQLITE_ROW ){
aac885a… drh 2461 int rid = db_column_int(&q1, 0);
aac885a… drh 2462 nPhantom++;
aac885a… drh 2463 if( !bCount ){
aac885a… drh 2464 fossil_print("%S (%d)\n", db_column_text(&q1,1), rid);
aac885a… drh 2465 }
aac885a… drh 2466 db_prepare(&q2,
aac885a… drh 2467 "WITH RECURSIVE deltasof(rid) AS ("
aac885a… drh 2468 " SELECT rid FROM delta WHERE srcid=%d"
aac885a… drh 2469 " UNION"
aac885a… drh 2470 " SELECT delta.rid FROM deltasof, delta"
aac885a… drh 2471 " WHERE delta.srcid=deltasof.rid)"
aac885a… drh 2472 "SELECT deltasof.rid, blob.uuid FROM deltasof LEFT JOIN blob"
aac885a… drh 2473 " ON blob.rid=deltasof.rid", rid
aac885a… drh 2474 );
aac885a… drh 2475 while( db_step(&q2)==SQLITE_ROW ){
aac885a… drh 2476 nDeltaPhantom++;
aac885a… drh 2477 if( !bCount ){
aac885a… drh 2478 fossil_print(" %S (%d)\n", db_column_text(&q2,1),
aac885a… drh 2479 db_column_int(&q2,0));
aac885a… drh 2480 }
aac885a… drh 2481 }
aac885a… drh 2482 db_finalize(&q2);
aac885a… drh 2483 }
aac885a… drh 2484 db_finalize(&q1);
aac885a… drh 2485 if( nPhantom ){
aac885a… drh 2486 fossil_print("Phantoms: %u Delta-phantoms: %u\n",
aac885a… drh 2487 nPhantom, nDeltaPhantom);
aac885a… drh 2488 }
aac885a… drh 2489 }else if( bDelta ){
aac885a… drh 2490 describe_artifacts_to_stdout(
aac885a… drh 2491 "IN (WITH RECURSIVE delta_phantom(rid) AS (\n"
aac885a… drh 2492 " SELECT delta.rid FROM blob, delta\n"
aac885a… drh 2493 " WHERE blob.size<0 AND delta.srcid=blob.rid\n"
aac885a… drh 2494 " UNION\n"
aac885a… drh 2495 " SELECT delta.rid FROM delta_phantom, delta\n"
aac885a… drh 2496 " WHERE delta.srcid=delta_phantom.rid)\n"
aac885a… drh 2497 " SELECT rid FROM delta_phantom)", 0);
aac885a… drh 2498 }else{
aac885a… drh 2499 describe_artifacts_to_stdout("IN (SELECT rid FROM blob WHERE size<0)", 0);
aac885a… drh 2500 }
aa12ac9… drh 2501 }
aa12ac9… drh 2502
aa12ac9… drh 2503 /* Maximum number of collision examples to remember */
aa12ac9… drh 2504 #define MAX_COLLIDE 25
aa12ac9… drh 2505
aa12ac9… drh 2506 /*
fd9b7bd… drh 2507 ** Generate a report on the number of collisions in artifact hashes
60af057… drh 2508 ** generated by the SQL given in the argument.
aa12ac9… drh 2509 */
60af057… drh 2510 static void collision_report(const char *zSql){
7425082… mgagnon 2511 int i, j;
aa12ac9… drh 2512 int nHash = 0;
aa12ac9… drh 2513 Stmt q;
fd9b7bd… drh 2514 char zPrev[HNAME_MAX+1];
aa12ac9… drh 2515 struct {
aa12ac9… drh 2516 int cnt;
aa12ac9… drh 2517 char *azHit[MAX_COLLIDE];
fd9b7bd… drh 2518 char z[HNAME_MAX+1];
fd9b7bd… drh 2519 } aCollide[HNAME_MAX+1];
aa12ac9… drh 2520 memset(aCollide, 0, sizeof(aCollide));
aa12ac9… drh 2521 memset(zPrev, 0, sizeof(zPrev));
60af057… drh 2522 db_prepare(&q,"%s",zSql/*safe-for-%s*/);
aa12ac9… drh 2523 while( db_step(&q)==SQLITE_ROW ){
aa12ac9… drh 2524 const char *zUuid = db_column_text(&q,0);
aa12ac9… drh 2525 int n = db_column_bytes(&q,0);
aa12ac9… drh 2526 int i;
aa12ac9… drh 2527 nHash++;
aa12ac9… drh 2528 for(i=0; zPrev[i] && zPrev[i]==zUuid[i]; i++){}
fd9b7bd… drh 2529 if( i>0 && i<=HNAME_MAX ){
aa12ac9… drh 2530 if( i>=4 && aCollide[i].cnt<MAX_COLLIDE ){
aa12ac9… drh 2531 aCollide[i].azHit[aCollide[i].cnt] = mprintf("%.*s", i, zPrev);
aa12ac9… drh 2532 }
aa12ac9… drh 2533 aCollide[i].cnt++;
aa12ac9… drh 2534 if( aCollide[i].z[0]==0 ) memcpy(aCollide[i].z, zPrev, n+1);
aa12ac9… drh 2535 }
aa12ac9… drh 2536 memcpy(zPrev, zUuid, n+1);
aa12ac9… drh 2537 }
aa12ac9… drh 2538 db_finalize(&q);
aa12ac9… drh 2539 @ <table border=1><thead>
aa12ac9… drh 2540 @ <tr><th>Length<th>Instances<th>First Instance</tr>
aa12ac9… drh 2541 @ </thead><tbody>
fd9b7bd… drh 2542 for(i=1; i<=HNAME_MAX; i++){
aa12ac9… drh 2543 if( aCollide[i].cnt==0 ) continue;
aa12ac9… drh 2544 @ <tr><td>%d(i)<td>%d(aCollide[i].cnt)<td>%h(aCollide[i].z)</tr>
aa12ac9… drh 2545 }
aa12ac9… drh 2546 @ </tbody></table>
aa12ac9… drh 2547 @ <p>Total number of hashes: %d(nHash)</p>
fd9b7bd… drh 2548 for(i=HNAME_MAX; i>=4; i--){
aa12ac9… drh 2549 if( aCollide[i].cnt==0 ) continue;
aa12ac9… drh 2550 if( aCollide[i].cnt>200 ) break;
aa12ac9… drh 2551 if( aCollide[i].cnt<25 ){
aa12ac9… drh 2552 @ <p>Collisions of length %d(i):
aa12ac9… drh 2553 }else{
aa12ac9… drh 2554 @ <p>First 25 collisions of length %d(i):
aa12ac9… drh 2555 }
aa12ac9… drh 2556 for(j=0; j<aCollide[i].cnt && j<MAX_COLLIDE; j++){
aa12ac9… drh 2557 char *zId = aCollide[i].azHit[j];
aa12ac9… drh 2558 if( zId==0 ) continue;
9a0132e… drh 2559 @ %z(href("%R/ambiguous/%s",zId))%h(zId)</a>
60af057… drh 2560 }
60af057… drh 2561 }
3cb9ba4… andygoth 2562 for(i=4; i<count(aCollide); i++){
60af057… drh 2563 for(j=0; j<aCollide[i].cnt && j<MAX_COLLIDE; j++){
60af057… drh 2564 fossil_free(aCollide[i].azHit[j]);
60af057… drh 2565 }
60af057… drh 2566 }
60af057… drh 2567 }
60af057… drh 2568
60af057… drh 2569 /*
60af057… drh 2570 ** WEBPAGE: hash-collisions
60af057… drh 2571 **
60af057… drh 2572 ** Show the number of hash collisions for hash prefixes of various lengths.
60af057… drh 2573 */
60af057… drh 2574 void hash_collisions_webpage(void){
60af057… drh 2575 login_check_credentials();
60af057… drh 2576 if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
fd9b7bd… drh 2577 style_header("Hash Prefix Collisions");
187424e… andygoth 2578 style_submenu_element("Activity Reports", "reports");
187424e… andygoth 2579 style_submenu_element("Stats", "stat");
60af057… drh 2580 @ <h1>Hash Prefix Collisions on Check-ins</h1>
60af057… drh 2581 collision_report("SELECT (SELECT uuid FROM blob WHERE rid=objid)"
60af057… drh 2582 " FROM event WHERE event.type='ci'"
60af057… drh 2583 " ORDER BY 1");
60af057… drh 2584 @ <h1>Hash Prefix Collisions on All Artifacts</h1>
60af057… drh 2585 collision_report("SELECT uuid FROM blob ORDER BY 1");
a2ce1f1… drh 2586 style_finish_page();
a2ce1f1… drh 2587 }
a2ce1f1… drh 2588
a2ce1f1… drh 2589 /*
a2ce1f1… drh 2590 ** WEBPAGE: clusterlist
a2ce1f1… drh 2591 **
a2ce1f1… drh 2592 ** Show information about all cluster artifacts in the database.
a2ce1f1… drh 2593 */
a2ce1f1… drh 2594 void clusterlist_page(void){
a2ce1f1… drh 2595 Stmt q;
e3ef536… drh 2596 int cnt = 1;
e3ef536… drh 2597 sqlite3_int64 szTotal = 0;
90764bd… drh 2598 sqlite3_int64 szCTotal = 0;
a2ce1f1… drh 2599 login_check_credentials();
90764bd… drh 2600 if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
a2ce1f1… drh 2601 style_header("All Cluster Artifacts");
3057775… drh 2602 style_submenu_element("All Artifactst", "bloblist");
3057775… drh 2603 if( g.perm.Admin ){
c675484… drh 2604 style_submenu_element("Xfer Log", "rcvfromlist");
3057775… drh 2605 }
3057775… drh 2606 style_submenu_element("Phantoms", "bloblist?phan");
3057775… drh 2607 if( g.perm.Write ){
3057775… drh 2608 style_submenu_element("Artifact Stats", "artifact_stats");
3057775… drh 2609 }
3057775… drh 2610
a2ce1f1… drh 2611 db_prepare(&q,
a2ce1f1… drh 2612 "SELECT blob.uuid, "
e3ef536… drh 2613 " blob.size, "
90764bd… drh 2614 " octet_length(blob.content), "
a2ce1f1… drh 2615 " datetime(rcvfrom.mtime),"
a2ce1f1… drh 2616 " user.login,"
a2ce1f1… drh 2617 " rcvfrom.ipaddr"
a2ce1f1… drh 2618 " FROM tagxref JOIN blob ON tagxref.rid=blob.rid"
a2ce1f1… drh 2619 " LEFT JOIN rcvfrom ON blob.rcvid=rcvfrom.rcvid"
a2ce1f1… drh 2620 " LEFT JOIN user ON user.uid=rcvfrom.uid"
a2ce1f1… drh 2621 " WHERE tagxref.tagid=%d"
a2ce1f1… drh 2622 " ORDER BY rcvfrom.mtime, blob.uuid",
a2ce1f1… drh 2623 TAG_CLUSTER
a2ce1f1… drh 2624 );
a2ce1f1… drh 2625 @ <table cellpadding="2" cellspacing="0" border="1">
90764bd… drh 2626 @ <tr><th>&nbsp;
90764bd… drh 2627 @ <th>Hash
90764bd… drh 2628 @ <th>Date&nbsp;Received
90764bd… drh 2629 @ <th>Size
90764bd… drh 2630 @ <th>Compressed&nbsp;Size
90764bd… drh 2631 if( g.perm.Admin ){
90764bd… drh 2632 @ <th>User<th>IP-Address
90764bd… drh 2633 }
a2ce1f1… drh 2634 while( db_step(&q)==SQLITE_ROW ){
a2ce1f1… drh 2635 const char *zUuid = db_column_text(&q, 0);
e3ef536… drh 2636 sqlite3_int64 sz = db_column_int64(&q, 1);
90764bd… drh 2637 sqlite3_int64 szC = db_column_int64(&q, 2);
90764bd… drh 2638 const char *zDate = db_column_text(&q, 3);
90764bd… drh 2639 const char *zUser = db_column_text(&q, 4);
90764bd… drh 2640 const char *zIp = db_column_text(&q, 5);
e3ef536… drh 2641 szTotal += sz;
90764bd… drh 2642 szCTotal += szC;
e3ef536… drh 2643 @ <tr><td align="right">%d(cnt++)
e3ef536… drh 2644 @ <td><a href="%R/info/%S(zUuid)">%S(zUuid)</a>
a2ce1f1… drh 2645 if( zDate ){
a2ce1f1… drh 2646 @ <td>%h(zDate)
a2ce1f1… drh 2647 }else{
a2ce1f1… drh 2648 @ <td>&nbsp;
a2ce1f1… drh 2649 }
e3ef536… drh 2650 @ <td align="right">%,lld(sz)
90764bd… drh 2651 @ <td align="right">%,lld(szC)
90764bd… drh 2652 if( g.perm.Admin ){
90764bd… drh 2653 if( zUser ){
90764bd… drh 2654 @ <td>%h(zUser)
90764bd… drh 2655 }else{
90764bd… drh 2656 @ <td>&nbsp;
90764bd… drh 2657 }
90764bd… drh 2658 if( zIp ){
90764bd… drh 2659 @ <td>%h(zIp)
90764bd… drh 2660 }else{
90764bd… drh 2661 @ <td>&nbsp;
90764bd… drh 2662 }
a2ce1f1… drh 2663 }
a2ce1f1… drh 2664 @ </tr>
a2ce1f1… drh 2665 }
a2ce1f1… drh 2666 @ </table>
a2ce1f1… drh 2667 db_finalize(&q);
90764bd… drh 2668 @ <p>Total size of all clusters: %,lld(szTotal) bytes,
90764bd… drh 2669 @ %,lld(szCTotal) bytes compressed</p>
112c713… drh 2670 style_finish_page();
932825b… drh 2671 }

Keyboard Shortcuts

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