@ -43,6 +43,9 @@
package org.eclipse.jgit.storage.file ;
package org.eclipse.jgit.storage.file ;
import static org.eclipse.jgit.storage.pack.PackExt.INDEX ;
import static org.eclipse.jgit.storage.pack.PackExt.PACK ;
import java.io.BufferedReader ;
import java.io.BufferedReader ;
import java.io.File ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileInputStream ;
@ -74,6 +77,7 @@ import org.eclipse.jgit.lib.RepositoryCache;
import org.eclipse.jgit.lib.RepositoryCache.FileKey ;
import org.eclipse.jgit.lib.RepositoryCache.FileKey ;
import org.eclipse.jgit.storage.pack.CachedPack ;
import org.eclipse.jgit.storage.pack.CachedPack ;
import org.eclipse.jgit.storage.pack.ObjectToPack ;
import org.eclipse.jgit.storage.pack.ObjectToPack ;
import org.eclipse.jgit.storage.pack.PackExt ;
import org.eclipse.jgit.storage.pack.PackWriter ;
import org.eclipse.jgit.storage.pack.PackWriter ;
import org.eclipse.jgit.util.FS ;
import org.eclipse.jgit.util.FS ;
import org.eclipse.jgit.util.FileUtils ;
import org.eclipse.jgit.util.FileUtils ;
@ -335,10 +339,23 @@ public class ObjectDirectory extends FileObjectDatabase {
public PackFile openPack ( final File pack )
public PackFile openPack ( final File pack )
throws IOException {
throws IOException {
final String p = pack . getName ( ) ;
final String p = pack . getName ( ) ;
if ( p . length ( ) ! = 50 | | ! p . startsWith ( "pack-" ) | | ! p . endsWith ( ".pack" ) ) //$NON-NLS-1$
if ( p . length ( ) ! = 50 | | ! p . startsWith ( "pack-" ) | | ! p . endsWith ( ".pack" ) ) //$NON-NLS-1$ //$NON-NLS-2$
throw new IOException ( MessageFormat . format ( JGitText . get ( ) . notAValidPack , pack ) ) ;
throw new IOException ( MessageFormat . format ( JGitText . get ( ) . notAValidPack , pack ) ) ;
PackFile res = new PackFile ( pack ) ;
// The pack and index are assumed to exist. The existence of other
// extensions needs to be explicitly checked.
//
int extensions = PACK . getBit ( ) | INDEX . getBit ( ) ;
final String base = p . substring ( 0 , p . length ( ) - 4 ) ;
for ( PackExt ext : PackExt . values ( ) ) {
if ( ( extensions & ext . getBit ( ) ) = = 0 ) {
final String name = base + ext . getExtension ( ) ;
if ( new File ( pack . getParentFile ( ) , name ) . exists ( ) )
extensions | = ext . getBit ( ) ;
}
}
PackFile res = new PackFile ( pack , extensions ) ;
insertPack ( res ) ;
insertPack ( res ) ;
return res ;
return res ;
}
}
@ -720,9 +737,14 @@ public class ObjectDirectory extends FileObjectDatabase {
if ( indexName . length ( ) ! = 49 | | ! indexName . endsWith ( ".idx" ) ) //$NON-NLS-1$
if ( indexName . length ( ) ! = 49 | | ! indexName . endsWith ( ".idx" ) ) //$NON-NLS-1$
continue ;
continue ;
final String base = indexName . substring ( 0 , indexName . length ( ) - 4 ) ;
final String base = indexName . substring ( 0 , indexName . length ( ) - 3 ) ;
final String packName = base + ".pack" ; //$NON-NLS-1$
int extensions = 0 ;
if ( ! names . contains ( packName ) ) {
for ( PackExt ext : PackExt . values ( ) ) {
if ( names . contains ( base + ext . getExtension ( ) ) )
extensions | = ext . getBit ( ) ;
}
if ( ( extensions & PACK . getBit ( ) ) = = 0 ) {
// Sometimes C Git's HTTP fetch transport leaves a
// Sometimes C Git's HTTP fetch transport leaves a
// .idx file behind and does not download the .pack.
// .idx file behind and does not download the .pack.
// We have to skip over such useless indexes.
// We have to skip over such useless indexes.
@ -730,6 +752,7 @@ public class ObjectDirectory extends FileObjectDatabase {
continue ;
continue ;
}
}
final String packName = base + PACK . getExtension ( ) ;
final PackFile oldPack = forReuse . remove ( packName ) ;
final PackFile oldPack = forReuse . remove ( packName ) ;
if ( oldPack ! = null ) {
if ( oldPack ! = null ) {
list . add ( oldPack ) ;
list . add ( oldPack ) ;
@ -737,7 +760,7 @@ public class ObjectDirectory extends FileObjectDatabase {
}
}
final File packFile = new File ( packDirectory , packName ) ;
final File packFile = new File ( packDirectory , packName ) ;
list . add ( new PackFile ( packFile ) ) ;
list . add ( new PackFile ( packFile , extensions ) ) ;
foundNew = true ;
foundNew = true ;
}
}