mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
refactor git repository matching for accepting optional .git suffix
This commit is contained in:
@@ -82,7 +82,10 @@ public class GitRepositoryHandler
|
|||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
public static final String TYPE_NAME = "git";
|
public static final String TYPE_NAME = "git";
|
||||||
|
|
||||||
|
|
||||||
|
public static final String DOT_GIT = ".git";
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(GitRepositoryHandler.class);
|
private static final Logger logger = LoggerFactory.getLogger(GitRepositoryHandler.class);
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ package sonia.scm.repository;
|
|||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||||
@@ -972,24 +973,45 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* This method checks whether or not the provided path belongs to the provided repository.
|
||||||
*
|
*
|
||||||
*
|
* @param repository The repository to be tested.
|
||||||
* @param repository
|
* @param path The path that might be part of the repository.
|
||||||
* @param path
|
* @return Returns <code>true</code> if path belongs to the repository. Returns <code>false</code> otherwise.
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private boolean isNameMatching(Repository repository, String path)
|
private boolean isNameMatching(Repository repository, String path) {
|
||||||
{
|
return isNameMatching(repository.getType(), repository.getName(), path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method checks whether or not the provided path belongs to the provided repository.
|
||||||
|
*
|
||||||
|
* @param repositoryType The type of the repository being tested.
|
||||||
|
* @param repositoryName The name of the repository being tested.
|
||||||
|
* @param path The path that might be part of the repository.
|
||||||
|
* @return Returns <code>true</code> if path belongs to the repository. Returns <code>false</code> otherwise.
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
boolean isNameMatching(String repositoryType, String repositoryName, String path) {
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
String name = repository.getName();
|
|
||||||
|
|
||||||
if (path.startsWith(name))
|
if (path.startsWith(repositoryName)) {
|
||||||
{
|
|
||||||
String sub = path.substring(name.length());
|
String pathPart = path.substring(repositoryName.length());
|
||||||
|
|
||||||
|
//TODO: this introduces a strong coupling to the git plugin. This can be resolved with a "Repository Matcher" API.
|
||||||
|
//ausformulieren, ticketId weg
|
||||||
|
if (GitRepositoryHandler.TYPE_NAME.equals(repositoryType)) {
|
||||||
|
|
||||||
|
//git repository may also be named <<repo-name>>.git by convention
|
||||||
|
if (pathPart.startsWith(GitRepositoryHandler.DOT_GIT)) {
|
||||||
|
//if this is the case, just also cut it away
|
||||||
|
pathPart = pathPart.substring(GitRepositoryHandler.DOT_GIT.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = Util.isEmpty(pathPart) || pathPart.startsWith(HttpUtil.SEPARATOR_PATH);
|
||||||
|
|
||||||
result = Util.isEmpty(sub) || sub.startsWith(HttpUtil.SEPARATOR_PATH);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ import sonia.scm.security.DefaultKeyGenerator;
|
|||||||
import sonia.scm.store.JAXBStoreFactory;
|
import sonia.scm.store.JAXBStoreFactory;
|
||||||
import sonia.scm.store.StoreFactory;
|
import sonia.scm.store.StoreFactory;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
@@ -92,6 +93,29 @@ public class DefaultRepositoryManagerTest extends RepositoryManagerTestBase
|
|||||||
assertNull(m.getFromUri("/git/project1/test-3/ka/some/path"));
|
assertNull(m.getFromUri("/git/project1/test-3/ka/some/path"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNameIsMatching() throws Exception {
|
||||||
|
DefaultRepositoryManager m = createManager();
|
||||||
|
|
||||||
|
assertThat(m.isNameMatching(GitRepositoryHandler.TYPE_NAME, "repo-name", "repo-name"), is(true));
|
||||||
|
assertThat(m.isNameMatching(GitRepositoryHandler.TYPE_NAME, "repo-name", "repo-name/"), is(true));
|
||||||
|
assertThat(m.isNameMatching(GitRepositoryHandler.TYPE_NAME, "repo-name", "repo-name/and-more-is-valid"), is(true));
|
||||||
|
assertThat(m.isNameMatching(GitRepositoryHandler.TYPE_NAME, "repo-name", "repo-name.git/and-more-is-valid"),
|
||||||
|
is(true));
|
||||||
|
|
||||||
|
|
||||||
|
assertThat(m.isNameMatching(GitRepositoryHandler.TYPE_NAME, "repo-name", "not-the-name"), is(false));
|
||||||
|
assertThat(m.isNameMatching(GitRepositoryHandler.TYPE_NAME, "repo-name", "repo-na"), is(false));
|
||||||
|
|
||||||
|
assertThat(m.isNameMatching(GitRepositoryHandler.TYPE_NAME, "repo-name", "/repo-name/"), is(false));
|
||||||
|
|
||||||
|
assertThat(m.isNameMatching(HgRepositoryHandler.TYPE_NAME, "repo-name", "repo-name.git/and-more-is-valid"),
|
||||||
|
is(false));
|
||||||
|
assertThat(m.isNameMatching(SvnRepositoryHandler.TYPE_NAME, "repo-name", "repo-name.git/and-more-is-valid"),
|
||||||
|
is(false));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user