@ -45,19 +45,26 @@ package org.eclipse.jgit.treewalk;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertFalse ;
import static org.junit.Assert.assertFalse ;
import static org.junit.Assert.assertNotNull ;
import static org.junit.Assert.assertTrue ;
import static org.junit.Assert.assertTrue ;
import java.io.File ;
import java.io.File ;
import java.security.MessageDigest ;
import java.security.MessageDigest ;
import org.eclipse.jgit.api.Git ;
import org.eclipse.jgit.api.Git ;
import org.eclipse.jgit.dircache.DirCache ;
import org.eclipse.jgit.dircache.DirCacheCheckout ;
import org.eclipse.jgit.dircache.DirCacheCheckout ;
import org.eclipse.jgit.dircache.DirCacheEditor ;
import org.eclipse.jgit.dircache.DirCacheEntry ;
import org.eclipse.jgit.dircache.DirCacheEntry ;
import org.eclipse.jgit.dircache.DirCacheIterator ;
import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit ;
import org.eclipse.jgit.lib.Constants ;
import org.eclipse.jgit.lib.Constants ;
import org.eclipse.jgit.lib.FileMode ;
import org.eclipse.jgit.lib.FileMode ;
import org.eclipse.jgit.lib.ObjectId ;
import org.eclipse.jgit.lib.ObjectId ;
import org.eclipse.jgit.lib.ObjectReader ;
import org.eclipse.jgit.lib.ObjectReader ;
import org.eclipse.jgit.lib.RepositoryTestCase ;
import org.eclipse.jgit.lib.RepositoryTestCase ;
import org.eclipse.jgit.revwalk.RevCommit ;
import org.eclipse.jgit.treewalk.filter.PathFilter ;
import org.eclipse.jgit.util.FileUtils ;
import org.eclipse.jgit.util.FileUtils ;
import org.eclipse.jgit.util.RawParseUtils ;
import org.eclipse.jgit.util.RawParseUtils ;
import org.junit.Before ;
import org.junit.Before ;
@ -208,6 +215,170 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
assertFalse ( fti . isModified ( dce , false ) ) ;
assertFalse ( fti . isModified ( dce , false ) ) ;
}
}
@Test
public void submoduleHeadMatchesIndex ( ) throws Exception {
Git git = new Git ( db ) ;
writeTrashFile ( "file.txt" , "content" ) ;
git . add ( ) . addFilepattern ( "file.txt" ) . call ( ) ;
final RevCommit id = git . commit ( ) . setMessage ( "create file" ) . call ( ) ;
final String path = "sub" ;
DirCache cache = db . lockDirCache ( ) ;
DirCacheEditor editor = cache . editor ( ) ;
editor . add ( new PathEdit ( path ) {
public void apply ( DirCacheEntry ent ) {
ent . setFileMode ( FileMode . GITLINK ) ;
ent . setObjectId ( id ) ;
}
} ) ;
editor . commit ( ) ;
Git . cloneRepository ( ) . setURI ( db . getDirectory ( ) . toURI ( ) . toString ( ) )
. setDirectory ( new File ( db . getWorkTree ( ) , path ) ) . call ( ) ;
TreeWalk walk = new TreeWalk ( db ) ;
DirCacheIterator indexIter = new DirCacheIterator ( db . readDirCache ( ) ) ;
FileTreeIterator workTreeIter = new FileTreeIterator ( db ) ;
walk . addTree ( indexIter ) ;
walk . addTree ( workTreeIter ) ;
walk . setFilter ( PathFilter . create ( path ) ) ;
assertTrue ( walk . next ( ) ) ;
assertTrue ( indexIter . idEqual ( workTreeIter ) ) ;
}
@Test
public void submoduleWithNoGitDirectory ( ) throws Exception {
Git git = new Git ( db ) ;
writeTrashFile ( "file.txt" , "content" ) ;
git . add ( ) . addFilepattern ( "file.txt" ) . call ( ) ;
final RevCommit id = git . commit ( ) . setMessage ( "create file" ) . call ( ) ;
final String path = "sub" ;
DirCache cache = db . lockDirCache ( ) ;
DirCacheEditor editor = cache . editor ( ) ;
editor . add ( new PathEdit ( path ) {
public void apply ( DirCacheEntry ent ) {
ent . setFileMode ( FileMode . GITLINK ) ;
ent . setObjectId ( id ) ;
}
} ) ;
editor . commit ( ) ;
File submoduleRoot = new File ( db . getWorkTree ( ) , path ) ;
assertTrue ( submoduleRoot . mkdir ( ) ) ;
assertTrue ( new File ( submoduleRoot , Constants . DOT_GIT ) . mkdir ( ) ) ;
TreeWalk walk = new TreeWalk ( db ) ;
DirCacheIterator indexIter = new DirCacheIterator ( db . readDirCache ( ) ) ;
FileTreeIterator workTreeIter = new FileTreeIterator ( db ) ;
walk . addTree ( indexIter ) ;
walk . addTree ( workTreeIter ) ;
walk . setFilter ( PathFilter . create ( path ) ) ;
assertTrue ( walk . next ( ) ) ;
assertFalse ( indexIter . idEqual ( workTreeIter ) ) ;
assertEquals ( ObjectId . zeroId ( ) , workTreeIter . getEntryObjectId ( ) ) ;
}
@Test
public void submoduleWithNoHead ( ) throws Exception {
Git git = new Git ( db ) ;
writeTrashFile ( "file.txt" , "content" ) ;
git . add ( ) . addFilepattern ( "file.txt" ) . call ( ) ;
final RevCommit id = git . commit ( ) . setMessage ( "create file" ) . call ( ) ;
final String path = "sub" ;
DirCache cache = db . lockDirCache ( ) ;
DirCacheEditor editor = cache . editor ( ) ;
editor . add ( new PathEdit ( path ) {
public void apply ( DirCacheEntry ent ) {
ent . setFileMode ( FileMode . GITLINK ) ;
ent . setObjectId ( id ) ;
}
} ) ;
editor . commit ( ) ;
assertNotNull ( Git . init ( ) . setDirectory ( new File ( db . getWorkTree ( ) , path ) )
. call ( ) . getRepository ( ) ) ;
TreeWalk walk = new TreeWalk ( db ) ;
DirCacheIterator indexIter = new DirCacheIterator ( db . readDirCache ( ) ) ;
FileTreeIterator workTreeIter = new FileTreeIterator ( db ) ;
walk . addTree ( indexIter ) ;
walk . addTree ( workTreeIter ) ;
walk . setFilter ( PathFilter . create ( path ) ) ;
assertTrue ( walk . next ( ) ) ;
assertFalse ( indexIter . idEqual ( workTreeIter ) ) ;
assertEquals ( ObjectId . zeroId ( ) , workTreeIter . getEntryObjectId ( ) ) ;
}
@Test
public void submoduleDirectoryIterator ( ) throws Exception {
Git git = new Git ( db ) ;
writeTrashFile ( "file.txt" , "content" ) ;
git . add ( ) . addFilepattern ( "file.txt" ) . call ( ) ;
final RevCommit id = git . commit ( ) . setMessage ( "create file" ) . call ( ) ;
final String path = "sub" ;
DirCache cache = db . lockDirCache ( ) ;
DirCacheEditor editor = cache . editor ( ) ;
editor . add ( new PathEdit ( path ) {
public void apply ( DirCacheEntry ent ) {
ent . setFileMode ( FileMode . GITLINK ) ;
ent . setObjectId ( id ) ;
}
} ) ;
editor . commit ( ) ;
Git . cloneRepository ( ) . setURI ( db . getDirectory ( ) . toURI ( ) . toString ( ) )
. setDirectory ( new File ( db . getWorkTree ( ) , path ) ) . call ( ) ;
TreeWalk walk = new TreeWalk ( db ) ;
DirCacheIterator indexIter = new DirCacheIterator ( db . readDirCache ( ) ) ;
FileTreeIterator workTreeIter = new FileTreeIterator ( db . getWorkTree ( ) ,
db . getFS ( ) , db . getConfig ( ) . get ( WorkingTreeOptions . KEY ) ) ;
walk . addTree ( indexIter ) ;
walk . addTree ( workTreeIter ) ;
walk . setFilter ( PathFilter . create ( path ) ) ;
assertTrue ( walk . next ( ) ) ;
assertTrue ( indexIter . idEqual ( workTreeIter ) ) ;
}
@Test
public void submoduleNestedWithHeadMatchingIndex ( ) throws Exception {
Git git = new Git ( db ) ;
writeTrashFile ( "file.txt" , "content" ) ;
git . add ( ) . addFilepattern ( "file.txt" ) . call ( ) ;
final RevCommit id = git . commit ( ) . setMessage ( "create file" ) . call ( ) ;
final String path = "sub/dir1/dir2" ;
DirCache cache = db . lockDirCache ( ) ;
DirCacheEditor editor = cache . editor ( ) ;
editor . add ( new PathEdit ( path ) {
public void apply ( DirCacheEntry ent ) {
ent . setFileMode ( FileMode . GITLINK ) ;
ent . setObjectId ( id ) ;
}
} ) ;
editor . commit ( ) ;
Git . cloneRepository ( ) . setURI ( db . getDirectory ( ) . toURI ( ) . toString ( ) )
. setDirectory ( new File ( db . getWorkTree ( ) , path ) ) . call ( ) ;
TreeWalk walk = new TreeWalk ( db ) ;
DirCacheIterator indexIter = new DirCacheIterator ( db . readDirCache ( ) ) ;
FileTreeIterator workTreeIter = new FileTreeIterator ( db ) ;
walk . addTree ( indexIter ) ;
walk . addTree ( workTreeIter ) ;
walk . setFilter ( PathFilter . create ( path ) ) ;
assertTrue ( walk . next ( ) ) ;
assertTrue ( indexIter . idEqual ( workTreeIter ) ) ;
}
private static String nameOf ( final AbstractTreeIterator i ) {
private static String nameOf ( final AbstractTreeIterator i ) {
return RawParseUtils . decode ( Constants . CHARSET , i . path , 0 , i . pathLen ) ;
return RawParseUtils . decode ( Constants . CHARSET , i . path , 0 , i . pathLen ) ;
}
}