mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-05-07 10:27:00 +02:00
#994 fixed empty repository name on non bare repositories
This commit is contained in:
@@ -35,6 +35,7 @@ package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -237,23 +238,18 @@ public final class RepositoryUtil
|
||||
public static String getRepositoryName(File baseDirectory, File directory)
|
||||
throws IOException
|
||||
{
|
||||
String name = null;
|
||||
String path = directory.getCanonicalPath();
|
||||
int directoryLength = baseDirectory.getCanonicalPath().length();
|
||||
String basePath = baseDirectory.getCanonicalPath();
|
||||
|
||||
if (directoryLength < path.length())
|
||||
{
|
||||
name = IOUtil.trimSeperatorChars(path.substring(directoryLength));
|
||||
Preconditions.checkArgument(
|
||||
path.startsWith(basePath),
|
||||
"repository path %s is not in the main repository path %s", path, basePath
|
||||
);
|
||||
|
||||
// replace windows path seperator
|
||||
name = name.replaceAll("\\\\", "/");
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("path is shorter as the main repository path");
|
||||
}
|
||||
String name = IOUtil.trimSeperatorChars(path.substring(basePath.length()));
|
||||
|
||||
return name;
|
||||
// replace windows path seperator
|
||||
return name.replaceAll("\\\\", "/");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package sonia.scm.repository;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class RepositoryUtilTest {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Mock
|
||||
private AbstractRepositoryHandler<SimpleRepositoryConfig> repositoryHandler;
|
||||
|
||||
private SimpleRepositoryConfig repositoryConfig;
|
||||
|
||||
@Before
|
||||
public void setUpMocks() {
|
||||
repositoryConfig = new SimpleRepositoryConfig();
|
||||
when(repositoryHandler.getConfig()).thenReturn(repositoryConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRepositoryName() throws IOException {
|
||||
File repositoryTypeRoot = temporaryFolder.newFolder();
|
||||
repositoryConfig.setRepositoryDirectory(repositoryTypeRoot);
|
||||
|
||||
File repository = new File(repositoryTypeRoot, "abc");
|
||||
String name = RepositoryUtil.getRepositoryName(repositoryHandler, repository.getPath());
|
||||
assertEquals("abc", name);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetRepositoryNameWithInvalidPath() throws IOException {
|
||||
File repositoryTypeRoot = temporaryFolder.newFolder();
|
||||
repositoryConfig.setRepositoryDirectory(repositoryTypeRoot);
|
||||
|
||||
File repository = new File("/etc/abc");
|
||||
String name = RepositoryUtil.getRepositoryName(repositoryHandler, repository.getPath());
|
||||
assertEquals("abc", name);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetRepositoryNameWithInvalidPathButSameLength() throws IOException {
|
||||
File repositoryTypeRoot = temporaryFolder.newFolder();
|
||||
repositoryConfig.setRepositoryDirectory(repositoryTypeRoot);
|
||||
|
||||
File repository = new File(temporaryFolder.newFolder(), "abc");
|
||||
|
||||
String name = RepositoryUtil.getRepositoryName(repositoryHandler, repository.getPath());
|
||||
assertEquals("abc", name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRepositoryNameWithSubDirectory() throws IOException {
|
||||
File repositoryTypeRoot = temporaryFolder.newFolder();
|
||||
repositoryConfig.setRepositoryDirectory(repositoryTypeRoot);
|
||||
|
||||
File repository = new File(repositoryTypeRoot, "abc/123");
|
||||
String name = RepositoryUtil.getRepositoryName(repositoryHandler, repository.getPath());
|
||||
assertEquals("abc/123", name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user