Pavel@27: /* Pavel@27: * Copyright 2000-2007 JetBrains s.r.o. Pavel@27: * Pavel@27: * Licensed under the Apache License, Version 2.0 (the "License"); Pavel@27: * you may not use this file except in compliance with the License. Pavel@27: * You may obtain a copy of the License at Pavel@27: * Pavel@27: * http://www.apache.org/licenses/LICENSE-2.0 Pavel@27: * Pavel@27: * Unless required by applicable law or agreed to in writing, software Pavel@27: * distributed under the License is distributed on an "AS IS" BASIS, Pavel@27: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Pavel@27: * See the License for the specific language governing permissions and Pavel@27: * limitations under the License. Pavel@27: */ Pavel@27: package jetbrains.buildServer.buildTriggers.vcs.mercurial.command; Pavel@27: Pavel@27: import jetbrains.buildServer.buildTriggers.vcs.mercurial.Constants; Pavel@27: import jetbrains.buildServer.buildTriggers.vcs.mercurial.PathUtil; Pavel@27: import jetbrains.buildServer.util.Hash; Pavel@44: import jetbrains.buildServer.util.StringUtil; Pavel@45: import jetbrains.buildServer.vcs.VcsRoot; Pavel@27: import org.jetbrains.annotations.NotNull; Pavel@27: Pavel@27: import java.io.File; Pavel@46: import java.util.HashSet; Pavel@45: import java.util.Set; Pavel@27: Pavel@27: /** Pavel@27: * Represents Mercurial repository settings Pavel@27: */ Pavel@27: public class Settings { Pavel@27: private String myRepository; Pavel@27: private String myHgCommandPath; Pavel@27: private File myWorkingDir; Pavel@27: private File myWorkFolderParentDir; Pavel@45: private String myUsername; Pavel@45: private String myPassword; Pavel@57: private String myBranchName; Pavel@57: private static final String DEFAULT_BRANCH_NAME = "default"; Pavel@27: Pavel@27: public Settings(@NotNull File workFolderParentDir, @NotNull VcsRoot vcsRoot) { Pavel@27: myWorkFolderParentDir = workFolderParentDir; Pavel@27: setRepository(vcsRoot.getProperty(Constants.REPOSITORY_PROP)); Pavel@27: setHgCommandPath(vcsRoot.getProperty(Constants.HG_COMMAND_PATH_PROP)); Pavel@57: myBranchName = vcsRoot.getProperty(Constants.BRANCH_NAME_PROP); Pavel@45: Pavel@45: myUsername = vcsRoot.getProperty(Constants.USERNAME); Pavel@45: myPassword = vcsRoot.getProperty(Constants.PASSWORD); Pavel@27: } Pavel@27: Pavel@27: public Settings() { Pavel@27: } Pavel@27: Pavel@27: public void setRepository(@NotNull final String repository) { Pavel@27: myRepository = repository; Pavel@27: } Pavel@27: Pavel@27: /** Pavel@57: * Returns name of the branch to use (returns 'default' if no branch specified) Pavel@57: * @return see above Pavel@57: */ Pavel@57: @NotNull Pavel@57: public String getBranchName() { Pavel@57: return StringUtil.isEmpty(myBranchName) ? DEFAULT_BRANCH_NAME : myBranchName; Pavel@57: } Pavel@57: Pavel@57: /** Pavel@57: * Returns true if current branch is default branch Pavel@57: * @return see above Pavel@57: */ Pavel@57: public boolean isDefaultBranch() { Pavel@57: return getBranchName().equals(DEFAULT_BRANCH_NAME); Pavel@57: } Pavel@57: Pavel@57: /** Pavel@27: * Returns path to hg command Pavel@27: * @return path to hg command Pavel@27: */ Pavel@27: @NotNull Pavel@27: public String getHgCommandPath() { Pavel@27: return myHgCommandPath; Pavel@27: } Pavel@27: Pavel@65: public String getUsername() { Pavel@65: return myUsername; Pavel@65: } Pavel@65: Pavel@65: public String getPassword() { Pavel@65: return myPassword; Pavel@65: } Pavel@65: Pavel@45: private final static Set AUTH_PROTOS = new HashSet(); Pavel@45: static { Pavel@45: AUTH_PROTOS.add("http://"); Pavel@45: AUTH_PROTOS.add("https://"); Pavel@45: AUTH_PROTOS.add("ssh://"); Pavel@45: } Pavel@45: Pavel@44: /** Pavel@45: * Returns URL to use for push command Pavel@45: * @return URL to use for push command Pavel@44: */ Pavel@67: public String getRepositoryUrl() { C:\Documents@63: if (containsCredentials(myRepository)) return myRepository; C:\Documents@63: Pavel@45: for (String proto: AUTH_PROTOS) { Pavel@45: if (myRepository.startsWith(proto)) { Pavel@93: String repoUrl = myRepository.substring(proto.length()); Pavel@93: int endIdx = repoUrl.indexOf('@'); Pavel@93: int slashIdx = repoUrl.indexOf('/'); Pavel@93: if (endIdx != -1 && slashIdx > endIdx) { Pavel@93: repoUrl = repoUrl.substring(endIdx+1); Pavel@93: } Pavel@93: Pavel@93: String cre = ""; Pavel@93: if (!StringUtil.isEmpty(myUsername)) { Pavel@93: cre += myUsername; Pavel@93: if (!StringUtil.isEmpty(myPassword)) { Pavel@93: cre += ":" + myPassword; Pavel@93: } Pavel@93: cre += "@"; Pavel@93: } Pavel@93: Pavel@93: return proto + cre + repoUrl; Pavel@45: } Pavel@45: } Pavel@45: Pavel@45: return myRepository; Pavel@44: } Pavel@44: C:\Documents@63: private boolean containsCredentials(final String repository) { C:\Documents@63: for (String proto: AUTH_PROTOS) { Pavel@64: if (repository.startsWith(proto)) { Pavel@64: String withoutProto = repository.substring(proto.length()); C:\Documents@63: int comma = withoutProto.indexOf(':'); C:\Documents@63: int at = withoutProto.indexOf('@'); C:\Documents@63: if (at != -1 && comma != -1 && at > comma) return true; C:\Documents@63: } C:\Documents@63: } C:\Documents@63: return false; C:\Documents@63: } C:\Documents@63: Pavel@27: public void setHgCommandPath(@NotNull final String hgCommandPath) { Pavel@27: myHgCommandPath = hgCommandPath; Pavel@27: } Pavel@27: Pavel@27: public void setWorkingDir(@NotNull final File workingDir) { Pavel@46: myWorkingDir = PathUtil.getCanonicalFile(workingDir); Pavel@27: } Pavel@27: Pavel@27: /** Pavel@57: * Returns directory where repository is supposed to be cloned, i.e. working directory of cloned repository Pavel@27: * @return repository working directory Pavel@27: */ Pavel@27: @NotNull Pavel@57: public File getLocalRepositoryDir() { Pavel@27: if (myWorkingDir != null) { Pavel@27: return myWorkingDir; Pavel@27: } Pavel@27: Pavel@27: return getDefaultWorkDir(myWorkFolderParentDir, myRepository); Pavel@27: } Pavel@27: Pavel@29: /** Pavel@29: * Returns true if current working directory contains copy of repository (contains .hg directory) Pavel@29: * @return see above Pavel@29: */ Pavel@29: public boolean hasCopyOfRepository() { Pavel@29: // need better way to check that repository copy is ok Pavel@57: return getLocalRepositoryDir().isDirectory() && new File(getLocalRepositoryDir(), ".hg").isDirectory(); Pavel@29: } Pavel@29: Pavel@27: public static String DEFAULT_WORK_DIR_PREFIX = "hg_"; Pavel@27: Pavel@27: private static File getDefaultWorkDir(@NotNull File workFolderParentDir, @NotNull String repPath) { Pavel@27: String workingDirname = DEFAULT_WORK_DIR_PREFIX + String.valueOf(Hash.calc(normalize(repPath))); Pavel@46: return PathUtil.getCanonicalFile(new File(workFolderParentDir, workingDirname)); Pavel@27: } Pavel@27: Pavel@27: private static String normalize(final String path) { Pavel@27: String normalized = PathUtil.normalizeSeparator(path); Pavel@27: if (path.endsWith("/")) { Pavel@27: return normalized.substring(0, normalized.length()-1); Pavel@27: } Pavel@27: return normalized; Pavel@27: } Pavel@27: }