0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68 (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54 ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20 te it and/or.**
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 modify it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76 lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65 ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 tributed in the
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20 l be useful,.**
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 but WITHOUT ANY
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20 RPOSE. See the
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50 GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 contains code u
0380: 73 65 64 20 74 6f 20 72 65 62 75 69 6c 64 20 74 sed to rebuild t
0390: 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a he database..*/.
03a0: 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 67 #include "config
03b0: 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 72 65 .h".#include "re
03c0: 62 75 69 6c 64 2e 68 22 0a 23 69 6e 63 6c 75 64 build.h".#includ
03d0: 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 0a 2f 2a e <assert.h>../*
03e0: 0a 2a 2a 20 53 63 68 65 6d 61 20 63 68 61 6e 67 .** Schema chang
03f0: 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e es.*/.static con
0400: 73 74 20 63 68 61 72 20 7a 53 63 68 65 6d 61 55 st char zSchemaU
0410: 70 64 61 74 65 73 5b 5d 20 3d 0a 40 20 2d 2d 20 pdates[] =.@ --
0420: 49 6e 64 65 78 20 6f 6e 20 74 68 65 20 64 65 6c Index on the del
0430: 74 61 20 74 61 62 6c 65 0a 40 20 2d 2d 0a 40 20 ta table.@ --.@
0440: 43 52 45 41 54 45 20 49 4e 44 45 58 20 49 46 20 CREATE INDEX IF
0450: 4e 4f 54 20 45 58 49 53 54 53 20 64 65 6c 74 61 NOT EXISTS delta
0460: 5f 69 31 20 4f 4e 20 64 65 6c 74 61 28 73 72 63 _i1 ON delta(src
0470: 69 64 29 3b 0a 40 0a 40 20 2d 2d 20 41 72 74 69 id);.@.@ -- Arti
0480: 66 61 63 74 73 20 74 68 61 74 20 73 68 6f 75 6c facts that shoul
0490: 64 20 6e 6f 74 20 62 65 20 70 72 6f 63 65 73 73 d not be process
04a0: 65 64 20 61 72 65 20 69 64 65 6e 74 69 66 69 65 ed are identifie
04b0: 64 20 69 6e 20 74 68 65 0a 40 20 2d 2d 20 22 73 d in the.@ -- "s
04c0: 68 75 6e 22 20 74 61 62 6c 65 2e 20 20 41 72 74 hun" table. Art
04d0: 69 66 61 63 74 73 20 74 68 61 74 20 61 72 65 20 ifacts that are
04e0: 63 6f 6e 74 72 6f 6c 2d 66 69 6c 65 20 66 6f 72 control-file for
04f0: 67 65 72 69 65 73 20 6f 72 0a 40 20 2d 2d 20 73 geries or.@ -- s
0500: 70 61 6d 20 6f 72 20 61 72 74 69 66 61 63 74 73 pam or artifacts
0510: 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74 73 20 whose contents
0520: 76 69 6f 6c 61 74 65 20 61 64 6d 69 6e 69 73 74 violate administ
0530: 72 61 74 69 76 65 20 70 6f 6c 69 63 79 0a 40 20 rative policy.@
0540: 2d 2d 20 63 61 6e 20 62 65 20 73 68 75 6e 6e 65 -- can be shunne
0550: 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 d in order to pr
0560: 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 event them from
0570: 63 6f 6e 74 61 6d 69 6e 61 74 69 6e 67 0a 40 20 contaminating.@
0580: 2d 2d 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 -- the repositor
0590: 79 2e 0a 40 20 2d 2d 0a 40 20 2d 2d 20 53 68 75 y..@ --.@ -- Shu
05a0: 6e 6e 65 64 20 61 72 74 69 66 61 63 74 73 20 64 nned artifacts d
05b0: 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 o not exist in t
05c0: 68 65 20 62 6c 6f 62 20 74 61 62 6c 65 2e 20 20 he blob table.
05d0: 48 65 6e 63 65 20 74 68 65 79 0a 40 20 2d 2d 20 Hence they.@ --
05e0: 68 61 76 65 20 6e 6f 74 20 61 72 74 69 66 61 63 have not artifac
05f0: 74 20 49 44 20 28 72 69 64 29 20 61 6e 64 20 77 t ID (rid) and w
0600: 65 20 74 68 75 73 20 6d 75 73 74 20 73 74 6f 72 e thus must stor
0610: 65 20 74 68 65 69 72 20 66 75 6c 6c 0a 40 20 2d e their full.@ -
0620: 2d 20 55 55 49 44 2e 0a 40 20 2d 2d 0a 40 20 43 - UUID..@ --.@ C
0630: 52 45 41 54 45 20 54 41 42 4c 45 20 49 46 20 4e REATE TABLE IF N
0640: 4f 54 20 45 58 49 53 54 53 20 73 68 75 6e 28 75 OT EXISTS shun(u
0650: 75 69 64 20 55 4e 49 51 55 45 29 3b 0a 40 0a 40 uid UNIQUE);.@.@
0660: 20 2d 2d 20 41 72 74 69 66 61 63 74 73 20 74 68 -- Artifacts th
0670: 61 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 at should not be
0680: 20 70 75 73 68 65 64 20 61 72 65 20 73 74 6f 72 pushed are stor
0690: 65 64 20 69 6e 20 74 68 65 20 22 70 72 69 76 61 ed in the "priva
06a0: 74 65 22 0a 40 20 2d 2d 20 74 61 62 6c 65 2e 20 te".@ -- table.
06b0: 20 0a 40 20 2d 2d 0a 40 20 43 52 45 41 54 45 20 .@ --.@ CREATE
06c0: 54 41 42 4c 45 20 49 46 20 4e 4f 54 20 45 58 49 TABLE IF NOT EXI
06d0: 53 54 53 20 70 72 69 76 61 74 65 28 72 69 64 20 STS private(rid
06e0: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY
06f0: 4b 45 59 29 3b 0a 40 0a 40 20 2d 2d 20 41 6e 20 KEY);.@.@ -- An
0700: 65 6e 74 72 79 20 69 6e 20 74 68 69 73 20 74 61 entry in this ta
0710: 62 6c 65 20 64 65 73 63 72 69 62 65 73 20 61 20 ble describes a
0720: 64 61 74 61 62 61 73 65 20 71 75 65 72 79 20 74 database query t
0730: 68 61 74 20 67 65 6e 65 72 61 74 65 73 20 61 0a hat generates a.
0740: 40 20 2d 2d 20 74 61 62 6c 65 20 6f 66 20 74 69 @ -- table of ti
0750: 63 6b 65 74 73 2e 0a 40 20 2d 2d 0a 40 20 43 52 ckets..@ --.@ CR
0760: 45 41 54 45 20 54 41 42 4c 45 20 49 46 20 4e 4f EATE TABLE IF NO
0770: 54 20 45 58 49 53 54 53 20 72 65 70 6f 72 74 66 T EXISTS reportf
0780: 6d 74 28 0a 40 20 20 20 20 72 6e 20 69 6e 74 65 mt(.@ rn inte
0790: 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 2c ger primary key,
07a0: 20 20 2d 2d 20 52 65 70 6f 72 74 20 6e 75 6d 62 -- Report numb
07b0: 65 72 0a 40 20 20 20 20 6f 77 6e 65 72 20 74 65 er.@ owner te
07c0: 78 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 xt,
07d0: 20 2d 2d 20 4f 77 6e 65 72 20 6f 66 20 74 68 69 -- Owner of thi
07e0: 73 20 72 65 70 6f 72 74 20 66 6f 72 6d 61 74 20 s report format
07f0: 28 6e 6f 74 20 75 73 65 64 29 0a 40 20 20 20 20 (not used).@
0800: 74 69 74 6c 65 20 74 65 78 74 2c 20 20 20 20 20 title text,
0810: 20 20 20 20 20 20 20 20 20 2d 2d 20 54 69 74 6c -- Titl
0820: 65 20 6f 66 20 74 68 69 73 20 72 65 70 6f 72 74 e of this report
0830: 0a 40 20 20 20 20 63 6f 6c 73 20 74 65 78 74 2c .@ cols text,
0840: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d -
0850: 2d 20 41 20 63 6f 6c 6f 72 2d 6b 65 79 20 73 70 - A color-key sp
0860: 65 63 69 66 69 63 61 74 69 6f 6e 0a 40 20 20 20 ecification.@
0870: 20 73 71 6c 63 6f 64 65 20 74 65 78 74 20 20 20 sqlcode text
0880: 20 20 20 20 20 20 20 20 20 20 2d 2d 20 41 6e 20 -- An
0890: 53 51 4c 20 53 45 4c 45 43 54 20 73 74 61 74 65 SQL SELECT state
08a0: 6d 65 6e 74 20 66 6f 72 20 74 68 69 73 20 72 65 ment for this re
08b0: 70 6f 72 74 0a 40 20 29 3b 0a 40 0a 40 20 2d 2d port.@ );.@.@ --
08c0: 20 53 6f 6d 65 20 74 69 63 6b 65 74 20 63 6f 6e Some ticket con
08d0: 74 65 6e 74 20 28 73 75 63 68 20 61 73 20 74 68 tent (such as th
08e0: 65 20 6f 72 69 67 69 6e 61 74 6f 72 73 20 65 6d e originators em
08f0: 61 69 6c 20 61 64 64 72 65 73 73 20 6f 72 20 63 ail address or c
0900: 6f 6e 74 61 63 74 0a 40 20 2d 2d 20 69 6e 66 6f ontact.@ -- info
0910: 72 6d 61 74 69 6f 6e 29 20 6e 65 65 64 73 20 74 rmation) needs t
0920: 6f 20 62 65 20 6f 62 73 63 75 72 65 64 20 74 6f o be obscured to
0930: 20 70 72 6f 74 65 63 74 20 70 72 69 76 61 63 79 protect privacy
0940: 2e 20 20 54 68 69 73 20 69 73 20 61 63 68 69 65 . This is achie
0950: 76 65 64 0a 40 20 2d 2d 20 62 79 20 73 74 6f 72 ved.@ -- by stor
0960: 69 6e 67 20 61 6e 20 53 48 41 31 20 68 61 73 68 ing an SHA1 hash
0970: 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e of the content.
0980: 20 20 46 6f 72 20 64 69 73 70 6c 61 79 2c 20 74 For display, t
0990: 68 65 20 68 61 73 68 20 69 73 0a 40 20 2d 2d 20 he hash is.@ --
09a0: 6d 61 70 70 65 64 20 62 61 63 6b 20 69 6e 74 6f mapped back into
09b0: 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 the original te
09c0: 78 74 20 75 73 69 6e 67 20 74 68 69 73 20 74 61 xt using this ta
09d0: 62 6c 65 2e 20 20 0a 40 20 2d 2d 0a 40 20 2d 2d ble. .@ --.@ --
09e0: 20 54 68 69 73 20 74 61 62 6c 65 20 63 6f 6e 74 This table cont
09f0: 61 69 6e 73 20 73 65 6e 73 69 74 69 76 65 20 69 ains sensitive i
0a00: 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 73 nformation and s
0a10: 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 68 61 hould not be sha
0a20: 72 65 64 0a 40 20 2d 2d 20 77 69 74 68 20 75 6e red.@ -- with un
0a30: 61 75 74 68 6f 72 69 7a 65 64 20 75 73 65 72 73 authorized users
0a40: 2e 0a 40 20 2d 2d 0a 40 20 43 52 45 41 54 45 20 ..@ --.@ CREATE
0a50: 54 41 42 4c 45 20 49 46 20 4e 4f 54 20 45 58 49 TABLE IF NOT EXI
0a60: 53 54 53 20 63 6f 6e 63 65 61 6c 65 64 28 0a 40 STS concealed(.@
0a70: 20 20 20 68 61 73 68 20 54 45 58 54 20 50 52 49 hash TEXT PRI
0a80: 4d 41 52 59 20 4b 45 59 2c 0a 40 20 20 20 63 6f MARY KEY,.@ co
0a90: 6e 74 65 6e 74 20 54 45 58 54 0a 40 20 29 3b 0a ntent TEXT.@ );.
0aa0: 3b 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 6c ;../*.** Variabl
0ab0: 65 73 20 75 73 65 64 20 66 6f 72 20 70 72 6f 67 es used for prog
0ac0: 72 65 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ress information
0ad0: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 .*/.static int t
0ae0: 6f 74 61 6c 53 69 7a 65 3b 20 20 20 20 20 20 20 otalSize;
0af0: 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 /* Total number
0b00: 6f 66 20 61 72 74 69 66 61 63 74 73 20 74 6f 20 of artifacts to
0b10: 70 72 6f 63 65 73 73 20 2a 2f 0a 73 74 61 74 69 process */.stati
0b20: 63 20 69 6e 74 20 70 72 6f 63 65 73 73 43 6e 74 c int processCnt
0b30: 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ; /* Number
0b40: 20 70 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 processed so fa
0b50: 72 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 r */.static int
0b60: 74 74 79 4f 75 74 70 75 74 3b 20 20 20 20 20 20 ttyOutput;
0b70: 20 2f 2a 20 44 6f 20 70 72 6f 67 72 65 73 73 20 /* Do progress
0b80: 6f 75 74 70 75 74 20 2a 2f 0a 73 74 61 74 69 63 output */.static
0b90: 20 42 61 67 20 62 61 67 44 6f 6e 65 3b 20 20 20 Bag bagDone;
0ba0: 20 20 20 20 20 20 2f 2a 20 42 61 67 20 6f 66 20 /* Bag of
0bb0: 72 65 63 6f 72 64 73 20 72 65 62 75 69 6c 74 20 records rebuilt
0bc0: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 */../*.** Called
0bd0: 20 61 66 74 65 72 20 65 61 63 68 20 61 72 74 69 after each arti
0be0: 66 61 63 74 20 69 73 20 70 72 6f 63 65 73 73 65 fact is processe
0bf0: 64 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 d.*/.static void
0c00: 20 72 65 62 75 69 6c 64 5f 73 74 65 70 5f 64 6f rebuild_step_do
0c10: 6e 65 28 72 69 64 29 7b 0a 20 20 2f 2a 20 61 73 ne(rid){. /* as
0c20: 73 65 72 74 28 20 62 61 67 5f 66 69 6e 64 28 26 sert( bag_find(&
0c30: 62 61 67 44 6f 6e 65 2c 20 72 69 64 29 3d 3d 30 bagDone, rid)==0
0c40: 20 29 3b 20 2a 2f 0a 20 20 62 61 67 5f 69 6e 73 ); */. bag_ins
0c50: 65 72 74 28 26 62 61 67 44 6f 6e 65 2c 20 72 69 ert(&bagDone, ri
0c60: 64 29 3b 0a 20 20 69 66 28 20 74 74 79 4f 75 74 d);. if( ttyOut
0c70: 70 75 74 20 29 7b 0a 20 20 20 20 70 72 6f 63 65 put ){. proce
0c80: 73 73 43 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 20 ssCnt++;. if
0c90: 28 21 67 2e 66 51 75 69 65 74 29 20 7b 0a 20 20 (!g.fQuiet) {.
0ca0: 20 20 20 20 70 72 69 6e 74 66 28 22 25 64 20 28 printf("%d (
0cb0: 25 64 25 25 29 2e 2e 2e 5c 72 22 2c 20 70 72 6f %d%%)...\r", pro
0cc0: 63 65 73 73 43 6e 74 2c 20 28 70 72 6f 63 65 73 cessCnt, (proces
0cd0: 73 43 6e 74 2a 31 30 30 2f 74 6f 74 61 6c 53 69 sCnt*100/totalSi
0ce0: 7a 65 29 29 3b 0a 20 20 20 20 20 20 66 66 6c 75 ze));. fflu
0cf0: 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 20 20 sh(stdout);.
0d00: 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 }. }.}../*.** R
0d10: 65 62 75 69 6c 64 20 63 72 6f 73 73 2d 72 65 66 ebuild cross-ref
0d20: 65 72 65 6e 63 69 6e 67 20 69 6e 66 6f 72 6d 61 erencing informa
0d30: 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 61 72 74 tion for the art
0d40: 69 66 61 63 74 0a 2a 2a 20 72 69 64 20 77 69 74 ifact.** rid wit
0d50: 68 20 63 6f 6e 74 65 6e 74 20 70 42 61 73 65 20 h content pBase
0d60: 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 and all of its d
0d70: 65 73 63 65 6e 64 61 6e 74 73 2e 20 20 54 68 69 escendants. Thi
0d80: 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 63 6c 65 s.** routine cle
0d90: 61 72 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ars the content
0da0: 62 75 66 66 65 72 20 62 65 66 6f 72 65 20 72 65 buffer before re
0db0: 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 turning..*/.stat
0dc0: 69 63 20 76 6f 69 64 20 72 65 62 75 69 6c 64 5f ic void rebuild_
0dd0: 73 74 65 70 28 69 6e 74 20 72 69 64 2c 20 69 6e step(int rid, in
0de0: 74 20 73 69 7a 65 2c 20 42 6c 6f 62 20 2a 70 42 t size, Blob *pB
0df0: 61 73 65 29 7b 0a 20 20 53 74 6d 74 20 71 31 3b ase){. Stmt q1;
0e00: 0a 20 20 42 61 67 20 63 68 69 6c 64 72 65 6e 3b . Bag children;
0e10: 0a 20 20 42 6c 6f 62 20 63 6f 70 79 3b 0a 20 20 . Blob copy;.
0e20: 42 6c 6f 62 20 2a 70 55 73 65 3b 0a 20 20 69 6e Blob *pUse;. in
0e30: 74 20 6e 43 68 69 6c 64 2c 20 69 2c 20 63 69 64 t nChild, i, cid
0e40: 3b 0a 0a 20 20 2f 2a 20 46 69 78 20 75 70 20 74 ;.. /* Fix up t
0e50: 68 65 20 22 62 6c 6f 62 2e 73 69 7a 65 22 20 66 he "blob.size" f
0e60: 69 65 6c 64 20 69 66 20 6e 65 65 64 65 64 2e 20 ield if needed.
0e70: 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 21 3d 62 */. if( size!=b
0e80: 6c 6f 62 5f 73 69 7a 65 28 70 42 61 73 65 29 20 lob_size(pBase)
0e90: 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f ){. db_multi_
0ea0: 65 78 65 63 28 0a 20 20 20 20 20 20 20 22 55 50 exec(. "UP
0eb0: 44 41 54 45 20 62 6c 6f 62 20 53 45 54 20 73 69 DATE blob SET si
0ec0: 7a 65 3d 25 64 20 57 48 45 52 45 20 72 69 64 3d ze=%d WHERE rid=
0ed0: 25 64 22 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 70 %d", blob_size(p
0ee0: 42 61 73 65 29 2c 20 72 69 64 0a 20 20 20 20 29 Base), rid. )
0ef0: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 ;. }.. /* Find
0f00: 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 all children of
0f10: 20 61 72 74 69 66 61 63 74 20 72 69 64 20 2a 2f artifact rid */
0f20: 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 . db_prepare(&q
0f30: 31 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 46 1, "SELECT rid F
0f40: 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 ROM delta WHERE
0f50: 73 72 63 69 64 3d 25 64 22 2c 20 72 69 64 29 3b srcid=%d", rid);
0f60: 0a 20 20 62 61 67 5f 69 6e 69 74 28 26 63 68 69 . bag_init(&chi
0f70: 6c 64 72 65 6e 29 3b 0a 20 20 77 68 69 6c 65 28 ldren);. while(
0f80: 20 64 62 5f 73 74 65 70 28 26 71 31 29 3d 3d 53 db_step(&q1)==S
0f90: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 QLITE_ROW ){.
0fa0: 20 69 6e 74 20 63 69 64 20 3d 20 64 62 5f 63 6f int cid = db_co
0fb0: 6c 75 6d 6e 5f 69 6e 74 28 26 71 31 2c 20 30 29 lumn_int(&q1, 0)
0fc0: 3b 0a 20 20 20 20 69 66 28 20 21 62 61 67 5f 66 ;. if( !bag_f
0fd0: 69 6e 64 28 26 62 61 67 44 6f 6e 65 2c 20 63 69 ind(&bagDone, ci
0fe0: 64 29 20 29 7b 0a 20 20 20 20 20 20 62 61 67 5f d) ){. bag_
0ff0: 69 6e 73 65 72 74 28 26 63 68 69 6c 64 72 65 6e insert(&children
1000: 2c 20 63 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 , cid);. }.
1010: 7d 0a 20 20 6e 43 68 69 6c 64 20 3d 20 62 61 67 }. nChild = bag
1020: 5f 63 6f 75 6e 74 28 26 63 68 69 6c 64 72 65 6e _count(&children
1030: 29 3b 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 );. db_finalize
1040: 28 26 71 31 29 3b 0a 0a 20 20 2f 2a 20 43 72 6f (&q1);.. /* Cro
1050: 73 73 6c 69 6e 6b 20 74 68 65 20 61 72 74 69 66 sslink the artif
1060: 61 63 74 20 2a 2f 0a 20 20 69 66 28 20 6e 43 68 act */. if( nCh
1070: 69 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20 70 55 ild==0 ){. pU
1080: 73 65 20 3d 20 70 42 61 73 65 3b 0a 20 20 7d 65 se = pBase;. }e
1090: 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f lse{. blob_co
10a0: 70 79 28 26 63 6f 70 79 2c 20 70 42 61 73 65 29 py(©, pBase)
10b0: 3b 0a 20 20 20 20 70 55 73 65 20 3d 20 26 63 6f ;. pUse = &co
10c0: 70 79 3b 0a 20 20 7d 0a 20 20 6d 61 6e 69 66 65 py;. }. manife
10d0: 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 28 72 69 64 st_crosslink(rid
10e0: 2c 20 70 55 73 65 29 3b 0a 20 20 62 6c 6f 62 5f , pUse);. blob_
10f0: 72 65 73 65 74 28 70 55 73 65 29 3b 0a 0a 20 20 reset(pUse);..
1100: 2f 2a 20 43 61 6c 6c 20 61 6c 6c 20 63 68 69 6c /* Call all chil
1110: 64 72 65 6e 20 72 65 63 75 72 73 69 76 65 6c 79 dren recursively
1120: 20 2a 2f 0a 20 20 66 6f 72 28 63 69 64 3d 62 61 */. for(cid=ba
1130: 67 5f 66 69 72 73 74 28 26 63 68 69 6c 64 72 65 g_first(&childre
1140: 6e 29 2c 20 69 3d 31 3b 20 63 69 64 3b 20 63 69 n), i=1; cid; ci
1150: 64 3d 62 61 67 5f 6e 65 78 74 28 26 63 68 69 6c d=bag_next(&chil
1160: 64 72 65 6e 2c 20 63 69 64 29 2c 20 69 2b 2b 29 dren, cid), i++)
1170: 7b 0a 20 20 20 20 53 74 6d 74 20 71 32 3b 0a 20 {. Stmt q2;.
1180: 20 20 20 69 6e 74 20 73 7a 3b 0a 20 20 20 20 69 int sz;. i
1190: 66 28 20 6e 43 68 69 6c 64 3d 3d 69 20 29 7b 0a f( nChild==i ){.
11a0: 20 20 20 20 20 20 70 55 73 65 20 3d 20 70 42 61 pUse = pBa
11b0: 73 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 se;. }else{.
11c0: 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 28 26 blob_copy(&
11d0: 63 6f 70 79 2c 20 70 42 61 73 65 29 3b 0a 20 20 copy, pBase);.
11e0: 20 20 20 20 70 55 73 65 20 3d 20 26 63 6f 70 79 pUse = ©
11f0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 70 ;. }. db_p
1200: 72 65 70 61 72 65 28 26 71 32 2c 20 22 53 45 4c repare(&q2, "SEL
1210: 45 43 54 20 63 6f 6e 74 65 6e 74 2c 20 73 69 7a ECT content, siz
1220: 65 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 e FROM blob WHER
1230: 45 20 72 69 64 3d 25 64 22 2c 20 63 69 64 29 3b E rid=%d", cid);
1240: 0a 20 20 20 20 69 66 28 20 64 62 5f 73 74 65 70 . if( db_step
1250: 28 26 71 32 29 3d 3d 53 51 4c 49 54 45 5f 52 4f (&q2)==SQLITE_RO
1260: 57 20 26 26 20 28 73 7a 20 3d 20 64 62 5f 63 6f W && (sz = db_co
1270: 6c 75 6d 6e 5f 69 6e 74 28 26 71 32 2c 31 29 29 lumn_int(&q2,1))
1280: 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 42 6c 6f >=0 ){. Blo
1290: 62 20 64 65 6c 74 61 3b 0a 20 20 20 20 20 20 64 b delta;. d
12a0: 62 5f 65 70 68 65 6d 65 72 61 6c 5f 62 6c 6f 62 b_ephemeral_blob
12b0: 28 26 71 32 2c 20 30 2c 20 26 64 65 6c 74 61 29 (&q2, 0, &delta)
12c0: 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 75 6e 63 ;. blob_unc
12d0: 6f 6d 70 72 65 73 73 28 26 64 65 6c 74 61 2c 20 ompress(&delta,
12e0: 26 64 65 6c 74 61 29 3b 0a 20 20 20 20 20 20 62 &delta);. b
12f0: 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 70 6c 79 28 lob_delta_apply(
1300: 70 55 73 65 2c 20 26 64 65 6c 74 61 2c 20 70 55 pUse, &delta, pU
1310: 73 65 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f se);. blob_
1320: 72 65 73 65 74 28 26 64 65 6c 74 61 29 3b 0a 20 reset(&delta);.
1330: 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 db_finalize
1340: 28 26 71 32 29 3b 0a 20 20 20 20 20 20 72 65 62 (&q2);. reb
1350: 75 69 6c 64 5f 73 74 65 70 28 63 69 64 2c 20 73 uild_step(cid, s
1360: 7a 2c 20 70 55 73 65 29 3b 0a 20 20 20 20 7d 65 z, pUse);. }e
1370: 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 5f 66 69 lse{. db_fi
1380: 6e 61 6c 69 7a 65 28 26 71 32 29 3b 0a 20 20 20 nalize(&q2);.
1390: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 70 55 blob_reset(pU
13a0: 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 se);. }. }.
13b0: 20 62 61 67 5f 63 6c 65 61 72 28 26 63 68 69 6c bag_clear(&chil
13c0: 64 72 65 6e 29 3b 0a 20 20 72 65 62 75 69 6c 64 dren);. rebuild
13d0: 5f 73 74 65 70 5f 64 6f 6e 65 28 72 69 64 29 3b _step_done(rid);
13e0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 .}../*.** Check
13f0: 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 74 68 to see if the th
1400: 65 20 22 73 79 6d 2d 74 72 75 6e 6b 22 20 74 61 e "sym-trunk" ta
1410: 67 20 65 78 69 73 74 73 2e 20 20 49 66 20 6e 6f g exists. If no
1420: 74 2c 20 63 72 65 61 74 65 20 69 74 0a 2a 2a 20 t, create it.**
1430: 61 6e 64 20 61 74 74 61 63 68 20 69 74 20 74 6f and attach it to
1440: 20 74 68 65 20 76 65 72 79 20 66 69 72 73 74 20 the very first
1450: 63 68 65 63 6b 2d 69 6e 2e 0a 2a 2f 0a 73 74 61 check-in..*/.sta
1460: 74 69 63 20 76 6f 69 64 20 72 65 62 75 69 6c 64 tic void rebuild
1470: 5f 74 61 67 5f 74 72 75 6e 6b 28 76 6f 69 64 29 _tag_trunk(void)
1480: 7b 0a 20 20 69 6e 74 20 74 61 67 69 64 20 3d 20 {. int tagid =
1490: 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 db_int(0, "SELEC
14a0: 54 20 31 20 46 52 4f 4d 20 74 61 67 20 57 48 45 T 1 FROM tag WHE
14b0: 52 45 20 74 61 67 6e 61 6d 65 3d 27 73 79 6d 2d RE tagname='sym-
14c0: 74 72 75 6e 6b 27 22 29 3b 0a 20 20 69 6e 74 20 trunk'");. int
14d0: 72 69 64 3b 0a 20 20 63 68 61 72 20 2a 7a 55 75 rid;. char *zUu
14e0: 69 64 3b 0a 0a 20 20 69 66 28 20 74 61 67 69 64 id;.. if( tagid
14f0: 3e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 72 >0 ) return;. r
1500: 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 id = db_int(0, "
1510: 53 45 4c 45 43 54 20 70 69 64 20 46 52 4f 4d 20 SELECT pid FROM
1520: 70 6c 69 6e 6b 20 41 53 20 78 20 57 48 45 52 45 plink AS x WHERE
1530: 20 4e 4f 54 20 45 58 49 53 54 53 28 22 0a 20 20 NOT EXISTS(".
1540: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1550: 22 20 20 53 45 4c 45 43 54 20 31 20 46 52 4f 4d " SELECT 1 FROM
1560: 20 70 6c 69 6e 6b 20 57 48 45 52 45 20 63 69 64 plink WHERE cid
1570: 3d 78 2e 70 69 64 29 22 29 3b 0a 20 20 69 66 28 =x.pid)");. if(
1580: 20 72 69 64 3d 3d 30 20 29 20 72 65 74 75 72 6e rid==0 ) return
1590: 3b 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 ;.. /* Add the
15a0: 74 72 75 6e 6b 20 74 61 67 20 74 6f 20 74 68 65 trunk tag to the
15b0: 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 77 68 6f root of the who
15c0: 6c 65 20 74 72 65 65 20 2a 2f 0a 20 20 7a 55 75 le tree */. zUu
15d0: 69 64 20 3d 20 64 62 5f 74 65 78 74 28 30 2c 20 id = db_text(0,
15e0: 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f "SELECT uuid FRO
15f0: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 M blob WHERE rid
1600: 3d 25 64 22 2c 20 72 69 64 29 3b 0a 20 20 69 66 =%d", rid);. if
1610: 28 20 7a 55 75 69 64 3d 3d 30 20 29 20 72 65 74 ( zUuid==0 ) ret
1620: 75 72 6e 3b 0a 20 20 74 61 67 5f 61 64 64 5f 61 urn;. tag_add_a
1630: 72 74 69 66 61 63 74 28 22 73 79 6d 2d 22 2c 20 rtifact("sym-",
1640: 22 74 72 75 6e 6b 22 2c 20 7a 55 75 69 64 2c 20 "trunk", zUuid,
1650: 30 2c 20 32 29 3b 0a 20 20 74 61 67 5f 61 64 64 0, 2);. tag_add
1660: 5f 61 72 74 69 66 61 63 74 28 22 22 2c 20 22 62 _artifact("", "b
1670: 72 61 6e 63 68 22 2c 20 7a 55 75 69 64 2c 20 22 ranch", zUuid, "
1680: 74 72 75 6e 6b 22 2c 20 32 29 3b 0a 7d 0a 0a 2f trunk", 2);.}../
1690: 2a 0a 2a 2a 20 43 6f 72 65 20 66 75 6e 63 74 69 *.** Core functi
16a0: 6f 6e 20 74 6f 20 72 65 62 75 69 6c 64 20 74 68 on to rebuild th
16b0: 65 20 69 6e 66 6f 6d 72 61 74 69 6f 6e 20 69 6e e infomration in
16c0: 20 74 68 65 20 64 65 72 69 76 65 64 20 74 61 62 the derived tab
16d0: 6c 65 73 20 6f 66 20 61 0a 2a 2a 20 66 6f 73 73 les of a.** foss
16e0: 69 6c 20 72 65 70 6f 73 69 74 6f 72 79 20 66 72 il repository fr
16f0: 6f 6d 20 74 68 65 20 62 6c 6f 62 73 2e 20 54 68 om the blobs. Th
1700: 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 is function is s
1710: 68 61 72 65 64 20 62 65 74 77 65 65 6e 0a 2a 2a hared between.**
1720: 20 27 72 65 62 75 69 6c 64 5f 64 61 74 61 62 61 'rebuild_databa
1730: 73 65 27 20 28 27 72 65 62 75 69 6c 64 27 29 20 se' ('rebuild')
1740: 61 6e 64 20 27 72 65 63 6f 6e 73 74 72 75 63 74 and 'reconstruct
1750: 5f 63 6d 64 27 0a 2a 2a 20 28 27 72 65 63 6f 6e _cmd'.** ('recon
1760: 73 74 72 75 63 74 27 29 2c 20 62 6f 74 68 20 6f struct'), both o
1770: 66 20 77 68 69 63 68 20 68 61 76 65 20 74 6f 20 f which have to
1780: 72 65 67 65 6e 65 72 61 74 65 20 74 68 69 73 20 regenerate this
1790: 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 information.** f
17a0: 72 6f 6d 20 73 63 72 61 74 63 68 2e 0a 2a 2a 0a rom scratch..**.
17b0: 2a 2a 20 49 66 20 74 68 65 20 72 61 6e 64 6f 6d ** If the random
17c0: 69 7a 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 ize parameter is
17d0: 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 true, then the
17e0: 42 4c 4f 42 73 20 61 72 65 20 64 65 6c 69 62 65 BLOBs are delibe
17f0: 72 61 74 65 6c 79 0a 2a 2a 20 65 78 74 72 61 63 rately.** extrac
1800: 74 65 64 20 69 6e 20 61 20 72 61 6e 64 6f 6d 20 ted in a random
1810: 6f 72 64 65 72 2e 20 20 54 68 69 73 20 66 65 61 order. This fea
1820: 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 ture is used to
1830: 74 65 73 74 20 74 68 65 0a 2a 2a 20 61 62 69 6c test the.** abil
1840: 69 74 79 20 6f 66 20 66 6f 73 73 69 6c 20 74 6f ity of fossil to
1850: 20 61 63 63 65 70 74 20 72 65 63 6f 72 64 73 20 accept records
1860: 69 6e 20 61 6e 79 20 6f 72 64 65 72 20 61 6e 64 in any order and
1870: 20 73 74 69 6c 6c 0a 2a 2a 20 63 6f 6e 73 74 72 still.** constr
1880: 75 63 74 20 61 20 73 61 6e 65 20 72 65 70 6f 73 uct a sane repos
1890: 69 74 6f 72 79 2e 0a 2a 2f 0a 69 6e 74 20 72 65 itory..*/.int re
18a0: 62 75 69 6c 64 5f 64 62 28 69 6e 74 20 72 61 6e build_db(int ran
18b0: 64 6f 6d 69 7a 65 2c 20 69 6e 74 20 64 6f 4f 75 domize, int doOu
18c0: 74 29 7b 0a 20 20 53 74 6d 74 20 73 3b 0a 20 20 t){. Stmt s;.
18d0: 69 6e 74 20 65 72 72 43 6e 74 20 3d 20 30 3b 0a int errCnt = 0;.
18e0: 20 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 3b 0a char *zTable;.
18f0: 0a 20 20 62 61 67 5f 69 6e 69 74 28 26 62 61 67 . bag_init(&bag
1900: 44 6f 6e 65 29 3b 0a 20 20 74 74 79 4f 75 74 70 Done);. ttyOutp
1910: 75 74 20 3d 20 64 6f 4f 75 74 3b 0a 20 20 70 72 ut = doOut;. pr
1920: 6f 63 65 73 73 43 6e 74 20 3d 20 30 3b 0a 20 20 ocessCnt = 0;.
1930: 69 66 20 28 21 67 2e 66 51 75 69 65 74 29 20 7b if (!g.fQuiet) {
1940: 0a 20 20 20 20 70 72 69 6e 74 66 28 22 30 20 28 . printf("0 (
1950: 30 25 25 29 2e 2e 2e 5c 72 22 29 3b 0a 20 20 20 0%%)...\r");.
1960: 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b fflush(stdout);
1970: 0a 20 20 7d 0a 20 20 64 62 5f 6d 75 6c 74 69 5f . }. db_multi_
1980: 65 78 65 63 28 7a 53 63 68 65 6d 61 55 70 64 61 exec(zSchemaUpda
1990: 74 65 73 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b tes);. for(;;){
19a0: 0a 20 20 20 20 7a 54 61 62 6c 65 20 3d 20 64 62 . zTable = db
19b0: 5f 74 65 78 74 28 30 2c 0a 20 20 20 20 20 20 20 _text(0,.
19c0: 22 53 45 4c 45 43 54 20 6e 61 6d 65 20 46 52 4f "SELECT name FRO
19d0: 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 M sqlite_master"
19e0: 0a 20 20 20 20 20 20 20 22 20 57 48 45 52 45 20 . " WHERE
19f0: 74 79 70 65 3d 27 74 61 62 6c 65 27 22 0a 20 20 type='table'".
1a00: 20 20 20 20 20 22 20 41 4e 44 20 6e 61 6d 65 20 " AND name
1a10: 4e 4f 54 20 49 4e 20 28 27 62 6c 6f 62 27 2c 27 NOT IN ('blob','
1a20: 64 65 6c 74 61 27 2c 27 72 63 76 66 72 6f 6d 27 delta','rcvfrom'
1a30: 2c 27 75 73 65 72 27 2c 22 0a 20 20 20 20 20 20 ,'user',".
1a40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a50: 20 20 20 22 27 63 6f 6e 66 69 67 27 2c 27 73 68 "'config','sh
1a60: 75 6e 27 2c 27 70 72 69 76 61 74 65 27 2c 27 72 un','private','r
1a70: 65 70 6f 72 74 66 6d 74 27 2c 22 0a 20 20 20 20 eportfmt',".
1a80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a90: 20 20 20 20 20 22 27 63 6f 6e 63 65 61 6c 65 64 "'concealed
1aa0: 27 29 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 ')". );. i
1ab0: 66 28 20 7a 54 61 62 6c 65 3d 3d 30 20 29 20 62 f( zTable==0 ) b
1ac0: 72 65 61 6b 3b 0a 20 20 20 20 64 62 5f 6d 75 6c reak;. db_mul
1ad0: 74 69 5f 65 78 65 63 28 22 44 52 4f 50 20 54 41 ti_exec("DROP TA
1ae0: 42 4c 45 20 25 51 22 2c 20 7a 54 61 62 6c 65 29 BLE %Q", zTable)
1af0: 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 61 62 6c ;. free(zTabl
1b00: 65 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 6d 75 6c e);. }. db_mul
1b10: 74 69 5f 65 78 65 63 28 7a 52 65 70 6f 73 69 74 ti_exec(zReposit
1b20: 6f 72 79 53 63 68 65 6d 61 32 29 3b 0a 20 20 74 orySchema2);. t
1b30: 69 63 6b 65 74 5f 63 72 65 61 74 65 5f 74 61 62 icket_create_tab
1b40: 6c 65 28 30 29 3b 0a 20 20 73 68 75 6e 5f 61 72 le(0);. shun_ar
1b50: 74 69 66 61 63 74 73 28 29 3b 0a 0a 20 20 64 62 tifacts();.. db
1b60: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 _multi_exec(.
1b70: 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 75 "INSERT INTO u
1b80: 6e 63 6c 75 73 74 65 72 65 64 22 0a 20 20 20 20 nclustered".
1b90: 20 22 20 53 45 4c 45 43 54 20 72 69 64 20 46 52 " SELECT rid FR
1ba0: 4f 4d 20 62 6c 6f 62 20 45 58 43 45 50 54 20 53 OM blob EXCEPT S
1bb0: 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 70 ELECT rid FROM p
1bc0: 72 69 76 61 74 65 22 0a 20 20 29 3b 0a 20 20 64 rivate". );. d
1bd0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 b_multi_exec(.
1be0: 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 "DELETE FROM
1bf0: 75 6e 63 6c 75 73 74 65 72 65 64 22 0a 20 20 20 unclustered".
1c00: 20 20 22 20 57 48 45 52 45 20 72 69 64 20 49 4e " WHERE rid IN
1c10: 20 28 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f (SELECT rid FRO
1c20: 4d 20 73 68 75 6e 20 4a 4f 49 4e 20 62 6c 6f 62 M shun JOIN blob
1c30: 20 55 53 49 4e 47 28 75 75 69 64 29 29 22 0a 20 USING(uuid))".
1c40: 20 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 );. db_multi_e
1c50: 78 65 63 28 0a 20 20 20 20 22 44 45 4c 45 54 45 xec(. "DELETE
1c60: 20 46 52 4f 4d 20 63 6f 6e 66 69 67 20 57 48 45 FROM config WHE
1c70: 52 45 20 6e 61 6d 65 20 49 4e 20 28 27 72 65 6d RE name IN ('rem
1c80: 6f 74 65 2d 63 6f 64 65 27 2c 20 27 72 65 6d 6f ote-code', 'remo
1c90: 74 65 2d 6d 61 78 69 64 27 29 22 0a 20 20 29 3b te-maxid')". );
1ca0: 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 3d 20 64 . totalSize = d
1cb0: 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 b_int(0, "SELECT
1cc0: 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 62 count(*) FROM b
1cd0: 6c 6f 62 22 29 3b 0a 20 20 64 62 5f 70 72 65 70 lob");. db_prep
1ce0: 61 72 65 28 26 73 2c 0a 20 20 20 20 20 22 53 45 are(&s,. "SE
1cf0: 4c 45 43 54 20 72 69 64 2c 20 73 69 7a 65 20 46 LECT rid, size F
1d00: 52 4f 4d 20 62 6c 6f 62 22 0a 20 20 20 20 20 22 ROM blob". "
1d10: 20 57 48 45 52 45 20 4e 4f 54 20 45 58 49 53 54 WHERE NOT EXIST
1d20: 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 S(SELECT 1 FROM
1d30: 73 68 75 6e 20 57 48 45 52 45 20 75 75 69 64 3d shun WHERE uuid=
1d40: 62 6c 6f 62 2e 75 75 69 64 29 22 0a 20 20 20 20 blob.uuid)".
1d50: 20 22 20 20 20 41 4e 44 20 4e 4f 54 20 45 58 49 " AND NOT EXI
1d60: 53 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f STS(SELECT 1 FRO
1d70: 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20 72 69 M delta WHERE ri
1d80: 64 3d 62 6c 6f 62 2e 72 69 64 29 22 0a 20 20 29 d=blob.rid)". )
1d90: 3b 0a 20 20 6d 61 6e 69 66 65 73 74 5f 63 72 6f ;. manifest_cro
1da0: 73 73 6c 69 6e 6b 5f 62 65 67 69 6e 28 29 3b 0a sslink_begin();.
1db0: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 while( db_step
1dc0: 28 26 73 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 (&s)==SQLITE_ROW
1dd0: 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 69 64 20 ){. int rid
1de0: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 = db_column_int(
1df0: 26 73 2c 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 &s, 0);. int
1e00: 73 69 7a 65 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e size = db_column
1e10: 5f 69 6e 74 28 26 73 2c 20 31 29 3b 0a 20 20 20 _int(&s, 1);.
1e20: 20 69 66 28 20 73 69 7a 65 3e 3d 30 20 29 7b 0a if( size>=0 ){.
1e30: 20 20 20 20 20 20 42 6c 6f 62 20 63 6f 6e 74 65 Blob conte
1e40: 6e 74 3b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e nt;. conten
1e50: 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 t_get(rid, &cont
1e60: 65 6e 74 29 3b 0a 20 20 20 20 20 20 72 65 62 75 ent);. rebu
1e70: 69 6c 64 5f 73 74 65 70 28 72 69 64 2c 20 73 69 ild_step(rid, si
1e80: 7a 65 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 ze, &content);.
1e90: 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62 5f 66 69 }. }. db_fi
1ea0: 6e 61 6c 69 7a 65 28 26 73 29 3b 0a 20 20 64 62 nalize(&s);. db
1eb0: 5f 70 72 65 70 61 72 65 28 26 73 2c 0a 20 20 20 _prepare(&s,.
1ec0: 20 20 22 53 45 4c 45 43 54 20 72 69 64 2c 20 73 "SELECT rid, s
1ed0: 69 7a 65 20 46 52 4f 4d 20 62 6c 6f 62 22 0a 20 ize FROM blob".
1ee0: 20 20 20 20 22 20 57 48 45 52 45 20 4e 4f 54 20 " WHERE NOT
1ef0: 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 EXISTS(SELECT 1
1f00: 46 52 4f 4d 20 73 68 75 6e 20 57 48 45 52 45 20 FROM shun WHERE
1f10: 75 75 69 64 3d 62 6c 6f 62 2e 75 75 69 64 29 22 uuid=blob.uuid)"
1f20: 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 64 . );. while( d
1f30: 62 5f 73 74 65 70 28 26 73 29 3d 3d 53 51 4c 49 b_step(&s)==SQLI
1f40: 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 69 6e TE_ROW ){. in
1f50: 74 20 72 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d t rid = db_colum
1f60: 6e 5f 69 6e 74 28 26 73 2c 20 30 29 3b 0a 20 20 n_int(&s, 0);.
1f70: 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 64 62 5f int size = db_
1f80: 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 73 2c 20 31 column_int(&s, 1
1f90: 29 3b 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3e );. if( size>
1fa0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 =0 ){. if(
1fb0: 21 62 61 67 5f 66 69 6e 64 28 26 62 61 67 44 6f !bag_find(&bagDo
1fc0: 6e 65 2c 20 72 69 64 29 20 29 7b 0a 20 20 20 20 ne, rid) ){.
1fd0: 20 20 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 Blob content
1fe0: 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e ;. conten
1ff0: 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 t_get(rid, &cont
2000: 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 ent);. re
2010: 62 75 69 6c 64 5f 73 74 65 70 28 72 69 64 2c 20 build_step(rid,
2020: 73 69 7a 65 2c 20 26 63 6f 6e 74 65 6e 74 29 3b size, &content);
2030: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
2040: 73 65 7b 0a 20 20 20 20 20 20 64 62 5f 6d 75 6c se{. db_mul
2050: 74 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54 20 ti_exec("INSERT
2060: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 OR IGNORE INTO p
2070: 68 61 6e 74 6f 6d 20 56 41 4c 55 45 53 28 25 64 hantom VALUES(%d
2080: 29 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 20 )", rid);.
2090: 72 65 62 75 69 6c 64 5f 73 74 65 70 5f 64 6f 6e rebuild_step_don
20a0: 65 28 72 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 e(rid);. }.
20b0: 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 }. db_finalize(
20c0: 26 73 29 3b 0a 20 20 6d 61 6e 69 66 65 73 74 5f &s);. manifest_
20d0: 63 72 6f 73 73 6c 69 6e 6b 5f 65 6e 64 28 29 3b crosslink_end();
20e0: 0a 20 20 72 65 62 75 69 6c 64 5f 74 61 67 5f 74 . rebuild_tag_t
20f0: 72 75 6e 6b 28 29 3b 0a 20 20 69 66 28 21 67 2e runk();. if(!g.
2100: 66 51 75 69 65 74 20 26 26 20 74 74 79 4f 75 74 fQuiet && ttyOut
2110: 70 75 74 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 put ){. print
2120: 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 72 f("\n");. }. r
2130: 65 74 75 72 6e 20 65 72 72 43 6e 74 3b 0a 7d 0a eturn errCnt;.}.
2140: 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 ./*.** COMMAND:
2150: 20 72 65 62 75 69 6c 64 0a 2a 2a 0a 2a 2a 20 55 rebuild.**.** U
2160: 73 61 67 65 3a 20 25 66 6f 73 73 69 6c 20 72 65 sage: %fossil re
2170: 62 75 69 6c 64 20 3f 52 45 50 4f 53 49 54 4f 52 build ?REPOSITOR
2180: 59 3f 0a 2a 2a 0a 2a 2a 20 52 65 63 6f 6e 73 74 Y?.**.** Reconst
2190: 72 75 63 74 20 74 68 65 20 6e 61 6d 65 64 20 72 ruct the named r
21a0: 65 70 6f 73 69 74 6f 72 79 20 64 61 74 61 62 61 epository databa
21b0: 73 65 20 66 72 6f 6d 20 74 68 65 20 63 6f 72 65 se from the core
21c0: 0a 2a 2a 20 72 65 63 6f 72 64 73 2e 20 20 52 75 .** records. Ru
21d0: 6e 20 74 68 69 73 20 63 6f 6d 6d 61 6e 64 20 61 n this command a
21e0: 66 74 65 72 20 75 70 64 61 74 69 6e 67 20 74 68 fter updating th
21f0: 65 20 66 6f 73 73 69 6c 0a 2a 2a 20 65 78 65 63 e fossil.** exec
2200: 75 74 61 62 6c 65 20 69 6e 20 61 20 77 61 79 20 utable in a way
2210: 74 68 61 74 20 63 68 61 6e 67 65 73 20 74 68 65 that changes the
2220: 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
2230: 2e 0a 2a 2f 0a 76 6f 69 64 20 72 65 62 75 69 6c ..*/.void rebuil
2240: 64 5f 64 61 74 61 62 61 73 65 28 76 6f 69 64 29 d_database(void)
2250: 7b 0a 20 20 69 6e 74 20 66 6f 72 63 65 46 6c 61 {. int forceFla
2260: 67 3b 0a 20 20 69 6e 74 20 72 61 6e 64 6f 6d 69 g;. int randomi
2270: 7a 65 46 6c 61 67 3b 0a 20 20 69 6e 74 20 65 72 zeFlag;. int er
2280: 72 43 6e 74 3b 0a 0a 20 20 66 6f 72 63 65 46 6c rCnt;.. forceFl
2290: 61 67 20 3d 20 66 69 6e 64 5f 6f 70 74 69 6f 6e ag = find_option
22a0: 28 22 66 6f 72 63 65 22 2c 22 66 22 2c 30 29 21 ("force","f",0)!
22b0: 3d 30 3b 0a 20 20 72 61 6e 64 6f 6d 69 7a 65 46 =0;. randomizeF
22c0: 6c 61 67 20 3d 20 66 69 6e 64 5f 6f 70 74 69 6f lag = find_optio
22d0: 6e 28 22 72 61 6e 64 6f 6d 69 7a 65 22 2c 20 30 n("randomize", 0
22e0: 2c 20 30 29 21 3d 30 3b 0a 20 20 69 66 28 20 67 , 0)!=0;. if( g
22f0: 2e 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 .argc==3 ){.
2300: 64 62 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74 6f db_open_reposito
2310: 72 79 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 ry(g.argv[2]);.
2320: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 5f 66 }else{. db_f
2330: 69 6e 64 5f 61 6e 64 5f 6f 70 65 6e 5f 72 65 70 ind_and_open_rep
2340: 6f 73 69 74 6f 72 79 28 31 29 3b 0a 20 20 20 20 ository(1);.
2350: 69 66 28 20 67 2e 61 72 67 63 21 3d 32 20 29 7b if( g.argc!=2 ){
2360: 0a 20 20 20 20 20 20 75 73 61 67 65 28 22 3f 52 . usage("?R
2370: 45 50 4f 53 49 54 4f 52 59 2d 46 49 4c 45 4e 41 EPOSITORY-FILENA
2380: 4d 45 3f 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ME?");. }.
2390: 20 64 62 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 20 db_close();.
23a0: 20 64 62 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74 db_open_reposit
23b0: 6f 72 79 28 67 2e 7a 52 65 70 6f 73 69 74 6f 72 ory(g.zRepositor
23c0: 79 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 64 62 yName);. }. db
23d0: 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 _begin_transacti
23e0: 6f 6e 28 29 3b 0a 20 20 74 74 79 4f 75 74 70 75 on();. ttyOutpu
23f0: 74 20 3d 20 31 3b 0a 20 20 65 72 72 43 6e 74 20 t = 1;. errCnt
2400: 3d 20 72 65 62 75 69 6c 64 5f 64 62 28 72 61 6e = rebuild_db(ran
2410: 64 6f 6d 69 7a 65 46 6c 61 67 2c 20 31 29 3b 0a domizeFlag, 1);.
2420: 20 20 69 66 28 20 65 72 72 43 6e 74 20 26 26 20 if( errCnt &&
2430: 21 66 6f 72 63 65 46 6c 61 67 20 29 7b 0a 20 20 !forceFlag ){.
2440: 20 20 70 72 69 6e 74 66 28 22 25 64 20 65 72 72 printf("%d err
2450: 6f 72 73 2e 20 52 6f 6c 6c 69 6e 67 20 62 61 63 ors. Rolling bac
2460: 6b 20 63 68 61 6e 67 65 73 2e 20 55 73 65 20 2d k changes. Use -
2470: 2d 66 6f 72 63 65 20 74 6f 20 66 6f 72 63 65 20 -force to force
2480: 61 20 63 6f 6d 6d 69 74 2e 5c 6e 22 2c 0a 20 20 a commit.\n",.
2490: 20 20 20 20 20 20 20 20 20 20 65 72 72 43 6e 74 errCnt
24a0: 29 3b 0a 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 );. db_end_tr
24b0: 61 6e 73 61 63 74 69 6f 6e 28 31 29 3b 0a 20 20 ansaction(1);.
24c0: 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 5f 65 6e }else{. db_en
24d0: 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 d_transaction(0)
24e0: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
24f0: 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 64 65 OMMAND: test-de
2500: 74 61 63 68 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 tach.**.** Chang
2510: 65 20 74 68 65 20 70 72 6f 6a 65 63 74 2d 63 6f e the project-co
2520: 64 65 20 61 6e 64 20 6d 61 6b 65 20 6f 74 68 65 de and make othe
2530: 72 20 63 68 61 6e 67 65 73 20 69 6e 20 6f 72 64 r changes in ord
2540: 65 72 20 74 6f 20 70 72 65 76 65 6e 74 0a 2a 2a er to prevent.**
2550: 20 74 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20 the repository
2560: 66 72 6f 6d 20 65 76 65 72 20 61 67 61 69 6e 20 from ever again
2570: 70 75 73 68 69 6e 67 20 6f 72 20 70 75 6c 6c 69 pushing or pulli
2580: 6e 67 20 74 6f 20 6f 74 68 65 72 0a 2a 2a 20 72 ng to other.** r
2590: 65 70 6f 73 69 74 6f 72 69 65 73 2e 20 20 55 73 epositories. Us
25a0: 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 22 ed to create a "
25b0: 74 65 73 74 22 20 72 65 70 6f 73 69 74 6f 72 79 test" repository
25c0: 20 66 6f 72 20 64 65 76 65 6c 6f 70 6d 65 6e 74 for development
25d0: 0a 2a 2a 20 74 65 73 74 69 6e 67 20 62 79 20 63 .** testing by c
25e0: 6c 6f 6e 69 6e 67 20 61 20 77 6f 72 6b 69 6e 67 loning a working
25f0: 20 70 72 6f 6a 65 63 74 20 72 65 70 6f 73 69 74 project reposit
2600: 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 74 65 73 ory..*/.void tes
2610: 74 5f 64 65 74 61 63 68 5f 63 6d 64 28 76 6f 69 t_detach_cmd(voi
2620: 64 29 7b 0a 20 20 64 62 5f 66 69 6e 64 5f 61 6e d){. db_find_an
2630: 64 5f 6f 70 65 6e 5f 72 65 70 6f 73 69 74 6f 72 d_open_repositor
2640: 79 28 31 29 3b 0a 20 20 64 62 5f 62 65 67 69 6e y(1);. db_begin
2650: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a _transaction();.
2660: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 db_multi_exec(
2670: 0a 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f . "DELETE FRO
2680: 4d 20 63 6f 6e 66 69 67 20 57 48 45 52 45 20 6e M config WHERE n
2690: 61 6d 65 3d 27 6c 61 73 74 2d 73 79 6e 63 2d 75 ame='last-sync-u
26a0: 72 6c 27 3b 22 0a 20 20 20 20 22 55 50 44 41 54 rl';". "UPDAT
26b0: 45 20 63 6f 6e 66 69 67 20 53 45 54 20 76 61 6c E config SET val
26c0: 75 65 3d 6c 6f 77 65 72 28 68 65 78 28 72 61 6e ue=lower(hex(ran
26d0: 64 6f 6d 62 6c 6f 62 28 32 30 29 29 29 22 0a 20 domblob(20)))".
26e0: 20 20 20 22 20 57 48 45 52 45 20 6e 61 6d 65 3d " WHERE name=
26f0: 27 70 72 6f 6a 65 63 74 2d 63 6f 64 65 27 3b 22 'project-code';"
2700: 0a 20 20 20 20 22 55 50 44 41 54 45 20 63 6f 6e . "UPDATE con
2710: 66 69 67 20 53 45 54 20 76 61 6c 75 65 3d 27 64 fig SET value='d
2720: 65 74 61 63 68 65 64 2d 27 20 7c 7c 20 76 61 6c etached-' || val
2730: 75 65 22 0a 20 20 20 20 22 20 57 48 45 52 45 20 ue". " WHERE
2740: 6e 61 6d 65 3d 27 70 72 6f 6a 65 63 74 2d 6e 61 name='project-na
2750: 6d 65 27 20 41 4e 44 20 76 61 6c 75 65 20 4e 4f me' AND value NO
2760: 54 20 47 4c 4f 42 20 27 64 65 74 61 63 68 65 64 T GLOB 'detached
2770: 2d 2a 27 3b 22 0a 20 20 29 3b 0a 20 20 64 62 5f -*';". );. db_
2780: 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 end_transaction(
2790: 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 0);.}../*.** COM
27a0: 4d 41 4e 44 3a 20 73 63 72 75 62 0a 2a 2a 20 25 MAND: scrub.** %
27b0: 66 6f 73 73 69 6c 20 73 63 72 75 62 20 5b 2d 2d fossil scrub [--
27c0: 76 65 72 69 6c 79 5d 20 5b 2d 2d 66 6f 72 63 65 verily] [--force
27d0: 5d 20 5b 52 45 50 4f 53 49 54 4f 52 59 5d 0a 2a ] [REPOSITORY].*
27e0: 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d 6d 61 6e 64 *.** The command
27f0: 20 72 65 6d 6f 76 65 73 20 73 65 6e 73 69 74 69 removes sensiti
2800: 76 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 ve information (
2810: 73 75 63 68 20 61 73 20 70 61 73 73 77 6f 72 64 such as password
2820: 73 29 20 66 72 6f 6d 20 61 0a 2a 2a 20 72 65 70 s) from a.** rep
2830: 6f 73 69 74 6f 72 79 20 73 6f 20 74 68 61 74 20 ository so that
2840: 74 68 65 20 72 65 73 70 6f 73 69 74 6f 72 79 20 the respository
2850: 63 61 6e 20 62 65 20 73 65 6e 74 20 74 6f 20 61 can be sent to a
2860: 6e 20 75 6e 74 72 75 73 74 65 64 20 72 65 61 64 n untrusted read
2870: 65 72 2e 0a 2a 2a 0a 2a 2a 20 42 79 20 64 65 66 er..**.** By def
2880: 61 75 6c 74 2c 20 6f 6e 6c 79 20 70 61 73 73 77 ault, only passw
2890: 6f 72 64 73 20 61 72 65 20 72 65 6d 6f 76 65 64 ords are removed
28a0: 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 . However, if t
28b0: 68 65 20 2d 2d 76 65 72 69 6c 79 20 6f 70 74 69 he --verily opti
28c0: 6f 6e 0a 2a 2a 20 69 73 20 61 64 64 65 64 2c 20 on.** is added,
28d0: 74 68 65 6e 20 70 72 69 76 61 74 65 20 62 72 61 then private bra
28e0: 6e 63 68 65 73 2c 20 63 6f 6e 63 65 61 6c 65 64 nches, concealed
28f0: 20 65 6d 61 69 6c 20 61 64 64 72 65 73 73 65 73 email addresses
2900: 2c 20 49 50 0a 2a 2a 20 61 64 64 72 65 73 73 65 , IP.** addresse
2910: 73 20 6f 66 20 63 6f 72 72 65 73 70 6f 6e 64 65 s of corresponde
2920: 6e 74 73 2c 20 61 6e 64 20 73 69 6d 69 6c 61 72 nts, and similar
2930: 20 70 72 69 76 61 63 79 2d 73 65 6e 73 69 74 69 privacy-sensiti
2940: 76 65 20 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 ve fields.** are
2950: 20 61 6c 73 6f 20 70 75 72 67 65 64 2e 0a 2a 2a also purged..**
2960: 0a 2a 2a 20 54 68 69 73 20 63 6f 6d 6d 61 6e 64 .** This command
2970: 20 70 65 72 6d 61 6e 65 6e 74 6c 79 20 64 65 6c permanently del
2980: 65 74 65 73 20 74 68 65 20 73 63 72 75 62 62 65 etes the scrubbe
2990: 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 d information.
29a0: 54 68 65 20 65 66 66 65 63 74 73 0a 2a 2a 20 6f The effects.** o
29b0: 66 20 74 68 69 73 20 63 6f 6d 6d 61 6e 64 20 61 f this command a
29c0: 72 65 20 69 72 72 65 76 65 72 73 69 62 6c 65 2e re irreversible.
29d0: 20 20 55 73 65 20 77 69 74 68 20 63 61 75 74 69 Use with cauti
29e0: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 73 on..**.** The us
29f0: 65 72 20 69 73 20 70 72 6f 6d 70 74 65 64 20 74 er is prompted t
2a00: 6f 20 63 6f 6e 66 69 72 6d 20 74 68 65 20 73 63 o confirm the sc
2a10: 72 75 62 20 75 6e 6c 65 73 73 20 74 68 65 20 2d rub unless the -
2a20: 2d 66 6f 72 63 65 20 6f 70 74 69 6f 6e 0a 2a 2a -force option.**
2a30: 20 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 76 6f 69 is used..*/.voi
2a40: 64 20 73 63 72 75 62 5f 63 6d 64 28 76 6f 69 64 d scrub_cmd(void
2a50: 29 7b 0a 20 20 69 6e 74 20 62 56 65 72 69 6c 79 ){. int bVerily
2a60: 20 3d 20 66 69 6e 64 5f 6f 70 74 69 6f 6e 28 22 = find_option("
2a70: 76 65 72 69 6c 79 22 2c 30 2c 30 29 21 3d 30 3b verily",0,0)!=0;
2a80: 0a 20 20 69 6e 74 20 62 46 6f 72 63 65 20 3d 20 . int bForce =
2a90: 66 69 6e 64 5f 6f 70 74 69 6f 6e 28 22 66 6f 72 find_option("for
2aa0: 63 65 22 2c 20 22 66 22 2c 20 30 29 21 3d 30 3b ce", "f", 0)!=0;
2ab0: 0a 20 20 69 6e 74 20 62 4e 65 65 64 52 65 62 75 . int bNeedRebu
2ac0: 69 6c 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 67 ild = 0;. if( g
2ad0: 2e 61 72 67 63 21 3d 32 20 26 26 20 67 2e 61 72 .argc!=2 && g.ar
2ae0: 67 63 21 3d 33 20 29 20 75 73 61 67 65 28 22 3f gc!=3 ) usage("?
2af0: 52 45 50 4f 53 49 54 4f 52 59 3f 22 29 3b 0a 20 REPOSITORY?");.
2b00: 20 69 66 28 20 67 2e 61 72 67 63 3d 3d 32 20 29 if( g.argc==2 )
2b10: 7b 0a 20 20 20 20 64 62 5f 6d 75 73 74 5f 62 65 {. db_must_be
2b20: 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a _within_tree();.
2b30: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 5f }else{. db_
2b40: 6f 70 65 6e 5f 72 65 70 6f 73 69 74 6f 72 79 28 open_repository(
2b50: 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 7d 0a g.argv[2]);. }.
2b60: 20 20 69 66 28 20 21 62 46 6f 72 63 65 20 29 7b if( !bForce ){
2b70: 0a 20 20 20 20 42 6c 6f 62 20 61 6e 73 3b 0a 20 . Blob ans;.
2b80: 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 61 6e blob_zero(&an
2b90: 73 29 3b 0a 20 20 20 20 70 72 6f 6d 70 74 5f 75 s);. prompt_u
2ba0: 73 65 72 28 22 53 63 72 75 62 62 69 6e 67 20 74 ser("Scrubbing t
2bb0: 68 65 20 72 65 70 6f 73 69 74 6f 72 79 20 77 69 he repository wi
2bc0: 6c 6c 20 70 65 72 6d 61 6e 65 6e 74 6c 79 20 72 ll permanently r
2bd0: 65 6d 6f 76 65 20 75 73 65 72 5c 6e 22 0a 20 20 emove user\n".
2be0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 70 "p
2bf0: 61 73 73 77 6f 72 64 73 20 61 6e 64 20 6f 74 68 asswords and oth
2c00: 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 er information.
2c10: 43 68 61 6e 67 65 73 20 63 61 6e 6e 6f 74 20 62 Changes cannot b
2c20: 65 20 75 6e 64 6f 6e 65 2e 5c 6e 22 0a 20 20 20 e undone.\n".
2c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 22 43 6f "Co
2c40: 6e 74 69 6e 75 65 20 28 79 2f 4e 29 3f 20 22 2c ntinue (y/N)? ",
2c50: 20 26 61 6e 73 29 3b 0a 20 20 20 20 69 66 28 20 &ans);. if(
2c60: 62 6c 6f 62 5f 73 74 72 28 26 61 6e 73 29 5b 30 blob_str(&ans)[0
2c70: 5d 21 3d 27 79 27 20 29 7b 0a 20 20 20 20 20 20 ]!='y' ){.
2c80: 65 78 69 74 28 31 29 3b 0a 20 20 20 20 7d 0a 20 exit(1);. }.
2c90: 20 7d 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 }. db_begin_tr
2ca0: 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 64 ansaction();. d
2cb0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 b_multi_exec(.
2cc0: 20 20 22 55 50 44 41 54 45 20 75 73 65 72 20 53 "UPDATE user S
2cd0: 45 54 20 70 77 3d 27 27 3b 22 0a 20 20 20 20 22 ET pw='';". "
2ce0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 6f 6e 66 DELETE FROM conf
2cf0: 69 67 20 57 48 45 52 45 20 6e 61 6d 65 3d 27 6c ig WHERE name='l
2d00: 61 73 74 2d 73 79 6e 63 2d 75 72 6c 27 3b 22 0a ast-sync-url';".
2d10: 20 20 29 3b 0a 20 20 69 66 28 20 62 56 65 72 69 );. if( bVeri
2d20: 6c 79 20 29 7b 0a 20 20 20 20 62 4e 65 65 64 52 ly ){. bNeedR
2d30: 65 62 75 69 6c 64 20 3d 20 64 62 5f 65 78 69 73 ebuild = db_exis
2d40: 74 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f ts("SELECT 1 FRO
2d50: 4d 20 70 72 69 76 61 74 65 22 29 3b 0a 20 20 20 M private");.
2d60: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
2d70: 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 "DELETE FR
2d80: 4f 4d 20 63 6f 6e 63 65 61 6c 65 64 3b 22 0a 20 OM concealed;".
2d90: 20 20 20 20 20 22 55 50 44 41 54 45 20 72 63 76 "UPDATE rcv
2da0: 66 72 6f 6d 20 53 45 54 20 69 70 61 64 64 72 3d from SET ipaddr=
2db0: 27 75 6e 6b 6e 6f 77 6e 27 3b 22 0a 20 20 20 20 'unknown';".
2dc0: 20 20 22 55 50 44 41 54 45 20 75 73 65 72 20 53 "UPDATE user S
2dd0: 45 54 20 70 68 6f 74 6f 3d 4e 55 4c 4c 2c 20 69 ET photo=NULL, i
2de0: 6e 66 6f 3d 27 27 3b 22 0a 20 20 20 20 20 20 22 nfo='';". "
2df0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 73 68 75 6e INSERT INTO shun
2e00: 20 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f SELECT uuid FRO
2e10: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 M blob WHERE rid
2e20: 20 49 4e 20 70 72 69 76 61 74 65 3b 22 0a 20 20 IN private;".
2e30: 20 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 );. }. if( !
2e40: 62 4e 65 65 64 52 65 62 75 69 6c 64 20 29 7b 0a bNeedRebuild ){.
2e50: 20 20 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 db_end_trans
2e60: 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 20 20 64 action(0);. d
2e70: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 56 41 b_multi_exec("VA
2e80: 43 55 55 4d 3b 22 29 3b 0a 20 20 7d 65 6c 73 65 CUUM;");. }else
2e90: 7b 0a 20 20 20 20 72 65 62 75 69 6c 64 5f 64 62 {. rebuild_db
2ea0: 28 30 2c 20 31 29 3b 0a 20 20 20 20 64 62 5f 65 (0, 1);. db_e
2eb0: 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 nd_transaction(0
2ec0: 29 3b 0a 20 20 7d 0a 7d 0a );. }.}.