Mercurial > hg > mercurial
view mercurial-agent/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialAgentSideVcsSupport.java @ 410:e182bb6b5921 Eluru-6.5.x
Don't use pull protocol in clone, do clone whenever it is possible
author | Dmitry Neverov <dmitry.neverov@jetbrains.com> |
---|---|
date | Sat, 03 Mar 2012 12:28:29 +0400 |
parents | 7700af586e11 |
children |
line wrap: on
line source
/* * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.buildTriggers.vcs.mercurial; import jetbrains.buildServer.agent.AgentRunningBuild; import jetbrains.buildServer.agent.BuildAgentConfiguration; import jetbrains.buildServer.agent.BuildProgressLogger; import jetbrains.buildServer.agent.vcs.AgentVcsSupport; import jetbrains.buildServer.agent.vcs.IncludeRuleUpdater; import jetbrains.buildServer.agent.vcs.UpdateByIncludeRules2; import jetbrains.buildServer.agent.vcs.UpdatePolicy; import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*; import jetbrains.buildServer.util.FileUtil; import jetbrains.buildServer.vcs.CheckoutRules; import jetbrains.buildServer.vcs.IncludeRule; import jetbrains.buildServer.vcs.VcsException; import jetbrains.buildServer.vcs.VcsRoot; import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; import java.util.Collections; import static java.util.Collections.singleton; import static jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CommandUtil.removePrivateData; import static jetbrains.buildServer.util.FileUtil.isEmptyDir; public class MercurialAgentSideVcsSupport extends AgentVcsSupport implements UpdateByIncludeRules2 { private final MirrorManager myMirrorManager; public MercurialAgentSideVcsSupport(BuildAgentConfiguration agentConfiguration) { myMirrorManager = new MirrorManager(agentConfiguration.getCacheDirectory("mercurial")); } public IncludeRuleUpdater getUpdater(@NotNull final VcsRoot vcsRoot, @NotNull final CheckoutRules checkoutRules, @NotNull final String toVersion, @NotNull final File checkoutDirectory, @NotNull final AgentRunningBuild build, boolean cleanCheckoutRequested) throws VcsException { final AgentPluginConfig config = new AgentPluginConfigImpl(build); final BuildProgressLogger logger = build.getBuildLogger(); return new IncludeRuleUpdater() { public void process(@NotNull final IncludeRule includeRule, @NotNull final File workingDir) throws VcsException { try { checkRuleIsValid(includeRule); Settings settings = new Settings(vcsRoot); if (config.isUseLocalMirrors()) { updateLocalMirror(vcsRoot, logger, config); } updateRepository(workingDir, settings, logger, config); updateWorkingDir(settings, workingDir, toVersion, logger); } catch (Exception e) { if (e instanceof VcsException) throw (VcsException) e; else throw new VcsException(e); } } public void dispose() throws VcsException { } }; } @NotNull @Override public String getName() { return Constants.VCS_NAME; } @NotNull @Override public UpdatePolicy getUpdatePolicy() { return this; } private void updateRepository(File workingDir, Settings settings, BuildProgressLogger logger, AgentPluginConfig config) throws VcsException, IOException { String defaultPullUrl = getDefaultPullUrl(settings, config.isUseLocalMirrors()); if (isEmptyDir(workingDir)) {//can do clone only in empty dir logger.message("Start cloning from " + removePrivateData(defaultPullUrl, singleton(settings.getPassword()))); CloneCommand clone = new CloneCommand(settings, workingDir); clone.setRepository(defaultPullUrl); clone.setUpdateWorkingDir(false); clone.setUseUncompressedTransfer(!config.isUseLocalMirrors() && settings.isUncompressedTransfer()); clone.execute(); writeDefaultPath(workingDir, settings.getRepository()); logger.message("Repository successfully cloned"); } else { if (!Settings.isValidRepository(workingDir)) { logger.message("Init repository at " + workingDir.getAbsolutePath()); new Init(settings, workingDir, settings.getRepository()).execute(); } logger.message("Start pulling changes from " + removePrivateData(defaultPullUrl, Collections.singleton(settings.getPassword()))); new PullCommand(settings, workingDir, defaultPullUrl).execute(config.getPullTimeout()); logger.message("Changes successfully pulled"); } } private void updateLocalMirror(VcsRoot root, BuildProgressLogger logger, AgentPluginConfig config) throws VcsException, IOException { Settings settings = new Settings(root); File mirrorDir = myMirrorManager.getMirrorDir(settings.getRepositoryUrl()); logger.message("Update local mirror at " + mirrorDir); if (isEmptyDir(mirrorDir)) { logger.message("Start cloning from " + removePrivateData(settings.getRepository(), singleton(settings.getPassword()))); CloneCommand clone = new CloneCommand(settings, mirrorDir); clone.setUpdateWorkingDir(false); clone.setUseUncompressedTransfer(settings.isUncompressedTransfer()); clone.execute(); writeDefaultPath(mirrorDir, settings.getRepository()); logger.message("Repository successfully cloned"); } else { if (!Settings.isValidRepository(mirrorDir)) { logger.message("Init repository at " + mirrorDir.getAbsolutePath()); new Init(settings, mirrorDir, settings.getRepository()).execute(); } final String defaultPullUrl = getDefaultPullUrl(settings, true); logger.message("Start pulling changes from " + removePrivateData(defaultPullUrl, Collections.singleton(settings.getPassword()))); new PullCommand(settings, mirrorDir, settings.getRepositoryUrl()).execute(config.getPullTimeout()); logger.message("Local mirror changes successfully pulled"); } } private void updateWorkingDir(final Settings settings, File workingDir, @NotNull final String version, final BuildProgressLogger logger) throws VcsException { logger.message("Updating folder " + workingDir.getAbsolutePath() + " to revision " + version); UpdateCommand uc = new UpdateCommand(settings, workingDir); ChangeSet cs = new ChangeSet(version); uc.setToId(cs.getId()); uc.execute(); logger.message("Folder successfully updated"); } private String getDefaultPullUrl(Settings settings, boolean useLocalMirror) throws IOException { if (useLocalMirror) { File mirrorDir = myMirrorManager.getMirrorDir(settings.getRepositoryUrl()); return mirrorDir.getCanonicalPath(); } else { return settings.getRepositoryUrl(); } } private void checkRuleIsValid(IncludeRule includeRule) throws VcsException { if (includeRule.getTo() != null && includeRule.getTo().length() > 0) { if (!".".equals(includeRule.getFrom()) && includeRule.getFrom().length() != 0) { throw new VcsException("Invalid include rule: " + includeRule.toString() + ", Mercurial plugin supports mapping of the form: +:.=>dir only."); } } } private void writeDefaultPath(@NotNull File workingDir, @NotNull String defaultPath) { File hgrc = new File(new File(workingDir, ".hg"), "hgrc"); String content = "[paths]\ndefault = " + defaultPath; FileUtil.writeFile(hgrc, content); } }