view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CatCommand.java @ 780:2afd4572bc92

Run commands requiring an authentication in non-interactive mode
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Wed, 16 Apr 2014 17:38:38 +0200
parents 609987ff0e9c
children f0327df85b9d
line wrap: on
line source
/*
 * Copyright 2000-2014 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 jetbrains.buildServer.buildTriggers.vcs.mercurial.HgFileUtil;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.OS;
import jetbrains.buildServer.log.Loggers;
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 java.util.Collections.singletonList;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.HgFileUtil.deleteDir;

public class CatCommand extends AuthCommand {
  private String myRevId;

  private List<String> myRelativePaths = new ArrayList<String>();
  private boolean myCheckForFailure = true;

  public CatCommand(@NotNull CommandSettings commandSettings,
                    @NotNull String hgPath,
                    @NotNull File workingDir,
                    @NotNull AuthSettings authSettings) {
    super(commandSettings, 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) {
      deleteDir(tempDir, Loggers.VCS);
      throw e;
    }
  }

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

  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()) {
      MercurialCommandLine cli = createCommandLine(tempDir);
      int cmdSize = cli.getCommandLineString().length() + 42;

      do {
        String path = paths.poll();
        cli.addParameter(path);
        cmdSize += path.length() + 3; //quotes + space
      } while (cmdSize < OS.getMaxCommandLineSize() && !paths.isEmpty());

      runCommand(cli, myCommandSettings.setCheckForFailure(checkFailure));
    }
  }

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