refactor git repository matching for accepting optional .git suffix

This commit is contained in:
Oliver Milke
2017-05-19 17:27:48 +02:00
parent 80fe417206
commit 9b932a325e
3 changed files with 63 additions and 14 deletions

View File

@@ -83,6 +83,9 @@ public class GitRepositoryHandler
/** Field description */
public static final String TYPE_NAME = "git";
public static final String DOT_GIT = ".git";
private static final Logger logger = LoggerFactory.getLogger(GitRepositoryHandler.class);
/** Field description */

View File

@@ -35,6 +35,7 @@ package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
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
* @param path
*
* @return
* @param repository The repository to be 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.
*/
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;
String name = repository.getName();
if (path.startsWith(name))
{
String sub = path.substring(name.length());
if (path.startsWith(repositoryName)) {
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;

View File

@@ -46,6 +46,7 @@ import sonia.scm.security.DefaultKeyGenerator;
import sonia.scm.store.JAXBStoreFactory;
import sonia.scm.store.StoreFactory;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@@ -92,6 +93,29 @@ public class DefaultRepositoryManagerTest extends RepositoryManagerTestBase
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 --------------------------------------------------------------
/**