Hex Artifact Content
Not logged in

Artifact cbdf43d0952b34fc46a60812553f9baf79c1583e:

File src/update.c part of check-in [76f169fca6] - Detect when the check-out contains missing files and filesystem objects that ought to be files but are not. Issue reasonable warnings. by drh on 2009-12-18 00:29:51.

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 6d 65 72 67 65 20 74 68 65  sed to merge the
0390: 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20   changes in the 
03a0: 63 75 72 72 65 6e 74 0a 2a 2a 20 63 68 65 63 6b  current.** check
03b0: 6f 75 74 20 69 6e 74 6f 20 61 20 64 69 66 66 65  out into a diffe
03c0: 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 61 6e 64  rent version and
03d0: 20 73 77 69 74 63 68 20 74 6f 20 74 68 61 74 20   switch to that 
03e0: 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 23 69 6e 63  version..*/.#inc
03f0: 6c 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a  lude "config.h".
0400: 23 69 6e 63 6c 75 64 65 20 22 75 70 64 61 74 65  #include "update
0410: 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73  .h".#include <as
0420: 73 65 72 74 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 52  sert.h>../*.** R
0430: 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 72  eturn true if ar
0440: 74 69 66 61 63 74 20 72 69 64 20 69 73 20 61 20  tifact rid is a 
0450: 76 65 72 73 69 6f 6e 0a 2a 2f 0a 69 6e 74 20 69  version.*/.int i
0460: 73 5f 61 5f 76 65 72 73 69 6f 6e 28 69 6e 74 20  s_a_version(int 
0470: 72 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 64  rid){.  return d
0480: 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43 54  b_exists("SELECT
0490: 20 31 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 57 48   1 FROM plink WH
04a0: 45 52 45 20 63 69 64 3d 25 64 22 2c 20 72 69 64  ERE cid=%d", rid
04b0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d  );.}../*.** COMM
04c0: 41 4e 44 3a 20 75 70 64 61 74 65 0a 2a 2a 0a 2a  AND: update.**.*
04d0: 2a 20 55 73 61 67 65 3a 20 25 66 6f 73 73 69 6c  * Usage: %fossil
04e0: 20 75 70 64 61 74 65 20 3f 56 45 52 53 49 4f 4e   update ?VERSION
04f0: 3f 20 3f 46 49 4c 45 53 2e 2e 2e 3f 0a 2a 2a 0a  ? ?FILES...?.**.
0500: 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 65  ** Change the ve
0510: 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72  rsion of the cur
0520: 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 20 74 6f  rent checkout to
0530: 20 56 45 52 53 49 4f 4e 2e 20 20 41 6e 79 20 75   VERSION.  Any u
0540: 6e 63 6f 6d 6d 69 74 74 65 64 0a 2a 2a 20 63 68  ncommitted.** ch
0550: 61 6e 67 65 73 20 61 72 65 20 72 65 74 61 69 6e  anges are retain
0560: 65 64 20 61 6e 64 20 61 70 70 6c 69 65 64 20 74  ed and applied t
0570: 6f 20 74 68 65 20 6e 65 77 20 63 68 65 63 6b 6f  o the new checko
0580: 75 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 45  ut..**.** The VE
0590: 52 53 49 4f 4e 20 61 72 67 75 6d 65 6e 74 20 63  RSION argument c
05a0: 61 6e 20 62 65 20 61 20 73 70 65 63 69 66 69 63  an be a specific
05b0: 20 76 65 72 73 69 6f 6e 20 6f 72 20 74 61 67 20   version or tag 
05c0: 6f 72 20 62 72 61 6e 63 68 20 6e 61 6d 65 2e 0a  or branch name..
05d0: 2a 2a 20 49 66 20 74 68 65 20 56 45 52 53 49 4f  ** If the VERSIO
05e0: 4e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6d  N argument is om
05f0: 69 74 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20  itted, then the 
0600: 6c 65 61 66 20 6f 66 20 74 68 65 20 74 68 65 20  leaf of the the 
0610: 73 75 62 74 72 65 65 0a 2a 2a 20 74 68 61 74 20  subtree.** that 
0620: 62 65 67 69 6e 73 20 61 74 20 74 68 65 20 63 75  begins at the cu
0630: 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 69 73  rrent version is
0640: 20 75 73 65 64 2c 20 69 66 20 74 68 65 72 65 20   used, if there 
0650: 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65  is only a single
0660: 0a 2a 2a 20 6c 65 61 66 2e 20 20 56 45 52 53 49  .** leaf.  VERSI
0670: 4f 4e 20 63 61 6e 20 61 6c 73 6f 20 62 65 20 22  ON can also be "
0680: 63 75 72 72 65 6e 74 22 20 74 6f 20 73 65 6c 65  current" to sele
0690: 63 74 20 74 68 65 20 6c 65 61 66 20 6f 66 20 74  ct the leaf of t
06a0: 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 76 65  he current.** ve
06b0: 72 73 69 6f 6e 20 6f 72 20 22 6c 61 74 65 73 74  rsion or "latest
06c0: 22 20 74 6f 20 73 65 6c 65 63 74 20 74 68 65 20  " to select the 
06d0: 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 68 65 63  most recent chec
06e0: 6b 2d 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f  k-in..**.** If o
06f0: 6e 65 20 6f 72 20 6d 6f 72 65 20 46 49 4c 45 53  ne or more FILES
0700: 20 61 72 65 20 6c 69 73 74 65 64 20 61 66 74 65   are listed afte
0710: 72 20 74 68 65 20 56 45 52 53 49 4f 4e 20 74 68  r the VERSION th
0720: 65 6e 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 6e  en only the.** n
0730: 61 6d 65 64 20 66 69 6c 65 73 20 61 72 65 20 63  amed files are c
0740: 61 6e 64 69 64 61 74 65 73 20 74 6f 20 62 65 20  andidates to be 
0750: 75 70 64 61 74 65 64 2e 20 20 49 66 20 46 49 4c  updated.  If FIL
0760: 45 53 20 69 73 20 6f 6d 69 74 74 65 64 2c 20 61  ES is omitted, a
0770: 6c 6c 0a 2a 2a 20 66 69 6c 65 73 20 69 6e 20 74  ll.** files in t
0780: 68 65 20 63 75 72 72 65 6e 74 20 63 68 65 63 6b  he current check
0790: 6f 75 74 20 61 72 65 20 73 75 62 6a 65 63 74 20  out are subject 
07a0: 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 2a  to be updated..*
07b0: 2a 0a 2a 2a 20 54 68 65 20 2d 6e 20 6f 72 20 2d  *.** The -n or -
07c0: 2d 6e 6f 63 68 61 6e 67 65 20 6f 70 74 69 6f 6e  -nochange option
07d0: 20 63 61 75 73 65 73 20 74 68 69 73 20 63 6f 6d   causes this com
07e0: 6d 61 6e 64 20 74 6f 20 64 6f 20 61 20 22 64 72  mand to do a "dr
07f0: 79 20 72 75 6e 22 2e 20 20 49 74 0a 2a 2a 20 70  y run".  It.** p
0800: 72 69 6e 74 73 20 6f 75 74 20 77 68 61 74 20 77  rints out what w
0810: 6f 75 6c 64 20 68 61 76 65 20 68 61 70 70 65 6e  ould have happen
0820: 65 64 20 62 75 74 20 64 6f 65 73 20 6e 6f 74 20  ed but does not 
0830: 61 63 74 75 61 6c 6c 79 20 6d 61 6b 65 20 61 6e  actually make an
0840: 79 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20  y.** changes to 
0850: 74 68 65 20 63 75 72 72 65 6e 74 20 63 68 65 63  the current chec
0860: 6b 6f 75 74 20 6f 72 20 74 68 65 20 72 65 70 6f  kout or the repo
0870: 73 69 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  sitory..**.** Th
0880: 65 20 2d 76 20 6f 72 20 2d 2d 76 65 72 62 6f 73  e -v or --verbos
0890: 65 20 6f 70 74 69 6f 6e 20 70 72 69 6e 74 73 20  e option prints 
08a0: 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69  status informati
08b0: 6f 6e 20 61 62 6f 75 74 20 75 6e 63 68 61 6e 67  on about unchang
08c0: 65 64 0a 2a 2a 20 66 69 6c 65 73 20 69 6e 20 61  ed.** files in a
08d0: 64 64 69 74 69 6f 6e 20 74 6f 20 74 68 6f 73 65  ddition to those
08e0: 20 66 69 6c 65 20 74 68 61 74 20 61 63 74 75 61   file that actua
08f0: 6c 6c 79 20 64 6f 20 63 68 61 6e 67 65 2e 0a 2a  lly do change..*
0900: 2f 0a 76 6f 69 64 20 75 70 64 61 74 65 5f 63 6d  /.void update_cm
0910: 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 76  d(void){.  int v
0920: 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  id;             
0930: 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 65 72 73   /* Current vers
0940: 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 74 69 64  ion */.  int tid
0950: 3d 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  =0;            /
0960: 2a 20 54 61 72 67 65 74 20 76 65 72 73 69 6f 6e  * Target version
0970: 20 2d 20 76 65 72 73 69 6f 6e 20 77 65 20 61 72   - version we ar
0980: 65 20 63 68 61 6e 67 69 6e 67 20 74 6f 20 2a 2f  e changing to */
0990: 0a 20 20 53 74 6d 74 20 71 3b 0a 20 20 69 6e 74  .  Stmt q;.  int
09a0: 20 6c 61 74 65 73 74 46 6c 61 67 3b 20 20 20 20   latestFlag;    
09b0: 20 20 20 2f 2a 20 2d 2d 6c 61 74 65 73 74 2e 20     /* --latest. 
09c0: 20 50 69 63 6b 20 74 68 65 20 6c 61 74 65 73 74   Pick the latest
09d0: 20 76 65 72 73 69 6f 6e 20 69 66 20 74 72 75 65   version if true
09e0: 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 63 68 61 6e   */.  int nochan
09f0: 67 65 46 6c 61 67 3b 20 20 20 20 20 2f 2a 20 2d  geFlag;     /* -
0a00: 6e 20 6f 72 20 2d 2d 6e 6f 63 68 61 6e 67 65 2e  n or --nochange.
0a10: 20 20 44 6f 20 61 20 64 72 79 20 72 75 6e 20 2a    Do a dry run *
0a20: 2f 0a 20 20 69 6e 74 20 76 65 72 62 6f 73 65 46  /.  int verboseF
0a30: 6c 61 67 3b 20 20 20 20 20 20 2f 2a 20 2d 76 20  lag;      /* -v 
0a40: 6f 72 20 2d 2d 76 65 72 62 6f 73 65 2e 20 20 4f  or --verbose.  O
0a50: 75 74 70 75 74 20 65 78 74 72 61 20 69 6e 66 6f  utput extra info
0a60: 72 6d 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 75 72  rmation */..  ur
0a70: 6c 5f 70 72 6f 78 79 5f 6f 70 74 69 6f 6e 73 28  l_proxy_options(
0a80: 29 3b 0a 20 20 6c 61 74 65 73 74 46 6c 61 67 20  );.  latestFlag 
0a90: 3d 20 66 69 6e 64 5f 6f 70 74 69 6f 6e 28 22 6c  = find_option("l
0aa0: 61 74 65 73 74 22 2c 30 2c 20 30 29 21 3d 30 3b  atest",0, 0)!=0;
0ab0: 0a 20 20 6e 6f 63 68 61 6e 67 65 46 6c 61 67 20  .  nochangeFlag 
0ac0: 3d 20 66 69 6e 64 5f 6f 70 74 69 6f 6e 28 22 6e  = find_option("n
0ad0: 6f 63 68 61 6e 67 65 22 2c 22 6e 22 2c 30 29 21  ochange","n",0)!
0ae0: 3d 30 3b 0a 20 20 76 65 72 62 6f 73 65 46 6c 61  =0;.  verboseFla
0af0: 67 20 3d 20 66 69 6e 64 5f 6f 70 74 69 6f 6e 28  g = find_option(
0b00: 22 76 65 72 62 6f 73 65 22 2c 22 76 22 2c 30 29  "verbose","v",0)
0b10: 21 3d 30 3b 0a 20 20 64 62 5f 6d 75 73 74 5f 62  !=0;.  db_must_b
0b20: 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b  e_within_tree();
0b30: 0a 20 20 76 69 64 20 3d 20 64 62 5f 6c 67 65 74  .  vid = db_lget
0b40: 5f 69 6e 74 28 22 63 68 65 63 6b 6f 75 74 22 2c  _int("checkout",
0b50: 20 30 29 3b 0a 20 20 69 66 28 20 76 69 64 3d 3d   0);.  if( vid==
0b60: 30 20 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f  0 ){.    fossil_
0b70: 66 61 74 61 6c 28 22 63 61 6e 6e 6f 74 20 66 69  fatal("cannot fi
0b80: 6e 64 20 63 75 72 72 65 6e 74 20 76 65 72 73 69  nd current versi
0b90: 6f 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  on");.  }.  if( 
0ba0: 64 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43  db_exists("SELEC
0bb0: 54 20 31 20 46 52 4f 4d 20 76 6d 65 72 67 65 22  T 1 FROM vmerge"
0bc0: 29 20 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f  ) ){.    fossil_
0bd0: 66 61 74 61 6c 28 22 63 61 6e 6e 6f 74 20 75 70  fatal("cannot up
0be0: 64 61 74 65 20 61 6e 20 75 6e 63 6f 6d 6d 69 74  date an uncommit
0bf0: 74 65 64 20 6d 65 72 67 65 22 29 3b 0a 20 20 7d  ted merge");.  }
0c00: 0a 0a 20 20 69 66 28 20 67 2e 61 72 67 63 3e 3d  ..  if( g.argc>=
0c10: 33 20 29 7b 0a 20 20 20 20 69 66 28 20 73 74 72  3 ){.    if( str
0c20: 63 6d 70 28 67 2e 61 72 67 76 5b 32 5d 2c 20 22  cmp(g.argv[2], "
0c30: 63 75 72 72 65 6e 74 22 29 3d 3d 30 20 29 7b 0a  current")==0 ){.
0c40: 20 20 20 20 20 20 2f 2a 20 49 66 20 56 45 52 53        /* If VERS
0c50: 49 4f 4e 20 69 73 20 22 63 75 72 72 65 6e 74 22  ION is "current"
0c60: 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 73  , then use the s
0c70: 61 6d 65 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f  ame algorithm to
0c80: 20 66 69 6e 64 20 74 68 65 0a 20 20 20 20 20 20   find the.      
0c90: 2a 2a 20 74 61 72 67 65 74 20 61 73 20 69 66 20  ** target as if 
0ca0: 56 45 52 53 49 4f 4e 20 77 65 72 65 20 6f 6d 69  VERSION were omi
0cb0: 74 74 65 64 2e 20 2a 2f 0a 20 20 20 20 7d 65 6c  tted. */.    }el
0cc0: 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 67 2e  se if( strcmp(g.
0cd0: 61 72 67 76 5b 32 5d 2c 20 22 6c 61 74 65 73 74  argv[2], "latest
0ce0: 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ")==0 ){.      /
0cf0: 2a 20 49 66 20 56 45 52 53 49 4f 4e 20 69 73 20  * If VERSION is 
0d00: 22 6c 61 74 65 73 74 22 2c 20 74 68 65 6e 20 75  "latest", then u
0d10: 73 65 20 74 68 65 20 73 61 6d 65 20 61 6c 67 6f  se the same algo
0d20: 72 69 74 68 6d 20 74 6f 20 66 69 6e 64 20 74 68  rithm to find th
0d30: 65 0a 20 20 20 20 20 20 2a 2a 20 74 61 72 67 65  e.      ** targe
0d40: 74 20 61 73 20 69 66 20 56 45 52 53 49 4f 4e 20  t as if VERSION 
0d50: 77 65 72 65 20 6f 6d 69 74 74 65 64 20 61 6e 64  were omitted and
0d60: 20 74 68 65 20 2d 2d 6c 61 74 65 73 74 20 66 6c   the --latest fl
0d70: 61 67 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20  ag is present.. 
0d80: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6c 61       */.      la
0d90: 74 65 73 74 46 6c 61 67 20 3d 20 31 3b 0a 20 20  testFlag = 1;.  
0da0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 74    }else{.      t
0db0: 69 64 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69 64  id = name_to_rid
0dc0: 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  (g.argv[2]);.   
0dd0: 20 20 20 69 66 28 20 74 69 64 3d 3d 30 20 29 7b     if( tid==0 ){
0de0: 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f  .        fossil_
0df0: 66 61 74 61 6c 28 22 6e 6f 20 73 75 63 68 20 76  fatal("no such v
0e00: 65 72 73 69 6f 6e 3a 20 25 73 22 2c 20 67 2e 61  ersion: %s", g.a
0e10: 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20 7d  rgv[2]);.      }
0e20: 65 6c 73 65 20 69 66 28 20 21 69 73 5f 61 5f 76  else if( !is_a_v
0e30: 65 72 73 69 6f 6e 28 74 69 64 29 20 29 7b 0a 20  ersion(tid) ){. 
0e40: 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 61         fossil_fa
0e50: 74 61 6c 28 22 6e 6f 20 73 75 63 68 20 76 65 72  tal("no such ver
0e60: 73 69 6f 6e 3a 20 25 73 22 2c 20 67 2e 61 72 67  sion: %s", g.arg
0e70: 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  v[2]);.      }. 
0e80: 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 21     }.  }.  if( !
0e90: 6e 6f 63 68 61 6e 67 65 46 6c 61 67 20 29 20 61  nochangeFlag ) a
0ea0: 75 74 6f 73 79 6e 63 28 41 55 54 4f 53 59 4e 43  utosync(AUTOSYNC
0eb0: 5f 50 55 4c 4c 29 3b 0a 20 20 0a 20 20 69 66 28  _PULL);.  .  if(
0ec0: 20 74 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 63   tid==0 ){.    c
0ed0: 6f 6d 70 75 74 65 5f 6c 65 61 76 65 73 28 76 69  ompute_leaves(vi
0ee0: 64 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 21  d, 1);.    if( !
0ef0: 6c 61 74 65 73 74 46 6c 61 67 20 26 26 20 64 62  latestFlag && db
0f00: 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20  _int(0, "SELECT 
0f10: 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 6c 65  count(*) FROM le
0f20: 61 76 65 73 22 29 3e 31 20 29 7b 0a 20 20 20 20  aves")>1 ){.    
0f30: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c    db_prepare(&q,
0f40: 20 0a 20 20 20 20 20 20 20 20 22 25 73 20 22 0a   .        "%s ".
0f50: 20 20 20 20 20 20 20 20 22 20 20 20 41 4e 44 20          "   AND 
0f60: 65 76 65 6e 74 2e 6f 62 6a 69 64 20 49 4e 20 6c  event.objid IN l
0f70: 65 61 76 65 73 22 0a 20 20 20 20 20 20 20 20 22  eaves".        "
0f80: 20 4f 52 44 45 52 20 42 59 20 65 76 65 6e 74 2e   ORDER BY event.
0f90: 6d 74 69 6d 65 20 44 45 53 43 22 2c 0a 20 20 20  mtime DESC",.   
0fa0: 20 20 20 20 20 74 69 6d 65 6c 69 6e 65 5f 71 75       timeline_qu
0fb0: 65 72 79 5f 66 6f 72 5f 74 74 79 28 29 0a 20 20  ery_for_tty().  
0fc0: 20 20 20 20 29 3b 0a 20 20 20 20 20 20 70 72 69      );.      pri
0fd0: 6e 74 5f 74 69 6d 65 6c 69 6e 65 28 26 71 2c 20  nt_timeline(&q, 
0fe0: 31 30 30 29 3b 0a 20 20 20 20 20 20 64 62 5f 66  100);.      db_f
0ff0: 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 20  inalize(&q);.   
1000: 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28     fossil_fatal(
1010: 22 4d 75 6c 74 69 70 6c 65 20 64 65 73 63 65 6e  "Multiple descen
1020: 64 61 6e 74 73 22 29 3b 0a 20 20 20 20 7d 0a 20  dants");.    }. 
1030: 20 20 20 74 69 64 20 3d 20 64 62 5f 69 6e 74 28     tid = db_int(
1040: 30 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 46  0, "SELECT rid F
1050: 52 4f 4d 20 6c 65 61 76 65 73 2c 20 65 76 65 6e  ROM leaves, even
1060: 74 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t".             
1070: 20 20 20 20 20 20 20 22 20 57 48 45 52 45 20 65         " WHERE e
1080: 76 65 6e 74 2e 6f 62 6a 69 64 3d 6c 65 61 76 65  vent.objid=leave
1090: 73 2e 72 69 64 22 0a 20 20 20 20 20 20 20 20 20  s.rid".         
10a0: 20 20 20 20 20 20 20 20 20 20 20 22 20 4f 52 44             " ORD
10b0: 45 52 20 42 59 20 65 76 65 6e 74 2e 6d 74 69 6d  ER BY event.mtim
10c0: 65 20 44 45 53 43 22 29 3b 20 0a 20 20 7d 0a 0a  e DESC"); .  }..
10d0: 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73    db_begin_trans
10e0: 61 63 74 69 6f 6e 28 29 3b 0a 20 20 76 66 69 6c  action();.  vfil
10f0: 65 5f 63 68 65 63 6b 5f 73 69 67 6e 61 74 75 72  e_check_signatur
1100: 65 28 76 69 64 2c 20 31 29 3b 0a 20 20 75 6e 64  e(vid, 1);.  und
1110: 6f 5f 62 65 67 69 6e 28 29 3b 0a 20 20 6c 6f 61  o_begin();.  loa
1120: 64 5f 76 66 69 6c 65 5f 66 72 6f 6d 5f 72 69 64  d_vfile_from_rid
1130: 28 74 69 64 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  (tid);..  /*.  *
1140: 2a 20 54 68 65 20 72 65 63 6f 72 64 2e 66 6e 20  * The record.fn 
1150: 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f  field is used to
1160: 20 6d 61 74 63 68 20 66 69 6c 65 73 20 61 67 61   match files aga
1170: 69 6e 73 74 20 65 61 63 68 20 6f 74 68 65 72 2e  inst each other.
1180: 20 20 54 68 65 0a 20 20 2a 2a 20 46 56 20 74 61    The.  ** FV ta
1190: 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65  ble contains one
11a0: 20 72 6f 77 20 66 6f 72 20 65 61 63 68 20 65 61   row for each ea
11b0: 63 68 20 75 6e 69 71 75 65 20 66 69 6c 65 6e 61  ch unique filena
11c0: 6d 65 20 69 6e 0a 20 20 2a 2a 20 69 6e 20 74 68  me in.  ** in th
11d0: 65 20 63 75 72 72 65 6e 74 20 63 68 65 63 6b 6f  e current checko
11e0: 75 74 2c 20 74 68 65 20 70 69 76 6f 74 2c 20 61  ut, the pivot, a
11f0: 6e 64 20 74 68 65 20 76 65 72 73 69 6f 6e 20 62  nd the version b
1200: 65 69 6e 67 20 6d 65 72 67 65 64 2e 0a 20 20 2a  eing merged..  *
1210: 2f 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65  /.  db_multi_exe
1220: 63 28 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42  c(.    "DROP TAB
1230: 4c 45 20 49 46 20 45 58 49 53 54 53 20 66 76 3b  LE IF EXISTS fv;
1240: 22 0a 20 20 20 20 22 43 52 45 41 54 45 20 54 45  ".    "CREATE TE
1250: 4d 50 20 54 41 42 4c 45 20 66 76 28 22 0a 20 20  MP TABLE fv(".  
1260: 20 20 22 20 20 66 6e 20 54 45 58 54 20 50 52 49    "  fn TEXT PRI
1270: 4d 41 52 59 20 4b 45 59 2c 22 20 20 20 20 20 20  MARY KEY,"      
1280: 2f 2a 20 54 68 65 20 66 69 6c 65 6e 61 6d 65 20  /* The filename 
1290: 72 65 6c 61 74 69 76 65 20 74 6f 20 72 6f 6f 74  relative to root
12a0: 20 2a 2f 0a 20 20 20 20 22 20 20 69 64 76 20 49   */.    "  idv I
12b0: 4e 54 45 47 45 52 2c 22 20 20 20 20 20 20 20 20  NTEGER,"        
12c0: 20 20 20 20 20 20 2f 2a 20 56 46 49 4c 45 20 65        /* VFILE e
12d0: 6e 74 72 79 20 66 6f 72 20 63 75 72 72 65 6e 74  ntry for current
12e0: 20 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20   version */.    
12f0: 22 20 20 69 64 74 20 49 4e 54 45 47 45 52 2c 22  "  idt INTEGER,"
1300: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1310: 20 56 46 49 4c 45 20 65 6e 74 72 79 20 66 6f 72   VFILE entry for
1320: 20 74 61 72 67 65 74 20 76 65 72 73 69 6f 6e 20   target version 
1330: 2a 2f 0a 20 20 20 20 22 20 20 63 68 6e 67 65 64  */.    "  chnged
1340: 20 42 4f 4f 4c 45 41 4e 2c 22 20 20 20 20 20 20   BOOLEAN,"      
1350: 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1360: 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20  current version 
1370: 68 61 73 20 62 65 65 6e 20 65 64 69 74 65 64 20  has been edited 
1380: 2a 2f 0a 20 20 20 20 22 20 20 72 69 64 76 20 49  */.    "  ridv I
1390: 4e 54 45 47 45 52 2c 22 20 20 20 20 20 20 20 20  NTEGER,"        
13a0: 20 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 49       /* Record I
13b0: 44 20 66 6f 72 20 63 75 72 72 65 6e 74 20 76 65  D for current ve
13c0: 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 22 20 20  rsion */.    "  
13d0: 72 69 64 74 20 49 4e 54 45 47 45 52 20 22 20 20  ridt INTEGER "  
13e0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
13f0: 63 6f 72 64 20 49 44 20 66 6f 72 20 74 61 72 67  cord ID for targ
1400: 65 74 20 2a 2f 0a 20 20 20 20 22 29 3b 22 0a 20  et */.    ");". 
1410: 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47     "INSERT OR IG
1420: 4e 4f 52 45 20 49 4e 54 4f 20 66 76 22 0a 20 20  NORE INTO fv".  
1430: 20 20 22 20 53 45 4c 45 43 54 20 70 61 74 68 6e    " SELECT pathn
1440: 61 6d 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  ame, 0, 0, 0, 0,
1450: 20 30 20 46 52 4f 4d 20 76 66 69 6c 65 22 0a 20   0 FROM vfile". 
1460: 20 29 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65   );.  db_prepare
1470: 28 26 71 2c 0a 20 20 20 20 22 53 45 4c 45 43 54  (&q,.    "SELECT
1480: 20 69 64 2c 20 70 61 74 68 6e 61 6d 65 2c 20 72   id, pathname, r
1490: 69 64 20 46 52 4f 4d 20 76 66 69 6c 65 22 0a 20  id FROM vfile". 
14a0: 20 20 20 22 20 57 48 45 52 45 20 76 69 64 3d 25     " WHERE vid=%
14b0: 64 22 2c 20 74 69 64 0a 20 20 29 3b 0a 20 20 77  d", tid.  );.  w
14c0: 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71  hile( db_step(&q
14d0: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
14e0: 0a 20 20 20 20 69 6e 74 20 69 64 20 3d 20 64 62  .    int id = db
14f0: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20  _column_int(&q, 
1500: 30 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  0);.    const ch
1510: 61 72 20 2a 66 6e 20 3d 20 64 62 5f 63 6f 6c 75  ar *fn = db_colu
1520: 6d 6e 5f 74 65 78 74 28 26 71 2c 20 31 29 3b 0a  mn_text(&q, 1);.
1530: 20 20 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62      int rid = db
1540: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20  _column_int(&q, 
1550: 32 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69  2);.    db_multi
1560: 5f 65 78 65 63 28 0a 20 20 20 20 20 20 22 55 50  _exec(.      "UP
1570: 44 41 54 45 20 66 76 20 53 45 54 20 69 64 74 3d  DATE fv SET idt=
1580: 25 64 2c 20 72 69 64 74 3d 25 64 20 57 48 45 52  %d, ridt=%d WHER
1590: 45 20 66 6e 3d 25 51 22 2c 0a 20 20 20 20 20 20  E fn=%Q",.      
15a0: 69 64 2c 20 72 69 64 2c 20 66 6e 0a 20 20 20 20  id, rid, fn.    
15b0: 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61  );.  }.  db_fina
15c0: 6c 69 7a 65 28 26 71 29 3b 0a 20 20 64 62 5f 70  lize(&q);.  db_p
15d0: 72 65 70 61 72 65 28 26 71 2c 0a 20 20 20 20 22  repare(&q,.    "
15e0: 53 45 4c 45 43 54 20 69 64 2c 20 70 61 74 68 6e  SELECT id, pathn
15f0: 61 6d 65 2c 20 72 69 64 2c 20 63 68 6e 67 65 64  ame, rid, chnged
1600: 20 46 52 4f 4d 20 76 66 69 6c 65 22 0a 20 20 20   FROM vfile".   
1610: 20 22 20 57 48 45 52 45 20 76 69 64 3d 25 64 22   " WHERE vid=%d"
1620: 2c 20 76 69 64 0a 20 20 29 3b 0a 20 20 77 68 69  , vid.  );.  whi
1630: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d  le( db_step(&q)=
1640: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
1650: 20 20 20 69 6e 74 20 69 64 20 3d 20 64 62 5f 63     int id = db_c
1660: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29  olumn_int(&q, 0)
1670: 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
1680: 20 2a 66 6e 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e   *fn = db_column
1690: 5f 74 65 78 74 28 26 71 2c 20 31 29 3b 0a 20 20  _text(&q, 1);.  
16a0: 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63    int rid = db_c
16b0: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29  olumn_int(&q, 2)
16c0: 3b 0a 20 20 20 20 69 6e 74 20 63 68 6e 67 65 64  ;.    int chnged
16d0: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74   = db_column_int
16e0: 28 26 71 2c 20 33 29 3b 0a 20 20 20 20 64 62 5f  (&q, 3);.    db_
16f0: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20  multi_exec(.    
1700: 20 20 22 55 50 44 41 54 45 20 66 76 20 53 45 54    "UPDATE fv SET
1710: 20 69 64 76 3d 25 64 2c 20 72 69 64 76 3d 25 64   idv=%d, ridv=%d
1720: 2c 20 63 68 6e 67 65 64 3d 25 64 20 57 48 45 52  , chnged=%d WHER
1730: 45 20 66 6e 3d 25 51 22 2c 0a 20 20 20 20 20 20  E fn=%Q",.      
1740: 69 64 2c 20 72 69 64 2c 20 63 68 6e 67 65 64 2c  id, rid, chnged,
1750: 20 66 6e 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20   fn.    );.  }. 
1760: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29   db_finalize(&q)
1770: 3b 0a 0a 20 20 2f 2a 20 49 66 20 46 49 4c 45 53  ;..  /* If FILES
1780: 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 20 63   appear on the c
1790: 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2c 20 72 65 6d  ommand-line, rem
17a0: 6f 76 65 20 66 72 6f 6d 20 74 68 65 20 22 66 76  ove from the "fv
17b0: 22 20 74 61 62 6c 65 0a 20 20 2a 2a 20 65 76 65  " table.  ** eve
17c0: 72 79 20 65 6e 74 72 79 20 74 68 61 74 20 69 73  ry entry that is
17d0: 20 6e 6f 74 20 6e 61 6d 65 64 20 6f 6e 20 74 68   not named on th
17e0: 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 0a  e command-line..
17f0: 20 20 2a 2f 0a 20 20 69 66 28 20 67 2e 61 72 67    */.  if( g.arg
1800: 63 3e 3d 34 20 29 7b 0a 20 20 20 20 42 6c 6f 62  c>=4 ){.    Blob
1810: 20 73 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20   sql;           
1820: 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d     /* SQL statem
1830: 65 6e 74 20 74 6f 20 70 75 72 67 65 20 75 6e 77  ent to purge unw
1840: 61 6e 74 65 64 20 65 6e 74 72 69 65 73 20 2a 2f  anted entries */
1850: 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 65 70 20  .    char *zSep 
1860: 3d 20 22 28 22 3b 20 20 20 20 20 20 2f 2a 20 53  = "(";      /* S
1870: 65 70 61 72 61 74 6f 72 20 69 6e 20 74 68 65 20  eparator in the 
1880: 6c 69 73 74 20 6f 66 20 66 69 6c 65 6e 61 6d 65  list of filename
1890: 73 20 2a 2f 0a 20 20 20 20 42 6c 6f 62 20 74 72  s */.    Blob tr
18a0: 65 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  eename;         
18b0: 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 64 20 66 69  /* Normalized fi
18c0: 6c 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 69 6e  lename */.    in
18d0: 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
18e0: 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
18f0: 6e 74 65 72 20 2a 2f 0a 0a 20 20 20 20 62 6c 6f  nter */..    blo
1900: 62 5f 7a 65 72 6f 28 26 73 71 6c 29 3b 0a 20 20  b_zero(&sql);.  
1910: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 26 73    blob_append(&s
1920: 71 6c 2c 20 22 44 45 4c 45 54 45 20 46 52 4f 4d  ql, "DELETE FROM
1930: 20 66 76 20 57 48 45 52 45 20 66 6e 20 4e 4f 54   fv WHERE fn NOT
1940: 20 49 4e 20 22 2c 20 2d 31 29 3b 0a 20 20 20 20   IN ", -1);.    
1950: 66 6f 72 28 69 3d 33 3b 20 69 3c 67 2e 61 72 67  for(i=3; i<g.arg
1960: 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66  c; i++){.      f
1970: 69 6c 65 5f 74 72 65 65 5f 6e 61 6d 65 28 67 2e  ile_tree_name(g.
1980: 61 72 67 76 5b 69 5d 2c 20 26 74 72 65 65 6e 61  argv[i], &treena
1990: 6d 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 6c  me, 1);.      bl
19a0: 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 71 6c 2c  ob_appendf(&sql,
19b0: 20 22 25 73 27 25 71 27 22 2c 20 7a 53 65 70 2c   "%s'%q'", zSep,
19c0: 20 62 6c 6f 62 5f 73 74 72 28 26 74 72 65 65 6e   blob_str(&treen
19d0: 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 62 6c 6f  ame));.      blo
19e0: 62 5f 72 65 73 65 74 28 26 74 72 65 65 6e 61 6d  b_reset(&treenam
19f0: 65 29 3b 0a 20 20 20 20 20 20 7a 53 65 70 20 3d  e);.      zSep =
1a00: 20 22 2c 22 3b 0a 20 20 20 20 7d 0a 20 20 20 20   ",";.    }.    
1a10: 62 6c 6f 62 5f 61 70 70 65 6e 64 28 26 73 71 6c  blob_append(&sql
1a20: 2c 20 22 29 22 2c 20 2d 31 29 3b 0a 20 20 20 20  , ")", -1);.    
1a30: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 62 6c  db_multi_exec(bl
1a40: 6f 62 5f 73 74 72 28 26 73 71 6c 29 29 3b 0a 20  ob_str(&sql));. 
1a50: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73     blob_reset(&s
1a60: 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20 64 62 5f 70  ql);.  }..  db_p
1a70: 72 65 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20  repare(&q, .    
1a80: 22 53 45 4c 45 43 54 20 66 6e 2c 20 69 64 76 2c  "SELECT fn, idv,
1a90: 20 72 69 64 76 2c 20 69 64 74 2c 20 72 69 64 74   ridv, idt, ridt
1aa0: 2c 20 63 68 6e 67 65 64 20 46 52 4f 4d 20 66 76  , chnged FROM fv
1ab0: 20 4f 52 44 45 52 20 42 59 20 31 22 0a 20 20 29   ORDER BY 1".  )
1ac0: 3b 0a 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74  ;.  while( db_st
1ad0: 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(&q)==SQLITE_R
1ae0: 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  OW ){.    const 
1af0: 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 64 62  char *zName = db
1b00: 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c  _column_text(&q,
1b10: 20 30 29 3b 20 20 2f 2a 20 54 68 65 20 66 69 6c   0);  /* The fil
1b20: 65 6e 61 6d 65 20 66 72 6f 6d 20 72 6f 6f 74 20  ename from root 
1b30: 2a 2f 0a 20 20 20 20 69 6e 74 20 69 64 76 20 3d  */.    int idv =
1b40: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26   db_column_int(&
1b50: 71 2c 20 31 29 3b 20 20 20 20 20 20 20 20 20 20  q, 1);          
1b60: 20 20 20 2f 2a 20 56 46 49 4c 45 20 65 6e 74 72     /* VFILE entr
1b70: 79 20 66 6f 72 20 63 75 72 72 65 6e 74 20 2a 2f  y for current */
1b80: 0a 20 20 20 20 69 6e 74 20 72 69 64 76 20 3d 20  .    int ridv = 
1b90: 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71  db_column_int(&q
1ba0: 2c 20 32 29 3b 20 20 20 20 20 20 20 20 20 20 20  , 2);           
1bb0: 20 2f 2a 20 52 65 63 6f 72 64 49 44 20 66 6f 72   /* RecordID for
1bc0: 20 63 75 72 72 65 6e 74 20 2a 2f 0a 20 20 20 20   current */.    
1bd0: 69 6e 74 20 69 64 74 20 3d 20 64 62 5f 63 6f 6c  int idt = db_col
1be0: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 33 29 3b 20  umn_int(&q, 3); 
1bf0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
1c00: 46 49 4c 45 20 65 6e 74 72 79 20 66 6f 72 20 74  FILE entry for t
1c10: 61 72 67 65 74 20 2a 2f 0a 20 20 20 20 69 6e 74  arget */.    int
1c20: 20 72 69 64 74 20 3d 20 64 62 5f 63 6f 6c 75 6d   ridt = db_colum
1c30: 6e 5f 69 6e 74 28 26 71 2c 20 34 29 3b 20 20 20  n_int(&q, 4);   
1c40: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 63 6f           /* Reco
1c50: 72 64 49 44 20 66 6f 72 20 74 61 72 67 65 74 20  rdID for target 
1c60: 2a 2f 0a 20 20 20 20 69 6e 74 20 63 68 6e 67 65  */.    int chnge
1c70: 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e  d = db_column_in
1c80: 74 28 26 71 2c 20 35 29 3b 20 20 20 20 20 20 20  t(&q, 5);       
1c90: 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 73     /* Current is
1ca0: 20 65 64 69 74 65 64 20 2a 2f 0a 20 20 20 20 63   edited */.    c
1cb0: 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 3b 20  har *zFullPath; 
1cc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75             /* Fu
1ce0: 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74  ll pathname of t
1cf0: 68 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 20 20  he file */..    
1d00: 7a 46 75 6c 6c 50 61 74 68 20 3d 20 6d 70 72 69  zFullPath = mpri
1d10: 6e 74 66 28 22 25 73 2f 25 73 22 2c 20 67 2e 7a  ntf("%s/%s", g.z
1d20: 4c 6f 63 61 6c 52 6f 6f 74 2c 20 7a 4e 61 6d 65  LocalRoot, zName
1d30: 29 3b 0a 20 20 20 20 69 66 28 20 69 64 76 3e 30  );.    if( idv>0
1d40: 20 26 26 20 72 69 64 76 3d 3d 30 20 26 26 20 69   && ridv==0 && i
1d50: 64 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  dt>0 ){.      /*
1d60: 20 43 6f 6e 66 6c 69 63 74 2e 20 20 54 68 69 73   Conflict.  This
1d70: 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 61   file has been a
1d80: 64 64 65 64 20 74 6f 20 74 68 65 20 63 75 72 72  dded to the curr
1d90: 65 6e 74 20 63 68 65 63 6b 6f 75 74 0a 20 20 20  ent checkout.   
1da0: 20 20 20 2a 2a 20 62 75 74 20 61 6c 73 6f 20 65     ** but also e
1db0: 78 69 73 74 73 20 69 6e 20 74 68 65 20 74 61 72  xists in the tar
1dc0: 67 65 74 20 63 68 65 63 6b 6f 75 74 2e 20 20 55  get checkout.  U
1dd0: 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  se the current v
1de0: 65 72 73 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f  ersion..      */
1df0: 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 43  .      printf("C
1e00: 4f 4e 46 4c 49 43 54 20 25 73 5c 6e 22 2c 20 7a  ONFLICT %s\n", z
1e10: 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Name);.    }else
1e20: 20 69 66 28 20 69 64 74 3e 30 20 26 26 20 69 64   if( idt>0 && id
1e30: 76 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  v==0 ){.      /*
1e40: 20 46 69 6c 65 20 61 64 64 65 64 20 69 6e 20 74   File added in t
1e50: 68 65 20 74 61 72 67 65 74 2e 20 2a 2f 0a 20 20  he target. */.  
1e60: 20 20 20 20 70 72 69 6e 74 66 28 22 41 44 44 20      printf("ADD 
1e70: 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  %s\n", zName);. 
1e80: 20 20 20 20 20 75 6e 64 6f 5f 73 61 76 65 28 7a       undo_save(z
1e90: 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Name);.      if(
1ea0: 20 21 6e 6f 63 68 61 6e 67 65 46 6c 61 67 20 29   !nochangeFlag )
1eb0: 20 76 66 69 6c 65 5f 74 6f 5f 64 69 73 6b 28 30   vfile_to_disk(0
1ec0: 2c 20 69 64 74 2c 20 30 29 3b 0a 20 20 20 20 7d  , idt, 0);.    }
1ed0: 65 6c 73 65 20 69 66 28 20 69 64 74 3e 30 20 26  else if( idt>0 &
1ee0: 26 20 69 64 76 3e 30 20 26 26 20 72 69 64 74 21  & idv>0 && ridt!
1ef0: 3d 72 69 64 76 20 26 26 20 63 68 6e 67 65 64 3d  =ridv && chnged=
1f00: 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  =0 ){.      /* T
1f10: 68 65 20 66 69 6c 65 20 69 73 20 75 6e 65 64 69  he file is unedi
1f20: 74 65 64 2e 20 20 43 68 61 6e 67 65 20 69 74 20  ted.  Change it 
1f30: 74 6f 20 74 68 65 20 74 61 72 67 65 74 20 76 65  to the target ve
1f40: 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 70  rsion */.      p
1f50: 72 69 6e 74 66 28 22 55 50 44 41 54 45 20 25 73  rintf("UPDATE %s
1f60: 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  \n", zName);.   
1f70: 20 20 20 75 6e 64 6f 5f 73 61 76 65 28 7a 4e 61     undo_save(zNa
1f80: 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  me);.      if( !
1f90: 6e 6f 63 68 61 6e 67 65 46 6c 61 67 20 29 20 76  nochangeFlag ) v
1fa0: 66 69 6c 65 5f 74 6f 5f 64 69 73 6b 28 30 2c 20  file_to_disk(0, 
1fb0: 69 64 74 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c  idt, 0);.    }el
1fc0: 73 65 20 69 66 28 20 69 64 74 3e 30 20 26 26 20  se if( idt>0 && 
1fd0: 69 64 76 3e 30 20 26 26 20 66 69 6c 65 5f 73 69  idv>0 && file_si
1fe0: 7a 65 28 7a 46 75 6c 6c 50 61 74 68 29 3c 30 20  ze(zFullPath)<0 
1ff0: 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ){.      /* The 
2000: 66 69 6c 65 20 6d 69 73 73 69 6e 67 20 66 72 6f  file missing fro
2010: 6d 20 74 68 65 20 6c 6f 63 61 6c 20 63 68 65 63  m the local chec
2020: 6b 2d 6f 75 74 2e 20 52 65 73 74 6f 72 65 20 69  k-out. Restore i
2030: 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a  t to the.      *
2040: 2a 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 61  * version that a
2050: 70 70 65 61 72 73 20 69 6e 20 74 68 65 20 74 61  ppears in the ta
2060: 72 67 65 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70  rget. */.      p
2070: 72 69 6e 74 66 28 22 55 50 44 41 54 45 20 25 73  rintf("UPDATE %s
2080: 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  \n", zName);.   
2090: 20 20 20 75 6e 64 6f 5f 73 61 76 65 28 7a 4e 61     undo_save(zNa
20a0: 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  me);.      if( !
20b0: 6e 6f 63 68 61 6e 67 65 46 6c 61 67 20 29 20 76  nochangeFlag ) v
20c0: 66 69 6c 65 5f 74 6f 5f 64 69 73 6b 28 30 2c 20  file_to_disk(0, 
20d0: 69 64 74 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c  idt, 0);.    }el
20e0: 73 65 20 69 66 28 20 69 64 74 3d 3d 30 20 26 26  se if( idt==0 &&
20f0: 20 69 64 76 3e 30 20 29 7b 0a 20 20 20 20 20 20   idv>0 ){.      
2100: 69 66 28 20 72 69 64 76 3d 3d 30 20 29 7b 0a 20  if( ridv==0 ){. 
2110: 20 20 20 20 20 20 20 2f 2a 20 41 64 64 65 64 20         /* Added 
2120: 69 6e 20 63 75 72 72 65 6e 74 20 63 68 65 63 6b  in current check
2130: 6f 75 74 2e 20 20 43 6f 6e 74 69 6e 75 65 20 74  out.  Continue t
2140: 6f 20 68 6f 6c 64 20 74 68 65 20 66 69 6c 65 20  o hold the file 
2150: 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73  as.        ** as
2160: 20 61 6e 20 61 64 64 69 74 69 6f 6e 20 2a 2f 0a   an addition */.
2170: 20 20 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69          db_multi
2180: 5f 65 78 65 63 28 22 55 50 44 41 54 45 20 76 66  _exec("UPDATE vf
2190: 69 6c 65 20 53 45 54 20 76 69 64 3d 25 64 20 57  ile SET vid=%d W
21a0: 48 45 52 45 20 69 64 3d 25 64 22 2c 20 74 69 64  HERE id=%d", tid
21b0: 2c 20 69 64 76 29 3b 0a 20 20 20 20 20 20 7d 65  , idv);.      }e
21c0: 6c 73 65 20 69 66 28 20 63 68 6e 67 65 64 20 29  lse if( chnged )
21d0: 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 64 69  {.        /* Edi
21e0: 74 65 64 20 6c 6f 63 61 6c 6c 79 20 62 75 74 20  ted locally but 
21f0: 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 74 68 65  deleted from the
2200: 20 74 61 72 67 65 74 2e 20 20 44 65 6c 65 74 65   target.  Delete
2210: 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20   it. */.        
2220: 70 72 69 6e 74 66 28 22 43 4f 4e 46 4c 49 43 54  printf("CONFLICT
2230: 20 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a   %s\n", zName);.
2240: 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2250: 20 20 20 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c       char *zFull
2260: 50 61 74 68 3b 0a 20 20 20 20 20 20 20 20 70 72  Path;.        pr
2270: 69 6e 74 66 28 22 52 45 4d 4f 56 45 20 25 73 5c  intf("REMOVE %s\
2280: 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  n", zName);.    
2290: 20 20 20 20 75 6e 64 6f 5f 73 61 76 65 28 7a 4e      undo_save(zN
22a0: 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7a 46  ame);.        zF
22b0: 75 6c 6c 50 61 74 68 20 3d 20 6d 70 72 69 6e 74  ullPath = mprint
22c0: 66 28 22 25 73 2f 25 73 22 2c 20 67 2e 7a 4c 6f  f("%s/%s", g.zLo
22d0: 63 61 6c 52 6f 6f 74 2c 20 7a 4e 61 6d 65 29 3b  calRoot, zName);
22e0: 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6e 6f  .        if( !no
22f0: 63 68 61 6e 67 65 46 6c 61 67 20 29 20 75 6e 6c  changeFlag ) unl
2300: 69 6e 6b 28 7a 46 75 6c 6c 50 61 74 68 29 3b 0a  ink(zFullPath);.
2310: 20 20 20 20 20 20 20 20 66 72 65 65 28 7a 46 75          free(zFu
2320: 6c 6c 50 61 74 68 29 3b 0a 20 20 20 20 20 20 7d  llPath);.      }
2330: 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69  .    }else if( i
2340: 64 74 3e 30 20 26 26 20 69 64 76 3e 30 20 26 26  dt>0 && idv>0 &&
2350: 20 72 69 64 74 21 3d 72 69 64 76 20 26 26 20 63   ridt!=ridv && c
2360: 68 6e 67 65 64 20 29 7b 0a 20 20 20 20 20 20 2f  hnged ){.      /
2370: 2a 20 4d 65 72 67 65 20 74 68 65 20 63 68 61 6e  * Merge the chan
2380: 67 65 73 20 69 6e 20 74 68 65 20 63 75 72 72 65  ges in the curre
2390: 6e 74 20 74 72 65 65 20 69 6e 74 6f 20 74 68 65  nt tree into the
23a0: 20 74 61 72 67 65 74 20 76 65 72 73 69 6f 6e 20   target version 
23b0: 2a 2f 0a 20 20 20 20 20 20 42 6c 6f 62 20 65 2c  */.      Blob e,
23c0: 20 72 2c 20 74 2c 20 76 3b 0a 20 20 20 20 20 20   r, t, v;.      
23d0: 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 70 72  int rc;.      pr
23e0: 69 6e 74 66 28 22 4d 45 52 47 45 20 25 73 5c 6e  intf("MERGE %s\n
23f0: 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ", zName);.     
2400: 20 75 6e 64 6f 5f 73 61 76 65 28 7a 4e 61 6d 65   undo_save(zName
2410: 29 3b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74  );.      content
2420: 5f 67 65 74 28 72 69 64 74 2c 20 26 74 29 3b 0a  _get(ridt, &t);.
2430: 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65        content_ge
2440: 74 28 72 69 64 76 2c 20 26 76 29 3b 0a 20 20 20  t(ridv, &v);.   
2450: 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 65 29     blob_zero(&e)
2460: 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 61  ;.      blob_rea
2470: 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 65 2c 20  d_from_file(&e, 
2480: 7a 46 75 6c 6c 50 61 74 68 29 3b 0a 20 20 20 20  zFullPath);.    
2490: 20 20 72 63 20 3d 20 62 6c 6f 62 5f 6d 65 72 67    rc = blob_merg
24a0: 65 28 26 76 2c 20 26 65 2c 20 26 74 2c 20 26 72  e(&v, &e, &t, &r
24b0: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3e  );.      if( rc>
24c0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =0 ){.        if
24d0: 28 20 21 6e 6f 63 68 61 6e 67 65 46 6c 61 67 20  ( !nochangeFlag 
24e0: 29 20 62 6c 6f 62 5f 77 72 69 74 65 5f 74 6f 5f  ) blob_write_to_
24f0: 66 69 6c 65 28 26 72 2c 20 7a 46 75 6c 6c 50 61  file(&r, zFullPa
2500: 74 68 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  th);.        if(
2510: 20 72 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 20   rc>0 ){.       
2520: 20 20 20 70 72 69 6e 74 66 28 22 2a 2a 2a 2a 2a     printf("*****
2530: 20 25 64 20 6d 65 72 67 65 20 63 6f 6e 66 6c 69   %d merge confli
2540: 63 74 73 20 69 6e 20 25 73 5c 6e 22 2c 20 72 63  cts in %s\n", rc
2550: 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
2560: 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
2570: 0a 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 28  .        printf(
2580: 22 2a 2a 2a 2a 2a 20 43 61 6e 6e 6f 74 20 6d 65  "***** Cannot me
2590: 72 67 65 20 62 69 6e 61 72 79 20 66 69 6c 65 20  rge binary file 
25a0: 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  %s\n", zName);. 
25b0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 6c 6f       }.      blo
25c0: 62 5f 72 65 73 65 74 28 26 76 29 3b 0a 20 20 20  b_reset(&v);.   
25d0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 65     blob_reset(&e
25e0: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65  );.      blob_re
25f0: 73 65 74 28 26 74 29 3b 0a 20 20 20 20 20 20 62  set(&t);.      b
2600: 6c 6f 62 5f 72 65 73 65 74 28 26 72 29 3b 0a 20  lob_reset(&r);. 
2610: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 76 65 72     }else if( ver
2620: 62 6f 73 65 46 6c 61 67 20 29 7b 0a 20 20 20 20  boseFlag ){.    
2630: 20 20 70 72 69 6e 74 66 28 22 55 4e 43 48 41 4e    printf("UNCHAN
2640: 47 45 44 20 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65  GED %s\n", zName
2650: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65  );.    }.    fre
2660: 65 28 7a 46 75 6c 6c 50 61 74 68 29 3b 0a 20 20  e(zFullPath);.  
2670: 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28  }.  db_finalize(
2680: 26 71 29 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  &q);.  .  /*.  *
2690: 2a 20 43 6c 65 61 6e 20 75 70 20 74 68 65 20 6d  * Clean up the m
26a0: 69 64 20 61 6e 64 20 70 69 64 20 56 46 49 4c 45  id and pid VFILE
26b0: 20 65 6e 74 72 69 65 73 2e 20 20 54 68 65 6e 20   entries.  Then 
26c0: 63 6f 6d 6d 69 74 20 74 68 65 20 63 68 61 6e 67  commit the chang
26d0: 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  es..  */.  if( n
26e0: 6f 63 68 61 6e 67 65 46 6c 61 67 20 29 7b 0a 20  ochangeFlag ){. 
26f0: 20 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61     db_end_transa
2700: 63 74 69 6f 6e 28 31 29 3b 20 20 2f 2a 20 57 69  ction(1);  /* Wi
2710: 74 68 20 2d 2d 6e 6f 63 68 61 6e 67 65 2c 20 72  th --nochange, r
2720: 6f 6c 6c 62 61 63 6b 20 63 68 61 6e 67 65 73 20  ollback changes 
2730: 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  */.  }else{.    
2740: 69 66 28 20 67 2e 61 72 67 63 3c 3d 33 20 29 7b  if( g.argc<=3 ){
2750: 0a 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 66 69  .      /* All fi
2760: 6c 65 73 20 75 70 64 61 74 65 64 2e 20 20 53 68  les updated.  Sh
2770: 69 66 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  ift the current 
2780: 63 68 65 63 6b 6f 75 74 20 74 6f 20 74 68 65 20  checkout to the 
2790: 74 61 72 67 65 74 2e 20 2a 2f 0a 20 20 20 20 20  target. */.     
27a0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22   db_multi_exec("
27b0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 76 66 69 6c  DELETE FROM vfil
27c0: 65 20 57 48 45 52 45 20 76 69 64 21 3d 25 64 22  e WHERE vid!=%d"
27d0: 2c 20 74 69 64 29 3b 0a 20 20 20 20 20 20 6d 61  , tid);.      ma
27e0: 6e 69 66 65 73 74 5f 74 6f 5f 64 69 73 6b 28 74  nifest_to_disk(t
27f0: 69 64 29 3b 0a 20 20 20 20 20 20 64 62 5f 6c 73  id);.      db_ls
2800: 65 74 5f 69 6e 74 28 22 63 68 65 63 6b 6f 75 74  et_int("checkout
2810: 22 2c 20 74 69 64 29 3b 0a 20 20 20 20 7d 65 6c  ", tid);.    }el
2820: 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 73  se{.      /* A s
2830: 75 62 73 65 74 20 6f 66 20 66 69 6c 65 73 20 68  ubset of files h
2840: 61 76 65 20 62 65 65 6e 20 63 68 65 63 6b 65 64  ave been checked
2850: 20 6f 75 74 2e 20 20 4b 65 65 70 20 74 68 65 20   out.  Keep the 
2860: 63 75 72 72 65 6e 74 0a 20 20 20 20 20 20 2a 2a  current.      **
2870: 20 63 68 65 63 6b 6f 75 74 20 75 6e 63 68 61 6e   checkout unchan
2880: 67 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 64 62  ged. */.      db
2890: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 22 44 45 4c  _multi_exec("DEL
28a0: 45 54 45 20 46 52 4f 4d 20 76 66 69 6c 65 20 57  ETE FROM vfile W
28b0: 48 45 52 45 20 76 69 64 21 3d 25 64 22 2c 20 76  HERE vid!=%d", v
28c0: 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  id);.    }.    d
28d0: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  b_end_transactio
28e0: 6e 28 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  n(0);.  }.}.../*
28f0: 0a 2a 2a 20 47 65 74 20 74 68 65 20 63 6f 6e 74  .** Get the cont
2900: 65 6e 74 73 20 6f 66 20 61 20 66 69 6c 65 20 77  ents of a file w
2910: 69 74 68 69 6e 20 61 20 67 69 76 65 6e 20 72 65  ithin a given re
2920: 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 69 6e 74 20 68  vision..*/.int h
2930: 69 73 74 6f 72 69 63 61 6c 5f 76 65 72 73 69 6f  istorical_versio
2940: 6e 5f 6f 66 5f 66 69 6c 65 28 0a 20 20 63 6f 6e  n_of_file(.  con
2950: 73 74 20 63 68 61 72 20 2a 72 65 76 69 73 69 6f  st char *revisio
2960: 6e 2c 20 20 20 20 2f 2a 20 54 68 65 20 62 61 73  n,    /* The bas
2970: 65 6c 69 6e 65 20 6e 61 6d 65 20 63 6f 6e 74 61  eline name conta
2980: 69 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a  ining the file *
2990: 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
29a0: 66 69 6c 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  file,        /* 
29b0: 46 75 6c 6c 20 74 72 65 65 6e 61 6d 65 20 6f 66  Full treename of
29c0: 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 42   the file */.  B
29d0: 6c 6f 62 20 2a 63 6f 6e 74 65 6e 74 2c 20 20 20  lob *content,   
29e0: 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 74          /* Put t
29f0: 68 65 20 63 6f 6e 74 65 6e 74 20 68 65 72 65 20  he content here 
2a00: 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65  */.  int errCode
2a10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a20: 20 45 72 72 6f 72 20 63 6f 64 65 20 69 66 20 66   Error code if f
2a30: 69 6c 65 20 6e 6f 74 20 66 6f 75 6e 64 2e 20 20  ile not found.  
2a40: 50 61 6e 69 63 20 69 66 20 30 2e 20 2a 2f 0a 29  Panic if 0. */.)
2a50: 7b 0a 20 20 42 6c 6f 62 20 6d 66 69 6c 65 3b 0a  {.  Blob mfile;.
2a60: 20 20 4d 61 6e 69 66 65 73 74 20 6d 3b 0a 20 20    Manifest m;.  
2a70: 69 6e 74 20 69 2c 20 72 69 64 3d 30 3b 0a 20 20  int i, rid=0;.  
2a80: 0a 20 20 69 66 28 20 72 65 76 69 73 69 6f 6e 20  .  if( revision 
2a90: 29 7b 0a 20 20 20 20 72 69 64 20 3d 20 6e 61 6d  ){.    rid = nam
2aa0: 65 5f 74 6f 5f 72 69 64 28 72 65 76 69 73 69 6f  e_to_rid(revisio
2ab0: 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  n);.  }else{.   
2ac0: 20 72 69 64 20 3d 20 64 62 5f 6c 67 65 74 5f 69   rid = db_lget_i
2ad0: 6e 74 28 22 63 68 65 63 6b 6f 75 74 22 2c 20 30  nt("checkout", 0
2ae0: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 73  );.  }.  if( !is
2af0: 5f 61 5f 76 65 72 73 69 6f 6e 28 72 69 64 29 20  _a_version(rid) 
2b00: 29 7b 0a 20 20 20 20 69 66 28 20 65 72 72 43 6f  ){.    if( errCo
2b10: 64 65 3e 30 20 29 20 72 65 74 75 72 6e 20 65 72  de>0 ) return er
2b20: 72 43 6f 64 65 3b 0a 20 20 20 20 66 6f 73 73 69  rCode;.    fossi
2b30: 6c 5f 66 61 74 61 6c 28 22 6e 6f 20 73 75 63 68  l_fatal("no such
2b40: 20 63 68 65 63 6b 2d 6f 75 74 3a 20 25 73 22 2c   check-out: %s",
2b50: 20 72 65 76 69 73 69 6f 6e 29 3b 0a 20 20 7d 0a   revision);.  }.
2b60: 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69    content_get(ri
2b70: 64 2c 20 26 6d 66 69 6c 65 29 3b 0a 20 20 0a 20  d, &mfile);.  . 
2b80: 20 69 66 28 20 6d 61 6e 69 66 65 73 74 5f 70 61   if( manifest_pa
2b90: 72 73 65 28 26 6d 2c 20 26 6d 66 69 6c 65 29 20  rse(&m, &mfile) 
2ba0: 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
2bb0: 69 3c 6d 2e 6e 46 69 6c 65 3b 20 69 2b 2b 29 7b  i<m.nFile; i++){
2bc0: 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d  .      if( strcm
2bd0: 70 28 6d 2e 61 46 69 6c 65 5b 69 5d 2e 7a 4e 61  p(m.aFile[i].zNa
2be0: 6d 65 2c 20 66 69 6c 65 29 3d 3d 30 20 29 7b 0a  me, file)==0 ){.
2bf0: 20 20 20 20 20 20 20 20 72 69 64 20 3d 20 75 75          rid = uu
2c00: 69 64 5f 74 6f 5f 72 69 64 28 6d 2e 61 46 69 6c  id_to_rid(m.aFil
2c10: 65 5b 69 5d 2e 7a 55 75 69 64 2c 20 30 29 3b 0a  e[i].zUuid, 0);.
2c20: 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 63          return c
2c30: 6f 6e 74 65 6e 74 5f 67 65 74 28 72 69 64 2c 20  ontent_get(rid, 
2c40: 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20 20  content);.      
2c50: 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
2c60: 65 72 72 43 6f 64 65 3c 3d 30 20 29 7b 0a 20 20  errCode<=0 ){.  
2c70: 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c      fossil_fatal
2c80: 28 22 66 69 6c 65 20 25 73 20 64 6f 65 73 20 6e  ("file %s does n
2c90: 6f 74 20 65 78 69 73 74 20 69 6e 20 62 61 73 65  ot exist in base
2ca0: 6c 69 6e 65 3a 20 25 73 22 2c 20 66 69 6c 65 2c  line: %s", file,
2cb0: 20 72 65 76 69 73 69 6f 6e 29 3b 0a 20 20 20 20   revision);.    
2cc0: 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65 72  }.  }else if( er
2cd0: 72 43 6f 64 65 3c 3d 30 20 29 7b 0a 20 20 20 20  rCode<=0 ){.    
2ce0: 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 63 6f  fossil_panic("co
2cf0: 75 6c 64 20 6e 6f 74 20 70 61 72 73 65 20 6d 61  uld not parse ma
2d00: 6e 69 66 65 73 74 20 66 6f 72 20 62 61 73 65 6c  nifest for basel
2d10: 69 6e 65 3a 20 25 73 22 2c 20 72 65 76 69 73 69  ine: %s", revisi
2d20: 6f 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  on);.  }.  retur
2d30: 6e 20 65 72 72 43 6f 64 65 3b 0a 7d 0a 0a 0a 2f  n errCode;.}.../
2d40: 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 72 65  *.** COMMAND: re
2d50: 76 65 72 74 0a 2a 2a 0a 2a 2a 20 55 73 61 67 65  vert.**.** Usage
2d60: 3a 20 25 66 6f 73 73 69 6c 20 72 65 76 65 72 74  : %fossil revert
2d70: 20 3f 2d 72 20 52 45 56 49 53 49 4f 4e 3f 20 46   ?-r REVISION? F
2d80: 49 4c 45 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 52 65  ILE ....**.** Re
2d90: 76 65 72 74 20 74 6f 20 74 68 65 20 63 75 72 72  vert to the curr
2da0: 65 6e 74 20 72 65 70 6f 73 69 74 6f 72 79 20 76  ent repository v
2db0: 65 72 73 69 6f 6e 20 6f 66 20 46 49 4c 45 2c 20  ersion of FILE, 
2dc0: 6f 72 20 74 6f 0a 2a 2a 20 74 68 65 20 76 65 72  or to.** the ver
2dd0: 73 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20  sion associated 
2de0: 77 69 74 68 20 62 61 73 65 6c 69 6e 65 20 52 45  with baseline RE
2df0: 56 49 53 49 4f 4e 20 69 66 20 74 68 65 20 2d 72  VISION if the -r
2e00: 20 66 6c 61 67 0a 2a 2a 20 61 70 70 65 61 72 73   flag.** appears
2e10: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 66 69 6c  ..**.** If a fil
2e20: 65 20 69 73 20 72 65 76 65 72 74 65 64 20 61 63  e is reverted ac
2e30: 63 69 64 65 6e 74 6c 79 2c 20 69 74 20 63 61 6e  cidently, it can
2e40: 20 62 65 20 72 65 73 74 6f 72 65 64 20 75 73 69   be restored usi
2e50: 6e 67 0a 2a 2a 20 74 68 65 20 22 66 6f 73 73 69  ng.** the "fossi
2e60: 6c 20 75 6e 64 6f 22 20 63 6f 6d 6d 61 6e 64 2e  l undo" command.
2e70: 0a 2a 2f 0a 76 6f 69 64 20 72 65 76 65 72 74 5f  .*/.void revert_
2e80: 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20 63 68 61  cmd(void){.  cha
2e90: 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 63 6f 6e 73  r *zFile;.  cons
2ea0: 74 20 63 68 61 72 20 2a 7a 52 65 76 69 73 69 6f  t char *zRevisio
2eb0: 6e 3b 0a 20 20 42 6c 6f 62 20 66 6e 61 6d 65 3b  n;.  Blob fname;
2ec0: 0a 20 20 42 6c 6f 62 20 72 65 63 6f 72 64 3b 0a  .  Blob record;.
2ed0: 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65    int i;.  int e
2ee0: 72 72 43 6f 64 65 3b 0a 20 20 69 6e 74 20 72 69  rrCode;.  int ri
2ef0: 64 20 3d 20 30 3b 0a 20 20 0a 20 20 7a 52 65 76  d = 0;.  .  zRev
2f00: 69 73 69 6f 6e 20 3d 20 66 69 6e 64 5f 6f 70 74  ision = find_opt
2f10: 69 6f 6e 28 22 72 65 76 69 73 69 6f 6e 22 2c 20  ion("revision", 
2f20: 22 72 22 2c 20 31 29 3b 0a 20 20 76 65 72 69 66  "r", 1);.  verif
2f30: 79 5f 61 6c 6c 5f 6f 70 74 69 6f 6e 73 28 29 3b  y_all_options();
2f40: 0a 20 20 0a 20 20 69 66 28 20 67 2e 61 72 67 63  .  .  if( g.argc
2f50: 3c 33 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28  <3 ){.    usage(
2f60: 22 3f 4f 50 54 49 4f 4e 53 3f 20 46 49 4c 45 20  "?OPTIONS? FILE 
2f70: 2e 2e 2e 22 29 3b 0a 20 20 7d 0a 20 20 64 62 5f  ...");.  }.  db_
2f80: 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74  must_be_within_t
2f90: 72 65 65 28 29 3b 0a 20 20 64 62 5f 62 65 67 69  ree();.  db_begi
2fa0: 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b  n_transaction();
2fb0: 0a 20 20 75 6e 64 6f 5f 62 65 67 69 6e 28 29 3b  .  undo_begin();
2fc0: 0a 0a 20 20 66 6f 72 28 69 3d 32 3b 20 69 3c 67  ..  for(i=2; i<g
2fd0: 2e 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  .argc; i++){.   
2fe0: 20 7a 46 69 6c 65 20 3d 20 6d 70 72 69 6e 74 66   zFile = mprintf
2ff0: 28 22 25 2f 22 2c 20 67 2e 61 72 67 76 5b 69 5d  ("%/", g.argv[i]
3000: 29 3b 0a 20 20 20 20 66 69 6c 65 5f 74 72 65 65  );.    file_tree
3010: 5f 6e 61 6d 65 28 7a 46 69 6c 65 2c 20 26 66 6e  _name(zFile, &fn
3020: 61 6d 65 2c 20 31 29 3b 0a 0a 20 20 20 20 69 66  ame, 1);..    if
3030: 28 20 7a 52 65 76 69 73 69 6f 6e 21 3d 30 20 29  ( zRevision!=0 )
3040: 7b 0a 20 20 20 20 20 20 65 72 72 43 6f 64 65 20  {.      errCode 
3050: 3d 20 68 69 73 74 6f 72 69 63 61 6c 5f 76 65 72  = historical_ver
3060: 73 69 6f 6e 5f 6f 66 5f 66 69 6c 65 28 7a 52 65  sion_of_file(zRe
3070: 76 69 73 69 6f 6e 2c 20 62 6c 6f 62 5f 73 74 72  vision, blob_str
3080: 28 26 66 6e 61 6d 65 29 2c 0a 20 20 20 20 20 20  (&fname),.      
3090: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30b0: 20 20 20 20 20 26 72 65 63 6f 72 64 2c 20 32 29       &record, 2)
30c0: 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
30d0: 20 20 20 72 69 64 20 3d 20 64 62 5f 69 6e 74 28     rid = db_int(
30e0: 30 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 46  0, "SELECT rid F
30f0: 52 4f 4d 20 76 66 69 6c 65 20 57 48 45 52 45 20  ROM vfile WHERE 
3100: 70 61 74 68 6e 61 6d 65 3d 25 42 22 2c 20 26 66  pathname=%B", &f
3110: 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28  name);.      if(
3120: 20 72 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20   rid==0 ){.     
3130: 20 20 20 65 72 72 43 6f 64 65 20 3d 20 32 3b 0a     errCode = 2;.
3140: 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
3150: 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74       content_get
3160: 28 72 69 64 2c 20 26 72 65 63 6f 72 64 29 3b 0a  (rid, &record);.
3170: 20 20 20 20 20 20 20 20 65 72 72 43 6f 64 65 20          errCode 
3180: 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
3190: 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 72 72 43   }..    if( errC
31a0: 6f 64 65 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  ode==2 ){.      
31b0: 66 6f 73 73 69 6c 5f 77 61 72 6e 69 6e 67 28 22  fossil_warning("
31c0: 66 69 6c 65 20 6e 6f 74 20 69 6e 20 72 65 70 6f  file not in repo
31d0: 73 69 74 6f 72 79 3a 20 25 73 22 2c 20 7a 46 69  sitory: %s", zFi
31e0: 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  le);.    }else{.
31f0: 20 20 20 20 20 20 75 6e 64 6f 5f 73 61 76 65 28        undo_save(
3200: 62 6c 6f 62 5f 73 74 72 28 26 66 6e 61 6d 65 29  blob_str(&fname)
3210: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 77 72  );.      blob_wr
3220: 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 72 65 63  ite_to_file(&rec
3230: 6f 72 64 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20  ord, zFile);.   
3240: 20 20 20 70 72 69 6e 74 66 28 22 25 73 20 72 65     printf("%s re
3250: 76 65 72 74 65 64 5c 6e 22 2c 20 7a 46 69 6c 65  verted\n", zFile
3260: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f  );.    }.    blo
3270: 62 5f 72 65 73 65 74 28 26 72 65 63 6f 72 64 29  b_reset(&record)
3280: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
3290: 28 26 66 6e 61 6d 65 29 3b 0a 20 20 20 20 66 72  (&fname);.    fr
32a0: 65 65 28 7a 46 69 6c 65 29 3b 0a 20 20 7d 0a 20  ee(zFile);.  }. 
32b0: 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74   db_end_transact
32c0: 69 6f 6e 28 30 29 3b 0a 7d 0a                    ion(0);.}.