@ -73,10 +73,10 @@ import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Commit ;
import org.eclipse.jgit.lib.Commit ;
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.FileRepository ;
import org.eclipse.jgit.lib.LockFile ;
import org.eclipse.jgit.lib.LockFile ;
import org.eclipse.jgit.lib.NullProgressMonitor ;
import org.eclipse.jgit.lib.NullProgressMonitor ;
import org.eclipse.jgit.lib.ObjectChecker ;
import org.eclipse.jgit.lib.ObjectChecker ;
import org.eclipse.jgit.lib.ObjectDatabase ;
import org.eclipse.jgit.lib.ObjectDirectory ;
import org.eclipse.jgit.lib.ObjectDirectory ;
import org.eclipse.jgit.lib.ObjectId ;
import org.eclipse.jgit.lib.ObjectId ;
import org.eclipse.jgit.lib.ObjectWriter ;
import org.eclipse.jgit.lib.ObjectWriter ;
@ -99,8 +99,13 @@ import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk ;
import org.eclipse.jgit.treewalk.TreeWalk ;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup ;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup ;
/** Wrapper to make creating test data easier. */
/ * *
public class TestRepository {
* Wrapper to make creating test data easier .
*
* @param < R >
* type of Repository the test data is stored on .
* /
public class TestRepository < R extends Repository > {
private static final PersonIdent author ;
private static final PersonIdent author ;
private static final PersonIdent committer ;
private static final PersonIdent committer ;
@ -119,7 +124,7 @@ public class TestRepository {
committer = new PersonIdent ( cn , ce , now , tz ) ;
committer = new PersonIdent ( cn , ce , now , tz ) ;
}
}
private final Repository db ;
private final R db ;
private final RevWalk pool ;
private final RevWalk pool ;
@ -132,9 +137,9 @@ public class TestRepository {
*
*
* @param db
* @param db
* the test repository to write into .
* the test repository to write into .
* @throws Exception
* @throws IO Exception
* /
* /
public TestRepository ( Repository db ) throws Exception {
public TestRepository ( R db ) throws IO Exception {
this ( db , new RevWalk ( db ) ) ;
this ( db , new RevWalk ( db ) ) ;
}
}
@ -145,9 +150,9 @@ public class TestRepository {
* the test repository to write into .
* the test repository to write into .
* @param rw
* @param rw
* the RevObject pool to use for object lookup .
* the RevObject pool to use for object lookup .
* @throws Exception
* @throws IO Exception
* /
* /
public TestRepository ( Repository db , RevWalk rw ) throws Exception {
public TestRepository ( R db , RevWalk rw ) throws IO Exception {
this . db = db ;
this . db = db ;
this . pool = rw ;
this . pool = rw ;
this . writer = new ObjectWriter ( db ) ;
this . writer = new ObjectWriter ( db ) ;
@ -155,7 +160,7 @@ public class TestRepository {
}
}
/** @return the repository this helper class operates against. */
/** @return the repository this helper class operates against. */
public Repository getRepository ( ) {
public R getRepository ( ) {
return db ;
return db ;
}
}
@ -443,25 +448,27 @@ public class TestRepository {
* @throws Exception
* @throws Exception
* /
* /
public void updateServerInfo ( ) throws Exception {
public void updateServerInfo ( ) throws Exception {
final ObjectDatabase odb = db . getObjectDatabase ( ) ;
if ( db instanceof FileRepository ) {
if ( odb instanceof ObjectDirectory ) {
final FileRepository fr = ( FileRepository ) db ;
RefWriter rw = new RefWriter ( db . getAllRefs ( ) . values ( ) ) {
RefWriter rw = new RefWriter ( fr . getAllRefs ( ) . values ( ) ) {
@Override
@Override
protected void writeFile ( final String name , final byte [ ] bin )
protected void writeFile ( final String name , final byte [ ] bin )
throws IOException {
throws IOException {
TestRepository . this . writeFile ( name , bin ) ;
File path = new File ( fr . getDirectory ( ) , name ) ;
TestRepository . this . writeFile ( path , bin ) ;
}
}
} ;
} ;
rw . writePackedRefs ( ) ;
rw . writePackedRefs ( ) ;
rw . writeInfoRefs ( ) ;
rw . writeInfoRefs ( ) ;
final StringBuilder w = new StringBuilder ( ) ;
final StringBuilder w = new StringBuilder ( ) ;
for ( PackFile p : ( ( ObjectDirectory ) odb ) . getPacks ( ) ) {
for ( PackFile p : fr . getObjectDatabase ( ) . getPacks ( ) ) {
w . append ( "P " ) ;
w . append ( "P " ) ;
w . append ( p . getPackFile ( ) . getName ( ) ) ;
w . append ( p . getPackFile ( ) . getName ( ) ) ;
w . append ( '\n' ) ;
w . append ( '\n' ) ;
}
}
writeFile ( "objects/info/packs" , Constants . encodeASCII ( w . toString ( ) ) ) ;
writeFile ( new File ( new File ( fr . getObjectDatabase ( ) . getDirectory ( ) ,
"info" ) , "packs" ) , Constants . encodeASCII ( w . toString ( ) ) ) ;
}
}
}
}
@ -563,38 +570,40 @@ public class TestRepository {
* @throws Exception
* @throws Exception
* /
* /
public void packAndPrune ( ) throws Exception {
public void packAndPrune ( ) throws Exception {
final ObjectDirectory odb = ( ObjectDirectory ) db . getObjectDatabase ( ) ;
if ( db . getObjectDatabase ( ) instanceof ObjectDirectory ) {
final PackWriter pw = new PackWriter ( db , NullProgressMonitor . INSTANCE ) ;
ObjectDirectory odb = ( ObjectDirectory ) db . getObjectDatabase ( ) ;
PackWriter pw = new PackWriter ( db , NullProgressMonitor . INSTANCE ) ;
Set < ObjectId > all = new HashSet < ObjectId > ( ) ;
for ( Ref r : db . getAllRefs ( ) . values ( ) )
all . add ( r . getObjectId ( ) ) ;
pw . preparePack ( all , Collections . < ObjectId > emptySet ( ) ) ;
final ObjectId name = pw . computeName ( ) ;
OutputStream out ;
final File pack = nameFor ( odb , name , ".pack" ) ;
Set < ObjectId > all = new HashSet < ObjectId > ( ) ;
out = new BufferedOutputStream ( new FileOutputStream ( pack ) ) ;
for ( Ref r : db . getAllRefs ( ) . values ( ) )
try {
all . add ( r . getObjectId ( ) ) ;
pw . writePack ( out ) ;
pw . preparePack ( all , Collections . < ObjectId > emptySet ( ) ) ;
} finally {
out . close ( ) ;
final ObjectId name = pw . computeName ( ) ;
}
OutputStream out ;
pack . setReadOnly ( ) ;
final File pack = nameFor ( odb , name , ".pack" ) ;
out = new BufferedOutputStream ( new FileOutputStream ( pack ) ) ;
try {
pw . writePack ( out ) ;
} finally {
out . close ( ) ;
}
pack . setReadOnly ( ) ;
final File idx = nameFor ( odb , name , ".idx" ) ;
out = new BufferedOutputStream ( new FileOutputStream ( idx ) ) ;
try {
pw . writeIndex ( out ) ;
} finally {
out . close ( ) ;
}
idx . setReadOnly ( ) ;
final File idx = nameFor ( odb , name , ".idx" ) ;
odb . openPack ( pack , idx ) ;
out = new BufferedOutputStream ( new FileOutputStream ( idx ) ) ;
updateServerInfo ( ) ;
try {
prunePacked ( odb ) ;
pw . writeIndex ( out ) ;
} finally {
out . close ( ) ;
}
}
idx . setReadOnly ( ) ;
odb . openPack ( pack , idx ) ;
updateServerInfo ( ) ;
prunePacked ( odb ) ;
}
}
private void prunePacked ( ObjectDirectory odb ) {
private void prunePacked ( ObjectDirectory odb ) {
@ -609,9 +618,8 @@ public class TestRepository {
return new File ( packdir , "pack-" + name . name ( ) + t ) ;
return new File ( packdir , "pack-" + name . name ( ) + t ) ;
}
}
private void writeFile ( final String name , final byte [ ] bin )
private void writeFile ( final File p , final byte [ ] bin ) throws IOException ,
throws IOException , ObjectWritingException {
ObjectWritingException {
final File p = new File ( db . getDirectory ( ) , name ) ;
final LockFile lck = new LockFile ( p ) ;
final LockFile lck = new LockFile ( p ) ;
if ( ! lck . lock ( ) )
if ( ! lck . lock ( ) )
throw new ObjectWritingException ( "Can't write " + p ) ;
throw new ObjectWritingException ( "Can't write " + p ) ;