@ -44,6 +44,9 @@
package org.eclipse.jgit.dircache ;
package org.eclipse.jgit.dircache ;
import static org.eclipse.jgit.lib.FileMode.TYPE_MASK ;
import static org.eclipse.jgit.lib.FileMode.TYPE_TREE ;
import java.io.IOException ;
import java.io.IOException ;
import java.text.MessageFormat ;
import java.text.MessageFormat ;
import java.util.Arrays ;
import java.util.Arrays ;
@ -51,9 +54,7 @@ import java.util.Arrays;
import org.eclipse.jgit.internal.JGitText ;
import org.eclipse.jgit.internal.JGitText ;
import org.eclipse.jgit.lib.AnyObjectId ;
import org.eclipse.jgit.lib.AnyObjectId ;
import org.eclipse.jgit.lib.ObjectReader ;
import org.eclipse.jgit.lib.ObjectReader ;
import org.eclipse.jgit.treewalk.AbstractTreeIterator ;
import org.eclipse.jgit.treewalk.CanonicalTreeParser ;
import org.eclipse.jgit.treewalk.CanonicalTreeParser ;
import org.eclipse.jgit.treewalk.TreeWalk ;
/ * *
/ * *
* Updates a { @link DirCache } by adding individual { @link DirCacheEntry } s .
* Updates a { @link DirCache } by adding individual { @link DirCacheEntry } s .
@ -163,27 +164,56 @@ public class DirCacheBuilder extends BaseDirCacheEditor {
* @throws IOException
* @throws IOException
* a tree cannot be read to iterate through its entries .
* a tree cannot be read to iterate through its entries .
* /
* /
public void addTree ( final byte [ ] pathPrefix , final int stage ,
public void addTree ( byte [ ] pathPrefix , int stage , ObjectReader reader ,
final ObjectReader reader , final AnyObjectId tree ) throws IOException {
AnyObjectId tree ) throws IOException {
final TreeWalk tw = new TreeWalk ( reader ) ;
CanonicalTreeParser p = createTreeParser ( pathPrefix , reader , tree ) ;
tw . addTree ( new CanonicalTreeParser ( pathPrefix , reader , tree
while ( ! p . eof ( ) ) {
. toObjectId ( ) ) ) ;
if ( isTree ( p ) ) {
tw . setRecursive ( true ) ;
p = enterTree ( p , reader ) ;
if ( tw . next ( ) ) {
continue ;
final DirCacheEntry newEntry = toEntry ( stage , tw ) ;
}
beforeAdd ( newEntry ) ;
fastAdd ( newEntry ) ;
DirCacheEntry first = toEntry ( stage , p ) ;
while ( tw . next ( ) )
beforeAdd ( first ) ;
fastAdd ( toEntry ( stage , tw ) ) ;
fastAdd ( first ) ;
p = p . next ( ) ;
break ;
}
// Rest of tree entries are correctly sorted; use fastAdd().
while ( ! p . eof ( ) ) {
if ( isTree ( p ) ) {
p = enterTree ( p , reader ) ;
} else {
fastAdd ( toEntry ( stage , p ) ) ;
p = p . next ( ) ;
}
}
}
}
}
private DirCacheEntry toEntry ( final int stage , final TreeWalk tw ) {
private static CanonicalTreeParser createTreeParser ( byte [ ] pathPrefix ,
final DirCacheEntry e = new DirCacheEntry ( tw . getRawPath ( ) , stage ) ;
ObjectReader reader , AnyObjectId tree ) throws IOException {
final AbstractTreeIterator i ;
return new CanonicalTreeParser ( pathPrefix , reader , tree ) ;
}
private static boolean isTree ( CanonicalTreeParser p ) {
return ( p . getEntryRawMode ( ) & TYPE_MASK ) = = TYPE_TREE ;
}
private static CanonicalTreeParser enterTree ( CanonicalTreeParser p ,
ObjectReader reader ) throws IOException {
p = p . createSubtreeIterator ( reader ) ;
return p . eof ( ) ? p . next ( ) : p ;
}
private static DirCacheEntry toEntry ( int stage , CanonicalTreeParser i ) {
byte [ ] buf = i . getEntryPathBuffer ( ) ;
int len = i . getEntryPathLength ( ) ;
byte [ ] path = new byte [ len ] ;
System . arraycopy ( buf , 0 , path , 0 , len ) ;
i = tw . getTree ( 0 , AbstractTreeIterator . class ) ;
DirCacheEntry e = new DirCacheEntry ( path , stage ) ;
e . setFileMode ( tw . getFileMode ( 0 ) ) ;
e . setFileMode ( i . getEntryRawMode ( ) ) ;
e . setObjectIdFromRaw ( i . idBuffer ( ) , i . idOffset ( ) ) ;
e . setObjectIdFromRaw ( i . idBuffer ( ) , i . idOffset ( ) ) ;
return e ;
return e ;
}
}