The enclosed two messages describe a bug in GNU diff 2.6 and 2.7 which
may cause CVS to perform an erroneous merge.  You may wish to use GNU
diff 2.5 or apply the patch supplied by Loren James Rittle below.  It
would be nice to add this to the CVS testsuite, but I haven't done so
because probably a lot of people who would like to run the testsuite
are using the buggy diff.

From: friedman@splode.com (Noah Friedman)
To: bug-gnu-utils@prep.ai.mit.edu
Cc: info-cvs@prep.ai.mit.edu
Subject: diffutils 2.7 -- diff3 merge bug
Date: Tue, 29 Oct 96 17:02:54 CST

I believe a change first introduced in GNU diff 2.6 causes diff3 sometimes
to produce incorrect merges.

Since this is a not a bug in CVS itself but can cause commits to CVS
repositories to be incorrect, am warning info-cvs@prep.ai.mit.edu as
well as reporting the bug to bug-gnu-utils@prep.ai.mit.edu.

I am including a simple test case as well as some sample outputs from
different versions of `diff' and `diff3' in the enclosed shar archive.
In addition, the file DESCRIPTION in that archive describes the problem
more fully.

If anyone has any advice for how to fix or to work around this bug, I would
appreciate it.  Using diff 2.5 seems like the most immediately obvious
solution, but I don't know if it will introduce other problems.
I do not understand the algorithms used by GNU diff well enough to suggest
any patches.

Unshar and enjoy.  ;-)

