Hex Artifact Content
Not logged in

Artifact 0d2cc71745fedc7c712d9c6ab313a020b59e8054:

File src/undo.c part of check-in [7b82a73bd3] - Remove the --yes option from the "revert" command. In its place, make the "revert" opration undoable. by drh on 2009-12-17 21:22:52. Also file src/undo.c part of check-in [76bc05d739] - merge with trunk by btheado on 2009-12-30 20:33:59.

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 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c  .** .** This fil
0370: 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
0380: 20 75 6e 64 6f 2f 72 65 64 6f 20 66 75 6e 63 74   undo/redo funct
0390: 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2f 0a 23 69 6e  ionality..*/.#in
03a0: 63 6c 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22  clude "config.h"
03b0: 0a 23 69 6e 63 6c 75 64 65 20 22 75 6e 64 6f 2e  .#include "undo.
03c0: 68 22 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f  h"..../*.** Undo
03d0: 20 74 68 65 20 63 68 61 6e 67 65 20 74 6f 20 74   the change to t
03e0: 68 65 20 66 69 6c 65 20 7a 50 61 74 68 6e 61 6d  he file zPathnam
03f0: 65 2e 20 20 7a 50 61 74 68 6e 61 6d 65 20 69 73  e.  zPathname is
0400: 20 74 68 65 20 70 61 74 68 6e 61 6d 65 0a 2a 2a   the pathname.**
0410: 20 6f 66 20 74 68 65 20 66 69 6c 65 20 72 65 6c   of the file rel
0420: 61 74 69 76 65 20 74 6f 20 74 68 65 20 72 6f 6f  ative to the roo
0430: 74 20 6f 66 20 74 68 65 20 72 65 70 6f 73 69 74  t of the reposit
0440: 6f 72 79 2e 20 20 49 66 20 72 65 64 6f 46 6c 61  ory.  If redoFla
0450: 67 20 69 73 0a 2a 2a 20 74 72 75 65 20 74 68 65  g is.** true the
0460: 20 72 65 64 6f 20 61 20 63 68 61 6e 67 65 2e 20   redo a change. 
0470: 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74   If there is not
0480: 68 69 6e 67 20 74 6f 20 75 6e 64 6f 20 28 6f 72  hing to undo (or
0490: 20 72 65 64 6f 29 20 74 68 65 6e 0a 2a 2a 20 74   redo) then.** t
04a0: 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
04b0: 20 6e 6f 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63   noop..*/.static
04c0: 20 76 6f 69 64 20 75 6e 64 6f 5f 6f 6e 65 28 63   void undo_one(c
04d0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
04e0: 6e 61 6d 65 2c 20 69 6e 74 20 72 65 64 6f 46 6c  name, int redoFl
04f0: 61 67 29 7b 0a 20 20 53 74 6d 74 20 71 3b 0a 20  ag){.  Stmt q;. 
0500: 20 63 68 61 72 20 2a 7a 46 75 6c 6c 6e 61 6d 65   char *zFullname
0510: 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26  ;.  db_prepare(&
0520: 71 2c 0a 20 20 20 20 22 53 45 4c 45 43 54 20 63  q,.    "SELECT c
0530: 6f 6e 74 65 6e 74 2c 20 65 78 69 73 74 73 66 6c  ontent, existsfl
0540: 61 67 20 46 52 4f 4d 20 75 6e 64 6f 20 57 48 45  ag FROM undo WHE
0550: 52 45 20 70 61 74 68 6e 61 6d 65 3d 25 51 20 41  RE pathname=%Q A
0560: 4e 44 20 72 65 64 6f 66 6c 61 67 3d 25 64 22 2c  ND redoflag=%d",
0570: 0a 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 2c  .     zPathname,
0580: 20 72 65 64 6f 46 6c 61 67 0a 20 20 29 3b 0a 20   redoFlag.  );. 
0590: 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 71 29   if( db_step(&q)
05a0: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
05b0: 20 20 20 20 69 6e 74 20 6f 6c 64 5f 65 78 69 73      int old_exis
05c0: 74 73 3b 0a 20 20 20 20 69 6e 74 20 6e 65 77 5f  ts;.    int new_
05d0: 65 78 69 73 74 73 3b 0a 20 20 20 20 42 6c 6f 62  exists;.    Blob
05e0: 20 63 75 72 72 65 6e 74 3b 0a 20 20 20 20 42 6c   current;.    Bl
05f0: 6f 62 20 6e 65 77 3b 0a 20 20 20 20 7a 46 75 6c  ob new;.    zFul
0600: 6c 6e 61 6d 65 20 3d 20 6d 70 72 69 6e 74 66 28  lname = mprintf(
0610: 22 25 73 2f 25 73 22 2c 20 67 2e 7a 4c 6f 63 61  "%s/%s", g.zLoca
0620: 6c 52 6f 6f 74 2c 20 7a 50 61 74 68 6e 61 6d 65  lRoot, zPathname
0630: 29 3b 0a 20 20 20 20 6e 65 77 5f 65 78 69 73 74  );.    new_exist
0640: 73 20 3d 20 66 69 6c 65 5f 73 69 7a 65 28 7a 46  s = file_size(zF
0650: 75 6c 6c 6e 61 6d 65 29 3e 3d 30 3b 0a 20 20 20  ullname)>=0;.   
0660: 20 69 66 28 20 6e 65 77 5f 65 78 69 73 74 73 20   if( new_exists 
0670: 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65  ){.      blob_re
0680: 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 63 75  ad_from_file(&cu
0690: 72 72 65 6e 74 2c 20 7a 46 75 6c 6c 6e 61 6d 65  rrent, zFullname
06a0: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
06b0: 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 63      blob_zero(&c
06c0: 75 72 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20  urrent);.    }. 
06d0: 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 6e 65     blob_zero(&ne
06e0: 77 29 3b 0a 20 20 20 20 6f 6c 64 5f 65 78 69 73  w);.    old_exis
06f0: 74 73 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69  ts = db_column_i
0700: 6e 74 28 26 71 2c 20 31 29 3b 0a 20 20 20 20 69  nt(&q, 1);.    i
0710: 66 28 20 6f 6c 64 5f 65 78 69 73 74 73 20 29 7b  f( old_exists ){
0720: 0a 20 20 20 20 20 20 64 62 5f 65 70 68 65 6d 65  .      db_epheme
0730: 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c 20  ral_blob(&q, 0, 
0740: 26 6e 65 77 29 3b 0a 20 20 20 20 7d 0a 20 20 20  &new);.    }.   
0750: 20 69 66 28 20 6f 6c 64 5f 65 78 69 73 74 73 20   if( old_exists 
0760: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 77  ){.      if( new
0770: 5f 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20  _exists ){.     
0780: 20 20 20 70 72 69 6e 74 66 28 22 25 73 20 25 73     printf("%s %s
0790: 5c 6e 22 2c 20 72 65 64 6f 46 6c 61 67 20 3f 20  \n", redoFlag ? 
07a0: 22 52 45 44 4f 22 20 3a 20 22 55 4e 44 4f 22 2c  "REDO" : "UNDO",
07b0: 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20   zPathname);.   
07c0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
07d0: 20 20 70 72 69 6e 74 66 28 22 4e 45 57 20 25 73    printf("NEW %s
07e0: 5c 6e 22 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b  \n", zPathname);
07f0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
0800: 6c 6f 62 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c  lob_write_to_fil
0810: 65 28 26 6e 65 77 2c 20 7a 46 75 6c 6c 6e 61 6d  e(&new, zFullnam
0820: 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
0830: 20 20 20 20 20 70 72 69 6e 74 66 28 22 44 45 4c       printf("DEL
0840: 45 54 45 20 25 73 5c 6e 22 2c 20 7a 50 61 74 68  ETE %s\n", zPath
0850: 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 75 6e 6c  name);.      unl
0860: 69 6e 6b 28 7a 46 75 6c 6c 6e 61 6d 65 29 3b 0a  ink(zFullname);.
0870: 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f 72      }.    blob_r
0880: 65 73 65 74 28 26 6e 65 77 29 3b 0a 20 20 20 20  eset(&new);.    
0890: 66 72 65 65 28 7a 46 75 6c 6c 6e 61 6d 65 29 3b  free(zFullname);
08a0: 0a 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65  .    db_finalize
08b0: 28 26 71 29 3b 0a 20 20 20 20 64 62 5f 70 72 65  (&q);.    db_pre
08c0: 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20 20 20  pare(&q, .      
08d0: 20 22 55 50 44 41 54 45 20 75 6e 64 6f 20 53 45   "UPDATE undo SE
08e0: 54 20 63 6f 6e 74 65 6e 74 3d 3a 63 2c 20 65 78  T content=:c, ex
08f0: 69 73 74 73 66 6c 61 67 3d 25 64 2c 20 72 65 64  istsflag=%d, red
0900: 6f 66 6c 61 67 3d 4e 4f 54 20 72 65 64 6f 66 6c  oflag=NOT redofl
0910: 61 67 22 0a 20 20 20 20 20 20 20 22 20 57 48 45  ag".       " WHE
0920: 52 45 20 70 61 74 68 6e 61 6d 65 3d 25 51 22 2c  RE pathname=%Q",
0930: 0a 20 20 20 20 20 20 20 6e 65 77 5f 65 78 69 73  .       new_exis
0940: 74 73 2c 20 7a 50 61 74 68 6e 61 6d 65 0a 20 20  ts, zPathname.  
0950: 20 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 77    );.    if( new
0960: 5f 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20  _exists ){.     
0970: 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 71   db_bind_blob(&q
0980: 2c 20 22 3a 63 22 2c 20 26 63 75 72 72 65 6e 74  , ":c", &current
0990: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 5f  );.    }.    db_
09a0: 73 74 65 70 28 26 71 29 3b 0a 20 20 20 20 62 6c  step(&q);.    bl
09b0: 6f 62 5f 72 65 73 65 74 28 26 63 75 72 72 65 6e  ob_reset(&curren
09c0: 74 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e  t);.  }.  db_fin
09d0: 61 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a 0a 2f 2a  alize(&q);.}../*
09e0: 0a 2a 2a 20 55 6e 64 6f 20 6f 72 20 72 65 64 6f  .** Undo or redo
09f0: 20 61 6c 6c 20 75 6e 64 6f 61 62 6c 65 20 6f 72   all undoable or
0a00: 20 72 65 64 6f 61 62 6c 65 20 63 68 61 6e 67 65   redoable change
0a10: 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
0a20: 64 20 75 6e 64 6f 5f 61 6c 6c 28 69 6e 74 20 72  d undo_all(int r
0a30: 65 64 6f 46 6c 61 67 29 7b 0a 20 20 53 74 6d 74  edoFlag){.  Stmt
0a40: 20 71 3b 0a 20 20 69 6e 74 20 75 63 69 64 3b 0a   q;.  int ucid;.
0a50: 20 20 69 6e 74 20 6e 63 69 64 3b 0a 20 20 64 62    int ncid;.  db
0a60: 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45  _prepare(&q, "SE
0a70: 4c 45 43 54 20 70 61 74 68 6e 61 6d 65 20 46 52  LECT pathname FR
0a80: 4f 4d 20 75 6e 64 6f 20 57 48 45 52 45 20 72 65  OM undo WHERE re
0a90: 64 6f 66 6c 61 67 3d 25 64 22 0a 20 20 20 20 20  doflag=%d".     
0aa0: 20 20 20 20 20 20 20 20 20 20 20 20 22 20 4f 52              " OR
0ab0: 44 45 52 20 42 59 20 2b 70 61 74 68 6e 61 6d 65  DER BY +pathname
0ac0: 22 2c 20 72 65 64 6f 46 6c 61 67 29 3b 0a 20 20  ", redoFlag);.  
0ad0: 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26  while( db_step(&
0ae0: 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  q)==SQLITE_ROW )
0af0: 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
0b00: 20 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 64 62   *zPathname = db
0b10: 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c  _column_text(&q,
0b20: 20 30 29 3b 0a 20 20 20 20 75 6e 64 6f 5f 6f 6e   0);.    undo_on
0b30: 65 28 7a 50 61 74 68 6e 61 6d 65 2c 20 72 65 64  e(zPathname, red
0b40: 6f 46 6c 61 67 29 3b 0a 20 20 7d 0a 20 20 64 62  oFlag);.  }.  db
0b50: 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20  _finalize(&q);. 
0b60: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a   db_multi_exec(.
0b70: 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50      "CREATE TEMP
0b80: 20 54 41 42 4c 45 20 75 6e 64 6f 5f 76 66 69 6c   TABLE undo_vfil
0b90: 65 5f 32 20 41 53 20 53 45 4c 45 43 54 20 2a 20  e_2 AS SELECT * 
0ba0: 46 52 4f 4d 20 76 66 69 6c 65 3b 22 0a 20 20 20  FROM vfile;".   
0bb0: 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 76 66   "DELETE FROM vf
0bc0: 69 6c 65 3b 22 0a 20 20 20 20 22 49 4e 53 45 52  ile;".    "INSER
0bd0: 54 20 49 4e 54 4f 20 76 66 69 6c 65 20 53 45 4c  T INTO vfile SEL
0be0: 45 43 54 20 2a 20 46 52 4f 4d 20 75 6e 64 6f 5f  ECT * FROM undo_
0bf0: 76 66 69 6c 65 3b 22 0a 20 20 20 20 22 44 45 4c  vfile;".    "DEL
0c00: 45 54 45 20 46 52 4f 4d 20 75 6e 64 6f 5f 76 66  ETE FROM undo_vf
0c10: 69 6c 65 3b 22 0a 20 20 20 20 22 49 4e 53 45 52  ile;".    "INSER
0c20: 54 20 49 4e 54 4f 20 75 6e 64 6f 5f 76 66 69 6c  T INTO undo_vfil
0c30: 65 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  e SELECT * FROM 
0c40: 75 6e 64 6f 5f 76 66 69 6c 65 5f 32 3b 22 0a 20  undo_vfile_2;". 
0c50: 20 20 20 22 44 52 4f 50 20 54 41 42 4c 45 20 75     "DROP TABLE u
0c60: 6e 64 6f 5f 76 66 69 6c 65 5f 32 3b 22 0a 20 20  ndo_vfile_2;".  
0c70: 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54    "CREATE TEMP T
0c80: 41 42 4c 45 20 75 6e 64 6f 5f 76 6d 65 72 67 65  ABLE undo_vmerge
0c90: 5f 32 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46  _2 AS SELECT * F
0ca0: 52 4f 4d 20 76 6d 65 72 67 65 3b 22 0a 20 20 20  ROM vmerge;".   
0cb0: 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 76 6d   "DELETE FROM vm
0cc0: 65 72 67 65 3b 22 0a 20 20 20 20 22 49 4e 53 45  erge;".    "INSE
0cd0: 52 54 20 49 4e 54 4f 20 76 6d 65 72 67 65 20 53  RT INTO vmerge S
0ce0: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 75 6e 64  ELECT * FROM und
0cf0: 6f 5f 76 6d 65 72 67 65 3b 22 0a 20 20 20 20 22  o_vmerge;".    "
0d00: 44 45 4c 45 54 45 20 46 52 4f 4d 20 75 6e 64 6f  DELETE FROM undo
0d10: 5f 76 6d 65 72 67 65 3b 22 0a 20 20 20 20 22 49  _vmerge;".    "I
0d20: 4e 53 45 52 54 20 49 4e 54 4f 20 75 6e 64 6f 5f  NSERT INTO undo_
0d30: 76 6d 65 72 67 65 20 53 45 4c 45 43 54 20 2a 20  vmerge SELECT * 
0d40: 46 52 4f 4d 20 75 6e 64 6f 5f 76 6d 65 72 67 65  FROM undo_vmerge
0d50: 5f 32 3b 22 0a 20 20 20 20 22 44 52 4f 50 20 54  _2;".    "DROP T
0d60: 41 42 4c 45 20 75 6e 64 6f 5f 76 6d 65 72 67 65  ABLE undo_vmerge
0d70: 5f 32 3b 22 0a 20 20 29 3b 0a 20 20 6e 63 69 64  _2;".  );.  ncid
0d80: 20 3d 20 64 62 5f 6c 67 65 74 5f 69 6e 74 28 22   = db_lget_int("
0d90: 75 6e 64 6f 5f 63 68 65 63 6b 6f 75 74 22 2c 20  undo_checkout", 
0da0: 30 29 3b 0a 20 20 75 63 69 64 20 3d 20 64 62 5f  0);.  ucid = db_
0db0: 6c 67 65 74 5f 69 6e 74 28 22 63 68 65 63 6b 6f  lget_int("checko
0dc0: 75 74 22 2c 20 30 29 3b 0a 20 20 64 62 5f 6c 73  ut", 0);.  db_ls
0dd0: 65 74 5f 69 6e 74 28 22 75 6e 64 6f 5f 63 68 65  et_int("undo_che
0de0: 63 6b 6f 75 74 22 2c 20 75 63 69 64 29 3b 0a 20  ckout", ucid);. 
0df0: 20 64 62 5f 6c 73 65 74 5f 69 6e 74 28 22 63 68   db_lset_int("ch
0e00: 65 63 6b 6f 75 74 22 2c 20 6e 63 69 64 29 3b 0a  eckout", ncid);.
0e10: 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74  }../*.** Reset t
0e20: 68 65 20 74 68 65 20 75 6e 64 6f 20 6d 65 6d 6f  he the undo memo
0e30: 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 75 6e 64 6f  ry..*/.void undo
0e40: 5f 72 65 73 65 74 28 76 6f 69 64 29 7b 0a 20 20  _reset(void){.  
0e50: 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
0e60: 72 20 7a 53 71 6c 5b 5d 20 3d 0a 20 20 20 20 40  r zSql[] =.    @
0e70: 20 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45   DROP TABLE IF E
0e80: 58 49 53 54 53 20 75 6e 64 6f 3b 0a 20 20 20 20  XISTS undo;.    
0e90: 40 20 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20  @ DROP TABLE IF 
0ea0: 45 58 49 53 54 53 20 75 6e 64 6f 5f 76 66 69 6c  EXISTS undo_vfil
0eb0: 65 3b 0a 20 20 20 20 40 20 44 52 4f 50 20 54 41  e;.    @ DROP TA
0ec0: 42 4c 45 20 49 46 20 45 58 49 53 54 53 20 75 6e  BLE IF EXISTS un
0ed0: 64 6f 5f 76 6d 65 72 67 65 3b 0a 20 20 20 20 3b  do_vmerge;.    ;
0ee0: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
0ef0: 28 7a 53 71 6c 29 3b 0a 20 20 64 62 5f 6c 73 65  (zSql);.  db_lse
0f00: 74 5f 69 6e 74 28 22 75 6e 64 6f 5f 61 76 61 69  t_int("undo_avai
0f10: 6c 61 62 6c 65 22 2c 20 30 29 3b 0a 20 20 64 62  lable", 0);.  db
0f20: 5f 6c 73 65 74 5f 69 6e 74 28 22 75 6e 64 6f 5f  _lset_int("undo_
0f30: 63 68 65 63 6b 6f 75 74 22 2c 20 30 29 3b 0a 7d  checkout", 0);.}
0f40: 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 63 61  ../*.** Begin ca
0f50: 70 74 75 72 69 6e 67 20 61 20 73 6e 61 70 73 68  pturing a snapsh
0f60: 6f 74 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  ot that can be u
0f70: 6e 64 6f 6e 65 2e 0a 2a 2f 0a 76 6f 69 64 20 75  ndone..*/.void u
0f80: 6e 64 6f 5f 62 65 67 69 6e 28 76 6f 69 64 29 7b  ndo_begin(void){
0f90: 0a 20 20 69 6e 74 20 63 69 64 3b 0a 20 20 73 74  .  int cid;.  st
0fa0: 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
0fb0: 7a 53 71 6c 5b 5d 20 3d 20 0a 20 20 20 20 40 20  zSql[] = .    @ 
0fc0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 75 6e 64  CREATE TABLE und
0fd0: 6f 28 0a 20 20 20 20 40 20 20 20 70 61 74 68 6e  o(.    @   pathn
0fe0: 61 6d 65 20 54 45 58 54 20 55 4e 49 51 55 45 2c  ame TEXT UNIQUE,
0ff0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
1000: 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65  Name of the file
1010: 0a 20 20 20 20 40 20 20 20 72 65 64 6f 66 6c 61  .    @   redofla
1020: 67 20 42 4f 4f 4c 45 41 4e 2c 20 20 20 20 20 20  g BOOLEAN,      
1030: 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 30 20             -- 0 
1040: 66 6f 72 20 75 6e 64 6f 61 62 6c 65 2e 20 20 31  for undoable.  1
1050: 20 66 6f 72 20 72 65 64 6f 61 62 6c 65 0a 20 20   for redoable.  
1060: 20 20 40 20 20 20 65 78 69 73 74 73 66 6c 61 67    @   existsflag
1070: 20 42 4f 4f 4c 45 41 4e 2c 20 20 20 20 20 20 20   BOOLEAN,       
1080: 20 20 20 20 20 20 20 20 2d 2d 20 54 72 75 65 20          -- True 
1090: 69 66 20 74 68 65 20 66 69 6c 65 20 65 78 69 73  if the file exis
10a0: 74 73 0a 20 20 20 20 40 20 20 20 63 6f 6e 74 65  ts.    @   conte
10b0: 6e 74 20 42 4c 4f 42 20 20 20 20 20 20 20 20 20  nt BLOB         
10c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
10d0: 53 61 76 65 64 20 63 6f 6e 74 65 6e 74 0a 20 20  Saved content.  
10e0: 20 20 40 20 29 3b 0a 20 20 20 20 40 20 43 52 45    @ );.    @ CRE
10f0: 41 54 45 20 54 41 42 4c 45 20 75 6e 64 6f 5f 76  ATE TABLE undo_v
1100: 66 69 6c 65 20 41 53 20 53 45 4c 45 43 54 20 2a  file AS SELECT *
1110: 20 46 52 4f 4d 20 76 66 69 6c 65 3b 0a 20 20 20   FROM vfile;.   
1120: 20 40 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   @ CREATE TABLE 
1130: 75 6e 64 6f 5f 76 6d 65 72 67 65 20 41 53 20 53  undo_vmerge AS S
1140: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 76 6d 65  ELECT * FROM vme
1150: 72 67 65 3b 0a 20 20 3b 0a 20 20 75 6e 64 6f 5f  rge;.  ;.  undo_
1160: 72 65 73 65 74 28 29 3b 0a 20 20 64 62 5f 6d 75  reset();.  db_mu
1170: 6c 74 69 5f 65 78 65 63 28 7a 53 71 6c 29 3b 0a  lti_exec(zSql);.
1180: 20 20 63 69 64 20 3d 20 64 62 5f 6c 67 65 74 5f    cid = db_lget_
1190: 69 6e 74 28 22 63 68 65 63 6b 6f 75 74 22 2c 20  int("checkout", 
11a0: 30 29 3b 0a 20 20 64 62 5f 6c 73 65 74 5f 69 6e  0);.  db_lset_in
11b0: 74 28 22 75 6e 64 6f 5f 63 68 65 63 6b 6f 75 74  t("undo_checkout
11c0: 22 2c 20 63 69 64 29 3b 0a 20 20 64 62 5f 6c 73  ", cid);.  db_ls
11d0: 65 74 5f 69 6e 74 28 22 75 6e 64 6f 5f 61 76 61  et_int("undo_ava
11e0: 69 6c 61 62 6c 65 22 2c 20 31 29 3b 0a 7d 0a 0a  ilable", 1);.}..
11f0: 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 63  /*.** Save the c
1200: 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f  urrent content o
1210: 66 20 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68  f the file zPath
1220: 6e 61 6d 65 20 73 6f 20 74 68 61 74 20 69 74 0a  name so that it.
1230: 2a 2a 20 77 69 6c 6c 20 62 65 20 75 6e 64 6f 61  ** will be undoa
1240: 62 6c 65 2e 20 20 54 68 65 20 6e 61 6d 65 20 69  ble.  The name i
1250: 73 20 72 65 6c 61 74 69 76 65 20 74 6f 20 74 68  s relative to th
1260: 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 0a 2a 2a  e root of the.**
1270: 20 74 72 65 65 2e 0a 2a 2f 0a 76 6f 69 64 20 75   tree..*/.void u
1280: 6e 64 6f 5f 73 61 76 65 28 63 6f 6e 73 74 20 63  ndo_save(const c
1290: 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d 65 29 7b  har *zPathname){
12a0: 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 6e 61  .  char *zFullna
12b0: 6d 65 3b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65  me;.  Blob conte
12c0: 6e 74 3b 0a 20 20 69 6e 74 20 65 78 69 73 74 73  nt;.  int exists
12d0: 46 6c 61 67 3b 0a 20 20 53 74 6d 74 20 71 3b 0a  Flag;.  Stmt q;.
12e0: 0a 20 20 7a 46 75 6c 6c 6e 61 6d 65 20 3d 20 6d  .  zFullname = m
12f0: 70 72 69 6e 74 66 28 22 25 73 2f 25 73 22 2c 20  printf("%s/%s", 
1300: 67 2e 7a 4c 6f 63 61 6c 52 6f 6f 74 2c 20 7a 50  g.zLocalRoot, zP
1310: 61 74 68 6e 61 6d 65 29 3b 0a 20 20 65 78 69 73  athname);.  exis
1320: 74 73 46 6c 61 67 20 3d 20 66 69 6c 65 5f 73 69  tsFlag = file_si
1330: 7a 65 28 7a 46 75 6c 6c 6e 61 6d 65 29 3e 3d 30  ze(zFullname)>=0
1340: 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65 28 26  ;.  db_prepare(&
1350: 71 2c 0a 20 20 20 20 22 52 45 50 4c 41 43 45 20  q,.    "REPLACE 
1360: 49 4e 54 4f 20 75 6e 64 6f 28 70 61 74 68 6e 61  INTO undo(pathna
1370: 6d 65 2c 72 65 64 6f 66 6c 61 67 2c 65 78 69 73  me,redoflag,exis
1380: 74 73 66 6c 61 67 2c 63 6f 6e 74 65 6e 74 29 22  tsflag,content)"
1390: 0a 20 20 20 20 22 20 56 41 4c 55 45 53 28 25 51  .    " VALUES(%Q
13a0: 2c 30 2c 25 64 2c 3a 63 29 22 2c 0a 20 20 20 20  ,0,%d,:c)",.    
13b0: 7a 50 61 74 68 6e 61 6d 65 2c 20 65 78 69 73 74  zPathname, exist
13c0: 73 46 6c 61 67 0a 20 20 29 3b 0a 20 20 69 66 28  sFlag.  );.  if(
13d0: 20 65 78 69 73 74 73 46 6c 61 67 20 29 7b 0a 20   existsFlag ){. 
13e0: 20 20 20 62 6c 6f 62 5f 72 65 61 64 5f 66 72 6f     blob_read_fro
13f0: 6d 5f 66 69 6c 65 28 26 63 6f 6e 74 65 6e 74 2c  m_file(&content,
1400: 20 7a 46 75 6c 6c 6e 61 6d 65 29 3b 0a 20 20 20   zFullname);.   
1410: 20 64 62 5f 62 69 6e 64 5f 62 6c 6f 62 28 26 71   db_bind_blob(&q
1420: 2c 20 22 3a 63 22 2c 20 26 63 6f 6e 74 65 6e 74  , ":c", &content
1430: 29 3b 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 46  );.  }.  free(zF
1440: 75 6c 6c 6e 61 6d 65 29 3b 0a 20 20 64 62 5f 73  ullname);.  db_s
1450: 74 65 70 28 26 71 29 3b 0a 20 20 64 62 5f 66 69  tep(&q);.  db_fi
1460: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 69 66  nalize(&q);.  if
1470: 28 20 65 78 69 73 74 73 46 6c 61 67 20 29 7b 0a  ( existsFlag ){.
1480: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26      blob_reset(&
1490: 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a  content);.  }.}.
14a0: 0a 2f 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20  ./*.** COMMAND: 
14b0: 75 6e 64 6f 0a 2a 2a 0a 2a 2a 20 55 73 61 67 65  undo.**.** Usage
14c0: 3a 20 25 66 6f 73 73 69 6c 20 75 6e 64 6f 20 3f  : %fossil undo ?
14d0: 46 49 4c 45 4e 41 4d 45 2e 2e 2e 3f 0a 2a 2a 0a  FILENAME...?.**.
14e0: 2a 2a 20 55 6e 64 6f 20 74 68 65 20 6d 6f 73 74  ** Undo the most
14f0: 20 72 65 63 65 6e 74 20 75 70 64 61 74 65 20 6f   recent update o
1500: 72 20 6d 65 72 67 65 20 6f 72 20 72 65 76 65 72  r merge or rever
1510: 74 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66  t operation.  If
1520: 20 46 49 4c 45 4e 41 4d 45 20 69 73 0a 2a 2a 20   FILENAME is.** 
1530: 73 70 65 63 69 66 69 65 64 20 74 68 65 6e 20 72  specified then r
1540: 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 65  estore the conte
1550: 6e 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20  nt of the named 
1560: 66 69 6c 65 28 73 29 20 62 75 74 20 6f 74 68 65  file(s) but othe
1570: 72 77 69 73 65 0a 2a 2a 20 6c 65 61 76 65 20 74  rwise.** leave t
1580: 68 65 20 75 70 64 61 74 65 20 6f 72 20 6d 65 72  he update or mer
1590: 67 65 20 6f 72 20 72 65 76 65 72 74 20 69 6e 20  ge or revert in 
15a0: 65 66 66 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 20  effect..**.** A 
15b0: 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 6f 66 20  single level of 
15c0: 75 6e 64 6f 2f 72 65 64 6f 20 69 73 20 73 75 70  undo/redo is sup
15d0: 70 6f 72 74 65 64 2e 20 20 54 68 65 20 75 6e 64  ported.  The und
15e0: 6f 2f 72 65 64 6f 20 73 74 61 63 6b 0a 2a 2a 20  o/redo stack.** 
15f0: 69 73 20 63 6c 65 61 72 65 64 20 62 79 20 74 68  is cleared by th
1600: 65 20 63 6f 6d 6d 69 74 20 61 6e 64 20 63 68 65  e commit and che
1610: 63 6b 6f 75 74 20 63 6f 6d 6d 61 6e 64 73 2e 0a  ckout commands..
1620: 2a 2f 0a 76 6f 69 64 20 75 6e 64 6f 5f 63 6d 64  */.void undo_cmd
1630: 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 75 6e  (void){.  int un
1640: 64 6f 5f 61 76 61 69 6c 61 62 6c 65 3b 0a 20 20  do_available;.  
1650: 64 62 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69  db_must_be_withi
1660: 6e 5f 74 72 65 65 28 29 3b 0a 20 20 64 62 5f 62  n_tree();.  db_b
1670: 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e  egin_transaction
1680: 28 29 3b 0a 20 20 75 6e 64 6f 5f 61 76 61 69 6c  ();.  undo_avail
1690: 61 62 6c 65 20 3d 20 64 62 5f 6c 67 65 74 5f 69  able = db_lget_i
16a0: 6e 74 28 22 75 6e 64 6f 5f 61 76 61 69 6c 61 62  nt("undo_availab
16b0: 6c 65 22 2c 20 30 29 3b 0a 20 20 69 66 28 20 67  le", 0);.  if( g
16c0: 2e 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20  .argc==2 ){.    
16d0: 69 66 28 20 75 6e 64 6f 5f 61 76 61 69 6c 61 62  if( undo_availab
16e0: 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 66  le!=1 ){.      f
16f0: 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 6e 6f 20  ossil_fatal("no 
1700: 75 70 64 61 74 65 20 6f 72 20 6d 65 72 67 65 20  update or merge 
1710: 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 76 61  operation is ava
1720: 69 6c 61 62 6c 65 20 74 6f 20 75 6e 64 6f 22 29  ilable to undo")
1730: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 64 6f  ;.    }.    undo
1740: 5f 61 6c 6c 28 30 29 3b 0a 20 20 20 20 64 62 5f  _all(0);.    db_
1750: 6c 73 65 74 5f 69 6e 74 28 22 75 6e 64 6f 5f 61  lset_int("undo_a
1760: 76 61 69 6c 61 62 6c 65 22 2c 20 32 29 3b 0a 20  vailable", 2);. 
1770: 20 7d 65 6c 73 65 20 69 66 28 20 67 2e 61 72 67   }else if( g.arg
1780: 63 3e 3d 33 20 29 7b 0a 20 20 20 20 69 6e 74 20  c>=3 ){.    int 
1790: 69 3b 0a 20 20 20 20 69 66 28 20 75 6e 64 6f 5f  i;.    if( undo_
17a0: 61 76 61 69 6c 61 62 6c 65 3d 3d 30 20 29 7b 0a  available==0 ){.
17b0: 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74        fossil_fat
17c0: 61 6c 28 22 6e 6f 20 75 70 64 61 74 65 20 6f 72  al("no update or
17d0: 20 6d 65 72 67 65 20 6f 70 65 72 61 74 69 6f 6e   merge operation
17e0: 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f   is available to
17f0: 20 75 6e 64 6f 22 29 3b 0a 20 20 20 20 7d 0a 20   undo");.    }. 
1800: 20 20 20 66 6f 72 28 69 3d 32 3b 20 69 3c 67 2e     for(i=2; i<g.
1810: 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  argc; i++){.    
1820: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
1830: 69 6c 65 20 3d 20 67 2e 61 72 67 76 5b 69 5d 3b  ile = g.argv[i];
1840: 0a 20 20 20 20 20 20 42 6c 6f 62 20 70 61 74 68  .      Blob path
1850: 3b 0a 20 20 20 20 20 20 66 69 6c 65 5f 74 72 65  ;.      file_tre
1860: 65 5f 6e 61 6d 65 28 7a 46 69 6c 65 2c 20 26 70  e_name(zFile, &p
1870: 61 74 68 2c 20 31 29 3b 0a 20 20 20 20 20 20 75  ath, 1);.      u
1880: 6e 64 6f 5f 6f 6e 65 28 62 6c 6f 62 5f 73 74 72  ndo_one(blob_str
1890: 28 26 70 61 74 68 29 2c 20 30 29 3b 0a 20 20 20  (&path), 0);.   
18a0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 70     blob_reset(&p
18b0: 61 74 68 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ath);.    }.  }.
18c0: 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63    db_end_transac
18d0: 74 69 6f 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  tion(0);.}../*.*
18e0: 2a 20 43 4f 4d 4d 41 4e 44 3a 20 72 65 64 6f 0a  * COMMAND: redo.
18f0: 2a 2a 0a 2a 2a 20 55 73 61 67 65 3a 20 25 66 6f  **.** Usage: %fo
1900: 73 73 69 6c 20 72 65 64 6f 20 3f 46 49 4c 45 4e  ssil redo ?FILEN
1910: 41 4d 45 2e 2e 2e 3f 0a 2a 2a 0a 2a 2a 20 52 65  AME...?.**.** Re
1920: 64 6f 20 74 68 65 20 61 6e 20 75 70 64 61 74 65  do the an update
1930: 20 6f 72 20 6d 65 72 67 65 20 6f 72 20 72 65 76   or merge or rev
1940: 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 20 74 68  ert operation th
1950: 61 74 20 68 61 73 20 62 65 65 6e 20 75 6e 64 6f  at has been undo
1960: 6e 65 0a 2a 2a 20 62 79 20 74 68 65 20 75 6e 64  ne.** by the und
1970: 6f 20 63 6f 6d 6d 61 6e 64 2e 20 20 49 66 20 46  o command.  If F
1980: 49 4c 45 4e 41 4d 45 20 69 73 20 73 70 65 63 69  ILENAME is speci
1990: 66 69 65 64 20 74 68 65 6e 20 72 65 73 74 6f 72  fied then restor
19a0: 65 20 74 68 65 20 63 68 61 6e 67 65 73 0a 2a 2a  e the changes.**
19b0: 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
19c0: 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 28   the named file(
19d0: 73 29 20 62 75 74 20 6f 74 68 65 72 77 69 73 65  s) but otherwise
19e0: 20 6c 65 61 76 65 20 74 68 65 20 75 70 64 61 74   leave the updat
19f0: 65 0a 2a 2a 20 6f 72 20 6d 65 72 67 65 20 75 6e  e.** or merge un
1a00: 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 69  done..**.** A si
1a10: 6e 67 6c 65 20 6c 65 76 65 6c 20 6f 66 20 75 6e  ngle level of un
1a20: 64 6f 2f 72 65 64 6f 20 69 73 20 73 75 70 70 6f  do/redo is suppo
1a30: 72 74 65 64 2e 20 20 54 68 65 20 75 6e 64 6f 2f  rted.  The undo/
1a40: 72 65 64 6f 20 73 74 61 63 6b 0a 2a 2a 20 69 73  redo stack.** is
1a50: 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65 20   cleared by the 
1a60: 63 6f 6d 6d 69 74 20 61 6e 64 20 63 68 65 63 6b  commit and check
1a70: 6f 75 74 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2f  out commands..*/
1a80: 0a 76 6f 69 64 20 72 65 64 6f 5f 63 6d 64 28 76  .void redo_cmd(v
1a90: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 75 6e 64 6f  oid){.  int undo
1aa0: 5f 61 76 61 69 6c 61 62 6c 65 3b 0a 20 20 64 62  _available;.  db
1ab0: 5f 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f  _must_be_within_
1ac0: 74 72 65 65 28 29 3b 0a 20 20 64 62 5f 62 65 67  tree();.  db_beg
1ad0: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
1ae0: 3b 0a 20 20 75 6e 64 6f 5f 61 76 61 69 6c 61 62  ;.  undo_availab
1af0: 6c 65 20 3d 20 64 62 5f 6c 67 65 74 5f 69 6e 74  le = db_lget_int
1b00: 28 22 75 6e 64 6f 5f 61 76 61 69 6c 61 62 6c 65  ("undo_available
1b10: 22 2c 20 30 29 3b 0a 20 20 69 66 28 20 67 2e 61  ", 0);.  if( g.a
1b20: 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66  rgc==2 ){.    if
1b30: 28 20 75 6e 64 6f 5f 61 76 61 69 6c 61 62 6c 65  ( undo_available
1b40: 21 3d 32 20 29 7b 0a 20 20 20 20 20 20 66 6f 73  !=2 ){.      fos
1b50: 73 69 6c 5f 66 61 74 61 6c 28 22 6e 6f 20 75 6e  sil_fatal("no un
1b60: 64 6f 6e 65 20 75 70 64 61 74 65 20 6f 72 20 6d  done update or m
1b70: 65 72 67 65 20 6f 70 65 72 61 74 69 6f 6e 20 69  erge operation i
1b80: 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 72  s available to r
1b90: 65 64 6f 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20  edo");.    }.   
1ba0: 20 75 6e 64 6f 5f 61 6c 6c 28 31 29 3b 0a 20 20   undo_all(1);.  
1bb0: 20 20 64 62 5f 6c 73 65 74 5f 69 6e 74 28 22 75    db_lset_int("u
1bc0: 6e 64 6f 5f 61 76 61 69 6c 61 62 6c 65 22 2c 20  ndo_available", 
1bd0: 31 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  1);.  }else if( 
1be0: 67 2e 61 72 67 63 3e 3d 33 20 29 7b 0a 20 20 20  g.argc>=3 ){.   
1bf0: 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28 20   int i;.    if( 
1c00: 75 6e 64 6f 5f 61 76 61 69 6c 61 62 6c 65 3d 3d  undo_available==
1c10: 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 73 73 69  0 ){.      fossi
1c20: 6c 5f 66 61 74 61 6c 28 22 6e 6f 20 75 70 64 61  l_fatal("no upda
1c30: 74 65 20 6f 72 20 6d 65 72 67 65 20 6f 70 65 72  te or merge oper
1c40: 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62  ation is availab
1c50: 6c 65 20 74 6f 20 72 65 64 6f 22 29 3b 0a 20 20  le to redo");.  
1c60: 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 32 3b    }.    for(i=2;
1c70: 20 69 3c 67 2e 61 72 67 63 3b 20 69 2b 2b 29 7b   i<g.argc; i++){
1c80: 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
1c90: 72 20 2a 7a 46 69 6c 65 20 3d 20 67 2e 61 72 67  r *zFile = g.arg
1ca0: 76 5b 69 5d 3b 0a 20 20 20 20 20 20 42 6c 6f 62  v[i];.      Blob
1cb0: 20 70 61 74 68 3b 0a 20 20 20 20 20 20 66 69 6c   path;.      fil
1cc0: 65 5f 74 72 65 65 5f 6e 61 6d 65 28 7a 46 69 6c  e_tree_name(zFil
1cd0: 65 2c 20 26 70 61 74 68 2c 20 31 29 3b 0a 20 20  e, &path, 1);.  
1ce0: 20 20 20 20 75 6e 64 6f 5f 6f 6e 65 28 62 6c 6f      undo_one(blo
1cf0: 62 5f 73 74 72 28 26 70 61 74 68 29 2c 20 30 29  b_str(&path), 0)
1d00: 3b 0a 20 20 20 20 20 20 62 6c 6f 62 5f 72 65 73  ;.      blob_res
1d10: 65 74 28 26 70 61 74 68 29 3b 0a 20 20 20 20 7d  et(&path);.    }
1d20: 0a 20 20 7d 0a 20 20 64 62 5f 65 6e 64 5f 74 72  .  }.  db_end_tr
1d30: 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a  ansaction(0);.}.