Mercurial > hg > mercurial
changeset 93:a0aea01c32b0
improve repository url parsing
author | Pavel.Sher |
---|---|
date | Tue, 19 Jan 2010 22:23:15 +0300 |
parents | dc26ddd9ffd4 |
children | 889f1bd868f8 064e6a7a74a5 |
files | mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/SettingsTest.java |
diffstat | 2 files changed, 64 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java Wed Dec 09 11:14:30 2009 +0300 +++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/Settings.java Tue Jan 19 22:23:15 2010 +0300 @@ -104,18 +104,25 @@ public String getRepositoryUrl() { if (containsCredentials(myRepository)) return myRepository; - String cre = ""; - if (!StringUtil.isEmpty(myUsername)) { - cre += myUsername; - if (!StringUtil.isEmpty(myPassword)) { - cre += ":" + myPassword; - } - cre += "@"; - } - for (String proto: AUTH_PROTOS) { if (myRepository.startsWith(proto)) { - return proto + cre + myRepository.substring(proto.length()); + String repoUrl = myRepository.substring(proto.length()); + int endIdx = repoUrl.indexOf('@'); + int slashIdx = repoUrl.indexOf('/'); + if (endIdx != -1 && slashIdx > endIdx) { + repoUrl = repoUrl.substring(endIdx+1); + } + + String cre = ""; + if (!StringUtil.isEmpty(myUsername)) { + cre += myUsername; + if (!StringUtil.isEmpty(myPassword)) { + cre += ":" + myPassword; + } + cre += "@"; + } + + return proto + cre + repoUrl; } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/SettingsTest.java Tue Jan 19 22:23:15 2010 +0300 @@ -0,0 +1,47 @@ +package jetbrains.buildServer.buildTriggers.vcs.mercurial; + +import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.Settings; +import jetbrains.buildServer.vcs.impl.VcsRootImpl; +import junit.framework.Assert; +import org.testng.annotations.Test; + +import java.io.File; + +/** + * @author Pavel.Sher + */ +@Test +public class SettingsTest extends Assert { + public void test_url_without_credentials() { + VcsRootImpl vcsRoot = createVcsRoot("http://host.com/path"); + Settings settings = new Settings(new File("."), vcsRoot); + assertEquals("http://user:pwd@host.com/path", settings.getRepositoryUrl()); + } + + public void test_url_with_credentials() { + VcsRootImpl vcsRoot = createVcsRoot("http://user:pwd@host.com/path"); + Settings settings = new Settings(new File("."), vcsRoot); + assertEquals("http://user:pwd@host.com/path", settings.getRepositoryUrl()); + } + + public void test_url_with_username() { + VcsRootImpl vcsRoot = createVcsRoot("http://user@host.com/path"); + Settings settings = new Settings(new File("."), vcsRoot); + assertEquals("http://user:pwd@host.com/path", settings.getRepositoryUrl()); + } + + public void test_url_with_at_after_slash() { + VcsRootImpl vcsRoot = createVcsRoot("http://host.com/path@"); + Settings settings = new Settings(new File("."), vcsRoot); + assertEquals("http://user:pwd@host.com/path@", settings.getRepositoryUrl()); + } + + private VcsRootImpl createVcsRoot(String url) { + VcsRootImpl vcsRoot = new VcsRootImpl(1, Constants.VCS_NAME); + vcsRoot.addProperty(Constants.HG_COMMAND_PATH_PROP, "hg.exe"); + vcsRoot.addProperty(Constants.REPOSITORY_PROP, url); + vcsRoot.addProperty(Constants.USERNAME, "user"); + vcsRoot.addProperty(Constants.PASSWORD, "pwd"); + return vcsRoot; + } +}