Hex Artifact Content
Not logged in

Artifact 04d5b28b92dbca7f9846ab41b394dafb6c094591:

File src/merge3.c part of check-in [434830cc00] - Turn off the debugging prints that were left on by mistake in the previous check-in. by drh on 2009-03-20 01:26:14.

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 64 65 66 69 6e 65 20 49 53 44 45 42 55  X.#define ISDEBU
03e0: 47 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  G 1.#else.#defin
03f0: 65 20 44 45 42 55 47 28 58 29 0a 23 64 65 66 69  e DEBUG(X).#defi
0400: 6e 65 20 49 53 44 45 42 55 47 20 30 0a 23 65 6e  ne ISDEBUG 0.#en
0410: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 63 6f 64  dif../*.** Opcod
0420: 65 73 2e 0a 2a 2a 0a 2a 2a 20 56 61 6c 75 65 73  es..**.** Values
0430: 20 61 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 68   are important h
0440: 65 72 65 2e 20 20 54 68 65 20 74 65 78 74 5f 64  ere.  The text_d
0450: 69 66 66 28 29 20 66 75 6e 63 74 69 6f 6e 20 72  iff() function r
0460: 65 74 75 72 6e 73 20 61 6e 20 61 72 72 61 79 0a  eturns an array.
0470: 2a 2a 20 6f 66 20 74 72 69 70 6c 65 73 20 6f 66  ** of triples of
0480: 20 69 6e 74 65 67 65 72 73 20 77 68 65 72 65 20   integers where 
0490: 77 69 74 68 69 6e 20 65 61 63 68 20 74 72 69 70  within each trip
04a0: 6c 65 2c 20 74 68 65 20 30 20 65 6c 65 6d 65 6e  le, the 0 elemen
04b0: 74 20 69 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  t is.** the numb
04c0: 65 72 20 6f 66 20 6c 69 6e 65 73 20 74 6f 20 63  er of lines to c
04d0: 6f 70 79 2c 20 74 68 65 20 31 20 65 6c 65 6d 65  opy, the 1 eleme
04e0: 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  nt is the number
04f0: 20 6f 66 20 6c 69 6e 65 73 20 74 6f 0a 2a 2a 20   of lines to.** 
0500: 64 65 6c 65 74 65 20 61 6e 64 20 74 68 65 20 32  delete and the 2
0510: 20 65 6c 65 6d 65 6e 74 20 69 73 20 74 68 65 20   element is the 
0520: 6e 75 6d 62 65 72 20 6f 66 20 6c 69 6e 65 73 20  number of lines 
0530: 74 6f 20 69 6e 73 65 72 74 2e 20 20 54 68 65 20  to insert.  The 
0540: 43 50 59 2c 0a 2a 2a 20 44 45 4c 2c 20 61 6e 64  CPY,.** DEL, and
0550: 20 49 4e 53 20 6f 70 63 6f 64 65 73 20 6d 75 73   INS opcodes mus
0560: 74 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  t correspond to 
0570: 74 68 65 73 65 20 69 6e 64 69 63 65 73 2e 0a 2a  these indices..*
0580: 2f 0a 23 64 65 66 69 6e 65 20 43 50 59 20 30 0a  /.#define CPY 0.
0590: 23 64 65 66 69 6e 65 20 44 45 4c 20 31 0a 23 64  #define DEL 1.#d
05a0: 65 66 69 6e 65 20 49 4e 53 20 32 0a 23 64 65 66  efine INS 2.#def
05b0: 69 6e 65 20 45 4e 44 20 33 0a 23 64 65 66 69 6e  ine END 3.#defin
05c0: 65 20 55 4e 4b 20 34 0a 0a 2f 2a 0a 2a 2a 20 43  e UNK 4../*.** C
05d0: 6f 6d 70 61 72 65 20 61 20 73 69 6e 67 6c 65 20  ompare a single 
05e0: 6c 69 6e 65 20 6f 66 20 74 65 78 74 20 66 72 6f  line of text fro
05f0: 6d 20 70 56 31 20 61 6e 64 20 70 56 32 2e 20 20  m pV1 and pV2.  
0600: 49 66 20 74 68 65 20 6c 69 6e 65 73 0a 2a 2a 20  If the lines.** 
0610: 61 72 65 20 74 68 65 20 73 61 6d 65 2c 20 72 65  are the same, re
0620: 74 75 72 6e 20 74 72 75 65 2e 20 20 52 65 74 75  turn true.  Retu
0630: 72 6e 20 66 61 6c 73 65 20 69 66 20 74 68 65 79  rn false if they
0640: 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 0a   are different..
0650: 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72  **.** The cursor
0660: 20 6f 6e 20 62 6f 74 68 20 70 56 31 20 61 6e 64   on both pV1 and
0670: 20 70 56 32 20 69 73 20 75 6e 63 68 61 6e 67 65   pV2 is unchange
0680: 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
0690: 20 73 61 6d 65 4c 69 6e 65 28 42 6c 6f 62 20 2a   sameLine(Blob *
06a0: 70 56 31 2c 20 42 6c 6f 62 20 2a 70 56 32 29 7b  pV1, Blob *pV2){
06b0: 0a 20 20 63 68 61 72 20 2a 7a 31 2c 20 2a 7a 32  .  char *z1, *z2
06c0: 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 7a 31  ;.  int i;..  z1
06d0: 20 3d 20 26 62 6c 6f 62 5f 62 75 66 66 65 72 28   = &blob_buffer(
06e0: 70 56 31 29 5b 62 6c 6f 62 5f 74 65 6c 6c 28 70  pV1)[blob_tell(p
06f0: 56 31 29 5d 3b 0a 20 20 7a 32 20 3d 20 26 62 6c  V1)];.  z2 = &bl
0700: 6f 62 5f 62 75 66 66 65 72 28 70 56 32 29 5b 62  ob_buffer(pV2)[b
0710: 6c 6f 62 5f 74 65 6c 6c 28 70 56 32 29 5d 3b 0a  lob_tell(pV2)];.
0720: 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d    for(i=0; z1[i]
0730: 21 3d 27 5c 6e 27 20 26 26 20 7a 31 5b 69 5d 3d  !='\n' && z1[i]=
0740: 3d 7a 32 5b 69 5d 3b 20 69 2b 2b 29 7b 7d 0a 20  =z2[i]; i++){}. 
0750: 20 72 65 74 75 72 6e 20 7a 32 5b 69 5d 3d 3d 27   return z2[i]=='
0760: 5c 6e 27 20 7c 7c 20 28 7a 32 5b 69 5d 3d 3d 27  \n' || (z2[i]=='
0770: 5c 72 27 20 26 26 20 7a 32 5b 69 2b 31 5d 3d 3d  \r' && z2[i+1]==
0780: 27 5c 6e 27 29 0a 20 20 20 20 20 20 20 20 20 20  '\n').          
0790: 7c 7c 20 28 7a 31 5b 69 5d 3d 3d 27 5c 72 27 20  || (z1[i]=='\r' 
07a0: 26 26 20 7a 32 5b 69 5d 3d 3d 27 5c 6e 27 20 26  && z2[i]=='\n' &
07b0: 26 20 7a 31 5b 69 2b 31 5d 3d 3d 27 5c 6e 27 29  & z1[i+1]=='\n')
07c0: 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 6d 69 6e 69  ;.}../* The mini
07d0: 6d 75 6d 20 6f 66 20 74 77 6f 20 69 6e 74 65 67  mum of two integ
07e0: 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 6d  ers */.#define m
07f0: 69 6e 28 41 2c 42 29 20 20 28 41 3c 42 3f 41 3a  in(A,B)  (A<B?A:
0800: 42 29 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 74  B)../*.** Do a t
0810: 68 72 65 65 2d 77 61 79 20 6d 65 72 67 65 2e 20  hree-way merge. 
0820: 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 4f 75 74   Initialize pOut
0830: 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20   to contain the 
0840: 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  result..**.** Th
0850: 65 20 6d 65 72 67 65 20 69 73 20 61 6e 20 65 64  e merge is an ed
0860: 69 74 20 61 67 61 69 6e 73 74 20 70 56 32 2e 20  it against pV2. 
0870: 20 42 6f 74 68 20 70 56 31 20 61 6e 64 20 70 56   Both pV1 and pV
0880: 32 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 6d 6d  2 have a.** comm
0890: 6f 6e 20 6f 72 69 67 69 6e 20 61 74 20 70 50 69  on origin at pPi
08a0: 76 6f 74 2e 20 20 41 70 70 6c 79 20 74 68 65 20  vot.  Apply the 
08b0: 63 68 61 6e 67 65 73 20 6f 66 20 70 50 69 76 6f  changes of pPivo
08c0: 74 20 3d 3d 3e 20 70 56 31 0a 2a 2a 20 74 6f 20  t ==> pV1.** to 
08d0: 70 56 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  pV2..**.** The r
08e0: 65 74 75 72 6e 20 69 73 20 30 20 75 70 6f 6e 20  eturn is 0 upon 
08f0: 63 6f 6d 70 6c 65 74 65 20 73 75 63 63 65 73 73  complete success
0900: 2e 20 49 66 20 61 6e 79 20 69 6e 70 75 74 20 66  . If any input f
0910: 69 6c 65 20 69 73 20 62 69 6e 61 72 79 2c 0a 2a  ile is binary,.*
0920: 2a 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64  * -1 is returned
0930: 20 61 6e 64 20 70 4f 75 74 20 69 73 20 75 6e 6d   and pOut is unm
0940: 6f 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65  odified.  If the
0950: 72 65 20 61 72 65 20 6d 65 72 67 65 0a 2a 2a 20  re are merge.** 
0960: 63 6f 6e 66 6c 69 63 74 73 2c 20 74 68 65 20 6d  conflicts, the m
0970: 65 72 67 65 20 70 72 6f 63 65 65 64 73 20 61 73  erge proceeds as
0980: 20 62 65 73 74 20 61 73 20 69 74 20 63 61 6e 20   best as it can 
0990: 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 0a  and the number .
09a0: 2a 2a 20 6f 66 20 63 6f 6e 66 6c 69 63 74 73 20  ** of conflicts 
09b0: 69 73 20 72 65 74 75 72 6e 73 0a 2a 2f 0a 69 6e  is returns.*/.in
09c0: 74 20 62 6c 6f 62 5f 6d 65 72 67 65 28 42 6c 6f  t blob_merge(Blo
09d0: 62 20 2a 70 50 69 76 6f 74 2c 20 42 6c 6f 62 20  b *pPivot, Blob 
09e0: 2a 70 56 31 2c 20 42 6c 6f 62 20 2a 70 56 32 2c  *pV1, Blob *pV2,
09f0: 20 42 6c 6f 62 20 2a 70 4f 75 74 29 7b 0a 20 20   Blob *pOut){.  
0a00: 69 6e 74 20 2a 61 43 31 3b 20 20 20 20 20 20 20  int *aC1;       
0a10: 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65         /* Change
0a20: 73 20 66 72 6f 6d 20 70 50 69 76 6f 74 20 74 6f  s from pPivot to
0a30: 20 70 56 31 20 2a 2f 0a 20 20 69 6e 74 20 2a 61   pV1 */.  int *a
0a40: 43 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  C2;             
0a50: 20 2f 2a 20 43 68 61 6e 67 65 73 20 66 72 6f 6d   /* Changes from
0a60: 20 70 50 69 76 6f 74 20 74 6f 20 70 56 32 20 2a   pPivot to pV2 *
0a70: 2f 0a 20 20 69 6e 74 20 69 31 2c 20 69 32 3b 20  /.  int i1, i2; 
0a80: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
0a90: 64 65 78 20 69 6e 74 6f 20 61 43 31 5b 5d 20 61  dex into aC1[] a
0aa0: 6e 64 20 61 43 32 5b 5d 20 2a 2f 0a 20 20 69 6e  nd aC2[] */.  in
0ab0: 74 20 6f 70 31 2c 20 6f 70 32 3b 20 20 20 20 20  t op1, op2;     
0ac0: 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66       /* Opcode f
0ad0: 6f 72 20 61 43 31 5b 5d 20 61 6e 64 20 61 43 32  or aC1[] and aC2
0ae0: 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 31 2c 20  [] */.  int n1, 
0af0: 6e 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  n2;            /
0b00: 2a 20 43 6f 75 6e 74 73 20 66 6f 72 20 6f 70 31  * Counts for op1
0b10: 20 61 6e 64 20 6f 70 32 20 2a 2f 0a 20 20 69 6e   and op2 */.  in
0b20: 74 20 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20  t mn;           
0b30: 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20       /* Minimum 
0b40: 63 6f 75 6e 74 20 6f 66 20 6f 70 31 20 61 6e 64  count of op1 and
0b50: 20 6f 70 32 20 2a 2f 0a 20 20 69 6e 74 20 6c 69   op2 */.  int li
0b60: 6d 69 74 31 2c 20 6c 69 6d 69 74 32 3b 20 20 20  mit1, limit2;   
0b70: 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 61 43 31   /* Sizes of aC1
0b80: 5b 5d 20 61 6e 64 20 61 43 32 5b 5d 20 2a 2f 0a  [] and aC2[] */.
0b90: 20 20 69 6e 74 20 6e 43 6f 6e 66 6c 69 63 74 20    int nConflict 
0ba0: 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  = 0;     /* Numb
0bb0: 65 72 20 6f 66 20 6d 65 72 67 65 20 63 6f 6e 66  er of merge conf
0bc0: 6c 69 63 74 73 20 73 65 65 6e 20 73 6f 20 66 61  licts seen so fa
0bd0: 72 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f  r */.  static co
0be0: 6e 73 74 20 63 68 61 72 20 7a 42 65 67 69 6e 5b  nst char zBegin[
0bf0: 5d 20 3d 20 22 3e 3e 3e 3e 3e 3e 3e 20 42 45 47  ] = ">>>>>>> BEG
0c00: 49 4e 20 4d 45 52 47 45 20 43 4f 4e 46 4c 49 43  IN MERGE CONFLIC
0c10: 54 5c 6e 22 3b 0a 20 20 73 74 61 74 69 63 20 63  T\n";.  static c
0c20: 6f 6e 73 74 20 63 68 61 72 20 7a 4d 69 64 5b 5d  onst char zMid[]
0c30: 20 20 20 3d 20 22 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d     = "==========
0c40: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
0c50: 3d 3d 5c 6e 22 3b 0a 20 20 73 74 61 74 69 63 20  ==\n";.  static 
0c60: 63 6f 6e 73 74 20 63 68 61 72 20 7a 45 6e 64 5b  const char zEnd[
0c70: 5d 20 20 20 3d 20 22 3c 3c 3c 3c 3c 3c 3c 20 45  ]   = "<<<<<<< E
0c80: 4e 44 20 4d 45 52 47 45 20 43 4f 4e 46 4c 49 43  ND MERGE CONFLIC
0c90: 54 5c 6e 22 3b 0a 0a 23 69 66 20 49 53 44 45 42  T\n";..#if ISDEB
0ca0: 55 47 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  UG.  static cons
0cb0: 74 20 63 68 61 72 20 2a 7a 4f 70 5b 5d 20 3d 20  t char *zOp[] = 
0cc0: 7b 20 22 43 50 59 22 2c 20 22 44 45 4c 22 2c 20  { "CPY", "DEL", 
0cd0: 22 49 4e 53 22 2c 20 22 45 4e 44 22 2c 20 22 55  "INS", "END", "U
0ce0: 4e 4b 22 20 7d 3b 0a 23 65 6e 64 69 66 0a 0a 20  NK" };.#endif.. 
0cf0: 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
0d00: 65 64 69 74 73 20 74 68 61 74 20 6f 63 63 75 72  edits that occur
0d10: 20 66 72 6f 6d 20 70 50 69 76 6f 74 20 3d 3e 20   from pPivot => 
0d20: 70 56 31 20 61 6e 64 20 70 50 69 76 6f 74 20 3d  pV1 and pPivot =
0d30: 3e 20 70 56 32 20 2a 2f 0a 20 20 61 43 31 20 3d  > pV2 */.  aC1 =
0d40: 20 74 65 78 74 5f 64 69 66 66 28 70 50 69 76 6f   text_diff(pPivo
0d50: 74 2c 20 70 56 31 2c 20 30 2c 20 30 29 3b 0a 20  t, pV1, 0, 0);. 
0d60: 20 61 43 32 20 3d 20 74 65 78 74 5f 64 69 66 66   aC2 = text_diff
0d70: 28 70 50 69 76 6f 74 2c 20 70 56 32 2c 20 30 2c  (pPivot, pV2, 0,
0d80: 20 30 29 3b 0a 20 20 69 66 28 20 61 43 32 3d 3d   0);.  if( aC2==
0d90: 30 20 7c 7c 20 61 43 32 3d 3d 30 20 29 7b 0a 20  0 || aC2==0 ){. 
0da0: 20 20 20 66 72 65 65 28 61 43 31 29 3b 0a 20 20     free(aC1);.  
0db0: 20 20 66 72 65 65 28 61 43 32 29 3b 0a 20 20 20    free(aC2);.   
0dc0: 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a   return -1;.  }.
0dd0: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 70 4f 75  .  blob_zero(pOu
0de0: 74 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  t);         /* M
0df0: 65 72 67 65 20 72 65 73 75 6c 74 73 20 73 74 6f  erge results sto
0e00: 72 65 64 20 69 6e 20 70 4f 75 74 20 2a 2f 0a 20  red in pOut */. 
0e10: 20 62 6c 6f 62 5f 72 65 77 69 6e 64 28 70 56 31   blob_rewind(pV1
0e20: 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 77  );        /* Rew
0e30: 69 6e 64 20 69 6e 70 75 74 73 3a 20 20 4e 65 65  ind inputs:  Nee
0e40: 64 65 64 20 74 6f 20 72 65 63 6f 6e 73 74 72 75  ded to reconstru
0e50: 63 74 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 62  ct output */.  b
0e60: 6c 6f 62 5f 72 65 77 69 6e 64 28 70 56 32 29 3b  lob_rewind(pV2);
0e70: 0a 20 20 62 6c 6f 62 5f 72 65 77 69 6e 64 28 70  .  blob_rewind(p
0e80: 50 69 76 6f 74 29 3b 0a 0a 20 20 2f 2a 20 44 65  Pivot);..  /* De
0e90: 74 65 72 6d 69 6e 65 20 74 68 65 20 6c 65 6e 67  termine the leng
0ea0: 74 68 20 6f 66 20 74 68 65 20 61 43 31 5b 5d 20  th of the aC1[] 
0eb0: 61 6e 64 20 61 43 32 5b 5d 20 63 68 61 6e 67 65  and aC2[] change
0ec0: 20 76 65 63 74 6f 72 73 20 2a 2f 0a 20 20 66 6f   vectors */.  fo
0ed0: 72 28 69 31 3d 30 3b 20 61 43 31 5b 69 31 5d 20  r(i1=0; aC1[i1] 
0ee0: 7c 7c 20 61 43 31 5b 69 31 2b 31 5d 20 7c 7c 20  || aC1[i1+1] || 
0ef0: 61 43 31 5b 69 31 2b 32 5d 3b 20 69 31 2b 3d 33  aC1[i1+2]; i1+=3
0f00: 29 7b 7d 0a 20 20 6c 69 6d 69 74 31 20 3d 20 69  ){}.  limit1 = i
0f10: 31 3b 0a 20 20 66 6f 72 28 69 32 3d 30 3b 20 61  1;.  for(i2=0; a
0f20: 43 32 5b 69 32 5d 20 7c 7c 20 61 43 32 5b 69 32  C2[i2] || aC2[i2
0f30: 2b 31 5d 20 7c 7c 20 61 43 32 5b 69 32 2b 32 5d  +1] || aC2[i2+2]
0f40: 3b 20 69 32 2b 3d 33 29 7b 7d 0a 20 20 6c 69 6d  ; i2+=3){}.  lim
0f50: 69 74 32 20 3d 20 69 32 3b 0a 0a 20 20 44 45 42  it2 = i2;..  DEB
0f60: 55 47 28 0a 20 20 20 20 66 6f 72 28 69 31 3d 30  UG(.    for(i1=0
0f70: 3b 20 69 31 3c 6c 69 6d 69 74 31 3b 20 69 31 2b  ; i1<limit1; i1+
0f80: 3d 33 29 7b 0a 20 20 20 20 20 20 70 72 69 6e 74  =3){.      print
0f90: 66 28 22 63 31 3a 20 25 34 64 20 25 34 64 20 25  f("c1: %4d %4d %
0fa0: 34 64 5c 6e 22 2c 20 61 43 31 5b 69 31 5d 2c 20  4d\n", aC1[i1], 
0fb0: 61 43 31 5b 69 31 2b 31 5d 2c 20 61 43 31 5b 69  aC1[i1+1], aC1[i
0fc0: 31 2b 32 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  1+2]);.    }.   
0fd0: 20 66 6f 72 28 69 32 3d 30 3b 20 69 32 3c 6c 69   for(i2=0; i2<li
0fe0: 6d 69 74 32 3b 20 69 32 2b 3d 33 29 7b 0a 20 20  mit2; i2+=3){.  
0ff0: 20 20 20 70 72 69 6e 74 66 28 22 63 32 3a 20 25     printf("c2: %
1000: 34 64 20 25 34 64 20 25 34 64 5c 6e 22 2c 20 61  4d %4d %4d\n", a
1010: 43 32 5b 69 32 5d 2c 20 61 43 32 5b 69 32 2b 31  C2[i2], aC2[i2+1
1020: 5d 2c 20 61 43 32 5b 69 32 2b 32 5d 29 3b 0a 20  ], aC2[i2+2]);. 
1030: 20 20 20 7d 0a 20 20 29 0a 0a 20 20 6f 70 31 20     }.  )..  op1 
1040: 3d 20 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 69  = op2 = UNK;.  i
1050: 31 20 3d 20 69 32 20 3d 20 2d 31 3b 0a 20 20 6e  1 = i2 = -1;.  n
1060: 31 20 3d 20 6e 32 20 3d 20 30 3b 0a 20 20 77 68  1 = n2 = 0;.  wh
1070: 69 6c 65 28 31 29 7b 0a 20 20 20 20 69 66 28 20  ile(1){.    if( 
1080: 6f 70 31 3d 3d 55 4e 4b 20 29 7b 0a 20 20 20 20  op1==UNK ){.    
1090: 20 20 69 66 28 20 6e 31 20 29 7b 0a 20 20 20 20    if( n1 ){.    
10a0: 20 20 20 20 6f 70 31 20 3d 20 69 31 20 25 20 33      op1 = i1 % 3
10b0: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
10c0: 20 20 20 20 20 20 20 69 31 2b 2b 3b 0a 20 20 20         i1++;.   
10d0: 20 20 20 20 20 77 68 69 6c 65 28 20 69 31 3c 6c       while( i1<l
10e0: 69 6d 69 74 31 20 26 26 20 61 43 31 5b 69 31 5d  imit1 && aC1[i1]
10f0: 3d 3d 30 20 29 7b 20 69 31 2b 2b 3b 20 7d 0a 20  ==0 ){ i1++; }. 
1100: 20 20 20 20 20 20 20 69 66 28 20 69 31 3e 3d 6c         if( i1>=l
1110: 69 6d 69 74 31 20 29 7b 0a 20 20 20 20 20 20 20  imit1 ){.       
1120: 20 20 20 6f 70 31 20 3d 20 45 4e 44 3b 0a 20 20     op1 = END;.  
1130: 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1140: 20 20 20 20 20 20 20 6f 70 31 20 3d 20 69 31 20         op1 = i1 
1150: 25 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 6e  % 3;.          n
1160: 31 20 3d 20 61 43 31 5b 69 31 5d 3b 0a 20 20 20  1 = aC1[i1];.   
1170: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1180: 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f 70 32     }.    if( op2
1190: 3d 3d 55 4e 4b 20 29 7b 0a 20 20 20 20 20 20 69  ==UNK ){.      i
11a0: 66 28 20 6e 32 20 29 7b 0a 20 20 20 20 20 20 20  f( n2 ){.       
11b0: 20 6f 70 32 20 3d 20 69 32 20 25 20 33 3b 0a 20   op2 = i2 % 3;. 
11c0: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
11d0: 20 20 20 20 69 32 2b 2b 3b 0a 20 20 20 20 20 20      i2++;.      
11e0: 20 20 77 68 69 6c 65 28 20 69 32 3c 6c 69 6d 69    while( i2<limi
11f0: 74 32 20 26 26 20 61 43 32 5b 69 32 5d 3d 3d 30  t2 && aC2[i2]==0
1200: 20 29 7b 20 69 32 2b 2b 3b 20 7d 0a 20 20 20 20   ){ i2++; }.    
1210: 20 20 20 20 69 66 28 20 69 32 3e 3d 6c 69 6d 69      if( i2>=limi
1220: 74 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  t2 ){.          
1230: 6f 70 32 20 3d 20 45 4e 44 3b 0a 20 20 20 20 20  op2 = END;.     
1240: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1250: 20 20 20 20 6f 70 32 20 3d 20 69 32 20 25 20 33      op2 = i2 % 3
1260: 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 32 20 3d  ;.          n2 =
1270: 20 61 43 32 5b 69 32 5d 3b 0a 20 20 20 20 20 20   aC2[i2];.      
1280: 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1290: 7d 0a 20 20 20 20 44 45 42 55 47 28 20 70 72 69  }.    DEBUG( pri
12a0: 6e 74 66 28 22 6f 70 31 3d 25 73 28 25 64 29 20  ntf("op1=%s(%d) 
12b0: 6f 70 32 3d 25 73 28 25 64 29 5c 6e 22 2c 20 7a  op2=%s(%d)\n", z
12c0: 4f 70 5b 6f 70 31 5d 2c 20 6e 31 2c 20 7a 4f 70  Op[op1], n1, zOp
12d0: 5b 6f 70 32 5d 2c 20 6e 32 29 3b 20 29 0a 20 20  [op2], n2); ).  
12e0: 20 20 69 66 28 20 6f 70 31 3d 3d 45 4e 44 20 29    if( op1==END )
12f0: 7b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 32 3d  {.      if( op2=
1300: 3d 49 4e 53 20 29 7b 0a 20 20 20 20 20 20 20 20  =INS ){.        
1310: 44 45 42 55 47 28 20 70 72 69 6e 74 66 28 22 49  DEBUG( printf("I
1320: 4e 53 45 52 54 20 25 64 20 46 52 4f 4d 20 32 5c  NSERT %d FROM 2\
1330: 6e 22 2c 20 6e 32 29 3b 20 29 0a 20 20 20 20 20  n", n2); ).     
1340: 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e     blob_copy_lin
1350: 65 73 28 70 4f 75 74 2c 20 70 56 32 2c 20 6e 32  es(pOut, pV2, n2
1360: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1370: 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 65 6c 73   break;.    }els
1380: 65 20 69 66 28 20 6f 70 32 3d 3d 45 4e 44 20 29  e if( op2==END )
1390: 7b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 31 3d  {.      if( op1=
13a0: 3d 49 4e 53 20 29 7b 0a 20 20 20 20 20 20 20 20  =INS ){.        
13b0: 44 45 42 55 47 28 20 70 72 69 6e 74 66 28 22 49  DEBUG( printf("I
13c0: 4e 53 45 52 54 20 25 64 20 46 52 4f 4d 20 31 5c  NSERT %d FROM 1\
13d0: 6e 22 2c 20 6e 31 29 3b 20 29 0a 20 20 20 20 20  n", n1); ).     
13e0: 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e     blob_copy_lin
13f0: 65 73 28 70 4f 75 74 2c 20 70 56 31 2c 20 6e 31  es(pOut, pV1, n1
1400: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1410: 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 65 6c 73   break;.    }els
1420: 65 20 69 66 28 20 6f 70 31 3d 3d 43 50 59 20 26  e if( op1==CPY &
1430: 26 20 6f 70 32 3d 3d 43 50 59 20 29 7b 0a 20 20  & op2==CPY ){.  
1440: 20 20 20 20 6d 6e 20 3d 20 6d 69 6e 28 6e 31 2c      mn = min(n1,
1450: 6e 32 29 3b 0a 20 20 20 20 20 20 44 45 42 55 47  n2);.      DEBUG
1460: 28 20 70 72 69 6e 74 66 28 22 43 4f 50 59 20 25  ( printf("COPY %
1470: 64 5c 6e 22 2c 20 6d 6e 29 3b 20 29 0a 20 20 20  d\n", mn); ).   
1480: 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e     blob_copy_lin
1490: 65 73 28 70 4f 75 74 2c 20 70 50 69 76 6f 74 2c  es(pOut, pPivot,
14a0: 20 6d 6e 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62   mn);.      blob
14b0: 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 30 2c 20 70  _copy_lines(0, p
14c0: 56 31 2c 20 6d 6e 29 3b 0a 20 20 20 20 20 20 62  V1, mn);.      b
14d0: 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 30  lob_copy_lines(0
14e0: 2c 20 70 56 32 2c 20 6d 6e 29 3b 0a 20 20 20 20  , pV2, mn);.    
14f0: 20 20 6e 31 20 2d 3d 20 6d 6e 3b 0a 20 20 20 20    n1 -= mn;.    
1500: 20 20 6e 32 20 2d 3d 20 6d 6e 3b 0a 20 20 20 20    n2 -= mn;.    
1510: 20 20 6f 70 31 20 3d 20 6f 70 32 20 3d 20 55 4e    op1 = op2 = UN
1520: 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  K;.    }else if(
1530: 20 6f 70 31 3d 3d 44 45 4c 20 26 26 20 6f 70 32   op1==DEL && op2
1540: 3d 3d 44 45 4c 20 29 7b 0a 20 20 20 20 20 20 6d  ==DEL ){.      m
1550: 6e 20 3d 20 6d 69 6e 28 6e 31 2c 6e 32 29 3b 0a  n = min(n1,n2);.
1560: 20 20 20 20 20 20 44 45 42 55 47 28 20 70 72 69        DEBUG( pri
1570: 6e 74 66 28 22 53 4b 49 50 20 25 64 20 62 6f 74  ntf("SKIP %d bot
1580: 68 5c 6e 22 2c 20 6d 6e 29 3b 20 29 0a 20 20 20  h\n", mn); ).   
1590: 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e     blob_copy_lin
15a0: 65 73 28 30 2c 20 70 50 69 76 6f 74 2c 20 6d 6e  es(0, pPivot, mn
15b0: 29 3b 0a 20 20 20 20 20 20 6e 31 20 2d 3d 20 6d  );.      n1 -= m
15c0: 6e 3b 0a 20 20 20 20 20 20 6e 32 20 2d 3d 20 6d  n;.      n2 -= m
15d0: 6e 3b 0a 20 20 20 20 20 20 6f 70 31 20 3d 20 6f  n;.      op1 = o
15e0: 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 20 20 7d 65  p2 = UNK;.    }e
15f0: 6c 73 65 20 69 66 28 20 6f 70 31 3d 3d 49 4e 53  lse if( op1==INS
1600: 20 26 26 20 6f 70 32 3d 3d 49 4e 53 20 26 26 20   && op2==INS && 
1610: 73 61 6d 65 4c 69 6e 65 28 70 56 31 2c 20 70 56  sameLine(pV1, pV
1620: 32 29 20 29 7b 0a 20 20 20 20 20 20 44 45 42 55  2) ){.      DEBU
1630: 47 28 20 70 72 69 6e 74 66 28 22 44 55 50 4c 49  G( printf("DUPLI
1640: 43 41 54 45 20 49 4e 53 45 52 54 5c 6e 22 29 3b  CATE INSERT\n");
1650: 20 29 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f   ).      blob_co
1660: 70 79 5f 6c 69 6e 65 73 28 70 4f 75 74 2c 20 70  py_lines(pOut, p
1670: 56 32 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 6c  V2, 1);.      bl
1680: 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 30 2c  ob_copy_lines(0,
1690: 20 70 56 31 2c 20 31 29 3b 0a 20 20 20 20 20 20   pV1, 1);.      
16a0: 6e 31 2d 2d 3b 0a 20 20 20 20 20 20 6e 32 2d 2d  n1--;.      n2--
16b0: 3b 0a 20 20 20 20 20 20 6f 70 31 20 3d 20 6f 70  ;.      op1 = op
16c0: 32 20 3d 20 55 4e 4b 3b 0a 20 20 20 20 7d 65 6c  2 = UNK;.    }el
16d0: 73 65 20 69 66 28 20 6f 70 31 3d 3d 43 50 59 20  se if( op1==CPY 
16e0: 26 26 20 6f 70 32 3d 3d 44 45 4c 20 29 7b 0a 20  && op2==DEL ){. 
16f0: 20 20 20 20 20 6d 6e 20 3d 20 6d 69 6e 28 6e 31       mn = min(n1
1700: 2c 6e 32 29 3b 0a 20 20 20 20 20 20 44 45 42 55  ,n2);.      DEBU
1710: 47 28 20 70 72 69 6e 74 66 28 22 53 4b 49 50 20  G( printf("SKIP 
1720: 25 64 20 74 77 6f 5c 6e 22 2c 20 6d 6e 29 3b 20  %d two\n", mn); 
1730: 29 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70  ).      blob_cop
1740: 79 5f 6c 69 6e 65 73 28 30 2c 20 70 50 69 76 6f  y_lines(0, pPivo
1750: 74 2c 20 6d 6e 29 3b 0a 20 20 20 20 20 20 62 6c  t, mn);.      bl
1760: 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 30 2c  ob_copy_lines(0,
1770: 20 70 56 31 2c 20 6d 6e 29 3b 0a 20 20 20 20 20   pV1, mn);.     
1780: 20 6e 31 20 2d 3d 20 6d 6e 3b 0a 20 20 20 20 20   n1 -= mn;.     
1790: 20 6e 32 20 2d 3d 20 6d 6e 3b 0a 20 20 20 20 20   n2 -= mn;.     
17a0: 20 6f 70 31 20 3d 20 6f 70 32 20 3d 20 55 4e 4b   op1 = op2 = UNK
17b0: 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
17c0: 6f 70 32 3d 3d 43 50 59 20 26 26 20 6f 70 31 3d  op2==CPY && op1=
17d0: 3d 44 45 4c 20 29 7b 0a 20 20 20 20 20 20 6d 6e  =DEL ){.      mn
17e0: 20 3d 20 6d 69 6e 28 6e 31 2c 6e 32 29 3b 0a 20   = min(n1,n2);. 
17f0: 20 20 20 20 20 44 45 42 55 47 28 20 70 72 69 6e       DEBUG( prin
1800: 74 66 28 22 53 4b 49 50 20 25 64 20 6f 6e 65 5c  tf("SKIP %d one\
1810: 6e 22 2c 20 6d 6e 29 3b 20 29 0a 20 20 20 20 20  n", mn); ).     
1820: 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73   blob_copy_lines
1830: 28 30 2c 20 70 50 69 76 6f 74 2c 20 6d 6e 29 3b  (0, pPivot, mn);
1840: 0a 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79  .      blob_copy
1850: 5f 6c 69 6e 65 73 28 30 2c 20 70 56 32 2c 20 6d  _lines(0, pV2, m
1860: 6e 29 3b 0a 20 20 20 20 20 20 6e 32 20 2d 3d 20  n);.      n2 -= 
1870: 6d 6e 3b 0a 20 20 20 20 20 20 6e 31 20 2d 3d 20  mn;.      n1 -= 
1880: 6d 6e 3b 0a 20 20 20 20 20 20 6f 70 31 20 3d 20  mn;.      op1 = 
1890: 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 20 20 7d  op2 = UNK;.    }
18a0: 65 6c 73 65 20 69 66 28 20 6f 70 31 3d 3d 43 50  else if( op1==CP
18b0: 59 20 26 26 20 6f 70 32 3d 3d 49 4e 53 20 29 7b  Y && op2==INS ){
18c0: 0a 20 20 20 20 20 20 44 45 42 55 47 28 20 70 72  .      DEBUG( pr
18d0: 69 6e 74 66 28 22 49 4e 53 45 52 54 20 25 64 20  intf("INSERT %d 
18e0: 74 77 6f 5c 6e 22 2c 20 6e 32 29 3b 20 29 0a 20  two\n", n2); ). 
18f0: 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c       blob_copy_l
1900: 69 6e 65 73 28 70 4f 75 74 2c 20 70 56 32 2c 20  ines(pOut, pV2, 
1910: 6e 32 29 3b 0a 20 20 20 20 20 20 6e 32 20 3d 20  n2);.      n2 = 
1920: 30 3b 0a 20 20 20 20 20 20 6f 70 32 20 3d 20 55  0;.      op2 = U
1930: 4e 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  NK;.    }else if
1940: 28 20 6f 70 32 3d 3d 43 50 59 20 26 26 20 6f 70  ( op2==CPY && op
1950: 31 3d 3d 49 4e 53 20 29 7b 0a 20 20 20 20 20 20  1==INS ){.      
1960: 44 45 42 55 47 28 20 70 72 69 6e 74 66 28 22 49  DEBUG( printf("I
1970: 4e 53 45 52 54 20 25 64 20 6f 6e 65 5c 6e 22 2c  NSERT %d one\n",
1980: 20 6e 31 29 3b 20 29 0a 20 20 20 20 20 20 62 6c   n1); ).      bl
1990: 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 70 4f  ob_copy_lines(pO
19a0: 75 74 2c 20 70 56 31 2c 20 6e 31 29 3b 0a 20 20  ut, pV1, n1);.  
19b0: 20 20 20 20 6e 31 20 3d 20 30 3b 0a 20 20 20 20      n1 = 0;.    
19c0: 20 20 6f 70 31 20 3d 20 55 4e 4b 3b 0a 20 20 20    op1 = UNK;.   
19d0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e   }else{.      in
19e0: 74 20 74 6f 53 6b 69 70 20 3d 20 30 3b 0a 20 20  t toSkip = 0;.  
19f0: 20 20 20 20 6e 43 6f 6e 66 6c 69 63 74 2b 2b 3b      nConflict++;
1a00: 0a 20 20 20 20 20 20 44 45 42 55 47 28 20 70 72  .      DEBUG( pr
1a10: 69 6e 74 66 28 22 43 4f 4e 46 4c 49 43 54 5c 6e  intf("CONFLICT\n
1a20: 22 29 3b 20 29 0a 20 20 20 20 20 20 62 6c 6f 62  "); ).      blob
1a30: 5f 61 70 70 65 6e 64 66 28 70 4f 75 74 2c 20 7a  _appendf(pOut, z
1a40: 42 65 67 69 6e 29 3b 0a 20 20 20 20 20 20 69 66  Begin);.      if
1a50: 28 20 6f 70 31 3d 3d 44 45 4c 20 29 7b 0a 20 20  ( op1==DEL ){.  
1a60: 20 20 20 20 20 20 74 6f 53 6b 69 70 20 3d 20 6e        toSkip = n
1a70: 31 3b 0a 20 20 20 20 20 20 20 20 69 31 2b 2b 3b  1;.        i1++;
1a80: 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 43 31  .        if( aC1
1a90: 5b 69 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  [i1] ){.        
1aa0: 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65    blob_copy_line
1ab0: 73 28 70 4f 75 74 2c 20 70 56 31 2c 20 61 43 31  s(pOut, pV1, aC1
1ac0: 5b 69 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d  [i1]);.        }
1ad0: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1ae0: 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f        blob_copy_
1af0: 6c 69 6e 65 73 28 70 4f 75 74 2c 20 70 56 31 2c  lines(pOut, pV1,
1b00: 20 6e 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20   n1);.      }.  
1b10: 20 20 20 20 6e 31 20 3d 20 30 3b 0a 20 20 20 20      n1 = 0;.    
1b20: 20 20 6f 70 31 20 3d 20 55 4e 4b 3b 0a 20 20 20    op1 = UNK;.   
1b30: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
1b40: 70 4f 75 74 2c 20 7a 4d 69 64 29 3b 0a 20 20 20  pOut, zMid);.   
1b50: 20 20 20 69 66 28 20 6f 70 32 3d 3d 44 45 4c 20     if( op2==DEL 
1b60: 29 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f  ){.        blob_
1b70: 63 6f 70 79 5f 6c 69 6e 65 73 28 30 2c 20 70 50  copy_lines(0, pP
1b80: 69 76 6f 74 2c 20 6e 32 29 3b 0a 20 20 20 20 20  ivot, n2);.     
1b90: 20 20 20 69 32 2b 2b 3b 0a 20 20 20 20 20 20 20     i2++;.       
1ba0: 20 69 66 28 20 61 43 32 5b 69 32 5d 20 29 7b 0a   if( aC2[i2] ){.
1bb0: 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 63            blob_c
1bc0: 6f 70 79 5f 6c 69 6e 65 73 28 70 4f 75 74 2c 20  opy_lines(pOut, 
1bd0: 70 56 32 2c 20 61 43 32 5b 69 32 5d 29 3b 0a 20  pV2, aC2[i2]);. 
1be0: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1bf0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 6c  else{.        bl
1c00: 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 70 4f  ob_copy_lines(pO
1c10: 75 74 2c 20 70 56 32 2c 20 6e 32 29 3b 0a 20 20  ut, pV2, n2);.  
1c20: 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1c30: 74 6f 53 6b 69 70 20 29 7b 0a 20 20 20 20 20 20  toSkip ){.      
1c40: 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65    blob_copy_line
1c50: 73 28 30 2c 20 70 50 69 76 6f 74 2c 20 74 6f 53  s(0, pPivot, toS
1c60: 6b 69 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  kip);.      }.  
1c70: 20 20 20 20 6e 32 20 3d 20 30 3b 0a 20 20 20 20      n2 = 0;.    
1c80: 20 20 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 20    op2 = UNK;.   
1c90: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28     blob_appendf(
1ca0: 70 4f 75 74 2c 20 7a 45 6e 64 29 3b 0a 20 20 20  pOut, zEnd);.   
1cb0: 20 7d 0a 20 20 7d 0a 0a 20 20 66 72 65 65 28 61   }.  }..  free(a
1cc0: 43 31 29 3b 0a 20 20 66 72 65 65 28 61 43 32 29  C1);.  free(aC2)
1cd0: 3b 0a 20 20 72 65 74 75 72 6e 20 6e 43 6f 6e 66  ;.  return nConf
1ce0: 6c 69 63 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  lict;.}../*.** C
1cf0: 4f 4d 4d 41 4e 44 3a 20 20 74 65 73 74 2d 33 2d  OMMAND:  test-3-
1d00: 77 61 79 2d 6d 65 72 67 65 0a 2a 2a 0a 2a 2a 20  way-merge.**.** 
1d10: 43 6f 6d 62 69 6e 65 20 63 68 61 6e 67 65 20 69  Combine change i
1d20: 6e 20 67 6f 69 6e 67 20 66 72 6f 6d 20 50 49 56  n going from PIV
1d30: 4f 54 2d 3e 56 45 52 53 49 4f 4e 31 20 77 69 74  OT->VERSION1 wit
1d40: 68 20 74 68 65 20 63 68 61 6e 67 65 20 67 6f 69  h the change goi
1d50: 6e 67 0a 2a 2a 20 66 72 6f 6d 20 50 49 56 4f 54  ng.** from PIVOT
1d60: 2d 3e 56 45 52 53 49 4f 4e 32 20 61 6e 64 20 77  ->VERSION2 and w
1d70: 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
1d80: 64 20 63 68 61 6e 67 65 73 20 69 6e 74 6f 20 4d  d changes into M
1d90: 45 52 47 45 44 2e 0a 2a 2f 0a 76 6f 69 64 20 64  ERGED..*/.void d
1da0: 65 6c 74 61 5f 33 77 61 79 6d 65 72 67 65 5f 63  elta_3waymerge_c
1db0: 6d 64 28 76 6f 69 64 29 7b 0a 20 20 42 6c 6f 62  md(void){.  Blob
1dc0: 20 70 69 76 6f 74 2c 20 76 31 2c 20 76 32 2c 20   pivot, v1, v2, 
1dd0: 6d 65 72 67 65 64 3b 0a 20 20 69 66 28 20 67 2e  merged;.  if( g.
1de0: 61 72 67 63 21 3d 36 20 29 7b 0a 20 20 20 20 66  argc!=6 ){.    f
1df0: 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 22 55  printf(stderr,"U
1e00: 73 61 67 65 3a 20 25 73 20 25 73 20 50 49 56 4f  sage: %s %s PIVO
1e10: 54 20 56 31 20 56 32 20 4d 45 52 47 45 44 5c 6e  T V1 V2 MERGED\n
1e20: 22 2c 20 67 2e 61 72 67 76 5b 30 5d 2c 20 67 2e  ", g.argv[0], g.
1e30: 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 78  argv[1]);.    ex
1e40: 69 74 28 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28  it(1);.  }.  if(
1e50: 20 62 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f   blob_read_from_
1e60: 66 69 6c 65 28 26 70 69 76 6f 74 2c 20 67 2e 61  file(&pivot, g.a
1e70: 72 67 76 5b 32 5d 29 3c 30 20 29 7b 0a 20 20 20  rgv[2])<0 ){.   
1e80: 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
1e90: 22 63 61 6e 6e 6f 74 20 72 65 61 64 20 25 73 5c  "cannot read %s\
1ea0: 6e 22 2c 20 67 2e 61 72 67 76 5b 32 5d 29 3b 0a  n", g.argv[2]);.
1eb0: 20 20 20 20 65 78 69 74 28 31 29 3b 0a 20 20 7d      exit(1);.  }
1ec0: 0a 20 20 69 66 28 20 62 6c 6f 62 5f 72 65 61 64  .  if( blob_read
1ed0: 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 76 31 2c 20  _from_file(&v1, 
1ee0: 67 2e 61 72 67 76 5b 33 5d 29 3c 30 20 29 7b 0a  g.argv[3])<0 ){.
1ef0: 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
1f00: 72 72 2c 22 63 61 6e 6e 6f 74 20 72 65 61 64 20  rr,"cannot read 
1f10: 25 73 5c 6e 22 2c 20 67 2e 61 72 67 76 5b 33 5d  %s\n", g.argv[3]
1f20: 29 3b 0a 20 20 20 20 65 78 69 74 28 31 29 3b 0a  );.    exit(1);.
1f30: 20 20 7d 0a 20 20 69 66 28 20 62 6c 6f 62 5f 72    }.  if( blob_r
1f40: 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 76  ead_from_file(&v
1f50: 32 2c 20 67 2e 61 72 67 76 5b 34 5d 29 3c 30 20  2, g.argv[4])<0 
1f60: 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73  ){.    fprintf(s
1f70: 74 64 65 72 72 2c 22 63 61 6e 6e 6f 74 20 72 65  tderr,"cannot re
1f80: 61 64 20 25 73 5c 6e 22 2c 20 67 2e 61 72 67 76  ad %s\n", g.argv
1f90: 5b 34 5d 29 3b 0a 20 20 20 20 65 78 69 74 28 31  [4]);.    exit(1
1fa0: 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 6d 65  );.  }.  blob_me
1fb0: 72 67 65 28 26 70 69 76 6f 74 2c 20 26 76 31 2c  rge(&pivot, &v1,
1fc0: 20 26 76 32 2c 20 26 6d 65 72 67 65 64 29 3b 0a   &v2, &merged);.
1fd0: 20 20 69 66 28 20 62 6c 6f 62 5f 77 72 69 74 65    if( blob_write
1fe0: 5f 74 6f 5f 66 69 6c 65 28 26 6d 65 72 67 65 64  _to_file(&merged
1ff0: 2c 20 67 2e 61 72 67 76 5b 35 5d 29 3c 62 6c 6f  , g.argv[5])<blo
2000: 62 5f 73 69 7a 65 28 26 6d 65 72 67 65 64 29 20  b_size(&merged) 
2010: 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73  ){.    fprintf(s
2020: 74 64 65 72 72 2c 22 63 61 6e 6e 6f 74 20 77 72  tderr,"cannot wr
2030: 69 74 65 20 25 73 5c 6e 22 2c 20 67 2e 61 72 67  ite %s\n", g.arg
2040: 76 5b 34 5d 29 3b 0a 20 20 20 20 65 78 69 74 28  v[4]);.    exit(
2050: 31 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72  1);.  }.  blob_r
2060: 65 73 65 74 28 26 70 69 76 6f 74 29 3b 0a 20 20  eset(&pivot);.  
2070: 62 6c 6f 62 5f 72 65 73 65 74 28 26 76 31 29 3b  blob_reset(&v1);
2080: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 76  .  blob_reset(&v
2090: 32 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74  2);.  blob_reset
20a0: 28 26 6d 65 72 67 65 64 29 3b 0a 7d 0a           (&merged);.}.