begin 666 merge-testcase.shar.gz
M'XL(",&,=C("`VUE<F=E+71E<W1C87-E+G-H87(`[5Q[;]M&$O_[^"FF;E#9
M@41)?C!GYX&V;MPS<$F+V+FZP.&:%;F26)-<E;NTHC[NL]_,+%^29;USUP-L
M&)9$[CQW]C?#W;$^_ZS="Y.V'CJ?P_4PU("_`O101A&(U!^&=Q+V1ZD*,E\&
MT)O`MV_?XVV19B:,-!R[W0.72!7(CR85O@$SE-`/(ZFAGZH8/Q)'RZD)6B"_
M$`<IT"J6<''Y]]=-2&6L[B2RD7<RG2!%,H">[*M4,K</A8X-B,)$@N@IXH6W
M$C"3$0[00^;40%60RQL12%`)=$]/O5:WTSH\A:YW=G(,YU?79,*+?AK*(!;)
MEWH4J4"ZOHI?D157*DM]"4&82M^H=`)CH>%#.]-I.U*^B-HZ]=N#)&L'8;_/
M#FC',A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Y%QZ8*(IEM:N)6F:LPX3:CX@M>J$52D!^(/W69AD;+J,^-CSXF
M(]NL-]V)ZBSN1$4T&HN4^QD?FAYJPBR:3XE96C;`DR;$<86.U)B$1EE@TX/&
M+![)6AMFK;^5VRJUX`&V.]1^)XU3]D^536T$9!^XD913R0?;N%\T86&BBA1%
M/W\92_YE-B[U'XD@"*F>:%8=4/66&J87IOPFG4!J/PU[>0-4WD[KQ(2D_2RR
MS7N7U&DY48GDUD-\BT+NJ$F,LN-0C<E/_?`C=0_CN[%*;Y$]I40[O=Q3>8G7
MLRAPL.+!R:%J!2<8,^-[;;_+P79VX8*6,07CK:TI8D6A%\<R(`JL.52/&R\=
MK:+,6DGQ<0F!2AH&;A-4!D$M-+:7K0S:O$<N-P_CCR@XY%!+=+@IFAE%-,!@
M,D-4(M/5UPNQ?CR),E'98,C?%I0-,)2-0_X8V28R<M;[Q$X)M9@E/ZL)VOB\
M=;`\8YX<S<F8=<A:F"'+@2MEQ`)(U\R`A9`JX]GO5UDKY>5,9E,<<5J2XS!E
-&.@X_P&I*Y$W($H``"'+
`
end

Date: Wed, 30 Oct 96 14:54:13 CST
From: Loren James Rittle <rittle@comm.mot.com>
To: friedman@splode.com
Cc: bug-gnu-utils@prep.ai.mit.edu, info-cvs@prep.ai.mit.edu
Subject: Re: diffutils 2.7 -- diff3 merge bug

Noah,

I have seen the problem you discuss in your e-mail, however I fail to
see how this situation is as critical as might be implied since it can
never arise without at least some user involvement (an update that
caused a merge is never automatically followed by a commit --- the
user has a chance to inspect the merged file).  However, I will agree
that I don't always look very closely at non-conflicted merges before
checking them back in.

You didn't give the exact CVS commands used to create a lossage,
but I added the following to your Makefile, to help me see the problem
in a CVS usage context:

t-older: testcase-older
	cp testcase-older t-older

t-yours: testcase-yours
	cp testcase-yours t-yours

t-mine: testcase-mine
	cp testcase-mine t-mine

# Assume cvs-1.9
cvs-test: t-older t-yours t-mine
	rm -rf /tmp/cvs-test-root x x2
	cvs -d /tmp/cvs-test-root init
	mkdir x
	cp t-older x/testcase
	cd x; cvs -d /tmp/cvs-test-root import -m '' x X X1
	rm -rf x
	cvs -d /tmp/cvs-test-root co x
	cvs -d /tmp/cvs-test-root co -d x2 x
	cp t-yours x/testcase
	cp t-mine x2/testcase
	cd x; cvs ci -m ''
	-cd x2; cvs ci -m ''
	cd x2; cvs update
	cat x2/testcase # at this point, user may commit blindly

It looks like whomever added shift_boundaries() in analyze.c, which
seems to be the source of the diff3 induced mischief, already provided
a means to disable the boundary shifting optimization (at least with
a recompile).

Here is the patch I applied to diff to disable this (currently)
overaggressive optimization:

[ rittle@supra ]; diff -c analyze.c-old analyze.c    
*** analyze.c-old       Wed Oct 30 14:10:27 1996
--- analyze.c   Wed Oct 30 13:48:57 1996
***************
*** 616,622 ****
     but usually it is cleaner to consider the following identical line
     to be the "change".  */
  
! int inhibit;
  
  static void
  shift_boundaries (filevec)
--- 616,622 ----
     but usually it is cleaner to consider the following identical line
     to be the "change".  */
  
! int inhibit = 1;
  
  static void
  shift_boundaries (filevec)

Now, diff-2.7 with the above patch produces:

[ rittle@supra ]; make diff-mine-yours 'DIFF=/usr/src/diffutils-2.7/diff'
/usr/src/diffutils-2.7/diff -a --horizon-lines=11 -- testcase-mine testcase-yours; true
16,18c16,18
<     // _titleColor = Color.black;
<     // _disabledTitleColor = Color.gray;
<     // _titleFont = Font.defaultFont ();
---
>     _titleColor = Color.black;
>     _disabledTitleColor = Color.gray;
>     _titleFont = Font.defaultFont ();
20,30d19
< 
<   /* Convenience constructor for instantiating a Button with
<    * bounds x, y, width, and height.  Equivalent to
<    *     foo = new Button ();
<    *     foo.init (x, y, width, height);
<    */
<   public Button (int x, int y, int width, int height)
<   {
<     this ();
<     init (x, y, width, height);
<   }

Whereas, stock diff-2.7 produces:

[ rittle@supra ]; make diff-mine-yours
diff -a --horizon-lines=11 -- testcase-mine testcase-yours; true
16,29c16,18
<     // _titleColor = Color.black;
<     // _disabledTitleColor = Color.gray;
<     // _titleFont = Font.defaultFont ();
<   }
< 
<   /* Convenience constructor for instantiating a Button with
<    * bounds x, y, width, and height.  Equivalent to
<    *     foo = new Button ();
<    *     foo.init (x, y, width, height);
<    */
<   public Button (int x, int y, int width, int height)
<   {
<     this ();
<     init (x, y, width, height);
---
>     _titleColor = Color.black;
>     _disabledTitleColor = Color.gray;
>     _titleFont = Font.defaultFont ();

A better solution might be to disable the boundary shifting code
unless explicitly turned on via command line argument.  That way
programs, like diff3, expecting unoptimized diff regions will work
correctly, yet users can get smaller diffs, if desired.  The problem
is that diff3 doesn't properly track changes once they have been
optimized.

BTW, I never did like the look of the `optimized diff regions', so I
consider this a good change for other reasons... :-)

Enjoy!

Regards,
Loren
-- 
Loren J. Rittle (rittle@comm.mot.com)	PGP KeyIDs: 1024/B98B3249 2048/ADCE34A5
Systems Technology Research (IL02/2240)	FP1024:6810D8AB3029874DD7065BC52067EAFD
Motorola, Inc.				FP2048:FDC0292446937F2A240BC07D42763672
(847) 576-7794				Call for verification of fingerprints.


From: Paul Eggert <eggert@shade.twinsun.com>
Date: 10 Nov 1996 10:07:15 -0800
To: friedman@splode.com (Noah Friedman)
CC: Loren James Rittle <rittle@comm.mot.com>
Subject: Re: diffutils 2.7 -- diff3 merge bug

I'm thinking of modifying diff's shift_boundaries function along the
following lines to work around the problem.  (I'll actually add another
option instead of the horizon_lines!=context hack in the following
code, but it's hard for me to send you the true patch since there are
so many other changes.)  Please give this a try and see what you think.

*** diffutils-2.7/analyze.c	Wed Nov 10 00:28:27 1993
--- diffutils-test/analyze.c	Sun Nov 10 10:00:31 1996
***************
*** 623,631 ****
       struct file_data filevec[];
  {
    int f;
! 
!   if (inhibit)
!     return;
  
    for (f = 0; f < 2; f++)
      {
--- 623,629 ----
       struct file_data filevec[];
  {
    int f;
!   int inhibit_hunk_merge = horizon_lines != context;
  
    for (f = 0; f < 2; f++)
      {
***************
*** 668,685 ****
  		 we can later determine whether the run has grown.  */
  	      runlength = i - start;
  
! 	      /* Move the changed region back, so long as the
! 		 previous unchanged line matches the last changed one.
! 		 This merges with previous changed regions.  */
! 
! 	      while (start && equivs[start - 1] == equivs[i - 1])
  		{
! 		  changed[--start] = 1;
! 		  changed[--i] = 0;
! 		  while (changed[start - 1])
! 		    start--;
! 		  while (other_changed[--j])
! 		    continue;
  		}
  
  	      /* Set CORRESPONDING to the end of the changed run, at the last
--- 666,686 ----
  		 we can later determine whether the run has grown.  */
  	      runlength = i - start;
  
! 	      if (! inhibit_hunk_merge)
  		{
! 		  /* Move the changed region back, so long as the
! 		     previous unchanged line matches the last changed one.
! 		     This merges with previous changed regions.  */
! 
! 		  while (start && equivs[start - 1] == equivs[i - 1])
! 		    {
! 		      changed[--start] = 1;
! 		      changed[--i] = 0;
! 		      while (changed[start - 1])
! 			start--;
! 		      while (other_changed[--j])
! 			continue;
! 		    }
  		}
  
  	      /* Set CORRESPONDING to the end of the changed run, at the last
***************
*** 687,699 ****
  		 CORRESPONDING == I_END means no such point has been found.  */
  	      corresponding = other_changed[j - 1] ? i : i_end;
  
! 	      /* Move the changed region forward, so long as the
! 		 first changed line matches the following unchanged one.
! 		 This merges with following changed regions.
  		 Do this second, so that if there are no merges,
  		 the changed region is moved forward as far as possible.  */
  
! 	      while (i != i_end && equivs[start] == equivs[i])
  		{
  		  changed[start++] = 0;
  		  changed[i++] = 1;
--- 688,702 ----
  		 CORRESPONDING == I_END means no such point has been found.  */
  	      corresponding = other_changed[j - 1] ? i : i_end;
  
! 	      /* Shift the changed region forward, so long as the
! 		 first changed line matches the following unchanged one,
! 		 but if INHIBIT_HUNK_MERGE is 1 do not shift if
! 		 this would merge with another changed region.
  		 Do this second, so that if there are no merges,
  		 the changed region is moved forward as far as possible.  */
  
! 	      while (i != i_end && equivs[start] == equivs[i]
! 		     && ! (inhibit_hunk_merge & other_changed[j + 1]))
  		{
  		  changed[start++] = 0;
  		  changed[i++] = 1;

Date: Fri, 5 Sep 1997 11:37:06 -0700
From: Paul Eggert <eggert@twinsun.com>
To: kingdon@cyclic.com
CC: bug-cvs@prep.ai.mit.edu
Subject: Re: proposed patch to ccvs/doc/DIFFUTILS-2.7-BUG

. . .

The ``other option'' is a new option that diff3 will pass to diff;
ordinary users probably use it.  In this respect, it's similar to the
--horizon-lines=NUM option of diffutils 2.7.

The new option will control the inhibit_hunk_merge variable that is
mentioned in the code that I sent.  The basic idea is to have `diff'
optionally avoid merging two hunks when shifting hunk boundaries.
Normally merging is a win, since it makes the diff output smaller;
but for diff3's purpose merging can be a loss.

