Hex Artifact Content
Not logged in

Artifact e01dd37d11fbb0bb9d5bc3957f07c98c8f9c92a6:

File src/merge3.c part of check-in [ba9af9aced] - Fix the merge conflict detection. by drh on 2007-11-29 00:37:41. Also file src/merge3.c part of check-in [d0305b305a] - Merged mainline into my branch to get the newest application. by aku on 2007-12-05 08:07:46.

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 6d 6f 64 75  .**.** This modu
0370: 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  le implements a 
0380: 33 2d 77 61 79 20 6d 65 72 67 65 0a 2a 2f 0a 23  3-way merge.*/.#
0390: 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 67 2e  include "config.
03a0: 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 6d 65 72  h".#include "mer
03b0: 67 65 33 2e 68 22 0a 0a 23 69 66 20 30 0a 23 64  ge3.h"..#if 0.#d
03c0: 65 66 69 6e 65 20 44 45 42 55 47 28 58 29 20 20  efine DEBUG(X)  
03d0: 58 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  X.#else.#define 
03e0: 44 45 42 55 47 28 58 29 0a 23 65 6e 64 69 66 0a  DEBUG(X).#endif.
03f0: 0a 2f 2a 0a 2a 2a 20 4f 70 63 6f 64 65 73 0a 2a  ./*.** Opcodes.*
0400: 2f 0a 23 64 65 66 69 6e 65 20 43 50 59 20 30 0a  /.#define CPY 0.
0410: 23 64 65 66 69 6e 65 20 44 45 4c 20 31 0a 23 64  #define DEL 1.#d
0420: 65 66 69 6e 65 20 49 4e 53 20 32 0a 23 64 65 66  efine INS 2.#def
0430: 69 6e 65 20 45 4e 44 20 33 0a 23 64 65 66 69 6e  ine END 3.#defin
0440: 65 20 55 4e 4b 20 34 0a 0a 2f 2a 0a 2a 2a 20 43  e UNK 4../*.** C
0450: 6f 6d 70 61 72 65 20 61 20 73 69 6e 67 6c 65 20  ompare a single 
0460: 6c 69 6e 65 20 6f 66 20 74 65 78 74 20 66 72 6f  line of text fro
0470: 6d 20 70 56 31 20 61 6e 64 20 70 56 32 2e 20 20  m pV1 and pV2.  
0480: 49 66 20 74 68 65 20 6c 69 6e 65 73 0a 2a 2a 20  If the lines.** 
0490: 61 72 65 20 74 68 65 20 73 61 6d 65 2c 20 72 65  are the same, re
04a0: 74 75 72 6e 20 74 72 75 65 2e 20 20 52 65 74 75  turn true.  Retu
04b0: 72 6e 20 66 61 6c 73 65 20 69 66 20 74 68 65 79  rn false if they
04c0: 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 0a   are different..
04d0: 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72  **.** The cursor
04e0: 20 6f 6e 20 62 6f 74 68 20 70 56 31 20 61 6e 64   on both pV1 and
04f0: 20 70 56 32 20 69 73 20 75 6e 63 68 61 6e 67 65   pV2 is unchange
0500: 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
0510: 20 73 61 6d 65 4c 69 6e 65 28 42 6c 6f 62 20 2a   sameLine(Blob *
0520: 70 56 31 2c 20 42 6c 6f 62 20 2a 70 56 32 29 7b  pV1, Blob *pV2){
0530: 0a 20 20 63 68 61 72 20 2a 7a 31 2c 20 2a 7a 32  .  char *z1, *z2
0540: 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 7a 31  ;.  int i;..  z1
0550: 20 3d 20 26 62 6c 6f 62 5f 62 75 66 66 65 72 28   = &blob_buffer(
0560: 70 56 31 29 5b 62 6c 6f 62 5f 74 65 6c 6c 28 70  pV1)[blob_tell(p
0570: 56 31 29 5d 3b 0a 20 20 7a 32 20 3d 20 26 62 6c  V1)];.  z2 = &bl
0580: 6f 62 5f 62 75 66 66 65 72 28 70 56 32 29 5b 62  ob_buffer(pV2)[b
0590: 6c 6f 62 5f 74 65 6c 6c 28 70 56 32 29 5d 3b 0a  lob_tell(pV2)];.
05a0: 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d    for(i=0; z1[i]
05b0: 21 3d 27 5c 6e 27 20 26 26 20 7a 31 5b 69 5d 3d  !='\n' && z1[i]=
05c0: 3d 7a 32 5b 69 5d 3b 20 69 2b 2b 29 7b 7d 0a 20  =z2[i]; i++){}. 
05d0: 20 72 65 74 75 72 6e 20 7a 32 5b 69 5d 3d 3d 27   return z2[i]=='
05e0: 5c 6e 27 20 7c 7c 20 28 7a 32 5b 69 5d 3d 3d 27  \n' || (z2[i]=='
05f0: 5c 72 27 20 26 26 20 7a 32 5b 69 2b 31 5d 3d 3d  \r' && z2[i+1]==
0600: 27 5c 6e 27 29 0a 20 20 20 20 20 20 20 20 20 20  '\n').          
0610: 7c 7c 20 28 7a 31 5b 69 5d 3d 3d 27 5c 72 27 20  || (z1[i]=='\r' 
0620: 26 26 20 7a 32 5b 69 5d 3d 3d 27 5c 6e 27 20 26  && z2[i]=='\n' &
0630: 26 20 7a 31 5b 69 2b 31 5d 3d 3d 27 5c 6e 27 29  & z1[i+1]=='\n')
0640: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20  ;.}../*.** Do a 
0650: 74 68 72 65 65 2d 77 61 79 20 6d 65 72 67 65 2e  three-way merge.
0660: 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 4f 75    Initialize pOu
0670: 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65  t to contain the
0680: 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54   result..**.** T
0690: 68 65 20 6d 65 72 67 65 20 69 73 20 61 6e 20 65  he merge is an e
06a0: 64 69 74 20 61 67 61 69 6e 73 74 20 70 56 32 2e  dit against pV2.
06b0: 20 20 42 6f 74 68 20 70 56 31 20 61 6e 64 20 70    Both pV1 and p
06c0: 56 32 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 6d  V2 have a.** com
06d0: 6d 6f 6e 20 6f 72 69 67 69 6e 20 61 74 20 70 50  mon origin at pP
06e0: 69 76 6f 74 2e 20 20 41 70 70 6c 79 20 74 68 65  ivot.  Apply the
06f0: 20 63 68 61 6e 67 65 73 20 6f 66 20 70 50 69 76   changes of pPiv
0700: 6f 74 20 3d 3d 3e 20 70 56 31 0a 2a 2a 20 74 6f  ot ==> pV1.** to
0710: 20 70 56 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   pV2..**.** The 
0720: 72 65 74 75 72 6e 20 69 73 20 30 20 75 70 6f 6e  return is 0 upon
0730: 20 63 6f 6d 70 6c 65 74 65 20 73 75 63 63 65 73   complete succes
0740: 73 2e 20 49 66 20 61 6e 79 20 69 6e 70 75 74 20  s. If any input 
0750: 66 69 6c 65 20 69 73 20 62 69 6e 61 72 79 2c 0a  file is binary,.
0760: 2a 2a 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65  ** -1 is returne
0770: 64 20 61 6e 64 20 70 4f 75 74 20 69 73 20 75 6e  d and pOut is un
0780: 6d 6f 64 69 66 69 65 64 2e 20 20 49 66 20 74 68  modified.  If th
0790: 65 72 65 20 61 72 65 20 6d 65 72 67 65 0a 2a 2a  ere are merge.**
07a0: 20 63 6f 6e 66 6c 69 63 74 73 2c 20 74 68 65 20   conflicts, the 
07b0: 6d 65 72 67 65 20 70 72 6f 63 65 65 64 73 20 61  merge proceeds a
07c0: 73 20 62 65 73 74 20 61 73 20 69 74 20 63 61 6e  s best as it can
07d0: 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20   and the number 
07e0: 0a 2a 2a 20 6f 66 20 63 6f 6e 66 6c 69 63 74 73  .** of conflicts
07f0: 20 69 73 20 72 65 74 75 72 6e 73 0a 2a 2f 0a 69   is returns.*/.i
0800: 6e 74 20 62 6c 6f 62 5f 6d 65 72 67 65 28 42 6c  nt blob_merge(Bl
0810: 6f 62 20 2a 70 50 69 76 6f 74 2c 20 42 6c 6f 62  ob *pPivot, Blob
0820: 20 2a 70 56 31 2c 20 42 6c 6f 62 20 2a 70 56 32   *pV1, Blob *pV2
0830: 2c 20 42 6c 6f 62 20 2a 70 4f 75 74 29 7b 0a 20  , Blob *pOut){. 
0840: 20 69 6e 74 20 2a 61 43 31 3b 20 20 2f 2a 20 43   int *aC1;  /* C
0850: 68 61 6e 67 65 73 20 66 72 6f 6d 20 70 50 69 76  hanges from pPiv
0860: 6f 74 20 74 6f 20 70 56 31 20 2a 2f 0a 20 20 69  ot to pV1 */.  i
0870: 6e 74 20 2a 61 43 32 3b 20 20 2f 2a 20 43 68 61  nt *aC2;  /* Cha
0880: 6e 67 65 73 20 66 72 6f 6d 20 70 50 69 76 6f 74  nges from pPivot
0890: 20 74 6f 20 70 56 32 20 2a 2f 0a 20 20 69 6e 74   to pV2 */.  int
08a0: 20 69 31 2c 20 69 32 3b 0a 20 20 69 6e 74 20 6f   i1, i2;.  int o
08b0: 70 31 2c 20 6f 70 32 3b 0a 20 20 69 6e 74 20 6c  p1, op2;.  int l
08c0: 69 6d 69 74 31 2c 20 6c 69 6d 69 74 32 3b 0a 20  imit1, limit2;. 
08d0: 20 69 6e 74 20 69 6e 43 6f 6e 66 6c 69 63 74 20   int inConflict 
08e0: 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 43 6f 6e 66  = 0;.  int nConf
08f0: 6c 69 63 74 20 3d 20 30 3b 0a 20 20 73 74 61 74  lict = 0;.  stat
0900: 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 42  ic const char zB
0910: 65 67 69 6e 5b 5d 20 3d 20 22 3e 3e 3e 3e 3e 3e  egin[] = ">>>>>>
0920: 3e 3e 20 42 45 47 49 4e 20 4d 45 52 47 45 20 43  >> BEGIN MERGE C
0930: 4f 4e 46 4c 49 43 54 20 3c 3c 3c 3c 3c 3c 3c 3c  ONFLICT <<<<<<<<
0940: 5c 6e 22 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  \n";.  static co
0950: 6e 73 74 20 63 68 61 72 20 7a 45 6e 64 5b 5d 20  nst char zEnd[] 
0960: 20 20 3d 20 22 3e 3e 3e 3e 3e 3e 3e 3e 3e 20 45    = ">>>>>>>>> E
0970: 4e 44 20 4d 45 52 47 45 20 43 4f 4e 46 4c 49 43  ND MERGE CONFLIC
0980: 54 20 3c 3c 3c 3c 3c 3c 3c 3c 3c 5c 6e 22 3b 0a  T <<<<<<<<<\n";.
0990: 0a 20 20 61 43 31 20 3d 20 74 65 78 74 5f 64 69  .  aC1 = text_di
09a0: 66 66 28 70 50 69 76 6f 74 2c 20 70 56 31 2c 20  ff(pPivot, pV1, 
09b0: 30 2c 20 30 29 3b 0a 20 20 61 43 32 20 3d 20 74  0, 0);.  aC2 = t
09c0: 65 78 74 5f 64 69 66 66 28 70 50 69 76 6f 74 2c  ext_diff(pPivot,
09d0: 20 70 56 32 2c 20 30 2c 20 30 29 3b 0a 0a 20 20   pV2, 0, 0);..  
09e0: 69 66 28 20 61 43 32 3d 3d 30 20 7c 7c 20 61 43  if( aC2==0 || aC
09f0: 32 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65  2==0 ){.    free
0a00: 28 61 43 31 29 3b 0a 20 20 20 20 66 72 65 65 28  (aC1);.    free(
0a10: 61 43 32 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  aC2);.    return
0a20: 20 2d 31 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f   -1;.  }.  blob_
0a30: 7a 65 72 6f 28 70 4f 75 74 29 3b 0a 20 20 62 6c  zero(pOut);.  bl
0a40: 6f 62 5f 72 65 77 69 6e 64 28 70 56 31 29 3b 0a  ob_rewind(pV1);.
0a50: 20 20 62 6c 6f 62 5f 72 65 77 69 6e 64 28 70 56    blob_rewind(pV
0a60: 32 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 77 69 6e  2);.  blob_rewin
0a70: 64 28 70 50 69 76 6f 74 29 3b 0a 0a 20 20 66 6f  d(pPivot);..  fo
0a80: 72 28 69 31 3d 30 3b 20 61 43 31 5b 69 31 5d 20  r(i1=0; aC1[i1] 
0a90: 7c 7c 20 61 43 31 5b 69 31 2b 31 5d 20 7c 7c 20  || aC1[i1+1] || 
0aa0: 61 43 31 5b 69 31 2b 32 5d 3b 20 69 31 2b 3d 33  aC1[i1+2]; i1+=3
0ab0: 29 7b 7d 0a 20 20 6c 69 6d 69 74 31 20 3d 20 69  ){}.  limit1 = i
0ac0: 31 3b 0a 20 20 66 6f 72 28 69 32 3d 30 3b 20 61  1;.  for(i2=0; a
0ad0: 43 32 5b 69 32 5d 20 7c 7c 20 61 43 32 5b 69 32  C2[i2] || aC2[i2
0ae0: 2b 31 5d 20 7c 7c 20 61 43 32 5b 69 32 2b 32 5d  +1] || aC2[i2+2]
0af0: 3b 20 69 32 2b 3d 33 29 7b 7d 0a 20 20 6c 69 6d  ; i2+=3){}.  lim
0b00: 69 74 32 20 3d 20 69 32 3b 0a 0a 20 20 44 45 42  it2 = i2;..  DEB
0b10: 55 47 28 0a 20 20 20 20 66 6f 72 28 69 31 3d 30  UG(.    for(i1=0
0b20: 3b 20 69 31 3c 6c 69 6d 69 74 31 3b 20 69 31 2b  ; i1<limit1; i1+
0b30: 3d 33 29 7b 0a 20 20 20 20 20 20 70 72 69 6e 74  =3){.      print
0b40: 66 28 22 63 31 3a 20 25 34 64 20 25 34 64 20 25  f("c1: %4d %4d %
0b50: 34 64 5c 6e 22 2c 20 61 43 31 5b 69 31 5d 2c 20  4d\n", aC1[i1], 
0b60: 61 43 31 5b 69 31 2b 31 5d 2c 20 61 43 31 5b 69  aC1[i1+1], aC1[i
0b70: 31 2b 32 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  1+2]);.    }.   
0b80: 20 66 6f 72 28 69 32 3d 30 3b 20 69 32 3c 6c 69   for(i2=0; i2<li
0b90: 6d 69 74 32 3b 20 69 32 2b 3d 33 29 7b 0a 20 20  mit2; i2+=3){.  
0ba0: 20 20 20 70 72 69 6e 74 66 28 22 63 32 3a 20 25     printf("c2: %
0bb0: 34 64 20 25 34 64 20 25 34 64 5c 6e 22 2c 20 61  4d %4d %4d\n", a
0bc0: 43 32 5b 69 32 5d 2c 20 61 43 32 5b 69 32 2b 31  C2[i2], aC2[i2+1
0bd0: 5d 2c 20 61 43 32 5b 69 32 2b 32 5d 29 3b 0a 20  ], aC2[i2+2]);. 
0be0: 20 20 20 7d 0a 20 20 29 0a 0a 20 20 6f 70 31 20     }.  )..  op1 
0bf0: 3d 20 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 69  = op2 = UNK;.  i
0c00: 31 20 3d 20 69 32 20 3d 20 30 3b 0a 20 20 77 68  1 = i2 = 0;.  wh
0c10: 69 6c 65 28 20 69 31 3c 6c 69 6d 69 74 31 20 26  ile( i1<limit1 &
0c20: 26 20 61 43 31 5b 69 31 5d 3d 3d 30 20 29 7b 20  & aC1[i1]==0 ){ 
0c30: 69 31 2b 2b 3b 20 7d 0a 20 20 77 68 69 6c 65 28  i1++; }.  while(
0c40: 20 69 32 3c 6c 69 6d 69 74 32 20 26 26 20 61 43   i2<limit2 && aC
0c50: 32 5b 69 32 5d 3d 3d 30 20 29 7b 20 69 32 2b 2b  2[i2]==0 ){ i2++
0c60: 3b 20 7d 0a 0a 20 20 77 68 69 6c 65 28 31 29 7b  ; }..  while(1){
0c70: 0a 20 20 20 20 69 66 28 20 6f 70 31 3d 3d 55 4e  .    if( op1==UN
0c80: 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  K ){.      if( i
0c90: 31 3e 3d 6c 69 6d 69 74 31 20 29 7b 0a 20 20 20  1>=limit1 ){.   
0ca0: 20 20 20 20 20 6f 70 31 20 3d 20 45 4e 44 3b 0a       op1 = END;.
0cb0: 20 20 20 20 20 20 20 20 44 45 42 55 47 28 20 70          DEBUG( p
0cc0: 72 69 6e 74 66 28 22 67 65 74 20 6f 70 31 3d 45  rintf("get op1=E
0cd0: 4e 44 5c 6e 22 29 3b 20 29 0a 20 20 20 20 20 20  ND\n"); ).      
0ce0: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6f  }else{.        o
0cf0: 70 31 20 3d 20 69 31 20 25 20 33 3b 0a 20 20 20  p1 = i1 % 3;.   
0d00: 20 20 20 20 20 61 43 31 5b 69 31 5d 2d 2d 3b 0a       aC1[i1]--;.
0d10: 20 20 20 20 20 20 20 20 44 45 42 55 47 28 20 70          DEBUG( p
0d20: 72 69 6e 74 66 28 22 67 65 74 20 6f 70 31 3d 25  rintf("get op1=%
0d30: 64 20 66 72 6f 6d 20 25 64 20 28 25 64 2d 3e 25  d from %d (%d->%
0d40: 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  d)\n",.         
0d50: 20 20 20 20 20 20 6f 70 31 2c 69 31 2c 61 43 31        op1,i1,aC1
0d60: 5b 69 31 5d 2b 31 2c 61 43 31 5b 69 31 5d 29 3b  [i1]+1,aC1[i1]);
0d70: 20 29 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65   ).        while
0d80: 28 20 69 31 3c 6c 69 6d 69 74 31 20 26 26 20 61  ( i1<limit1 && a
0d90: 43 31 5b 69 31 5d 3d 3d 30 20 29 7b 20 69 31 2b  C1[i1]==0 ){ i1+
0da0: 2b 3b 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20  +; }.      }.   
0db0: 20 7d 0a 20 20 20 20 69 66 28 20 6f 70 32 3d 3d   }.    if( op2==
0dc0: 55 4e 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28  UNK ){.      if(
0dd0: 20 69 32 3e 3d 6c 69 6d 69 74 32 20 29 7b 0a 20   i2>=limit2 ){. 
0de0: 20 20 20 20 20 20 20 6f 70 32 20 3d 20 45 4e 44         op2 = END
0df0: 3b 0a 20 20 20 20 20 20 20 20 44 45 42 55 47 28  ;.        DEBUG(
0e00: 20 70 72 69 6e 74 66 28 22 67 65 74 20 6f 70 32   printf("get op2
0e10: 3d 45 4e 44 5c 6e 22 29 3b 20 29 0a 20 20 20 20  =END\n"); ).    
0e20: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
0e30: 20 6f 70 32 20 3d 20 69 32 20 25 20 33 3b 0a 20   op2 = i2 % 3;. 
0e40: 20 20 20 20 20 20 20 61 43 32 5b 69 32 5d 2d 2d         aC2[i2]--
0e50: 3b 0a 20 20 20 20 20 20 20 20 44 45 42 55 47 28  ;.        DEBUG(
0e60: 20 70 72 69 6e 74 66 28 22 67 65 74 20 6f 70 32   printf("get op2
0e70: 3d 25 64 20 66 72 6f 6d 20 25 64 20 28 25 64 2d  =%d from %d (%d-
0e80: 3e 25 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20  >%d)\n",.       
0e90: 20 20 20 20 20 20 20 20 6f 70 32 2c 69 32 2c 61          op2,i2,a
0ea0: 43 32 5b 69 32 5d 2b 31 2c 61 43 32 5b 69 32 5d  C2[i2]+1,aC2[i2]
0eb0: 29 3b 20 29 0a 20 20 20 20 20 20 20 20 77 68 69  ); ).        whi
0ec0: 6c 65 28 20 69 32 3c 6c 69 6d 69 74 32 20 26 26  le( i2<limit2 &&
0ed0: 20 61 43 32 5b 69 32 5d 3d 3d 30 20 29 7b 20 69   aC2[i2]==0 ){ i
0ee0: 32 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 7d 0a 20  2++; }.      }. 
0ef0: 20 20 20 7d 0a 20 20 20 20 44 45 42 55 47 28 20     }.    DEBUG( 
0f00: 70 72 69 6e 74 66 28 22 6f 70 31 3d 25 64 20 6f  printf("op1=%d o
0f10: 70 32 3d 25 64 5c 6e 22 2c 20 6f 70 31 2c 20 6f  p2=%d\n", op1, o
0f20: 70 32 29 3b 20 29 0a 20 20 20 20 69 66 28 20 6f  p2); ).    if( o
0f30: 70 31 3d 3d 45 4e 44 20 29 7b 0a 20 20 20 20 20  p1==END ){.     
0f40: 20 69 66 28 20 6f 70 32 3d 3d 49 4e 53 20 29 7b   if( op2==INS ){
0f50: 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f  .        blob_co
0f60: 70 79 5f 6c 69 6e 65 73 28 70 4f 75 74 2c 20 70  py_lines(pOut, p
0f70: 56 32 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  V2, 1);.        
0f80: 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 20 20 20  op2 = UNK;.     
0f90: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
0fa0: 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
0fb0: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 32     }else if( op2
0fc0: 3d 3d 45 4e 44 20 29 7b 0a 20 20 20 20 20 20 69  ==END ){.      i
0fd0: 66 28 20 6f 70 31 3d 3d 49 4e 53 20 29 7b 0a 20  f( op1==INS ){. 
0fe0: 20 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79         blob_copy
0ff0: 5f 6c 69 6e 65 73 28 70 4f 75 74 2c 20 70 56 31  _lines(pOut, pV1
1000: 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 6f 70  , 1);.        op
1010: 31 20 3d 20 55 4e 4b 3b 0a 20 20 20 20 20 20 7d  1 = UNK;.      }
1020: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72  else{.        br
1030: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1040: 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 31 3d 3d   }else if( op1==
1050: 49 4e 53 20 26 26 20 6f 70 32 3d 3d 49 4e 53 20  INS && op2==INS 
1060: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 6e  ){.      if( !in
1070: 43 6f 6e 66 6c 69 63 74 20 26 26 20 73 61 6d 65  Conflict && same
1080: 4c 69 6e 65 28 70 56 31 2c 20 70 56 32 29 20 29  Line(pV1, pV2) )
1090: 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 63  {.        blob_c
10a0: 6f 70 79 5f 6c 69 6e 65 73 28 70 4f 75 74 2c 20  opy_lines(pOut, 
10b0: 70 56 32 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  pV2, 1);.       
10c0: 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73   blob_copy_lines
10d0: 28 30 2c 20 70 56 31 2c 20 30 29 3b 0a 20 20 20  (0, pV1, 0);.   
10e0: 20 20 20 20 20 6f 70 31 20 3d 20 55 4e 4b 3b 0a       op1 = UNK;.
10f0: 20 20 20 20 20 20 20 20 6f 70 32 20 3d 20 55 4e          op2 = UN
1100: 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  K;.      }else{.
1110: 20 20 20 20 20 20 20 20 69 66 28 20 21 69 6e 43          if( !inC
1120: 6f 6e 66 6c 69 63 74 20 29 7b 0a 20 20 20 20 20  onflict ){.     
1130: 20 20 20 20 20 69 6e 43 6f 6e 66 6c 69 63 74 20       inConflict 
1140: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 6e  = 1;.          n
1150: 43 6f 6e 66 6c 69 63 74 2b 2b 3b 0a 20 20 20 20  Conflict++;.    
1160: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
1170: 64 66 28 70 4f 75 74 2c 20 7a 42 65 67 69 6e 29  df(pOut, zBegin)
1180: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1190: 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69      blob_copy_li
11a0: 6e 65 73 28 70 4f 75 74 2c 20 70 56 31 2c 20 31  nes(pOut, pV1, 1
11b0: 29 3b 0a 20 20 20 20 20 20 20 20 6f 70 31 20 3d  );.        op1 =
11c0: 20 55 4e 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20   UNK;.      }.  
11d0: 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 31 3d    }else if( op1=
11e0: 3d 49 4e 53 20 29 7b 0a 20 20 20 20 20 20 62 6c  =INS ){.      bl
11f0: 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 70 4f  ob_copy_lines(pO
1200: 75 74 2c 20 70 56 31 2c 20 31 29 3b 0a 20 20 20  ut, pV1, 1);.   
1210: 20 20 20 6f 70 31 20 3d 20 55 4e 4b 3b 0a 20 20     op1 = UNK;.  
1220: 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 32 3d    }else if( op2=
1230: 3d 49 4e 53 20 29 7b 0a 20 20 20 20 20 20 62 6c  =INS ){.      bl
1240: 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 70 4f  ob_copy_lines(pO
1250: 75 74 2c 20 70 56 32 2c 20 31 29 3b 0a 20 20 20  ut, pV2, 1);.   
1260: 20 20 20 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20     op2 = UNK;.  
1270: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1280: 66 28 20 69 6e 43 6f 6e 66 6c 69 63 74 20 29 7b  f( inConflict ){
1290: 0a 20 20 20 20 20 20 20 20 69 6e 43 6f 6e 66 6c  .        inConfl
12a0: 69 63 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ict = 0;.       
12b0: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 4f   blob_appendf(pO
12c0: 75 74 2c 20 7a 45 6e 64 29 3b 0a 20 20 20 20 20  ut, zEnd);.     
12d0: 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f 70 31   }.      if( op1
12e0: 3d 3d 44 45 4c 20 7c 7c 20 6f 70 32 3d 3d 44 45  ==DEL || op2==DE
12f0: 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f  L ){.        blo
1300: 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 30 2c 20  b_copy_lines(0, 
1310: 70 50 69 76 6f 74 2c 20 31 29 3b 0a 20 20 20 20  pPivot, 1);.    
1320: 20 20 20 20 69 66 28 20 6f 70 32 3d 3d 43 50 59      if( op2==CPY
1330: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 6c   ){.          bl
1340: 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 30 2c  ob_copy_lines(0,
1350: 20 70 56 32 2c 20 31 29 3b 0a 20 20 20 20 20 20   pV2, 1);.      
1360: 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
1370: 6f 70 31 3d 3d 43 50 59 20 29 7b 0a 20 20 20 20  op1==CPY ){.    
1380: 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f        blob_copy_
1390: 6c 69 6e 65 73 28 30 2c 20 70 56 31 2c 20 31 29  lines(0, pV1, 1)
13a0: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
13b0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
13c0: 20 61 73 73 65 72 74 28 20 6f 70 31 3d 3d 43 50   assert( op1==CP
13d0: 59 20 26 26 20 6f 70 32 3d 3d 43 50 59 20 29 3b  Y && op2==CPY );
13e0: 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f  .        blob_co
13f0: 70 79 5f 6c 69 6e 65 73 28 70 4f 75 74 2c 20 70  py_lines(pOut, p
1400: 50 69 76 6f 74 2c 20 31 29 3b 0a 20 20 20 20 20  Pivot, 1);.     
1410: 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e     blob_copy_lin
1420: 65 73 28 30 2c 20 70 56 31 2c 20 31 29 3b 0a 20  es(0, pV1, 1);. 
1430: 20 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79         blob_copy
1440: 5f 6c 69 6e 65 73 28 30 2c 20 70 56 32 2c 20 31  _lines(0, pV2, 1
1450: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1460: 20 6f 70 31 20 3d 20 55 4e 4b 3b 0a 20 20 20 20   op1 = UNK;.    
1470: 20 20 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 20    op2 = UNK;.   
1480: 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 43   }.  }.  if( inC
1490: 6f 6e 66 6c 69 63 74 20 29 7b 0a 20 20 20 20 62  onflict ){.    b
14a0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 4f 75 74  lob_appendf(pOut
14b0: 2c 20 7a 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20  , zEnd);.  }..  
14c0: 66 72 65 65 28 61 43 31 29 3b 0a 20 20 66 72 65  free(aC1);.  fre
14d0: 65 28 61 43 32 29 3b 0a 20 20 72 65 74 75 72 6e  e(aC2);.  return
14e0: 20 6e 43 6f 6e 66 6c 69 63 74 3b 0a 7d 0a 0a 2f   nConflict;.}../
14f0: 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74  *.** COMMAND:  t
1500: 65 73 74 2d 33 2d 77 61 79 2d 6d 65 72 67 65 0a  est-3-way-merge.
1510: 2a 2a 0a 2a 2a 20 43 6f 6d 62 69 6e 65 20 63 68  **.** Combine ch
1520: 61 6e 67 65 20 69 6e 20 67 6f 69 6e 67 20 66 72  ange in going fr
1530: 6f 6d 20 50 49 56 4f 54 2d 3e 56 45 52 53 49 4f  om PIVOT->VERSIO
1540: 4e 31 20 77 69 74 68 20 74 68 65 20 63 68 61 6e  N1 with the chan
1550: 67 65 20 67 6f 69 6e 67 0a 2a 2a 20 66 72 6f 6d  ge going.** from
1560: 20 50 49 56 4f 54 2d 3e 56 45 52 53 49 4f 4e 32   PIVOT->VERSION2
1570: 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 63   and write the c
1580: 6f 6d 62 69 6e 65 64 20 63 68 61 6e 67 65 73 20  ombined changes 
1590: 69 6e 74 6f 20 4d 45 52 47 45 44 2e 0a 2a 2f 0a  into MERGED..*/.
15a0: 76 6f 69 64 20 64 65 6c 74 61 5f 33 77 61 79 6d  void delta_3waym
15b0: 65 72 67 65 5f 63 6d 64 28 76 6f 69 64 29 7b 0a  erge_cmd(void){.
15c0: 20 20 42 6c 6f 62 20 70 69 76 6f 74 2c 20 76 31    Blob pivot, v1
15d0: 2c 20 76 32 2c 20 6d 65 72 67 65 64 3b 0a 20 20  , v2, merged;.  
15e0: 69 66 28 20 67 2e 61 72 67 63 21 3d 36 20 29 7b  if( g.argc!=6 ){
15f0: 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64  .    fprintf(std
1600: 65 72 72 2c 22 55 73 61 67 65 3a 20 25 73 20 25  err,"Usage: %s %
1610: 73 20 50 49 56 4f 54 20 56 31 20 56 32 20 4d 45  s PIVOT V1 V2 ME
1620: 52 47 45 44 5c 6e 22 2c 20 67 2e 61 72 67 76 5b  RGED\n", g.argv[
1630: 30 5d 2c 20 67 2e 61 72 67 76 5b 31 5d 29 3b 0a  0], g.argv[1]);.
1640: 20 20 20 20 65 78 69 74 28 31 29 3b 0a 20 20 7d      exit(1);.  }
1650: 0a 20 20 69 66 28 20 62 6c 6f 62 5f 72 65 61 64  .  if( blob_read
1660: 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 70 69 76 6f  _from_file(&pivo
1670: 74 2c 20 67 2e 61 72 67 76 5b 32 5d 29 3c 30 20  t, g.argv[2])<0 
1680: 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73  ){.    fprintf(s
1690: 74 64 65 72 72 2c 22 63 61 6e 6e 6f 74 20 72 65  tderr,"cannot re
16a0: 61 64 20 25 73 5c 6e 22 2c 20 67 2e 61 72 67 76  ad %s\n", g.argv
16b0: 5b 32 5d 29 3b 0a 20 20 20 20 65 78 69 74 28 31  [2]);.    exit(1
16c0: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 62 6c 6f  );.  }.  if( blo
16d0: 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65  b_read_from_file
16e0: 28 26 76 31 2c 20 67 2e 61 72 67 76 5b 33 5d 29  (&v1, g.argv[3])
16f0: 3c 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  <0 ){.    fprint
1700: 66 28 73 74 64 65 72 72 2c 22 63 61 6e 6e 6f 74  f(stderr,"cannot
1710: 20 72 65 61 64 20 25 73 5c 6e 22 2c 20 67 2e 61   read %s\n", g.a
1720: 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 65 78 69  rgv[3]);.    exi
1730: 74 28 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  t(1);.  }.  if( 
1740: 62 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66  blob_read_from_f
1750: 69 6c 65 28 26 76 32 2c 20 67 2e 61 72 67 76 5b  ile(&v2, g.argv[
1760: 34 5d 29 3c 30 20 29 7b 0a 20 20 20 20 66 70 72  4])<0 ){.    fpr
1770: 69 6e 74 66 28 73 74 64 65 72 72 2c 22 63 61 6e  intf(stderr,"can
1780: 6e 6f 74 20 72 65 61 64 20 25 73 5c 6e 22 2c 20  not read %s\n", 
1790: 67 2e 61 72 67 76 5b 34 5d 29 3b 0a 20 20 20 20  g.argv[4]);.    
17a0: 65 78 69 74 28 31 29 3b 0a 20 20 7d 0a 20 20 62  exit(1);.  }.  b
17b0: 6c 6f 62 5f 6d 65 72 67 65 28 26 70 69 76 6f 74  lob_merge(&pivot
17c0: 2c 20 26 76 31 2c 20 26 76 32 2c 20 26 6d 65 72  , &v1, &v2, &mer
17d0: 67 65 64 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62  ged);.  if( blob
17e0: 5f 77 72 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26  _write_to_file(&
17f0: 6d 65 72 67 65 64 2c 20 67 2e 61 72 67 76 5b 35  merged, g.argv[5
1800: 5d 29 3c 62 6c 6f 62 5f 73 69 7a 65 28 26 6d 65  ])<blob_size(&me
1810: 72 67 65 64 29 20 29 7b 0a 20 20 20 20 66 70 72  rged) ){.    fpr
1820: 69 6e 74 66 28 73 74 64 65 72 72 2c 22 63 61 6e  intf(stderr,"can
1830: 6e 6f 74 20 77 72 69 74 65 20 25 73 5c 6e 22 2c  not write %s\n",
1840: 20 67 2e 61 72 67 76 5b 34 5d 29 3b 0a 20 20 20   g.argv[4]);.   
1850: 20 65 78 69 74 28 31 29 3b 0a 20 20 7d 0a 20 20   exit(1);.  }.  
1860: 62 6c 6f 62 5f 72 65 73 65 74 28 26 70 69 76 6f  blob_reset(&pivo
1870: 74 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74  t);.  blob_reset
1880: 28 26 76 31 29 3b 0a 20 20 62 6c 6f 62 5f 72 65  (&v1);.  blob_re
1890: 73 65 74 28 26 76 32 29 3b 0a 20 20 62 6c 6f 62  set(&v2);.  blob
18a0: 5f 72 65 73 65 74 28 26 6d 65 72 67 65 64 29 3b  _reset(&merged);
18b0: 0a 7d 0a                                         .}.