Harmonize repository resolution

This commit is contained in:
René Pfeuffer
2018-11-27 15:31:57 +01:00
parent bc629ec648
commit d4db39755f
31 changed files with 52 additions and 69 deletions

View File

@@ -168,7 +168,7 @@ public abstract class AbstractRepositoryHandler<C extends RepositoryConfig>
* @throws NotSupportedFeatureException
*/
@Override
public ImportHandler getImportHandler() throws NotSupportedFeatureException
public ImportHandler getImportHandler()
{
throw new NotSupportedFeatureException("import");
}

View File

@@ -76,7 +76,7 @@ public abstract class AbstractSimpleRepositoryHandler<C extends RepositoryConfig
@Override
public Repository create(Repository repository) {
File nativeDirectory = resolveNativeDirectory(repository);
File nativeDirectory = resolveNativeDirectory(repository.getId());
try {
create(repository, nativeDirectory);
postCreate(repository, nativeDirectory);
@@ -106,10 +106,10 @@ public abstract class AbstractSimpleRepositoryHandler<C extends RepositoryConfig
}
@Override
public File getDirectory(Repository repository) {
public File getDirectory(String repositoryId) {
File directory;
if (isConfigured()) {
directory = resolveNativeDirectory(repository);
directory = resolveNativeDirectory(repositoryId);
} else {
throw new ConfigurationException("RepositoryHandler is not configured");
}
@@ -167,7 +167,7 @@ public abstract class AbstractSimpleRepositoryHandler<C extends RepositoryConfig
return content;
}
private File resolveNativeDirectory(Repository repository) {
return new File(repositoryLocationResolver.getRepositoryDirectory(repository), REPOSITORIES_NATIVE_DIRECTORY);
private File resolveNativeDirectory(String repositoryId) {
return new File(repositoryLocationResolver.getRepositoryDirectory(repositoryId), REPOSITORIES_NATIVE_DIRECTORY);
}
}

View File

@@ -178,7 +178,7 @@ public abstract class DirectoryHealthCheck implements HealthCheck
else if (handler instanceof RepositoryDirectoryHandler)
{
File directory =
((RepositoryDirectoryHandler) handler).getDirectory(repository);
((RepositoryDirectoryHandler) handler).getDirectory(repository.getId());
if (directory == null)
{

View File

@@ -28,8 +28,8 @@ public class InitialRepositoryLocationResolver {
this.context = context;
}
public InitialRepositoryLocation getRelativeRepositoryPath(Repository repository) {
String relativePath = DEFAULT_REPOSITORY_PATH + File.separator + repository.getId();
public InitialRepositoryLocation getRelativeRepositoryPath(String repositoryId) {
String relativePath = DEFAULT_REPOSITORY_PATH + File.separator + repositoryId;
return new InitialRepositoryLocation(new File(context.getBaseDirectory(), relativePath), relativePath);
}

View File

@@ -11,7 +11,8 @@ import java.nio.file.Path;
public interface PathBasedRepositoryDAO extends RepositoryDAO {
/**
* Get the current path of the repository. This works for existing repositories only, not for repositories that should be created.
* Get the current path of the repository for the given id.
* This works for existing repositories only, not for repositories that should be created.
*/
Path getPath(Repository repository) ;
Path getPath(String repositoryId) ;
}

View File

@@ -43,9 +43,8 @@ import java.io.File;
public interface RepositoryDirectoryHandler extends RepositoryHandler {
/**
* Get the current directory of the given repository
* @param repository
* Get the current directory of the repository for the given id.
* @return the current directory of the given repository
*/
File getDirectory(Repository repository);
File getDirectory(String repositoryId);
}

View File

@@ -29,15 +29,11 @@ public class RepositoryLocationResolver {
this.initialRepositoryLocationResolver = initialRepositoryLocationResolver;
}
public File getRepositoryDirectory(Repository repository){
public File getRepositoryDirectory(String repositoryId) {
if (repositoryDAO instanceof PathBasedRepositoryDAO) {
PathBasedRepositoryDAO pathBasedRepositoryDAO = (PathBasedRepositoryDAO) repositoryDAO;
return pathBasedRepositoryDAO.getPath(repository).toFile();
return pathBasedRepositoryDAO.getPath(repositoryId).toFile();
}
return initialRepositoryLocationResolver.getRelativeRepositoryPath(repository).getAbsolutePath();
}
public File getRepositoryDirectory(String repositoryId) {
return getRepositoryDirectory(repositoryDAO.get(repositoryId));
return initialRepositoryLocationResolver.getRelativeRepositoryPath(repositoryId).getAbsolutePath();
}
}

View File

@@ -32,9 +32,7 @@ public class InitialRepositoryLocationResolverTest {
@Test
public void shouldComputeInitialDirectory() {
InitialRepositoryLocationResolver resolver = new InitialRepositoryLocationResolver(context);
Repository repository = new Repository();
repository.setId("ABC");
InitialRepositoryLocationResolver.InitialRepositoryLocation directory = resolver.getRelativeRepositoryPath(repository);
InitialRepositoryLocationResolver.InitialRepositoryLocation directory = resolver.getRelativeRepositoryPath("ABC");
assertThat(directory.getAbsolutePath()).isEqualTo(new File(context.getBaseDirectory(), "repositories/ABC"));
assertThat(directory.getRelativePath()).isEqualTo( "repositories/ABC");

View File

@@ -35,7 +35,7 @@ package sonia.scm.repository.xml;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import sonia.scm.NotFoundException;
import sonia.scm.ContextEntry;
import sonia.scm.SCMContextProvider;
import sonia.scm.io.FileSystem;
import sonia.scm.repository.InitialRepositoryLocationResolver;
@@ -47,14 +47,11 @@ import sonia.scm.repository.Repository;
import sonia.scm.store.ConfigurationStoreFactory;
import sonia.scm.xml.AbstractXmlDAO;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
import static sonia.scm.ContextEntry.ContextBuilder.entity;
/**
* @author Sebastian Sdorra
*/
@@ -104,7 +101,7 @@ public class XmlRepositoryDAO
@Override
public void modify(Repository repository) {
RepositoryPath repositoryPath = findExistingRepositoryPath(repository).orElseThrow(() -> new InternalRepositoryException(repository, "path object for repository not found"));
RepositoryPath repositoryPath = findExistingRepositoryPath(repository.getId()).orElseThrow(() -> new InternalRepositoryException(repository, "path object for repository not found"));
repositoryPath.setRepository(repository);
repositoryPath.setToBeSynchronized(true);
storeDB();
@@ -112,7 +109,7 @@ public class XmlRepositoryDAO
@Override
public void add(Repository repository) {
InitialRepositoryLocation initialLocation = initialRepositoryLocationResolver.getRelativeRepositoryPath(repository);
InitialRepositoryLocation initialLocation = initialRepositoryLocationResolver.getRelativeRepositoryPath(repository.getId());
try {
fileSystem.create(initialLocation.getAbsolutePath());
} catch (IOException e) {
@@ -153,7 +150,7 @@ public class XmlRepositoryDAO
@Override
public void delete(Repository repository) {
Path directory = getPath(repository);
Path directory = getPath(repository.getId());
super.delete(repository);
try {
fileSystem.destroy(directory.toFile());
@@ -173,19 +170,19 @@ public class XmlRepositoryDAO
}
@Override
public Path getPath(Repository repository) {
public Path getPath(String repositoryId) {
return context
.getBaseDirectory()
.toPath()
.resolve(
findExistingRepositoryPath(repository)
findExistingRepositoryPath(repositoryId)
.map(RepositoryPath::getPath)
.orElseThrow(() -> new InternalRepositoryException(repository, "could not find base directory for repository")));
.orElseThrow(() -> new InternalRepositoryException(ContextEntry.ContextBuilder.entity("repository", repositoryId), "could not find base directory for repository")));
}
private Optional<RepositoryPath> findExistingRepositoryPath(Repository repository) {
private Optional<RepositoryPath> findExistingRepositoryPath(String repositoryId) {
return db.values().stream()
.filter(repoPath -> repoPath.getId().equals(repository.getId()))
.filter(repoPath -> repoPath.getId().equals(repositoryId))
.findAny();
}
}

View File

@@ -89,7 +89,7 @@ public class XmlRepositoryDAOTest {
XmlRepositoryDAO dao = new XmlRepositoryDAO(storeFactory, new InitialRepositoryLocationResolver(context), fileSystem, context);
Path path = dao.getPath(existingRepository);
Path path = dao.getPath(existingRepository.getId());
assertThat(path.toString()).isEqualTo(context.getBaseDirectory().getPath() + "/path");
}
@@ -102,7 +102,7 @@ public class XmlRepositoryDAOTest {
XmlRepositoryDAO dao = new XmlRepositoryDAO(storeFactory, new InitialRepositoryLocationResolver(context), fileSystem, context);
Path path = dao.getPath(existingRepository);
Path path = dao.getPath(existingRepository.getId());
assertThat(path.toString()).isEqualTo("/tmp/path");
}

View File

@@ -121,7 +121,7 @@ public class GitGcTask implements Runnable {
}
private void gc(Repository repository){
File file = repositoryHandler.getDirectory(repository);
File file = repositoryHandler.getDirectory(repository.getId());
Git git = null;
try {
git = open(file);

View File

@@ -119,7 +119,7 @@ public abstract class AbstractGitIncomingOutgoingCommand
Git git = Git.wrap(open());
GitUtil.fetch(git, handler.getDirectory(remoteRepository), remoteRepository);
GitUtil.fetch(git, handler.getDirectory(remoteRepository.getId()), remoteRepository);
ObjectId localId = getDefaultBranch(git.getRepository());
ObjectId remoteId = null;

View File

@@ -196,7 +196,7 @@ public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand
*/
protected String getRemoteUrl(sonia.scm.repository.Repository repository)
{
return getRemoteUrl(handler.getDirectory(repository));
return getRemoteUrl(handler.getDirectory(repository.getId()));
}
//~--- methods --------------------------------------------------------------

View File

@@ -196,12 +196,12 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
private PullResponse pullFromScmRepository(Repository sourceRepository)
throws IOException
{
File sourceDirectory = handler.getDirectory(sourceRepository);
File sourceDirectory = handler.getDirectory(sourceRepository.getId());
Preconditions.checkArgument(sourceDirectory.exists(),
"source repository directory does not exists");
File targetDirectory = handler.getDirectory(repository);
File targetDirectory = handler.getDirectory(repository.getId());
Preconditions.checkArgument(sourceDirectory.exists(),
"target repository directory does not exists");

View File

@@ -73,7 +73,7 @@ public class GitRepositoryServiceProvider extends RepositoryServiceProvider
public GitRepositoryServiceProvider(GitRepositoryHandler handler, Repository repository) {
this.handler = handler;
this.repository = repository;
this.context = new GitContext(handler.getDirectory(repository), repository);
this.context = new GitContext(handler.getDirectory(repository.getId()), repository);
}
//~--- methods --------------------------------------------------------------

View File

@@ -106,7 +106,7 @@ public class GitRepositoryResolver implements RepositoryResolver<HttpServletRequ
if (config.isValid())
{
File gitdir = handler.getDirectory(repo);
File gitdir = handler.getDirectory(repo.getId());
if (gitdir == null) {
throw new RepositoryNotFoundException(repositoryName);
}

View File

@@ -111,7 +111,7 @@ public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
repositoryHandler.setConfig(config);
initRepository();
File path = repositoryHandler.getDirectory(repository);
File path = repositoryHandler.getDirectory(repository.getId());
assertEquals(repoPath.toString() + File.separator + AbstractSimpleRepositoryHandler.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
}
}

View File

@@ -92,9 +92,9 @@ public class AbstractRemoteCommandTestBase
outgoing = Git.init().setDirectory(outgoingDirectory).setBare(false).call();
handler = mock(GitRepositoryHandler.class);
when(handler.getDirectory(incomingRepository)).thenReturn(
when(handler.getDirectory(incomingRepository.getId())).thenReturn(
incomingDirectory);
when(handler.getDirectory(outgoingRepository)).thenReturn(
when(handler.getDirectory(outgoingRepository.getId())).thenReturn(
outgoingDirectory);
}

View File

@@ -124,7 +124,7 @@ public class AbstractHgHandler
protected AbstractHgHandler(HgRepositoryHandler handler, HgContext context,
Repository repository)
{
this(handler, context, repository, handler.getDirectory(repository));
this(handler, context, repository, handler.getDirectory(repository.getId()));
}
/**

View File

@@ -114,7 +114,6 @@ public class HgRepositoryHandler
RepositoryLocationResolver repositoryLocationResolver)
{
super(storeFactory, repositoryLocationResolver);
this.repositoryLocationResolver = repositoryLocationResolver;
this.hgContextProvider = hgContextProvider;
try
@@ -428,10 +427,6 @@ public class HgRepositoryHandler
}
}
public File getDirectory(String repositoryId) {
return repositoryLocationResolver.getRepositoryDirectory(repositoryId);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@@ -439,6 +434,4 @@ public class HgRepositoryHandler
/** Field description */
private JAXBContext jaxbContext;
private final RepositoryLocationResolver repositoryLocationResolver;
}

View File

@@ -78,7 +78,7 @@ public class AbstractHgPushOrPullCommand extends AbstractCommand
if (repo != null)
{
url =
handler.getDirectory(request.getRemoteRepository()).getAbsolutePath();
handler.getDirectory(request.getRemoteRepository().getId()).getAbsolutePath();
}
else if (request.getRemoteUrl() != null)
{

View File

@@ -81,7 +81,7 @@ public class HgIncomingCommand extends AbstractCommand
@Override
@SuppressWarnings("unchecked")
public ChangesetPagingResult getIncomingChangesets(IncomingCommandRequest request) {
File remoteRepository = handler.getDirectory(request.getRemoteRepository());
File remoteRepository = handler.getDirectory(request.getRemoteRepository().getId());
com.aragost.javahg.Repository repository = open();

View File

@@ -83,7 +83,7 @@ public class HgOutgoingCommand extends AbstractCommand
public ChangesetPagingResult getOutgoingChangesets(
OutgoingCommandRequest request)
{
File remoteRepository = handler.getDirectory(request.getRemoteRepository());
File remoteRepository = handler.getDirectory(request.getRemoteRepository().getId());
com.aragost.javahg.Repository repository = open();

View File

@@ -81,7 +81,7 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
{
this.repository = repository;
this.handler = handler;
this.repositoryDirectory = handler.getDirectory(repository);
this.repositoryDirectory = handler.getDirectory(repository.getId());
this.context = new HgCommandContext(hookManager, handler, repository,
repositoryDirectory);
}

View File

@@ -253,8 +253,7 @@ public class HgCGIServlet extends HttpServlet implements ScmProviderHttpServlet
HttpServletResponse response, Repository repository)
throws IOException, ServletException
{
String name = repository.getName();
File directory = handler.getDirectory(repository);
File directory = handler.getDirectory(repository.getId());
CGIExecutor executor = cgiExecutorFactory.createExecutor(configuration,
getServletContext(), request, response);

View File

@@ -93,7 +93,7 @@ public class HgRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
repositoryHandler.setConfig(hgConfig);
initRepository();
File path = repositoryHandler.getDirectory(repository);
File path = repositoryHandler.getDirectory(repository.getId());
assertEquals(repoPath.toString() + File.separator + AbstractSimpleRepositoryHandler.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
}
}

View File

@@ -94,9 +94,9 @@ public abstract class IncomingOutgoingTestBase extends AbstractTestBase
outgoing = Repository.create(createConfig(temp), outgoingDirectory);
handler = mock(HgRepositoryHandler.class);
when(handler.getDirectory(incomingRepository)).thenReturn(
when(handler.getDirectory(incomingRepository.getId())).thenReturn(
incomingDirectory);
when(handler.getDirectory(outgoingRepository)).thenReturn(
when(handler.getDirectory(outgoingRepository.getId())).thenReturn(
outgoingDirectory);
when(handler.getConfig()).thenReturn(temp.getConfig());
when(handler.getHgContext()).thenReturn(new HgContext());

View File

@@ -63,7 +63,7 @@ public class SvnRepositoryServiceProvider extends RepositoryServiceProvider
Repository repository)
{
this.repository = repository;
this.context = new SvnContext(handler.getDirectory(repository));
this.context = new SvnContext(handler.getDirectory(repository.getId()));
}
//~--- methods --------------------------------------------------------------

View File

@@ -292,7 +292,7 @@ public class SvnDAVConfig extends DAVConfig
if (repository != null)
{
directory = handler.getDirectory(repository);
directory = handler.getDirectory(repository.getId());
}
return directory;

View File

@@ -115,7 +115,7 @@ public class SvnRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
repositoryHandler.setConfig(svnConfig);
initRepository();
File path = repositoryHandler.getDirectory(repository);
File path = repositoryHandler.getDirectory(repository.getId());
assertEquals(repoPath.toString()+File.separator+ AbstractSimpleRepositoryHandler.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
}
}

View File

@@ -100,7 +100,7 @@ public abstract class SimpleRepositoryHandlerTestBase extends AbstractTestBase {
repository = RepositoryTestData.createHeartOfGold();
File repoDirectory = new File(baseDirectory, repository.getId());
repoPath = repoDirectory.toPath();
when(repoDao.getPath(repository)).thenReturn(repoPath);
when(repoDao.getPath(repository.getId())).thenReturn(repoPath);
return new File(repoDirectory, AbstractSimpleRepositoryHandler.REPOSITORIES_NATIVE_DIRECTORY);
}