view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CatCommand.java @ 181:0ea2ad14ce97

Add local mirrors for agent checkout. To turn them on set agent property teamcity.hg.use.local.mirrors = true.
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Tue, 01 Mar 2011 17:55:41 +0300
parents 5198b02fc5e9
children 643fa1236f4e d31d7c81b637
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.command;

import com.intellij.execution.configurations.GeneralCommandLine;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class CatCommand extends BaseCommand {
  private String myRevId;
  private final static int MAX_CMD_LEN = 900;

  public CatCommand(@NotNull Settings settings, @NotNull File workingDir) {
    super(settings, workingDir);
  }

  public void setRevId(final String revId) {
    myRevId = revId;
  }

  public File execute(List<String> relPaths) throws VcsException {
    File tempDir;
    try {
      tempDir = FileUtil.createTempDirectory("mercurial", "catresult");
    } catch (IOException e) {
      throw new VcsException("Unable to create temporary directory");
    }
    for (String path: relPaths) {
      final File parentFile = new File(tempDir, path).getParentFile();
      if (!parentFile.isDirectory() && !parentFile.mkdirs()) {
        throw new VcsException("Failed to create directory: " + parentFile.getAbsolutePath());
      }
    }

    Queue<String> paths = new LinkedList<String>(relPaths);
    while (!paths.isEmpty()) {
      GeneralCommandLine cli = createCommandLine(tempDir);
      int cmdSize = cli.getCommandLineString().length();

      do {
        String path = paths.poll();
        cli.addParameter(path);
        cmdSize += path.length();
      } while (cmdSize < MAX_CMD_LEN && !paths.isEmpty());

      runCommand(cli);
    }

    return tempDir;
  }

  private GeneralCommandLine createCommandLine(final File tempDir) {
    GeneralCommandLine cli = createCommandLine();
    cli.addParameter("cat");
    cli.addParameter("-o");
    cli.addParameter(tempDir.getAbsolutePath() + File.separator + "%p");
    if (myRevId != null) {
      cli.addParameter("-r");
      cli.addParameter(myRevId);
    }
    return cli;
  }
}