@ -47,16 +47,22 @@ package org.eclipse.jgit.merge;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertFalse ;
import static org.junit.Assert.assertTrue ;
import static org.junit.Assert.fail ;
import java.io.IOException ;
import java.util.Arrays ;
import java.util.List ;
import org.eclipse.jgit.dircache.DirCache ;
import org.eclipse.jgit.dircache.DirCacheBuilder ;
import org.eclipse.jgit.lib.AnyObjectId ;
import org.eclipse.jgit.lib.CommitBuilder ;
import org.eclipse.jgit.lib.FileMode ;
import org.eclipse.jgit.lib.ObjectId ;
import org.eclipse.jgit.lib.ObjectInserter ;
import org.eclipse.jgit.lib.PersonIdent ;
import org.eclipse.jgit.revwalk.RevCommit ;
import org.eclipse.jgit.revwalk.RevWalk ;
import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase ;
import org.eclipse.jgit.treewalk.TreeWalk ;
import org.junit.Test ;
@ -87,6 +93,81 @@ public class SimpleMergeTest extends SampleDataRepositoryTestCase {
assertEquals ( "02ba32d3649e510002c21651936b7077aa75ffa9" , ourMerger . getResultTreeId ( ) . name ( ) ) ;
}
@Test
public void testDuplicateParents ( ) throws Exception {
ObjectId commitId ;
RevCommit newCommit ;
final ObjectInserter ow = db . newObjectInserter ( ) ;
RevWalk rw = new RevWalk ( db ) ;
ObjectId parentA = db . resolve ( "a" ) ;
ObjectId parentB = db . resolve ( "b" ) ;
ObjectId [ ] parentIds_AA = new ObjectId [ ] { parentA , parentA } ;
ObjectId [ ] parentIds_AB = new ObjectId [ ] { parentA , parentB } ;
ObjectId [ ] parentIds_BA = new ObjectId [ ] { parentB , parentA } ;
ObjectId [ ] parentIds_BBAB = new ObjectId [ ] { parentB , parentB , parentA ,
parentB } ;
try {
commitId = commit ( ow , db . readDirCache ( ) , parentA , parentA ) ;
fail ( "an expected exception did not occur" ) ;
} catch ( IllegalArgumentException e ) {
//
}
commitId = commit ( ow , db . readDirCache ( ) , parentA , parentB ) ;
newCommit = rw . parseCommit ( commitId ) ;
assertEquals ( 2 , newCommit . getParentCount ( ) ) ;
commitId = commit ( ow , db . readDirCache ( ) , parentB , parentA ) ;
newCommit = rw . parseCommit ( commitId ) ;
assertEquals ( 2 , newCommit . getParentCount ( ) ) ;
try {
commitId = commit ( ow , db . readDirCache ( ) , parentIds_AA ) ;
fail ( "an expected exception did not occur" ) ;
} catch ( IllegalArgumentException e ) {
//
}
commitId = commit ( ow , db . readDirCache ( ) , parentIds_AB ) ;
newCommit = rw . parseCommit ( commitId ) ;
assertEquals ( 2 , newCommit . getParentCount ( ) ) ;
commitId = commit ( ow , db . readDirCache ( ) , parentIds_BA ) ;
newCommit = rw . parseCommit ( commitId ) ;
assertEquals ( 2 , newCommit . getParentCount ( ) ) ;
try {
commitId = commit ( ow , db . readDirCache ( ) , parentIds_BBAB ) ;
fail ( "an expected exception did not occur" ) ;
} catch ( IllegalArgumentException e ) {
//
}
try {
commitId = commit ( ow , db . readDirCache ( ) ,
Arrays . asList ( parentIds_AA ) ) ;
fail ( "an expected exception did not occur" ) ;
} catch ( IllegalArgumentException e ) {
//
}
commitId = commit ( ow , db . readDirCache ( ) , parentIds_AB ) ;
newCommit = rw . parseCommit ( commitId ) ;
assertEquals ( 2 , newCommit . getParentCount ( ) ) ;
commitId = commit ( ow , db . readDirCache ( ) , parentIds_BA ) ;
newCommit = rw . parseCommit ( commitId ) ;
assertEquals ( 2 , newCommit . getParentCount ( ) ) ;
try {
commitId = commit ( ow , db . readDirCache ( ) , parentIds_BBAB ) ;
fail ( "an expected exception did not occur" ) ;
} catch ( IllegalArgumentException e ) {
//
}
}
@Test
public void testTrivialTwoWay_disjointhistories ( ) throws IOException {
Merger ourMerger = MergeStrategy . SIMPLE_TWO_WAY_IN_CORE . newMerger ( db ) ;
@ -391,4 +472,31 @@ public class SimpleMergeTest extends SampleDataRepositoryTestCase {
odi . flush ( ) ;
return id ;
}
private ObjectId commit ( final ObjectInserter odi , final DirCache treeB ,
final AnyObjectId parentId1 , final AnyObjectId parentId2 )
throws Exception {
final CommitBuilder c = new CommitBuilder ( ) ;
c . setTreeId ( treeB . writeTree ( odi ) ) ;
c . setAuthor ( new PersonIdent ( "A U Thor" , "a.u.thor" , 1L , 0 ) ) ;
c . setCommitter ( c . getAuthor ( ) ) ;
c . setParentIds ( parentId1 , parentId2 ) ;
c . setMessage ( "Tree " + c . getTreeId ( ) . name ( ) ) ;
ObjectId id = odi . insert ( c ) ;
odi . flush ( ) ;
return id ;
}
private ObjectId commit ( final ObjectInserter odi , final DirCache treeB ,
List < ObjectId > parents ) throws Exception {
final CommitBuilder c = new CommitBuilder ( ) ;
c . setTreeId ( treeB . writeTree ( odi ) ) ;
c . setAuthor ( new PersonIdent ( "A U Thor" , "a.u.thor" , 1L , 0 ) ) ;
c . setCommitter ( c . getAuthor ( ) ) ;
c . setParentIds ( parents ) ;
c . setMessage ( "Tree " + c . getTreeId ( ) . name ( ) ) ;
ObjectId id = odi . insert ( c ) ;
odi . flush ( ) ;
return id ;
}
}