Hex Artifact Content
Not logged in

Artifact f281e3565d4660dfed9a13ee628de23d136b0327:

File src/update.c part of check-in [a36177bcce] - Add the "undo" and "redo" commands. Untested at this point so don't try to use them yet. by drh on 2007-09-11 02:25:19.

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 75 6e 64 6f  ure(vid);.  undo
0960: 5f 62 65 67 69 6e 28 29 3b 0a 20 20 6c 6f 61 64  _begin();.  load
0970: 5f 76 66 69 6c 65 5f 66 72 6f 6d 5f 72 69 64 28  _vfile_from_rid(
0980: 74 69 64 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  tid);..  /*.  **
0990: 20 54 68 65 20 72 65 63 6f 72 64 2e 66 6e 20 66   The record.fn f
09a0: 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f 20  ield is used to 
09b0: 6d 61 74 63 68 20 66 69 6c 65 73 20 61 67 61 69  match files agai
09c0: 6e 73 74 20 65 61 63 68 20 6f 74 68 65 72 2e 20  nst each other. 
09d0: 20 54 68 65 0a 20 20 2a 2a 20 46 56 20 74 61 62   The.  ** FV tab
09e0: 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20  le contains one 
09f0: 72 6f 77 20 66 6f 72 20 65 61 63 68 20 65 61 63  row for each eac
0a00: 68 20 75 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d  h unique filenam
0a10: 65 20 69 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65  e in.  ** in the
0a20: 20 63 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75   current checkou
0a30: 74 2c 20 74 68 65 20 70 69 76 6f 74 2c 20 61 6e  t, the pivot, an
0a40: 64 20 74 68 65 20 76 65 72 73 69 6f 6e 20 62 65  d the version be
0a50: 69 6e 67 20 6d 65 72 67 65 64 2e 0a 20 20 2a 2f  ing merged..  */
0a60: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
0a70: 28 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c  (.    "DROP TABL
0a80: 45 20 49 46 20 45 58 49 53 54 53 20 66 76 3b 22  E IF EXISTS fv;"
0a90: 0a 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d  .    "CREATE TEM
0aa0: 50 20 54 41 42 4c 45 20 66 76 28 22 0a 20 20 20  P TABLE fv(".   
0ab0: 20 22 20 20 66 6e 20 54 45 58 54 20 50 52 49 4d   "  fn TEXT PRIM
0ac0: 41 52 59 20 4b 45 59 2c 22 20 20 20 20 20 20 2f  ARY KEY,"      /
0ad0: 2a 20 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 2a  * The filename *
0ae0: 2f 0a 20 20 20 20 22 20 20 69 64 76 20 49 4e 54  /.    "  idv INT
0af0: 45 47 45 52 2c 22 20 20 20 20 20 20 20 20 20 20  EGER,"          
0b00: 20 20 20 20 2f 2a 20 56 46 49 4c 45 20 65 6e 74      /* VFILE ent
0b10: 72 79 20 66 6f 72 20 63 75 72 72 65 6e 74 20 76  ry for current v
0b20: 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 22 20  ersion */.    " 
0b30: 20 69 64 74 20 49 4e 54 45 47 45 52 2c 22 20 20   idt INTEGER,"  
0b40: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
0b50: 46 49 4c 45 20 65 6e 74 72 79 20 66 6f 72 20 74  FILE entry for t
0b60: 61 72 67 65 74 20 76 65 72 73 69 6f 6e 20 2a 2f  arget version */
0b70: 0a 20 20 20 20 22 20 20 63 68 6e 67 65 64 20 42  .    "  chnged B
0b80: 4f 4f 4c 45 41 4e 2c 22 20 20 20 20 20 20 20 20  OOLEAN,"        
0b90: 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 75     /* True if cu
0ba0: 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 68 61  rrent version ha
0bb0: 73 20 62 65 65 6e 20 65 64 69 74 65 64 20 2a 2f  s been edited */
0bc0: 0a 20 20 20 20 22 20 20 72 69 64 76 20 49 4e 54  .    "  ridv INT
0bd0: 45 47 45 52 2c 22 20 20 20 20 20 20 20 20 20 20  EGER,"          
0be0: 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 49 44 20     /* Record ID 
0bf0: 66 6f 72 20 63 75 72 72 65 6e 74 20 76 65 72 73  for current vers
0c00: 69 6f 6e 20 2a 2f 0a 20 20 20 20 22 20 20 72 69  ion */.    "  ri
0c10: 64 74 20 49 4e 54 45 47 45 52 20 22 20 20 20 20  dt INTEGER "    
0c20: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 63 6f           /* Reco
0c30: 72 64 20 49 44 20 66 6f 72 20 74 61 72 67 65 74  rd ID for target
0c40: 20 2a 2f 0a 20 20 20 20 22 29 3b 22 0a 20 20 20   */.    ");".   
0c50: 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f   "INSERT OR IGNO
0c60: 52 45 20 49 4e 54 4f 20 66 76 22 0a 20 20 20 20  RE INTO fv".    
0c70: 22 20 53 45 4c 45 43 54 20 70 61 74 68 6e 61 6d  " SELECT pathnam
0c80: 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  e, 0, 0, 0, 0, 0
0c90: 20 46 52 4f 4d 20 76 66 69 6c 65 22 0a 20 20 29   FROM vfile".  )
0ca0: 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26  ;.  db_prepare(&
0cb0: 71 2c 0a 20 20 20 20 22 53 45 4c 45 43 54 20 69  q,.    "SELECT i
0cc0: 64 2c 20 70 61 74 68 6e 61 6d 65 2c 20 72 69 64  d, pathname, rid
0cd0: 20 46 52 4f 4d 20 76 66 69 6c 65 22 0a 20 20 20   FROM vfile".   
0ce0: 20 22 20 57 48 45 52 45 20 76 69 64 3d 25 64 22   " WHERE vid=%d"
0cf0: 2c 20 74 69 64 0a 20 20 29 3b 0a 20 20 77 68 69  , tid.  );.  whi
0d00: 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d  le( db_step(&q)=
0d10: 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
0d20: 20 20 20 69 6e 74 20 69 64 20 3d 20 64 62 5f 63     int id = db_c
0d30: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29  olumn_int(&q, 0)
0d40: 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
0d50: 20 2a 66 6e 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e   *fn = db_column
0d60: 5f 74 65 78 74 28 26 71 2c 20 31 29 3b 0a 20 20  _text(&q, 1);.  
0d70: 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63    int rid = db_c
0d80: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29  olumn_int(&q, 2)
0d90: 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65  ;.    db_multi_e
0da0: 78 65 63 28 0a 20 20 20 20 20 20 22 55 50 44 41  xec(.      "UPDA
0db0: 54 45 20 66 76 20 53 45 54 20 69 64 74 3d 25 64  TE fv SET idt=%d
0dc0: 2c 20 72 69 64 74 3d 25 64 20 57 48 45 52 45 20  , ridt=%d WHERE 
0dd0: 66 6e 3d 25 51 22 2c 0a 20 20 20 20 20 20 69 64  fn=%Q",.      id
0de0: 2c 20 72 69 64 2c 20 66 6e 0a 20 20 20 20 29 3b  , rid, fn.    );
0df0: 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69  .  }.  db_finali
0e00: 7a 65 28 26 71 29 3b 0a 20 20 64 62 5f 70 72 65  ze(&q);.  db_pre
0e10: 70 61 72 65 28 26 71 2c 0a 20 20 20 20 22 53 45  pare(&q,.    "SE
0e20: 4c 45 43 54 20 69 64 2c 20 70 61 74 68 6e 61 6d  LECT id, pathnam
0e30: 65 2c 20 72 69 64 2c 20 63 68 6e 67 65 64 20 46  e, rid, chnged F
0e40: 52 4f 4d 20 76 66 69 6c 65 22 0a 20 20 20 20 22  ROM vfile".    "
0e50: 20 57 48 45 52 45 20 76 69 64 3d 25 64 22 2c 20   WHERE vid=%d", 
0e60: 76 69 64 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65  vid.  );.  while
0e70: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
0e80: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
0e90: 20 69 6e 74 20 69 64 20 3d 20 64 62 5f 63 6f 6c   int id = db_col
0ea0: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b 0a  umn_int(&q, 0);.
0eb0: 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
0ec0: 66 6e 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74  fn = db_column_t
0ed0: 65 78 74 28 26 71 2c 20 31 29 3b 0a 20 20 20 20  ext(&q, 1);.    
0ee0: 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63 6f 6c  int rid = db_col
0ef0: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29 3b 0a  umn_int(&q, 2);.
0f00: 20 20 20 20 69 6e 74 20 63 68 6e 67 65 64 20 3d      int chnged =
0f10: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26   db_column_int(&
0f20: 71 2c 20 33 29 3b 0a 20 20 20 20 64 62 5f 6d 75  q, 3);.    db_mu
0f30: 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20  lti_exec(.      
0f40: 22 55 50 44 41 54 45 20 66 76 20 53 45 54 20 69  "UPDATE fv SET i
0f50: 64 76 3d 25 64 2c 20 72 69 64 76 3d 25 64 2c 20  dv=%d, ridv=%d, 
0f60: 63 68 6e 67 65 64 3d 25 64 20 57 48 45 52 45 20  chnged=%d WHERE 
0f70: 66 6e 3d 25 51 22 2c 0a 20 20 20 20 20 20 69 64  fn=%Q",.      id
0f80: 2c 20 72 69 64 2c 20 63 68 6e 67 65 64 2c 20 66  , rid, chnged, f
0f90: 6e 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20 64  n.    );.  }.  d
0fa0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a  b_finalize(&q);.
0fb0: 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71  .  db_prepare(&q
0fc0: 2c 20 0a 20 20 20 20 22 53 45 4c 45 43 54 20 66  , .    "SELECT f
0fd0: 6e 2c 20 69 64 76 2c 20 72 69 64 76 2c 20 69 64  n, idv, ridv, id
0fe0: 74 2c 20 72 69 64 74 2c 20 63 68 6e 67 65 64 20  t, ridt, chnged 
0ff0: 46 52 4f 4d 20 66 76 20 4f 52 44 45 52 20 42 59  FROM fv ORDER BY
1000: 20 31 22 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65   1".  );.  while
1010: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
1020: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
1030: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1040: 6d 65 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74  me = db_column_t
1050: 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20  ext(&q, 0);.    
1060: 69 6e 74 20 69 64 76 20 3d 20 64 62 5f 63 6f 6c  int idv = db_col
1070: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 31 29 3b 0a  umn_int(&q, 1);.
1080: 20 20 20 20 69 6e 74 20 72 69 64 76 20 3d 20 64      int ridv = d
1090: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c  b_column_int(&q,
10a0: 20 32 29 3b 0a 20 20 20 20 69 6e 74 20 69 64 74   2);.    int idt
10b0: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74   = db_column_int
10c0: 28 26 71 2c 20 33 29 3b 0a 20 20 20 20 69 6e 74  (&q, 3);.    int
10d0: 20 72 69 64 74 20 3d 20 64 62 5f 63 6f 6c 75 6d   ridt = db_colum
10e0: 6e 5f 69 6e 74 28 26 71 2c 20 34 29 3b 0a 20 20  n_int(&q, 4);.  
10f0: 20 20 69 6e 74 20 63 68 6e 67 65 64 20 3d 20 64    int chnged = d
1100: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c  b_column_int(&q,
1110: 20 35 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 64   5);..    if( id
1120: 76 3e 30 20 26 26 20 72 69 64 76 3d 3d 30 20 26  v>0 && ridv==0 &
1130: 26 20 69 64 74 3e 30 20 29 7b 0a 20 20 20 20 20  & idt>0 ){.     
1140: 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 2e 20 20 54   /* Conflict.  T
1150: 68 69 73 20 66 69 6c 65 20 68 61 73 20 62 65 65  his file has bee
1160: 6e 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 63  n added to the c
1170: 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 0a  urrent checkout.
1180: 20 20 20 20 20 20 2a 2a 20 62 75 74 20 61 6c 73        ** but als
1190: 6f 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20  o exists in the 
11a0: 74 61 72 67 65 74 20 63 68 65 63 6b 6f 75 74 2e  target checkout.
11b0: 20 20 55 73 65 20 74 68 65 20 63 75 72 72 65 6e    Use the curren
11c0: 74 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20 20 20  t version..     
11d0: 20 2a 2f 0a 20 20 20 20 20 20 70 72 69 6e 74 66   */.      printf
11e0: 28 22 43 4f 4e 46 4c 49 43 54 20 25 73 5c 6e 22  ("CONFLICT %s\n"
11f0: 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65  , zName);.    }e
1200: 6c 73 65 20 69 66 28 20 69 64 74 3e 30 20 26 26  lse if( idt>0 &&
1210: 20 69 64 76 3d 3d 30 20 29 7b 0a 20 20 20 20 20   idv==0 ){.     
1220: 20 2f 2a 20 46 69 6c 65 20 61 64 64 65 64 20 69   /* File added i
1230: 6e 20 74 68 65 20 74 61 72 67 65 74 2e 20 2a 2f  n the target. */
1240: 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 41  .      printf("A
1250: 44 44 20 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29  DD %s\n", zName)
1260: 3b 0a 20 20 20 20 20 20 75 6e 64 6f 5f 73 61 76  ;.      undo_sav
1270: 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  e(zName);.      
1280: 76 66 69 6c 65 5f 74 6f 5f 64 69 73 6b 28 30 2c  vfile_to_disk(0,
1290: 20 69 64 74 2c 20 30 29 3b 0a 20 20 20 20 7d 65   idt, 0);.    }e
12a0: 6c 73 65 20 69 66 28 20 69 64 74 3e 30 20 26 26  lse if( idt>0 &&
12b0: 20 69 64 76 3e 30 20 26 26 20 72 69 64 74 21 3d   idv>0 && ridt!=
12c0: 72 69 64 76 20 26 26 20 63 68 6e 67 65 64 3d 3d  ridv && chnged==
12d0: 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  0 ){.      /* Th
12e0: 65 20 66 69 6c 65 20 69 73 20 75 6e 65 64 69 74  e file is unedit
12f0: 65 64 2e 20 20 43 68 61 6e 67 65 20 69 74 20 74  ed.  Change it t
1300: 6f 20 74 68 65 20 74 61 72 67 65 74 20 76 65 72  o the target ver
1310: 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 70 72  sion */.      pr
1320: 69 6e 74 66 28 22 55 50 44 41 54 45 20 25 73 5c  intf("UPDATE %s\
1330: 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  n", zName);.    
1340: 20 20 75 6e 64 6f 5f 73 61 76 65 28 7a 4e 61 6d    undo_save(zNam
1350: 65 29 3b 0a 20 20 20 20 20 20 76 66 69 6c 65 5f  e);.      vfile_
1360: 74 6f 5f 64 69 73 6b 28 30 2c 20 69 64 74 2c 20  to_disk(0, idt, 
1370: 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  0);.    }else if
1380: 28 20 69 64 74 3d 3d 30 20 26 26 20 69 64 76 3e  ( idt==0 && idv>
1390: 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63  0 ){.      if( c
13a0: 68 6e 67 65 64 20 29 7b 0a 20 20 20 20 20 20 20  hnged ){.       
13b0: 20 70 72 69 6e 74 66 28 22 43 4f 4e 46 4c 49 43   printf("CONFLIC
13c0: 54 20 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b  T %s\n", zName);
13d0: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
13e0: 20 20 20 20 20 20 63 68 61 72 20 2a 7a 46 75 6c        char *zFul
13f0: 6c 50 61 74 68 3b 0a 20 20 20 20 20 20 20 20 70  lPath;.        p
1400: 72 69 6e 74 66 28 22 52 45 4d 4f 56 45 20 25 73  rintf("REMOVE %s
1410: 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  \n", zName);.   
1420: 20 20 20 20 20 75 6e 64 6f 5f 73 61 76 65 28 7a       undo_save(z
1430: 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7a  Name);.        z
1440: 46 75 6c 6c 50 61 74 68 20 3d 20 6d 70 72 69 6e  FullPath = mprin
1450: 74 66 28 22 25 73 2f 25 73 22 2c 20 67 2e 7a 4c  tf("%s/%s", g.zL
1460: 6f 63 61 6c 52 6f 6f 74 2c 20 7a 4e 61 6d 65 29  ocalRoot, zName)
1470: 3b 0a 20 20 20 20 20 20 20 20 75 6e 6c 69 6e 6b  ;.        unlink
1480: 28 7a 46 75 6c 6c 50 61 74 68 29 3b 0a 20 20 20  (zFullPath);.   
1490: 20 20 20 20 20 66 72 65 65 28 7a 46 75 6c 6c 50       free(zFullP
14a0: 61 74 68 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ath);.      }.  
14b0: 20 20 7d 65 6c 73 65 20 69 66 28 20 69 64 74 3e    }else if( idt>
14c0: 30 20 26 26 20 69 64 76 3e 30 20 26 26 20 72 69  0 && idv>0 && ri
14d0: 64 74 21 3d 72 69 64 76 20 26 26 20 63 68 6e 67  dt!=ridv && chng
14e0: 65 64 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4d  ed ){.      /* M
14f0: 65 72 67 65 20 74 68 65 20 63 68 61 6e 67 65 73  erge the changes
1500: 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
1510: 74 72 65 65 20 69 6e 74 6f 20 74 68 65 20 74 61  tree into the ta
1520: 72 67 65 74 20 76 65 72 73 69 6f 6e 20 2a 2f 0a  rget version */.
1530: 20 20 20 20 20 20 42 6c 6f 62 20 65 2c 20 72 2c        Blob e, r,
1540: 20 74 2c 20 76 3b 0a 20 20 20 20 20 20 63 68 61   t, v;.      cha
1550: 72 20 2a 7a 46 75 6c 6c 50 61 74 68 3b 0a 20 20  r *zFullPath;.  
1560: 20 20 20 20 70 72 69 6e 74 66 28 22 4d 45 52 47      printf("MERG
1570: 45 20 25 73 5c 6e 22 2c 20 7a 4e 61 6d 65 29 3b  E %s\n", zName);
1580: 0a 20 20 20 20 20 20 75 6e 64 6f 5f 73 61 76 65  .      undo_save
1590: 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7a  (zName);.      z
15a0: 46 75 6c 6c 50 61 74 68 20 3d 20 6d 70 72 69 6e  FullPath = mprin
15b0: 74 66 28 22 25 73 2f 25 73 22 2c 20 67 2e 7a 4c  tf("%s/%s", g.zL
15c0: 6f 63 61 6c 52 6f 6f 74 2c 20 7a 4e 61 6d 65 29  ocalRoot, zName)
15d0: 3b 0a 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f  ;.      content_
15e0: 67 65 74 28 72 69 64 74 2c 20 26 74 29 3b 0a 20  get(ridt, &t);. 
15f0: 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74       content_get
1600: 28 72 69 64 76 2c 20 26 76 29 3b 0a 20 20 20 20  (ridv, &v);.    
1610: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 65 29 3b    blob_zero(&e);
1620: 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 61 64  .      blob_read
1630: 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 65 2c 20 7a  _from_file(&e, z
1640: 46 75 6c 6c 50 61 74 68 29 3b 0a 20 20 20 20 20  FullPath);.     
1650: 20 62 6c 6f 62 5f 6d 65 72 67 65 28 26 76 2c 20   blob_merge(&v, 
1660: 26 65 2c 20 26 74 2c 20 26 72 29 3b 0a 20 20 20  &e, &t, &r);.   
1670: 20 20 20 62 6c 6f 62 5f 77 72 69 74 65 5f 74 6f     blob_write_to
1680: 5f 66 69 6c 65 28 26 72 2c 20 7a 46 75 6c 6c 50  _file(&r, zFullP
1690: 61 74 68 29 3b 0a 20 20 20 20 20 20 66 72 65 65  ath);.      free
16a0: 28 7a 46 75 6c 6c 50 61 74 68 29 3b 0a 20 20 20  (zFullPath);.   
16b0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 76     blob_reset(&v
16c0: 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65  );.      blob_re
16d0: 73 65 74 28 26 65 29 3b 0a 20 20 20 20 20 20 62  set(&e);.      b
16e0: 6c 6f 62 5f 72 65 73 65 74 28 26 74 29 3b 0a 20  lob_reset(&t);. 
16f0: 20 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28       blob_reset(
1700: 26 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  &r);.    }.  }. 
1710: 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29   db_finalize(&q)
1720: 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43  ;.  .  /*.  ** C
1730: 6c 65 61 6e 20 75 70 20 74 68 65 20 6d 69 64 20  lean up the mid 
1740: 61 6e 64 20 70 69 64 20 56 46 49 4c 45 20 65 6e  and pid VFILE en
1750: 74 72 69 65 73 2e 20 20 54 68 65 6e 20 63 6f 6d  tries.  Then com
1760: 6d 69 74 20 74 68 65 20 63 68 61 6e 67 65 73 2e  mit the changes.
1770: 0a 20 20 2a 2f 0a 20 20 64 62 5f 6d 75 6c 74 69  .  */.  db_multi
1780: 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 46 52  _exec("DELETE FR
1790: 4f 4d 20 76 66 69 6c 65 20 57 48 45 52 45 20 76  OM vfile WHERE v
17a0: 69 64 21 3d 25 64 22 2c 20 74 69 64 29 3b 0a 20  id!=%d", tid);. 
17b0: 20 6d 61 6e 69 66 65 73 74 5f 74 6f 5f 64 69 73   manifest_to_dis
17c0: 6b 28 74 69 64 29 3b 0a 20 20 64 62 5f 6c 73 65  k(tid);.  db_lse
17d0: 74 5f 69 6e 74 28 22 63 68 65 63 6b 6f 75 74 22  t_int("checkout"
17e0: 2c 20 74 69 64 29 3b 0a 20 20 64 62 5f 65 6e 64  , tid);.  db_end
17f0: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
1800: 0a 7d 0a                                         .}.