view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CatCommand.java @ 399:45f25ca68312 Faradi-7.0.x

Support local mirrors for subrepos
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 02 Mar 2012 14:24:58 +0400
parents 061e5f3a6bad
children 74487e2c6356
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.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import static com.intellij.openapi.util.io.FileUtil.delete;
import static java.util.Collections.singletonList;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CommandExecutionSettingsBuilder.with;

public class CatCommand extends VcsRootCommand {
  private String myRevId;
  private final static int MAX_CMD_LEN = 900;
  private List<String> myRelativePaths = new ArrayList<String>();
  private boolean myCheckForFailure = true;

  public CatCommand(@NotNull String hgPath, @NotNull File workingDir, @NotNull AuthSettings authSettings) {
    super(hgPath, workingDir, authSettings);
  }

  public CatCommand files(@NotNull String relativePath) {
    myRelativePaths = singletonList(relativePath);
    return this;
  }

  public CatCommand files(@NotNull List<String> relativePaths) {
    myRelativePaths = relativePaths;
    return this;
  }

  public CatCommand setRevId(final String revId) {
    myRevId = revId;
    return this;
  }

  public CatCommand atRevision(@NotNull ChangeSet cset) {
    myRevId = cset.getId();
    return this;
  }

  public CatCommand checkForFailure(boolean doCheckForFailure) {
    myCheckForFailure = doCheckForFailure;
    return this;
  }

  public File call() throws VcsException {
    return execute(myRelativePaths, myCheckForFailure);
  }

  public File execute(List<String> relPaths) throws VcsException {
    return execute(relPaths, true);
  }

  public File execute(List<String> relPaths, boolean checkFailure) throws VcsException {
    File tempDir = null;
    try {
      tempDir = createTmpDir();
      createDirectories(relPaths, tempDir);
      catFiles(relPaths, checkFailure, tempDir);
      return tempDir;
    } catch (VcsException e) {
      if (tempDir != null)
        delete(tempDir);
      throw e;
    }
  }

  private File createTmpDir() throws VcsException {
    try {
      return FileUtil.createTempDirectory("mercurial", "catresult");
    } catch (IOException e) {
      throw new VcsException("Unable to create temporary directory");
    }
  }

  private void createDirectories(List<String> relPaths, File tempDir) throws VcsException {
    for (String path: relPaths) {
      File parentFile = new File(tempDir, path).getParentFile();
      if (!parentFile.isDirectory() && !parentFile.mkdirs())
        throw new VcsException("Failed to create directory: " + parentFile.getAbsolutePath());
    }
  }

  private void catFiles(List<String> relPaths, boolean checkFailure, File tempDir) throws VcsException {
    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, with().checkForFailure(checkFailure));
    }
  }

  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;
  }
}