Hex Artifact Content
Not logged in

Artifact b966dfba2bae917eaf5f0a567fd9cb53d2be401f:

File src/merge3.c part of check-in [36b96b8616] - Rework the merge algorithm. It now only works for text files. But, it no longer gets confused by line endings (\r\n versus \n) and it reports conflicts. by drh on 2007-11-16 20:42:31.

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 62 6c 6f 62 5f 62 75 66 66 65 72 28 70   = blob_buffer(p
0560: 56 31 29 3b 0a 20 20 7a 32 20 3d 20 62 6c 6f 62  V1);.  z2 = blob
0570: 5f 62 75 66 66 65 72 28 70 56 32 29 3b 0a 20 20  _buffer(pV2);.  
0580: 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d 21 3d  for(i=0; z1[i]!=
0590: 27 5c 6e 27 20 26 26 20 7a 31 5b 69 5d 3d 3d 7a  '\n' && z1[i]==z
05a0: 32 5b 69 5d 3b 20 69 2b 2b 29 7b 7d 0a 20 20 72  2[i]; i++){}.  r
05b0: 65 74 75 72 6e 20 7a 32 5b 69 5d 3d 3d 27 5c 6e  eturn z2[i]=='\n
05c0: 27 20 7c 7c 20 28 7a 32 5b 69 5d 3d 3d 27 5c 72  ' || (z2[i]=='\r
05d0: 27 20 26 26 20 7a 32 5b 69 2b 31 5d 3d 3d 27 5c  ' && z2[i+1]=='\
05e0: 6e 27 29 0a 20 20 20 20 20 20 20 20 20 20 7c 7c  n').          ||
05f0: 20 28 7a 31 5b 69 5d 3d 3d 27 5c 72 27 20 26 26   (z1[i]=='\r' &&
0600: 20 7a 32 5b 69 5d 3d 3d 27 5c 6e 27 20 26 26 20   z2[i]=='\n' && 
0610: 7a 31 5b 69 2b 31 5d 3d 3d 27 5c 6e 27 29 3b 0a  z1[i+1]=='\n');.
0620: 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 74 68  }../*.** Do a th
0630: 72 65 65 2d 77 61 79 20 6d 65 72 67 65 2e 20 20  ree-way merge.  
0640: 49 6e 69 74 69 61 6c 69 7a 65 20 70 4f 75 74 20  Initialize pOut 
0650: 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 72  to contain the r
0660: 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  esult..**.** The
0670: 20 6d 65 72 67 65 20 69 73 20 61 6e 20 65 64 69   merge is an edi
0680: 74 20 61 67 61 69 6e 73 74 20 70 56 32 2e 20 20  t against pV2.  
0690: 42 6f 74 68 20 70 56 31 20 61 6e 64 20 70 56 32  Both pV1 and pV2
06a0: 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 6d 6d 6f   have a.** commo
06b0: 6e 20 6f 72 69 67 69 6e 20 61 74 20 70 50 69 76  n origin at pPiv
06c0: 6f 74 2e 20 20 41 70 70 6c 79 20 74 68 65 20 63  ot.  Apply the c
06d0: 68 61 6e 67 65 73 20 6f 66 20 70 50 69 76 6f 74  hanges of pPivot
06e0: 20 3d 3d 3e 20 70 56 31 0a 2a 2a 20 74 6f 20 70   ==> pV1.** to p
06f0: 56 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  V2..**.** The re
0700: 74 75 72 6e 20 69 73 20 30 20 75 70 6f 6e 20 63  turn is 0 upon c
0710: 6f 6d 70 6c 65 74 65 20 73 75 63 63 65 73 73 2e  omplete success.
0720: 20 49 66 20 61 6e 79 20 69 6e 70 75 74 20 66 69   If any input fi
0730: 6c 65 20 69 73 20 62 69 6e 61 72 79 2c 0a 2a 2a  le is binary,.**
0740: 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64 20   -1 is returned 
0750: 61 6e 64 20 70 4f 75 74 20 69 73 20 75 6e 6d 6f  and pOut is unmo
0760: 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 72  dified.  If ther
0770: 65 20 61 72 65 20 6d 65 72 67 65 0a 2a 2a 20 63  e are merge.** c
0780: 6f 6e 66 6c 69 63 74 73 2c 20 74 68 65 20 6d 65  onflicts, the me
0790: 72 67 65 20 70 72 6f 63 65 65 64 73 20 61 73 20  rge proceeds as 
07a0: 62 65 73 74 20 61 73 20 69 74 20 63 61 6e 20 61  best as it can a
07b0: 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 0a 2a  nd the number .*
07c0: 2a 20 6f 66 20 63 6f 6e 66 6c 69 63 74 73 20 69  * of conflicts i
07d0: 73 20 72 65 74 75 72 6e 73 0a 2a 2f 0a 69 6e 74  s returns.*/.int
07e0: 20 62 6c 6f 62 5f 6d 65 72 67 65 28 42 6c 6f 62   blob_merge(Blob
07f0: 20 2a 70 50 69 76 6f 74 2c 20 42 6c 6f 62 20 2a   *pPivot, Blob *
0800: 70 56 31 2c 20 42 6c 6f 62 20 2a 70 56 32 2c 20  pV1, Blob *pV2, 
0810: 42 6c 6f 62 20 2a 70 4f 75 74 29 7b 0a 20 20 69  Blob *pOut){.  i
0820: 6e 74 20 2a 61 43 31 3b 20 20 2f 2a 20 43 68 61  nt *aC1;  /* Cha
0830: 6e 67 65 73 20 66 72 6f 6d 20 70 50 69 76 6f 74  nges from pPivot
0840: 20 74 6f 20 70 56 31 20 2a 2f 0a 20 20 69 6e 74   to pV1 */.  int
0850: 20 2a 61 43 32 3b 20 20 2f 2a 20 43 68 61 6e 67   *aC2;  /* Chang
0860: 65 73 20 66 72 6f 6d 20 70 50 69 76 6f 74 20 74  es from pPivot t
0870: 6f 20 70 56 32 20 2a 2f 0a 20 20 69 6e 74 20 69  o pV2 */.  int i
0880: 31 2c 20 69 32 3b 0a 20 20 69 6e 74 20 6f 70 31  1, i2;.  int op1
0890: 2c 20 6f 70 32 3b 0a 20 20 69 6e 74 20 6c 69 6d  , op2;.  int lim
08a0: 69 74 31 2c 20 6c 69 6d 69 74 32 3b 0a 20 20 69  it1, limit2;.  i
08b0: 6e 74 20 69 6e 43 6f 6e 66 6c 69 63 74 20 3d 20  nt inConflict = 
08c0: 30 3b 0a 20 20 69 6e 74 20 6e 43 6f 6e 66 6c 69  0;.  int nConfli
08d0: 63 74 20 3d 20 30 3b 0a 20 20 73 74 61 74 69 63  ct = 0;.  static
08e0: 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 42 65 67   const char zBeg
08f0: 69 6e 5b 5d 20 3d 20 22 3e 3e 3e 3e 3e 3e 3e 3e  in[] = ">>>>>>>>
0900: 20 42 45 47 49 4e 20 4d 45 52 47 45 20 43 4f 4e   BEGIN MERGE CON
0910: 46 4c 49 43 54 20 3c 3c 3c 3c 3c 3c 3c 3c 5c 6e  FLICT <<<<<<<<\n
0920: 22 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ";.  static cons
0930: 74 20 63 68 61 72 20 7a 45 6e 64 5b 5d 20 20 20  t char zEnd[]   
0940: 3d 20 22 3e 3e 3e 3e 3e 3e 3e 3e 3e 20 45 4e 44  = ">>>>>>>>> END
0950: 20 4d 45 52 47 45 20 43 4f 4e 46 4c 49 43 54 20   MERGE CONFLICT 
0960: 3c 3c 3c 3c 3c 3c 3c 3c 3c 5c 6e 22 3b 0a 0a 20  <<<<<<<<<\n";.. 
0970: 20 61 43 31 20 3d 20 74 65 78 74 5f 64 69 66 66   aC1 = text_diff
0980: 28 70 50 69 76 6f 74 2c 20 70 56 31 2c 20 30 2c  (pPivot, pV1, 0,
0990: 20 30 29 3b 0a 20 20 61 43 32 20 3d 20 74 65 78   0);.  aC2 = tex
09a0: 74 5f 64 69 66 66 28 70 50 69 76 6f 74 2c 20 70  t_diff(pPivot, p
09b0: 56 32 2c 20 30 2c 20 30 29 3b 0a 0a 20 20 69 66  V2, 0, 0);..  if
09c0: 28 20 61 43 32 3d 3d 30 20 7c 7c 20 61 43 32 3d  ( aC2==0 || aC2=
09d0: 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 61  =0 ){.    free(a
09e0: 43 31 29 3b 0a 20 20 20 20 66 72 65 65 28 61 43  C1);.    free(aC
09f0: 32 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 2d  2);.    return -
0a00: 31 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 7a 65  1;.  }.  blob_ze
0a10: 72 6f 28 70 4f 75 74 29 3b 0a 20 20 62 6c 6f 62  ro(pOut);.  blob
0a20: 5f 72 65 77 69 6e 64 28 70 56 31 29 3b 0a 20 20  _rewind(pV1);.  
0a30: 62 6c 6f 62 5f 72 65 77 69 6e 64 28 70 56 32 29  blob_rewind(pV2)
0a40: 3b 0a 20 20 62 6c 6f 62 5f 72 65 77 69 6e 64 28  ;.  blob_rewind(
0a50: 70 50 69 76 6f 74 29 3b 0a 0a 20 20 66 6f 72 28  pPivot);..  for(
0a60: 69 31 3d 30 3b 20 61 43 31 5b 69 31 5d 20 7c 7c  i1=0; aC1[i1] ||
0a70: 20 61 43 31 5b 69 31 2b 31 5d 20 7c 7c 20 61 43   aC1[i1+1] || aC
0a80: 31 5b 69 31 2b 32 5d 3b 20 69 31 2b 3d 33 29 7b  1[i1+2]; i1+=3){
0a90: 7d 0a 20 20 6c 69 6d 69 74 31 20 3d 20 69 31 3b  }.  limit1 = i1;
0aa0: 0a 20 20 66 6f 72 28 69 32 3d 30 3b 20 61 43 32  .  for(i2=0; aC2
0ab0: 5b 69 32 5d 20 7c 7c 20 61 43 32 5b 69 32 2b 31  [i2] || aC2[i2+1
0ac0: 5d 20 7c 7c 20 61 43 32 5b 69 32 2b 32 5d 3b 20  ] || aC2[i2+2]; 
0ad0: 69 32 2b 3d 33 29 7b 7d 0a 20 20 6c 69 6d 69 74  i2+=3){}.  limit
0ae0: 32 20 3d 20 69 32 3b 0a 0a 20 20 44 45 42 55 47  2 = i2;..  DEBUG
0af0: 28 0a 20 20 20 20 66 6f 72 28 69 31 3d 30 3b 20  (.    for(i1=0; 
0b00: 69 31 3c 6c 69 6d 69 74 31 3b 20 69 31 2b 3d 33  i1<limit1; i1+=3
0b10: 29 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28  ){.      printf(
0b20: 22 63 31 3a 20 25 34 64 20 25 34 64 20 25 34 64  "c1: %4d %4d %4d
0b30: 5c 6e 22 2c 20 61 43 31 5b 69 31 5d 2c 20 61 43  \n", aC1[i1], aC
0b40: 31 5b 69 31 2b 31 5d 2c 20 61 43 31 5b 69 31 2b  1[i1+1], aC1[i1+
0b50: 32 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  2]);.    }.    f
0b60: 6f 72 28 69 32 3d 30 3b 20 69 32 3c 6c 69 6d 69  or(i2=0; i2<limi
0b70: 74 32 3b 20 69 32 2b 3d 33 29 7b 0a 20 20 20 20  t2; i2+=3){.    
0b80: 20 70 72 69 6e 74 66 28 22 63 32 3a 20 25 34 64   printf("c2: %4d
0b90: 20 25 34 64 20 25 34 64 5c 6e 22 2c 20 61 43 32   %4d %4d\n", aC2
0ba0: 5b 69 32 5d 2c 20 61 43 32 5b 69 32 2b 31 5d 2c  [i2], aC2[i2+1],
0bb0: 20 61 43 32 5b 69 32 2b 32 5d 29 3b 0a 20 20 20   aC2[i2+2]);.   
0bc0: 20 7d 0a 20 20 29 0a 0a 20 20 6f 70 31 20 3d 20   }.  )..  op1 = 
0bd0: 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 69 31 20  op2 = UNK;.  i1 
0be0: 3d 20 69 32 20 3d 20 30 3b 0a 20 20 77 68 69 6c  = i2 = 0;.  whil
0bf0: 65 28 20 69 31 3c 6c 69 6d 69 74 31 20 26 26 20  e( i1<limit1 && 
0c00: 61 43 31 5b 69 31 5d 3d 3d 30 20 29 7b 20 69 31  aC1[i1]==0 ){ i1
0c10: 2b 2b 3b 20 7d 0a 20 20 77 68 69 6c 65 28 20 69  ++; }.  while( i
0c20: 32 3c 6c 69 6d 69 74 32 20 26 26 20 61 43 32 5b  2<limit2 && aC2[
0c30: 69 32 5d 3d 3d 30 20 29 7b 20 69 32 2b 2b 3b 20  i2]==0 ){ i2++; 
0c40: 7d 0a 0a 20 20 77 68 69 6c 65 28 31 29 7b 0a 20  }..  while(1){. 
0c50: 20 20 20 69 66 28 20 6f 70 31 3d 3d 55 4e 4b 20     if( op1==UNK 
0c60: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 31 3e  ){.      if( i1>
0c70: 3d 6c 69 6d 69 74 31 20 29 7b 0a 20 20 20 20 20  =limit1 ){.     
0c80: 20 20 20 6f 70 31 20 3d 20 45 4e 44 3b 0a 20 20     op1 = END;.  
0c90: 20 20 20 20 20 20 44 45 42 55 47 28 20 70 72 69        DEBUG( pri
0ca0: 6e 74 66 28 22 67 65 74 20 6f 70 31 3d 45 4e 44  ntf("get op1=END
0cb0: 5c 6e 22 29 3b 20 29 0a 20 20 20 20 20 20 7d 65  \n"); ).      }e
0cc0: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6f 70 31  lse{.        op1
0cd0: 20 3d 20 69 31 20 25 20 33 3b 0a 20 20 20 20 20   = i1 % 3;.     
0ce0: 20 20 20 61 43 31 5b 69 31 5d 2d 2d 3b 0a 20 20     aC1[i1]--;.  
0cf0: 20 20 20 20 20 20 44 45 42 55 47 28 20 70 72 69        DEBUG( pri
0d00: 6e 74 66 28 22 67 65 74 20 6f 70 31 3d 25 64 20  ntf("get op1=%d 
0d10: 66 72 6f 6d 20 25 64 20 28 25 64 2d 3e 25 64 29  from %d (%d->%d)
0d20: 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
0d30: 20 20 20 20 6f 70 31 2c 69 31 2c 61 43 31 5b 69      op1,i1,aC1[i
0d40: 31 5d 2b 31 2c 61 43 31 5b 69 31 5d 29 3b 20 29  1]+1,aC1[i1]); )
0d50: 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
0d60: 69 31 3c 6c 69 6d 69 74 31 20 26 26 20 61 43 31  i1<limit1 && aC1
0d70: 5b 69 31 5d 3d 3d 30 20 29 7b 20 69 31 2b 2b 3b  [i1]==0 ){ i1++;
0d80: 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
0d90: 0a 20 20 20 20 69 66 28 20 6f 70 32 3d 3d 55 4e  .    if( op2==UN
0da0: 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  K ){.      if( i
0db0: 32 3e 3d 6c 69 6d 69 74 32 20 29 7b 0a 20 20 20  2>=limit2 ){.   
0dc0: 20 20 20 20 20 6f 70 32 20 3d 20 45 4e 44 3b 0a       op2 = END;.
0dd0: 20 20 20 20 20 20 20 20 44 45 42 55 47 28 20 70          DEBUG( p
0de0: 72 69 6e 74 66 28 22 67 65 74 20 6f 70 32 3d 45  rintf("get op2=E
0df0: 4e 44 5c 6e 22 29 3b 20 29 0a 20 20 20 20 20 20  ND\n"); ).      
0e00: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6f  }else{.        o
0e10: 70 32 20 3d 20 69 32 20 25 20 33 3b 0a 20 20 20  p2 = i2 % 3;.   
0e20: 20 20 20 20 20 61 43 32 5b 69 32 5d 2d 2d 3b 0a       aC2[i2]--;.
0e30: 20 20 20 20 20 20 20 20 44 45 42 55 47 28 20 70          DEBUG( p
0e40: 72 69 6e 74 66 28 22 67 65 74 20 6f 70 32 3d 25  rintf("get op2=%
0e50: 64 20 66 72 6f 6d 20 25 64 20 28 25 64 2d 3e 25  d from %d (%d->%
0e60: 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  d)\n",.         
0e70: 20 20 20 20 20 20 6f 70 32 2c 69 32 2c 61 43 32        op2,i2,aC2
0e80: 5b 69 32 5d 2b 31 2c 61 43 32 5b 69 32 5d 29 3b  [i2]+1,aC2[i2]);
0e90: 20 29 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65   ).        while
0ea0: 28 20 69 32 3c 6c 69 6d 69 74 32 20 26 26 20 61  ( i2<limit2 && a
0eb0: 43 32 5b 69 32 5d 3d 3d 30 20 29 7b 20 69 32 2b  C2[i2]==0 ){ i2+
0ec0: 2b 3b 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20  +; }.      }.   
0ed0: 20 7d 0a 20 20 20 20 44 45 42 55 47 28 20 70 72   }.    DEBUG( pr
0ee0: 69 6e 74 66 28 22 6f 70 31 3d 25 64 20 6f 70 32  intf("op1=%d op2
0ef0: 3d 25 64 5c 6e 22 2c 20 6f 70 31 2c 20 6f 70 32  =%d\n", op1, op2
0f00: 29 3b 20 29 0a 20 20 20 20 69 66 28 20 6f 70 31  ); ).    if( op1
0f10: 3d 3d 45 4e 44 20 29 7b 0a 20 20 20 20 20 20 69  ==END ){.      i
0f20: 66 28 20 6f 70 32 3d 3d 49 4e 53 20 29 7b 0a 20  f( op2==INS ){. 
0f30: 20 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79         blob_copy
0f40: 5f 6c 69 6e 65 73 28 70 4f 75 74 2c 20 70 56 32  _lines(pOut, pV2
0f50: 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 6f 70  , 1);.        op
0f60: 32 20 3d 20 55 4e 4b 3b 0a 20 20 20 20 20 20 7d  2 = UNK;.      }
0f70: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72  else{.        br
0f80: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
0f90: 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 32 3d 3d   }else if( op2==
0fa0: 45 4e 44 20 29 7b 0a 20 20 20 20 20 20 69 66 28  END ){.      if(
0fb0: 20 6f 70 31 3d 3d 49 4e 53 20 29 7b 0a 20 20 20   op1==INS ){.   
0fc0: 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c       blob_copy_l
0fd0: 69 6e 65 73 28 70 4f 75 74 2c 20 70 56 31 2c 20  ines(pOut, pV1, 
0fe0: 31 29 3b 0a 20 20 20 20 20 20 20 20 6f 70 31 20  1);.        op1 
0ff0: 3d 20 55 4e 4b 3b 0a 20 20 20 20 20 20 7d 65 6c  = UNK;.      }el
1000: 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61  se{.        brea
1010: 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1020: 65 6c 73 65 20 69 66 28 20 6f 70 31 3d 3d 49 4e  else if( op1==IN
1030: 53 20 26 26 20 6f 70 32 3d 3d 49 4e 53 20 29 7b  S && op2==INS ){
1040: 0a 20 20 20 20 20 20 69 66 28 20 21 69 6e 43 6f  .      if( !inCo
1050: 6e 66 6c 69 63 74 20 26 26 20 73 61 6d 65 4c 69  nflict && sameLi
1060: 6e 65 28 70 56 31 2c 20 70 56 32 29 20 29 7b 0a  ne(pV1, pV2) ){.
1070: 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70          blob_cop
1080: 79 5f 6c 69 6e 65 73 28 70 4f 75 74 2c 20 70 56  y_lines(pOut, pV
1090: 32 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 62  2, 1);.        b
10a0: 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 30  lob_copy_lines(0
10b0: 2c 20 70 56 31 2c 20 30 29 3b 0a 20 20 20 20 20  , pV1, 0);.     
10c0: 20 20 20 6f 70 31 20 3d 20 55 4e 4b 3b 0a 20 20     op1 = UNK;.  
10d0: 20 20 20 20 20 20 6f 70 32 20 3d 20 55 4e 4b 3b        op2 = UNK;
10e0: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
10f0: 20 20 20 20 20 20 69 66 28 20 21 69 6e 43 6f 6e        if( !inCon
1100: 66 6c 69 63 74 20 29 7b 0a 20 20 20 20 20 20 20  flict ){.       
1110: 20 20 20 69 6e 43 6f 6e 66 6c 69 63 74 20 3d 20     inConflict = 
1120: 31 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 43 6f  1;.          nCo
1130: 6e 66 6c 69 63 74 2b 2b 3b 0a 20 20 20 20 20 20  nflict++;.      
1140: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
1150: 28 70 4f 75 74 2c 20 7a 42 65 67 69 6e 29 3b 0a  (pOut, zBegin);.
1160: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1170: 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65    blob_copy_line
1180: 73 28 70 4f 75 74 2c 20 70 56 31 2c 20 31 29 3b  s(pOut, pV1, 1);
1190: 0a 20 20 20 20 20 20 20 20 6f 70 31 20 3d 20 55  .        op1 = U
11a0: 4e 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  NK;.      }.    
11b0: 7d 65 6c 73 65 20 69 66 28 20 6f 70 31 3d 3d 49  }else if( op1==I
11c0: 4e 53 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62  NS ){.      blob
11d0: 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 70 4f 75 74  _copy_lines(pOut
11e0: 2c 20 70 56 31 2c 20 31 29 3b 0a 20 20 20 20 20  , pV1, 1);.     
11f0: 20 6f 70 31 20 3d 20 55 4e 4b 3b 0a 20 20 20 20   op1 = UNK;.    
1200: 7d 65 6c 73 65 20 69 66 28 20 6f 70 32 3d 3d 49  }else if( op2==I
1210: 4e 53 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62  NS ){.      blob
1220: 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 70 4f 75 74  _copy_lines(pOut
1230: 2c 20 70 56 32 2c 20 31 29 3b 0a 20 20 20 20 20  , pV2, 1);.     
1240: 20 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 20 20   op2 = UNK;.    
1250: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
1260: 20 69 6e 43 6f 6e 66 6c 69 63 74 20 29 7b 0a 20   inConflict ){. 
1270: 20 20 20 20 20 20 20 69 6e 43 6f 6e 66 6c 69 63         inConflic
1280: 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  t = 0;.        b
1290: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 4f 75 74  lob_appendf(pOut
12a0: 2c 20 7a 45 6e 64 29 3b 0a 20 20 20 20 20 20 7d  , zEnd);.      }
12b0: 0a 20 20 20 20 20 20 69 66 28 20 6f 70 31 3d 3d  .      if( op1==
12c0: 44 45 4c 20 7c 7c 20 6f 70 32 3d 3d 44 45 4c 20  DEL || op2==DEL 
12d0: 29 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f  ){.        blob_
12e0: 63 6f 70 79 5f 6c 69 6e 65 73 28 30 2c 20 70 50  copy_lines(0, pP
12f0: 69 76 6f 74 2c 20 31 29 3b 0a 20 20 20 20 20 20  ivot, 1);.      
1300: 20 20 69 66 28 20 6f 70 32 3d 3d 43 50 59 20 29    if( op2==CPY )
1310: 7b 0a 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62  {.          blob
1320: 5f 63 6f 70 79 5f 6c 69 6e 65 73 28 30 2c 20 70  _copy_lines(0, p
1330: 56 32 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  V2, 1);.        
1340: 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6f 70  }.        if( op
1350: 31 3d 3d 43 50 59 20 29 7b 0a 20 20 20 20 20 20  1==CPY ){.      
1360: 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69      blob_copy_li
1370: 6e 65 73 28 30 2c 20 70 56 31 2c 20 31 29 3b 0a  nes(0, pV1, 1);.
1380: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1390: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
13a0: 73 73 65 72 74 28 20 6f 70 31 3d 3d 43 50 59 20  ssert( op1==CPY 
13b0: 26 26 20 6f 70 32 3d 3d 43 50 59 20 29 3b 0a 20  && op2==CPY );. 
13c0: 20 20 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79         blob_copy
13d0: 5f 6c 69 6e 65 73 28 70 4f 75 74 2c 20 70 50 69  _lines(pOut, pPi
13e0: 76 6f 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  vot, 1);.       
13f0: 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c 69 6e 65 73   blob_copy_lines
1400: 28 30 2c 20 70 56 31 2c 20 31 29 3b 0a 20 20 20  (0, pV1, 1);.   
1410: 20 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 5f 6c       blob_copy_l
1420: 69 6e 65 73 28 30 2c 20 70 56 32 2c 20 31 29 3b  ines(0, pV2, 1);
1430: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6f  .      }.      o
1440: 70 31 20 3d 20 55 4e 4b 3b 0a 20 20 20 20 20 20  p1 = UNK;.      
1450: 6f 70 32 20 3d 20 55 4e 4b 3b 0a 20 20 20 20 7d  op2 = UNK;.    }
1460: 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 43 6f 6e  .  }.  if( inCon
1470: 66 6c 69 63 74 20 29 7b 0a 20 20 20 20 62 6c 6f  flict ){.    blo
1480: 62 5f 61 70 70 65 6e 64 66 28 70 4f 75 74 2c 20  b_appendf(pOut, 
1490: 7a 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 66 72  zEnd);.  }..  fr
14a0: 65 65 28 61 43 31 29 3b 0a 20 20 66 72 65 65 28  ee(aC1);.  free(
14b0: 61 43 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e  aC2);.  return n
14c0: 43 6f 6e 66 6c 69 63 74 3b 0a 7d 0a 0a 2f 2a 0a  Conflict;.}../*.
14d0: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 20 74 65 73  ** COMMAND:  tes
14e0: 74 2d 33 2d 77 61 79 2d 6d 65 72 67 65 0a 2a 2a  t-3-way-merge.**
14f0: 0a 2a 2a 20 43 6f 6d 62 69 6e 65 20 63 68 61 6e  .** Combine chan
1500: 67 65 20 69 6e 20 67 6f 69 6e 67 20 66 72 6f 6d  ge in going from
1510: 20 50 49 56 4f 54 2d 3e 56 45 52 53 49 4f 4e 31   PIVOT->VERSION1
1520: 20 77 69 74 68 20 74 68 65 20 63 68 61 6e 67 65   with the change
1530: 20 67 6f 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20 50   going.** from P
1540: 49 56 4f 54 2d 3e 56 45 52 53 49 4f 4e 32 20 61  IVOT->VERSION2 a
1550: 6e 64 20 77 72 69 74 65 20 74 68 65 20 63 6f 6d  nd write the com
1560: 62 69 6e 65 64 20 63 68 61 6e 67 65 73 20 69 6e  bined changes in
1570: 74 6f 20 4d 45 52 47 45 44 2e 0a 2a 2f 0a 76 6f  to MERGED..*/.vo
1580: 69 64 20 64 65 6c 74 61 5f 33 77 61 79 6d 65 72  id delta_3waymer
1590: 67 65 5f 63 6d 64 28 76 6f 69 64 29 7b 0a 20 20  ge_cmd(void){.  
15a0: 42 6c 6f 62 20 70 69 76 6f 74 2c 20 76 31 2c 20  Blob pivot, v1, 
15b0: 76 32 2c 20 6d 65 72 67 65 64 3b 0a 20 20 69 66  v2, merged;.  if
15c0: 28 20 67 2e 61 72 67 63 21 3d 36 20 29 7b 0a 20  ( g.argc!=6 ){. 
15d0: 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
15e0: 72 2c 22 55 73 61 67 65 3a 20 25 73 20 25 73 20  r,"Usage: %s %s 
15f0: 50 49 56 4f 54 20 56 31 20 56 32 20 4d 45 52 47  PIVOT V1 V2 MERG
1600: 45 44 5c 6e 22 2c 20 67 2e 61 72 67 76 5b 30 5d  ED\n", g.argv[0]
1610: 2c 20 67 2e 61 72 67 76 5b 31 5d 29 3b 0a 20 20  , g.argv[1]);.  
1620: 20 20 65 78 69 74 28 31 29 3b 0a 20 20 7d 0a 20    exit(1);.  }. 
1630: 20 69 66 28 20 62 6c 6f 62 5f 72 65 61 64 5f 66   if( blob_read_f
1640: 72 6f 6d 5f 66 69 6c 65 28 26 70 69 76 6f 74 2c  rom_file(&pivot,
1650: 20 67 2e 61 72 67 76 5b 32 5d 29 3c 30 20 29 7b   g.argv[2])<0 ){
1660: 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64  .    fprintf(std
1670: 65 72 72 2c 22 63 61 6e 6e 6f 74 20 72 65 61 64  err,"cannot read
1680: 20 25 73 5c 6e 22 2c 20 67 2e 61 72 67 76 5b 32   %s\n", g.argv[2
1690: 5d 29 3b 0a 20 20 20 20 65 78 69 74 28 31 29 3b  ]);.    exit(1);
16a0: 0a 20 20 7d 0a 20 20 69 66 28 20 62 6c 6f 62 5f  .  }.  if( blob_
16b0: 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26  read_from_file(&
16c0: 76 31 2c 20 67 2e 61 72 67 76 5b 33 5d 29 3c 30  v1, g.argv[3])<0
16d0: 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28   ){.    fprintf(
16e0: 73 74 64 65 72 72 2c 22 63 61 6e 6e 6f 74 20 72  stderr,"cannot r
16f0: 65 61 64 20 25 73 5c 6e 22 2c 20 67 2e 61 72 67  ead %s\n", g.arg
1700: 76 5b 33 5d 29 3b 0a 20 20 20 20 65 78 69 74 28  v[3]);.    exit(
1710: 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 62 6c  1);.  }.  if( bl
1720: 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c  ob_read_from_fil
1730: 65 28 26 76 32 2c 20 67 2e 61 72 67 76 5b 34 5d  e(&v2, g.argv[4]
1740: 29 3c 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  )<0 ){.    fprin
1750: 74 66 28 73 74 64 65 72 72 2c 22 63 61 6e 6e 6f  tf(stderr,"canno
1760: 74 20 72 65 61 64 20 25 73 5c 6e 22 2c 20 67 2e  t read %s\n", g.
1770: 61 72 67 76 5b 34 5d 29 3b 0a 20 20 20 20 65 78  argv[4]);.    ex
1780: 69 74 28 31 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f  it(1);.  }.  blo
1790: 62 5f 6d 65 72 67 65 28 26 70 69 76 6f 74 2c 20  b_merge(&pivot, 
17a0: 26 76 31 2c 20 26 76 32 2c 20 26 6d 65 72 67 65  &v1, &v2, &merge
17b0: 64 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62 5f 77  d);.  if( blob_w
17c0: 72 69 74 65 5f 74 6f 5f 66 69 6c 65 28 26 6d 65  rite_to_file(&me
17d0: 72 67 65 64 2c 20 67 2e 61 72 67 76 5b 35 5d 29  rged, g.argv[5])
17e0: 3c 62 6c 6f 62 5f 73 69 7a 65 28 26 6d 65 72 67  <blob_size(&merg
17f0: 65 64 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  ed) ){.    fprin
1800: 74 66 28 73 74 64 65 72 72 2c 22 63 61 6e 6e 6f  tf(stderr,"canno
1810: 74 20 77 72 69 74 65 20 25 73 5c 6e 22 2c 20 67  t write %s\n", g
1820: 2e 61 72 67 76 5b 34 5d 29 3b 0a 20 20 20 20 65  .argv[4]);.    e
1830: 78 69 74 28 31 29 3b 0a 20 20 7d 0a 20 20 62 6c  xit(1);.  }.  bl
1840: 6f 62 5f 72 65 73 65 74 28 26 70 69 76 6f 74 29  ob_reset(&pivot)
1850: 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26  ;.  blob_reset(&
1860: 76 31 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65  v1);.  blob_rese
1870: 74 28 26 76 32 29 3b 0a 20 20 62 6c 6f 62 5f 72  t(&v2);.  blob_r
1880: 65 73 65 74 28 26 6d 65 72 67 65 64 29 3b 0a 7d  eset(&merged);.}
1890: 0a                                               .