@ -78,16 +78,13 @@ import java.util.Objects;
import java.util.Set ;
import java.util.Set ;
import java.util.TreeMap ;
import java.util.TreeMap ;
import java.util.concurrent.Callable ;
import java.util.concurrent.Callable ;
import java.util.concurrent.ExecutionException ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
import java.util.concurrent.Executors ;
import java.util.concurrent.Future ;
import java.util.regex.Pattern ;
import java.util.regex.Pattern ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
import java.util.stream.Stream ;
import java.util.stream.Stream ;
import org.eclipse.jgit.annotations.NonNull ;
import org.eclipse.jgit.annotations.NonNull ;
import org.eclipse.jgit.api.errors.JGitInternalException ;
import org.eclipse.jgit.dircache.DirCacheIterator ;
import org.eclipse.jgit.dircache.DirCacheIterator ;
import org.eclipse.jgit.errors.CancelledException ;
import org.eclipse.jgit.errors.CancelledException ;
import org.eclipse.jgit.errors.CorruptObjectException ;
import org.eclipse.jgit.errors.CorruptObjectException ;
@ -232,8 +229,11 @@ public class GC {
* /
* /
// TODO(ms): in 5.0 change signature and return Future<Collection<PackFile>>
// TODO(ms): in 5.0 change signature and return Future<Collection<PackFile>>
public Collection < PackFile > gc ( ) throws IOException , ParseException {
public Collection < PackFile > gc ( ) throws IOException , ParseException {
final GcLog gcLog = background ? new GcLog ( repo ) : null ;
if ( ! background ) {
if ( gcLog ! = null & & ! gcLog . lock ( background ) ) {
return doGc ( ) ;
}
final GcLog gcLog = new GcLog ( repo ) ;
if ( ! gcLog . lock ( ) ) {
// there is already a background gc running
// there is already a background gc running
return Collections . emptyList ( ) ;
return Collections . emptyList ( ) ;
}
}
@ -241,48 +241,31 @@ public class GC {
Callable < Collection < PackFile > > gcTask = ( ) - > {
Callable < Collection < PackFile > > gcTask = ( ) - > {
try {
try {
Collection < PackFile > newPacks = doGc ( ) ;
Collection < PackFile > newPacks = doGc ( ) ;
if ( automatic & & tooManyLooseObjects ( ) & & gcLog ! = null ) {
if ( automatic & & tooManyLooseObjects ( ) ) {
String message = JGitText . get ( ) . gcTooManyUnpruned ;
String message = JGitText . get ( ) . gcTooManyUnpruned ;
gcLog . write ( message ) ;
gcLog . write ( message ) ;
gcLog . commit ( ) ;
gcLog . commit ( ) ;
}
}
return newPacks ;
return newPacks ;
} catch ( IOException | ParseException e ) {
} catch ( IOException | ParseException e ) {
if ( background ) {
try {
if ( gcLog = = null ) {
gcLog . write ( e . getMessage ( ) ) ;
// Lacking a log, there's no way to report this.
StringWriter sw = new StringWriter ( ) ;
return Collections . emptyList ( ) ;
e . printStackTrace ( new PrintWriter ( sw ) ) ;
}
gcLog . write ( sw . toString ( ) ) ;
try {
gcLog . commit ( ) ;
gcLog . write ( e . getMessage ( ) ) ;
} catch ( IOException e2 ) {
StringWriter sw = new StringWriter ( ) ;
e2 . addSuppressed ( e ) ;
e . printStackTrace ( new PrintWriter ( sw ) ) ;
LOG . error ( e2 . getMessage ( ) , e2 ) ;
gcLog . write ( sw . toString ( ) ) ;
gcLog . commit ( ) ;
} catch ( IOException e2 ) {
e2 . addSuppressed ( e ) ;
LOG . error ( e2 . getMessage ( ) , e2 ) ;
}
} else {
throw new JGitInternalException ( e . getMessage ( ) , e ) ;
}
}
} finally {
} finally {
if ( gcLog ! = null ) {
gcLog . unlock ( ) ;
gcLog . unlock ( ) ;
}
}
}
return Collections . emptyList ( ) ;
return Collections . emptyList ( ) ;
} ;
} ;
Future < Collection < PackFile > > result = executor . submit ( gcTask ) ;
// TODO(ms): in 5.0 change signature and return the Future
if ( background ) {
executor . submit ( gcTask ) ;
// TODO(ms): in 5.0 change signature and return the Future
return Collections . emptyList ( ) ;
return Collections . emptyList ( ) ;
}
try {
return result . get ( ) ;
} catch ( InterruptedException | ExecutionException e ) {
throw new IOException ( e ) ;
}
}
}
private Collection < PackFile > doGc ( ) throws IOException , ParseException {
private Collection < PackFile > doGc ( ) throws IOException , ParseException {