@ -77,15 +77,12 @@ 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.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 ;
@ -248,8 +245,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 ( ) ;
}
}
@ -257,18 +257,13 @@ 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 ) {
if ( gcLog = = null ) {
// Lacking a log, there's no way to report this.
return Collections . emptyList ( ) ;
}
try {
try {
gcLog . write ( e . getMessage ( ) ) ;
gcLog . write ( e . getMessage ( ) ) ;
StringWriter sw = new StringWriter ( ) ;
StringWriter sw = new StringWriter ( ) ;
@ -279,27 +274,15 @@ public class GC {
e2 . addSuppressed ( e ) ;
e2 . addSuppressed ( e ) ;
LOG . error ( e2 . getMessage ( ) , e2 ) ;
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 ) ;
if ( background ) {
// TODO(ms): in 5.0 change signature and return the Future
// TODO(ms): in 5.0 change signature and return the Future
executor ( ) . submit ( gcTask ) ;
return Collections . emptyList ( ) ;
return Collections . emptyList ( ) ;
}
}
try {
return result . get ( ) ;
} catch ( InterruptedException | ExecutionException e ) {
throw new IOException ( e ) ;
}
}
private ExecutorService executor ( ) {
private ExecutorService executor ( ) {
return ( executor ! = null ) ? executor : WorkQueue . getExecutor ( ) ;
return ( executor ! = null ) ? executor : WorkQueue . getExecutor ( ) ;