Hex Artifact Content
Not logged in

Artifact eb47b7b9da681bd3c078212637a39c46e420978d:

File src/update.c part of check-in [6607844a01] - Added options to the "timeline" CLI command. Additional help comments. by drh on 2007-08-18 11:42:24.

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 43  sert.h>../*.** C
0430: 4f 4d 4d 41 4e 44 3a 20 75 70 64 61 74 65 0a 2a  OMMAND: update.*
0440: 2a 0a 2a 2a 20 55 73 61 67 65 3a 20 25 66 6f 73  *.** Usage: %fos
0450: 73 69 6c 20 75 70 64 61 74 65 20 3f 56 45 52 53  sil update ?VERS
0460: 49 4f 4e 3f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  ION?.**.** The o
0470: 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74  ptional argument
0480: 20 69 73 20 61 20 76 65 72 73 69 6f 6e 20 74 68   is a version th
0490: 61 74 20 73 68 6f 75 6c 64 20 62 65 63 6f 6d 65  at should become
04a0: 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
04b0: 76 65 72 73 69 6f 6e 2e 20 20 49 66 20 74 68 65  version.  If the
04c0: 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6d 69   argument is omi
04d0: 74 74 65 64 2c 20 74 68 65 6e 20 75 73 65 20 74  tted, then use t
04e0: 68 65 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 2a  he leaf of the.*
04f0: 2a 20 74 72 65 65 20 74 68 61 74 20 62 65 67 69  * tree that begi
0500: 6e 73 20 77 69 74 68 20 74 68 65 20 63 75 72 72  ns with the curr
0510: 65 6e 74 20 76 65 72 73 69 6f 6e 2c 20 69 66 20  ent version, if 
0520: 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20  there is only a 
0530: 0a 2a 2a 20 73 69 6e 67 6c 65 20 6c 65 61 66 2e  .** single leaf.
0540: 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f    .**.** This co
0550: 6d 6d 61 6e 64 20 69 73 20 64 69 66 66 65 72 65  mmand is differe
0560: 6e 74 20 66 72 6f 6d 20 74 68 65 20 22 63 68 65  nt from the "che
0570: 63 6b 6f 75 74 22 20 69 6e 20 74 68 61 74 20 65  ckout" in that e
0580: 64 69 74 73 20 61 72 65 0a 2a 2a 20 6e 6f 74 20  dits are.** not 
0590: 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 45 64  overwritten.  Ed
05a0: 69 74 73 20 61 72 65 20 6d 65 72 67 65 64 20 69  its are merged i
05b0: 6e 74 6f 20 74 68 65 20 6e 65 77 20 76 65 72 73  nto the new vers
05c0: 69 6f 6e 2e 0a 2a 2a 0a 2a 2f 0a 76 6f 69 64 20  ion..**.*/.void 
05d0: 75 70 64 61 74 65 5f 63 6d 64 28 76 6f 69 64 29  update_cmd(void)
05e0: 7b 0a 20 20 69 6e 74 20 76 69 64 3b 20 20 20 20  {.  int vid;    
05f0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
0600: 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 2a 2f 0a  rent version */.
0610: 20 20 69 6e 74 20 74 69 64 3b 20 20 20 20 20 20    int tid;      
0620: 20 20 20 20 20 20 20 20 2f 2a 20 54 61 72 67 65          /* Targe
0630: 74 20 76 65 72 73 69 6f 6e 20 2d 20 76 65 72 73  t version - vers
0640: 69 6f 6e 20 77 65 20 61 72 65 20 63 68 61 6e 67  ion we are chang
0650: 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 53 74 6d 74  ing to */.  Stmt
0660: 20 71 3b 0a 0a 20 20 69 66 28 20 67 2e 61 72 67   q;..  if( g.arg
0670: 63 21 3d 33 20 26 26 20 67 2e 61 72 67 63 21 3d  c!=3 && g.argc!=
0680: 32 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22  2 ){.    usage("
0690: 3f 56 45 52 53 49 4f 4e 3f 22 29 3b 0a 20 20 7d  ?VERSION?");.  }
06a0: 0a 20 20 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69  .  db_must_be_wi
06b0: 74 68 69 6e 5f 74 72 65 65 28 29 3b 0a 20 20 76  thin_tree();.  v
06c0: 69 64 20 3d 20 64 62 5f 6c 67 65 74 5f 69 6e 74  id = db_lget_int
06d0: 28 22 63 68 65 63 6b 6f 75 74 22 2c 20 30 29 3b  ("checkout", 0);
06e0: 0a 20 20 69 66 28 20 76 69 64 3d 3d 30 20 29 7b  .  if( vid==0 ){
06f0: 0a 20 20 20 20 76 69 64 20 3d 20 31 3b 0a 20 20  .    vid = 1;.  
0700: 7d 0a 20 20 69 66 28 20 64 62 5f 65 78 69 73 74  }.  if( db_exist
0710: 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  s("SELECT 1 FROM
0720: 20 76 6d 65 72 67 65 22 29 20 29 7b 0a 20 20 20   vmerge") ){.   
0730: 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 63   fossil_fatal("c
0740: 61 6e 6e 6f 74 20 75 70 64 61 74 65 20 61 6e 20  annot update an 
0750: 75 6e 63 6f 6d 6d 69 74 74 65 64 20 6d 65 72 67  uncommitted merg
0760: 65 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67  e");.  }.  if( g
0770: 2e 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20  .argc==3 ){.    
0780: 74 69 64 20 3d 20 6e 61 6d 65 5f 74 6f 5f 72 69  tid = name_to_ri
0790: 64 28 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20  d(g.argv[2]);.  
07a0: 20 20 69 66 28 20 74 69 64 3d 3d 30 20 29 7b 0a    if( tid==0 ){.
07b0: 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74        fossil_fat
07c0: 61 6c 28 22 6e 6f 74 20 61 20 76 65 72 73 69 6f  al("not a versio
07d0: 6e 3a 20 25 73 22 2c 20 67 2e 61 72 67 76 5b 32  n: %s", g.argv[2
07e0: 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ]);.    }.    if
07f0: 28 20 21 64 62 5f 65 78 69 73 74 73 28 22 53 45  ( !db_exists("SE
0800: 4c 45 43 54 20 31 20 46 52 4f 4d 20 6d 6c 69 6e  LECT 1 FROM mlin
0810: 6b 20 57 48 45 52 45 20 6d 69 64 3d 25 64 22 2c  k WHERE mid=%d",
0820: 20 74 69 64 29 20 29 7b 0a 20 20 20 20 20 20 66   tid) ){.      f
0830: 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 6e 6f 74  ossil_fatal("not
0840: 20 61 20 76 65 72 73 69 6f 6e 3a 20 25 73 22 2c   a version: %s",
0850: 20 67 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20   g.argv[2]);.   
0860: 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
0870: 63 6f 6d 70 75 74 65 5f 6c 65 61 76 65 73 28 76  compute_leaves(v
0880: 69 64 29 3b 0a 20 20 20 20 69 66 28 20 64 62 5f  id);.    if( db_
0890: 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 63  int(0, "SELECT c
08a0: 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 6c 65 61  ount(*) FROM lea
08b0: 76 65 73 22 29 3e 31 20 29 7b 0a 20 20 20 20 20  ves")>1 ){.     
08c0: 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 6d   fossil_fatal("m
08d0: 75 6c 74 69 70 6c 65 20 64 65 73 63 65 6e 64 65  ultiple descende
08e0: 6e 74 73 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20  nts");.    }.   
08f0: 20 74 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c   tid = db_int(0,
0900: 20 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f   "SELECT rid FRO
0910: 4d 20 6c 65 61 76 65 73 22 29 3b 20 0a 20 20 7d  M leaves"); .  }
0920: 0a 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61  ..  db_begin_tra
0930: 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 76 66  nsaction();.  vf
0940: 69 6c 65 5f 63 68 65 63 6b 5f 73 69 67 6e 61 74  ile_check_signat
0950: 75 72 65 28 76 69 64 29 3b 0a 20 20 6c 6f 61 64  ure(vid);.  load
0960: 5f 76 66 69 6c 65 5f 66 72 6f 6d 5f 72 69 64 28  _vfile_from_rid(
0970: 74 69 64 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  tid);..  /*.  **
0980: 20 54 68 65 20 72 65 63 6f 72 64 2e 66 6e 20 66   The record.fn f
0990: 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f 20  ield is used to 
09a0: 6d 61 74 63 68 20 66 69 6c 65 73 20 61 67 61 69  match files agai
09b0: 6e 73 74 20 65 61 63 68 20 6f 74 68 65 72 2e 20  nst each other. 
09c0: 20 54 68 65 0a 20 20 2a 2a 20 46 56 20 74 61 62   The.  ** FV tab
09d0: 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20  le contains one 
09e0: 72 6f 77 20 66 6f 72 20 65 61 63 68 20 65 61 63  row for each eac
09f0: 68 20 75 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d  h unique filenam
0a00: 65 20 69 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65  e in.  ** in the
0a10: 20 63 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75   current checkou
0a20: 74 2c 20 74 68 65 20 70 69 76 6f 74 2c 20 61 6e  t, the pivot, an
0a30: 64 20 74 68 65 20 76 65 72 73 69 6f 6e 20 62 65  d the version be
0a40: 69 6e 67 20 6d 65 72 67 65 64 2e 0a 20 20 2a 2f  ing merged..  */
0a50: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
0a60: 28 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c  (.    "DROP TABL
0a70: 45 20 49 46 20 45 58 49 53 54 53 20 66 76 3b 22  E IF EXISTS fv;"
0a80: 0a 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d  .    "CREATE TEM
0a90: 50 20 54 41 42 4c 45 20 66 76 28 22 0a 20 20 20  P TABLE fv(".   
0aa0: 20 22 20 20 66 6e 20 54 45 58 54 20 50 52 49 4d   "  fn TEXT PRIM
0ab0: 41 52 59 20 4b 45 59 2c 22 20 20 20 20 20 20 2f  ARY KEY,"      /
0ac0: 2a 20 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 2a  * The filename *
0ad0: 2f 0a 20 20 20 20 22 20 20 69 64 76 20 49 4e 54  /.    "  idv INT
0ae0: 45 47 45 52 2c 22 20 20 20 20 20 20 20 20 20 20  EGER,"          
0af0: 20 20 20 20 2f 2a 20 56 46 49 4c 45 20 65 6e 74      /* VFILE ent
0b00: 72 79 20 66 6f 72 20 63 75 72 72 65 6e 74 20 76  ry for current v
0b10: 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 22 20  ersion */.    " 
0b20: 20 69 64 74 20 49 4e 54 45 47 45 52 2c 22 20 20   idt INTEGER,"  
0b30: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
0b40: 46 49 4c 45 20 65 6e 74 72 79 20 66 6f 72 20 74  FILE entry for t
0b50: 61 72 67 65 74 20 76 65 72 73 69 6f 6e 20 2a 2f  arget version */
0b60: 0a 20 20 20 20 22 20 20 63 68 6e 67 65 64 20 42  .    "  chnged B
0b70: 4f 4f 4c 45 41 4e 2c 22 20 20 20 20 20 20 20 20  OOLEAN,"        
0b80: 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 75     /* True if cu
0b90: 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 68 61  rrent version ha
0ba0: 73 20 62 65 65 6e 20 65 64 69 74 65 64 20 2a 2f  s been edited */
0bb0: 0a 20 20 20 20 22 20 20 72 69 64 76 20 49 4e 54  .    "  ridv INT
0bc0: 45 47 45 52 2c 22 20 20 20 20 20 20 20 20 20 20  EGER,"          
0bd0: 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 49 44 20     /* Record ID 
0be0: 66 6f 72 20 63 75 72 72 65 6e 74 20 76 65 72 73  for current vers
0bf0: 69 6f 6e 20 2a 2f 0a 20 20 20 20 22 20 20 72 69  ion */.    "  ri
0c00: 64 74 20 49 4e 54 45 47 45 52 20 22 20 20 20 20  dt INTEGER "    
0c10: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 63 6f           /* Reco
0c20: 72 64 20 49 44 20 66 6f 72 20 74 61 72 67 65 74  rd ID for target
0c30: 20 2a 2f 0a 20 20 20 20 22 29 3b 22 0a 20 20 20   */.    ");".   
0c40: 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f   "INSERT OR IGNO
0c50: 52 45 20 49 4e 54 4f 20 66 76 22 0a 20 20 20 20  RE INTO fv".    
0c60: 22 20 53 45 4c 45 43 54 20 70 61 74 68 6e 61 6d  " SELECT pathnam
0c70: 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  e, 0, 0, 0, 0, 0
0c80: 20 46 52 4f 4d 20 76 66 69 6c 65 22 0a 20 20 29   FROM vfile".  )
0c90: 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26  ;.  db_prepare(&
0ca0: 71 2c 0a 20 20 20 20 22 53 45 4c 45 43 54 20 69  q,.    "SELECT i
0cb0: 64 2c 20 70 61 74 68 6e 61 6d 65 2c 20 72 69 64  d, pathname, rid
0cc0: 20 46 52 4f 4d 20 76 66 69 6c 65 22 0a 20 20 20   FROM vfile".   
0cd0: 20 22 20 57 48 45 52 45 20 76 69 64 3d 25 64 22   " WHERE vid=%d"
0ce0: 2c 20 74 69 64 0a 20 20 29 3b 0a 20 20 77 68 69  , tid.  );.  whi
0cf0: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d  le( db_step(&q)=
0d00: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
0d10: 20 20 20 69 6e 74 20 69 64 20 3d 20 64 62 5f 63     int id = db_c
0d20: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29  olumn_int(&q, 0)
0d30: 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
0d40: 20 2a 66 6e 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e   *fn = db_column
0d50: 5f 74 65 78 74 28 26 71 2c 20 31 29 3b 0a 20 20  _text(&q, 1);.  
0d60: 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63    int rid = db_c
0d70: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29  olumn_int(&q, 2)
0d80: 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65  ;.    db_multi_e
0d90: 78 65 63 28 0a 20 20 20 20 20 20 22 55 50 44 41  xec(.      "UPDA
0da0: 54 45 20 66 76 20 53 45 54 20 69 64 74 3d 25 64  TE fv SET idt=%d
0db0: 2c 20 72 69 64 74 3d 25 64 20 57 48 45 52 45 20  , ridt=%d WHERE 
0dc0: 66 6e 3d 25 51 22 2c 0a 20 20 20 20 20 20 69 64  fn=%Q",.      id
0dd0: 2c 20 72 69 64 2c 20 66 6e 0a 20 20 20 20 29 3b  , rid, fn.    );
0de0: 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69  .  }.  db_finali
0df0: 7a 65 28 26 71 29 3b 0a 20 20 64 62 5f 70 72 65  ze(&q);.  db_pre
0e00: 70 61 72 65 28 26 71 2c 0a 20 20 20 20 22 53 45  pare(&q,.    "SE
0e10: 4c 45 43 54 20 69 64 2c 20 70 61 74 68 6e 61 6d  LECT id, pathnam
0e20: 65 2c 20 72 69 64 2c 20 63 68 6e 67 65 64 20 46  e, rid, chnged F
0e30: 52 4f 4d 20 76 66 69 6c 65 22 0a 20 20 20 20 22  ROM vfile".    "
0e40: 20 57 48 45 52 45 20 76 69 64 3d 25 64 22 2c 20   WHERE vid=%d", 
0e50: 76 69 64 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65  vid.  );.  while
0e60: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
0e70: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
0e80: 20 69 6e 74 20 69 64 20 3d 20 64 62 5f 63 6f 6c   int id = db_col
0e90: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a  umn_int(&q, 0);.
0ea0: 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
0eb0: 66 6e 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74  fn = db_column_t
0ec0: 65 78 74 28 26 71 2c 20 31 29 3b 0a 20 20 20 20  ext(&q, 1);.    
0ed0: 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63 6f 6c  int rid = db_col
0ee0: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29 3b 0a  umn_int(&q, 2);.
0ef0: 20 20 20 20 69 6e 74 20 63 68 6e 67 65 64 20 3d      int chnged =
0f00: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26   db_column_int(&
0f10: 71 2c 20 33 29 3b 0a 20 20 20 20 64 62 5f 6d 75  q, 3);.    db_mu
0f20: 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20  lti_exec(.      
0f30: 22 55 50 44 41 54 45 20 66 76 20 53 45 54 20 69  "UPDATE fv SET i
0f40: 64 76 3d 25 64 2c 20 72 69 64 76 3d 25 64 2c 20  dv=%d, ridv=%d, 
0f50: 63 68 6e 67 65 64 3d 25 64 20 57 48 45 52 45 20  chnged=%d WHERE 
0f60: 66 6e 3d 25 51 22 2c 0a 20 20 20 20 20 20 69 64  fn=%Q",.      id
0f70: 2c 20 72 69 64 2c 20 63 68 6e 67 65 64 2c 20 66  , rid, chnged, f
0f80: 6e 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20 64  n.    );.  }.  d
0f90: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a  b_finalize(&q);.
0fa0: 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71  .  db_prepare(&q
0fb0: 2c 20 0a 20 20 20 20 22 53 45 4c 45 43 54 20 66  , .    "SELECT f
0fc0: 6e 2c 20 69 64 76 2c 20 72 69 64 76 2c 20 69 64  n, idv, ridv, id
0fd0: 74 2c 20 72 69 64 74 2c 20 63 68 6e 67 65 64 20  t, ridt, chnged 
0fe0: 46 52 4f 4d 20 66 76 20 4f 52 44 45 52 20 42 59  FROM fv ORDER BY
0ff0: 20 31 22 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65   1".  );.  while
1000: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
1010: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
1020: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1030: 6d 65 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74  me = db_column_t
1040: 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20  ext(&q, 0);.    
1050: 69 6e 74 20 69 64 76 20 3d 20 64 62 5f 63 6f 6c  int idv = db_col
1060: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 31 29 3b 0a  umn_int(&q, 1);.
1070: 20 20 20 20 69 6e 74 20 72 69 64 76 20 3d 20 64      int ridv = d
1080: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c  b_column_int(&q,
1090: 20 32 29 3b 0a 20 20 20 20 69 6e 74 20 69 64 74   2);.    int idt
10a0: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74   = db_column_int
10b0: 28 26 71 2c 20 33 29 3b 0a 20 20 20 20 69 6e 74  (&q, 3);.    int
10c0: 20 72 69 64 74 20 3d 20 64 62 5f 63 6f 6c 75 6d   ridt = db_colum
10d0: 6e 5f 69 6e 74 28 26 71 2c 20 34 29 3b 0a 20 20  n_int(&q, 4);.  
10e0: 20 20 69 6e 74 20 63 68 6e 67 65 64 20 3d 20 64    int chnged = d
10f0: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c  b_column_int(&q,
1100: 20 35 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 64   5);..    if( id
1110: 76 3e 30 20 26 26 20 72 69 64 76 3d 3d 30 20 26  v>0 && ridv==0 &
1120: 26 20 69 64 74 3e 30 20 29 7b 0a 20 20 20 20 20  & idt>0 ){.     
1130: 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 2e 20 20 54   /* Conflict.  T
1140: 68 69 73 20 66 69 6c 65 20 68 61 73 20 62 65 65  his file has bee
1150: 6e 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 63  n added to the c
1160: 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 0a  urrent checkout.
1170: 20 20 20 20 20 20 2a 2a 20 62 75 74 20 61 6c 73        ** but als
1180: 6f 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20  o exists in the 
1190: 74 61 72 67 65 74 20 63 68 65 63 6b 6f 75 74 2e  target checkout.
11a0: 20 20 55 73 65 20 74 68 65 20 63 75 72 72 65 6e    Use the curren
11b0: 74 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20 20 20  t version..     
11c0: 20 2a 2f 0a 20 20 20 20 20 20 70 72 69 6e 74 66   */.      printf
11d0: 28 22 43 4f 4e 46 4c 49 43 54 20 25 73 5c 6e 22  ("CONFLICT %s\n"
11e0: 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65  , zName);.    }e
11f0: 6c 73 65 20 69 66 28 20 69 64 74 3e 30 20 26 26  lse if( idt>0 &&
1200: 20 69 64 76 3d 3d 30 20 29 7b 0a 20 20 20 20 20   idv==0 ){.     
1210: 20 2f 2a 20 46 69 6c 65 20 61 64 64 65 64 20 69   /* File added i
1220: 6e 20 74 68 65 20 74 61 72 67 65 74 2e 20 2a 2f  n the target. */
1230: 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 41  .      printf("A
1240: 44 44 20 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29  DD %s\n", zName)
1250: 3b 0a 20 20 20 20 20 20 76 66 69 6c 65 5f 74 6f  ;.      vfile_to
1260: 5f 64 69 73 6b 28 30 2c 20 69 64 74 2c 20 30 29  _disk(0, idt, 0)
1270: 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1280: 69 64 74 3e 30 20 26 26 20 69 64 76 3e 30 20 26  idt>0 && idv>0 &
1290: 26 20 72 69 64 74 21 3d 72 69 64 76 20 26 26 20  & ridt!=ridv && 
12a0: 63 68 6e 67 65 64 3d 3d 30 20 29 7b 0a 20 20 20  chnged==0 ){.   
12b0: 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 69     /* The file i
12c0: 73 20 75 6e 65 64 69 74 65 64 2e 20 20 43 68 61  s unedited.  Cha
12d0: 6e 67 65 20 69 74 20 74 6f 20 74 68 65 20 74 61  nge it to the ta
12e0: 72 67 65 74 20 76 65 72 73 69 6f 6e 20 2a 2f 0a  rget version */.
12f0: 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 55 50        printf("UP
1300: 44 41 54 45 20 25 73 5c 6e 22 2c 20 7a 4e 61 6d  DATE %s\n", zNam
1310: 65 29 3b 0a 20 20 20 20 20 20 76 66 69 6c 65 5f  e);.      vfile_
1320: 74 6f 5f 64 69 73 6b 28 30 2c 20 69 64 74 2c 20  to_disk(0, idt, 
1330: 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  0);.    }else if
1340: 28 20 69 64 74 3d 3d 30 20 26 26 20 69 64 76 3e  ( idt==0 && idv>
1350: 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63  0 ){.      if( c
1360: 68 6e 67 65 64 20 29 7b 0a 20 20 20 20 20 20 20  hnged ){.       
1370: 20 70 72 69 6e 74 66 28 22 43 4f 4e 46 4c 49 43   printf("CONFLIC
1380: 54 20 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b  T %s\n", zName);
1390: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
13a0: 20 20 20 20 20 20 63 68 61 72 20 2a 7a 46 75 6c        char *zFul
13b0: 6c 50 61 74 68 3b 0a 20 20 20 20 20 20 20 20 70  lPath;.        p
13c0: 72 69 6e 74 66 28 22 52 45 4d 4f 56 45 20 25 73  rintf("REMOVE %s
13d0: 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  \n", zName);.   
13e0: 20 20 20 20 20 7a 46 75 6c 6c 50 61 74 68 20 3d       zFullPath =
13f0: 20 6d 70 72 69 6e 74 66 28 22 25 73 2f 25 73 22   mprintf("%s/%s"
1400: 2c 20 67 2e 7a 4c 6f 63 61 6c 52 6f 6f 74 2c 20  , g.zLocalRoot, 
1410: 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
1420: 75 6e 6c 69 6e 6b 28 7a 46 75 6c 6c 50 61 74 68  unlink(zFullPath
1430: 29 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 28  );.        free(
1440: 7a 46 75 6c 6c 50 61 74 68 29 3b 0a 20 20 20 20  zFullPath);.    
1450: 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
1460: 28 20 69 64 74 3e 30 20 26 26 20 69 64 76 3e 30  ( idt>0 && idv>0
1470: 20 26 26 20 72 69 64 74 21 3d 72 69 64 76 20 26   && ridt!=ridv &
1480: 26 20 63 68 6e 67 65 64 20 29 7b 0a 20 20 20 20  & chnged ){.    
1490: 20 20 2f 2a 20 4d 65 72 67 65 20 74 68 65 20 63    /* Merge the c
14a0: 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 63 75  hanges in the cu
14b0: 72 72 65 6e 74 20 74 72 65 65 20 69 6e 74 6f 20  rrent tree into 
14c0: 74 68 65 20 74 61 72 67 65 74 20 76 65 72 73 69  the target versi
14d0: 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 42 6c 6f 62  on */.      Blob
14e0: 20 65 2c 20 72 2c 20 74 2c 20 76 3b 0a 20 20 20   e, r, t, v;.   
14f0: 20 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50 61     char *zFullPa
1500: 74 68 3b 0a 20 20 20 20 20 20 70 72 69 6e 74 66  th;.      printf
1510: 28 22 4d 45 52 47 45 20 25 73 5c 6e 22 2c 20 7a  ("MERGE %s\n", z
1520: 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7a 46 75  Name);.      zFu
1530: 6c 6c 50 61 74 68 20 3d 20 6d 70 72 69 6e 74 66  llPath = mprintf
1540: 28 22 25 73 2f 25 73 22 2c 20 67 2e 7a 4c 6f 63  ("%s/%s", g.zLoc
1550: 61 6c 52 6f 6f 74 2c 20 7a 4e 61 6d 65 29 3b 0a  alRoot, zName);.
1560: 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65        content_ge
1570: 74 28 72 69 64 74 2c 20 26 74 29 3b 0a 20 20 20  t(ridt, &t);.   
1580: 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 28 72     content_get(r
1590: 69 64 76 2c 20 26 76 29 3b 0a 20 20 20 20 20 20  idv, &v);.      
15a0: 62 6c 6f 62 5f 7a 65 72 6f 28 26 65 29 3b 0a 20  blob_zero(&e);. 
15b0: 20 20 20 20 20 62 6c 6f 62 5f 72 65 61 64 5f 66       blob_read_f
15c0: 72 6f 6d 5f 66 69 6c 65 28 26 65 2c 20 7a 46 75  rom_file(&e, zFu
15d0: 6c 6c 50 61 74 68 29 3b 0a 20 20 20 20 20 20 62  llPath);.      b
15e0: 6c 6f 62 5f 6d 65 72 67 65 28 26 76 2c 20 26 65  lob_merge(&v, &e
15f0: 2c 20 26 74 2c 20 26 72 29 3b 0a 20 20 20 20 20  , &t, &r);.     
1600: 20 62 6c 6f 62 5f 77 72 69 74 65 5f 74 6f 5f 66   blob_write_to_f
1610: 69 6c 65 28 26 72 2c 20 7a 46 75 6c 6c 50 61 74  ile(&r, zFullPat
1620: 68 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 7a  h);.      free(z
1630: 46 75 6c 6c 50 61 74 68 29 3b 0a 20 20 20 20 20  FullPath);.     
1640: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 76 29 3b   blob_reset(&v);
1650: 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 65  .      blob_rese
1660: 74 28 26 65 29 3b 0a 20 20 20 20 20 20 62 6c 6f  t(&e);.      blo
1670: 62 5f 72 65 73 65 74 28 26 74 29 3b 0a 20 20 20  b_reset(&t);.   
1680: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 72     blob_reset(&r
1690: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64  );.    }.  }.  d
16a0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a  b_finalize(&q);.
16b0: 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65    .  /*.  ** Cle
16c0: 61 6e 20 75 70 20 74 68 65 20 6d 69 64 20 61 6e  an up the mid an
16d0: 64 20 70 69 64 20 56 46 49 4c 45 20 65 6e 74 72  d pid VFILE entr
16e0: 69 65 73 2e 20 20 54 68 65 6e 20 63 6f 6d 6d 69  ies.  Then commi
16f0: 74 20 74 68 65 20 63 68 61 6e 67 65 73 2e 0a 20  t the changes.. 
1700: 20 2a 2f 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65   */.  db_multi_e
1710: 78 65 63 28 22 44 45 4c 45 54 45 20 46 52 4f 4d  xec("DELETE FROM
1720: 20 76 66 69 6c 65 20 57 48 45 52 45 20 76 69 64   vfile WHERE vid
1730: 21 3d 25 64 22 2c 20 74 69 64 29 3b 0a 20 20 6d  !=%d", tid);.  m
1740: 61 6e 69 66 65 73 74 5f 74 6f 5f 64 69 73 6b 28  anifest_to_disk(
1750: 74 69 64 29 3b 0a 20 20 64 62 5f 6c 73 65 74 5f  tid);.  db_lset_
1760: 69 6e 74 28 22 63 68 65 63 6b 6f 75 74 22 2c 20  int("checkout", 
1770: 74 69 64 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74  tid);.  db_end_t
1780: 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 7d  ransaction(0);.}
1790: 0a                                               .