Fix deletion of temporary clone directory

This commit is contained in:
René Pfeuffer
2018-11-07 11:13:21 +01:00
parent e2766d494a
commit 04c5d6f84a
2 changed files with 13 additions and 6 deletions

View File

@@ -11,13 +11,11 @@ import sonia.scm.repository.InternalRepositoryException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Random;
public class SimpleGitWorkdirFactory implements GitWorkdirFactory { public class SimpleGitWorkdirFactory implements GitWorkdirFactory {
private static final Logger logger = LoggerFactory.getLogger(SimpleGitWorkdirFactory.class); private static final Logger logger = LoggerFactory.getLogger(SimpleGitWorkdirFactory.class);
private final Random random = new Random();
private final File poolDirectory; private final File poolDirectory;
public SimpleGitWorkdirFactory() { public SimpleGitWorkdirFactory() {
@@ -30,13 +28,22 @@ public class SimpleGitWorkdirFactory implements GitWorkdirFactory {
public WorkingCopy createWorkingCopy(GitContext gitContext) { public WorkingCopy createWorkingCopy(GitContext gitContext) {
try { try {
Repository clone = cloneRepository(gitContext.getDirectory(), new File(poolDirectory, Long.toString(random.nextLong()))); Repository clone = cloneRepository(gitContext.getDirectory(), createNewWorkdir());
return new WorkingCopy(clone, this::close); return new WorkingCopy(clone, this::close);
} catch (GitAPIException e) { } catch (GitAPIException e) {
throw new InternalRepositoryException("could not clone working copy of repository", e); throw new InternalRepositoryException("could not clone working copy of repository", e);
} catch (IOException e) {
throw new InternalRepositoryException("could not create temporary directory for copy of repository", e);
} }
} }
private File createNewWorkdir() throws IOException {
File workdir = File.createTempFile("workdir", "", poolDirectory);
workdir.delete();
workdir.mkdir();
return workdir;
}
protected Repository cloneRepository(File bareRepository, File target) throws GitAPIException { protected Repository cloneRepository(File bareRepository, File target) throws GitAPIException {
return Git.cloneRepository() return Git.cloneRepository()
.setURI(bareRepository.getAbsolutePath()) .setURI(bareRepository.getAbsolutePath())
@@ -48,9 +55,9 @@ public class SimpleGitWorkdirFactory implements GitWorkdirFactory {
private void close(Repository repository) { private void close(Repository repository) {
repository.close(); repository.close();
try { try {
FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE); FileUtils.delete(repository.getWorkTree(), FileUtils.RECURSIVE);
} catch (IOException e) { } catch (IOException e) {
logger.warn("could not delete temporary git workdir '{}'", repository.getDirectory(), e); logger.warn("could not delete temporary git workdir '{}'", repository.getWorkTree(), e);
} }
} }
} }

View File

@@ -66,7 +66,7 @@ public class SimpleGitWorkdirFactoryTest extends AbstractGitCommandTestBase {
File directory; File directory;
try (WorkingCopy workingCopy = factory.createWorkingCopy(createContext())) { try (WorkingCopy workingCopy = factory.createWorkingCopy(createContext())) {
directory = workingCopy.get().getDirectory(); directory = workingCopy.get().getWorkTree();
} }
assertThat(directory).doesNotExist(); assertThat(directory).doesNotExist();
} }